minmea.c 14 KB

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