monitor_garmin.c 8.6 KB

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