3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * handler_doror.c - Door Relay code
7 * This file is licenced under the 3-clause BSD Licence. See the file
8 * COPYING for full details.
24 #define DOOR_UNLOCKED_DELAY 5 // Time in seconds before the door re-locks
29 int Door_InitHandler();
30 int Door_CanDispense(int User, int Item);
31 int Door_DoDispense(int User, int Item);
34 tHandler gDoor_Handler = {
40 char *gsDoor_SerialPort = "/dev/ttyS3";
43 int Door_InitHandler(void)
50 int Door_CanDispense(int User, int Item)
53 printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
56 if( Item != 0 ) return -1;
58 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
61 printf("Door_CanDispense: User %i not in door\n", User);
67 printf("Door_CanDispense: User %i can open the door\n", User);
74 * \brief Actually do a dispense from the coke machine
76 int Door_DoDispense(int User, int Item)
78 int door_serial_handle;
81 printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
85 if( Item != 0 ) return -1;
87 // Check if user is in door
88 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
91 printf("Door_CanDispense: User %i not in door\n", User);
96 door_serial_handle = InitSerial(gsDoor_SerialPort, 9600);
98 if( write(door_serial_handle, "ATH1\n", 5) != 5 ) {
99 fprintf(stderr, "Unable to open door (sending ATH1)\n");
100 perror("Sending ATH1");
104 // Wait before re-locking
105 sleep(DOOR_UNLOCKED_DELAY);
107 if( write(door_serial_handle, "ATH0\n", 5) != 5 ) {
108 fprintf(stderr, "Oh, hell! Door not re-locking, big error (sending ATH0 failed)\n");
109 perror("Sending ATH0");
113 close(door_serial_handle);
116 printf("Door_DoDispense: User %i opened door\n", User);