driver.c 5.8 KB

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