wtp can send an empty discovery request

FossilOrigin-Name: 440ee8b077901e593d994ef900088b648204f1c8637b35189a5cddd18ec8f08f
This commit is contained in:
7u83@mail.ru
2018-03-12 17:01:40 +00:00
parent 835dc82e6f
commit 8b2e36e912
21 changed files with 272 additions and 208 deletions

View File

@ -221,7 +221,7 @@ cw_action_out_t *cw_actionlist_out_add(cw_actionlist_out_t t, struct cw_action_o
mavl_add(t,o,NULL);
}
struct mlistelem * e = mlist_replace(o->mlist,NULL,a);
struct mlistelem * e = mlist_replace(o->mlist,a);
if (!e)
e = mlist_append(o->mlist,a);

View File

@ -669,7 +669,9 @@ extern int cw_in_wtp_descriptor(struct conn *conn, struct cw_action_in *a, uint8
//extern int cw_out_generic(struct conn *conn,struct cw_action_in * a,uint8_t *data,int len);
*/
/*
extern int cw_out_generic(struct conn *conn, struct cw_action_out *a, uint8_t * dst);
*/
/*
//extern int cw_out_ac_descriptor(struct conn *conn, uint32_t elem_id, uint8_t * dst,

View File

@ -73,6 +73,8 @@ struct conn {
mavl_t remote_cfg;
mavl_t local_cfg;
mbag_t outgoing;

View File

@ -308,6 +308,7 @@ struct cw_DescriptorSubelemDef {
#define CW_APPEND 2
#define CW_PREPEND 3
#define CW_REPLACE 4
#define CW_IGNORE 5
int cw_check_missing_mand(struct cw_MsgData *msgdata, mavl_t keys );
@ -359,7 +360,8 @@ int cw_in_generic(struct conn * conn, struct cw_ElemHandler * handler,
int cw_in_generic(struct cw_ElemHandler * handler, struct cw_ElemHandlerParams * params,
uint8_t * elem_data, int elem_len);
int cw_out_generic(struct cw_ElemHandler * handler, struct cw_ElemHandlerParams * params
, uint8_t * dst);
extern int cw_in_wtp_reboot_statistics(struct conn *conn, struct cw_action_in *a,
uint8_t * data, int len, struct sockaddr *from);

View File

@ -4,31 +4,28 @@
#include "dbg.h"
#include "log.h"
#include "msgset.h"
#include "ktv.h"
int cw_out_generic(struct conn *conn, struct cw_action_out *a, uint8_t * dst)
int cw_out_generic(struct cw_ElemHandler * handler, struct cw_ElemHandlerParams * params
, uint8_t * dst)
{
/* Get the item to put */
struct mbag_item *item = NULL;
if (a->get) {
item = a->get(conn, a);
}
struct cw_KTV * elem;
int start, len;
/* Get the element to put */
elem = mavl_get(params->conn->local_cfg, handler->key);
/* Size for msg elem header depends on
vendor specific payload */
int start = a->vendor_id ? 10 : 4;
start = handler->vendor ? 10 : 4;
int len;
if (!item) {
if (elem == NULL) {
const char *vendor="";
if ( a->vendor_id ) {
vendor=cw_strvendor(a->vendor_id);
if ( handler->vendor ) {
vendor=cw_strvendor(handler->vendor);
}
if (a->mand) {
if ( 0) {
/* cw_log(LOG_ERR,
"Can't put mandatory element %s %d - (%s) into %s. No value for '%s' found.",
vendor,
@ -45,15 +42,15 @@ int cw_out_generic(struct conn *conn, struct cw_action_out *a, uint8_t * dst)
*/
}
return 0;
} else {
len = cw_put_mbag_item(dst + start, item);
}
}
len = handler->type->put(elem,dst+start);
/*(cw_put_mbag_item(dst + start, item);*/
if (a->vendor_id)
return len + cw_put_elem_vendor_hdr(dst, a->vendor_id, a->elem_id, len);
if (handler->vendor)
return len + cw_put_elem_vendor_hdr(dst, handler->vendor, handler->id, len);
return len + cw_put_elem_hdr(dst, a->elem_id, len);
return len + cw_put_elem_hdr(dst, handler->id, len);
}

View File

@ -27,7 +27,7 @@
#include "log.h"
#include "dbg.h"
#include "msgset.h"
/**
@ -39,10 +39,56 @@
int cw_put_msg(struct conn *conn, uint8_t * rawout)
{
uint8_t *msgptr,*dst;
int type;
struct cw_MsgData * msg;
struct mlistelem * elem;
int len,l;
/* rawout is already initialized, so we can get
msg type from buffer */
uint8_t *msgptr = rawout + cw_get_hdr_msg_offset(rawout);
* msg type from buffer */
msgptr = rawout + cw_get_hdr_msg_offset(rawout);
type = cw_get_msg_type(msgptr);
printf("Looking in %p for %d\n", conn->msgset,type);
msg = cw_msgset_get_msgdata(conn->msgset,type);
if (msg == NULL){
cw_log(LOG_ERR,"Error: Can't create message of type %d (%s) - no definition found.",
type, cw_strmsg(type));
}
dst = msgptr+8;
len =0;
mlist_foreach(elem,msg->elements_list){
struct cw_ElemData * data;
struct cw_ElemHandler * handler;
struct cw_ElemHandlerParams params;
data = mlistelem_dataptr(elem);
handler = cw_msgset_get_elemhandler(conn->msgset,data->proto,data->vendor,data->id);
printf("Elem: %d %d %d %s\n", data->proto, data->vendor, data->id, handler->name);
if (handler->put == NULL){
continue;
}
params.conn=conn;
params.elemdata = data;
l = handler->put(handler,&params,dst+len);
len += l;
}
cw_set_msg_elems_len(msgptr, len);
if (type & 1) {
/* It's a request, so we have to set seqnum */
int s = conn_get_next_seqnum(conn);
cw_set_msg_seqnum(msgptr,s);
}
return len;
printf("Message to send: %s\n",msg->name);
/* create search paramaters */
@ -52,9 +98,11 @@ int cw_put_msg(struct conn *conn, uint8_t * rawout)
as.item_id = CW_ITEM_NONE;
as.vendor_id = 0;
uint8_t *dst = msgptr+8;
dst = msgptr+8;
printf("Put Message\n");
exit(0);
/// TODO XXXX
//mlist_t m = cw_actionlist_out_get(conn->actions->out,cw_get_msg_type(msgptr));
mlist_t m =0;
@ -70,9 +118,10 @@ int cw_put_msg(struct conn *conn, uint8_t * rawout)
struct mlistelem *e;
int len = 0;
len = 0;
for (e=m->first; e; e=e->next) {
int l;
cw_action_out_t *ae=(cw_action_out_t*)e;
//printf("Put %d %i %s\n",ae->msg_id,ae->elem_id,ae->item_id);
@ -84,7 +133,7 @@ int cw_put_msg(struct conn *conn, uint8_t * rawout)
/* Element is from next msg, close action */
break;
}
int l=0;
l=0;
if (ae->out) {
// printf("Out Call with len =%d\n",len);

View File

@ -122,7 +122,7 @@ struct mavl *mavl_create ( int ( *cmp ) ( const void *, const void * ),
void *mavl_add ( struct mavl *t, const void *data, int *exists );
/*void *mavl_add ( struct mavl *t, const void *data );*/
void * mavl_get ( struct mavl *t , void *data );
void * mavl_get ( struct mavl *t , const void *data );
void *mavl_del ( struct mavl *t, const void *data );
void *mavl_replace ( struct mavl *t, const void *data, int * result );
void mavl_destroy ( struct mavl *t );

View File

@ -29,7 +29,7 @@
* @param data Element to get
* @return pointer to element or NULL if not found.
*/
void * mavl_get(struct mavl *t ,void *data)
void * mavl_get(struct mavl *t ,const void *data)
{
struct mavlnode *n = t->root;
while(n){

View File

@ -32,7 +32,7 @@ struct mlistelem *mlist_find(mlist_t l, struct mlistelem *start, void *data)
}
struct mlistelem * mlist_replace(mlist_t l, struct mlistelem *start, void *data)
struct mlistelem * xmlist_replace(mlist_t l, struct mlistelem *start, void *data)
{
/*
struct mlistelem *e;

View File

@ -64,10 +64,13 @@ typedef struct mlist * mlist_t;
mlist_t mlist_create(int (*cmp) (const void *v1, const void *v2), void (*del)(void *), size_t data_size);
struct mlistelem *mlist_append(mlist_t l, void *data);
struct mlistelem * mlist_get(mlist_t list, const void *data);
void mlist_destroy(mlist_t l);
struct mlistelem *mlist_replace(mlist_t l, void *data);
extern struct mlistelem *mlist_find(mlist_t l, struct mlistelem *start, void *data);
extern struct mlistelem *mlist_replace(mlist_t l, struct mlistelem *start, void *data);

12
src/cw/mlist_get.c Normal file
View File

@ -0,0 +1,12 @@
#include "mlist.h"
struct mlistelem * mlist_get(mlist_t list, const void *data){
struct mlistelem * elem;
mlist_foreach(elem,list){
void *tdata = mlistelem_dataptr(elem);
if (list->cmp(tdata,data)==0){
return tdata;
}
}
return NULL;
}

14
src/cw/mlist_replace.c Normal file
View File

@ -0,0 +1,14 @@
#include "mlist.h"
struct mlistelem * mlist_replace(mlist_t list, void *data)
{
struct mlistelem *e;
e = mlist_get(list,data);
if (e == NULL)
return NULL;
memcpy(mlistelem_dataptr(e), data,list->data_size);
return e;
}

View File

@ -62,9 +62,9 @@ static void msgdata_destroy(struct cw_MsgData *data)
{
if (!data)
return;
/* if (data->elements_list)
if (data->elements_list)
mlist_destroy(data->elements_list);
*/
if (data->elements_tree)
mavl_destroy(data->elements_tree);
free(data);
@ -92,7 +92,6 @@ void cw_msgset_destroy(struct cw_MsgSet *set)
free(set);
}
/**
* @brief Create a message set
* @return Message set create, NULL if an error has occured
@ -186,6 +185,7 @@ static int update_msgdata(struct cw_MsgSet *set, struct cw_MsgData *msgdata,
ed.vendor = elemdef->vendor;
ed.mand = elemdef->mand;
/* add message element to the elements tree */
result = mavl_replace(msgdata->elements_tree, &ed, &replaced);
if (!replaced) {
@ -197,8 +197,45 @@ static int update_msgdata(struct cw_MsgSet *set, struct cw_MsgData *msgdata,
elemdef->proto,
elemdef->vendor, elemdef->id, handler->name);
}
{
struct mlistelem *elem;
elem = mlist_get(msgdata->elements_list,&ed);
if (elem==NULL){
printf("Elem is not in mlist\n");
}
else{
printf("Elem was in mlsit\n");
}
}
/* add message elemeent to the elements list */
switch ( elemdef->op & 0xff){
case CW_IGNORE:
break;
case CW_DELETE:
break;
case CW_APPEND:
mlist_append(msgdata->elements_list, &ed);
break;
default:
case CW_REPLACE:
if (mlist_replace(msgdata->elements_list, &ed)==NULL){
mlist_append(msgdata->elements_list, &ed);
}
break;
}
}
if (msgdata->mand_keys!=NULL){
mlist_destroy(msgdata->mand_keys);
}
@ -258,6 +295,8 @@ int cw_msgset_add(struct cw_MsgSet *set,
msg->elements_tree = mavl_create(cmp_elemdata, NULL,
sizeof(struct cw_ElemData));
msg->mand_keys=NULL;
msg->elements_list = mlist_create(cmp_elemdata,NULL,sizeof(struct cw_ElemData));
}
/* Overwrite the found message */
@ -299,5 +338,5 @@ struct cw_MsgData *cw_msgset_get_msgdata(struct cw_MsgSet *set, int type)
{
struct cw_MsgData search;
search.type = type;
return mavl_find_ptr(set->msgdata, &search);
return mavl_find(set->msgdata, &search);
}

View File

@ -32,6 +32,7 @@ struct cw_ElemData{
struct cw_ElemHandlerParams {
struct conn * conn;
struct cw_MsgData * msgdata;
struct cw_ElemData * elemdata;
struct sockaddr *from;
mavl_t mand_found;
};
@ -47,6 +48,9 @@ struct cw_ElemHandler {
const char * key;
int (*get)(struct cw_ElemHandler * handler, struct cw_ElemHandlerParams * params,
uint8_t*data, int len);
int (*put)(struct cw_ElemHandler * handler, struct cw_ElemHandlerParams * params, uint8_t * dst);
/*
int (*end_in)(struct conn *conn,struct cw_action_in *a,uint8_t*elem,int len,struct sockaddr *from);
*/
@ -83,7 +87,8 @@ extern struct cw_MsgSet * cw_msgset_create();
extern void cw_msgset_destroy(struct cw_MsgSet * set);
extern int cw_msgset_add(struct cw_MsgSet * set,
struct cw_MsgDef messages[], struct cw_ElemHandler handlers[]);
mlist_t cw_msgset_get_msg(struct cw_MsgSet * set, int type);
/*mlist_t cw_msgset_get_msg(struct cw_MsgSet * set, int type);*/
struct cw_MsgData * cw_msgset_get_msgdata(struct cw_MsgSet *set,int type);
struct cw_ElemHandler * cw_msgset_get_elemhandler(struct cw_MsgSet * set,
int proto, int vendor, int id);