cgps.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. * Copyright 2005 Jeff Francis <jeff@gritch.org>
  3. *
  4. * This file is Copyright 2005 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 + \
  62. DATAWIN_OVERHEAD)
  63. /* This is the minimum ysize we'll accept for the 'datawin' window in
  64. COMPASS mode. */
  65. #define MIN_COMPASS_DATAWIN_YSIZE (DATAWIN_COMPASS_FIELDS + DATAWIN_OVERHEAD)
  66. /* This is the maximum number of satellites gpsd can track. */
  67. #define MAX_POSSIBLE_SATS (MAXCHANNELS - 2)
  68. /* This is the maximum ysize we need for the 'satellites' window. */
  69. #define MAX_SATWIN_SIZE (MAX_POSSIBLE_SATS + SATWIN_OVERHEAD)
  70. /* Minimum xsize to display 3rd window with DOPs, etc. */
  71. #define MIN_ERRWIN_SIZE 100
  72. #include "gpsd_config.h" /* must be before all includes */
  73. #include <ctype.h>
  74. #include <curses.h>
  75. #include <errno.h>
  76. #include <math.h>
  77. #include <signal.h>
  78. #include <stdbool.h>
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81. #include <string.h>
  82. #include <time.h>
  83. #include <unistd.h>
  84. #include "gps.h"
  85. #include "gps_json.h" /* for GPS_JSON_RESPONSE_MAX */
  86. #include "compiler.h" /* for UNUSED */
  87. #include "gpsdclient.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. /* initialize curses and set up screen windows */
  216. static void windowsetup(void)
  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 -
  478. ((struct satellite_t*)p1)->used;
  479. }
  480. return ((struct satellite_t*)p1)->PRN - ((struct satellite_t*)p2)->PRN;
  481. }
  482. /* This gets called once for each new GPS sentence. */
  483. static void update_gps_panel(struct gps_data_t *gpsdata, char *message)
  484. {
  485. int newstate;
  486. char scr[80];
  487. char buf1[20], buf2[20];
  488. /* This is for the satellite status display. Originally lifted from
  489. * xgps.c. Note that the satellite list may be truncated based on
  490. * available screen size, or may only show satellites used for the
  491. * fix. */
  492. (void)mvwprintw(satellites, 0, 19, "Seen %2d/Used %2d",
  493. gpsdata->satellites_visible,
  494. gpsdata->satellites_used);
  495. if (0 != (VERSION_SET &gpsdata->set)) {
  496. /* got version, check it */
  497. /* FIXME: expected API version not available ? */
  498. if (0 != strcmp(gpsdata->version.release, VERSION)) {
  499. (void)fprintf(stderr, "cgps: WARNING gpsd release %s, API: %d.%d, "
  500. "expected %s ",
  501. gpsdata->version.release,
  502. gpsdata->version.proto_major,
  503. gpsdata->version.proto_minor,
  504. VERSION);
  505. sleep(2);
  506. }
  507. }
  508. if (gpsdata->satellites_visible != 0) {
  509. int sat_no;
  510. int loop_end = (display_sats < gpsdata->satellites_visible) ? \
  511. display_sats : gpsdata->satellites_visible;
  512. qsort( gpsdata->skyview, gpsdata->satellites_visible,
  513. sizeof( struct satellite_t), sat_cmp);
  514. /* displayed all sats that fit, maybe all of them */
  515. for (sat_no = 0; sat_no < loop_end; sat_no++) {
  516. int column = 1; /* column to write to */
  517. char *gnssid;
  518. char sigid[2] = " ";
  519. char health = ' ';
  520. if ( 0 == gpsdata->skyview[sat_no].svid) {
  521. gnssid = " ";
  522. } else {
  523. switch (gpsdata->skyview[sat_no].gnssid) {
  524. default:
  525. gnssid = " ";
  526. break;
  527. case GNSSID_GPS:
  528. gnssid = "GP"; /* GPS */
  529. break;
  530. case GNSSID_SBAS:
  531. gnssid = "SB"; /* SBAS */
  532. break;
  533. case GNSSID_GAL:
  534. gnssid = "GA"; /* GALILEO */
  535. break;
  536. case GNSSID_BD:
  537. gnssid = "BD"; /* BeiDou */
  538. break;
  539. case GNSSID_IMES:
  540. gnssid = "IM"; /* IMES */
  541. break;
  542. case GNSSID_QZSS:
  543. gnssid = "QZ"; /* QZSS */
  544. break;
  545. case GNSSID_GLO:
  546. gnssid = "GL"; /* GLONASS */
  547. break;
  548. case GNSSID_IRNSS:
  549. gnssid = "IR"; // IRNSS
  550. break;
  551. }
  552. if (1 < gpsdata->skyview[sat_no].sigid &&
  553. 8 > gpsdata->skyview[sat_no].sigid) {
  554. /* Do not display L1, or missing */
  555. /* max is 8 */
  556. sigid[0] = '0' + gpsdata->skyview[sat_no].sigid;
  557. sigid[1] = '\0';
  558. }
  559. }
  560. (void)mvwaddstr(satellites, sat_no + 2, column, gnssid);
  561. column += 2;
  562. (void)mvwaddstr(satellites, sat_no + 2, column, sigid);
  563. /* no GPS uses PRN 0, NMEA 4.0 here
  564. * NMEA 4.0 uses 1-437 */
  565. column += 2;
  566. (void)mvwaddstr(satellites, sat_no + 2, column,
  567. int_to_str(gpsdata->skyview[sat_no].PRN,
  568. 1, 438));
  569. column += 5;
  570. (void)mvwaddstr(satellites, sat_no + 2, column,
  571. tenth_to_str(gpsdata->skyview[sat_no].elevation,
  572. -90.0, 90.0));
  573. column += 7;
  574. (void)mvwaddstr(satellites, sat_no + 2, column,
  575. tenth_to_str(gpsdata->skyview[sat_no].azimuth,
  576. 0.0, 359.0));
  577. column += 6;
  578. (void)mvwaddstr(satellites, sat_no + 2, column,
  579. tenth_to_str(gpsdata->skyview[sat_no].ss,
  580. 0.0, 254.0));
  581. column += 6;
  582. if (SAT_HEALTH_BAD == gpsdata->skyview[sat_no].health) {
  583. /* only mark known unhealthy */
  584. health = 'u';
  585. }
  586. (void)mvwprintw(satellites, sat_no + 2, column, " %c%c ",
  587. health,
  588. gpsdata->skyview[sat_no].used ? 'Y' : 'N');
  589. }
  590. /* Display More... ? */
  591. if (sat_no < gpsdata->satellites_visible) {
  592. /* Too many sats to show them all, tell the user. */
  593. (void)mvwprintw(satellites, sat_no + 2, 1, "%s", "More...");
  594. } else {
  595. /* Clear old data from the unused lines at bottom. */
  596. for ( ; sat_no < display_sats; sat_no++) {
  597. (void)mvwprintw(satellites, sat_no + 2, 1, "%-*s",
  598. SATELLITES_WIDTH - 3, "");
  599. }
  600. /* remove More... */
  601. (void)mvwhline(satellites, sat_no + 2, 1, 0, 8);
  602. }
  603. } else {
  604. int sat_no = 0;
  605. /* no sats, clear screen */
  606. for ( ; sat_no < display_sats; sat_no++) {
  607. (void)mvwprintw(satellites, sat_no + 2, 1, "%-*s",
  608. SATELLITES_WIDTH - 3, "");
  609. }
  610. /* remove More... */
  611. (void)mvwhline(satellites, sat_no + 2, 1, 0, 8);
  612. }
  613. /* turn off cursor */
  614. curs_set(0);
  615. /* Print time/date. with (leap_second) */
  616. if (0 < gpsdata->fix.time.tv_sec) {
  617. (void)timespec_to_iso8601(gpsdata->fix.time, scr, sizeof(scr));
  618. } else
  619. (void)strlcpy(scr, " n/a", sizeof(scr));
  620. (void)snprintf(buf1, sizeof(buf1), " (%u)", gpsdata->leap_seconds);
  621. (void)strlcat(scr, buf1, sizeof(scr));
  622. (void)mvwprintw(datawin, 1, DATAWIN_VALUE_OFFSET - 2, "%-*s", 26, scr);
  623. /* Fill in the latitude. */
  624. if (gpsdata->fix.mode >= MODE_2D) {
  625. deg_to_str2(deg_type, gpsdata->fix.latitude,
  626. scr, sizeof(scr), " N", " S");
  627. } else
  628. (void)strlcpy(scr, "n/a", sizeof(scr));
  629. (void)mvwprintw(datawin, 2, DATAWIN_VALUE_OFFSET, " %-*s", 25, scr);
  630. /* Fill in the longitude. */
  631. if (gpsdata->fix.mode >= MODE_2D) {
  632. deg_to_str2(deg_type, gpsdata->fix.longitude,
  633. scr, sizeof(scr), " E", " W");
  634. } else
  635. (void)strlcpy(scr, "n/a", sizeof(scr));
  636. (void)mvwprintw(datawin, 3, DATAWIN_VALUE_OFFSET, " %-*s", 25, scr);
  637. /* Fill in the altitudes. */
  638. if (gpsdata->fix.mode >= MODE_3D) {
  639. if (0 != isfinite(gpsdata->fix.altHAE))
  640. (void)snprintf(buf1, sizeof(buf1), "%11.3f,",
  641. gpsdata->fix.altHAE * altfactor);
  642. else
  643. (void)strlcpy(buf1, " n/a,", sizeof(buf1));
  644. if (0 != isfinite(gpsdata->fix.altMSL))
  645. (void)snprintf(buf2, sizeof(buf2), "%11.3f ",
  646. gpsdata->fix.altMSL * altfactor);
  647. else
  648. (void)strlcpy(buf2, " n/a ", sizeof(buf2));
  649. (void)strlcpy(scr, buf1, sizeof(scr));
  650. (void)strlcat(scr, buf2, sizeof(scr));
  651. (void)strlcat(scr, altunits, sizeof(scr));
  652. } else {
  653. (void)strlcpy(scr, " n/a, n/a ", sizeof(scr));
  654. }
  655. (void)mvwprintw(datawin, 4, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
  656. /* Fill in the speed. */
  657. if (isfinite(gpsdata->fix.speed) != 0)
  658. (void)snprintf(scr, sizeof(scr), "%8.2f %s",
  659. gpsdata->fix.speed * speedfactor, speedunits);
  660. else
  661. (void)strlcpy(scr, " n/a", sizeof(scr));
  662. (void)mvwprintw(datawin, 5, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
  663. /* Fill in the track. */
  664. if (!magnetic_flag) {
  665. (void)strlcpy(scr, " (true, var): ", sizeof(scr));
  666. } else {
  667. (void)strlcpy(scr, " (mag, var): ", sizeof(scr));
  668. }
  669. if (gpsdata->fix.mode >= MODE_2D && isfinite(gpsdata->fix.track) != 0) {
  670. char buf1[20], buf2[20];
  671. if (!magnetic_flag || isfinite(gpsdata->fix.magnetic_track) == 0) {
  672. (void)snprintf(buf1, sizeof(buf1), "%5.1f,",
  673. gpsdata->fix.track);
  674. } else {
  675. (void)snprintf(buf1, sizeof(buf1), "%5.1f,",
  676. gpsdata->fix.magnetic_track);
  677. }
  678. (void)strlcat(scr, buf1, sizeof(scr));
  679. if (0 != isfinite(gpsdata->fix.magnetic_var)) {
  680. (void)snprintf(buf2, sizeof(buf2), "%6.1f",
  681. gpsdata->fix.magnetic_var);
  682. (void)strlcat(scr, buf2, sizeof(scr));
  683. } else {
  684. (void)strlcat(scr, " ", sizeof(scr));
  685. }
  686. } else
  687. (void)strlcat(scr, " n/a", sizeof(scr));
  688. (void)mvwprintw(datawin, 6, DATAWIN_VALUE_OFFSET - 10, "%-*s deg",
  689. 32, scr);
  690. /* Fill in the rate of climb. */
  691. if (isfinite(gpsdata->fix.climb) != 0)
  692. (void)snprintf(scr, sizeof(scr), "%8.2f %s/min",
  693. gpsdata->fix.climb * altfactor * 60, altunits);
  694. else
  695. (void)strlcpy(scr, " n/a", sizeof(scr));
  696. (void)mvwprintw(datawin, 7, DATAWIN_VALUE_OFFSET, "%-*s", 27, scr);
  697. /* Fill in the GPS status and the time since the last state
  698. * change. */
  699. if (0 == gpsdata->online.tv_sec &&
  700. 0 == gpsdata->online.tv_nsec) {
  701. newstate = 0;
  702. (void)strlcpy(scr, "OFFLINE", sizeof(scr));
  703. } else {
  704. const char *fmt;
  705. const char *mod = "";
  706. newstate = gpsdata->fix.mode;
  707. switch (gpsdata->fix.status) {
  708. case STATUS_DGPS_FIX:
  709. mod = "DGPS ";
  710. break;
  711. case STATUS_RTK_FIX:
  712. mod = "RTK ";
  713. break;
  714. case STATUS_RTK_FLT:
  715. mod = "RTK ";
  716. break;
  717. case STATUS_DR:
  718. mod = "DR ";
  719. break;
  720. case STATUS_GNSSDR:
  721. mod = "GNSSDR ";
  722. break;
  723. case STATUS_TIME:
  724. mod = "FIXED ";
  725. break;
  726. case STATUS_PPS_FIX:
  727. mod = "P(Y) ";
  728. break;
  729. case STATUS_SIM:
  730. mod = "SIM ";
  731. break;
  732. default:
  733. /* ignore: */
  734. mod = "";
  735. break;
  736. }
  737. switch (gpsdata->fix.mode) {
  738. case MODE_2D:
  739. fmt = "2D %sFIX (%d secs)";
  740. break;
  741. case MODE_3D:
  742. if (STATUS_TIME == gpsdata->fix.status) {
  743. fmt = "%sSURVEYED (%d secs)";
  744. } else {
  745. fmt = "3D %sFIX (%d secs)";
  746. }
  747. break;
  748. default:
  749. fmt = "NO %sFIX (%d secs)";
  750. break;
  751. }
  752. (void)snprintf(scr, sizeof(scr), fmt, mod,
  753. (int)(time(NULL) - status_timer));
  754. }
  755. (void)mvwprintw(datawin, 8, DATAWIN_VALUE_OFFSET + 1, "%-*s", 26, scr);
  756. /* Note that the following fields are exceptions to the
  757. * sizing rule. The minimum window size does not include these
  758. * fields, if the window is too small, they get excluded. This
  759. * may or may not change if/when the output for these fields is
  760. * fixed and/or people request their permanence. They're only
  761. * there in the first place because I arbitrarily thought they
  762. * sounded interesting. ;^) */
  763. if (window_ysize >= (MIN_GPS_DATAWIN_YSIZE + 5)) {
  764. int row = 9;
  765. char *ep_str;
  766. char *dop_str;
  767. char *str;
  768. /* Fill in the estimated latitude position error, XDOP. */
  769. ep_str = ep_to_str(gpsdata->fix.epx, altfactor, altunits);
  770. dop_str = dop_to_str(gpsdata->dop.xdop);
  771. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%s, %-*s",
  772. dop_str, 11, ep_str);
  773. /* Fill in the estimated longitude position error, YDOP. */
  774. ep_str = ep_to_str(gpsdata->fix.epy, altfactor, altunits);
  775. dop_str = dop_to_str(gpsdata->dop.ydop);
  776. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%s, %-*s",
  777. dop_str, 11, ep_str);
  778. /* Fill in the estimated velocity error, VDOP. */
  779. ep_str = ep_to_str(gpsdata->fix.epv, altfactor, altunits);
  780. dop_str = dop_to_str(gpsdata->dop.vdop);
  781. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%s, %-*s",
  782. dop_str, 11, ep_str);
  783. /* extra tall screen, show more DOPs */
  784. if (show_more_dops) {
  785. /* Fill in the estimated horizontal (2D) error, HDOP */
  786. ep_str = ep_to_str(gpsdata->fix.eph, altfactor, altunits);
  787. dop_str = dop_to_str(gpsdata->dop.hdop);
  788. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8,
  789. "%s, %-*s", dop_str, 11, ep_str);
  790. /* (spherical) position error, 3D error, PDOP */
  791. ep_str = ep_to_str(gpsdata->fix.sep, altfactor, altunits);
  792. dop_str = dop_to_str(gpsdata->dop.pdop);
  793. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8,
  794. "%s, %-*s", dop_str, 11, ep_str);
  795. /* time dilution of precision, TDOP */
  796. /* FIXME: time ep? */
  797. dop_str = dop_to_str(gpsdata->dop.tdop);
  798. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%-*s",
  799. 18, dop_str);
  800. /* geometric dilution of precision, GDOP */
  801. /* FIXME: gdop ep? */
  802. dop_str = dop_to_str(gpsdata->dop.gdop);
  803. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%-*s",
  804. 18, dop_str);
  805. }
  806. /* extra large screen, show ECEF */
  807. if (show_ecefs) {
  808. char *estr;
  809. /* Fill in the ECEF's. */
  810. estr = ecef_to_str(gpsdata->fix.ecef.x, gpsdata->fix.ecef.vx,
  811. 1, " m");
  812. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET - 3, "%-*s",
  813. 27, estr);
  814. estr = ecef_to_str(gpsdata->fix.ecef.y, gpsdata->fix.ecef.vy,
  815. 1, " m");
  816. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET - 3, "%-*s",
  817. 27, estr);
  818. estr = ecef_to_str(gpsdata->fix.ecef.z, gpsdata->fix.ecef.vz,
  819. 1, " m");
  820. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET - 3, "%-*s",
  821. 27, estr);
  822. }
  823. /* Fill in the estimated speed error, EPS. */
  824. ep_str = ep_to_str(gpsdata->fix.eps, speedfactor, speedunits);
  825. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8,
  826. "%-*s ", 14, ep_str);
  827. /* Fill in the estimated track error, EPD. */
  828. ep_str = ep_to_str(gpsdata->fix.epd, speedfactor, "deg");
  829. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%-*s ",
  830. 14, ep_str);
  831. /* Fill in the time offset, milliseconds. */
  832. if (0 < gpsdata->fix.time.tv_sec) {
  833. timespec_t ts_now, ts_diff;
  834. char ts_str[TIMESPEC_LEN];
  835. (void)clock_gettime(CLOCK_REALTIME, &ts_now);
  836. TS_SUB(&ts_diff, &ts_now, &gpsdata->fix.time);
  837. (void)snprintf(scr, sizeof(scr), "%s sec",
  838. timespec_str(&ts_diff, ts_str, sizeof(ts_str)));
  839. } else {
  840. (void)strlcpy(scr, " n/a", sizeof(scr));
  841. }
  842. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 8, "%-*s", 18,
  843. scr);
  844. /* Fill in the grid square (esr thought *this* one was interesting). */
  845. if ((isfinite(gpsdata->fix.longitude) != 0 &&
  846. isfinite(gpsdata->fix.latitude) != 0))
  847. str = maidenhead(gpsdata->fix.latitude,gpsdata->fix.longitude);
  848. else
  849. str = "n/a";
  850. (void)mvwprintw(datawin, row++, DATAWIN_VALUE_OFFSET + 9, "%-*s",
  851. 18, str);
  852. }
  853. /* Be quiet if the user requests silence. */
  854. if (!silent_flag && raw_flag) {
  855. if (NULL != message) {
  856. size_t message_len = strlen(message);
  857. if (0 < message_len) {
  858. if ( '\r' == message[message_len - 1]) {
  859. /* remove any trailing \r */
  860. message[message_len - 1] = '\0';
  861. }
  862. (void)wprintw(messages, "\n%s", message);
  863. (void)wrefresh(messages);
  864. }
  865. }
  866. }
  867. /* Reset the status_timer if the state has changed. */
  868. if (newstate != state) {
  869. status_timer = time(NULL);
  870. state = newstate;
  871. }
  872. (void)wrefresh(datawin);
  873. (void)wrefresh(satellites);
  874. }
  875. static void usage(char *prog)
  876. {
  877. (void)fprintf(stderr,
  878. "Usage: %s [-h] [-l {d|m|s}] [-m] [-s] [-V] "
  879. "[server[:port:[device]]]\n\n"
  880. " -D debug-level Set debug level\n"
  881. " -h Show this help, then exit\n"
  882. " -l {d|m|s} Select lat/lon format\n"
  883. " d = DD.ddddddd\n"
  884. " m = DD MM.mmmmmm'\n"
  885. " s = DD MM' SS.sssss\"\n"
  886. " -m Display track as the estimated magnetic track\n"
  887. " Valid or USA (Lower 48 + AK) and Western Europe.\n"
  888. " -s Be silent (don't print raw gpsd data)\n"
  889. " -V Show version, then exit\n",
  890. prog);
  891. exit(EXIT_FAILURE);
  892. }
  893. /*
  894. * No protocol dependencies above this line
  895. */
  896. int main(int argc, char *argv[])
  897. {
  898. int option;
  899. unsigned int flags = WATCH_ENABLE;
  900. int wait_clicks = 0; /* cycles to wait before gpsd timeout */
  901. /* buffer to hold one JSON message */
  902. char message[GPS_JSON_RESPONSE_MAX];
  903. switch (gpsd_units()) {
  904. case imperial:
  905. altfactor = METERS_TO_FEET;
  906. altunits = "ft";
  907. speedfactor = MPS_TO_MPH;
  908. speedunits = "mph";
  909. break;
  910. case nautical:
  911. altfactor = METERS_TO_FEET;
  912. altunits = "ft";
  913. speedfactor = MPS_TO_KNOTS;
  914. speedunits = "kts";
  915. break;
  916. case metric:
  917. altfactor = 1;
  918. altunits = "m";
  919. speedfactor = MPS_TO_KPH;
  920. speedunits = "kph";
  921. break;
  922. default:
  923. /* leave the default alone */
  924. break;
  925. }
  926. /* Process the options. Print help if requested. */
  927. while ((option = getopt(argc, argv, "D:hl:msu:V")) != -1) {
  928. switch (option) {
  929. case 'D':
  930. debug = atoi(optarg);
  931. gps_enable_debug(debug, stderr);
  932. break;
  933. case 'm':
  934. magnetic_flag = true;
  935. break;
  936. case 's':
  937. silent_flag = true;
  938. break;
  939. case 'u':
  940. switch (optarg[0]) {
  941. case 'i':
  942. altfactor = METERS_TO_FEET;
  943. altunits = "ft";
  944. speedfactor = MPS_TO_MPH;
  945. speedunits = "mph";
  946. continue;
  947. case 'n':
  948. altfactor = METERS_TO_FEET;
  949. altunits = "ft";
  950. speedfactor = MPS_TO_KNOTS;
  951. speedunits = "knots";
  952. continue;
  953. case 'm':
  954. altfactor = 1;
  955. altunits = "m";
  956. speedfactor = MPS_TO_KPH;
  957. speedunits = "kph";
  958. continue;
  959. default:
  960. (void)fprintf(stderr, "Unknown -u argument: %s\n", optarg);
  961. }
  962. break;
  963. case 'V':
  964. (void)fprintf(stderr, "%s: %s (revision %s)\n",
  965. argv[0], VERSION, REVISION);
  966. exit(EXIT_SUCCESS);
  967. case 'l':
  968. switch (optarg[0]) {
  969. case 'd':
  970. deg_type = deg_dd;
  971. continue;
  972. case 'm':
  973. deg_type = deg_ddmm;
  974. continue;
  975. case 's':
  976. deg_type = deg_ddmmss;
  977. continue;
  978. default:
  979. (void)fprintf(stderr, "Unknown -l argument: %s\n", optarg);
  980. }
  981. break;
  982. case 'h':
  983. default:
  984. usage(argv[0]);
  985. break;
  986. }
  987. }
  988. /* Grok the server, port, and device. */
  989. if (optind < argc) {
  990. gpsd_source_spec(argv[optind], &source);
  991. } else
  992. gpsd_source_spec(NULL, &source);
  993. /* Open the stream to gpsd. */
  994. if (gps_open(source.server, source.port, &gpsdata) != 0) {
  995. (void)fprintf(stderr,
  996. "cgps: no gpsd running or network error: %d, %s\n",
  997. errno, gps_errstr(errno));
  998. exit(EXIT_FAILURE);
  999. }
  1000. /* note: we're assuming BSD-style reliable signals here */
  1001. (void)signal(SIGINT, die);
  1002. (void)signal(SIGHUP, die);
  1003. (void)signal(SIGWINCH, resize);
  1004. windowsetup();
  1005. status_timer = time(NULL);
  1006. if (source.device != NULL)
  1007. flags |= WATCH_DEVICE;
  1008. (void)gps_stream(&gpsdata, flags, source.device);
  1009. /* heart of the client */
  1010. for (;;) {
  1011. int c;
  1012. /* wait 1/2 second for gpsd */
  1013. if (!gps_waiting(&gpsdata, 500000)) {
  1014. /* 240 tries at 0.5 seconds a try is a 2 minute timeout */
  1015. if ( 240 < wait_clicks++ )
  1016. die(GPS_TIMEOUT);
  1017. } else {
  1018. wait_clicks = 0;
  1019. errno = 0;
  1020. *message = '\0';
  1021. if (gps_read(&gpsdata, message, sizeof(message)) == -1) {
  1022. (void)fprintf(stderr, "cgps: socket error 4\n");
  1023. die(errno == 0 ? GPS_GONE : GPS_ERROR);
  1024. } else {
  1025. /* Here's where updates go now that things are established. */
  1026. #ifdef TRUENORTH
  1027. if (compass_flag)
  1028. update_compass_panel(&gpsdata);
  1029. else
  1030. #endif /* TRUENORTH */
  1031. update_gps_panel(&gpsdata, message);
  1032. }
  1033. }
  1034. /* Check for user input. */
  1035. c = wgetch(datawin);
  1036. switch (c) {
  1037. /* Quit */
  1038. case 'q':
  1039. die(CGPS_QUIT);
  1040. break;
  1041. /* Toggle spewage of raw gpsd data. */
  1042. case 's':
  1043. silent_flag = !silent_flag;
  1044. break;
  1045. /* Clear the spewage area. */
  1046. case 'c':
  1047. (void)werase(messages);
  1048. break;
  1049. default:
  1050. break;
  1051. }
  1052. }
  1053. }
  1054. // vim: set expandtab shiftwidth=4