paddlemgr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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/socket.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <sys/un.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <poll.h>
  31. #include <time.h>
  32. #include <zlib.h>
  33. #include "../common/common_defs.h"
  34. #include "../commhub/commhub.h"
  35. #include "../commhub/client_utils.h"
  36. #include "../common/gpsmath.h"
  37. #define PADDLEMGR_VERSION "0.1.1"
  38. // structure for loading stop definitions
  39. //
  40. typedef struct stop_struct {
  41. // scheduled arrival time
  42. //
  43. int hour;
  44. int min;
  45. // coordinates
  46. //
  47. double lat;
  48. double lon;
  49. int route;
  50. int trip;
  51. int stop;
  52. // human readable stop name
  53. //
  54. char name[STOP_NAME_LEN];
  55. } stop;
  56. int commhub_fd = -1;
  57. // Currently selected paddle
  58. //
  59. int current_paddle_num = 0;
  60. // Number of stops on said paddle
  61. //
  62. int current_paddle_len = 0;
  63. // Index of the active stop on this paddle
  64. //
  65. int current_paddle_idx = 0;
  66. // Data block to hold loaded paddle
  67. //
  68. stop current_paddle[MAX_PADDLE_SIZE] = {{0}};
  69. int load_paddle(int paddlenum) {
  70. char buffer[LINE_BUFFER_SIZE];
  71. char buffer2[LINE_BUFFER_SIZE];
  72. FILE *f;
  73. int i, eol;
  74. int n;
  75. sprintf(buffer, "%s%d.paddle", CONFIG_FILE_PATH, paddlenum);
  76. f = fopen(buffer, "rb");
  77. if (!f) {
  78. printf("Paddle not found: %s\n", buffer);
  79. return -1;
  80. }
  81. current_paddle_num = paddlenum;
  82. current_paddle_idx = 0;
  83. current_paddle_len = 0;
  84. n = 0;
  85. // For each line in the input file
  86. //
  87. while( fgets(buffer, LINE_BUFFER_SIZE, f) ) {
  88. if(current_paddle_idx >= MAX_PADDLE_SIZE) {
  89. fprintf(stderr, "Paddle %d has overflowed its maximum size of %d stops and has been truncated!\n", current_paddle_num, MAX_PADDLE_SIZE);
  90. break;
  91. }
  92. strip_crlf(buffer); //get rid of any trailing CR/LF characters
  93. n++;
  94. i = eol = 0;
  95. i += get_field(buffer2, buffer + i, LINE_BUFFER_SIZE, &eol);
  96. // Skip any blank or comment lines
  97. //
  98. if(eol || buffer2[0] == '#') { continue; }
  99. // Clear this row
  100. //
  101. memset(current_paddle + current_paddle_len, 0, sizeof(stop));
  102. current_paddle[current_paddle_len].hour = strtol(buffer2, NULL, 10);
  103. i += get_field(buffer2, buffer + i, LINE_BUFFER_SIZE, &eol);
  104. if(eol) { printf("Line %d too short in paddle %d: \"%s\"\n", n, paddlenum, buffer); continue; }
  105. current_paddle[current_paddle_len].min = strtol(buffer2, NULL, 10);
  106. i += get_field(buffer2, buffer + i, LINE_BUFFER_SIZE, &eol);
  107. if(eol) { printf("Line %d too short in paddle %d: \"%s\"\n", n, paddlenum, buffer); continue; }
  108. current_paddle[current_paddle_len].lat = strtod(buffer2, NULL);
  109. i += get_field(buffer2, buffer + i, LINE_BUFFER_SIZE, &eol);
  110. if(eol) { printf("Line %d too short in paddle %d: \"%s\"\n", n, paddlenum, buffer); continue; }
  111. current_paddle[current_paddle_len].lon = strtod(buffer2, NULL);
  112. i += get_field(buffer2, buffer + i, LINE_BUFFER_SIZE, &eol);
  113. if(eol) { printf("Line %d too short in paddle %d: \"%s\"\n", n, paddlenum, buffer); continue; }
  114. current_paddle[current_paddle_len].route = strtol(buffer2, NULL, 10);
  115. i += get_field(buffer2, buffer + i, LINE_BUFFER_SIZE, &eol);
  116. if(eol) { printf("Line %d too short in paddle %d: \"%s\"\n", n, paddlenum, buffer); continue; }
  117. current_paddle[current_paddle_len].trip = strtol(buffer2, NULL, 10);
  118. i += get_field(buffer2, buffer + i, LINE_BUFFER_SIZE, &eol);
  119. if(eol) { printf("Line %d too short in paddle %d: \"%s\"\n", n, paddlenum, buffer); continue; }
  120. current_paddle[current_paddle_len].stop = strtol(buffer2, NULL, 10);
  121. i += get_field(buffer2, buffer + i, LINE_BUFFER_SIZE, &eol);
  122. strncpy(current_paddle[current_paddle_len].name, buffer2, STOP_NAME_LEN - 1);
  123. current_paddle_len++;
  124. }
  125. fclose(f);
  126. return paddlenum;
  127. }
  128. int clear_paddle() {
  129. current_paddle_num = 0;
  130. current_paddle_idx = 0;
  131. current_paddle_len = 0;
  132. return 0;
  133. }
  134. int where_the_hell_are_we() {
  135. int i;
  136. // by default, we will report that we are still at our last known stop
  137. //
  138. int found_stop = current_paddle_idx;
  139. struct tm temp;
  140. time_t now;
  141. time_t sched;
  142. now = time(NULL);
  143. if (current_paddle_num == 0) {
  144. return -1;
  145. }
  146. #ifdef ROLLOVER_FORWARD_ONLY
  147. // If we are in rollover-forward mode, we will not actively re-select any previous stops, nor the current one.
  148. // This allows two stops to have the same location but different route numbers for places where the route changes at a stop.
  149. //
  150. i = current_paddle_idx;
  151. #else
  152. i = 0;
  153. #endif
  154. for( ; i < current_paddle_len; i++) {
  155. // populate our time structure based on now
  156. // so the date will be correct
  157. //
  158. localtime_r(&now,&temp);
  159. // Set the expected arrival time
  160. //
  161. temp.tm_hour = current_paddle[i].hour;
  162. // Set the expected arrival time
  163. //
  164. temp.tm_min = current_paddle[i].min;
  165. // and convert it back to a scheduled arrival time in UTC unix timestamp format
  166. //
  167. sched = mktime( &temp );
  168. // First we do the time check, because that's cheap integer math
  169. // then GPS distance last because that's expensive trig
  170. //
  171. if( (abs(now - sched) <= ROLLOVER_TIME_WINDOW) &&
  172. (GPS_Dist(gps_stat.lat, gps_stat.lon, current_paddle[i].lat, current_paddle[i].lon) <= ROLLOVER_DISTANCE)) {
  173. // update our found_stop index to the matching stop
  174. //
  175. found_stop = i;
  176. #ifndef ROLLOVER_TO_FURTHEST_STOP
  177. // if ROLLOVER_TO_FURTHEST_STOP is NOT defined, we break as soon as
  178. // we've found ANY matching stop (even the one we started at)
  179. //
  180. break;
  181. #endif
  182. }
  183. }
  184. return found_stop;
  185. }
  186. int send_status_update() {
  187. struct message_record outgoing_msg;
  188. stop_status current = {0};
  189. if(commhub_fd < 0) {
  190. return -1;
  191. }
  192. current.paddle = current_paddle_num;
  193. if(current_paddle_num) {
  194. current.route = current_paddle[current_paddle_idx].route;
  195. current.trip = current_paddle[current_paddle_idx].trip;
  196. current.stop = current_paddle[current_paddle_idx].stop;
  197. current.lat = current_paddle[current_paddle_idx].lat;
  198. current.lon = current_paddle[current_paddle_idx].lon;
  199. strncpy(current.stopname, current_paddle[current_paddle_idx].name, STOP_NAME_LEN - 1);
  200. }
  201. prepare_message(&outgoing_msg, MAILBOX_STOP_STATUS, &current, sizeof(current));
  202. return send_message(commhub_fd, &outgoing_msg);
  203. }
  204. // This function sends an update to the driver and to the diagnostic log saying we're doing a stop rollover
  205. //
  206. int send_driver_update() {
  207. struct message_record outgoing_msg;
  208. if(commhub_fd < 0) { return -1; }
  209. if(current_paddle_num) {
  210. format_log_message(&outgoing_msg,
  211. LOGLEVEL_DEBUG,
  212. "%02d:%02d %s",
  213. current_paddle[current_paddle_idx].hour,
  214. current_paddle[current_paddle_idx].min,
  215. current_paddle[current_paddle_idx].name);
  216. send_message(commhub_fd, &outgoing_msg);
  217. format_driver_message(&outgoing_msg,
  218. LOGLEVEL_EVENT,
  219. "%02d:%02d %s",
  220. current_paddle[current_paddle_idx].hour,
  221. current_paddle[current_paddle_idx].min,
  222. current_paddle[current_paddle_idx].name);
  223. return send_message(commhub_fd, &outgoing_msg);
  224. }
  225. return -1;
  226. }
  227. int send_vault_drop() {
  228. struct message_record outgoing_msg;
  229. if(commhub_fd < 0) { return -1; }
  230. prepare_message(&outgoing_msg, MAILBOX_VAULT_DROP, "", 0);
  231. return send_message(commhub_fd, &outgoing_msg);
  232. }
  233. message_callback_return handle_status_req(struct message_record *msg, void *param) {
  234. send_status_update();
  235. return MESSAGE_HANDLED_CONT;
  236. }
  237. message_callback_return handle_gps_update(struct message_record *msg, void *param) {
  238. int tempidx;
  239. //The system callback will have already handled this message and put it into the correct data structure
  240. //
  241. //If either we have no current paddle, or no real GPS data, ignore this message
  242. //
  243. if( (!current_paddle_num) || (!gps_stat.gps_good) ) {
  244. return MESSAGE_HANDLED_CONT;
  245. }
  246. tempidx = where_the_hell_are_we();
  247. if(tempidx < 0) {
  248. return MESSAGE_HANDLED_CONT;
  249. }
  250. if(tempidx != current_paddle_idx) {
  251. current_paddle_idx = tempidx;
  252. send_status_update();
  253. send_driver_update();
  254. send_vault_drop();
  255. }
  256. return MESSAGE_HANDLED_CONT;
  257. }
  258. message_callback_return handle_next_req(struct message_record *msg, void *param) {
  259. if(current_paddle_idx < (current_paddle_len - 1)) {
  260. current_paddle_idx++;
  261. send_driver_update();
  262. }
  263. send_status_update();
  264. return MESSAGE_HANDLED_CONT;
  265. }
  266. message_callback_return handle_prev_req(struct message_record *msg, void *param) {
  267. if(current_paddle_idx > 0) {
  268. current_paddle_idx--;
  269. send_driver_update();
  270. }
  271. send_status_update();
  272. return MESSAGE_HANDLED_CONT;
  273. }
  274. message_callback_return handle_set_paddle_req(struct message_record *msg, void *param) {
  275. struct message_record outgoing_msg;
  276. set_paddle_req *req = (set_paddle_req *)msg->payload;
  277. clear_paddle();
  278. req->result = load_paddle(req->request);
  279. if(req->result > 0) {
  280. send_driver_update();
  281. }
  282. prepare_message(&outgoing_msg, MAILBOX_PADDLE_ACK, req, sizeof(set_paddle_req));
  283. send_message(commhub_fd, &outgoing_msg);
  284. send_status_update();
  285. return MESSAGE_HANDLED_CONT;
  286. }
  287. void maintain_ipc_hub_connect(char *progname) {
  288. struct message_record outgoing_msg;
  289. // if we have no connection to the communication hub
  290. //
  291. if(commhub_fd < 0) {
  292. // try and get one
  293. //
  294. commhub_fd = connect_to_message_server(progname);
  295. // if it worked
  296. //
  297. if(commhub_fd >= 0) {
  298. // Subscribe to the command mailboxes we act on
  299. //
  300. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_SET_PADDLE, strlen(MAILBOX_SET_PADDLE));
  301. send_message(commhub_fd,&outgoing_msg);
  302. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_NEXT_STOP, strlen(MAILBOX_NEXT_STOP));
  303. send_message(commhub_fd,&outgoing_msg);
  304. prepare_message(&outgoing_msg, MAILBOX_SUBSCRIBE, MAILBOX_PREV_STOP, strlen(MAILBOX_PREV_STOP));
  305. send_message(commhub_fd,&outgoing_msg);
  306. // Subscribe to the relevant status management mailboxes
  307. //
  308. subscribe_to_default_messages(commhub_fd);
  309. // Request updated status information...
  310. //
  311. prepare_message(&outgoing_msg, MAILBOX_STATUS_REQUEST, "", 0);
  312. send_message(commhub_fd,&outgoing_msg);
  313. }
  314. }
  315. }
  316. int main(int argc, char **argv) {
  317. struct pollfd fds[2];
  318. int nfds = 0;
  319. int poll_return = 0;
  320. int read_return = 0;
  321. int i;
  322. struct message_record incoming_msg;
  323. int send_initial_status_update = 0;
  324. #ifdef DEBUG_PRINT
  325. long long int _usec_now, _usec_prv, _usec_del;
  326. _usec_now = get_usec_time();
  327. _usec_prv = _usec_now;
  328. _usec_del = 60000000;
  329. #endif
  330. if ( (argc>1) && (
  331. (strncmp(argv[1], "-h", 3)==0) ||
  332. (strncmp(argv[1], "-v", 3)==0) ) ) {
  333. printf("paddlemgr version: %s\n", PADDLEMGR_VERSION);
  334. exit(0);
  335. }
  336. //----
  337. // try to restore as much state as possible
  338. //
  339. init_state_info();
  340. gps_stat.lat = state_info.lat;
  341. gps_stat.lon = state_info.lon;
  342. gps_stat.gps_good = state_info.gps_good;
  343. gps_stat.num_sats = state_info.num_sats;
  344. if (state_info.paddle != 0) {
  345. if (load_paddle(state_info.paddle) >= 0) {
  346. send_initial_status_update = 1;
  347. }
  348. }
  349. //----
  350. configure_signal_handlers(argv[0]);
  351. maintain_ipc_hub_connect(argv[0]);
  352. // Register our default keep-up-with-system status callbacks
  353. //
  354. register_system_status_callbacks();
  355. // Add our module-specific callbacks
  356. //
  357. register_dispatch_callback(MAILBOX_GPS_STATUS, CALLBACK_USER(1), handle_gps_update, NULL);
  358. register_dispatch_callback(MAILBOX_STATUS_REQUEST, CALLBACK_USER(2), handle_status_req, NULL);
  359. register_dispatch_callback(MAILBOX_SET_PADDLE, CALLBACK_USER(3), handle_set_paddle_req, NULL);
  360. register_dispatch_callback(MAILBOX_NEXT_STOP, CALLBACK_USER(4), handle_next_req, NULL);
  361. register_dispatch_callback(MAILBOX_PREV_STOP, CALLBACK_USER(5), handle_prev_req, NULL);
  362. //---
  363. if (send_initial_status_update) { send_status_update(); }
  364. //---
  365. while( exit_request_status == EXIT_REQUEST_NONE ) {
  366. RESET_WATCHDOG();
  367. #ifdef DEBUG_PRINT
  368. _usec_now = get_usec_time();
  369. if ((_usec_now - _usec_prv) > _usec_del) {
  370. printf("[%lli] paddlemgr: heartbeat\n", get_usec_time());
  371. fflush(stdout);
  372. _usec_prv = _usec_now;
  373. }
  374. #endif
  375. maintain_ipc_hub_connect(argv[0]);
  376. nfds=0;
  377. if(commhub_fd >= 0) {
  378. fds[nfds].fd = commhub_fd;
  379. fds[nfds].events = POLLIN;
  380. nfds++;
  381. }
  382. if(nfds > 0) {
  383. poll_return = poll(fds, nfds, POLL_TIMEOUT);
  384. }
  385. else {
  386. usleep(POLL_TIMEOUT * 1000);
  387. poll_return = 0;
  388. }
  389. if(poll_return <= 0) {
  390. continue;
  391. }
  392. //---- If we got a message
  393. for(i=0; i < nfds; i++) {
  394. if( fds[i].fd == commhub_fd ) {
  395. //If we've lost connection, break this loop and poll all over again
  396. //
  397. if(fds[i].revents & (POLLERR | POLLHUP | POLLNVAL)) {
  398. close(commhub_fd);
  399. commhub_fd = -1;
  400. break;
  401. }
  402. if(fds[i].revents & POLLIN) {
  403. read_return = get_message(commhub_fd, &incoming_msg);
  404. if( read_return < 0 ) {
  405. close(commhub_fd);
  406. commhub_fd = -1;
  407. break;
  408. }
  409. // This passes the received message through the callback list
  410. //
  411. process_message(&incoming_msg);
  412. }
  413. }
  414. }
  415. }
  416. if(commhub_fd >= 0) {
  417. close(commhub_fd);
  418. }
  419. printf("Goodbye.\n");
  420. return 0;
  421. }