paddlemgr.c 13 KB

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