lcdgps.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright 2005 Jeff Francis <jeff@gritch.org>
  3. *
  4. * This file is Copyright 2010 by the GPSD project
  5. * SPDX-License-Identifier: BSD-2-clause
  6. *
  7. */
  8. /*
  9. Jeff Francis
  10. jeff@gritch.org
  11. A client that passes gpsd data to lcdproc, turning your car computer
  12. into a very expensive feature-free GPS receiver ;^). Currently
  13. assumes a 4x40 LCD and writes data formatted to fit that size
  14. screen. Also displays Maidenhead grid square output for the hams among us.
  15. This program assumes that LCDd (lcdproc) is running locally on the
  16. default (13666) port. The #defines LCDDHOST and LCDDPORT can be
  17. changed to talk to a different host and TCP port.
  18. */
  19. #define LCDDHOST "localhost"
  20. #define LCDDPORT 13666
  21. #define CLIMB 3
  22. #include "../include/gpsd_config.h" /* must be before all includes */
  23. #include <arpa/inet.h>
  24. #include <errno.h>
  25. #ifdef HAVE_GETOPT_LONG
  26. #include <getopt.h> // for getopt_long()
  27. #endif
  28. #include <math.h>
  29. #include <netdb.h> /* for gethostbyname() */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #ifndef AF_UNSPEC
  35. #include <sys/socket.h>
  36. #include <sys/stat.h>
  37. #include <sys/types.h>
  38. #endif /* AF_UNSPEC */
  39. #ifndef INADDR_ANY
  40. #include <netinet/in.h>
  41. #endif /* INADDR_ANY */
  42. #include "../include/gps.h"
  43. #include "../include/gpsdclient.h"
  44. #include "../include/os_compat.h"
  45. /* Prototypes. */
  46. ssize_t sockreadline(int sockd,void *vptr,size_t maxlen);
  47. ssize_t sockwriteline(int sockd,const void *vptr,size_t n);
  48. int send_lcd(char *buf);
  49. static struct fixsource_t source;
  50. static struct gps_data_t gpsdata;
  51. static float altfactor = METERS_TO_FEET;
  52. static float speedfactor = MPS_TO_MPH;
  53. static char *altunits = "ft";
  54. static char *speedunits = "mph";
  55. double avgclimb, climb[CLIMB];
  56. /* Global socket descriptor for LCDd. */
  57. int sd;
  58. /* Read a line from a socket */
  59. ssize_t sockreadline(int sockd,void *vptr,size_t maxlen) {
  60. ssize_t n;
  61. char c,*buffer;
  62. buffer=vptr;
  63. for (n = 1; n < (ssize_t)maxlen; n++) {
  64. ssize_t rc;
  65. if ((rc=read(sockd,&c,1))==1) {
  66. *buffer++=c;
  67. if (c=='\n')
  68. break;
  69. }
  70. else if (rc==0) {
  71. if (n==1)
  72. return(0);
  73. else
  74. break;
  75. }
  76. else {
  77. if (errno==EINTR)
  78. continue;
  79. return(-1);
  80. }
  81. }
  82. *buffer=0;
  83. return(n);
  84. }
  85. /* Write a line to a socket */
  86. ssize_t sockwriteline(int sockd,const void *vptr,size_t n) {
  87. size_t nleft;
  88. const char *buffer;
  89. buffer=vptr;
  90. nleft=n;
  91. while (nleft>0) {
  92. ssize_t nwritten;
  93. if ((nwritten= write(sockd,buffer,nleft))<=0) {
  94. if (errno==EINTR)
  95. nwritten=0;
  96. else
  97. return(-1);
  98. }
  99. nleft-=nwritten;
  100. buffer+=nwritten;
  101. }
  102. return(n);
  103. }
  104. // send a command to the LCD
  105. int send_lcd(char *buf) {
  106. int res;
  107. char rcvbuf[256];
  108. size_t outlen;
  109. // Limit the size of outgoing strings. codacy hates strlen()
  110. outlen = strnlen(buf, 256);
  111. if (outlen > 255) {
  112. outlen = 256;
  113. }
  114. // send the command
  115. (void)sockwriteline(sd,buf,outlen);
  116. // TODO: check return status
  117. // read the data
  118. res=sockreadline(sd,rcvbuf,sizeof(rcvbuf)-1);
  119. // null-terminate the string before printing
  120. // rcvbuf[res-1]=0; FIX-ME: not using this at the moment...
  121. // return the result
  122. return(res);
  123. }
  124. // reset the LCD
  125. static void reset_lcd(void) {
  126. /* Initialize. In theory, we should look at what's returned, as it
  127. tells us info on the attached LCD module. TODO. */
  128. send_lcd("hello\n");
  129. /* Set up the screen */
  130. send_lcd("client_set name {GPSD test}\n");
  131. send_lcd("screen_add gpsd\n");
  132. send_lcd("widget_add gpsd one string\n");
  133. send_lcd("widget_add gpsd two string\n");
  134. send_lcd("widget_add gpsd three string\n");
  135. send_lcd("widget_add gpsd four string\n");
  136. }
  137. static enum deg_str_type deg_type = deg_dd;
  138. // This gets called once for each new sentence.
  139. static void update_lcd(struct gps_data_t *gpsdata)
  140. {
  141. char tmpbuf[255];
  142. const char *gridsquare;
  143. // Get our location in Maidenhead.
  144. gridsquare = maidenhead(gpsdata->fix.latitude,gpsdata->fix.longitude);
  145. // Fill in the latitude and longitude.
  146. if (gpsdata->fix.mode >= MODE_2D) {
  147. int track;
  148. char *s;
  149. s = deg_to_str(deg_type, gpsdata->fix.latitude);
  150. snprintf(tmpbuf, sizeof(tmpbuf) - 1,
  151. "widget_set gpsd one 1 1 {Lat: %s %c}\n", s,
  152. (gpsdata->fix.latitude < 0) ? 'S' : 'N');
  153. send_lcd(tmpbuf);
  154. s = deg_to_str(deg_type, gpsdata->fix.longitude);
  155. snprintf(tmpbuf, sizeof(tmpbuf) - 1,
  156. "widget_set gpsd two 1 2 {Lon: %s %c}\n", s,
  157. (gpsdata->fix.longitude < 0) ? 'W' : 'E');
  158. send_lcd(tmpbuf);
  159. /* As a pilot, a heading of "0" gives me the heebie-jeebies (ie, 0
  160. == "invalid heading data", 360 == "North"). */
  161. track=(int)(gpsdata->fix.track);
  162. if (track == 0) track = 360;
  163. snprintf(tmpbuf, sizeof(tmpbuf) - 1,
  164. "widget_set gpsd three 1 3 {%.1f %s %d deg}\n",
  165. gpsdata->fix.speed*speedfactor, speedunits,
  166. track);
  167. send_lcd(tmpbuf);
  168. } else {
  169. send_lcd("widget_set gpsd one 1 1 {Lat: n/a}\n");
  170. send_lcd("widget_set gpsd two 1 2 {Lon: n/a}\n");
  171. send_lcd("widget_set gpsd three 1 3 {n/a}\n");
  172. }
  173. // Fill in the altitude and fix status.
  174. if (gpsdata->fix.mode == MODE_3D) {
  175. int n;
  176. for(n=0;n<CLIMB-2;n++) climb[n]=climb[n+1];
  177. climb[CLIMB-1]=gpsdata->fix.climb;
  178. avgclimb=0.0;
  179. for(n=0;n<CLIMB;n++) avgclimb+=climb[n];
  180. avgclimb/=CLIMB;
  181. snprintf(tmpbuf, sizeof(tmpbuf) - 1,
  182. "widget_set gpsd four 1 4 {%d %s %s %d fpm }\n",
  183. (int)(gpsdata->fix.altMSL * altfactor), altunits,
  184. gridsquare, (int)(avgclimb * METERS_TO_FEET * 60));
  185. } else {
  186. snprintf(tmpbuf, sizeof(tmpbuf) - 1, "widget_set gpsd four 1 4 {n/a}\n");
  187. }
  188. send_lcd(tmpbuf);
  189. }
  190. static void usage( char *prog)
  191. {
  192. (void)fprintf(stderr,
  193. "Usage: %s [OPTIONS] [server[:port:[device]]]\n\n"
  194. " -? Show this help, then exit\n"
  195. #ifdef HAVE_GETOPT_LONG
  196. " --sleep Sleep for 10 seconds before starting\n"
  197. " --help Show this help, then exit\n"
  198. " --version Show version, then exit\n"
  199. #endif
  200. " -h Show this help, then exit\n"
  201. " -j Turn on anti-jitter buffering\n"
  202. " -l {d|m|s} Select lat/lon format\n"
  203. " d = DD.dddddd (default)\n"
  204. " m = DD MM.mmmm'\n"
  205. " s = DD MM' SS.sss\"\n"
  206. " -s Sleep for 10 seconds before starting\n"
  207. " -u {i|m|m} Select Units\n"
  208. " i = Imperial (default)\n"
  209. " n = Nautical\n"
  210. " m = Metric\n"
  211. " -V Show version, then exit\n"
  212. , prog);
  213. exit(EXIT_FAILURE);
  214. }
  215. int main(int argc, char *argv[])
  216. {
  217. int rc;
  218. struct sockaddr_in localAddr, servAddr;
  219. struct hostent *h;
  220. const char *optstring = "?hl:su:V";
  221. int n;
  222. #ifdef HAVE_GETOPT_LONG
  223. int option_index = 0;
  224. static struct option long_options[] = {
  225. {"sleep", no_argument, NULL, 's'},
  226. {"help", no_argument, NULL, 'h'},
  227. {"version", no_argument, NULL, 'V' },
  228. {NULL, 0, NULL, 0},
  229. };
  230. #endif
  231. for(n=0;n<CLIMB;n++) climb[n]=0.0;
  232. switch (gpsd_units()) {
  233. case imperial:
  234. altfactor = METERS_TO_FEET;
  235. altunits = "ft";
  236. speedfactor = MPS_TO_MPH;
  237. speedunits = "mph";
  238. break;
  239. case nautical:
  240. altfactor = METERS_TO_FEET;
  241. altunits = "ft";
  242. speedfactor = MPS_TO_KNOTS;
  243. speedunits = "knots";
  244. break;
  245. case metric:
  246. altfactor = 1;
  247. altunits = "m";
  248. speedfactor = MPS_TO_KPH;
  249. speedunits = "km/h";
  250. break;
  251. default:
  252. /* leave the default alone */
  253. break;
  254. }
  255. /* Process the options. Print help if requested. */
  256. while (1) {
  257. int ch;
  258. #ifdef HAVE_GETOPT_LONG
  259. ch = getopt_long(argc, argv, optstring, long_options, &option_index);
  260. #else
  261. ch = getopt(argc, argv, optstring);
  262. #endif
  263. if (ch == -1) {
  264. break;
  265. }
  266. switch (ch) {
  267. case '?':
  268. case 'h':
  269. default:
  270. usage(argv[0]);
  271. break;
  272. case 'l':
  273. switch (optarg[0]) {
  274. case 'd':
  275. deg_type = deg_dd;
  276. continue;
  277. case 'm':
  278. deg_type = deg_ddmm;
  279. continue;
  280. case 's':
  281. deg_type = deg_ddmmss;
  282. continue;
  283. default:
  284. (void)fprintf(stderr, "Unknown -l argument: %s\n", optarg);
  285. }
  286. break;
  287. case 's':
  288. sleep(10);
  289. continue;
  290. case 'u':
  291. switch ( optarg[0] ) {
  292. case 'i':
  293. altfactor = METERS_TO_FEET;
  294. altunits = "ft";
  295. speedfactor = MPS_TO_MPH;
  296. speedunits = "mph";
  297. continue;
  298. case 'n':
  299. altfactor = METERS_TO_FEET;
  300. altunits = "ft";
  301. speedfactor = MPS_TO_KNOTS;
  302. speedunits = "knots";
  303. continue;
  304. case 'm':
  305. altfactor = 1;
  306. altunits = "m";
  307. speedfactor = MPS_TO_KPH;
  308. speedunits = "km/h";
  309. continue;
  310. default:
  311. break;
  312. }
  313. (void)fprintf(stderr, "Unknown -u argument: %s\n", optarg);
  314. break;
  315. case 'V':
  316. (void)fprintf(stderr, "lcdgps revision " REVISION "\n");
  317. exit(EXIT_SUCCESS);
  318. }
  319. }
  320. /* Grok the server, port, and device. */
  321. if (optind < argc) {
  322. gpsd_source_spec(argv[optind], &source);
  323. } else
  324. gpsd_source_spec(NULL, &source);
  325. /* Daemonize... */
  326. if (os_daemon(0, 0) != 0)
  327. (void)fprintf(stderr,
  328. "lcdgps: daemonization failed: %s\n",
  329. strerror(errno));
  330. /* Open the stream to gpsd. */
  331. if (gps_open(source.server, source.port, &gpsdata) != 0) {
  332. (void)fprintf( stderr,
  333. "lcdgps: no gpsd running or network error: %d, %s\n",
  334. errno, gps_errstr(errno));
  335. exit(EXIT_FAILURE);
  336. }
  337. /* Connect to LCDd */
  338. h = gethostbyname(LCDDHOST);
  339. if (h==NULL) {
  340. printf("%s: unknown host '%s'\n",argv[0],LCDDHOST);
  341. exit(EXIT_FAILURE);
  342. }
  343. servAddr.sin_family = h->h_addrtype;
  344. memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
  345. servAddr.sin_port = htons(LCDDPORT);
  346. /* create socket */
  347. sd = socket(AF_INET, SOCK_STREAM, 0);
  348. if (BAD_SOCKET(sd)) {
  349. perror("cannot open socket ");
  350. exit(EXIT_FAILURE);
  351. }
  352. /* bind any port number */
  353. localAddr.sin_family = AF_INET;
  354. localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  355. localAddr.sin_port = htons(0);
  356. /* coverity[uninit_use_in_call] */
  357. rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
  358. if (rc == -1) {
  359. printf("%s: cannot bind port TCP %d\n",argv[0],LCDDPORT);
  360. perror("error ");
  361. exit(EXIT_FAILURE);
  362. }
  363. /* connect to server */
  364. /* coverity[uninit_use_in_call] */
  365. rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
  366. if (rc == -1) {
  367. perror("cannot connect ");
  368. exit(EXIT_FAILURE);
  369. }
  370. /* Do the initial field label setup. */
  371. reset_lcd();
  372. /* Here's where updates go. */
  373. unsigned int flags = WATCH_ENABLE;
  374. if (source.device != NULL)
  375. flags |= WATCH_DEVICE;
  376. (void)gps_stream(&gpsdata, flags, source.device);
  377. for (;;) { /* heart of the client */
  378. if (!gps_waiting(&gpsdata, 50000000)) {
  379. (void)fprintf(stderr, "lcdgps: error while waiting\n");
  380. exit(EXIT_FAILURE);
  381. } else {
  382. (void)gps_read(&gpsdata, NULL, 0);
  383. update_lcd(&gpsdata);
  384. }
  385. }
  386. }
  387. // vim: set expandtab shiftwidth=4