gps_main.c 20 KB

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