piu_main.c 15 KB

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