minmea.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. if (value == -1)
  126. value = 0;
  127. value = (10 * value) + (*field - '0');
  128. if (scale)
  129. scale *= 10;
  130. } else if (*field == '.' && scale == 0) {
  131. scale = 1;
  132. } else {
  133. goto parse_error;
  134. }
  135. field++;
  136. }
  137. }
  138. if ((sign || scale) && value == -1)
  139. goto parse_error;
  140. if (value == -1) {
  141. /* No digits were scanned. */
  142. value = 0;
  143. scale = 0;
  144. } else if (scale == 0) {
  145. /* No decimal point. */
  146. scale = 1;
  147. }
  148. if (sign)
  149. value *= sign;
  150. *va_arg(ap, struct minmea_float *) = (struct minmea_float) {value, scale};
  151. } break;
  152. case 'i': { // Integer value, default 0 (int).
  153. int value = 0;
  154. if (field) {
  155. char *endptr;
  156. value = strtol(field, &endptr, 10);
  157. if (minmea_isfield(*endptr))
  158. goto parse_error;
  159. }
  160. *va_arg(ap, int *) = value;
  161. } break;
  162. case 's': { // String value (char *).
  163. char *buf = va_arg(ap, char *);
  164. if (field) {
  165. while (minmea_isfield(*field))
  166. *buf++ = *field++;
  167. }
  168. *buf = '\0';
  169. } break;
  170. case 't': { // NMEA talker+sentence identifier (char *).
  171. // This field is always mandatory.
  172. if (!field)
  173. goto parse_error;
  174. if (field[0] != '$')
  175. goto parse_error;
  176. for (int i=0; i<5; i++)
  177. if (!minmea_isfield(field[1+i]))
  178. goto parse_error;
  179. char *buf = va_arg(ap, char *);
  180. memcpy(buf, field+1, 5);
  181. buf[5] = '\0';
  182. } break;
  183. case 'D': { // Date (int, int, int), -1 if empty.
  184. struct minmea_date *date = va_arg(ap, struct minmea_date *);
  185. int d = -1, m = -1, y = -1;
  186. if (field && minmea_isfield(*field)) {
  187. // Always six digits.
  188. for (int i=0; i<6; i++)
  189. if (!isdigit((unsigned char) field[i]))
  190. goto parse_error;
  191. d = strtol((char[]) {field[0], field[1], '\0'}, NULL, 10);
  192. m = strtol((char[]) {field[2], field[3], '\0'}, NULL, 10);
  193. y = strtol((char[]) {field[4], field[5], '\0'}, NULL, 10);
  194. }
  195. date->day = d;
  196. date->month = m;
  197. date->year = y;
  198. } break;
  199. case 'T': { // Time (int, int, int, int), -1 if empty.
  200. struct minmea_time *time = va_arg(ap, struct minmea_time *);
  201. int h = -1, i = -1, s = -1, u = -1;
  202. if (field && minmea_isfield(*field)) {
  203. // Minimum required: integer time.
  204. for (int i=0; i<6; i++)
  205. if (!isdigit((unsigned char) field[i]))
  206. goto parse_error;
  207. h = strtol((char[]) {field[0], field[1], '\0'}, NULL, 10);
  208. i = strtol((char[]) {field[2], field[3], '\0'}, NULL, 10);
  209. s = strtol((char[]) {field[4], field[5], '\0'}, NULL, 10);
  210. field += 6;
  211. // Extra: fractional time. Saved as microseconds.
  212. if (*field++ == '.') {
  213. int value = 0;
  214. int scale = 1000000;
  215. while (isdigit((unsigned char) *field) && scale > 1) {
  216. value = (value * 10) + (*field++ - '0');
  217. scale /= 10;
  218. }
  219. u = value * scale;
  220. } else {
  221. u = 0;
  222. }
  223. }
  224. time->hours = h;
  225. time->minutes = i;
  226. time->seconds = s;
  227. time->microseconds = u;
  228. } break;
  229. case '_': { // Ignore the field.
  230. } break;
  231. default: { // Unknown.
  232. goto parse_error;
  233. } break;
  234. }
  235. next_field();
  236. }
  237. result = true;
  238. parse_error:
  239. va_end(ap);
  240. return result;
  241. }
  242. bool minmea_talker_id(char talker[3], const char *sentence)
  243. {
  244. char type[6];
  245. if (!minmea_scan(sentence, "t", type))
  246. return false;
  247. talker[0] = type[0];
  248. talker[1] = type[1];
  249. talker[2] = '\0';
  250. return true;
  251. }
  252. enum minmea_sentence_id minmea_sentence_id(const char *sentence)
  253. {
  254. if (!minmea_check(sentence))
  255. return MINMEA_INVALID;
  256. char type[6];
  257. if (!minmea_scan(sentence, "t", type))
  258. return MINMEA_INVALID;
  259. if (!strcmp(type+2, "RMC"))
  260. return MINMEA_SENTENCE_RMC;
  261. if (!strcmp(type+2, "GGA"))
  262. return MINMEA_SENTENCE_GGA;
  263. if (!strcmp(type+2, "GSA"))
  264. return MINMEA_SENTENCE_GSA;
  265. if (!strcmp(type+2, "GST"))
  266. return MINMEA_SENTENCE_GST;
  267. if (!strcmp(type+2, "GSV"))
  268. return MINMEA_SENTENCE_GSV;
  269. return MINMEA_UNKNOWN;
  270. }
  271. bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence)
  272. {
  273. // $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
  274. char type[6];
  275. char validity;
  276. int latitude_direction;
  277. int longitude_direction;
  278. int variation_direction;
  279. if (!minmea_scan(sentence, "tTcfdfdffDfd",
  280. type,
  281. &frame->time,
  282. &validity,
  283. &frame->latitude, &latitude_direction,
  284. &frame->longitude, &longitude_direction,
  285. &frame->speed,
  286. &frame->course,
  287. &frame->date,
  288. &frame->variation, &variation_direction))
  289. return false;
  290. if (strcmp(type+2, "RMC"))
  291. return false;
  292. frame->valid = (validity == 'A');
  293. frame->latitude.value *= latitude_direction;
  294. frame->longitude.value *= longitude_direction;
  295. frame->variation.value *= variation_direction;
  296. return true;
  297. }
  298. bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence)
  299. {
  300. // $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
  301. char type[6];
  302. int latitude_direction;
  303. int longitude_direction;
  304. if (!minmea_scan(sentence, "tTfdfdiiffcfci_",
  305. type,
  306. &frame->time,
  307. &frame->latitude, &latitude_direction,
  308. &frame->longitude, &longitude_direction,
  309. &frame->fix_quality,
  310. &frame->satellites_tracked,
  311. &frame->hdop,
  312. &frame->altitude, &frame->altitude_units,
  313. &frame->height, &frame->height_units,
  314. &frame->dgps_age))
  315. return false;
  316. if (strcmp(type+2, "GGA"))
  317. return false;
  318. frame->latitude.value *= latitude_direction;
  319. frame->longitude.value *= longitude_direction;
  320. return true;
  321. }
  322. bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence)
  323. {
  324. // $GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39
  325. char type[6];
  326. if (!minmea_scan(sentence, "tciiiiiiiiiiiiifff",
  327. type,
  328. &frame->mode,
  329. &frame->fix_type,
  330. &frame->sats[0],
  331. &frame->sats[1],
  332. &frame->sats[2],
  333. &frame->sats[3],
  334. &frame->sats[4],
  335. &frame->sats[5],
  336. &frame->sats[6],
  337. &frame->sats[7],
  338. &frame->sats[8],
  339. &frame->sats[9],
  340. &frame->sats[10],
  341. &frame->sats[11],
  342. &frame->pdop,
  343. &frame->hdop,
  344. &frame->vdop))
  345. return false;
  346. if (strcmp(type+2, "GSA"))
  347. return false;
  348. return true;
  349. }
  350. bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence)
  351. {
  352. // $GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58
  353. char type[6];
  354. if (!minmea_scan(sentence, "tTfffffff",
  355. type,
  356. &frame->time,
  357. &frame->rms_deviation,
  358. &frame->semi_major_deviation,
  359. &frame->semi_minor_deviation,
  360. &frame->semi_major_orientation,
  361. &frame->latitude_error_deviation,
  362. &frame->longitude_error_deviation,
  363. &frame->altitude_error_deviation))
  364. return false;
  365. if (strcmp(type+2, "GST"))
  366. return false;
  367. return true;
  368. }
  369. bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence)
  370. {
  371. // $GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74
  372. char type[6];
  373. if (!minmea_scan(sentence, "tiiiiiiiiiiiiiiiiiii",
  374. type,
  375. &frame->total_msgs,
  376. &frame->msg_nr,
  377. &frame->total_sats,
  378. &frame->sats[0].nr,
  379. &frame->sats[0].elevation,
  380. &frame->sats[0].azimuth,
  381. &frame->sats[0].snr,
  382. &frame->sats[1].nr,
  383. &frame->sats[1].elevation,
  384. &frame->sats[1].azimuth,
  385. &frame->sats[1].snr,
  386. &frame->sats[2].nr,
  387. &frame->sats[2].elevation,
  388. &frame->sats[2].azimuth,
  389. &frame->sats[2].snr,
  390. &frame->sats[3].nr,
  391. &frame->sats[3].elevation,
  392. &frame->sats[3].azimuth,
  393. &frame->sats[3].snr
  394. )) {
  395. return false;
  396. }
  397. if (strcmp(type+2, "GSV"))
  398. return false;
  399. return true;
  400. }
  401. int minmea_gettimeofday(struct timeval *tv, const struct minmea_date *date, const struct minmea_time *time)
  402. {
  403. if (date->year == -1 || time->hours == -1)
  404. return -1;
  405. struct tm tm;
  406. tm.tm_year = 2000 + date->year - 1900;
  407. tm.tm_mon = date->month - 1;
  408. tm.tm_mday = date->day;
  409. tm.tm_hour = time->hours;
  410. tm.tm_min = time->minutes;
  411. tm.tm_sec = time->seconds;
  412. tm.tm_isdst = 0;
  413. time_t timestamp = timegm(&tm);
  414. if (timestamp != -1) {
  415. tv->tv_sec = timestamp;
  416. tv->tv_usec = time->microseconds;
  417. return 0;
  418. } else {
  419. return -1;
  420. }
  421. }
  422. /* vim: set ts=4 sw=4 et: */