From: John Hodge Date: Sat, 19 Feb 2011 03:25:12 +0000 (+0800) Subject: Debugging sqlite cokebank, slight fixes to coke handler X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=f4603391737979ef0ffd939d8d3379430091aa08;p=tpg%2Fopendispense2.git Debugging sqlite cokebank, slight fixes to coke handler --- diff --git a/src/cokebank_sqlite/main.c b/src/cokebank_sqlite/main.c index 0d5e139..bcb6735 100644 --- a/src/cokebank_sqlite/main.c +++ b/src/cokebank_sqlite/main.c @@ -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; } diff --git a/src/server/handler_coke.c b/src/server/handler_coke.c index 56810bd..dbc2a81 100644 --- a/src/server/handler_coke.c +++ b/src/server/handler_coke.c @@ -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); diff --git a/src/server/server.c b/src/server/server.c index 80c526d..17e8549 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -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"); }