From c0ecf99409a64a3cfe44127dc21503bf3e47ca80 Mon Sep 17 00:00:00 2001 From: Noe Rubinstein Date: Fri, 12 Oct 2012 18:02:04 +0200 Subject: Use SQLite in build_rom.c (untested) --- build_rom.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/build_rom.c b/build_rom.c index 4346154..70a1583 100644 --- a/build_rom.c +++ b/build_rom.c @@ -7,6 +7,9 @@ #include #include #include +#ifndef OLD_DB_FORMAT +# include +#endif #define __packed __attribute__((__packed__)) @@ -65,7 +68,12 @@ int rand_range(int max) { return (int)(rand()/(double)RAND_MAX*max); } unsigned date(void) { return time(NULL) / 86400; } static char *next_serial = "next_serial.txt"; + +#ifdef OLD_DB_FORMAT static char *db = "db.bin"; +#else +static char *db = "db.sqlite"; +#endif static void print_usage(char *name) { @@ -80,7 +88,8 @@ static void print_usage(char *name) "\t-m MAC\tUse this MAC address + the 2 next\n" "\t-f MAC\tUse random MAC addresses (start of range)\n" "\t-t MAC\tUse random MAC addresses (end of range)\n" - "\t-n NUM\tUse this number for the serial\n", name); + "\t-n NUM\tUse this number for the serial\n" + , name); } int main(int argc, char *argv[]) @@ -302,8 +311,61 @@ unsigned get_next_serial(void) return cur; } +#ifndef OLD_DB_FORMAT +# define TEST_IN_SQLITE3(cond, bind) \ + sqlite3 *db; \ + sqlite3_stmt *stmt; \ + int rc, res; \ +\ + rc = sqlite3_open(fname, &db); \ + if (SQLITE_OK != rc) { \ + fprintf(stderr, "could not open sqlite database \"%s\" " \ + "(error %d)\n", fname, rc); \ + sqlite3_close(db); \ + res = -2; \ + goto opened; \ + } \ +\ + rc = sqlite3_prepare_v2(db, "SELECT 1 FROM xioh_data " \ + "WHERE " cond, \ + -1, &stmt, NULL); \ + if (SQLITE_OK != rc) { \ + fprintf(stderr, "could not compile statement (error %d)\n", rc); \ + sqlite3_close(db); \ + res = -4; \ + goto prepared; \ + } \ +\ + bind; \ +\ + rc = sqlite3_step(stmt); \ +\ + switch (rc) { \ + case SQLITE_DONE: \ + res = false; \ + break; \ + case SQLITE_ROW: \ + res = true; \ + break; \ + default: \ + fprintf(stderr, "could not execute statement (error %d)\n", rc); \ + res = -5; \ + } \ +\ +prepared: \ + sqlite3_finalize(stmt); \ +opened: \ + sqlite3_close(db); \ +\ + if (res < 0) \ + exit(-res); \ + else \ + return res; +#endif + bool mac_used(const char *fname, mac *addr) { +#ifdef OLD_DB_FORMAT FILE *f = fopen(fname, "r"); struct xioh_data xioh_data; unsigned i; @@ -325,10 +387,19 @@ bool mac_used(const char *fname, mac *addr) fclose(f); return false; +#else + TEST_IN_SQLITE3("mac1 == ? OR mac2 == ? OR mac3 == ?", + int i; + for (i = 1; i <= 3; i++) + sqlite3_bind_blob(stmt, i, addr, sizeof(*addr), + SQLITE_STATIC)) + +#endif } bool serial_used(const char *fname, int serial) { +#ifdef OLD_DB_FORMAT FILE *f = fopen(fname, "r"); struct xioh_data xioh_data; @@ -347,10 +418,14 @@ bool serial_used(const char *fname, int serial) fclose(f); return false; +#else + TEST_IN_SQLITE3("serial_number == ?", sqlite3_bind_int(stmt, 1, serial)) +#endif } void record_use(char *fname, struct xioh_data *xioh_data) { +#ifdef OLD_DB_FORMAT FILE *f = fopen(fname, "a"); if (!f) { printf("Can't open file \"%s\": %s\n", fname, strerror(errno)); @@ -359,6 +434,57 @@ void record_use(char *fname, struct xioh_data *xioh_data) fwrite(xioh_data, sizeof(*xioh_data), 1, f); fclose(f); +#else + int rc, i; + sqlite3 *db; + sqlite3_stmt *stmt; + + rc = sqlite3_open(fname, &db); + if (SQLITE_OK != rc) { + fprintf(stderr, "could not open sqlite database \"%s\" (error %d)\n", fname, rc); + sqlite3_close(db); + exit(2); + } + + rc = sqlite3_prepare_v2(db, + "INSERT INTO xioh_data (xioh_data, version, date," + "hardware_version, serial_number, mac1, mac2, mac3 ) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)", -1, &stmt, NULL); + + if (SQLITE_OK != rc) { + fprintf(stderr, "could not compile statement (error %d)\n", rc); + sqlite3_close(db); + exit(5); + } + + sqlite3_bind_blob(stmt, 1, xioh_data, sizeof(*xioh_data), + SQLITE_TRANSIENT); + sqlite3_bind_int(stmt, 2, xioh_data->version); + sqlite3_bind_int(stmt, 3, xioh_data->serial.date); + sqlite3_bind_int(stmt, 4, xioh_data->serial.version); + sqlite3_bind_int(stmt, 5, xioh_data->serial.number); + for (i = 0; i < MAC_NUM; i++) + sqlite3_bind_blob(stmt, 6, &xioh_data->addr[i], + sizeof(*xioh_data->addr), + SQLITE_TRANSIENT); + + rc = sqlite3_step(stmt); + if (SQLITE_DONE != rc) { + fprintf(stderr, "could not insert into database: (error %d)\n", rc); + dump_xioh_data(xioh_data); + sqlite3_finalize(stmt); + sqlite3_close(db); + exit(4); + } + + sqlite3_finalize(stmt); + rc = sqlite3_close(db); + + if (SQLITE_OK != rc) { + fprintf(stderr, "Error on closing database (error %d)\n", rc); + exit(2); + } +#endif } mac mac_between(mac from, mac to) -- cgit v1.2.3