driver_nmea0183.c 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554
  1. /*
  2. * This file is Copyright (c) 2010-2018 by the GPSD project
  3. * SPDX-License-Identifier: BSD-2-clause
  4. */
  5. #include "gpsd_config.h" /* must be before all includes */
  6. #include <ctype.h> /* for isdigit() */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdbool.h>
  10. #include <math.h>
  11. #include <string.h>
  12. #include <stdarg.h>
  13. #include <time.h>
  14. #include "gpsd.h"
  15. #include "strfuncs.h"
  16. #ifdef NMEA0183_ENABLE
  17. #include "timespec.h"
  18. /**************************************************************************
  19. *
  20. * Parser helpers begin here
  21. *
  22. **************************************************************************/
  23. /* process a pair of latitude/longitude fields starting at field index BEGIN
  24. * The input fields look like this:
  25. * field[0]: 4404.1237962
  26. * field[1]: N
  27. * field[2]: 12118.8472460
  28. * field[3]: W
  29. * input format of lat/lon is NMEA style DDDMM.mmmmmmm
  30. * yes, 7 digits of precision from survey grade GPS
  31. *
  32. * return: 0 == OK, non zero is failure.
  33. */
  34. static int do_lat_lon(char *field[], struct gps_fix_t *out)
  35. {
  36. double d, m;
  37. double lon;
  38. double lat;
  39. if ('\0' == field[0][0] ||
  40. '\0' == field[1][0] ||
  41. '\0' == field[2][0] ||
  42. '\0' == field[3][0]) {
  43. return 1;
  44. }
  45. lat = safe_atof(field[0]);
  46. m = 100.0 * modf(lat / 100.0, &d);
  47. lat = d + m / 60.0;
  48. if ('S' == field[1][0])
  49. lat = -lat;
  50. lon = safe_atof(field[2]);
  51. m = 100.0 * modf(lon / 100.0, &d);
  52. lon = d + m / 60.0;
  53. if ('W' == field[3][0])
  54. lon = -lon;
  55. if (0 == isfinite(lat) ||
  56. 0 == isfinite(lon)) {
  57. return 2;
  58. }
  59. out->latitude = lat;
  60. out->longitude = lon;
  61. return 0;
  62. }
  63. /* process an FAA mode character
  64. * return status as in session->gpsdata.status
  65. */
  66. static int faa_mode(char mode)
  67. {
  68. int newstatus = STATUS_FIX;
  69. switch (mode) {
  70. case '\0': /* missing */
  71. newstatus = STATUS_NO_FIX;
  72. break;
  73. case 'A': /* Autonomous */
  74. default:
  75. newstatus = STATUS_FIX;
  76. break;
  77. case 'D': /* Differential */
  78. newstatus = STATUS_DGPS_FIX;
  79. break;
  80. case 'E': /* Estimated dead reckoning */
  81. newstatus = STATUS_DR;
  82. break;
  83. case 'F': /* Float RTK */
  84. newstatus = STATUS_RTK_FLT;
  85. break;
  86. case 'N': /* Data Not Valid */
  87. /* already handled, for paranoia sake also here */
  88. newstatus = STATUS_NO_FIX;
  89. break;
  90. case 'P': /* Precise (NMEA 4+) */
  91. newstatus = STATUS_DGPS_FIX; /* sort of DGPS */
  92. break;
  93. case 'R': /* fixed RTK */
  94. newstatus = STATUS_RTK_FIX;
  95. break;
  96. case 'S': /* simulator */
  97. newstatus = STATUS_NO_FIX; /* or maybe MODE_FIX? */
  98. break;
  99. }
  100. return newstatus;
  101. }
  102. /**************************************************************************
  103. *
  104. * Scary timestamp fudging begins here
  105. *
  106. * Four sentences, GGA and GLL and RMC and ZDA, contain timestamps.
  107. * GGA/GLL/RMC timestamps look like hhmmss.ss, with the trailing .ss,
  108. * or .sss, part optional.
  109. * RMC has a date field, in the format ddmmyy. ZDA has separate fields
  110. * for day/month/year, with a 4-digit year. This means that for RMC we
  111. * must supply a century and for GGA and GLL we must supply a century,
  112. * year, and day. We get the missing data from a previous RMC or ZDA;
  113. * century in RMC is supplied from the daemon's context (initialized at
  114. * startup time) if there has been no previous ZDA.
  115. *
  116. **************************************************************************/
  117. #define DD(s) ((int)((s)[0]-'0')*10+(int)((s)[1]-'0'))
  118. /* sentence supplied ddmmyy, but no century part
  119. *
  120. * return: 0 == OK, greater than zero on failure
  121. */
  122. static int merge_ddmmyy(char *ddmmyy, struct gps_device_t *session)
  123. {
  124. int yy;
  125. int mon;
  126. int mday;
  127. int year;
  128. unsigned i; /* NetBSD complains about signed array index */
  129. if (NULL == ddmmyy) {
  130. return 1;
  131. }
  132. for (i = 0; i < 6; i++) {
  133. /* NetBSD 6 wants the cast */
  134. if (0 == isdigit((int)ddmmyy[i])) {
  135. /* catches NUL and non-digits */
  136. /* Telit HE910 can set year to "-1" (1999 - 2000) */
  137. GPSD_LOG(LOG_WARN, &session->context->errout,
  138. "merge_ddmmyy(%s), malformed date\n", ddmmyy);
  139. return 2;
  140. }
  141. }
  142. /* check for termination */
  143. if ('\0' != ddmmyy[6]) {
  144. /* missing NUL */
  145. GPSD_LOG(LOG_WARN, &session->context->errout,
  146. "merge_ddmmyy(%s), malformed date\n", ddmmyy);
  147. return 3;
  148. }
  149. /* should be no defects left to segfault DD() */
  150. yy = DD(ddmmyy + 4);
  151. mon = DD(ddmmyy + 2);
  152. mday = DD(ddmmyy);
  153. /* check for century wrap */
  154. if (session->nmea.date.tm_year % 100 == 99 && yy == 0)
  155. gpsd_century_update(session, session->context->century + 100);
  156. year = (session->context->century + yy);
  157. /* 32 bit systems will break in 2038.
  158. * Telix fails on GPS rollover to 2099, which 32 bit system
  159. * can not handle. So wrap at 2080. That way 64 bit systems
  160. * work until 2080, and 2099 gets reported as 1999.
  161. * since GPS epoch started in 1980, allows for old NMEA to work.
  162. */
  163. if (2080 <= year) {
  164. year -= 100;
  165. }
  166. if ( (1 > mon ) || (12 < mon ) ) {
  167. GPSD_LOG(LOG_WARN, &session->context->errout,
  168. "merge_ddmmyy(%s), malformed month\n", ddmmyy);
  169. return 4;
  170. } else if ( (1 > mday ) || (31 < mday ) ) {
  171. GPSD_LOG(LOG_WARN, &session->context->errout,
  172. "merge_ddmmyy(%s), malformed day\n", ddmmyy);
  173. return 5;
  174. } else {
  175. GPSD_LOG(LOG_DATA, &session->context->errout,
  176. "merge_ddmmyy(%s) sets year %d\n",
  177. ddmmyy, year);
  178. session->nmea.date.tm_year = year - 1900;
  179. session->nmea.date.tm_mon = mon - 1;
  180. session->nmea.date.tm_mday = mday;
  181. }
  182. return 0;
  183. }
  184. /* decode an hhmmss.ss string into struct tm data and nsecs
  185. *
  186. * return: 0 == OK, otherwise failure
  187. */
  188. static int decode_hhmmss(struct tm *date, long *nsec, char *hhmmss,
  189. struct gps_device_t *session)
  190. {
  191. int old_hour = date->tm_hour;
  192. int i, sublen;
  193. if (NULL == hhmmss) {
  194. return 1;
  195. }
  196. for (i = 0; i < 6; i++) {
  197. /* NetBSD 6 wants the cast */
  198. if (0 == isdigit((int)hhmmss[i])) {
  199. /* catches NUL and non-digits */
  200. GPSD_LOG(LOG_WARN, &session->context->errout,
  201. "decode_hhmmss(%s), malformed time\n", hhmmss);
  202. return 2;
  203. }
  204. }
  205. /* don't check for termination, might have fractional seconds */
  206. date->tm_hour = DD(hhmmss);
  207. if (date->tm_hour < old_hour) /* midnight wrap */
  208. date->tm_mday++;
  209. date->tm_min = DD(hhmmss + 2);
  210. date->tm_sec = DD(hhmmss + 4);
  211. if ('.' == hhmmss[6] &&
  212. /* NetBSD 6 wants the cast */
  213. 0 != isdigit((int)hhmmss[7])) {
  214. i = atoi(hhmmss + 7);
  215. sublen = strlen(hhmmss + 7);
  216. *nsec = (long)i * (long)pow(10.0, 9 - sublen);
  217. } else {
  218. *nsec = 0;
  219. }
  220. return 0;
  221. }
  222. /* update from a UTC time
  223. *
  224. * return: 0 == OK, greater than zero on failure
  225. */
  226. static int merge_hhmmss(char *hhmmss, struct gps_device_t *session)
  227. {
  228. int old_hour = session->nmea.date.tm_hour;
  229. int i, sublen;
  230. if (NULL == hhmmss) {
  231. return 1;
  232. }
  233. for (i = 0; i < 6; i++) {
  234. /* NetBSD 6 wants the cast */
  235. if (0 == isdigit((int)hhmmss[i])) {
  236. /* catches NUL and non-digits */
  237. GPSD_LOG(LOG_WARN, &session->context->errout,
  238. "merge_hhmmss(%s), malformed time\n", hhmmss);
  239. return 2;
  240. }
  241. }
  242. /* don't check for termination, might have fractional seconds */
  243. session->nmea.date.tm_hour = DD(hhmmss);
  244. if (session->nmea.date.tm_hour < old_hour) /* midnight wrap */
  245. session->nmea.date.tm_mday++;
  246. session->nmea.date.tm_min = DD(hhmmss + 2);
  247. session->nmea.date.tm_sec = DD(hhmmss + 4);
  248. session->nmea.subseconds.tv_sec = 0;
  249. if ('.' == hhmmss[6] &&
  250. /* NetBSD 6 wants the cast */
  251. 0 != isdigit((int)hhmmss[7])) {
  252. i = atoi(hhmmss + 7);
  253. sublen = strlen(hhmmss + 7);
  254. session->nmea.subseconds.tv_nsec = (long)i *
  255. (long)pow(10.0, 9 - sublen);
  256. } else {
  257. session->nmea.subseconds.tv_nsec = 0;
  258. }
  259. return 0;
  260. }
  261. static void register_fractional_time(const char *tag, const char *fld,
  262. struct gps_device_t *session)
  263. {
  264. char ts_buf[TIMESPEC_LEN];
  265. if (fld[0] != '\0') {
  266. session->nmea.last_frac_time = session->nmea.this_frac_time;
  267. DTOTS(&session->nmea.this_frac_time, safe_atof(fld));
  268. session->nmea.latch_frac_time = true;
  269. GPSD_LOG(LOG_DATA, &session->context->errout,
  270. "%s: registers fractional time %s\n",
  271. tag,
  272. timespec_str(&session->nmea.this_frac_time, ts_buf,
  273. sizeof(ts_buf)));
  274. }
  275. }
  276. /**************************************************************************
  277. *
  278. * NMEA sentence handling begins here
  279. *
  280. **************************************************************************/
  281. /* process xxVTG
  282. * $GPVTG,054.7,T,034.4,M,005.5,N,010.2,K
  283. * $GPVTG,054.7,T,034.4,M,005.5,N,010.2,K,A
  284. *
  285. * where:
  286. * 1,2 054.7,T True track made good (degrees)
  287. * 3,4 034.4,M Magnetic track made good
  288. * 5,6 005.5,N Ground speed, knots
  289. * 7,8 010.2,K Ground speed, Kilometers per hour
  290. * 9 A Mode Indicator (optional)
  291. * see faa_mode() for possible mode values
  292. *
  293. * see also:
  294. * https://gpsd.gitlab.io/gpsd/NMEA.html#_vtg_track_made_good_and_ground_speed
  295. */
  296. static gps_mask_t processVTG(int count,
  297. char *field[],
  298. struct gps_device_t *session)
  299. {
  300. gps_mask_t mask = ONLINE_SET;
  301. if( (field[1][0] == '\0') || (field[5][0] == '\0')){
  302. return mask;
  303. }
  304. /* ignore empty/missing field, fix mode of last resort */
  305. if ((count > 9) && ('\0' != field[9][0])) {
  306. switch (field[9][0]) {
  307. case 'A':
  308. /* Autonomous, 2D or 3D fix */
  309. /* FALL THROUGH */
  310. case 'D':
  311. /* Differential, 2D or 3D fix */
  312. // MODE_SET here causes issues
  313. // mask |= MODE_SET;
  314. break;
  315. case 'E':
  316. /* Estimated, DR only */
  317. /* FALL THROUGH */
  318. case 'N':
  319. /* Not Valid */
  320. // MODE_SET here causes issues
  321. // mask |= MODE_SET;
  322. // nothing to use here, leave
  323. return mask;
  324. default:
  325. /* Huh? */
  326. break;
  327. }
  328. }
  329. // set true track
  330. session->newdata.track = safe_atof(field[1]);
  331. mask |= TRACK_SET;
  332. // set magnetic variation
  333. if (field[3][0] != '\0'){ // ignore empty fields
  334. session->newdata.magnetic_track = safe_atof(field[3]);
  335. mask |= MAGNETIC_TRACK_SET;
  336. }
  337. session->newdata.speed = safe_atof(field[5]) * KNOTS_TO_MPS;
  338. mask |= SPEED_SET;
  339. GPSD_LOG(LOG_DATA, &session->context->errout,
  340. "VTG: course(T)=%.2f, course(M)=%.2f, speed=%.2f",
  341. session->newdata.track, session->newdata.magnetic_track,
  342. session->newdata.speed);
  343. return mask;
  344. }
  345. /* Recommend Minimum Course Specific GPS/TRANSIT Data */
  346. static gps_mask_t processRMC(int count, char *field[],
  347. struct gps_device_t *session)
  348. {
  349. /*
  350. * RMC,225446.33,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E,A*68
  351. * 1 225446.33 Time of fix 22:54:46 UTC
  352. * 2 A Status of Fix:
  353. * A = Autonomous, valid;
  354. * D = Differential, valid;
  355. * V = invalid
  356. * 3,4 4916.45,N Latitude 49 deg. 16.45 min North
  357. * 5,6 12311.12,W Longitude 123 deg. 11.12 min West
  358. * 7 000.5 Speed over ground, Knots
  359. * 8 054.7 Course Made Good, True north
  360. * 9 181194 Date of fix 18 November 1994
  361. * 10,11 020.3,E Magnetic variation 20.3 deg East
  362. * 12 A FAA mode indicator (NMEA 2.3 and later)
  363. * see faa_mode() for possible mode values
  364. * 13 V Nav Status (NMEA 4.1 and later)
  365. * A=autonomous,
  366. * D=differential,
  367. * E=Estimated,
  368. * M=Manual input mode
  369. * N=not valid,
  370. * S=Simulator,
  371. * V = Valid
  372. * *68 mandatory nmea_checksum
  373. *
  374. * SiRF chipsets don't return either Mode Indicator or magnetic variation.
  375. */
  376. gps_mask_t mask = ONLINE_SET;
  377. char status = field[2][0];
  378. int newstatus;
  379. switch (status) {
  380. default:
  381. /* missing */
  382. /* FALLTHROUGH */
  383. case 'V':
  384. /* Invalid */
  385. session->gpsdata.status = STATUS_NO_FIX;
  386. session->newdata.mode = MODE_NO_FIX;
  387. mask |= STATUS_SET | MODE_SET;
  388. break;
  389. case 'D':
  390. /* Differential Fix */
  391. /* FALLTHROUGH */
  392. case 'A':
  393. /* Valid Fix */
  394. /*
  395. * The MTK3301, Royaltek RGM-3800, and possibly other
  396. * devices deliver bogus time values when the navigation
  397. * warning bit is set.
  398. */
  399. if ('\0' != field[1][0] &&
  400. 9 < count &&
  401. '\0' != field[9][0]) {
  402. if (0 == merge_hhmmss(field[1], session) &&
  403. 0 == merge_ddmmyy(field[9], session)) {
  404. /* got a good data/time */
  405. mask |= TIME_SET;
  406. register_fractional_time(field[0], field[1], session);
  407. }
  408. }
  409. /* else, no point to the time only case, no regressions with that */
  410. if (0 == do_lat_lon(&field[3], &session->newdata)) {
  411. newstatus = STATUS_FIX;
  412. mask |= LATLON_SET;
  413. if (MODE_2D >= session->gpsdata.fix.mode) {
  414. /* we have at least a 2D fix */
  415. /* might cause blinking */
  416. session->newdata.mode = MODE_2D;
  417. mask |= MODE_SET;
  418. }
  419. } else {
  420. newstatus = STATUS_NO_FIX;
  421. session->newdata.mode = MODE_NO_FIX;
  422. mask |= MODE_SET;
  423. }
  424. if ('\0' != field[7][0]) {
  425. session->newdata.speed = safe_atof(field[7]) * KNOTS_TO_MPS;
  426. mask |= SPEED_SET;
  427. }
  428. if ('\0' != field[8][0]) {
  429. session->newdata.track = safe_atof(field[8]);
  430. mask |= TRACK_SET;
  431. }
  432. /* get magnetic variation */
  433. if ('\0' != field[10][0] &&
  434. '\0' != field[11][0]) {
  435. session->newdata.magnetic_var = safe_atof(field[10]);
  436. switch (field[11][0]) {
  437. case 'E':
  438. /* no change */
  439. break;
  440. case 'W':
  441. session->newdata.magnetic_var = -session->newdata.magnetic_var;
  442. break;
  443. default:
  444. /* huh? */
  445. session->newdata.magnetic_var = NAN;
  446. break;
  447. }
  448. if (0 == isfinite(session->newdata.magnetic_var) ||
  449. 0.09 >= fabs(session->newdata.magnetic_var)) {
  450. /* some GPS set 0.0,E, or 0,w instead of blank */
  451. session->newdata.magnetic_var = NAN;
  452. } else {
  453. mask |= MAGNETIC_TRACK_SET;
  454. }
  455. }
  456. if (count >= 12) {
  457. newstatus = faa_mode(field[12][0]);
  458. }
  459. /*
  460. * This copes with GPSes like the Magellan EC-10X that *only* emit
  461. * GPRMC. In this case we set mode and status here so the client
  462. * code that relies on them won't mistakenly believe it has never
  463. * received a fix.
  464. */
  465. if (3 < session->gpsdata.satellites_used) {
  466. /* 4 sats used means 3D */
  467. session->newdata.mode = MODE_3D;
  468. mask |= MODE_SET;
  469. } else if (0 != isfinite(session->gpsdata.fix.altHAE) ||
  470. 0 != isfinite(session->gpsdata.fix.altMSL)) {
  471. /* we probably have at least a 3D fix */
  472. /* this handles old GPS that do not report 3D */
  473. session->newdata.mode = MODE_3D;
  474. mask |= MODE_SET;
  475. }
  476. session->gpsdata.status = newstatus;
  477. }
  478. GPSD_LOG(LOG_DATA, &session->context->errout,
  479. "RMC: ddmmyy=%s hhmmss=%s lat=%.2f lon=%.2f "
  480. "speed=%.2f track=%.2f mode=%d var=%.1f status=%d\n",
  481. field[9], field[1],
  482. session->newdata.latitude,
  483. session->newdata.longitude,
  484. session->newdata.speed,
  485. session->newdata.track,
  486. session->newdata.mode,
  487. session->newdata.magnetic_var,
  488. session->gpsdata.status);
  489. return mask;
  490. }
  491. /* Geographic position - Latitude, Longitude */
  492. static gps_mask_t processGLL(int count, char *field[],
  493. struct gps_device_t *session)
  494. {
  495. /* Introduced in NMEA 3.0.
  496. *
  497. * $GPGLL,4916.45,N,12311.12,W,225444,A,A*5C
  498. *
  499. * 1,2: 4916.46,N Latitude 49 deg. 16.45 min. North
  500. * 3,4: 12311.12,W Longitude 123 deg. 11.12 min. West
  501. * 5: 225444 Fix taken at 22:54:44 UTC
  502. * 6: A Data valid
  503. * 7: A Autonomous mode
  504. * 8: *5C Mandatory NMEA checksum
  505. *
  506. * 1,2 Latitude, N (North) or S (South)
  507. * 3,4 Longitude, E (East) or W (West)
  508. * 5 UTC of position
  509. * 6 A = Active, V = Invalid data
  510. * 7 Mode Indicator
  511. * See faa_mode() for possible mode values.
  512. *
  513. * I found a note at <http://www.secoh.ru/windows/gps/nmfqexep.txt>
  514. * indicating that the Garmin 65 does not return time and status.
  515. * SiRF chipsets don't return the Mode Indicator.
  516. * This code copes gracefully with both quirks.
  517. *
  518. * Unless you care about the FAA indicator, this sentence supplies nothing
  519. * that GPRMC doesn't already. But at least two (Garmin GPS 48 and
  520. * Magellan Triton 400) actually ship updates in GLL that aren't redundant.
  521. *
  522. */
  523. char *status = field[7];
  524. gps_mask_t mask = ONLINE_SET;
  525. if (field[5][0] != '\0') {
  526. if (0 == merge_hhmmss(field[5], session)) {
  527. register_fractional_time(field[0], field[5], session);
  528. if (session->nmea.date.tm_year == 0)
  529. GPSD_LOG(LOG_WARN, &session->context->errout,
  530. "can't use GLL time until after ZDA or RMC"
  531. " has supplied a year.\n");
  532. else {
  533. mask = TIME_SET;
  534. }
  535. }
  536. }
  537. if ('\0' == field[6][0] ||
  538. 'V' == field[6][0]) {
  539. /* Invalid */
  540. session->gpsdata.status = STATUS_NO_FIX;
  541. session->newdata.mode = MODE_NO_FIX;
  542. mask |= STATUS_SET | MODE_SET;
  543. } else if ('A' == field[6][0] &&
  544. (count < 8 || *status != 'N') &&
  545. 0 == do_lat_lon(&field[1], &session->newdata)) {
  546. int newstatus;
  547. mask |= LATLON_SET;
  548. newstatus = STATUS_FIX;
  549. if (count >= 8) {
  550. newstatus = faa_mode(*status);
  551. }
  552. /*
  553. * This is a bit dodgy. Technically we shouldn't set the mode
  554. * bit until we see GSA, or similar. But it may be later in the
  555. * cycle, some devices like the FV-18 don't send it by default,
  556. * and elsewhere in the code we want to be able to test for the
  557. * presence of a valid fix with mode > MODE_NO_FIX.
  558. */
  559. if (0 != isfinite(session->gpsdata.fix.altHAE) ||
  560. 0 != isfinite(session->gpsdata.fix.altMSL)) {
  561. session->newdata.mode = MODE_3D;
  562. mask |= MODE_SET;
  563. } else if (3 < session->gpsdata.satellites_used) {
  564. /* 4 sats used means 3D */
  565. session->newdata.mode = MODE_3D;
  566. mask |= MODE_SET;
  567. } else if (MODE_2D > session->gpsdata.fix.mode ||
  568. (0 == isfinite(session->oldfix.altHAE) &&
  569. 0 == isfinite(session->oldfix.altMSL))) {
  570. session->newdata.mode = MODE_2D;
  571. mask |= MODE_SET;
  572. }
  573. session->gpsdata.status = newstatus;
  574. } else {
  575. session->gpsdata.status = STATUS_NO_FIX;
  576. session->newdata.mode = MODE_NO_FIX;
  577. mask |= STATUS_SET | MODE_SET;
  578. }
  579. GPSD_LOG(LOG_DATA, &session->context->errout,
  580. "GLL: hhmmss=%s lat=%.2f lon=%.2f mode=%d status=%d\n",
  581. field[5],
  582. session->newdata.latitude,
  583. session->newdata.longitude,
  584. session->newdata.mode,
  585. session->gpsdata.status);
  586. return mask;
  587. }
  588. /* Geographic position - Latitude, Longitude, and more */
  589. static gps_mask_t processGNS(int count UNUSED, char *field[],
  590. struct gps_device_t *session)
  591. {
  592. /* Introduced in NMEA 4.0?
  593. *
  594. * This mostly duplicates RMC, except for the multi GNSS mode
  595. * indicatore.
  596. *
  597. * Example. Ignore the line break.
  598. * $GPGNS,224749.00,3333.4268304,N,11153.3538273,W,D,19,0.6,406.110,
  599. * -26.294,6.0,0138,S,*6A
  600. *
  601. * 1: 224749.00 UTC HHMMSS.SS. 22:47:49.00
  602. * 2: 3333.4268304 Latitude DDMM.MMMMM. 33 deg. 33.4268304 min
  603. * 3: N Latitude North
  604. * 4: 12311.12 Longitude 111 deg. 53.3538273 min
  605. * 5: W Longitude West
  606. * 6: D FAA mode indicator
  607. * see faa_mode() for possible mode values
  608. * May be one to four characters.
  609. * Char 1 = GPS
  610. * Char 2 = GLONASS
  611. * Char 3 = ?
  612. * Char 4 = ?
  613. * 7: 19 Number of Satellites used in solution
  614. * 8: 0.6 HDOP
  615. * 9: 406110 MSL Altitude in meters
  616. * 10: -26.294 Geoid separation in meters
  617. * 11: 6.0 Age of differential corrections, in seconds
  618. * 12: 0138 Differential reference station ID
  619. * 13: S NMEA 4.1+ Navigation status
  620. * S = Safe
  621. * C = Caution
  622. * U = Unsafe
  623. * V = Not valid for navigation
  624. * 8: *6A Mandatory NMEA checksum
  625. *
  626. */
  627. int newstatus;
  628. int satellites_used;
  629. gps_mask_t mask = ONLINE_SET;
  630. if (field[1][0] != '\0') {
  631. if (0 == merge_hhmmss(field[1], session)) {
  632. register_fractional_time(field[0], field[1], session);
  633. if (session->nmea.date.tm_year == 0) {
  634. GPSD_LOG(LOG_WARN, &session->context->errout,
  635. "can't use GNS time until after ZDA or RMC"
  636. " has supplied a year.\n");
  637. } else {
  638. mask = TIME_SET;
  639. }
  640. }
  641. }
  642. /* FAA mode: not valid, ignore
  643. * Yes, in 2019 a GLONASS only fix may be valid, but not worth
  644. * the confusion */
  645. if ('\0' == field[6][0] || /* FAA mode: missing */
  646. 'N' == field[6][0]) { /* FAA mode: not valid */
  647. session->newdata.mode = MODE_NO_FIX;
  648. mask |= MODE_SET;
  649. return mask;
  650. }
  651. /* navigation status, assume S=safe and C=caution are OK */
  652. /* can be missing on valid fix */
  653. if ('U' == field[13][0] || /* Unsafe */
  654. 'V' == field[13][0]) { /* not valid */
  655. return mask;
  656. }
  657. satellites_used = atoi(field[7]);
  658. if (0 == do_lat_lon(&field[2], &session->newdata)) {
  659. mask |= LATLON_SET;
  660. session->newdata.mode = MODE_2D;
  661. if ('\0' != field[9][0]) {
  662. /* altitude is MSL */
  663. session->newdata.altMSL = safe_atof(field[9]);
  664. if (0 != isfinite(session->newdata.altMSL)) {
  665. mask |= ALTITUDE_SET;
  666. if (3 < satellites_used) {
  667. /* more than 3 sats used means 3D */
  668. session->newdata.mode = MODE_3D;
  669. }
  670. }
  671. /* only need geoid_sep if in 3D mode */
  672. if ('\0' != field[10][0]) {
  673. session->newdata.geoid_sep = safe_atof(field[10]);
  674. }
  675. /* Let gpsd_error_model() deal with geoid_sep and altHAE */
  676. }
  677. } else {
  678. session->newdata.mode = MODE_NO_FIX;
  679. mask |= MODE_SET;
  680. }
  681. if (field[8][0] != '\0') {
  682. session->gpsdata.dop.hdop = safe_atof(field[8]);
  683. }
  684. newstatus = faa_mode(field[6][0]);
  685. session->gpsdata.status = newstatus;
  686. mask |= MODE_SET;
  687. /* get DGPS stuff */
  688. if ('\0' != field[11][0] &&
  689. '\0' != field[12][0]) {
  690. /* both, or neither */
  691. session->newdata.dgps_age = safe_atof(field[11]);
  692. session->newdata.dgps_station = atoi(field[12]);
  693. }
  694. GPSD_LOG(LOG_DATA, &session->context->errout,
  695. "GNS: hhmmss=%s lat=%.2f lon=%.2f mode=%d status=%d\n",
  696. field[1],
  697. session->newdata.latitude,
  698. session->newdata.longitude,
  699. session->newdata.mode,
  700. session->gpsdata.status);
  701. return mask;
  702. }
  703. /* Global Positioning System Fix Data */
  704. static gps_mask_t processGGA(int c UNUSED, char *field[],
  705. struct gps_device_t *session)
  706. {
  707. /*
  708. * GGA,123519,4807.038,N,01131.324,E,1,08,0.9,545.4,M,46.9,M, , *42
  709. * 1 123519 Fix taken at 12:35:19 UTC
  710. * 2,3 4807.038,N Latitude 48 deg 07.038' N
  711. * 4,5 01131.324,E Longitude 11 deg 31.324' E
  712. * 6 1 Fix quality:
  713. * 0 = invalid,
  714. * 1 = GPS,
  715. * u-blox may use 1 for Estimated
  716. * 2 = DGPS,
  717. * 3 = PPS (Precise Position Service),
  718. * 4 = RTK (Real Time Kinematic) with fixed integers,
  719. * 5 = Float RTK,
  720. * 6 = Estimated,
  721. * 7 = Manual,
  722. * 8 = Simulator
  723. * 7 08 Number of satellites in use
  724. * 8 0.9 Horizontal dilution of position
  725. * 9,10 545.4,M Altitude, Meters MSL
  726. * 11,12 46.9,M Height of geoid (mean sea level) above WGS84
  727. * ellipsoid, in Meters
  728. * 13 33 time in seconds since last DGPS update
  729. * usually empty
  730. * 14 1023 DGPS station ID number (0000-1023)
  731. * usually empty
  732. *
  733. * Some GPS, like the SiRFstarV in NMEA mode, send both GPGSA and
  734. * GLGPSA with identical data.
  735. */
  736. gps_mask_t mask = ONLINE_SET;
  737. int newstatus;
  738. char last_last_gga_talker = session->nmea.last_gga_talker;
  739. int fix;
  740. int satellites_visible;
  741. session->nmea.last_gga_talker = field[0][1];
  742. if (0 == strlen(field[6])) {
  743. /* no data is no data, assume no fix
  744. * the test/daemon/myguide-3100.log shows lat/lon/alt but
  745. * no status, and related RMC shows no fix. */
  746. fix = -1;
  747. } else {
  748. fix = atoi(field[6]);
  749. }
  750. switch (fix) {
  751. case 0: /* no fix */
  752. newstatus = STATUS_NO_FIX;
  753. break;
  754. case 1:
  755. /* could be 2D, 3D, GNSSDR */
  756. newstatus = STATUS_FIX;
  757. break;
  758. case 2: /* differential */
  759. newstatus = STATUS_DGPS_FIX;
  760. break;
  761. case 3:
  762. /* GPS PPS, fix valid, could be 2D, 3D, GNSSDR */
  763. newstatus = STATUS_PPS_FIX;
  764. break;
  765. case 4: /* RTK integer */
  766. newstatus = STATUS_RTK_FIX;
  767. break;
  768. case 5: /* RTK float */
  769. newstatus = STATUS_RTK_FLT;
  770. break;
  771. case 6:
  772. /* dead reckoning, could be valid or invalid */
  773. newstatus = STATUS_DR;
  774. break;
  775. case 7:
  776. /* manual input, surveyed */
  777. newstatus = STATUS_TIME;
  778. break;
  779. case 8:
  780. /* simulated mode */
  781. /* Garmin GPSMAP and Gecko sends an 8, but undocumented why */
  782. newstatus = STATUS_SIM;
  783. break;
  784. default:
  785. newstatus = -1;
  786. break;
  787. }
  788. if (0 <= newstatus) {
  789. session->gpsdata.status = newstatus;
  790. mask = STATUS_SET;
  791. }
  792. /*
  793. * There are some receivers (the Trimble Placer 450 is an example) that
  794. * don't ship a GSA with mode 1 when they lose satellite lock. Instead
  795. * they just keep reporting GGA and GSA on subsequent cycles with the
  796. * timestamp not advancing and a bogus mode.
  797. *
  798. * On the assumption that GGA is only issued once per cycle we can
  799. * detect this here (it would be nicer to do it on GSA but GSA has
  800. * no timestamp).
  801. *
  802. * SiRFstarV breaks this assumption, sending GGA with different
  803. * talker IDs.
  804. */
  805. if ('\0' != last_last_gga_talker &&
  806. last_last_gga_talker != session->nmea.last_gga_talker) {
  807. /* skip the time check */
  808. session->nmea.latch_mode = 0;
  809. } else {
  810. session->nmea.latch_mode = strncmp(field[1],
  811. session->nmea.last_gga_timestamp,
  812. sizeof(session->nmea.last_gga_timestamp))==0;
  813. }
  814. if (session->nmea.latch_mode) {
  815. session->gpsdata.status = STATUS_NO_FIX;
  816. session->newdata.mode = MODE_NO_FIX;
  817. GPSD_LOG(LOG_PROG, &session->context->errout,
  818. "xxGGA: latch mode\n");
  819. } else
  820. (void)strlcpy(session->nmea.last_gga_timestamp, field[1],
  821. sizeof(session->nmea.last_gga_timestamp));
  822. /* satellites_visible is used as an accumulator in xxGSV
  823. * so if we set it here we break xxGSV
  824. * Some GPS, like SiRFstarV NMEA, report per GNSS used
  825. * counts in GPGGA and GLGGA.
  826. * session->gpsdata.satellites_visible = atoi(field[7]);
  827. */
  828. satellites_visible = atoi(field[7]);
  829. if (0 == merge_hhmmss(field[1], session)) {
  830. register_fractional_time(field[0], field[1], session);
  831. if (session->nmea.date.tm_year == 0)
  832. GPSD_LOG(LOG_WARN, &session->context->errout,
  833. "can't use GGA time until after ZDA or RMC"
  834. " has supplied a year.\n");
  835. else {
  836. mask |= TIME_SET;
  837. }
  838. }
  839. if (0 == do_lat_lon(&field[2], &session->newdata)) {
  840. session->newdata.mode = MODE_2D;
  841. mask |= LATLON_SET;
  842. if ('\0' != field[11][0]) {
  843. session->newdata.geoid_sep = safe_atof(field[11]);
  844. } else {
  845. session->newdata.geoid_sep = wgs84_separation(
  846. session->newdata.latitude, session->newdata.longitude);
  847. }
  848. /*
  849. * SiRF chipsets up to version 2.2 report a null altitude field.
  850. * See <http://www.sirf.com/Downloads/Technical/apnt0033.pdf>.
  851. * If we see this, force mode to 2D at most.
  852. */
  853. if ('\0' != field[9][0]) {
  854. /* altitude is MSL */
  855. session->newdata.altMSL = safe_atof(field[9]);
  856. /* Let gpsd_error_model() deal with altHAE */
  857. mask |= ALTITUDE_SET;
  858. /*
  859. * This is a bit dodgy. Technically we shouldn't set the mode
  860. * bit until we see GSA. But it may be later in the cycle,
  861. * some devices like the FV-18 don't send it by default, and
  862. * elsewhere in the code we want to be able to test for the
  863. * presence of a valid fix with mode > MODE_NO_FIX.
  864. *
  865. * Use satellites_visible as double check on MODE_3D
  866. */
  867. if (4 <= satellites_visible) {
  868. session->newdata.mode = MODE_3D;
  869. }
  870. }
  871. if (3 > satellites_visible) {
  872. session->newdata.mode = MODE_NO_FIX;
  873. }
  874. } else {
  875. session->newdata.mode = MODE_NO_FIX;
  876. }
  877. mask |= MODE_SET;
  878. if ('\0' != field[8][0]) {
  879. /* why not to newdata? */
  880. session->gpsdata.dop.hdop = safe_atof(field[8]);
  881. }
  882. /* get DGPS stuff */
  883. if ('\0' != field[13][0] &&
  884. '\0' != field[14][0]) {
  885. /* both, or neither */
  886. double age;
  887. int station;
  888. age = safe_atof(field[13]);
  889. station = atoi(field[14]);
  890. if (0.09 < age ||
  891. 0 < station) {
  892. /* ignore both zeros */
  893. session->newdata.dgps_age = age;
  894. session->newdata.dgps_station = station;
  895. }
  896. }
  897. GPSD_LOG(LOG_DATA, &session->context->errout,
  898. "GGA: hhmmss=%s lat=%.2f lon=%.2f altMSL=%.2f mode=%d status=%d\n",
  899. field[1],
  900. session->newdata.latitude,
  901. session->newdata.longitude,
  902. session->newdata.altMSL,
  903. session->newdata.mode,
  904. session->gpsdata.status);
  905. return mask;
  906. }
  907. static gps_mask_t processGST(int count, char *field[],
  908. struct gps_device_t *session)
  909. /* GST - GPS Pseudorange Noise Statistics */
  910. {
  911. /*
  912. * GST,hhmmss.ss,x,x,x,x,x,x,x,*hh
  913. * 1 UTC time of associated GGA fix
  914. * 2 Total RMS standard deviation of ranges inputs to the nav solution
  915. * 3 Standard deviation (meters) of semi-major axis of error ellipse
  916. * 4 Standard deviation (meters) of semi-minor axis of error ellipse
  917. * 5 Orientation of semi-major axis of error ellipse (true north degrees)
  918. * 6 Standard deviation (meters) of latitude error
  919. * 7 Standard deviation (meters) of longitude error
  920. * 8 Standard deviation (meters) of altitude error
  921. * 9 Checksum
  922. */
  923. struct tm date;
  924. timespec_t ts;
  925. int ret;
  926. char ts_buf[TIMESPEC_LEN];
  927. gps_mask_t mask = ONLINE_SET;
  928. if (0 > count) {
  929. return mask;
  930. }
  931. /* since it is NOT current time, do not register_fractional_time() */
  932. // compute start of today
  933. if (0 < session->nmea.date.tm_year) {
  934. // Do not bother if no current year
  935. memset(&date, 0, sizeof(date));
  936. date.tm_year = session->nmea.date.tm_year;
  937. date.tm_mon = session->nmea.date.tm_mon;
  938. date.tm_mday = session->nmea.date.tm_mday;
  939. /* note this is not full UTC, just HHMMSS.ss */
  940. /* this is not the current time,
  941. * it references another GPA of the same stamp. So do not set
  942. * any time stamps with it */
  943. ret = decode_hhmmss(&date, &ts.tv_nsec, field[1], session);
  944. } else {
  945. ret = 1;
  946. }
  947. if (0 == ret) {
  948. // convert to timespec_t , tv_nsec already set
  949. session->gpsdata.gst.utctime.tv_sec = mkgmtime(&date);
  950. session->gpsdata.gst.utctime.tv_nsec = ts.tv_nsec;
  951. } else {
  952. /* no idea of UTC time now */
  953. session->gpsdata.gst.utctime.tv_sec = 0;
  954. session->gpsdata.gst.utctime.tv_nsec = 0;
  955. }
  956. session->gpsdata.gst.rms_deviation = safe_atof(field[2]);
  957. session->gpsdata.gst.smajor_deviation = safe_atof(field[3]);
  958. session->gpsdata.gst.sminor_deviation = safe_atof(field[4]);
  959. session->gpsdata.gst.smajor_orientation = safe_atof(field[5]);
  960. session->gpsdata.gst.lat_err_deviation = safe_atof(field[6]);
  961. session->gpsdata.gst.lon_err_deviation = safe_atof(field[7]);
  962. session->gpsdata.gst.alt_err_deviation = safe_atof(field[8]);
  963. GPSD_LOG(LOG_DATA, &session->context->errout,
  964. "GST: utc = %s, rms = %.2f, maj = %.2f, min = %.2f,"
  965. " ori = %.2f, lat = %.2f, lon = %.2f, alt = %.2f\n",
  966. timespec_str(&session->gpsdata.gst.utctime, ts_buf,
  967. sizeof(ts_buf)),
  968. session->gpsdata.gst.rms_deviation,
  969. session->gpsdata.gst.smajor_deviation,
  970. session->gpsdata.gst.sminor_deviation,
  971. session->gpsdata.gst.smajor_orientation,
  972. session->gpsdata.gst.lat_err_deviation,
  973. session->gpsdata.gst.lon_err_deviation,
  974. session->gpsdata.gst.alt_err_deviation);
  975. mask = GST_SET | ONLINE_SET;
  976. return mask;
  977. }
  978. /* convert NMEA sigid to ublox sigid */
  979. static unsigned char nmea_sigid_to_ubx(unsigned char nmea_sigid)
  980. {
  981. unsigned char ubx_sigid = 0;
  982. switch (nmea_sigid) {
  983. default:
  984. /* FALLTHROUGH */
  985. case 0:
  986. /* missing, assume GPS L1 */
  987. ubx_sigid = 0;
  988. break;
  989. case 1:
  990. /* L1 */
  991. ubx_sigid = 0;
  992. break;
  993. case 2:
  994. /* E5, could be 5 or 6. */
  995. ubx_sigid = 5;
  996. break;
  997. case 3:
  998. /* B2 or L2, could be 2 or 3. */
  999. ubx_sigid = 2;
  1000. break;
  1001. case 5:
  1002. /* L2 */
  1003. ubx_sigid = 4;
  1004. break;
  1005. case 6:
  1006. /* L2CL */
  1007. ubx_sigid = 3;
  1008. break;
  1009. case 7:
  1010. /* E1, could be 0 or 1. */
  1011. ubx_sigid = 0;
  1012. break;
  1013. }
  1014. return ubx_sigid;
  1015. }
  1016. /* Deal with range-mapping attempts to use IDs 1-32 by Beidou, etc.
  1017. *
  1018. * See struct satellite_t in gps.h for ubx and nmea gnssid and svid mappings
  1019. *
  1020. * char *talker -- NMEA talker string
  1021. * int nmea_satnum -- NMEA (All ver) satellite number (kinda the PRN)
  1022. * int nmea_gnssid -- NMEA 4.10 gnssid, if known, otherwise zero
  1023. * unsigned char *ubx_gnssid -- returned u-blox gnssid
  1024. * unsigned char *ubx_svid -- returned u-blox gnssid
  1025. *
  1026. * Return the NMEA 2.x to 4.0 extended PRN
  1027. */
  1028. static int nmeaid_to_prn(char *talker, int nmea_satnum,
  1029. int nmea_gnssid,
  1030. unsigned char *ubx_gnssid,
  1031. unsigned char *ubx_svid)
  1032. {
  1033. /*
  1034. * According to https://github.com/mvglasow/satstat/wiki/NMEA-IDs
  1035. * and u-blox documentation.
  1036. * NMEA IDs can be roughly divided into the following ranges:
  1037. *
  1038. * 1..32: GPS
  1039. * 33..64: Various SBAS systems (EGNOS, WAAS, SDCM, GAGAN, MSAS)
  1040. * 65..96: GLONASS
  1041. * 152..158: Various SBAS systems (EGNOS, WAAS, SDCM, GAGAN, MSAS)
  1042. * 173..182: IMES
  1043. * 193..197: QZSS (undocumented u-blox goes to 199)
  1044. * 201..235: Beidou (not NMEA, not u-blox?)
  1045. * 301..336: Galileo
  1046. * 401..437: Beidou
  1047. * null: GLONASS unused
  1048. *
  1049. * The issue is what to do when GPSes from these different systems
  1050. * fight for IDs in the 1-32 range, as in this pair of Beidou sentences
  1051. *
  1052. * $BDGSV,2,1,07,01,00,000,45,02,13,089,35,03,00,000,37,04,00,000,42*6E
  1053. * $BDGSV,2,2,07,05,27,090,,13,19,016,,11,07,147,*5E
  1054. *
  1055. * Because the PRNs are only used for generating a satellite
  1056. * chart, mistakes here aren't dangerous. The code will record
  1057. * and use multiple sats with the same ID in one skyview; in
  1058. * effect, they're recorded by the order in which they occur
  1059. * rather than by PRN.
  1060. */
  1061. int nmea2_prn = nmea_satnum;
  1062. *ubx_gnssid = 0; /* default to ubx_gnssid is GPS */
  1063. *ubx_svid = 0; /* default to unnknown ubx_svid */
  1064. if (1 > nmea_satnum) {
  1065. /* uh, oh... */
  1066. nmea2_prn = 0;
  1067. } else if (0 < nmea_gnssid) {
  1068. /* this switch handles case where nmea_gnssid is known */
  1069. switch (nmea_gnssid) {
  1070. default:
  1071. /* x = IMES Not defined by NMEA 4.10 */
  1072. /* FALLTHROUGH */
  1073. case 0:
  1074. /* none given, ignore */
  1075. nmea2_prn = 0;
  1076. break;
  1077. case 1:
  1078. if (33 > nmea_satnum) {
  1079. /* 1 = GPS 1-32 */
  1080. *ubx_gnssid = 0;
  1081. *ubx_svid = nmea_satnum;
  1082. } else if (65 > nmea_satnum) {
  1083. /* 1 = SBAS 33-64 */
  1084. *ubx_gnssid = 1;
  1085. *ubx_svid = 87 + nmea_satnum;
  1086. } else if (152 > nmea_satnum) {
  1087. /* Huh? */
  1088. *ubx_gnssid = 0;
  1089. *ubx_svid = 0;
  1090. nmea2_prn = 0;
  1091. } else if (158 > nmea_satnum) {
  1092. /* 1 = SBAS 152-158 */
  1093. *ubx_gnssid = 1;
  1094. *ubx_svid = nmea_satnum;
  1095. } else if (193 > nmea_satnum) {
  1096. /* Huh? */
  1097. *ubx_gnssid = 0;
  1098. *ubx_svid = 0;
  1099. nmea2_prn = 0;
  1100. } else if (200 > nmea_satnum) {
  1101. /* 1 = QZSS 193-197 */
  1102. /* undocumented u-blox goes to 199 */
  1103. *ubx_gnssid = 3;
  1104. *ubx_svid = nmea_satnum - 192;
  1105. } else {
  1106. /* Huh? */
  1107. *ubx_gnssid = 0;
  1108. *ubx_svid = 0;
  1109. nmea2_prn = 0;
  1110. }
  1111. break;
  1112. case 2:
  1113. /* 2 = GLONASS 65-96, nul */
  1114. *ubx_gnssid = 6;
  1115. *ubx_svid = nmea_satnum;
  1116. break;
  1117. case 3:
  1118. /* 3 = Galileo 1-36 */
  1119. *ubx_gnssid = 2;
  1120. *ubx_svid = nmea_satnum;
  1121. nmea2_prn = 300 + nmea_satnum;
  1122. break;
  1123. case 4:
  1124. /* 4 - BeiDou 1-37 */
  1125. *ubx_gnssid = 3;
  1126. *ubx_svid = nmea_satnum;
  1127. nmea2_prn = 300 + nmea_satnum;
  1128. break;
  1129. }
  1130. /* left with NMEA 2.x to NMEA 4.0 satnums
  1131. * use talker ID to disambiguate */
  1132. } else if (32 >= nmea_satnum) {
  1133. *ubx_svid = nmea_satnum;
  1134. switch (talker[0]) {
  1135. case 'G':
  1136. if (talker[1] == 'A') {
  1137. /* Galileo */
  1138. nmea2_prn = 300 + nmea_satnum;
  1139. *ubx_gnssid = 2;
  1140. } else if (talker[1] == 'B') {
  1141. /* map Beidou IDs 1..37 to 401..437 */
  1142. *ubx_gnssid = 3;
  1143. nmea2_prn = 400 + nmea_satnum;
  1144. } else if (talker[1] == 'L') {
  1145. /* GLONASS GL doesn't seem to do this, better safe than sorry */
  1146. nmea2_prn = 64 + nmea_satnum;
  1147. *ubx_gnssid = 6;
  1148. } else if (talker[1] == 'N') {
  1149. /* all of them, but only GPS is 0 < PRN < 33 */
  1150. } else if (talker[1] == 'P') {
  1151. /* GPS,SBAS,QZSS, but only GPS is 0 < PRN < 33 */
  1152. } /* else ?? */
  1153. break;
  1154. case 'B':
  1155. if (talker[1] == 'D') {
  1156. /* map Beidou IDs */
  1157. nmea2_prn = 400 + nmea_satnum;
  1158. *ubx_gnssid = 3;
  1159. } /* else ?? */
  1160. break;
  1161. case 'P':
  1162. /* Quectel EC25 & EC21 use PQxxx for BeiDou */
  1163. if (talker[1] == 'Q') {
  1164. /* map Beidou IDs */
  1165. nmea2_prn = 400 + nmea_satnum;
  1166. *ubx_gnssid = 3;
  1167. } /* else ?? */
  1168. break;
  1169. case 'Q':
  1170. if (talker[1] == 'Z') {
  1171. /* QZSS */
  1172. nmea2_prn = 192 + nmea_satnum;
  1173. *ubx_gnssid = 5;
  1174. } /* else ? */
  1175. break;
  1176. default:
  1177. /* huh? */
  1178. break;
  1179. }
  1180. } else if (64 >= nmea_satnum) {
  1181. // NMEA-ID (33..64) to SBAS PRN 120-151.
  1182. /* SBAS */
  1183. *ubx_gnssid = 1;
  1184. *ubx_svid = 87 + nmea_satnum;
  1185. } else if (96 >= nmea_satnum) {
  1186. /* GLONASS 65..96 */
  1187. *ubx_gnssid = 6;
  1188. *ubx_svid = nmea_satnum - 64;
  1189. } else if (120 > nmea_satnum) {
  1190. /* Huh? */
  1191. *ubx_gnssid = 0;
  1192. *ubx_svid = 0;
  1193. nmea2_prn = 0;
  1194. } else if (158 >= nmea_satnum) {
  1195. /* SBAS 120..158 */
  1196. *ubx_gnssid = 1;
  1197. *ubx_svid = nmea_satnum;
  1198. } else if (173 > nmea_satnum) {
  1199. /* Huh? */
  1200. *ubx_gnssid = 0;
  1201. *ubx_svid = 0;
  1202. nmea2_prn = 0;
  1203. } else if (182 >= nmea_satnum) {
  1204. /* IMES 173..182 */
  1205. *ubx_gnssid = 4;
  1206. *ubx_svid = nmea_satnum - 172;
  1207. } else if (193 > nmea_satnum) {
  1208. /* Huh? */
  1209. *ubx_gnssid = 0;
  1210. *ubx_svid = 0;
  1211. nmea2_prn = 0;
  1212. } else if (197 >= nmea_satnum) {
  1213. /* QZSS 193..197 */
  1214. /* undocumented u-blox goes to 199 */
  1215. *ubx_gnssid = 5;
  1216. *ubx_svid = nmea_satnum - 192;
  1217. } else if (201 > nmea_satnum) {
  1218. /* Huh? */
  1219. *ubx_gnssid = 0;
  1220. *ubx_svid = 0;
  1221. nmea2_prn = 0;
  1222. } else if (237 >= nmea_satnum) {
  1223. /* BeiDou, non-standard, some SiRF put BeiDou 201-237 */
  1224. /* $GBGSV,2,2,05,209,07,033,*62 */
  1225. *ubx_gnssid = 3;
  1226. *ubx_svid = nmea_satnum - 200;
  1227. nmea2_prn += 200; /* move up to 400 where NMEA 2.x wants it. */
  1228. } else if (301 > nmea_satnum) {
  1229. /* Huh? */
  1230. *ubx_gnssid = 0;
  1231. *ubx_svid = 0;
  1232. nmea2_prn = 0;
  1233. } else if (356 >= nmea_satnum) {
  1234. /* Galileo 301..356 */
  1235. *ubx_gnssid = 2;
  1236. *ubx_svid = nmea_satnum - 300;
  1237. } else if (401 > nmea_satnum) {
  1238. /* Huh? */
  1239. *ubx_gnssid = 0;
  1240. *ubx_svid = 0;
  1241. nmea2_prn = 0;
  1242. } else if (437 >= nmea_satnum) {
  1243. /* BeiDou */
  1244. *ubx_gnssid = 3;
  1245. *ubx_svid = nmea_satnum - 400;
  1246. } else {
  1247. /* greater than 437 Huh? */
  1248. *ubx_gnssid = 0;
  1249. *ubx_svid = 0;
  1250. nmea2_prn = 0;
  1251. }
  1252. return nmea2_prn;
  1253. }
  1254. static gps_mask_t processGSA(int count, char *field[],
  1255. struct gps_device_t *session)
  1256. /* GPS DOP and Active Satellites */
  1257. {
  1258. #define GSA_TALKER field[0][1]
  1259. /*
  1260. * eg1. $GPGSA,A,3,,,,,,16,18,,22,24,,,3.6,2.1,2.2*3C
  1261. * eg2. $GPGSA,A,3,19,28,14,18,27,22,31,39,,,,,1.7,1.0,1.3*35
  1262. * NMEA 4.10: $GNGSA,A,3,13,12,22,19,08,21,,,,,,,1.05,0.64,0.83,4*0B
  1263. * 1 = Mode:
  1264. * M=Manual, forced to operate in 2D or 3D
  1265. * A=Automatic, 3D/2D
  1266. * 2 = Mode:
  1267. * 1=Fix not available,
  1268. * 2=2D,
  1269. * 3=3D
  1270. * 3-14 = PRNs of satellites used in position fix (null for unused fields)
  1271. * 15 = PDOP
  1272. * 16 = HDOP
  1273. * 17 = VDOP
  1274. * 18 - NMEA 4.1+ GNSS System ID, u-blox extended
  1275. * 1 = GPS L1C/A, L2CL, L2CM
  1276. * 2 = GLONASS L1 OF, L2 OF
  1277. * 3 = Galileo E1C, E1B, E5 bl, E5 bQ
  1278. * 4 = BeiDou B1I D1, B1I D2, B2I D1, B2I D12
  1279. *
  1280. * Not all documentation specifies the number of PRN fields, it
  1281. * may be variable. Most doc that specifies says 12 PRNs.
  1282. *
  1283. * The Navior-24 CH-4701 outputs 30 fields, 24 PRNs!
  1284. * GPGSA,A,3,27,23,13,07,25,,,,,,,,,,,,,,,,,,,,07.9,06.0,05.2
  1285. *
  1286. * The Skytraq S2525F8-BD-RTK output both GPGSA and BDGSA in the
  1287. * same cycle:
  1288. * $GPGSA,A,3,23,31,22,16,03,07,,,,,,,1.8,1.1,1.4*3E
  1289. * $BDGSA,A,3,214,,,,,,,,,,,,1.8,1.1,1.4*18
  1290. * These need to be combined like GPGSV and BDGSV
  1291. *
  1292. * Some GPS emit GNGSA. So far we have not seen a GPS emit GNGSA
  1293. * and then another flavor of xxGSA
  1294. *
  1295. * Some Skytraq will emit all GPS in one GNGSA, Then follow with
  1296. * another GNGSA with the BeiDou birds.
  1297. *
  1298. * SEANEXX, SiRFstarIV, and others also do it twice in one cycle:
  1299. * $GNGSA,A,3,31,26,21,,,,,,,,,,3.77,2.55,2.77*1A
  1300. * $GNGSA,A,3,75,86,87,,,,,,,,,,3.77,2.55,2.77*1C
  1301. * seems like the first is GNSS and the second GLONASS
  1302. *
  1303. * u-blox 9 outputs one per GNSS on each cycle. Note the
  1304. * extra last parameter which is NMEA gnssid:
  1305. * $GNGSA,A,3,13,16,21,15,10,29,27,20,,,,,1.05,0.64,0.83,1*03
  1306. * $GNGSA,A,3,82,66,81,,,,,,,,,,1.05,0.64,0.83,2*0C
  1307. * $GNGSA,A,3,07,12,33,,,,,,,,,,1.05,0.64,0.83,3*0A
  1308. * $GNGSA,A,3,13,12,22,19,08,21,,,,,,,1.05,0.64,0.83,4*0B
  1309. * Also note the NMEA 4.0 GLONASS PRN (82) in an NMEA 4.1
  1310. * sentence.
  1311. */
  1312. gps_mask_t mask = ONLINE_SET;
  1313. char last_last_gsa_talker = session->nmea.last_gsa_talker;
  1314. int nmea_gnssid = 0;
  1315. int nmea_sigid = 0;
  1316. int ubx_sigid = 0;
  1317. /*
  1318. * One chipset called the i.Trek M3 issues GPGSA lines that look like
  1319. * this: "$GPGSA,A,1,,,,*32" when it has no fix. This is broken
  1320. * in at least two ways: it's got the wrong number of fields, and
  1321. * it claims to be a valid sentence (A flag) when it isn't.
  1322. * Alarmingly, it's possible this error may be generic to SiRFstarIII.
  1323. */
  1324. if (18 > count) {
  1325. GPSD_LOG(LOG_DATA, &session->context->errout,
  1326. "xxGSA: malformed, setting ONLINE_SET only.\n");
  1327. mask = ONLINE_SET;
  1328. } else if (session->nmea.latch_mode) {
  1329. /* last GGA had a non-advancing timestamp; don't trust this GSA */
  1330. mask = ONLINE_SET;
  1331. GPSD_LOG(LOG_DATA, &session->context->errout,
  1332. "xxGSA: non-advancing timestamp\n");
  1333. } else {
  1334. int i;
  1335. session->newdata.mode = atoi(field[2]);
  1336. /*
  1337. * The first arm of this conditional ignores dead-reckoning
  1338. * fixes from an Antaris chipset. which returns E in field 2
  1339. * for a dead-reckoning estimate. Fix by Andreas Stricker.
  1340. */
  1341. if ('E' == field[2][0])
  1342. mask = ONLINE_SET;
  1343. else
  1344. mask = MODE_SET;
  1345. GPSD_LOG(LOG_PROG, &session->context->errout,
  1346. "xxGSA sets mode %d\n", session->newdata.mode);
  1347. if (19 < count ) {
  1348. GPSD_LOG(LOG_WARN, &session->context->errout,
  1349. "xxGSA: count %d too long!\n", count);
  1350. } else {
  1351. /* Just ignore the last fields of the Navior CH-4701 */
  1352. if (field[15][0] != '\0')
  1353. session->gpsdata.dop.pdop = safe_atof(field[15]);
  1354. if (field[16][0] != '\0')
  1355. session->gpsdata.dop.hdop = safe_atof(field[16]);
  1356. if (field[17][0] != '\0')
  1357. session->gpsdata.dop.vdop = safe_atof(field[17]);
  1358. if (19 == count && '\0' != field[18][0]) {
  1359. /* get the NMEA 4.10 sigid */
  1360. nmea_sigid = atoi(field[18]);
  1361. /* FIXME: ubx_sigid not used yet */
  1362. ubx_sigid = nmea_sigid_to_ubx(nmea_sigid);
  1363. }
  1364. }
  1365. /*
  1366. * might have gone from GPGSA to GLGSA/BDGSA
  1367. * or GNGSA to GNGSA
  1368. * in which case accumulate
  1369. */
  1370. if ( '\0' == session->nmea.last_gsa_talker
  1371. || (GSA_TALKER == session->nmea.last_gsa_talker
  1372. && 'N' != GSA_TALKER) ) {
  1373. session->gpsdata.satellites_used = 0;
  1374. memset(session->nmea.sats_used, 0, sizeof(session->nmea.sats_used));
  1375. GPSD_LOG(LOG_DATA, &session->context->errout,
  1376. "xxGSA: clear sats_used\n");
  1377. }
  1378. session->nmea.last_gsa_talker = GSA_TALKER;
  1379. if ((session->nmea.last_gsa_talker == 'B') ||
  1380. (session->nmea.last_gsa_talker == 'D') ||
  1381. (session->nmea.last_gsa_talker == 'Q'))
  1382. /* Quectel EC25 & EC21 use PQGSA for BeiDou */
  1383. session->nmea.seen_bdgsa = true;
  1384. else if (session->nmea.last_gsa_talker == 'L')
  1385. session->nmea.seen_glgsa = true;
  1386. else if (session->nmea.last_gsa_talker == 'N')
  1387. session->nmea.seen_gngsa = true;
  1388. else if (session->nmea.last_gsa_talker == 'A')
  1389. session->nmea.seen_gagsa = true;
  1390. /* the magic 6 here counts the tag, two mode fields, and DOP fields */
  1391. for (i = 0; i < count - 6; i++) {
  1392. int prn;
  1393. int nmea_satnum;
  1394. unsigned char ubx_gnssid; /* UNUSED */
  1395. unsigned char ubx_svid; /* UNUSED */
  1396. /* skip empty fields, otherwise empty becomes prn=200 */
  1397. nmea_satnum = atoi(field[i + 3]);
  1398. if (1 > nmea_satnum) {
  1399. continue;
  1400. }
  1401. prn = nmeaid_to_prn(field[0], nmea_satnum, nmea_gnssid,
  1402. &ubx_gnssid, &ubx_svid);
  1403. #ifdef __UNUSED__
  1404. /* debug */
  1405. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1406. "%s nmeaid_to_prn: nmea_gnssid %d nmea_satnum %d ubx_gnssid %d "
  1407. "ubx_svid %d nmea2_prn %d\n", field[0],
  1408. nmea_gnssid, nmea_satnum, ubx_gnssid, ubx_svid, prn);
  1409. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1410. "%s count %d\n", field[0], count);
  1411. #endif /* __UNUSED__ */
  1412. if (prn > 0) {
  1413. /* check first BEFORE over-writing memory */
  1414. if (MAXCHANNELS <= session->gpsdata.satellites_used) {
  1415. /* This should never happen as xxGSA is limited to 12,
  1416. * except for the Navior-24 CH-4701.
  1417. * But it could happen with multiple GSA per cycle */
  1418. break;
  1419. }
  1420. session->nmea.sats_used[session->gpsdata.satellites_used++] =
  1421. (unsigned short)prn;
  1422. }
  1423. }
  1424. mask |= DOP_SET | USED_IS;
  1425. GPSD_LOG(LOG_DATA, &session->context->errout,
  1426. "xxGSA: mode=%d used=%d pdop=%.2f hdop=%.2f vdop=%.2f "
  1427. "ubx_sigid %d\n",
  1428. session->newdata.mode,
  1429. session->gpsdata.satellites_used,
  1430. session->gpsdata.dop.pdop,
  1431. session->gpsdata.dop.hdop,
  1432. session->gpsdata.dop.vdop, ubx_sigid);
  1433. }
  1434. /* assumes GLGSA or BDGSA, if present, is emitted directly
  1435. * after the GPGSA*/
  1436. if ((session->nmea.seen_glgsa || session->nmea.seen_bdgsa ||
  1437. session->nmea.seen_gagsa) && GSA_TALKER == 'P') {
  1438. mask = ONLINE_SET;
  1439. /* first of two GNGSA */
  1440. /* if mode == 1 some GPS only output 1 GNGSA, so ship mode always */
  1441. } else if ( 'N' != last_last_gsa_talker && 'N' == GSA_TALKER) {
  1442. mask = ONLINE_SET | MODE_SET;
  1443. }
  1444. /* cast for 32/64 compatibility */
  1445. GPSD_LOG(LOG_PROG, &session->context->errout,
  1446. "xxGSA: mask %#llx\n", (long long unsigned)mask);
  1447. return mask;
  1448. #undef GSA_TALKER
  1449. }
  1450. /* xxGSV - GPS Satellites in View */
  1451. static gps_mask_t processGSV(int count, char *field[],
  1452. struct gps_device_t *session)
  1453. {
  1454. #define GSV_TALKER field[0][1]
  1455. /*
  1456. * GSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75
  1457. * 1) 2 Number of sentences for full data
  1458. * 2) 1 Sentence 1 of 2
  1459. * 3) 08 Total number of satellites in view
  1460. * 4) 01 Satellite PRN number
  1461. * 5) 40 Elevation, degrees
  1462. * 6) 083 Azimuth, degrees
  1463. * 7) 46 Signal-to-noise ratio in decibels
  1464. * <repeat for up to 4 satellites per sentence>
  1465. * ) NMEA 4.1 signalId
  1466. * ) checksum
  1467. *
  1468. * NMEA 4.1+:
  1469. * $GAGSV,3,1,09,02,00,179,,04,09,321,,07,11,134,11,11,10,227,,7*7F
  1470. * after the satellite block, before the checksum, new field:
  1471. * 7 NMEA Signal ID
  1472. * 1 = GPS L1C/A, BeiDou B1I D1, BeiDou B1I D2, GLONASS L1 OF
  1473. * 2 = Galileo E5 bl, E5 bQ
  1474. * 3 = BeiDou B2I D1, B2I D2
  1475. * 5 = GPS L2 CM
  1476. * 6 = GPS L2 CL
  1477. * 7 = Galileo E1C, E1B
  1478. *
  1479. * Can occur with talker IDs:
  1480. * BD (Beidou),
  1481. * GA (Galileo),
  1482. * GB (Beidou),
  1483. * GL (GLONASS),
  1484. * GN (GLONASS, any combination GNSS),
  1485. * GP (GPS, SBAS, QZSS),
  1486. * QZ (QZSS).
  1487. *
  1488. * As of April 2019:
  1489. * no gpsd regressions have GNGSV
  1490. * every xxGSV cycle starts with GPGSV
  1491. * xxGSV cycles may be spread over several xxRMC cycles
  1492. *
  1493. * GL may be (incorrectly) used when GSVs are mixed containing
  1494. * GLONASS, GN may be (incorrectly) used when GSVs contain GLONASS
  1495. * only. Usage is inconsistent.
  1496. *
  1497. * In the GLONASS version sat IDs run from 65-96 (NMEA0183
  1498. * standardizes this). At least two GPSes, the BU-353 GLONASS and
  1499. * the u-blox NEO-M8N, emit a GPGSV set followed by a GLGSV set.
  1500. * We have also seen two GPSes, the Skytraq S2525F8-BD-RTK and a
  1501. * SiRF-IV variant, that emit GPGSV followed by BDGSV. We need to
  1502. * combine these.
  1503. *
  1504. * The following shows how the Skytraq S2525F8-BD-RTK output both
  1505. * GPGSV and BDGSV in the same cycle:
  1506. * $GPGSV,4,1,13,23,66,310,29,03,65,186,33,26,43,081,27,16,41,124,38*78
  1507. * $GPGSV,4,2,13,51,37,160,38,04,37,066,25,09,34,291,07,22,26,156,37*77
  1508. * $GPGSV,4,3,13,06,19,301,,31,17,052,20,193,11,307,,07,11,232,27*4F
  1509. * $GPGSV,4,4,13,01,03,202,30*4A
  1510. * $BDGSV,1,1,02,214,55,153,40,208,01,299,*67
  1511. *
  1512. * The driver automatically adapts to either case, but it takes until the
  1513. * second cycle (usually 10 seconds from device connect) for it to
  1514. * learn to expect BSDGV or GLGSV.
  1515. *
  1516. * Some GPS (Garmin 17N) spread the xxGSV over several cycles. So
  1517. * cycles, or cycle time, can not be used to determine start of
  1518. * xxGSV cycle.
  1519. *
  1520. * NMEA 4.1 adds a signal-ID field just before the checksum. First
  1521. * seen in May 2015 on a u-blox M8. It can output 2 sets of GPGSV
  1522. * in one cycle, one for L1C and the other for L2C.
  1523. */
  1524. int n, fldnum;
  1525. unsigned char nmea_sigid = 0;
  1526. int nmea_gnssid = 0;
  1527. unsigned char ubx_sigid = 0;
  1528. if (count <= 3) {
  1529. GPSD_LOG(LOG_WARN, &session->context->errout,
  1530. "malformed xxGSV - fieldcount %d <= 3\n",
  1531. count);
  1532. gpsd_zero_satellites(&session->gpsdata);
  1533. return ONLINE_SET;
  1534. }
  1535. GPSD_LOG(LOG_PROG, &session->context->errout,
  1536. "x%cGSV: part %s of %s, last_gsv_talker '%#x' "
  1537. " last_gsv_sigid %u\n",
  1538. GSV_TALKER, field[2], field[1],
  1539. session->nmea.last_gsv_talker,
  1540. session->nmea.last_gsv_sigid);
  1541. /*
  1542. * This check used to be !=0, but we have loosen it a little to let by
  1543. * NMEA 4.1 GSVs with an extra signal-ID field at the end.
  1544. */
  1545. switch (count % 4) {
  1546. case 0:
  1547. /* normal, pre-NMEA 4.10 */
  1548. break;
  1549. case 1:
  1550. /* NMEA 4.10, get the signal ID */
  1551. nmea_sigid = atoi(field[count - 1]);
  1552. ubx_sigid = nmea_sigid_to_ubx(nmea_sigid);
  1553. break;
  1554. default:
  1555. /* bad count */
  1556. GPSD_LOG(LOG_WARN, &session->context->errout,
  1557. "malformed GPGSV - fieldcount %d %% 4 != 0\n", count);
  1558. gpsd_zero_satellites(&session->gpsdata);
  1559. return ONLINE_SET;
  1560. }
  1561. session->nmea.await = atoi(field[1]);
  1562. if ((session->nmea.part = atoi(field[2])) < 1) {
  1563. GPSD_LOG(LOG_WARN, &session->context->errout,
  1564. "malformed GPGSV - bad part\n");
  1565. gpsd_zero_satellites(&session->gpsdata);
  1566. return ONLINE_SET;
  1567. }
  1568. if (session->nmea.part == 1) {
  1569. /*
  1570. * might have gone from GPGSV to GLGSV/BDGSV/QZGSV,
  1571. * in which case accumulate
  1572. *
  1573. * NMEA 4.1 might have gone from GPGVS,sigid=x to GPGSV,sigid=y
  1574. *
  1575. * session->nmea.last_gsv_talker is zero at cycle start
  1576. */
  1577. if (session->nmea.last_gsv_talker == '\0' ||
  1578. ('P' == GSV_TALKER &&
  1579. 0 == ubx_sigid)) {
  1580. GPSD_LOG(LOG_PROG, &session->context->errout,
  1581. "x%cGSV: new part %d, last_gsv_talker '%#x', zeroing\n",
  1582. GSV_TALKER,
  1583. session->nmea.part,
  1584. session->nmea.last_gsv_talker);
  1585. gpsd_zero_satellites(&session->gpsdata);
  1586. }
  1587. session->nmea.last_gsv_talker = GSV_TALKER;
  1588. session->nmea.last_gsv_sigid = ubx_sigid; /* UNUSED */
  1589. switch (GSV_TALKER) {
  1590. case 'A':
  1591. session->nmea.seen_gagsv = true;
  1592. break;
  1593. case 'B':
  1594. /* FALLTHROUGH */
  1595. case 'D':
  1596. /* FALLTHROUGH */
  1597. case 'Q':
  1598. /* Quectel EC25 & EC21 use PQGSA for BeiDou */
  1599. session->nmea.seen_bdgsv = true;
  1600. break;
  1601. case 'L':
  1602. session->nmea.seen_glgsv = true;
  1603. break;
  1604. case 'P':
  1605. session->nmea.seen_gpgsv = true;
  1606. break;
  1607. case 'Z':
  1608. session->nmea.seen_qzss = true;
  1609. break;
  1610. default:
  1611. /* uh, what? */
  1612. break;
  1613. }
  1614. }
  1615. for (fldnum = 4; fldnum < count / 4 * 4;) {
  1616. struct satellite_t *sp;
  1617. int nmea_svid;
  1618. if (session->gpsdata.satellites_visible >= MAXCHANNELS) {
  1619. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1620. "xxGSV: internal error - too many satellites [%d]!\n",
  1621. session->gpsdata.satellites_visible);
  1622. gpsd_zero_satellites(&session->gpsdata);
  1623. break;
  1624. }
  1625. sp = &session->gpsdata.skyview[session->gpsdata.satellites_visible];
  1626. nmea_svid = atoi(field[fldnum++]);
  1627. if (0 == nmea_svid) {
  1628. /* skip bogus fields */
  1629. continue;
  1630. }
  1631. /* FIXME: this ignores possible NMEA 4.1 Signal ID hint */
  1632. sp->PRN = (short)nmeaid_to_prn(field[0], nmea_svid, nmea_gnssid,
  1633. &sp->gnssid, &sp->svid);
  1634. #ifdef __UNUSED__
  1635. {
  1636. /* debug */
  1637. char ts_buf[TIMESPEC_LEN];
  1638. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1639. "%s nmeaid_to_prn: nmea_gnssid %d nmea_satnum %d "
  1640. "ubx_gnssid %d ubx_svid %d nmea2_prn %d\n", field[0],
  1641. nmea_gnssid, nmea_svid, sp->gnssid, sp->svid, sp->PRN);
  1642. }
  1643. #endif /* __UNUSED__ */
  1644. sp->elevation = (double)atoi(field[fldnum++]);
  1645. sp->azimuth = (double)atoi(field[fldnum++]);
  1646. sp->ss = (double)atoi(field[fldnum++]);
  1647. sp->used = false;
  1648. sp->sigid = ubx_sigid;
  1649. /* sadly NMEA 4.1 does not tell us which sigid (L1, L2) is
  1650. * used. So if the ss is zero, do not mark used */
  1651. if (0 < sp->PRN && 0 < sp->ss) {
  1652. for (n = 0; n < MAXCHANNELS; n++)
  1653. if (session->nmea.sats_used[n] == (unsigned short)sp->PRN) {
  1654. sp->used = true;
  1655. break;
  1656. }
  1657. }
  1658. /*
  1659. * Incrementing this unconditionally falls afoul of chipsets like
  1660. * the Motorola Oncore GT+ that emit empty fields at the end of the
  1661. * last sentence in a GPGSV set if the number of satellites is not
  1662. * a multiple of 4.
  1663. */
  1664. session->gpsdata.satellites_visible++;
  1665. }
  1666. #if __UNUSED
  1667. /* debug code */
  1668. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1669. "x%cGSV: vis %d gagsv %d bdgsv %d glgsv %d qzss %d\n",
  1670. GSV_TALKER,
  1671. session->gpsdata.satellites_visible,
  1672. session->nmea.seen_gagsv,
  1673. session->nmea.seen_bdgsv,
  1674. session->nmea.seen_glgsv,
  1675. session->nmea.seen_qzss);
  1676. #endif
  1677. /*
  1678. * Alas, we can't sanity check field counts when there are multiple sat
  1679. * pictures, because the visible member counts *all* satellites - you
  1680. * get a bad result on the second and later SV spans. Note, this code
  1681. * assumes that if any of the special sat pics occur they come right
  1682. * after a stock GPGSV one.
  1683. *
  1684. * FIXME: Add per-talker totals so we can do this check properly.
  1685. */
  1686. if (!(session->nmea.seen_glgsv || session->nmea.seen_bdgsv
  1687. || session->nmea.seen_qzss || session->nmea.seen_gagsv))
  1688. if (session->nmea.part == session->nmea.await
  1689. && atoi(field[3]) != session->gpsdata.satellites_visible)
  1690. GPSD_LOG(LOG_WARN, &session->context->errout,
  1691. "GPGSV field 3 value of %d != actual count %d\n",
  1692. atoi(field[3]), session->gpsdata.satellites_visible);
  1693. /* not valid data until we've seen a complete set of parts */
  1694. if (session->nmea.part < session->nmea.await) {
  1695. GPSD_LOG(LOG_PROG, &session->context->errout,
  1696. "xxGSV: Partial satellite data (%d of %d).\n",
  1697. session->nmea.part, session->nmea.await);
  1698. return ONLINE_SET;
  1699. }
  1700. /*
  1701. * This sanity check catches an odd behavior of SiRFstarII receivers.
  1702. * When they can't see any satellites at all (like, inside a
  1703. * building) they sometimes cough up a hairball in the form of a
  1704. * GSV packet with all the azimuth entries 0 (but nonzero
  1705. * elevations). This behavior was observed under SiRF firmware
  1706. * revision 231.000.000_A2.
  1707. */
  1708. for (n = 0; n < session->gpsdata.satellites_visible; n++)
  1709. if (session->gpsdata.skyview[n].azimuth != 0)
  1710. goto sane;
  1711. GPSD_LOG(LOG_WARN, &session->context->errout,
  1712. "xxGSV: Satellite data no good (%d of %d).\n",
  1713. session->nmea.part, session->nmea.await);
  1714. gpsd_zero_satellites(&session->gpsdata);
  1715. return ONLINE_SET;
  1716. sane:
  1717. session->gpsdata.skyview_time.tv_sec = 0;
  1718. session->gpsdata.skyview_time.tv_nsec = 0;
  1719. GPSD_LOG(LOG_DATA, &session->context->errout,
  1720. "xxGSV: Satellite data OK (%d of %d).\n",
  1721. session->nmea.part, session->nmea.await);
  1722. /* assumes GLGSV or BDGSV group, if present, is emitted after the GPGSV */
  1723. if ((session->nmea.seen_glgsv || session->nmea.seen_bdgsv
  1724. || session->nmea.seen_qzss || session->nmea.seen_gagsv)
  1725. && GSV_TALKER == 'P')
  1726. return ONLINE_SET;
  1727. #if __UNUSED
  1728. /* debug code */
  1729. GPSD_LOG(LOG_ERROR, &session->context->errout,
  1730. "x%cGSV: set skyview_time %s frac_time %.2f\n", GSV_TALKER,
  1731. timespec_str(&session->gpsdata.skyview_time, ts_buf, sizeof(ts_buf)),
  1732. session->nmea.this_frac_time);
  1733. #endif
  1734. /* clear computed DOPs so they get recomputed. */
  1735. /* FIXME: this kills GPS reported dops... */
  1736. session->gpsdata.dop.xdop = NAN;
  1737. session->gpsdata.dop.ydop = NAN;
  1738. session->gpsdata.dop.tdop = NAN;
  1739. session->gpsdata.dop.gdop = NAN;
  1740. return SATELLITE_SET;
  1741. #undef GSV_TALKER
  1742. }
  1743. /* Garmin Estimated Position Error */
  1744. static gps_mask_t processPGRME(int c UNUSED, char *field[],
  1745. struct gps_device_t *session)
  1746. {
  1747. /*
  1748. * $PGRME,15.0,M,45.0,M,25.0,M*22
  1749. * 1 = horizontal error estimate
  1750. * 2 = units
  1751. * 3 = vertical error estimate
  1752. * 4 = units
  1753. * 5 = spherical error estimate
  1754. * 6 = units
  1755. * *
  1756. * * Garmin won't say, but the general belief is that these are 50% CEP.
  1757. * * We follow the advice at <http://gpsinformation.net/main/errors.htm>.
  1758. * * If this assumption changes here, it should also change in garmin.c
  1759. * * where we scale error estimates from Garmin binary packets, and
  1760. * * in libgpsd_core.c where we generate $PGRME.
  1761. */
  1762. gps_mask_t mask = ONLINE_SET;
  1763. if ('M' != field[2][0] ||
  1764. 'M' != field[4][0] ||
  1765. 'M' != field[6][0]) {
  1766. mask = ONLINE_SET;
  1767. } else {
  1768. session->newdata.epx = session->newdata.epy =
  1769. safe_atof(field[1]) * (1 / sqrt(2))
  1770. * (GPSD_CONFIDENCE / CEP50_SIGMA);
  1771. session->newdata.epv =
  1772. safe_atof(field[3]) * (GPSD_CONFIDENCE / CEP50_SIGMA);
  1773. session->newdata.sep =
  1774. safe_atof(field[5]) * (GPSD_CONFIDENCE / CEP50_SIGMA);
  1775. mask = HERR_SET | VERR_SET | PERR_IS;
  1776. }
  1777. GPSD_LOG(LOG_DATA, &session->context->errout,
  1778. "PGRME: epx=%.2f epy=%.2f sep=%.2f\n",
  1779. session->newdata.epx,
  1780. session->newdata.epy,
  1781. session->newdata.sep);
  1782. return mask;
  1783. }
  1784. /* Garmin GPS Fix Data Sentence
  1785. *
  1786. * FIXME: seems to happen after cycle ender, so little happens...
  1787. */
  1788. static gps_mask_t processPGRMF(int c UNUSED, char *field[],
  1789. struct gps_device_t *session)
  1790. {
  1791. /*
  1792. * $PGRMF,290,293895,160305,093802,13,5213.1439,N,02100.6511,E,A,2,0,226,2,1*11
  1793. *
  1794. * 1 = GPS week
  1795. * 2 = GPS seconds
  1796. * 3 = UTC Date ddmmyy
  1797. * 4 = UTC time hhmmss
  1798. * 5 = GPS leap seconds
  1799. * 6 = Latitude ddmm.mmmm
  1800. * 7 = N or S
  1801. * 8 = Longitude dddmm.mmmm
  1802. * 9 = E or W
  1803. * 10 = Mode, M = Manual, A = Automatic
  1804. * 11 = Fix type, 0 = No fix, 2 = 2D fix, 2 = 3D fix
  1805. * 12 = Ground Speed, 0 to 1151 km/hr
  1806. * 13 = Course over ground, 0 to 359 degrees true
  1807. * 14 = pdop, 0 to 9
  1808. * 15 = dop, 0 to 9
  1809. */
  1810. gps_mask_t mask = ONLINE_SET;
  1811. if (0 == merge_hhmmss(field[4], session) &&
  1812. 0 == merge_ddmmyy(field[3], session)) {
  1813. /* got a good data/time */
  1814. mask |= TIME_SET;
  1815. }
  1816. if ('A' != field[10][0]) {
  1817. /* Huh? */
  1818. return mask;
  1819. }
  1820. if ('\0' != field[5][0]) {
  1821. session->context->leap_seconds = atoi(field[5]);
  1822. session->context->valid = LEAP_SECOND_VALID;
  1823. }
  1824. if (0 == do_lat_lon(&field[6], &session->newdata)) {
  1825. mask |= LATLON_SET;
  1826. }
  1827. switch (field[11][0]) {
  1828. default:
  1829. /* Huh? */
  1830. break;
  1831. case '0':
  1832. session->newdata.mode = MODE_NO_FIX;
  1833. mask |= MODE_SET;
  1834. break;
  1835. case '1':
  1836. session->newdata.mode = MODE_2D;
  1837. mask |= MODE_SET;
  1838. break;
  1839. case '2':
  1840. session->newdata.mode = MODE_3D;
  1841. mask |= MODE_SET;
  1842. break;
  1843. }
  1844. session->newdata.speed = safe_atof(field[12]) / MPS_TO_KPH;
  1845. session->newdata.track = safe_atof(field[13]);
  1846. mask |= SPEED_SET | TRACK_SET;
  1847. session->gpsdata.dop.pdop = safe_atof(field[14]);
  1848. session->gpsdata.dop.tdop = safe_atof(field[15]);
  1849. mask |= DOP_SET;
  1850. GPSD_LOG(LOG_DATA, &session->context->errout,
  1851. "PGRMF: pdop %.1f tdop %.1f \n",
  1852. session->gpsdata.dop.pdop,
  1853. session->gpsdata.dop.tdop);
  1854. return mask;
  1855. }
  1856. /* Garmin Map Datum
  1857. *
  1858. * FIXME: seems to happen after cycle ender, so nothing happens...
  1859. */
  1860. static gps_mask_t processPGRMM(int c UNUSED, char *field[],
  1861. struct gps_device_t *session)
  1862. {
  1863. /*
  1864. * $PGRMM,NAD83*29
  1865. * 1 = Map Datum
  1866. */
  1867. gps_mask_t mask = ONLINE_SET;
  1868. if ('\0' != field[1][0]) {
  1869. strlcpy(session->newdata.datum, field[1],
  1870. sizeof(session->newdata.datum));
  1871. }
  1872. GPSD_LOG(LOG_DATA, &session->context->errout,
  1873. "PGRMM: datum=%.40s\n",
  1874. session->newdata.datum);
  1875. return mask;
  1876. }
  1877. /* Garmin Altitude Information */
  1878. static gps_mask_t processPGRMZ(int c UNUSED, char *field[],
  1879. struct gps_device_t *session)
  1880. {
  1881. /*
  1882. * $PGRMZ,246,f,3*1B
  1883. * 1 = Altitude (probably MSL) in feet
  1884. * 2 = f (feet)
  1885. * 3 = Mode
  1886. * 1 = No Fix
  1887. * 2 = 2D Fix
  1888. * 3 = 3D Fix
  1889. *
  1890. * From: Garmin Proprietary NMEA 0183 Sentences
  1891. * technical Specifications
  1892. * 190-00684-00, Revision C December 2008
  1893. */
  1894. gps_mask_t mask = ONLINE_SET;
  1895. if ('f' == field[2][0] &&
  1896. 0 < strlen(field[1])) {
  1897. /* have a GPS altitude, must be 3D */
  1898. /* seems to be altMSL. regressions show this matches GPGGA MSL */
  1899. session->newdata.altMSL = atoi(field[1]) / METERS_TO_FEET;
  1900. mask |= (ALTITUDE_SET);
  1901. }
  1902. switch (field[3][0]) {
  1903. default:
  1904. /* Huh? */
  1905. break;
  1906. case '1':
  1907. session->newdata.mode = MODE_NO_FIX;
  1908. mask |= MODE_SET;
  1909. break;
  1910. case '2':
  1911. session->newdata.mode = MODE_2D;
  1912. mask |= MODE_SET;
  1913. break;
  1914. case '3':
  1915. session->newdata.mode = MODE_3D;
  1916. mask |= MODE_SET;
  1917. break;
  1918. }
  1919. GPSD_LOG(LOG_DATA, &session->context->errout,
  1920. "PGRMZ: altMSL %.2f mode %d\n",
  1921. session->newdata.altMSL,
  1922. session->newdata.mode);
  1923. return mask;
  1924. }
  1925. /* Magellan Status */
  1926. static gps_mask_t processPMGNST(int c UNUSED, char *field[],
  1927. struct gps_device_t *session)
  1928. {
  1929. /*
  1930. * $PMGNST,01.75,3,T,816,11.1,-00496,00*43
  1931. * 1 = Firmware version number
  1932. * 2 = Mode (1 = no fix, 2 = 2D fix, 3 = 3D fix)
  1933. * 3 = T if we have a fix
  1934. * 4 = battery percentage left in tenths of a percent
  1935. * 5 = time left on the GPS battery in hours
  1936. * 6 = numbers change (freq. compensation?)
  1937. * 7 = PRN number receiving current focus
  1938. */
  1939. gps_mask_t mask = ONLINE_SET;
  1940. int newmode = atoi(field[3]);
  1941. if ('T' == field[4][0]) {
  1942. switch(newmode) {
  1943. default:
  1944. session->newdata.mode = MODE_NO_FIX;
  1945. break;
  1946. case 2:
  1947. session->newdata.mode = MODE_2D;
  1948. break;
  1949. case 3:
  1950. session->newdata.mode = MODE_3D;
  1951. break;
  1952. }
  1953. } else {
  1954. /* Can report 3D fix, but 'F' for no fix */
  1955. session->newdata.mode = MODE_NO_FIX;
  1956. }
  1957. mask |= MODE_SET;
  1958. GPSD_LOG(LOG_DATA, &session->context->errout,
  1959. "PMGNST: mode: %d\n",
  1960. session->newdata.mode);
  1961. return mask;
  1962. }
  1963. /* SiRF Estimated Position Errors */
  1964. static gps_mask_t processPSRFEPE(int c UNUSED, char *field[],
  1965. struct gps_device_t *session)
  1966. {
  1967. /*
  1968. * $PSRFEPE,100542.000,A,0.7,6.82,10.69,0.0,180.0*24
  1969. * 1 = UTC Time hhmmss.sss
  1970. * 2 = Status. A = Valid, V = Data not valid
  1971. * 3 = HDOP
  1972. * 4 = EHPE meters (Estimated Horizontal Position Error)
  1973. * 5 = EVPE meters (Estimated Vertical Position Error)
  1974. * 6 = EHVE meters (Estimated Speed Over Ground/Velocity Error)
  1975. * 7 = EHE degrees (Estimated Heading Error)
  1976. *
  1977. * SiRF won't say if these are 1-sigma or what...
  1978. */
  1979. gps_mask_t mask = STATUS_SET;
  1980. /* get time/ valid or not */
  1981. if ('\0' != field[1][0]) {
  1982. if (0 == merge_hhmmss(field[1], session)) {
  1983. register_fractional_time(field[0], field[1], session);
  1984. if (session->nmea.date.tm_year == 0) {
  1985. GPSD_LOG(LOG_WARN, &session->context->errout,
  1986. "can't use PSRFEPE time until after ZDA or RMC"
  1987. " has supplied a year.\n");
  1988. } else {
  1989. mask |= TIME_SET;
  1990. }
  1991. }
  1992. }
  1993. if ('A' != field[2][0]) {
  1994. /* Huh? */
  1995. return mask;
  1996. }
  1997. if ('\0' != field[3][0]) {
  1998. /* This adds nothing, it just agrees with the gpsd calculation
  1999. * from the skyview. Which is a nice confirmation. */
  2000. session->gpsdata.dop.hdop = safe_atof(field[3]);
  2001. mask |= DOP_SET;
  2002. }
  2003. if ('\0' != field[4][0]) {
  2004. /* EHPE (Estimated Horizontal Position Error) */
  2005. session->newdata.eph = safe_atof(field[4]);
  2006. mask |= HERR_SET;
  2007. }
  2008. if ('\0' != field[5][0]) {
  2009. /* Estimated Vertical Position Error (meters, 0.01 resolution) */
  2010. session->newdata.epv = safe_atof(field[5]);
  2011. mask |= VERR_SET;
  2012. }
  2013. if ('\0' != field[6][0]) {
  2014. /* Estimated Horizontal Speed Error meters/sec */
  2015. session->newdata.eps = safe_atof(field[6]);
  2016. }
  2017. if ('\0' != field[7][0]) {
  2018. /* Estimated Heading Error degrees */
  2019. session->newdata.epd = safe_atof(field[7]);
  2020. }
  2021. GPSD_LOG(LOG_PROG, &session->context->errout,
  2022. "PSRFEPE: hdop=%.1f eph=%.1f epv=%.1f eps=%.1f epd=%.1f\n",
  2023. session->gpsdata.dop.hdop,
  2024. session->newdata.eph,
  2025. session->newdata.epv,
  2026. session->newdata.eps,
  2027. session->newdata.epd);
  2028. return mask;
  2029. }
  2030. /* NMEA Map Datum
  2031. *
  2032. * FIXME: seems to happen after cycle ender, so nothing happens...
  2033. */
  2034. static gps_mask_t processDTM(int c UNUSED, char *field[],
  2035. struct gps_device_t *session)
  2036. {
  2037. /*
  2038. * $GPDTM,W84,C*52
  2039. * $GPDTM,xxx,x,xx.xxxx,x,xx.xxxx,x,,xxx*hh<CR><LF>
  2040. * 1 = Local datum code (xxx):
  2041. * W84 – WGS84
  2042. * W72 – WGS72
  2043. * S85 – SGS85
  2044. * P90 – PE90
  2045. * 999 – User defined
  2046. * IHO datum code
  2047. * 2 = Local datum sub code (x)
  2048. * 3 = Latitude offset in minutes (xx.xxxx)
  2049. * 4 = Latitude offset mark (N: +, S: -) (x)
  2050. * 5 = Longitude offset in minutes (xx.xxxx)
  2051. * 6 = Longitude offset mark (E: +, W: -) (x)
  2052. * 7 = Altitude offset in meters. Always null
  2053. * 8 = Datum (xxx):
  2054. * W84 – WGS84
  2055. * W72 – WGS72
  2056. * S85 – SGS85
  2057. * P90 – PE90
  2058. * 999 – User defined
  2059. * IHO datum code
  2060. * 9 = checksum
  2061. */
  2062. int i;
  2063. static struct
  2064. {
  2065. char *code;
  2066. char *name;
  2067. } codes[] = {
  2068. {"W84", "WGS84"},
  2069. {"W72", "WGS72"},
  2070. {"S85", "SGS85"},
  2071. {"P90", "PE90"},
  2072. {"999", "User Defined"},
  2073. {"", ""},
  2074. };
  2075. gps_mask_t mask = ONLINE_SET;
  2076. if ('\0' == field[1][0]) {
  2077. return mask;
  2078. }
  2079. for (i = 0; ; i++) {
  2080. if ('\0' == codes[i].code[0]) {
  2081. /* not found */
  2082. strlcpy(session->newdata.datum, field[1],
  2083. sizeof(session->newdata.datum));
  2084. break;
  2085. }
  2086. if (0 ==strcmp(codes[i].code, field[1])) {
  2087. strlcpy(session->newdata.datum, codes[i].name,
  2088. sizeof(session->newdata.datum));
  2089. break;
  2090. }
  2091. }
  2092. GPSD_LOG(LOG_DATA, &session->context->errout,
  2093. "xxDTM: datum=%.40s\n",
  2094. session->newdata.datum);
  2095. return mask;
  2096. }
  2097. /* NMEA 3.0 Estimated Position Error */
  2098. static gps_mask_t processGBS(int c UNUSED, char *field[],
  2099. struct gps_device_t *session)
  2100. {
  2101. /*
  2102. * $GPGBS,082941.00,2.4,1.5,3.9,25,,-43.7,27.5*65
  2103. * 1) UTC time of the fix associated with this sentence (hhmmss.ss)
  2104. * 2) Expected error in latitude (meters)
  2105. * 3) Expected error in longitude (meters)
  2106. * 4) Expected error in altitude (meters)
  2107. * 5) PRN of most likely failed satellite
  2108. * 6) Probability of missed detection for most likely failed satellite
  2109. * 7) Estimate of bias in meters on most likely failed satellite
  2110. * 8) Standard deviation of bias estimate
  2111. * 9) NMEA 4.1 GNSS ID
  2112. * 10) NMEA 4.1 Signal ID
  2113. * Checksum
  2114. *
  2115. * Fields 2, 3 and 4 are one standard deviation.
  2116. */
  2117. gps_mask_t mask = ONLINE_SET;
  2118. /* register fractional time for end-of-cycle detection */
  2119. register_fractional_time(field[0], field[1], session);
  2120. /* check that we're associated with the current fix */
  2121. if (session->nmea.date.tm_hour == DD(field[1])
  2122. && session->nmea.date.tm_min == DD(field[1] + 2)
  2123. && session->nmea.date.tm_sec == DD(field[1] + 4)) {
  2124. session->newdata.epy = safe_atof(field[2]);
  2125. session->newdata.epx = safe_atof(field[3]);
  2126. session->newdata.epv = safe_atof(field[4]);
  2127. GPSD_LOG(LOG_DATA, &session->context->errout,
  2128. "GBS: epx=%.2f epy=%.2f epv=%.2f\n",
  2129. session->newdata.epx,
  2130. session->newdata.epy,
  2131. session->newdata.epv);
  2132. mask = HERR_SET | VERR_SET;
  2133. } else {
  2134. GPSD_LOG(LOG_PROG, &session->context->errout,
  2135. "second in $GPGBS error estimates doesn't match.\n");
  2136. }
  2137. return mask;
  2138. }
  2139. static gps_mask_t processZDA(int c UNUSED, char *field[],
  2140. struct gps_device_t *session)
  2141. /* Time & Date */
  2142. {
  2143. /*
  2144. * $GPZDA,160012.71,11,03,2004,-1,00*7D
  2145. * 1) UTC time (hours, minutes, seconds, may have fractional subsecond)
  2146. * 2) Day, 01 to 31
  2147. * 3) Month, 01 to 12
  2148. * 4) Year (4 digits)
  2149. * 5) Local zone description, 00 to +- 13 hours
  2150. * 6) Local zone minutes description, apply same sign as local hours
  2151. * 7) Checksum
  2152. *
  2153. * Note: some devices, like the u-blox ANTARIS 4h, are known to ship ZDAs
  2154. * with some fields blank under poorly-understood circumstances (probably
  2155. * when they don't have satellite lock yet).
  2156. */
  2157. gps_mask_t mask = ONLINE_SET;
  2158. int year, mon, mday, century;
  2159. if (field[1][0] == '\0' || field[2][0] == '\0' || field[3][0] == '\0'
  2160. || field[4][0] == '\0') {
  2161. GPSD_LOG(LOG_WARN, &session->context->errout, "ZDA fields are empty\n");
  2162. return mask;
  2163. }
  2164. if (0 != merge_hhmmss(field[1], session)) {
  2165. /* bad time */
  2166. return mask;
  2167. }
  2168. /*
  2169. * We don't register fractional time here because want to leave
  2170. * ZDA out of end-of-cycle detection. Some devices sensibly emit it only
  2171. * when they have a fix, so watching for it can make them look
  2172. * like they have a variable fix reporting cycle.
  2173. */
  2174. year = atoi(field[4]);
  2175. mon = atoi(field[3]);
  2176. mday = atoi(field[2]);
  2177. century = year - year % 100;
  2178. if ( (1900 > year ) || (2200 < year ) ) {
  2179. GPSD_LOG(LOG_WARN, &session->context->errout,
  2180. "malformed ZDA year: %s\n", field[4]);
  2181. } else if ( (1 > mon ) || (12 < mon ) ) {
  2182. GPSD_LOG(LOG_WARN, &session->context->errout,
  2183. "malformed ZDA month: %s\n", field[3]);
  2184. } else if ( (1 > mday ) || (31 < mday ) ) {
  2185. GPSD_LOG(LOG_WARN, &session->context->errout,
  2186. "malformed ZDA day: %s\n", field[2]);
  2187. } else {
  2188. gpsd_century_update(session, century);
  2189. session->nmea.date.tm_year = year - 1900;
  2190. session->nmea.date.tm_mon = mon - 1;
  2191. session->nmea.date.tm_mday = mday;
  2192. mask = TIME_SET;
  2193. }
  2194. return mask;
  2195. }
  2196. static gps_mask_t processHDG(int c UNUSED, char *field[],
  2197. struct gps_device_t *session)
  2198. {
  2199. /*
  2200. * $SDHDG,234.6,,,1.3,E*34
  2201. *
  2202. * $--HDG,h.h,d.d,a,v.v,a*hh<CR><LF>
  2203. * Magnetic sensor heading, degrees
  2204. * Magnetic deviation, degrees E/W
  2205. * Magnetic variation, degrees, E/W
  2206. *
  2207. * 1. To obtain Magnetic Heading:
  2208. * Add Easterly deviation (E) to Magnetic Sensor Reading
  2209. * Subtract Westerly deviation (W) from Magnetic Sensor Reading
  2210. * 2. To obtain True Heading:
  2211. * Add Easterly variation (E) to Magnetic Heading
  2212. * Subtract Westerly variation (W) from Magnetic Heading
  2213. * 3. Variation and deviation fields shall be null fields if unknown.
  2214. */
  2215. gps_mask_t mask = ONLINE_SET;
  2216. double sensor_heading;
  2217. double magnetic_deviation;
  2218. if ( 0 == strlen(field[1])) {
  2219. /* no data */
  2220. return mask;
  2221. }
  2222. sensor_heading = safe_atof(field[1]);
  2223. if ((0.0 > sensor_heading) || (360.0 < sensor_heading)) {
  2224. /* bad data */
  2225. return mask;
  2226. }
  2227. magnetic_deviation = safe_atof(field[2]);
  2228. if ((0.0 > magnetic_deviation) || (360.0 < magnetic_deviation)) {
  2229. /* bad data */
  2230. return mask;
  2231. }
  2232. switch (field[2][0]) {
  2233. case 'E':
  2234. sensor_heading += magnetic_deviation;
  2235. break;
  2236. case 'W':
  2237. sensor_heading += magnetic_deviation;
  2238. break;
  2239. default:
  2240. /* ignore */
  2241. break;
  2242. }
  2243. /* good data */
  2244. session->newdata.magnetic_track = sensor_heading;
  2245. mask |= MAGNETIC_TRACK_SET;
  2246. /* get magnetic variation */
  2247. if ('\0' != field[3][0] &&
  2248. '\0' != field[4][0]) {
  2249. session->newdata.magnetic_var = safe_atof(field[3]);
  2250. switch (field[4][0]) {
  2251. case 'E':
  2252. /* no change */
  2253. mask |= MAGNETIC_TRACK_SET;
  2254. break;
  2255. case 'W':
  2256. session->newdata.magnetic_var = -session->newdata.magnetic_var;
  2257. mask |= MAGNETIC_TRACK_SET;
  2258. break;
  2259. default:
  2260. /* huh? */
  2261. session->newdata.magnetic_var = NAN;
  2262. break;
  2263. }
  2264. }
  2265. GPSD_LOG(LOG_DATA, &session->context->errout,
  2266. "$SDHDG heading %lf var %.1f\n",
  2267. session->newdata.magnetic_track,
  2268. session->newdata.magnetic_var);
  2269. return mask;
  2270. }
  2271. static gps_mask_t processHDT(int c UNUSED, char *field[],
  2272. struct gps_device_t *session)
  2273. {
  2274. /*
  2275. * $HEHDT,341.8,T*21
  2276. *
  2277. * HDT,x.x*hh<cr><lf>
  2278. *
  2279. * The only data field is true heading in degrees.
  2280. * The following field is required to be 'T' indicating a true heading.
  2281. * It is followed by a mandatory nmea_checksum.
  2282. */
  2283. gps_mask_t mask = ONLINE_SET;
  2284. double heading;
  2285. if ( 0 == strlen(field[1])) {
  2286. /* no data */
  2287. return mask;
  2288. }
  2289. heading = safe_atof(field[1]);
  2290. if ((0.0 > heading) || (360.0 < heading)) {
  2291. /* bad data */
  2292. return mask;
  2293. }
  2294. /* good data */
  2295. session->gpsdata.attitude.heading = heading;
  2296. mask |= (ATTITUDE_SET);
  2297. GPSD_LOG(LOG_DATA, &session->context->errout,
  2298. "$HEHDT heading %lf.\n",
  2299. session->gpsdata.attitude.heading);
  2300. return mask;
  2301. }
  2302. static gps_mask_t processDBT(int c UNUSED, char *field[],
  2303. struct gps_device_t *session)
  2304. {
  2305. /*
  2306. * $SDDBT,7.7,f,2.3,M,1.3,F*05
  2307. * 1) Depth below sounder in feet
  2308. * 2) Fixed value 'f' indicating feet
  2309. * 3) Depth below sounder in meters
  2310. * 4) Fixed value 'M' indicating meters
  2311. * 5) Depth below sounder in fathoms
  2312. * 6) Fixed value 'F' indicating fathoms
  2313. * 7) Checksum.
  2314. *
  2315. * In real-world sensors, sometimes not all three conversions are reported.
  2316. */
  2317. gps_mask_t mask = ONLINE_SET;
  2318. if (field[3][0] != '\0') {
  2319. session->newdata.depth = safe_atof(field[3]);
  2320. mask |= (ALTITUDE_SET);
  2321. } else if (field[1][0] != '\0') {
  2322. session->newdata.depth = safe_atof(field[1]) / METERS_TO_FEET;
  2323. mask |= (ALTITUDE_SET);
  2324. } else if (field[5][0] != '\0') {
  2325. session->newdata.depth = safe_atof(field[5]) / METERS_TO_FATHOMS;
  2326. mask |= (ALTITUDE_SET);
  2327. }
  2328. GPSD_LOG(LOG_DATA, &session->context->errout,
  2329. "mode %d, depth %lf.\n",
  2330. session->newdata.mode,
  2331. session->newdata.depth);
  2332. return mask;
  2333. }
  2334. /* GPS Text message */
  2335. static gps_mask_t processTXT(int count, char *field[],
  2336. struct gps_device_t *session)
  2337. {
  2338. /*
  2339. * $GNTXT,01,01,01,PGRM inv format*2A
  2340. * 1 Number of sentences for full data
  2341. * 1 Sentence 1 of 1
  2342. * 01 Message type
  2343. * 00 - error
  2344. * 01 - warning
  2345. * 02 - notice
  2346. * 07 - user
  2347. * PGRM inv format ASCII text
  2348. *
  2349. * Can occur with talker IDs:
  2350. * BD (Beidou),
  2351. * GA (Galileo),
  2352. * GB (Beidou),
  2353. * GL (GLONASS),
  2354. * GN (GLONASS, any combination GNSS),
  2355. * GP (GPS, SBAS, QZSS),
  2356. * QZ (QZSS).
  2357. */
  2358. gps_mask_t mask = ONLINE_SET;
  2359. int msgType = 0;
  2360. char *msgType_txt = "Unknown";
  2361. if ( 5 != count) {
  2362. return mask;
  2363. }
  2364. msgType = atoi(field[3]);
  2365. switch ( msgType ) {
  2366. case 0:
  2367. msgType_txt = "Error";
  2368. break;
  2369. case 1:
  2370. msgType_txt = "Warning";
  2371. break;
  2372. case 2:
  2373. msgType_txt = "Notice";
  2374. break;
  2375. case 7:
  2376. msgType_txt = "User";
  2377. break;
  2378. }
  2379. /* maximum text lenght unknown, guess 80 */
  2380. GPSD_LOG(LOG_WARN, &session->context->errout,
  2381. "TXT: %.10s: %.80s\n",
  2382. msgType_txt, field[4]);
  2383. return mask;
  2384. }
  2385. #ifdef TNT_ENABLE
  2386. static gps_mask_t processTNTHTM(int c UNUSED, char *field[],
  2387. struct gps_device_t *session)
  2388. {
  2389. /*
  2390. * Proprietary sentence for True North Technologies Magnetic Compass.
  2391. * This may also apply to some Honeywell units since they may have been
  2392. * designed by True North.
  2393. $PTNTHTM,14223,N,169,N,-43,N,13641,2454*15
  2394. HTM,x.x,a,x.x,a,x.x,a,x.x,x.x*hh<cr><lf>
  2395. Fields in order:
  2396. 1. True heading (compass measurement + deviation + variation)
  2397. 2. magnetometer status character:
  2398. C = magnetometer calibration alarm
  2399. L = low alarm
  2400. M = low warning
  2401. N = normal
  2402. O = high warning
  2403. P = high alarm
  2404. V = magnetometer voltage level alarm
  2405. 3. pitch angle
  2406. 4. pitch status character - see field 2 above
  2407. 5. roll angle
  2408. 6. roll status character - see field 2 above
  2409. 7. dip angle
  2410. 8. relative magnitude horizontal component of earth's magnetic field
  2411. *hh mandatory nmea_checksum
  2412. By default, angles are reported as 26-bit integers: weirdly, the
  2413. technical manual says either 0 to 65535 or -32768 to 32767 can
  2414. occur as a range.
  2415. */
  2416. gps_mask_t mask = ONLINE_SET;
  2417. session->gpsdata.attitude.heading = safe_atof(field[1]);
  2418. session->gpsdata.attitude.mag_st = *field[2];
  2419. session->gpsdata.attitude.pitch = safe_atof(field[3]);
  2420. session->gpsdata.attitude.pitch_st = *field[4];
  2421. session->gpsdata.attitude.roll = safe_atof(field[5]);
  2422. session->gpsdata.attitude.roll_st = *field[6];
  2423. session->gpsdata.attitude.dip = safe_atof(field[7]);
  2424. session->gpsdata.attitude.mag_x = safe_atof(field[8]);
  2425. mask |= (ATTITUDE_SET);
  2426. GPSD_LOG(LOG_DATA, &session->context->errout,
  2427. "$PTNTHTM heading %lf (%c).\n",
  2428. session->gpsdata.attitude.heading,
  2429. session->gpsdata.attitude.mag_st);
  2430. return mask;
  2431. }
  2432. static gps_mask_t processTNTA(int c UNUSED, char *field[],
  2433. struct gps_device_t *session)
  2434. {
  2435. /*
  2436. * Proprietary sentence for iSync GRClok/LNRClok.
  2437. $PTNTA,20000102173852,1,T4,,,6,1,0*32
  2438. 1. Date/time in format year, month, day, hour, minute, second
  2439. 2. Oscillator quality 0:warming up, 1:freerun, 2:disciplined.
  2440. 3. Always T4. Format indicator.
  2441. 4. Interval ppsref-ppsout in [ns]. Blank if no ppsref.
  2442. 5. Fine phase comparator in approx. [ns]. Always close to -500 or
  2443. +500 if not disciplined. Blank if no ppsref.
  2444. 6. iSync Status. 0:warming up or no light, 1:tracking set-up,
  2445. 2:track to PPSREF, 3:synch to PPSREF, 4:Free Run. Track OFF,
  2446. 5:FR. PPSREF unstable, 6:FR. No PPSREF, 7:FREEZE, 8:factory
  2447. used, 9:searching Rb line
  2448. 7. GPS messages indicator. 0:do not take account, 1:take account,
  2449. but no message, 2:take account, partially ok, 3:take account,
  2450. totally ok.
  2451. 8. Transfer quality of date/time. 0:no, 1:manual, 2:GPS, older
  2452. than x hours, 3:GPS, fresh.
  2453. */
  2454. gps_mask_t mask = ONLINE_SET;
  2455. if (strcmp(field[3], "T4") == 0) {
  2456. struct oscillator_t *osc = &session->gpsdata.osc;
  2457. unsigned int quality = atoi(field[2]);
  2458. unsigned int delta = atoi(field[4]);
  2459. unsigned int fine = atoi(field[5]);
  2460. unsigned int status = atoi(field[6]);
  2461. char deltachar = field[4][0];
  2462. osc->running = (quality > 0);
  2463. osc->reference = (deltachar && (deltachar != '?'));
  2464. if (osc->reference) {
  2465. if (delta < 500) {
  2466. osc->delta = fine;
  2467. } else {
  2468. osc->delta = ((delta < 500000000) ? delta : 1000000000 - delta);
  2469. }
  2470. } else {
  2471. osc->delta = 0;
  2472. }
  2473. osc->disciplined = ((quality == 2) && (status == 3));
  2474. mask |= OSCILLATOR_SET;
  2475. GPSD_LOG(LOG_DATA, &session->context->errout,
  2476. "PTNTA,T4: quality=%s, delta=%s, fine=%s, status=%s\n",
  2477. field[2], field[4], field[5], field[6]);
  2478. }
  2479. return mask;
  2480. }
  2481. #endif /* TNT_ENABLE */
  2482. #ifdef OCEANSERVER_ENABLE
  2483. static gps_mask_t processOHPR(int c UNUSED, char *field[],
  2484. struct gps_device_t *session)
  2485. {
  2486. /*
  2487. * Proprietary sentence for OceanServer Magnetic Compass.
  2488. OHPR,x.x,x.x,x.x,x.x,x.x,x.x,x.x,x.x,x.x,x.x,x.x,x.x,x.x,x.x,x.x,x.x,x.x,x.x*hh<cr><lf>
  2489. Fields in order:
  2490. 1. Azimuth
  2491. 2. Pitch Angle
  2492. 3. Roll Angle
  2493. 4. Sensor temp, degrees centigrade
  2494. 5. Depth (feet)
  2495. 6. Magnetic Vector Length
  2496. 7-9. 3 axis Magnetic Field readings x,y,z
  2497. 10. Acceleration Vector Length
  2498. 11-13. 3 axis Acceleration Readings x,y,z
  2499. 14. Reserved
  2500. 15-16. 2 axis Gyro Output, X,y
  2501. 17. Reserved
  2502. 18. Reserved
  2503. *hh mandatory nmea_checksum
  2504. */
  2505. gps_mask_t mask = ONLINE_SET;
  2506. session->gpsdata.attitude.heading = safe_atof(field[1]);
  2507. session->gpsdata.attitude.pitch = safe_atof(field[2]);
  2508. session->gpsdata.attitude.roll = safe_atof(field[3]);
  2509. session->gpsdata.attitude.temp = safe_atof(field[4]);
  2510. session->gpsdata.attitude.depth = safe_atof(field[5]) / METERS_TO_FEET;
  2511. session->gpsdata.attitude.mag_len = safe_atof(field[6]);
  2512. session->gpsdata.attitude.mag_x = safe_atof(field[7]);
  2513. session->gpsdata.attitude.mag_y = safe_atof(field[8]);
  2514. session->gpsdata.attitude.mag_z = safe_atof(field[9]);
  2515. session->gpsdata.attitude.acc_len = safe_atof(field[10]);
  2516. session->gpsdata.attitude.acc_x = safe_atof(field[11]);
  2517. session->gpsdata.attitude.acc_y = safe_atof(field[12]);
  2518. session->gpsdata.attitude.acc_z = safe_atof(field[13]);
  2519. session->gpsdata.attitude.gyro_x = safe_atof(field[15]);
  2520. session->gpsdata.attitude.gyro_y = safe_atof(field[16]);
  2521. mask |= (ATTITUDE_SET);
  2522. GPSD_LOG(LOG_DATA, &session->context->errout,
  2523. "Heading %lf.\n", session->gpsdata.attitude.heading);
  2524. return mask;
  2525. }
  2526. #endif /* OCEANSERVER_ENABLE */
  2527. #ifdef ASHTECH_ENABLE
  2528. /* Ashtech sentences take this format:
  2529. * $PASHDR,type[,val[,val]]*CS
  2530. * type is an alphabetic subsentence type
  2531. *
  2532. * Oxford Technical Solutions (OxTS) also uses the $PASHR sentence,
  2533. * but with a very different sentence contents:
  2534. * $PASHR,HHMMSS.SSS,HHH.HH,T,RRR.RR,PPP.PP,aaa.aa,r.rrr,p.ppp,h.hhh,Q1,Q2*CS
  2535. *
  2536. * so field 1 in ASHTECH is always alphabetic and numeric in OXTS
  2537. *
  2538. */
  2539. static gps_mask_t processPASHR(int c UNUSED, char *field[],
  2540. struct gps_device_t *session)
  2541. {
  2542. gps_mask_t mask = ONLINE_SET;
  2543. char ts_buf[TIMESPEC_LEN];
  2544. if (0 == strcmp("ACK", field[1])) {
  2545. /* ACK */
  2546. GPSD_LOG(LOG_DATA, &session->context->errout, "PASHR,ACK\n");
  2547. return ONLINE_SET;
  2548. } else if (0 == strcmp("MCA", field[1])) {
  2549. /* MCA, raw data */
  2550. GPSD_LOG(LOG_DATA, &session->context->errout, "PASHR,MCA\n");
  2551. return ONLINE_SET;
  2552. } else if (0 == strcmp("NAK", field[1])) {
  2553. /* NAK */
  2554. GPSD_LOG(LOG_DATA, &session->context->errout, "PASHR,NAK\n");
  2555. return ONLINE_SET;
  2556. } else if (0 == strcmp("PBN", field[1])) {
  2557. /* PBN, position data */
  2558. /* FIXME: decode this for ECEF */
  2559. GPSD_LOG(LOG_DATA, &session->context->errout, "PASHR,PBN\n");
  2560. return ONLINE_SET;
  2561. } else if (0 == strcmp("POS", field[1])) { /* 3D Position */
  2562. /* $PASHR,POS,
  2563. *
  2564. * 2: position type:
  2565. * 0 = autonomous
  2566. * 1 = position differentially corrected with RTCM code
  2567. * 2 = position differentially corrected with CPD float solution
  2568. * 3 = position is CPD fixed solution
  2569. */
  2570. mask |= MODE_SET | STATUS_SET | CLEAR_IS;
  2571. if (0 == strlen(field[2])) {
  2572. /* empty first field means no 3D fix is available */
  2573. session->gpsdata.status = STATUS_NO_FIX;
  2574. session->newdata.mode = MODE_NO_FIX;
  2575. } else {
  2576. int satellites_used;
  2577. /* if we make it this far, we at least have a 3D fix */
  2578. session->newdata.mode = MODE_3D;
  2579. if (1 <= atoi(field[2]))
  2580. session->gpsdata.status = STATUS_DGPS_FIX;
  2581. else
  2582. session->gpsdata.status = STATUS_FIX;
  2583. /* don't use as this breaks the GPGSV counter
  2584. * session->gpsdata.satellites_used = atoi(field[3]); */
  2585. satellites_used = atoi(field[3]);
  2586. if (0 == merge_hhmmss(field[4], session)) {
  2587. register_fractional_time(field[0], field[4], session);
  2588. mask |= TIME_SET;
  2589. }
  2590. if (0 == do_lat_lon(&field[5], &session->newdata)) {
  2591. mask |= LATLON_SET;
  2592. if ('\0' != field[9][0]) {
  2593. /* altitude is already WGS 84 */
  2594. session->newdata.altHAE = safe_atof(field[9]);
  2595. mask |= ALTITUDE_SET;
  2596. }
  2597. }
  2598. session->newdata.track = safe_atof(field[11]);
  2599. session->newdata.speed = safe_atof(field[12]) / MPS_TO_KPH;
  2600. session->newdata.climb = safe_atof(field[13]);
  2601. session->gpsdata.dop.pdop = safe_atof(field[14]);
  2602. session->gpsdata.dop.hdop = safe_atof(field[15]);
  2603. session->gpsdata.dop.vdop = safe_atof(field[16]);
  2604. session->gpsdata.dop.tdop = safe_atof(field[17]);
  2605. mask |= (SPEED_SET | TRACK_SET | CLIMB_SET);
  2606. mask |= DOP_SET;
  2607. GPSD_LOG(LOG_DATA, &session->context->errout,
  2608. "PASHR,POS: hhmmss=%s lat=%.2f lon=%.2f altHAE=%.f"
  2609. " speed=%.2f track=%.2f climb=%.2f mode=%d status=%d"
  2610. " pdop=%.2f hdop=%.2f vdop=%.2f tdop=%.2f used=%d\n",
  2611. field[4], session->newdata.latitude,
  2612. session->newdata.longitude, session->newdata.altHAE,
  2613. session->newdata.speed, session->newdata.track,
  2614. session->newdata.climb, session->newdata.mode,
  2615. session->gpsdata.status, session->gpsdata.dop.pdop,
  2616. session->gpsdata.dop.hdop, session->gpsdata.dop.vdop,
  2617. session->gpsdata.dop.tdop, satellites_used);
  2618. }
  2619. } else if (0 == strcmp("RID", field[1])) { /* Receiver ID */
  2620. (void)snprintf(session->subtype, sizeof(session->subtype) - 1,
  2621. "%s ver %s", field[2], field[3]);
  2622. GPSD_LOG(LOG_DATA, &session->context->errout,
  2623. "PASHR,RID: subtype=%s mask={}\n",
  2624. session->subtype);
  2625. return mask;
  2626. } else if (0 == strcmp("SAT", field[1])) { /* Satellite Status */
  2627. struct satellite_t *sp;
  2628. int i, n = session->gpsdata.satellites_visible = atoi(field[2]);
  2629. session->gpsdata.satellites_used = 0;
  2630. for (i = 0, sp = session->gpsdata.skyview;
  2631. sp < session->gpsdata.skyview + n; sp++, i++) {
  2632. sp->PRN = (short)atoi(field[3 + i * 5 + 0]);
  2633. sp->azimuth = (double)atoi(field[3 + i * 5 + 1]);
  2634. sp->elevation = (double)atoi(field[3 + i * 5 + 2]);
  2635. sp->ss = safe_atof(field[3 + i * 5 + 3]);
  2636. sp->used = false;
  2637. if (field[3 + i * 5 + 4][0] == 'U') {
  2638. sp->used = true;
  2639. session->gpsdata.satellites_used++;
  2640. }
  2641. }
  2642. GPSD_LOG(LOG_DATA, &session->context->errout,
  2643. "PASHR,SAT: used=%d\n",
  2644. session->gpsdata.satellites_used);
  2645. session->gpsdata.skyview_time.tv_sec = 0;
  2646. session->gpsdata.skyview_time.tv_nsec = 0;
  2647. mask |= SATELLITE_SET | USED_IS;
  2648. } else if (0 == strcmp("T", field[3])) { /* Assume OxTS PASHR */
  2649. /* FIXME: decode OxTS $PASHDR, time is wrong, breaks cycle order */
  2650. if (0 == merge_hhmmss(field[1], session)) {
  2651. register_fractional_time(field[0], field[1], session);
  2652. /* mask |= TIME_SET; confuses cycle order */
  2653. }
  2654. session->gpsdata.attitude.heading = safe_atof(field[2]);
  2655. session->gpsdata.attitude.roll = safe_atof(field[4]);
  2656. session->gpsdata.attitude.pitch = safe_atof(field[5]);
  2657. /* mask |= ATTITUDE_SET; * confuses cycle order ?? */
  2658. GPSD_LOG(LOG_DATA, &session->context->errout,
  2659. "PASHR (OxTS) time %s, heading %lf.\n",
  2660. timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
  2661. session->gpsdata.attitude.heading);
  2662. }
  2663. return mask;
  2664. }
  2665. #endif /* ASHTECH_ENABLE */
  2666. #ifdef MTK3301_ENABLE
  2667. static gps_mask_t processMTK3301(int c UNUSED, char *field[],
  2668. struct gps_device_t *session)
  2669. {
  2670. int msg, reason;
  2671. msg = atoi(&(session->nmea.field[0])[4]);
  2672. switch (msg) {
  2673. case 001: /* ACK / NACK */
  2674. reason = atoi(field[2]);
  2675. if (atoi(field[1]) == -1)
  2676. GPSD_LOG(LOG_WARN, &session->context->errout,
  2677. "MTK NACK: unknown sentence\n");
  2678. else if (reason < 3) {
  2679. const char *mtk_reasons[] = {
  2680. "Invalid",
  2681. "Unsupported",
  2682. "Valid but Failed",
  2683. "Valid success"
  2684. };
  2685. GPSD_LOG(LOG_WARN, &session->context->errout,
  2686. "MTK NACK: %s, reason: %s\n",
  2687. field[1], mtk_reasons[reason]);
  2688. }
  2689. else
  2690. GPSD_LOG(LOG_DATA, &session->context->errout,
  2691. "MTK ACK: %s\n", field[1]);
  2692. return ONLINE_SET;
  2693. case 424: /* PPS pulse width response */
  2694. /*
  2695. * Response will look something like: $PMTK424,0,0,1,0,69*12
  2696. * The pulse width is in field 5 (69 in this example). This
  2697. * sentence is poorly documented at:
  2698. * http://www.trimble.com/embeddedsystems/condor-gps-module.aspx?dtID=documentation
  2699. *
  2700. * Packet Type: 324 PMTK_API_SET_OUTPUT_CTL
  2701. * Packet meaning
  2702. * Write the TSIP / antenna / PPS configuration data to the Flash memory.
  2703. * DataField [Data0]:TSIP Packet[on/off]
  2704. * 0 - Disable TSIP output (Default).
  2705. * 1 - Enable TSIP output.
  2706. * [Data1]:Antenna Detect[on/off]
  2707. * 0 - Disable antenna detect function (Default).
  2708. * 1 - Enable antenna detect function.
  2709. * [Data2]:PPS on/off
  2710. * 0 - Disable PPS function.
  2711. * 1 - Enable PPS function (Default).
  2712. * [Data3]:PPS output timing
  2713. * 0 - Always output PPS (Default).
  2714. * 1 - Only output PPS when GPS position is fixed.
  2715. * [Data4]:PPS pulse width
  2716. * 1~16367999: 61 ns~(61x 16367999) ns (Default = 69)
  2717. *
  2718. * The documentation does not give the units of the data field.
  2719. * Andy Walls <andy@silverblocksystems.net> says:
  2720. *
  2721. * "The best I can figure using an oscilloscope, is that it is
  2722. * in units of 16.368000 MHz clock cycles. It may be
  2723. * different for any other unit other than the Trimble
  2724. * Condor. 69 cycles / 16368000 cycles/sec = 4.216 microseconds
  2725. * [which is the pulse width I have observed]"
  2726. *
  2727. * Support for this theory comes from the fact that crystal
  2728. * TXCOs with a 16.368MHZ period are commonly available from
  2729. * multiple vendors. Furthermore, 61*69 = 4209, which is
  2730. * close to the observed cycle time and suggests that the
  2731. * documentation is trying to indicate 61ns units.
  2732. *
  2733. * He continues:
  2734. *
  2735. * "I chose [127875] because to divides 16368000 nicely and the
  2736. * pulse width is close to 1/100th of a second. Any number
  2737. * the user wants to use would be fine. 127875 cycles /
  2738. * 16368000 cycles/second = 1/128 seconds = 7.8125
  2739. * milliseconds"
  2740. */
  2741. /* too short? Make it longer */
  2742. if (atoi(field[5]) < 127875)
  2743. (void)nmea_send(session, "$PMTK324,0,0,1,0,127875");
  2744. return ONLINE_SET;
  2745. case 705: /* return device subtype */
  2746. (void)strlcat(session->subtype, field[1], sizeof(session->subtype));
  2747. (void)strlcat(session->subtype, "-", sizeof(session->subtype));
  2748. (void)strlcat(session->subtype, field[2], sizeof(session->subtype));
  2749. return ONLINE_SET;
  2750. default:
  2751. GPSD_LOG(LOG_PROG, &session->context->errout,
  2752. "MTK: unknown msg: %d\n", msg);
  2753. return ONLINE_SET; /* ignore */
  2754. }
  2755. }
  2756. #endif /* MTK3301_ENABLE */
  2757. #ifdef SKYTRAQ_ENABLE
  2758. /* Recommended Minimum 3D GNSS Data */
  2759. static gps_mask_t processPSTI030(int count, char *field[],
  2760. struct gps_device_t *session)
  2761. {
  2762. /*
  2763. * $PSTI,030,hhmmss.sss,A,dddmm.mmmmmmm,a,dddmm.mmmmmmm,a,x.x,x.x,x.x,x.x,ddmmyy,a.x.x,x.x*hh<CR><LF>
  2764. * 1 030 Sentence ID
  2765. * 2 225446.334 Time of fix 22:54:46 UTC
  2766. * 3 A Status of Fix: A = Autonomous, valid;
  2767. * V = invalid
  2768. * 4,5 4916.45,N Latitude 49 deg. 16.45 min North
  2769. * 6,7 12311.12,W Longitude 123 deg. 11.12 min West
  2770. * 8 103.323 Mean Sea Level meters
  2771. * 9 0.00 East Velocity meters/sec
  2772. * 10 0.00 North Velocity meters/sec
  2773. * 11 0.00 Up Velocity meters/sec
  2774. * 12 181194 Date of fix 18 November 1994
  2775. * 13 A FAA mode indicator
  2776. * See faa_mode() for possible mode values.
  2777. * 14 1.2 RTK Age
  2778. * 15 4.2 RTK Ratio
  2779. * 16 *68 mandatory nmea_checksum
  2780. *
  2781. * In private email, SkyTraq says F mode is 10x more accurate
  2782. * than R mode.
  2783. */
  2784. gps_mask_t mask = ONLINE_SET;
  2785. if ( 16 != count )
  2786. return mask;
  2787. if ('V' == field[3][0] ||
  2788. 'N' == field[13][0]) {
  2789. /* nav warning, or FAA not valid, ignore the rest of the data */
  2790. session->gpsdata.status = STATUS_NO_FIX;
  2791. session->newdata.mode = MODE_NO_FIX;
  2792. mask |= MODE_SET;
  2793. } else if ('A' == field[3][0]) {
  2794. double east, north, climb;
  2795. /* data valid */
  2796. if (field[2][0] != '\0' && field[12][0] != '\0') {
  2797. /* good date and time */
  2798. if (0 == merge_hhmmss(field[2], session) &&
  2799. 0 == merge_ddmmyy(field[12], session)) {
  2800. mask |= TIME_SET;
  2801. register_fractional_time( "PSTI030", field[2], session);
  2802. }
  2803. }
  2804. if (0 == do_lat_lon(&field[4], &session->newdata)) {
  2805. session->newdata.mode = MODE_2D;
  2806. mask |= LATLON_SET;
  2807. if ('\0' != field[8][0]) {
  2808. /* altitude is MSL */
  2809. session->newdata.altMSL = safe_atof(field[8]);
  2810. mask |= ALTITUDE_SET;
  2811. session->newdata.mode = MODE_3D;
  2812. /* Let gpsd_error_model() deal with geoid_sep and altHAE */
  2813. }
  2814. mask |= MODE_SET;
  2815. }
  2816. /* convert ENU to track */
  2817. /* this has more precision than GPVTG, GPVTG comes earlier
  2818. * in the cycle */
  2819. east = safe_atof(field[9]); /* east velocity m/s */
  2820. north = safe_atof(field[10]); /* north velocity m/s */
  2821. /* up velocity m/s */
  2822. climb = safe_atof(field[11]);
  2823. session->newdata.NED.velN = north;
  2824. session->newdata.NED.velE = east;
  2825. session->newdata.NED.velD = -climb;
  2826. mask |= VNED_SET;
  2827. session->gpsdata.status = faa_mode(field[13][0]);
  2828. /* Ignore RTK Age and RTK Ratio, for now */
  2829. }
  2830. GPSD_LOG(LOG_DATA, &session->context->errout,
  2831. "PSTI,030: ddmmyy=%s hhmmss=%s lat=%.2f lon=%.2f "
  2832. "status=%d, RTK(Age=%s Ratio=%s)\n",
  2833. field[12], field[2],
  2834. session->newdata.latitude,
  2835. session->newdata.longitude,
  2836. session->gpsdata.status,
  2837. field[14], field[15]);
  2838. return mask;
  2839. }
  2840. /*
  2841. * Skytraq sentences take this format:
  2842. * $PSTI,type[,val[,val]]*CS
  2843. * type is a 2 or 3 digit subsentence type
  2844. *
  2845. * Note: this sentence can be at least 100 chars long.
  2846. * That violates the NMEA 3.01 max of 82.
  2847. *
  2848. */
  2849. static gps_mask_t processPSTI(int count, char *field[],
  2850. struct gps_device_t *session)
  2851. {
  2852. gps_mask_t mask = ONLINE_SET;
  2853. if ( 0 != strncmp(session->subtype, "Skytraq", 7) ) {
  2854. /* this is skytraq, but not marked yet, so probe for Skytraq */
  2855. (void)gpsd_write(session, "\xA0\xA1\x00\x02\x02\x01\x03\x0d\x0a",9);
  2856. }
  2857. if (0 == strcmp("00", field[1]) ) {
  2858. if ( 4 != count )
  2859. return mask;
  2860. /* 1 PPS Timing report ID */
  2861. GPSD_LOG(LOG_DATA, &session->context->errout,
  2862. "PSTI,00: Mode: %s, Length: %s, Quant: %s\n",
  2863. field[2], field[3], field[4]);
  2864. return mask;
  2865. }
  2866. if (0 == strcmp("001", field[1])) {
  2867. /* Active Antenna Status Report */
  2868. GPSD_LOG(LOG_DATA, &session->context->errout,
  2869. "PSTI,001: Count: %d\n", count);
  2870. return mask;
  2871. }
  2872. if (0 == strcmp("005", field[1])) {
  2873. /* GPIO 10 event-triggered time & position stamp. */
  2874. GPSD_LOG(LOG_DATA, &session->context->errout,
  2875. "PSTI,005: Count: %d\n", count);
  2876. return mask;
  2877. }
  2878. if (0 == strcmp("030", field[1])) {
  2879. /* Recommended Minimum 3D GNSS Data */
  2880. return processPSTI030(count, field, session);
  2881. }
  2882. if (0 == strcmp("032", field[1])) {
  2883. if ( 16 != count )
  2884. return mask;
  2885. /* RTK Baseline */
  2886. if ( 0 == strcmp(field[4], "A")) {
  2887. /* Status Valid */
  2888. if (field[2][0] != '\0' && field[3][0] != '\0') {
  2889. /* good date and time */
  2890. if (0 == merge_hhmmss(field[2], session) &&
  2891. 0 == merge_ddmmyy(field[3], session)) {
  2892. mask |= TIME_SET;
  2893. register_fractional_time( "PSTI032", field[2], session);
  2894. }
  2895. }
  2896. }
  2897. GPSD_LOG( LOG_DATA,&session->context->errout,
  2898. "PSTI,032: stat:%s mode: %s E: %s N: %s U:%s L:%s C:%s\n",
  2899. field[4], field[5],
  2900. field[6], field[7], field[8],
  2901. field[9], field[10]);
  2902. return mask;
  2903. }
  2904. GPSD_LOG(LOG_DATA, &session->context->errout,
  2905. "PSTI,%s: Unknown type, Count: %d\n", field[1], count);
  2906. return mask;
  2907. }
  2908. /*
  2909. * Skytraq undocumented debug sentences take this format:
  2910. * $STI,type,val*CS
  2911. * type is a 2 char subsentence type
  2912. * Note: NO checksum
  2913. */
  2914. static gps_mask_t processSTI(int count, char *field[],
  2915. struct gps_device_t *session)
  2916. {
  2917. gps_mask_t mask = ONLINE_SET;
  2918. if ( 0 != strncmp(session->subtype, "Skytraq", 7) ) {
  2919. /* this is skytraq, but marked yet, so probe for Skytraq */
  2920. (void)gpsd_write(session, "\xA0\xA1\x00\x02\x02\x01\x03\x0d\x0a",9);
  2921. }
  2922. if ( 0 == strcmp( field[1], "IC") ) {
  2923. /* $STI,IC,error=XX, this is always very bad, but undocumented */
  2924. GPSD_LOG(LOG_ERROR, &session->context->errout,
  2925. "Skytraq: $STI,%s,%s\n", field[1], field[2]);
  2926. return mask;
  2927. }
  2928. GPSD_LOG(LOG_DATA, &session->context->errout,
  2929. "STI,%s: Unknown type, Count: %d\n", field[1], count);
  2930. return mask;
  2931. }
  2932. #endif /* SKYTRAQ_ENABLE */
  2933. /**************************************************************************
  2934. *
  2935. * Entry points begin here
  2936. *
  2937. **************************************************************************/
  2938. gps_mask_t nmea_parse(char *sentence, struct gps_device_t * session)
  2939. /* parse an NMEA sentence, unpack it into a session structure */
  2940. {
  2941. typedef gps_mask_t(*nmea_decoder) (int count, char *f[],
  2942. struct gps_device_t * session);
  2943. static struct
  2944. {
  2945. char *name;
  2946. int nf; /* minimum number of fields required to parse */
  2947. bool cycle_continue; /* cycle continuer? */
  2948. nmea_decoder decoder;
  2949. } nmea_phrase[] = {
  2950. {"PGRMB", 0, false, NULL}, /* ignore Garmin DGPS Beacon Info */
  2951. {"PGRMC", 0, false, NULL}, /* ignore Garmin Sensor Config */
  2952. {"PGRME", 7, false, processPGRME},
  2953. {"PGRMF", 15, false, processPGRMF}, /* Garmin GPS Fix Data */
  2954. {"PGRMH", 0, false, NULL}, /* ignore Garmin Aviation Height... */
  2955. {"PGRMI", 0, false, NULL}, /* ignore Garmin Sensor Init */
  2956. {"PGRMM", 2, false, processPGRMM}, /* Garmin Map Datum */
  2957. {"PGRMO", 0, false, NULL}, /* ignore Garmin Sentence Enable */
  2958. {"PGRMT", 0, false, NULL}, /* ignore Garmin Sensor Info */
  2959. {"PGRMV", 0, false, NULL}, /* ignore Garmin 3D Velocity Info */
  2960. {"PGRMZ", 4, false, processPGRMZ},
  2961. /*
  2962. * Basic sentences must come after the PG* ones, otherwise
  2963. * Garmins can get stuck in a loop that looks like this:
  2964. *
  2965. * 1. A Garmin GPS in NMEA mode is detected.
  2966. *
  2967. * 2. PGRMC is sent to reconfigure to Garmin binary mode.
  2968. * If successful, the GPS echoes the phrase.
  2969. *
  2970. * 3. nmea_parse() sees the echo as RMC because the talker
  2971. * ID is ignored, and fails to recognize the echo as
  2972. * PGRMC and ignore it.
  2973. *
  2974. * 4. The mode is changed back to NMEA, resulting in an
  2975. * infinite loop.
  2976. */
  2977. {"AAM", 0, false, NULL}, /* ignore Waypoint Arrival Alarm */
  2978. {"APB", 0, false, NULL}, /* ignore Autopilot Sentence B */
  2979. {"BOD", 0, false, NULL}, /* ignore Bearing Origin to Destination */
  2980. /* ignore Bearing & Distance to Waypoint, Great Circle*/
  2981. {"BWC", 0, false, NULL},
  2982. {"DBT", 7, true, processDBT},
  2983. {"DPT", 0, false, NULL}, /* ignore depth */
  2984. {"DTM", 2, false, processDTM}, /* datum */
  2985. {"GBS", 7, false, processGBS},
  2986. {"GGA", 13, false, processGGA},
  2987. {"GLC", 0, false, NULL}, /* ignore Geographic Position, LoranC */
  2988. {"GLL", 7, false, processGLL},
  2989. {"GNS", 13, false, processGNS},
  2990. {"GRS", 0, false, NULL}, /* ignore GNSS Range Residuals */
  2991. {"GSA", 18, false, processGSA},
  2992. {"GST", 8, false, processGST},
  2993. {"GSV", 0, false, processGSV},
  2994. /* ignore Heading, Deviation and Variation */
  2995. {"HDG", 0, false, processHDG},
  2996. {"HDT", 1, false, processHDT},
  2997. {"MSS", 0, false, NULL}, /* ignore beacon receiver status */
  2998. {"MTW", 0, false, NULL}, /* ignore Water Temperature */
  2999. {"MWD", 0, false, NULL}, /* ignore Wind Direction and Speed */
  3000. {"MWV", 0, false, NULL}, /* ignore Wind Speed and Angle */
  3001. #ifdef OCEANSERVER_ENABLE
  3002. {"OHPR", 18, false, processOHPR},
  3003. #endif /* OCEANSERVER_ENABLE */
  3004. #ifdef ASHTECH_ENABLE
  3005. /* general handler for Ashtech */
  3006. {"PASHR", 3, false, processPASHR},
  3007. #endif /* ASHTECH_ENABLE */
  3008. {"PMGNST", 8, false, processPMGNST}, /* Magellan Status */
  3009. #ifdef MTK3301_ENABLE
  3010. {"PMTK", 3, false, processMTK3301},
  3011. /* for some reason the parser no longer triggering on leading chars */
  3012. {"PMTK001", 3, false, processMTK3301},
  3013. {"PMTK424", 3, false, processMTK3301},
  3014. {"PMTK705", 3, false, processMTK3301},
  3015. {"PMTKCHN", 0, false, NULL}, /* ignore MediaTek Channel Status */
  3016. #endif /* MTK3301_ENABLE */
  3017. {"PRWIZCH", 0, false, NULL}, /* ignore Rockwell Channel Status */
  3018. {"PSRFEPE", 7, false, processPSRFEPE}, /* SiRF Estimated Errors */
  3019. {"PTFTTXT", 0, false, NULL}, /* ignore unknown uptime */
  3020. {"PUBX", 0, false, NULL}, /* ignore u-blox Antaris */
  3021. #ifdef TNT_ENABLE
  3022. {"PTNTHTM", 9, false, processTNTHTM},
  3023. {"PTNTA", 8, false, processTNTA},
  3024. #endif /* TNT_ENABLE */
  3025. #ifdef SKYTRAQ_ENABLE
  3026. {"PSTI", 2, false, processPSTI}, /* $PSTI Skytraq */
  3027. {"STI", 2, false, processSTI}, /* $STI Skytraq */
  3028. #endif /* SKYTRAQ_ENABLE */
  3029. // $PSTM ST Micro STA8088xx/STA8089xx/STA8090xx
  3030. {"PSTM", 0, false, NULL},
  3031. /* ignore Recommended Minimum Navigation Info, waypoint */
  3032. {"RMB", 0, false, NULL},
  3033. {"RMC", 8, false, processRMC},
  3034. {"RTE", 0, false, NULL}, /* ignore Routes */
  3035. {"TXT", 5, false, processTXT},
  3036. {"VHW", 0, false, NULL}, /* ignore Water Speed and Heading */
  3037. {"VLW", 0, false, NULL}, /* ignore Dual ground/water distance */
  3038. {"VTG", 5, false, processVTG},
  3039. {"XDR", 0, false, NULL}, /* ignore $HCXDR, IMU? */
  3040. {"XTE", 0, false, NULL}, /* ignore Cross-Track Error */
  3041. {"ZDA", 4, false, processZDA},
  3042. };
  3043. int count;
  3044. gps_mask_t mask = 0;
  3045. unsigned int i, thistag, lasttag;
  3046. char *p, *e;
  3047. volatile char *t;
  3048. uint64_t lasttag_mask = 0;
  3049. uint64_t thistag_mask = 0;
  3050. char ts_buf1[TIMESPEC_LEN];
  3051. char ts_buf2[TIMESPEC_LEN];
  3052. #ifdef SKYTRAQ_ENABLE
  3053. bool skytraq_sti = false;
  3054. #endif
  3055. /*
  3056. * We've had reports that on the Garmin GPS-10 the device sometimes
  3057. * (1:1000 or so) sends garbage packets that have a valid checksum
  3058. * but are like 2 successive NMEA packets merged together in one
  3059. * with some fields lost. Usually these are much longer than the
  3060. * legal limit for NMEA, so we can cope by just tossing out overlong
  3061. * packets. This may be a generic bug of all Garmin chipsets.
  3062. */
  3063. if (strlen(sentence) > NMEA_MAX) {
  3064. GPSD_LOG(LOG_WARN, &session->context->errout,
  3065. "Overlong packet of %zd chars rejected.\n",
  3066. strlen(sentence));
  3067. return ONLINE_SET;
  3068. }
  3069. /* make an editable copy of the sentence */
  3070. (void)strlcpy((char *)session->nmea.fieldcopy, sentence,
  3071. sizeof(session->nmea.fieldcopy) - 1);
  3072. /* discard the checksum part */
  3073. for (p = (char *)session->nmea.fieldcopy;
  3074. (*p != '*') && (*p >= ' ');)
  3075. ++p;
  3076. if (*p == '*')
  3077. *p++ = ','; /* otherwise we drop the last field */
  3078. #ifdef SKYTRAQ_ENABLE_UNUSED
  3079. /* $STI is special, no trailing *, or chacksum */
  3080. if ( 0 != strncmp( "STI,", sentence, 4) ) {
  3081. skytraq_sti = true;
  3082. *p++ = ','; /* otherwise we drop the last field */
  3083. }
  3084. #endif
  3085. *p = '\0';
  3086. e = p;
  3087. /* split sentence copy on commas, filling the field array */
  3088. count = 0;
  3089. t = p; /* end of sentence */
  3090. p = (char *)session->nmea.fieldcopy + 1; /* beginning of tag, 'G' not '$' */
  3091. /* while there is a search string and we haven't run off the buffer... */
  3092. while ((p != NULL) && (p <= t)) {
  3093. session->nmea.field[count] = p; /* we have a field. record it */
  3094. if ((p = strchr(p, ',')) != NULL) { /* search for the next delimiter */
  3095. *p = '\0'; /* replace it with a NUL */
  3096. count++; /* bump the counters and continue */
  3097. p++;
  3098. }
  3099. }
  3100. /* point remaining fields at empty string, just in case */
  3101. for (i = (unsigned int)count;
  3102. i <
  3103. (unsigned)(sizeof(session->nmea.field) /
  3104. sizeof(session->nmea.field[0])); i++)
  3105. session->nmea.field[i] = e;
  3106. /* sentences handlers will tell us when they have fractional time */
  3107. session->nmea.latch_frac_time = false;
  3108. #ifdef __UNUSED
  3109. // debug
  3110. GPSD_LOG(&session->context->errout, 0,
  3111. "NMEA0183: got %s\n", session->nmea.field[0]);
  3112. #endif // __UNUSED
  3113. /* dispatch on field zero, the sentence tag */
  3114. for (thistag = i = 0;
  3115. i < (unsigned)(sizeof(nmea_phrase) / sizeof(nmea_phrase[0])); ++i) {
  3116. char *s = session->nmea.field[0];
  3117. if (strlen(nmea_phrase[i].name) == 3
  3118. #ifdef SKYTRAQ_ENABLE
  3119. /* $STI is special */
  3120. && !skytraq_sti
  3121. #endif
  3122. ) {
  3123. s += 2; /* skip talker ID */
  3124. }
  3125. if (strcmp(nmea_phrase[i].name, s) == 0) {
  3126. if (NULL == nmea_phrase[i].decoder) {
  3127. /* no decoder for this sentence */
  3128. mask = ONLINE_SET;
  3129. GPSD_LOG(LOG_DATA, &session->context->errout,
  3130. "No decoder for sentence %s\n",
  3131. session->nmea.field[0]);
  3132. break;
  3133. }
  3134. if (count < nmea_phrase[i].nf) {
  3135. /* sentence to short */
  3136. mask = ONLINE_SET;
  3137. GPSD_LOG(LOG_DATA, &session->context->errout,
  3138. "Sentence %s too short\n", session->nmea.field[0]);
  3139. break;
  3140. }
  3141. mask = (nmea_phrase[i].decoder)(count, session->nmea.field,
  3142. session);
  3143. if (nmea_phrase[i].cycle_continue)
  3144. session->nmea.cycle_continue = true;
  3145. /*
  3146. * Must force this to be nz, as we're going to rely on a zero
  3147. * value to mean "no previous tag" later.
  3148. */
  3149. thistag = i + 1;
  3150. break;
  3151. }
  3152. }
  3153. /* prevent overaccumulation of sat reports */
  3154. if (!str_starts_with(session->nmea.field[0] + 2, "GSV"))
  3155. session->nmea.last_gsv_talker = '\0';
  3156. if (!str_starts_with(session->nmea.field[0] + 2, "GSA"))
  3157. session->nmea.last_gsa_talker = '\0';
  3158. /* timestamp recording for fixes happens here */
  3159. if ((mask & TIME_SET) != 0) {
  3160. session->newdata.time = gpsd_utc_resolve(session);
  3161. GPSD_LOG(LOG_DATA, &session->context->errout,
  3162. "%s time is %s = %d-%02d-%02dT%02d:%02d:%02d.%03ldZ\n",
  3163. session->nmea.field[0],
  3164. timespec_str(&session->newdata.time, ts_buf1, sizeof(ts_buf1)),
  3165. 1900 + session->nmea.date.tm_year,
  3166. session->nmea.date.tm_mon + 1,
  3167. session->nmea.date.tm_mday,
  3168. session->nmea.date.tm_hour,
  3169. session->nmea.date.tm_min,
  3170. session->nmea.date.tm_sec,
  3171. session->nmea.subseconds.tv_nsec / 1000000L);
  3172. /*
  3173. * If we have time and PPS is available, assume we have good time.
  3174. * Because this is a generic driver we don't really have enough
  3175. * information for a sharper test, so we'll leave it up to the
  3176. * PPS code to do its own sanity filtering.
  3177. */
  3178. mask |= NTPTIME_IS;
  3179. }
  3180. /*
  3181. * The end-of-cycle detector. This code depends on just one
  3182. * assumption: if a sentence with a timestamp occurs just before
  3183. * start of cycle, then it is always good to trigger a report on
  3184. * that sentence in the future. For devices with a fixed cycle
  3185. * this should work perfectly, locking in detection after one
  3186. * cycle. Most split-cycle devices (Garmin 48, for example) will
  3187. * work fine. Problems will only arise if a a sentence that
  3188. * occurs just before timestamp increments also occurs in
  3189. * mid-cycle, as in the Garmin eXplorist 210; those might jitter.
  3190. */
  3191. /* cast for 32/64 bit compat */
  3192. GPSD_LOG(LOG_DATA, &session->context->errout,
  3193. "%s time %s last %s latch %d cont %d enders %#llx\n",
  3194. session->nmea.field[0],
  3195. timespec_str(&session->nmea.this_frac_time, ts_buf1,
  3196. sizeof(ts_buf1)),
  3197. timespec_str(&session->nmea.last_frac_time, ts_buf2,
  3198. sizeof(ts_buf2)),
  3199. session->nmea.latch_frac_time,
  3200. session->nmea.cycle_continue,
  3201. (unsigned long long)session->nmea.cycle_enders);
  3202. lasttag = session->nmea.lasttag;
  3203. if (0 < session->nmea.lasttag) {
  3204. lasttag_mask = (uint64_t)1 << lasttag;
  3205. }
  3206. if (0 < thistag) {
  3207. thistag_mask = (uint64_t)1 << thistag;
  3208. }
  3209. if (session->nmea.latch_frac_time) {
  3210. timespec_t ts_delta;
  3211. TS_SUB(&ts_delta, &session->nmea.this_frac_time,
  3212. &session->nmea.last_frac_time);
  3213. if (0.01 < fabs(TSTONS(&ts_delta))) {
  3214. /* time changed */
  3215. mask |= CLEAR_IS;
  3216. GPSD_LOG(LOG_PROG, &session->context->errout,
  3217. "%s starts a reporting cycle. lasttag %d\n",
  3218. session->nmea.field[0], lasttag);
  3219. /*
  3220. * Have we seen a previously timestamped NMEA tag?
  3221. * If so, designate as end-of-cycle marker.
  3222. * But not if there are continuation sentences;
  3223. * those get sorted after the last timestamped sentence
  3224. *
  3225. */
  3226. if (0 < lasttag &&
  3227. 0 == (session->nmea.cycle_enders & lasttag_mask) &&
  3228. !session->nmea.cycle_continue) {
  3229. session->nmea.cycle_enders |= lasttag_mask;
  3230. /* cast for 32/64 bit compat */
  3231. GPSD_LOG(LOG_PROG, &session->context->errout,
  3232. "tagged %s as a cycle ender. %#llx\n",
  3233. nmea_phrase[lasttag - 1].name,
  3234. (unsigned long long)lasttag_mask);
  3235. }
  3236. }
  3237. } else {
  3238. /* extend the cycle to an un-timestamped sentence? */
  3239. if (0 != (session->nmea.cycle_enders & lasttag_mask)) {
  3240. GPSD_LOG(LOG_PROG, &session->context->errout,
  3241. "%s is just after a cycle ender.\n",
  3242. session->nmea.field[0]);
  3243. }
  3244. if (session->nmea.cycle_continue) {
  3245. GPSD_LOG(LOG_PROG, &session->context->errout,
  3246. "%s extends the reporting cycle.\n",
  3247. session->nmea.field[0]);
  3248. /* change ender */
  3249. session->nmea.cycle_enders &= ~lasttag_mask;
  3250. session->nmea.cycle_enders |= thistag_mask;
  3251. }
  3252. }
  3253. /* here's where we check for end-of-cycle */
  3254. if ((session->nmea.latch_frac_time || session->nmea.cycle_continue)
  3255. && (session->nmea.cycle_enders & thistag_mask)!=0) {
  3256. GPSD_LOG(LOG_PROG, &session->context->errout,
  3257. "%s ends a reporting cycle.\n",
  3258. session->nmea.field[0]);
  3259. mask |= REPORT_IS;
  3260. }
  3261. if (session->nmea.latch_frac_time)
  3262. session->nmea.lasttag = thistag;
  3263. /* we might have a (somewhat) reliable end-of-cycle */
  3264. if (session->nmea.cycle_enders != 0)
  3265. session->cycle_end_reliable = true;
  3266. /* don't downgrade mode if holding previous fix */
  3267. /* usually because of xxRMC which does not report 2D/3D */
  3268. if (MODE_SET == (mask & MODE_SET) &&
  3269. MODE_3D == session->gpsdata.fix.mode &&
  3270. MODE_NO_FIX != session->newdata.mode &&
  3271. (0 != isfinite(session->lastfix.altHAE) ||
  3272. 0 != isfinite(session->oldfix.altHAE) ||
  3273. 0 != isfinite(session->lastfix.altMSL) ||
  3274. 0 != isfinite(session->oldfix.altMSL))) {
  3275. session->newdata.mode = session->gpsdata.fix.mode;
  3276. }
  3277. return mask;
  3278. }
  3279. #endif /* NMEA0183_ENABLE */
  3280. void nmea_add_checksum(char *sentence)
  3281. /* add NMEA checksum to a possibly *-terminated sentence */
  3282. {
  3283. unsigned char sum = '\0';
  3284. char c, *p = sentence;
  3285. if (*p == '$' || *p == '!') {
  3286. p++;
  3287. }
  3288. while (((c = *p) != '*') && (c != '\0')) {
  3289. sum ^= c;
  3290. p++;
  3291. }
  3292. *p++ = '*';
  3293. (void)snprintf(p, 5, "%02X\r\n", (unsigned)sum);
  3294. }
  3295. ssize_t nmea_write(struct gps_device_t *session, char *buf, size_t len UNUSED)
  3296. /* ship a command to the GPS, adding * and correct checksum */
  3297. {
  3298. (void)strlcpy(session->msgbuf, buf, sizeof(session->msgbuf));
  3299. if (session->msgbuf[0] == '$') {
  3300. (void)strlcat(session->msgbuf, "*", sizeof(session->msgbuf));
  3301. nmea_add_checksum(session->msgbuf);
  3302. } else
  3303. (void)strlcat(session->msgbuf, "\r\n", sizeof(session->msgbuf));
  3304. session->msgbuflen = strlen(session->msgbuf);
  3305. return gpsd_write(session, session->msgbuf, session->msgbuflen);
  3306. }
  3307. ssize_t nmea_send(struct gps_device_t * session, const char *fmt, ...)
  3308. {
  3309. char buf[BUFSIZ];
  3310. va_list ap;
  3311. va_start(ap, fmt);
  3312. (void)vsnprintf(buf, sizeof(buf) - 5, fmt, ap);
  3313. va_end(ap);
  3314. return nmea_write(session, buf, strlen(buf));
  3315. }