monitor_garmin.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Garmin binary object for the GPS packet monitor.
  3. *
  4. * This file is Copyright 2011 by the GPSD project
  5. * SPDX-License-Identifier: BSD-2-clause
  6. */
  7. #include "gpsd_config.h" /* must be before all includes */
  8. #include <assert.h>
  9. #include <math.h>
  10. #include "gpsd.h"
  11. #include "bits.h"
  12. #include "gpsmon.h"
  13. #if defined(GARMIN_ENABLE) && defined(BINARY_ENABLE)
  14. extern const struct gps_type_t driver_garmin_ser_binary;
  15. static WINDOW *miscwin, *mid51win, *mid114win;
  16. #define display (void)mvwprintw
  17. #define GARMIN_CHANNELS 12
  18. #define GPSD_LEU16TOH(x) getleu16((char *)(&(x)), 0)
  19. #define GPSD_LE16TOH(x) getles16((char *)(&(x)), 0)
  20. #define GPSD_LE32TOH(x) getles32((char *)(&(x)), 0)
  21. #pragma pack(1)
  22. /* Satellite Data Record */
  23. typedef struct __attribute__((__packed__))
  24. {
  25. uint8_t svid;
  26. uint16_t snr;
  27. uint8_t elev;
  28. uint16_t azmth;
  29. uint8_t status;
  30. } cpo_sat_data;
  31. /* Position Record */
  32. typedef struct __attribute__((__packed__))
  33. {
  34. float alt;
  35. float epe;
  36. float eph;
  37. float epv;
  38. int16_t fix;
  39. double gps_tow;
  40. double lat;
  41. double lon;
  42. float lon_vel;
  43. float lat_vel;
  44. float alt_vel;
  45. float msl_hght;
  46. int16_t leap_sec;
  47. int32_t grmn_days;
  48. } cpo_pvt_data;
  49. /* Receiver Measurement Record */
  50. typedef struct __attribute__((__packed__))
  51. {
  52. // cppcheck-suppress unusedStructMember
  53. uint32_t cycles;
  54. // cppcheck-suppress unusedStructMember
  55. double pr;
  56. // cppcheck-suppress unusedStructMember
  57. uint16_t phase;
  58. // cppcheck-suppress unusedStructMember
  59. int8_t slp_dtct;
  60. // cppcheck-suppress unusedStructMember
  61. uint8_t snr_dbhz;
  62. uint8_t svid;
  63. // cppcheck-suppress unusedStructMember
  64. int8_t valid;
  65. } cpo_rcv_sv_data;
  66. typedef struct __attribute__((__packed__))
  67. {
  68. // cppcheck-suppress unusedStructMember
  69. double rcvr_tow;
  70. // cppcheck-suppress unusedStructMember
  71. int16_t rcvr_wn;
  72. // cppcheck-suppress unusedStructMember
  73. cpo_rcv_sv_data sv[GARMIN_CHANNELS];
  74. } cpo_rcv_data;
  75. /* description of the status */
  76. static char *fixdesc[] = {
  77. "no fix",
  78. "no fix",
  79. "2D",
  80. "3D",
  81. "2D dif",
  82. "3D dif",
  83. };
  84. /* check range of an unsigned quantity */
  85. #define CHECK_RANGE(vec, i) ((i) < sizeof(vec)/sizeof(vec[0]))
  86. static bool garmin_bin_initialize(void)
  87. {
  88. unsigned int i;
  89. if(serial) {
  90. monitor_complain("Direct mode doesn't supported.");
  91. return false;
  92. }
  93. miscwin = subwin(devicewin, 1, 80, 1, 0);
  94. mid51win = subwin(devicewin, 12, 18, 2, 0);
  95. mid114win = subwin(devicewin, GARMIN_CHANNELS + 3, 23, 2, 18);
  96. if (miscwin == NULL || mid51win == NULL || mid114win == NULL)
  97. return false;
  98. (void)syncok(miscwin, true);
  99. (void)syncok(mid51win, true);
  100. (void)syncok(mid114win, true);
  101. (void)wattrset(miscwin, A_BOLD);
  102. display(miscwin, 0, 0, "Time:");
  103. (void)wattrset(miscwin, A_NORMAL);
  104. (void)wborder(mid51win, 0, 0, 0, 0, 0, 0, 0, 0),
  105. (void)wattrset(mid51win, A_BOLD);
  106. display(mid51win, 0, 4, " Position ");
  107. display(mid51win, 1, 2, "Fix:");
  108. display(mid51win, 2, 2, "Lat:");
  109. (void)mvwaddch(mid51win, 2, 16, ACS_DEGREE);
  110. display(mid51win, 3, 2, "Lon:");
  111. (void)mvwaddch(mid51win, 3, 16, ACS_DEGREE);
  112. display(mid51win, 4, 2, "Alt: m");
  113. display(mid51win, 5, 2, "Speed: m/s");
  114. display(mid51win, 6, 2, "Climb: m/s");
  115. display(mid51win, 7, 2, "Leap: sec");
  116. display(mid51win, 8, 2, "epe: m");
  117. display(mid51win, 9, 2, "eph: m");
  118. display(mid51win, 10, 2, "epv: m");
  119. display(mid51win, 11, 3, " ID 51 (0x33) ");
  120. (void)wattrset(mid51win, A_NORMAL);
  121. (void)wborder(mid114win, 0, 0, 0, 0, 0, 0, 0, 0),
  122. (void)wattrset(mid114win, A_BOLD);
  123. display(mid114win, 1, 1, "Ch PRN Az El SNR ST");
  124. for (i = 0; i < GARMIN_CHANNELS; i++) {
  125. display(mid114win, (int)i + 2, 1, "%2d", i);
  126. }
  127. display(mid114win, 0, 5, " Satellite ");
  128. display(mid114win, 14, 4, " ID 114 (0x72) ");
  129. (void)wattrset(mid114win, A_NORMAL);
  130. /* initialize the GPS context's time fields */
  131. gpsd_time_init(session.context, time(NULL));
  132. return true;
  133. }
  134. static void garmin_bin_update(uint16_t pkt_id, uint32_t pkt_size UNUSED,
  135. unsigned char *pkt_data)
  136. {
  137. int i;
  138. cpo_sat_data *sats = NULL;
  139. cpo_pvt_data *pvt = NULL;
  140. //cpo_rcv_data *rmd = NULL;
  141. char tbuf[JSON_DATE_MAX+1];
  142. switch (pkt_id) {
  143. case 0x29: /* Receiver Measurement Record */
  144. case 0x34:
  145. /* for future use */
  146. //rmd = (cpo_rcv_data *)pkt_data;
  147. monitor_log("RMD 0x%02x=", pkt_id);
  148. break;
  149. case 0x33:
  150. /* Position Record, Mid_51 */
  151. display(miscwin, 0, 6, "%-24s",
  152. timespec_to_iso8601(session.gpsdata.fix.time,
  153. tbuf, sizeof(tbuf)));
  154. pvt = (cpo_pvt_data *)pkt_data;
  155. display(mid51win, 1, 7, "%s",
  156. (CHECK_RANGE(fixdesc, GPSD_LEU16TOH(pvt->fix)) ?
  157. fixdesc[GPSD_LEU16TOH(pvt->fix)] : "unknown"));
  158. display(mid51win, 2, 8, "%3.5f", pvt->lat * RAD_2_DEG);
  159. display(mid51win, 3, 8, "%3.5f", pvt->lon * RAD_2_DEG);
  160. display(mid51win, 4, 8, "%8.2f", pvt->alt + pvt->msl_hght);
  161. display(mid51win, 5, 9, "%5.1f", hypot(pvt->lon_vel, pvt->lat_vel));
  162. display(mid51win, 6, 9, "%5.1f", pvt->alt_vel);
  163. display(mid51win, 7, 8, "%d", (int)GPSD_LE16TOH(pvt->leap_sec));
  164. /* error value is very large when status no fix */
  165. if (GPSD_LEU16TOH(pvt->fix) < 2)
  166. pvt->epe = pvt->eph = pvt->epv = NAN;
  167. display(mid51win, 8, 7, "%6.2f", pvt->epe);
  168. display(mid51win, 9, 7, "%6.2f", pvt->eph);
  169. display(mid51win, 10, 7, "%6.2f", pvt->epv);
  170. monitor_log("PVT 0x%02x=", pkt_id);
  171. break;
  172. case 0x72: /* Satellite Data Record */
  173. sats = (cpo_sat_data *)pkt_data;
  174. for (i = 0; i < GARMIN_CHANNELS; i++, sats++) {
  175. display(mid114win, i + 2, 3, " %3u %3u %2u %4.1f %2x",
  176. sats->svid,
  177. GPSD_LE16TOH(sats->azmth),
  178. sats->elev,
  179. (float)GPSD_LE16TOH(sats->snr) / 100.0,
  180. sats->status);
  181. }
  182. monitor_log("SAT 0x%02x=", pkt_id);
  183. break;
  184. case 0xff: /* Product Data Record */
  185. monitor_log("PDR 0x%02x=", pkt_id);
  186. break;
  187. default:
  188. monitor_log("UNK 0x%02x=", pkt_id);
  189. break;
  190. }
  191. }
  192. static void garmin_bin_ser_update(void)
  193. {
  194. unsigned char *buf;
  195. size_t len;
  196. uint16_t pkt_id;
  197. uint32_t pkt_size;
  198. unsigned char pkt_data[255];
  199. int i, j;
  200. unsigned char c;
  201. unsigned char chksum;
  202. bool pkt_good = false, got_dle = false;
  203. buf = session.lexer.outbuffer;
  204. len = session.lexer.outbuflen;
  205. if (!(buf[0] == (unsigned char)0x10 && /* DLE */
  206. buf[len-2] == (unsigned char)0x10 && /* DLE */
  207. buf[len-1] == (unsigned char)0x03)) /* ETX */
  208. goto end; /* bad pkt */
  209. if (buf[1] == (unsigned char)0x10 && buf[2] != (unsigned char)0x10)
  210. goto end; /* bad pkt */
  211. pkt_id = (uint16_t)buf[1];
  212. if (buf[2] == (unsigned char)0x10 && buf[3] != (unsigned char)0x10)
  213. goto end; /* bad pkt */
  214. pkt_size = (uint32_t)buf[2];
  215. chksum = buf[1] + buf[2]; /* pkt_id + pkt_size */
  216. j = 0;
  217. for (i = 0; i <= 255; i++) {
  218. if (pkt_size == (uint32_t)j)
  219. break;
  220. c = buf[i+3];
  221. if (got_dle) {
  222. got_dle = false;
  223. if (c != (unsigned char)0x10)
  224. goto end; /* bad pkt */
  225. } else {
  226. pkt_data[j++] = c;
  227. chksum += c;
  228. if (c == (unsigned char)0x10)
  229. got_dle = true;
  230. }
  231. }
  232. /* check pkt chksum */
  233. if ((unsigned char)(-chksum) == buf[len-3])
  234. pkt_good = true;
  235. end:
  236. if (pkt_good) {
  237. if(serial)
  238. /* good packet, send ACK */
  239. (void)monitor_control_send(
  240. (unsigned char *)"\x10\x06\x00\xfa\x10\x03", 6);
  241. garmin_bin_update(pkt_id, pkt_size, pkt_data);
  242. } else {
  243. if(serial)
  244. /* bad packet, send NAK */
  245. (void)monitor_control_send(
  246. (unsigned char *)"\x10\x15\x00\xeb\x10\x03", 6);
  247. monitor_log("BAD 0x%02x=", buf[1]);
  248. }
  249. }
  250. static void garmin_bin_wrap(void)
  251. {
  252. (void)delwin(miscwin);
  253. (void)delwin(mid51win);
  254. (void)delwin(mid114win);
  255. }
  256. const struct monitor_object_t garmin_bin_ser_mmt = {
  257. .initialize = garmin_bin_initialize,
  258. .update = garmin_bin_ser_update,
  259. .command = NULL,
  260. .wrap = garmin_bin_wrap,
  261. .min_y = 16,.min_x = 80,
  262. .driver = &driver_garmin_ser_binary,
  263. };
  264. #endif /* defined(GARMIN_ENABLE) && defined(BINARY_ENABLE) */
  265. // vim: set expandtab shiftwidth=4