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.

421 lines
10KB

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