Simple SQLite3 interface is available.
FossilOrigin-Name: 5bbd9c9c08c71292176295c13c8a5021e9ea7476998d4312f6bc900ae773a412
This commit is contained in:
parent
2dbe69d18c
commit
718b3c75b1
115
src/ac/db.c
115
src/ac/db.c
@ -5,6 +5,7 @@
|
||||
#include "capwap/dbg.h"
|
||||
#include "capwap/capwap_items.h"
|
||||
#include "capwap/conn.h"
|
||||
#include "capwap/item.h"
|
||||
|
||||
#include "conf.h"
|
||||
|
||||
@ -16,21 +17,37 @@ const char * init_tables = "\
|
||||
CREATE TABLE IF NOT EXISTS acips (acid TEXT,ip TEXT); \
|
||||
CREATE TABLE IF NOT EXISTS wtps (wtpid TEXT PRIMARY KEY, wtp_name TEXT,lastseen TIMESTAMP); \
|
||||
CREATE TABLE IF NOT EXISTS wtpprops (\
|
||||
wtpid TEXT,\
|
||||
rid INTEGER,\
|
||||
prop TEXT,\
|
||||
wtpid TEXT NOT NULL,\
|
||||
id TEXT NOT NULL,\
|
||||
sub_id TEXT NOT NULL,\
|
||||
val TEXT,\
|
||||
upd INTEGER,\
|
||||
PRIMARY KEY(wtpid,rid,prop)\
|
||||
PRIMARY KEY(wtpid,id,sub_id)\
|
||||
);\
|
||||
";
|
||||
|
||||
int db_init()
|
||||
{
|
||||
const char * filename="ac.sqlite3";
|
||||
|
||||
int rc;
|
||||
const char * filename="ac.sqlite3";
|
||||
cw_dbg(DBG_INFO,"Initializing Sqlite3 DB: %s, SQLite3 Version %s",filename,SQLITE_VERSION);
|
||||
int rc = sqlite3_open(filename,&handle);
|
||||
|
||||
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);
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
cw_log(LOG_ERR,"Error opening SQLite3 DB %s: %s",filename,sqlite3_errmsg(handle));
|
||||
@ -80,7 +97,7 @@ int db_start()
|
||||
|
||||
/* Prepare statement to update a WTP property */
|
||||
sql = "INSERT OR REPLACE INTO wtpprops\
|
||||
(wtpid,rid,prop,val,upd)\
|
||||
(wtpid,id,sub_id,val,upd)\
|
||||
VALUES (?,?,?,?,?)";
|
||||
|
||||
rc = sqlite3_prepare_v2(handle, sql,-1, &put_wtp_prop_stmt,0);
|
||||
@ -89,7 +106,7 @@ int db_start()
|
||||
|
||||
|
||||
|
||||
sql = "SELECT * FROM wtpprops WHERE upd>0 AND wtpid=?";
|
||||
sql = "SELECT wtpid,id,sub_id,val FROM wtpprops WHERE upd>0 AND wtpid=?";
|
||||
rc = sqlite3_prepare_v2(handle, sql,-1, &get_tasks_stmt,0);
|
||||
if (rc)
|
||||
goto errX;
|
||||
@ -114,11 +131,11 @@ void db_ping()
|
||||
}
|
||||
}
|
||||
|
||||
void db_put_wtp_prop(const char *wtp_id,int rid, const char * prop,const char * val)
|
||||
void db_put_wtp_prop(const char *wtp_id,const char * id,const char *sub_id,const char * val)
|
||||
{
|
||||
int rc;
|
||||
|
||||
DBGX("Putting %s:%s",prop,val);
|
||||
DBGX("Putting %s/%s:%s",id,sub_id,val);
|
||||
|
||||
sqlite3_reset(put_wtp_prop_stmt);
|
||||
sqlite3_clear_bindings(put_wtp_prop_stmt);
|
||||
@ -126,10 +143,14 @@ DBGX("Putting %s:%s",prop,val);
|
||||
if(sqlite3_bind_text(put_wtp_prop_stmt,1,wtp_id,-1,SQLITE_STATIC))
|
||||
goto errX;
|
||||
|
||||
if(sqlite3_bind_int(put_wtp_prop_stmt,2,rid))
|
||||
if(sqlite3_bind_text(put_wtp_prop_stmt,2,id,-1,SQLITE_STATIC))
|
||||
goto errX;
|
||||
|
||||
if (sqlite3_bind_text(put_wtp_prop_stmt,3,prop,-1,SQLITE_STATIC))
|
||||
if (!sub_id)
|
||||
sub_id=CW_ITEM_NONE;
|
||||
|
||||
|
||||
if (sqlite3_bind_text(put_wtp_prop_stmt,3,sub_id,-1,SQLITE_STATIC))
|
||||
goto errX;
|
||||
|
||||
if (sqlite3_bind_text(put_wtp_prop_stmt,4,val,-1,SQLITE_STATIC))
|
||||
@ -151,63 +172,79 @@ errX:
|
||||
}
|
||||
|
||||
|
||||
int db_get_tasks(struct conn * conn,const char * wtpid)
|
||||
mavl_conststr_t db_get_update_tasks(struct conn * conn,const char * wtpid)
|
||||
{
|
||||
|
||||
sqlite3_reset(get_tasks_stmt);
|
||||
sqlite3_clear_bindings(get_tasks_stmt);
|
||||
|
||||
mavl_conststr_t r = mavl_create_conststr();
|
||||
if (!r)
|
||||
return NULL;
|
||||
|
||||
|
||||
|
||||
|
||||
if(sqlite3_bind_text(get_tasks_stmt,1,wtpid,-1,SQLITE_STATIC))
|
||||
goto errX;
|
||||
|
||||
int rc;
|
||||
|
||||
|
||||
|
||||
//rc = sqlite3_step(get_tasks_stmt);
|
||||
while (SQLITE_ROW == sqlite3_step(get_tasks_stmt)) {
|
||||
|
||||
const char *prop = (const char*)sqlite3_column_text(get_tasks_stmt,2);
|
||||
int ii;
|
||||
DBGX("-----------------------------------------------------","");
|
||||
for (ii=0; ii<5; ii++){
|
||||
|
||||
DBGX("CVALL: %s",(const char*)sqlite3_column_text(get_tasks_stmt,ii));
|
||||
|
||||
|
||||
}
|
||||
|
||||
const char *id = (const char*)sqlite3_column_text(get_tasks_stmt,1);
|
||||
if (!id) {
|
||||
DBGX("::::::::::::::::::::::::::::::::::::::::::::::::::::NULL ID!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!","");
|
||||
continue;
|
||||
}
|
||||
|
||||
const char *sub_id = (const char*)sqlite3_column_text(get_tasks_stmt,2);
|
||||
|
||||
const char *val = (const char*)sqlite3_column_text(get_tasks_stmt,3);
|
||||
|
||||
DBGX("Prop: %s Val: %s",prop,val);
|
||||
DBGX("ID: (%s), SubID (%s), Val (%s)",id,sub_id,val);
|
||||
|
||||
struct cw_itemdef * cwi = cw_item_get_by_name(prop,capwap_itemdefs);
|
||||
const struct cw_itemdef * cwi = cw_itemdef_get(conn->actions->items,id,sub_id);
|
||||
if (!cwi) {
|
||||
DBGX("Not found: %s",prop);
|
||||
return 0;
|
||||
DBGX("Not item definition found for: %s/%s",id,sub_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
uint8_t data[1024];
|
||||
printf("Type: %s\n",cwi->type->name);
|
||||
uint8_t data[2048];
|
||||
|
||||
if (!cwi->type->from_str) {
|
||||
cw_log(LOG_ERR,"Can't convert from SQL %s",prop);
|
||||
return 1;
|
||||
cw_log(LOG_ERR,"Can't convert from string %s/%s - No method defined.",id,sub_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
mbag_item_t * i = cwi->type->from_str(val);
|
||||
i->id=cwi->id;
|
||||
DBGX("Item made: %s",i->id);
|
||||
DBGX("Item data: %s",i->data);
|
||||
i->id=cwi->id;
|
||||
|
||||
mbag_set(conn->outgoing,i);
|
||||
|
||||
|
||||
/*
|
||||
if (i->type == MBAG_STR) {
|
||||
mbag_set_strn(conn->outgoing, a->item_id, (char *) data, len);
|
||||
return 1;
|
||||
}
|
||||
*/
|
||||
|
||||
mavl_add(r,(void*)cwi->id);
|
||||
}
|
||||
|
||||
if (r->count)
|
||||
return r;
|
||||
|
||||
|
||||
// DBGX("The SQL RC: %d\n",rc);
|
||||
return 1;
|
||||
mavl_destroy(r);
|
||||
return NULL;
|
||||
|
||||
|
||||
errX:
|
||||
if (rc) {
|
||||
@ -215,7 +252,11 @@ errX:
|
||||
rc,sqlite3_errmsg(handle));
|
||||
}
|
||||
|
||||
return 0;
|
||||
if (r)
|
||||
mavl_destroy(r);
|
||||
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
|
10
src/ac/db.h
10
src/ac/db.h
@ -1,8 +1,16 @@
|
||||
#ifndef CW_MAVL_H
|
||||
#define CW_MAVL_H
|
||||
|
||||
#include "capwap/mavl.h"
|
||||
|
||||
extern void db_ping();
|
||||
extern int db_init();
|
||||
int db_start();
|
||||
void db_put_wtp_prop(const char *wtp_id,int rid, const char * prop,const char * val);
|
||||
int db_get_tasks(struct conn * conn,const char * wtpid);
|
||||
void db_put_wtp_prop(const char *wtp_id,const char * id,const char *sub_id,const char * val);
|
||||
mavl_conststr_t db_get_update_tasks(struct conn * conn,const char * wtpid);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -347,7 +347,7 @@ void config_to_sql(struct conn *conn)
|
||||
char str[256];
|
||||
if (i->type->to_str){
|
||||
i->type->to_str(i,str);
|
||||
db_put_wtp_prop(wtp_id,-1,cwi->id,str);
|
||||
db_put_wtp_prop(wtp_id,cwi->id,cwi->sub_id,str);
|
||||
}
|
||||
else{
|
||||
cw_log(LOG_ERR,"Can't converto to str");
|
||||
@ -448,24 +448,28 @@ static void wtpman_run(void *arg)
|
||||
}
|
||||
|
||||
mavl_del_all(conn->outgoing);
|
||||
db_get_tasks(conn,sock_addr2str(&conn->addr));
|
||||
|
||||
mavl_conststr_t r = db_get_update_tasks(conn,sock_addr2str(&conn->addr));
|
||||
|
||||
if ( !r )
|
||||
continue;
|
||||
|
||||
//printf("Conn: %d\n",conn->outgoing->count);
|
||||
if (!conn->outgoing->count)
|
||||
continue;
|
||||
|
||||
if ( conn->outgoing->count ) {
|
||||
rc = cw_send_request(conn, CW_MSG_CONFIGURATION_UPDATE_REQUEST);
|
||||
// DBGX("Have %d tasks",r->count);
|
||||
|
||||
DBGX("MAV MERGE","");
|
||||
mavl_merge(conn->config,conn->outgoing);
|
||||
DBGX("MAV MERGE DONE","");
|
||||
|
||||
|
||||
printf("Conn aa: %d\n",conn->outgoing->count);
|
||||
config_to_sql(conn);
|
||||
|
||||
}
|
||||
DBGX("Sending Request","");
|
||||
rc = cw_send_request(conn, CW_MSG_CONFIGURATION_UPDATE_REQUEST);
|
||||
DBGX("Request Sent","");
|
||||
mavl_merge(conn->config,conn->outgoing);
|
||||
mavl_destroy(conn->outgoing);
|
||||
conn->outgoing=mbag_create();
|
||||
DBGX("Mavel merged","");
|
||||
config_to_sql(conn);
|
||||
DBGX("Back tu SQL","");
|
||||
mavl_destroy(r);
|
||||
DBGX("DESTROY R","");
|
||||
|
||||
|
||||
|
||||
|
@ -76,6 +76,7 @@ MAVLOBJS= \
|
||||
mavl_foreach.o \
|
||||
mavl_foreach_lr.o \
|
||||
mavl_merge.o \
|
||||
mavl_create_conststr.o \
|
||||
utf8.o \
|
||||
file.o
|
||||
|
||||
|
@ -166,10 +166,12 @@ static int cw_action_out_cmp(const void *elem1, const void *elem2)
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
|
||||
/*
|
||||
r = e1->vendor_id - e2->vendor_id;
|
||||
if (r != 0)
|
||||
return r;
|
||||
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -213,7 +215,6 @@ cw_actionlist_out_t cw_actionlist_out_create()
|
||||
|
||||
cw_action_out_t *cw_actionlist_out_add(cw_actionlist_out_t t, struct cw_action_out * a)
|
||||
{
|
||||
DBGX("Adding out action: %d %d %p ",a->msg_id,a->elem_id,a->item_id);
|
||||
return cw_actionlist_add(t, a, sizeof(struct cw_action_out));
|
||||
}
|
||||
|
||||
|
@ -318,7 +318,7 @@ cw_action_out_t capwap_actions_ac_out[] = {
|
||||
|
||||
|
||||
/* -------------------------------------------------------------------------------
|
||||
* Echo Response
|
||||
* Echo Response OUT
|
||||
*/
|
||||
|
||||
{CW_MSG_ECHO_RESPONSE, CW_ITEM_NONE}
|
||||
@ -326,14 +326,19 @@ cw_action_out_t capwap_actions_ac_out[] = {
|
||||
|
||||
|
||||
/* -------------------------------------------------------------------------------
|
||||
* Update Request
|
||||
* Update Request OUT
|
||||
*/
|
||||
{CW_MSG_CONFIGURATION_UPDATE_REQUEST, CW_ITEM_NONE}
|
||||
,
|
||||
|
||||
/* Result Code */
|
||||
{CW_MSG_CONFIGURATION_UPDATE_REQUEST, CW_ITEM_WTP_NAME, 0,
|
||||
CW_ELEM_WTP_NAME, NULL,cw_out_generic, cw_out_get_outgoing, 1}
|
||||
CW_ELEM_WTP_NAME, NULL,cw_out_generic, cw_out_get_outgoing, 0}
|
||||
,
|
||||
|
||||
/* Location Data */
|
||||
{CW_MSG_CONFIGURATION_UPDATE_REQUEST, CW_ITEM_LOCATION_DATA, 0,
|
||||
CW_ELEM_LOCATION_DATA, NULL,cw_out_generic, cw_out_get_outgoing,0}
|
||||
,
|
||||
|
||||
|
||||
|
@ -163,6 +163,10 @@ cw_action_in_t capwap_actions_wtp_in[] = {
|
||||
CW_ACTION_IN_WTP_NAME, 0}
|
||||
,
|
||||
|
||||
/* Location Data */
|
||||
{0, 0, CW_STATE_RUN, CW_MSG_CONFIGURATION_UPDATE_REQUEST,
|
||||
CW_ACTION_IN_LOCATION_DATA, 0}
|
||||
,
|
||||
|
||||
|
||||
/* Element: Vendor Specific */
|
||||
|
@ -161,11 +161,11 @@ cw_action_out_t cipwap_actions_ac_out[] = {
|
||||
* Configuration Update Request
|
||||
*/
|
||||
{CW_MSG_CONFIGURATION_UPDATE_REQUEST, CW_ITEM_WTP_NAME, CW_VENDOR_ID_CISCO,
|
||||
CW_CISCO_RAD_NAME, NULL,cw_out_generic, cw_out_get_outgoing,1}
|
||||
CW_CISCO_RAD_NAME, NULL,cw_out_generic, cw_out_get_outgoing,0}
|
||||
,
|
||||
|
||||
{CW_MSG_CONFIGURATION_UPDATE_REQUEST, CW_ITEM_AP_MODE_AND_TYPE, CW_VENDOR_ID_CISCO,
|
||||
CW_CISCO_AP_MODE_AND_TYPE, NULL,cw_out_generic, cw_out_get_outgoing,1}
|
||||
CW_CISCO_AP_MODE_AND_TYPE, NULL,cw_out_generic, cw_out_get_outgoing,0}
|
||||
,
|
||||
|
||||
|
||||
|
@ -74,7 +74,7 @@ int cw_out_generic(struct conn *conn, struct cw_action_out *a, uint8_t * dst) //
|
||||
);
|
||||
}
|
||||
else{
|
||||
cw_dbg(DBG_WARN,"No output for element %s%d -(%s) in %s. Item %d not found.",
|
||||
cw_dbg(DBG_WARN,"No output for element %s%d -(%s) in %s. Item %s not found.",
|
||||
vendor,
|
||||
a->elem_id, cw_strelemp(conn->actions, a->elem_id)
|
||||
, cw_strmsg(a->msg_id),a->item_id);
|
||||
|
@ -113,7 +113,7 @@ int cw_put_msg(struct conn *conn, uint8_t * rawout)
|
||||
* alread initilaized in buffer
|
||||
* Message alements are taken fom actiondef in #conn->action
|
||||
*/
|
||||
int cw_put_custum_msg(struct conn *conn, uint8_t * rawout, mbag_t elems)
|
||||
int cw_put_custom_msg(struct conn *conn, uint8_t * rawout, mavl_conststr_t elems)
|
||||
{
|
||||
/* rawout is already initialized, so we can get
|
||||
msg type from buffer */
|
||||
@ -122,13 +122,21 @@ int cw_put_custum_msg(struct conn *conn, uint8_t * rawout, mbag_t elems)
|
||||
|
||||
MAVLITER_DEFINE(it,elems);
|
||||
mavliter_foreach(&it){
|
||||
mavliter_get(&it);
|
||||
const char *i= mavliter_get(&it);
|
||||
/* preapare action search */
|
||||
cw_action_out_t as;
|
||||
as.msg_id=msg_id;
|
||||
as.item_id=i;
|
||||
as.vendor_id=0;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
cw_action_out_t as;
|
||||
|
||||
as.item_id = CW_ITEM_NONE;
|
||||
as.vendor_id = 0;
|
||||
|
||||
|
||||
|
||||
|
@ -212,6 +212,12 @@ extern void * mavliter_seek(mavliter_t *i,void *d);
|
||||
|
||||
|
||||
|
||||
typedef mavl_t mavl_conststr_t;
|
||||
extern mavl_conststr_t mavl_create_conststr();
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
static inline void *mavl_replace_data(struct mavl *t, void *data, int len)
|
||||
{
|
||||
|
12
src/capwap/mavl_create_conststr.c
Normal file
12
src/capwap/mavl_create_conststr.c
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
#include "mavl.h"
|
||||
|
||||
static int cmp(const void *v1,const void*v2)
|
||||
{
|
||||
return strcmp( (char*)v1,(char*)v2);
|
||||
}
|
||||
|
||||
mavl_conststr_t mavl_create_conststr()
|
||||
{
|
||||
return mavl_create(cmp,NULL);
|
||||
}
|
@ -30,7 +30,7 @@ static void mavl_del_all0(struct mavl *t ,struct mavlnode * n)
|
||||
void mavl_del_all(struct mavl *t)
|
||||
{
|
||||
mavl_del_all0(t,t->root);
|
||||
t->root=0;
|
||||
t->root=NULL;
|
||||
}
|
||||
|
||||
|
||||
|
@ -42,7 +42,7 @@ static struct mbag_item * mbag_fromstr(const char *src)
|
||||
return NULL;
|
||||
|
||||
i->type = MBAG_STR;
|
||||
i->data = strdup(src);
|
||||
i->data = strndup(src,2000);
|
||||
return i;
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@
|
||||
"capwap_timers":"2590",
|
||||
"cisco_ap_mode_and_type":"516",
|
||||
"idle_timeout":"300",
|
||||
"location_data":"Germany",
|
||||
,
|
||||
"radios":{
|
||||
"0":{
|
||||
"admin_state":"1",
|
||||
@ -36,5 +36,5 @@
|
||||
"wtp_frame_tunnel_mode":"4",
|
||||
"wtp_group_name":"tobias",
|
||||
"wtp_mac_type":"1",
|
||||
"wtp_name":"hihihi"
|
||||
"wtp_name":"Thomas Mann"
|
||||
}
|
Loading…
Reference in New Issue
Block a user