driver_sirf.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500
  1. /*
  2. * This is the gpsd driver for SiRF GPSes operating in binary mode.
  3. * It also handles early u-bloxes that were SiRF derivatives.
  4. *
  5. * The advantages: Reports climb/sink rate (raw-mode clients won't see this).
  6. * Also, we can flag DGPS satellites used in the skyview when SBAS is in use.
  7. * The disadvantages: Doesn't return PDOP or VDOP, just HDOP.
  8. *
  9. * Chris Kuethe, our SiRF expert, tells us:
  10. *
  11. * "I don't see any indication in any of my material that PDOP, GDOP
  12. * or VDOP are output. There are quantities called Estimated
  13. * {Horizontal Position, Vertical Position, Time, Horizonal Velocity}
  14. * Error, but those are apparently only valid when SiRFDRive is
  15. * active."
  16. *
  17. * "(SiRFdrive is their Dead Reckoning augmented firmware. It
  18. * allows you to feed odometer ticks, gyro and possibly
  19. * accelerometer inputs to the chip to allow it to continue
  20. * to navigate in the absence of satellite information, and
  21. * to improve fixes when you do have satellites.)"
  22. *
  23. * "[When we need RINEX data, we can get it from] SiRF Message #5.
  24. * If it's no longer implemented on your receiver, messages
  25. * 7, 28, 29 and 30 will give you the same information."
  26. *
  27. * There is a known problem with the SiRF IV: it is prone to freeze
  28. * when being switched back to NMEA mode from SiRF binary. The
  29. * failure is randomly flaky, you may get away with several mode
  30. * flips before triggering it. Powering off the device resets and
  31. * unfreezes it. We have tries waiting on command acknowledges as
  32. * the manual advises; this does not fix the problem.
  33. *
  34. * This file is Copyright (c) 2010-2018 by the GPSD project
  35. * SPDX-License-Identifier: BSD-2-clause
  36. */
  37. #include "gpsd_config.h" /* must be before all includes */
  38. #include <ctype.h>
  39. #include <math.h>
  40. #include <stdbool.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <strings.h>
  45. #include <unistd.h>
  46. #include "gpsd.h"
  47. #include "bits.h"
  48. #include "strfuncs.h"
  49. #include "timespec.h"
  50. #if defined(SIRF_ENABLE) && defined(BINARY_ENABLE)
  51. #define HI(n) ((n) >> 8)
  52. #define LO(n) ((n) & 0xff)
  53. /*
  54. * According to the protocol reference, if you don't get ACK/NACK in response
  55. * to a control send withing 6 seconds, you should just retry.
  56. */
  57. #define SIRF_RETRY_TIME 6
  58. /* Poll Software Version MID 132 */
  59. static unsigned char versionprobe[] = {
  60. 0xa0, 0xa2, 0x00, 0x02,
  61. 0x84, /* MID 132 */
  62. 0x00, /* unused */
  63. 0x00, 0x00, 0xb0, 0xb3
  64. };
  65. #ifdef RECONFIGURE_ENABLE
  66. /* Poll Navigation Parameters MID 152
  67. * query for MID 19 */
  68. static unsigned char navparams[] = {
  69. 0xa0, 0xa2, 0x00, 0x02,
  70. 0x98, /* MID 152 */
  71. 0x00,
  72. 0x00, 0x00, 0xb0, 0xb3
  73. };
  74. /* DGPS Source MID 133 */
  75. static unsigned char dgpscontrol[] = {
  76. 0xa0, 0xa2, 0x00, 0x07,
  77. 0x85, /* MID 133 */
  78. 0x01, /* use SBAS */
  79. 0x00, 0x00,
  80. 0x00, 0x00, 0x00,
  81. 0x00, 0x00, 0xb0, 0xb3
  82. };
  83. /* Set SBAS Parameters MID 170 */
  84. static unsigned char sbasparams[] = {
  85. 0xa0, 0xa2, 0x00, 0x06,
  86. 0xaa, /* MID 170 */
  87. 0x00, /* SBAS PRN */
  88. 0x01, /* SBAS Mode */
  89. 0x00, /* Auto PRN */
  90. 0x00, 0x00,
  91. 0x00, 0x00, 0xb0, 0xb3
  92. };
  93. /* Set Message Rate MID 166 */
  94. static unsigned char requestecef[] = {
  95. 0xa0, 0xa2, 0x00, 0x08,
  96. 0xa6, /* MID 166 */
  97. 0x00, /* enable 1 */
  98. 0x02, /* MID 2 */
  99. 0x01, /* once per Sec */
  100. 0x00, 0x00, /* unused */
  101. 0x00, 0x00, /* unused */
  102. 0x00, 0x00, 0xb0, 0xb3
  103. };
  104. /* Set Message Rate MID 166 */
  105. static unsigned char requesttracker[] = {
  106. 0xa0, 0xa2, 0x00, 0x08,
  107. 0xa6, /* MID 166 */
  108. 0x00, /* enable 1 */
  109. 0x04, /* MID 4 */
  110. 0x03, /* every 3 sec */
  111. 0x00, 0x00, /* unused */
  112. 0x00, 0x00, /* unused */
  113. 0x00, 0x00, 0xb0, 0xb3
  114. };
  115. /* disable MID XX */
  116. static unsigned char unsetmidXX[] = {
  117. 0xa0, 0xa2, 0x00, 0x08,
  118. 0xa6, /* MID 166 */
  119. 0x00, /* enable XX */
  120. 0x00, /* MID 0xXX */
  121. 0x00, /* rate: never */
  122. 0x00, 0x00, /* reserved */
  123. 0x00, 0x00, /* reserved */
  124. 0x00, 0x00, 0xb0, 0xb3
  125. };
  126. /* message to enable:
  127. * MID 7 Clock Status
  128. * MID 8 50Bps subframe data
  129. * MID 17 Differential Corrections
  130. * MID 28 Nav Lib Measurement Data
  131. * MID 29 Nav Lib DGPS Data
  132. * MID 30 Nav Lib SV State Data
  133. * MID 31 Nav Lib Initialization data
  134. * at 1Hz rate */
  135. static unsigned char enablesubframe[] = {
  136. 0xa0, 0xa2, 0x00, 0x19,
  137. 0x80, /* MID 128 initialize Data Source */
  138. 0x00, 0x00, 0x00, 0x00, /* EXEF X */
  139. 0x00, 0x00, 0x00, 0x00, /* ECEF Y */
  140. 0x00, 0x00, 0x00, 0x00, /* ECEF Z */
  141. 0x00, 0x00, 0x00, 0x00, /* clock drift */
  142. 0x00, 0x00, 0x00, 0x00, /* time of week */
  143. 0x00, 0x00, /* week number */
  144. 0x0C, /* Chans 1-12 */
  145. /* change the next 0x10 to 0x08
  146. * for factory reset */
  147. /* 0x10 turns on MIDs 7, 8, 17, 28, 29, 30 and 31 */
  148. 0x10,
  149. 0x00, 0x00, 0xb0, 0xb3
  150. };
  151. /* disable subframe data */
  152. static unsigned char disablesubframe[] = {
  153. 0xa0, 0xa2, 0x00, 0x19,
  154. 0x80, /* MID 128 initialize Data Source */
  155. 0x00, 0x00, 0x00, 0x00, /* EXEF X */
  156. 0x00, 0x00, 0x00, 0x00, /* ECEF Y */
  157. 0x00, 0x00, 0x00, 0x00, /* ECEF Z */
  158. 0x00, 0x00, 0x00, 0x00, /* clock drift */
  159. 0x00, 0x00, 0x00, 0x00, /* time of week */
  160. 0x00, 0x00, /* week number */
  161. 0x0C, /* Chans 1-12 */
  162. /* 0x00 turns off MIDs 7, 8, 17, 28, 29, 30 and 31 */
  163. 0x00, /* reset bit map */
  164. 0x00, 0x00, 0xb0, 0xb3
  165. };
  166. /* mode control MID */
  167. static unsigned char modecontrol[] = {
  168. 0xa0, 0xa2, 0x00, 0x0e,
  169. 0x88, /* MID 136 Mode Control */
  170. 0x00, 0x00, /* pad bytes */
  171. 0x00, /* degraded mode off */
  172. 0x00, 0x00, /* pad bytes */
  173. 0x00, 0x00, /* altitude */
  174. 0x00, /* altitude hold auto */
  175. 0x00, /* use last computed alt */
  176. 0x00, /* reserved */
  177. 0x00, /* disable degraded mode */
  178. 0x00, /* disable dead reckoning */
  179. 0x01, /* enable track smoothing */
  180. 0x00, 0x00, 0xb0, 0xb3
  181. };
  182. /* enable 1 PPS Time MID 52 *
  183. * using Set Message Rate MID 166 */
  184. static unsigned char enablemid52[] = {
  185. 0xa0, 0xa2, 0x00, 0x08,
  186. 0xa6, /* MID 166 */
  187. 0x00, /* enable/disable one message */
  188. 0x34, /* MID 52 */
  189. 0x01, /* sent once per second */
  190. 0x00, 0x00, 0x00, 0x00, /* reserved, set to zero */
  191. 0x00, 0xdb, 0xb0, 0xb3
  192. };
  193. #endif /* RECONFIGURE_ENABLE */
  194. static gps_mask_t sirf_msg_debug(struct gps_device_t *,
  195. unsigned char *, size_t);
  196. static gps_mask_t sirf_msg_errors(struct gps_device_t *,
  197. unsigned char *, size_t);
  198. static gps_mask_t sirf_msg_navdata(struct gps_device_t *, unsigned char *,
  199. size_t);
  200. static gps_mask_t sirf_msg_navsol(struct gps_device_t *, unsigned char *,
  201. size_t);
  202. static gps_mask_t sirf_msg_nlmd(struct gps_device_t *, unsigned char *,
  203. size_t);
  204. static gps_mask_t sirf_msg_ppstime(struct gps_device_t *, unsigned char *,
  205. size_t);
  206. static gps_mask_t sirf_msg_nl(struct gps_device_t *, unsigned char *,
  207. size_t);
  208. static gps_mask_t sirf_msg_ee(struct gps_device_t *, unsigned char *,
  209. size_t);
  210. static gps_mask_t sirf_msg_svinfo(struct gps_device_t *, unsigned char *,
  211. size_t);
  212. static gps_mask_t sirf_msg_swversion(struct gps_device_t *, unsigned char *,
  213. size_t);
  214. static gps_mask_t sirf_msg_sysparam(struct gps_device_t *, unsigned char *,
  215. size_t);
  216. static gps_mask_t sirf_msg_dgpsstatus(struct gps_device_t *, unsigned char *,
  217. size_t);
  218. static gps_mask_t sirf_msg_ublox(struct gps_device_t *, unsigned char *,
  219. size_t);
  220. static bool sirf_write(struct gps_device_t *session, unsigned char *msg)
  221. {
  222. unsigned int crc;
  223. size_t i, len;
  224. bool ok;
  225. unsigned int type = (unsigned int)msg[4];
  226. /* do not write if -b (readonly) option set */
  227. if (session->context->readonly)
  228. return true;
  229. /*
  230. * Control strings spaced too closely together confuse the SiRF
  231. * IV. This wasn't an issue on older SiRFs, but they've gone to a
  232. * lower-powered processor that apparently has trouble keeping up.
  233. * Now you have to wait for the ACK, otherwise chaos ensues.
  234. * Add instrumentation to reveal when this may happen.
  235. */
  236. /* can also be false because ACK was received after last send */
  237. if (session->driver.sirf.need_ack > 0) {
  238. GPSD_LOG(LOG_WARN, &session->context->errout,
  239. "SiRF: warning, write of MID %#02x while "
  240. "awaiting ACK for %#02x.\n",
  241. type, session->driver.sirf.need_ack);
  242. }
  243. len = (size_t) ((msg[2] << 8) | msg[3]);
  244. /* calculate CRC */
  245. crc = 0;
  246. /* coverity_submit[tainted_data] */
  247. for (i = 0; i < len; i++)
  248. crc += (int)msg[4 + i];
  249. crc &= 0x7fff;
  250. /* enter CRC after payload */
  251. msg[len + 4] = (unsigned char)((crc & 0xff00) >> 8);
  252. msg[len + 5] = (unsigned char)(crc & 0x00ff);
  253. GPSD_LOG(LOG_PROG, &session->context->errout,
  254. "SiRF: Writing MID %#02x:\n", type);
  255. ok = (gpsd_write(session, (const char *)msg, len + 8) ==
  256. (ssize_t) (len + 8));
  257. session->driver.sirf.need_ack = type;
  258. return ok;
  259. }
  260. #ifdef CONTROLSEND_ENABLE
  261. static ssize_t sirf_control_send(struct gps_device_t *session, char *msg,
  262. size_t len)
  263. {
  264. session->msgbuf[0] = (char)0xa0;
  265. session->msgbuf[1] = (char)0xa2;
  266. session->msgbuf[2] = (len >> 8) & 0xff;
  267. session->msgbuf[3] = len & 0xff;
  268. memcpy(session->msgbuf + 4, msg, len);
  269. session->msgbuf[len + 6] = (char)0xb0;
  270. session->msgbuf[len + 7] = (char)0xb3;
  271. session->msgbuflen = len + 8;
  272. /* *INDENT-OFF* */
  273. return sirf_write(session,
  274. (unsigned char *)session->msgbuf) ? (int)session->msgbuflen : -1;
  275. /* *INDENT-ON* */
  276. }
  277. #endif /* CONTROLSEND_ENABLE */
  278. #ifdef RECONFIGURE_ENABLE
  279. static bool sirfbin_speed(struct gps_device_t *session, speed_t speed, char parity, int stopbits)
  280. /* change speed in binary mode */
  281. {
  282. static unsigned char msg[] = {
  283. 0xa0, 0xa2, 0x00, 0x09,
  284. 0x86, /* byte 4:
  285. * Set Binary Serial Port
  286. * MID 134 */
  287. 0x00, 0x00, 0x12, 0xc0, /* bytes 5-8: 4800 bps */
  288. 0x08, /* byte 9: 8 data bits */
  289. 0x01, /* byte 10: 1 stop bit */
  290. 0x00, /* byte 11: no parity */
  291. 0x00, /* byte 12: reserved pad */
  292. 0x00, 0x00, 0xb0, 0xb3
  293. };
  294. GPSD_LOG(LOG_PROG, &session->context->errout,
  295. "SiRF: sirf_speed(%u,%c,%d)\n",
  296. (unsigned int)speed, parity, stopbits);
  297. if (9600 >= speed) {
  298. GPSD_LOG(LOG_WARN, &session->context->errout,
  299. "SiRF may lag at 9600bps or less.\n");
  300. }
  301. switch (parity) {
  302. case 'E':
  303. case 2:
  304. parity = (char)2;
  305. break;
  306. case 'O':
  307. case 1:
  308. parity = (char)1;
  309. break;
  310. case 'N':
  311. case 0:
  312. default:
  313. parity = (char)0;
  314. break;
  315. }
  316. msg[5] = (unsigned char)((speed >> 24) & 0xff);
  317. msg[6] = (unsigned char)((speed >> 16) & 0xff);
  318. msg[7] = (unsigned char)((speed >> 8) & 0xff);
  319. msg[8] = (unsigned char)(speed & 0xff);
  320. msg[10] = (unsigned char)stopbits;
  321. msg[11] = (unsigned char)parity;
  322. return (sirf_write(session, msg));
  323. }
  324. /* switch from binary to NMEA at specified baud */
  325. /* FIXME: does not seem to work... */
  326. static bool sirf_to_nmea(struct gps_device_t *session, speed_t speed)
  327. {
  328. static unsigned char msg[] = { 0xa0, 0xa2, 0x00, 0x18,
  329. 0x81, 0x02,
  330. 0x01, 0x01, /* GGA */
  331. 0x00, 0x00, /* suppress GLL */
  332. 0x01, 0x01, /* GSA */
  333. 0x05, 0x01, /* GSV */
  334. 0x01, 0x01, /* RMC */
  335. 0x00, 0x00, /* suppress VTG */
  336. 0x00, 0x01, /* suppress MSS */
  337. 0x00, 0x01, /* suppress EPE */
  338. 0x00, 0x01, /* suppress EPE */
  339. 0x00, 0x01, /* suppress ZDA */
  340. 0x00, 0x00, /* unused */
  341. 0x12, 0xc0, /* 4800 bps */
  342. 0xb0, 0xb3
  343. };
  344. if (speed >= 0xffff) {
  345. GPSD_LOG(LOG_ERROR, &session->context->errout,
  346. "SiRF: can't switch from SiRF to NMEA because "
  347. " current speed %u is big.",
  348. (unsigned int)speed);
  349. return false;
  350. }
  351. /* stop binary initialization */
  352. session->cfg_stage = UINT_MAX;
  353. msg[26] = (unsigned char)HI(speed);
  354. msg[27] = (unsigned char)LO(speed);
  355. return sirf_write(session, msg);
  356. }
  357. static void sirfbin_mode(struct gps_device_t *session, int mode)
  358. {
  359. if (mode == MODE_NMEA) {
  360. (void)sirf_to_nmea(session, session->gpsdata.dev.baudrate);
  361. } else if (mode == MODE_BINARY) {
  362. char parity = '0';
  363. switch (session->gpsdata.dev.parity) {
  364. default:
  365. case 'N':
  366. parity = '0';
  367. break;
  368. case 'O':
  369. parity = '1';
  370. break;
  371. case 'E':
  372. parity = '2';
  373. break;
  374. }
  375. // gpsd only supports 8[NO]1 or 7[EO]2
  376. // thus the strange use of stopbits
  377. (void)nmea_send(session,
  378. "$PSRF100,0,%d,%d,%d,%c",
  379. session->gpsdata.dev.baudrate,
  380. 9 - session->gpsdata.dev.stopbits,
  381. session->gpsdata.dev.stopbits, parity);
  382. /* reset binary init steps */
  383. session->cfg_stage = 0;
  384. session->cfg_step = 0;
  385. }
  386. }
  387. #endif /* RECONFIGURE_ENABLE */
  388. /* Debug messages MID 255 (0xff) */
  389. static gps_mask_t sirf_msg_debug(struct gps_device_t *device,
  390. unsigned char *buf, size_t len)
  391. {
  392. char msgbuf[MAX_PACKET_LENGTH * 3 + 2];
  393. int i;
  394. memset(msgbuf, 0, (int)sizeof(msgbuf));
  395. /* FIXME: always/only ID 255 */
  396. if (0xe1 == buf[0]) { /* Development statistics messages */
  397. if (2 > len) {
  398. /* too short */
  399. return 0;
  400. }
  401. for (i = 2; i < (int)len; i++)
  402. str_appendf(msgbuf, sizeof(msgbuf), "%c", buf[i] ^ 0xff);
  403. GPSD_LOG(LOG_PROG, &device->context->errout,
  404. "SiRF: MID 0xe1 (225) SID %#0x %s\n", buf[1], msgbuf);
  405. } else if (0xff == (unsigned char)buf[0]) { /* Debug messages */
  406. for (i = 1; i < (int)len; i++)
  407. if (isprint(buf[i]))
  408. str_appendf(msgbuf, sizeof(msgbuf), "%c", buf[i]);
  409. else
  410. str_appendf(msgbuf, sizeof(msgbuf),
  411. "\\x%02x", (unsigned int)buf[i]);
  412. GPSD_LOG(LOG_PROG, &device->context->errout,
  413. "SiRF: DBG 0xff: %s\n", msgbuf);
  414. }
  415. return 0;
  416. }
  417. /* decode Error ID Data MID 10 (0x0a) */
  418. static gps_mask_t sirf_msg_errors(struct gps_device_t *device,
  419. unsigned char *buf,
  420. size_t len UNUSED)
  421. {
  422. /* FIXME: decode count: bytes 4 and 5 */
  423. switch (getbeu16(buf, 1)) {
  424. case 2:
  425. /* ErrId_CS_SVParity */
  426. GPSD_LOG(LOG_PROG, &device->context->errout,
  427. "SiRF: EID 0x0a type 2: Subframe %u error on PRN %u\n",
  428. getbeu32(buf, 9), getbeu32(buf, 5));
  429. break;
  430. case 4107:
  431. GPSD_LOG(LOG_PROG, &device->context->errout,
  432. "SiRF: EID 0x0a type 4107: neither KF nor LSQ fix.\n");
  433. break;
  434. default:
  435. GPSD_LOG(LOG_PROG, &device->context->errout,
  436. "SiRF: EID 0x0a: Error MID %d\n",
  437. getbeu16(buf, 1));
  438. break;
  439. }
  440. return 0;
  441. }
  442. /* Navigation Library Measurement Data MID 28 (0x1c) */
  443. static gps_mask_t sirf_msg_nlmd(struct gps_device_t *session,
  444. unsigned char *buf UNUSED, size_t len)
  445. {
  446. double gps_tow = 0.0;
  447. if (len != 56)
  448. return 0;
  449. /* oh barf, SiRF claims to be IEEE754 but supports two
  450. * different double orders, neither IEEE754 */
  451. /* FIXME - decode the time, since this is the first MID with a
  452. * good time stamp this will be good for ntpshm time */
  453. GPSD_LOG(LOG_PROG, &session->context->errout,
  454. "SiRF: MID 0x1c, NLMD, gps_tow: %f\n",
  455. (double)gps_tow);
  456. return 0;
  457. }
  458. /* MID_SiRFNavNotification MID 51 (0x33) */
  459. static gps_mask_t sirf_msg_navnot(struct gps_device_t *session,
  460. unsigned char *buf, size_t len)
  461. {
  462. const char *definition = "Unknown";
  463. gps_mask_t mask = 0;
  464. if (len < 3)
  465. return 0;
  466. switch (buf[1]) {
  467. case 1:
  468. /* last message sent every cycle */
  469. definition = "SID_GPS_SIRFNAV_COMPLETE";
  470. /* so push a report now */
  471. mask = REPORT_IS;
  472. break;
  473. case 2:
  474. definition = "SID_GPS_SIRFNAV_TIMING";
  475. break;
  476. case 3:
  477. definition = "SID_GPS_DEMO_TIMING";
  478. break;
  479. case 4:
  480. definition = "SID_GPS_SIRFNAV_TIME_TAGS";
  481. break;
  482. case 5:
  483. definition = "SID_GPS_NAV_IS801_PSEUDORANGE_DATA";
  484. break;
  485. case 6:
  486. definition = "GPS_TRACKER_LOADER_STATE";
  487. break;
  488. case 7:
  489. definition = "SSB_SIRFNAV_START";
  490. break;
  491. case 8:
  492. definition = "SSB_SIRFNAV_STOP";
  493. break;
  494. case 9:
  495. definition = "SSB_RESULT";
  496. break;
  497. case 16:
  498. definition = "DEMO_TEST_STATUS";
  499. break;
  500. case 17:
  501. definition = "DEMO_TEST_STATE";
  502. break;
  503. case 18:
  504. definition = "DEMO_TEST_DATA";
  505. break;
  506. case 19:
  507. definition = "DEMO_TEST_STATS";
  508. break;
  509. case 20:
  510. definition = "DEMO_TEST_ERROR";
  511. break;
  512. default:
  513. definition = "Unknown";
  514. break;
  515. }
  516. GPSD_LOG(LOG_PROG, &session->context->errout,
  517. "SiRF IV: NavNotification 51 (0x33), SID: %d (%s), len %lu\n",
  518. buf[1], definition, (long unsigned)len);
  519. return mask;
  520. }
  521. /* Multiconstellation Navigation Data response MID 67,1 (0x43)
  522. * SIRF_MSG_SSB_GNSS_NAV_DATA
  523. * this replaces the deprecated MID 41 */
  524. static gps_mask_t sirf_msg_67_1(struct gps_device_t *session,
  525. unsigned char *buf, size_t len)
  526. {
  527. gps_mask_t mask = 0;
  528. uint32_t solution_validity;
  529. uint32_t solution_info;
  530. uint32_t gps_tow = 0;
  531. uint32_t msecs; /* tow in ms */
  532. uint32_t gps_tow_sub_ms = 0;
  533. uint16_t gps_week = 0;
  534. timespec_t gps_tow_ns = {0};
  535. timespec_t now = {0};
  536. int16_t time_bias = 0;
  537. uint8_t time_accuracy = 0;
  538. uint8_t time_source = 0;
  539. struct tm unpacked_date;
  540. unsigned char datum;
  541. int64_t clk_bias;
  542. uint32_t clk_bias_error;
  543. int32_t clk_offset;
  544. uint32_t clk_offset_error;
  545. int16_t heading_rate; /* rate of change cog deg/s * 100 */
  546. uint32_t distance_travel; /* distance traveled m * 100 */
  547. uint16_t distance_travel_error; /* distance traveled error in m * 100 */
  548. uint32_t ehpe; /* Est horizontal position error * 100 */
  549. unsigned char num_svs_in_sol; /* Num of satellites used in solution */
  550. uint32_t sv_list_1;
  551. uint32_t sv_list_2;
  552. uint32_t sv_list_3;
  553. uint32_t sv_list_4;
  554. uint32_t sv_list_5;
  555. uint32_t additional_info;
  556. int debug_base = LOG_PROG;
  557. char ts_buf[TIMESPEC_LEN];
  558. if (len < 126)
  559. return 0;
  560. GPSD_LOG(LOG_PROG, &session->context->errout,
  561. "SiRF V: MID 67,1 Multiconstellation Navigation Data Response \n");
  562. solution_validity = getbeu32(buf, 2);
  563. if (0 != solution_validity) {
  564. /* invalid fix, just give up */
  565. return 0;
  566. }
  567. solution_info = getbeu32(buf, 6);
  568. gps_week = getbeu16(buf, 10);
  569. msecs = getbeu32(buf, 12);
  570. gps_tow = msecs / 1000;
  571. gps_tow_ns.tv_sec = gps_tow;
  572. /* get ms part */
  573. gps_tow_sub_ms = msecs % 1000;
  574. /* add in the ns */
  575. gps_tow_ns.tv_nsec = (gps_tow_sub_ms * 1000000L) + getbeu32(buf, 16);
  576. now = gpsd_gpstime_resolv(session, gps_week, gps_tow_ns);
  577. /* we'll not use this time, instead the unpacked date below,
  578. * to get the right epoch */
  579. time_bias = getbes16(buf, 20); /* add in the ns */
  580. /* time_accuracy is an odd 8 bit float */
  581. time_accuracy = getub(buf, 22);
  582. time_source = getub(buf, 23); /* unused */
  583. memset(&unpacked_date, 0, sizeof(unpacked_date));
  584. unpacked_date.tm_year = (int)getbeu16(buf, 24) - 1900;
  585. unpacked_date.tm_mon = (int)getub(buf, 26) - 1;
  586. unpacked_date.tm_mday = (int)getub(buf, 27);
  587. unpacked_date.tm_hour = (int)getub(buf, 28);
  588. unpacked_date.tm_min = (int)getub(buf, 29);
  589. unpacked_date.tm_sec = (int)getbeu16(buf, 30) / 1000;
  590. session->context->leap_seconds = (int)getub(buf, 32);
  591. session->context->valid |= LEAP_SECOND_VALID;
  592. session->newdata.time.tv_sec = mkgmtime(&unpacked_date);
  593. session->newdata.time.tv_nsec = gps_tow_ns.tv_nsec;
  594. /* got time now */
  595. mask |= TIME_SET;
  596. datum = getub(buf, 33);
  597. datum_code_string(datum, session->newdata.datum,
  598. sizeof(session->newdata.datum));
  599. clk_bias = getbes64(buf, 34) / 100.0;
  600. clk_bias_error = getbeu32(buf, 42) / 100.0;
  601. clk_offset = getbes32(buf, 46) / 100.0;
  602. clk_offset_error = getbeu32(buf, 50) / 100.0;
  603. session->newdata.latitude = getbes32(buf, 54) * 1e-7;
  604. session->newdata.longitude = getbes32(buf, 58) * 1e-7;
  605. /* altitude WGS84 */
  606. session->newdata.altHAE = getbes32(buf, 62) * 1e-2;
  607. /* altitude MSL */
  608. session->newdata.altMSL = getbes32(buf, 66) * 1e-2;
  609. /* Let gpsd_error_model() deal with geoid_sep */
  610. mask |= LATLON_SET;
  611. switch (solution_info & 0x07) {
  612. case 0: /* no fix */
  613. session->newdata.mode = MODE_NO_FIX;
  614. break;
  615. case 1: /* unused */
  616. session->newdata.mode = MODE_NO_FIX;
  617. break;
  618. case 2: /* unused */
  619. session->newdata.mode = MODE_NO_FIX;
  620. break;
  621. case 3: /* 3-SV KF Solution */
  622. session->newdata.mode = MODE_2D;
  623. break;
  624. case 4: /* Four or more SV KF Solution */
  625. session->newdata.mode = MODE_3D;
  626. break;
  627. case 5: /* 2-D Least-squares Solution */
  628. session->newdata.mode = MODE_2D;
  629. break;
  630. case 6: /* 3-D Least-squaresSolution */
  631. session->newdata.mode = MODE_3D;
  632. break;
  633. case 7: /* DR solution, assume 3D */
  634. session->newdata.mode = MODE_3D;
  635. break;
  636. default: /* can't really happen */
  637. session->newdata.mode = MODE_NO_FIX;
  638. break;
  639. }
  640. mask |= MODE_SET;
  641. if (!(solution_info & 0x01000)) {
  642. /* sog - speed over ground m/s * 100 */
  643. session->newdata.speed = getbeu16(buf, 70) / 100.0;
  644. mask |= SPEED_SET;
  645. }
  646. /* cog - course over ground fm true north deg * 100 */
  647. session->newdata.track = getbeu16(buf, 72) / 100.0;
  648. mask |= TRACK_SET;
  649. /* climb_rate - vertical velocity m/s * 100 */
  650. session->newdata.climb = getbes16(buf, 74) / 100.0;
  651. if (session->newdata.mode == MODE_3D)
  652. mask |= ALTITUDE_SET | CLIMB_SET;
  653. heading_rate = getbes16(buf, 76); /* rate of change cog deg/s * 100 */
  654. distance_travel = getbeu32(buf, 78); /* distance traveled m * 100 */
  655. /* heading_error error of cog deg * 100 */
  656. session->newdata.epd = getbeu16(buf, 82) / 100.0;
  657. /* distance traveled error in m * 100 */
  658. distance_travel_error = getbeu16(buf, 84) / 100.0;
  659. ehpe = getbeu32(buf, 86); /* Estimated horizontal position error * 100 */
  660. /* Estimated vertical position error * 100 */
  661. session->newdata.epv = getbeu32(buf, 90) / 100.0;
  662. /* Estimated horizontal velocity error * 100 */
  663. session->newdata.eps = getbeu16(buf, 94) / 100.0;
  664. mask |= SPEEDERR_SET;
  665. session->gpsdata.dop.gdop = (int)getub(buf, 96) / 5.0;
  666. session->gpsdata.dop.pdop = (int)getub(buf, 97) / 5.0;
  667. session->gpsdata.dop.hdop = (int)getub(buf, 98) / 5.0;
  668. session->gpsdata.dop.vdop = (int)getub(buf, 99) / 5.0;
  669. session->gpsdata.dop.tdop = (int)getub(buf, 100) / 5.0;
  670. mask |= DOP_SET;
  671. num_svs_in_sol = getub(buf, 101);
  672. sv_list_1 = getbeu32(buf, 102);
  673. sv_list_2 = getbeu32(buf, 106);
  674. sv_list_3 = getbeu32(buf, 110);
  675. sv_list_4 = getbeu32(buf, 114);
  676. sv_list_5 = getbeu32(buf, 118);
  677. additional_info = getbeu32(buf, 122);
  678. mask |= REPORT_IS; /* send it */
  679. /* skip all the debug pushing and popping, unless needed */
  680. if (session->context->errout.debug >= debug_base) {
  681. /* coerce time_t to long to placate older OS, like 32-bit FreeBSD,
  682. * where time_t is int */
  683. GPSD_LOG(debug_base, &session->context->errout,
  684. "GPS Week %d, tow %d.%03d, time %s\n",
  685. gps_week, gps_tow, gps_tow_sub_ms,
  686. timespec_str(&now, ts_buf, sizeof(ts_buf)));
  687. GPSD_LOG(debug_base, &session->context->errout,
  688. "UTC time %s leaps %u, datum %s\n",
  689. timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
  690. session->context->leap_seconds,
  691. session->newdata.datum);
  692. GPSD_LOG(debug_base, &session->context->errout,
  693. "packed: %02d%02d%02d %02d:%02d:%02d\n",
  694. unpacked_date.tm_mday, unpacked_date.tm_mon + 1,
  695. unpacked_date.tm_year % 100,
  696. unpacked_date.tm_hour, unpacked_date.tm_min,
  697. unpacked_date.tm_sec);
  698. GPSD_LOG(debug_base, &session->context->errout,
  699. "solution_info %08x\n", solution_info);
  700. GPSD_LOG(debug_base, &session->context->errout,
  701. "lat %.7f lon %.7f altHAE %.2f altMSL %.2f\n",
  702. session->newdata.latitude, session->newdata.longitude,
  703. session->newdata.altHAE, session->newdata.altMSL);
  704. GPSD_LOG(debug_base, &session->context->errout,
  705. "speed %.2f track %.2f climb %.2f heading_rate %d\n",
  706. session->newdata.speed, session->newdata.track,
  707. session->newdata.climb, heading_rate);
  708. GPSD_LOG(debug_base, &session->context->errout,
  709. "time_bias %d time_accuracy %u, time_source %u\n",
  710. time_bias, time_accuracy, time_source);
  711. GPSD_LOG(debug_base, &session->context->errout,
  712. "distance_travel %u distance_travel_error %d\n",
  713. distance_travel, distance_travel_error);
  714. GPSD_LOG(debug_base, &session->context->errout,
  715. "clk_bias %.2f clk_bias_error %u\n",
  716. clk_bias / 100.0, clk_bias_error);
  717. GPSD_LOG(debug_base, &session->context->errout,
  718. "clk_offset %d clk_offset_error %u\n",
  719. clk_offset, clk_offset_error);
  720. GPSD_LOG(debug_base, &session->context->errout,
  721. "ehpe %d epv %.2f eps %.2f epd %.2f num_svs_in_sol %u\n",
  722. ehpe, session->newdata.epv, session->newdata.eps,
  723. session->newdata.epd, num_svs_in_sol);
  724. GPSD_LOG(debug_base, &session->context->errout,
  725. "sv_list_1 %08x sv_list_2 %08x sv_list_3 %08x\n",
  726. sv_list_1, sv_list_2, sv_list_3);
  727. GPSD_LOG(debug_base, &session->context->errout,
  728. "sv_list_4 %08x sv_list_5 %08x add_info %08x\n",
  729. sv_list_4, sv_list_5, additional_info);
  730. }
  731. return mask;
  732. }
  733. /* Multiconstellation Navigation Data response MID 67,16 (0x43)
  734. * this replaces the deprecated MID 41 */
  735. static gps_mask_t sirf_msg_67_16(struct gps_device_t *session,
  736. unsigned char *buf, size_t len)
  737. {
  738. gps_mask_t mask = 0;
  739. uint32_t gps_tow = 0;
  740. uint32_t gps_tow_sub_ms = 0;
  741. uint16_t gps_week = 0;
  742. timespec_t gps_tow_ns = {0};
  743. timespec_t now = {0};
  744. int16_t time_bias = 0;
  745. uint8_t time_accuracy = 0;
  746. uint8_t time_source = 0;
  747. uint8_t msg_info = 0;
  748. uint8_t num_of_sats = 0;
  749. unsigned int sat_num;
  750. int st; /* index into skyview */
  751. char ts_buf[TIMESPEC_LEN];
  752. if (198 > len) {
  753. /* always payload of 15 sats */
  754. return 0;
  755. }
  756. GPSD_LOG(LOG_PROG, &session->context->errout,
  757. "SiRF V: MID 67,16 Multiconstellation Satellite Data Response\n");
  758. gps_week = getbeu16(buf, 2);
  759. gps_tow = getbeu32(buf, 4) / 1000;
  760. /* get ms part, convert to ns */
  761. gps_tow_sub_ms = 1000000 * (getbeu32(buf, 4) % 1000);
  762. gps_tow_sub_ms += getbeu32(buf, 8); /* add in the ns */
  763. gps_tow_ns.tv_sec = gps_tow;
  764. gps_tow_ns.tv_nsec = gps_tow_sub_ms;
  765. session->newdata.time = gpsd_gpstime_resolv(session, gps_week, gps_tow_ns);
  766. session->gpsdata.skyview_time = session->newdata.time;
  767. time_bias = getbes16(buf, 12);
  768. /* time_accuracy is an odd 8 bit float */
  769. time_accuracy = getub(buf, 14);
  770. time_source = getub(buf, 15);
  771. msg_info = getub(buf, 16);
  772. if (0 == (msg_info & 0x0f)) {
  773. /* WTF? */
  774. return 0;
  775. }
  776. if (1 == (msg_info & 0x0f)) {
  777. /* first set, zero the sats */
  778. gpsd_zero_satellites(&session->gpsdata);
  779. }
  780. st = ((msg_info & 0x0f) - 1) * 15;
  781. num_of_sats = getub(buf, 17);
  782. /* got time now */
  783. mask |= TIME_SET;
  784. /* skip all the debug pushing and popping, unless needed */
  785. if (session->context->errout.debug >= LOG_IO) {
  786. /* coerce time_t to long to placate older OS, like 32-bit FreeBSD,
  787. * where time_t is int */
  788. GPSD_LOG(LOG_IO, &session->context->errout,
  789. "GPS Week %d, tow %d.%03d, time %s\n",
  790. gps_week, gps_tow, gps_tow_sub_ms,
  791. timespec_str(&now, ts_buf, sizeof(ts_buf)));
  792. GPSD_LOG(LOG_IO, &session->context->errout,
  793. "Time bias: %u ns, accuracy %#02x, source %u, "
  794. "msg_info %#02x, sats %u\n",
  795. time_bias, time_accuracy, time_source, msg_info,
  796. num_of_sats);
  797. }
  798. session->gpsdata.satellites_visible = num_of_sats;
  799. /* used? */
  800. /* now decode the individual sat data */
  801. /* num_of_sats is total sats tracked, not the number of sats in this
  802. message */
  803. for (sat_num = 0; sat_num < num_of_sats; sat_num++) {
  804. unsigned offset;
  805. uint16_t sat_info;
  806. uint16_t other_info;
  807. unsigned char gnssId_sirf;
  808. unsigned char gnssId;
  809. unsigned char svId;
  810. short PRN;
  811. double azimuth;
  812. double elevation;
  813. short avg_cno;
  814. double ss;
  815. uint32_t status;
  816. offset = 18 + (sat_num * 12);
  817. if (offset >= len) {
  818. /* end of this message */
  819. break;
  820. }
  821. sat_info = getbeu16(buf, offset);
  822. if (0 == sat_info) {
  823. /* emtpy slot, ignore */
  824. continue;;
  825. }
  826. /* 0 = GPS/QZSS
  827. 1 = SBAS
  828. 2 = GLONASS
  829. 3 = Galileo
  830. 4 = BDS
  831. */
  832. gnssId_sirf = sat_info >> 13;
  833. svId = sat_info & 0x0ff;
  834. other_info = (sat_info >> 8) & 0x1f;
  835. /* make up a PRN based on gnssId:svId, using table 4-55
  836. * from (CS-303979-SP-9) SiRFstarV OSP Extensions
  837. * Note: the Qualcomm doc is very vague
  838. */
  839. switch (gnssId_sirf) {
  840. case 0:
  841. /* GPS, 1-32 maps to 1-32
  842. * 173 to 182: QZSS IMES
  843. * 183 to 187: QZSS SAIF
  844. * 193 to 202: QZSS */
  845. if ((173 <= svId) && (182 >= svId)){
  846. /* IMES */
  847. gnssId = 4;
  848. PRN = svId;
  849. svId -= 172;
  850. } else if ((193 <= svId) && (202 >= svId)){
  851. /* QZSS */
  852. gnssId = 5;
  853. PRN = svId;
  854. svId -= 192;
  855. } else {
  856. /* GPS, or?? */
  857. gnssId = 0;
  858. PRN = svId;
  859. }
  860. break;
  861. case 1:
  862. /* SBAS, 120-158 maps to 120-158 */
  863. if (120 > svId || 158 < svId) {
  864. /* skip bad svId */
  865. continue;
  866. }
  867. gnssId = 1;
  868. PRN = svId;
  869. break;
  870. case 2:
  871. /* GLONASS, 1-32 maps to 65-96 */
  872. if (1 > svId) {
  873. /* skip bad svId */
  874. continue;
  875. }
  876. if (32 < svId) {
  877. /* skip bad svId */
  878. continue;
  879. }
  880. gnssId = 6;
  881. PRN = svId + 64;
  882. break;
  883. case 3:
  884. /* Galileo, 1-36 maps to 211-246 */
  885. if (1 > svId) {
  886. /* skip bad svId */
  887. continue;
  888. }
  889. if (37 < svId) {
  890. /* skip bad svId */
  891. continue;
  892. }
  893. gnssId = 2;
  894. PRN = svId + 210;
  895. break;
  896. case 4:
  897. /* BeiDou, 1-37 maps to 159-163,33-64 */
  898. if (1 > svId) {
  899. /* skip bad svId */
  900. continue;
  901. } else if (6 > svId) {
  902. /* 1-5 maps to 159-163 */
  903. PRN = svId + 158;
  904. } else if (37 < svId) {
  905. /* skip bad svId */
  906. continue;
  907. } else {
  908. /* 6-37 maps to 33-64 */
  909. PRN = svId + 27;
  910. }
  911. gnssId = 3;
  912. break;
  913. default:
  914. /* Huh? Skip bad gnssId */
  915. continue;
  916. }
  917. /* note tenths in az and el */
  918. azimuth = (double)getbeu16(buf, offset + 2) / 10.0;
  919. /* what, no negative elevation? */
  920. elevation = (double)getbeu16(buf, offset + 4) / 10.0;
  921. avg_cno = getbeu16(buf, offset + 6);
  922. ss = (double)avg_cno / 10.0;
  923. status = getbeu32(buf, offset + 8);
  924. if ((0 == avg_cno) && (0 >= elevation) && (0 >= azimuth)) {
  925. /* null data, skip it */
  926. continue;
  927. }
  928. session->gpsdata.skyview[st].PRN = PRN;
  929. session->gpsdata.skyview[st].svid = svId;
  930. session->gpsdata.skyview[st].gnssid = gnssId;
  931. session->gpsdata.skyview[st].azimuth = azimuth;
  932. session->gpsdata.skyview[st].elevation = elevation;
  933. session->gpsdata.skyview[st].ss = ss;
  934. if (0x08000 == (status & 0x08000)) {
  935. session->gpsdata.skyview[st].used = true;
  936. }
  937. GPSD_LOG(LOG_IO, &session->context->errout,
  938. "sat_info %04x gnssId %u svId %3u o %2u PRN %3u az %4.1f "
  939. "el %5.1f ss %5.1f\n",
  940. sat_info, gnssId, svId, other_info, PRN, azimuth,
  941. elevation, ss);
  942. st++;
  943. if (st == MAXCHANNELS) {
  944. /* filled up skyview */
  945. break;
  946. }
  947. }
  948. if ((msg_info >> 4) == (msg_info & 0x0f)) {
  949. /* got all the sats */
  950. mask |= SATELLITE_SET;
  951. }
  952. return mask;
  953. }
  954. /* Multiconstellation Navigation Data response MID 67 (0x43)
  955. * this replaces the deprecated MID 41 */
  956. static gps_mask_t sirf_msg_67(struct gps_device_t *session,
  957. unsigned char *buf, size_t len)
  958. {
  959. gps_mask_t mask = 0;
  960. if (len < 2)
  961. return 0;
  962. switch (buf[1]) {
  963. case 1:
  964. return sirf_msg_67_1(session, buf, len);
  965. case 16:
  966. return sirf_msg_67_16(session, buf, len);
  967. default:
  968. GPSD_LOG(LOG_PROG, &session->context->errout,
  969. "SiRF V: unused MID 67 (0x43), SID: %d, len %ld\n", buf[1],
  970. (long)len);
  971. }
  972. return mask;
  973. }
  974. /* MID_QUERY_RESP MID 81 (0x51) */
  975. static gps_mask_t sirf_msg_qresp(struct gps_device_t *session,
  976. unsigned char *buf, size_t len)
  977. {
  978. if (len < 3)
  979. return 0;
  980. GPSD_LOG(LOG_PROG, &session->context->errout,
  981. "SiRF IV: unused MID_QUERY_RESP 0x51 (81), Q MID: %d, "
  982. "SID: %d Elen: %d\n",
  983. buf[1], buf[2], buf[3]);
  984. return 0;
  985. }
  986. /* Statistics Channel MID 225 (0xe1) */
  987. static gps_mask_t sirf_msg_stats(struct gps_device_t *session,
  988. unsigned char *buf, size_t len)
  989. {
  990. const char *definition = "Unknown";
  991. char output[255] = "unused";
  992. uint16_t ttff_reset;
  993. uint16_t ttff_aid;
  994. uint16_t ttff_nav;
  995. if (2 > len)
  996. return 0;
  997. switch (buf[1]) {
  998. case 6:
  999. definition = "SSB_SIRF_STATS 6";
  1000. ttff_reset = getbeu16(buf, 2);
  1001. ttff_aid = getbeu16(buf, 4);
  1002. ttff_nav = getbeu16(buf, 6);
  1003. (void)snprintf(output, sizeof(output),
  1004. "ttff reset %.1f, aid %.1f nav %.1f",
  1005. ttff_reset * 0.1, ttff_aid * 0.1, ttff_nav * 0.1);
  1006. break;
  1007. case 7:
  1008. definition = "SSB_SIRF_STATS 7";
  1009. ttff_reset = getbeu16(buf, 2);
  1010. ttff_aid = getbeu16(buf, 4);
  1011. ttff_nav = getbeu16(buf, 6);
  1012. (void)snprintf(output, sizeof(output),
  1013. "ttff reset %.1f, aid %.1f nav %.1f",
  1014. ttff_reset * 0.1, ttff_aid * 0.1, ttff_nav * 0.1);
  1015. break;
  1016. case 32:
  1017. definition = "SIRF_MSG_SSB_DL_COMPAT_REC_OUT ";
  1018. break;
  1019. case 33:
  1020. definition = "SIRF_MSG_SSB_DL_OUT_TERM";
  1021. break;
  1022. case 34:
  1023. definition = "SIRF_MSG_SSB_DL_STATUS_OUT";
  1024. break;
  1025. case 35:
  1026. definition = "SIRF_MSG_SSB_SIRF_INTERNAL_OUT";
  1027. break;
  1028. case 65:
  1029. definition = "SIRF_MSG_SSB_EE_SEA_PROVIDE_EPH_EXT";
  1030. break;
  1031. default:
  1032. definition = "Unknown";
  1033. break;
  1034. }
  1035. GPSD_LOG(LOG_PROG, &session->context->errout,
  1036. "SiRF IV: MID 225 (0xe1), SID: %d (%s)%s\n",
  1037. buf[1], definition, output);
  1038. return 0;
  1039. }
  1040. /* MID_TCXO_LEARNING_OUT MID 93 (0x5d) */
  1041. static gps_mask_t sirf_msg_tcxo(struct gps_device_t *session,
  1042. unsigned char *buf, size_t len)
  1043. {
  1044. const char *definition = "Unknown";
  1045. uint32_t gps_tow = 0;
  1046. uint16_t gps_week = 0;
  1047. timespec_t gps_tow_ns = {0};
  1048. char output[255] = "";
  1049. timespec_t now = {0};
  1050. gps_mask_t mask = 0;
  1051. unsigned int time_status = 0;
  1052. int clock_offset = 0;
  1053. unsigned int temp = 0;
  1054. if (len < 2)
  1055. return 0;
  1056. switch (buf[1]) {
  1057. case 1:
  1058. definition = "CLOCK_MODEL_DATA_BASE_OUT";
  1059. break;
  1060. case 2:
  1061. definition = "TEMPERATURE_TABLE";
  1062. break;
  1063. case 4:
  1064. definition = "TEMP_RECORDER_MESSAGE";
  1065. break;
  1066. case 5:
  1067. definition = "EARC";
  1068. break;
  1069. case 6:
  1070. definition = "RTC_ALARM";
  1071. break;
  1072. case 7:
  1073. definition = "RTC_CAL";
  1074. break;
  1075. case 8:
  1076. definition = "MPM_ACQUIRED";
  1077. break;
  1078. case 9:
  1079. definition = "MPM_SEARCHES";
  1080. break;
  1081. case 10:
  1082. definition = "MPM_PREPOS";
  1083. break;
  1084. case 11:
  1085. definition = "MICRO_NAV_MEASUREMENT";
  1086. break;
  1087. case 12:
  1088. definition = "TCXO_UNCEARTAINTY";
  1089. break;
  1090. case 13:
  1091. definition = "SYSTEM_TIME_STAMP";
  1092. break;
  1093. case 18:
  1094. if (26 > len) {
  1095. GPSD_LOG(LOG_PROG, &session->context->errout,
  1096. "SiRF IV: TCXO 0x5D (93), SID: %d BAD len %zd\n",
  1097. buf[1], len);
  1098. return 0;
  1099. }
  1100. definition = "SIRF_MSG_SSB_XO_TEMP_REC_VALUE";
  1101. gps_tow = getbeu32(buf, 2);
  1102. gps_week = getbeu16(buf, 6);
  1103. time_status = getub(buf, 8);
  1104. clock_offset = getsb(buf, 9); /* looks like leapseconds? */
  1105. temp = getub(buf, 22);
  1106. gps_tow_ns.tv_sec = gps_tow / 100;
  1107. gps_tow_ns.tv_nsec = (gps_tow % 100) * 10000000L;
  1108. session->newdata.time = gpsd_gpstime_resolv(session, gps_week,
  1109. gps_tow_ns);
  1110. /* skip all the debug pushing and popping, unless needed */
  1111. if (session->context->errout.debug >= LOG_PROG) {
  1112. /* coerce time_t to long to placate older OS, like 32-bit FreeBSD,
  1113. * where time_t is int */
  1114. (void)snprintf(output, sizeof(output),
  1115. ", GPS Week %d, tow %d, time %lld, time_status %d "
  1116. "ClockOffset %d, Temp %.1f",
  1117. gps_week, gps_tow, (long long)now.tv_sec,
  1118. time_status,
  1119. clock_offset, temp * 0.54902);
  1120. }
  1121. if (7 == (time_status & 7)) {
  1122. mask |= TIME_SET;
  1123. }
  1124. break;
  1125. default:
  1126. definition = "Unknown";
  1127. break;
  1128. }
  1129. GPSD_LOG(LOG_PROG, &session->context->errout,
  1130. "SiRF IV: TCXO 0x5D (93), SID: %d (%s)%s\n",
  1131. buf[1], definition, output);
  1132. return mask;
  1133. }
  1134. /* Software Version String MID 6
  1135. * response to Poll Software Version MID 132 */
  1136. static gps_mask_t sirf_msg_swversion(struct gps_device_t *session,
  1137. unsigned char *buf, size_t len)
  1138. {
  1139. double fv;
  1140. unsigned char *cp;
  1141. if (1 > len)
  1142. return 0;
  1143. if ((3 < len) && (len == (unsigned int)(buf[1] + buf[2] + 3))) {
  1144. /* new style message, Version 4+, max 162 bytes */
  1145. (void)strlcpy(session->subtype, (char *)buf + 3,
  1146. sizeof(session->subtype));
  1147. (void)strlcat(session->subtype, ";", sizeof(session->subtype));
  1148. (void)strlcat(session->subtype, (char *)buf + 3 + buf[1],
  1149. sizeof(session->subtype));
  1150. session->driver.sirf.driverstate |= SIRF_GE_232;
  1151. /* FIXME: this only finding major version, not minor version */
  1152. for (cp = buf+1; *cp!=(unsigned char)'\0' && isdigit(*cp)==0; cp++)
  1153. continue;
  1154. fv = safe_atof((const char *)cp);
  1155. } else {
  1156. /* old style, version 3 and below */
  1157. (void)strlcpy(session->subtype, (char *)buf + 1,
  1158. sizeof(session->subtype));
  1159. for (cp = buf+1; *cp!=(unsigned char)'\0' && isdigit(*cp)==0; cp++)
  1160. continue;
  1161. fv = safe_atof((const char *)cp);
  1162. if (fv < 231) {
  1163. session->driver.sirf.driverstate |= SIRF_LT_231;
  1164. #ifdef RECONFIGURE_ENABLE
  1165. if (fv > 200)
  1166. sirfbin_mode(session, 0);
  1167. #endif /* RECONFIGURE_ENABLE */
  1168. } else if (fv < 232) {
  1169. session->driver.sirf.driverstate |= SIRF_EQ_231;
  1170. } else {
  1171. session->driver.sirf.driverstate |= SIRF_GE_232;
  1172. }
  1173. if (strstr((char *)(buf + 1), "ES"))
  1174. GPSD_LOG(LOG_INF, &session->context->errout,
  1175. "SiRF: Firmware has XTrac capability\n");
  1176. }
  1177. GPSD_LOG(LOG_PROG, &session->context->errout,
  1178. "SiRF: fv: %0.2f, Driver state flags are: %0x\n",
  1179. fv, session->driver.sirf.driverstate);
  1180. session->driver.sirf.time_seen = 0;
  1181. GPSD_LOG(LOG_DATA, &session->context->errout,
  1182. "SiRF: FV MID 0x06: subtype='%s' len=%lu buf1 %u buf2 %u\n",
  1183. session->subtype, (long)len, buf[1], buf[2]);
  1184. return DEVICEID_SET;
  1185. }
  1186. /* subframe data MID 8 */
  1187. static gps_mask_t sirf_msg_navdata(struct gps_device_t *session,
  1188. unsigned char *buf, size_t len)
  1189. {
  1190. unsigned int i, chan, svid;
  1191. uint32_t words[10];
  1192. if (len != 43)
  1193. return 0;
  1194. chan = (unsigned int)getub(buf, 1);
  1195. svid = (unsigned int)getub(buf, 2);
  1196. for (i = 0; i < 10; i++) {
  1197. words[i] = (uint32_t)getbeu32(buf, 4 * i + 3);
  1198. }
  1199. GPSD_LOG(LOG_DATA, &session->context->errout,
  1200. "SiRF: NavData chan %u svid %u\n",chan,svid);
  1201. #ifdef RECONFIGURE_ENABLE
  1202. /* SiRF recommends at least 57600 for SiRF IV nav data */
  1203. if (!session->context->readonly && session->gpsdata.dev.baudrate < 57600) {
  1204. /* some USB are also too slow, no way to tell which ones */
  1205. GPSD_LOG(LOG_WARN, &session->context->errout,
  1206. "WARNING: SiRF: link too slow, disabling subframes.\n");
  1207. (void)sirf_write(session, disablesubframe);
  1208. }
  1209. #endif /* RECONFIGURE_ENABLE */
  1210. return gpsd_interpret_subframe_raw(session, svid, words);
  1211. }
  1212. /* max channels allowed in old MID 4 SiRF format */
  1213. #define SIRF_CHANNELS 12
  1214. /* decode Measured Tracker Data response ID 4 (0x04)
  1215. * deprecated on Sirfstar V, use MID 67,16 instead */
  1216. static gps_mask_t sirf_msg_svinfo(struct gps_device_t *session,
  1217. unsigned char *buf, size_t len)
  1218. {
  1219. int st, i, j, nsv;
  1220. uint32_t hsec; /* TOW in hundredths of seconds */
  1221. timespec_t ts_tow;
  1222. char ts_buf[TIMESPEC_LEN];
  1223. if (len != 188)
  1224. return 0;
  1225. hsec = getbeu32(buf, 3);
  1226. ts_tow.tv_sec = hsec / 100;
  1227. ts_tow.tv_nsec = (long)((hsec % 100) * 10000000L);
  1228. session->gpsdata.skyview_time = gpsd_gpstime_resolv(session,
  1229. (unsigned short)getbes16(buf, 1), ts_tow);
  1230. gpsd_zero_satellites(&session->gpsdata);
  1231. for (i = st = nsv = 0; i < SIRF_CHANNELS; i++) {
  1232. int cn;
  1233. int off = 8 + 15 * i;
  1234. bool good;
  1235. short prn = (short)getub(buf, off);
  1236. unsigned short stat = (unsigned short)getbeu16(buf, off + 3);
  1237. session->gpsdata.skyview[st].PRN = prn;
  1238. session->gpsdata.skyview[st].svid = prn;
  1239. if (120 <= prn && 158 >= prn) {
  1240. /* SBAS */
  1241. session->gpsdata.skyview[st].gnssid = 1;
  1242. } else {
  1243. /* GPS */
  1244. session->gpsdata.skyview[st].gnssid = 0;
  1245. }
  1246. session->gpsdata.skyview[st].azimuth =
  1247. (double)(((unsigned)getub(buf, off + 1) * 3.0) / 2.0);
  1248. session->gpsdata.skyview[st].elevation =
  1249. (double)((unsigned)getub(buf, off + 2) / 2.0);
  1250. cn = 0;
  1251. for (j = 0; j < 10; j++)
  1252. cn += (int)getub(buf, off + 5 + j);
  1253. session->gpsdata.skyview[st].ss = (float)(cn / 10.0);
  1254. session->gpsdata.skyview[st].used = (bool)(stat & 0x01);
  1255. good = session->gpsdata.skyview[st].PRN != 0 &&
  1256. session->gpsdata.skyview[st].azimuth != 0 &&
  1257. session->gpsdata.skyview[st].elevation != 0;
  1258. #ifdef __UNUSED__
  1259. GPSD_LOG(LOG_PROG, &session->context->errout,
  1260. "SiRF: PRN=%2d El=%3.2f Az=%3.2f ss=%3d stat=%04x %c\n",
  1261. prn,
  1262. getub(buf, off + 2) / 2.0,
  1263. (getub(buf, off + 1) * 3) / 2.0,
  1264. cn / 10, stat, good ? '*' : ' ');
  1265. #endif /* UNUSED */
  1266. if (good != 0) {
  1267. st += 1;
  1268. if (stat & 0x01)
  1269. nsv++;
  1270. }
  1271. }
  1272. session->gpsdata.satellites_visible = st;
  1273. session->gpsdata.satellites_used = nsv;
  1274. /* mark SBAS sats in use if SBAS was in use as of the last MID 27 */
  1275. for (i = 0; i < st; i++) {
  1276. int prn = session->gpsdata.skyview[i].PRN;
  1277. if ((120 <= prn && 158 >= prn) &&
  1278. session->gpsdata.status == STATUS_DGPS_FIX &&
  1279. session->driver.sirf.dgps_source == SIRF_DGPS_SOURCE_SBAS) {
  1280. /* used does not seem right, DGPS means got the correction
  1281. * data, not that the geometry was improved... */
  1282. session->gpsdata.skyview[i].used = true;
  1283. }
  1284. }
  1285. if (st < 3) {
  1286. GPSD_LOG(LOG_PROG, &session->context->errout,
  1287. "SiRF: NTPD not enough satellites seen: %d\n", st);
  1288. } else {
  1289. /* SiRF says if 3 sats in view the time is good */
  1290. GPSD_LOG(LOG_PROG, &session->context->errout,
  1291. "SiRF: NTPD valid time MID 0x04, seen=%#02x, time:%s, "
  1292. "leap:%d\n",
  1293. session->driver.sirf.time_seen,
  1294. timespec_str(&session->gpsdata.skyview_time, ts_buf,
  1295. sizeof(ts_buf)),
  1296. session->context->leap_seconds);
  1297. }
  1298. GPSD_LOG(LOG_DATA, &session->context->errout,
  1299. "SiRF: MTD 0x04: visible=%d mask={SATELLITE}\n",
  1300. session->gpsdata.satellites_visible);
  1301. return SATELLITE_SET;
  1302. }
  1303. static double sirf_time_offset(struct gps_device_t *session)
  1304. /* return NTP time-offset fudge factor for this device */
  1305. {
  1306. double retval = 0;
  1307. /* we need to have seen UTC time with a valid leap-year offset */
  1308. if ((session->driver.sirf.time_seen & TIME_SEEN_UTC_2) != 0) {
  1309. retval = NAN;
  1310. }
  1311. /* the PPS time message */
  1312. else if (session->driver.sirf.lastid == (unsigned char)52) {
  1313. retval = 0.3;
  1314. }
  1315. /* u-blox EMND message */
  1316. else if (session->driver.sirf.lastid == (unsigned char)98) {
  1317. retval = 0.570;
  1318. }
  1319. #ifdef __UNUSED__
  1320. /* geodetic-data message */
  1321. else if (session->driver.sirf.lastid == (unsigned char)41) {
  1322. retval = 0.570;
  1323. }
  1324. #endif /* __UNUSED__ */
  1325. /* the Navigation Solution message */
  1326. else if (session->driver.sirf.lastid == (unsigned char)2) {
  1327. if (session->sourcetype == source_usb) {
  1328. retval = 0.640; /* USB, expect +/- 50mS jitter */
  1329. } else {
  1330. switch (session->gpsdata.dev.baudrate) {
  1331. default:
  1332. retval = 0.704; /* WAG */
  1333. break;
  1334. case 4800:
  1335. retval = 0.704; /* fudge valid at 4800bps */
  1336. break;
  1337. case 9600:
  1338. retval = 0.688;
  1339. break;
  1340. case 19200:
  1341. retval = 0.484;
  1342. break;
  1343. case 38400:
  1344. retval = 0.845; /* 0.388; ?? */
  1345. break;
  1346. }
  1347. }
  1348. }
  1349. return retval;
  1350. }
  1351. /* Measured Navigation Data Out ID 2 (0x02) */
  1352. static gps_mask_t sirf_msg_navsol(struct gps_device_t *session,
  1353. unsigned char *buf, size_t len)
  1354. {
  1355. unsigned short navtype;
  1356. unsigned short nav_mode2;
  1357. unsigned short gps_week;
  1358. uint32_t iTOW;
  1359. timespec_t tow;
  1360. gps_mask_t mask = 0;
  1361. char ts_buf[TIMESPEC_LEN];
  1362. /* later versions are 47 bytes long */
  1363. if (41 > len)
  1364. return 0;
  1365. /*
  1366. * A count of satellites used is an unsigned byte at offset 28
  1367. * and an array of unsigned bytes listing satellite PRNs used
  1368. * in this fix begins at offset 29, but we don't use either because
  1369. * in JSON the used bits are reported in the SKY sentence;
  1370. * we get that data from the svinfo packet.
  1371. */
  1372. /* position/velocity is bytes 1-18 */
  1373. session->newdata.ecef.x = (double)getbes32(buf, 1);
  1374. session->newdata.ecef.y = (double)getbes32(buf, 5);
  1375. session->newdata.ecef.z = (double)getbes32(buf, 9);
  1376. session->newdata.ecef.vx = (double)getbes16(buf, 13) / 8.0;
  1377. session->newdata.ecef.vy = (double)getbes16(buf, 15) / 8.0;
  1378. session->newdata.ecef.vz = (double)getbes16(buf, 17) / 8.0;
  1379. mask |= ECEF_SET | VECEF_SET;
  1380. /* fix status is byte 19 */
  1381. navtype = (unsigned short)getub(buf, 19);
  1382. session->gpsdata.status = STATUS_NO_FIX;
  1383. session->newdata.mode = MODE_NO_FIX;
  1384. if ((navtype & 0x80) != 0)
  1385. session->gpsdata.status = STATUS_DGPS_FIX;
  1386. else if ((navtype & 0x07) > 0 && (navtype & 0x07) < 7)
  1387. session->gpsdata.status = STATUS_FIX;
  1388. if ((navtype & 0x07) == 4 || (navtype & 0x07) == 6)
  1389. session->newdata.mode = MODE_3D;
  1390. else if (session->gpsdata.status != 0)
  1391. session->newdata.mode = MODE_2D;
  1392. /* byte 20 is HDOP */
  1393. session->gpsdata.dop.hdop = (double)getub(buf, 20) / 5.0;
  1394. /* byte 21 is nav_mode2, not clear how to interpret that */
  1395. nav_mode2 = getub(buf, 21);
  1396. gps_week = getbes16(buf, 22);
  1397. iTOW = getbeu32(buf, 24);
  1398. /* Gack. The doc says early SiRF scales iTOW by 100, later ones
  1399. * by 1000. But that does not seem to be true in sirfstar V. */
  1400. tow.tv_sec = iTOW / 100;
  1401. tow.tv_nsec = (iTOW % 100) * 10000000L;
  1402. session->newdata.time = gpsd_gpstime_resolv(session, gps_week, tow);
  1403. if (session->newdata.mode <= MODE_NO_FIX) {
  1404. GPSD_LOG(LOG_PROG, &session->context->errout,
  1405. "SiRF: NTPD no fix, mode: %d\n",
  1406. session->newdata.mode);
  1407. } else {
  1408. GPSD_LOG(LOG_PROG, &session->context->errout,
  1409. "SiRF: MID 0x02 NTPD valid time, seen %#02x time %s "
  1410. "leap %d nav_mode2 %#x\n",
  1411. session->driver.sirf.time_seen,
  1412. timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
  1413. session->context->leap_seconds,
  1414. nav_mode2);
  1415. }
  1416. /* clear computed DOPs so they get recomputed. */
  1417. session->gpsdata.dop.tdop = NAN;
  1418. mask |= TIME_SET | STATUS_SET | MODE_SET | DOP_SET | USED_IS;
  1419. if ( 3 <= session->gpsdata.satellites_visible ) {
  1420. mask |= NTPTIME_IS;
  1421. }
  1422. GPSD_LOG(LOG_PROG, &session->context->errout,
  1423. "SiRF: MND 0x02: Navtype %#0x, Status %d mode %d\n",
  1424. navtype, session->gpsdata.status, session->newdata.mode);
  1425. GPSD_LOG(LOG_DATA, &session->context->errout,
  1426. "SiRF: MND 0x02: gpsd_week %u iTOW %u\n",
  1427. gps_week, iTOW);
  1428. GPSD_LOG(LOG_DATA, &session->context->errout,
  1429. "SiRF: MND 0x02: time %s ecef x: %.2f y: %.2f z: %.2f "
  1430. "mode %d status %d hdop %.2f used %d\n",
  1431. timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
  1432. session->newdata.ecef.x,
  1433. session->newdata.ecef.y, session->newdata.ecef.z,
  1434. session->newdata.mode, session->gpsdata.status,
  1435. session->gpsdata.dop.hdop, session->gpsdata.satellites_used);
  1436. return mask;
  1437. }
  1438. #ifdef __UNUSED__
  1439. /***************************************************************************
  1440. We've stopped interpreting GND (0x29) for the following reasons:
  1441. 1) Versions of SiRF firmware still in wide circulation (and likely to be
  1442. so for a while) don't report a valid time field, leading to annoying
  1443. twice-per-second jitter in client displays.
  1444. 2) What we wanted out of this that MND didn't give us was horizontal and
  1445. vertical error estimates. But we have to do our own error estimation by
  1446. computing DOPs from the skyview covariance matrix anyway, because we
  1447. want separate epx and epy errors a la NMEA 3.0.
  1448. 3) The fix-merge logic in gpsd.c is (unavoidably) NMEA-centric and
  1449. thinks multiple sentences in one cycle should be treated as
  1450. incremental updates. This leads to various silly results when (as
  1451. in GND) a subsequent sentence is (a) intended to be a complete fix
  1452. in itself, and (b) frequently broken.
  1453. 4) Ignoring this dodgy sentence allows us to go to a nice clean single
  1454. fix update per cycle.
  1455. Code left in place in case we need to reverse this decision.
  1456. ***************************************************************************/
  1457. static gps_mask_t sirf_msg_geodetic(struct gps_device_t *session,
  1458. unsigned char *buf, size_t len)
  1459. {
  1460. unsigned short navtype;
  1461. gps_mask_t mask = 0;
  1462. double eph;
  1463. double dbl_tmp;
  1464. if (len != 91)
  1465. return 0;
  1466. session->gpsdata.sentence_length = 91;
  1467. navtype = (unsigned short)getbeu16(buf, 3);
  1468. session->gpsdata.status = STATUS_NO_FIX;
  1469. session->newdata.mode = MODE_NO_FIX;
  1470. if (navtype & 0x80)
  1471. session->gpsdata.status = STATUS_DGPS_FIX;
  1472. else if ((navtype & 0x07) > 0 && (navtype & 0x07) < 7)
  1473. session->gpsdata.status = STATUS_FIX;
  1474. session->newdata.mode = MODE_NO_FIX;
  1475. if ((navtype & 0x07) == 4 || (navtype & 0x07) == 6)
  1476. session->newdata.mode = MODE_3D;
  1477. else if (session->gpsdata.status)
  1478. session->newdata.mode = MODE_2D;
  1479. GPSD_LOG(LOG_PROG, &session->context->errout,
  1480. "SiRF: GND 0x29: Navtype = 0x%0x, Status = %d, mode = %d\n",
  1481. navtype, session->gpsdata.status, session->newdata.mode);
  1482. mask |= STATUS_SET | MODE_SET;
  1483. session->newdata.latitude = getbes32(buf, 23) * 1e-7;
  1484. session->newdata.longitude = getbes32(buf, 27) * 1e-7;
  1485. if (session->newdata.latitude != 0 && session->newdata.latitude != 0)
  1486. mask |= LATLON_SET;
  1487. if ((eph = getbes32(buf, 50)) > 0) {
  1488. session->newdata.eph = eph * 1e-2;
  1489. mask |= HERR_SET;
  1490. }
  1491. dbl_temp = getbes32(buf, 54) * 1e-2;
  1492. if (0.01 < dbl_temp)
  1493. session->newdata.epv = dbl_temp;
  1494. if ((session->newdata.eps = getbes16(buf, 62) * 1e-2) > 0)
  1495. mask |= SPEEDERR_SET;
  1496. /* HDOP should be available at byte 89, but in 231 it's zero. */
  1497. //session->gpsdata.dop.hdop = (unsigned int)getub(buf, 89) * 0.2;
  1498. if ((session->newdata.mode > MODE_NO_FIX)
  1499. && (session->driver.sirf.driverstate & SIRF_GE_232)) {
  1500. struct tm unpacked_date;
  1501. double subseconds;
  1502. /*
  1503. * Early versions of the SiRF protocol manual don't document
  1504. * this sentence at all. Some that do incorrectly
  1505. * describe UTC Day, Hour, and Minute as 2-byte quantities,
  1506. * not 1-byte. Chris Kuethe, our SiRF expert, tells us:
  1507. *
  1508. * "The Geodetic Navigation packet (0x29) was not fully
  1509. * implemented in firmware prior to version 2.3.2. So for
  1510. * anyone running 231.000.000 or earlier (including ES,
  1511. * SiRFDRive, XTrac trains) you won't get UTC time. I don't
  1512. * know what's broken in firmwares before 2.3.1..."
  1513. *
  1514. * To work around the incomplete implementation of this
  1515. * packet in 231, we used to assume that only the altitude field
  1516. * from this packet is valid. But even this doesn't necessarily
  1517. * seem to be the case. Instead, we do our own computation
  1518. * of geoid separation now.
  1519. *
  1520. * UTC is left all zeros in 231 and older firmware versions,
  1521. * and misdocumented in version 1.4 of the Protocol Reference.
  1522. * Documented: Real:
  1523. * UTC year 2 2
  1524. * UTC month 1 1
  1525. * UTC day 2 1
  1526. * UTC hour 2 1
  1527. * UTC minute 2 1
  1528. * UTC second 2 2
  1529. * 11 8
  1530. *
  1531. * Documentation of this field was corrected in the 1.6 version
  1532. * of the protocol manual.
  1533. */
  1534. memset(&unpacked_date, 0, sizeof(unpacked_date));
  1535. unpacked_date.tm_year = (int)getbeu16(buf, 11) - 1900;
  1536. unpacked_date.tm_mon = (int)getub(buf, 13) - 1;
  1537. unpacked_date.tm_mday = (int)getub(buf, 14);
  1538. unpacked_date.tm_hour = (int)getub(buf, 15);
  1539. unpacked_date.tm_min = (int)getub(buf, 16);
  1540. subseconds = getbeu16(buf, 17);
  1541. unpacked_date.tm_sec = subseconds / 1000;
  1542. session->newdata.time.tv_sec = mkgmtime(&unpacked_date);
  1543. session->newdata.time.tv_nsec = (long)((subseconds % 1000) * 1000000L);
  1544. GPSD_LOG(LOG_PROG, &session->context->errout,
  1545. "SiRF: GND 0x29 UTC: %lf\n",
  1546. session->newdata.time);
  1547. if (session->newdata.mode <= MODE_NO_FIX) {
  1548. GPSD_LOG(LOG_PROG, &session->context->errou,
  1549. "SiRF: NTPD no fix, mode: $d\n",
  1550. session->newdata.mode);
  1551. } else if (0 == unpacked_date.tm_year) {
  1552. GPSD_LOG(LOG_PROG, &session->context->errou,
  1553. "SiRF: NTPD no year\n",
  1554. session->newdata.mode);
  1555. } else {
  1556. GPSD_LOG(LOG_PROG, &session->context->errout,
  1557. "SiRF: NTPD valid time MID 0x29, seen=%#02x\n",
  1558. session->driver.sirf.time_seen);
  1559. }
  1560. if ( 3 <= session->gpsdata.satellites_visible ) {
  1561. mask |= NTPTIME_IS;
  1562. }
  1563. /* alititude WGS84 */
  1564. session->newdata.altHAE = getbes32(buf, 31) * 1e-2;
  1565. session->newdata.altMSL = getbes32(buf, 35) * 1e-2;
  1566. /* Let gpsd_error_model() deal with geoid_sep and altHAE */
  1567. /* skip 1 byte of map datum */
  1568. session->newdata.speed = getbeu16(buf, 40) * 1e-2;
  1569. session->newdata.track = getbeu16(buf, 42) * 1e-2;
  1570. /* skip 2 bytes of magnetic variation */
  1571. session->newdata.climb = getbes16(buf, 46) * 1e-2;
  1572. mask |= TIME_SET | SPEED_SET | TRACK_SET;
  1573. if (session->newdata.mode == MODE_3D)
  1574. mask |= ALTITUDE_SET | CLIMB_SET;
  1575. }
  1576. GPSD_LOG(LOG_DATA, &session->context->errout,
  1577. "SiRF: GND 0x29: time=%.2f lat=%.2f lon=%.2f altHAE=%.2f "
  1578. "track=%.2f speed=%.2f mode=%d status=%d\n",
  1579. session->newdata.time,
  1580. session->newdata.latitude,
  1581. session->newdata.longitude,
  1582. session->newdata.altHAE,
  1583. session->newdata.track,
  1584. session->newdata.speed,
  1585. session->newdata.mode,
  1586. session->gpsdata.status);
  1587. return mask;
  1588. }
  1589. #endif /* __UNUSED__ */
  1590. /* decode Navigation Parameters MID 19 (0x13) response to ID 152 */
  1591. static gps_mask_t sirf_msg_sysparam(struct gps_device_t *session,
  1592. unsigned char *buf, size_t len)
  1593. {
  1594. if (len < 65)
  1595. return 0;
  1596. /* save these to restore them in the revert method */
  1597. session->driver.sirf.nav_parameters_seen = true;
  1598. session->driver.sirf.altitude_hold_mode = (unsigned char)getub(buf, 5);
  1599. session->driver.sirf.altitude_hold_source = (unsigned char)getub(buf, 6);
  1600. session->driver.sirf.altitude_source_input = getbes16(buf, 7);
  1601. session->driver.sirf.degraded_mode = (unsigned char)getub(buf, 9);
  1602. session->driver.sirf.degraded_timeout = (unsigned char)getub(buf, 10);
  1603. session->driver.sirf.dr_timeout = (unsigned char)getub(buf, 11);
  1604. session->driver.sirf.track_smooth_mode = (unsigned char)getub(buf, 12);
  1605. return 0;
  1606. }
  1607. /* DGPS status MID 27 (0x1b) */
  1608. /* only documentented from prorocol version 1.7 (2005) onwards */
  1609. static gps_mask_t sirf_msg_dgpsstatus(struct gps_device_t *session,
  1610. unsigned char *buf, size_t len UNUSED)
  1611. {
  1612. session->driver.sirf.dgps_source = (unsigned int)getub(buf, 1);
  1613. return 0;
  1614. }
  1615. /* decode Extended Measured Navigation Data MID 98 (0x62)
  1616. * Used in u-blox TIM GPS receivers (SiRF2-ublox)
  1617. * "Firmware Release 2.1 UBX 1.0" */
  1618. static gps_mask_t sirf_msg_ublox(struct gps_device_t *session,
  1619. unsigned char *buf, size_t len UNUSED)
  1620. {
  1621. gps_mask_t mask;
  1622. unsigned short navtype;
  1623. char ts_buf[TIMESPEC_LEN];
  1624. if (len != 39)
  1625. return 0;
  1626. /* this packet is only sent by u-blox firmware from version 1.32 */
  1627. mask = LATLON_SET | ALTITUDE_SET | SPEED_SET | TRACK_SET | CLIMB_SET |
  1628. STATUS_SET | MODE_SET | DOP_SET;
  1629. session->newdata.latitude = (double)getbes32(buf, 1) * RAD_2_DEG * 1e-8;
  1630. session->newdata.longitude = (double)getbes32(buf, 5) * RAD_2_DEG * 1e-8;
  1631. /* defaults to WGS84 */
  1632. session->newdata.altHAE = (double)getbes32(buf, 9) * 1e-3;
  1633. session->newdata.speed = (double)getbes32(buf, 13) * 1e-3;
  1634. session->newdata.climb = (double)getbes32(buf, 17) * 1e-3;
  1635. session->newdata.track = (double)getbes32(buf, 21) * RAD_2_DEG * 1e-8;
  1636. navtype = (unsigned short)getub(buf, 25);
  1637. session->gpsdata.status = STATUS_NO_FIX;
  1638. session->newdata.mode = MODE_NO_FIX;
  1639. if (navtype & 0x80)
  1640. session->gpsdata.status = STATUS_DGPS_FIX;
  1641. else if ((navtype & 0x07) > 0 && (navtype & 0x07) < 7)
  1642. session->gpsdata.status = STATUS_FIX;
  1643. if ((navtype & 0x07) == 4 || (navtype & 0x07) == 6)
  1644. session->newdata.mode = MODE_3D;
  1645. else if (session->gpsdata.status)
  1646. session->newdata.mode = MODE_2D;
  1647. GPSD_LOG(LOG_PROG, &session->context->errout,
  1648. "SiRF: EMND 0x62: Navtype = 0x%0x, Status = %d, mode = %d\n",
  1649. navtype, session->gpsdata.status, session->newdata.mode);
  1650. if (navtype & 0x40) { /* UTC corrected timestamp? */
  1651. struct tm unpacked_date;
  1652. uint32_t msec;
  1653. mask |= TIME_SET;
  1654. if ( 3 <= session->gpsdata.satellites_visible ) {
  1655. mask |= NTPTIME_IS;
  1656. }
  1657. memset(&unpacked_date, 0, sizeof(unpacked_date));
  1658. unpacked_date.tm_year = (int)getbeu16(buf, 26) - 1900;
  1659. unpacked_date.tm_mon = (int)getub(buf, 28) - 1;
  1660. unpacked_date.tm_mday = (int)getub(buf, 29);
  1661. unpacked_date.tm_hour = (int)getub(buf, 30);
  1662. unpacked_date.tm_min = (int)getub(buf, 31);
  1663. msec = getbeu16(buf, 32);
  1664. unpacked_date.tm_sec = msec / 1000;
  1665. session->newdata.time.tv_sec = mkgmtime(&unpacked_date);
  1666. /* ms to ns */
  1667. session->newdata.time.tv_nsec = (msec % 1000) * 1000000L;
  1668. if (0 == (session->driver.sirf.time_seen & TIME_SEEN_UTC_2)) {
  1669. GPSD_LOG(LOG_RAW, &session->context->errout,
  1670. "SiRF: NTPD just SEEN_UTC_2\n");
  1671. }
  1672. GPSD_LOG(LOG_PROG, &session->context->errout,
  1673. "SiRF: NTPD valid time MID 0x62, seen=%#02x\n",
  1674. session->driver.sirf.time_seen);
  1675. session->driver.sirf.time_seen |= TIME_SEEN_UTC_2;
  1676. /* The mode byte, bit 6 tells us if leap second is valid.
  1677. * But not what the leap second is.
  1678. * session->context->valid |= LEAP_SECOND_VALID;
  1679. */
  1680. }
  1681. session->gpsdata.dop.gdop = (int)getub(buf, 34) / 5.0;
  1682. session->gpsdata.dop.pdop = (int)getub(buf, 35) / 5.0;
  1683. session->gpsdata.dop.hdop = (int)getub(buf, 36) / 5.0;
  1684. session->gpsdata.dop.vdop = (int)getub(buf, 37) / 5.0;
  1685. session->gpsdata.dop.tdop = (int)getub(buf, 38) / 5.0;
  1686. session->driver.sirf.driverstate |= UBLOX;
  1687. GPSD_LOG(LOG_DATA, &session->context->errout,
  1688. "SiRF: EMD 0x62: time=%s lat=%.2f lon=%.2f altHAE=%.2f "
  1689. "speed=%.2f track=%.2f climb=%.2f mode=%d status=%d gdop=%.2f "
  1690. "pdop=%.2f hdop=%.2f vdop=%.2f tdop=%.2f\n",
  1691. timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
  1692. session->newdata.latitude,
  1693. session->newdata.longitude, session->newdata.altHAE,
  1694. session->newdata.speed, session->newdata.track,
  1695. session->newdata.climb, session->newdata.mode,
  1696. session->gpsdata.status, session->gpsdata.dop.gdop,
  1697. session->gpsdata.dop.pdop, session->gpsdata.dop.hdop,
  1698. session->gpsdata.dop.vdop, session->gpsdata.dop.tdop);
  1699. return mask;
  1700. }
  1701. /* decode PPS Time MID 52 (0x34) */
  1702. static gps_mask_t sirf_msg_ppstime(struct gps_device_t *session,
  1703. unsigned char *buf, size_t len)
  1704. {
  1705. gps_mask_t mask = 0;
  1706. if (len < 19)
  1707. return 0;
  1708. GPSD_LOG(LOG_PROG, &session->context->errout,
  1709. "SiRF: PPS 0x34: Status = %#02x\n",
  1710. getub(buf, 14));
  1711. if (((int)getub(buf, 14) & 0x07) == 0x07) { /* valid UTC time? */
  1712. struct tm unpacked_date;
  1713. memset(&unpacked_date, 0, sizeof(unpacked_date));
  1714. unpacked_date.tm_hour = (int)getub(buf, 1);
  1715. unpacked_date.tm_min = (int)getub(buf, 2);
  1716. unpacked_date.tm_sec = (int)getub(buf, 3);
  1717. unpacked_date.tm_mday = (int)getub(buf, 4);
  1718. unpacked_date.tm_mon = (int)getub(buf, 5) - 1;
  1719. unpacked_date.tm_year = (int)getbeu16(buf, 6) - 1900;
  1720. session->newdata.time.tv_sec = mkgmtime(&unpacked_date);
  1721. session->newdata.time.tv_nsec = 0;
  1722. session->context->leap_seconds = (int)getbeu16(buf, 8);
  1723. // Ignore UTCOffsetFrac1
  1724. session->context->valid |= LEAP_SECOND_VALID;
  1725. if (0 == (session->driver.sirf.time_seen & TIME_SEEN_UTC_2)) {
  1726. GPSD_LOG(LOG_RAW, &session->context->errout,
  1727. "SiRF: NTPD just SEEN_UTC_2\n");
  1728. }
  1729. GPSD_LOG(LOG_PROG, &session->context->errout,
  1730. "SiRF: NTPD valid time MID 0x34, seen=%#02x, leap=%d\n",
  1731. session->driver.sirf.time_seen,
  1732. session->context->leap_seconds);
  1733. session->driver.sirf.time_seen |= TIME_SEEN_UTC_2;
  1734. mask |= TIME_SET;
  1735. if ( 3 <= session->gpsdata.satellites_visible ) {
  1736. mask |= NTPTIME_IS;
  1737. }
  1738. }
  1739. return mask;
  1740. }
  1741. /* decode Navigation Library Measurement Data MID 28 (0x38) */
  1742. static gps_mask_t sirf_msg_nl(struct gps_device_t *session,
  1743. unsigned char *buf, size_t len)
  1744. {
  1745. if (len != 67)
  1746. return 0;
  1747. switch ( buf[1] ) {
  1748. case 1:
  1749. GPSD_LOG(LOG_PROG, &session->context->errout,
  1750. "SiRF IV: unused NL 0x38, SubID: 1, GPS Data\n");
  1751. break;
  1752. case 2:
  1753. GPSD_LOG(LOG_PROG, &session->context->errout,
  1754. "SiRF IV: unused NL 0x38, SubID: 2, EE Integrity\n");
  1755. break;
  1756. case 3:
  1757. GPSD_LOG(LOG_PROG, &session->context->errout,
  1758. "SiRF IV: unused NL 0x38, SubID: 3, EE Integrity\n");
  1759. break;
  1760. case 4:
  1761. GPSD_LOG(LOG_PROG, &session->context->errout,
  1762. "SiRF IV: unused NL 0x38, SubID: 4, EE Clock Bias\n");
  1763. break;
  1764. case 5:
  1765. GPSD_LOG(LOG_PROG, &session->context->errout,
  1766. "SiRF IV: unused NL 0x38, SubID: 4, 50bps\n");
  1767. break;
  1768. case 32:
  1769. GPSD_LOG(LOG_PROG, &session->context->errout,
  1770. "SiRF IV: unused NL 0x38, SubID: 4, ECLM ACK/NACK\n");
  1771. break;
  1772. case 33:
  1773. GPSD_LOG(LOG_PROG, &session->context->errout,
  1774. "SiRF IV: unused NL 0x38, SubID: 4, ECLM EE Age\n");
  1775. break;
  1776. case 34:
  1777. GPSD_LOG(LOG_PROG, &session->context->errout,
  1778. "SiRF IV: unused NL 0x38, SubID: 4, ECLM SGEE Age\n");
  1779. break;
  1780. case 35:
  1781. GPSD_LOG(LOG_PROG, &session->context->errout,
  1782. "SiRF IV: unused NL 0x38, SubID: 4, ECLM Download Intiate\n");
  1783. break;
  1784. case 255:
  1785. GPSD_LOG(LOG_PROG, &session->context->errout,
  1786. "SiRF IV: unused NL 0x38, SubID: 4, EE ACK\n");
  1787. break;
  1788. default:
  1789. GPSD_LOG(LOG_PROG, &session->context->errout,
  1790. "SiRF IV: unused NL 0x38, unknown SubID: %d\n",
  1791. buf[1]);
  1792. }
  1793. return 0;
  1794. }
  1795. /* decode Extended Ephemeris Data MID 56 (0x38) */
  1796. static gps_mask_t sirf_msg_ee(struct gps_device_t *session,
  1797. unsigned char *buf, size_t len)
  1798. {
  1799. if (len != 67)
  1800. return 0;
  1801. switch ( buf[1] ) {
  1802. case 1:
  1803. GPSD_LOG(LOG_PROG, &session->context->errout,
  1804. "SiRF IV: unused EE 0x40, SubID: 1\n");
  1805. break;
  1806. case 2:
  1807. GPSD_LOG(LOG_PROG, &session->context->errout,
  1808. "SiRF IV: unused EE 0x40, SubID: 2, PRN: %d\n",
  1809. buf[2]);
  1810. break;
  1811. default:
  1812. GPSD_LOG(LOG_PROG, &session->context->errout,
  1813. "SiRF IV: unused EE 0x40, unknown SubID: %d\n",
  1814. buf[1]);
  1815. }
  1816. return 0;
  1817. }
  1818. gps_mask_t sirf_parse(struct gps_device_t * session, unsigned char *buf,
  1819. size_t len)
  1820. {
  1821. if (len == 0)
  1822. return 0;
  1823. buf += 4;
  1824. len -= 8;
  1825. /* cast for 32/64 bit compatiility */
  1826. GPSD_LOG(LOG_RAW, &session->context->errout,
  1827. "SiRF: Raw packet type %#04x len %ld\n", buf[0],
  1828. (long)len);
  1829. session->driver.sirf.lastid = buf[0];
  1830. /* could change if the set of messages we enable does */
  1831. session->cycle_end_reliable = true;
  1832. switch (buf[0]) {
  1833. case 0x02: /* Measure Navigation Data Out MID 2 */
  1834. if ((session->driver.sirf.driverstate & UBLOX) == 0)
  1835. return sirf_msg_navsol(session, buf,
  1836. len) | (CLEAR_IS | REPORT_IS);
  1837. else {
  1838. GPSD_LOG(LOG_PROG, &session->context->errout,
  1839. "SiRF: MID 2 (0x02) MND skipped, u-blox flag is on.\n");
  1840. return 0;
  1841. }
  1842. case 0x04: /* Measured tracker data out MID 4 */
  1843. return sirf_msg_svinfo(session, buf, len);
  1844. case 0x05: /* Raw Tracker Data Out MID 5 */
  1845. GPSD_LOG(LOG_PROG, &session->context->errout,
  1846. "SiRF: unused MID 5 (0x05) Raw Tracker Data\n");
  1847. return 0;
  1848. case 0x06: /* Software Version String MID 6 */
  1849. return sirf_msg_swversion(session, buf, len);
  1850. case 0x07: /* Clock Status Data MID 7 */
  1851. GPSD_LOG(LOG_PROG, &session->context->errout,
  1852. "SiRF: unused MID 7 (0x07) CLK\n");
  1853. return 0;
  1854. case 0x08: /* subframe data MID 8 */
  1855. /* extract leap-second from this */
  1856. /*
  1857. * Chris Kuethe says:
  1858. * "Message 8 is generated as the data is received. It is not
  1859. * buffered on the chip. So when you enable message 8, you'll
  1860. * get one subframe every 6 seconds. Of the data received, the
  1861. * almanac and ephemeris are buffered and stored, so you can
  1862. * query them at will. Alas, the time parameters are not
  1863. * stored, which is really lame, as the UTC-GPS correction
  1864. * changes 1 second every few years. Maybe."
  1865. */
  1866. return sirf_msg_navdata(session, buf, len);
  1867. case 0x09: /* CPU Throughput MID 9 (0x09) */
  1868. GPSD_LOG(LOG_PROG, &session->context->errout,
  1869. "SiRF: THR 0x09: SegStatMax=%.3f, SegStatLat=%3.f, "
  1870. "AveTrkTime=%.3f, Last MS=%u\n",
  1871. (float)getbeu16(buf, 1) / 186, (float)getbeu16(buf, 3) / 186,
  1872. (float)getbeu16(buf, 5) / 186, getbeu16(buf, 7));
  1873. return 0;
  1874. case 0x0a: /* Error ID Data MID 10 */
  1875. return sirf_msg_errors(session, buf, len);
  1876. case 0x0b: /* Command Acknowledgement MID 11 */
  1877. if (2 > len) {
  1878. return 0;
  1879. }
  1880. if (2 == len) {
  1881. GPSD_LOG(LOG_PROG, &session->context->errout,
  1882. "SiRF: ACK 0x0b: %#02x\n", getub(buf, 1));
  1883. } else {
  1884. /* SiRF III+, has ACK ID */
  1885. GPSD_LOG(LOG_PROG, &session->context->errout,
  1886. "SiRF: ACK 0x0b: %#02x/%02x\n",
  1887. getub(buf, 1), getub(buf, 2));
  1888. }
  1889. session->driver.sirf.need_ack = 0;
  1890. return 0;
  1891. case 0x0c: /* Command NAcknowledgement MID 12 */
  1892. if (2 > len) {
  1893. return 0;
  1894. }
  1895. if (2 == len) {
  1896. GPSD_LOG(LOG_PROG, &session->context->errout,
  1897. "SiRF: NACK 0x0c: %#02x\n", getub(buf, 1));
  1898. } else {
  1899. /* SiRF III+, has NACK ID */
  1900. GPSD_LOG(LOG_PROG, &session->context->errout,
  1901. "SiRF: NACK 0x0c: %#02x/%02x\n",
  1902. getub(buf, 1), getub(buf, 2));
  1903. }
  1904. /* ugh -- there's no alternative but silent failure here */
  1905. session->driver.sirf.need_ack = 0;
  1906. return 0;
  1907. case 0x0d: /* Visible List MID 13 */
  1908. /* no data her not already in MID 67,16 */
  1909. GPSD_LOG(LOG_PROG, &session->context->errout,
  1910. "SiRF: unused MID 13 (0x0d) Visible List, len %zd\n", len);
  1911. return 0;
  1912. case 0x0e: /* Almanac Data MID 14 */
  1913. GPSD_LOG(LOG_PROG, &session->context->errout,
  1914. "SiRF: unused MID 14 (0x0e) ALM\n");
  1915. return 0;
  1916. case 0x0f: /* Ephemeris Data MID 15 */
  1917. GPSD_LOG(LOG_PROG, &session->context->errout,
  1918. "SiRF: unused MID 15 (0x0f) EPH\n");
  1919. return 0;
  1920. case 0x11: /* Differential Corrections MID 17 */
  1921. GPSD_LOG(LOG_PROG, &session->context->errout,
  1922. "SiRF: unused MID 17 (0x11) DIFF\n");
  1923. return 0;
  1924. case 0x12: /* OK To Send MID 18 (0x12) */
  1925. GPSD_LOG(LOG_PROG, &session->context->errout,
  1926. "SiRF: MID 18 (0x12) OkToSend: OK = %d\n",
  1927. getub(buf, 1));
  1928. return 0;
  1929. case 0x13: /* Navigation Parameters MID 19 (0x13) */
  1930. return sirf_msg_sysparam(session, buf, len);
  1931. case 0x1b: /* DGPS status MID 27 */
  1932. return sirf_msg_dgpsstatus(session, buf, len);
  1933. case 0x1c: /* Navigation Library Measurement Data MID 28 */
  1934. return sirf_msg_nlmd(session, buf, len);
  1935. case 0x1d: /* Navigation Library DGPS Data MID 29 */
  1936. GPSD_LOG(LOG_PROG, &session->context->errout,
  1937. "SiRF: unused MID 29 (0x1d) NLDG\n");
  1938. return 0;
  1939. case 0x1e: /* Navigation Library SV State Data MID 30 */
  1940. GPSD_LOG(LOG_PROG, &session->context->errout,
  1941. "SiRF: unused MID 30 (0x1e) NLSV\n");
  1942. return 0;
  1943. case 0x1f: /* Navigation Library Initialization Data MID 31 */
  1944. GPSD_LOG(LOG_PROG, &session->context->errout,
  1945. "SiRF: unused MID 32 (0x1f) NLID\n");
  1946. return 0;
  1947. case 0x29: /* Geodetic Navigation Data MID 41 */
  1948. GPSD_LOG(LOG_PROG, &session->context->errout,
  1949. "SiRF: unused MID 41 (0x29) Geodetic Nav Data\n");
  1950. return 0;
  1951. case 0x32: /* SBAS corrections MID 50 */
  1952. GPSD_LOG(LOG_PROG, &session->context->errout,
  1953. "SiRF: unused MID 50 (0x32) SBAS\n");
  1954. return 0;
  1955. case 0x33: /* MID_SiRFNavNotification MID 51, 0x33 */
  1956. return sirf_msg_navnot(session, buf, len);
  1957. case 0x34: /* PPS Time MID 52 */
  1958. /*
  1959. * Carl Carter from SiRF writes: "We do not output on the
  1960. * second (unless you are using MID 52). We make
  1961. * measurements in the receiver in time with an internal
  1962. * counter that is not slaved to GPS time, so the measurements
  1963. * are made at a time that wanders around the second. Then,
  1964. * after the measurements are made (all normalized to the same
  1965. * point in time) we dispatch the navigation software to make
  1966. * a solution, and that solution comes out some 200 to 300 ms
  1967. * after the measurement time. So you may get a message at
  1968. * 700 ms after the second that uses measurements time tagged
  1969. * 450 ms after the second. And if some other task jumps up
  1970. * and delays things, that message may not come out until 900
  1971. * ms after the second. Things can get out of sync to the
  1972. * point that if you try to resolve the GPS time of our 1 PPS
  1973. * pulses using the navigation messages, you will find it
  1974. * impossible to be consistent. That is why I added
  1975. * MID 52 to our system -- it is tied to the creation of the 1
  1976. * PPS and always comes out right around the top of the
  1977. * second."
  1978. */
  1979. return sirf_msg_ppstime(session, buf, len);
  1980. case 0x38: /* EE Output MID 56 */
  1981. return sirf_msg_ee(session, buf, len);
  1982. case 0x40: /* Nav Library MID 64 */
  1983. return sirf_msg_nl(session, buf, len);
  1984. case 0x43: /* Multiconstellation Nav Data Response MID 67 */
  1985. return sirf_msg_67(session, buf, len);
  1986. case 0x47: /* Hardware Config MID 71 */
  1987. /* MID_HW_CONFIG_REQ */
  1988. GPSD_LOG(LOG_PROG, &session->context->errout,
  1989. "SiRF IV: unused MID 71 (0x47) Hardware Config Request, "
  1990. "len %zd\n", len);
  1991. return 0;
  1992. case 0x51: /* MID_QUERY_RESP MID 81 */
  1993. return sirf_msg_qresp(session, buf, len);
  1994. case 0x5c: /* Controller Interference Report MID 92 */
  1995. GPSD_LOG(LOG_PROG, &session->context->errout,
  1996. "SiRF IV: unused MID 92 (0x5c) CW Interference Report\n");
  1997. return 0;
  1998. case 0x5d: /* TCXO Output MID 93 */
  1999. return sirf_msg_tcxo(session, buf, len);
  2000. case 0x62: /* u-blox Extended Measured Navigation Data MID 98 */
  2001. GPSD_LOG(LOG_PROG, &session->context->errout,
  2002. "SiRF: MID 98 (0x62) u-blox EMND\n");
  2003. return sirf_msg_ublox(session, buf, len) | (CLEAR_IS | REPORT_IS);
  2004. case 0x80: /* Initialize Data Source MID 128 */
  2005. GPSD_LOG(LOG_PROG, &session->context->errout,
  2006. "SiRF: unused MID 128 (0x80) INIT\n");
  2007. return 0;
  2008. case 0xe1: /* statistics messages MID 225 */
  2009. return sirf_msg_stats(session, buf, len);
  2010. case 0xff: /* Debug messages MID 255 */
  2011. (void)sirf_msg_debug(session, buf, len);
  2012. return 0;
  2013. default:
  2014. GPSD_LOG(LOG_PROG, &session->context->errout,
  2015. "SiRF: Unknown packet id %d (%#x) length %zd\n",
  2016. buf[0], buf[0], len);
  2017. return 0;
  2018. }
  2019. }
  2020. static gps_mask_t sirfbin_parse_input(struct gps_device_t *session)
  2021. {
  2022. if (session->lexer.type == SIRF_PACKET) {
  2023. return sirf_parse(session, session->lexer.outbuffer,
  2024. session->lexer.outbuflen);
  2025. #ifdef NMEA0183_ENABLE
  2026. } else if (session->lexer.type == NMEA_PACKET) {
  2027. return nmea_parse((char *)session->lexer.outbuffer, session);
  2028. #endif /* NMEA0183_ENABLE */
  2029. } else
  2030. return 0;
  2031. }
  2032. static void sirfbin_init_query(struct gps_device_t *session)
  2033. {
  2034. GPSD_LOG(LOG_PROG, &session->context->errout,
  2035. "SiRF: Probing for firmware version.\n");
  2036. /* reset binary init steps */
  2037. session->cfg_stage = 0;
  2038. session->cfg_step = 0;
  2039. /* MID 132 */
  2040. (void)sirf_write(session, versionprobe);
  2041. /* ask twice, SiRF IV on USB often misses the first request */
  2042. (void)sirf_write(session, versionprobe);
  2043. }
  2044. static void sirfbin_event_hook(struct gps_device_t *session, event_t event)
  2045. {
  2046. static unsigned char moderevert[] = {
  2047. 0xa0, 0xa2, 0x00, 0x0e,
  2048. 0x88,
  2049. 0x00, 0x00, /* pad bytes */
  2050. 0x00, /* degraded mode */
  2051. 0x00, 0x00, /* pad bytes */
  2052. 0x00, 0x00, /* altitude source */
  2053. 0x00, /* altitude hold mode */
  2054. 0x00, /* use last computed alt */
  2055. 0x00, /* reserved */
  2056. 0x00, /* degraded mode timeout */
  2057. 0x00, /* dead reckoning timeout */
  2058. 0x00, /* track smoothing */
  2059. 0x00, 0x00, 0xb0, 0xb3
  2060. };
  2061. if (session->context->readonly)
  2062. return;
  2063. switch (event) {
  2064. case event_identified:
  2065. /* FALLTHROUGH */
  2066. case event_reactivate:
  2067. if (session->lexer.type == NMEA_PACKET) {
  2068. GPSD_LOG(LOG_PROG, &session->context->errout,
  2069. "SiRF: Switching chip mode to binary.\n");
  2070. (void)nmea_send(session,
  2071. "$PSRF100,0,%d,8,1,0",
  2072. session->gpsdata.dev.baudrate);
  2073. }
  2074. break;
  2075. case event_configure:
  2076. /* This wakes up on every received packet.
  2077. * Use this hook to step, slowly, through the init messages.
  2078. * We try, but not always succeed, to wait for the ACK/NACK.
  2079. * Send a message only every 15 times so we get an ACK/NACK
  2080. * before next one.
  2081. *
  2082. * This tries to avoid overrunning the input buffer, and makes
  2083. * it much easier to identify which messages get a NACK
  2084. */
  2085. if (UINT_MAX == session->cfg_stage) {
  2086. /* init done */
  2087. return;
  2088. }
  2089. session->cfg_step++;
  2090. if ((0 < session->driver.sirf.need_ack) &&
  2091. (15 > session->cfg_step)) {
  2092. /* we are waiting for ACK, just wait for 15 messages */
  2093. return;
  2094. }
  2095. session->cfg_step = 0;
  2096. session->cfg_stage++;
  2097. GPSD_LOG(LOG_PROG, &session->context->errout, "stage: %d\n",
  2098. session->cfg_stage);
  2099. switch (session->cfg_stage) {
  2100. case 0:
  2101. /* this slot used by event_identified */
  2102. return;
  2103. case 1:
  2104. (void)sirf_write(session, versionprobe);
  2105. break;
  2106. #ifdef RECONFIGURE_ENABLE
  2107. case 2:
  2108. /* unset MID 0x40 = 64 first since there is a flood of them */
  2109. GPSD_LOG(LOG_PROG, &session->context->errout,
  2110. "SiRF: unset MID 0x40.\n");
  2111. unsetmidXX[5] = 1; /* enable/disable */
  2112. unsetmidXX[6] = 0x40; /* MID 0x40 */
  2113. (void)sirf_write(session, unsetmidXX);
  2114. break;
  2115. case 3:
  2116. /*
  2117. * The response to this request will save the navigation
  2118. * parameters so they can be reverted before close.
  2119. */
  2120. GPSD_LOG(LOG_PROG, &session->context->errout,
  2121. "SiRF: Requesting navigation parameters.\n");
  2122. (void)sirf_write(session, navparams);
  2123. break;
  2124. case 4:
  2125. /* unset GND (0x29 = 41), it's not reliable on SiRF II */
  2126. GPSD_LOG(LOG_PROG, &session->context->errout,
  2127. "SiRF: unset MID 0x29.\n");
  2128. unsetmidXX[5] = 1; /* enable/disable */
  2129. unsetmidXX[6] = 0x29; /* MID 0x29 */
  2130. (void)sirf_write(session, unsetmidXX);
  2131. break;
  2132. case 5:
  2133. if (!session->context->readonly) {
  2134. GPSD_LOG(LOG_PROG, &session->context->errout,
  2135. "SiRF: Setting Navigation Parameters.\n");
  2136. (void)sirf_write(session, modecontrol);
  2137. }
  2138. break;
  2139. case 6:
  2140. GPSD_LOG(LOG_PROG, &session->context->errout,
  2141. "SiRF: Requesting periodic ecef reports.\n");
  2142. (void)sirf_write(session, requestecef);
  2143. break;
  2144. case 7:
  2145. GPSD_LOG(LOG_PROG, &session->context->errout,
  2146. "SiRF: Requesting periodic tracker reports.\n");
  2147. (void)sirf_write(session, requesttracker);
  2148. break;
  2149. case 8:
  2150. GPSD_LOG(LOG_PROG, &session->context->errout,
  2151. "SiRF: Setting DGPS control to use SBAS.\n");
  2152. (void)sirf_write(session, dgpscontrol);
  2153. break;
  2154. case 9:
  2155. GPSD_LOG(LOG_PROG, &session->context->errout,
  2156. "SiRF: Setting SBAS to auto/integrity mode.\n");
  2157. (void)sirf_write(session, sbasparams);
  2158. break;
  2159. case 10:
  2160. GPSD_LOG(LOG_PROG, &session->context->errout,
  2161. "SiRF: Enabling PPS message MID 52 (0x32).\n");
  2162. /* Not supported on some GPS.
  2163. * It will be NACKed is not supported */
  2164. (void)sirf_write(session, enablemid52);
  2165. break;
  2166. case 11:
  2167. /* SiRF recommends at least 57600 for SiRF IV subframe data */
  2168. if (session->gpsdata.dev.baudrate >= 57600) {
  2169. /* fast enough, turn on subframe data */
  2170. GPSD_LOG(LOG_PROG, &session->context->errout,
  2171. "SiRF: Enabling subframe transmission.\n");
  2172. (void)sirf_write(session, enablesubframe);
  2173. } else {
  2174. /* too slow, turn off subframe data */
  2175. GPSD_LOG(LOG_PROG, &session->context->errout,
  2176. "SiRF: Disabling subframe transmission.\n");
  2177. (void)sirf_write(session, disablesubframe);
  2178. }
  2179. break;
  2180. case 12:
  2181. /*
  2182. * Disable navigation debug messages (the value 5 is magic)
  2183. * must be done *after* subframe enable.
  2184. */
  2185. GPSD_LOG(LOG_PROG, &session->context->errout,
  2186. "SiRF: disable MID 7, 28, 29, 30, 31.\n");
  2187. unsetmidXX[5] = 5;
  2188. unsetmidXX[6] = 0;
  2189. (void)sirf_write(session, unsetmidXX);
  2190. break;
  2191. #endif /* RECONFIGURE_ENABLE */
  2192. default:
  2193. /* initialization is done */
  2194. session->cfg_stage = UINT_MAX;
  2195. session->cfg_step = 0;
  2196. return;
  2197. }
  2198. break;
  2199. case event_deactivate:
  2200. putbyte(moderevert, 7, session->driver.sirf.degraded_mode);
  2201. putbe16(moderevert, 10, session->driver.sirf.altitude_source_input);
  2202. putbyte(moderevert, 12, session->driver.sirf.altitude_hold_mode);
  2203. putbyte(moderevert, 13, session->driver.sirf.altitude_hold_source);
  2204. putbyte(moderevert, 15, session->driver.sirf.degraded_timeout);
  2205. putbyte(moderevert, 16, session->driver.sirf.dr_timeout);
  2206. putbyte(moderevert, 17, session->driver.sirf.track_smooth_mode);
  2207. GPSD_LOG(LOG_PROG, &session->context->errout,
  2208. "SiRF: Reverting navigation parameters...\n");
  2209. (void)sirf_write(session, moderevert);
  2210. break;
  2211. case event_driver_switch:
  2212. /* do what here? */
  2213. break;
  2214. case event_triggermatch:
  2215. /* do what here? */
  2216. break;
  2217. case event_wakeup:
  2218. /* do what here? */
  2219. break;
  2220. }
  2221. }
  2222. /* this is everything we export */
  2223. /* *INDENT-OFF* */
  2224. const struct gps_type_t driver_sirf =
  2225. {
  2226. .type_name = "SiRF", /* full name of type */
  2227. .packet_type = SIRF_PACKET, /* associated lexer packet type */
  2228. .flags = DRIVER_STICKY, /* remember this */
  2229. .trigger = NULL, /* no trigger */
  2230. .channels = SIRF_CHANNELS, /* consumer-grade GPS */
  2231. .probe_detect = NULL, /* no probe */
  2232. .get_packet = generic_get, /* be prepared for SiRF or NMEA */
  2233. .parse_packet = sirfbin_parse_input,/* parse message packets */
  2234. .rtcm_writer = gpsd_write, /* send RTCM data straight */
  2235. .init_query = sirfbin_init_query,/* non-perturbing initial qury */
  2236. .event_hook = sirfbin_event_hook,/* lifetime event handler */
  2237. #ifdef RECONFIGURE_ENABLE
  2238. .speed_switcher = sirfbin_speed, /* we can change baud rate */
  2239. .mode_switcher = sirfbin_mode, /* there's a mode switcher */
  2240. .rate_switcher = NULL, /* no sample-rate switcher */
  2241. .min_cycle.tv_sec = 1, /* not relevant, no rate switch */
  2242. .min_cycle.tv_nsec = 0, /* not relevant, no rate switch */
  2243. #endif /* RECONFIGURE_ENABLE */
  2244. #ifdef CONTROLSEND_ENABLE
  2245. .control_send = sirf_control_send,/* how to send a control string */
  2246. #endif /* CONTROLSEND_ENABLE */
  2247. .time_offset = sirf_time_offset,
  2248. };
  2249. /* *INDENT-ON* */
  2250. #endif /* defined(SIRF_ENABLE) && defined(BINARY_ENABLE) */