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.
19 #define DOOR_UNLOCKED_DELAY 10 // 10 seconds before it re-locks
24 int Door_InitHandler();
25 int Door_CanDispense(int User, int Item);
26 int Door_DoDispense(int User, int Item);
29 tHandler gDoor_Handler = {
35 char *gsDoor_Password = "";
36 // int giDoor_SerialFD;
39 int Door_InitHandler(void)
46 int Door_CanDispense(int User, int Item)
49 printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item);
52 if( Item != 0 ) return -1;
54 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
57 printf("Door_CanDispense: User %i not in door\n", User);
63 printf("Door_CanDispense: User %i can open the door\n", User);
70 * \brief Actually do a dispense from the coke machine
72 int Door_DoDispense(int User, int Item)
75 char buf[512]; // Buffer flush location - the sewer :)
78 printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
82 if( Item != 0 ) return -1;
84 // Check if user is in door
85 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
88 printf("Door_CanDispense: User %i not in door\n", User);
94 pipe = popen("llogin door -w -", "w");
95 if( !pipe || pipe == (void*)-1 ) {
97 printf("Door_DoDispense: llogin failure\n");
101 if( fread(buf, 512, 1, pipe) == 0 ) return -1; // Flush!
104 fputs(gsDoor_Password, pipe);
106 if( fread(buf, 512, 1, pipe) == 0 ) return -1; // Flush!
108 // ATH1 - Unlock door
109 fputs("ATH1\n", pipe);
110 if( fread(buf, 512, 1, pipe) == 0 ) return -1; // Flush!
112 // Wait before re-locking
113 sleep(DOOR_UNLOCKED_DELAY);
116 fputs("ATH0\n", pipe);
117 if( fread(buf, 512, 1, pipe) == 0 ) return -1; // Flush!
122 printf("Door_DoDispense: User %i opened door\n", User);