minmea.c 14 KB

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