From 189939d8928c4b490a97411b977166fa83d147e6 Mon Sep 17 00:00:00 2001 From: christophe Date: Thu, 10 Jan 2013 19:14:52 +0100 Subject: [PATCH 1/6] Adding sqlite support for logging, need to add command line option --- Makefile | 2 +- README.markdown | 6 +- wmr100.c | 255 +++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 216 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index 3e6666d..28e1c03 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CFLAGS += `pkg-config libhid --cflags` -pedantic -Wall -D_GNU_SOURCE -LIBS += `pkg-config libhid --libs` +LIBS += `pkg-config libhid --libs` -lsqlite3 -lpthread wmr100: wmr100.c cc ${CFLAGS} -o wmr100 wmr100.c ${LIBS} diff --git a/README.markdown b/README.markdown index 7f01698..69e3c28 100644 --- a/README.markdown +++ b/README.markdown @@ -24,11 +24,15 @@ You'll need to setup the udev rules (see udev/README) if you want to run this not as root. This is due to how libhid accesses the USB ports. +This fork add support to sqlite logging + Requisites ---------- libhid-dev (or similarly named) package installed. pkg-config package installed. +libsqlite3-dev package installed. +libusb-dev package installed. Building -------- @@ -42,7 +46,7 @@ To keep the default HIDManager from taking the wmr100, run this once: `make setu If you want to use different software to read the wmr100 device, you should undo this by running `make unsetup_osx` and then reboot so that the HIDManager will take control of the device again. - +To install on raspberry pi use this script : https://github.com/think-free/pi-scripts/raw/master/InstallWmr100OnPi.sh Usage ----- diff --git a/wmr100.c b/wmr100.c index ce8933e..0b9e396 100644 --- a/wmr100.c +++ b/wmr100.c @@ -15,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ #include @@ -25,11 +24,18 @@ #include #include #include +#include +#include #define WMR100_VENDOR_ID 0x0fde #define WMR100_PRODUCT_ID 0xca01 +/**************************** + Variables definition +****************************/ + /* globals */ + #define OUTPUT_BOTH 3 #define OUTPUT_FILE 2 #define OUTPUT_STDOUT 1 @@ -37,6 +43,9 @@ int gOutput = OUTPUT_BOTH; /* constants */ + +#define MAXSENSORS 5 + int const RECV_PACKET_LEN = 8; int const BUF_SIZE = 255; unsigned char const PATHLEN = 2; @@ -45,6 +54,12 @@ int const PATH_OUT[] = { 0xff000001, 0xff000002 }; unsigned char const INIT_PACKET1[] = { 0x20, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00 }; unsigned char const INIT_PACKET2[] = { 0x01, 0xd0, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00 }; +char *const SMILIES[] = { " ", ":D", ":(", ":|" }; +char *const TRENDS[] = { "-", "U", "D" }; +char *const WINDIES[] = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NWN" }; + +/* WMR */ + typedef struct _WMR { int pos; int remain; @@ -54,8 +69,46 @@ typedef struct _WMR { char *data_filename; } WMR; -void dump_packet(unsigned char *packet, int len) -{ +WMR *wmr = NULL; + +/* Sqlite loggin */ + +typedef struct _TEMP { + + float temp; + int smile; + int humidity; + float dewpoint; + char *trend; +} TEMP; + +typedef struct _WATER { + + float temp; +} WATER; + +typedef struct _CURRENTCONDITION { + + int pressure; + int forecast; + int rain_rate; + float rain_hour_total; + float rain_all_total; + char *wind_dir; + float wind_speed; + float wind_avg_speed; + int uv; + WATER water[MAXSENSORS]; + TEMP temp[MAXSENSORS]; +} CURRENTCONDITION; + +CURRENTCONDITION *currentcondition = NULL; + +/**************************** + Dump packet +****************************/ + +void dump_packet(unsigned char *packet, int len){ int i; printf("Receive packet len %d: ", len); @@ -68,7 +121,7 @@ void dump_packet(unsigned char *packet, int len) WMR methods ****************************/ -WMR *wmr_new() { +WMR *wmr_new(){ WMR *wmr = malloc(sizeof(WMR)); wmr->remain = 0; wmr->buffer = malloc(BUF_SIZE); @@ -81,7 +134,7 @@ WMR *wmr_new() { return wmr; } -void wmr_send_packet_init(WMR *wmr) { +void wmr_send_packet_init(WMR *wmr){ int ret; ret = hid_set_output_report(wmr->hid, PATH_IN, PATHLEN, (char*)INIT_PACKET1, sizeof(INIT_PACKET1)); @@ -91,7 +144,7 @@ void wmr_send_packet_init(WMR *wmr) { } } -void wmr_send_packet_ready(WMR *wmr) { +void wmr_send_packet_ready(WMR *wmr){ int ret; ret = hid_set_output_report(wmr->hid, PATH_IN, PATHLEN, (char*)INIT_PACKET2, sizeof(INIT_PACKET2)); @@ -101,7 +154,7 @@ void wmr_send_packet_ready(WMR *wmr) { } } -int wmr_init(WMR *wmr) { +int wmr_init(WMR *wmr){ hid_return ret; HIDInterfaceMatcher matcher = { WMR100_VENDOR_ID, WMR100_PRODUCT_ID, NULL, NULL, 0 }; int retries; @@ -150,11 +203,12 @@ int wmr_init(WMR *wmr) { return 0; } -void wmr_print_state(WMR *wmr) { +void wmr_print_state(WMR *wmr){ + fprintf(stderr, "WMR: HID: %p\n", (void *)wmr->hid); } -int wmr_close(WMR *wmr) { +int wmr_close(WMR *wmr){ hid_return ret; ret = hid_close(wmr->hid); @@ -179,8 +233,7 @@ int wmr_close(WMR *wmr) { return 0; } -void wmr_read_packet(WMR *wmr) -{ +void wmr_read_packet(WMR *wmr){ int ret, len; ret = hid_interrupt_read(wmr->hid, @@ -202,8 +255,7 @@ void wmr_read_packet(WMR *wmr) /* dump_packet(wmr->buffer + 1, wmr->remain); */ } -int wmr_read_byte(WMR *wmr) -{ +int wmr_read_byte(WMR *wmr){ while(wmr->remain == 0) { wmr_read_packet(wmr); } @@ -257,8 +309,11 @@ void wmr_log_data(WMR *wmr, char *msg) { printf("DATA[%s]:%s\n", outstr, msg); } -void wmr_handle_rain(WMR *wmr, unsigned char *data, int len) -{ +/**************************** + Data handlers + ****************************/ + +void wmr_handle_rain(WMR *wmr, unsigned char *data, int len){ int sensor, power, rate; float hour, day, total; int smi, sho, sda, smo, syr; @@ -283,11 +338,7 @@ void wmr_handle_rain(WMR *wmr, unsigned char *data, int len) free(msg); } -char *const SMILIES[] = { " ", ":D", ":(", ":|" }; -char *const TRENDS[] = { "-", "U", "D" }; - -void wmr_handle_temp(WMR *wmr, unsigned char *data, int len) -{ +void wmr_handle_temp(WMR *wmr, unsigned char *data, int len){ int sensor, st, smiley, trend, humidity; float temp, dewpoint; char *trendTxt = ""; @@ -313,8 +364,7 @@ void wmr_handle_temp(WMR *wmr, unsigned char *data, int len) free(msg); } -void wmr_handle_water(WMR *wmr, unsigned char *data, int len) -{ +void wmr_handle_water(WMR *wmr, unsigned char *data, int len){ int sensor; float temp; char *msg; @@ -329,8 +379,7 @@ void wmr_handle_water(WMR *wmr, unsigned char *data, int len) free(msg); } -void wmr_handle_pressure(WMR *wmr, unsigned char *data, int len) -{ +void wmr_handle_pressure(WMR *wmr, unsigned char *data, int len){ int pressure, forecast, alt_pressure, alt_forecast; char *msg; @@ -344,8 +393,7 @@ void wmr_handle_pressure(WMR *wmr, unsigned char *data, int len) free(msg); } -void wmr_handle_uv(WMR *wmr, unsigned char *data, int len) -{ +void wmr_handle_uv(WMR *wmr, unsigned char *data, int len){ char *msg; asprintf(&msg, "type=UV"); @@ -353,10 +401,7 @@ void wmr_handle_uv(WMR *wmr, unsigned char *data, int len) free(msg); } -char *const WINDIES[] = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NWN" }; - -void wmr_handle_wind(WMR *wmr, unsigned char *data, int len) -{ +void wmr_handle_wind(WMR *wmr, unsigned char *data, int len){ char *msg; int wind_dir, power, low_speed, high_speed; char *wind_str; @@ -377,8 +422,7 @@ void wmr_handle_wind(WMR *wmr, unsigned char *data, int len) free(msg); } -void wmr_handle_clock(WMR *wmr, unsigned char *data, int len) -{ +void wmr_handle_clock(WMR *wmr, unsigned char *data, int len){ int power, powered, battery, rf, level, mi, hr, dy, mo, yr; char *msg; @@ -399,8 +443,11 @@ void wmr_handle_clock(WMR *wmr, unsigned char *data, int len) free(msg); } -void wmr_handle_packet(WMR *wmr, unsigned char *data, int len) -{ +/**************************** + Processing + ****************************/ + +void wmr_handle_packet(WMR *wmr, unsigned char *data, int len){ if (gOutput & OUTPUT_STDOUT) dump_packet(data, len); @@ -429,8 +476,7 @@ void wmr_handle_packet(WMR *wmr, unsigned char *data, int len) } } -void wmr_read_data(WMR *wmr) -{ +void wmr_read_data(WMR *wmr){ int i, j, unk1, type, data_len; unsigned char *data; @@ -497,17 +543,13 @@ void wmr_read_data(WMR *wmr) wmr_send_packet_ready(wmr); } -void wmr_process(WMR *wmr) -{ +void wmr_process(WMR *wmr){ while(true) { wmr_read_data(wmr); } } -WMR *wmr = NULL; - -void cleanup(int sig_num) -{ +void cleanup(int sig_num){ printf("Caught signal, cleaning up\n"); if (wmr != NULL) { wmr_close(wmr); @@ -517,11 +559,121 @@ void cleanup(int sig_num) exit(0); } -int main(int argc, char* argv[]) -{ +/**************************** + Sqlite database +****************************/ + +sqlite3* openDb(){ + + sqlite3 *db; + char dbFile[16]; + time_t t; + struct tm *tmp; + t = time(NULL); + tmp = gmtime(&t); + + strftime(dbFile, sizeof(dbFile), "weather-%Y.db", tmp); + + printf("Opening Sqlite database %s...\n",dbFile); + if(SQLITE_OK != sqlite3_open(dbFile, &db)) { + fprintf(stderr,"Error: Can't open database file\n"); + return 0; + } + return db; +} + +void closeDb(sqlite3 *db){ + sqlite3_close(db); +} + +bool checkTables(sqlite3 *db) { + + int i; + char consulta[36]; + int nTablas = 1; + char *tabla[] = { + "history" + }; + char *create[] = { + "CREATE TABLE history(" + "history INTEGER PRIMARY KEY," + "date TEXT," + "pressure INTEGER," + "forecast INTEGER," + "rain_rate INTEGER," + "rain_hour_total REAL," + "rain_all_total REAL," + "wind_dir TEXT," + "wind_speed REAL," + "wind_avg_speed REAL," + "uv INTEGER," + "temp1 REAL," + "temp2 REAL," + "temp3 REAL," + "temp4 REAL," + "temp5 REAL," + "smile1 TEXT," + "smile2 TEXT," + "smile3 TEXT," + "smile4 TEXT," + "smile5 TEXT," + "humidity1 INTEGER," + "humidity2 INTEGER," + "humidity3 INTEGER," + "humidity4 INTEGER," + "humidity5 INTEGER," + "dewpoint1 REAL," + "dewpoint2 REAL," + "dewpoint3 REAL," + "dewpoint4 REAL," + "dewpoint5 REAL," + "trend1 TEXT," + "trend2 TEXT," + "trend3 TEXT," + "trend4 TEXT," + "trend5 TEXT," + "waterTemp1 REAL," + "waterTemp2 REAL," + "waterTemp3 REAL," + "waterTemp4 REAL," + "waterTemp5 REAL);" + }; + + for( i = 0; i < nTablas; i++) { + sprintf(consulta, "SELECT COUNT(*) FROM %s;", tabla[i]); + if(SQLITE_OK != sqlite3_exec(db, consulta, 0, 0, 0)) { + fprintf(stderr,"La tabla %s no existe\n",tabla[i]); + if(SQLITE_OK != sqlite3_exec(db, create[i], 0, 0, 0)) { + fprintf(stderr,"Error al crear la tabla %s\n",tabla[i]); + return false; + } + } + } + return true; +} + +void* sqliteLoggerThreadFct(void *args){ + + while(1){ + + sleep(1); + printf("In the thread\n"); + } + +} + +/**************************** + Main + ****************************/ + +int main(int argc, char* argv[]){ + int ret; int c; - + sqlite3 *db; + + pthread_t sqliteLoggerThread; + signal(SIGINT, cleanup); signal(SIGTERM, cleanup); @@ -551,6 +703,19 @@ int main(int argc, char* argv[]) } } + /* SQLITE INIT */ + ; + if ((db = openDb()) == 0){ + exit(-1); + } + checkTables(db); + closeDb(db); + + pthread_create(&sqliteLoggerThread, NULL, &sqliteLoggerThreadFct, NULL); + /* pthread_cancel(sqliteLoggerThread); */ + + /* WMR INIT */ + wmr = wmr_new(); if (wmr == NULL) { fprintf(stderr, "wmr_new failed\n"); From 46ffa530a033b882e5536979f1f802d699cffcc3 Mon Sep 17 00:00:00 2001 From: christophe Date: Thu, 10 Jan 2013 19:43:10 +0100 Subject: [PATCH 2/6] Uploading rights files >_< --- Makefile | 2 +- wmr100.c | 178 +++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 148 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 28e1c03..cb8a236 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS += `pkg-config libhid --cflags` -pedantic -Wall -D_GNU_SOURCE +CFLAGS += `pkg-config libhid --cflags` -pedantic -Wall -D_GNU_SOURCE -Wno-overlength-strings LIBS += `pkg-config libhid --libs` -lsqlite3 -lpthread wmr100: wmr100.c diff --git a/wmr100.c b/wmr100.c index 0b9e396..085a7d5 100644 --- a/wmr100.c +++ b/wmr100.c @@ -45,6 +45,7 @@ int gOutput = OUTPUT_BOTH; /* constants */ #define MAXSENSORS 5 +#define RECORD_HISTORY 60 int const RECV_PACKET_LEN = 8; int const BUF_SIZE = 255; @@ -93,6 +94,7 @@ typedef struct _CURRENTCONDITION { int forecast; int rain_rate; float rain_hour_total; + float rain_day_total; float rain_all_total; char *wind_dir; float wind_speed; @@ -104,6 +106,11 @@ typedef struct _CURRENTCONDITION { CURRENTCONDITION *currentcondition = NULL; +/* SQLITE WRITTER THREAD */ + +pthread_t sqliteLoggerThread; +pthread_mutex_t currentcondition_lock; + /**************************** Dump packet ****************************/ @@ -333,6 +340,13 @@ void wmr_handle_rain(WMR *wmr, unsigned char *data, int len){ smo = data[13]; syr = data[14] + 2000; + pthread_mutex_lock(¤tcondition_lock); + currentcondition->rain_rate = rate; + currentcondition->rain_hour_total = hour; + currentcondition->rain_day_total = day; + currentcondition->rain_all_total = total; + pthread_mutex_unlock(¤tcondition_lock); + asprintf(&msg, "type=RAIN,sensor=%d,power=%d,rate=%d,hour_total=%.2f,day_total=%.2f,all_total=%.2f,since=%04d%02d%02d%02d%02d", sensor, power, rate, hour, day, total, syr, smo, sda, sho, smi); wmr_log_data(wmr, msg); free(msg); @@ -358,6 +372,14 @@ void wmr_handle_temp(WMR *wmr, unsigned char *data, int len){ dewpoint = (data[6] + ((data[7] & 0x0f) << 8)) / 10.0; if ((data[7] >> 4) == 0x8) dewpoint = -dewpoint; + + pthread_mutex_lock(¤tcondition_lock); + currentcondition->temp[sensor].temp = temp; + currentcondition->temp[sensor].smile = smiley; + currentcondition->temp[sensor].humidity = humidity; + currentcondition->temp[sensor].dewpoint = dewpoint; + currentcondition->temp[sensor].trend = trendTxt; + pthread_mutex_unlock(¤tcondition_lock); asprintf(&msg, "type=TEMP,sensor=%d,smile=%d,trend=%s,temp=%.1f,humidity=%d,dewpoint=%.1f", sensor, smiley, trendTxt, temp, humidity, dewpoint); wmr_log_data(wmr, msg); @@ -374,6 +396,10 @@ void wmr_handle_water(WMR *wmr, unsigned char *data, int len){ temp = (data[3] + ((data[4] & 0x0f) << 8)) / 10.0; if ((data[4] >> 4) == 0x8) temp = -temp; + pthread_mutex_lock(¤tcondition_lock); + currentcondition->water[sensor].temp = temp; + pthread_mutex_unlock(¤tcondition_lock); + asprintf(&msg, "type=WATER,sensor=%d,temp=%.1f", sensor, temp); wmr_log_data(wmr, msg); free(msg); @@ -388,6 +414,11 @@ void wmr_handle_pressure(WMR *wmr, unsigned char *data, int len){ alt_pressure = data[4] + ((data[5] & 0x0f) << 8); alt_forecast = data[5] >> 4; + pthread_mutex_lock(¤tcondition_lock); + currentcondition->pressure = pressure; + currentcondition->forecast = forecast; + pthread_mutex_unlock(¤tcondition_lock); + asprintf(&msg, "type=PRESSURE,pressure=%d,forecast=%d,altpressure=%d,altforecast=%d", pressure, forecast, alt_pressure, alt_forecast); wmr_log_data(wmr, msg); free(msg); @@ -417,6 +448,12 @@ void wmr_handle_wind(WMR *wmr, unsigned char *data, int len){ high_speed = data[6] << 4; avg_speed = (high_speed + low_speed) / 10.0; + pthread_mutex_lock(¤tcondition_lock); + currentcondition->wind_speed = wind_speed; + currentcondition->wind_dir = wind_str; + currentcondition->wind_avg_speed = avg_speed; + pthread_mutex_unlock(¤tcondition_lock); + asprintf(&msg, "type=WIND,power=%d,dir=%s,speed=%.1f,avgspeed=%.1f", power, wind_str, wind_speed, avg_speed); wmr_log_data(wmr, msg); free(msg); @@ -550,12 +587,17 @@ void wmr_process(WMR *wmr){ } void cleanup(int sig_num){ + printf("Caught signal, cleaning up\n"); + if (wmr != NULL) { wmr_close(wmr); wmr = NULL; } + pthread_cancel(sqliteLoggerThread); + pthread_mutex_destroy(¤tcondition_lock); + exit(0); } @@ -566,17 +608,16 @@ void cleanup(int sig_num){ sqlite3* openDb(){ sqlite3 *db; - char dbFile[16]; + char dbFile[20]; time_t t; struct tm *tmp; t = time(NULL); tmp = gmtime(&t); - strftime(dbFile, sizeof(dbFile), "weather-%Y.db", tmp); + strftime(dbFile, sizeof(dbFile), "weather-%Y-%m.db", tmp); - printf("Opening Sqlite database %s...\n",dbFile); if(SQLITE_OK != sqlite3_open(dbFile, &db)) { - fprintf(stderr,"Error: Can't open database file\n"); + fprintf(stderr,"Error: Can't open database file : %s\n", dbFile); return 0; } return db; @@ -586,12 +627,12 @@ void closeDb(sqlite3 *db){ sqlite3_close(db); } -bool checkTables(sqlite3 *db) { +bool checkTablesCreated(sqlite3 *db) { int i; - char consulta[36]; - int nTablas = 1; - char *tabla[] = { + char request[36]; + int nTables = 1; + char *tables[] = { "history" }; char *create[] = { @@ -602,6 +643,7 @@ bool checkTables(sqlite3 *db) { "forecast INTEGER," "rain_rate INTEGER," "rain_hour_total REAL," + "rain_day_total REAL," "rain_all_total REAL," "wind_dir TEXT," "wind_speed REAL," @@ -612,11 +654,11 @@ bool checkTables(sqlite3 *db) { "temp3 REAL," "temp4 REAL," "temp5 REAL," - "smile1 TEXT," - "smile2 TEXT," - "smile3 TEXT," - "smile4 TEXT," - "smile5 TEXT," + "smiley1 INTEGER," + "smiley2 INTEGER," + "smiley3 INTEGER," + "smiley4 INTEGER," + "smiley5 INTEGER," "humidity1 INTEGER," "humidity2 INTEGER," "humidity3 INTEGER," @@ -639,12 +681,12 @@ bool checkTables(sqlite3 *db) { "waterTemp5 REAL);" }; - for( i = 0; i < nTablas; i++) { - sprintf(consulta, "SELECT COUNT(*) FROM %s;", tabla[i]); - if(SQLITE_OK != sqlite3_exec(db, consulta, 0, 0, 0)) { - fprintf(stderr,"La tabla %s no existe\n",tabla[i]); + for( i = 0; i < nTables; i++) { + sprintf(request, "SELECT COUNT(*) FROM %s;", tables[i]); + if(SQLITE_OK != sqlite3_exec(db, request, 0, 0, 0)) { + fprintf(stderr,"The table %s no existe\n",tables[i]); if(SQLITE_OK != sqlite3_exec(db, create[i], 0, 0, 0)) { - fprintf(stderr,"Error al crear la tabla %s\n",tabla[i]); + fprintf(stderr,"Error, can't create table : %s\n",tables[i]); return false; } } @@ -656,10 +698,58 @@ void* sqliteLoggerThreadFct(void *args){ while(1){ - sleep(1); - printf("In the thread\n"); - } + sqlite3 *db; + char request[2000]; + char currenttime[20]; + time_t t; + struct tm *tmp; + /* Record every RECORD_HISTORY */ + + sleep(RECORD_HISTORY); + + if ((db = openDb()) == 0){ + exit(-1); + } + checkTablesCreated(db); + + /* Check if the database should be changed */ + + t = time(NULL); + tmp = gmtime(&t); + + strftime(currenttime, sizeof(currenttime), "%Y%m%d%H%M%S", tmp); + + pthread_mutex_lock(¤tcondition_lock); + + sprintf(request, "insert into history (date,pressure,forecast,rain_rate,rain_hour_total,rain_day_total,rain_all_total,wind_dir,wind_speed,wind_avg_speed,uv,temp1,temp2,temp3,temp4,temp5,smiley1,smiley2,smiley3,smiley4,smiley5,humidity1 ,humidity2 ,humidity3 ,humidity4 ,humidity5 ,dewpoint1 ,dewpoint2 ,dewpoint3 ,dewpoint4 ,dewpoint5 ,trend1,trend2,trend3,trend4,trend5,waterTemp1,waterTemp2,waterTemp3,waterTemp4,waterTemp5) values('%s',%d,%d,%d,%.2f,%.2f,%.2f,'%s',%.2f,%.2f,%d,%.2f,%.2f,%.2f,%.2f,%.2f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%.2f,%.2f,%.2f,%.2f,%.2f,'%s','%s','%s','%s','%s',%.2f,%.2f,%.2f,%.2f,%.2f);", + currenttime, currentcondition->pressure, currentcondition->forecast, + currentcondition->rain_rate, currentcondition->rain_hour_total, currentcondition->rain_day_total,currentcondition->rain_all_total, + currentcondition->wind_dir, currentcondition->wind_speed, currentcondition->wind_avg_speed, + currentcondition->uv, + currentcondition->temp[0].temp,currentcondition->temp[1].temp,currentcondition->temp[2].temp,currentcondition->temp[3].temp,currentcondition->temp[4].temp, + currentcondition->temp[0].smile,currentcondition->temp[1].smile,currentcondition->temp[2].smile,currentcondition->temp[3].smile,currentcondition->temp[4].smile, + currentcondition->temp[0].humidity,currentcondition->temp[1].humidity,currentcondition->temp[2].humidity,currentcondition->temp[3].humidity,currentcondition->temp[4].humidity, + currentcondition->temp[0].dewpoint,currentcondition->temp[1].dewpoint,currentcondition->temp[2].dewpoint,currentcondition->temp[3].dewpoint,currentcondition->temp[4].dewpoint, + currentcondition->temp[0].trend,currentcondition->temp[1].trend,currentcondition->temp[2].trend,currentcondition->temp[3].trend,currentcondition->temp[4].trend, + currentcondition->water[0].temp,currentcondition->water[1].temp,currentcondition->water[2].temp,currentcondition->water[3].temp,currentcondition->water[4].temp); + + pthread_mutex_unlock(¤tcondition_lock); + + + if(SQLITE_OK != sqlite3_exec(db, request, 0, 0, 0)) { + fprintf(stderr, "[%s] Can't write to database\n",currenttime); + fprintf(stderr, " Request : %s\n", request ); + fprintf(stderr, " Error : %s\n",sqlite3_errmsg(db)); + } + else { + printf("[%s] Write to database ok\n",currenttime); + } + + /* Close the database */ + + closeDb(db); + } } /**************************** @@ -670,9 +760,7 @@ int main(int argc, char* argv[]){ int ret; int c; - sqlite3 *db; - - pthread_t sqliteLoggerThread; + int i; signal(SIGINT, cleanup); signal(SIGTERM, cleanup); @@ -703,16 +791,44 @@ int main(int argc, char* argv[]){ } } - /* SQLITE INIT */ - ; - if ((db = openDb()) == 0){ - exit(-1); + /* CURRENT CONDITION INITIALISATION */ + + currentcondition = malloc(sizeof(CURRENTCONDITION) + 10 * sizeof(char)); + + currentcondition->pressure = -1; + currentcondition->forecast = -1; + currentcondition->rain_rate = -1; + currentcondition->rain_hour_total = -1.0; + currentcondition->rain_day_total = -1.0; + currentcondition->rain_all_total = -1.0; + currentcondition->wind_dir = ""; + currentcondition->wind_speed = -1.0; + currentcondition->wind_avg_speed = -1.0; + currentcondition->uv = -1; + + for (i=0; i < 5; i++){ + + currentcondition->temp[i].temp = -1.0; + currentcondition->temp[i].smile = -1; + currentcondition->temp[i].humidity = -1; + currentcondition->temp[i].dewpoint = -1.0; + currentcondition->temp[i].trend = ""; + + currentcondition->water[i].temp = -1.0; } - checkTables(db); - closeDb(db); + + /* LOGGER THREAD INIT */ pthread_create(&sqliteLoggerThread, NULL, &sqliteLoggerThreadFct, NULL); - /* pthread_cancel(sqliteLoggerThread); */ + if (pthread_mutex_init(¤tcondition_lock, NULL) != 0) + { + fprintf(stderr,"\nMutex init failed\n"); + return 1; + } + + /* TODO : Move to close function */ + /* pthread_cancel(sqliteLoggerThread); */ + /* pthread_mutex_destroy(&lock); */ /* WMR INIT */ From f76b9b88abb3c4c4cd4f3779959968e68c916753 Mon Sep 17 00:00:00 2001 From: Christophe Meurice Date: Tue, 15 Jan 2013 20:14:51 +0100 Subject: [PATCH 3/6] Adding various tables to db --- wmr100.c | 211 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 147 insertions(+), 64 deletions(-) diff --git a/wmr100.c b/wmr100.c index 085a7d5..39c1e7d 100644 --- a/wmr100.c +++ b/wmr100.c @@ -45,7 +45,7 @@ int gOutput = OUTPUT_BOTH; /* constants */ #define MAXSENSORS 5 -#define RECORD_HISTORY 60 +#define RECORD_HISTORY 3 int const RECV_PACKET_LEN = 8; int const BUF_SIZE = 255; @@ -75,7 +75,7 @@ WMR *wmr = NULL; /* Sqlite loggin */ typedef struct _TEMP { - + bool active; float temp; int smile; int humidity; @@ -84,7 +84,7 @@ typedef struct _TEMP { } TEMP; typedef struct _WATER { - + bool active; float temp; } WATER; @@ -374,6 +374,7 @@ void wmr_handle_temp(WMR *wmr, unsigned char *data, int len){ if ((data[7] >> 4) == 0x8) dewpoint = -dewpoint; pthread_mutex_lock(¤tcondition_lock); + currentcondition->temp[sensor].active = true; currentcondition->temp[sensor].temp = temp; currentcondition->temp[sensor].smile = smiley; currentcondition->temp[sensor].humidity = humidity; @@ -397,7 +398,8 @@ void wmr_handle_water(WMR *wmr, unsigned char *data, int len){ if ((data[4] >> 4) == 0x8) temp = -temp; pthread_mutex_lock(¤tcondition_lock); - currentcondition->water[sensor].temp = temp; + currentcondition->water[sensor].active = true; + currentcondition->water[sensor].temp = temp; pthread_mutex_unlock(¤tcondition_lock); asprintf(&msg, "type=WATER,sensor=%d,temp=%.1f", sensor, temp); @@ -631,60 +633,66 @@ bool checkTablesCreated(sqlite3 *db) { int i; char request[36]; - int nTables = 1; + int nTables = 7; char *tables[] = { - "history" + "history", + "temperature", + "smiley", + "humidity", + "dewpoint", + "trend", + "waterTemp" }; char *create[] = { "CREATE TABLE history(" - "history INTEGER PRIMARY KEY," - "date TEXT," - "pressure INTEGER," - "forecast INTEGER," - "rain_rate INTEGER," - "rain_hour_total REAL," - "rain_day_total REAL," - "rain_all_total REAL," - "wind_dir TEXT," - "wind_speed REAL," - "wind_avg_speed REAL," - "uv INTEGER," - "temp1 REAL," - "temp2 REAL," - "temp3 REAL," - "temp4 REAL," - "temp5 REAL," - "smiley1 INTEGER," - "smiley2 INTEGER," - "smiley3 INTEGER," - "smiley4 INTEGER," - "smiley5 INTEGER," - "humidity1 INTEGER," - "humidity2 INTEGER," - "humidity3 INTEGER," - "humidity4 INTEGER," - "humidity5 INTEGER," - "dewpoint1 REAL," - "dewpoint2 REAL," - "dewpoint3 REAL," - "dewpoint4 REAL," - "dewpoint5 REAL," - "trend1 TEXT," - "trend2 TEXT," - "trend3 TEXT," - "trend4 TEXT," - "trend5 TEXT," - "waterTemp1 REAL," - "waterTemp2 REAL," - "waterTemp3 REAL," - "waterTemp4 REAL," - "waterTemp5 REAL);" + "history INTEGER PRIMARY KEY," + "date TEXT," + "pressure INTEGER," + "forecast INTEGER," + "rain_rate INTEGER," + "rain_hour_total REAL," + "rain_day_total REAL," + "rain_all_total REAL," + "wind_dir TEXT," + "wind_speed REAL," + "wind_avg_speed REAL," + "uv INTEGER);", + "CREATE TABLE temperature(" + "temperature INTEGER PRIMARY KEY," + "date TEXT," + "sensor INTEGER," + "value REAL);", + "CREATE TABLE smiley(" + "smiley INTEGER PRIMARY KEY," + "date TEXT," + "sensor INTEGER," + "value INTEGER);", + "CREATE TABLE humidity(" + "humidity INTEGER PRIMARY KEY," + "date TEXT," + "sensor INTEGER," + "value INTEGER);", + "CREATE TABLE dewpoint(" + "dewpoint INTEGER PRIMARY KEY," + "date TEXT," + "sensor INTEGER," + "value REAL);", + "CREATE TABLE trend(" + "trend INTEGER PRIMARY KEY," + "date TEXT," + "sensor INTEGER," + "value TEXT);", + "CREATE TABLE waterTemp(" + "waterTemp INTEGER PRIMARY KEY," + "date TEXT," + "sensor INTEGER," + "value REAL);" }; for( i = 0; i < nTables; i++) { sprintf(request, "SELECT COUNT(*) FROM %s;", tables[i]); if(SQLITE_OK != sqlite3_exec(db, request, 0, 0, 0)) { - fprintf(stderr,"The table %s no existe\n",tables[i]); + fprintf(stderr,"The table [%s] does not existe, creating it\n",tables[i]); if(SQLITE_OK != sqlite3_exec(db, create[i], 0, 0, 0)) { fprintf(stderr,"Error, can't create table : %s\n",tables[i]); return false; @@ -694,6 +702,18 @@ bool checkTablesCreated(sqlite3 *db) { return true; } +void writeToDb( sqlite3 *db, char *request, char *currenttime){ + + if(SQLITE_OK != sqlite3_exec(db, request, 0, 0, 0)) { + fprintf(stderr, "[%s] Can't write to database\n",currenttime); + fprintf(stderr, " Request : %s\n", request ); + fprintf(stderr, " Error : %s\n",sqlite3_errmsg(db)); + } + else { + printf("[%s] Write to database ok\n",currenttime); + } +} + void* sqliteLoggerThreadFct(void *args){ while(1){ @@ -703,6 +723,9 @@ void* sqliteLoggerThreadFct(void *args){ char currenttime[20]; time_t t; struct tm *tmp; + + int i; + bool active; /* Record every RECORD_HISTORY */ @@ -722,28 +745,86 @@ void* sqliteLoggerThreadFct(void *args){ pthread_mutex_lock(¤tcondition_lock); - sprintf(request, "insert into history (date,pressure,forecast,rain_rate,rain_hour_total,rain_day_total,rain_all_total,wind_dir,wind_speed,wind_avg_speed,uv,temp1,temp2,temp3,temp4,temp5,smiley1,smiley2,smiley3,smiley4,smiley5,humidity1 ,humidity2 ,humidity3 ,humidity4 ,humidity5 ,dewpoint1 ,dewpoint2 ,dewpoint3 ,dewpoint4 ,dewpoint5 ,trend1,trend2,trend3,trend4,trend5,waterTemp1,waterTemp2,waterTemp3,waterTemp4,waterTemp5) values('%s',%d,%d,%d,%.2f,%.2f,%.2f,'%s',%.2f,%.2f,%d,%.2f,%.2f,%.2f,%.2f,%.2f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%.2f,%.2f,%.2f,%.2f,%.2f,'%s','%s','%s','%s','%s',%.2f,%.2f,%.2f,%.2f,%.2f);", + sprintf(request, "insert into history (date,pressure,forecast,rain_rate,rain_hour_total,rain_day_total,rain_all_total,wind_dir,wind_speed,wind_avg_speed,uv) values('%s',%d,%d,%d,%.2f,%.2f,%.2f,'%s',%.2f,%.2f,%d);", currenttime, currentcondition->pressure, currentcondition->forecast, currentcondition->rain_rate, currentcondition->rain_hour_total, currentcondition->rain_day_total,currentcondition->rain_all_total, currentcondition->wind_dir, currentcondition->wind_speed, currentcondition->wind_avg_speed, - currentcondition->uv, - currentcondition->temp[0].temp,currentcondition->temp[1].temp,currentcondition->temp[2].temp,currentcondition->temp[3].temp,currentcondition->temp[4].temp, - currentcondition->temp[0].smile,currentcondition->temp[1].smile,currentcondition->temp[2].smile,currentcondition->temp[3].smile,currentcondition->temp[4].smile, - currentcondition->temp[0].humidity,currentcondition->temp[1].humidity,currentcondition->temp[2].humidity,currentcondition->temp[3].humidity,currentcondition->temp[4].humidity, - currentcondition->temp[0].dewpoint,currentcondition->temp[1].dewpoint,currentcondition->temp[2].dewpoint,currentcondition->temp[3].dewpoint,currentcondition->temp[4].dewpoint, - currentcondition->temp[0].trend,currentcondition->temp[1].trend,currentcondition->temp[2].trend,currentcondition->temp[3].trend,currentcondition->temp[4].trend, - currentcondition->water[0].temp,currentcondition->water[1].temp,currentcondition->water[2].temp,currentcondition->water[3].temp,currentcondition->water[4].temp); + currentcondition->uv); pthread_mutex_unlock(¤tcondition_lock); + writeToDb(db,request,currenttime); - if(SQLITE_OK != sqlite3_exec(db, request, 0, 0, 0)) { - fprintf(stderr, "[%s] Can't write to database\n",currenttime); - fprintf(stderr, " Request : %s\n", request ); - fprintf(stderr, " Error : %s\n",sqlite3_errmsg(db)); + /* Processing temp / humidity sensor */ + + for (i=0; i < MAXSENSORS; i++){ + + pthread_mutex_lock(¤tcondition_lock); + active = currentcondition->temp[i].active; + pthread_mutex_unlock(¤tcondition_lock); + + if (active){ + + /* Temperature */ + pthread_mutex_lock(¤tcondition_lock); + sprintf(request, "insert into temperature (date,sensor,value) values('%s',%d,%.2f);", + currenttime, i, currentcondition->temp[i].temp); + pthread_mutex_unlock(¤tcondition_lock); + + writeToDb(db,request,currenttime); + + /* Smiley */ + pthread_mutex_lock(¤tcondition_lock); + sprintf(request, "insert into smiley (date,sensor,value) values('%s',%d,%d);", + currenttime, i, currentcondition->temp[i].smile); + pthread_mutex_unlock(¤tcondition_lock); + + writeToDb(db,request,currenttime); + + /* Humidity */ + pthread_mutex_lock(¤tcondition_lock); + sprintf(request, "insert into humidity (date,sensor,value) values('%s',%d,%d);", + currenttime, i, currentcondition->temp[i].humidity); + pthread_mutex_unlock(¤tcondition_lock); + + writeToDb(db,request,currenttime); + + /* Dewpoint */ + pthread_mutex_lock(¤tcondition_lock); + sprintf(request, "insert into dewpoint (date,sensor,value) values('%s',%d,%.2f);", + currenttime, i, currentcondition->temp[i].dewpoint); + pthread_mutex_unlock(¤tcondition_lock); + + writeToDb(db,request,currenttime); + + /* Trend */ + pthread_mutex_lock(¤tcondition_lock); + sprintf(request, "insert into trend (date,sensor,value) values('%s',%d,%s);", + currenttime, i, currentcondition->temp[i].trend); + pthread_mutex_unlock(¤tcondition_lock); + + writeToDb(db,request,currenttime); + } } - else { - printf("[%s] Write to database ok\n",currenttime); + + /* Processing water sensor */ + + for (i=0; i < MAXSENSORS; i++){ + + pthread_mutex_lock(¤tcondition_lock); + active = currentcondition->temp[i].active; + pthread_mutex_unlock(¤tcondition_lock); + + if (active){ + + /* Water Temp */ + pthread_mutex_lock(¤tcondition_lock); + sprintf(request, "insert into waterTemp (date,sensor,value) values('%s',%d,%.2f);", + currenttime, i, currentcondition->water[i].temp); + pthread_mutex_unlock(¤tcondition_lock); + + writeToDb(db,request,currenttime); + } } /* Close the database */ @@ -806,14 +887,16 @@ int main(int argc, char* argv[]){ currentcondition->wind_avg_speed = -1.0; currentcondition->uv = -1; - for (i=0; i < 5; i++){ + for (i=0; i < MAXSENSORS; i++){ + currentcondition->temp[i].active = false; currentcondition->temp[i].temp = -1.0; currentcondition->temp[i].smile = -1; currentcondition->temp[i].humidity = -1; currentcondition->temp[i].dewpoint = -1.0; currentcondition->temp[i].trend = ""; + currentcondition->water[i].active = false; currentcondition->water[i].temp = -1.0; } From 497956de32f154785aea3a93ede6034b2acef5a0 Mon Sep 17 00:00:00 2001 From: Christophe Meurice Date: Tue, 15 Jan 2013 20:20:28 +0100 Subject: [PATCH 4/6] Minor corrections --- wmr100.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wmr100.c b/wmr100.c index 39c1e7d..2a4fda9 100644 --- a/wmr100.c +++ b/wmr100.c @@ -45,7 +45,7 @@ int gOutput = OUTPUT_BOTH; /* constants */ #define MAXSENSORS 5 -#define RECORD_HISTORY 3 +#define RECORD_HISTORY 60 int const RECV_PACKET_LEN = 8; int const BUF_SIZE = 255; @@ -799,7 +799,7 @@ void* sqliteLoggerThreadFct(void *args){ /* Trend */ pthread_mutex_lock(¤tcondition_lock); - sprintf(request, "insert into trend (date,sensor,value) values('%s',%d,%s);", + sprintf(request, "insert into trend (date,sensor,value) values('%s',%d,'%s');", currenttime, i, currentcondition->temp[i].trend); pthread_mutex_unlock(¤tcondition_lock); @@ -822,7 +822,7 @@ void* sqliteLoggerThreadFct(void *args){ sprintf(request, "insert into waterTemp (date,sensor,value) values('%s',%d,%.2f);", currenttime, i, currentcondition->water[i].temp); pthread_mutex_unlock(¤tcondition_lock); - + writeToDb(db,request,currenttime); } } From 9b62062d0c5d19d74ade7f2f204da70d864642e2 Mon Sep 17 00:00:00 2001 From: Christophe Meurice Date: Tue, 15 Jan 2013 20:25:27 +0100 Subject: [PATCH 5/6] Minor corrections --- wmr100.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wmr100.c b/wmr100.c index 2a4fda9..8248294 100644 --- a/wmr100.c +++ b/wmr100.c @@ -812,7 +812,7 @@ void* sqliteLoggerThreadFct(void *args){ for (i=0; i < MAXSENSORS; i++){ pthread_mutex_lock(¤tcondition_lock); - active = currentcondition->temp[i].active; + active = currentcondition->water[i].active; pthread_mutex_unlock(¤tcondition_lock); if (active){ From a31cd389d1a5711ab79ca95813568a74375e3c13 Mon Sep 17 00:00:00 2001 From: Christophe Meurice Date: Wed, 16 Jan 2013 13:25:38 +0100 Subject: [PATCH 6/6] Adding command line options --- Makefile | 2 +- wmr100.c | 112 ++++++++++++++++++++++++++++++------------------------- 2 files changed, 63 insertions(+), 51 deletions(-) diff --git a/Makefile b/Makefile index cb8a236..28e1c03 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS += `pkg-config libhid --cflags` -pedantic -Wall -D_GNU_SOURCE -Wno-overlength-strings +CFLAGS += `pkg-config libhid --cflags` -pedantic -Wall -D_GNU_SOURCE LIBS += `pkg-config libhid --libs` -lsqlite3 -lpthread wmr100: wmr100.c diff --git a/wmr100.c b/wmr100.c index 8248294..2e7929c 100644 --- a/wmr100.c +++ b/wmr100.c @@ -34,13 +34,11 @@ Variables definition ****************************/ -/* globals */ +/* Loggin output */ -#define OUTPUT_BOTH 3 -#define OUTPUT_FILE 2 -#define OUTPUT_STDOUT 1 - -int gOutput = OUTPUT_BOTH; +bool gOutputStdout = false; +bool gOutputFile = false; +bool gOutputSqlite = false; /* constants */ @@ -307,12 +305,12 @@ void wmr_log_data(WMR *wmr, char *msg) { } } - if (gOutput & OUTPUT_FILE) + if (gOutputFile) { fprintf(out, "DATA[%s]:%s\n", outstr, msg); fflush(out); } - if (gOutput & OUTPUT_STDOUT) + if (gOutputStdout) printf("DATA[%s]:%s\n", outstr, msg); } @@ -487,7 +485,7 @@ void wmr_handle_clock(WMR *wmr, unsigned char *data, int len){ ****************************/ void wmr_handle_packet(WMR *wmr, unsigned char *data, int len){ - if (gOutput & OUTPUT_STDOUT) + if (gOutputStdout) dump_packet(data, len); switch(data[1]) { @@ -597,9 +595,11 @@ void cleanup(int sig_num){ wmr = NULL; } - pthread_cancel(sqliteLoggerThread); - pthread_mutex_destroy(¤tcondition_lock); - + if (gOutputSqlite) { + pthread_cancel(sqliteLoggerThread); + pthread_mutex_destroy(¤tcondition_lock); + } + exit(0); } @@ -626,6 +626,7 @@ sqlite3* openDb(){ } void closeDb(sqlite3 *db){ + sqlite3_close(db); } @@ -847,21 +848,21 @@ int main(int argc, char* argv[]){ signal(SIGTERM, cleanup); /* Parse the command line parameters */ - while ((c = getopt(argc, argv, "hsfb")) != -1) + while ((c = getopt(argc, argv, "hsfd")) != -1) { switch (c) { case 'h': - fprintf(stderr, "Options:\n\t-s: output to sdtout only\n\t-f: output to file only\n\t-b: output to both [default]\n"); + fprintf(stderr, "Options:\n\t-s: output to sdtout only\n\t-f: output to file only\n\t-b: output to both [default]\n\t-d: output to sqlite\n"); return 1; case 's': - gOutput = OUTPUT_STDOUT; + gOutputStdout = true; break; case 'f': - gOutput = OUTPUT_FILE; + gOutputFile = true; break; - case 'b': - gOutput = OUTPUT_BOTH; + case 'd': + gOutputSqlite = true; break; case '?': if (isprint(optopt)) @@ -872,41 +873,52 @@ int main(int argc, char* argv[]){ } } - /* CURRENT CONDITION INITIALISATION */ + fprintf(stderr, "I will writte datas to : \n"); + if ( gOutputStdout ) + fprintf(stderr, " - Stdout\n"); + if ( gOutputFile ) + fprintf(stderr, " - File\n"); + if ( gOutputSqlite ) + fprintf(stderr, " - Sqlite database\n"); + + if (gOutputSqlite) { - currentcondition = malloc(sizeof(CURRENTCONDITION) + 10 * sizeof(char)); + /* CURRENT CONDITION INITIALISATION */ - currentcondition->pressure = -1; - currentcondition->forecast = -1; - currentcondition->rain_rate = -1; - currentcondition->rain_hour_total = -1.0; - currentcondition->rain_day_total = -1.0; - currentcondition->rain_all_total = -1.0; - currentcondition->wind_dir = ""; - currentcondition->wind_speed = -1.0; - currentcondition->wind_avg_speed = -1.0; - currentcondition->uv = -1; + currentcondition = malloc(sizeof(CURRENTCONDITION) + 10 * sizeof(char)); + + currentcondition->pressure = -1; + currentcondition->forecast = -1; + currentcondition->rain_rate = -1; + currentcondition->rain_hour_total = -1.0; + currentcondition->rain_day_total = -1.0; + currentcondition->rain_all_total = -1.0; + currentcondition->wind_dir = ""; + currentcondition->wind_speed = -1.0; + currentcondition->wind_avg_speed = -1.0; + currentcondition->uv = -1; + + for (i=0; i < MAXSENSORS; i++){ - for (i=0; i < MAXSENSORS; i++){ - - currentcondition->temp[i].active = false; - currentcondition->temp[i].temp = -1.0; - currentcondition->temp[i].smile = -1; - currentcondition->temp[i].humidity = -1; - currentcondition->temp[i].dewpoint = -1.0; - currentcondition->temp[i].trend = ""; - - currentcondition->water[i].active = false; - currentcondition->water[i].temp = -1.0; - } - - /* LOGGER THREAD INIT */ - - pthread_create(&sqliteLoggerThread, NULL, &sqliteLoggerThreadFct, NULL); - if (pthread_mutex_init(¤tcondition_lock, NULL) != 0) - { - fprintf(stderr,"\nMutex init failed\n"); - return 1; + currentcondition->temp[i].active = false; + currentcondition->temp[i].temp = -1.0; + currentcondition->temp[i].smile = -1; + currentcondition->temp[i].humidity = -1; + currentcondition->temp[i].dewpoint = -1.0; + currentcondition->temp[i].trend = ""; + + currentcondition->water[i].active = false; + currentcondition->water[i].temp = -1.0; + } + + /* LOGGER THREAD INIT */ + + pthread_create(&sqliteLoggerThread, NULL, &sqliteLoggerThreadFct, NULL); + if (pthread_mutex_init(¤tcondition_lock, NULL) != 0) + { + fprintf(stderr,"\nMutex init failed\n"); + return 1; + } } /* TODO : Move to close function */