piu_main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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 <stdio.h>
  21. #include <stdlib.h>
  22. #include <poll.h>
  23. #include <unistd.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <signal.h>
  27. #include <time.h>
  28. #include "../common/common_defs.h"
  29. #include "../commhub/commhub.h"
  30. #include "../commhub/client_utils.h"
  31. int piu_fd = -1;
  32. int hub_fd = -1;
  33. device_test_vector piu_test_vector = {
  34. .dev_id = "rider_ui",
  35. .init_string = "/0:\r/1:\r/m:09 00 03 42 5A 44 56\r",
  36. .n_reply_lines = 4,
  37. .reply_strings = (char *[]){NULL, NULL, "/OK:m:09 00 03 42 5A 44 56", "/M:^"},
  38. };
  39. char diag[DIAG_BUFFER_SIZE] = {0};
  40. #define PIU_NUM_LINES 2
  41. static char *piu_set_line_cmd[PIU_NUM_LINES] = {"/0:", "/1:"};
  42. static piu_message def_msgs[PIU_NUM_LINES] = {{0}};
  43. static piu_message cur_msgs[PIU_NUM_LINES] = {{0}};
  44. int update_piu_display(int line) {
  45. piu_message *msg;
  46. if( (line >= PIU_NUM_LINES) || (line < 0) ) {
  47. return -1;
  48. }
  49. if(piu_fd < 0) {
  50. return -1;
  51. }
  52. msg = (cur_msgs[line].seconds == 0)?(def_msgs + line):(cur_msgs + line);
  53. // write the command prefix
  54. //
  55. write(piu_fd, piu_set_line_cmd[line], 3);
  56. // write the actual message
  57. //
  58. write(piu_fd, msg->message, strlen(msg->message));
  59. // write the CR to signal EOL
  60. //
  61. write(piu_fd, "\r", 1);
  62. //printf("%s%s\n",piu_set_line_cmd[line], msg->message);
  63. return 0;
  64. }
  65. message_callback_return handle_display_message(struct message_record *msg, void *param) {
  66. piu_message *cmd = (piu_message *) msg->payload;
  67. piu_message *tgt = NULL;
  68. // obtain a timestamp for NOW
  69. //
  70. time_t now = time(NULL);
  71. int touched = 0;
  72. // if we have an update for a line that doesn't exist, ignore it
  73. //
  74. if( (cmd->line >= PIU_NUM_LINES) || (cmd->line < 0) ) {
  75. return MESSAGE_HANDLED_CONT;
  76. }
  77. // if we're updating the default message
  78. //
  79. if(cmd->seconds == 0) {
  80. // get a pointer to our message target
  81. //
  82. tgt = def_msgs + cmd->line;
  83. // just do it...
  84. //
  85. memcpy(tgt, cmd, sizeof(piu_message));
  86. // force terminate the string just in case
  87. //
  88. tgt->message[PIU_MESSAGE_LEN - 1] = '\0';
  89. // priority is meaningless for default messages
  90. //
  91. tgt->priority = 0;
  92. //if the active message on this line has expired, a change in
  93. // default message will need to trigger an update
  94. //
  95. if(cur_msgs[cmd->line].seconds < now) {
  96. touched = 1;
  97. }
  98. }
  99. else
  100. {
  101. // get a pointer to our message target
  102. //
  103. tgt = cur_msgs + cmd->line;
  104. // If the current message has expired or will expire this second (thus bringing up the default message)
  105. // OR the new message is of higher priority then we need to do the repacement
  106. //
  107. if( (tgt->seconds <= now) || (cmd->priority >= tgt->priority) ) {
  108. // perform the copy
  109. //
  110. memcpy(tgt, cmd, sizeof(piu_message));
  111. // force terminate the string just in case
  112. //
  113. tgt->message[PIU_MESSAGE_LEN - 1] = '\0';
  114. // set the expiration time on the new message to the timestamp of NOW
  115. // plus the desired duration
  116. //
  117. tgt->seconds = now + cmd->seconds;
  118. touched = 1;
  119. }
  120. }
  121. // if we have made any changes...
  122. //
  123. if(touched) {
  124. update_piu_display(cmd->line);
  125. }
  126. return MESSAGE_HANDLED_CONT;
  127. }
  128. int handle_message_timeouts() {
  129. int i;
  130. time_t now = time(NULL);
  131. // For each display line
  132. //
  133. for(i = 0; i < PIU_NUM_LINES; i++) {
  134. // Check to see if it has an active current message
  135. //
  136. if(cur_msgs[i].seconds != 0) {
  137. // If so, see if that message has passed its expiration time
  138. //
  139. if(cur_msgs[i].seconds < now) {
  140. // if it has...
  141. //
  142. cur_msgs[i].seconds = 0;
  143. // flag it inactive
  144. // and update the PIU
  145. //
  146. update_piu_display(i);
  147. }
  148. }
  149. }
  150. return 0;
  151. }
  152. #ifdef BEEP_WITH_MAGSTRIPE_READER
  153. char dup_notify_str[MAX_PAYLOAD_LENGTH] = {0};
  154. long long dup_notify_usec = 0;
  155. message_callback_return handle_DIU_beep(struct message_record *msg, void *param) {
  156. int is_dup;
  157. long long dup_usec_delta = 0;
  158. if(strncmp(msg->payload, dup_notify_str, MAX_PAYLOAD_LENGTH)) {
  159. strncpy(dup_notify_str, msg->payload, MAX_PAYLOAD_LENGTH - 1);
  160. dup_notify_str[MAX_PAYLOAD_LENGTH - 1] = '\0';
  161. is_dup = 0;
  162. dup_notify_usec = 0;
  163. }
  164. else {
  165. is_dup = 1;
  166. dup_usec_delta = get_usec_time() - dup_notify_usec;
  167. dup_notify_usec = get_usec_time();
  168. }
  169. switch(msg->payload[0])
  170. {
  171. case LOGLEVEL_EVENT:
  172. if(!is_dup || (dup_usec_delta >= DUP_USEC_BEEP_THRESHOLD)) {
  173. PIU_ACK_BEEP(piu_fd);
  174. }
  175. break;
  176. case LOGLEVEL_REJECT:
  177. if(!is_dup || (dup_usec_delta >= DUP_USEC_BEEP_THRESHOLD)) {
  178. PIU_ERROR_BEEP(piu_fd);
  179. }
  180. break;
  181. case LOGLEVEL_ACCEPT:
  182. if(!is_dup || (dup_usec_delta >= DUP_USEC_BEEP_THRESHOLD)) {
  183. PIU_ACK_BEEP(piu_fd);
  184. }
  185. break;
  186. case LOGLEVEL_ERROR:
  187. if(!is_dup || (dup_usec_delta >= DUP_USEC_BEEP_THRESHOLD)) {
  188. PIU_CRITICAL_BEEP(piu_fd);
  189. }
  190. break;
  191. default:
  192. break;
  193. }
  194. return MESSAGE_HANDLED_CONT;
  195. }
  196. #endif
  197. //-------------------------
  198. void maintain_ipc_hub_connect(char *progname) {
  199. struct message_record outgoing_msg;
  200. // if we have no connection to the IPC hub
  201. if(hub_fd < 0) {
  202. // try and get one
  203. //
  204. hub_fd = connect_to_message_server(progname);
  205. if(hub_fd >= 0)
  206. {
  207. // Subscribe to the default status messages
  208. //
  209. subscribe_to_default_messages(hub_fd);
  210. // Subscribe to our specific message
  211. //
  212. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_PIU_MESSAGE, strlen(MAILBOX_PIU_MESSAGE));
  213. send_message(hub_fd,&outgoing_msg);
  214. #ifdef BEEP_WITH_MAGSTRIPE_READER
  215. // if BEEP_WITH_MAGSTRIPE_READER is defined, that means our diu board's speaker/amp is FUCKING BROKEN and we
  216. // need to intercept DIU messages and induce the magstripe reader on the PIU to beep when they occur even
  217. // though it is a stupid idea to do so since any other activity on the magstripe reader (like swiping a card...)
  218. // will cause it to either miss the card or disrupt the beeping.
  219. //
  220. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_DRIVER_NOTIFY, strlen(MAILBOX_DRIVER_NOTIFY));
  221. send_message(hub_fd,&outgoing_msg);
  222. #endif
  223. }
  224. else
  225. {
  226. fprintf(stderr, "Cannot connect to IPC hub!\n");
  227. }
  228. }
  229. }
  230. int main(int argc, char **argv) {
  231. char line[LINE_BUFFER_SIZE] = {0};
  232. struct message_record incoming_msg;
  233. struct message_record outgoing_msg;
  234. struct pollfd fds[2];
  235. int nfd;
  236. int poll_return;
  237. int read_return;
  238. int i, j;
  239. long long int _usec_now, _usec_prv, _usec_del;
  240. _usec_now = get_usec_time();
  241. _usec_prv = _usec_now;
  242. _usec_del = 60000000;
  243. long long int _usec_ratelimit_now, _usec_ratelimit_prv, _usec_ratelimit_del;
  244. _usec_ratelimit_now = get_usec_time();
  245. _usec_ratelimit_prv = _usec_ratelimit_now;
  246. _usec_ratelimit_del = 1000000;
  247. configure_signal_handlers(argv[0]);
  248. maintain_ipc_hub_connect(argv[0]);
  249. // Register our defualt system message processing callbacks
  250. //
  251. register_system_status_callbacks();
  252. // Register our module specific message processing callbacks
  253. //
  254. register_dispatch_callback(MAILBOX_PIU_MESSAGE, CALLBACK_USER(1), &handle_display_message, NULL);
  255. #ifdef BEEP_WITH_MAGSTRIPE_READER
  256. register_dispatch_callback(MAILBOX_DRIVER_NOTIFY, CALLBACK_USER(2), &handle_DIU_beep, NULL);
  257. #endif
  258. // loop until we get asked to exit...
  259. //
  260. while( exit_request_status == EXIT_REQUEST_NONE ) {
  261. RESET_WATCHDOG();
  262. //DEBUG
  263. _usec_now = get_usec_time();
  264. if ((_usec_now - _usec_prv) > _usec_del) {
  265. printf("[%lli] piu_minder: heartbeat\n", get_usec_time());
  266. _usec_prv = _usec_now;
  267. }
  268. //DEBUG
  269. maintain_ipc_hub_connect(argv[0]);
  270. if(piu_fd < 0) {
  271. piu_fd = open_rs232_device(PASSENGER_UI_PORT, USE_DEFAULT_BAUD, RS232_LINE);
  272. if(piu_fd >= 0) {
  273. read_return = test_and_init_device(piu_fd, &piu_test_vector, diag);
  274. if(read_return) {
  275. fprintf(stderr, "PIU Init Failed on %s: %s\n", PASSENGER_UI_PORT, diag);
  276. close(piu_fd);
  277. piu_fd = -1;
  278. }
  279. // If we successfully connected...
  280. //
  281. else {
  282. // iterate through each line
  283. //
  284. for(j = 0; j < PIU_NUM_LINES; j++) {
  285. // and update the display to be current
  286. //
  287. update_piu_display(j);
  288. }
  289. }
  290. }
  291. else {
  292. _usec_ratelimit_now = get_usec_time();
  293. if ((_usec_ratelimit_now - _usec_ratelimit_prv) > _usec_ratelimit_del) {
  294. fprintf(stderr, "Cannot open serial port %s for PIU!\n", PASSENGER_UI_PORT);
  295. _usec_ratelimit_prv = _usec_ratelimit_now;
  296. }
  297. }
  298. }
  299. nfd = 0;
  300. if(hub_fd >= 0) {
  301. fds[nfd].fd = hub_fd;
  302. fds[nfd].events = POLLIN;
  303. fds[nfd].revents = 0;
  304. nfd++;
  305. }
  306. if(piu_fd >= 0) {
  307. fds[nfd].fd = piu_fd;
  308. fds[nfd].events = POLLIN;
  309. fds[nfd].revents = 0;
  310. nfd++;
  311. }
  312. // if we have any file descriptors, poll them
  313. //
  314. if(nfd > 0) {
  315. poll_return = poll(fds, nfd, POLL_TIMEOUT);
  316. }
  317. // otherwise, whistle and look busy
  318. //
  319. else {
  320. poll_return = 0;
  321. sleep(1);
  322. }
  323. //--------------------------------------------------------------------------------
  324. // No matter what poll() returned, we need to do some basic background work here...
  325. //
  326. //--------------------------------------------------------------------------------
  327. handle_message_timeouts();
  328. //--------------------------------------------------------------------------------
  329. // if poll didn't net us any work to do,
  330. //
  331. if(poll_return < 1) {
  332. //lets try again
  333. //
  334. continue;
  335. }
  336. for(i = 0; i < nfd; i++) {
  337. //If we're looking at the PIU...
  338. //
  339. if( fds[i].fd == piu_fd ) {
  340. // if poll says our serial port has become bogus...
  341. //
  342. if(fds[i].revents & (POLLHUP | POLLERR | POLLNVAL)) {
  343. fprintf(stderr, "This is very odd... Poll returned flags %d on our serial port...\n", fds[i].revents);
  344. // close it
  345. //
  346. close(piu_fd);
  347. // flag it invalid
  348. piu_fd = -1;
  349. // and break out of the for loop to allow the while to cycle
  350. //
  351. break;
  352. }
  353. if(fds[i].revents & POLLIN) {
  354. read_return = read(fds[i].fd, line, sizeof(line));
  355. if(read_return > 0) {
  356. char *trav = line;
  357. line[read_return] = '\0';
  358. strip_crlf(line);
  359. // advance until EOL or we hit our start sentinel
  360. //
  361. while(*trav && (*trav != '/') ) {
  362. trav++;
  363. }
  364. // Check to see that our address header is intact...
  365. //
  366. if( (trav[0] == '/') && (trav[2] == ':') ) {
  367. switch(trav[1]) {
  368. case 'M':
  369. // advance past the header
  370. //
  371. trav += 3;
  372. // Ignore ACK message from the magstripe reader
  373. //
  374. if(strcmp(trav, "^")) {
  375. prepare_message(&outgoing_msg, MAILBOX_TOKEN_MAG, trav, strlen(trav) + 1);
  376. send_message(hub_fd, &outgoing_msg);
  377. }
  378. break;
  379. case 'R':
  380. // advance past the header
  381. //
  382. trav += 3;
  383. // Ignore null reads.
  384. //
  385. if(strcmp(trav,"0|0")) {
  386. prepare_message(&outgoing_msg, MAILBOX_TOKEN_RFID, trav, strlen(trav) + 1);
  387. send_message(hub_fd, &outgoing_msg);
  388. }
  389. break;
  390. // handle warnings
  391. //
  392. case '*':
  393. // debugs
  394. //
  395. case '#':
  396. // and errors
  397. //
  398. case '!':
  399. // send them all to the log server
  400. //
  401. format_log_message(&outgoing_msg, trav[1], "PIU Reports: %s", trav + 3);
  402. send_message(hub_fd, &outgoing_msg);
  403. // but in the case of errors, send them to the driver too
  404. //
  405. if(trav[1] == '!') {
  406. format_driver_message(&outgoing_msg, trav[1], "PIU Reports: %s", trav + 3);
  407. send_message(hub_fd, &outgoing_msg);
  408. }
  409. break;
  410. // ignore any message addresses that we don't know what to do with
  411. //
  412. default:
  413. printf("Ignoring command \"%s\"\n", trav);
  414. break;
  415. }
  416. }
  417. else {
  418. //printf("Ignoring non-command line \"%s\"\n", trav);
  419. }
  420. }
  421. else
  422. {
  423. fprintf(stderr, "Read from %s returned %d!\n", PASSENGER_UI_PORT, read_return);
  424. //close it
  425. close(piu_fd);
  426. //flag it invalid
  427. piu_fd = -1;
  428. //and break out of the for loop to allow the while to cycle
  429. break;
  430. }
  431. }
  432. }
  433. //If we're looking at the IPC hub...
  434. else if( fds[i].fd == hub_fd )
  435. {
  436. //if poll says our connection to the IPC hub has died...
  437. if(fds[i].revents & (POLLHUP | POLLERR | POLLNVAL))
  438. {
  439. //complain
  440. fprintf(stderr, "The connection to the IPC hub has gone away...\n");
  441. //close it
  442. close(hub_fd);
  443. //flag it dead
  444. hub_fd = -1;
  445. //break out of the for loop
  446. break;
  447. }
  448. // if we have mail in any of our mailboxes...
  449. //
  450. if(fds[i].revents & POLLIN) {
  451. read_return = get_message(hub_fd, &incoming_msg);
  452. if(read_return < 0) {
  453. // complain
  454. //
  455. fprintf(stderr, "The connection to the IPC hub has gone away...\n");
  456. // close it
  457. //
  458. close(hub_fd);
  459. // flag it dead
  460. //
  461. hub_fd = -1;
  462. // break out of the for loop
  463. //
  464. break;
  465. }
  466. else {
  467. message_callback_return msg_status;
  468. msg_status = process_message(&incoming_msg);
  469. if (msg_status < 0) { }
  470. }
  471. }
  472. }
  473. }
  474. }
  475. printf("Exiting.\n");
  476. close(piu_fd);
  477. close(hub_fd);
  478. return 0;
  479. }