diff --git a/06-TimeManagement/omod6_kern_time.c b/06-TimeManagement/omod6_kern_time.c index 345c952..268610b 100644 --- a/06-TimeManagement/omod6_kern_time.c +++ b/06-TimeManagement/omod6_kern_time.c @@ -23,6 +23,8 @@ #include #include + + //Internal Kernel API (Time Management): //1. Implement kernel module with API in sysfs or procfs, which is able to: @@ -54,7 +56,7 @@ uint32_t TimerInterval; uint32_t TimerCounter; uint32_t TimerStartTick; -char LogMessage[PAGE_SIZE]; +char LogMessage[256]; uint32_t GetTimeStamp(void) { @@ -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(); @@ -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); @@ -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); } @@ -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; } diff --git a/06-TimeManagement/omod6_kern_time2.chklog b/06-TimeManagement/omod6_kern_time2.chklog new file mode 100644 index 0000000..8d7da4a --- /dev/null +++ b/06-TimeManagement/omod6_kern_time2.chklog @@ -0,0 +1,30 @@ +bash$ +bash$ ./chkpatch.sh omod6_kern_time.c +WARNING: Prefer kstrto 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$ diff --git a/06-TimeManagement/omod6_kern_time2.log b/06-TimeManagement/omod6_kern_time2.log new file mode 100644 index 0000000..cb0287a --- /dev/null +++ b/06-TimeManagement/omod6_kern_time2.log @@ -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$