Fixed segfault in coke code, diabled debug
[tpg/opendispense2.git] / src / server / handler_coke.c
index 79ae262..f7d27b6 100644 (file)
@@ -21,7 +21,7 @@
 #include <pthread.h>
 
 #define READ_TIMEOUT   2       // 2 seconds for ReadChar
-#define TRACE_COKE     1
+#define TRACE_COKE     0
 
 // === IMPORTS ===
 
@@ -51,6 +51,8 @@ pthread_mutex_t       gCoke_Mutex = PTHREAD_MUTEX_INITIALIZER;
 // == CODE ===
 int Coke_InitHandler()
 {
+       CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
+       
        printf("connecting to coke machine...\n");
        
        giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
@@ -82,7 +84,6 @@ int Coke_InitHandler()
        
        AddPeriodicFunction(Coke_int_UpdateSlotStatuses);
        
-       CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
        return 0;
 }
 
@@ -130,10 +131,19 @@ void Coke_int_UpdateSlotStatuses(void)
        pthread_mutex_lock(&gCoke_Mutex);
        
        WaitForColon();
+       #if TRACE_COKE
+       printf("Coke_int_UpdateSlotStatuses: send d7\n");
+       #endif
        Writef("d7\r\n");       // Update slot statuses
        if( WaitForColon() )    goto ret;
-       Writef("s\n");
-       ReadLine(sizeof tmp, tmp);      // Read back what we just said
+       #if TRACE_COKE
+       printf("Coke_int_UpdateSlotStatuses: send s\n");
+       #endif
+       Writef("s\r\n");
+       do {
+               i = ReadLine(sizeof tmp, tmp);  // Read back what we just said
+               if( i == -1 )   goto ret;
+       } while(tmp[0] == ':' || tmp[0] == 's');
        
        for( i = 0; i <= 6; i ++ )
        {
@@ -144,6 +154,9 @@ void Coke_int_UpdateSlotStatuses(void)
                        #endif
                        goto ret;       // I give up :(
                }
+               #if TRACE_COKE
+               printf("Coke_int_UpdateSlotStatuses: tmp = '%s'\n", tmp);
+               #endif
                Coke_int_GetSlotStatus(tmp, i);
        }
 
@@ -196,6 +209,15 @@ int Coke_DoDispense(int UNUSED(User), int Item)
                printf("Coke_DoDispense: sending 'd7'\n");
                #endif
                Writef("d7\r\n");
+               ret ++;
+       }
+       if( ret == 3 )
+       {
+               #if TRACE_COKE
+               printf("Coke_DoDispense: timed out\n");
+               #endif
+               pthread_mutex_unlock(&gCoke_Mutex);
+               return -1;
        }
 
        #if TRACE_COKE

UCC git Repository :: git.ucc.asn.au