123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- #include "../include/gpsd_config.h"
- #include <assert.h>
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include "../include/gps.h"
- #include "../include/gpsdclient.h"
- #include "../include/os_compat.h"
- static struct exportmethod_t exportmethods[] = {
- #if defined(DBUS_EXPORT_ENABLE)
- {"dbus", GPSD_DBUS_EXPORT, "DBUS broadcast"},
- #endif
- #ifdef SHM_EXPORT_ENABLE
- {"shm", GPSD_SHARED_MEMORY, "shared memory"},
- #endif
- #ifdef SOCKET_EXPORT_ENABLE
- {"sockets", NULL, "JSON via sockets"},
- #endif
- };
- char *deg_to_str2(enum deg_str_type type, double f,
- char *buf, unsigned int buf_size,
- const char *suffix_pos, const char *suffix_neg)
- {
- int dsec, sec, deg, min;
- double fdsec, fsec, fdeg, fmin;
- const char *suffix = "";
- if (20 > buf_size) {
- (void)strlcpy(buf, "Err", buf_size);
- return buf;
- }
- if (!isfinite(f) || 360.0 < fabs(f)) {
- (void)strlcpy(buf, "n/a", buf_size);
- return buf;
- }
-
- if (0.0 > f) {
- f = -f;
- if (NULL != suffix_neg) {
- suffix = suffix_neg;
- }
- } else if (NULL != suffix_pos) {
- suffix = suffix_pos;
- }
-
-
- switch (type) {
- default:
-
- type = deg_dd;
-
- f += 0.5 * 1e-8;
- break;
- case deg_dd:
-
- f += 0.5 * 1e-8;
- break;
- case deg_ddmm:
-
- f += (0.5 * 1e-6) / 60;
- break;
- case deg_ddmmss:
- f += (0.5 * 1e-5) / 3600;
- break;
- }
- fmin = modf(f, &fdeg);
- deg = (int)fdeg;
- if (360 == deg) {
-
- deg = 0;
- fmin = 0.0;
- }
- if (deg_dd == type) {
-
- long frac_deg = (long)(fmin * 100000000.0);
-
- (void)snprintf(buf, buf_size, "%3d.%08ld%s", deg, frac_deg, suffix);
- return buf;
- }
- fsec = modf(fmin * 60, &fmin);
- min = (int)fmin;
- if (deg_ddmm == type) {
-
- sec = (int)(fsec * 1000000.0);
- (void)snprintf(buf, buf_size, "%3d %02d.%06d'%s", deg, min, sec,
- suffix);
- return buf;
- }
-
- fdsec = modf(fsec * 60.0, &fsec);
- sec = (int)fsec;
- dsec = (int)(fdsec * 100000.0);
- (void)snprintf(buf, buf_size, "%3d %02d' %02d.%05d\"%s", deg, min, sec,
- dsec, suffix);
- return buf;
- }
- char *deg_to_str(enum deg_str_type type, double f)
- {
- static char buf[20];
- return deg_to_str2(type, f, buf, sizeof(buf), "", "");
- }
- enum unit gpsd_units(void)
- {
- char *envu = NULL;
- if ((envu = getenv("GPSD_UNITS")) != NULL && *envu != '\0') {
- if (0 == strcasecmp(envu, "imperial")) {
- return imperial;
- }
- if (0 == strcasecmp(envu, "nautical")) {
- return nautical;
- }
- if (0 == strcasecmp(envu, "metric")) {
- return metric;
- }
-
- }
- if (((envu = getenv("LC_MEASUREMENT")) != NULL && *envu != '\0')
- || ((envu = getenv("LANG")) != NULL && *envu != '\0')) {
- if (strncasecmp(envu, "en_US", 5) == 0
- || strcasecmp(envu, "C") == 0 || strcasecmp(envu, "POSIX") == 0) {
- return imperial;
- }
-
- return metric;
- }
-
- return unspecified;
- }
- void gpsd_source_spec(const char *arg, struct fixsource_t *source)
- {
-
- source->server = (char *)"localhost";
- source->port = (char *)DEFAULT_GPSD_PORT;
- source->device = NULL;
- if (arg != NULL) {
- char *colon1, *skipto, *rbrk;
- source->spec = (char *)arg;
- assert(source->spec != NULL);
- skipto = source->spec;
- if (*skipto == '[' && (rbrk = strchr(skipto, ']')) != NULL) {
- skipto = rbrk;
- }
- colon1 = strchr(skipto, ':');
- if (colon1 != NULL) {
- char *colon2;
- *colon1 = '\0';
- if (colon1 != source->spec) {
- source->server = source->spec;
- }
- source->port = colon1 + 1;
- colon2 = strchr(source->port, ':');
- if (colon2 != NULL) {
- *colon2 = '\0';
- source->device = colon2 + 1;
- }
- } else if (strchr(source->spec, '/') != NULL) {
- source->device = source->spec;
- } else {
- source->server = source->spec;
- }
- }
- if (*source->server == '[') {
- char *rbrk = strchr(source->server, ']');
- ++source->server;
- if (rbrk != NULL)
- *rbrk = '\0';
- }
- }
- char *maidenhead(double lat, double lon)
- {
-
-
- static char buf[9];
- int t1;
-
- if (179.99999 < lon) {
-
- lon = 179.99999;
- }
- lon += 180.0;
-
- t1 = (int)(lon / 20);
- buf[0] = (char)t1 + 'A';
- if ('R' < buf[0]) {
-
- buf[0] = 'R';
- }
- lon -= (float)t1 * 20.0;
-
- t1 = (int)lon / 2;
- buf[2] = (char)t1 + '0';
- lon -= (float)t1 * 2;
-
- lon *= 60.0;
-
- t1 = (int)(lon / 5);
- buf[4] = (char) ((char)t1 + 'a');
- lon -= (float)(t1 * 5);
-
- lon *= 60.0;
-
- t1 = (int)(lon / 30);
- if (9 < t1) {
-
- t1 = 9;
- }
- buf[6] = (char) ((char)t1 + '0');
-
- if (89.99999 < lat) {
-
- lat = 89.99999;
- }
- lat += 90.0;
-
- t1 = (int)(lat / 10.0);
- buf[1] = (char)t1 + 'A';
- if ('R' < buf[1]) {
-
- buf[1] = 'R';
- }
- lat -= (float)t1 * 10.0;
-
- buf[3] = (char)lat + '0';
- lat -= (int)lat;
-
- lat *= 60.0;
-
- t1 = (int)(lat / 2.5);
- buf[5] = (char)((char)t1 + 'a');
- lat -= (float)(t1 * 2.5);
-
- lat *= 60.0;
-
- t1 = (int)(lat / 15);
- if (9 < t1) {
-
- t1 = 9;
- }
- buf[7] = (char) ((char)t1 + '0');
- buf[8] = '\0';
- return buf;
- }
- #define NITEMS(x) (int)(sizeof(x)/sizeof(x[0]))
- struct exportmethod_t *export_lookup(const char *name)
- {
- struct exportmethod_t *mp, *method = NULL;
- for (mp = exportmethods;
- mp < exportmethods + NITEMS(exportmethods);
- mp++)
- if (strcmp(mp->name, name) == 0)
- method = mp;
- return method;
- }
- void export_list(FILE *fp)
- {
- struct exportmethod_t *method;
- for (method = exportmethods;
- method < exportmethods + NITEMS(exportmethods);
- method++)
- (void)fprintf(fp, "%s: %s\n", method->name, method->description);
- }
- struct exportmethod_t *export_default(void)
- {
- return (NITEMS(exportmethods) > 0) ? &exportmethods[0] : NULL;
- }
|