X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=src%2Fserver%2Fhandler_coke.c;h=9ec2576aa5f458614721d7fa504e7d2045a5dcf8;hb=4479b79dd78867af8d7cd236df73ded9bf5cc07a;hp=188beb44ffb2b85ab9408e30715b71387cad89df;hpb=b45406d25eee76ccd4cb95541edd98f2e498c69a;p=tpg%2Fopendispense2.git diff --git a/src/server/handler_coke.c b/src/server/handler_coke.c index 188beb4..9ec2576 100644 --- a/src/server/handler_coke.c +++ b/src/server/handler_coke.c @@ -18,6 +18,7 @@ #include #include #include +#include #define READ_TIMEOUT 2 // 2 seconds for ReadChar #define TRACE_COKE 1 @@ -26,6 +27,8 @@ // === PROTOTYPES === int Coke_InitHandler(); + int Coke_int_GetSlotStatus(char *Buffer, int Slot); +void Coke_int_UpdateSlotStatuses(void); int Coke_CanDispense(int User, int Item); int Coke_DoDispense(int User, int Item); int Writef(const char *Format, ...); @@ -42,6 +45,8 @@ tHandler gCoke_Handler = { char *gsCoke_SerialPort = "/dev/ttyS0"; int giCoke_SerialFD; regex_t gCoke_StatusRegex; + int gaCoke_CachedStatus[7]; +pthread_mutex_t gCoke_Mutex = PTHREAD_MUTEX_INITIALIZER; // == CODE === int Coke_InitHandler() @@ -70,117 +75,92 @@ int Coke_InitHandler() Writef("n5 Slot5\n"); WaitForColon(); Writef("n6 Coke\n"); + + Coke_int_UpdateSlotStatuses(); } } + AddPeriodicFunction(Coke_int_UpdateSlotStatuses); + CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED); return 0; } -int Coke_CanDispense(int UNUSED(User), int Item) +int Coke_int_GetSlotStatus(char *Buffer, int Slot) { - char tmp[40], *status; regmatch_t matches[4]; int ret; - - // Sanity please - if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD + char *status; - // Can't dispense if the machine is not connected - if( giCoke_SerialFD == -1 ) - return -2; + // Parse status response + ret = RunRegex(&gCoke_StatusRegex, Buffer, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response"); + if( ret ) { + return -1; + } + + // Get slot status + Buffer[ matches[3].rm_eo ] = '\0'; + status = &Buffer[ matches[3].rm_so ]; #if TRACE_COKE - printf("Coke_CanDispense: Flushing\n"); + printf("Coke_CanDispense: Machine responded slot status '%s'\n", status); #endif - - - // Wait for a prompt - ret = 0; - while( WaitForColon() && ret < 3 ) - { - // Flush the input buffer - char tmpbuf[512]; - read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf)); - #if TRACE_COKE - printf("Coke_CanDispense: sending 'd7'\n"); - #endif - Writef("d7\r\n"); - ret ++; - } - // Check for a timeout error - if( !(ret < 3) ) { - fprintf(stderr, "Coke machine timed out\n"); - return -2; // -EMYBAD + + if( strcmp(status, "full") == 0 ) { + gaCoke_CachedStatus[Slot] = 0; // 0: Avaliiable + return 0; } - - // You need to do a 'd7' before reading the status - // - Otherwise it sometimes reports a full slot as empty - // [TPG] (2011-02-19) - if( ret == 0 ) - { - Writef("d7\r\n"); - WaitForColon(); + else { + gaCoke_CachedStatus[Slot] = 1; // 1: Empty + return 1; } +} - // TODO: Handle "not ok" response to D7 +/** + * \brief Update the status of all coke slots + * \note Uses goto to reduce the chance of the lock being kept + */ +void Coke_int_UpdateSlotStatuses(void) +{ + int i, len; + char tmp[40]; - #if TRACE_COKE - printf("Coke_CanDispense: sending 's%i'\n", Item); - #endif + if( giCoke_SerialFD == -1 ) return ; - // Ask the coke machine - Writef("s%i\r\n", Item); - - #if TRACE_COKE - printf("Coke_CanDispense: reading response\n"); - #endif - // Read from the machine (ignoring empty lines) - while( (ret = ReadLine(sizeof(tmp)-1, tmp)) == 0 ); - #if TRACE_COKE - printf("ret = %i, tmp = '%s'\n", ret, tmp); - #endif - // Read back-echoed lines - while( tmp[0] == ':' || tmp[1] != 'l' ) + pthread_mutex_lock(&gCoke_Mutex); + + WaitForColon(); + Writef("d7\r\n"); // Update slot statuses + if( WaitForColon() ) goto ret; + Writef("s\n"); + ReadLine(sizeof tmp, tmp); // Read back what we just said + + for( i = 0; i <= 6; i ++ ) { - ret = ReadLine(sizeof(tmp)-1, tmp); - printf("ret = %i, tmp = '%s'\n", ret, tmp); - } - - // Catch an error - if( ret <= 0 ) { - fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret); - if( ret == -1 ) { - perror("Coke Machine"); + len = ReadLine(sizeof tmp, tmp); + if( len == -1 ) { + #if TRACE_COKE + printf("Coke_int_UpdateSlotStatuses: Read failed on slot %i\n", i); + #endif + goto ret; // I give up :( } - return -1; + Coke_int_GetSlotStatus(tmp, i); } - - #if TRACE_COKE - printf("Coke_CanDispense: wait for the prompt again\n"); - #endif - - // Eat rest of response - WaitForColon(); - // Parse status response - ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response"); - if( ret ) { - return -1; - } +ret: + pthread_mutex_unlock(&gCoke_Mutex); +} - // Get slot status - tmp[ matches[3].rm_eo ] = '\0'; - status = &tmp[ matches[3].rm_so ]; +int Coke_CanDispense(int UNUSED(User), int Item) +{ + // Sanity please + if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD - #if TRACE_COKE - printf("Coke_CanDispense: Machine responded slot status '%s'\n", status); - #endif - - if( strcmp(status, "full") == 0 ) - return 0; - - return 1; + // Can't dispense if the machine is not connected + if( giCoke_SerialFD == -1 ) + return -2; + + return gaCoke_CachedStatus[Item]; } /** @@ -189,7 +169,7 @@ int Coke_CanDispense(int UNUSED(User), int Item) int Coke_DoDispense(int UNUSED(User), int Item) { char tmp[32]; - int ret; + int ret, len; // Sanity please if( Item < 0 || Item > 6 ) return -1; @@ -198,6 +178,9 @@ int Coke_DoDispense(int UNUSED(User), int Item) if( giCoke_SerialFD == -1 ) return -2; + // LOCK + pthread_mutex_lock(&gCoke_Mutex); + #if TRACE_COKE printf("Coke_DoDispense: flushing input\n"); #endif @@ -213,6 +196,7 @@ int Coke_DoDispense(int UNUSED(User), int Item) printf("Coke_DoDispense: sending 'd7'\n"); #endif Writef("d7\r\n"); + ret ++; } #if TRACE_COKE @@ -224,7 +208,10 @@ int Coke_DoDispense(int UNUSED(User), int Item) // Read empty lines and echo-backs do { ret = ReadLine(sizeof(tmp)-1, tmp); - if( ret == -1 ) return -1; + if( ret == -1 ) { + pthread_mutex_unlock(&gCoke_Mutex); + return -1; + } #if TRACE_COKE printf("Coke_DoDispense: read %i '%s'\n", ret, tmp); #endif @@ -236,17 +223,31 @@ int Coke_DoDispense(int UNUSED(User), int Item) printf("Coke_DoDispense: done\n"); #endif - // TODO: Regex + // TODO: Regex instead? if( strcmp(tmp, "ok") == 0 ) { // We think dispense worked // - The machine returns 'ok' if an empty slot is dispensed, even if // it doesn't actually try to dispense (no sound) - return 0; + ret = 0; } - - printf("Machine returned unknown value '%s'\n", tmp); - - return -1; + else { + printf("Coke_DoDispense: Machine returned unknown value '%s'\n", tmp); + ret = -1; + } + + #if TRACE_COKE + printf("Coke_DoDispense: Updating slot status\n"); + #endif + // Update status + Writef("s%i\r\n", Item); + len = ReadLine(sizeof tmp, tmp); + if(len == -1) gaCoke_CachedStatus[Item] = -1; + Coke_int_GetSlotStatus(tmp, Item); + + // Release and return + pthread_mutex_unlock(&gCoke_Mutex); + + return ret; } char ReadChar()