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 10 // 10 seconds before it 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_Password = "";
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)
85 printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
89 if( Item != 0 ) return -1;
91 // Check if user is in door
92 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
95 printf("Door_CanDispense: User %i not in door\n", User);
100 // Create stdin/stdout
101 if( pipe(stdin_pair) || pipe(stdout_pair) )
118 // Close write end of stdin, and set it to #0
119 close(stdin_pair[1]); dup2(stdin_pair[0], 0);
120 // Close read end of stdout, and set it to #1
121 close(stdout_pair[0]); dup2(stdout_pair[1], 1);
123 execl("/bin/sh", "sh", "-c", "llogin door -w-", NULL);
128 child_stdin = fdopen(stdin_pair[1], "w");
129 close(stdin_pair[0]); // child stdin read
130 close(stdout_pair[1]); // child stdout write
132 if(read(stdout_pair[1], buf, 512) < 0) {
134 printf("Door_DoDispense: fread fail\n");
140 if( fputs(gsDoor_Password, child_stdin) <= 0 ) {
142 printf("Door_DoDispense: fputs password\n");
146 fputs("\n", child_stdin);
150 printf("Door_DoDispense: Door unlock\n");
153 // ATH1 - Unlock door
154 if( fputs("ATH1\n", child_stdin) <= 0) {
156 printf("Door_DoDispense: fputs unlock\n");
161 // Wait before re-locking
162 sleep(DOOR_UNLOCKED_DELAY);
166 printf("Door_DoDispense: Door re-lock\n");
170 if( fputs("ATH0\n", child_stdin) <= 0 ) {
172 printf("Door_DoDispense: fputs lock\n");
178 close(stdin_pair[1]); // child stdin write
179 close(stdout_pair[0]); // child stdout read
182 printf("Door_DoDispense: User %i opened door\n", User);