diu_main.c 40 KB

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