gpsdclient.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 "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 "gps.h"
  15. #include "gpsdclient.h"
  16. #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. source->port = colon1 + 1;
  227. colon2 = strchr(source->port, ':');
  228. if (colon2 != NULL) {
  229. *colon2 = '\0';
  230. source->device = colon2 + 1;
  231. }
  232. } else if (strchr(source->spec, '/') != NULL) {
  233. source->device = source->spec;
  234. } else {
  235. source->server = source->spec;
  236. }
  237. }
  238. if (*source->server == '[') {
  239. char *rbrk = strchr(source->server, ']');
  240. ++source->server;
  241. if (rbrk != NULL)
  242. *rbrk = '\0';
  243. }
  244. }
  245. /* lat/lon to Maidenhead */
  246. char *maidenhead(double lat, double lon)
  247. {
  248. /*
  249. * Specification at
  250. * https://en.wikipedia.org/wiki/Maidenhead_Locator_System
  251. *
  252. * There's a fair amount of slop in how Maidenhead converters operate
  253. * that can make it look like this one is wrong.
  254. *
  255. * 1. Many return caps for paces 5 and 6 when according to the spec
  256. * they should return smalls.
  257. *
  258. * 2. Some converters, including QGrid from which this code was originally
  259. * derived, add an 0.5 offset to the divided e and n just before it
  260. * is cast to integer and used for places 5 and 6. This appears to be
  261. * intended as a round-to-nearest hack (as opposed to the implicit
  262. * round down from the cast). If I'm reading the spec right it
  263. * is not correct to do this.
  264. */
  265. /* FIXME: convert lat/lon to integer seconds, then do all the
  266. * math on integers */
  267. static char buf[9];
  268. int t1;
  269. /* longitude */
  270. if (179.99999 < lon) {
  271. /* force 180, just inside lon_sq 'R'
  272. * otherwise we get illegal 'S' */
  273. lon = 179.99999;
  274. }
  275. lon += 180.0;
  276. // divide into 18 zones (fields) each 20 degrees lon
  277. t1 = (int)(lon / 20);
  278. buf[0] = (char)t1 + 'A';
  279. if ('R' < buf[0]) {
  280. /* A to R */
  281. buf[0] = 'R';
  282. }
  283. lon -= (float)t1 * 20.0;
  284. // divide into 10 zones (squares) each 2 degrees lon
  285. t1 = (int)lon / 2;
  286. buf[2] = (char)t1 + '0';
  287. lon -= (float)t1 * 2;
  288. // lon is now degrees
  289. lon *= 60.0;
  290. // divide into 24 zones (subsquares) each 5 minute (5/60 deg) lon
  291. t1 = (int)(lon / 5);
  292. buf[4] = (char) ((char)t1 + 'a');
  293. lon -= (float)(t1 * 5);
  294. // lon is now seconds
  295. lon *= 60.0;
  296. // divide into 10 zones (extended squares) each 30 seconds (5/600 deg) lon
  297. t1 = (int)(lon / 30);
  298. if (9 < t1) {
  299. // ugh, floating point gunk.
  300. t1 = 9;
  301. }
  302. buf[6] = (char) ((char)t1 + '0');
  303. /* latitude */
  304. if (89.99999 < lat) {
  305. /* force 90 to just inside lat_sq 'R'
  306. * otherwise we get illegal 'S' */
  307. lat = 89.99999;
  308. }
  309. lat += 90.0;
  310. // divide into 18 zones (fields) each 10 degrees lat
  311. t1 = (int)(lat / 10.0);
  312. buf[1] = (char)t1 + 'A';
  313. if ('R' < buf[1]) {
  314. /* A to R, North Pole is R */
  315. buf[1] = 'R';
  316. }
  317. lat -= (float)t1 * 10.0;
  318. // divide into 10 zones (squares) each 1 degrees lat
  319. buf[3] = (char)lat + '0';
  320. lat -= (int)lat;
  321. // lat is now degrees
  322. lat *= 60.0;
  323. // divide into 24 zones (subsquares) each 2.5 minute (5/120 deg) lat
  324. t1 = (int)(lat / 2.5);
  325. buf[5] = (char)((char)t1 + 'a');
  326. lat -= (float)(t1 * 2.5);
  327. // lat is now seconds
  328. lat *= 60.0;
  329. // divide into 10 zones (extended squares) each 15 seconds (5/1200 deg) lat
  330. t1 = (int)(lat / 15);
  331. if (9 < t1) {
  332. // ugh, floating point gunk.
  333. t1 = 9;
  334. }
  335. buf[7] = (char) ((char)t1 + '0');
  336. buf[8] = '\0';
  337. return buf;
  338. }
  339. #define NITEMS(x) (int)(sizeof(x)/sizeof(x[0])) /* from gpsd.h-tail */
  340. /* Look up an available export method by name */
  341. struct exportmethod_t *export_lookup(const char *name)
  342. {
  343. struct exportmethod_t *mp, *method = NULL;
  344. for (mp = exportmethods;
  345. mp < exportmethods + NITEMS(exportmethods);
  346. mp++)
  347. if (strcmp(mp->name, name) == 0)
  348. method = mp;
  349. return method;
  350. }
  351. /* list known export methods */
  352. void export_list(FILE *fp)
  353. {
  354. struct exportmethod_t *method;
  355. for (method = exportmethods;
  356. method < exportmethods + NITEMS(exportmethods);
  357. method++)
  358. (void)fprintf(fp, "%s: %s\n", method->name, method->description);
  359. }
  360. struct exportmethod_t *export_default(void)
  361. {
  362. return (NITEMS(exportmethods) > 0) ? &exportmethods[0] : NULL;
  363. }
  364. /* gpsdclient.c ends here */
  365. // vim: set expandtab shiftwidth=4