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)
87 printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item);
91 if( Item != 0 ) return -1;
93 // Check if user is in door
94 if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
97 printf("Door_CanDispense: User %i not in door\n", User);
103 // Create stdin/stdout
104 if( pipe(stdin_pair) || pipe(stdout_pair) )
121 // Close write end of stdin, and set it to #0
122 close(stdin_pair[1]); dup2(stdin_pair[0], 0);
123 // Close read end of stdout, and set it to #1
124 close(stdout_pair[0]); dup2(stdout_pair[1], 1);
126 execl("/bin/sh", "sh", "-c", "llogin door -w-", NULL);
131 child_stdin = fdopen(stdin_pair[1], "w");
132 close(stdin_pair[0]); // child stdin read
133 close(stdout_pair[1]); // child stdout write
137 child_stdin = popen("llogin door -w -", "w");
138 if( !child_stdin || child_stdin == (void*)-1 ) {
140 printf("Door_DoDispense: llogin failure\n");
146 if(fread(buf, 1, 512, child_stdin) == 0) {
148 printf("Door_DoDispense: fread fail\n");
154 if( fputs(gsDoor_Password, child_stdin) <= 0 ) {
156 printf("Door_DoDispense: fputs password\n");
160 fputs("\n", child_stdin);
164 printf("Door_DoDispense: Door unlock\n");
167 // ATH1 - Unlock door
168 if( fputs("ATH1\n", child_stdin) <= 0) {
170 printf("Door_DoDispense: fputs unlock\n");
175 // Wait before re-locking
176 sleep(DOOR_UNLOCKED_DELAY);
180 printf("Door_DoDispense: Door re-lock\n");
184 if( fputs("ATH0\n", child_stdin) <= 0 ) {
186 printf("Door_DoDispense: fputs lock\n");
193 close(stdin_pair[1]); // child stdin write
194 close(stdout_pair[0]); // child stdout read
200 printf("Door_DoDispense: User %i opened door\n", User);