2018-02-24 00:30:12 +01:00
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include "mbag.h"
|
2018-02-24 00:30:44 +01:00
|
|
|
#include "mavl.h"
|
2018-03-03 17:42:28 +01:00
|
|
|
#include "cw/debug.h"
|
2018-03-03 08:15:19 +01:00
|
|
|
#include "cw/log.h"
|
2018-02-24 00:30:12 +01:00
|
|
|
|
2018-02-24 00:30:44 +01:00
|
|
|
#include "message_set.h"
|
2018-02-24 00:30:12 +01:00
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
static int cmp_cw_elemhandler_by_id(const mavldata_t * elem1, const mavldata_t *elem2){
|
|
|
|
struct cw_ElemHandler * e1 = elem1->ptr;
|
|
|
|
struct cw_ElemHandler * e2 = elem2->ptr;
|
2018-02-24 00:30:44 +01:00
|
|
|
int r;
|
|
|
|
r = e1->id - e2->id;
|
|
|
|
if (r!=0)
|
|
|
|
return r;
|
|
|
|
r = e1->vendor - e2->vendor;
|
|
|
|
if (r!=0)
|
|
|
|
return r;
|
|
|
|
r = e1->proto - e2->proto;
|
|
|
|
if (r != 0)
|
|
|
|
return r;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
static int cmp_cw_elemhandler_by_key(const mavldata_t *elem1, const mavldata_t *elem2){
|
|
|
|
struct cw_ElemHandler * e1 = elem1->ptr;
|
|
|
|
struct cw_ElemHandler * e2 = elem2->ptr;
|
2018-03-03 08:15:19 +01:00
|
|
|
return strcmp(e1->key,e2->key);
|
|
|
|
}
|
2018-02-24 00:30:44 +01:00
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
static int cmp_msgdata(const mavldata_t * elem1, const mavldata_t *elem2)
|
2018-02-24 00:30:44 +01:00
|
|
|
{
|
2018-03-03 17:42:28 +01:00
|
|
|
struct cw_MsgData * e1 = elem1->ptr;
|
|
|
|
struct cw_MsgData * e2 = elem2->ptr;
|
2018-02-24 01:52:39 +01:00
|
|
|
return e1->type - e2->type;
|
2018-02-24 00:30:44 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
static int cmp_elemdata(const mavldata_t * elem1, const mavldata_t *elem2)
|
|
|
|
{
|
|
|
|
struct cw_ElemData * e1 = elem1->ptr;
|
|
|
|
struct cw_ElemData * e2 = elem2->ptr;
|
|
|
|
int r;
|
|
|
|
r = e1->id - e2->id;
|
|
|
|
if (r!=0)
|
|
|
|
return r;
|
|
|
|
r = e1->vendor - e2->vendor;
|
|
|
|
if (r!=0)
|
|
|
|
return r;
|
|
|
|
r = e1->proto - e2->proto;
|
|
|
|
if (r != 0)
|
|
|
|
return r;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-03 08:15:19 +01:00
|
|
|
static void msgdata_destroy(struct cw_MsgData *data){
|
|
|
|
if (!data)
|
|
|
|
return;
|
2018-03-03 17:42:28 +01:00
|
|
|
/* if (data->elements_list)
|
2018-03-03 08:15:19 +01:00
|
|
|
mlist_destroy(data->elements_list);
|
2018-03-03 17:42:28 +01:00
|
|
|
*/
|
2018-03-03 08:15:19 +01:00
|
|
|
if (data->elements_tree)
|
|
|
|
mavl_destroy( data->elements_tree );
|
|
|
|
free(data);
|
|
|
|
}
|
2018-02-24 00:30:44 +01:00
|
|
|
|
2018-03-03 08:15:19 +01:00
|
|
|
static struct cw_MsgData * msgdata_create(){
|
|
|
|
struct cw_MsgData * msg;
|
2018-03-03 17:42:28 +01:00
|
|
|
|
2018-03-03 08:15:19 +01:00
|
|
|
msg = malloc( sizeof(struct cw_MsgData));
|
|
|
|
if (!msg)
|
|
|
|
return NULL;
|
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
msg->elements_tree = mavl_create(cmp_elemdata,mavl_free_bin);
|
2018-03-03 08:15:19 +01:00
|
|
|
if (!msg->elements_tree){
|
|
|
|
msgdata_destroy(msg);
|
|
|
|
return NULL;
|
2018-02-24 00:30:44 +01:00
|
|
|
}
|
2018-03-03 17:42:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
return msg;
|
2018-03-03 08:15:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
static void del_mavl_msdgdata( mavldata_t * d ){
|
|
|
|
msgdata_destroy(d->ptr);
|
|
|
|
}
|
|
|
|
|
2018-03-03 08:15:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Destroy a message set
|
|
|
|
* @param set Message set to destroy
|
|
|
|
*/
|
|
|
|
void cw_msgset_destroy(struct cw_MsgSet * set){
|
|
|
|
if (set->messages)
|
|
|
|
mavl_destroy(set->messages);
|
2018-03-03 17:42:28 +01:00
|
|
|
if (set->handlers_by_id)
|
|
|
|
mavl_destroy(set->handlers_by_id);
|
|
|
|
if (set->handlers_by_key)
|
|
|
|
mavl_destroy(set->handlers_by_key);
|
2018-02-24 00:30:44 +01:00
|
|
|
free(set);
|
|
|
|
}
|
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
|
2018-03-03 08:15:19 +01:00
|
|
|
/**
|
|
|
|
* @brief Create a message set
|
|
|
|
* @return Message set create, NULL if an error has occured
|
|
|
|
*/
|
|
|
|
struct cw_MsgSet * cw_msgset_create(){
|
2018-02-24 00:30:44 +01:00
|
|
|
|
|
|
|
/* allocate memory for a message_set */
|
2018-03-03 08:15:19 +01:00
|
|
|
struct cw_MsgSet * set = malloc(sizeof(struct cw_MsgSet ));
|
2018-02-24 00:30:44 +01:00
|
|
|
if (set==NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2018-03-03 08:15:19 +01:00
|
|
|
memset(set,0,sizeof(struct cw_MsgSet));
|
2018-02-24 00:30:44 +01:00
|
|
|
|
2018-03-03 08:15:19 +01:00
|
|
|
/* create mavl for all_elems by id */
|
2018-03-03 17:42:28 +01:00
|
|
|
set->handlers_by_id = mavl_create(cmp_cw_elemhandler_by_id,mavl_free_bin);
|
|
|
|
if (set->handlers_by_id==NULL){
|
2018-03-03 08:15:19 +01:00
|
|
|
cw_msgset_destroy(set);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create mavl for all_elems by id */
|
2018-03-03 17:42:28 +01:00
|
|
|
set->handlers_by_key = mavl_create(cmp_cw_elemhandler_by_key,NULL);
|
|
|
|
if (set->handlers_by_key==NULL){
|
2018-03-03 08:15:19 +01:00
|
|
|
cw_msgset_destroy(set);
|
2018-02-24 00:30:44 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create mavl for messages */
|
2018-03-03 17:42:28 +01:00
|
|
|
set->messages = mavl_create(cmp_msgdata,del_mavl_msdgdata);
|
2018-02-24 00:30:44 +01:00
|
|
|
if (set->messages==NULL){
|
2018-03-03 08:15:19 +01:00
|
|
|
cw_msgset_destroy(set);
|
2018-02-24 00:30:44 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
struct cw_ElemHandler * cw_msgset_get_elemhandler(struct cw_MsgSet * set,
|
|
|
|
int proto, int vendor, int id)
|
|
|
|
{
|
|
|
|
struct cw_ElemHandler search;
|
|
|
|
search.proto=proto;
|
|
|
|
search.vendor=vendor;
|
|
|
|
search.id=id;
|
|
|
|
return mavl_find_ptr(set->handlers_by_id,&search);
|
|
|
|
}
|
2018-02-24 00:30:44 +01:00
|
|
|
|
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
static int update_msgdata(struct cw_MsgSet * set, struct cw_MsgData * msgdata,
|
|
|
|
struct cw_MsgDef * msgdef)
|
|
|
|
{
|
|
|
|
struct cw_ElemDef * elemdef;
|
|
|
|
|
|
|
|
/* iterate through all defined elements */
|
|
|
|
for(elemdef = msgdef->elements; elemdef->id; elemdef++){
|
|
|
|
struct cw_ElemHandler * handler;
|
|
|
|
|
|
|
|
handler = cw_msgset_get_elemhandler(set,
|
|
|
|
elemdef->proto,
|
|
|
|
elemdef->vendor,
|
|
|
|
elemdef->id);
|
|
|
|
|
|
|
|
if (!handler){
|
|
|
|
cw_log(LOG_ERR, "No handler for message element: %d %d %d",
|
|
|
|
elemdef->proto,
|
|
|
|
elemdef->vendor,
|
|
|
|
elemdef->id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBG_START(NULL,DBG_INFO)
|
|
|
|
"Have an element %d %d %d, %s",
|
|
|
|
elemdef->proto,
|
|
|
|
elemdef->id,
|
|
|
|
elemdef->vendor,
|
|
|
|
handler->name
|
|
|
|
|
|
|
|
DBG_END
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-24 23:58:31 +01:00
|
|
|
|
|
|
|
|
2018-03-03 08:15:19 +01:00
|
|
|
int cw_msgset_add(struct cw_MsgSet * set,
|
|
|
|
struct cw_MsgDef messages[],
|
2018-03-03 17:42:28 +01:00
|
|
|
struct cw_ElemHandler handlers[]
|
2018-03-03 08:15:19 +01:00
|
|
|
){
|
2018-02-24 23:58:31 +01:00
|
|
|
|
2018-03-03 08:15:19 +01:00
|
|
|
struct cw_ElemHandler * handler;
|
2018-03-03 17:42:28 +01:00
|
|
|
struct cw_MsgDef * msgdef;
|
|
|
|
|
2018-03-03 08:15:19 +01:00
|
|
|
/* Create mavl for all handlers */
|
2018-03-03 17:42:28 +01:00
|
|
|
for(handler = handlers; handler->id; handler++){
|
|
|
|
mavldata_t copy;
|
|
|
|
copy.ptr = malloc(sizeof(struct cw_ElemHandler));
|
|
|
|
if (!copy.ptr){
|
2018-03-03 08:15:19 +01:00
|
|
|
cw_log(LOG_ERR,"Can't alloc mem:", strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
DBG_START(NULL,DBG_MOD)
|
|
|
|
" Adding handler for element %d - %s - with key: %s",
|
|
|
|
handler->id,
|
|
|
|
handler->name,
|
|
|
|
handler->key
|
|
|
|
DBG_END
|
|
|
|
|
|
|
|
memcpy(copy.ptr,handler,sizeof(struct cw_ElemHandler));
|
2018-03-03 08:15:19 +01:00
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
mavl_replace(set->handlers_by_id,©);
|
|
|
|
mavl_replace(set->handlers_by_key,©);
|
2018-03-03 08:15:19 +01:00
|
|
|
}
|
2018-02-24 23:58:31 +01:00
|
|
|
|
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
for (msgdef=messages; msgdef->type !=0; msgdef++){
|
|
|
|
struct cw_MsgData search, *msg;
|
2018-03-03 08:15:19 +01:00
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
/* Look if message already exists */
|
|
|
|
search.type=msgdef->type;
|
|
|
|
msg = mavl_find_ptr(set->messages,&search);
|
|
|
|
|
|
|
|
if (!msg){
|
|
|
|
msg = msgdata_create();
|
|
|
|
if (!msg){
|
|
|
|
cw_log(LOG_ERR,"Can't create messae");
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-24 23:58:31 +01:00
|
|
|
}
|
2018-03-03 08:15:19 +01:00
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
/* Overwrite the found message */
|
|
|
|
if (msgdef->name)
|
|
|
|
msg->name=msgdef->name;
|
|
|
|
if (msgdef->states)
|
|
|
|
msg->states=msgdef->states;
|
|
|
|
msg->receiver=msgdef->receiver;
|
2018-03-03 08:15:19 +01:00
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
DBG_START(NULL,DBG_INFO)
|
|
|
|
"Add message Type:%d - %s ",msgdef->type,msgdef->name
|
|
|
|
DBG_END
|
2018-03-03 08:15:19 +01:00
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
|
|
|
|
update_msgdata(set,msg,msgdef);
|
2018-02-24 00:30:44 +01:00
|
|
|
}
|
2018-03-03 17:42:28 +01:00
|
|
|
|
|
|
|
DBG_START(NULL,DBG_INFO) "Hello world" DBG_END
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
return 0;
|
2018-02-24 00:30:12 +01:00
|
|
|
}
|
2018-02-24 01:52:39 +01:00
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
/*
|
2018-03-03 08:15:19 +01:00
|
|
|
struct cw_ElemHandler * cw_message_set_find_element(
|
|
|
|
struct cw_MsgSet * set,
|
|
|
|
struct cw_ElemHandler * element){
|
|
|
|
return mavl_find(set->all_elems_by_id,element);
|
2018-02-24 23:58:31 +01:00
|
|
|
}
|
2018-03-03 17:42:28 +01:00
|
|
|
*/
|
2018-02-24 23:58:31 +01:00
|
|
|
|
|
|
|
|
2018-03-03 17:42:28 +01:00
|
|
|
/*
|
2018-03-03 08:15:19 +01:00
|
|
|
mlist_t cw_msgset_get_msg(struct cw_MsgSet * set, int type){
|
|
|
|
struct cw_MsgData search;
|
2018-02-24 23:58:31 +01:00
|
|
|
search.type = type;
|
2018-03-03 08:15:19 +01:00
|
|
|
struct cw_MsgData * result = mavl_find(set->messages,&search);
|
2018-02-24 23:58:31 +01:00
|
|
|
if (!result){
|
|
|
|
printf ("no result\n");
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
return result->elements_list;
|
2018-03-02 13:36:03 +01:00
|
|
|
}
|
2018-03-03 17:42:28 +01:00
|
|
|
*/
|