driver_garmin.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. /*
  2. * This file contains two drivers for Garmin receivers and some code
  3. * shared by both drivers.
  4. *
  5. * One driver "garmin_usb_binary" handles the Garmin binary packet
  6. * format supported by the USB Garmins tested with the Garmin 18 and
  7. * other models. (There is also "garmin_usb_binary_old".) These are ONLY
  8. * for USB devices reporting as: 091e:0003.
  9. *
  10. * The other driver "garmin_ser_binary" is for Garmin receivers via a
  11. * serial port, whether or not one uses a USB/serial adaptor or a real
  12. * serial port. These receivers provide adequate NMEA support, so it
  13. * often makes sense to just put them into NMEA mode.
  14. *
  15. * On Linux, USB Garmins (091e:0003) need the Linux garmin_gps driver and
  16. * will not function without it. On other operating systems, it is clear
  17. * garmin_usb_binary_old does not work since it requires the Linux
  18. * garmin_gps module.
  19. *
  20. * This code has been tested and at least at one time is known to work on
  21. * big- and little-endian CPUs and 32 and 64 bit cpu modes.
  22. *
  23. *
  24. * Documentation for the Garmin protocols can be found via
  25. * http://www.garmin.com/support/commProtocol.html
  26. * The file IOSDK.zip contains IntfSpec.pdf, which describes the
  27. * protocol in terms of Application, Link, and Physical. This
  28. * identical file is also available at:
  29. * http://www.garmin.com/support/pdf/iop_spec.pdf
  30. * An older version of iop_spec.pdf that describes only Serial Binary
  31. * is available at:
  32. * http://vancouver-webpages.com/pub/peter/iop_spec.pdf
  33. * Information about the GPS 18
  34. * http://www.garmin.com/manuals/425_TechnicalSpecification.pdf
  35. *
  36. * There is one physical link protocol for serial which uses DLE/ETX
  37. * framing. There is another physical protocol for USB which relies
  38. * on the packetization intrinsic to USB bulk pipes.
  39. *
  40. * There are several link protocols; all devices implement L000.
  41. * There are then product-specific protocols; most devices implement
  42. * L001. Link protocols are the same and carried over either Physical
  43. * protocol.
  44. *
  45. * Application protocols are named A000 and then with different
  46. * 3-digit numbers. They are carried over Link protocols.
  47. *
  48. * Thus, much of the higher-level code dealing the data formats is
  49. * shared between USB Binary and Serial Binary.
  50. *
  51. * This code is partly from the Garmin IOSDK and partly from the
  52. * sample code in the Linux garmin_gps driver.
  53. *
  54. * bad code by: Gary E. Miller <gem@rellim.com>
  55. *
  56. * -D 3 = packet trace
  57. * -D 4 = packet details
  58. * -D 5 = more packet details
  59. * -D 6 = very excessive details
  60. *
  61. * limitations:
  62. *
  63. * do not have from garmin:
  64. * pdop
  65. * hdop
  66. * vdop
  67. * magnetic variation
  68. *
  69. * known bugs:
  70. * hangs in the fread loop instead of keeping state and returning.
  71. *
  72. * TODO:
  73. *
  74. * ?? Add probe function for Serial Binary to start PVT output.
  75. *
  76. * This file is Copyright (c) 2010-2019 by the GPSD project
  77. * SPDX-License-Identifier: BSD-2-clause
  78. */
  79. #include "gpsd_config.h" /* must be before all includes */
  80. #include <errno.h>
  81. #include <math.h>
  82. #include <stdbool.h>
  83. #include <stdio.h>
  84. #include <string.h>
  85. #include <time.h>
  86. #include <unistd.h>
  87. #if defined(HAVE_LIBUSB)
  88. #include <libusb.h>
  89. #endif
  90. #include "gpsd.h"
  91. #include "bits.h"
  92. #include "timespec.h"
  93. #define GPSD_LE16TOH(x) getles16((char *)(&(x)), 0)
  94. #define GPSD_LE32TOH(x) getles32((char *)(&(x)), 0)
  95. #ifdef GARMIN_ENABLE
  96. #define USE_RMD 0
  97. /* Used in Serial Physical Layer */
  98. #define ETX 0x03
  99. #define ACK 0x06
  100. #define DLE 0x10
  101. #define NAK 0x15
  102. #define GARMIN_LAYERID_TRANSPORT (uint8_t) 0
  103. #define GARMIN_LAYERID_APPL (uint32_t) 20
  104. // Linux Garmin USB driver layer-id to use for some control mechanisms
  105. #define GARMIN_LAYERID_PRIVATE 0x01106E4B
  106. // packet ids used in private layer
  107. #define PRIV_PKTID_SET_DEBUG 1
  108. #define PRIV_PKTID_SET_MODE 2
  109. #define PRIV_PKTID_INFO_REQ 3
  110. #define PRIV_PKTID_INFO_RESP 4
  111. #define PRIV_PKTID_RESET_REQ 5
  112. #define PRIV_PKTID_SET_DEF_MODE 6
  113. #define MODE_NATIVE 0
  114. #define MODE_GARMIN_SERIAL 1
  115. #define GARMIN_PKTID_TRANSPORT_START_SESSION_REQ 5
  116. #define GARMIN_PKTID_TRANSPORT_START_SESSION_RESP 6
  117. #define GARMIN_PKTID_PROTOCOL_ARRAY 253
  118. #define GARMIN_PKTID_PRODUCT_RQST 254
  119. #define GARMIN_PKTID_PRODUCT_DATA 255
  120. /* 0x29 ')' */
  121. #define GARMIN_PKTID_RMD41_DATA 41
  122. /* 0x33 '3' */
  123. #define GARMIN_PKTID_PVT_DATA 51
  124. /* 0x33 '4' */
  125. #define GARMIN_PKTID_RMD_DATA 52
  126. /* 0x72 'r' */
  127. #define GARMIN_PKTID_SAT_DATA 114
  128. #define GARMIN_PKTID_L001_XFER_CMPLT 12
  129. #define GARMIN_PKTID_L001_COMMAND_DATA 10
  130. #define GARMIN_PKTID_L001_DATE_TIME_DATA 14
  131. #define GARMIN_PKTID_L001_RECORDS 27
  132. #define GARMIN_PKTID_L001_WPT_DATA 35
  133. #define CMND_ABORT 0
  134. #define CMND_START_PVT_DATA 49
  135. #define CMND_STOP_PVT_DATA 50
  136. #define CMND_START_RM_DATA 110
  137. #define MAX_BUFFER_SIZE 4096
  138. #define GARMIN_CHANNELS 12
  139. // something magic about 64, garmin driver will not return more than
  140. // 64 at a time. If you read less than 64 bytes the next read will
  141. // just get the last of the 64 byte buffer.
  142. #define ASYNC_DATA_SIZE 64
  143. #pragma pack(1)
  144. // This is the data format of the satellite data from the garmin USB
  145. typedef struct __attribute__((__packed__))
  146. {
  147. uint8_t svid;
  148. uint16_t snr; // 0 - 0xffff
  149. uint8_t elev;
  150. uint16_t azmth;
  151. uint8_t status; // bit 0, has ephemeris, 1, has diff correction
  152. // bit 2 used in solution
  153. // bit 3??
  154. } cpo_sat_data;
  155. /* Garmin D800_Pvt_Datetype_Type */
  156. /* packet type: GARMIN_PKTID_PVT_DATA 52 */
  157. /* This is the data format of the position data from the garmin USB */
  158. typedef struct __attribute__((__packed__))
  159. {
  160. float alt; /* altitude above WGS 84 (meters) */
  161. float epe; /* estimated position error, 2 sigma (meters) */
  162. float eph; /* epe, but horizontal only (meters) */
  163. float epv; /* epe but vertical only (meters ) */
  164. int16_t fix; /* 0 - failed integrity check
  165. * 1 - invalid or unavailable fix
  166. * 2 - 2D
  167. * 3 - 3D
  168. * 4 - 2D Diff
  169. * 5 - 3D Diff
  170. */
  171. double gps_tow; /* gps time of week (seconds) */
  172. double lat; /* ->latitude (radians) */
  173. double lon; /* ->longitude (radians) */
  174. float lon_vel; /* velocity east (meters/second) */
  175. float lat_vel; /* velocity north (meters/second) */
  176. float alt_vel; /* velocity up (meters/sec) */
  177. // Garmin GPS25 uses pkt_id 0x28 and does not output the
  178. // next 3 items
  179. float msl_hght; /* height of WGS 84 above MSL (meters) */
  180. int16_t leap_sec; /* diff between GPS and UTC (seconds) */
  181. int32_t grmn_days; /* days from UTC December 31st, 1989 to the
  182. * beginning of the current week */
  183. } cpo_pvt_data;
  184. typedef struct __attribute__((__packed__))
  185. {
  186. uint32_t cycles;
  187. double pr;
  188. uint16_t phase;
  189. int8_t slp_dtct;
  190. uint8_t snr_dbhz;
  191. uint8_t svid;
  192. int8_t valid;
  193. } cpo_rcv_sv_data;
  194. /* packet type: GARMIN_PKTID_RMD_DATA 53 */
  195. /* seems identical to the packet id 0x29 from the Garmin GPS 25 */
  196. typedef struct __attribute__((__packed__))
  197. {
  198. double rcvr_tow;
  199. int16_t rcvr_wn;
  200. cpo_rcv_sv_data sv[GARMIN_CHANNELS];
  201. } cpo_rcv_data;
  202. // This is the packet format to/from the Garmin USB
  203. typedef struct __attribute__((__packed__))
  204. {
  205. uint8_t mPacketType;
  206. uint8_t mReserved1;
  207. uint16_t mReserved2;
  208. uint16_t mPacketId;
  209. uint16_t mReserved3;
  210. uint32_t mDataSize;
  211. union
  212. {
  213. //int8_t chars[MAX_BUFFER_SIZE];
  214. // cppcheck-suppress unusedStructMember
  215. uint8_t uchars[MAX_BUFFER_SIZE];
  216. cpo_pvt_data pvt;
  217. cpo_sat_data sats;
  218. } mData;
  219. } Packet_t;
  220. // useful funcs to read/write ints
  221. // floats and doubles are Intel (little-endian) order only...
  222. static inline void set_int16(uint8_t * buf, uint32_t value)
  223. {
  224. buf[0] = (uint8_t) (0x0FF & value);
  225. buf[1] = (uint8_t) (0x0FF & (value >> 8));
  226. }
  227. static inline void set_int32(uint8_t * buf, uint32_t value)
  228. {
  229. buf[0] = (uint8_t) (0x0FF & value);
  230. buf[1] = (uint8_t) (0x0FF & (value >> 8));
  231. buf[2] = (uint8_t) (0x0FF & (value >> 16));
  232. buf[3] = (uint8_t) (0x0FF & (value >> 24));
  233. }
  234. static inline uint16_t get_uint16(const uint8_t * buf)
  235. {
  236. return (uint16_t) (0xFF & buf[0])
  237. | ((uint16_t) (0xFF & buf[1]) << 8);
  238. }
  239. #if defined(HAVE_LIBUSB) && defined(__linux__)
  240. static inline uint32_t get_int32(const uint8_t * buf)
  241. {
  242. return (uint32_t) (0xFF & buf[0])
  243. | ((uint32_t) (0xFF & buf[1]) << 8)
  244. | ((uint32_t) (0xFF & buf[2]) << 16)
  245. | ((uint32_t) (0xFF & buf[3]) << 24);
  246. }
  247. #endif /* HAVE_LIBUSB */
  248. // convert radians to degrees
  249. static inline double radtodeg(double rad)
  250. {
  251. return (double)(rad * RAD_2_DEG);
  252. }
  253. static gps_mask_t PrintSERPacket(struct gps_device_t *session,
  254. unsigned char pkt_id, int pkt_len,
  255. unsigned char *buf);
  256. #if defined(HAVE_LIBUSB) && defined(__linux__)
  257. static gps_mask_t PrintUSBPacket(struct gps_device_t *session,
  258. Packet_t * pkt);
  259. #endif /* HAVE_LIBUSB */
  260. gps_mask_t PrintSERPacket(struct gps_device_t *session, unsigned char pkt_id,
  261. int pkt_len, unsigned char *buf)
  262. {
  263. gps_mask_t mask = ONLINE_SET;
  264. int i = 0, j = 0;
  265. uint16_t prod_id = 0;
  266. uint16_t ver = 0;
  267. int maj_ver;
  268. int min_ver;
  269. time_t time_l = 0;
  270. char msg_buf[512] = "";
  271. char *msg = NULL;
  272. cpo_sat_data *sats = NULL;
  273. cpo_pvt_data *pvt = NULL;
  274. cpo_rcv_data *rmd = NULL;
  275. GPSD_LOG(LOG_DATA, &session->context->errout,
  276. "Garmin: PrintSERPacket(, %#02x, %#02x, )\n", pkt_id, pkt_len);
  277. session->cycle_end_reliable = true;
  278. switch (pkt_id) {
  279. case ACK:
  280. GPSD_LOG(LOG_PROG, &session->context->errout, "Garmin: ACK\n");
  281. break;
  282. case NAK:
  283. GPSD_LOG(LOG_PROG, &session->context->errout, "Garmin: NAK\n");
  284. break;
  285. case GARMIN_PKTID_L001_COMMAND_DATA:
  286. prod_id = get_uint16((uint8_t *) buf);
  287. switch (prod_id) {
  288. case CMND_ABORT:
  289. msg = "Abort current xfer";
  290. break;
  291. case CMND_START_PVT_DATA:
  292. msg = "Start Xmit PVT data";
  293. break;
  294. case CMND_STOP_PVT_DATA:
  295. msg = "Stop Xmit PVT data";
  296. break;
  297. case CMND_START_RM_DATA:
  298. msg = "Start RMD data";
  299. break;
  300. default:
  301. (void)snprintf(msg_buf, sizeof(msg_buf), "Unknown: %u",
  302. (unsigned int)prod_id);
  303. msg = msg_buf;
  304. break;
  305. }
  306. GPSD_LOG(LOG_PROG, &session->context->errout,
  307. "Garmin: Appl, Command Data: %s\n", msg);
  308. break;
  309. case GARMIN_PKTID_PRODUCT_RQST:
  310. GPSD_LOG(LOG_PROG, &session->context->errout,
  311. "Garmin: Appl, Product Data req\n");
  312. break;
  313. case GARMIN_PKTID_PRODUCT_DATA:
  314. prod_id = get_uint16((uint8_t *) buf);
  315. ver = get_uint16((uint8_t *) & buf[2]);
  316. maj_ver = (int)(ver / 100);
  317. min_ver = (int)(ver - (maj_ver * 100));
  318. GPSD_LOG(LOG_PROG, &session->context->errout,
  319. "Garmin: Appl, Product Data, sz: %d\n",
  320. pkt_len);
  321. (void)snprintf(session->subtype, sizeof(session->subtype),
  322. "%d: %d.%02d", (int)prod_id, maj_ver, min_ver);
  323. GPSD_LOG(LOG_INF, &session->context->errout,
  324. "Garmin: Product ID: %d, SoftVer: %d.%02d\n",
  325. prod_id, maj_ver, min_ver);
  326. GPSD_LOG(LOG_INF, &session->context->errout,
  327. "Garmin: Product Desc: %s\n", &buf[4]);
  328. mask |= DEVICEID_SET;
  329. GPSD_LOG(LOG_DATA, &session->context->errout,
  330. "Garmin: PRODUCT_DATA: subtype=%s\n",
  331. session->subtype);
  332. break;
  333. case GARMIN_PKTID_PVT_DATA:
  334. GPSD_LOG(LOG_PROG, &session->context->errout,
  335. "Garmin: Appl, PVT Data Sz: %d\n", pkt_len);
  336. pvt = (cpo_pvt_data *) buf;
  337. // 631065600, unix seconds for 31 Dec 1989 Zulu
  338. time_l = (time_t) (631065600 + (GPSD_LE32TOH(pvt->grmn_days) * 86400));
  339. // TODO, convert grmn_days to context->gps_week
  340. time_l -= GPSD_LE16TOH(pvt->leap_sec);
  341. session->context->leap_seconds = (int)GPSD_LE16TOH(pvt->leap_sec);
  342. session->context->valid = LEAP_SECOND_VALID;
  343. // gps_tow is always like x.999 or x.998 just round it to nearest sec
  344. // FIXME! this will break 5Hz garmins...
  345. time_l += (time_t) round(pvt->gps_tow);
  346. DTOTS(&session->context->gps_tow, pvt->gps_tow);
  347. session->newdata.time.tv_sec = time_l;
  348. session->newdata.time.tv_nsec = 0;
  349. GPSD_LOG(LOG_PROG, &session->context->errout,
  350. "Garmin: time_l: %ld\n", (long int)time_l);
  351. session->newdata.latitude = radtodeg(pvt->lat);
  352. session->newdata.longitude = radtodeg(pvt->lon);
  353. // altitude is WGS84
  354. session->newdata.altHAE = pvt->alt;
  355. // geoid separation from WGS 84
  356. // gpsd sign is opposite of garmin sign
  357. session->newdata.geoid_sep = -pvt->msl_hght;
  358. /* Estimated position error in meters. 2 sigma
  359. * We follow the advice at <http://gpsinformation.net/main/errors.htm>.
  360. * Since GPS data is not gaussian, this is marginal advice...
  361. * If this assumption changes here, it should also change in
  362. * nmea_parse.c where we analyze PGRME.
  363. */
  364. session->newdata.sep = pvt->epe * (GPSD_CONFIDENCE / CEP50_SIGMA);
  365. /* eph, horizaontal error, 2 sigma */
  366. session->newdata.eph = pvt->eph * (GPSD_CONFIDENCE / CEP50_SIGMA);
  367. /* eph, horizaontal error, 2 sigma */
  368. session->newdata.epv = pvt->epv * (GPSD_CONFIDENCE / CEP50_SIGMA);
  369. /* meters/sec */
  370. session->newdata.NED.velN = pvt->lat_vel;
  371. session->newdata.NED.velE = pvt->lon_vel;
  372. session->newdata.NED.velD = -pvt->alt_vel;
  373. switch (GPSD_LE16TOH(pvt->fix)) {
  374. case 0:
  375. case 1:
  376. default:
  377. // no fix
  378. session->gpsdata.status = STATUS_NO_FIX;
  379. session->newdata.mode = MODE_NO_FIX;
  380. break;
  381. case 2:
  382. // 2D fix
  383. session->gpsdata.status = STATUS_FIX;
  384. session->newdata.mode = MODE_2D;
  385. break;
  386. case 3:
  387. // 3D fix
  388. session->gpsdata.status = STATUS_FIX;
  389. session->newdata.mode = MODE_3D;
  390. break;
  391. case 4:
  392. // 2D Differential fix
  393. session->gpsdata.status = STATUS_DGPS_FIX;
  394. session->newdata.mode = MODE_2D;
  395. break;
  396. case 5:
  397. // 3D differential fix
  398. session->gpsdata.status = STATUS_DGPS_FIX;
  399. session->newdata.mode = MODE_3D;
  400. break;
  401. }
  402. GPSD_LOG(LOG_PROG, &session->context->errout,
  403. "Garmin: Appl, mode %d, status %d\n",
  404. session->newdata.mode, session->gpsdata.status);
  405. /* save some expensive calculations if not needed */
  406. if (session->context->errout.debug >= LOG_INF) {
  407. GPSD_LOG(LOG_INF, &session->context->errout,
  408. "Garmin: UTC Time: %lld\n",
  409. (long long)session->newdata.time.tv_sec);
  410. GPSD_LOG(LOG_INF, &session->context->errout,
  411. "Garmin: Geoid Separation (MSL-WGS84): from garmin %lf, "
  412. "calculated %lf\n",
  413. -pvt->msl_hght,
  414. wgs84_separation(session->newdata.latitude,
  415. session->newdata.longitude));
  416. GPSD_LOG(LOG_INF, &session->context->errout,
  417. "Garmin: Alt: %.3f, Epe: %.3f, Eph: %.3f, Epv: %.3f, "
  418. "Fix: %d, Gps_tow: %f, Lat: %.3f, Lon: %.3f, "
  419. "LonVel: %.3f, LatVel: %.3f, AltVel: %.3f, MslHgt: %.3f, "
  420. "Leap: %d, GarminDays: %d\n",
  421. pvt->alt, pvt->epe, pvt->eph, pvt->epv,
  422. GPSD_LE16TOH(pvt->fix),
  423. pvt->gps_tow, session->newdata.latitude,
  424. session->newdata.longitude, pvt->lon_vel, pvt->lat_vel,
  425. pvt->alt_vel, pvt->msl_hght, GPSD_LE16TOH(pvt->leap_sec),
  426. GPSD_LE32TOH(pvt->grmn_days));
  427. }
  428. if (session->newdata.mode > MODE_NO_FIX) {
  429. /* data only valid with a fix */
  430. mask |=
  431. TIME_SET | LATLON_SET | ALTITUDE_SET | STATUS_SET | MODE_SET |
  432. HERR_SET | PERR_IS | CLEAR_IS | REPORT_IS | VNED_SET;
  433. /*
  434. * Garmin documentation says we should wait until four good fixes
  435. * have been seen before trying to use the device for precision
  436. * time service.
  437. */
  438. if (session->fixcnt > 3)
  439. mask |= NTPTIME_IS;
  440. }
  441. GPSD_LOG(LOG_DATA, &session->context->errout,
  442. "Garmin: PVT_DATA: time=%lld, lat=%.2f lon=%.2f "
  443. "eph=%.2f sep=%.2f epv=%.2f mode=%d status=%d\n",
  444. (long long)session->newdata.time.tv_sec,
  445. session->newdata.latitude,
  446. session->newdata.longitude,
  447. session->newdata.eph,
  448. session->newdata.sep,
  449. session->newdata.epv,
  450. session->newdata.mode,
  451. session->gpsdata.status);
  452. break;
  453. case GARMIN_PKTID_RMD_DATA:
  454. case GARMIN_PKTID_RMD41_DATA:
  455. rmd = (cpo_rcv_data *) buf;
  456. GPSD_LOG(LOG_DATA, &session->context->errout,
  457. "Garmin: PVT RMD Data Sz: %d\n", pkt_len);
  458. GPSD_LOG(LOG_PROG, &session->context->errout,
  459. "Garmin: PVT RMD rcvr_tow: %f, rcvr_wn: %d\n",
  460. rmd->rcvr_tow, rmd->rcvr_wn);
  461. for (i = 0; i < GARMIN_CHANNELS; i++) {
  462. GPSD_LOG(LOG_INF, &session->context->errout,
  463. "Garmin: PVT RMD Sat: %3u, cycles: %9u, pr: %16.6f, "
  464. "phase: %7.3f, slp_dtct: %3s, snr: %3u, Valid: %3s\n",
  465. (int)rmd->sv[i].svid + 1,
  466. GPSD_LE32TOH(rmd->sv[i].cycles),
  467. rmd->sv[i].pr,
  468. (GPSD_LE16TOH(rmd->sv[i].phase) * 360.0) / 2048.0,
  469. rmd->sv[i].slp_dtct != 0 ? "Yes" : "No",
  470. rmd->sv[i].snr_dbhz,
  471. rmd->sv[i].valid != 0 ? "Yes" : "No");
  472. }
  473. break;
  474. case GARMIN_PKTID_SAT_DATA:
  475. // record ID 0x72 (114)
  476. GPSD_LOG(LOG_PROG, &session->context->errout,
  477. "Garmin: SAT Data Sz: %d\n", pkt_len);
  478. sats = (cpo_sat_data *) buf;
  479. session->gpsdata.satellites_used = 0;
  480. gpsd_zero_satellites(&session->gpsdata);
  481. for (i = 0, j = 0; i < GARMIN_CHANNELS; i++, sats++) {
  482. GPSD_LOG(LOG_INF, &session->context->errout,
  483. "Garmin: Sat %2d, snr: %5u, elev: %2d, Azmth: %3d, "
  484. "Stat: x%x\n",
  485. sats->svid, GPSD_LE16TOH(sats->snr), sats->elev,
  486. GPSD_LE16TOH(sats->azmth),
  487. sats->status);
  488. if (255 == (int)sats->svid) {
  489. // Garmin uses 255 for empty
  490. // gpsd uses 0 for empty
  491. continue;
  492. }
  493. if ((int)sats->svid <= 32) {
  494. /* GPS 1-32 */
  495. session->gpsdata.skyview[j].PRN = (short)sats->svid;
  496. } else {
  497. /* SBAS 33-64 */
  498. session->gpsdata.skyview[j].PRN = (short)sats->svid + 87;
  499. }
  500. session->gpsdata.skyview[j].azimuth =
  501. (short)GPSD_LE16TOH(sats->azmth);
  502. session->gpsdata.skyview[j].elevation = (short)sats->elev;
  503. if (0xffff == sats->snr) {
  504. session->gpsdata.skyview[j].ss = NAN;
  505. } else {
  506. // Garmin does not document this. snr is in dB*100
  507. // Known, but not seen satellites have a dB value of -1*100
  508. session->gpsdata.skyview[j].ss =
  509. (float)(GPSD_LE16TOH(sats->snr) / 100.0);
  510. }
  511. // FIX-ME: Garmin documents this, but Daniel Dorau
  512. // <daniel.dorau@gmx.de> says the behavior on his GPSMap60CSX
  513. // doesn't match it.
  514. if ((uint8_t) 0 != (sats->status & 4)) {
  515. // used in solution?
  516. session->gpsdata.skyview[j].used = true;
  517. session->gpsdata.satellites_used++;
  518. }
  519. session->gpsdata.satellites_visible++;
  520. j++;
  521. }
  522. session->gpsdata.skyview_time.tv_sec = 0;
  523. session->gpsdata.skyview_time.tv_nsec = 0;
  524. mask |= USED_IS | SATELLITE_SET;
  525. GPSD_LOG(LOG_DATA, &session->context->errout,
  526. "Garmin: SAT_DATA: visible=%d used=%d\n",
  527. session->gpsdata.satellites_visible,
  528. session->gpsdata.satellites_used);
  529. break;
  530. case GARMIN_PKTID_PROTOCOL_ARRAY:
  531. // Pid_Protocol_Array, ID 253
  532. // this packet is never requested, it just comes, in some case
  533. // after a GARMIN_PKTID_PRODUCT_RQST
  534. GPSD_LOG(LOG_INF, &session->context->errout,
  535. "Garmin: Appl, Product Capability, sz: %d\n",
  536. pkt_len);
  537. for (i = 0; i < pkt_len; i += 3) {
  538. GPSD_LOG(LOG_INF, &session->context->errout,
  539. "Garmin: %c%03d\n",
  540. buf[i], get_uint16((uint8_t *) & buf[i + 1]));
  541. }
  542. break;
  543. default:
  544. GPSD_LOG(LOG_WARN, &session->context->errout,
  545. "Garmin: Unknown packet id: %#02x, Sz: %#02x\n",
  546. pkt_id, pkt_len);
  547. break;
  548. }
  549. GPSD_LOG(LOG_DATA, &session->context->errout,
  550. "Garmin: PrintSERPacket(, %#02x, %#02x, ) mask=(%s)\n",
  551. pkt_id, pkt_len, gps_maskdump(mask));
  552. return mask;
  553. }
  554. #if defined(HAVE_LIBUSB) && defined(__linux__)
  555. // This works around cppcheck not looking into enough config branches
  556. // cppcheck-suppress unusedFunction
  557. static gps_mask_t PrintUSBPacket(struct gps_device_t *session, Packet_t * pkt)
  558. /* For debugging, decodes and prints some known packets */
  559. {
  560. gps_mask_t mask = 0;
  561. int maj_ver;
  562. int min_ver;
  563. uint32_t mode = 0;
  564. uint16_t prod_id = 0;
  565. uint32_t veri = 0;
  566. uint32_t serial;
  567. uint32_t mDataSize = get_int32((uint8_t *) & pkt->mDataSize);
  568. uint8_t *buffer = (uint8_t *) pkt;
  569. GPSD_LOG(LOG_PROG, &session->context->errout, "Garmin: PrintUSBPacket()\n");
  570. if (DLE == pkt->mPacketType) {
  571. GPSD_LOG(LOG_PROG, &session->context->errout,
  572. "Garmin: really a SER packet!\n");
  573. return PrintSERPacket(session,
  574. (unsigned char)buffer[1],
  575. (int)buffer[2], (unsigned char *)(buffer + 3));
  576. }
  577. if (4096 < mDataSize) {
  578. GPSD_LOG(LOG_WARN, &session->context->errout,
  579. "Garmin: bogus packet, size too large=%d\n",
  580. mDataSize);
  581. return 0;
  582. }
  583. switch (pkt->mPacketType) {
  584. case GARMIN_LAYERID_TRANSPORT:
  585. /* Garmin USB layer specific */
  586. switch (pkt->mPacketId) {
  587. case GARMIN_PKTID_TRANSPORT_START_SESSION_REQ:
  588. // Pid_Start_Session, ID 5
  589. GPSD_LOG(LOG_PROG, &session->context->errout,
  590. "Garmin: Transport, Start Session req\n");
  591. break;
  592. case GARMIN_PKTID_TRANSPORT_START_SESSION_RESP:
  593. // Pid_Session_Started, ID 6
  594. mode = get_int32(&pkt->mData.uchars[0]);
  595. GPSD_LOG(LOG_PROG, &session->context->errout,
  596. "Garmin: Transport, Start Session resp, unit: 0x%x\n",
  597. mode);
  598. break;
  599. default:
  600. GPSD_LOG(LOG_PROG, &session->context->errout,
  601. "Garmin: Transport, Packet: Type %d %d %d, ID: %d,"
  602. "Sz: %d\n",
  603. pkt->mPacketType, pkt->mReserved1, pkt->mReserved2,
  604. pkt->mPacketId, mDataSize);
  605. break;
  606. }
  607. break;
  608. case GARMIN_LAYERID_APPL:
  609. /* raw data transport, shared with Garmin Serial Driver */
  610. mask = PrintSERPacket(session,
  611. (unsigned char)pkt->mPacketId,
  612. (int)mDataSize,
  613. (unsigned char *)pkt->mData.uchars);
  614. break;
  615. case 75:
  616. // private, garmin USB kernel driver specific
  617. switch (pkt->mPacketId) {
  618. case PRIV_PKTID_SET_MODE:
  619. prod_id = get_uint16(&pkt->mData.uchars[0]);
  620. GPSD_LOG(LOG_PROG, &session->context->errout,
  621. "Garmin: Private, Set Mode: %d\n", prod_id);
  622. break;
  623. case PRIV_PKTID_INFO_REQ:
  624. GPSD_LOG(LOG_PROG, &session->context->errout,
  625. "Garmin: Private, ID: Info Req\n");
  626. break;
  627. case PRIV_PKTID_INFO_RESP:
  628. veri = get_int32(pkt->mData.uchars);
  629. maj_ver = (int)(veri >> 16);
  630. min_ver = (int)(veri & 0xffff);
  631. mode = get_int32(&pkt->mData.uchars[4]);
  632. serial = get_int32(&pkt->mData.uchars[8]);
  633. GPSD_LOG(LOG_PROG, &session->context->errout,
  634. "Garmin: Private, ID: Info Resp\n");
  635. GPSD_LOG(LOG_INF, &session->context->errout,
  636. "Garmin: USB Driver found, Version %d.%d, Mode: %d, GPS Serial# %u\n",
  637. maj_ver, min_ver, mode, serial);
  638. break;
  639. default:
  640. GPSD_LOG(LOG_PROG, &session->context->errout,
  641. "Garmin: Private, Packet: ID: %d, Sz: %d\n",
  642. pkt->mPacketId, mDataSize);
  643. break;
  644. }
  645. break;
  646. default:
  647. GPSD_LOG(LOG_PROG, &session->context->errout,
  648. "Garmin: Packet: Type %d %d %d, ID: %d, Sz: %d\n",
  649. pkt->mPacketType, pkt->mReserved1, pkt->mReserved2,
  650. pkt->mPacketId, mDataSize);
  651. break;
  652. }
  653. return mask;
  654. }
  655. #endif /* HAVE_LIBUSB */
  656. #if defined(HAVE_LIBUSB) && defined(__linux__)
  657. /* build and send a packet w/ USB protocol */
  658. static void Build_Send_USB_Packet(struct gps_device_t *session,
  659. uint32_t layer_id, uint32_t pkt_id,
  660. uint32_t length, uint32_t data)
  661. {
  662. uint8_t *buffer = (uint8_t *) session->driver.garmin.Buffer;
  663. Packet_t *thePacket = (Packet_t *) buffer;
  664. ssize_t theBytesReturned = 0;
  665. ssize_t theBytesToWrite = 12 + (ssize_t) length;
  666. set_int32(buffer, layer_id);
  667. set_int32(buffer + 4, pkt_id);
  668. set_int32(buffer + 8, length);
  669. if (2 == length) {
  670. set_int16(buffer + 12, data);
  671. } else if (4 == length) {
  672. set_int32(buffer + 12, data);
  673. }
  674. (void)PrintUSBPacket(session, thePacket);
  675. theBytesReturned = gpsd_write(session, (const char *)thePacket,
  676. (size_t) theBytesToWrite);
  677. GPSD_LOG(LOG_PROG, &session->context->errout,
  678. "Garmin: SendPacket(), wrote %zd bytes\n",
  679. theBytesReturned);
  680. // Garmin says:
  681. // If the packet size was an exact multiple of the USB packet
  682. // size, we must make a final write call with no data
  683. // as a practical matter no known packets are 64 bytes long so
  684. // this is untested
  685. // So here goes just in case
  686. if (0 == (theBytesToWrite % ASYNC_DATA_SIZE)) {
  687. char *n = "";
  688. (void)gpsd_write(session, n, 0);
  689. }
  690. }
  691. #endif /* HAVE_LIBUSB && __linux__ */
  692. /* build and send a packet in serial protocol */
  693. /* layer_id unused */
  694. // FIX-ME: This should go through the common message buffer someday
  695. static void Build_Send_SER_Packet(struct gps_device_t *session,
  696. uint32_t layer_id UNUSED, uint32_t pkt_id,
  697. uint32_t length, uint32_t data)
  698. {
  699. uint8_t *buffer = (uint8_t *) session->driver.garmin.Buffer;
  700. uint8_t *buffer0 = buffer;
  701. Packet_t *thePacket = (Packet_t *) buffer;
  702. ssize_t theBytesReturned = 0;
  703. ssize_t theBytesToWrite = 6 + (ssize_t) length;
  704. uint8_t chksum = 0;
  705. *buffer++ = (uint8_t) DLE;
  706. *buffer++ = (uint8_t) pkt_id;
  707. chksum = pkt_id;
  708. *buffer++ = (uint8_t) length;
  709. chksum += length;
  710. /* ??? What is this doing? */
  711. if (2 == length) {
  712. /* carefull! no DLE stuffing here! */
  713. set_int16(buffer, data);
  714. chksum += buffer[0];
  715. chksum += buffer[1];
  716. } else if (4 == length) {
  717. /* carefull! no DLE stuffing here! */
  718. set_int32(buffer, data);
  719. chksum += buffer[0];
  720. chksum += buffer[1];
  721. chksum += buffer[2];
  722. chksum += buffer[3];
  723. }
  724. /* ??? How is data copied to the buffer? */
  725. buffer += length;
  726. // Add checksum
  727. *buffer++ = -chksum;
  728. if (DLE == -chksum) {
  729. /* stuff another DLE */
  730. *buffer++ = (uint8_t) DLE;
  731. theBytesToWrite++;
  732. }
  733. // Add DLE, ETX
  734. *buffer++ = (uint8_t) DLE;
  735. /* we used to say n++ here, but scan-build complains */
  736. *buffer = (uint8_t) ETX;
  737. (void)PrintSERPacket(session,
  738. (unsigned char)buffer0[1],
  739. (int)buffer0[2], (unsigned char *)(buffer0 + 3));
  740. theBytesReturned = gpsd_write(session, (const char *)thePacket,
  741. (size_t) theBytesToWrite);
  742. GPSD_LOG(LOG_PROG, &session->context->errout,
  743. "Garmin: SendPacket(), wrote %zd bytes\n",
  744. theBytesReturned);
  745. }
  746. #if defined(HAVE_LIBUSB) && defined(__linux__)
  747. /*
  748. * is_usb_device() - is a specified device USB matching given vendor/product?
  749. *
  750. * BUG: Doesn't actually match against path yet. Must finish this function
  751. * by querying /sys/dev/char, either directly or using libudev. Greg KH
  752. * assures this is possible, though he is vague about how.
  753. *
  754. * libudev: http://www.kernel.org/pub/linux/utils/kernel/hotplug/libudev/
  755. */
  756. // This works around cppcheck not looking into enough config branches
  757. // cppcheck-suppress unusedFunction
  758. static bool is_usb_device(const char *path UNUSED, int vendor, int product,
  759. struct gpsd_errout_t *errout)
  760. {
  761. // discover devices
  762. libusb_device **list;
  763. ssize_t cnt;
  764. ssize_t i = 0;
  765. bool found = false;
  766. GPSD_LOG(LOG_INF, errout, "attempting USB device enumeration.\n");
  767. (void)libusb_init(NULL);
  768. if ((cnt = libusb_get_device_list(NULL, &list)) < 0) {
  769. GPSD_LOG(LOG_ERROR, errout, "USB device list call failed.\n");
  770. libusb_exit(NULL);
  771. return false;
  772. }
  773. for (i = 0; i < cnt; i++) {
  774. struct libusb_device_descriptor desc;
  775. libusb_device *dev = list[i];
  776. int r = libusb_get_device_descriptor(dev, &desc);
  777. if (r < 0) {
  778. GPSD_LOG(LOG_ERROR, errout,
  779. "USB descriptor fetch failed on device %zd.\n", i);
  780. continue;
  781. }
  782. /* we can extract device descriptor data */
  783. GPSD_LOG(LOG_INF, errout,
  784. "%04x:%04x (bus %d, device %d)\n",
  785. desc.idVendor, desc.idProduct,
  786. libusb_get_bus_number(dev),
  787. libusb_get_device_address(dev));
  788. /* we match if vendor and product ID are right */
  789. if (desc.idVendor == (uint16_t)vendor && desc.idProduct == (uint16_t)product) {
  790. found = true;
  791. break;
  792. }
  793. }
  794. GPSD_LOG(LOG_INF, errout,
  795. "vendor/product match with %04x:%04x %sfound\n",
  796. vendor, product, found ? "" : "not ");
  797. libusb_free_device_list(list, 1);
  798. libusb_exit(NULL);
  799. return found;
  800. }
  801. #endif /* HAVE_LIBUSB */
  802. /*
  803. * garmin_usb_detect() - detect a Garmin USB device connected to session fd.
  804. *
  805. * This is ONLY for USB devices reporting as: 091e:0003.
  806. *
  807. * This driver ONLY works in Linux and ONLY when the garmin_gps kernel
  808. * module is installed.
  809. *
  810. * This is only necessary because under Linux Garmin USB devices need a
  811. * kernel module rather than being normal USB-serial devices.
  812. *
  813. * The actual wire protocol from the Garmin device is very strange. There
  814. * are no delimiters. End of packet is signaled by a zero-length read
  815. * on the USB device, and start of packet is the next read. You can't just
  816. * ignore the zero reads and pass the data through - you'd never be able
  817. * to tell where the packet boundaries are.
  818. *
  819. * The garmin_usb module's job is to grab the packet and frame it in
  820. * DLEs (with DLE stuffing). This makes the USB packets look as
  821. * though they came from a regular Garmin *serial* device, which is how
  822. * most of the processing for both types can be unified here.
  823. *
  824. * return 1 is device found
  825. * return 0 if not
  826. */
  827. static bool garmin_usb_detect(struct gps_device_t *session UNUSED)
  828. {
  829. #if defined(__linux__)
  830. /*
  831. * Only perform this check if we're looking at a USB-serial
  832. * device. This prevents drivers for attached serial GPSes
  833. * fronm being rudely elbowed aside by this one if they happen
  834. * to be trying to coexist with the Garmin.
  835. */
  836. if (session->sourcetype != source_usb)
  837. return false;
  838. else {
  839. #ifdef HAVE_LIBUSB
  840. if (!is_usb_device(session->gpsdata.dev.path, 0x091e, 0x0003,
  841. &session->context->errout))
  842. return false;
  843. if (!gpsd_set_raw(session)) {
  844. GPSD_LOG(LOG_ERROR, &session->context->errout,
  845. "Garmin: garmin_usb_detect: error changing port attributes: %s\n",
  846. strerror(errno));
  847. return false;
  848. }
  849. if (sizeof(session->driver.garmin.Buffer) < sizeof(Packet_t)) {
  850. /* dunno how this happens, but it does on some compilers */
  851. GPSD_LOG(LOG_ERROR, &session->context->errout,
  852. "Garmin: garmin_usb_detect: Compile error, garmin.Buffer too small.\n");
  853. return false;
  854. }
  855. // FIXME!!! needs to use libusb totally and move garmin_gps aside */
  856. // set Mode 1, mode 0 is broken somewhere past 2.6.14
  857. // but how?
  858. GPSD_LOG(LOG_PROG, &session->context->errout,
  859. "Garmin: Set garmin_gps driver mode = 0\n");
  860. Build_Send_USB_Packet(session, GARMIN_LAYERID_PRIVATE,
  861. PRIV_PKTID_SET_MODE, 4, MODE_GARMIN_SERIAL);
  862. // expect no return packet !?
  863. return true;
  864. #else
  865. return false;
  866. #endif /* HAVE_LIBUSB */
  867. }
  868. #else
  869. return false;
  870. #endif /* __linux__ */
  871. }
  872. static void garmin_event_hook(struct gps_device_t *session, event_t event)
  873. {
  874. if (session->context->readonly)
  875. return;
  876. /*
  877. * FIX-ME: It might not be necessary to call this on reactivate.
  878. * Experiment to see if the holds its settings through a close.
  879. */
  880. if (event == event_identified || event == event_reactivate) {
  881. // Tell the device to send product data
  882. GPSD_LOG(LOG_PROG, &session->context->errout,
  883. "Garmin: Get Product Data\n");
  884. Build_Send_SER_Packet(session, GARMIN_LAYERID_APPL,
  885. GARMIN_PKTID_PRODUCT_RQST, 0, 0);
  886. // turn on PVT data 49
  887. GPSD_LOG(LOG_PROG, &session->context->errout,
  888. "Garmin: Set to send reports every 1 second\n");
  889. Build_Send_SER_Packet(session, GARMIN_LAYERID_APPL,
  890. GARMIN_PKTID_L001_COMMAND_DATA, 2,
  891. CMND_START_PVT_DATA);
  892. #if USE_RMD
  893. // turn on RMD data 110
  894. GPSD_LOG(LOG_PROG, &session->context->errout,
  895. "Garmin: Set to send Raw sat data\n");
  896. Build_Send_SER_Packet(session, GARMIN_LAYERID_APPL,
  897. GARMIN_PKTID_L001_COMMAND_DATA, 2,
  898. CMND_START_RM_DATA);
  899. #endif
  900. }
  901. if (event == event_deactivate)
  902. /* FIX-ME: is any action needed, or is closing the port sufficient? */
  903. GPSD_LOG(LOG_PROG, &session->context->errout,
  904. "Garmin: garmin_close()\n");
  905. }
  906. #define Send_ACK() Build_Send_SER_Packet(session, 0, ACK, 0, 0)
  907. #define Send_NAK() Build_Send_SER_Packet(session, 0, NAK, 0, 0)
  908. gps_mask_t garmin_ser_parse(struct gps_device_t *session)
  909. {
  910. unsigned char *buf = session->lexer.outbuffer;
  911. size_t len = session->lexer.outbuflen;
  912. unsigned char data_buf[MAX_BUFFER_SIZE];
  913. unsigned char c;
  914. int i = 0;
  915. size_t n = 0;
  916. int data_index = 0;
  917. int got_dle = 0;
  918. unsigned char pkt_id = 0;
  919. unsigned char pkt_len = 0;
  920. unsigned char chksum = 0;
  921. gps_mask_t mask = 0;
  922. struct timespec delay;
  923. GPSD_LOG(LOG_RAW, &session->context->errout,
  924. "Garmin: garmin_ser_parse()\n");
  925. if (6 > len) {
  926. /* WTF? */
  927. /* minimum packet; <DLE> [pkt id] [length=0] [chksum] <DLE> <STX> */
  928. Send_NAK();
  929. GPSD_LOG(LOG_RAW, &session->context->errout,
  930. "Garmin: serial too short: %zd\n", len);
  931. return 0;
  932. }
  933. /* debug */
  934. for (i = 0; i < (int)len; i++) {
  935. GPSD_LOG(LOG_RAW, &session->context->errout,
  936. "Garmin: Char: %#02x\n", buf[i]);
  937. }
  938. if ('\x10' != buf[0]) {
  939. Send_NAK();
  940. GPSD_LOG(LOG_RAW, &session->context->errout,
  941. "Garmin: buf[0] not DLE\n");
  942. return 0;
  943. }
  944. n = 1;
  945. pkt_id = buf[n++];
  946. chksum = pkt_id;
  947. if ('\x10' == pkt_id) {
  948. if ('\x10' != buf[n++]) {
  949. Send_NAK();
  950. GPSD_LOG(LOG_RAW, &session->context->errout,
  951. "Garmin: Bad pkt_id %#02x\n", pkt_id);
  952. return 0;
  953. }
  954. }
  955. pkt_len = buf[n++];
  956. chksum += pkt_len;
  957. if ('\x10' == pkt_len) {
  958. if ('\x10' != buf[n++]) {
  959. GPSD_LOG(LOG_RAW, &session->context->errout,
  960. "Garmin: Bad pkt_len %#02x\n", pkt_len);
  961. Send_NAK();
  962. return 0;
  963. }
  964. }
  965. data_index = 0;
  966. for (i = 0; i < 256; i++) {
  967. if ((int)pkt_len == data_index) {
  968. // got it all
  969. break;
  970. }
  971. if (len < n + i) {
  972. GPSD_LOG(LOG_RAW, &session->context->errout,
  973. "Garmin: Packet too short %zd < %zd\n",
  974. len, n + i);
  975. Send_NAK();
  976. return 0;
  977. }
  978. c = buf[n + i];
  979. if (got_dle) {
  980. got_dle = 0;
  981. if ('\x10' != c) {
  982. Send_NAK();
  983. GPSD_LOG(LOG_RAW, &session->context->errout,
  984. "Garmin: Bad DLE %#02x\n", c);
  985. return 0;
  986. }
  987. } else {
  988. chksum += c;
  989. data_buf[data_index++] = c;
  990. if ('\x10' == c) {
  991. got_dle = 1;
  992. }
  993. }
  994. }
  995. /* get checksum */
  996. if (len < n + i) {
  997. Send_NAK();
  998. GPSD_LOG(LOG_RAW, &session->context->errout,
  999. "Garmin: No checksum, Packet too short %zd < %zd\n", len,
  1000. n + i);
  1001. return 0;
  1002. }
  1003. c = buf[n + i++];
  1004. chksum += c;
  1005. /* get final DLE */
  1006. if (len < n + i) {
  1007. Send_NAK();
  1008. GPSD_LOG(LOG_RAW, &session->context->errout,
  1009. "Garmin: No final DLE, Packet too short %zd < %zd\n", len,
  1010. n + i);
  1011. return 0;
  1012. }
  1013. c = buf[n + i++];
  1014. if ('\x10' != c) {
  1015. Send_NAK();
  1016. GPSD_LOG(LOG_RAW, &session->context->errout,
  1017. "Garmin: Final DLE not DLE\n");
  1018. return 0;
  1019. }
  1020. /* get final ETX */
  1021. if (len < n + i) {
  1022. Send_NAK();
  1023. GPSD_LOG(LOG_RAW, &session->context->errout,
  1024. "Garmin: No final ETX, Packet too short %zd < %zd\n", len,
  1025. n + i);
  1026. return 0;
  1027. }
  1028. /* we used to say n++ here, but scan-build complains */
  1029. c = buf[n + i];
  1030. if ('\x03' != c) {
  1031. Send_NAK();
  1032. GPSD_LOG(LOG_RAW, &session->context->errout,
  1033. "Garmin: Final ETX not ETX\n");
  1034. return 0;
  1035. }
  1036. /* debug */
  1037. for (i = 0; i < data_index; i++) {
  1038. GPSD_LOG(LOG_RAW, &session->context->errout,
  1039. "Garmin: Char: %#02x\n", data_buf[i]);
  1040. }
  1041. GPSD_LOG(LOG_DATA, &session->context->errout,
  1042. "Garmin: garmin_ser_parse() Type: %#02x, Len: %#02x, chksum: %#02x\n",
  1043. pkt_id, pkt_len, chksum);
  1044. mask = PrintSERPacket(session, pkt_id, pkt_len, data_buf);
  1045. // sending ACK too soon might hang the session
  1046. // so send ACK last, after a pause
  1047. /* wait 300 uSec */
  1048. delay.tv_sec = 0;
  1049. delay.tv_nsec = 300000L;
  1050. nanosleep(&delay, NULL);
  1051. Send_ACK();
  1052. GPSD_LOG(LOG_DATA, &session->context->errout,
  1053. "Garmin: garmin_ser_parse( )\n");
  1054. return mask;
  1055. }
  1056. #ifdef RECONFIGURE_ENABLE
  1057. static void settle(void)
  1058. {
  1059. struct timespec delay, rem;
  1060. memset(&delay, 0, sizeof(delay));
  1061. delay.tv_sec = 0;
  1062. delay.tv_nsec = 333000000L;
  1063. nanosleep(&delay, &rem);
  1064. }
  1065. static void garmin_switcher(struct gps_device_t *session, int mode)
  1066. {
  1067. if (mode == MODE_NMEA) {
  1068. const unsigned char switcher[] =
  1069. { 0x10, 0x0A, 0x02, 0x26, 0x00, 0xCE, 0x10, 0x03 };
  1070. // Note hard-coded string length in the next line...
  1071. ssize_t status = gpsd_write(session, (char *)switcher,
  1072. sizeof(switcher));
  1073. if (status == (ssize_t)sizeof(switcher)) {
  1074. GPSD_LOG(LOG_PROG, &session->context->errout,
  1075. "Garmin: => GPS: turn off binary %02x %02x %02x... \n",
  1076. switcher[0], switcher[1], switcher[2]);
  1077. } else {
  1078. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1079. "Garmin: => GPS: FAILED\n");
  1080. }
  1081. settle(); // wait 333mS, essential!
  1082. /* once a sec, no binary, no averaging, NMEA 2.3, WAAS */
  1083. (void)nmea_send(session, "$PGRMC1,1,1");
  1084. //(void)nmea_send(fd, "$PGRMC1,1,1,1,,,,2,W,N");
  1085. (void)nmea_send(session, "$PGRMI,,,,,,,R");
  1086. settle(); // wait 333mS, essential!
  1087. } else {
  1088. (void)nmea_send(session, "$PGRMC1,1,2,1,,,,2,W,N");
  1089. (void)nmea_send(session, "$PGRMI,,,,,,,R");
  1090. settle(); // wait 333mS, essential!
  1091. }
  1092. }
  1093. #endif /* RECONFIGURE_ENABLE */
  1094. #ifdef CONTROLSEND_ENABLE
  1095. static ssize_t garmin_control_send(struct gps_device_t *session,
  1096. char *buf, size_t buflen)
  1097. /* not used by the daemon, it's for gpsctl and friends */
  1098. {
  1099. session->msgbuflen = buflen;
  1100. (void)memcpy(session->msgbuf, buf, buflen);
  1101. return gpsd_write(session, session->msgbuf, session->msgbuflen);
  1102. }
  1103. #endif /* CONTROLSEND_ENABLE */
  1104. static double garmin_time_offset(struct gps_device_t *session)
  1105. {
  1106. if (session->sourcetype == source_usb) {
  1107. return 0.035; /* Garmin USB, expect +/- 40mS jitter */
  1108. }
  1109. /* only two sentences ships time */
  1110. /* but the PVT data is always first */
  1111. switch (session->gpsdata.dev.baudrate) {
  1112. case 4800:
  1113. return 0.430; /* TBD */
  1114. case 9600:
  1115. return 0.430; /* tested 12Arp10 */
  1116. case 19200:
  1117. return 0.430; /* TBD */
  1118. case 38400:
  1119. return 0.430; /* TBD */
  1120. }
  1121. return 0.430; /* WTF? WAG */
  1122. }
  1123. /* this is everything we export */
  1124. /* *INDENT-OFF* */
  1125. const struct gps_type_t driver_garmin_usb_binary =
  1126. {
  1127. .type_name = "Garmin USB binary", /* full name of type */
  1128. .packet_type = GARMIN_PACKET, /* associated lexer packet type */
  1129. .flags = DRIVER_STICKY, /* remember this */
  1130. .trigger = NULL, /* no trigger, it has a probe */
  1131. .channels = GARMIN_CHANNELS, /* consumer-grade GPS */
  1132. .probe_detect = garmin_usb_detect,/* how to detect at startup time */
  1133. .get_packet = generic_get, /* how to grab a packet */
  1134. .parse_packet = garmin_ser_parse, /* parse message packets */
  1135. .rtcm_writer = NULL, /* don't send DGPS corrections */
  1136. .init_query = NULL, /* non-perturbing initial query */
  1137. .event_hook = garmin_event_hook,/* lifetime ebent handler */
  1138. #ifdef RECONFIGURE_ENABLE
  1139. .speed_switcher = NULL, /* no speed switcher */
  1140. .mode_switcher = NULL, /* Garmin USB Binary has no NMEA */
  1141. .rate_switcher = NULL, /* no sample-rate switcher */
  1142. .min_cycle.tv_sec = 0,
  1143. .min_cycle.tv_nsec = 10000000, /* 10Hz */
  1144. #endif /* RECONFIGURE_ENABLE */
  1145. #ifdef CONTROLSEND_ENABLE
  1146. .control_send = garmin_control_send, /* send raw bytes */
  1147. #endif /* CONTROLSEND_ENABLE */
  1148. .time_offset = garmin_time_offset,
  1149. };
  1150. /* *INDENT-ON* */
  1151. /* *INDENT-OFF* */
  1152. const struct gps_type_t driver_garmin_ser_binary =
  1153. {
  1154. .type_name = "Garmin Serial binary", /* full name of type */
  1155. .packet_type = GARMIN_PACKET, /* associated lexer packet type */
  1156. .flags = DRIVER_STICKY, /* remember this */
  1157. .trigger = NULL, /* no trigger, it has a probe */
  1158. .channels = GARMIN_CHANNELS, /* consumer-grade GPS */
  1159. .probe_detect = NULL, /* how to detect at startup time */
  1160. .get_packet = generic_get, /* how to grab a packet */
  1161. .parse_packet = garmin_ser_parse, /* parse message packets */
  1162. .rtcm_writer = NULL, /* don't send DGPS corrections */
  1163. .init_query = NULL, /* non-perturbing initial query */
  1164. .event_hook = NULL, /* lifetime event handler */
  1165. #ifdef RECONFIGURE_ENABLE
  1166. .speed_switcher = NULL, /* no speed switcher */
  1167. .mode_switcher = garmin_switcher, /* how to change modes */
  1168. .rate_switcher = NULL, /* no sample-rate switcher */
  1169. .min_cycle.tv_sec = 0,
  1170. .min_cycle.tv_nsec = 10000000, /* 10Hz */
  1171. #endif /* RECONFIGURE_ENABLE */
  1172. #ifdef CONTROLSEND_ENABLE
  1173. .control_send = garmin_control_send, /* send raw bytes */
  1174. #endif /* CONTROLSEND_ENABLE */
  1175. .time_offset = garmin_time_offset,
  1176. };
  1177. /* *INDENT-ON* */
  1178. #endif /* GARMIN_ENABLE */