Devuan fork of gpsd
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gpsd_json.c 127 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754
  1. /****************************************************************************
  2. NAME
  3. gpsd_json.c - move data between in-core and JSON structures
  4. DESCRIPTION
  5. These are functions (used only by the daemon) to dump the contents
  6. of various core data structures in JSON.
  7. PERMISSIONS
  8. Written by Eric S. Raymond, 2009
  9. This file is Copyright (c) 2010-2019 by the GPSD project
  10. SPDX-License-Identifier: BSD-2-clause
  11. ***************************************************************************/
  12. #include "gpsd_config.h" /* must be before all includes */
  13. #include <assert.h>
  14. #include <ctype.h>
  15. #include <math.h>
  16. #include <stdio.h>
  17. #include <string.h> /* for strcat(), strlcpy() */
  18. #include "gpsd.h"
  19. #include "bits.h"
  20. #include "strfuncs.h"
  21. #ifdef SOCKET_EXPORT_ENABLE
  22. #include "gps_json.h"
  23. #include "timespec.h"
  24. #include "revision.h"
  25. /* *INDENT-OFF* */
  26. #define JSON_BOOL(x) ((x)?"true":"false")
  27. /*
  28. * Manifest names for the gnss_type enum - must be kept synced with it.
  29. * Also, masks so we can tell what packet types correspond to each class.
  30. */
  31. /* the map of device class names */
  32. struct classmap_t {
  33. char *name;
  34. int typemask;
  35. int packetmask;
  36. };
  37. #define CLASSMAP_NITEMS 5
  38. struct classmap_t classmap[CLASSMAP_NITEMS] = {
  39. /* name typemask packetmask */
  40. {"ANY", 0, 0},
  41. {"GPS", SEEN_GPS, GPS_TYPEMASK},
  42. {"RTCM2", SEEN_RTCM2, PACKET_TYPEMASK(RTCM2_PACKET)},
  43. {"RTCM3", SEEN_RTCM3, PACKET_TYPEMASK(RTCM3_PACKET)},
  44. {"AIS", SEEN_AIS, PACKET_TYPEMASK(AIVDM_PACKET)},
  45. };
  46. /* *INDENT-ON* */
  47. char *json_stringify(char *to,
  48. size_t len,
  49. const char *from)
  50. /* escape double quotes and control characters inside a JSON string */
  51. {
  52. const char *sp;
  53. char *tp;
  54. tp = to;
  55. /*
  56. * The limit is len-6 here because we need to be leave room for
  57. * each character to generate an up to 6-character Java-style
  58. * escape
  59. */
  60. for (sp = from; *sp != '\0' && ((tp - to) < ((int)len - 6)); sp++) {
  61. if (!isascii((unsigned char) *sp) || iscntrl((unsigned char) *sp)) {
  62. *tp++ = '\\';
  63. switch (*sp) {
  64. case '\b':
  65. *tp++ = 'b';
  66. break;
  67. case '\f':
  68. *tp++ = 'f';
  69. break;
  70. case '\n':
  71. *tp++ = 'n';
  72. break;
  73. case '\r':
  74. *tp++ = 'r';
  75. break;
  76. case '\t':
  77. *tp++ = 't';
  78. break;
  79. default:
  80. /* ugh, we'd prefer a C-style escape here, but this is JSON */
  81. /* http://www.ietf.org/rfc/rfc4627.txt
  82. * section 2.5, escape is \uXXXX */
  83. /* don't forget the NUL in the output count! */
  84. (void)snprintf(tp, 6, "u%04x", 0x00ff & (unsigned int)*sp);
  85. tp += strlen(tp);
  86. }
  87. } else {
  88. if (*sp == '"' || *sp == '\\')
  89. *tp++ = '\\';
  90. *tp++ = *sp;
  91. }
  92. }
  93. *tp = '\0';
  94. return to;
  95. }
  96. void json_version_dump( char *reply, size_t replylen)
  97. {
  98. (void)snprintf(reply, replylen,
  99. "{\"class\":\"VERSION\",\"release\":\"%s\",\"rev\":\"%s\","
  100. "\"proto_major\":%d,\"proto_minor\":%d}\r\n",
  101. VERSION, REVISION,
  102. GPSD_PROTO_MAJOR_VERSION, GPSD_PROTO_MINOR_VERSION);
  103. }
  104. void json_tpv_dump(const struct gps_device_t *session,
  105. const struct gps_policy_t *policy,
  106. char *reply, size_t replylen)
  107. {
  108. const struct gps_data_t *gpsdata = &session->gpsdata;
  109. assert(replylen > sizeof(char *));
  110. (void)strlcpy(reply, "{\"class\":\"TPV\",", replylen);
  111. if (gpsdata->dev.path[0] != '\0')
  112. /* Note: Assumes /dev paths are always plain ASCII */
  113. str_appendf(reply, replylen, "\"device\":\"%s\",", gpsdata->dev.path);
  114. if (STATUS_DGPS_FIX <= gpsdata->status) {
  115. /* to save rebuilding all the regressions, skip NO_FIX and FIX */
  116. str_appendf(reply, replylen, "\"status\":%d,", gpsdata->status);
  117. }
  118. str_appendf(reply, replylen, "\"mode\":%d,", gpsdata->fix.mode);
  119. if (0 < gpsdata->fix.time.tv_sec) {
  120. char tbuf[JSON_DATE_MAX+1];
  121. str_appendf(reply, replylen,
  122. "\"time\":\"%s\",",
  123. timespec_to_iso8601(gpsdata->fix.time,
  124. tbuf, sizeof(tbuf)));
  125. }
  126. if (LEAP_SECOND_VALID == (session->context->valid & LEAP_SECOND_VALID)) {
  127. str_appendf(reply, replylen, "\"leapseconds\":%d,",
  128. session->context->leap_seconds);
  129. }
  130. if (isfinite(gpsdata->fix.ept) != 0)
  131. str_appendf(reply, replylen, "\"ept\":%.3f,", gpsdata->fix.ept);
  132. /*
  133. * Suppressing TPV fields that would be invalid because the fix
  134. * quality doesn't support them is nice for cutting down on the
  135. * volume of meaningless output, but the real reason to do it is
  136. * that we've observed that geodetic fix computation is unstable
  137. * in a way that tends to change low-order digits in invalid
  138. * fixes. Dumping these tends to cause cross-architecture failures
  139. * in the regression tests. This effect has been seen on SiRF-II
  140. * chips, which are quite common.
  141. */
  142. if (gpsdata->fix.mode >= MODE_2D) {
  143. double altitude = NAN;
  144. if (isfinite(gpsdata->fix.latitude) != 0)
  145. str_appendf(reply, replylen,
  146. "\"lat\":%.9f,", gpsdata->fix.latitude);
  147. if (isfinite(gpsdata->fix.longitude) != 0)
  148. str_appendf(reply, replylen,
  149. "\"lon\":%.9f,", gpsdata->fix.longitude);
  150. if (0 != isfinite(gpsdata->fix.altHAE)) {
  151. altitude = gpsdata->fix.altHAE;
  152. str_appendf(reply, replylen,
  153. "\"altHAE\":%.3f,", gpsdata->fix.altHAE);
  154. }
  155. if (0 != isfinite(gpsdata->fix.altMSL)) {
  156. altitude = gpsdata->fix.altMSL;
  157. str_appendf(reply, replylen,
  158. "\"altMSL\":%.3f,", gpsdata->fix.altMSL);
  159. }
  160. if (0 != isfinite(altitude)) {
  161. // DEPRECATED, undefined
  162. str_appendf(reply, replylen,
  163. "\"alt\":%.3f,", altitude);
  164. }
  165. if (isfinite(gpsdata->fix.epx) != 0)
  166. str_appendf(reply, replylen, "\"epx\":%.3f,", gpsdata->fix.epx);
  167. if (isfinite(gpsdata->fix.epy) != 0)
  168. str_appendf(reply, replylen, "\"epy\":%.3f,", gpsdata->fix.epy);
  169. if (isfinite(gpsdata->fix.epv) != 0)
  170. str_appendf(reply, replylen, "\"epv\":%.3f,", gpsdata->fix.epv);
  171. if (isfinite(gpsdata->fix.track) != 0)
  172. str_appendf(reply, replylen, "\"track\":%.4f,", gpsdata->fix.track);
  173. if (0 != isfinite(gpsdata->fix.magnetic_track))
  174. str_appendf(reply, replylen, "\"magtrack\":%.4f,",
  175. gpsdata->fix.magnetic_track);
  176. if (0 != isfinite(gpsdata->fix.magnetic_var))
  177. str_appendf(reply, replylen, "\"magvar\":%.1f,",
  178. gpsdata->fix.magnetic_var);
  179. if (isfinite(gpsdata->fix.speed) != 0)
  180. str_appendf(reply, replylen, "\"speed\":%.3f,", gpsdata->fix.speed);
  181. if ((gpsdata->fix.mode >= MODE_3D) && isfinite(gpsdata->fix.climb) != 0)
  182. str_appendf(reply, replylen, "\"climb\":%.3f,", gpsdata->fix.climb);
  183. if (isfinite(gpsdata->fix.epd) != 0)
  184. str_appendf(reply, replylen, "\"epd\":%.4f,", gpsdata->fix.epd);
  185. if (isfinite(gpsdata->fix.eps) != 0)
  186. str_appendf(reply, replylen, "\"eps\":%.2f,", gpsdata->fix.eps);
  187. if (gpsdata->fix.mode >= MODE_3D) {
  188. if (isfinite(gpsdata->fix.epc) != 0)
  189. str_appendf(reply, replylen, "\"epc\":%.2f,", gpsdata->fix.epc);
  190. /* ECEF is in meters, so %.3f is millimeter resolution */
  191. if (0 != isfinite(gpsdata->fix.ecef.x))
  192. str_appendf(reply, replylen, "\"ecefx\":%.2f,",
  193. gpsdata->fix.ecef.x);
  194. if (0 != isfinite(gpsdata->fix.ecef.y))
  195. str_appendf(reply, replylen, "\"ecefy\":%.2f,",
  196. gpsdata->fix.ecef.y);
  197. if (0 != isfinite(gpsdata->fix.ecef.z))
  198. str_appendf(reply, replylen, "\"ecefz\":%.2f,",
  199. gpsdata->fix.ecef.z);
  200. if (0 != isfinite(gpsdata->fix.ecef.vx))
  201. str_appendf(reply, replylen, "\"ecefvx\":%.2f,",
  202. gpsdata->fix.ecef.vx);
  203. if (0 != isfinite(gpsdata->fix.ecef.vy))
  204. str_appendf(reply, replylen, "\"ecefvy\":%.2f,",
  205. gpsdata->fix.ecef.vy);
  206. if (0 != isfinite(gpsdata->fix.ecef.vz))
  207. str_appendf(reply, replylen, "\"ecefvz\":%.2f,",
  208. gpsdata->fix.ecef.vz);
  209. if (0 != isfinite(gpsdata->fix.ecef.pAcc))
  210. str_appendf(reply, replylen, "\"ecefpAcc\":%.2f,",
  211. gpsdata->fix.ecef.pAcc);
  212. if (0 != isfinite(gpsdata->fix.ecef.vAcc))
  213. str_appendf(reply, replylen, "\"ecefvAcc\":%.2f,",
  214. gpsdata->fix.ecef.vAcc);
  215. /* NED is in meters, so %.3f is millimeter resolution */
  216. if (0 != isfinite(gpsdata->fix.NED.relPosN))
  217. str_appendf(reply, replylen, "\"relN\":%.3f,",
  218. gpsdata->fix.NED.relPosN);
  219. if (0 != isfinite(gpsdata->fix.NED.relPosE))
  220. str_appendf(reply, replylen, "\"relE\":%.3f,",
  221. gpsdata->fix.NED.relPosE);
  222. if (0 != isfinite(gpsdata->fix.NED.relPosD))
  223. str_appendf(reply, replylen, "\"relD\":%.3f,",
  224. gpsdata->fix.NED.relPosD);
  225. if (0 != isfinite(gpsdata->fix.NED.velN))
  226. str_appendf(reply, replylen, "\"velN\":%.3f,",
  227. gpsdata->fix.NED.velN);
  228. if (0 != isfinite(gpsdata->fix.NED.velE))
  229. str_appendf(reply, replylen, "\"velE\":%.3f,",
  230. gpsdata->fix.NED.velE);
  231. if (0 != isfinite(gpsdata->fix.NED.velD))
  232. str_appendf(reply, replylen, "\"velD\":%.3f,",
  233. gpsdata->fix.NED.velD);
  234. if (0 != isfinite(gpsdata->fix.geoid_sep))
  235. str_appendf(reply, replylen, "\"geoidSep\":%.3f,",
  236. gpsdata->fix.geoid_sep);
  237. }
  238. if (policy->timing) {
  239. char rtime_str[TIMESPEC_LEN];
  240. char ts_buf[TIMESPEC_LEN];
  241. struct timespec rtime_tmp;
  242. (void)clock_gettime(CLOCK_REALTIME, &rtime_tmp);
  243. str_appendf(reply, replylen, "\"rtime\":%s,",
  244. timespec_str(&rtime_tmp, rtime_str,
  245. sizeof(rtime_str)));
  246. if (session->pps_thread.ppsout_count) {
  247. char ts_str[TIMESPEC_LEN];
  248. struct timedelta_t timedelta;
  249. /* ugh - de-consting this might get us in trouble someday */
  250. pps_thread_ppsout(&((struct gps_device_t *)session)->pps_thread,
  251. &timedelta);
  252. str_appendf(reply, replylen, "\"pps\":%s,",
  253. timespec_str(&timedelta.clock, ts_str,
  254. sizeof(ts_str)));
  255. /* TODO: add PPS precision to JSON output */
  256. }
  257. str_appendf(reply, replylen,
  258. "\"sor\":%s,\"chars\":%lu,\"sats\":%2d,"
  259. "\"week\":%u,\"tow\":%lld.%03ld,\"rollovers\":%d",
  260. timespec_str(&session->sor, ts_buf, sizeof(ts_buf)),
  261. session->chars,
  262. gpsdata->satellites_used,
  263. session->context->gps_week,
  264. (long long)session->context->gps_tow.tv_sec,
  265. session->context->gps_tow.tv_nsec / 1000000L,
  266. session->context->rollovers);
  267. }
  268. /* at the end because it is new and microjson chokes on new items */
  269. if (0 != isfinite(gpsdata->fix.eph))
  270. str_appendf(reply, replylen, "\"eph\":%.3f,", gpsdata->fix.eph);
  271. if (0 != isfinite(gpsdata->fix.sep))
  272. str_appendf(reply, replylen, "\"sep\":%.3f,", gpsdata->fix.sep);
  273. if ('\0' != gpsdata->fix.datum[0])
  274. str_appendf(reply, replylen, "\"datum\":\"%.40s\",",
  275. gpsdata->fix.datum);
  276. if (0 != isfinite(gpsdata->fix.depth))
  277. str_appendf(reply, replylen,
  278. "\"depth\":%.3f,", gpsdata->fix.depth);
  279. if (0 != isfinite(gpsdata->fix.dgps_age) &&
  280. 0 <= gpsdata->fix.dgps_station) {
  281. /* both, or none */
  282. str_appendf(reply, replylen,
  283. "\"dgpsAge\":%.1f,", gpsdata->fix.dgps_age);
  284. str_appendf(reply, replylen,
  285. "\"dgpsSta\":%d,", gpsdata->fix.dgps_station);
  286. }
  287. }
  288. str_rstrip_char(reply, ',');
  289. (void)strlcat(reply, "}\r\n", replylen);
  290. }
  291. void json_noise_dump(const struct gps_data_t *gpsdata,
  292. char *reply, size_t replylen)
  293. {
  294. assert(replylen > sizeof(char *));
  295. (void)strlcpy(reply, "{\"class\":\"GST\",", replylen);
  296. if (gpsdata->dev.path[0] != '\0')
  297. str_appendf(reply, replylen, "\"device\":\"%s\",", gpsdata->dev.path);
  298. if (0 < gpsdata->gst.utctime.tv_sec) {
  299. char tbuf[JSON_DATE_MAX+1];
  300. str_appendf(reply, replylen,
  301. "\"time\":\"%s\",",
  302. timespec_to_iso8601(gpsdata->gst.utctime,
  303. tbuf, sizeof(tbuf)));
  304. }
  305. #define ADD_GST_FIELD(tag, field) do { \
  306. if (isfinite(gpsdata->gst.field) != 0) \
  307. str_appendf(reply, replylen, "\"" tag "\":%.3f,", gpsdata->gst.field); \
  308. } while(0)
  309. ADD_GST_FIELD("rms", rms_deviation);
  310. ADD_GST_FIELD("major", smajor_deviation);
  311. ADD_GST_FIELD("minor", sminor_deviation);
  312. ADD_GST_FIELD("orient", smajor_orientation);
  313. ADD_GST_FIELD("lat", lat_err_deviation);
  314. ADD_GST_FIELD("lon", lon_err_deviation);
  315. ADD_GST_FIELD("alt", alt_err_deviation);
  316. #undef ADD_GST_FIELD
  317. str_rstrip_char(reply, ',');
  318. (void)strlcat(reply, "}\r\n", replylen);
  319. }
  320. void json_sky_dump(const struct gps_data_t *datap,
  321. char *reply, size_t replylen)
  322. {
  323. int i, reported = 0;
  324. assert(replylen > sizeof(char *));
  325. (void)strlcpy(reply, "{\"class\":\"SKY\",", replylen);
  326. if (datap->dev.path[0] != '\0')
  327. str_appendf(reply, replylen, "\"device\":\"%s\",", datap->dev.path);
  328. if (0 < datap->skyview_time.tv_sec) {
  329. char tbuf[JSON_DATE_MAX+1];
  330. str_appendf(reply, replylen,
  331. "\"time\":\"%s\",",
  332. timespec_to_iso8601(datap->skyview_time,
  333. tbuf, sizeof(tbuf)));
  334. }
  335. if (isfinite(datap->dop.xdop) != 0)
  336. str_appendf(reply, replylen, "\"xdop\":%.2f,", datap->dop.xdop);
  337. if (isfinite(datap->dop.ydop) != 0)
  338. str_appendf(reply, replylen, "\"ydop\":%.2f,", datap->dop.ydop);
  339. if (isfinite(datap->dop.vdop) != 0)
  340. str_appendf(reply, replylen, "\"vdop\":%.2f,", datap->dop.vdop);
  341. if (isfinite(datap->dop.tdop) != 0)
  342. str_appendf(reply, replylen, "\"tdop\":%.2f,", datap->dop.tdop);
  343. if (isfinite(datap->dop.hdop) != 0)
  344. str_appendf(reply, replylen, "\"hdop\":%.2f,", datap->dop.hdop);
  345. if (isfinite(datap->dop.gdop) != 0)
  346. str_appendf(reply, replylen, "\"gdop\":%.2f,", datap->dop.gdop);
  347. if (isfinite(datap->dop.pdop) != 0)
  348. str_appendf(reply, replylen, "\"pdop\":%.2f,", datap->dop.pdop);
  349. /* insurance against flaky drivers */
  350. for (i = 0; i < datap->satellites_visible; i++)
  351. if (datap->skyview[i].PRN)
  352. reported++;
  353. if (reported) {
  354. (void)strlcat(reply, "\"satellites\":[", replylen);
  355. for (i = 0; i < reported; i++) {
  356. if (datap->skyview[i].PRN) {
  357. str_appendf(reply, replylen, "{\"PRN\":%d,",
  358. datap->skyview[i].PRN);
  359. if (0 != isfinite(datap->skyview[i].elevation) &&
  360. 90 >= fabs(datap->skyview[i].elevation)) {
  361. str_appendf(reply, replylen, "\"el\":%.1f,",
  362. datap->skyview[i].elevation);
  363. }
  364. if (0 != isfinite(datap->skyview[i].azimuth) &&
  365. 0 <= fabs(datap->skyview[i].azimuth) &&
  366. 359 >= fabs(datap->skyview[i].azimuth)) {
  367. str_appendf(reply, replylen, "\"az\":%.1f,",
  368. datap->skyview[i].azimuth);
  369. }
  370. if (0 != isfinite(datap->skyview[i].ss)) {
  371. str_appendf(reply, replylen, "\"ss\":%.1f,",
  372. datap->skyview[i].ss);
  373. }
  374. str_appendf(reply, replylen,
  375. "\"used\":%s",
  376. datap->skyview[i].used ? "true" : "false");
  377. if (0 != datap->skyview[i].svid) {
  378. str_appendf(reply, replylen,
  379. ",\"gnssid\":%d,\"svid\":%d",
  380. datap->skyview[i].gnssid,
  381. datap->skyview[i].svid);
  382. }
  383. if (0 != datap->skyview[i].sigid) {
  384. str_appendf(reply, replylen,
  385. ",\"sigid\":%d", datap->skyview[i].sigid);
  386. }
  387. if (GNSSID_GLO == datap->skyview[i].gnssid &&
  388. 0 <= datap->skyview[i].freqid &&
  389. 16 >= datap->skyview[i].freqid) {
  390. str_appendf(reply, replylen,
  391. ",\"freqid\":%d", datap->skyview[i].freqid);
  392. }
  393. if (SAT_HEALTH_UNK != datap->skyview[i].health) {
  394. str_appendf(reply, replylen,
  395. ",\"health\":%d", datap->skyview[i].health);
  396. }
  397. (void)strlcat(reply, "},", replylen);
  398. }
  399. }
  400. str_rstrip_char(reply, ',');
  401. (void)strlcat(reply, "]", replylen);
  402. }
  403. str_rstrip_char(reply, ',');
  404. (void)strlcat(reply, "}\r\n", replylen);
  405. }
  406. void json_device_dump(const struct gps_device_t *device,
  407. char *reply, size_t replylen)
  408. {
  409. struct classmap_t *cmp;
  410. char buf1[JSON_VAL_MAX * 2 + 1];
  411. (void)strlcpy(reply, "{\"class\":\"DEVICE\",\"path\":\"", replylen);
  412. (void)strlcat(reply, device->gpsdata.dev.path, replylen);
  413. (void)strlcat(reply, "\",", replylen);
  414. if (device->device_type != NULL) {
  415. (void)strlcat(reply, "\"driver\":\"", replylen);
  416. (void)strlcat(reply, device->device_type->type_name, replylen);
  417. (void)strlcat(reply, "\",", replylen);
  418. }
  419. if (device->subtype[0] != '\0') {
  420. (void)strlcat(reply, "\"subtype\":\"", replylen);
  421. (void)strlcat(reply,
  422. json_stringify(buf1, sizeof(buf1), device->subtype),
  423. replylen);
  424. (void)strlcat(reply, "\",", replylen);
  425. }
  426. if (device->subtype1[0] != '\0') {
  427. (void)strlcat(reply, "\"subtype1\":\"", replylen);
  428. (void)strlcat(reply,
  429. json_stringify(buf1, sizeof(buf1), device->subtype1),
  430. replylen);
  431. (void)strlcat(reply, "\",", replylen);
  432. }
  433. /*
  434. * There's an assumption here: Anything that we type service_sensor is
  435. * a serial device with the usual control parameters.
  436. */
  437. if (0 < device->gpsdata.online.tv_sec) {
  438. /* odd, using online, not activated, time */
  439. str_appendf(reply, replylen, "\"activated\":\"%s\",",
  440. timespec_to_iso8601(device->gpsdata.online,
  441. buf1, sizeof(buf1)));
  442. if (device->observed != 0) {
  443. int mask = 0;
  444. for (cmp = classmap; cmp < classmap + NITEMS(classmap); cmp++)
  445. if ((device->observed & cmp->packetmask) != 0)
  446. mask |= cmp->typemask;
  447. if (mask != 0)
  448. str_appendf(reply, replylen, "\"flags\":%d,", mask);
  449. }
  450. if (device->servicetype == service_sensor) {
  451. /* speed can be 0 if the device is not currently active */
  452. speed_t speed = gpsd_get_speed(device);
  453. if (speed != 0)
  454. str_appendf(reply, replylen,
  455. "\"native\":%d,\"bps\":%d,\"parity\":\"%c\","
  456. "\"stopbits\":%u,\"cycle\":%lld.%02ld,",
  457. device->gpsdata.dev.driver_mode,
  458. (int)speed,
  459. device->gpsdata.dev.parity,
  460. device->gpsdata.dev.stopbits,
  461. (long long)device->gpsdata.dev.cycle.tv_sec,
  462. device->gpsdata.dev.cycle.tv_nsec / 10000000);
  463. #ifdef RECONFIGURE_ENABLE
  464. if (device->device_type != NULL
  465. && device->device_type->rate_switcher != NULL)
  466. str_appendf(reply, replylen,
  467. "\"mincycle\":%lld.%02ld,",
  468. (long long)device->device_type->min_cycle.tv_sec,
  469. device->device_type->min_cycle.tv_nsec /
  470. 10000000);
  471. #endif /* RECONFIGURE_ENABLE */
  472. }
  473. }
  474. str_rstrip_char(reply, ',');
  475. (void)strlcat(reply, "}\r\n", replylen);
  476. }
  477. void json_watch_dump(const struct gps_policy_t *ccp,
  478. char *reply, size_t replylen)
  479. {
  480. (void)snprintf(reply, replylen,
  481. "{\"class\":\"WATCH\",\"enable\":%s,\"json\":%s,"
  482. "\"nmea\":%s,\"raw\":%d,\"scaled\":%s,\"timing\":%s,"
  483. "\"split24\":%s,\"pps\":%s,",
  484. ccp->watcher ? "true" : "false",
  485. ccp->json ? "true" : "false",
  486. ccp->nmea ? "true" : "false",
  487. ccp->raw,
  488. ccp->scaled ? "true" : "false",
  489. ccp->timing ? "true" : "false",
  490. ccp->split24 ? "true" : "false",
  491. ccp->pps ? "true" : "false");
  492. if (ccp->devpath[0] != '\0')
  493. str_appendf(reply, replylen, "\"device\":\"%s\",", ccp->devpath);
  494. str_rstrip_char(reply, ',');
  495. (void)strlcat(reply, "}\r\n", replylen);
  496. }
  497. void json_subframe_dump(const struct gps_data_t *datap,
  498. char buf[], size_t buflen)
  499. {
  500. const struct subframe_t *subframe = &datap->subframe;
  501. const bool scaled = datap->policy.scaled;
  502. (void)snprintf(buf, buflen, "{\"class\":\"SUBFRAME\",\"device\":\"%s\","
  503. "\"tSV\":%u,\"TOW17\":%u,\"frame\":%u,\"scaled\":%s",
  504. datap->dev.path,
  505. (unsigned int)subframe->tSVID,
  506. (unsigned int)subframe->TOW17,
  507. (unsigned int)subframe->subframe_num,
  508. JSON_BOOL(scaled));
  509. if ( 1 == subframe->subframe_num ) {
  510. if (scaled) {
  511. str_appendf(buf, buflen,
  512. ",\"EPHEM1\":{\"WN\":%u,\"IODC\":%u,\"L2\":%u,"
  513. "\"ura\":%u,\"hlth\":%u,\"L2P\":%u,\"Tgd\":%g,"
  514. "\"toc\":%lu,\"af2\":%.4g,\"af1\":%.6e,\"af0\":%.7e}",
  515. (unsigned int)subframe->sub1.WN,
  516. (unsigned int)subframe->sub1.IODC,
  517. (unsigned int)subframe->sub1.l2,
  518. subframe->sub1.ura,
  519. subframe->sub1.hlth,
  520. (unsigned int)subframe->sub1.l2p,
  521. subframe->sub1.d_Tgd,
  522. (unsigned long)subframe->sub1.l_toc,
  523. subframe->sub1.d_af2,
  524. subframe->sub1.d_af1,
  525. subframe->sub1.d_af0);
  526. } else {
  527. str_appendf(buf, buflen,
  528. ",\"EPHEM1\":{\"WN\":%u,\"IODC\":%u,\"L2\":%u,"
  529. "\"ura\":%u,\"hlth\":%u,\"L2P\":%u,\"Tgd\":%d,"
  530. "\"toc\":%u,\"af2\":%ld,\"af1\":%d,\"af0\":%d}",
  531. (unsigned int)subframe->sub1.WN,
  532. (unsigned int)subframe->sub1.IODC,
  533. (unsigned int)subframe->sub1.l2,
  534. subframe->sub1.ura,
  535. subframe->sub1.hlth,
  536. (unsigned int)subframe->sub1.l2p,
  537. (int)subframe->sub1.Tgd,
  538. (unsigned int)subframe->sub1.toc,
  539. (long)subframe->sub1.af2,
  540. (int)subframe->sub1.af1,
  541. (int)subframe->sub1.af0);
  542. }
  543. } else if ( 2 == subframe->subframe_num ) {
  544. if (scaled) {
  545. str_appendf(buf, buflen,
  546. ",\"EPHEM2\":{\"IODE\":%u,\"Crs\":%.6e,\"deltan\":%.6e,"
  547. "\"M0\":%.11e,\"Cuc\":%.6e,\"e\":%f,\"Cus\":%.6e,"
  548. "\"sqrtA\":%.11g,\"toe\":%lu,\"FIT\":%u,\"AODO\":%u}",
  549. (unsigned int)subframe->sub2.IODE,
  550. subframe->sub2.d_Crs,
  551. subframe->sub2.d_deltan,
  552. subframe->sub2.d_M0,
  553. subframe->sub2.d_Cuc,
  554. subframe->sub2.d_eccentricity,
  555. subframe->sub2.d_Cus,
  556. subframe->sub2.d_sqrtA,
  557. (unsigned long)subframe->sub2.l_toe,
  558. (unsigned int)subframe->sub2.fit,
  559. (unsigned int)subframe->sub2.u_AODO);
  560. } else {
  561. str_appendf(buf, buflen,
  562. ",\"EPHEM2\":{\"IODE\":%u,\"Crs\":%d,\"deltan\":%d,"
  563. "\"M0\":%ld,\"Cuc\":%d,\"e\":%ld,\"Cus\":%d,"
  564. "\"sqrtA\":%lu,\"toe\":%lu,\"FIT\":%u,\"AODO\":%u}",
  565. (unsigned int)subframe->sub2.IODE,
  566. (int)subframe->sub2.Crs,
  567. (int)subframe->sub2.deltan,
  568. (long)subframe->sub2.M0,
  569. (int)subframe->sub2.Cuc,
  570. (long)subframe->sub2.e,
  571. (int)subframe->sub2.Cus,
  572. (unsigned long)subframe->sub2.sqrtA,
  573. (unsigned long)subframe->sub2.toe,
  574. (unsigned int)subframe->sub2.fit,
  575. (unsigned int)subframe->sub2.AODO);
  576. }
  577. } else if ( 3 == subframe->subframe_num ) {
  578. if (scaled) {
  579. str_appendf(buf, buflen,
  580. ",\"EPHEM3\":{\"IODE\":%3u,\"IDOT\":%.6g,\"Cic\":%.6e,"
  581. "\"Omega0\":%.11e,\"Cis\":%.7g,\"i0\":%.11e,\"Crc\":%.7g,"
  582. "\"omega\":%.11e,\"Omegad\":%.6e}",
  583. (unsigned int)subframe->sub3.IODE,
  584. subframe->sub3.d_IDOT,
  585. subframe->sub3.d_Cic,
  586. subframe->sub3.d_Omega0,
  587. subframe->sub3.d_Cis,
  588. subframe->sub3.d_i0,
  589. subframe->sub3.d_Crc,
  590. subframe->sub3.d_omega,
  591. subframe->sub3.d_Omegad );
  592. } else {
  593. str_appendf(buf, buflen,
  594. ",\"EPHEM3\":{\"IODE\":%u,\"IDOT\":%u,\"Cic\":%u,"
  595. "\"Omega0\":%ld,\"Cis\":%d,\"i0\":%ld,\"Crc\":%d,"
  596. "\"omega\":%ld,\"Omegad\":%ld}",
  597. (unsigned int)subframe->sub3.IODE,
  598. (unsigned int)subframe->sub3.IDOT,
  599. (unsigned int)subframe->sub3.Cic,
  600. (long int)subframe->sub3.Omega0,
  601. (int)subframe->sub3.Cis,
  602. (long int)subframe->sub3.i0,
  603. (int)subframe->sub3.Crc,
  604. (long int)subframe->sub3.omega,
  605. (long int)subframe->sub3.Omegad );
  606. }
  607. } else if ( subframe->is_almanac ) {
  608. if (scaled) {
  609. str_appendf(buf, buflen,
  610. ",\"ALMANAC\":{\"ID\":%d,\"Health\":%u,"
  611. "\"e\":%g,\"toa\":%lu,"
  612. "\"deltai\":%.10e,\"Omegad\":%.5e,\"sqrtA\":%.10g,"
  613. "\"Omega0\":%.10e,\"omega\":%.10e,\"M0\":%.11e,"
  614. "\"af0\":%.5e,\"af1\":%.5e}",
  615. (int)subframe->sub5.almanac.sv,
  616. (unsigned int)subframe->sub5.almanac.svh,
  617. subframe->sub5.almanac.d_eccentricity,
  618. (unsigned long)subframe->sub5.almanac.l_toa,
  619. subframe->sub5.almanac.d_deltai,
  620. subframe->sub5.almanac.d_Omegad,
  621. subframe->sub5.almanac.d_sqrtA,
  622. subframe->sub5.almanac.d_Omega0,
  623. subframe->sub5.almanac.d_omega,
  624. subframe->sub5.almanac.d_M0,
  625. subframe->sub5.almanac.d_af0,
  626. subframe->sub5.almanac.d_af1);
  627. } else {
  628. str_appendf(buf, buflen,
  629. ",\"ALMANAC\":{\"ID\":%d,\"Health\":%u,"
  630. "\"e\":%u,\"toa\":%u,"
  631. "\"deltai\":%d,\"Omegad\":%d,\"sqrtA\":%lu,"
  632. "\"Omega0\":%ld,\"omega\":%ld,\"M0\":%ld,"
  633. "\"af0\":%d,\"af1\":%d}",
  634. (int)subframe->sub5.almanac.sv,
  635. (unsigned int)subframe->sub5.almanac.svh,
  636. (unsigned int)subframe->sub5.almanac.e,
  637. (unsigned int)subframe->sub5.almanac.toa,
  638. (int)subframe->sub5.almanac.deltai,
  639. (int)subframe->sub5.almanac.Omegad,
  640. (unsigned long)subframe->sub5.almanac.sqrtA,
  641. (long)subframe->sub5.almanac.Omega0,
  642. (long)subframe->sub5.almanac.omega,
  643. (long)subframe->sub5.almanac.M0,
  644. (int)subframe->sub5.almanac.af0,
  645. (int)subframe->sub5.almanac.af1);
  646. }
  647. } else if ( 4 == subframe->subframe_num ) {
  648. str_appendf(buf, buflen,
  649. ",\"pageid\":%u",
  650. (unsigned int)subframe->pageid);
  651. switch (subframe->pageid ) {
  652. case 13:
  653. case 52:
  654. {
  655. int i;
  656. /* decoding of ERD to SV is non trivial and not done yet */
  657. str_appendf(buf, buflen,
  658. ",\"ERD\":{\"ai\":%u,", subframe->sub4_13.ai);
  659. /* 1-index loop to construct json, rather than giant snprintf */
  660. for(i = 1 ; i <= 30; i++){
  661. str_appendf(buf, buflen,
  662. "\"ERD%d\":%d,", i, subframe->sub4_13.ERD[i]);
  663. }
  664. str_rstrip_char(buf, ',');
  665. str_appendf(buf, buflen, "}");
  666. break;
  667. }
  668. case 55:
  669. /* JSON is UTF-8. double quote, backslash and
  670. * control charactores (U+0000 through U+001F).must be
  671. * escaped. */
  672. /* system message can be 24 bytes, JSON can escape all
  673. * chars so up to 24*6 long. */
  674. {
  675. char buf1[25 * 6];
  676. (void)json_stringify(buf1, sizeof(buf1), subframe->sub4_17.str);
  677. str_appendf(buf, buflen,
  678. ",\"system_message\":\"%.144s\"", buf1);
  679. }
  680. break;
  681. case 56:
  682. if (scaled) {
  683. str_appendf(buf, buflen,
  684. ",\"IONO\":{\"a0\":%.5g,\"a1\":%.5g,\"a2\":%.5g,"
  685. "\"a3\":%.5g,\"b0\":%.5g,\"b1\":%.5g,\"b2\":%.5g,"
  686. "\"b3\":%.5g,\"A1\":%.11e,\"A0\":%.11e,\"tot\":%lld,"
  687. "\"WNt\":%u,\"ls\":%d,\"WNlsf\":%u,\"DN\":%u,"
  688. "\"lsf\":%d}",
  689. subframe->sub4_18.d_alpha0,
  690. subframe->sub4_18.d_alpha1,
  691. subframe->sub4_18.d_alpha2,
  692. subframe->sub4_18.d_alpha3,
  693. subframe->sub4_18.d_beta0,
  694. subframe->sub4_18.d_beta1,
  695. subframe->sub4_18.d_beta2,
  696. subframe->sub4_18.d_beta3,
  697. subframe->sub4_18.d_A1,
  698. subframe->sub4_18.d_A0,
  699. (long long)subframe->sub4_18.t_tot,
  700. (unsigned int)subframe->sub4_18.WNt,
  701. (int)subframe->sub4_18.leap,
  702. (unsigned int)subframe->sub4_18.WNlsf,
  703. (unsigned int)subframe->sub4_18.DN,
  704. (int)subframe->sub4_18.lsf);
  705. } else {
  706. str_appendf(buf, buflen,
  707. ",\"IONO\":{\"a0\":%d,\"a1\":%d,\"a2\":%d,\"a3\":%d,"
  708. "\"b0\":%d,\"b1\":%d,\"b2\":%d,\"b3\":%d,"
  709. "\"A1\":%ld,\"A0\":%ld,\"tot\":%u,\"WNt\":%u,"
  710. "\"ls\":%d,\"WNlsf\":%u,\"DN\":%u,\"lsf\":%d}",
  711. (int)subframe->sub4_18.alpha0,
  712. (int)subframe->sub4_18.alpha1,
  713. (int)subframe->sub4_18.alpha2,
  714. (int)subframe->sub4_18.alpha3,
  715. (int)subframe->sub4_18.beta0,
  716. (int)subframe->sub4_18.beta1,
  717. (int)subframe->sub4_18.beta2,
  718. (int)subframe->sub4_18.beta3,
  719. (long)subframe->sub4_18.A1,
  720. (long)subframe->sub4_18.A0,
  721. (unsigned int)subframe->sub4_18.tot,
  722. (unsigned int)subframe->sub4_18.WNt,
  723. (int)subframe->sub4_18.leap,
  724. (unsigned int)subframe->sub4_18.WNlsf,
  725. (unsigned int)subframe->sub4_18.DN,
  726. (int)subframe->sub4_18.lsf);
  727. }
  728. break;
  729. case 25:
  730. case 63:
  731. {
  732. int i;
  733. str_appendf(buf, buflen,
  734. ",\"HEALTH\":{\"data_id\":%d,",
  735. (int)subframe->data_id);
  736. /* 1-index loop to construct json, rather than giant snprintf */
  737. for(i = 1 ; i <= 32; i++){
  738. str_appendf(buf, buflen,
  739. "\"SV%d\":%d,",
  740. i, (int)subframe->sub4_25.svf[i]);
  741. }
  742. for(i = 0 ; i < 8; i++){ /* 0-index */
  743. str_appendf(buf, buflen,
  744. "\"SVH%d\":%d,",
  745. i+25, (int)subframe->sub4_25.svhx[i]);
  746. }
  747. str_rstrip_char(buf, ',');
  748. str_appendf(buf, buflen, "}");
  749. break;
  750. }
  751. }
  752. } else if ( 5 == subframe->subframe_num ) {
  753. str_appendf(buf, buflen,
  754. ",\"pageid\":%u",
  755. (unsigned int)subframe->pageid);
  756. if ( 51 == subframe->pageid ) {
  757. int i;
  758. /* subframe5, page 25 */
  759. str_appendf(buf, buflen,
  760. ",\"HEALTH2\":{\"toa\":%lu,\"WNa\":%u,",
  761. (unsigned long)subframe->sub5_25.l_toa,
  762. (unsigned int)subframe->sub5_25.WNa);
  763. /* 1-index loop to construct json */
  764. for(i = 1 ; i <= 24; i++){
  765. str_appendf(buf, buflen,
  766. "\"SV%d\":%d,",
  767. i, (int)subframe->sub5_25.sv[i]);
  768. }
  769. str_rstrip_char(buf, ',');
  770. str_appendf(buf, buflen, "}");
  771. }
  772. }
  773. (void)strlcat(buf, "}\r\n", buflen);
  774. }
  775. /* RAW dump - should be good enough to make a RINEX 3 file */
  776. void json_raw_dump(const struct gps_data_t *gpsdata,
  777. char *reply, size_t replylen)
  778. {
  779. int i;
  780. assert(replylen > sizeof(char *));
  781. if (0 == gpsdata->raw.mtime.tv_sec) {
  782. /* no data to dump */
  783. return;
  784. }
  785. (void)strlcpy(reply, "{\"class\":\"RAW\",", replylen);
  786. if (gpsdata->dev.path[0] != '\0')
  787. str_appendf(reply, replylen, "\"device\":\"%s\",", gpsdata->dev.path);
  788. str_appendf(reply, replylen, "\"time\":%lld,\"nsec\":%ld,\"rawdata\":[",
  789. (long long)gpsdata->raw.mtime.tv_sec,
  790. gpsdata->raw.mtime.tv_nsec);
  791. for (i = 0; i < MAXCHANNELS; i++) {
  792. bool comma = false;
  793. if (0 == gpsdata->raw.meas[i].svid ||
  794. 255 == gpsdata->raw.meas[i].svid) {
  795. /* skip empty and GLONASS 255 */
  796. continue;
  797. }
  798. str_appendf(reply, replylen,
  799. "{\"gnssid\":%u,\"svid\":%u,\"snr\":%u,"
  800. "\"obs\":\"%s\",\"lli\":%1u,\"locktime\":%u",
  801. gpsdata->raw.meas[i].gnssid, gpsdata->raw.meas[i].svid,
  802. gpsdata->raw.meas[i].snr,
  803. gpsdata->raw.meas[i].obs_code, gpsdata->raw.meas[i].lli,
  804. gpsdata->raw.meas[i].locktime);
  805. if (0 < gpsdata->raw.meas[i].sigid) {
  806. str_appendf(reply, replylen, ",\"sigid\":%u",
  807. gpsdata->raw.meas[i].sigid);
  808. }
  809. if (GNSSID_GLO == gpsdata->raw.meas[i].gnssid) {
  810. str_appendf(reply, replylen, ",\"freqid\":%u",
  811. gpsdata->raw.meas[i].freqid);
  812. }
  813. comma = true;
  814. if (0 != isfinite(gpsdata->raw.meas[i].pseudorange) &&
  815. 1.0 < gpsdata->raw.meas[i].pseudorange) {
  816. if (comma)
  817. (void)strlcat(reply, ",", replylen);
  818. str_appendf(reply, replylen, "\"pseudorange\":%f",
  819. gpsdata->raw.meas[i].pseudorange);
  820. comma = true;
  821. if (0 != isfinite(gpsdata->raw.meas[i].carrierphase)) {
  822. str_appendf(reply, replylen, ",\"carrierphase\":%f",
  823. gpsdata->raw.meas[i].carrierphase);
  824. comma = true;
  825. }
  826. }
  827. if (0 != isfinite(gpsdata->raw.meas[i].doppler)) {
  828. if (comma)
  829. (void)strlcat(reply, ",", replylen);
  830. str_appendf(reply, replylen, "\"doppler\":%f",
  831. gpsdata->raw.meas[i].doppler);
  832. comma = true;
  833. }
  834. /* L2 C/A pseudo range, RINEX C2C */
  835. if (0 != isfinite(gpsdata->raw.meas[i].c2c) &&
  836. 1.0 < gpsdata->raw.meas[i].c2c) {
  837. if (comma)
  838. (void)strlcat(reply, ",", replylen);
  839. str_appendf(reply, replylen, "\"c2c\":%f",
  840. gpsdata->raw.meas[i].c2c);
  841. comma = true;
  842. /* L2 C/A carrier phase, RINEX L2C */
  843. if (0 != isfinite(gpsdata->raw.meas[i].l2c)) {
  844. if (comma)
  845. (void)strlcat(reply, ",", replylen);
  846. str_appendf(reply, replylen, "\"l2c\":%f",
  847. gpsdata->raw.meas[i].l2c);
  848. comma = true;
  849. }
  850. }
  851. (void)strlcat(reply, "},", replylen);
  852. }
  853. str_rstrip_char(reply, ',');
  854. (void)strlcat(reply, "]", replylen);
  855. str_rstrip_char(reply, ',');
  856. (void)strlcat(reply, "}\r\n", replylen);
  857. }
  858. #if defined(RTCM104V2_ENABLE)
  859. void json_rtcm2_dump(const struct rtcm2_t *rtcm,
  860. const char *device,
  861. char buf[], size_t buflen)
  862. /* dump the contents of a parsed RTCM104 message as JSON */
  863. {
  864. char buf1[JSON_VAL_MAX * 2 + 1];
  865. unsigned int n;
  866. (void)snprintf(buf, buflen, "{\"class\":\"RTCM2\",");
  867. if (device != NULL && device[0] != '\0')
  868. str_appendf(buf, buflen, "\"device\":\"%s\",", device);
  869. str_appendf(buf, buflen,
  870. "\"type\":%u,\"station_id\":%u,\"zcount\":%0.1f,"
  871. "\"seqnum\":%u,\"length\":%u,\"station_health\":%u,",
  872. rtcm->type, rtcm->refstaid, rtcm->zcount, rtcm->seqnum,
  873. rtcm->length, rtcm->stathlth);
  874. switch (rtcm->type) {
  875. case 1:
  876. case 9:
  877. (void)strlcat(buf, "\"satellites\":[", buflen);
  878. for (n = 0; n < rtcm->gps_ranges.nentries; n++) {
  879. const struct gps_rangesat_t *rsp = &rtcm->gps_ranges.sat[n];
  880. str_appendf(buf, buflen,
  881. "{\"ident\":%u,\"udre\":%u,\"iod\":%u,"
  882. "\"prc\":%0.3f,\"rrc\":%0.3f},",
  883. rsp->ident,
  884. rsp->udre, rsp->iod,
  885. rsp->prc, rsp->rrc);
  886. }
  887. str_rstrip_char(buf, ',');
  888. (void)strlcat(buf, "]", buflen);
  889. break;
  890. case 3:
  891. if (rtcm->ecef.valid)
  892. str_appendf(buf, buflen,
  893. "\"x\":%.2f,\"y\":%.2f,\"z\":%.2f,",
  894. rtcm->ecef.x, rtcm->ecef.y, rtcm->ecef.z);
  895. break;
  896. case 4:
  897. if (rtcm->reference.valid) {
  898. /*
  899. * Beware! Needs to stay synchronized with a JSON
  900. * enumeration map in the parser. This interpretation of
  901. * NAVSYSTEM_GALILEO is assumed from RTCM3, it's not
  902. * actually documented in RTCM 2.1.
  903. */
  904. static char *navsysnames[] = { "GPS", "GLONASS", "GALILEO" };
  905. str_appendf(buf, buflen,
  906. "\"system\":\"%s\",\"sense\":%1d,"
  907. "\"datum\":\"%s\",\"dx\":%.1f,\"dy\":%.1f,"
  908. "\"dz\":%.1f,",
  909. rtcm->reference.system >= NITEMS(navsysnames)
  910. ? "UNKNOWN"
  911. : navsysnames[rtcm->reference.system],
  912. rtcm->reference.sense,
  913. rtcm->reference.datum,
  914. rtcm->reference.dx,
  915. rtcm->reference.dy, rtcm->reference.dz);
  916. }
  917. break;
  918. case 5:
  919. (void)strlcat(buf, "\"satellites\":[", buflen);
  920. for (n = 0; n < rtcm->conhealth.nentries; n++) {
  921. const struct consat_t *csp = &rtcm->conhealth.sat[n];
  922. str_appendf(buf, buflen,
  923. "{\"ident\":%u,\"iodl\":%s,\"health\":%1u,"
  924. "\"snr\":%d,\"health_en\":%s,\"new_data\":%s,"
  925. "\"los_warning\":%s,\"tou\":%u},",
  926. csp->ident,
  927. JSON_BOOL(csp->iodl),
  928. (unsigned)csp->health,
  929. csp->snr,
  930. JSON_BOOL(csp->health_en),
  931. JSON_BOOL(csp->new_data),
  932. JSON_BOOL(csp->los_warning), csp->tou);
  933. }
  934. str_rstrip_char(buf, ',');
  935. (void)strlcat(buf, "]", buflen);
  936. break;
  937. case 6: /* NOP msg */
  938. break;
  939. case 7:
  940. (void)strlcat(buf, "\"satellites\":[", buflen);
  941. for (n = 0; n < rtcm->almanac.nentries; n++) {
  942. const struct station_t *ssp = &rtcm->almanac.station[n];
  943. str_appendf(buf, buflen,
  944. "{\"lat\":%.4f,\"lon\":%.4f,\"range\":%u,"
  945. "\"frequency\":%.1f,\"health\":%u,"
  946. "\"station_id\":%u,\"bitrate\":%u},",
  947. ssp->latitude,
  948. ssp->longitude,
  949. ssp->range,
  950. ssp->frequency,
  951. ssp->health, ssp->station_id, ssp->bitrate);
  952. }
  953. str_rstrip_char(buf, ',');
  954. (void)strlcat(buf, "]", buflen);
  955. break;
  956. case 13:
  957. str_appendf(buf, buflen,
  958. "\"status\":%s,\"rangeflag\":%s,"
  959. "\"lat\":%.2f,\"lon\":%.2f,\"range\":%u,",
  960. JSON_BOOL(rtcm->xmitter.status),
  961. JSON_BOOL(rtcm->xmitter.rangeflag),
  962. rtcm->xmitter.lat,
  963. rtcm->xmitter.lon,
  964. rtcm->xmitter.range);
  965. break;
  966. case 14:
  967. str_appendf(buf, buflen,
  968. "\"week\":%u,\"hour\":%u,\"leapsecs\":%u,",
  969. rtcm->gpstime.week,
  970. rtcm->gpstime.hour,
  971. rtcm->gpstime.leapsecs);
  972. break;
  973. case 16:
  974. str_appendf(buf, buflen,
  975. "\"message\":\"%s\"", json_stringify(buf1,
  976. sizeof(buf1),
  977. rtcm->message));
  978. break;
  979. case 31:
  980. (void)strlcat(buf, "\"satellites\":[", buflen);
  981. for (n = 0; n < rtcm->glonass_ranges.nentries; n++) {
  982. const struct glonass_rangesat_t *rsp = &rtcm->glonass_ranges.sat[n];
  983. str_appendf(buf, buflen,
  984. "{\"ident\":%u,\"udre\":%u,\"change\":%s,"
  985. "\"tod\":%u,\"prc\":%0.3f,\"rrc\":%0.3f},",
  986. rsp->ident,
  987. rsp->udre,
  988. JSON_BOOL(rsp->change),
  989. rsp->tod,
  990. rsp->prc, rsp->rrc);
  991. }
  992. str_rstrip_char(buf, ',');
  993. (void)strlcat(buf, "]", buflen);
  994. break;
  995. default:
  996. (void)strlcat(buf, "\"data\":[", buflen);
  997. for (n = 0; n < rtcm->length; n++)
  998. str_appendf(buf, buflen, "\"0x%08x\",", rtcm->words[n]);
  999. str_rstrip_char(buf, ',');
  1000. (void)strlcat(buf, "]", buflen);
  1001. break;
  1002. }
  1003. str_rstrip_char(buf, ',');
  1004. (void)strlcat(buf, "}\r\n", buflen);
  1005. }
  1006. #endif /* defined(RTCM104V2_ENABLE) */
  1007. #if defined(RTCM104V3_ENABLE)
  1008. void json_rtcm3_dump(const struct rtcm3_t *rtcm,
  1009. const char *device,
  1010. char buf[], size_t buflen)
  1011. /* dump the contents of a parsed RTCM104v3 message as JSON */
  1012. {
  1013. char buf1[JSON_VAL_MAX * 2 + 1];
  1014. unsigned short i;
  1015. unsigned int n;
  1016. (void)snprintf(buf, buflen, "{\"class\":\"RTCM3\",");
  1017. if (device != NULL && device[0] != '\0')
  1018. str_appendf(buf, buflen, "\"device\":\"%s\",", device);
  1019. str_appendf(buf, buflen, "\"type\":%u,", rtcm->type);
  1020. str_appendf(buf, buflen, "\"length\":%u,", rtcm->length);
  1021. #define CODE(x) (unsigned int)(x)
  1022. #define INT(x) (unsigned int)(x)
  1023. switch (rtcm->type) {
  1024. case 1001:
  1025. str_appendf(buf, buflen,
  1026. "\"station_id\":%u,\"tow\":%d,\"sync\":\"%s\","
  1027. "\"smoothing\":\"%s\",\"interval\":\"%u\",",
  1028. rtcm->rtcmtypes.rtcm3_1001.header.station_id,
  1029. (int)rtcm->rtcmtypes.rtcm3_1001.header.tow,
  1030. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1001.header.sync),
  1031. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1001.header.smoothing),
  1032. rtcm->rtcmtypes.rtcm3_1001.header.interval);
  1033. (void)strlcat(buf, "\"satellites\":[", buflen);
  1034. for (i = 0; i < rtcm->rtcmtypes.rtcm3_1001.header.satcount; i++) {
  1035. #define R1001 rtcm->rtcmtypes.rtcm3_1001.rtk_data[i]
  1036. str_appendf(buf, buflen,
  1037. "{\"ident\":%u,\"ind\":%u,\"prange\":%8.2f,"
  1038. "\"delta\":%6.4f,\"lockt\":%u},",
  1039. R1001.ident,
  1040. CODE(R1001.L1.indicator),
  1041. R1001.L1.pseudorange,
  1042. R1001.L1.rangediff,
  1043. INT(R1001.L1.locktime));
  1044. #undef R1001
  1045. }
  1046. str_rstrip_char(buf, ',');
  1047. (void)strlcat(buf, "]", buflen);
  1048. break;
  1049. case 1002:
  1050. str_appendf(buf, buflen,
  1051. "\"station_id\":%u,\"tow\":%d,\"sync\":\"%s\","
  1052. "\"smoothing\":\"%s\",\"interval\":\"%u\",",
  1053. rtcm->rtcmtypes.rtcm3_1002.header.station_id,
  1054. (int)rtcm->rtcmtypes.rtcm3_1002.header.tow,
  1055. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1002.header.sync),
  1056. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1002.header.smoothing),
  1057. rtcm->rtcmtypes.rtcm3_1002.header.interval);
  1058. (void)strlcat(buf, "\"satellites\":[", buflen);
  1059. for (i = 0; i < rtcm->rtcmtypes.rtcm3_1002.header.satcount; i++) {
  1060. #define R1002 rtcm->rtcmtypes.rtcm3_1002.rtk_data[i]
  1061. str_appendf(buf, buflen,
  1062. "{\"ident\":%u,\"ind\":%u,\"prange\":%8.2f,"
  1063. "\"delta\":%6.4f,\"lockt\":%u,\"amb\":%u,"
  1064. "\"CNR\":%.2f},",
  1065. R1002.ident,
  1066. CODE(R1002.L1.indicator),
  1067. R1002.L1.pseudorange,
  1068. R1002.L1.rangediff,
  1069. INT(R1002.L1.locktime),
  1070. INT(R1002.L1.ambiguity),
  1071. R1002.L1.CNR);
  1072. #undef R1002
  1073. }
  1074. str_rstrip_char(buf, ',');
  1075. (void)strlcat(buf, "]", buflen);
  1076. break;
  1077. case 1003:
  1078. str_appendf(buf, buflen,
  1079. "\"station_id\":%u,\"tow\":%d,\"sync\":\"%s\","
  1080. "\"smoothing\":\"%s\",\"interval\":\"%u\",",
  1081. rtcm->rtcmtypes.rtcm3_1003.header.station_id,
  1082. (int)rtcm->rtcmtypes.rtcm3_1003.header.tow,
  1083. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1003.header.sync),
  1084. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1003.header.smoothing),
  1085. rtcm->rtcmtypes.rtcm3_1003.header.interval);
  1086. (void)strlcat(buf, "\"satellites\":[", buflen);
  1087. for (i = 0; i < rtcm->rtcmtypes.rtcm3_1003.header.satcount; i++) {
  1088. #define R1003 rtcm->rtcmtypes.rtcm3_1003.rtk_data[i]
  1089. str_appendf(buf, buflen,
  1090. "{\"ident\":%u,"
  1091. "\"L1\":{\"ind\":%u,\"prange\":%8.2f,"
  1092. "\"delta\":%6.4f,\"lockt\":%u},"
  1093. "\"L2\":{\"ind\":%u,\"prange\":%8.2f,"
  1094. "\"delta\":%6.4f,\"lockt\":%u},"
  1095. "},",
  1096. R1003.ident,
  1097. CODE(R1003.L1.indicator),
  1098. R1003.L1.pseudorange,
  1099. R1003.L1.rangediff,
  1100. INT(R1003.L1.locktime),
  1101. CODE(R1003.L2.indicator),
  1102. R1003.L2.pseudorange,
  1103. R1003.L2.rangediff,
  1104. INT(R1003.L2.locktime));
  1105. #undef R1003
  1106. }
  1107. str_rstrip_char(buf, ',');
  1108. (void)strlcat(buf, "]", buflen);
  1109. break;
  1110. case 1004:
  1111. str_appendf(buf, buflen,
  1112. "\"station_id\":%u,\"tow\":%d,\"sync\":\"%s\","
  1113. "\"smoothing\":\"%s\",\"interval\":\"%u\",",
  1114. rtcm->rtcmtypes.rtcm3_1004.header.station_id,
  1115. (int)rtcm->rtcmtypes.rtcm3_1004.header.tow,
  1116. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1004.header.sync),
  1117. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1004.header.smoothing),
  1118. rtcm->rtcmtypes.rtcm3_1004.header.interval);
  1119. (void)strlcat(buf, "\"satellites\":[", buflen);
  1120. for (i = 0; i < rtcm->rtcmtypes.rtcm3_1004.header.satcount; i++) {
  1121. #define R1004 rtcm->rtcmtypes.rtcm3_1004.rtk_data[i]
  1122. str_appendf(buf, buflen,
  1123. "{\"ident\":%u,"
  1124. "\"L1\":{\"ind\":%u,\"prange\":%8.2f,"
  1125. "\"delta\":%6.4f,\"lockt\":%u,"
  1126. "\"amb\":%u,\"CNR\":%.2f},"
  1127. "\"L2\":{\"ind\":%u,\"prange\":%8.2f,"
  1128. "\"delta\":%6.4f,\"lockt\":%u,"
  1129. "\"CNR\":%.2f}"
  1130. "},",
  1131. R1004.ident,
  1132. CODE(R1004.L1.indicator),
  1133. R1004.L1.pseudorange,
  1134. R1004.L1.rangediff,
  1135. INT(R1004.L1.locktime),
  1136. INT(R1004.L1.ambiguity),
  1137. R1004.L1.CNR,
  1138. CODE(R1004.L2.indicator),
  1139. R1004.L2.pseudorange,
  1140. R1004.L2.rangediff,
  1141. INT(R1004.L2.locktime),
  1142. R1004.L2.CNR);
  1143. #undef R1004
  1144. }
  1145. str_rstrip_char(buf, ',');
  1146. (void)strlcat(buf, "]", buflen);
  1147. break;
  1148. case 1005:
  1149. str_appendf(buf, buflen,
  1150. "\"station_id\":%u,\"system\":[",
  1151. rtcm->rtcmtypes.rtcm3_1005.station_id);
  1152. if ((rtcm->rtcmtypes.rtcm3_1005.system & 0x04)!=0)
  1153. (void)strlcat(buf, "\"GPS\",", buflen);
  1154. if ((rtcm->rtcmtypes.rtcm3_1005.system & 0x02)!=0)
  1155. (void)strlcat(buf, "\"GLONASS\",", buflen);
  1156. if ((rtcm->rtcmtypes.rtcm3_1005.system & 0x01)!=0)
  1157. (void)strlcat(buf, "\"GALILEO\",", buflen);
  1158. str_rstrip_char(buf, ',');
  1159. str_appendf(buf, buflen,
  1160. "],\"refstation\":%s,\"sro\":%s,"
  1161. "\"x\":%.4f,\"y\":%.4f,\"z\":%.4f,",
  1162. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1005.reference_station),
  1163. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1005.single_receiver),
  1164. rtcm->rtcmtypes.rtcm3_1005.ecef_x,
  1165. rtcm->rtcmtypes.rtcm3_1005.ecef_y,
  1166. rtcm->rtcmtypes.rtcm3_1005.ecef_z);
  1167. break;
  1168. case 1006:
  1169. str_appendf(buf, buflen,
  1170. "\"station_id\":%u,\"system\":[",
  1171. rtcm->rtcmtypes.rtcm3_1006.station_id);
  1172. if ((rtcm->rtcmtypes.rtcm3_1006.system & 0x04)!=0)
  1173. (void)strlcat(buf, "\"GPS\",", buflen);
  1174. if ((rtcm->rtcmtypes.rtcm3_1006.system & 0x02)!=0)
  1175. (void)strlcat(buf, "\"GLONASS\",", buflen);
  1176. if ((rtcm->rtcmtypes.rtcm3_1006.system & 0x01)!=0)
  1177. (void)strlcat(buf, "\"GALILEO\",", buflen);
  1178. str_rstrip_char(buf, ',');
  1179. str_appendf(buf, buflen,
  1180. "],\"refstation\":%s,\"sro\":%s,"
  1181. "\"x\":%.4f,\"y\":%.4f,\"z\":%.4f,",
  1182. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1006.reference_station),
  1183. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1006.single_receiver),
  1184. rtcm->rtcmtypes.rtcm3_1006.ecef_x,
  1185. rtcm->rtcmtypes.rtcm3_1006.ecef_y,
  1186. rtcm->rtcmtypes.rtcm3_1006.ecef_z);
  1187. str_appendf(buf, buflen,
  1188. "\"h\":%.4f,",
  1189. rtcm->rtcmtypes.rtcm3_1006.height);
  1190. break;
  1191. case 1007:
  1192. str_appendf(buf, buflen,
  1193. "\"station_id\":%u,\"desc\":\"%s\",\"setup_id\":%u",
  1194. rtcm->rtcmtypes.rtcm3_1007.station_id,
  1195. rtcm->rtcmtypes.rtcm3_1007.descriptor,
  1196. rtcm->rtcmtypes.rtcm3_1007.setup_id);
  1197. break;
  1198. case 1008:
  1199. str_appendf(buf, buflen,
  1200. "\"station_id\":%u,\"desc\":\"%s\","
  1201. "\"setup_id\":%u,\"serial\":\"%s\"",
  1202. rtcm->rtcmtypes.rtcm3_1008.station_id,
  1203. rtcm->rtcmtypes.rtcm3_1008.descriptor,
  1204. INT(rtcm->rtcmtypes.rtcm3_1008.setup_id),
  1205. rtcm->rtcmtypes.rtcm3_1008.serial);
  1206. break;
  1207. case 1009:
  1208. str_appendf(buf, buflen,
  1209. "\"station_id\":%u,\"tow\":%lld,\"sync\":\"%s\","
  1210. "\"smoothing\":\"%s\",\"interval\":\"%u\","
  1211. "\"satcount\":\"%u\",",
  1212. rtcm->rtcmtypes.rtcm3_1009.header.station_id,
  1213. (long long)rtcm->rtcmtypes.rtcm3_1009.header.tow,
  1214. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1009.header.sync),
  1215. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1009.header.smoothing),
  1216. rtcm->rtcmtypes.rtcm3_1009.header.interval,
  1217. rtcm->rtcmtypes.rtcm3_1009.header.satcount);
  1218. (void)strlcat(buf, "\"satellites\":[", buflen);
  1219. for (i = 0; i < rtcm->rtcmtypes.rtcm3_1009.header.satcount; i++) {
  1220. #define R1009 rtcm->rtcmtypes.rtcm3_1009.rtk_data[i]
  1221. str_appendf(buf, buflen,
  1222. "{\"ident\":%u,\"ind\":%u,\"channel\":%u,"
  1223. "\"prange\":%8.2f,\"delta\":%6.4f,\"lockt\":%u},",
  1224. R1009.ident,
  1225. CODE(R1009.L1.indicator),
  1226. R1009.L1.channel,
  1227. R1009.L1.pseudorange,
  1228. R1009.L1.rangediff,
  1229. INT(R1009.L1.locktime));
  1230. #undef R1009
  1231. }
  1232. str_rstrip_char(buf, ',');
  1233. (void)strlcat(buf, "]", buflen);
  1234. break;
  1235. case 1010:
  1236. str_appendf(buf, buflen,
  1237. "\"station_id\":%u,\"tow\":%d,\"sync\":\"%s\","
  1238. "\"smoothing\":\"%s\",\"interval\":\"%u\",",
  1239. rtcm->rtcmtypes.rtcm3_1010.header.station_id,
  1240. (int)rtcm->rtcmtypes.rtcm3_1010.header.tow,
  1241. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1010.header.sync),
  1242. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1010.header.smoothing),
  1243. rtcm->rtcmtypes.rtcm3_1010.header.interval);
  1244. (void)strlcat(buf, "\"satellites\":[", buflen);
  1245. for (i = 0; i < rtcm->rtcmtypes.rtcm3_1010.header.satcount; i++) {
  1246. #define R1010 rtcm->rtcmtypes.rtcm3_1010.rtk_data[i]
  1247. str_appendf(buf, buflen,
  1248. "{\"ident\":%u,\"ind\":%u,\"channel\":%u,"
  1249. "\"prange\":%8.2f,\"delta\":%6.4f,\"lockt\":%u,"
  1250. "\"amb\":%u,\"CNR\":%.2f},",
  1251. R1010.ident,
  1252. CODE(R1010.L1.indicator),
  1253. R1010.L1.channel,
  1254. R1010.L1.pseudorange,
  1255. R1010.L1.rangediff,
  1256. INT(R1010.L1.locktime),
  1257. INT(R1010.L1.ambiguity),
  1258. R1010.L1.CNR);
  1259. #undef R1010
  1260. }
  1261. str_rstrip_char(buf, ',');
  1262. (void)strlcat(buf, "]", buflen);
  1263. break;
  1264. case 1011:
  1265. str_appendf(buf, buflen,
  1266. "\"station_id\":%u,\"tow\":%d,\"sync\":\"%s\","
  1267. "\"smoothing\":\"%s\",\"interval\":\"%u\",",
  1268. rtcm->rtcmtypes.rtcm3_1011.header.station_id,
  1269. (int)rtcm->rtcmtypes.rtcm3_1011.header.tow,
  1270. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1011.header.sync),
  1271. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1011.header.smoothing),
  1272. rtcm->rtcmtypes.rtcm3_1011.header.interval);
  1273. (void)strlcat(buf, "\"satellites\":[", buflen);
  1274. for (i = 0; i < rtcm->rtcmtypes.rtcm3_1011.header.satcount; i++) {
  1275. #define R1011 rtcm->rtcmtypes.rtcm3_1011.rtk_data[i]
  1276. str_appendf(buf, buflen,
  1277. "{\"ident\":%u,\"channel\":%u,"
  1278. "\"L1\":{\"ind\":%u,"
  1279. "\"prange\":%8.2f,\"delta\":%6.4f,\"lockt\":%u},"
  1280. "\"L2:{\"ind\":%u,\"prange\":%8.2f,"
  1281. "\"delta\":%6.4f,\"lockt\":%u}"
  1282. "}",
  1283. R1011.ident,R1011.L1.channel,
  1284. CODE(R1011.L1.indicator),
  1285. R1011.L1.pseudorange,
  1286. R1011.L1.rangediff,
  1287. INT(R1011.L1.locktime),
  1288. CODE(R1011.L2.indicator),
  1289. R1011.L2.pseudorange,
  1290. R1011.L2.rangediff,
  1291. INT(R1011.L2.locktime));
  1292. #undef R1011
  1293. }
  1294. str_rstrip_char(buf, ',');
  1295. (void)strlcat(buf, "]", buflen);
  1296. break;
  1297. case 1012:
  1298. str_appendf(buf, buflen,
  1299. "\"station_id\":%u,\"tow\":%d,\"sync\":\"%s\","
  1300. "\"smoothing\":\"%s\",\"interval\":\"%u\",",
  1301. rtcm->rtcmtypes.rtcm3_1012.header.station_id,
  1302. (int)rtcm->rtcmtypes.rtcm3_1012.header.tow,
  1303. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1012.header.sync),
  1304. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1012.header.smoothing),
  1305. rtcm->rtcmtypes.rtcm3_1012.header.interval);
  1306. (void)strlcat(buf, "\"satellites\":[", buflen);
  1307. for (i = 0; i < rtcm->rtcmtypes.rtcm3_1012.header.satcount; i++) {
  1308. #define R1012 rtcm->rtcmtypes.rtcm3_1012.rtk_data[i]
  1309. str_appendf(buf, buflen,
  1310. "{\"ident\":%u,\"channel\":%u,"
  1311. "\"L1\":{\"ind\":%u,\"prange\":%8.2f,"
  1312. "\"delta\":%6.4f,\"lockt\":%u,\"amb\":%u,"
  1313. "\"CNR\":%.2f},"
  1314. "\"L2\":{\"ind\":%u,\"prange\":%8.2f,"
  1315. "\"delta\":%6.4f,\"lockt\":%u,"
  1316. "\"CNR\":%.2f},"
  1317. "},",
  1318. R1012.ident,
  1319. R1012.L1.channel,
  1320. CODE(R1012.L1.indicator),
  1321. R1012.L1.pseudorange,
  1322. R1012.L1.rangediff,
  1323. INT(R1012.L1.locktime),
  1324. INT(R1012.L1.ambiguity),
  1325. R1012.L1.CNR,
  1326. CODE(R1012.L2.indicator),
  1327. R1012.L2.pseudorange,
  1328. R1012.L2.rangediff,
  1329. INT(R1012.L2.locktime),
  1330. R1012.L2.CNR);
  1331. #undef R1012
  1332. }
  1333. str_rstrip_char(buf, ',');
  1334. (void)strlcat(buf, "]", buflen);
  1335. break;
  1336. case 1013:
  1337. str_appendf(buf, buflen,
  1338. "\"station_id\":%u,\"mjd\":%u,\"sec\":%u,"
  1339. "\"leapsecs\":%u,",
  1340. rtcm->rtcmtypes.rtcm3_1013.station_id,
  1341. rtcm->rtcmtypes.rtcm3_1013.mjd,
  1342. rtcm->rtcmtypes.rtcm3_1013.sod,
  1343. INT(rtcm->rtcmtypes.rtcm3_1013.leapsecs));
  1344. for (i = 0; i < (unsigned short)rtcm->rtcmtypes.rtcm3_1013.ncount; i++)
  1345. str_appendf(buf, buflen,
  1346. "{\"id\":%u,\"sync\":\"%s\",\"interval\":%u}",
  1347. rtcm->rtcmtypes.rtcm3_1013.announcements[i].id,
  1348. JSON_BOOL(rtcm->rtcmtypes.rtcm3_1013.
  1349. announcements[i].sync),
  1350. rtcm->rtcmtypes.rtcm3_1013.
  1351. announcements[i].interval);
  1352. break;
  1353. case 1014:
  1354. str_appendf(buf, buflen,
  1355. "\"netid\":%u,\"subnetid\":%u,\"statcount\":%u"
  1356. "\"master\":%u,\"aux\":%u,\"lat\":%f,\"lon\":%f,"
  1357. "\"alt\":%f,",
  1358. rtcm->rtcmtypes.rtcm3_1014.network_id,
  1359. rtcm->rtcmtypes.rtcm3_1014.subnetwork_id,
  1360. rtcm->rtcmtypes.rtcm3_1014.stationcount,
  1361. rtcm->rtcmtypes.rtcm3_1014.master_id,
  1362. rtcm->rtcmtypes.rtcm3_1014.aux_id,
  1363. rtcm->rtcmtypes.rtcm3_1014.d_lat,
  1364. rtcm->rtcmtypes.rtcm3_1014.d_lon,
  1365. rtcm->rtcmtypes.rtcm3_1014.d_alt);
  1366. break;
  1367. case 1015:
  1368. break;
  1369. case 1016:
  1370. break;
  1371. case 1017:
  1372. break;
  1373. case 1018:
  1374. break;
  1375. case 1019:
  1376. break;
  1377. case 1020:
  1378. break;
  1379. case 1029:
  1380. str_appendf(buf, buflen,
  1381. "\"station_id\":%u,\"mjd\":%u,\"sec\":%u,"
  1382. "\"len\":%zd,\"units\":%zd,\"msg\":\"%s\",",
  1383. rtcm->rtcmtypes.rtcm3_1029.station_id,
  1384. rtcm->rtcmtypes.rtcm3_1029.mjd,
  1385. rtcm->rtcmtypes.rtcm3_1029.sod,
  1386. rtcm->rtcmtypes.rtcm3_1029.len,
  1387. rtcm->rtcmtypes.rtcm3_1029.unicode_units,
  1388. json_stringify(buf1, sizeof(buf1),
  1389. (char *)rtcm->rtcmtypes.rtcm3_1029.text));
  1390. break;
  1391. case 1033:
  1392. str_appendf(buf, buflen,
  1393. "\"station_id\":%u,\"desc\":\"%s\","
  1394. "\"setup_id\":%u,\"serial\":\"%s\","
  1395. "\"receiver\":\"%s\",\"firmware\":\"%s\"",
  1396. rtcm->rtcmtypes.rtcm3_1033.station_id,
  1397. rtcm->rtcmtypes.rtcm3_1033.descriptor,
  1398. INT(rtcm->rtcmtypes.rtcm3_1033.setup_id),
  1399. rtcm->rtcmtypes.rtcm3_1033.serial,
  1400. rtcm->rtcmtypes.rtcm3_1033.receiver,
  1401. rtcm->rtcmtypes.rtcm3_1033.firmware);
  1402. break;
  1403. default:
  1404. (void)strlcat(buf, "\"data\":[", buflen);
  1405. for (n = 0; n < rtcm->length; n++)
  1406. str_appendf(buf, buflen,
  1407. "\"0x%02x\",",(unsigned int)rtcm->rtcmtypes.data[n]);
  1408. str_rstrip_char(buf, ',');
  1409. (void)strlcat(buf, "]", buflen);
  1410. break;
  1411. }
  1412. str_rstrip_char(buf, ',');
  1413. (void)strlcat(buf, "}\r\n", buflen);
  1414. #undef CODE
  1415. #undef INT
  1416. }
  1417. #endif /* defined(RTCM104V3_ENABLE) */
  1418. #if defined(AIVDM_ENABLE)
  1419. void json_aivdm_dump(const struct ais_t *ais,
  1420. const char *device, bool scaled,
  1421. char *buf, size_t buflen)
  1422. {
  1423. char buf1[JSON_VAL_MAX * 2 + 1];
  1424. char buf2[JSON_VAL_MAX * 2 + 1];
  1425. char buf3[JSON_VAL_MAX * 2 + 1];
  1426. char scratchbuf[MAX_PACKET_LENGTH*2+1];
  1427. int i;
  1428. static char *nav_legends[] = {
  1429. "Under way using engine",
  1430. "At anchor",
  1431. "Not under command",
  1432. "Restricted manoeuverability",
  1433. "Constrained by her draught",
  1434. "Moored",
  1435. "Aground",
  1436. "Engaged in fishing",
  1437. "Under way sailing",
  1438. "Reserved for HSC",
  1439. "Reserved for WIG",
  1440. "Reserved",
  1441. "Reserved",
  1442. "Reserved",
  1443. "Reserved",
  1444. "Not defined",
  1445. };
  1446. static char *epfd_legends[] = {
  1447. "Undefined",
  1448. "GPS",
  1449. "GLONASS",
  1450. "Combined GPS/GLONASS",
  1451. "Loran-C",
  1452. "Chayka",
  1453. "Integrated navigation system",
  1454. "Surveyed",
  1455. "Galileo",
  1456. };
  1457. #define EPFD_DISPLAY(n) (((n) < (unsigned int)NITEMS(epfd_legends)) ? epfd_legends[n] : "INVALID EPFD")
  1458. static char *ship_type_legends[100] = {
  1459. "Not available",
  1460. "Reserved for future use",
  1461. "Reserved for future use",
  1462. "Reserved for future use",
  1463. "Reserved for future use",
  1464. "Reserved for future use",
  1465. "Reserved for future use",
  1466. "Reserved for future use",
  1467. "Reserved for future use",
  1468. "Reserved for future use",
  1469. "Reserved for future use",
  1470. "Reserved for future use",
  1471. "Reserved for future use",
  1472. "Reserved for future use",
  1473. "Reserved for future use",
  1474. "Reserved for future use",
  1475. "Reserved for future use",
  1476. "Reserved for future use",
  1477. "Reserved for future use",
  1478. "Reserved for future use",
  1479. "Wing in ground (WIG) - all ships of this type",
  1480. "Wing in ground (WIG) - Hazardous category A",
  1481. "Wing in ground (WIG) - Hazardous category B",
  1482. "Wing in ground (WIG) - Hazardous category C",
  1483. "Wing in ground (WIG) - Hazardous category D",
  1484. "Wing in ground (WIG) - Reserved for future use",
  1485. "Wing in ground (WIG) - Reserved for future use",
  1486. "Wing in ground (WIG) - Reserved for future use",
  1487. "Wing in ground (WIG) - Reserved for future use",
  1488. "Wing in ground (WIG) - Reserved for future use",
  1489. "Fishing",
  1490. "Towing",
  1491. "Towing: length exceeds 200m or breadth exceeds 25m",
  1492. "Dredging or underwater ops",
  1493. "Diving ops",
  1494. "Military ops",
  1495. "Sailing",
  1496. "Pleasure Craft",
  1497. "Reserved",
  1498. "Reserved",
  1499. "High speed craft (HSC) - all ships of this type",
  1500. "High speed craft (HSC) - Hazardous category A",
  1501. "High speed craft (HSC) - Hazardous category B",
  1502. "High speed craft (HSC) - Hazardous category C",
  1503. "High speed craft (HSC) - Hazardous category D",
  1504. "High speed craft (HSC) - Reserved for future use",
  1505. "High speed craft (HSC) - Reserved for future use",
  1506. "High speed craft (HSC) - Reserved for future use",
  1507. "High speed craft (HSC) - Reserved for future use",
  1508. "High speed craft (HSC) - No additional information",
  1509. "Pilot Vessel",
  1510. "Search and Rescue vessel",
  1511. "Tug",
  1512. "Port Tender",
  1513. "Anti-pollution equipment",
  1514. "Law Enforcement",
  1515. "Spare - Local Vessel",
  1516. "Spare - Local Vessel",
  1517. "Medical Transport",
  1518. "Ship according to RR Resolution No. 18",
  1519. "Passenger - all ships of this type",
  1520. "Passenger - Hazardous category A",
  1521. "Passenger - Hazardous category B",
  1522. "Passenger - Hazardous category C",
  1523. "Passenger - Hazardous category D",
  1524. "Passenger - Reserved for future use",
  1525. "Passenger - Reserved for future use",
  1526. "Passenger - Reserved for future use",
  1527. "Passenger - Reserved for future use",
  1528. "Passenger - No additional information",
  1529. "Cargo - all ships of this type",
  1530. "Cargo - Hazardous category A",
  1531. "Cargo - Hazardous category B",
  1532. "Cargo - Hazardous category C",
  1533. "Cargo - Hazardous category D",
  1534. "Cargo - Reserved for future use",
  1535. "Cargo - Reserved for future use",
  1536. "Cargo - Reserved for future use",
  1537. "Cargo - Reserved for future use",
  1538. "Cargo - No additional information",
  1539. "Tanker - all ships of this type",
  1540. "Tanker - Hazardous category A",
  1541. "Tanker - Hazardous category B",
  1542. "Tanker - Hazardous category C",
  1543. "Tanker - Hazardous category D",
  1544. "Tanker - Reserved for future use",
  1545. "Tanker - Reserved for future use",
  1546. "Tanker - Reserved for future use",
  1547. "Tanker - Reserved for future use",
  1548. "Tanker - No additional information",
  1549. "Other Type - all ships of this type",
  1550. "Other Type - Hazardous category A",
  1551. "Other Type - Hazardous category B",
  1552. "Other Type - Hazardous category C",
  1553. "Other Type - Hazardous category D",
  1554. "Other Type - Reserved for future use",
  1555. "Other Type - Reserved for future use",
  1556. "Other Type - Reserved for future use",
  1557. "Other Type - Reserved for future use",
  1558. "Other Type - no additional information",
  1559. };
  1560. #define SHIPTYPE_DISPLAY(n) (((n) < (unsigned int)NITEMS(ship_type_legends)) ? ship_type_legends[n] : "INVALID SHIP TYPE")
  1561. static const char *station_type_legends[] = {
  1562. "All types of mobiles",
  1563. "Reserved for future use",
  1564. "All types of Class B mobile stations",
  1565. "SAR airborne mobile station",
  1566. "Aid to Navigation station",
  1567. "Class B shipborne mobile station",
  1568. "Regional use and inland waterways",
  1569. "Regional use and inland waterways",
  1570. "Regional use and inland waterways",
  1571. "Regional use and inland waterways",
  1572. "Reserved for future use",
  1573. "Reserved for future use",
  1574. "Reserved for future use",
  1575. "Reserved for future use",
  1576. "Reserved for future use",
  1577. "Reserved for future use",
  1578. };
  1579. #define STATIONTYPE_DISPLAY(n) (((n) < (unsigned int)NITEMS(station_type_legends)) ? station_type_legends[n] : "INVALID STATION TYPE")
  1580. static const char *navaid_type_legends[] = {
  1581. "Unspecified",
  1582. "Reference point",
  1583. "RACON",
  1584. "Fixed offshore structure",
  1585. "Spare, Reserved for future use.",
  1586. "Light, without sectors",
  1587. "Light, with sectors",
  1588. "Leading Light Front",
  1589. "Leading Light Rear",
  1590. "Beacon, Cardinal N",
  1591. "Beacon, Cardinal E",
  1592. "Beacon, Cardinal S",
  1593. "Beacon, Cardinal W",
  1594. "Beacon, Port hand",
  1595. "Beacon, Starboard hand",
  1596. "Beacon, Preferred Channel port hand",
  1597. "Beacon, Preferred Channel starboard hand",
  1598. "Beacon, Isolated danger",
  1599. "Beacon, Safe water",
  1600. "Beacon, Special mark",
  1601. "Cardinal Mark N",
  1602. "Cardinal Mark E",
  1603. "Cardinal Mark S",
  1604. "Cardinal Mark W",
  1605. "Port hand Mark",
  1606. "Starboard hand Mark",
  1607. "Preferred Channel Port hand",
  1608. "Preferred Channel Starboard hand",
  1609. "Isolated danger",
  1610. "Safe Water",
  1611. "Special Mark",
  1612. "Light Vessel / LANBY / Rigs",
  1613. };
  1614. #define NAVAIDTYPE_DISPLAY(n) (((n) < (unsigned int)NITEMS(navaid_type_legends)) ? navaid_type_legends[n] : "INVALID NAVAID TYPE")
  1615. // cppcheck-suppress variableScope
  1616. static const char *signal_legends[] = {
  1617. "N/A",
  1618. "Serious emergency – stop or divert according to instructions.",
  1619. "Vessels shall not proceed.",
  1620. "Vessels may proceed. One way traffic.",
  1621. "Vessels may proceed. Two way traffic.",
  1622. "Vessels shall proceed on specific orders only.",
  1623. "Vessels in main channel shall not proceed.",
  1624. "Vessels in main channel shall proceed on specific orders only.",
  1625. "Vessels in main channel shall proceed on specific orders only.",
  1626. "I = \"in-bound\" only acceptable.",
  1627. "O = \"out-bound\" only acceptable.",
  1628. "F = both \"in- and out-bound\" acceptable.",
  1629. "XI = Code will shift to \"I\" in due time.",
  1630. "XO = Code will shift to \"O\" in due time.",
  1631. "X = Vessels shall proceed only on direction.",
  1632. };
  1633. #define SIGNAL_DISPLAY(n) (((n) < (unsigned int)NITEMS(signal_legends)) ? signal_legends[n] : "INVALID SIGNAL TYPE")
  1634. static const char *route_type[32] = {
  1635. "Undefined (default)",
  1636. "Mandatory",
  1637. "Recommended",
  1638. "Alternative",
  1639. "Recommended route through ice",
  1640. "Ship route plan",
  1641. "Reserved for future use.",
  1642. "Reserved for future use.",
  1643. "Reserved for future use.",
  1644. "Reserved for future use.",
  1645. "Reserved for future use.",
  1646. "Reserved for future use.",
  1647. "Reserved for future use.",
  1648. "Reserved for future use.",
  1649. "Reserved for future use.",
  1650. "Reserved for future use.",
  1651. "Reserved for future use.",
  1652. "Reserved for future use.",
  1653. "Reserved for future use.",
  1654. "Reserved for future use.",
  1655. "Reserved for future use.",
  1656. "Reserved for future use.",
  1657. "Reserved for future use.",
  1658. "Reserved for future use.",
  1659. "Reserved for future use.",
  1660. "Reserved for future use.",
  1661. "Reserved for future use.",
  1662. "Reserved for future use.",
  1663. "Reserved for future use.",
  1664. "Reserved for future use.",
  1665. "Reserved for future use.",
  1666. "Cancel route identified by message linkage",
  1667. };
  1668. // cppcheck-suppress variableScope
  1669. static const char *idtypes[] = {
  1670. "mmsi",
  1671. "imo",
  1672. "callsign",
  1673. "other",
  1674. };
  1675. // cppcheck-suppress variableScope
  1676. static const char *racon_status[] = {
  1677. "No RACON installed",
  1678. "RACON not monitored",
  1679. "RACON operational",
  1680. "RACON ERROR"
  1681. };
  1682. // cppcheck-suppress variableScope
  1683. static const char *light_status[] = {
  1684. "No light or no monitoring",
  1685. "Light ON",
  1686. "Light OFF",
  1687. "Light ERROR"
  1688. };
  1689. // cppcheck-suppress variableScope
  1690. static const char *rta_status[] = {
  1691. "Operational",
  1692. "Limited operation",
  1693. "Out of order",
  1694. "N/A",
  1695. };
  1696. // cppcheck-suppress variableScope
  1697. const char *position_types[8] = {
  1698. "Not available",
  1699. "Port-side to",
  1700. "Starboard-side to",
  1701. "Mediterranean (end-on) mooring",
  1702. "Mooring buoy",
  1703. "Anchorage",
  1704. "Reserved for future use",
  1705. "Reserved for future use",
  1706. };
  1707. (void)snprintf(buf, buflen, "{\"class\":\"AIS\",");
  1708. if (device != NULL && device[0] != '\0')
  1709. str_appendf(buf, buflen, "\"device\":\"%s\",", device);
  1710. str_appendf(buf, buflen,
  1711. "\"type\":%u,\"repeat\":%u,\"mmsi\":%u,\"scaled\":%s,",
  1712. ais->type, ais->repeat, ais->mmsi, JSON_BOOL(scaled));
  1713. switch (ais->type) {
  1714. case 1: /* Position Report */
  1715. case 2:
  1716. case 3:
  1717. if (scaled) {
  1718. char turnlegend[20];
  1719. char speedlegend[20];
  1720. /*
  1721. * Express turn as nan if not available,
  1722. * "fastleft"/"fastright" for fast turns.
  1723. */
  1724. if (ais->type1.turn == -128)
  1725. (void)strlcpy(turnlegend, "\"nan\"", sizeof(turnlegend));
  1726. else if (ais->type1.turn == -127)
  1727. (void)strlcpy(turnlegend, "\"fastleft\"", sizeof(turnlegend));
  1728. else if (ais->type1.turn == 127)
  1729. (void)strlcpy(turnlegend, "\"fastright\"",
  1730. sizeof(turnlegend));
  1731. else {
  1732. double rot1 = ais->type1.turn / 4.733;
  1733. (void)snprintf(turnlegend, sizeof(turnlegend),
  1734. "%.0f", rot1 * rot1);
  1735. }
  1736. /*
  1737. * Express speed as nan if not available,
  1738. * "fast" for fast movers.
  1739. */
  1740. if (ais->type1.speed == AIS_SPEED_NOT_AVAILABLE)
  1741. (void)strlcpy(speedlegend, "\"nan\"", sizeof(speedlegend));
  1742. else if (ais->type1.speed == AIS_SPEED_FAST_MOVER)
  1743. (void)strlcpy(speedlegend, "\"fast\"", sizeof(speedlegend));
  1744. else
  1745. (void)snprintf(speedlegend, sizeof(speedlegend),
  1746. "%.1f", ais->type1.speed / 10.0);
  1747. str_appendf(buf, buflen,
  1748. "\"status\":%u,\"status_text\":\"%s\","
  1749. "\"turn\":%s,\"speed\":%s,"
  1750. "\"accuracy\":%s,\"lon\":%.6f,\"lat\":%.6f,"
  1751. "\"course\":%.1f,\"heading\":%u,\"second\":%u,"
  1752. "\"maneuver\":%u,\"raim\":%s,\"radio\":%u}\r\n",
  1753. ais->type1.status,
  1754. nav_legends[ais->type1.status],
  1755. turnlegend,
  1756. speedlegend,
  1757. JSON_BOOL(ais->type1.accuracy),
  1758. ais->type1.lon / AIS_LATLON_DIV,
  1759. ais->type1.lat / AIS_LATLON_DIV,
  1760. ais->type1.course / 10.0,
  1761. ais->type1.heading,
  1762. ais->type1.second,
  1763. ais->type1.maneuver,
  1764. JSON_BOOL(ais->type1.raim), ais->type1.radio);
  1765. } else {
  1766. str_appendf(buf, buflen,
  1767. "\"status\":%u,\"status_text\":\"%s\","
  1768. "\"turn\":%d,\"speed\":%u,"
  1769. "\"accuracy\":%s,\"lon\":%d,\"lat\":%d,"
  1770. "\"course\":%u,\"heading\":%u,\"second\":%u,"
  1771. "\"maneuver\":%u,\"raim\":%s,\"radio\":%u}\r\n",
  1772. ais->type1.status,
  1773. nav_legends[ais->type1.status],
  1774. ais->type1.turn,
  1775. ais->type1.speed,
  1776. JSON_BOOL(ais->type1.accuracy),
  1777. ais->type1.lon,
  1778. ais->type1.lat,
  1779. ais->type1.course,
  1780. ais->type1.heading,
  1781. ais->type1.second,
  1782. ais->type1.maneuver,
  1783. JSON_BOOL(ais->type1.raim), ais->type1.radio);
  1784. }
  1785. break;
  1786. case 4: /* Base Station Report */
  1787. case 11: /* UTC/Date Response */
  1788. /* some fields have beem merged to an ISO8601 date */
  1789. if (scaled) {
  1790. // The use of %u instead of %04u for the year is to allow
  1791. // out-of-band year values.
  1792. str_appendf(buf, buflen,
  1793. "\"timestamp\":\"%04u-%02u-%02uT%02u:%02u:%02uZ\","
  1794. "\"accuracy\":%s,\"lon\":%.6f,\"lat\":%.6f,"
  1795. "\"epfd\":%u,\"epfd_text\":\"%s\","
  1796. "\"raim\":%s,\"radio\":%u}\r\n",
  1797. ais->type4.year,
  1798. ais->type4.month,
  1799. ais->type4.day,
  1800. ais->type4.hour,
  1801. ais->type4.minute,
  1802. ais->type4.second,
  1803. JSON_BOOL(ais->type4.accuracy),
  1804. ais->type4.lon / AIS_LATLON_DIV,
  1805. ais->type4.lat / AIS_LATLON_DIV,
  1806. ais->type4.epfd,
  1807. EPFD_DISPLAY(ais->type4.epfd),
  1808. JSON_BOOL(ais->type4.raim), ais->type4.radio);
  1809. } else {
  1810. str_appendf(buf, buflen,
  1811. "\"timestamp\":\"%04u-%02u-%02uT%02u:%02u:%02uZ\","
  1812. "\"accuracy\":%s,\"lon\":%d,\"lat\":%d,"
  1813. "\"epfd\":%u,\"epfd_text\":\"%s\","
  1814. "\"raim\":%s,\"radio\":%u}\r\n",
  1815. ais->type4.year,
  1816. ais->type4.month,
  1817. ais->type4.day,
  1818. ais->type4.hour,
  1819. ais->type4.minute,
  1820. ais->type4.second,
  1821. JSON_BOOL(ais->type4.accuracy),
  1822. ais->type4.lon,
  1823. ais->type4.lat,
  1824. ais->type4.epfd,
  1825. EPFD_DISPLAY(ais->type4.epfd),
  1826. JSON_BOOL(ais->type4.raim), ais->type4.radio);
  1827. }
  1828. break;
  1829. case 5: /* Ship static and voyage related data */
  1830. /* some fields have beem merged to an ISO8601 partial date */
  1831. if (scaled) {
  1832. /* *INDENT-OFF* */
  1833. str_appendf(buf, buflen,
  1834. "\"imo\":%u,\"ais_version\":%u,\"callsign\":\"%s\","
  1835. "\"shipname\":\"%s\","
  1836. "\"shiptype\":%u,\"shiptype_text\":\"%s\","
  1837. "\"to_bow\":%u,\"to_stern\":%u,\"to_port\":%u,"
  1838. "\"to_starboard\":%u,"
  1839. "\"epfd\":%u,\"epfd_text\":\"%s\","
  1840. "\"eta\":\"%02u-%02uT%02u:%02uZ\","
  1841. "\"draught\":%.1f,\"destination\":\"%s\","
  1842. "\"dte\":%u}\r\n",
  1843. ais->type5.imo,
  1844. ais->type5.ais_version,
  1845. json_stringify(buf1, sizeof(buf1),
  1846. ais->type5.callsign),
  1847. json_stringify(buf2, sizeof(buf2),
  1848. ais->type5.shipname),
  1849. ais->type5.shiptype,
  1850. SHIPTYPE_DISPLAY(ais->type5.shiptype),
  1851. ais->type5.to_bow, ais->type5.to_stern,
  1852. ais->type5.to_port, ais->type5.to_starboard,
  1853. ais->type5.epfd,
  1854. EPFD_DISPLAY(ais->type5.epfd),
  1855. ais->type5.month,
  1856. ais->type5.day,
  1857. ais->type5.hour, ais->type5.minute,
  1858. ais->type5.draught / 10.0,
  1859. json_stringify(buf3, sizeof(buf3),
  1860. ais->type5.destination),
  1861. ais->type5.dte);
  1862. /* *INDENT-ON* */
  1863. } else {
  1864. str_appendf(buf, buflen,
  1865. "\"imo\":%u,\"ais_version\":%u,\"callsign\":\"%s\","
  1866. "\"shipname\":\"%s\","
  1867. "\"shiptype\":%u,\"shiptype_text\":\"%s\","
  1868. "\"to_bow\":%u,\"to_stern\":%u,\"to_port\":%u,"
  1869. "\"to_starboard\":%u,"
  1870. "\"epfd\":%u,\"epfd_text\":\"%s\","
  1871. "\"eta\":\"%02u-%02uT%02u:%02uZ\","
  1872. "\"draught\":%u,\"destination\":\"%s\","
  1873. "\"dte\":%u}\r\n",
  1874. ais->type5.imo,
  1875. ais->type5.ais_version,
  1876. json_stringify(buf1, sizeof(buf1),
  1877. ais->type5.callsign),
  1878. json_stringify(buf2, sizeof(buf2),
  1879. ais->type5.shipname),
  1880. ais->type5.shiptype,
  1881. SHIPTYPE_DISPLAY(ais->type5.shiptype),
  1882. ais->type5.to_bow,
  1883. ais->type5.to_stern,
  1884. ais->type5.to_port,
  1885. ais->type5.to_starboard,
  1886. ais->type5.epfd,
  1887. EPFD_DISPLAY(ais->type5.epfd),
  1888. ais->type5.month,
  1889. ais->type5.day,
  1890. ais->type5.hour,
  1891. ais->type5.minute,
  1892. ais->type5.draught,
  1893. json_stringify(buf3, sizeof(buf3),
  1894. ais->type5.destination),
  1895. ais->type5.dte);
  1896. }
  1897. break;
  1898. case 6: /* Binary Message */
  1899. str_appendf(buf, buflen,
  1900. "\"seqno\":%u,\"dest_mmsi\":%u,"
  1901. "\"retransmit\":%s,\"dac\":%u,\"fid\":%u,",
  1902. ais->type6.seqno,
  1903. ais->type6.dest_mmsi,
  1904. JSON_BOOL(ais->type6.retransmit),
  1905. ais->type6.dac,
  1906. ais->type6.fid);
  1907. if (!ais->type6.structured) {
  1908. str_appendf(buf, buflen,
  1909. "\"data\":\"%zd:%s\"}\r\n",
  1910. ais->type6.bitcount,
  1911. json_stringify(buf1, sizeof(buf1),
  1912. gpsd_hexdump(
  1913. scratchbuf, sizeof(scratchbuf),
  1914. (char *)ais->type6.bitdata,
  1915. BITS_TO_BYTES(ais->type6.bitcount))));
  1916. break;
  1917. }
  1918. if (ais->type6.dac == 200) {
  1919. switch (ais->type6.fid) {
  1920. case 21:
  1921. str_appendf(buf, buflen,
  1922. "\"country\":\"%s\",\"locode\":\"%s\","
  1923. "\"section\":\"%s\",\"terminal\":\"%s\","
  1924. "\"hectometre\":\"%s\",\"eta\":\"%u-%uT%u:%u\","
  1925. "\"tugs\":%u,\"airdraught\":%u}\r\n",
  1926. ais->type6.dac200fid21.country,
  1927. ais->type6.dac200fid21.locode,
  1928. ais->type6.dac200fid21.section,
  1929. ais->type6.dac200fid21.terminal,
  1930. ais->type6.dac200fid21.hectometre,
  1931. ais->type6.dac200fid21.month,
  1932. ais->type6.dac200fid21.day,
  1933. ais->type6.dac200fid21.hour,
  1934. ais->type6.dac200fid21.minute,
  1935. ais->type6.dac200fid21.tugs,
  1936. ais->type6.dac200fid21.airdraught);
  1937. break;
  1938. case 22:
  1939. str_appendf(buf, buflen,
  1940. "\"country\":\"%s\",\"locode\":\"%s\","
  1941. "\"section\":\"%s\","
  1942. "\"terminal\":\"%s\",\"hectometre\":\"%s\","
  1943. "\"eta\":\"%u-%uT%u:%u\","
  1944. "\"status\":%u,\"status_text\":\"%s\"}\r\n",
  1945. ais->type6.dac200fid22.country,
  1946. ais->type6.dac200fid22.locode,
  1947. ais->type6.dac200fid22.section,
  1948. ais->type6.dac200fid22.terminal,
  1949. ais->type6.dac200fid22.hectometre,
  1950. ais->type6.dac200fid22.month,
  1951. ais->type6.dac200fid22.day,
  1952. ais->type6.dac200fid22.hour,
  1953. ais->type6.dac200fid22.minute,
  1954. ais->type6.dac200fid22.status,
  1955. rta_status[ais->type6.dac200fid22.status]);
  1956. break;
  1957. case 55:
  1958. str_appendf(buf, buflen,
  1959. "\"crew\":%u,\"passengers\":%u,\"personnel\":%u}\r\n",
  1960. ais->type6.dac200fid55.crew,
  1961. ais->type6.dac200fid55.passengers,
  1962. ais->type6.dac200fid55.personnel);
  1963. break;
  1964. }
  1965. }
  1966. else if (ais->type6.dac == 235 || ais->type6.dac == 250) {
  1967. switch (ais->type6.fid) {
  1968. case 10: /* GLA - AtoN monitoring data */
  1969. str_appendf(buf, buflen,
  1970. "\"off_pos\":%s,\"alarm\":%s,"
  1971. "\"stat_ext\":%u,",
  1972. JSON_BOOL(ais->type6.dac235fid10.off_pos),
  1973. JSON_BOOL(ais->type6.dac235fid10.alarm),
  1974. ais->type6.dac235fid10.stat_ext);
  1975. if (scaled && ais->type6.dac235fid10.ana_int != 0)
  1976. str_appendf(buf, buflen,
  1977. "\"ana_int\":%.2f,",
  1978. ais->type6.dac235fid10.ana_int*0.05);
  1979. else
  1980. str_appendf(buf, buflen,
  1981. "\"ana_int\":%u,",
  1982. ais->type6.dac235fid10.ana_int);
  1983. if (scaled && ais->type6.dac235fid10.ana_ext1 != 0)
  1984. str_appendf(buf, buflen,
  1985. "\"ana_ext1\":%.2f,",
  1986. ais->type6.dac235fid10.ana_ext1*0.05);
  1987. else
  1988. str_appendf(buf, buflen,
  1989. "\"ana_ext1\":%u,",
  1990. ais->type6.dac235fid10.ana_ext1);
  1991. if (scaled && ais->type6.dac235fid10.ana_ext2 != 0)
  1992. str_appendf(buf, buflen,
  1993. "\"ana_ext2\":%.2f,",
  1994. ais->type6.dac235fid10.ana_ext2*0.05);
  1995. else
  1996. str_appendf(buf, buflen,
  1997. "\"ana_ext2\":%u,",
  1998. ais->type6.dac235fid10.ana_ext2);
  1999. str_appendf(buf, buflen,
  2000. "\"racon\":%u,"
  2001. "\"racon_text\":\"%s\","
  2002. "\"light\":%u,"
  2003. "\"light_text\":\"%s\"",
  2004. ais->type6.dac235fid10.racon,
  2005. racon_status[ais->type6.dac235fid10.racon],
  2006. ais->type6.dac235fid10.light,
  2007. light_status[ais->type6.dac235fid10.light]);
  2008. str_rstrip_char(buf, ',');
  2009. (void)strlcat(buf, "}\r\n", buflen);
  2010. break;
  2011. }
  2012. }
  2013. else if (ais->type6.dac == 1) {
  2014. char buf4[JSON_VAL_MAX * 2 + 1];
  2015. switch (ais->type6.fid) {
  2016. case 12: /* IMO236 -Dangerous cargo indication */
  2017. /* some fields have beem merged to an ISO8601 partial date */
  2018. str_appendf(buf, buflen,
  2019. "\"lastport\":\"%s\","
  2020. "\"departure\":\"%02u-%02uT%02u:%02uZ\","
  2021. "\"nextport\":\"%s\","
  2022. "\"eta\":\"%02u-%02uT%02u:%02uZ\&