Debugging sqlite cokebank, slight fixes to coke handler
authorJohn Hodge <[email protected]>
Sat, 19 Feb 2011 03:25:12 +0000 (11:25 +0800)
committerJohn Hodge <[email protected]>
Sat, 19 Feb 2011 03:25:12 +0000 (11:25 +0800)
src/cokebank_sqlite/main.c
src/server/handler_coke.c
src/server/server.c

index 0d5e139..bcb6735 100644 (file)
@@ -35,6 +35,14 @@ const char * const csBank_DatabaseSetup =
 "      FOREIGN KEY (acct_id) REFERENCES accounts (acct_id) ON DELETE CASCADE"
 //                      Deletion of the account frees the card  ^ ^ ^
 ");"
+"CREATE TABLE IF NOT EXISTS items ("
+"      item_id INTEGER PRIMARY KEY NOT NULL,"
+"      item_handler STRING NOT NULL,"
+"      item_index INTEGER NOT NULL,"
+"      item_name STRING NOT NULL,"
+"      item_price INTEGER NOT NULL,"
+"      item_is_enabled BOOLEAN NOT NULL DEFAULT true"
+");"
 "INSERT INTO accounts (acct_name,acct_is_admin,acct_uid) VALUES ('root',1,0);"
 "INSERT INTO accounts (acct_name,acct_is_internal,acct_uid) VALUES ('"COKEBANK_SALES_ACCT"',1,-1);"
 "INSERT INTO accounts (acct_name,acct_is_internal,acct_uid) VALUES ('"COKEBANK_DEBT_ACCT"',1,-2);"
@@ -286,9 +294,9 @@ int Bank_GetAcctByName(const char *Name)
        if( !statement )        return -1;
        
        ret = sqlite3_column_int(statement, 0);
-       if( ret == 0 )  return -1;
-       
        sqlite3_finalize(statement);
+       
+       if( ret == 0 )  return -1;
        return ret;
 }
 
@@ -538,14 +546,24 @@ sqlite3_stmt *Bank_int_QuerySingle(sqlite3 *Database, const char *Query)
        sqlite3_stmt    *ret;
         int    rv;
        
+       #if DEBUG
+       printf("Bank_int_QuerySingle: (Query='%s')\n", Query);
+       #endif
+       
        // Prepare query
        ret = Bank_int_MakeStatemnt(Database, Query);
-       if( !ret )      return NULL;
+       if( !ret ) {
+               printf("Bank_int_QuerySingle: RETURN NULL ret=NULL\n");
+               return NULL;
+       }
        
        // Get row
        rv = sqlite3_step(ret);
        // - Empty result set
-       if( rv == SQLITE_DONE ) return NULL;
+       if( rv == SQLITE_DONE ) {
+               printf("Bank_int_QuerySingle: RETURN NULL (rv == SQLITE_DONE)\n");
+               return NULL;
+       }
        // - Other error
        if( rv != SQLITE_ROW ) {
                fprintf(stderr, "SQLite Error: %s\n", sqlite3_errmsg(gBank_Database));
@@ -553,6 +571,7 @@ sqlite3_stmt *Bank_int_QuerySingle(sqlite3 *Database, const char *Query)
                return NULL;
        }
        
+       printf("Bank_int_QuerySingle: RETURN %p\n", ret);
        return ret;
 }
 
index 56810bd..dbc2a81 100644 (file)
@@ -72,22 +72,22 @@ int Coke_CanDispense(int UNUSED(User), int Item)
        printf("Coke_CanDispense: Flushing\n");
        #endif
        
-       // Flush the input buffer
-       {
-               char    tmpbuf[512];
-               read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
-       }
        
        // Wait for a prompt
        ret = 0;
-       do {
+       while( WaitForColon() && ret < 3 )
+       {
+               // Flush the input buffer
+               char    tmpbuf[512];
+               read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
                #if TRACE_COKE
-               printf("Coke_DoDispense: sending 'd7'\n");
+               printf("Coke_CanDispense: sending 'd7'\n");
                #endif
                write(giCoke_SerialFD, "d7\r\n", 4);
-       } while( WaitForColon() && ret++ < 3 );
+               ret ++;
+       }
 
-       if( ret == 3 ) {
+       if( !(ret < 3) ) {
                fprintf(stderr, "Coke machine timed out\n");
                return -2;      // -EMYBAD
        }
@@ -169,20 +169,19 @@ int Coke_DoDispense(int UNUSED(User), int Item)
        #if TRACE_COKE
        printf("Coke_DoDispense: flushing input\n");
        #endif
-       // Flush the input buffer
+       
+       // Wait for prompt
+       ret = 0;
+       while( WaitForColon() && ret < 3 )
        {
+               // Flush the input buffer
                char    tmpbuf[512];
                read(giCoke_SerialFD, tmpbuf, sizeof(tmpbuf));
-       }
-       
-       // Wait for prompt
-       i = 0;
-       do {
                #if TRACE_COKE
                printf("Coke_DoDispense: sending 'd7'\n");
                #endif
                write(Item, "d7\r\n", 4);
-       } while( WaitForColon() && i++ < 3 );
+       }
 
        #if TRACE_COKE
        printf("Coke_DoDispense: sending 'd%i'\n", Item);
index 80c526d..17e8549 100644 (file)
@@ -1163,7 +1163,7 @@ void Debug(tClient *Client, const char *Format, ...)
        //printf("%010i [%i] ", (int)time(NULL), Client->ID);
        printf("[%i] ", Client->ID);
        va_start(args, Format);
-       vprintf(NULL, 0, Format, args);
+       vprintf(Format, args);
        va_end(args);
        printf("\n");
 }

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