Untested fix to coke code (flushes buffer beofre reading)
authorJohn Hodge <[email protected]>
Wed, 2 Feb 2011 03:48:45 +0000 (11:48 +0800)
committerJohn Hodge <[email protected]>
Wed, 2 Feb 2011 03:48:45 +0000 (11:48 +0800)
- Changed dispense logic to only do a transfer on a non-zero price

src/server/dispense.c
src/server/handler_coke.c
src/server/handler_door.c

index 81d49dc..09fe567 100644 (file)
@@ -29,11 +29,14 @@ int DispenseItem(int ActualUser, int User, tItem *Item)
        }
 
        // Subtract the balance
-       reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name);
-       if( !reason )   reason = Item->Name;    // TODO: Should I instead return an error?
-       ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_SALES_ACCT), Item->Price, reason);
-       free(reason);
-       if(ret) return 2;       // 2: No balance
+       if( Item->Price )
+       {
+               reason = mkstr("Dispense - %s:%i %s", handler->Name, Item->ID, Item->Name);
+               if( !reason )   reason = Item->Name;    // TODO: Should I instead return an error?
+               ret = _Transfer( User, Bank_GetAcctByName(COKEBANK_SALES_ACCT), Item->Price, reason);
+               free(reason);
+               if(ret) return 2;       // 2: No balance
+       }
        
        // Get username for debugging
        username = Bank_GetAcctName(User);
@@ -44,7 +47,8 @@ int DispenseItem(int ActualUser, int User, tItem *Item)
                if(ret) {
                        Log_Error("Dispense failed after deducting cost (%s dispensing %s - %ic)",
                                username, Item->Name, Item->Price);
-                       _Transfer( Bank_GetAcctByName(COKEBANK_SALES_ACCT), User, Item->Price, "rollback" );
+                       if( Item->Price )
+                               _Transfer( Bank_GetAcctByName(COKEBANK_SALES_ACCT), User, Item->Price, "rollback" );
                        free( username );
                        return -1;      // 1: Unkown Error again
                }
@@ -156,7 +160,7 @@ int _GetMinBalance(int Account)
        // Admin to -$10
        if( flags & USER_FLAG_ADMIN )   return -1000;
        
-       // Coke to -$10
+       // Coke to -$5
        if( flags & USER_FLAG_COKE )    return -500;
        
        // Anyone else, non-negative
index 5dc4a35..a6b3cb9 100644 (file)
@@ -65,6 +65,12 @@ int Coke_CanDispense(int UNUSED(User), int Item)
        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 {
@@ -134,6 +140,12 @@ int Coke_DoDispense(int UNUSED(User), int Item)
        // 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;
index a819d77..1341679 100644 (file)
@@ -28,11 +28,12 @@ tHandler    gDoor_Handler = {
        Door_CanDispense,
        Door_DoDispense
 };
-//char *gsDoor_SerialPort = "/dev/ttyS0";
+char   *gsDoor_Password;
+char   *gsDoor_Command;
 // int giDoor_SerialFD;
 
 // == CODE ===
-int Door_InitHandler()
+int Door_InitHandler(void)
 {
 //     printf("connecting to door...\n");
 //     giDoor_SerialFD = open(gsDoor_SerialPort, O_RDWR);
@@ -51,6 +52,9 @@ int Door_CanDispense(int User, int Item)
        
        if( !(Bank_GetFlags(User) & USER_FLAG_DOORGROUP) )
                return 1;
+               
+       gsDoor_Command = malloc(sizeof("llogin door -w ")+strlen(gsDoor_Password));
+       sprintf(gsDoor_Command, "llogin door -w %s", gsDoor_Password);
        
        return 0;
 }
@@ -60,6 +64,7 @@ int Door_CanDispense(int User, int Item)
  */
 int Door_DoDispense(int User, int Item)
 {
+       FILE    *pipe;
        // Sanity please
        if( Item != 0 ) return -1;
        
@@ -68,6 +73,15 @@ int Door_DoDispense(int User, int Item)
                return 1;
        
        // llogin or other
+       pipe = popen(gsDoor_Command, "w");
+       if( !pipe || pipe == (void*)-1 )
+               return -1;
+       
+       fputs("ATH1F", pipe);
+       sleep(1);
+       fputs("ATH10", pipe);
+       
+       pclose(pipe);
 
        return 0;
 }

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