gpsdclient.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * gpsdclient.c -- support functions for GPSD clients
  3. *
  4. * This file is Copyright 2010 by the GPSD project
  5. * SPDX-License-Identifier: BSD-2-clause
  6. */
  7. #include "../include/gpsd_config.h" /* must be before all includes */
  8. #include <assert.h>
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <strings.h> /* for strcasecmp() */
  14. #include "../include/gps.h"
  15. #include "../include/gpsdclient.h"
  16. #include "../include/os_compat.h"
  17. static struct exportmethod_t exportmethods[] = {
  18. #if defined(DBUS_EXPORT_ENABLE)
  19. {"dbus", GPSD_DBUS_EXPORT, "DBUS broadcast"},
  20. #endif /* defined(DBUS_EXPORT_ENABLE) */
  21. #ifdef SHM_EXPORT_ENABLE
  22. {"shm", GPSD_SHARED_MEMORY, "shared memory"},
  23. #endif /* SOCKET_EXPORT_ENABLE */
  24. #ifdef SOCKET_EXPORT_ENABLE
  25. {"sockets", NULL, "JSON via sockets"},
  26. #endif /* SOCKET_EXPORT_ENABLE */
  27. };
  28. /* convert value of double degrees to a buffer.
  29. * add suffix_pos or suffix_neg depending on sign.
  30. * buffer should be at least 20 bytes.
  31. * Return a pointer to the buffer.
  32. *
  33. * deg_str_type:
  34. * deg_dd : return DD.ddddddd[suffix]
  35. * deg_ddmm : return DD MM.mmmmmm'[suffix]
  36. * deg_ddmmss : return DD MM' SS.sssss"[suffix]
  37. *
  38. * returns 'n/a' for 360 < f or -360 > f
  39. *
  40. * NOTE: 360.0 is rolled over to 0.0
  41. *
  42. * for cm level accuracy, at sea level, we need degrees
  43. * to 7+ decimal places
  44. * Ref: https://en.wikipedia.org/wiki/Decimal_degrees
  45. *
  46. */
  47. char *deg_to_str2(enum deg_str_type type, double f,
  48. char *buf, unsigned int buf_size,
  49. const char *suffix_pos, const char *suffix_neg)
  50. {
  51. int dsec, sec, deg, min;
  52. double fdsec, fsec, fdeg, fmin;
  53. const char *suffix = "";
  54. if (20 > buf_size) {
  55. (void)strlcpy(buf, "Err", buf_size);
  56. return buf;
  57. }
  58. if (!isfinite(f) || 360.0 < fabs(f)) {
  59. (void)strlcpy(buf, "n/a", buf_size);
  60. return buf;
  61. }
  62. /* suffix? */
  63. if (0.0 > f) {
  64. f = -f;
  65. if (NULL != suffix_neg) {
  66. suffix = suffix_neg;
  67. }
  68. } else if (NULL != suffix_pos) {
  69. suffix = suffix_pos;
  70. }
  71. /* add rounding quanta */
  72. /* IEEE 754 wants round to nearest even.
  73. * We cheat and just round to nearest.
  74. * Intel trying to kill off round to nearest even. */
  75. switch (type) {
  76. default:
  77. /* huh? */
  78. type = deg_dd;
  79. /* It's not worth battling fallthrough warnings just for two lines */
  80. f += 0.5 * 1e-8; /* round up */
  81. break;
  82. case deg_dd:
  83. /* DD.dddddddd */
  84. f += 0.5 * 1e-8; /* round up */
  85. break;
  86. case deg_ddmm:
  87. /* DD MM.mmmmmm */
  88. f += (0.5 * 1e-6) / 60; /* round up */
  89. break;
  90. case deg_ddmmss:
  91. f += (0.5 * 1e-5) / 3600; /* round up */
  92. break;
  93. }
  94. fmin = modf(f, &fdeg);
  95. deg = (int)fdeg;
  96. if (360 == deg) {
  97. /* fix round-up roll-over */
  98. deg = 0;
  99. fmin = 0.0;
  100. }
  101. if (deg_dd == type) {
  102. /* DD.dddddddd */
  103. long frac_deg = (long)(fmin * 100000000.0);
  104. /* cm level accuracy requires the %08ld */
  105. (void)snprintf(buf, buf_size, "%3d.%08ld%s", deg, frac_deg, suffix);
  106. return buf;
  107. }
  108. fsec = modf(fmin * 60, &fmin);
  109. min = (int)fmin;
  110. if (deg_ddmm == type) {
  111. /* DD MM.mmmmmm */
  112. sec = (int)(fsec * 1000000.0);
  113. (void)snprintf(buf, buf_size, "%3d %02d.%06d'%s", deg, min, sec,
  114. suffix);
  115. return buf;
  116. }
  117. /* else DD MM SS.sss */
  118. fdsec = modf(fsec * 60.0, &fsec);
  119. sec = (int)fsec;
  120. dsec = (int)(fdsec * 100000.0);
  121. (void)snprintf(buf, buf_size, "%3d %02d' %02d.%05d\"%s", deg, min, sec,
  122. dsec, suffix);
  123. return buf;
  124. }
  125. /* convert absolute value of double degrees to a static string.
  126. * Return a pointer to the static string.
  127. * WARNING: Not thread safe.
  128. *
  129. * deg_str_type:
  130. * deg_dd : return DD.ddddddd
  131. * deg_ddmm : return DD MM.mmmmmm'
  132. * deg_ddmmss : return DD MM' SS.sssss"
  133. *
  134. * returns 'n/a' for 360 < f
  135. *
  136. * NOTE: 360.0 is rolled over to 0.0
  137. *
  138. * for cm level accuracy, at sea level, we need degrees
  139. * to 7+ decimal places
  140. * Ref: https://en.wikipedia.org/wiki/Decimal_degrees
  141. *
  142. */
  143. char *deg_to_str(enum deg_str_type type, double f)
  144. {
  145. static char buf[20];
  146. return deg_to_str2(type, f, buf, sizeof(buf), "", "");
  147. }
  148. /*
  149. * check the environment to determine proper GPS units
  150. *
  151. * clients should only call this if no user preference is specified on
  152. * the command line or via X resources.
  153. *
  154. * return imperial - Use miles/feet
  155. * nautical - Use knots/feet
  156. * metric - Use km/meters
  157. * unspecified - use compiled default
  158. *
  159. * In order check these environment vars:
  160. * GPSD_UNITS one of:
  161. * imperial = miles/feet
  162. * nautical = knots/feet
  163. * metric = km/meters
  164. * LC_MEASUREMENT
  165. * en_US = miles/feet
  166. * C = miles/feet
  167. * POSIX = miles/feet
  168. * [other] = km/meters
  169. * LANG
  170. * en_US = miles/feet
  171. * C = miles/feet
  172. * POSIX = miles/feet
  173. * [other] = km/meters
  174. *
  175. * if none found then return compiled in default
  176. */
  177. enum unit gpsd_units(void)
  178. {
  179. char *envu = NULL;
  180. if ((envu = getenv("GPSD_UNITS")) != NULL && *envu != '\0') {
  181. if (0 == strcasecmp(envu, "imperial")) {
  182. return imperial;
  183. }
  184. if (0 == strcasecmp(envu, "nautical")) {
  185. return nautical;
  186. }
  187. if (0 == strcasecmp(envu, "metric")) {
  188. return metric;
  189. }
  190. /* unrecognized, ignore it */
  191. }
  192. if (((envu = getenv("LC_MEASUREMENT")) != NULL && *envu != '\0')
  193. || ((envu = getenv("LANG")) != NULL && *envu != '\0')) {
  194. if (strncasecmp(envu, "en_US", 5) == 0
  195. || strcasecmp(envu, "C") == 0 || strcasecmp(envu, "POSIX") == 0) {
  196. return imperial;
  197. }
  198. /* Other, must be metric */
  199. return metric;
  200. }
  201. /* TODO: allow a compile time default here */
  202. return unspecified;
  203. }
  204. // standard parsing of a GPS data source spec
  205. void gpsd_source_spec(const char *arg, struct fixsource_t *source)
  206. {
  207. // the casts attempt to head off a -Wwrite-strings warning
  208. source->server = (char *)"localhost";
  209. source->port = (char *)DEFAULT_GPSD_PORT;
  210. source->device = NULL;
  211. if (arg != NULL) {
  212. char *colon1, *skipto, *rbrk;
  213. source->spec = (char *)arg;
  214. assert(source->spec != NULL);
  215. skipto = source->spec;
  216. if (*skipto == '[' && (rbrk = strchr(skipto, ']')) != NULL) {
  217. skipto = rbrk;
  218. }
  219. colon1 = strchr(skipto, ':');
  220. if (colon1 != NULL) {
  221. char *colon2;
  222. *colon1 = '\0';
  223. if (colon1 != source->spec) {
  224. source->server = source->spec;
  225. }
  226. if ('\0' != colon1[1] &&
  227. ':' != colon1[1]) {
  228. // override default only if there is a port string.
  229. source->port = colon1 + 1;
  230. }
  231. colon2 = strchr(colon1 + 1, ':');
  232. if (colon2 != NULL) {
  233. *colon2 = '\0';
  234. if ('\0' != colon2[1]) {
  235. // override default only if there is a device string.
  236. source->device = colon2 + 1;
  237. }
  238. }
  239. } else if (strchr(source->spec, '/') != NULL) {
  240. source->device = source->spec;
  241. } else {
  242. source->server = source->spec;
  243. }
  244. }
  245. if ('[' == *source->server) {
  246. char *rbrk = strchr(source->server, ']');
  247. ++source->server;
  248. if (NULL != rbrk) {
  249. *rbrk = '\0';
  250. }
  251. }
  252. }
  253. /* lat/lon to Maidenhead
  254. * Warning, not thread safe due to static return string
  255. *
  256. * Return: pointer to NUL terminated static string
  257. */
  258. const char *maidenhead(double lat, double lon)
  259. {
  260. /*
  261. * Specification at
  262. * https://en.wikipedia.org/wiki/Maidenhead_Locator_System
  263. *
  264. * There's a fair amount of slop in how Maidenhead converters operate
  265. * that can make it look like this one is wrong.
  266. *
  267. * 1. Many return caps for paces 5 and 6 when according to the spec
  268. * they should return smalls.
  269. *
  270. * 2. Some converters, including QGrid from which this code was originally
  271. * derived, add an 0.5 offset to the divided e and n just before it
  272. * is cast to integer and used for places 5 and 6. This appears to be
  273. * intended as a round-to-nearest hack (as opposed to the implicit
  274. * round down from the cast). If I'm reading the spec right it
  275. * is not correct to do this.
  276. */
  277. /* FIXME: convert lat/lon to integer seconds, then do all the
  278. * math on integers */
  279. static char buf[9];
  280. int t1;
  281. // range check
  282. if (180.001 < fabs(lon) ||
  283. 90.001 < fabs(lat)) {
  284. return " n/a ";
  285. }
  286. // longitude
  287. if (179.99999 < lon) {
  288. /* force 180, just inside lon_sq 'R'
  289. * otherwise we get illegal 'S' */
  290. lon = 179.99999;
  291. }
  292. lon += 180.0;
  293. // divide into 18 zones (fields) each 20 degrees lon
  294. t1 = (int)(lon / 20);
  295. buf[0] = (char)t1 + 'A';
  296. if ('R' < buf[0]) {
  297. // A to R
  298. buf[0] = 'R';
  299. }
  300. lon -= (float)t1 * 20.0;
  301. // divide into 10 zones (squares) each 2 degrees lon
  302. t1 = (int)lon / 2;
  303. buf[2] = (char)t1 + '0';
  304. lon -= (float)t1 * 2;
  305. // lon is now degrees
  306. lon *= 60.0;
  307. // divide into 24 zones (subsquares) each 5 minute (5/60 deg) lon
  308. t1 = (int)(lon / 5);
  309. buf[4] = (char) ((char)t1 + 'a');
  310. lon -= (float)(t1 * 5);
  311. // lon is now seconds
  312. lon *= 60.0;
  313. // divide into 10 zones (extended squares) each 30 seconds (5/600 deg) lon
  314. t1 = (int)(lon / 30);
  315. if (9 < t1) {
  316. // ugh, floating point gunk.
  317. t1 = 9;
  318. }
  319. buf[6] = (char) ((char)t1 + '0');
  320. // latitude
  321. if (89.99999 < lat) {
  322. /* force 90 to just inside lat_sq 'R'
  323. * otherwise we get illegal 'S' */
  324. lat = 89.99999;
  325. }
  326. lat += 90.0;
  327. // divide into 18 zones (fields) each 10 degrees lat
  328. t1 = (int)(lat / 10.0);
  329. buf[1] = (char)t1 + 'A';
  330. if ('R' < buf[1]) {
  331. // A to R, North Pole is R
  332. buf[1] = 'R';
  333. }
  334. lat -= (float)t1 * 10.0;
  335. // divide into 10 zones (squares) each 1 degrees lat
  336. buf[3] = (char)lat + '0';
  337. lat -= (int)lat;
  338. // lat is now degrees
  339. lat *= 60.0;
  340. // divide into 24 zones (subsquares) each 2.5 minute (5/120 deg) lat
  341. t1 = (int)(lat / 2.5);
  342. buf[5] = (char)((char)t1 + 'a');
  343. lat -= (float)(t1 * 2.5);
  344. // lat is now seconds
  345. lat *= 60.0;
  346. // divide into 10 zones (extended squares) each 15 seconds (5/1200 deg) lat
  347. t1 = (int)(lat / 15);
  348. if (9 < t1) {
  349. // ugh, floating point gunk.
  350. t1 = 9;
  351. }
  352. buf[7] = (char) ((char)t1 + '0');
  353. buf[8] = '\0';
  354. return buf;
  355. }
  356. #define NITEMS(x) (int)(sizeof(x)/sizeof(x[0])) // from gpsd.h-tail
  357. // Look up an available export method by name
  358. struct exportmethod_t *export_lookup(const char *name)
  359. {
  360. struct exportmethod_t *mp, *method = NULL;
  361. for (mp = exportmethods;
  362. mp < exportmethods + NITEMS(exportmethods);
  363. mp++) {
  364. if (0 == strcmp(mp->name, name)) {
  365. method = mp;
  366. }
  367. }
  368. return method;
  369. }
  370. // list known export methods
  371. void export_list(FILE *fp)
  372. {
  373. struct exportmethod_t *method;
  374. for (method = exportmethods;
  375. method < exportmethods + NITEMS(exportmethods);
  376. method++)
  377. (void)fprintf(fp, "%s: %s\n", method->name, method->description);
  378. }
  379. struct exportmethod_t *export_default(void)
  380. {
  381. return (NITEMS(exportmethods) > 0) ? &exportmethods[0] : NULL;
  382. }
  383. // vim: set expandtab shiftwidth=4