Skip to content

Commit

Permalink
Lesson06: Update module source (show real datetime, new string parsing)
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg.Khokhlov <[email protected]>
  • Loading branch information
OlegH-ua committed Nov 13, 2018
1 parent bfd9066 commit 4d37222
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 25 deletions.
68 changes: 43 additions & 25 deletions 06-TimeManagement/omod6_kern_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <linux/rtc.h>
#include <linux/delay.h>



//Internal Kernel API (Time Management):

//1. Implement kernel module with API in sysfs or procfs, which is able to:
Expand Down Expand Up @@ -54,7 +56,7 @@ uint32_t TimerInterval;
uint32_t TimerCounter;
uint32_t TimerStartTick;

char LogMessage[PAGE_SIZE];
char LogMessage[256];

uint32_t GetTimeStamp(void)
{
Expand All @@ -74,8 +76,8 @@ static ssize_t ktime_show(struct class *cl,
char *buf)
{
int L;

uint32_t TS, dT, T;
struct rtc_time tm;

T = GetTickCount();
TS = GetTimeStamp();
Expand All @@ -93,7 +95,11 @@ static ssize_t ktime_show(struct class *cl,
sprintf(buf, "ktime (%u ticks, %u HZ):\n", (uint32_t)get_jiffies_64(), HZ);
sprintf(strchr(buf, 0), "%u module call\n", ModCallCount);
sprintf(strchr(buf, 0), "%u sec from last call\n", dT);
sprintf(strchr(buf, 0), "%u sec curr abs time from Epoch (year:%d)\n", TS, 1970+TS/(3600*24*365));
// sprintf(strchr(buf, 0), "%u sec curr abs time from Epoch (year:%d)\n", TS, 1970+TS/(3600*24*365));
rtc_time64_to_tm(TS, &tm);
//https://ru.wikipedia.org/wiki/Time.h
sprintf(strchr(buf, 0), "%u sec curr abs time from Epoch (%d.%02d.%02d, %02d:%02d:%02d)\n", TS,
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
sprintf(strchr(buf, 0), "%u sec prev call abs time from Epoch\n", LastCallTimeSec);
sprintf(strchr(buf, 0), "%u ms - Timer Interval\n", TimerInterval);
sprintf(strchr(buf, 0), "%u times timer called\n", TimerCounter);
Expand All @@ -117,8 +123,7 @@ void KernelTimerFunc(struct timer_list *t)
TimerCounter++;
T = GetTickCount();
dT = T - TimerStartTick;
pr_info("omod6 timer[%u]: %s (delay = %u ms)\n",
TimerCounter, LogMessage, dT);
pr_info("omod6 timer[%u]: %s (delay = %u ms)\n", TimerCounter, LogMessage, dT);
}


Expand All @@ -127,30 +132,43 @@ static ssize_t ktime_store(struct class *cl,
const char *buf, size_t count)
{
int interval = 0;
char *cp;
int n;

n = sscanf(buf, "%d %s", &interval, LogMessage);
pr_info("omod6 store (%d, \'%s\')\n", interval, LogMessage);
if (n >= 1) {
TimerInterval = interval;
// start, stop or reconfig timer
if (interval == 0) {
if (timer_pending(&MyTimer)) {
del_timer(&MyTimer);
pr_info("omod6 Timer disabled\n");
if (sscanf(buf, "%d", &interval) == 1) {
cp = strchr(buf, ' ');
if (cp) {
while (*cp == ' ')
cp++;
for (n = 0; n < sizeof(LogMessage) - 1; n++) {
if (*cp < ' ')
break;
LogMessage[n] = *cp++;
}
} else {
unsigned long expired = get_jiffies_64() +
interval * HZ / 1000;
//if (timer_pending(&MyTimer)) {
// mod_timer_pending(&MyTimer, expired);
//} else {
mod_timer(&MyTimer, expired);
// add_timer(&MyTimer);
//}
TimerStartTick = GetTickCount();
pr_info("omod6 Timer started (interval=%u)\n", interval);
LogMessage[n] = 0;
}
}

pr_info("omod6 store (%d, \'%s\')\n", interval, LogMessage);

TimerInterval = interval;

// start, stop or reconfig timer
if (interval == 0) {
if (timer_pending(&MyTimer)) {
del_timer(&MyTimer);
pr_info("omod6 Timer disabled\n");
}
} else {
unsigned long expired = get_jiffies_64() + interval * HZ / 1000;
//if (timer_pending(&MyTimer)) {
// mod_timer_pending(&MyTimer, expired);
//} else {
mod_timer(&MyTimer, expired);
// add_timer(&MyTimer);
//}
TimerStartTick = GetTickCount();
pr_info("omod6 Timer started (interval=%u)\n", interval);
}
return count;
}
Expand Down
30 changes: 30 additions & 0 deletions 06-TimeManagement/omod6_kern_time2.chklog
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
bash$
bash$ ./chkpatch.sh omod6_kern_time.c
WARNING: Prefer kstrto<type> to single variable sscanf
#138: FILE: /home/olegh/gl-kernel-training-2018/06-TimeManagement/omod6_kern_time.c:138:
+ if (sscanf(buf, "%d", &interval) == 1) {
+ cp = strchr(buf, ' ');
+ if (cp) {
+ while (*cp == ' ')
+ cp++;
+ for (n = 0; n < sizeof(LogMessage) - 1; n++) {
+ if (*cp < ' ')
+ break;
+ LogMessage[n] = *cp++;
+ }
+ LogMessage[n] = 0;
+ }
+ }

total: 0 errors, 1 warnings, 222 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

/home/olegh/gl-kernel-training-2018/06-TimeManagement/omod6_kern_time.c has style problems, please review.

NOTE: Ignored message types: LONG_LINE LONG_LINE_COMMENT LONG_LINE_STRING

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
bash$
35 changes: 35 additions & 0 deletions 06-TimeManagement/omod6_kern_time2.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
bash$
bash$
bash$ sudo insmod omod6_kern_time.ko
bash$ cat /sys/class/omod6/ktime
ktime (50320923 ticks, 250 HZ):
1 module call
4 sec from last call
1542138138 sec curr abs time from Epoch (2018.11.13, 19:42:18)
1542138134 sec prev call abs time from Epoch
0 ms - Timer Interval
0 times timer called
bash$
bash$
bash$ echo "2000 Hello from module timer!" > /sys/class/omod6/ktime
bash$
bash$ cat /sys/class/omod6/ktime
ktime (50325075 ticks, 250 HZ):
2 module call
17 sec from last call
1542138155 sec curr abs time from Epoch (2018.11.13, 19:42:35)
1542138138 sec prev call abs time from Epoch
2000 ms - Timer Interval
1 times timer called
bash$
bash$
bash$ dmesg
[201578.642960] omod6 Kernel time function test module started.
[201593.096793] omod6 store (2000, 'Hello from module timer!')
[201593.096800] omod6 Timer started (interval=2000)
[201595.113490] omod6 timer[1]: Hello from module timer! (delay = 2020 ms)
bash$
bash$
bash$ sudo rmmod omod6_kern_time
bash$
bash$

0 comments on commit 4d37222

Please sign in to comment.