00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <assert.h>
00043 #include <ctype.h>
00044 #include <errno.h>
00045 #include <stdio.h>
00046 #include <time.h>
00047
00048 #include "os_time.h"
00049 #include "olsr_logging.h"
00050 #include "olsr.h"
00051 #include "olsr_clock.h"
00052
00053
00054 static uint32_t now_times;
00055 static struct timeval first_tv;
00056 static struct timeval last_tv;
00057
00058 static int olsr_get_timezone(void);
00059
00063 void
00064 olsr_clock_init(void) {
00065
00066 if (os_gettimeofday(&first_tv, NULL)) {
00067 OLSR_ERROR(LOG_TIMER, "OS clock is not working, have to shut down OLSR (%s)\n", strerror(errno));
00068 olsr_exit(1);
00069 }
00070 last_tv = first_tv;
00071 olsr_clock_update();
00072 }
00073
00077 void
00078 olsr_clock_update(void)
00079 {
00080 struct timeval tv;
00081 uint32_t t;
00082
00083 if (os_gettimeofday(&tv, NULL) != 0) {
00084 OLSR_ERROR(LOG_SCHEDULER, "OS clock is not working, have to shut down OLSR (%s)\n", strerror(errno));
00085 olsr_exit(1);
00086 }
00087
00088
00089 if (tv.tv_sec < last_tv.tv_sec || (tv.tv_sec == last_tv.tv_sec && tv.tv_usec < last_tv.tv_usec)
00090 || tv.tv_sec - last_tv.tv_sec > 60) {
00091 OLSR_WARN(LOG_SCHEDULER, "Time jump (%d.%06d to %d.%06d)\n",
00092 (int32_t) (last_tv.tv_sec), (int32_t) (last_tv.tv_usec), (int32_t) (tv.tv_sec), (int32_t) (tv.tv_usec));
00093
00094 t = (last_tv.tv_sec - first_tv.tv_sec) * 1000 + (last_tv.tv_usec - first_tv.tv_usec) / 1000;
00095 t++;
00096
00097 first_tv = tv;
00098 first_tv.tv_sec -= (t / 1000);
00099 first_tv.tv_usec -= ((t % 1000) * 1000);
00100
00101 if (first_tv.tv_usec < 0) {
00102 first_tv.tv_sec--;
00103 first_tv.tv_usec += 1000000;
00104 }
00105 last_tv = tv;
00106 now_times = t;
00107 }
00108 last_tv = tv;
00109 now_times = (tv.tv_sec - first_tv.tv_sec) * 1000 + (tv.tv_usec - first_tv.tv_usec) / 1000;
00110 }
00111
00116 uint32_t
00117 olsr_clock_getNow(void) {
00118 return now_times;
00119 }
00120
00127 int32_t
00128 olsr_clock_getRelative(uint32_t absolute)
00129 {
00130 uint32_t diff;
00131 if (absolute > now_times) {
00132 diff = absolute - now_times;
00133
00134
00135 if (diff > (1u << 31)) {
00136 return -(int32_t) (0xffffffff - diff);
00137 }
00138 return (int32_t) (diff);
00139 }
00140
00141 diff = now_times - absolute;
00142
00143 if (diff > (1u << 31)) {
00144 return (int32_t) (0xffffffff - diff);
00145 }
00146 return -(int32_t) (diff);
00147 }
00148
00154 bool
00155 olsr_clock_isPast(uint32_t absolute)
00156 {
00157 if (absolute > now_times) {
00158 return absolute - now_times > (1u << 31);
00159 }
00160
00161 return now_times - absolute <= (1u << 31);
00162 }
00163
00178 uint8_t
00179 olsr_clock_encode_olsrv1(const uint32_t interval)
00180 {
00181 uint8_t a = 0, b = 0;
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 unsigned unscaled_interval = interval;
00196 while (unscaled_interval >= 62) {
00197 unscaled_interval >>= 1;
00198 b++;
00199 }
00200
00201 if (0 < b) {
00202 if (15 < --b) {
00203 a = 15;
00204 b = 15;
00205 } else {
00206 a = (interval >> (b + 2)) - 15;
00207 }
00208 }
00209
00210 return (a << 4) + b;
00211 }
00212
00248 uint32_t
00249 olsr_clock_decode_olsrv1(const uint8_t me)
00250 {
00251 const uint8_t a = me >> 4;
00252 const uint8_t b = me & 0x0F;
00253
00254 if (b >= 8) {
00255 return ((16 + a) << (b - 8)) * 1000;
00256 }
00257 assert(me == olsr_clock_encode_olsrv1(((16 + a) * 1000) >> (8 - b)));
00258 return ((16 + a) * 1000) >> (8 - b);
00259 }
00260
00265 char *
00266 olsr_clock_to_string(struct millitxt_buf *buffer, uint32_t t) {
00267 sprintf(buffer->buf, "%u.%03u", t/1000, t%1000);
00268 return buffer->buf;
00269 }
00270
00275 uint32_t olsr_clock_parse_string(char *txt) {
00276 uint32_t t = 0;
00277 int fractionDigits = 0;
00278 bool frac = false;
00279
00280 while (fractionDigits < 3 && *txt) {
00281 if (*txt == '.' && !frac) {
00282 frac = true;
00283 txt++;
00284 }
00285
00286 if (!isdigit(*txt)) {
00287 break;
00288 }
00289
00290 t = t * 10 + (*txt++ - '0');
00291 if (frac) {
00292 fractionDigits++;
00293 }
00294 }
00295
00296 while (fractionDigits++ < 3) {
00297 t *= 10;
00298 }
00299 return t;
00300 }
00301
00302
00310 const char *
00311 olsr_clock_getWallclockString(struct timeval_buf *buf)
00312 {
00313 struct timeval now;
00314 int sec, usec;
00315
00316 os_gettimeofday(&now, NULL);
00317
00318 sec = (int)now.tv_sec + olsr_get_timezone();
00319 usec = (int)now.tv_usec;
00320
00321 snprintf(buf->buf, sizeof(buf), "%02d:%02d:%02d.%06d",
00322 (sec % 86400) / 3600, (sec % 3600) / 60, sec % 60, usec);
00323
00324 return buf->buf;
00325 }
00326
00334 const char *
00335 olsr_clock_toClockString(struct timeval_buf *buf, uint32_t clk)
00336 {
00337 unsigned int msec = clk % 1000;
00338 unsigned int sec = clk / 1000;
00339
00340 snprintf(buf->buf, sizeof(buf),
00341 "%02u:%02u:%02u.%03u", sec / 3600, (sec % 3600) / 60, (sec % 60), (msec % MSEC_PER_SEC));
00342
00343 return buf->buf;
00344 }
00345
00352 static int
00353 olsr_get_timezone(void)
00354 {
00355 #define OLSR_TIMEZONE_UNINITIALIZED -1
00356 static int time_diff = OLSR_TIMEZONE_UNINITIALIZED;
00357 if (time_diff == OLSR_TIMEZONE_UNINITIALIZED) {
00358 int dir;
00359 const time_t t = time(NULL);
00360 const struct tm gmt = *gmtime(&t);
00361 const struct tm *loc = localtime(&t);
00362
00363 time_diff = (loc->tm_hour - gmt.tm_hour) * 60 * 60 + (loc->tm_min - gmt.tm_min) * 60;
00364
00365
00366
00367
00368
00369
00370 dir = loc->tm_year - gmt.tm_year;
00371 if (!dir) {
00372 dir = loc->tm_yday - gmt.tm_yday;
00373 }
00374
00375 time_diff += dir * 24 * 60 * 60;
00376 }
00377 return time_diff;
00378 }
00379
00380
00381
00382
00383
00384
00385