You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

drivers.c 66KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. /*
  2. * This file is Copyright (c) 2010-2018 by the GPSD project
  3. * SPDX-License-Identifier: BSD-2-clause
  4. */
  5. #include "gpsd_config.h" /* must be before all includes */
  6. #include <assert.h>
  7. #include <ctype.h>
  8. #include <stdarg.h>
  9. #include <stdbool.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include "gpsd.h"
  15. #include "bits.h" /* for getbeu16(), to extract big-endian words */
  16. #include "strfuncs.h"
  17. ssize_t generic_get(struct gps_device_t *session)
  18. {
  19. return packet_get(session->gpsdata.gps_fd, &session->lexer);
  20. }
  21. gps_mask_t generic_parse_input(struct gps_device_t *session)
  22. {
  23. if (session->lexer.type == BAD_PACKET)
  24. return 0;
  25. else if (session->lexer.type == COMMENT_PACKET) {
  26. gpsd_set_century(session);
  27. return 0;
  28. #ifdef NMEA0183_ENABLE
  29. } else if (session->lexer.type == NMEA_PACKET) {
  30. const struct gps_type_t **dp;
  31. gps_mask_t st = 0;
  32. char *sentence = (char *)session->lexer.outbuffer;
  33. if (sentence[strlen(sentence)-1] != '\n')
  34. GPSD_LOG(LOG_IO, &session->context->errout,
  35. "<= GPS: %s\n", sentence);
  36. else
  37. GPSD_LOG(LOG_IO, &session->context->errout,
  38. "<= GPS: %s", sentence);
  39. if ((st=nmea_parse(sentence, session)) == 0) {
  40. GPSD_LOG(LOG_WARN, &session->context->errout,
  41. "unknown sentence: \"%s\"\n", sentence);
  42. }
  43. for (dp = gpsd_drivers; *dp; dp++) {
  44. char *trigger = (*dp)->trigger;
  45. if (trigger!=NULL && str_starts_with(sentence, trigger)) {
  46. GPSD_LOG(LOG_PROG, &session->context->errout,
  47. "found trigger string %s.\n", trigger);
  48. if (*dp != session->device_type) {
  49. (void)gpsd_switch_driver(session, (*dp)->type_name);
  50. if (session->device_type != NULL
  51. && session->device_type->event_hook != NULL)
  52. session->device_type->event_hook(session,
  53. event_triggermatch);
  54. st |= DEVICEID_SET;
  55. }
  56. }
  57. }
  58. return st;
  59. #endif /* NMEA0183_ENABLE */
  60. } else {
  61. GPSD_LOG(LOG_SHOUT, &session->context->errout,
  62. "packet type %d fell through (should never happen): %s.\n",
  63. session->lexer.type, gpsd_prettydump(session));
  64. return 0;
  65. }
  66. }
  67. /**************************************************************************
  68. *
  69. * Generic driver -- make no assumptions about the device type
  70. *
  71. **************************************************************************/
  72. /* *INDENT-OFF* */
  73. const struct gps_type_t driver_unknown = {
  74. .type_name = "Unknown", /* full name of type */
  75. .packet_type = COMMENT_PACKET, /* associated lexer packet type */
  76. .flags = DRIVER_NOFLAGS, /* no flags set */
  77. .trigger = NULL, /* it's the default */
  78. .channels = 12, /* consumer-grade GPS */
  79. .probe_detect = NULL, /* no probe */
  80. .get_packet = generic_get, /* use generic packet getter */
  81. .parse_packet = generic_parse_input, /* how to interpret a packet */
  82. .rtcm_writer = NULL, /* write RTCM data straight */
  83. .init_query = NULL, /* non-perturbing initial query */
  84. .event_hook = NULL, /* lifetime event handler */
  85. #ifdef RECONFIGURE_ENABLE
  86. .speed_switcher = NULL, /* no speed switcher */
  87. .mode_switcher = NULL, /* no mode switcher */
  88. .rate_switcher = NULL, /* no sample-rate switcher */
  89. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  90. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  91. #endif /* RECONFIGURE_ENABLE */
  92. #ifdef CONTROLSEND_ENABLE
  93. .control_send = NULL, /* how to send control strings */
  94. #endif /* CONTROLSEND_ENABLE */
  95. .time_offset = NULL, /* no method for NTP fudge factor */
  96. };
  97. /* *INDENT-ON* */
  98. #ifdef NMEA0183_ENABLE
  99. /**************************************************************************
  100. *
  101. * NMEA 0183
  102. *
  103. * This is separate from the 'unknown' driver because we don't want to
  104. * ship NMEA subtype probe strings to a device until we've seen at
  105. * least one NMEA packet. This avoids spamming devices that might
  106. * actually be USB modems or other things in USB device class FF that
  107. * just happen to have one of 'our' adaptor chips in front of them.
  108. *
  109. **************************************************************************/
  110. static void nmea_event_hook(struct gps_device_t *session, event_t event)
  111. {
  112. if (session->context->readonly)
  113. return;
  114. /*
  115. * This is where we try to tickle NMEA devices into revealing their
  116. * inner natures.
  117. */
  118. if (event == event_configure) {
  119. /*
  120. * The reason for splitting these probes up by packet sequence
  121. * number, interleaving them with the first few packet receives,
  122. * is because many generic-NMEA devices get confused if you send
  123. * too much at them in one go.
  124. *
  125. * A fast response to an early probe will change drivers so the
  126. * later ones won't be sent at all. Thus, for best overall
  127. * performance, order these to probe for the most popular types
  128. * soonest.
  129. *
  130. * Note: don't make the trigger strings identical to the probe,
  131. * because some NMEA devices (notably SiRFs) will just echo
  132. * unknown strings right back at you. A useful dodge is to append
  133. * a comma to the trigger, because that won't be in the response
  134. * unless there is actual following data.
  135. */
  136. switch (session->lexer.counter) {
  137. #ifdef NMEA0183_ENABLE
  138. case 0:
  139. /* probe for Garmin serial GPS -- expect $PGRMC followed by data */
  140. GPSD_LOG(LOG_PROG, &session->context->errout,
  141. "=> Probing for Garmin NMEA\n");
  142. (void)nmea_send(session, "$PGRMCE");
  143. break;
  144. #endif /* NMEA0183_ENABLE */
  145. #ifdef SIRF_ENABLE
  146. case 1:
  147. /*
  148. * We used to try to probe for SiRF by issuing
  149. * "$PSRF105,1" and expecting "$Ack Input105.". But it
  150. * turns out this only works for SiRF-IIs; SiRF-I and
  151. * SiRF-III don't respond. Sadly, the MID132 binary
  152. * request for firmware version is ignored in NMEA mode.
  153. * Thus the only reliable probe is to try to flip the SiRF
  154. * into binary mode, cluing in the library to revert it on
  155. * close.
  156. *
  157. * SiRFs dominate the consumer-grade GPS-mouse market, so
  158. * we used to put this test first. Unfortunately this
  159. * causes problems for gpsctl, as it cannot select the
  160. * NMEA driver without switching the device back to binary
  161. * mode! Fix this if we ever find a nondisruptive probe
  162. * string.
  163. */
  164. GPSD_LOG(LOG_PROG, &session->context->errout,
  165. "=> Probing for SiRF\n");
  166. (void)nmea_send(session,
  167. "$PSRF100,0,%d,%d,%d,0",
  168. session->gpsdata.dev.baudrate,
  169. 9 - session->gpsdata.dev.stopbits,
  170. session->gpsdata.dev.stopbits);
  171. break;
  172. #endif /* SIRF_ENABLE */
  173. #ifdef NMEA0183_ENABLE
  174. case 2:
  175. /* probe for the FV-18 -- expect $PFEC,GPint followed by data */
  176. GPSD_LOG(LOG_PROG, &session->context->errout,
  177. "=> Probing for FV-18\n");
  178. (void)nmea_send(session, "$PFEC,GPint");
  179. break;
  180. case 3:
  181. /* probe for the Trimble Copernicus */
  182. GPSD_LOG(LOG_PROG, &session->context->errout,
  183. "=> Probing for Trimble Copernicus\n");
  184. (void)nmea_send(session, "$PTNLSNM,0139,01");
  185. break;
  186. #endif /* NMEA0183_ENABLE */
  187. #ifdef EVERMORE_ENABLE
  188. case 4:
  189. GPSD_LOG(LOG_PROG, &session->context->errout,
  190. "=> Probing for Evermore\n");
  191. /* Enable checksum and GGA(1s), GLL(0s), GSA(1s), GSV(1s), RMC(1s), VTG(0s), PEMT101(0s) */
  192. /* EverMore will reply with: \x10\x02\x04\x38\x8E\xC6\x10\x03 */
  193. (void)gpsd_write(session,
  194. "\x10\x02\x12\x8E\x7F\x01\x01\x00\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x13\x10\x03",
  195. 22);
  196. break;
  197. #endif /* EVERMORE_ENABLE */
  198. #ifdef GPSCLOCK_ENABLE
  199. case 5:
  200. /* probe for Furuno Electric GH-79L4-N (GPSClock); expect $PFEC,GPssd */
  201. GPSD_LOG(LOG_PROG, &session->context->errout,
  202. "=> Probing for GPSClock\n");
  203. (void)nmea_send(session, "$PFEC,GPsrq");
  204. break;
  205. #endif /* GPSCLOCK_ENABLE */
  206. #ifdef ASHTECH_ENABLE
  207. case 6:
  208. /* probe for Ashtech -- expect $PASHR,RID */
  209. GPSD_LOG(LOG_PROG, &session->context->errout,
  210. "=> Probing for Ashtech\n");
  211. (void)nmea_send(session, "$PASHQ,RID");
  212. break;
  213. #endif /* ASHTECH_ENABLE */
  214. #ifdef UBLOX_ENABLE
  215. case 7:
  216. /* probe for UBX -- query port configuration */
  217. GPSD_LOG(LOG_PROG, &session->context->errout,
  218. "=> Probing for UBX\n");
  219. (void)ubx_write(session, 0x06, 0x00, NULL, 0);
  220. break;
  221. #endif /* UBLOX_ENABLE */
  222. #ifdef MTK3301_ENABLE
  223. case 8:
  224. /* probe for MTK-3301 -- expect $PMTK705 */
  225. GPSD_LOG(LOG_PROG, &session->context->errout,
  226. "=> Probing for MediaTek\n");
  227. (void)nmea_send(session, "$PMTK605");
  228. break;
  229. #endif /* MTK3301_ENABLE */
  230. #ifdef GREIS_ENABLE
  231. case 9:
  232. /* probe for Javad GREIS -- expect reply with JAVAD */
  233. GPSD_LOG(LOG_PROG, &session->context->errout,
  234. "=> Probing for Javad GREIS\n");
  235. /* TODO: confirm this actually gets JAVAD response */
  236. (void)nmea_send(session, "print,/par/rcv/vendor");
  237. break;
  238. #endif /* GREIS_ENABLE */
  239. default:
  240. break;
  241. }
  242. }
  243. }
  244. /* *INDENT-OFF* */
  245. const struct gps_type_t driver_nmea0183 = {
  246. .type_name = "NMEA0183", /* full name of type */
  247. .packet_type = NMEA_PACKET, /* associated lexer packet type */
  248. .flags = DRIVER_NOFLAGS, /* remember this */
  249. .trigger = NULL, /* it's the default */
  250. .channels = 12, /* consumer-grade GPS */
  251. .probe_detect = NULL, /* no probe */
  252. .get_packet = generic_get, /* use generic packet getter */
  253. .parse_packet = generic_parse_input, /* how to interpret a packet */
  254. .rtcm_writer = gpsd_write, /* write RTCM data straight */
  255. .init_query = NULL, /* non-perturbing initial query */
  256. .event_hook = nmea_event_hook, /* lifetime event handler */
  257. #ifdef RECONFIGURE_ENABLE
  258. .speed_switcher = NULL, /* no speed switcher */
  259. #ifdef BINARY_ENABLE
  260. .mode_switcher = NULL, /* maybe switchable if it was a SiRF */
  261. #else
  262. .mode_switcher = NULL, /* no binary mode to revert to */
  263. #endif /* BINARY_ENABLE */
  264. .rate_switcher = NULL, /* no sample-rate switcher */
  265. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  266. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  267. #endif /* RECONFIGURE_ENABLE */
  268. #ifdef CONTROLSEND_ENABLE
  269. .control_send = nmea_write, /* how to send control strings */
  270. #endif /* CONTROLSEND_ENABLE */
  271. .time_offset = NULL, /* no method for NTP fudge factor */
  272. };
  273. /* *INDENT-ON* */
  274. #if defined(GARMIN_ENABLE) && defined(NMEA0183_ENABLE)
  275. /**************************************************************************
  276. *
  277. * Garmin NMEA
  278. *
  279. **************************************************************************/
  280. #ifdef RECONFIGURE_ENABLE
  281. static void garmin_mode_switch(struct gps_device_t *session, int mode)
  282. /* only does anything in one direction, going to Garmin binary driver */
  283. {
  284. struct timespec delay;
  285. if (mode == MODE_BINARY) {
  286. (void)nmea_send(session, "$PGRMC1,1,2,1,,,,2,W,N");
  287. (void)nmea_send(session, "$PGRMI,,,,,,,R");
  288. /* wait 333 uSec, standard Garmin settling time */
  289. delay.tv_sec = 0;
  290. delay.tv_nsec = 333000L;
  291. nanosleep(&delay, NULL);
  292. }
  293. }
  294. #endif /* RECONFIGURE_ENABLE */
  295. static void garmin_nmea_event_hook(struct gps_device_t *session,
  296. event_t event)
  297. {
  298. if (session->context->readonly)
  299. return;
  300. if (event == event_driver_switch) {
  301. /* forces a reconfigure as the following packets come in */
  302. session->lexer.counter = 0;
  303. }
  304. if (event == event_configure) {
  305. /*
  306. * And here's that reconfigure. It's split up like this because
  307. * receivers like the Garmin GPS-10 don't handle having having a lot of
  308. * probes shoved at them very well.
  309. */
  310. switch (session->lexer.counter) {
  311. case 0:
  312. /* reset some config, AutoFix, WGS84, PPS
  313. * Set the PPS pulse length to 40ms which leaves the Garmin 18-5hz
  314. * with a 160ms low state.
  315. * NOTE: new PPS only takes effect after next power cycle
  316. */
  317. (void)nmea_send(session, "$PGRMC,A,,100,,,,,,A,,1,2,1,30");
  318. break;
  319. case 1:
  320. /* once a sec, no averaging, NMEA 2.3, WAAS */
  321. (void)nmea_send(session, "$PGRMC1,1,1,1,,,,2,W,N");
  322. break;
  323. case 2:
  324. /* get some more config info */
  325. (void)nmea_send(session, "$PGRMC1E");
  326. break;
  327. case 3:
  328. /* turn off all output except GGA */
  329. (void)nmea_send(session, "$PGRMO,,2");
  330. (void)nmea_send(session, "$PGRMO,GPGGA,1");
  331. break;
  332. case 4:
  333. /* enable GPGGA, GPGSA, GPGSV, GPRMC on Garmin serial GPS */
  334. (void)nmea_send(session, "$PGRMO,GPGSA,1");
  335. break;
  336. case 5:
  337. (void)nmea_send(session, "$PGRMO,GPGSV,1");
  338. break;
  339. case 6:
  340. (void)nmea_send(session, "$PGRMO,GPRMC,1");
  341. break;
  342. case 7:
  343. (void)nmea_send(session, "$PGRMO,PGRME,1");
  344. break;
  345. }
  346. }
  347. }
  348. /* *INDENT-OFF* */
  349. const struct gps_type_t driver_garmin = {
  350. .type_name = "Garmin NMEA", /* full name of type */
  351. .packet_type = NMEA_PACKET, /* associated lexer packet type */
  352. .flags = DRIVER_STICKY, /* remember this */
  353. .trigger = "$PGRMC,", /* Garmin private */
  354. .channels = 12, /* not used by this driver */
  355. .probe_detect = NULL, /* no probe */
  356. .get_packet = generic_get, /* use generic packet getter */
  357. .parse_packet = generic_parse_input, /* how to interpret a packet */
  358. .rtcm_writer = NULL, /* some do, some don't, skip for now */
  359. .init_query = NULL, /* non-perturbing initial query */
  360. .event_hook = garmin_nmea_event_hook, /* lifetime event handler */
  361. #ifdef RECONFIGURE_ENABLE
  362. .speed_switcher = NULL, /* no speed switcher */
  363. .mode_switcher = garmin_mode_switch, /* mode switcher */
  364. .rate_switcher = NULL, /* no sample-rate switcher */
  365. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  366. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  367. #endif /*RECONFIGURE_ENABLE */
  368. #ifdef CONTROLSEND_ENABLE
  369. .control_send = nmea_write, /* how to send control strings */
  370. #endif /* CONTROLSEND_ENABLE */
  371. .time_offset = NULL, /* no method for NTP fudge factor */
  372. };
  373. /* *INDENT-ON* */
  374. #endif /* GARMIN_ENABLE && NMEA0183_ENABLE */
  375. #ifdef ASHTECH_ENABLE
  376. /**************************************************************************
  377. *
  378. * Ashtech (then Thales, now Magellan Professional) Receivers
  379. *
  380. **************************************************************************/
  381. static void ashtech_event_hook(struct gps_device_t *session, event_t event)
  382. {
  383. if (session->context->readonly)
  384. return;
  385. if (event == event_wakeup)
  386. (void)nmea_send(session, "$PASHQ,RID");
  387. if (event == event_identified) {
  388. /* turn WAAS on. can't hurt... */
  389. (void)nmea_send(session, "$PASHS,WAS,ON");
  390. /* reset to known output state */
  391. (void)nmea_send(session, "$PASHS,NME,ALL,A,OFF");
  392. /* then turn on some useful sentences */
  393. #ifdef __future__
  394. /* we could parse these, but they're oversize so they get dropped */
  395. (void)nmea_send(session, "$PASHS,NME,POS,A,ON");
  396. (void)nmea_send(session, "$PASHS,NME,SAT,A,ON");
  397. #else
  398. (void)nmea_send(session, "$PASHS,NME,GGA,A,ON");
  399. (void)nmea_send(session, "$PASHS,NME,GSA,A,ON");
  400. (void)nmea_send(session, "$PASHS,NME,GSV,A,ON");
  401. (void)nmea_send(session, "$PASHS,NME,RMC,A,ON");
  402. #endif
  403. (void)nmea_send(session, "$PASHS,NME,ZDA,A,ON");
  404. }
  405. }
  406. /* *INDENT-OFF* */
  407. const struct gps_type_t driver_ashtech = {
  408. .type_name = "Ashtech", /* full name of type */
  409. .packet_type = NMEA_PACKET, /* associated lexer packet type */
  410. .flags = DRIVER_STICKY, /* remember this */
  411. .trigger = "$PASHR,RID,", /* Ashtech receivers respond thus */
  412. .channels = 24, /* not used, GG24 has 24 channels */
  413. .probe_detect = NULL, /* no probe */
  414. .get_packet = generic_get, /* how to get a packet */
  415. .parse_packet = generic_parse_input, /* how to interpret a packet */
  416. .rtcm_writer = gpsd_write, /* write RTCM data straight */
  417. .init_query = NULL, /* non-perturbing initial query */
  418. .event_hook = ashtech_event_hook, /* lifetime event handler */
  419. #ifdef RECONFIGURE_ENABLE
  420. .speed_switcher = NULL, /* no speed switcher */
  421. .mode_switcher = NULL, /* no mode switcher */
  422. .rate_switcher = NULL, /* no sample-rate switcher */
  423. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  424. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  425. #endif /* RECONFIGURE_ENABLE */
  426. #ifdef CONTROLSEND_ENABLE
  427. .control_send = nmea_write, /* how to send control strings */
  428. #endif /* CONTROLSEND_ENABLE */
  429. .time_offset = NULL, /* no method for NTP fudge factor */
  430. };
  431. /* *INDENT-ON* */
  432. #endif /* ASHTECH_ENABLE */
  433. #ifdef FV18_ENABLE
  434. /**************************************************************************
  435. *
  436. * FV18 -- uses 2 stop bits, needs to be told to send GSAs
  437. *
  438. **************************************************************************/
  439. static void fv18_event_hook(struct gps_device_t *session, event_t event)
  440. {
  441. if (session->context->readonly)
  442. return;
  443. /*
  444. * Tell an FV18 to send GSAs so we'll know if 3D is accurate.
  445. * Suppress GLL and VTG. Enable ZDA so dates will be accurate for replay.
  446. * It's possible we might not need to redo this on event_reactivate,
  447. * but doing so is safe and cheap.
  448. */
  449. if (event == event_identified || event == event_reactivate)
  450. (void)nmea_send(session,
  451. "$PFEC,GPint,GSA01,DTM00,ZDA01,RMC01,GLL00,VTG00,GSV05");
  452. }
  453. /* *INDENT-OFF* */
  454. const struct gps_type_t driver_fv18 = {
  455. .type_name = "San Jose Navigation FV18", /* full name of type */
  456. .packet_type = NMEA_PACKET, /* associated lexer packet type */
  457. .flags = DRIVER_STICKY, /* remember this */
  458. .trigger = "$PFEC,GPint,", /* FV18s should echo the probe */
  459. .channels = 12, /* not used by this driver */
  460. .probe_detect = NULL, /* no probe */
  461. .get_packet = generic_get, /* how to get a packet */
  462. .parse_packet = generic_parse_input, /* how to interpret a packet */
  463. .rtcm_writer = gpsd_write, /* write RTCM data straight */
  464. .init_query = NULL, /* non-perturbing initial query */
  465. .event_hook = fv18_event_hook, /* lifetime event handler */
  466. #ifdef RECONFIGURE_ENABLE
  467. .speed_switcher = NULL, /* no speed switcher */
  468. .mode_switcher = NULL, /* no mode switcher */
  469. .rate_switcher = NULL, /* no sample-rate switcher */
  470. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  471. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  472. #endif /* RECONFIGURE_ENABLE */
  473. #ifdef CONTROLSEND_ENABLE
  474. .control_send = nmea_write, /* how to send control strings */
  475. #endif /* CONTROLSEND_ENABLE */
  476. .time_offset = NULL, /* no method for NTP fudge factor */
  477. };
  478. /* *INDENT-ON* */
  479. #endif /* FV18_ENABLE */
  480. #ifdef GPSCLOCK_ENABLE
  481. /**************************************************************************
  482. *
  483. * Furuno Electric GPSClock (GH-79L4)
  484. *
  485. **************************************************************************/
  486. /*
  487. * Based on http://www.tecsys.de/fileadmin/user_upload/pdf/gh79_1an_intant.pdf
  488. */
  489. /* *INDENT-OFF* */
  490. const struct gps_type_t driver_gpsclock = {
  491. .type_name = "Furuno Electric GH-79L4", /* full name of type */
  492. .packet_type = NMEA_PACKET, /* associated lexer packet type */
  493. .flags = DRIVER_STICKY, /* remember this */
  494. .trigger = "$PFEC,GPssd", /* GPSClock should return this */
  495. .channels = 12, /* not used by this driver */
  496. .probe_detect = NULL, /* no probe */
  497. .get_packet = generic_get, /* how to get a packet */
  498. .parse_packet = generic_parse_input, /* how to interpret a packet */
  499. .rtcm_writer = gpsd_write, /* write RTCM data straight */
  500. .init_query = NULL, /* non-perturbing initial query */
  501. .event_hook = NULL, /* no lifetime event handler */
  502. #ifdef RECONFIGURE_ENABLE
  503. .speed_switcher = NULL, /* no speed switcher */
  504. .mode_switcher = NULL, /* no mode switcher */
  505. .rate_switcher = NULL, /* sample rate is fixed */
  506. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  507. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  508. #endif /* RECONFIGURE_ENABLE */
  509. #ifdef CONTROLSEND_ENABLE
  510. .control_send = nmea_write, /* how to send control strings */
  511. #endif /* CONTROLSEND_ENABLE */
  512. .time_offset = NULL, /* no method for NTP fudge factor */
  513. };
  514. /* *INDENT-ON* */
  515. #endif /* GPSCLOCK_ENABLE */
  516. #ifdef TRIPMATE_ENABLE
  517. /**************************************************************************
  518. *
  519. * TripMate -- extended NMEA, gets faster fix when primed with lat/long/time
  520. *
  521. **************************************************************************/
  522. /*
  523. * Some technical FAQs on the TripMate:
  524. * http://vancouver-webpages.com/pub/peter/tripmate.faq
  525. * http://www.asahi-net.or.jp/~KN6Y-GTU/tripmate/trmfaqe.html
  526. * The TripMate was discontinued sometime before November 1998
  527. * and was replaced by the Zodiac EarthMate.
  528. */
  529. static void tripmate_event_hook(struct gps_device_t *session, event_t event)
  530. {
  531. if (session->context->readonly)
  532. return;
  533. /* TripMate requires this response to the ASTRAL it sends at boot time */
  534. if (event == event_identified)
  535. (void)nmea_send(session, "$IIGPQ,ASTRAL");
  536. /* stop it sending PRWIZCH */
  537. if (event == event_identified || event == event_reactivate)
  538. (void)nmea_send(session, "$PRWIILOG,ZCH,V,,");
  539. }
  540. /* *INDENT-OFF* */
  541. static const struct gps_type_t driver_tripmate = {
  542. .type_name = "Delorme TripMate", /* full name of type */
  543. .packet_type = NMEA_PACKET, /* lexer packet type */
  544. .flags = DRIVER_STICKY, /* no rollover or other flags */
  545. .trigger ="ASTRAL", /* tells us to switch */
  546. .channels = 12, /* consumer-grade GPS */
  547. .probe_detect = NULL, /* no probe */
  548. .get_packet = generic_get, /* how to get a packet */
  549. .parse_packet = generic_parse_input, /* how to interpret a packet */
  550. .rtcm_writer = gpsd_write, /* send RTCM data straight */
  551. .init_query = NULL, /* non-perturbing query */
  552. .event_hook = tripmate_event_hook, /* lifetime event handler */
  553. #ifdef RECONFIGURE_ENABLE
  554. .speed_switcher= NULL, /* no speed switcher */
  555. .mode_switcher = NULL, /* no mode switcher */
  556. .rate_switcher = NULL, /* no sample-rate switcher */
  557. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  558. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  559. #endif /* RECONFIGURE_ENABLE */
  560. #ifdef CONTROLSEND_ENABLE
  561. .control_send = nmea_write, /* how to send control strings */
  562. #endif /* CONTROLSEND_ENABLE */
  563. .time_offset = NULL, /* no method for NTP fudge factor */
  564. };
  565. /* *INDENT-ON* */
  566. #endif /* TRIPMATE_ENABLE */
  567. #ifdef EARTHMATE_ENABLE
  568. /**************************************************************************
  569. *
  570. * Zodiac EarthMate textual mode
  571. *
  572. * Note: This is the pre-2003 version using Zodiac binary protocol.
  573. * There is a good HOWTO at <http://www.hamhud.net/ka9mva/earthmate.htm>.
  574. * It has been replaced with a design that uses a SiRF chipset.
  575. *
  576. **************************************************************************/
  577. static void earthmate_event_hook(struct gps_device_t *session, event_t event)
  578. {
  579. struct timespec delay;
  580. if (session->context->readonly)
  581. return;
  582. if (event == event_triggermatch) {
  583. (void)gpsd_write(session, "EARTHA\r\n", 8);
  584. /* wait 10,000 uSec */
  585. delay.tv_sec = 0;
  586. delay.tv_nsec = 10000000L;
  587. nanosleep(&delay, NULL);
  588. (void)gpsd_switch_driver(session, "Zodiac");
  589. }
  590. }
  591. /* *INDENT-OFF* */
  592. static const struct gps_type_t driver_earthmate = {
  593. .type_name = "Pre-2003 Delorme EarthMate",
  594. .packet_type = NMEA_PACKET, /* associated lexer packet type */
  595. .flags = DRIVER_STICKY, /* no rollover or other flags */
  596. .trigger = "EARTHA", /* Earthmate trigger string */
  597. .channels = 12, /* not used by NMEA parser */
  598. .probe_detect = NULL, /* no probe */
  599. .get_packet = generic_get, /* how to get a packet */
  600. .parse_packet = generic_parse_input, /* how to interpret a packet */
  601. .rtcm_writer = NULL, /* don't send RTCM data */
  602. .init_query = NULL, /* non-perturbing query */
  603. .event_hook = earthmate_event_hook, /* lifetime event handler */
  604. #ifdef RECONFIGURE_ENABLE
  605. .speed_switcher= NULL, /* no speed switcher */
  606. .mode_switcher = NULL, /* no mode switcher */
  607. .rate_switcher = NULL, /* no sample-rate switcher */
  608. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  609. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  610. #endif /* RECONFIGURE_ENABLE */
  611. #ifdef CONTROLSEND_ENABLE
  612. .control_send = nmea_write, /* never actually used. */
  613. #endif /* CONTROLSEND_ENABLE */
  614. .time_offset = NULL, /* no method for NTP fudge factor */
  615. };
  616. /* *INDENT-ON* */
  617. #endif /* EARTHMATE_ENABLE */
  618. #endif /* NMEA0183_ENABLE */
  619. #ifdef TNT_ENABLE
  620. /**************************************************************************
  621. * True North Technologies - Revolution 2X Digital compass
  622. *
  623. * More info: http://www.tntc.com/
  624. *
  625. * This is a digital compass which uses magnetometers to measure the
  626. * strength of the earth's magnetic field. Based on these measurements
  627. * it provides a compass heading using NMEA formatted output strings.
  628. * This is useful to supplement the heading provided by another GPS
  629. * unit. A GPS heading is unreliable at slow speed or no speed.
  630. *
  631. **************************************************************************/
  632. static ssize_t tnt_control_send(struct gps_device_t *session,
  633. char *msg, size_t len UNUSED)
  634. /* send a control string in TNT native formal */
  635. {
  636. ssize_t status;
  637. unsigned char sum = '\0';
  638. char c, *p = msg;
  639. if (*p == '@') {
  640. p++;
  641. }
  642. #ifdef __UNUSED__
  643. else {
  644. GPSD_LOG(LOG_ERROR, &session->context->errout,
  645. "Bad TNT sentence: '%s'\n", msg);
  646. }
  647. #endif /* __UNUSED__ */
  648. while (((c = *p) != '\0')) {
  649. sum ^= c;
  650. p++;
  651. }
  652. (void)snprintf(p, 6, "*%02X\r\n", (unsigned int)sum);
  653. status = gpsd_write(session, msg, strlen(msg));
  654. return status;
  655. }
  656. static bool tnt_send(struct gps_device_t *session, const char *fmt, ...)
  657. /* printf(3)-like TNT command generator */
  658. {
  659. char buf[BUFSIZ];
  660. va_list ap;
  661. ssize_t sent;
  662. va_start(ap, fmt);
  663. (void)vsnprintf(buf, sizeof(buf) - 5, fmt, ap);
  664. va_end(ap);
  665. sent = tnt_control_send(session, buf, strlen(buf));
  666. if (sent == (ssize_t) strlen(buf)) {
  667. GPSD_LOG(LOG_IO, &session->context->errout,
  668. "=> GPS: %s\n", buf);
  669. return true;
  670. } else {
  671. GPSD_LOG(LOG_WARN, &session->context->errout,
  672. "=> GPS: %s FAILED\n", buf);
  673. return false;
  674. }
  675. }
  676. #ifdef RECONFIGURE_ENABLE
  677. static bool tnt_speed(struct gps_device_t *session,
  678. speed_t speed, char parity UNUSED, int stopbits UNUSED)
  679. {
  680. /*
  681. * Baud rate change followed by device reset.
  682. * See page 40 of Technical Guide 1555-B. We need:
  683. * 2400 -> 1, 4800 -> 2, 9600 -> 3, 19200 -> 4, 38400 -> 5
  684. */
  685. unsigned int val = speed / 2400u; /* 2400->1, 4800->2, 9600->4, 19200->8... */
  686. unsigned int i = 0;
  687. /* fast way to compute log2(val) */
  688. while ((val >> i) > 1)
  689. ++i;
  690. return tnt_send(session, "@B6=%d", i + 1)
  691. && tnt_send(session, "@F28.6=1");
  692. }
  693. #endif /* RECONFIGURE_ENABLE */
  694. static void tnt_event_hook(struct gps_device_t *session, event_t event)
  695. /* TNT lifetime event hook */
  696. {
  697. if (session->context->readonly)
  698. return;
  699. if (event == event_wakeup) {
  700. (void)tnt_send(session, "@F0.3=1"); /* set run mode */
  701. (void)tnt_send(session, "@F2.2=1"); /* report in degrees */
  702. }
  703. }
  704. /* *INDENT-OFF* */
  705. const struct gps_type_t driver_trueNorth = {
  706. .type_name = "True North", /* full name of type */
  707. .packet_type = NMEA_PACKET, /* associated lexer packet type */
  708. .flags = DRIVER_STICKY, /* remember this */
  709. .trigger = "$PTNTHTM", /* their proprietary sentence */
  710. .channels = 0, /* not an actual GPS at all */
  711. .probe_detect = NULL, /* no probe in run mode */
  712. .get_packet = generic_get, /* how to get a packet */
  713. .parse_packet = generic_parse_input, /* how to interpret a packet */
  714. .rtcm_writer = NULL, /* Don't send */
  715. .init_query = NULL, /* non-perturbing initial query */
  716. .event_hook = tnt_event_hook, /* lifetime event handler */
  717. #ifdef RECONFIGURE_ENABLE
  718. .speed_switcher = tnt_speed, /* no speed switcher */
  719. .mode_switcher = NULL, /* no mode switcher */
  720. .rate_switcher = NULL, /* no wrapup */
  721. .min_cycle.tv_sec = 0, /* not relevant, no rate switch */
  722. .min_cycle.tv_nsec = 500000000, /* not relevant, no rate switch */
  723. #endif /* RECONFIGURE_ENABLE */
  724. #ifdef CONTROLSEND_ENABLE
  725. .control_send = tnt_control_send, /* how to send control strings */
  726. #endif /* CONTROLSEND_ENABLE */
  727. .time_offset = NULL,
  728. };
  729. /* *INDENT-ON* */
  730. #endif
  731. #ifdef OCEANSERVER_ENABLE
  732. /**************************************************************************
  733. * OceanServer - Digital Compass, OS5000 Series
  734. *
  735. * More info: http://www.ocean-server.com/download/OS5000_Compass_Manual.pdf
  736. *
  737. * This is a digital compass which uses magnetometers to measure the
  738. * strength of the earth's magnetic field. Based on these measurements
  739. * it provides a compass heading using NMEA formatted output strings.
  740. * This is useful to supplement the heading provided by another GPS
  741. * unit. A GPS heading is unreliable at slow speed or no speed.
  742. *
  743. **************************************************************************/
  744. static int oceanserver_send(struct gpsd_errout_t *errout,
  745. const int fd, const char *fmt, ...)
  746. {
  747. int status;
  748. char buf[BUFSIZ];
  749. va_list ap;
  750. va_start(ap, fmt);
  751. (void)vsnprintf(buf, sizeof(buf) - 5, fmt, ap);
  752. va_end(ap);
  753. (void)strlcat(buf, "", sizeof(buf));
  754. status = (int)write(fd, buf, strlen(buf));
  755. (void)tcdrain(fd);
  756. if (status == (int)strlen(buf)) {
  757. GPSD_LOG(LOG_IO, errout, "=> GPS: %s\n", buf);
  758. return status;
  759. } else {
  760. GPSD_LOG(LOG_WARN, errout, "=> GPS: %s FAILED\n", buf);
  761. return -1;
  762. }
  763. }
  764. static void oceanserver_event_hook(struct gps_device_t *session,
  765. event_t event)
  766. {
  767. if (session->context->readonly)
  768. return;
  769. if (event == event_configure && session->lexer.counter == 0) {
  770. /* report in NMEA format */
  771. (void)oceanserver_send(&session->context->errout,
  772. session->gpsdata.gps_fd, "2\n");
  773. /* ship all fields */
  774. (void)oceanserver_send(&session->context->errout,
  775. session->gpsdata.gps_fd, "X2047");
  776. }
  777. }
  778. /* *INDENT-OFF* */
  779. static const struct gps_type_t driver_oceanServer = {
  780. .type_name = "OceanServer OS5000", /* full name of type */
  781. .packet_type = NMEA_PACKET, /* associated lexer packet type */
  782. .flags = DRIVER_STICKY, /* no rollover or other flags */
  783. .trigger = "$OHPR,", /* detect their main sentence */
  784. .channels = 0, /* not an actual GPS at all */
  785. .probe_detect = NULL,
  786. .get_packet = generic_get, /* how to get a packet */
  787. .parse_packet = generic_parse_input, /* how to interpret a packet */
  788. .rtcm_writer = NULL, /* Don't send */
  789. .init_query = NULL, /* non-perturbing initial query */
  790. .event_hook = oceanserver_event_hook,
  791. #ifdef RECONFIGURE_ENABLE
  792. .speed_switcher = NULL, /* no speed switcher */
  793. .mode_switcher = NULL, /* no mode switcher */
  794. .rate_switcher = NULL, /* no wrapup */
  795. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  796. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  797. #endif /* RECONFIGURE_ENABLE */
  798. #ifdef CONTROLSEND_ENABLE
  799. .control_send = nmea_write, /* how to send control strings */
  800. #endif /* CONTROLSEND_ENABLE */
  801. .time_offset = NULL,
  802. };
  803. /* *INDENT-ON* */
  804. #endif
  805. #ifdef FURY_ENABLE
  806. /**************************************************************************
  807. *
  808. * Jackson Labs Fury, a high-precision laboratory clock
  809. *
  810. * Will also support other Jackon Labs boards, including the Firefly.
  811. *
  812. * Note: you must either build with fixed_port_speed=115200 or tweak the
  813. * speed on the port to 115200 before running. The device's default mode
  814. * does not stream output, so our hunt loop will simply time out otherwise.
  815. *
  816. **************************************************************************/
  817. static bool fury_rate_switcher(struct gps_device_t *session, double rate)
  818. {
  819. char buf[78];
  820. double inverted;
  821. /* rate is a frequency, but the command takes interval in # of seconds */
  822. if (rate == 0.0)
  823. inverted = 0.0;
  824. else
  825. inverted = 1.0/rate;
  826. if (inverted > 256)
  827. return false;
  828. (void)snprintf(buf, sizeof(buf), "GPS:GPGGA %d\r\n", (int)inverted);
  829. (void)gpsd_write(session, buf, strlen(buf));
  830. return true;
  831. }
  832. static void fury_event_hook(struct gps_device_t *session, event_t event)
  833. {
  834. if (event == event_wakeup && gpsd_get_speed(session) == 115200)
  835. (void)fury_rate_switcher(session, 1.0);
  836. else if (event == event_deactivate)
  837. (void)fury_rate_switcher(session, 0.0);
  838. }
  839. /* *INDENT-OFF* */
  840. static const struct gps_type_t driver_fury = {
  841. .type_name = "Jackson Labs Fury", /* full name of type */
  842. .packet_type = NMEA_PACKET, /* associated lexer packet type */
  843. .flags = DRIVER_STICKY, /* no rollover or other flags */
  844. .trigger = NULL, /* detect their main sentence */
  845. .channels = 0, /* not an actual GPS at all */
  846. .probe_detect = NULL,
  847. .get_packet = generic_get, /* how to get a packet */
  848. .parse_packet = generic_parse_input, /* how to interpret a packet */
  849. .rtcm_writer = NULL, /* Don't send */
  850. .init_query = NULL, /* non-perturbing initial query */
  851. .event_hook = fury_event_hook,
  852. #ifdef RECONFIGURE_ENABLE
  853. .speed_switcher = NULL, /* no speed switcher */
  854. .mode_switcher = NULL, /* no mode switcher */
  855. .rate_switcher = fury_rate_switcher,
  856. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  857. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  858. #endif /* RECONFIGURE_ENABLE */
  859. #ifdef CONTROLSEND_ENABLE
  860. .control_send = nmea_write, /* how to send control strings */
  861. #endif /* CONTROLSEND_ENABLE */
  862. .time_offset = NULL,
  863. };
  864. /* *INDENT-ON* */
  865. #endif /* FURY_ENABLE */
  866. #ifdef RTCM104V2_ENABLE
  867. /**************************************************************************
  868. *
  869. * RTCM-104 (v2), used for broadcasting DGPS corrections and by DGPS radios
  870. *
  871. **************************************************************************/
  872. static gps_mask_t rtcm104v2_analyze(struct gps_device_t *session)
  873. {
  874. rtcm2_unpack(&session->gpsdata.rtcm2, (char *)session->lexer.isgps.buf);
  875. /* extra guard prevents expensive hexdump calls */
  876. if (session->context->errout.debug >= LOG_RAW)
  877. GPSD_LOG(LOG_RAW, &session->context->errout,
  878. "RTCM 2.x packet type 0x%02x length %d words "
  879. "from %zd bytes: %s\n",
  880. session->gpsdata.rtcm2.type,
  881. session->gpsdata.rtcm2.length + 2,
  882. session->lexer.isgps.buflen,
  883. gpsd_hexdump(session->msgbuf, sizeof(session->msgbuf),
  884. (char *)session->lexer.isgps.buf,
  885. (session->gpsdata.rtcm2.length +
  886. 2) * sizeof(isgps30bits_t)));
  887. session->cycle_end_reliable = true;
  888. return RTCM2_SET;
  889. }
  890. /* *INDENT-OFF* */
  891. static const struct gps_type_t driver_rtcm104v2 = {
  892. .type_name = "RTCM104V2", /* full name of type */
  893. .packet_type = RTCM2_PACKET, /* associated lexer packet type */
  894. .flags = DRIVER_NOFLAGS, /* no rollover or other flags */
  895. .trigger = NULL, /* no recognition string */
  896. .channels = 0, /* not used */
  897. .probe_detect = NULL, /* no probe */
  898. .get_packet = generic_get, /* how to get a packet */
  899. .parse_packet = rtcm104v2_analyze, /* */
  900. .rtcm_writer = NULL, /* don't send RTCM data, */
  901. .init_query = NULL, /* non-perturbing initial query */
  902. .event_hook = NULL, /* no event_hook */
  903. #ifdef RECONFIGURE_ENABLE
  904. .speed_switcher= NULL, /* no speed switcher */
  905. .mode_switcher = NULL, /* no mode switcher */
  906. .rate_switcher = NULL, /* no sample-rate switcher */
  907. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  908. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  909. #endif /* RECONFIGURE_ENABLE */
  910. #ifdef CONTROLSEND_ENABLE
  911. .control_send = NULL, /* how to send control strings */
  912. #endif /* CONTROLSEND_ENABLE */
  913. .time_offset = NULL,
  914. };
  915. /* *INDENT-ON* */
  916. #endif /* RTCM104V2_ENABLE */
  917. #ifdef RTCM104V3_ENABLE
  918. /**************************************************************************
  919. *
  920. * RTCM-104 (v3), used for broadcasting DGPS corrections and by DGPS radios
  921. *
  922. **************************************************************************/
  923. static gps_mask_t rtcm104v3_analyze(struct gps_device_t *session)
  924. {
  925. uint16_t type = getbeu16(session->lexer.inbuffer, 3) >> 4;
  926. GPSD_LOG(LOG_RAW, &session->context->errout, "RTCM 3.x packet %d\n", type);
  927. rtcm3_unpack(session->context,
  928. &session->gpsdata.rtcm3,
  929. (char *)session->lexer.outbuffer);
  930. session->cycle_end_reliable = true;
  931. return RTCM3_SET;
  932. }
  933. /* *INDENT-OFF* */
  934. static const struct gps_type_t driver_rtcm104v3 = {
  935. .type_name = "RTCM104V3", /* full name of type */
  936. .packet_type = RTCM3_PACKET, /* associated lexer packet type */
  937. .flags = DRIVER_NOFLAGS, /* no rollover or other flags */
  938. .trigger = NULL, /* no recognition string */
  939. .channels = 0, /* not used */
  940. .probe_detect = NULL, /* no probe */
  941. .get_packet = generic_get, /* how to get a packet */
  942. .parse_packet = rtcm104v3_analyze, /* */
  943. .rtcm_writer = NULL, /* don't send RTCM data, */
  944. .init_query = NULL, /* non-perturbing initial query */
  945. .event_hook = NULL, /* no event hook */
  946. #ifdef RECONFIGURE_ENABLE
  947. .speed_switcher= NULL, /* no speed switcher */
  948. .mode_switcher = NULL, /* no mode switcher */
  949. .rate_switcher = NULL, /* no sample-rate switcher */
  950. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  951. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  952. #endif /* RECONFIGURE_ENABLE */
  953. #ifdef CONTROLSEND_ENABLE
  954. .control_send = NULL, /* how to send control strings */
  955. #endif /* CONTROLSEND_ENABLE */
  956. .time_offset = NULL,
  957. };
  958. /* *INDENT-ON* */
  959. #endif /* RTCM104V3_ENABLE */
  960. #ifdef GARMINTXT_ENABLE
  961. /**************************************************************************
  962. *
  963. * Garmin Simple Text protocol
  964. *
  965. **************************************************************************/
  966. /* *INDENT-OFF* */
  967. static const struct gps_type_t driver_garmintxt = {
  968. .type_name = "Garmin Simple Text", /* full name of type */
  969. .packet_type = GARMINTXT_PACKET, /* associated lexer packet type */
  970. .flags = DRIVER_NOFLAGS, /* no rollover or other flags */
  971. .trigger = NULL, /* no recognition string */
  972. .channels = 0, /* not used */
  973. .probe_detect = NULL, /* no probe */
  974. .get_packet = generic_get, /* how to get a packet */
  975. .parse_packet = garmintxt_parse, /* how to parse one */
  976. .rtcm_writer = NULL, /* don't send RTCM data, */
  977. .init_query = NULL, /* non-perturbing initial query */
  978. .event_hook = NULL, /* no event hook */
  979. #ifdef RECONFIGURE_ENABLE
  980. .speed_switcher= NULL, /* no speed switcher */
  981. .mode_switcher = NULL, /* no mode switcher */
  982. .rate_switcher = NULL, /* no sample-rate switcher */
  983. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  984. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  985. #endif /* RECONFIGURE_ENABLE */
  986. #ifdef CONTROLSEND_ENABLE
  987. .control_send = NULL, /* how to send control strings */
  988. #endif /* CONTROLSEND_ENABLE */
  989. .time_offset = NULL,
  990. };
  991. /* *INDENT-ON* */
  992. #endif /* GARMINTXT_ENABLE */
  993. #ifdef MTK3301_ENABLE
  994. /**************************************************************************
  995. *
  996. * MediaTek MTK-3301, 3329, 3333, 3339
  997. *
  998. * OEMs for several GPS vendors, notably including Garmin, FasTrax, Trimble,
  999. * and AdaFruit. Website at <http://www.mediatek.com/>.
  1000. *
  1001. * The Trimble Condor appears to be an MTK3329. It behaves as an MTK3301
  1002. * and positively acknowledges all 3301 sentences as valid. It ignores $PMTK
  1003. * sentence fields that are not implemented in the Trimble Condor. It does
  1004. * not have power-save mode and ignores $PMTK320. For $PMTK314 it silently
  1005. * ignores periodic enabling of messages that aren't supported.
  1006. *
  1007. * From its datasheet the MTK3339 seems to add QZSS support not present in
  1008. * earlier versions. The data sheet says it has 66 channels, which mkes
  1009. * sense given the multi-constellation capability. The channel count
  1010. * in the driver is never used by the NMEA driver so leaving the lower MTK3301
  1011. * value in there is OK.
  1012. *
  1013. * MTK3333 support 10Hz.
  1014. *
  1015. * The Adafruit GPS HAT for the Raspberry Pi is an MTK3339. It works with this
  1016. * driver; in fact AdaFruit's overview page for the product features GPSD.
  1017. *
  1018. **************************************************************************/
  1019. static void mtk3301_event_hook(struct gps_device_t *session, event_t event)
  1020. {
  1021. /*
  1022. 0 NMEA_SEN_GLL, GPGLL interval - Geographic Position - Latitude longitude
  1023. 1 NMEA_SEN_RMC, GPRMC interval - Recommended Minimum Specific GNSS Sentence
  1024. 2 NMEA_SEN_VTG, GPVTG interval - Course Over Ground and Ground Speed
  1025. 3 NMEA_SEN_GGA, GPGGA interval - GPS Fix Data
  1026. 4 NMEA_SEN_GSA, GPGSA interval - GNSS DOPS and Active Satellites
  1027. 5 NMEA_SEN_GSV, GPGSV interval - GNSS Satellites in View
  1028. 6 NMEA_SEN_GRS, GPGRS interval - GNSS Range Residuals
  1029. 7 NMEA_SEN_GST, GPGST interval - GNSS Pseudorange Errors Statistics
  1030. 13 NMEA_SEN_MALM, PMTKALM interval - GPS almanac information
  1031. 14 NMEA_SEN_MEPH, PMTKEPH interval - GPS ephemeris information
  1032. 15 NMEA_SEN_MDGP, PMTKDGP interval - GPS differential correction information
  1033. 16 NMEA_SEN_MDBG, PMTKDBG interval – MTK debug information
  1034. 17 NMEA_SEN_ZDA, GPZDA interval - Time & Date
  1035. 18 NMEA_SEN_MCHN, PMTKCHN interval – GPS channel status
  1036. "$PMTK314,1,1,1,1,1,5,1,1,0,0,0,0,0,0,0,0,0,1,0"
  1037. */
  1038. if (session->context->readonly)
  1039. return;
  1040. if (event == event_triggermatch) {
  1041. (void)nmea_send(session, "$PMTK320,0"); /* power save off */
  1042. (void)nmea_send(session, "$PMTK300,1000,0,0,0.0,0.0");/* Fix interval */
  1043. (void)nmea_send(session,
  1044. "$PMTK314,0,1,0,1,1,5,1,1,0,0,0,0,0,0,0,0,0,1,0");
  1045. (void)nmea_send(session, "$PMTK301,2"); /* DGPS is WAAS */
  1046. (void)nmea_send(session, "$PMTK313,1"); /* SBAS enable */
  1047. /* PMTK_API_Q_OUTPUT_CTL - Query PPS pulse width - Trimble only?
  1048. * http://trl.trimble.com/docushare/dsweb/Get/Document-482603/CONDOR_UG_2C_75263-00.pdf *
  1049. * badly documented */
  1050. (void)nmea_send(session, "$PMTK424");
  1051. }
  1052. }
  1053. #ifdef RECONFIGURE_ENABLE
  1054. static bool mtk3301_rate_switcher(struct gps_device_t *session, double rate)
  1055. {
  1056. char buf[78];
  1057. unsigned int milliseconds = (unsigned int)(1000 * rate);
  1058. if (rate > 1) {
  1059. // force no slower than 1Hz
  1060. milliseconds = 1000;
  1061. } else if (rate < 0.1) {
  1062. // force no faster than 10Hz, MTK3333 can do 10Hz
  1063. milliseconds = 100;
  1064. }
  1065. (void)snprintf(buf, sizeof(buf), "$PMTK300,%u,0,0,0,0", milliseconds);
  1066. (void)nmea_send(session, buf); /* Fix interval */
  1067. return true;
  1068. }
  1069. #endif /* RECONFIGURE_ENABLE */
  1070. /* *INDENT-OFF* */
  1071. const struct gps_type_t driver_mtk3301 = {
  1072. .type_name = "MTK-3301", /* full name of type */
  1073. .packet_type = NMEA_PACKET, /* associated lexer packet type */
  1074. .flags = DRIVER_STICKY, /* remember this */
  1075. .trigger = "$PMTK705,", /* firmware release name and version */
  1076. .channels = 12, /* not used by this driver */
  1077. .probe_detect = NULL, /* no probe */
  1078. .get_packet = generic_get, /* how to get a packet */
  1079. .parse_packet = generic_parse_input, /* how to interpret a packet */
  1080. .rtcm_writer = gpsd_write, /* write RTCM data straight */
  1081. .init_query = NULL, /* non-perturbing initial query */
  1082. .event_hook = mtk3301_event_hook, /* lifetime event handler */
  1083. #ifdef RECONFIGURE_ENABLE
  1084. .speed_switcher = NULL, /* no speed switcher */
  1085. .mode_switcher = NULL, /* no mode switcher */
  1086. .rate_switcher = mtk3301_rate_switcher, /* sample rate switcher */
  1087. .min_cycle.tv_sec = 0,
  1088. .min_cycle.tv_nsec = 100000000, /* max 10Hz */
  1089. #endif /* RECONFIGURE_ENABLE */
  1090. #ifdef CONTROLSEND_ENABLE
  1091. .control_send = nmea_write, /* how to send control strings */
  1092. #endif /* CONTROLSEND_ENABLE */
  1093. .time_offset = NULL,
  1094. };
  1095. /* *INDENT-ON* */
  1096. #endif /* MTK3301_ENABLE */
  1097. #ifdef ISYNC_ENABLE
  1098. /**************************************************************************
  1099. *
  1100. * Spectratime LNRCLOK / GRCLOK iSync GPS-disciplined rubidium oscillators
  1101. *
  1102. * These devices comprise a u-blox 6 attached to a separate iSync
  1103. * microcontroller which drives the rubidium oscillator. The iSync
  1104. * microcontroller can be configured to pass through the underlying
  1105. * GPS communication channel, while still using the GPS PPSREF signal
  1106. * to discipline the rubidium oscillator.
  1107. *
  1108. * The iSync can also generate its own periodic status packets in NMEA
  1109. * format. These describe the state of the oscillator (including
  1110. * whether or not the oscillator PPSOUT is synced to the GPS PPSREF).
  1111. * These packets are transmitted in the middle of the underlying GPS
  1112. * packets, forcing us to handle interrupted NMEA packets.
  1113. *
  1114. * The default state of the device is to generate no periodic output.
  1115. * We send a probe string to initiate beating of the iSync-proprietary
  1116. * $PTNTS,B message, which is then detected as a NMEA trigger.
  1117. *
  1118. **************************************************************************/
  1119. static ssize_t isync_write(struct gps_device_t *session, const char *data)
  1120. {
  1121. return gpsd_write(session, data, strlen(data));
  1122. }
  1123. static bool isync_detect(struct gps_device_t *session)
  1124. {
  1125. speed_t old_baudrate;
  1126. char old_parity;
  1127. unsigned int old_stopbits;
  1128. /* Set 9600 8N1 */
  1129. old_baudrate = session->gpsdata.dev.baudrate;
  1130. old_parity = session->gpsdata.dev.parity;
  1131. old_stopbits = session->gpsdata.dev.stopbits;
  1132. gpsd_set_speed(session, 9600, 'N', 1);
  1133. /* Cancel pass-through mode and initiate beating of $PTNTS,B
  1134. * message, which can subsequently be detected as a trigger.
  1135. */
  1136. (void)isync_write(session, "@@@@\r\nMAW0C0B\r\n");
  1137. /* return serial port to original settings */
  1138. gpsd_set_speed(session, old_baudrate, old_parity, old_stopbits);
  1139. return false;
  1140. }
  1141. static void isync_event_hook(struct gps_device_t *session, event_t event)
  1142. {
  1143. if (session->context->readonly)
  1144. return;
  1145. if (event == event_driver_switch) {
  1146. session->lexer.counter = 0;
  1147. }
  1148. if (event == event_configure) {
  1149. switch (session->lexer.counter) {
  1150. case 1:
  1151. /* Configure timing and frequency flags:
  1152. * - Thermal compensation active
  1153. * - PPSREF active
  1154. * - PPSOUT active
  1155. */
  1156. (void)isync_write(session, "MAW040B\r\n");
  1157. /* Configure tracking flags:
  1158. * - Save frequency every 24 hours
  1159. * - Align PPSOUT to PPSINT
  1160. * - Track PPSREF
  1161. */
  1162. (void)isync_write(session, "MAW0513\r\n");
  1163. /* Configure tracking start flags:
  1164. * - Tracking restart allowed
  1165. * - Align to PPSREF
  1166. */
  1167. (void)isync_write(session, "MAW0606\r\n");
  1168. /* Configure tracking window:
  1169. * - 4us
  1170. */
  1171. (void)isync_write(session, "MAW1304\r\n");
  1172. /* Configure alarm window:
  1173. * - 4us
  1174. */
  1175. (void)isync_write(session, "MAW1404\r\n");
  1176. break;
  1177. case 2:
  1178. /* Configure pulse every d second:
  1179. * - pulse every second
  1180. */
  1181. (void)isync_write(session, "MAW1701\r\n");
  1182. /* Configure pulse origin:
  1183. * - zero offset
  1184. */
  1185. (void)isync_write(session, "MAW1800\r\n");
  1186. /* Configure pulse width:
  1187. * - 600ms
  1188. */
  1189. (void)isync_write(session, "MAW1223C34600\r\n");
  1190. break;
  1191. case 3:
  1192. /* Configure GPS resource utilization:
  1193. * - do not consider GPS messages
  1194. */
  1195. (void)isync_write(session, "MAW2200\r\n");
  1196. /* Restart sync */
  1197. (void)isync_write(session, "SY1\r\n");
  1198. /* Restart tracking */
  1199. (void)isync_write(session, "TR1\r\n");
  1200. break;
  1201. case 4:
  1202. /* Cancel BTx messages (if any) */
  1203. (void)isync_write(session, "BT0\r\n");
  1204. /* Configure messages coming out every second:
  1205. * - Oscillator status ($PTNTA) at 750ms
  1206. */
  1207. (void)isync_write(session, "MAW0B00\r\n");
  1208. (void)isync_write(session, "MAW0C0A\r\n");
  1209. break;
  1210. case 5:
  1211. /* Enable GPS passthrough and force u-blox driver to
  1212. * select NMEA mode.
  1213. */
  1214. session->mode = 0;
  1215. session->drivers_identified = 0;
  1216. (void)isync_write(session, "@@@@GPS\r\n");
  1217. break;
  1218. case 6:
  1219. /* Trigger detection of underlying u-blox (if necessary) */
  1220. (void)ubx_write(session, 0x06, 0x00, NULL, 0);
  1221. break;
  1222. }
  1223. }
  1224. }
  1225. /* *INDENT-OFF* */
  1226. const struct gps_type_t driver_isync = {
  1227. .type_name = "iSync", /* full name of type */
  1228. .packet_type = NMEA_PACKET, /* associated lexer packet type */
  1229. .flags = DRIVER_STICKY, /* remember this */
  1230. .trigger = "$PTNTS,B,", /* iSync status message */
  1231. .channels = 50, /* copied from driver_ubx */
  1232. .probe_detect = isync_detect, /* how to detect at startup time */
  1233. .get_packet = generic_get, /* how to get a packet */
  1234. .parse_packet = generic_parse_input, /* how to interpret a packet */
  1235. .init_query = NULL, /* non-perturbing initial query */
  1236. .event_hook = isync_event_hook, /* lifetime event handler */
  1237. #ifdef RECONFIGURE_ENABLE
  1238. .speed_switcher = NULL, /* no speed switcher */
  1239. .mode_switcher = NULL, /* no mode switcher */
  1240. .rate_switcher = NULL, /* no sample-rate switcher */
  1241. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  1242. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  1243. #endif /* RECONFIGURE_ENABLE */
  1244. #ifdef CONTROLSEND_ENABLE
  1245. .control_send = nmea_write, /* how to send control strings */
  1246. #endif /* CONTROLSEND_ENABLE */
  1247. .time_offset = NULL, /* no method for NTP fudge factor */
  1248. };
  1249. /* *INDENT-ON* */
  1250. #endif /* ISYNC_ENABLE */
  1251. #ifdef AIVDM_ENABLE
  1252. /**************************************************************************
  1253. *
  1254. * AIVDM - ASCII armoring of binary AIS packets
  1255. *
  1256. **************************************************************************/
  1257. static bool aivdm_decode(const char *buf, size_t buflen,
  1258. struct gps_device_t *session,
  1259. struct ais_t *ais,
  1260. int debug)
  1261. {
  1262. #ifdef __UNUSED_DEBUG__
  1263. char *sixbits[64] = {
  1264. "000000", "000001", "000010", "000011", "000100",
  1265. "000101", "000110", "000111", "001000", "001001",
  1266. "001010", "001011", "001100", "001101", "001110",
  1267. "001111", "010000", "010001", "010010", "010011",
  1268. "010100", "010101", "010110", "010111", "011000",
  1269. "011001", "011010", "011011", "011100", "011101",
  1270. "011110", "011111", "100000", "100001", "100010",
  1271. "100011", "100100", "100101", "100110", "100111",
  1272. "101000", "101001", "101010", "101011", "101100",
  1273. "101101", "101110", "101111", "110000", "110001",
  1274. "110010", "110011", "110100", "110101", "110110",
  1275. "110111", "111000", "111001", "111010", "111011",
  1276. "111100", "111101", "111110", "111111",
  1277. };
  1278. #endif /* __UNUSED_DEBUG__ */
  1279. int nfrags, ifrag, nfields = 0;
  1280. unsigned char *field[NMEA_MAX*2];
  1281. unsigned char fieldcopy[NMEA_MAX*2+1];
  1282. unsigned char *data, *cp;
  1283. char const *cp1;
  1284. int pad;
  1285. struct aivdm_context_t *ais_context;
  1286. int i;
  1287. if (buflen == 0)
  1288. return false;
  1289. /* we may need to dump the raw packet */
  1290. GPSD_LOG(LOG_PROG, &session->context->errout,
  1291. "AIVDM packet length %zd: %s\n", buflen, buf);
  1292. /* first clear the result, making sure we don't return garbage */
  1293. memset(ais, 0, sizeof(*ais));
  1294. /* discard overlong sentences */
  1295. if (strlen(buf) > sizeof(fieldcopy)-1) {
  1296. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1297. "overlong AIVDM packet.\n");
  1298. return false;
  1299. }
  1300. /* discard sentences with high-half characters in them, they're corrupted */
  1301. for (cp1 = buf; *cp1; cp1++) {
  1302. if (!isascii(*cp1)) {
  1303. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1304. "corrupted AIVDM packet.\n");
  1305. return false;
  1306. }
  1307. }
  1308. /* extract packet fields */
  1309. (void)strlcpy((char *)fieldcopy, buf, sizeof(fieldcopy));
  1310. field[nfields++] = (unsigned char *)buf;
  1311. for (cp = fieldcopy;
  1312. cp < fieldcopy + buflen; cp++)
  1313. {
  1314. if (
  1315. (*cp == (unsigned char)',') ||
  1316. (*cp == (unsigned char)'*')
  1317. ) {
  1318. *cp = '\0';
  1319. field[nfields++] = cp + 1;
  1320. }
  1321. }
  1322. #ifdef __UNDEF_DEBUG_
  1323. for(int i=0;i<nfields;i++)
  1324. GPSD_LOG(LOG_DATA, &session->context->errout,
  1325. "field [%d] [%s]\n", i, field[i]);
  1326. #endif
  1327. /* discard sentences with exiguous commas; catches run-ons */
  1328. if (nfields < 7) {
  1329. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1330. "malformed AIVDM packet.\n");
  1331. return false;
  1332. }
  1333. switch (field[4][0]) {
  1334. case '\0':
  1335. /*
  1336. * Apparently an empty channel is normal for AIVDO sentences,
  1337. * which makes sense as they don't come in over radio. This
  1338. * is going to break if there's ever an AIVDO type 24, though.
  1339. */
  1340. if (!str_starts_with((const char *)field[0], "!AIVDO"))
  1341. GPSD_LOG(LOG_INF, &session->context->errout,
  1342. "invalid empty AIS channel. Assuming 'A'\n");
  1343. ais_context = &session->driver.aivdm.context[0];
  1344. session->driver.aivdm.ais_channel ='A';
  1345. break;
  1346. case '1':
  1347. if (strcmp((char *)field[4], (char *)"12") == 0) {
  1348. GPSD_LOG(LOG_INF, &session->context->errout,
  1349. "ignoring bogus AIS channel '12'.\n");
  1350. return false;
  1351. }
  1352. /* fall through */
  1353. case 'A':
  1354. ais_context = &session->driver.aivdm.context[0];
  1355. session->driver.aivdm.ais_channel ='A';
  1356. break;
  1357. case '2':
  1358. case 'B':
  1359. ais_context = &session->driver.aivdm.context[1];
  1360. session->driver.aivdm.ais_channel ='B';
  1361. break;
  1362. case 'C':
  1363. GPSD_LOG(LOG_INF, &session->context->errout,
  1364. "ignoring AIS channel C (secure AIS).\n");
  1365. return false;
  1366. default:
  1367. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1368. "invalid AIS channel 0x%0X .\n", field[4][0]);
  1369. return false;
  1370. }
  1371. nfrags = atoi((char *)field[1]); /* number of fragments to expect */
  1372. ifrag = atoi((char *)field[2]); /* fragment id */
  1373. data = field[5];
  1374. pad = 0;
  1375. if(isdigit(field[6][0]))
  1376. pad = field[6][0] - '0'; /* number of padding bits ASCII encoded*/
  1377. GPSD_LOG(LOG_PROG, &session->context->errout,
  1378. "nfrags=%d, ifrag=%d, decoded_frags=%d, data=%s, pad=%d\n",
  1379. nfrags, ifrag, ais_context->decoded_frags, data, pad);
  1380. /* assemble the binary data */
  1381. /* check fragment ordering */
  1382. if (ifrag != ais_context->decoded_frags + 1) {
  1383. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1384. "invalid fragment #%d received, expected #%d.\n",
  1385. ifrag, ais_context->decoded_frags + 1);
  1386. if (ifrag != 1)
  1387. return false;
  1388. /* else, ifrag==1: Just discard all that was previously decoded and
  1389. * simply handle that packet */
  1390. ais_context->decoded_frags = 0;
  1391. }
  1392. if (ifrag == 1) {
  1393. (void)memset(ais_context->bits, '\0', sizeof(ais_context->bits));
  1394. ais_context->bitlen = 0;
  1395. }
  1396. /* wacky 6-bit encoding, shades of FIELDATA */
  1397. for (cp = data; cp < data + strlen((char *)data); cp++) {
  1398. unsigned char ch;
  1399. ch = *cp;
  1400. ch -= 48;
  1401. if (ch >= 40)
  1402. ch -= 8;
  1403. #ifdef __UNUSED_DEBUG__
  1404. GPSD_LOG(LOG_RAW, &session->context->errout,
  1405. "%c: %s\n", *cp, sixbits[ch]);
  1406. #endif /* __UNUSED_DEBUG__ */
  1407. for (i = 5; i >= 0; i--) {
  1408. if ((ch >> i) & 0x01) {
  1409. ais_context->bits[ais_context->bitlen / 8] |=
  1410. (1 << (7 - ais_context->bitlen % 8));
  1411. }
  1412. ais_context->bitlen++;
  1413. if (ais_context->bitlen > sizeof(ais_context->bits)) {
  1414. GPSD_LOG(LOG_INF, &session->context->errout,
  1415. "overlong AIVDM payload truncated.\n");
  1416. return false;
  1417. }
  1418. }
  1419. }
  1420. ais_context->bitlen -= pad;
  1421. /* time to pass buffered-up data to where it's actually processed? */
  1422. if (ifrag == nfrags) {
  1423. if (debug >= LOG_INF) {
  1424. size_t clen = BITS_TO_BYTES(ais_context->bitlen);
  1425. GPSD_LOG(LOG_INF, &session->context->errout,
  1426. "AIVDM payload is %zd bits, %zd chars: %s\n",
  1427. ais_context->bitlen, clen,
  1428. gpsd_hexdump(session->msgbuf, sizeof(session->msgbuf),
  1429. (char *)ais_context->bits, clen));
  1430. }
  1431. /* clear waiting fragments count */
  1432. ais_context->decoded_frags = 0;
  1433. /* decode the assembled binary packet */
  1434. return ais_binary_decode(&session->context->errout,
  1435. ais,
  1436. ais_context->bits,
  1437. ais_context->bitlen,
  1438. &ais_context->type24_queue);
  1439. }
  1440. /* we're still waiting on another sentence */
  1441. ais_context->decoded_frags++;
  1442. return false;
  1443. }
  1444. static gps_mask_t aivdm_analyze(struct gps_device_t *session)
  1445. {
  1446. if (session->lexer.type == AIVDM_PACKET) {
  1447. if (aivdm_decode
  1448. ((char *)session->lexer.outbuffer, session->lexer.outbuflen,
  1449. session, &session->gpsdata.ais,
  1450. session->context->errout.debug)) {
  1451. return ONLINE_SET | AIS_SET;
  1452. } else
  1453. return ONLINE_SET;
  1454. #ifdef NMEA0183_ENABLE
  1455. } else if (session->lexer.type == NMEA_PACKET) {
  1456. return nmea_parse((char *)session->lexer.outbuffer, session);
  1457. #endif /* NMEA0183_ENABLE */
  1458. } else
  1459. return 0;
  1460. }
  1461. /* *INDENT-OFF* */
  1462. const struct gps_type_t driver_aivdm = {
  1463. /* Full name of type */
  1464. .type_name = "AIVDM", /* associated lexer packet type */
  1465. .packet_type = AIVDM_PACKET, /* numeric packet type */
  1466. .flags = DRIVER_NOFLAGS, /* no rollover or other flags */
  1467. .trigger = NULL, /* identifying response */
  1468. .channels = 0, /* not used by this driver */
  1469. .probe_detect = NULL, /* no probe */
  1470. .get_packet = generic_get, /* how to get a packet */
  1471. .parse_packet = aivdm_analyze, /* how to analyze a packet */
  1472. .rtcm_writer = NULL, /* don't send RTCM data, */
  1473. .init_query = NULL, /* non-perturbing initial query */
  1474. .event_hook = NULL, /* lifetime event handler */
  1475. #ifdef RECONFIGURE_ENABLE
  1476. .speed_switcher = NULL, /* no speed switcher */
  1477. .mode_switcher = NULL, /* no mode switcher */
  1478. .rate_switcher = NULL, /* no rate switcher */
  1479. .min_cycle.tv_sec = 1, /* max 1Hz */
  1480. .min_cycle.tv_nsec = 0,
  1481. #endif /* RECONFIGURE_ENABLE */
  1482. #ifdef CONTROLSEND_ENABLE
  1483. .control_send = NULL, /* no control sender */
  1484. #endif /* CONTROLSEND_ENABLE */
  1485. .time_offset = NULL, /* no NTP communication */
  1486. };
  1487. /* *INDENT-ON* */
  1488. #endif /* AIVDM_ENABLE */
  1489. #ifdef PASSTHROUGH_ENABLE
  1490. /**************************************************************************
  1491. *
  1492. * JSON passthrough driver
  1493. *
  1494. **************************************************************************/
  1495. static void path_rewrite(struct gps_device_t *session, char *prefix)
  1496. /* prepend the session path to the value of a specified attribute */
  1497. {
  1498. /*
  1499. * Hack the packet to reflect its origin. This code is supposed
  1500. * to insert the path naming the remote gpsd instance into the
  1501. * beginning of the path attribute, followed by a # to separate it
  1502. * from the device.
  1503. */
  1504. char *prefloc;
  1505. assert(prefix != NULL && session->lexer.outbuffer != NULL);
  1506. /* possibly the rewrite has been done already, this comw up in gpsmon */
  1507. if (strstr((char *)session->lexer.outbuffer, session->gpsdata.dev.path) != NULL)
  1508. return;
  1509. for (prefloc = (char *)session->lexer.outbuffer;
  1510. prefloc < (char *)session->lexer.outbuffer+session->lexer.outbuflen;
  1511. prefloc++)
  1512. if (str_starts_with(prefloc, prefix)) {
  1513. char *sfxloc;
  1514. char copy[sizeof(session->lexer.outbuffer)+1];
  1515. (void)strlcpy(copy,
  1516. (char *)session->lexer.outbuffer,
  1517. sizeof(copy));
  1518. prefloc += strlen(prefix);
  1519. (void)strlcpy(prefloc,
  1520. session->gpsdata.dev.path,
  1521. sizeof(session->gpsdata.dev.path));
  1522. if ((sfxloc = strchr(prefloc, '#')))
  1523. *sfxloc = '\0'; /* Avoid accumulating multiple device names */
  1524. (void)strlcat((char *)session->lexer.outbuffer, "#",
  1525. sizeof(session->lexer.outbuffer));
  1526. (void)strlcat((char *)session->lexer.outbuffer,
  1527. copy + (prefloc-(char *)session->lexer.outbuffer),
  1528. sizeof(session->lexer.outbuffer));
  1529. }
  1530. session->lexer.outbuflen = strlen((char *)session->lexer.outbuffer);
  1531. }
  1532. static gps_mask_t json_pass_packet(struct gps_device_t *session)
  1533. {
  1534. GPSD_LOG(LOG_IO, &session->context->errout,
  1535. "<= GPS: %s\n", (char *)session->lexer.outbuffer);
  1536. if (strstr(session->gpsdata.dev.path, ":/") != NULL && strstr(session->gpsdata.dev.path, "localhost") == NULL)
  1537. {
  1538. /* devices and paths need to be edited */
  1539. if (strstr((char *)session->lexer.outbuffer, "DEVICE") != NULL)
  1540. path_rewrite(session, "\"path\":\"");
  1541. path_rewrite(session, "\"device\":\"");
  1542. /* mark certain responses without a path or device attribute */
  1543. if (session->gpsdata.dev.path[0] != '\0') {
  1544. if (strstr((char *)session->lexer.outbuffer, "VERSION") != NULL
  1545. || strstr((char *)session->lexer.outbuffer, "WATCH") != NULL
  1546. || strstr((char *)session->lexer.outbuffer, "DEVICES") != NULL) {
  1547. session->lexer.outbuffer[session->lexer.outbuflen-1] = '\0';
  1548. (void)strlcat((char *)session->lexer.outbuffer, ",\"remote\":\"",
  1549. sizeof(session->lexer.outbuffer));
  1550. (void)strlcat((char *)session->lexer.outbuffer,
  1551. session->gpsdata.dev.path,
  1552. sizeof(session->lexer.outbuffer));
  1553. (void)strlcat((char *)session->lexer.outbuffer, "\"}",
  1554. sizeof(session->lexer.outbuffer));
  1555. }
  1556. session->lexer.outbuflen = strlen((char *)session->lexer.outbuffer);
  1557. }
  1558. }
  1559. GPSD_LOG(LOG_PROG, &session->context->errout,
  1560. "JSON, passing through %s\n",
  1561. (char *)session->lexer.outbuffer);
  1562. return PASSTHROUGH_IS;
  1563. }
  1564. /* *INDENT-OFF* */
  1565. const struct gps_type_t driver_json_passthrough = {
  1566. .type_name = "JSON slave driver", /* full name of type */
  1567. .packet_type = JSON_PACKET, /* associated lexer packet type */
  1568. .flags = DRIVER_NOFLAGS, /* don't remember this */
  1569. .trigger = NULL, /* it's the default */
  1570. .channels = 0, /* not used */
  1571. .probe_detect = NULL, /* no probe */
  1572. .get_packet = generic_get, /* use generic packet getter */
  1573. .parse_packet = json_pass_packet, /* how to interpret a packet */
  1574. .rtcm_writer = NULL, /* write RTCM data straight */
  1575. .init_query = NULL, /* non-perturbing initial query */
  1576. .event_hook = NULL, /* lifetime event handler */
  1577. #ifdef RECONFIGURE_ENABLE
  1578. .speed_switcher = NULL, /* no speed switcher */
  1579. .mode_switcher = NULL, /* no mode switcher */
  1580. .rate_switcher = NULL, /* no sample-rate switcher */
  1581. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  1582. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  1583. #endif /* RECONFIGURE_ENABLE */
  1584. #ifdef CONTROLSEND_ENABLE
  1585. .control_send = NULL, /* how to send control strings */
  1586. #endif /* CONTROLSEND_ENABLE */
  1587. .time_offset = NULL, /* no method for NTP fudge factor */
  1588. };
  1589. /* *INDENT-ON* */
  1590. #endif /* PASSTHROUGH_ENABLE */
  1591. /* *INDENT-OFF* */
  1592. const struct gps_type_t driver_pps = {
  1593. .type_name = "PPS", /* full name of type */
  1594. .packet_type = BAD_PACKET, /* associated lexer packet type */
  1595. .flags = DRIVER_NOFLAGS, /* don't remember this */
  1596. .trigger = NULL, /* it's the default */
  1597. .channels = 0, /* not used */
  1598. .probe_detect = NULL, /* no probe */
  1599. .get_packet = NULL, /* use generic packet getter */
  1600. .parse_packet = NULL, /* how to interpret a packet */
  1601. .rtcm_writer = NULL, /* write RTCM data straight */
  1602. .init_query = NULL, /* non-perturbing initial query */
  1603. .event_hook = NULL, /* lifetime event handler */
  1604. #ifdef RECONFIGURE_ENABLE
  1605. .speed_switcher = NULL, /* no speed switcher */
  1606. .mode_switcher = NULL, /* no mode switcher */
  1607. .rate_switcher = NULL, /* no sample-rate switcher */
  1608. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  1609. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  1610. #endif /* RECONFIGURE_ENABLE */
  1611. #ifdef CONTROLSEND_ENABLE
  1612. .control_send = NULL, /* how to send control strings */
  1613. #endif /* CONTROLSEND_ENABLE */
  1614. .time_offset = NULL, /* no method for NTP fudge factor */
  1615. };
  1616. /* *INDENT-ON* */
  1617. extern const struct gps_type_t driver_evermore;
  1618. extern const struct gps_type_t driver_garmin_ser_binary;
  1619. extern const struct gps_type_t driver_garmin_usb_binary;
  1620. extern const struct gps_type_t driver_geostar;
  1621. extern const struct gps_type_t driver_greis;
  1622. extern const struct gps_type_t driver_italk;
  1623. extern const struct gps_type_t driver_navcom;
  1624. extern const struct gps_type_t driver_nmea2000;
  1625. extern const struct gps_type_t driver_oncore;
  1626. extern const struct gps_type_t driver_sirf;
  1627. extern const struct gps_type_t driver_skytraq;
  1628. extern const struct gps_type_t driver_superstar2;
  1629. extern const struct gps_type_t driver_tsip;
  1630. extern const struct gps_type_t driver_ubx;
  1631. extern const struct gps_type_t driver_zodiac;
  1632. /* the point of this rigamarole is to not have to export a table size */
  1633. static const struct gps_type_t *gpsd_driver_array[] = {
  1634. &driver_unknown,
  1635. #ifdef NMEA0183_ENABLE
  1636. &driver_nmea0183,
  1637. #ifdef ASHTECH_ENABLE
  1638. &driver_ashtech,
  1639. #endif /* ASHTECH_ENABLE */
  1640. #ifdef TRIPMATE_ENABLE
  1641. &driver_tripmate,
  1642. #endif /* TRIPMATE_ENABLE */
  1643. #ifdef EARTHMATE_ENABLE
  1644. &driver_earthmate,
  1645. #endif /* EARTHMATE_ENABLE */
  1646. #ifdef GPSCLOCK_ENABLE
  1647. &driver_gpsclock,
  1648. #endif /* GPSCLOCK_ENABLE */
  1649. #ifdef GARMIN_ENABLE
  1650. &driver_garmin,
  1651. #endif /* GARMIN_ENABLE */
  1652. #ifdef MTK3301_ENABLE
  1653. &driver_mtk3301,
  1654. #endif /* MTK3301_ENABLE */
  1655. #ifdef OCEANSERVER_ENABLE
  1656. &driver_oceanServer,
  1657. #endif /* OCEANSERVER_ENABLE */
  1658. #ifdef FV18_ENABLE
  1659. &driver_fv18,
  1660. #endif /* FV18_ENABLE */
  1661. #ifdef TNT_ENABLE
  1662. &driver_trueNorth,
  1663. #endif /* TNT_ENABLE */
  1664. #ifdef FURY_ENABLE
  1665. &driver_fury,
  1666. #endif /* FURY_ENABLE */
  1667. #ifdef AIVDM_ENABLE
  1668. &driver_aivdm,
  1669. #endif /* AIVDM_ENABLE */
  1670. #endif /* NMEA0183_ENABLE */
  1671. #ifdef EVERMORE_ENABLE
  1672. &driver_evermore,
  1673. #endif /* EVERMORE_ENABLE */
  1674. #ifdef GARMIN_ENABLE
  1675. /* be sure to try Garmin Serial Binary before Garmin USB Binary */
  1676. &driver_garmin_ser_binary,
  1677. &driver_garmin_usb_binary,
  1678. #endif /* GARMIN_ENABLE */
  1679. #ifdef GEOSTAR_ENABLE
  1680. &driver_geostar,
  1681. #endif /* GEOSTAR_ENABLE */
  1682. #ifdef GREIS_ENABLE
  1683. &driver_greis,
  1684. #endif /* GREIS_ENABLE */
  1685. #ifdef ITRAX_ENABLE
  1686. &driver_italk,
  1687. #endif /* ITRAX_ENABLE */
  1688. #ifdef ONCORE_ENABLE
  1689. &driver_oncore,
  1690. #endif /* ONCORE_ENABLE */
  1691. #ifdef NAVCOM_ENABLE
  1692. &driver_navcom,
  1693. #endif /* NAVCOM_ENABLE */
  1694. #ifdef SIRF_ENABLE
  1695. &driver_sirf,
  1696. #endif /* SIRF_ENABLE */
  1697. #ifdef SKYTRAQ_ENABLE
  1698. &driver_skytraq,
  1699. #endif /* SKYTRAQ_ENABLE */
  1700. #ifdef SUPERSTAR2_ENABLE
  1701. &driver_superstar2,
  1702. #endif /* SUPERSTAR2_ENABLE */
  1703. #ifdef TSIP_ENABLE
  1704. &driver_tsip,
  1705. #endif /* TSIP_ENABLE */
  1706. #ifdef ISYNC_ENABLE
  1707. &driver_isync,
  1708. #endif /* ISYNC_ENABLE */
  1709. #ifdef UBLOX_ENABLE
  1710. &driver_ubx,
  1711. #endif /* UBLOX_ENABLE */
  1712. #ifdef ZODIAC_ENABLE
  1713. &driver_zodiac,
  1714. #endif /* ZODIAC_ENABLE */
  1715. #ifdef NMEA2000_ENABLE
  1716. &driver_nmea2000,
  1717. #endif /* NMEA2000_ENABLE */
  1718. #ifdef RTCM104V2_ENABLE
  1719. &driver_rtcm104v2,
  1720. #endif /* RTCM104V2_ENABLE */
  1721. #ifdef RTCM104V3_ENABLE
  1722. &driver_rtcm104v3,
  1723. #endif /* RTCM104V3_ENABLE */
  1724. #ifdef GARMINTXT_ENABLE
  1725. &driver_garmintxt,
  1726. #endif /* GARMINTXT_ENABLE */
  1727. #ifdef PASSTHROUGH_ENABLE
  1728. &driver_json_passthrough,
  1729. #endif /* PASSTHROUGH_ENABLE */
  1730. &driver_pps,
  1731. NULL,
  1732. };
  1733. const struct gps_type_t **gpsd_drivers = &gpsd_driver_array[0];