X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Fserver%2Fhandler_door.c;h=99425268a648d634196d8f0c7653e611768e1b41;hb=91d11fd7f9bf911c6abc59a18e17ec5890cee148;hp=1ffcc857feb599bdb05d0ce9ac03cc4ae05f984d;hpb=dce0ef0f3c15f9e38e57d4a14c904c339affac4e;p=tpg%2Fopendispense2.git diff --git a/src/server/handler_door.c b/src/server/handler_door.c index 1ffcc85..9942526 100644 --- a/src/server/handler_door.c +++ b/src/server/handler_door.c @@ -14,6 +14,10 @@ #include #include +#define DEBUG 1 + +#define DOOR_UNLOCKED_DELAY 10 // 10 seconds before it re-locks + // === IMPORTS === // === PROTOTYPES === @@ -28,17 +32,12 @@ tHandler gDoor_Handler = { Door_CanDispense, Door_DoDispense }; -//char *gsDoor_SerialPort = "/dev/ttyS0"; +char *gsDoor_Password = ""; // int giDoor_SerialFD; // == CODE === -int Door_InitHandler() -{ -// printf("connecting to door...\n"); -// giDoor_SerialFD = open(gsDoor_SerialPort, O_RDWR); -// if( giDoor_SerialFD == -1 ) { -// fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsDoor_SerialPort); -// } +int Door_InitHandler(void) +{ return 0; } @@ -46,8 +45,23 @@ int Door_InitHandler() */ int Door_CanDispense(int User, int Item) { + #if DEBUG + printf("Door_CanDispense: (User=%i,Item=%i)\n", User, Item); + #endif // Sanity please - if( Item == 0 ) return -1; + if( Item != 0 ) return -1; + + if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) ) + { + #if DEBUG + printf("Door_CanDispense: User %i not in door\n", User); + #endif + return 1; + } + + #if DEBUG + printf("Door_CanDispense: User %i can open the door\n", User); + #endif return 0; } @@ -57,13 +71,56 @@ int Door_CanDispense(int User, int Item) */ int Door_DoDispense(int User, int Item) { - + FILE *pipe; + char buf[512]; // Buffer flush location - the sewer :) + + #if DEBUG + printf("Door_DoDispense: (User=%i,Item=%i)\n", User, Item); + #endif + // Sanity please if( Item != 0 ) return -1; // Check if user is in door + if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) ) + { + #if DEBUG + printf("Door_CanDispense: User %i not in door\n", User); + #endif + return 1; + } // llogin or other + pipe = popen("llogin door -w -", "w"); + if( !pipe || pipe == (void*)-1 ) { + #if DEBUG + printf("Door_DoDispense: llogin failure\n"); + #endif + return -1; + } + if( fread(buf, 512, 1, pipe) == 0 ) return -1; // Flush! + + // Send password + fputs(gsDoor_Password, pipe); + fputs("\n", pipe); + if( fread(buf, 512, 1, pipe) == 0 ) return -1; // Flush! + + // ATH1 - Unlock door + fputs("ATH1\n", pipe); + if( fread(buf, 512, 1, pipe) == 0 ) return -1; // Flush! + + // Wait before re-locking + sleep(DOOR_UNLOCKED_DELAY); + + // Re-lock the door + fputs("ATH0\n", pipe); + if( fread(buf, 512, 1, pipe) == 0 ) return -1; // Flush! + + pclose(pipe); + + #if DEBUG + printf("Door_DoDispense: User %i opened door\n", User); + #endif return 0; }