Untested fix to coke code (flushes buffer beofre reading)
[tpg/opendispense2.git] / src / server / handler_coke.c
index 030740c..a6b3cb9 100644 (file)
@@ -52,7 +52,7 @@ int Coke_InitHandler()
        return 0;
 }
 
-int Coke_CanDispense(int User, int Item)
+int Coke_CanDispense(int UNUSED(User), int Item)
 {
        char    tmp[40], *status;
        regmatch_t      matches[4];
@@ -61,6 +61,17 @@ int Coke_CanDispense(int User, int Item)
        // Sanity please
        if( Item < 0 || Item > 6 )      return -1;      // -EYOURBAD
        
+       // Can't dispense if the machine is not connected
+       if( giCoke_SerialFD == -1 )
+               return -2;
+       
+       // Flush the input buffer
+       {
+               char    tmpbuf[512];
+               read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
+       }
+       
+       // Wait for a prompt
        ret = 0;
        do {
                write(giCoke_SerialFD, "d7\r\n", 4);
@@ -118,7 +129,7 @@ int Coke_CanDispense(int User, int Item)
 /**
  * \brief Actually do a dispense from the coke machine
  */
-int Coke_DoDispense(int User, int Item)
+int Coke_DoDispense(int UNUSED(User), int Item)
 {
        char    tmp[32];
         int    i, ret;
@@ -126,6 +137,16 @@ int Coke_DoDispense(int User, int Item)
        // Sanity please
        if( Item < 0 || Item > 6 )      return -1;
 
+       // Can't dispense if the machine is not connected
+       if( giCoke_SerialFD == -1 )
+               return -2;
+       
+       // Flush the input buffer
+       {
+               char    tmpbuf[512];
+               read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
+       }
+
        // Wait for prompt
        i = 0;
        do {

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