1 /* $OpenBSD: tty_endrun.c,v 1.1 2009/05/06 18:21:23 stevesk Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Marc Balmer <mbalmer@openbsd.org> 5 * Copyright (c) 2009 Kevin Steves <stevesk@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* 21 * A tty line discipline to decode the EndRun Technologies native 22 * time-of-day message. 23 * http://www.endruntechnologies.com/ 24 */ 25 26 /* 27 * EndRun Format: 28 * 29 * T YYYY DDD HH:MM:SS zZZ m<CR><LF> 30 * 31 * T is the Time Figure of Merit (TFOM) character (described below). 32 * This is the on-time character, transmitted during the first 33 * millisecond of each second. 34 * 35 * YYYY is the year 36 * DDD is the day-of-year 37 * : is the colon character (0x3A) 38 * HH is the hour of the day 39 * MM is the minute of the hour 40 * SS is the second of the minute 41 * z is the sign of the offset to UTC, + implies time is ahead of UTC. 42 * ZZ is the magnitude of the offset to UTC in units of half-hours. 43 * Non-zero only when the Timemode is Local. 44 * m is the Timemode character and is one of: 45 * G = GPS 46 * L = Local 47 * U = UTC 48 * <CR> is the ASCII carriage return character (0x0D) 49 * <LF> is the ASCII line feed character (0x0A) 50 */ 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/proc.h> 55 #include <sys/malloc.h> 56 #include <sys/sensors.h> 57 #include <sys/tty.h> 58 #include <sys/conf.h> 59 #include <sys/time.h> 60 61 #ifdef ENDRUN_DEBUG 62 #define DPRINTFN(n, x) do { if (endrundebug > (n)) printf x; } while (0) 63 int endrundebug = 0; 64 #else 65 #define DPRINTFN(n, x) 66 #endif 67 #define DPRINTF(x) DPRINTFN(0, x) 68 69 int endrunopen(dev_t, struct tty *); 70 int endrunclose(struct tty *, int); 71 int endruninput(int, struct tty *); 72 void endrunattach(int); 73 74 #define ENDRUNLEN 27 /* strlen("6 2009 018 20:41:17 +00 U\r\n") */ 75 #define NUMFLDS 6 76 #ifdef ENDRUN_DEBUG 77 #define TRUSTTIME 30 78 #else 79 #define TRUSTTIME (10 * 60) /* 10 minutes */ 80 #endif 81 82 int endrun_count, endrun_nxid; 83 84 struct endrun { 85 char cbuf[ENDRUNLEN]; /* receive buffer */ 86 struct ksensor time; /* the timedelta sensor */ 87 struct ksensor signal; /* signal status */ 88 struct ksensordev timedev; 89 struct timespec ts; /* current timestamp */ 90 struct timespec lts; /* timestamp of last TFOM */ 91 struct timeout endrun_tout; /* invalidate sensor */ 92 int64_t gap; /* gap between two sentences */ 93 int64_t last; /* last time rcvd */ 94 #define SYNC_SCAN 1 /* scanning for '\n' */ 95 #define SYNC_EOL 2 /* '\n' seen, next char TFOM */ 96 int sync; 97 int pos; /* position in rcv buffer */ 98 int no_pps; /* no PPS although requested */ 99 #ifdef ENDRUN_DEBUG 100 char tfom; 101 #endif 102 }; 103 104 /* EndRun decoding */ 105 void endrun_scan(struct endrun *, struct tty *); 106 void endrun_decode(struct endrun *, struct tty *, char *fld[], int fldcnt); 107 108 /* date and time conversion */ 109 int endrun_atoi(char *s, int len); 110 int endrun_date_to_nano(char *s1, char *s2, int64_t *nano); 111 int endrun_time_to_nano(char *s, int64_t *nano); 112 int endrun_offset_to_nano(char *s, int64_t *nano); 113 114 /* degrade the timedelta sensor */ 115 void endrun_timeout(void *); 116 117 void 118 endrunattach(int dummy) 119 { 120 } 121 122 int 123 endrunopen(dev_t dev, struct tty *tp) 124 { 125 struct proc *p = curproc; 126 struct endrun *np; 127 int error; 128 129 DPRINTF(("endrunopen\n")); 130 if (tp->t_line == ENDRUNDISC) 131 return ENODEV; 132 if ((error = suser(p, 0)) != 0) 133 return error; 134 np = malloc(sizeof(struct endrun), M_DEVBUF, M_WAITOK|M_ZERO); 135 snprintf(np->timedev.xname, sizeof(np->timedev.xname), "endrun%d", 136 endrun_nxid++); 137 endrun_count++; 138 np->time.status = SENSOR_S_UNKNOWN; 139 np->time.type = SENSOR_TIMEDELTA; 140 #ifndef ENDRUN_DEBUG 141 np->time.flags = SENSOR_FINVALID; 142 #endif 143 sensor_attach(&np->timedev, &np->time); 144 145 np->signal.type = SENSOR_PERCENT; 146 np->signal.status = SENSOR_S_UNKNOWN; 147 np->signal.value = 100000LL; 148 strlcpy(np->signal.desc, "Signal", sizeof(np->signal.desc)); 149 sensor_attach(&np->timedev, &np->signal); 150 151 np->sync = SYNC_SCAN; 152 #ifdef ENDRUN_DEBUG 153 np->tfom = '0'; 154 #endif 155 tp->t_sc = (caddr_t)np; 156 157 error = linesw[TTYDISC].l_open(dev, tp); 158 if (error) { 159 free(np, M_DEVBUF); 160 tp->t_sc = NULL; 161 } else { 162 sensordev_install(&np->timedev); 163 timeout_set(&np->endrun_tout, endrun_timeout, np); 164 } 165 166 return error; 167 } 168 169 int 170 endrunclose(struct tty *tp, int flags) 171 { 172 struct endrun *np = (struct endrun *)tp->t_sc; 173 174 DPRINTF(("endrunclose\n")); 175 tp->t_line = TTYDISC; /* switch back to termios */ 176 timeout_del(&np->endrun_tout); 177 sensordev_deinstall(&np->timedev); 178 free(np, M_DEVBUF); 179 tp->t_sc = NULL; 180 endrun_count--; 181 if (endrun_count == 0) 182 endrun_nxid = 0; 183 return linesw[TTYDISC].l_close(tp, flags); 184 } 185 186 /* collect EndRun sentence from tty */ 187 int 188 endruninput(int c, struct tty *tp) 189 { 190 struct endrun *np = (struct endrun *)tp->t_sc; 191 struct timespec ts; 192 int64_t gap; 193 long tmin, tmax; 194 195 if (np->sync == SYNC_EOL) { 196 nanotime(&ts); 197 np->pos = 0; 198 np->sync = SYNC_SCAN; 199 np->cbuf[np->pos++] = c; /* TFOM char */ 200 201 gap = (ts.tv_sec * 1000000000LL + ts.tv_nsec) - 202 (np->lts.tv_sec * 1000000000LL + np->lts.tv_nsec); 203 204 np->lts.tv_sec = ts.tv_sec; 205 np->lts.tv_nsec = ts.tv_nsec; 206 207 if (gap <= np->gap) 208 goto nogap; 209 210 np->ts.tv_sec = ts.tv_sec; 211 np->ts.tv_nsec = ts.tv_nsec; 212 np->gap = gap; 213 214 /* 215 * If a tty timestamp is available, make sure its value is 216 * reasonable by comparing against the timestamp just taken. 217 * If they differ by more than 2 seconds, assume no PPS signal 218 * is present, note the fact, and keep using the timestamp 219 * value. When this happens, the sensor state is set to 220 * CRITICAL later when the EndRun sentence is decoded. 221 */ 222 if (tp->t_flags & (TS_TSTAMPDCDSET | TS_TSTAMPDCDCLR | 223 TS_TSTAMPCTSSET | TS_TSTAMPCTSCLR)) { 224 tmax = lmax(np->ts.tv_sec, tp->t_tv.tv_sec); 225 tmin = lmin(np->ts.tv_sec, tp->t_tv.tv_sec); 226 if (tmax - tmin > 1) 227 np->no_pps = 1; 228 else { 229 np->ts.tv_sec = tp->t_tv.tv_sec; 230 np->ts.tv_nsec = tp->t_tv.tv_usec * 231 1000L; 232 np->no_pps = 0; 233 } 234 } 235 } else if (c == '\n') { 236 if (np->pos == ENDRUNLEN - 1) { 237 /* don't copy '\n' into cbuf */ 238 np->cbuf[np->pos] = '\0'; 239 endrun_scan(np, tp); 240 } 241 np->sync = SYNC_EOL; 242 } else { 243 if (np->pos < ENDRUNLEN - 1) 244 np->cbuf[np->pos++] = c; 245 } 246 247 nogap: 248 /* pass data to termios */ 249 return linesw[TTYDISC].l_rint(c, tp); 250 } 251 252 /* Scan the EndRun sentence just received */ 253 void 254 endrun_scan(struct endrun *np, struct tty *tp) 255 { 256 int fldcnt = 0, n; 257 char *fld[NUMFLDS], *cs; 258 259 DPRINTFN(1, ("%s\n", np->cbuf)); 260 /* split into fields */ 261 fld[fldcnt++] = &np->cbuf[0]; 262 for (cs = NULL, n = 0; n < np->pos && cs == NULL; n++) { 263 switch (np->cbuf[n]) { 264 case '\r': 265 np->cbuf[n] = '\0'; 266 cs = &np->cbuf[n + 1]; 267 break; 268 case ' ': 269 if (fldcnt < NUMFLDS) { 270 np->cbuf[n] = '\0'; 271 fld[fldcnt++] = &np->cbuf[n + 1]; 272 } else { 273 DPRINTF(("endrun: nr of fields in sentence " 274 "exceeds expected: %d\n", NUMFLDS)); 275 return; 276 } 277 break; 278 } 279 } 280 endrun_decode(np, tp, fld, fldcnt); 281 } 282 283 /* Decode the time string */ 284 void 285 endrun_decode(struct endrun *np, struct tty *tp, char *fld[], int fldcnt) 286 { 287 int64_t date_nano, time_nano, offset_nano, endrun_now; 288 char tfom; 289 290 if (fldcnt != NUMFLDS) { 291 DPRINTF(("endrun: field count mismatch, %d\n", fldcnt)); 292 return; 293 } 294 if (endrun_time_to_nano(fld[3], &time_nano) == -1) { 295 DPRINTF(("endrun: illegal time, %s\n", fld[3])); 296 return; 297 } 298 if (endrun_date_to_nano(fld[1], fld[2], &date_nano) == -1) { 299 DPRINTF(("endrun: illegal date, %s %s\n", fld[1], fld[2])); 300 return; 301 } 302 offset_nano = 0; 303 /* only parse offset when timemode is local */ 304 if (fld[5][0] == 'L' && 305 endrun_offset_to_nano(fld[4], &offset_nano) == -1) { 306 DPRINTF(("endrun: illegal offset, %s\n", fld[4])); 307 return; 308 } 309 310 endrun_now = date_nano + time_nano + offset_nano; 311 if (endrun_now <= np->last) { 312 DPRINTF(("endrun: time not monotonically increasing " 313 "last %lld now %lld\n", 314 (long long)np->last, (long long)endrun_now)); 315 return; 316 } 317 np->last = endrun_now; 318 np->gap = 0LL; 319 #ifdef ENDRUN_DEBUG 320 if (np->time.status == SENSOR_S_UNKNOWN) { 321 np->time.status = SENSOR_S_OK; 322 timeout_add_sec(&np->endrun_tout, TRUSTTIME); 323 } 324 #endif 325 326 np->time.value = np->ts.tv_sec * 1000000000LL + 327 np->ts.tv_nsec - endrun_now; 328 np->time.tv.tv_sec = np->ts.tv_sec; 329 np->time.tv.tv_usec = np->ts.tv_nsec / 1000L; 330 if (np->time.status == SENSOR_S_UNKNOWN) { 331 np->time.status = SENSOR_S_OK; 332 np->time.flags &= ~SENSOR_FINVALID; 333 strlcpy(np->time.desc, "EndRun", sizeof(np->time.desc)); 334 } 335 /* 336 * Only update the timeout if the clock reports the time as valid. 337 * 338 * Time Figure Of Merit (TFOM) values: 339 * 340 * 6 - time error is < 100 us 341 * 7 - time error is < 1 ms 342 * 8 - time error is < 10 ms 343 * 9 - time error is > 10 ms, 344 * unsynchronized state if never locked to CDMA 345 */ 346 347 switch (tfom = fld[0][0]) { 348 case '6': 349 case '7': 350 case '8': 351 np->time.status = SENSOR_S_OK; 352 np->signal.status = SENSOR_S_OK; 353 timeout_add_sec(&np->endrun_tout, TRUSTTIME); 354 break; 355 case '9': 356 np->signal.status = SENSOR_S_WARN; 357 break; 358 default: 359 DPRINTF(("endrun: invalid TFOM: '%c'\n", tfom)); 360 np->signal.status = SENSOR_S_CRIT; 361 break; 362 } 363 364 #ifdef ENDRUN_DEBUG 365 if (np->tfom != tfom) { 366 DPRINTF(("endrun: TFOM changed from %c to %c\n", 367 np->tfom, tfom)); 368 np->tfom = tfom; 369 } 370 #endif 371 372 /* 373 * If tty timestamping is requested, but no PPS signal is present, set 374 * the sensor state to CRITICAL. 375 */ 376 if (np->no_pps) 377 np->time.status = SENSOR_S_CRIT; 378 } 379 380 int 381 endrun_atoi(char *s, int len) 382 { 383 int n; 384 char *p; 385 386 /* make sure the input contains only numbers */ 387 for (n = 0, p = s; n < len && *p && *p >= '0' && *p <= '9'; n++, p++) 388 ; 389 if (n != len || *p != '\0') 390 return -1; 391 392 for (n = 0; *s; s++) 393 n = n * 10 + *s - '0'; 394 395 return n; 396 } 397 398 /* 399 * Convert date fields from EndRun to nanoseconds since the epoch. 400 * The year string must be of the form YYYY . 401 * The day of year string must be of the form DDD . 402 * Return 0 on success, -1 if illegal characters are encountered. 403 */ 404 int 405 endrun_date_to_nano(char *y, char *doy, int64_t *nano) 406 { 407 struct clock_ymdhms clock; 408 time_t secs; 409 int n, i; 410 int year_days = 365; 411 int month_days[] = { 412 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 413 }; 414 415 #define FEBRUARY 2 416 417 #define LEAPYEAR(x) \ 418 ((x) % 4 == 0 && \ 419 (x) % 100 != 0) || \ 420 (x) % 400 == 0 421 422 if ((n = endrun_atoi(y, 4)) == -1) 423 return -1; 424 clock.dt_year = n; 425 426 if (LEAPYEAR(n)) { 427 month_days[FEBRUARY]++; 428 year_days++; 429 } 430 431 if ((n = endrun_atoi(doy, 3)) == -1 || n == 0 || n > year_days) 432 return -1; 433 434 /* convert day of year to month, day */ 435 for (i = 1; n > month_days[i]; i++) { 436 n -= month_days[i]; 437 } 438 clock.dt_mon = i; 439 clock.dt_day = n; 440 441 DPRINTFN(1, ("mm/dd %d/%d\n", i, n)); 442 443 clock.dt_hour = clock.dt_min = clock.dt_sec = 0; 444 445 secs = clock_ymdhms_to_secs(&clock); 446 *nano = secs * 1000000000LL; 447 return 0; 448 } 449 450 /* 451 * Convert time field from EndRun to nanoseconds since midnight. 452 * The string must be of the form HH:MM:SS . 453 * Return 0 on success, -1 if illegal characters are encountered. 454 */ 455 int 456 endrun_time_to_nano(char *s, int64_t *nano) 457 { 458 struct clock_ymdhms clock; 459 time_t secs; 460 int n; 461 462 if (s[2] != ':' || s[5] != ':') 463 return -1; 464 465 s[2] = '\0'; 466 s[5] = '\0'; 467 468 if ((n = endrun_atoi(&s[0], 2)) == -1 || n > 23) 469 return -1; 470 clock.dt_hour = n; 471 if ((n = endrun_atoi(&s[3], 2)) == -1 || n > 59) 472 return -1; 473 clock.dt_min = n; 474 if ((n = endrun_atoi(&s[6], 2)) == -1 || n > 60) 475 return -1; 476 clock.dt_sec = n; 477 478 DPRINTFN(1, ("hh:mm:ss %d:%d:%d\n", (int)clock.dt_hour, 479 (int)clock.dt_min, 480 (int)clock.dt_sec)); 481 secs = clock.dt_hour * 3600 482 + clock.dt_min * 60 483 + clock.dt_sec; 484 485 DPRINTFN(1, ("secs %lu\n", (unsigned long)secs)); 486 487 *nano = secs * 1000000000LL; 488 return 0; 489 } 490 491 int 492 endrun_offset_to_nano(char *s, int64_t *nano) 493 { 494 time_t secs; 495 int n; 496 497 if (!(s[0] == '+' || s[0] == '-')) 498 return -1; 499 500 if ((n = endrun_atoi(&s[1], 2)) == -1) 501 return -1; 502 secs = n * 30 * 60; 503 504 *nano = secs * 1000000000LL; 505 if (s[0] == '+') 506 *nano = -*nano; 507 508 DPRINTFN(1, ("offset secs %lu nanosecs %lld\n", 509 (unsigned long)secs, (long long)*nano)); 510 511 return 0; 512 } 513 514 /* 515 * Degrade the sensor state if we received no EndRun string for more than 516 * TRUSTTIME seconds. 517 */ 518 void 519 endrun_timeout(void *xnp) 520 { 521 struct endrun *np = xnp; 522 523 if (np->time.status == SENSOR_S_OK) { 524 np->time.status = SENSOR_S_WARN; 525 /* 526 * further degrade in TRUSTTIME seconds if no new valid EndRun 527 * strings are received. 528 */ 529 timeout_add_sec(&np->endrun_tout, TRUSTTIME); 530 } else 531 np->time.status = SENSOR_S_CRIT; 532 } 533