3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * handler_door.c - Door Relay code
7 * This file is licenced under the 3-clause BSD Licence. See the file
8 * COPYING for full details.
23 #define DOOR_UNLOCKED_DELAY 5 // Time in seconds before the door re-locks
28 int Door_InitHandler();
29 int Door_CanDispense(int User, int Item);
30 int Door_DoDispense(int User, int Item);
31 int writes(int fd, const char *str);
32 char *ReadStatus(int FD);
35 tHandler gDoor_Handler = {
41 char *gsDoor_SerialPort; // Set from config in main.c
44 int Door_InitHandler(void)
51 int Door_CanDispense(int User, int Item)
54 printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
57 if( Item != 0 ) return -1;
59 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
62 printf("Door_CanDispense: User %i not in door\n", User);
68 printf("Door_CanDispense: User %i can open the door\n", User);
75 * \brief Actually do a dispense from the coke machine
77 int Door_DoDispense(int User, int Item)
79 int door_serial_handle;
82 printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
86 if( Item != 0 ) return -1;
88 // Check if user is in door
89 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
92 printf("Door_CanDispense: User %i not in door\n", User);
97 door_serial_handle = InitSerial(gsDoor_SerialPort, 115200);
98 if(door_serial_handle < 0) {
99 fprintf(stderr, "Unable to open door serial '%s'\n", gsDoor_SerialPort);
100 perror("Opening door port");
104 // Disable local echo
107 tcgetattr(door_serial_handle, &info);
108 info.c_cflag &= ~CLOCAL;
109 tcsetattr(door_serial_handle, TCSANOW, &info);
112 // flush(door_serial_handle);
114 writes(door_serial_handle, "4;");
117 char *status = ReadStatus(door_serial_handle);
118 if( !status ) return -1;
119 if( strcmp(status, "Opening door") != 0 ) {
120 fprintf(stderr, "Unknown/unexpected door status '%s'\n", status);
125 close(door_serial_handle);
128 printf("Door_DoDispense: User %i opened door\n", User);
134 int writes(int fd, const char *str)
136 int len = strlen(str);
138 if( len != write(fd, str, len) )
145 char *ReadStatus(int FD)
149 len = read(FD, tmpbuf, sizeof(tmpbuf)-1);
151 char *msg = strchr(tmpbuf, ',');
153 fprintf(stderr, "Door returned malformed data (no ',')\n");
157 char *end = strchr(tmpbuf, ';');
159 fprintf(stderr, "Door returned malformed data (no ';')\n");