gps_main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * Copyright (c) 2019 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * PopuFare is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <sys/time.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <poll.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <signal.h>
  28. #include <time.h>
  29. #include <ctype.h>
  30. #include <string.h>
  31. #include "../common/common_defs.h"
  32. #include "../commhub/commhub.h"
  33. #include "../commhub/client_utils.h"
  34. #define GPS_MINDER_VERSION "2.1.1"
  35. int gps_fd = -1;
  36. int hub_fd = -1;
  37. time_t last_hub_try = 0;
  38. time_t last_diu_clock = 0;
  39. gps_status my_gps_stat = {0};
  40. state_info_t my_state_info = {0};
  41. time_t mkgmtime(struct tm *tm);
  42. // This function takes a GPS timestamp (in gross GPS float format + gross integer date) and
  43. // makes it into a sane UTC timestamp. If we are more than MAX_GPS_CLOCK_DRIFT seconds off
  44. // from GPS time, it sets the system clock to GPS time.
  45. //
  46. int handle_gps_time(float gtime, int date) {
  47. int day,month,year;
  48. int hour,min,sec,frac;
  49. int n = 0;
  50. time_t utc, now;
  51. char buffer[32] = {0};
  52. struct message_record outgoing_msg;
  53. now = time(NULL);
  54. day = month = year = 0;
  55. hour = min = sec = frac = 0;
  56. // Just to be *ahem* clear, as per NMEA standard the date is encoded DDMMYY, and the time of day
  57. // is encoded HHMMSS[.frac] where there is an optional fractional second field denoted by a decimal point.
  58. // we must be able to decode the fractional seconds field but we discard the information since it is not
  59. // reliable enough to be worth the bother.
  60. //
  61. // Start out with zero parsed fields
  62. //
  63. n = 0;
  64. // Construct and then re-parse the time from its icky format to discrete values (this should result
  65. // in at least three (possibly four) fields.
  66. //
  67. sprintf(buffer,"%010.3f", gtime);
  68. n += sscanf(buffer,"%02d%02d%02d.%d", &hour, &min, &sec, &frac);
  69. // Construct and then re-parse the date from its icky format to discrete values. This should result in three fields.
  70. //
  71. sprintf(buffer,"%06d", date);
  72. n += sscanf(buffer,"%02d%02d%02d", &day, &month, &year);
  73. if(n >= 6) //if we scanned at all required fields
  74. {
  75. struct tm gpstm = {0};
  76. // GPS date only uses two digits for year, so we must assume the century
  77. //
  78. year += GPS_DATE_CENTURY;
  79. // tm.tm_year is based on the year 1900
  80. //
  81. gpstm.tm_year = year - 1900;
  82. // January = month 0 in struct tm.tm_mon whereas January = month 1 in an NMEA GPS date.
  83. //
  84. gpstm.tm_mon = month - 1;
  85. gpstm.tm_mday = day;
  86. gpstm.tm_hour = hour;
  87. gpstm.tm_min = min;
  88. gpstm.tm_sec = sec;
  89. // Go and turn the struct tm we've just populated into an utc time stamp (seconds since epoch)
  90. //
  91. utc = mkgmtime(&gpstm);
  92. // Most importantly... Remember what the self-reported GPS time stamp is.
  93. //
  94. my_gps_stat.gpstime = utc;
  95. // printf("%02d-%02d-%04d %02d:%02d:%02d\n",day,month,year,hour,min,sec);
  96. // printf("CALCULATED: %d\nSYSTEM : %d\n\n",(int)utc,(int)time(NULL));
  97. // if we have more than MAX_GPS_CLOCK_DRIFT seconds of clock drift
  98. //
  99. if(abs(utc - now) > MAX_GPS_CLOCK_DRIFT) {
  100. struct timeval ts = {0};
  101. // Set the timeval struct to the calculated utc timestamp from the GPS unit.
  102. //
  103. ts.tv_sec = utc;
  104. ts.tv_usec = 0;
  105. // Set the system clock from GPS time using said timeval struct.
  106. //
  107. settimeofday(&ts, NULL);
  108. //system("/sbin/hwclock --systohc"); //Go and push the new system clock value into the hardware realtime clock chip.
  109. // If we have a valid connection to the IPC hub
  110. //
  111. if( hub_fd >= 0) {
  112. // Stick a message into the diagnostic log to record the fact that we've set the system clock from GPS
  113. //
  114. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Set syatem clock from previous value (%d) to GPS time (%d).", (int)now, (int)utc );
  115. send_message(hub_fd, &outgoing_msg);
  116. }
  117. }
  118. }
  119. return 0;
  120. }
  121. #ifdef CLEAR_GPS_ON_STALE
  122. static inline void clear_stale_gps_data() {
  123. my_gps_stat.lat = my_gps_stat.lon = my_gps_stat.heading = my_gps_stat.velocity = 0;
  124. my_gps_stat.num_sats = 0;
  125. }
  126. #else
  127. static inline void clear_stale_gps_data() { }
  128. #endif
  129. static int handle_stale_gps_condition() {
  130. // This return code will be > 0 if this function generates a status change that
  131. // will then need to be communicated to other modules in the system via a message to
  132. // MAILBOX_GPS_STATUS.
  133. //
  134. int return_code = 0;
  135. // This will hold the gps_good flag as it stood at the beginning of this subroutine
  136. // previous to any adjustments we make.
  137. //
  138. int previous_good_flag = my_gps_stat.gps_good;
  139. int stale_time = 0;
  140. time_t now = time(NULL);
  141. stale_time = (now - my_gps_stat.stamp);
  142. // If we have entered this function with the impression that we have a valid GPS fix...
  143. //
  144. if(previous_good_flag > 0) {
  145. // If it has been at least GPS_STALE_THRESHOLD seconds since the last fix
  146. //
  147. if( stale_time >= GPS_STALE_THRESHOLD ) {
  148. clear_stale_gps_data();
  149. my_gps_stat.gps_good = 0;
  150. return_code |= 1;
  151. }
  152. }
  153. // If we have determined that we need to declare the GPS data stale and invalid and
  154. // we have a valid connection to the IPC hub we should use that IPC hub connection to
  155. // add a note to the diagnostic log indicating that we've declared the GPS data stale.
  156. //
  157. if( return_code && (hub_fd >= 0) ) {
  158. struct message_record outgoing_msg;
  159. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "GPS fix has been stale for %d seconds, setting GPS = NO.", stale_time);
  160. send_message(hub_fd, &outgoing_msg);
  161. }
  162. return return_code;
  163. }
  164. //------------
  165. // Standard c parsing and tokenization functions are having trouble with blank fields,
  166. // so these custom field parsing routines were developed.
  167. //
  168. // Each takes in a string and parses the first field, where appropriate.
  169. // It then advances to the next token by searching for the field delimiter (',')
  170. // and pushing the pointer one past it, prepping it for next use.
  171. //
  172. // A default value of '0' is assigned if no conversion has taken place.
  173. //
  174. // Return NULL on error or end of string.
  175. //
  176. static char *_convert_advance_nop(char *in) {
  177. if (!in) { return NULL; }
  178. if ((*in) == ',') { return in+1; }
  179. in = strchr(in, ',');
  180. if (in) { return in+1; }
  181. return in;
  182. }
  183. static char *_convert_advance_float(char *in, float *f) {
  184. *f = 0.0;
  185. if (!in) { return NULL; }
  186. if ((*in) != ',') { *f = strtof(in, NULL); }
  187. if ((*in) == ',') { return in+1; }
  188. in = strchr(in, ',');
  189. if (in) { return in+1; }
  190. return in;
  191. }
  192. static char *_convert_advance_char(char *in, char *ch) {
  193. *ch = '\0';
  194. if (!in) { return NULL; }
  195. if ((*in) != ',') { *ch = *in; }
  196. if ((*in) == ',') { return in+1; }
  197. in = strchr(in, ',');
  198. if (in) { return in+1; }
  199. return in;
  200. }
  201. //----
  202. int update_gps(char *in) {
  203. // This will hold the number of matched variables populated by sscanf().
  204. //
  205. int num = 0;
  206. // This will allow us to know if we have made a transition from an invalid
  207. // GPS fix to a valid one so that we can log this information.
  208. //
  209. int previous_good_flag = my_gps_stat.gps_good;
  210. // This return code will be > 0 if this function generates a status change that
  211. // will then need to be communicated to other modules in the system via a message to
  212. // MAILBOX_GPS_STATUS.
  213. //
  214. int return_code = 0;
  215. if(!strncmp(in,"$GPRMC",6)) {
  216. float f1=0;
  217. char f2=0;
  218. float f3=0;
  219. char f4=0;
  220. float f5=0;
  221. char f6=0;
  222. float f7=0;
  223. float f8=0;
  224. float f9=0;
  225. float f10=0;
  226. char f11=0;
  227. char *chp;
  228. chp = in;
  229. num=0;
  230. do {
  231. // ignore first token
  232. //
  233. chp = _convert_advance_nop(in);
  234. if (!chp) { break; }
  235. chp = _convert_advance_float(chp, &f1);
  236. if (!chp) { break; }
  237. num++;
  238. chp = _convert_advance_char(chp, &f2);
  239. if (!chp) { break; }
  240. num++;
  241. chp = _convert_advance_float(chp, &f3);
  242. if (!chp) { break; }
  243. num++;
  244. chp = _convert_advance_char(chp, &f4);
  245. if (!chp) { break; }
  246. num++;
  247. chp = _convert_advance_float(chp, &f5);
  248. if (!chp) { break; }
  249. num++;
  250. chp = _convert_advance_char(chp, &f6);
  251. if (!chp) { break; }
  252. num++;
  253. chp = _convert_advance_float(chp, &f7);
  254. if (!chp) { break; }
  255. num++;
  256. chp = _convert_advance_float(chp, &f8);
  257. if (!chp) { break; }
  258. num++;
  259. chp = _convert_advance_float(chp, &f9);
  260. if (!chp) { break; }
  261. num++;
  262. chp = _convert_advance_float(chp, &f10);
  263. if (!chp) { break; }
  264. num++;
  265. chp = _convert_advance_char(chp, &f11);
  266. if (!chp) { break; }
  267. num++;
  268. } while (0);
  269. // If we have a full GPRMC sentence we can consider setting the time
  270. //
  271. if(num == 11) {
  272. // Require at least MIN_SATS_FOR_TIME satellites to accept a new system clock value from the GPS unit.
  273. // This is to keep a crummy GPS fix from generating a bogus or unstable system time.
  274. //
  275. if(my_gps_stat.num_sats >= MIN_SATS_FOR_TIME) {
  276. // Pass the time field (f1) and the date field (f9) in to the routine that sets the system clock if needed.
  277. // (this routine also stores the utc timestamp derived from the GPS date and time fields so it can be passed to
  278. // other modules that may have a need for this information).
  279. //
  280. //handle_gps_time(f1,(int)f9);
  281. }
  282. }
  283. if(num > 0) {
  284. // update snapshot with latitude
  285. //
  286. my_gps_stat.lat = f3 * ((f4 == 'N')?(1):(-1));
  287. // longitude
  288. //
  289. my_gps_stat.lon = f5 * ((f6 == 'E')?(1):(-1));
  290. // meters per second (converted from knots)
  291. //
  292. my_gps_stat.velocity = f7 / 1.94384449f;
  293. // course
  294. //
  295. my_gps_stat.heading = f8;
  296. // update snapshot's staledate
  297. //
  298. my_gps_stat.stamp = time(NULL);
  299. return_code |= 1;
  300. }
  301. }
  302. else if(!strncmp(in,"$GPGGA",6)) {
  303. float f1=0;
  304. float f2=0;
  305. char f3=0;
  306. float f4=0;
  307. char f5=0;
  308. int f6=0;
  309. int f7=0;
  310. float f8=0;
  311. float f9=0;
  312. char f10=0;
  313. float f11=0;
  314. char f12=0;
  315. //num=sscanf(in,"$GPGGA,%d,%f,%c,%f,%c,%d,%d,%f,%f,%c,%f,%c",&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9,&f10,&f11,&f12);
  316. num=sscanf(in,"$GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c,%f,%c",&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9,&f10,&f11,&f12);
  317. if(num == 12) {
  318. // require 3 satellites minimum
  319. //
  320. if ( f7 >= 3 ) {
  321. // store GPS valid flag in snapshot
  322. //
  323. my_gps_stat.gps_good = f6;
  324. // store number of satellites in view in snapshot
  325. //
  326. my_gps_stat.num_sats = f7;
  327. // Do NOT store the timestamp since we only want to remember timestamps of position
  328. // fixes and the GPGGA message is all metadata (number of satellites, fix quality, etc...).
  329. // It is worth noting that GPGGA does report altitude, but that is not a piece of information
  330. // we track because the road is where the road is, and if a bus becomes airborn we have much
  331. // bigger problems than figuring out how far off the ground it is...
  332. //
  333. return_code |= 1;
  334. }
  335. }
  336. }
  337. return_code |= handle_stale_gps_condition();
  338. // If we have a connection to the IPC hub and we had previously not had a valid
  339. // GPS fix but we now do, make a note of it in the diagnostic log.
  340. //
  341. if( (previous_good_flag == 0) && (my_gps_stat.gps_good != 0) && (hub_fd >= 0) ) {
  342. struct message_record outgoing_msg;
  343. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "GPS fix is now valid with %d satellites. Setting GPS = YES.", my_gps_stat.num_sats);
  344. send_message(hub_fd, &outgoing_msg);
  345. }
  346. return return_code;
  347. }
  348. void maintain_ipc_hub_connect(char *progname) {
  349. struct message_record outgoing_msg;
  350. // if we have no connection to the IPC hub
  351. //
  352. if(hub_fd < 0) {
  353. // if we haven't tried the hub in a few seconds
  354. //
  355. if( (time(NULL) - last_hub_try) > HUB_RETRY_TIME ) {
  356. // retry it
  357. //
  358. last_hub_try = time(NULL);
  359. // try and get one
  360. //
  361. hub_fd = connect_to_message_server(progname);
  362. if(hub_fd >= 0) {
  363. //Subscribe to the default status messages
  364. //
  365. subscribe_to_default_messages(hub_fd);
  366. // Ask for a status update
  367. //
  368. prepare_message(&outgoing_msg, MAILBOX_STATUS_REQUEST, "", 0);
  369. send_message(hub_fd,&outgoing_msg);
  370. }
  371. else {
  372. fprintf(stderr, "Cannot connect to IPC hub!\n");
  373. }
  374. }
  375. }
  376. }
  377. // This might be better done at a higher level but kept here
  378. // in case we need it.
  379. //
  380. int reset_gps() {
  381. char msg_gps_off[] = "AT+CGPS=0\n",
  382. msg_gps_on[] = "AT+CGPS=1\n",
  383. msg_gps_poll_off[] = "AT+CGPSINFOCFG=0\n",
  384. msg_gps_poll_on[] = "AT+CGPSINFOCFG=3,31\n";
  385. size_t n;
  386. if (gps_fd < 0) { return -1; }
  387. n = strlen(msg_gps_off)+1;
  388. write(gps_fd, msg_gps_off, n);
  389. n = strlen(msg_gps_poll_off)+1;
  390. write(gps_fd, msg_gps_poll_off, n);
  391. n = strlen(msg_gps_on)+1;
  392. write(gps_fd, msg_gps_on, n);
  393. n = strlen(msg_gps_poll_on)+1;
  394. write(gps_fd, msg_gps_poll_on, n);
  395. return 0;
  396. }
  397. //----------------------------------
  398. int main(int argc, char **argv) {
  399. int i;
  400. char line[LINE_BUFFER_SIZE] = {0};
  401. struct message_record incoming_msg;
  402. struct message_record outgoing_msg;
  403. struct pollfd fds[2];
  404. int nfd;
  405. int poll_return;
  406. int read_return;
  407. time_t now;
  408. time_t last_stale_gps_check = 0;
  409. #ifdef DEBUG_PRINT
  410. long long int _usec_now, _usec_prv, _usec_del;
  411. _usec_now = get_usec_time();
  412. _usec_prv = _usec_now;
  413. _usec_del = 60000000;
  414. #endif
  415. long long int _usec_ratelimit_now, _usec_ratelimit_prv, _usec_ratelimit_del;
  416. _usec_ratelimit_now = get_usec_time();
  417. _usec_ratelimit_del = 1000000;
  418. _usec_ratelimit_prv = _usec_ratelimit_now - _usec_ratelimit_del;
  419. if ( (argc>1) && (
  420. (strncmp(argv[1], "-h", 3)==0) ||
  421. (strncmp(argv[1], "-v", 3)==0) ) ) {
  422. printf("gps_minder version: %s\n", GPS_MINDER_VERSION);
  423. exit(0);
  424. }
  425. // Restore GPS state in case `gps_minder` has been restarted
  426. //
  427. get_state_info(&my_state_info);
  428. my_gps_stat.lat = my_state_info.lat;
  429. my_gps_stat.lon = my_state_info.lon;
  430. my_gps_stat.heading = my_state_info.heading;
  431. my_gps_stat.velocity = my_state_info.velocity;
  432. my_gps_stat.num_sats = my_state_info.num_sats;
  433. my_gps_stat.gps_good = my_state_info.gps_good;
  434. my_gps_stat.stamp = my_state_info.stamp;
  435. my_gps_stat.gpstime = my_state_info.gpstime;
  436. // Configure our signal handlers to deal with SIGINT, SIGTERM, etc...
  437. // and make graceful exits while logging
  438. //
  439. configure_signal_handlers(argv[0]);
  440. // Make an initial attempt to get in touch with the
  441. // interprocess communication hub (it may not be up yet depending on the start order)
  442. //
  443. maintain_ipc_hub_connect(argv[0]);
  444. // Register our defualt system message processing callbacks
  445. //
  446. register_system_status_callbacks();
  447. // loop until we get asked to exit...
  448. //
  449. while( exit_request_status == EXIT_REQUEST_NONE ) {
  450. RESET_WATCHDOG();
  451. #ifdef DEBUG_PRINT
  452. _usec_now = get_usec_time();
  453. if ((_usec_now - _usec_prv) > _usec_del) {
  454. printf("[%lli] gps_minder: heartbeat\n", get_usec_time());
  455. _usec_prv = _usec_now;
  456. }
  457. #endif
  458. maintain_ipc_hub_connect(argv[0]);
  459. if(gps_fd < 0) {
  460. gps_fd = open_rs232_device(GPS_PORT, GPS_DEFAULT_BAUD, RS232_LINE);
  461. if(gps_fd < 0) {
  462. _usec_ratelimit_now = get_usec_time();
  463. if ((_usec_ratelimit_now - _usec_ratelimit_prv) > _usec_ratelimit_del) {
  464. fprintf(stderr, "Cannot open serial port %s for GPS!\n", GPS_PORT);
  465. _usec_ratelimit_prv = _usec_ratelimit_now;
  466. }
  467. }
  468. }
  469. //---
  470. now = time(NULL);
  471. // Every second we want to check to make sure that our GPS data have note gone stale...
  472. //
  473. if((now - last_stale_gps_check) > 0) {
  474. // If the stale check results in an update to my_gps_stat, we must update any other
  475. // modules which may be tracking GPS status via the IPC hub.
  476. //
  477. if( handle_stale_gps_condition() > 0 ) {
  478. // If we have a connection to the IPC hub
  479. //
  480. if(hub_fd >= 0) {
  481. // Go and toss the data to any other modules who happen to care about GPS
  482. //
  483. prepare_message(&outgoing_msg, MAILBOX_GPS_STATUS, &my_gps_stat, sizeof(my_gps_stat));
  484. send_message(hub_fd, &outgoing_msg);
  485. }
  486. }
  487. // Either way, remember that we did this stale check.
  488. //
  489. last_stale_gps_check = now;
  490. }
  491. //---
  492. if (hup_request_status) {
  493. fprintf(stderr, "gps_minder: hup request\n");
  494. hup_request_status = 0;
  495. }
  496. //---
  497. nfd = 0;
  498. if(hub_fd >= 0) {
  499. fds[nfd].fd = hub_fd;
  500. fds[nfd].events = POLLIN;
  501. fds[nfd].revents = 0;
  502. nfd++;
  503. }
  504. if(gps_fd >= 0) {
  505. fds[nfd].fd = gps_fd;
  506. fds[nfd].events = POLLIN;
  507. fds[nfd].revents = 0;
  508. nfd++;
  509. }
  510. // if we have any file descriptors, poll them
  511. //
  512. if(nfd > 0) {
  513. poll_return = poll(fds, nfd, POLL_TIMEOUT);
  514. }
  515. // otherwise, whistle and look busy
  516. //
  517. else {
  518. poll_return = 0; //(this keeps us from buringing 100% cpu cycles if we don't have contact with either
  519. sleep(1); //the IPC hub or the DIU hardware).
  520. }
  521. //--------------------------------------------------------------------------------------------------
  522. // if poll didn't net us any work to do,
  523. //
  524. if (poll_return < 1) {
  525. // lets try again
  526. //
  527. continue;
  528. }
  529. // Loop through all polled file descriptors
  530. //
  531. for (i = 0; i < nfd; i++) {
  532. // If we're looking at the DIU...
  533. //
  534. if ( fds[i].fd == gps_fd ) {
  535. // if poll says our serial port has become bogus...
  536. //
  537. if (fds[i].revents & (POLLHUP | POLLERR | POLLNVAL)) {
  538. fprintf(stderr, "This is very odd... Poll returned flags %d on our serial port...\n", fds[i].revents);
  539. // close it, flag as invalid,
  540. // and break out of the for loop to allow the while to cycle
  541. //
  542. close(gps_fd);
  543. gps_fd = -1;
  544. break;
  545. }
  546. if (fds[i].revents & POLLIN) {
  547. read_return = read(fds[i].fd, line, sizeof(line));
  548. if (read_return > 0) {
  549. char *trav = line;
  550. line[read_return] = '\0';
  551. strip_crlf(line);
  552. // advance until EOL or we hit our start sentinel
  553. //
  554. while (*trav && (*trav != '$') ) {
  555. trav++;
  556. }
  557. // Check to see that our address header is intact...
  558. //
  559. if (trav[0] == '$') {
  560. switch (trav[1]) {
  561. case 'G': //-----------------------------------"/G:" means it is a new GPS input
  562. // If this GPS update constitutes a meaningful piece of data
  563. //
  564. if(update_gps(trav) > 0) {
  565. //and we have a connection to the IPC hub
  566. //
  567. if(hub_fd >= 0) {
  568. // Go and toss the data to any other modules who happen to care about GPS
  569. //
  570. prepare_message(&outgoing_msg, MAILBOX_GPS_STATUS, &my_gps_stat, sizeof(my_gps_stat));
  571. send_message(hub_fd, &outgoing_msg);
  572. }
  573. // Remember that we did a stale GPS check as part of our update.
  574. //
  575. last_stale_gps_check = now;
  576. }
  577. break;
  578. // ignore any message addresses that we don't know what to do with
  579. //
  580. default:
  581. printf("Ignoring command \"%s\"\n", trav);
  582. break;
  583. }
  584. }
  585. else { }
  586. }
  587. else {
  588. fprintf(stderr, "Read from %s returned %d!\n", DRIVER_UI_PORT, read_return);
  589. // close it, flag it invalid and break out of the
  590. // for loop to allow for the while to cycle.
  591. //
  592. close(gps_fd);
  593. gps_fd = -1;
  594. break;
  595. }
  596. }
  597. }
  598. // If we're looking at the IPC hub...
  599. //
  600. else if ( fds[i].fd == hub_fd ) {
  601. // if poll says our connection to the IPC hub has died...
  602. //
  603. if(fds[i].revents & (POLLHUP | POLLERR | POLLNVAL)) {
  604. fprintf(stderr, "The connection to the IPC hub has gone away...\n"); //complain
  605. // close it, flag it invalid and break out of the
  606. // for loop to allow for the while to cycle.
  607. //
  608. close(hub_fd);
  609. hub_fd = -1;
  610. break;
  611. }
  612. // if we have mail in any of our mailboxes...
  613. //
  614. if (fds[i].revents & POLLIN) {
  615. read_return = get_message(hub_fd, &incoming_msg);
  616. if (read_return < 0) {
  617. fprintf(stderr, "The connection to the IPC hub has gone away...\n"); //complain
  618. // close it, flag it invalid and break out of the
  619. // for loop to allow for the while to cycle.
  620. //
  621. close(hub_fd);
  622. hub_fd = -1;
  623. break;
  624. }
  625. else {
  626. message_callback_return msg_status;
  627. msg_status = process_message(&incoming_msg);
  628. if (msg_status) { }
  629. }
  630. }
  631. }
  632. }
  633. }
  634. if (hub_fd >= 0) { close(hub_fd); }
  635. if (gps_fd >= 0) { close(gps_fd); }
  636. return 0;
  637. }