driver.c 5.6 KB

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