2014-07-11 22:12:11 +02:00
|
|
|
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
2016-03-03 20:46:20 +01:00
|
|
|
#include "cw/log.h"
|
|
|
|
#include "cw/dbg.h"
|
|
|
|
#include "cw/capwap_items.h"
|
|
|
|
#include "cw/conn.h"
|
|
|
|
#include "cw/item.h"
|
2014-07-13 19:18:56 +02:00
|
|
|
|
2014-07-15 07:33:17 +02:00
|
|
|
#include "conf.h"
|
|
|
|
|
2014-07-13 19:18:56 +02:00
|
|
|
static sqlite3 *handle;
|
|
|
|
|
2014-07-14 22:44:46 +02:00
|
|
|
|
|
|
|
const char * init_tables = "\
|
2014-07-15 07:33:17 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS acs (acid TEXT PRIMARY KEY, acname TEXT, lastseen TIMESTAMP); \
|
2014-07-14 22:44:46 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS acips (acid TEXT,ip TEXT); \
|
2015-04-29 12:23:12 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS wtps (wtpid TEXT PRIMARY KEY, wtp_name TEXT,lastseen TIMESTAMP); \
|
|
|
|
CREATE TABLE IF NOT EXISTS wtpprops (\
|
2015-05-02 02:02:06 +02:00
|
|
|
wtpid TEXT NOT NULL,\
|
|
|
|
id TEXT NOT NULL,\
|
|
|
|
sub_id TEXT NOT NULL,\
|
2015-04-29 12:23:12 +02:00
|
|
|
val TEXT,\
|
|
|
|
upd INTEGER,\
|
2015-05-02 02:02:06 +02:00
|
|
|
PRIMARY KEY(wtpid,id,sub_id)\
|
2015-04-29 12:23:12 +02:00
|
|
|
);\
|
2015-05-16 17:10:42 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS wlans (wlanid INTEGER PRIMARY KEY);\
|
|
|
|
CREATE TABLE IF NOT EXISTS wlanprops (wlanid INTEGER, id TEXT NOT NULL, val TEXT, PRIMARY KEY(wlanid,id));\
|
2014-07-14 22:44:46 +02:00
|
|
|
";
|
2015-05-16 17:10:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-14 22:44:46 +02:00
|
|
|
|
2014-07-13 19:18:56 +02:00
|
|
|
int db_init()
|
|
|
|
{
|
2015-05-02 02:02:06 +02:00
|
|
|
|
|
|
|
int rc;
|
2014-07-13 19:18:56 +02:00
|
|
|
const char * filename="ac.sqlite3";
|
2015-04-30 14:10:59 +02:00
|
|
|
cw_dbg(DBG_INFO,"Initializing Sqlite3 DB: %s, SQLite3 Version %s",filename,SQLITE_VERSION);
|
2015-05-02 02:02:06 +02:00
|
|
|
|
|
|
|
rc = sqlite3_config(SQLITE_CONFIG_SERIALIZED);
|
|
|
|
if (rc!=SQLITE_OK){
|
|
|
|
cw_log(LOG_ERR,"Error configuring SQLite3: %s",sqlite3_errmsg(handle));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = sqlite3_initialize();
|
|
|
|
if (rc!=SQLITE_OK){
|
|
|
|
cw_log(LOG_ERR,"Error initializing SQLite3 DB : %s",sqlite3_errmsg(handle));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rc = sqlite3_open(filename,&handle);
|
2015-04-29 12:23:12 +02:00
|
|
|
if (rc != SQLITE_OK)
|
2014-07-13 19:18:56 +02:00
|
|
|
{
|
2015-04-29 12:23:12 +02:00
|
|
|
cw_log(LOG_ERR,"Error opening SQLite3 DB %s: %s",filename,sqlite3_errmsg(handle));
|
2014-07-13 19:18:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-14 22:44:46 +02:00
|
|
|
const char * cmd = init_tables;
|
2014-07-13 19:18:56 +02:00
|
|
|
rc = sqlite3_exec(handle,cmd,0,0,0);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
const char *em = sqlite3_errmsg(handle);
|
2015-04-29 12:23:12 +02:00
|
|
|
cw_log(LOG_ERR,"Error executing SQL \"%s\"\nSQL Error Message: %s",cmd, em);
|
2014-07-13 19:18:56 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-15 07:33:17 +02:00
|
|
|
|
2015-04-29 19:25:09 +02:00
|
|
|
static sqlite3_stmt * ping_stmt;
|
2015-04-29 12:23:12 +02:00
|
|
|
static sqlite3_stmt * put_wtp_prop_stmt;
|
2015-04-30 14:10:59 +02:00
|
|
|
static sqlite3_stmt * get_tasks_stmt;
|
2015-04-29 19:25:09 +02:00
|
|
|
|
2015-04-29 12:23:12 +02:00
|
|
|
|
2014-07-15 07:33:17 +02:00
|
|
|
|
2014-07-15 00:34:41 +02:00
|
|
|
int db_start()
|
|
|
|
{
|
2015-04-29 12:23:12 +02:00
|
|
|
cw_dbg(DBG_INFO,"Starting Sqlite3 DB");
|
|
|
|
|
|
|
|
const char *sql="";
|
2014-07-15 17:11:16 +02:00
|
|
|
|
2014-07-15 07:33:17 +02:00
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
int rc = sqlite3_prepare_v2(handle, "INSERT INTO acs (acid,acname) VALUES (?,?);",-1,&stmt,0);
|
2014-07-15 17:11:16 +02:00
|
|
|
if (rc)
|
|
|
|
goto errX;
|
2014-07-15 07:33:17 +02:00
|
|
|
|
|
|
|
rc = sqlite3_bind_text(stmt,1,conf_acid,-1,SQLITE_STATIC);
|
|
|
|
|
2014-07-17 22:58:58 +02:00
|
|
|
rc = sqlite3_bind_text(stmt,2,conf_acname,-1,SQLITE_STATIC);
|
2014-07-15 07:33:17 +02:00
|
|
|
|
|
|
|
sqlite3_step(stmt);
|
|
|
|
|
|
|
|
rc = sqlite3_prepare_v2(handle, "UPDATE acs SET lastseen=datetime('now') WHERE acid=?;",-1,&ping_stmt,0);
|
|
|
|
rc = sqlite3_bind_text(ping_stmt,1,conf_acid,-1,SQLITE_STATIC);
|
|
|
|
|
2015-04-29 12:23:12 +02:00
|
|
|
|
2015-04-30 14:10:59 +02:00
|
|
|
/* Prepare statement to update a WTP property */
|
2015-04-29 12:23:12 +02:00
|
|
|
sql = "INSERT OR REPLACE INTO wtpprops\
|
2015-05-02 02:02:06 +02:00
|
|
|
(wtpid,id,sub_id,val,upd)\
|
2015-04-29 12:23:12 +02:00
|
|
|
VALUES (?,?,?,?,?)";
|
|
|
|
|
|
|
|
rc = sqlite3_prepare_v2(handle, sql,-1, &put_wtp_prop_stmt,0);
|
|
|
|
if (rc)
|
|
|
|
goto errX;
|
|
|
|
|
|
|
|
|
2015-04-30 14:10:59 +02:00
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
sql = "SELECT wtpid,id,sub_id,val FROM wtpprops WHERE upd>0 AND wtpid=?";
|
2015-04-30 14:10:59 +02:00
|
|
|
rc = sqlite3_prepare_v2(handle, sql,-1, &get_tasks_stmt,0);
|
|
|
|
if (rc)
|
|
|
|
goto errX;
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-29 12:23:12 +02:00
|
|
|
|
2014-07-15 17:15:42 +02:00
|
|
|
return 1;
|
2014-07-15 07:33:17 +02:00
|
|
|
|
2014-07-15 17:11:16 +02:00
|
|
|
errX:
|
2015-04-29 12:23:12 +02:00
|
|
|
cw_log(LOG_ERR,"Fatal: Can't start Sqlite3 DB, Error while executing '%s' - %d - %s",sql,rc,sqlite3_errmsg(handle));
|
2014-07-15 17:11:16 +02:00
|
|
|
return 0;
|
2014-07-14 22:44:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-15 17:11:16 +02:00
|
|
|
void db_ping()
|
2014-07-11 22:12:11 +02:00
|
|
|
{
|
2014-07-15 17:11:16 +02:00
|
|
|
int rc = sqlite3_step(ping_stmt);
|
2014-07-17 22:58:58 +02:00
|
|
|
if (rc!=SQLITE_DONE){
|
2015-04-29 19:25:09 +02:00
|
|
|
cw_log(LOG_ERR,"Error: Can't ping database, error code %d - %s",rc,sqlite3_errmsg(handle));
|
2014-07-11 22:12:11 +02:00
|
|
|
}
|
2014-07-15 17:11:16 +02:00
|
|
|
}
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
void db_put_wtp_prop(const char *wtp_id,const char * id,const char *sub_id,const char * val)
|
2015-04-29 12:23:12 +02:00
|
|
|
{
|
|
|
|
int rc;
|
2015-04-29 19:25:09 +02:00
|
|
|
|
2015-05-02 10:45:16 +02:00
|
|
|
//DBGX("Putting %s/%s:%s",id,sub_id,val);
|
2015-05-01 12:50:28 +02:00
|
|
|
|
2015-04-29 19:25:09 +02:00
|
|
|
sqlite3_reset(put_wtp_prop_stmt);
|
|
|
|
sqlite3_clear_bindings(put_wtp_prop_stmt);
|
|
|
|
|
|
|
|
if(sqlite3_bind_text(put_wtp_prop_stmt,1,wtp_id,-1,SQLITE_STATIC))
|
|
|
|
goto errX;
|
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
if(sqlite3_bind_text(put_wtp_prop_stmt,2,id,-1,SQLITE_STATIC))
|
2015-04-29 19:25:09 +02:00
|
|
|
goto errX;
|
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
if (!sub_id)
|
|
|
|
sub_id=CW_ITEM_NONE;
|
|
|
|
|
|
|
|
|
|
|
|
if (sqlite3_bind_text(put_wtp_prop_stmt,3,sub_id,-1,SQLITE_STATIC))
|
2015-04-29 19:25:09 +02:00
|
|
|
goto errX;
|
|
|
|
|
|
|
|
if (sqlite3_bind_text(put_wtp_prop_stmt,4,val,-1,SQLITE_STATIC))
|
|
|
|
goto errX;
|
|
|
|
|
|
|
|
if (sqlite3_bind_int(put_wtp_prop_stmt,5,0))
|
|
|
|
goto errX;
|
|
|
|
|
|
|
|
|
|
|
|
if (sqlite3_step(put_wtp_prop_stmt))
|
|
|
|
goto errX;
|
|
|
|
return;
|
|
|
|
errX:
|
|
|
|
if (rc) {
|
|
|
|
cw_log(LOG_ERR,"Can't update database with WTP props: %d - %s",
|
|
|
|
rc,sqlite3_errmsg(handle));
|
|
|
|
}
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-04-29 12:23:12 +02:00
|
|
|
}
|
2014-07-11 22:12:11 +02:00
|
|
|
|
2015-04-29 19:25:09 +02:00
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
mavl_conststr_t db_get_update_tasks(struct conn * conn,const char * wtpid)
|
2015-04-29 19:25:09 +02:00
|
|
|
{
|
2015-04-30 14:10:59 +02:00
|
|
|
|
2015-05-01 00:16:54 +02:00
|
|
|
sqlite3_reset(get_tasks_stmt);
|
|
|
|
sqlite3_clear_bindings(get_tasks_stmt);
|
2015-04-29 19:25:09 +02:00
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
mavl_conststr_t r = mavl_create_conststr();
|
|
|
|
if (!r)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-30 14:10:59 +02:00
|
|
|
if(sqlite3_bind_text(get_tasks_stmt,1,wtpid,-1,SQLITE_STATIC))
|
|
|
|
goto errX;
|
|
|
|
|
|
|
|
int rc;
|
|
|
|
|
2015-05-01 12:50:28 +02:00
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
|
2015-05-01 12:50:28 +02:00
|
|
|
//rc = sqlite3_step(get_tasks_stmt);
|
|
|
|
while (SQLITE_ROW == sqlite3_step(get_tasks_stmt)) {
|
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
int ii;
|
2015-05-02 10:45:16 +02:00
|
|
|
//DBGX("-----------------------------------------------------","");
|
2015-05-02 02:02:06 +02:00
|
|
|
for (ii=0; ii<5; ii++){
|
|
|
|
|
2015-05-02 10:45:16 +02:00
|
|
|
//DBGX("CVALL: %s",(const char*)sqlite3_column_text(get_tasks_stmt,ii));
|
2015-05-02 02:02:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *id = (const char*)sqlite3_column_text(get_tasks_stmt,1);
|
|
|
|
if (!id) {
|
2015-05-02 10:45:16 +02:00
|
|
|
//DBGX("::::::::::::::::::::::::::::::::::::::::::::::::::::NULL ID!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!","");
|
2015-05-02 02:02:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *sub_id = (const char*)sqlite3_column_text(get_tasks_stmt,2);
|
|
|
|
|
2015-05-01 12:50:28 +02:00
|
|
|
const char *val = (const char*)sqlite3_column_text(get_tasks_stmt,3);
|
|
|
|
|
2015-05-02 10:45:16 +02:00
|
|
|
//DBGX("ID: (%s), SubID (%s), Val (%s)",id,sub_id,val);
|
2015-05-01 12:50:28 +02:00
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
const struct cw_itemdef * cwi = cw_itemdef_get(conn->actions->items,id,sub_id);
|
2015-05-01 12:50:28 +02:00
|
|
|
if (!cwi) {
|
2015-05-02 10:45:16 +02:00
|
|
|
//DBGX("Not item definition found for: %s/%s",id,sub_id);
|
2015-05-02 02:02:06 +02:00
|
|
|
continue;
|
2015-05-01 12:50:28 +02:00
|
|
|
}
|
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
uint8_t data[2048];
|
2015-05-01 12:50:28 +02:00
|
|
|
|
|
|
|
if (!cwi->type->from_str) {
|
2015-05-02 02:02:06 +02:00
|
|
|
cw_log(LOG_ERR,"Can't convert from string %s/%s - No method defined.",id,sub_id);
|
|
|
|
continue;
|
2015-05-01 12:50:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mbag_item_t * i = cwi->type->from_str(val);
|
2015-05-02 02:02:06 +02:00
|
|
|
i->id=cwi->id;
|
2015-05-01 12:50:28 +02:00
|
|
|
|
|
|
|
mbag_set(conn->outgoing,i);
|
2015-05-02 02:02:06 +02:00
|
|
|
|
|
|
|
mavl_add(r,(void*)cwi->id);
|
2015-04-30 14:10:59 +02:00
|
|
|
}
|
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
if (r->count)
|
|
|
|
return r;
|
2015-05-01 12:50:28 +02:00
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
|
|
|
|
mavl_destroy(r);
|
|
|
|
return NULL;
|
2015-05-01 12:50:28 +02:00
|
|
|
|
2015-04-30 14:10:59 +02:00
|
|
|
|
|
|
|
errX:
|
|
|
|
if (rc) {
|
2015-05-01 00:16:54 +02:00
|
|
|
cw_log(LOG_ERR,"Can't get tasks: %d - %s",
|
2015-04-30 14:10:59 +02:00
|
|
|
rc,sqlite3_errmsg(handle));
|
|
|
|
}
|
|
|
|
|
2015-05-02 02:02:06 +02:00
|
|
|
if (r)
|
|
|
|
mavl_destroy(r);
|
|
|
|
|
|
|
|
|
|
|
|
return NULL;
|
2015-04-29 19:25:09 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-01 12:50:28 +02:00
|
|
|
|
|
|
|
|