paddlemgr.c 14 KB

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