diu_main.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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 <openssl/md5.h>
  30. #include <ctype.h>
  31. #include <string.h>
  32. #include "../common/common_defs.h"
  33. #include "../commhub/commhub.h"
  34. #include "../commhub/client_utils.h"
  35. #include "driver.h"
  36. #include "touchscreen.h"
  37. #include "mongoose.h"
  38. #define _SLEN 1024
  39. /*
  40. #include "fbutil.h"
  41. #include "expat-2.0.1/lib/expat.h"
  42. #include "menu.h"
  43. */
  44. int diu_fd = -1;
  45. int hub_fd = -1;
  46. time_t last_hub_try = 0;
  47. time_t last_diu_clock = 0;
  48. gps_status my_gps_stat={0};
  49. time_t mkgmtime(struct tm *tm);
  50. int token_diag_serial = 0;
  51. char token_diag_string[LINE_BUFFER_SIZE] = {0};
  52. //unsigned char menu_checksum[MD5_DIGEST_LENGTH];
  53. time_t paddle_req_timeout = 0;
  54. set_paddle_req paddle_req = {0};
  55. driver_status my_driver_status={0};
  56. int update_driver_status = 0;
  57. int md5_of_file(char *filename, void *result)
  58. {
  59. FILE *f = NULL;
  60. char chunk[LINE_BUFFER_SIZE]={0};
  61. int retval = 0;
  62. MD5_CTX ctx;
  63. if(!result)
  64. return -1;
  65. if(!filename)
  66. return -1;
  67. if(!MD5_Init(&ctx))
  68. {
  69. return -1;
  70. }
  71. f = fopen(filename, "rb");
  72. if(!f)
  73. {
  74. return -1;
  75. }
  76. while(1)
  77. {
  78. retval = fread(chunk, 1, LINE_BUFFER_SIZE, f);
  79. if(retval <= 0)
  80. {
  81. break;
  82. }
  83. else
  84. {
  85. MD5_Update(&ctx, chunk, retval);
  86. }
  87. }
  88. fclose(f);
  89. MD5_Final((unsigned char *)result, &ctx);
  90. return 0;
  91. }
  92. //--------------------
  93. void beep(int fd, int hz, int milis)
  94. {
  95. char buffer[64];
  96. int n;
  97. n = sprintf(buffer, "/B:%X,%X\r", hz, milis);
  98. write(fd,buffer,n);
  99. }
  100. void set_backlight(int fd, int on)
  101. {
  102. char buffer[64];
  103. int n;
  104. n = sprintf(buffer, "/D:%c\r", on?'0':'1');
  105. write(fd,buffer,n);
  106. }
  107. void set_diu_clock(int fd, time_t tt)
  108. {
  109. struct tm t;
  110. char buffer[16];
  111. int n;
  112. localtime_r(&tt, &t);
  113. n = sprintf(buffer, "/C:%02d%02d\r", t.tm_hour, t.tm_min);
  114. write(fd, buffer, n);
  115. }
  116. //--------------------
  117. void clear_diu_messages() {
  118. }
  119. //--------------------
  120. // This function takes a GPS timestamp (in gross GPS float format + gross integer date) and
  121. //makes it into a sane UTC timestamp. If we are more than MAX_GPS_CLOCK_DRIFT seconds off
  122. //from GPS time, it sets the system clock to GPS time.
  123. int handle_gps_time(float gtime, int date)
  124. {
  125. int day,month,year;
  126. int hour,min,sec,frac;
  127. int n = 0;
  128. time_t utc,now;
  129. char buffer[32] = {0};
  130. struct message_record outgoing_msg;
  131. now = time(NULL);
  132. day = month = year = 0;
  133. hour = min = sec = frac = 0;
  134. // Just to be *ahem* clear, as per NMEA standard the date is encoded DDMMYY, and the time of day
  135. // is encoded HHMMSS[.frac] where there is an optional fractional second field denoted by a decimal point.
  136. // we must be able to decode the fractional seconds field but we discard the information since it is not
  137. // reliable enough to be worth the bother.
  138. // Whoever thought that NMEA GPS should encode the time in this format ought to be shot...
  139. n = 0; //Start out with zero parsed fields
  140. // Construct and then re-parse the time from its icky format to discrete values (this should result
  141. // in at least three (possibly four) fields.
  142. sprintf(buffer,"%010.3f", gtime);
  143. n += sscanf(buffer,"%02d%02d%02d.%d", &hour, &min, &sec, &frac);
  144. //Construct and then re-parse the date from its icky format to discrete values. This should result in three fields.
  145. sprintf(buffer,"%06d", date);
  146. n += sscanf(buffer,"%02d%02d%02d", &day, &month, &year);
  147. if(n >= 6) //if we scanned at all required fields
  148. {
  149. struct tm gpstm = {0};
  150. year += GPS_DATE_CENTURY; //GPS date only uses two digits for year, so we must assume the century
  151. gpstm.tm_year = year - 1900; //tm.tm_year is based on the year 1900
  152. gpstm.tm_mon = month - 1; //January = month 0 in struct tm.tm_mon whereas January = month 1 in an NMEA GPS date.
  153. gpstm.tm_mday = day;
  154. gpstm.tm_hour = hour;
  155. gpstm.tm_min = min;
  156. gpstm.tm_sec = sec;
  157. utc = mkgmtime(&gpstm); //Go and turn the struct tm we've just populated into an utc time stamp (seconds since epoch)
  158. my_gps_stat.gpstime = utc; //Most importantly... Remember what the self-reported GPS time stamp is.
  159. // printf("%02d-%02d-%04d %02d:%02d:%02d\n",day,month,year,hour,min,sec);
  160. // printf("CALCULATED: %d\nSYSTEM : %d\n\n",(int)utc,(int)time(NULL));
  161. if(abs(utc - now) > MAX_GPS_CLOCK_DRIFT) //if we have more than MAX_GPS_CLOCK_DRIFT seconds of clock drift
  162. {
  163. struct timeval ts = {0};
  164. ts.tv_sec = utc; //Set the timeval struct to the calculated utc timestamp from the GPS unit.
  165. ts.tv_usec = 0;
  166. settimeofday(&ts, NULL); //Set the system clock from GPS time using said timeval struct.
  167. //system("/sbin/hwclock --systohc"); //Go and push the new system clock value into the hardware realtime clock chip.
  168. if( hub_fd >= 0) //If we have a valid connection to the IPC hub
  169. {
  170. //Stick a message into the diagnostic log to record the fact that we've set the system clock from GPS
  171. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Set syatem clock from previous value (%d) to GPS time (%d).", (int)now, (int)utc );
  172. send_message(hub_fd, &outgoing_msg);
  173. }
  174. }
  175. }
  176. return 0;
  177. }
  178. #ifdef CLEAR_GPS_ON_STALE
  179. static inline void clear_stale_gps_data()
  180. {
  181. my_gps_stat.lat = my_gps_stat.lon = my_gps_stat.heading = my_gps_stat.velocity = 0;
  182. my_gps_stat.num_sats = 0;
  183. }
  184. #else
  185. static inline void clear_stale_gps_data()
  186. {
  187. }
  188. #endif
  189. static int handle_stale_gps_condition()
  190. {
  191. // This return code will be > 0 if this function generates a status change that
  192. // will then need to be communicated to other modules in the system via a message to
  193. // MAILBOX_GPS_STATUS.
  194. int return_code = 0;
  195. // This will hold the gps_good flag as it stood at the beginning of this subroutine
  196. // previous to any adjustments we make.
  197. int previous_good_flag = my_gps_stat.gps_good;
  198. int stale_time = 0;
  199. time_t now = time(NULL);
  200. stale_time = (now - my_gps_stat.stamp);
  201. // If we have entered this function with the impression that we have a valid GPS fix...
  202. if(previous_good_flag > 0)
  203. {
  204. // If it has been at least GPS_STALE_THRESHOLD seconds since the last fix
  205. if( stale_time >= GPS_STALE_THRESHOLD )
  206. {
  207. clear_stale_gps_data();
  208. my_gps_stat.gps_good = 0;
  209. return_code |= 1;
  210. }
  211. }
  212. // If we have determined that we need to declare the GPS data stale and invalid and
  213. // we have a valid connection to the IPC hub we should use that IPC hub connection to
  214. // add a note to the diagnostic log indicating that we've declared the GPS data stale.
  215. if( return_code && (hub_fd >= 0) )
  216. {
  217. struct message_record outgoing_msg;
  218. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "GPS fix has been stale for %d seconds, setting GPS = NO.", stale_time);
  219. send_message(hub_fd, &outgoing_msg);
  220. }
  221. return return_code;
  222. }
  223. int update_gps(char *in)
  224. {
  225. // This will hold the number of matched variables populated by sscanf().
  226. int num = 0;
  227. // This will allow us to know if we have made a transition from an invalid
  228. // GPS fix to a valid one so that we can log this information.
  229. int previous_good_flag = my_gps_stat.gps_good;
  230. // This return code will be > 0 if this function generates a status change that
  231. // will then need to be communicated to other modules in the system via a message to
  232. // MAILBOX_GPS_STATUS.
  233. int return_code = 0;
  234. if(!strncmp(in,"$GPRMC",6))
  235. {
  236. float f1=0;
  237. char f2=0;
  238. float f3=0;
  239. char f4=0;
  240. float f5=0;
  241. char f6=0;
  242. float f7=0;
  243. float f8=0;
  244. int f9=0;
  245. float f10=0;
  246. char f11=0;
  247. num = sscanf(in,"$GPRMC,%f,%c,%f,%c,%f,%c,%f,%f,%d,%f,%c",&f1,&f2,&f3,&f4,&f5,&f6,&f7,&f8,&f9,&f10,&f11);
  248. if(num == 11) //If we have a full GPRMC sentence we can consider setting the time
  249. {
  250. // Require at least MIN_SATS_FOR_TIME satellites to accept a new system clock value from the GPS unit.
  251. // This is to keep a crummy GPS fix from generating a bogus or unstable system time.
  252. if(my_gps_stat.num_sats >= MIN_SATS_FOR_TIME)
  253. {
  254. // Pass the time field (f1) and the date field (f9) in to the routine that sets the system clock if needed.
  255. // (this routine also stores the utc timestamp derived from the GPS date and time fields so it can be passed to
  256. // other modules that may have a need for this information).
  257. handle_gps_time(f1,f9);
  258. }
  259. }
  260. if(num > 0)
  261. {
  262. my_gps_stat.lat = f3 * ((f4 == 'N')?(1):(-1)); //update snapshot with latitude
  263. my_gps_stat.lon = f5 * ((f6 == 'E')?(1):(-1)); //longitude
  264. my_gps_stat.velocity = f7 / 1.94384449f; //meters per second (converted from knots)
  265. my_gps_stat.heading = f8; //course
  266. my_gps_stat.stamp = time(NULL); //update snapshot's staledate
  267. return_code |= 1;
  268. }
  269. }
  270. else if(!strncmp(in,"$GPGGA",6))
  271. {
  272. int f1=0;
  273. float f2=0;
  274. char f3=0;
  275. float f4=0;
  276. char f5=0;
  277. int f6=0;
  278. int f7=0;
  279. float f8=0;
  280. float f9=0;
  281. char f10=0;
  282. float f11=0;
  283. char f12=0;
  284. 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);
  285. if(num == 12)
  286. {
  287. if ( f7 >= 3 ) // require 3 satellites minimum
  288. {
  289. my_gps_stat.gps_good = f6; //store GPS valid flag in snapshot
  290. my_gps_stat.num_sats = f7; //store number of satellites in view in snapshot
  291. // Do NOT store the timestamp since we only want to remember timestamps of position
  292. // fixes and the GPGGA message is all metadata (number of satellites, fix quality, etc...).
  293. // It is worth noting that GPGGA does report altitude, but that is not a piece of information
  294. // we track because the road is where the road is, and if a bus becomes airborn we have much
  295. // bigger problems than figuring out how far off the ground it is...
  296. return_code |= 1;
  297. }
  298. }
  299. }
  300. return_code |= handle_stale_gps_condition();
  301. // If we have a connection to the IPC hub and we had previously not had a valid
  302. // GPS fix but we now do, make a note of it in the diagnostic log.
  303. if( (previous_good_flag == 0) && (my_gps_stat.gps_good != 0) && (hub_fd >= 0) )
  304. {
  305. struct message_record outgoing_msg;
  306. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "GPS fix is now valid with %d satellites. Setting GPS = YES.", my_gps_stat.num_sats);
  307. send_message(hub_fd, &outgoing_msg);
  308. }
  309. return return_code;
  310. }
  311. void maintain_ipc_hub_connect(char *progname)
  312. {
  313. struct message_record outgoing_msg;
  314. if(hub_fd < 0) //if we have no connection to the IPC hub
  315. {
  316. if( (time(NULL) - last_hub_try) > HUB_RETRY_TIME ) //if we haven't tried the hub in a few seconds
  317. {
  318. last_hub_try = time(NULL); //retry it
  319. hub_fd = connect_to_message_server(progname); //try and get one
  320. if(hub_fd >= 0)
  321. {
  322. //Subscribe to the default status messages
  323. subscribe_to_default_messages(hub_fd);
  324. //Subscribe to our specific message
  325. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_DRIVER_NOTIFY, strlen(MAILBOX_DRIVER_NOTIFY));
  326. send_message(hub_fd,&outgoing_msg);
  327. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_PADDLE_ACK, strlen(MAILBOX_PADDLE_ACK));
  328. send_message(hub_fd,&outgoing_msg);
  329. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_VAULT_DROP, strlen(MAILBOX_VAULT_DROP));
  330. send_message(hub_fd,&outgoing_msg);
  331. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_TOKEN_MAG, strlen(MAILBOX_TOKEN_MAG));
  332. send_message(hub_fd,&outgoing_msg);
  333. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_TOKEN_RFID, strlen(MAILBOX_TOKEN_RFID));
  334. send_message(hub_fd,&outgoing_msg);
  335. //Ask for a status update
  336. prepare_message(&outgoing_msg, MAILBOX_STATUS_REQUEST, "", 0);
  337. send_message(hub_fd,&outgoing_msg);
  338. }
  339. else
  340. {
  341. fprintf(stderr, "Cannot connect to IPC hub!\n");
  342. }
  343. }
  344. }
  345. }
  346. //menutree *mt = NULL;
  347. //int redraw_flag = 0;
  348. message_callback_return handle_status_request(struct message_record *msg, void *param)
  349. {
  350. struct message_record outgoing_msg;
  351. if(hub_fd >= 0)
  352. {
  353. prepare_message(&outgoing_msg, MAILBOX_DRIVER_STATUS, &my_driver_status, sizeof(my_driver_status));
  354. send_message(hub_fd, &outgoing_msg);
  355. prepare_message(&outgoing_msg, MAILBOX_GPS_STATUS, &my_gps_stat, sizeof(my_gps_stat));
  356. send_message(hub_fd, &outgoing_msg);
  357. }
  358. return MESSAGE_HANDLED_CONT;
  359. }
  360. message_callback_return handle_vault_drop(struct message_record *msg, void *param)
  361. {
  362. if(diu_fd >= 0)
  363. {
  364. write(diu_fd, "/V:\r", 4);
  365. }
  366. return MESSAGE_HANDLED_CONT;
  367. }
  368. char dup_notify_str[MAX_PAYLOAD_LENGTH] = {0};
  369. int dup_notify_count = 0;
  370. long long dup_notify_usec = 0;
  371. message_callback_return handle_driver_notify(struct message_record *msg, void *param)
  372. {
  373. int is_dup;
  374. char dup_text[MAX_PAYLOAD_LENGTH] = {0};
  375. //pixel bgcolor = FBCOLOR_WHITE;
  376. //pixel textcolor = FBCOLOR_BLACK;
  377. long long dup_usec_delta = 0;
  378. if(strncmp((const char *)(msg->payload), dup_notify_str, MAX_PAYLOAD_LENGTH))
  379. {
  380. dup_notify_count = 1;
  381. strncpy(dup_notify_str, (const char *)(msg->payload), MAX_PAYLOAD_LENGTH - 1);
  382. dup_notify_str[MAX_PAYLOAD_LENGTH - 1] = '\0';
  383. is_dup = 0;
  384. dup_notify_usec = 0;
  385. }
  386. else
  387. {
  388. dup_notify_count++;
  389. is_dup = 1;
  390. dup_usec_delta = get_usec_time() - dup_notify_usec;
  391. dup_notify_usec = get_usec_time();
  392. }
  393. switch(msg->payload[0])
  394. {
  395. case LOGLEVEL_EVENT:
  396. if(!is_dup || (dup_usec_delta >= DUP_USEC_BEEP_THRESHOLD))
  397. {
  398. DIU_ACK_BEEP(diu_fd);
  399. }
  400. break;
  401. case LOGLEVEL_REJECT:
  402. //bgcolor = FBCOLOR_LTRED;
  403. if(!is_dup || (dup_usec_delta >= DUP_USEC_BEEP_THRESHOLD))
  404. {
  405. DIU_ERROR_BEEP(diu_fd);
  406. }
  407. break;
  408. case LOGLEVEL_ACCEPT:
  409. //bgcolor = FBCOLOR_LTGREEN;
  410. if(!is_dup || (dup_usec_delta >= DUP_USEC_BEEP_THRESHOLD))
  411. {
  412. DIU_ACK_BEEP(diu_fd);
  413. }
  414. break;
  415. case LOGLEVEL_ERROR:
  416. //bgcolor = FBCOLOR_RED;
  417. //textcolor = FBCOLOR_CYAN;
  418. if(!is_dup || (dup_usec_delta >= DUP_USEC_BEEP_THRESHOLD))
  419. {
  420. DIU_CRITICAL_BEEP(diu_fd);
  421. }
  422. break;
  423. default:
  424. break;
  425. }
  426. if(is_dup)
  427. {
  428. snprintf(dup_text, MAX_PAYLOAD_LENGTH, "%d x %s", dup_notify_count, &msg->payload[1]);
  429. //replace_diu_message(bgcolor, textcolor, dup_text);
  430. }
  431. else
  432. {
  433. //add_diu_message(bgcolor, textcolor, (char *)(&(msg->payload[1])));
  434. }
  435. //redraw_flag |= 1;
  436. return MESSAGE_HANDLED_CONT;
  437. }
  438. message_callback_return handle_paddle_ack(struct message_record *msg, void *param)
  439. {
  440. set_paddle_req *pr = (set_paddle_req *)msg->payload;
  441. paddle_req.result = pr->result;
  442. return MESSAGE_HANDLED_CONT;
  443. }
  444. message_callback_return handle_token_diag(struct message_record *msg, void *param)
  445. {
  446. strncpy(token_diag_string, (const char *)(msg->payload), sizeof(token_diag_string));
  447. token_diag_string[sizeof(token_diag_string) - 1] = '\0';
  448. token_diag_serial++;
  449. return MESSAGE_HANDLED_CONT;
  450. }
  451. //----------------------------------
  452. time_t diu_error_burst = 0;
  453. int diu_error_counter = 0;
  454. static inline int can_report_diu_error()
  455. {
  456. time_t now = time(NULL);
  457. //If our last potential burst lockout has expired...
  458. if( (now - diu_error_burst) >= DIU_ERROR_RATE_LIMIT)
  459. {
  460. diu_error_counter = 0; //reset our burst counter
  461. diu_error_burst = now; //and start the "potential burst" time at this message
  462. }
  463. if(diu_error_counter < DIU_ERROR_BURST_LIMIT) //if we haven't hit our burst limit...
  464. {
  465. diu_error_counter++; //count this message against our burst limit
  466. return 1; //and allow it to pass
  467. }
  468. else //if we have hit our limit
  469. {
  470. return 0; //ignore this message
  471. }
  472. }
  473. //----------------------------------
  474. //-------- web server functions ----
  475. static const char *s_http_port = "60535";
  476. static struct mg_serve_http_opts s_http_server_opts;
  477. static int is_websocket(const struct mg_connection *nc) {
  478. return nc->flags & MG_F_IS_WEBSOCKET;
  479. }
  480. static void send_ws_heartbeat(struct mg_connection *nc) {
  481. int n;
  482. char buf[_SLEN];
  483. struct mg_connection *c;
  484. for (c=mg_next(nc->mgr, NULL); c ; c = mg_next(nc->mgr, c)) {
  485. if (c==nc) { continue; }
  486. if (!is_websocket(c)) { continue; }
  487. printf("...\n");
  488. snprintf(buf, _SLEN, "hello");
  489. n = strlen(buf);
  490. mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, buf, n);
  491. }
  492. }
  493. static void process_ws_message(struct websocket_message *ws_msg) {
  494. int i;
  495. char *data;
  496. data = (char *)(ws_msg->data);
  497. printf("#ws, got(%i)\n:", (int)(ws_msg->size));
  498. for (i=0; i<ws_msg->size; i++) {
  499. printf("%c", data[i]);
  500. }
  501. printf("\n");
  502. }
  503. //
  504. // ui wants status information
  505. //
  506. static void handle_status_input(struct mg_connection *nc, struct http_message *hm) {
  507. char buf[_SLEN];
  508. //char msg[][_SLEN] = { "fail error", "ok .", };
  509. //DEBUG sample message
  510. snprintf(buf, _SLEN, "ok msg=status\nroute=%s|trip=%s|stop=%s|gps=%i|tunnel=%i|date=%s|eqiupno=%s|nmsg=%s|last_token=%s|config=%s|firmware=%s|imei=%s|imsi=%s|eth0=%s",
  511. "9900", "1", "0",
  512. 1, 1,
  513. "2019-08-13 09;23:00",
  514. "9999", "0",
  515. "",
  516. "20190730v403 2019-11-05 02:36:43",
  517. "1.11 2019-07-30 16:33:08",
  518. "356136074279052",
  519. "310260877138191",
  520. "00:80:66:10:E8:8A");
  521. /*
  522. printf(">> status:\nbuf(%i):\n%s\n", (int)strlen(buf), buf);
  523. //snprintf(buf, _SLEN, "ok xxx=foo bar;yyy=baz qux;...");
  524. printf(">> status\n");
  525. */
  526. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  527. (unsigned long)strlen(buf), buf);
  528. }
  529. // driver logout
  530. //
  531. static void handle_logout_input(struct mg_connection *nc, struct http_message *hm) {
  532. char msg[][_SLEN] = {
  533. "fail error",
  534. "ok .",
  535. };
  536. printf(">> logout\n");
  537. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  538. (unsigned long)strlen(msg[1]), msg[1]);
  539. }
  540. // (manual) next stop pressed
  541. //
  542. static void handle_nextstop_input(struct mg_connection *nc, struct http_message *hm) {
  543. char msg[][_SLEN] = {
  544. "fail error",
  545. "ok .",
  546. };
  547. printf(">> nextstop\n");
  548. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  549. (unsigned long)strlen(msg[1]), msg[1]);
  550. }
  551. // (manual) previous stop pressed
  552. //
  553. static void handle_prevstop_input(struct mg_connection *nc, struct http_message *hm) {
  554. char msg[][_SLEN] = {
  555. "fail error",
  556. "ok .",
  557. };
  558. printf(">> prevstop\n");
  559. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  560. (unsigned long)strlen(msg[1]), msg[1]);
  561. }
  562. // generic fare input
  563. //
  564. static void handle_fare_input(struct mg_connection *nc, struct http_message *hm) {
  565. int ret;
  566. char s_fare[_SLEN], s_count[_SLEN];
  567. char msg[][_SLEN] = {
  568. "fail error",
  569. "ok .",
  570. };
  571. ret = mg_get_http_var(&(hm->body), "fare", s_fare, _SLEN);
  572. if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; }
  573. printf("got fare %s\n", s_fare);
  574. ret = mg_get_http_var(&(hm->body), "count", s_count, _SLEN);
  575. if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; }
  576. printf("got count %s\n", s_count);
  577. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  578. (unsigned long)strlen(msg[1]), msg[1]);
  579. }
  580. // driver paddle (route) input
  581. //
  582. static void handle_paddle_input(struct mg_connection *nc, struct http_message *hm) {
  583. int ret;
  584. char s_paddle[_SLEN];
  585. char msg[][_SLEN] = {
  586. "fail paddle_unknown",
  587. "ok paddle",
  588. };
  589. ret = mg_get_http_var(&(hm->body), "paddle", s_paddle, _SLEN);
  590. if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; }
  591. //DEBUG
  592. printf("#got paddle %s\n", s_paddle);
  593. //DEBUG
  594. if (strncmp(s_paddle,"9900", strlen("9900"))!=0) {
  595. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  596. (unsigned long)strlen(msg[0]), msg[0]);
  597. return;
  598. }
  599. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  600. (unsigned long)strlen(msg[1]), msg[1]);
  601. }
  602. // driver login
  603. //
  604. static void handle_driver_login(struct mg_connection *nc, struct http_message *hm) {
  605. int ret;
  606. char s_driver[_SLEN], s_pin[_SLEN];
  607. int idrv=-1;
  608. char msg_fail[] = "fail driver_login";
  609. char msg_success[] = "ok driver";
  610. ret = mg_get_http_var(&(hm->body), "driver", s_driver, _SLEN);
  611. if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; }
  612. ret = mg_get_http_var(&(hm->body), "pin", s_pin, _SLEN);
  613. if (ret<=0) { mg_http_send_error(nc, 404, NULL); return; }
  614. idrv = atoi(s_driver);
  615. ret = driver_login(idrv, s_pin);
  616. if (ret!=0) {
  617. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  618. (unsigned long)strlen(msg_fail), msg_fail);
  619. return;
  620. }
  621. mg_printf(nc, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n%s",
  622. (unsigned long)strlen(msg_success), msg_success);
  623. }
  624. // api point
  625. //
  626. static void handle_req(struct mg_connection *nc, struct http_message *hm) {
  627. int ret;
  628. char s_action[_SLEN];
  629. char _default_msg[_SLEN] = "";
  630. struct mg_str hdr;
  631. ret = mg_get_http_var(&(hm->body), "action", s_action, _SLEN);
  632. if (ret==0) {
  633. printf(">>> %p\n", s_action);
  634. mg_http_send_error(nc, 404, NULL);
  635. return;
  636. }
  637. printf("#req: action: (%s)\n", s_action);
  638. if (strncmp(s_action, "driverlogin", strlen("driverlogin"))==0) {
  639. handle_driver_login(nc, hm);
  640. }
  641. else if (strncmp(s_action, "paddleinput", strlen("paddleinput"))==0) {
  642. handle_paddle_input(nc, hm);
  643. }
  644. else if (strncmp(s_action, "prevstop", strlen("prevstop"))==0) {
  645. handle_prevstop_input(nc, hm);
  646. }
  647. else if (strncmp(s_action, "nextstop", strlen("nextstop"))==0) {
  648. handle_nextstop_input(nc, hm);
  649. }
  650. else if (strncmp(s_action, "status", strlen("status"))==0) {
  651. handle_status_input(nc, hm);
  652. }
  653. else if (strncmp(s_action, "logout", strlen("logout"))==0) {
  654. handle_logout_input(nc, hm);
  655. }
  656. else if (strncmp(s_action, "fare", strlen("fare"))==0) {
  657. handle_fare_input(nc, hm);
  658. }
  659. else {
  660. mg_http_send_error(nc, 404, NULL);
  661. }
  662. }
  663. static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
  664. struct http_message *hm = (struct http_message *) ev_data;
  665. char buf[1024];
  666. int debug_print = 0;
  667. if (debug_print) {
  668. mg_sock_addr_to_str(&(nc->sa), buf, 1023, MG_SOCK_STRINGIFY_IP);
  669. printf("%s\n", buf);
  670. }
  671. switch (ev){
  672. case MG_EV_HTTP_REQUEST:
  673. printf("http request\n");
  674. if (mg_vcmp(&hm->uri, "/req")==0) {
  675. handle_req(nc, (struct http_message *)ev_data);
  676. }
  677. else {
  678. mg_serve_http(nc, (struct http_message *) ev_data, s_http_server_opts);
  679. }
  680. break;
  681. case MG_EV_WEBSOCKET_HANDSHAKE_DONE:
  682. printf("ws handshake done\n");
  683. break;
  684. case MG_EV_WEBSOCKET_FRAME:
  685. process_ws_message((struct websocket_message *)ev_data);
  686. break;
  687. case MG_EV_CLOSE:
  688. if (is_websocket(nc)) { printf("ws closed\n"); }
  689. break;
  690. default:
  691. //printf("? %i\n", ev);
  692. break;
  693. }
  694. }
  695. //-------- web server functions ----
  696. int main(int argc, char **argv)
  697. {
  698. char line[LINE_BUFFER_SIZE] = {0};
  699. struct message_record incoming_msg;
  700. struct message_record outgoing_msg;
  701. struct pollfd fds[32];
  702. int nfd=0;
  703. int poll_return;
  704. int read_return;
  705. int i;
  706. time_t down_time = 0;
  707. int calibration = 0;
  708. time_t now;
  709. int retval = 0;
  710. int tz = 0;
  711. int tx = 0;
  712. int ty = 0;
  713. time_t last_stale_gps_check = 0;
  714. // setup mongoose web server
  715. //
  716. struct mg_mgr mgr;
  717. struct mg_connection *nc;
  718. mg_mgr_init(&mgr, NULL);
  719. nc = mg_bind(&mgr, s_http_port, ev_handler);
  720. if (!nc) {
  721. printf("failed to create listener\n");
  722. return 1;
  723. }
  724. mg_set_protocol_http_websocket(nc);
  725. s_http_server_opts.document_root = "html";
  726. s_http_server_opts.enable_directory_listing = "no";
  727. /*
  728. if(open_framebuffer())
  729. {
  730. fprintf(stderr,"Cannot open framebuffer!\n");
  731. }
  732. mt = load_menutree(DRIVER_MENU_FILE);
  733. if(!mt)
  734. {
  735. fprintf(stderr, "Error loading %s\n", DRIVER_MENU_FILE);
  736. return -1;
  737. }
  738. else
  739. {
  740. //Remember a checksum so we don't have to boot a logged in driver for a system-wide HUP if
  741. //the menu tree hasn't indeed changed...
  742. md5_of_file(DRIVER_MENU_FILE, menu_checksum);
  743. // print_menu_tree(mt);
  744. if(init_menutree(mt))
  745. {
  746. fprintf(stderr, "Error initializing menu tree from %s\n", DRIVER_MENU_FILE);
  747. }
  748. redraw_flag = 1;
  749. }
  750. */
  751. //Configure our signal handlers to deal with SIGINT, SIGTERM, etc... and make graceful exits while logging
  752. configure_signal_handlers(argv[0]);
  753. //Make an initial attempt to get in touch with the interprocess communication hub (it may not be up yet depending on the start order)
  754. maintain_ipc_hub_connect(argv[0]);
  755. //Register our defualt system message processing callbacks
  756. register_system_status_callbacks();
  757. register_dispatch_callback(MAILBOX_STATUS_REQUEST, CALLBACK_USER(1), handle_status_request, NULL);
  758. register_dispatch_callback(MAILBOX_DRIVER_NOTIFY, CALLBACK_USER(2), handle_driver_notify, NULL);
  759. register_dispatch_callback(MAILBOX_PADDLE_ACK, CALLBACK_USER(3), handle_paddle_ack, NULL);
  760. register_dispatch_callback(MAILBOX_VAULT_DROP, CALLBACK_USER(4), handle_vault_drop, NULL);
  761. register_dispatch_callback(MAILBOX_TOKEN_MAG, CALLBACK_USER(5), handle_token_diag, NULL);
  762. register_dispatch_callback(MAILBOX_TOKEN_RFID, CALLBACK_USER(6), handle_token_diag, NULL);
  763. clear_diu_messages();
  764. // This is the main dispatch loop:
  765. //
  766. // * reset watchdog to make sure we haven't crashed/frozen
  767. // * if need be, open a connection to the DIU microcontroller, quieting all messages except for acks
  768. // * handle GPS message dispatch through IPC
  769. // * if need be, handle reload of menu.xml
  770. // * manage driver status message communication
  771. // * handle paddle change
  772. // * draw menu
  773. // * listen on the mailboxes for messages and process. This includes
  774. // - touchscreen events
  775. // - gps updates
  776. // - warning/debug/error messages
  777. //
  778. while( exit_request_status == EXIT_REQUEST_NONE ) //loop until we get asked to exit...
  779. {
  780. //DEBUG
  781. //printf("[%lli] diu_minder: heartbeat\n", get_usec_time());
  782. //DEBUG
  783. RESET_WATCHDOG();
  784. // service our UI updates
  785. //mg_mgr_poll(&mgr, POLL_TIMEOUT);
  786. maintain_ipc_hub_connect(argv[0]);
  787. if(diu_fd < 0)
  788. {
  789. diu_fd = open_rs232_device(DRIVER_UI_PORT, USE_DEFAULT_BAUD, RS232_LINE);
  790. if(diu_fd < 0)
  791. {
  792. //fprintf(stderr, "Cannot open serial port %s for DIU!\n", DRIVER_UI_PORT);
  793. }
  794. else
  795. {
  796. write(diu_fd, "/Q:aK\r", 6); //Turn on all messages except ACKs for sent commands
  797. }
  798. }
  799. now = time(NULL);
  800. //Every second, we want to update the DIU clock (even though it only shows minutes, this covers power events...)
  801. if((now - last_diu_clock) > 0)
  802. {
  803. if(diu_fd)
  804. {
  805. set_diu_clock(diu_fd, now);
  806. last_diu_clock = now;
  807. }
  808. //Also request a UI redraw if nothing else has, just so that status line gets redrawn
  809. //redraw_flag |= 1;
  810. }
  811. //Every second we want to check to make sure that our GPS data have note gone stale...
  812. if((now - last_stale_gps_check) > 0)
  813. {
  814. // If the stale check results in an update to my_gps_stat, we must update any other
  815. // modules which may be tracking GPS status via the IPC hub.
  816. if( handle_stale_gps_condition() > 0 )
  817. {
  818. //If we have a connection to the IPC hub
  819. if(hub_fd >= 0)
  820. {
  821. //Go and toss the data to any other modules who happen to care about GPS
  822. prepare_message(&outgoing_msg, MAILBOX_GPS_STATUS, &my_gps_stat, sizeof(my_gps_stat));
  823. send_message(hub_fd, &outgoing_msg);
  824. }
  825. }
  826. // Either way, remember that we did this stale check.
  827. last_stale_gps_check = now;
  828. }
  829. if(hup_request_status)
  830. {
  831. //unsigned char temp_menu_checksum[MD5_DIGEST_LENGTH];
  832. hup_request_status = 0;
  833. /*
  834. //Go and generate a checksum of the new menu file
  835. md5_of_file(DRIVER_MENU_FILE, temp_menu_checksum);
  836. //if it differs from the old one, reload the menu file...
  837. if(memcmp(menu_checksum, temp_menu_checksum, MD5_DIGEST_LENGTH))
  838. {
  839. //Update the remembered checksum to be that of the new menu...
  840. memcpy(menu_checksum, temp_menu_checksum, MD5_DIGEST_LENGTH);
  841. //Free the old menu tree
  842. if(mt)
  843. {
  844. free_menutree(mt);
  845. mt = NULL;
  846. }
  847. //Try and load the new one...
  848. mt = load_menutree(DRIVER_MENU_FILE);
  849. //If that failed, bitch about it!
  850. if(!mt)
  851. {
  852. fprintf(stderr, "Error loading %s\n", DRIVER_MENU_FILE);
  853. return -1;
  854. }
  855. else //otherwise initialize the menu tree
  856. {
  857. if(init_menutree(mt))
  858. {
  859. fprintf(stderr, "Error initializing menu tree from %s\n", DRIVER_MENU_FILE);
  860. }
  861. redraw_flag = 1;
  862. }
  863. }
  864. */
  865. }
  866. //If it is time to send out a driver status update
  867. if(update_driver_status && (hub_fd >= 0))
  868. {
  869. //do so...
  870. prepare_message(&outgoing_msg, MAILBOX_DRIVER_STATUS, &my_driver_status, sizeof(my_driver_status));
  871. send_message(hub_fd, &outgoing_msg);
  872. update_driver_status = 0;
  873. }
  874. //If a paddle change request has resulted in a change
  875. //if(check_paddle_request(mt))
  876. if(check_paddle_request())
  877. {
  878. //do a redraw of the UI
  879. //redraw_flag |= 1;
  880. }
  881. //If we have to redraw the UI
  882. //if(redraw_flag && !calibration)
  883. if(!calibration)
  884. {
  885. //Redraw the menu reflecting any changes from the last touchscreen input
  886. //or other stimulus
  887. //draw_menu(mt);
  888. #ifdef TOUCHSCREEN_QUIET
  889. write(diu_fd, "/Q:t\r", 5); //Un-Quiet the touch screen, now that we've responded to the user we want
  890. #endif //to hear if they have any further input to give us...
  891. //redraw_flag = 0; //Clear the 'redraw required' flag
  892. }
  893. nfd = 0;
  894. if(hub_fd >= 0)
  895. {
  896. fds[nfd].fd = hub_fd;
  897. fds[nfd].events = POLLIN;
  898. fds[nfd].revents = 0;
  899. nfd++;
  900. }
  901. if(diu_fd >= 0)
  902. {
  903. fds[nfd].fd = diu_fd;
  904. fds[nfd].events = POLLIN;
  905. fds[nfd].revents = 0;
  906. nfd++;
  907. }
  908. // experimental
  909. //
  910. for (nc = mgr.active_connections; nc != NULL; nc = nc->next) {
  911. //printf(" cp2...%p\n", nc);
  912. if (nc->sock != INVALID_SOCKET) {
  913. if (nfd < 32) {
  914. fds[nfd].fd = nc->sock;
  915. fds[nfd].events = POLLIN | POLLOUT;
  916. fds[nfd].revents = 0;
  917. nfd++;
  918. }
  919. }
  920. }
  921. //printf("nfd %i\n", nfd);
  922. if(nfd > 0) //if we have any file descriptors, poll them
  923. {
  924. poll_return = poll(fds, nfd, POLL_TIMEOUT);
  925. }
  926. else //otherwise, whistle and look busy
  927. {
  928. poll_return = 0; //(this keeps us from buringing 100% cpu cycles if we don't have contact with either
  929. sleep(1); //the IPC hub or the DIU hardware).
  930. }
  931. //--------------------------------------------------------------------------------------------------
  932. if(poll_return < 1) //if poll didn't net us any work to do,
  933. {
  934. //printf("no poll...\n");
  935. continue; //lets try again
  936. }
  937. for(i = 0; i < nfd; i++) //Loop through all polled file descriptors
  938. {
  939. if( fds[i].fd == diu_fd ) //If we're looking at the DIU...
  940. {
  941. //DEBUG
  942. printf("##diu_fd\n");
  943. if(fds[i].revents & (POLLHUP | POLLERR | POLLNVAL)) //if poll says our serial port has become bogus...
  944. {
  945. fprintf(stderr, "This is very odd... Poll returned flags %d on our serial port...\n", fds[i].revents);
  946. close(diu_fd); //close it
  947. diu_fd = -1; //flag it invalid
  948. break; //and break out of the for loop to allow the while to cycle
  949. }
  950. if(fds[i].revents & POLLIN)
  951. {
  952. read_return = read(fds[i].fd, line, sizeof(line));
  953. if(read_return > 0)
  954. {
  955. char *trav = line;
  956. line[read_return] = '\0';
  957. strip_crlf(line);
  958. while(*trav && (*trav != '/') ) //advance until EOL or we hit our start sentinel
  959. {
  960. trav++;
  961. }
  962. //Check to see that our address header is intact...
  963. if( (trav[0] == '/') && (trav[2] == ':') )
  964. {
  965. switch(trav[1])
  966. {
  967. case 'T': //-----------------------------------"/T:" means it's a touchscreen event
  968. trav += 3; //advance past the header
  969. retval = sscanf(trav, "%x,%x,%x", &tz, &tx, &ty);
  970. if(retval == 3)
  971. {
  972. if(tz)
  973. {
  974. if(down_time == 0)
  975. {
  976. down_time = time(NULL);
  977. }
  978. else
  979. {
  980. if( (time(NULL) - down_time) > TS_CALIB_HOLD_TIME)
  981. {
  982. begin_touchscreen_calibration();
  983. calibration = 1;
  984. }
  985. }
  986. if(!calibration)
  987. {
  988. /*
  989. //printf("Touch at (%d, %d)\n", translate_ts_x(tx), translate_ts_y(ty));
  990. if(process_pen(mt, translate_ts_x(tx), translate_ts_y(ty), 1) > 0)
  991. {
  992. #ifdef TOUCHSCREEN_QUIET
  993. write(diu_fd, "/Q:T\r", 5);
  994. #endif
  995. //redraw_flag = 1;
  996. }
  997. */
  998. }
  999. else
  1000. {
  1001. if(advance_touchscreen_calibration(tx, ty, tz))
  1002. {
  1003. calibration = 0;
  1004. //redraw_flag = 1;
  1005. }
  1006. else
  1007. {
  1008. draw_touchscreen_calibration();
  1009. }
  1010. }
  1011. }
  1012. else
  1013. {
  1014. down_time = 0;
  1015. //printf("Touch Release\n");
  1016. if(!calibration)
  1017. {
  1018. /*
  1019. if(process_pen(mt, 0,0,0) > 0)
  1020. {
  1021. #ifdef TOUCHSCREEN_QUIET
  1022. write(diu_fd, "/Q:T\r", 5);
  1023. #endif
  1024. //redraw_flag = 1;
  1025. }
  1026. */
  1027. }
  1028. else
  1029. {
  1030. if(advance_touchscreen_calibration(tx, ty, tz))
  1031. {
  1032. calibration = 0;
  1033. //redraw_flag = 1;
  1034. }
  1035. else
  1036. {
  1037. draw_touchscreen_calibration();
  1038. }
  1039. }
  1040. }
  1041. }
  1042. break;
  1043. case 'G': //-----------------------------------"/G:" means it is a new GPS input
  1044. trav += 3; //advance past the header
  1045. //If this GPS update constitutes a meaningful piece of data
  1046. if(update_gps(trav) > 0)
  1047. {
  1048. //and we have a connection to the IPC hub
  1049. if(hub_fd >= 0)
  1050. {
  1051. //Go and toss the data to any other modules who happen to care about GPS
  1052. prepare_message(&outgoing_msg, MAILBOX_GPS_STATUS, &my_gps_stat, sizeof(my_gps_stat));
  1053. send_message(hub_fd, &outgoing_msg);
  1054. }
  1055. last_stale_gps_check = now; //Remember that we did a stale GPS check as part of our update.
  1056. }
  1057. break;
  1058. case '*': //handle warnings
  1059. case '#': //debugs
  1060. case '!': //and errors
  1061. //If this DIU error/debug message has not run afoul of the rate limiting policy...
  1062. if( can_report_diu_error() )
  1063. {
  1064. format_log_message(&outgoing_msg, trav[1], "DIU Reports: %s", trav + 3); //send them all to the log server
  1065. send_message(hub_fd, &outgoing_msg);
  1066. if(trav[1] == '!') //but in the case of errors, send them to the driver too
  1067. {
  1068. format_driver_message(&outgoing_msg, trav[1], "DIU Reports: %s", trav + 3);
  1069. send_message(hub_fd, &outgoing_msg);
  1070. }
  1071. }
  1072. break;
  1073. default: //ignore any message addresses that we don't know what to do with
  1074. printf("Ignoring command \"%s\"\n", trav);
  1075. break;
  1076. }
  1077. }
  1078. else
  1079. {
  1080. // printf("Ignoring non-command line \"%s\"\n", trav);
  1081. }
  1082. }
  1083. else
  1084. {
  1085. fprintf(stderr, "Read from %s returned %d!\n", DRIVER_UI_PORT, read_return);
  1086. close(diu_fd); //close it
  1087. diu_fd = -1; //flag it invalid
  1088. break; //and break out of the for loop to allow the while to cycle
  1089. }
  1090. }
  1091. }
  1092. else if( fds[i].fd == hub_fd ) //If we're looking at the IPC hub...
  1093. {
  1094. //DEBUG
  1095. //printf("##hub_fd\n");
  1096. if(fds[i].revents & (POLLHUP | POLLERR | POLLNVAL)) //if poll says our connection to the IPC hub has died...
  1097. {
  1098. fprintf(stderr, "The connection to the IPC hub has gone away...\n"); //complain
  1099. close(hub_fd); //close it
  1100. hub_fd = -1; //flag it dead
  1101. break; //break out of the for loop
  1102. }
  1103. if(fds[i].revents & POLLIN) //if we have mail in any of our mailboxes...
  1104. {
  1105. read_return = get_message(hub_fd, &incoming_msg);
  1106. if(read_return < 0)
  1107. {
  1108. fprintf(stderr, "The connection to the IPC hub has gone away...\n"); //complain
  1109. close(hub_fd); //close it
  1110. hub_fd = -1; //flag it dead
  1111. break; //break out of the for loop
  1112. }
  1113. else
  1114. {
  1115. message_callback_return msg_status;
  1116. msg_status = process_message(&incoming_msg);
  1117. }
  1118. }
  1119. }
  1120. else {
  1121. //printf("##mongoos\n");
  1122. if (fds[i].revents & (POLLIN | POLLOUT)) {
  1123. mg_mgr_poll(&mgr, 10);
  1124. }
  1125. }
  1126. }
  1127. }
  1128. //set_color(255,255,255);
  1129. //cls();
  1130. //present_framebuffer();
  1131. //close_framebuffer();
  1132. if(hub_fd >= 0)
  1133. {
  1134. close(hub_fd);
  1135. }
  1136. if(diu_fd >= 0)
  1137. {
  1138. write(diu_fd, "/C:----\r",8);
  1139. close(diu_fd);
  1140. }
  1141. return 0;
  1142. }