minmea.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
  3. * This program is free software. It comes without any warranty, to the extent
  4. * permitted by applicable law. You can redistribute it and/or modify it under
  5. * the terms of the Do What The Fuck You Want To Public License, Version 2, as
  6. * published by Sam Hocevar. See the COPYING file for more details.
  7. */
  8. #include "minmea.h"
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <stdarg.h>
  13. #define boolstr(s) ((s) ? "true" : "false")
  14. static int hex2int(char c)
  15. {
  16. if (c >= '0' && c <= '9')
  17. return c - '0';
  18. if (c >= 'A' && c <= 'F')
  19. return c - 'A' + 10;
  20. if (c >= 'a' && c <= 'f')
  21. return c - 'a' + 10;
  22. return -1;
  23. }
  24. bool minmea_check(const char *sentence)
  25. {
  26. uint8_t checksum = 0x00;
  27. // Sequence length is limited.
  28. if (strlen(sentence) > MINMEA_MAX_LENGTH + 3)
  29. return false;
  30. // A valid sentence starts with "$".
  31. if (*sentence++ != '$')
  32. return false;
  33. // The optional checksum is an XOR of all bytes between "$" and "*".
  34. while (*sentence && *sentence != '*' && isprint((unsigned char) *sentence))
  35. checksum ^= *sentence++;
  36. if (*sentence == '*') {
  37. // Extract checksum.
  38. sentence++;
  39. int upper = hex2int(*sentence++);
  40. if (upper == -1)
  41. return false;
  42. int lower = hex2int(*sentence++);
  43. if (lower == -1)
  44. return false;
  45. int expected = upper << 4 | lower;
  46. // Check for checksum mismatch.
  47. if (checksum != expected)
  48. return false;
  49. }
  50. // The only stuff allowed at this point is a newline.
  51. if (*sentence && strcmp(sentence, "\n") && strcmp(sentence, "\r\n"))
  52. return false;
  53. return true;
  54. }
  55. static inline bool minmea_isfield(char c) {
  56. return isprint((unsigned char) c) && c != ',' && c != '*';
  57. }
  58. bool minmea_scan(const char *sentence, const char *format, ...)
  59. {
  60. bool result = false;
  61. bool optional = false;
  62. va_list ap;
  63. va_start(ap, format);
  64. const char *field = sentence;
  65. #define next_field() \
  66. do { \
  67. /* Progress to the next field. */ \
  68. while (minmea_isfield(*sentence)) \
  69. sentence++; \
  70. /* Make sure there is a field there. */ \
  71. if (*sentence == ',') { \
  72. sentence++; \
  73. field = sentence; \
  74. } else { \
  75. field = NULL; \
  76. } \
  77. } while (0)
  78. while (*format) {
  79. char type = *format++;
  80. if (type == ';') {
  81. // All further fields are optional.
  82. optional = true;
  83. continue;
  84. }
  85. if (!field && !optional) {
  86. // Field requested but we ran out if input. Bail out.
  87. goto parse_error;
  88. }
  89. switch (type) {
  90. case 'c': { // Single character field (char).
  91. char value = '\0';
  92. if (field && minmea_isfield(*field))
  93. value = *field;
  94. *va_arg(ap, char *) = value;
  95. } break;
  96. case 'd': { // Single character direction field (int).
  97. int value = 0;
  98. if (field && minmea_isfield(*field)) {
  99. switch (*field) {
  100. case 'N':
  101. case 'E':
  102. value = 1;
  103. break;
  104. case 'S':
  105. case 'W':
  106. value = -1;
  107. break;
  108. default:
  109. goto parse_error;
  110. }
  111. }
  112. *va_arg(ap, int *) = value;
  113. } break;
  114. case 'f': { // Fractional value with scale (struct minmea_float).
  115. int sign = 0;
  116. int_least32_t value = -1;
  117. int_least32_t scale = 0;
  118. if (field) {
  119. while (minmea_isfield(*field)) {
  120. if (*field == '+' && !sign && value == -1) {
  121. sign = 1;
  122. } else if (*field == '-' && !sign && value == -1) {
  123. sign = -1;
  124. } else if (isdigit((unsigned char) *field)) {
  125. int digit = *field - '0';
  126. if (value == -1)
  127. value = 0;
  128. if (value > (INT_LEAST32_MAX-digit) / 10) {
  129. /* we ran out of bits, what do we do? */
  130. if (scale) {
  131. /* truncate extra precision */
  132. break;
  133. } else {
  134. /* integer overflow. bail out. */
  135. goto parse_error;
  136. }
  137. }
  138. value = (10 * value) + digit;
  139. if (scale)
  140. scale *= 10;
  141. } else if (*field == '.' && scale == 0) {
  142. scale = 1;
  143. } else {
  144. goto parse_error;
  145. }
  146. field++;
  147. }
  148. }
  149. if ((sign || scale) && value == -1)
  150. goto parse_error;
  151. if (value == -1) {
  152. /* No digits were scanned. */
  153. value = 0;
  154. scale = 0;
  155. } else if (scale == 0) {
  156. /* No decimal point. */
  157. scale = 1;
  158. }
  159. if (sign)
  160. value *= sign;
  161. *va_arg(ap, struct minmea_float *) = (struct minmea_float) {value, scale};
  162. } break;
  163. case 'i': { // Integer value, default 0 (int).
  164. int value = 0;
  165. if (field) {
  166. char *endptr;
  167. value = strtol(field, &endptr, 10);
  168. if (minmea_isfield(*endptr))
  169. goto parse_error;
  170. }
  171. *va_arg(ap, int *) = value;
  172. } break;
  173. case 's': { // String value (char *).
  174. char *buf = va_arg(ap, char *);
  175. if (field) {
  176. while (minmea_isfield(*field))
  177. *buf++ = *field++;
  178. }
  179. *buf = '\0';
  180. } break;
  181. case 't': { // NMEA talker+sentence identifier (char *).
  182. // This field is always mandatory.
  183. if (!field)
  184. goto parse_error;
  185. if (field[0] != '$')
  186. goto parse_error;
  187. for (int f=0; f<5; f++)
  188. if (!minmea_isfield(field[1+f]))
  189. goto parse_error;
  190. char *buf = va_arg(ap, char *);
  191. memcpy(buf, field+1, 5);
  192. buf[5] = '\0';
  193. } break;
  194. case 'D': { // Date (int, int, int), -1 if empty.
  195. struct minmea_date *date = va_arg(ap, struct minmea_date *);
  196. int d = -1, m = -1, y = -1;
  197. if (field && minmea_isfield(*field)) {
  198. // Always six digits.
  199. for (int f=0; f<6; f++)
  200. if (!isdigit((unsigned char) field[f]))
  201. goto parse_error;
  202. d = strtol((char[]) {field[0], field[1], '\0'}, NULL, 10);
  203. m = strtol((char[]) {field[2], field[3], '\0'}, NULL, 10);
  204. y = strtol((char[]) {field[4], field[5], '\0'}, NULL, 10);
  205. }
  206. date->day = d;
  207. date->month = m;
  208. date->year = y;
  209. } break;
  210. case 'T': { // Time (int, int, int, int), -1 if empty.
  211. struct minmea_time *time = va_arg(ap, struct minmea_time *);
  212. int h = -1, i = -1, s = -1, u = -1;
  213. if (field && minmea_isfield(*field)) {
  214. // Minimum required: integer time.
  215. for (int f=0; f<6; f++)
  216. if (!isdigit((unsigned char) field[f]))
  217. goto parse_error;
  218. h = strtol((char[]) {field[0], field[1], '\0'}, NULL, 10);
  219. i = strtol((char[]) {field[2], field[3], '\0'}, NULL, 10);
  220. s = strtol((char[]) {field[4], field[5], '\0'}, NULL, 10);
  221. field += 6;
  222. // Extra: fractional time. Saved as microseconds.
  223. if (*field++ == '.') {
  224. int value = 0;
  225. int scale = 1000000;
  226. while (isdigit((unsigned char) *field) && scale > 1) {
  227. value = (value * 10) + (*field++ - '0');
  228. scale /= 10;
  229. }
  230. u = value * scale;
  231. } else {
  232. u = 0;
  233. }
  234. }
  235. time->hours = h;
  236. time->minutes = i;
  237. time->seconds = s;
  238. time->microseconds = u;
  239. } break;
  240. case '_': { // Ignore the field.
  241. } break;
  242. default: { // Unknown.
  243. goto parse_error;
  244. } break;
  245. }
  246. next_field();
  247. }
  248. result = true;
  249. parse_error:
  250. va_end(ap);
  251. return result;
  252. }
  253. bool minmea_talker_id(char talker[3], const char *sentence)
  254. {
  255. char type[6];
  256. if (!minmea_scan(sentence, "t", type))
  257. return false;
  258. talker[0] = type[0];
  259. talker[1] = type[1];
  260. talker[2] = '\0';
  261. return true;
  262. }
  263. enum minmea_sentence_id minmea_sentence_id(const char *sentence)
  264. {
  265. if (!minmea_check(sentence))
  266. return MINMEA_INVALID;
  267. char type[6];
  268. if (!minmea_scan(sentence, "t", type))
  269. return MINMEA_INVALID;
  270. if (!strcmp(type+2, "RMC"))
  271. return MINMEA_SENTENCE_RMC;
  272. if (!strcmp(type+2, "GGA"))
  273. return MINMEA_SENTENCE_GGA;
  274. if (!strcmp(type+2, "GSA"))
  275. return MINMEA_SENTENCE_GSA;
  276. if (!strcmp(type+2, "GLL"))
  277. return MINMEA_SENTENCE_GLL;
  278. if (!strcmp(type+2, "GST"))
  279. return MINMEA_SENTENCE_GST;
  280. if (!strcmp(type+2, "GSV"))
  281. return MINMEA_SENTENCE_GSV;
  282. return MINMEA_UNKNOWN;
  283. }
  284. bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence)
  285. {
  286. // $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
  287. char type[6];
  288. char validity;
  289. int latitude_direction;
  290. int longitude_direction;
  291. int variation_direction;
  292. if (!minmea_scan(sentence, "tTcfdfdffDfd",
  293. type,
  294. &frame->time,
  295. &validity,
  296. &frame->latitude, &latitude_direction,
  297. &frame->longitude, &longitude_direction,
  298. &frame->speed,
  299. &frame->course,
  300. &frame->date,
  301. &frame->variation, &variation_direction))
  302. return false;
  303. if (strcmp(type+2, "RMC"))
  304. return false;
  305. frame->valid = (validity == 'A');
  306. frame->latitude.value *= latitude_direction;
  307. frame->longitude.value *= longitude_direction;
  308. frame->variation.value *= variation_direction;
  309. return true;
  310. }
  311. bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence)
  312. {
  313. // $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
  314. char type[6];
  315. int latitude_direction;
  316. int longitude_direction;
  317. if (!minmea_scan(sentence, "tTfdfdiiffcfci_",
  318. type,
  319. &frame->time,
  320. &frame->latitude, &latitude_direction,
  321. &frame->longitude, &longitude_direction,
  322. &frame->fix_quality,
  323. &frame->satellites_tracked,
  324. &frame->hdop,
  325. &frame->altitude, &frame->altitude_units,
  326. &frame->height, &frame->height_units,
  327. &frame->dgps_age))
  328. return false;
  329. if (strcmp(type+2, "GGA"))
  330. return false;
  331. frame->latitude.value *= latitude_direction;
  332. frame->longitude.value *= longitude_direction;
  333. return true;
  334. }
  335. bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence)
  336. {
  337. // $GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39
  338. char type[6];
  339. if (!minmea_scan(sentence, "tciiiiiiiiiiiiifff",
  340. type,
  341. &frame->mode,
  342. &frame->fix_type,
  343. &frame->sats[0],
  344. &frame->sats[1],
  345. &frame->sats[2],
  346. &frame->sats[3],
  347. &frame->sats[4],
  348. &frame->sats[5],
  349. &frame->sats[6],
  350. &frame->sats[7],
  351. &frame->sats[8],
  352. &frame->sats[9],
  353. &frame->sats[10],
  354. &frame->sats[11],
  355. &frame->pdop,
  356. &frame->hdop,
  357. &frame->vdop))
  358. return false;
  359. if (strcmp(type+2, "GSA"))
  360. return false;
  361. return true;
  362. }
  363. bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence)
  364. {
  365. // $GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41$;
  366. char type[6];
  367. int latitude_direction;
  368. int longitude_direction;
  369. if (!minmea_scan(sentence, "tfdfdTcc",
  370. type,
  371. &frame->latitude, &latitude_direction,
  372. &frame->longitude, &longitude_direction,
  373. &frame->time,
  374. &frame->status,
  375. &frame->mode))
  376. return false;
  377. if (strcmp(type+2, "GLL"))
  378. return false;
  379. frame->latitude.value *= latitude_direction;
  380. frame->longitude.value *= longitude_direction;
  381. return true;
  382. }
  383. bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence)
  384. {
  385. // $GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58
  386. char type[6];
  387. if (!minmea_scan(sentence, "tTfffffff",
  388. type,
  389. &frame->time,
  390. &frame->rms_deviation,
  391. &frame->semi_major_deviation,
  392. &frame->semi_minor_deviation,
  393. &frame->semi_major_orientation,
  394. &frame->latitude_error_deviation,
  395. &frame->longitude_error_deviation,
  396. &frame->altitude_error_deviation))
  397. return false;
  398. if (strcmp(type+2, "GST"))
  399. return false;
  400. return true;
  401. }
  402. bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence)
  403. {
  404. // $GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74
  405. char type[6];
  406. if (!minmea_scan(sentence, "tiiiiiiiiiiiiiiiiiii",
  407. type,
  408. &frame->total_msgs,
  409. &frame->msg_nr,
  410. &frame->total_sats,
  411. &frame->sats[0].nr,
  412. &frame->sats[0].elevation,
  413. &frame->sats[0].azimuth,
  414. &frame->sats[0].snr,
  415. &frame->sats[1].nr,
  416. &frame->sats[1].elevation,
  417. &frame->sats[1].azimuth,
  418. &frame->sats[1].snr,
  419. &frame->sats[2].nr,
  420. &frame->sats[2].elevation,
  421. &frame->sats[2].azimuth,
  422. &frame->sats[2].snr,
  423. &frame->sats[3].nr,
  424. &frame->sats[3].elevation,
  425. &frame->sats[3].azimuth,
  426. &frame->sats[3].snr
  427. )) {
  428. return false;
  429. }
  430. if (strcmp(type+2, "GSV"))
  431. return false;
  432. return true;
  433. }
  434. int minmea_gettimeofday(struct timeval *tv, const struct minmea_date *date, const struct minmea_time *time)
  435. {
  436. if (date->year == -1 || time->hours == -1)
  437. return -1;
  438. struct tm tm;
  439. memset(&tm, 0, sizeof(tm));
  440. tm.tm_year = 2000 + date->year - 1900;
  441. tm.tm_mon = date->month - 1;
  442. tm.tm_mday = date->day;
  443. tm.tm_hour = time->hours;
  444. tm.tm_min = time->minutes;
  445. tm.tm_sec = time->seconds;
  446. time_t timestamp = timegm(&tm);
  447. if (timestamp != -1) {
  448. tv->tv_sec = timestamp;
  449. tv->tv_usec = time->microseconds;
  450. return 0;
  451. } else {
  452. return -1;
  453. }
  454. }
  455. /* vim: set ts=4 sw=4 et: */