piu_main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. configure_signal_handlers(argv[0]);
  240. maintain_ipc_hub_connect(argv[0]);
  241. // Register our defualt system message processing callbacks
  242. //
  243. register_system_status_callbacks();
  244. // Register our module specific message processing callbacks
  245. //
  246. register_dispatch_callback(MAILBOX_PIU_MESSAGE, CALLBACK_USER(1), &handle_display_message, NULL);
  247. #ifdef BEEP_WITH_MAGSTRIPE_READER
  248. register_dispatch_callback(MAILBOX_DRIVER_NOTIFY, CALLBACK_USER(2), &handle_DIU_beep, NULL);
  249. #endif
  250. // loop until we get asked to exit...
  251. //
  252. while( exit_request_status == EXIT_REQUEST_NONE ) {
  253. RESET_WATCHDOG();
  254. // DEBUG
  255. //
  256. printf("[%lli] piu_minder: heartbeat\n", get_usec_time());
  257. //
  258. // DEBUG
  259. maintain_ipc_hub_connect(argv[0]);
  260. if(piu_fd < 0) {
  261. piu_fd = open_rs232_device(PASSENGER_UI_PORT, USE_DEFAULT_BAUD, RS232_LINE);
  262. if(piu_fd >= 0) {
  263. read_return = test_and_init_device(piu_fd, &piu_test_vector, diag);
  264. if(read_return) {
  265. fprintf(stderr, "PIU Init Failed on %s: %s\n", PASSENGER_UI_PORT, diag);
  266. close(piu_fd);
  267. piu_fd = -1;
  268. }
  269. // If we successfully connected...
  270. //
  271. else {
  272. // iterate through each line
  273. //
  274. for(j = 0; j < PIU_NUM_LINES; j++) {
  275. // and update the display to be current
  276. //
  277. update_piu_display(j);
  278. }
  279. }
  280. }
  281. else {
  282. fprintf(stderr, "Cannot open serial port %s for PIU!\n", PASSENGER_UI_PORT);
  283. }
  284. }
  285. nfd = 0;
  286. if(hub_fd >= 0) {
  287. fds[nfd].fd = hub_fd;
  288. fds[nfd].events = POLLIN;
  289. fds[nfd].revents = 0;
  290. nfd++;
  291. }
  292. if(piu_fd >= 0) {
  293. fds[nfd].fd = piu_fd;
  294. fds[nfd].events = POLLIN;
  295. fds[nfd].revents = 0;
  296. nfd++;
  297. }
  298. // if we have any file descriptors, poll them
  299. //
  300. if(nfd > 0) {
  301. poll_return = poll(fds, nfd, POLL_TIMEOUT);
  302. }
  303. // otherwise, whistle and look busy
  304. //
  305. else {
  306. poll_return = 0;
  307. sleep(1);
  308. }
  309. //--------------------------------------------------------------------------------
  310. // No matter what poll() returned, we need to do some basic background work here...
  311. //
  312. //--------------------------------------------------------------------------------
  313. handle_message_timeouts();
  314. //--------------------------------------------------------------------------------
  315. // if poll didn't net us any work to do,
  316. //
  317. if(poll_return < 1) {
  318. //lets try again
  319. //
  320. continue;
  321. }
  322. for(i = 0; i < nfd; i++) {
  323. //If we're looking at the PIU...
  324. //
  325. if( fds[i].fd == piu_fd ) {
  326. // if poll says our serial port has become bogus...
  327. //
  328. if(fds[i].revents & (POLLHUP | POLLERR | POLLNVAL)) {
  329. fprintf(stderr, "This is very odd... Poll returned flags %d on our serial port...\n", fds[i].revents);
  330. // close it
  331. //
  332. close(piu_fd);
  333. // flag it invalid
  334. piu_fd = -1;
  335. // and break out of the for loop to allow the while to cycle
  336. //
  337. break;
  338. }
  339. if(fds[i].revents & POLLIN) {
  340. read_return = read(fds[i].fd, line, sizeof(line));
  341. if(read_return > 0) {
  342. char *trav = line;
  343. line[read_return] = '\0';
  344. strip_crlf(line);
  345. // advance until EOL or we hit our start sentinel
  346. //
  347. while(*trav && (*trav != '/') ) {
  348. trav++;
  349. }
  350. // Check to see that our address header is intact...
  351. //
  352. if( (trav[0] == '/') && (trav[2] == ':') ) {
  353. switch(trav[1]) {
  354. case 'M':
  355. // advance past the header
  356. //
  357. trav += 3;
  358. // Ignore ACK message from the magstripe reader
  359. //
  360. if(strcmp(trav, "^")) {
  361. prepare_message(&outgoing_msg, MAILBOX_TOKEN_MAG, trav, strlen(trav) + 1);
  362. send_message(hub_fd, &outgoing_msg);
  363. }
  364. break;
  365. case 'R':
  366. // advance past the header
  367. //
  368. trav += 3;
  369. // Ignore null reads.
  370. //
  371. if(strcmp(trav,"0|0")) {
  372. prepare_message(&outgoing_msg, MAILBOX_TOKEN_RFID, trav, strlen(trav) + 1);
  373. send_message(hub_fd, &outgoing_msg);
  374. }
  375. break;
  376. // handle warnings
  377. //
  378. case '*':
  379. // debugs
  380. //
  381. case '#':
  382. // and errors
  383. //
  384. case '!':
  385. // send them all to the log server
  386. //
  387. format_log_message(&outgoing_msg, trav[1], "PIU Reports: %s", trav + 3);
  388. send_message(hub_fd, &outgoing_msg);
  389. // but in the case of errors, send them to the driver too
  390. //
  391. if(trav[1] == '!') {
  392. format_driver_message(&outgoing_msg, trav[1], "PIU Reports: %s", trav + 3);
  393. send_message(hub_fd, &outgoing_msg);
  394. }
  395. break;
  396. // ignore any message addresses that we don't know what to do with
  397. //
  398. default:
  399. printf("Ignoring command \"%s\"\n", trav);
  400. break;
  401. }
  402. }
  403. else {
  404. //printf("Ignoring non-command line \"%s\"\n", trav);
  405. }
  406. }
  407. else
  408. {
  409. fprintf(stderr, "Read from %s returned %d!\n", PASSENGER_UI_PORT, read_return);
  410. //close it
  411. close(piu_fd);
  412. //flag it invalid
  413. piu_fd = -1;
  414. //and break out of the for loop to allow the while to cycle
  415. break;
  416. }
  417. }
  418. }
  419. //If we're looking at the IPC hub...
  420. else if( fds[i].fd == hub_fd )
  421. {
  422. //if poll says our connection to the IPC hub has died...
  423. if(fds[i].revents & (POLLHUP | POLLERR | POLLNVAL))
  424. {
  425. //complain
  426. fprintf(stderr, "The connection to the IPC hub has gone away...\n");
  427. //close it
  428. close(hub_fd);
  429. //flag it dead
  430. hub_fd = -1;
  431. //break out of the for loop
  432. break;
  433. }
  434. // if we have mail in any of our mailboxes...
  435. //
  436. if(fds[i].revents & POLLIN) {
  437. read_return = get_message(hub_fd, &incoming_msg);
  438. if(read_return < 0) {
  439. // complain
  440. //
  441. fprintf(stderr, "The connection to the IPC hub has gone away...\n");
  442. // close it
  443. //
  444. close(hub_fd);
  445. // flag it dead
  446. //
  447. hub_fd = -1;
  448. // break out of the for loop
  449. //
  450. break;
  451. }
  452. else {
  453. message_callback_return msg_status;
  454. msg_status = process_message(&incoming_msg);
  455. if (msg_status < 0) { }
  456. }
  457. }
  458. }
  459. }
  460. }
  461. printf("Exiting.\n");
  462. close(piu_fd);
  463. close(hub_fd);
  464. return 0;
  465. }