Devuan fork of gpsd
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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /*
  2. * Copyright (c) 2005 Jeff Francis <jeff@gritch.org>
  3. *
  4. * This file is Copyright (c)2010-2018 by the GPSD project
  5. * SPDX-License-Identifier: BSD-2-clause
  6. */
  7. /*
  8. Jeff Francis
  9. jeff@gritch.org
  10. Kind of a curses version of xgps for use with gpsd.
  11. */
  12. /*
  13. * The True North compass fails with current gpsd versions for reasons
  14. * the dev team has been unable to diagnose due to not having test hardware.
  15. * The support for it is conditioned out in order to simplify moving
  16. * to the new JSON-based protocol and reduce startup time.
  17. */
  18. #undef TRUENORTH
  19. /* ==================================================================
  20. These #defines should be modified if changing the number of fields
  21. to be displayed.
  22. ================================================================== */
  23. /* This defines how much overhead is contained in the 'datawin' window
  24. (eg, box around the window takes two lines). */
  25. #define DATAWIN_OVERHEAD 2
  26. /* This defines how much overhead is contained in the 'satellites'
  27. window (eg, box around the window takes two lines, plus the column
  28. headers take another line). */
  29. #define SATWIN_OVERHEAD 2
  30. /* This is how many display fields are output in the 'datawin' window
  31. when in GPS mode. Change this value if you add or remove fields
  32. from the 'datawin' window for the GPS mode. */
  33. #define DATAWIN_GPS_FIELDS 8
  34. /* Count of optional fields that we'll display if we have the room. */
  35. #define DATAWIN_OPTIONAL_FIELDS 7
  36. /* This is how many display fields are output in the 'datawin' window
  37. when in COMPASS mode. Change this value if you add or remove fields
  38. from the 'datawin' window for the COMPASS mode. */
  39. #define DATAWIN_COMPASS_FIELDS 6
  40. /* This is how far over in the 'datawin' window to indent the field
  41. descriptions. */
  42. #define DATAWIN_DESC_OFFSET 2
  43. /* This is how far over in the 'datawin' window to indent the field
  44. values. */
  45. #define DATAWIN_VALUE_OFFSET 17
  46. /* This is the width of the 'datawin' window. It's recommended to
  47. keep DATAWIN_WIDTH + SATELLITES_WIDTH <= 80 so it'll fit on a
  48. "standard" 80x24 screen. */
  49. #define DATAWIN_WIDTH 45
  50. /* This is the width of the 'satellites' window. It's recommended to
  51. keep DATAWIN_WIDTH + SATELLITES_WIDTH <= 80 so it'll fit on a
  52. "standard" 80x24 screen. */
  53. #define SATELLITES_WIDTH 35
  54. /* ================================================================
  55. You shouldn't have to modify any #define values below this line.
  56. ================================================================ */
  57. /* This is the minimum ysize we'll accept for the 'datawin' window in
  58. GPS mode. */
  59. #define MIN_GPS_DATAWIN_YSIZE (DATAWIN_GPS_FIELDS + DATAWIN_OVERHEAD)
  60. /* And the maximum ysize we'll try to use */
  61. #define MAX_GPS_DATAWIN_YSIZE (DATAWIN_GPS_FIELDS + DATAWIN_OPTIONAL_FIELDS + DATAWIN_OVERHEAD)
  62. /* This is the minimum ysize we'll accept for the 'datawin' window in
  63. COMPASS mode. */
  64. #define MIN_COMPASS_DATAWIN_YSIZE (DATAWIN_COMPASS_FIELDS + DATAWIN_OVERHEAD)
  65. /* This is the maximum number of satellites gpsd can track. */
  66. #define MAX_POSSIBLE_SATS (MAXCHANNELS - 2)
  67. /* This is the maximum ysize we need for the 'satellites' window. */
  68. #define MAX_SATWIN_SIZE (MAX_POSSIBLE_SATS + SATWIN_OVERHEAD)
  69. /* Minimum xsize to display 3rd window with DOPs, etc. */
  70. #define MIN_ERRWIN_SIZE 100
  71. #include "gpsd_config.h" /* must be before all includes */
  72. #include <ctype.h>
  73. #include <curses.h>
  74. #include <errno.h>
  75. #include <math.h>
  76. #include <signal.h>
  77. #include <stdbool.h>
  78. #include <stdio.h>
  79. #include <stdlib.h>
  80. #include <string.h>
  81. #include <time.h>
  82. #include <unistd.h>
  83. #include "gps.h"
  84. #include "gps_json.h" /* for GPS_JSON_RESPONSE_MAX */
  85. #include "compiler.h" /* for UNUSED */
  86. #include "gpsdclient.h"
  87. #include "revision.h"
  88. #include "os_compat.h"
  89. #include "timespec.h"
  90. static struct gps_data_t gpsdata;
  91. static time_t status_timer; /* Time of last state change. */
  92. static int state = 0; /* or MODE_NO_FIX=1, MODE_2D=2, MODE_3D=3 */
  93. static float altfactor = METERS_TO_FEET;
  94. static float speedfactor = MPS_TO_MPH;
  95. static char *altunits = "ft";
  96. static char *speedunits = "mph";
  97. static struct fixsource_t source;
  98. static int debug;
  99. static WINDOW *datawin, *satellites, *messages;
  100. static bool raw_flag = false;
  101. static bool show_ecefs = false; /* taller screen, show ECEFs */
  102. static bool show_more_dops = false; /* tall screen, show more DOPs */
  103. static bool silent_flag = false;
  104. static bool magnetic_flag = false;
  105. static int window_ysize = 0;
  106. static int display_sats = 0; /* number of rows of sats to display */
  107. #ifdef TRUENORTH
  108. static bool compass_flag = false;
  109. #endif /* TRUENORTH */
  110. /* pseudo-signals indicating reason for termination */
  111. #define CGPS_QUIT 0 /* voluntary termination */
  112. #define GPS_GONE -1 /* GPS device went away */
  113. #define GPS_ERROR -2 /* low-level failure in GPS read */
  114. #define GPS_TIMEOUT -3 /* low-level failure in GPS waiting */
  115. /* range test an int, return 4 chars + NUL */
  116. static const char *int_to_str(int val, int min, int max)
  117. {
  118. static char buf[20];
  119. if (val < min || val > max) {
  120. return " n/a";
  121. }
  122. (void)snprintf(buf, sizeof(buf), "%4d", val);
  123. return buf;
  124. }
  125. /* range test an double, to tenths, return 5 chars + NUL */
  126. static const char *tenth_to_str(double val, double min, double max)
  127. {
  128. static char buf[20];
  129. if (0 == isfinite(val) ||
  130. val < min ||
  131. val > max) {
  132. return " n/a";
  133. }
  134. (void)snprintf(buf, sizeof(buf), "%5.1f", val);
  135. return buf;
  136. }
  137. /* format a DOP into a 5 char string, handle NAN, INFINITE */
  138. static char *dop_to_str(double dop)
  139. {
  140. static char buf[20];
  141. if (isfinite(dop) == 0) {
  142. return " n/a ";
  143. }
  144. (void)snprintf(buf, sizeof(buf), "%5.2f", dop);
  145. return buf;
  146. }
  147. /* format an EP into a string, handle NAN, INFINITE */
  148. static char *ep_to_str(double ep, double factor, char *units)
  149. {
  150. static char buf[20];
  151. double val;
  152. if (isfinite(ep) == 0) {
  153. return " n/a ";
  154. }
  155. /* somehow these go negative now and then... */
  156. val = fabs(ep * factor);
  157. if ( 100 <= val ) {
  158. (void)snprintf(buf, sizeof(buf), "+/-%5d %.3s", (int)val, units);
  159. } else {
  160. (void)snprintf(buf, sizeof(buf), "+/-%5.1f %.3s", val, units);
  161. }
  162. return buf;
  163. }
  164. /* format an ECEF p and v into a string, handle NAN, INFINITE */
  165. static char *ecef_to_str(double pos, double vel, double factor, char *units)
  166. {
  167. static char buf[128];
  168. if (isfinite(pos) == 0) {
  169. if (isfinite(vel) == 0) {
  170. return " n/a n/a ";
  171. } else {
  172. (void)snprintf(buf, sizeof(buf), " n/a %8.3f%.4s/s",
  173. vel * factor, units);
  174. }
  175. } else {
  176. (void)snprintf(buf, sizeof(buf), "% 14.3f%.4s %8.3f%.4s/s",
  177. pos * factor, units, vel * factor, units);
  178. }
  179. return buf;
  180. }
  181. /* Function to call when we're all done. Does a bit of clean-up. */
  182. static void die(int sig)
  183. {
  184. if (!isendwin())
  185. {
  186. /* Move the cursor to the bottom left corner. */
  187. (void)mvcur(0, COLS - 1, LINES - 1, 0);
  188. /* Put input attributes back the way they were. */
  189. (void)echo();
  190. /* Done with curses. */
  191. (void)endwin();
  192. }
  193. /* We're done talking to gpsd. */
  194. (void)gps_close(&gpsdata);
  195. switch (sig) {
  196. case CGPS_QUIT:
  197. break;
  198. case GPS_GONE:
  199. (void)fprintf(stderr, "cgps: GPS hung up.\n");
  200. break;
  201. case GPS_ERROR:
  202. (void)fprintf(stderr, "cgps: GPS read returned error\n");
  203. break;
  204. case GPS_TIMEOUT:
  205. (void)fprintf(stderr, "cgps: GPS timeout\n");
  206. break;
  207. default:
  208. (void)fprintf(stderr, "cgps: caught signal %d\n", sig);
  209. break;
  210. }
  211. /* Bye! */
  212. exit(EXIT_SUCCESS);
  213. }
  214. static enum deg_str_type deg_type = deg_dd;
  215. static void windowsetup(void)
  216. /* initialize curses and set up screen windows */
  217. {
  218. /* Set the window sizes per the following criteria:
  219. *
  220. * 1. Set the window size to display the maximum number of
  221. * satellites possible, but not more than can be fit in a
  222. * window the size of the GPS report window. We have to set
  223. * the limit that way because MAXCHANNELS has been made large
  224. * in order to prepare for survey-grade receivers..
  225. *
  226. * 2. If the screen size will not allow for the full complement of
  227. * satellites to be displayed, set the windows sizes smaller, but
  228. * not smaller than the number of lines necessary to display all of
  229. * the fields in the 'datawin'. The list of displayed satellites
  230. * will be truncated to fit the available window size. (TODO: If
  231. * the satellite list is truncated, omit the satellites not used to
  232. * obtain the current fix.)
  233. *
  234. * 3. If the screen is tall enough to display all possible
  235. * satellites (MAXCHANNELS - 2) with space still left at the bottom,
  236. * add a window at the bottom in which to scroll raw gpsd data.
  237. *
  238. * 4. If the screen is tall enough to display extra data, expand
  239. * data window down to show DOPs, ECEFs, etc.
  240. */
  241. int xsize, ysize;
  242. /* Fire up curses. */
  243. (void)initscr();
  244. (void)noecho();
  245. getmaxyx(stdscr, ysize, xsize);
  246. /* turn off cursor */
  247. curs_set(0);
  248. #ifdef TRUENORTH
  249. if (compass_flag) {
  250. if (ysize == MIN_COMPASS_DATAWIN_YSIZE) {
  251. raw_flag = false;
  252. window_ysize = MIN_COMPASS_DATAWIN_YSIZE;
  253. } else if (ysize > MIN_COMPASS_DATAWIN_YSIZE) {
  254. raw_flag = true;
  255. window_ysize = MIN_COMPASS_DATAWIN_YSIZE;
  256. } else {
  257. (void)mvprintw(0, 0,
  258. "Your screen must be at least 80x%d to run cgps.",
  259. MIN_COMPASS_DATAWIN_YSIZE);
  260. (void)refresh();
  261. (void)sleep(5);
  262. die(0);
  263. }
  264. } else
  265. #endif /* TRUENORTH */
  266. {
  267. if (ysize > MAX_GPS_DATAWIN_YSIZE + 10) {
  268. raw_flag = true;
  269. show_ecefs = true;
  270. show_more_dops = true;
  271. window_ysize = MAX_GPS_DATAWIN_YSIZE + 7;
  272. } else if (ysize > MAX_GPS_DATAWIN_YSIZE + 6) {
  273. raw_flag = true;
  274. show_ecefs = false;
  275. show_more_dops = true;
  276. window_ysize = MAX_GPS_DATAWIN_YSIZE + 4;
  277. } else if (ysize > MAX_GPS_DATAWIN_YSIZE) {
  278. raw_flag = true;
  279. show_ecefs = false;
  280. show_more_dops = false;
  281. window_ysize = MAX_GPS_DATAWIN_YSIZE;
  282. } else if (ysize == MAX_GPS_DATAWIN_YSIZE) {
  283. raw_flag = false;
  284. show_ecefs = false;
  285. show_more_dops = false;
  286. window_ysize = MAX_GPS_DATAWIN_YSIZE;
  287. } else if (ysize > MIN_GPS_DATAWIN_YSIZE) {
  288. raw_flag = true;
  289. show_ecefs = false;
  290. show_more_dops = false;
  291. window_ysize = MIN_GPS_DATAWIN_YSIZE;
  292. } else if (ysize == MIN_GPS_DATAWIN_YSIZE) {
  293. raw_flag = false;
  294. show_ecefs = false;
  295. show_more_dops = false;
  296. window_ysize = MIN_GPS_DATAWIN_YSIZE;
  297. } else {
  298. (void)mvprintw(0, 0,
  299. "Your screen must be at least 80x%d to run cgps.",
  300. MIN_GPS_DATAWIN_YSIZE);
  301. (void)refresh();
  302. (void)sleep(5);
  303. die(0);
  304. }
  305. display_sats = window_ysize - SATWIN_OVERHEAD - (int)raw_flag;
  306. }
  307. #ifdef TRUENORTH
  308. /* Set up the screen for either a compass or a gps receiver. */
  309. if (compass_flag) {
  310. /* We're a compass, set up accordingly. */
  311. int row = 1;
  312. datawin = newwin(window_ysize, DATAWIN_WIDTH, 0, 0);
  313. (void)nodelay(datawin, (bool) TRUE);
  314. if (raw_flag) {
  315. messages = newwin(0, 0, window_ysize, 0);
  316. (void)scrollok(messages, true);
  317. (void)wsetscrreg(messages, 0, ysize - (window_ysize));
  318. }
  319. (void)refresh();
  320. /* Do the initial field label setup. */
  321. (void)mvwprintw(datawin, row++, DATAWIN_DESC_OFFSET, "Time:");
  322. /* FIXME: prolly should be heading... */
  323. (void)mvwprintw(datawin, row++, DATAWIN_DESC_OFFSET, "Track:");
  324. (void)mvwprintw(datawin, row++, DATAWIN_DESC_OFFSET, "Pitch:");
  325. (void)mvwprintw(datawin, row++, DATAWIN_DESC_OFFSET, "Roll:");
  326. (void)mvwprintw(datawin, row++, DATAWIN_DESC_OFFSET, "Dip:");
  327. (void)mvwprintw(datawin, row++, DATAWIN_DESC_OFFSET, "Rcvr Type:");
  328. (void)wborder(datawin, 0, 0, 0, 0, 0, 0, 0, 0);
  329. } else
  330. #endif /* TRUENORTH */
  331. {
  332. /* We're a GPS, set up accordingly. */
  333. int row = 1;
  334. datawin = newwin(window_ysize, DATAWIN_WIDTH, 0, 0);
  335. satellites =
  336. newwin(window_ysize, SATELLITES_WIDTH, 0, DATAWIN_WIDTH);
  337. (void)nodelay(datawin, (bool) TRUE);
  338. if (raw_flag) {
  339. messages =
  340. newwin(ysize - (window_ysize), xsize, window_ysize, 0);
  341. (void)scrollok(messages, true);
  342. (void)wsetscrreg(messages, 0, ysize - (window_ysize));
  343. }
  344. (void)refresh();
  345. /* Do the initial field label setup. */
  346. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Time:");
  347. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Latitude:");
  348. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Longitude:");
  349. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Alt (HAE, MSL):");
  350. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Speed:");
  351. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Track ");
  352. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Climb:");
  353. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET, "Status:");
  354. /* Note that the following fields are exceptions to the
  355. * sizing rule. The minimum window size does not include these
  356. * fields, if the window is too small, they get excluded. This
  357. * may or may not change if/when the output for these fields is
  358. * fixed and/or people request their permanance. They're only
  359. * there in the first place because I arbitrarily thought they
  360. * sounded interesting. ;^) */
  361. if (window_ysize >= MAX_GPS_DATAWIN_YSIZE) {
  362. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  363. "Long Err (XDOP, EPX):");
  364. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  365. "Lat Err (YDOP, EPY):");
  366. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  367. "Alt Err (VDOP, EPV):");
  368. if (show_more_dops) {
  369. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  370. "2D Err (HDOP, CEP):");
  371. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  372. "3D Err (PDOP, SEP):");
  373. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  374. "Time Err (TDOP):");
  375. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  376. "Geo Err (GDOP):");
  377. }
  378. if (show_ecefs) {
  379. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  380. "ECEF X, VX:");
  381. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  382. "ECEF Y, VY:");
  383. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  384. "ECEF Z, VZ:");
  385. }
  386. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  387. "Speed Err (EPS):");
  388. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  389. "Track Err (EPD):");
  390. /* it's actually esr that thought *these* were interesting */
  391. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  392. "Time offset:");
  393. (void)mvwaddstr(datawin, row++, DATAWIN_DESC_OFFSET,
  394. "Grid Square:");
  395. }
  396. (void)wborder(datawin, 0, 0, 0, 0, 0, 0, 0, 0);
  397. /* PRN is not unique for all GNSS systems.
  398. * Each GNSS (GPS, GALILEO, BeiDou, etc.) numbers their PRN from 1.
  399. * What we really have here is USI, Universal Sat ID
  400. * The USI for each GNSS satellite is unique, starting at 1.
  401. * Not all GPS receivers compute the USI the same way. YMMV
  402. *
  403. * Javad (GREIS) GPS receivers compute USI this way:
  404. * GPS is USI 1-37, GLONASS 38-70, GALILEO 71-119, SBAS 120-142,
  405. * QZSS 193-197, BeiDou 211-247
  406. *
  407. * Geostar GPS receivers compute USI this way:
  408. * GPS is USI 1 to 32, SBAS is 33 to 64, GLONASS is 65 to 96 */
  409. (void)mvwaddstr(satellites, 1, 1,
  410. " PRN Elev Azim SNR Use ");
  411. (void)wborder(satellites, 0, 0, 0, 0, 0, 0, 0, 0);
  412. }
  413. }
  414. static void resize(int sig UNUSED)
  415. /* cope with terminal resize */
  416. {
  417. if (!isendwin())
  418. {
  419. (void)endwin();
  420. windowsetup();
  421. }
  422. }
  423. #ifdef TRUENORTH
  424. /* This gets called once for each new compass sentence. */
  425. static void update_compass_panel(struct gps_data_t *gpsdata)
  426. {
  427. char scr[128];
  428. int row = 1;
  429. /* Print time/date. */
  430. if (0 < gpsdata->fix.time.tv_sec) {
  431. (void)timespec_to_iso8601(gpsdata->fix.time, scr, sizeof(scr));
  432. } else
  433. (void)strlcpy(scr, "n/a", sizeof(scr));
  434. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
  435. /* Fill in the track. */
  436. /* FIXME: prolly should be heading... */
  437. if (isfinite(gpsdata->fix.track) != 0) {
  438. (void)snprintf(scr, sizeof(scr), "%.1f degrees", gpsdata->fix.track);
  439. } else
  440. (void)strlcpy(scr, "n/a", sizeof(scr));
  441. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
  442. /* Fill in the climb. */
  443. if (isfinite(gpsdata->fix.climb) != 0) {
  444. (void)snprintf(scr, sizeof(scr), "%.2f", gpsdata->fix.climb);
  445. } else
  446. (void)strlcpy(scr, "n/a", sizeof(scr));
  447. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
  448. /* Fill in the speed. */
  449. if (isfinite(gpsdata->fix.speed) != 0)
  450. (void)snprintf(scr, sizeof(scr), "%.2f", gpsdata->fix.speed);
  451. else
  452. (void)strlcpy(scr, "n/a", sizeof(scr));
  453. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
  454. /* Fill in the altitude. */
  455. if (isfinite(gpsdata->fix.altHAE) != 0)
  456. (void)snprintf(scr, sizeof(scr), "%.3f", gpsdata->fix.altHAE);
  457. else
  458. (void)strlcpy(scr, "n/a", sizeof(scr));
  459. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
  460. /* When we need to fill in receiver type again, do it here. */
  461. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
  462. (void)wrefresh(datawin);
  463. if (raw_flag && !silent_flag) {
  464. /* Be quiet if the user requests silence. */
  465. (void)waddstr(messages, message);
  466. (void)wrefresh(messages);
  467. }
  468. }
  469. #endif /* TRUENORTH */
  470. /* sort the skyviews
  471. * Used = Y first, then used = N
  472. * then sort by PRN
  473. */
  474. static int sat_cmp(const void *p1, const void *p2)
  475. {
  476. if ( ((struct satellite_t*)p2)->used - ((struct satellite_t*)p1)->used ) {
  477. return ((struct satellite_t*)p2)->used - ((struct satellite_t*)p1)->used;
  478. }
  479. return ((struct satellite_t*)p1)->PRN - ((struct satellite_t*)p2)->PRN;
  480. }
  481. static void update_gps_panel(struct gps_data_t *gpsdata, char *message)
  482. /* This gets called once for each new GPS sentence. */
  483. {
  484. int newstate;
  485. char scr[80];
  486. /* This is for the satellite status display. Originally lifted from
  487. * xgps.c. Note that the satellite list may be truncated based on
  488. * available screen size, or may only show satellites used for the
  489. * fix. */
  490. (void)mvwprintw(satellites, 0, 19, "Seen %2d/Used %2d",
  491. gpsdata->satellites_visible,
  492. gpsdata->satellites_used);
  493. if (0 != (VERSION_SET &gpsdata->set)) {
  494. /* got version, check it */
  495. /* FIXME: expected API version not available ? */
  496. if (0 != strcmp(gpsdata->version.release, VERSION)) {
  497. (void)fprintf(stderr, "cgps: WARNING gpsd release %s, API: %d.%d, "
  498. "expected %s ",
  499. gpsdata->version.release,
  500. gpsdata->version.proto_major,
  501. gpsdata->version.proto_minor,
  502. VERSION);
  503. sleep(2);
  504. }
  505. }
  506. if (gpsdata->satellites_visible != 0) {
  507. int sat_no;
  508. int loop_end = (display_sats < gpsdata->satellites_visible) ? \
  509. display_sats : gpsdata->satellites_visible;
  510. qsort( gpsdata->skyview, gpsdata->satellites_visible,
  511. sizeof( struct satellite_t), sat_cmp);
  512. /* displayed all sats that fit, maybe all of them */
  513. for (sat_no = 0; sat_no < loop_end; sat_no++) {
  514. int column = 1; /* column to write to */
  515. char *gnssid;
  516. char sigid[2] = " ";
  517. char health = ' ';
  518. if ( 0 == gpsdata->skyview[sat_no].svid) {
  519. gnssid = " ";
  520. } else {
  521. switch (gpsdata->skyview[sat_no].gnssid) {
  522. default:
  523. gnssid = " ";
  524. break;
  525. case 0:
  526. gnssid = "GP"; /* GPS */
  527. break;
  528. case 1:
  529. gnssid = "SB"; /* GPS */
  530. break;
  531. case 2:
  532. gnssid = "GA"; /* GALILEO */
  533. break;
  534. case 3:
  535. gnssid = "BD"; /* BeiDou */
  536. break;
  537. case 4:
  538. gnssid = "IM"; /* IMES */
  539. break;
  540. case 5:
  541. gnssid = "QZ"; /* QZSS */
  542. break;
  543. case 6:
  544. gnssid = "GL"; /* GLONASS */
  545. break;
  546. }
  547. if (1 < gpsdata->skyview[sat_no].sigid &&
  548. 8 > gpsdata->skyview[sat_no].sigid) {
  549. /* Do not display L1, or missing */
  550. /* max is 8 */
  551. sigid[0] = '0' + gpsdata->skyview[sat_no].sigid;
  552. sigid[1] = '\0';
  553. }
  554. }
  555. (void)mvwaddstr(satellites, sat_no + 2, column, gnssid);
  556. column += 2;
  557. (void)mvwaddstr(satellites, sat_no + 2, column, sigid);
  558. /* no GPS uses PRN 0, NMEA 4.0 here
  559. * NMEA 4.0 uses 1-437 */
  560. column += 2;
  561. (void)mvwaddstr(satellites, sat_no + 2, column,
  562. int_to_str(gpsdata->skyview[sat_no].PRN,
  563. 1, 438));
  564. column += 5;
  565. (void)mvwaddstr(satellites, sat_no + 2, column,
  566. tenth_to_str(gpsdata->skyview[sat_no].elevation,
  567. -90.0, 90.0));
  568. column += 7;
  569. (void)mvwaddstr(satellites, sat_no + 2, column,
  570. tenth_to_str(gpsdata->skyview[sat_no].azimuth,
  571. 0.0, 359.0));
  572. column += 6;
  573. (void)mvwaddstr(satellites, sat_no + 2, column,
  574. tenth_to_str(gpsdata->skyview[sat_no].ss,
  575. 0.0, 254.0));
  576. column += 6;
  577. if (SAT_HEALTH_BAD == gpsdata->skyview[sat_no].health) {
  578. /* only mark known unhealthy */
  579. health = 'u';
  580. }
  581. (void)mvwprintw(satellites, sat_no + 2, column, " %c%c ",
  582. health,
  583. gpsdata->skyview[sat_no].used ? 'Y' : 'N');
  584. }
  585. /* Display More... ? */
  586. if (sat_no < gpsdata->satellites_visible) {
  587. /* Too many sats to show them all, tell the user. */
  588. (void)mvwprintw(satellites, sat_no + 2, 1, "%s", "More...");
  589. } else {
  590. /* Clear old data from the unused lines at bottom. */
  591. for ( ; sat_no < display_sats; sat_no++) {
  592. (void)mvwprintw(satellites, sat_no + 2, 1, "%-*s",
  593. SATELLITES_WIDTH - 3, "");
  594. }
  595. /* remove More... */
  596. (void)mvwhline(satellites, sat_no + 2, 1, 0, 8);
  597. }
  598. } else {
  599. int sat_no = 0;
  600. /* no sats, clear screen */
  601. for ( ; sat_no < display_sats; sat_no++) {
  602. (void)mvwprintw(satellites, sat_no + 2, 1, "%-*s",
  603. SATELLITES_WIDTH - 3, "");
  604. }
  605. /* remove More... */
  606. (void)mvwhline(satellites, sat_no + 2, 1, 0, 8);
  607. }
  608. /* turn off cursor */
  609. curs_set(0);
  610. /* Print time/date. */
  611. if (0 < gpsdata->fix.time.tv_sec) {
  612. (void)timespec_to_iso8601(gpsdata->fix.time, scr, sizeof(scr));
  613. } else
  614. (void)strlcpy(scr, " n/a", sizeof(scr));
  615. (void)mvwprintw(datawin, 1, DATAWIN_VALUE_OFFSET, "%-*s", 26, scr);
  616. /* Fill in the latitude. */
  617. if (gpsdata->fix.mode >= MODE_2D) {
  618. deg_to_str2(deg_type, gpsdata->fix.latitude,
  619. scr, sizeof(scr), " N", " S");
  620. } else
  621. (void)strlcpy(scr, "n/a", sizeof(scr));
  622. (void)mvwprintw(datawin, 2, DATAWIN_VALUE_OFFSET, " %-*s", 25, scr);
  623. /* Fill in the longitude. */
  624. if (gpsdata->fix.mode >= MODE_2D) {
  625. deg_to_str2(deg_type, gpsdata->fix.longitude,
  626. scr, sizeof(scr), " E", " W");
  627. } else
  628. (void)strlcpy(scr, "n/a", sizeof(scr));
  629. (void)mvwprintw(datawin, 3, DATAWIN_VALUE_OFFSET, " %-*s", 25, scr);
  630. /* Fill in the altitudes. */
  631. if (gpsdata->fix.mode >= MODE_3D) {
  632. char buf1[20], buf2[20];
  633. if (0 != isfinite(gpsdata->fix.altHAE))
  634. (void)snprintf(buf1, sizeof(buf1), "%11.3f,",
  635. gpsdata->fix.altHAE * altfactor);
  636. else
  637. (void)strlcpy(buf1, " n/a,", sizeof(buf1));
  638. if (0 != isfinite(gpsdata->fix.altMSL))
  639. (void)snprintf(buf2, sizeof(buf2), "%11.3f ",
  640. gpsdata->fix.altMSL * altfactor);
  641. else
  642. (void)strlcpy(buf2, " n/a ", sizeof(buf2));
  643. (void)strlcpy(scr, buf1, sizeof(scr));
  644. (void)strlcat(scr, buf2, sizeof(scr));
  645. (void)strlcat(scr, altunits, sizeof(scr));
  646. } else {
  647. (void)strlcpy(scr, " n/a, n/a ", sizeof(scr));
  648. }
  649. (void)mvwprintw(datawin, 4, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
  650. /* Fill in the speed. */
  651. if (isfinite(gpsdata->fix.speed) != 0)
  652. (void)snprintf(scr, sizeof(scr), "%8.2f %s",
  653. gpsdata->fix.speed * speedfactor, speedunits);
  654. else
  655. (void)strlcpy(scr, " n/a", sizeof(scr));
  656. (void)mvwprintw(datawin, 5, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
  657. /* Fill in the track. */
  658. if (!magnetic_flag) {
  659. (void)strlcpy(scr, " (true, var): ", sizeof(scr));
  660. } else {
  661. (void)strlcpy(scr, " (mag, var): ", sizeof(scr));
  662. }
  663. if (gpsdata->fix.mode >= MODE_2D && isfinite(gpsdata->fix.track) != 0) {
  664. char buf1[20], buf2[20];
  665. if (!magnetic_flag || isfinite(gpsdata->fix.magnetic_track) == 0) {
  666. (void)snprintf(buf1, sizeof(buf1), "%5.1f,",
  667. gpsdata->fix.track);
  668. } else {
  669. (void)snprintf(buf1, sizeof(buf1), "%5.1f,",
  670. gpsdata->fix.magnetic_track);
  671. }
  672. (void)strlcat(scr, buf1, sizeof(scr));
  673. if (0 != isfinite(gpsdata->fix.magnetic_var)) {
  674. (void)snprintf(buf2, sizeof(buf2), "%6.1f",
  675. gpsdata->fix.magnetic_var);
  676. (void)strlcat(scr, buf2, sizeof(scr));
  677. } else {
  678. (void)strlcat(scr, " ", sizeof(scr));
  679. }
  680. } else
  681. (void)strlcat(scr, " n/a", sizeof(scr));
  682. (void)mvwprintw(datawin, 6, DATAWIN_VALUE_OFFSET - 10, "%-*s deg",
  683. 32, scr);
  684. /* Fill in the rate of climb. */
  685. if (isfinite(gpsdata->fix.climb) != 0)
  686. (void)snprintf(scr, sizeof(scr), "%8.2f %s/min",
  687. gpsdata->fix.climb * altfactor * 60, altunits);
  688. else
  689. (void)strlcpy(scr, " n/a", sizeof(scr));
  690. (void)mvwprintw(datawin, 7, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
  691. /* Fill in the GPS status and the time since the last state
  692. * change. */
  693. if (0 == gpsdata->online.tv_sec &&
  694. 0 == gpsdata->online.tv_nsec) {
  695. newstate = 0;
  696. (void)strlcpy(scr, "OFFLINE", sizeof(scr));
  697. } else {
  698. const char *fmt;
  699. const char *mod = "";
  700. newstate = gpsdata->fix.mode;
  701. switch (gpsdata->status) {
  702. case STATUS_DGPS_FIX:
  703. mod = "DGPS ";
  704. break;
  705. case STATUS_RTK_FIX:
  706. mod = "RTK ";
  707. break;
  708. case STATUS_RTK_FLT:
  709. mod = "RTK ";
  710. break;
  711. case STATUS_DR:
  712. mod = "DR ";
  713. break;
  714. case STATUS_GNSSDR:
  715. mod = "GNSSDR ";
  716. break;
  717. case STATUS_TIME:
  718. mod = "FIXED ";
  719. break;
  720. case STATUS_PPS_FIX:
  721. mod = "PPS ";
  722. break;
  723. default:
  724. /* ignore: */
  725. mod = "";
  726. break;
  727. }
  728. switch (gpsdata->fix.mode) {
  729. case MODE_2D:
  730. fmt = "2D %sFIX (%d secs)";
  731. break;
  732. case MODE_3D:
  733. if (STATUS_TIME == gpsdata->status) {
  734. fmt = "%sSURVEYED (%d secs)";
  735. } else {
  736. fmt = "3D %sFIX (%d secs)";
  737. }
  738. break;
  739. default:
  740. fmt = "NO %sFIX (%d secs)";
  741. break;
  742. }
  743. (void)snprintf(scr, sizeof(scr), fmt, mod,
  744. (int)(time(NULL) - status_timer));
  745. }
  746. (void)mvwprintw(datawin, 8, DATAWIN_VALUE_OFFSET + 1, "%-*s", 26, scr);
  747. /* Note that the following fields are exceptions to the
  748. * sizing rule. The minimum window size does not include these
  749. * fields, if the window is too small, they get excluded. This
  750. * may or may not change if/when the output for these fields is
  751. * fixed and/or people request their permanence. They're only
  752. * there in the first place because I arbitrarily thought they
  753. * sounded interesting. ;^) */
  754. if (window_ysize >= (MIN_GPS_DATAWIN_YSIZE + 5)) {
  755. int row = 9;
  756. char *ep_str;
  757. char *dop_str;
  758. char *str;
  759. /* Fill in the estimated latitude position error, XDOP. */
  760. ep_str = ep_to_str(gpsdata->fix.epx, altfactor, altunits);
  761. dop_str = dop_to_str(gpsdata->dop.xdop);
  762. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%s, %-*s",
  763. dop_str, 11, ep_str);
  764. /* Fill in the estimated longitude position error, YDOP. */
  765. ep_str = ep_to_str(gpsdata->fix.epy, altfactor, altunits);
  766. dop_str = dop_to_str(gpsdata->dop.ydop);
  767. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%s, %-*s",
  768. dop_str, 11, ep_str);
  769. /* Fill in the estimated velocity error, VDOP. */
  770. ep_str = ep_to_str(gpsdata->fix.epv, altfactor, altunits);
  771. dop_str = dop_to_str(gpsdata->dop.vdop);
  772. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%s, %-*s",
  773. dop_str, 11, ep_str);
  774. /* extra tall screen, show more DOPs */
  775. if (show_more_dops) {
  776. /* Fill in the estimated horizontal (2D) error, HDOP */
  777. ep_str = ep_to_str(gpsdata->fix.eph, altfactor, altunits);
  778. dop_str = dop_to_str(gpsdata->dop.hdop);
  779. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8,
  780. "%s, %-*s", dop_str, 11, ep_str);
  781. /* (spherical) position error, 3D error, PDOP */
  782. ep_str = ep_to_str(gpsdata->fix.sep, altfactor, altunits);
  783. dop_str = dop_to_str(gpsdata->dop.pdop);
  784. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8,
  785. "%s, %-*s", dop_str, 11, ep_str);
  786. /* time dilution of precision, TDOP */
  787. /* FIXME: time ep? */
  788. dop_str = dop_to_str(gpsdata->dop.tdop);
  789. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%-*s",
  790. 18, dop_str);
  791. /* geometric dilution of precision, GDOP */
  792. /* FIXME: gdop ep? */
  793. dop_str = dop_to_str(gpsdata->dop.gdop);
  794. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%-*s",
  795. 18, dop_str);
  796. }
  797. /* extra large screen, show ECEF */
  798. if (show_ecefs) {
  799. char *estr;
  800. /* Fill in the ECEF's. */
  801. estr = ecef_to_str(gpsdata->fix.ecef.x, gpsdata->fix.ecef.vx,
  802. 1, " m");
  803. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET - 3, "%-*s",
  804. 27, estr);
  805. estr = ecef_to_str(gpsdata->fix.ecef.y, gpsdata->fix.ecef.vy,
  806. 1, " m");
  807. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET - 3, "%-*s",
  808. 27, estr);
  809. estr = ecef_to_str(gpsdata->fix.ecef.z, gpsdata->fix.ecef.vz,
  810. 1, " m");
  811. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET - 3, "%-*s",
  812. 27, estr);
  813. }
  814. /* Fill in the estimated speed error, EPS. */
  815. ep_str = ep_to_str(gpsdata->fix.eps, speedfactor, speedunits);
  816. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8,
  817. "%-*s ", 14, ep_str);
  818. /* Fill in the estimated track error, EPD. */
  819. ep_str = ep_to_str(gpsdata->fix.epd, speedfactor, "deg");
  820. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%-*s ",
  821. 14, ep_str);
  822. /* Fill in the time offset, milliseconds. */
  823. if (0 < gpsdata->fix.time.tv_sec) {
  824. timespec_t ts_now, ts_diff;
  825. (void)clock_gettime(CLOCK_REALTIME, &ts_now);
  826. TS_SUB(&ts_diff, &ts_now, &gpsdata->fix.time);
  827. (void)snprintf(scr, sizeof(scr), "%lld.%03ld sec",
  828. (long long)ts_diff.tv_sec,
  829. ts_diff.tv_nsec / 1000000);
  830. } else {
  831. (void)strlcpy(scr, " n/a", sizeof(scr));
  832. }
  833. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%-*s", 18,
  834. scr);
  835. /* Fill in the grid square (esr thought *this* one was interesting). */
  836. if ((isfinite(gpsdata->fix.longitude) != 0 &&
  837. isfinite(gpsdata->fix.latitude) != 0))
  838. str = maidenhead(gpsdata->fix.latitude,gpsdata->fix.longitude);
  839. else
  840. str = "n/a";
  841. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 9, "%-*s",
  842. 18, str);
  843. }
  844. /* Be quiet if the user requests silence. */
  845. if (!silent_flag && raw_flag) {
  846. if (NULL != message) {
  847. size_t message_len = strlen(message);
  848. if (0 < message_len) {
  849. if ( '\r' == message[message_len - 1]) {
  850. /* remove any trailing \r */
  851. message[message_len - 1] = '\0';
  852. }
  853. (void)wprintw(messages, "\n%s", message);
  854. (void)wrefresh(messages);
  855. }
  856. }
  857. }
  858. /* Reset the status_timer if the state has changed. */
  859. if (newstate != state) {
  860. status_timer = time(NULL);
  861. state = newstate;
  862. }
  863. (void)wrefresh(datawin);
  864. (void)wrefresh(satellites);
  865. }
  866. static void usage(char *prog)
  867. {
  868. (void)fprintf(stderr,
  869. "Usage: %s [-h] [-l {d|m|s}] [-m] [-s] [-V] "
  870. "[server[:port:[device]]]\n\n"
  871. " -D debug-level Set debug level\n"
  872. " -h Show this help, then exit\n"
  873. " -l {d|m|s} Select lat/lon format\n"
  874. " d = DD.ddddddd\n"
  875. " m = DD MM.mmmmmm'\n"
  876. " s = DD MM' SS.sssss\"\n"
  877. " -m Display track as the estimated magnetic track\n"
  878. " Valid or USA (Lower 48 + AK) and Western Europe.\n"
  879. " -s Be silent (don't print raw gpsd data)\n"
  880. " -V Show version, then exit\n",
  881. prog);
  882. exit(EXIT_FAILURE);
  883. }
  884. /*
  885. * No protocol dependencies above this line
  886. */
  887. int main(int argc, char *argv[])
  888. {
  889. int option;
  890. unsigned int flags = WATCH_ENABLE;
  891. int wait_clicks = 0; /* cycles to wait before gpsd timeout */
  892. /* buffer to hold one JSON message */
  893. char message[GPS_JSON_RESPONSE_MAX];
  894. switch (gpsd_units()) {
  895. case imperial:
  896. altfactor = METERS_TO_FEET;
  897. altunits = "ft";
  898. speedfactor = MPS_TO_MPH;
  899. speedunits = "mph";
  900. break;
  901. case nautical:
  902. altfactor = METERS_TO_FEET;
  903. altunits = "ft";
  904. speedfactor = MPS_TO_KNOTS;
  905. speedunits = "kts";
  906. break;
  907. case metric:
  908. altfactor = 1;
  909. altunits = "m";
  910. speedfactor = MPS_TO_KPH;
  911. speedunits = "kph";
  912. break;
  913. default:
  914. /* leave the default alone */
  915. break;
  916. }
  917. /* Process the options. Print help if requested. */
  918. while ((option = getopt(argc, argv, "D:hl:msu:V")) != -1) {
  919. switch (option) {
  920. case 'D':
  921. debug = atoi(optarg);
  922. gps_enable_debug(debug, stderr);
  923. break;
  924. case 'm':
  925. magnetic_flag = true;
  926. break;
  927. case 's':
  928. silent_flag = true;
  929. break;
  930. case 'u':
  931. switch (optarg[0]) {
  932. case 'i':
  933. altfactor = METERS_TO_FEET;
  934. altunits = "ft";
  935. speedfactor = MPS_TO_MPH;
  936. speedunits = "mph";
  937. continue;
  938. case 'n':
  939. altfactor = METERS_TO_FEET;
  940. altunits = "ft";
  941. speedfactor = MPS_TO_KNOTS;
  942. speedunits = "knots";
  943. continue;
  944. case 'm':
  945. altfactor = 1;
  946. altunits = "m";
  947. speedfactor = MPS_TO_KPH;
  948. speedunits = "kph";
  949. continue;
  950. default:
  951. (void)fprintf(stderr, "Unknown -u argument: %s\n", optarg);
  952. }
  953. break;
  954. case 'V':
  955. (void)fprintf(stderr, "%s: %s (revision %s)\n",
  956. argv[0], VERSION, REVISION);
  957. exit(EXIT_SUCCESS);
  958. case 'l':
  959. switch (optarg[0]) {
  960. case 'd':
  961. deg_type = deg_dd;
  962. continue;
  963. case 'm':
  964. deg_type = deg_ddmm;
  965. continue;
  966. case 's':
  967. deg_type = deg_ddmmss;
  968. continue;
  969. default:
  970. (void)fprintf(stderr, "Unknown -l argument: %s\n", optarg);
  971. }
  972. break;
  973. case 'h':
  974. default:
  975. usage(argv[0]);
  976. break;
  977. }
  978. }
  979. /* Grok the server, port, and device. */
  980. if (optind < argc) {
  981. gpsd_source_spec(argv[optind], &source);
  982. } else
  983. gpsd_source_spec(NULL, &source);
  984. /* Open the stream to gpsd. */
  985. if (gps_open(source.server, source.port, &gpsdata) != 0) {
  986. (void)fprintf(stderr,
  987. "cgps: no gpsd running or network error: %d, %s\n",
  988. errno, gps_errstr(errno));
  989. exit(EXIT_FAILURE);
  990. }
  991. /* note: we're assuming BSD-style reliable signals here */
  992. (void)signal(SIGINT, die);
  993. (void)signal(SIGHUP, die);
  994. (void)signal(SIGWINCH, resize);
  995. windowsetup();
  996. status_timer = time(NULL);
  997. if (source.device != NULL)
  998. flags |= WATCH_DEVICE;
  999. (void)gps_stream(&gpsdata, flags, source.device);
  1000. /* heart of the client */
  1001. for (;;) {
  1002. int c;
  1003. /* wait 1/2 second for gpsd */
  1004. if (!gps_waiting(&gpsdata, 500000)) {
  1005. /* 240 tries at 0.5 seconds a try is a 2 minute timeout */
  1006. if ( 240 < wait_clicks++ )
  1007. die(GPS_TIMEOUT);
  1008. } else {
  1009. wait_clicks = 0;
  1010. errno = 0;
  1011. *message = '\0';
  1012. if (gps_read(&gpsdata, message, sizeof(message)) == -1) {
  1013. (void)fprintf(stderr, "cgps: socket error 4\n");
  1014. die(errno == 0 ? GPS_GONE : GPS_ERROR);
  1015. } else {
  1016. /* Here's where updates go now that things are established. */
  1017. #ifdef TRUENORTH
  1018. if (compass_flag)
  1019. update_compass_panel(&gpsdata);
  1020. else
  1021. #endif /* TRUENORTH */
  1022. update_gps_panel(&gpsdata, message);
  1023. }
  1024. }
  1025. /* Check for user input. */
  1026. c = wgetch(datawin);
  1027. switch (c) {
  1028. /* Quit */
  1029. case 'q':
  1030. die(CGPS_QUIT);
  1031. break;
  1032. /* Toggle spewage of raw gpsd data. */
  1033. case 's':
  1034. silent_flag = !silent_flag;
  1035. break;
  1036. /* Clear the spewage area. */
  1037. case 'c':
  1038. (void)werase(messages);
  1039. break;
  1040. default:
  1041. break;
  1042. }
  1043. }
  1044. }