driver.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "driver.h"
  21. // This function searches the driver file and checks a driver, pin pair returning 0 on success
  22. // or -1 on failure.
  23. //
  24. int driver_login(int drivernum, char *pinnum) {
  25. FILE *f;
  26. char name[DRIVER_NAME_LEN]={0};
  27. char pin[64];
  28. char line[LINE_BUFFER_SIZE]={0};
  29. char *inc_line;
  30. struct message_record outgoing_msg;
  31. int drv;
  32. int retval;
  33. if (!drivernum || !pinnum) { return -1; }
  34. // open the driver file
  35. //
  36. f=fopen(DRIVERS_FILE,"rb");
  37. if (!f) {
  38. format_log_message(&outgoing_msg, LOGLEVEL_ERROR, "Cannot open %s\n", DRIVERS_FILE);
  39. send_message(hub_fd,&outgoing_msg);
  40. // fail if we can't read it
  41. //
  42. return -1;
  43. }
  44. // while we haven't hit EOF
  45. //
  46. while (!feof(f)) {
  47. // read it line by line
  48. //
  49. if (!fgets(line,sizeof(line)-1,f)) {
  50. break;
  51. }
  52. // terminating each line safely
  53. //
  54. line[sizeof(line)-1] = '\0';
  55. inc_line = line;
  56. // and ignoring any leading whitespace
  57. //
  58. while (1) {
  59. char c = *inc_line;
  60. if ( (c == ' ') ||(c == '\t') ||(c == '\r') ||(c == '\n') ) {
  61. inc_line++;
  62. continue;
  63. }
  64. break;
  65. }
  66. // and ignoring blank/comment lines
  67. //
  68. if ( (inc_line[0] == '#') || (inc_line[0] == '\0')) {
  69. continue;
  70. }
  71. name[0]='\0';
  72. pin[0]='\0';
  73. // parse the line format (drivernum pin_num drivername)
  74. //
  75. retval=sscanf(inc_line,"%d %63[0123456789] %63[^\r\n]",&drv,pin,name);
  76. // if we get at least a driver number and pin number
  77. //
  78. if (retval >= 2) {
  79. // check to see if we've found a match
  80. //
  81. if (drv != drivernum) {
  82. continue;
  83. }
  84. // if the PIN matches
  85. //
  86. if (!strcmp(pinnum, pin)) {
  87. my_driver_status.logged_in_driver = drivernum;
  88. strncpy(my_driver_status.driver_name, name, DRIVER_NAME_LEN);
  89. my_driver_status.driver_name[DRIVER_NAME_LEN - 1] = '\0';
  90. my_driver_status.equip_num = get_equip_num();
  91. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Successful login for driver %d\n", drivernum);
  92. send_message(hub_fd,&outgoing_msg);
  93. update_driver_status |= 1;
  94. fclose(f);
  95. return 0;
  96. }
  97. else {
  98. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Failed login for driver %d\n", drivernum);
  99. send_message(hub_fd,&outgoing_msg);
  100. fclose(f);
  101. return -1;
  102. }
  103. }
  104. }
  105. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Failed login for unknown driver %d\n", drivernum);
  106. send_message(hub_fd,&outgoing_msg);
  107. fclose(f);
  108. return -1;
  109. }
  110. // Log a driver out and cancel any pending set paddle request
  111. //
  112. int driver_logout() {
  113. struct message_record outgoing_msg;
  114. if (my_driver_status.logged_in_driver) {
  115. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Driver logout for driver %d\n", my_driver_status.logged_in_driver);
  116. send_message(hub_fd,&outgoing_msg);
  117. }
  118. my_driver_status.logged_in_driver = 0;
  119. my_driver_status.driver_name[0] = '\0';
  120. paddle_req_timeout = 0;
  121. update_driver_status |= 1;
  122. return 0;
  123. }
  124. //--------------
  125. int make_paddle_request(int paddle) {
  126. struct message_record outgoing_msg;
  127. paddle_req.result = 0;
  128. paddle_req.request = paddle;
  129. paddle_req_timeout = time(NULL) + PADDLE_REQ_TIMEOUT;
  130. prepare_message(&outgoing_msg, MAILBOX_SET_PADDLE, &paddle_req, sizeof(paddle_req));
  131. return send_message(hub_fd, &outgoing_msg);
  132. }
  133. int check_paddle_request() {
  134. char buf[LINE_BUFFER_SIZE];
  135. struct message_record outgoing_msg;
  136. if (paddle_req_timeout != 0) {
  137. if (paddle_req.request == paddle_req.result) {
  138. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Set Paddle %d\n", paddle_req.request);
  139. send_message(hub_fd, &outgoing_msg);
  140. paddle_req_timeout = 0;
  141. snprintf(buf, LINE_BUFFER_SIZE, "paddle ok %i", paddle_req.result);
  142. ws_send(&g_mgr, buf);
  143. return 1;
  144. }
  145. else if ( (paddle_req.result < 0) || (paddle_req_timeout < time(NULL)) ) {
  146. format_log_message(&outgoing_msg, LOGLEVEL_DEBUG, "Failed to set Paddle %d\n", paddle_req.request);
  147. send_message(hub_fd, &outgoing_msg);
  148. paddle_req.result = 0;
  149. paddle_req.request = 0;
  150. paddle_req_timeout = 0;
  151. ws_send(&g_mgr, "paddle fail");
  152. return 1;
  153. }
  154. }
  155. return 0;
  156. }
  157. //--------------
  158. void action_nextstop() {
  159. struct message_record outgoing_msg;
  160. prepare_message(&outgoing_msg, MAILBOX_NEXT_STOP, "", 0);
  161. send_message(hub_fd, &outgoing_msg);
  162. }
  163. void action_prevstop() {
  164. struct message_record outgoing_msg;
  165. prepare_message(&outgoing_msg, MAILBOX_PREV_STOP, "", 0);
  166. send_message(hub_fd, &outgoing_msg);
  167. }