From 8a1a713c1247c75228930349a6070a6c67b3452e Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@mail.ru> Date: Tue, 9 Aug 2022 21:45:46 +0200 Subject: [PATCH] new type CW_TYPE_STRUCT --- src/cw/Makefile | 1 + src/cw/cw_type_struct.c | 95 +++++++++++++++++++++++++++++++++++++++++ src/cw/val.h | 17 ++++++-- 3 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 src/cw/cw_type_struct.c diff --git a/src/cw/Makefile b/src/cw/Makefile index a2919b90..7ea11197 100644 --- a/src/cw/Makefile +++ b/src/cw/Makefile @@ -68,6 +68,7 @@ CWSRC=\ cw_type_bool.c\ cw_type_str.c\ cw_type_dword.c\ + cw_type_struct.c\ cw_type_ipaddress.c\ cw_type_word.c\ cw_type_sysptr.c\ diff --git a/src/cw/cw_type_struct.c b/src/cw/cw_type_struct.c new file mode 100644 index 00000000..be8e2807 --- /dev/null +++ b/src/cw/cw_type_struct.c @@ -0,0 +1,95 @@ +#include "cw.h" +#include "val.h" + + +static int read_struct(cw_Cfg_t * cfg,const cw_ValStruct_t * stru, const char *pkey, + const uint8_t * data, int len) +{ + char key[CW_KTV_MAX_KEY_LEN]; + int pos, i,l; + + + pos=0; i=0; + while (stru[i].type != NULL){ + if(stru[i].position!=-1) + pos=stru[i].position; + + if (stru[i].key!=NULL) + sprintf(key,"%s/%s",pkey,stru[i].key); + else + sprintf(key,"%s",pkey); + + switch (stru[i].len){ + case CW_STRUCT_LEN_BYTE: + /* read len from next byte */ + l = cw_get_byte(data+pos); + pos ++; + break; + case CW_STRUCT_LEN_WORD: + /* read len from next word */ + l = cw_get_word(data+pos); + pos ++; + break; + case -1: + l = len-pos; + break; + default: + l = stru[i].len; + if (pos+l > len){ + l = len-pos; + } + + } + + l=stru[i].type->read(cfg,key,data+pos,l,stru[i].valguard); + +// result = cw_ktv_add(ktv,key,stru[i].type,stru[i].valguard,data+pos,l); + +// stru[i].type->to_str(result,dbstr,100); +// cw_dbg(DBG_ELEM_DETAIL, "Read (%d): %s: %s",pos,key,dbstr); +// printf("READ STRUCT (%d): %s: %s\n",pos,key,dbstr); + + if (stru[i].len==-1){ + ///l = result->type->len(result); + } + else { + l = stru[i].len; + } + + if(stru[i].position == -1) + pos+=l; + + i++; + } + + return pos; +} + + +static int bread(cw_Cfg_t *cfg, const char * key, const uint8_t *src, int len, const void *param) +{ + cw_ValStruct_t * stru = (cw_ValStruct_t *) param; + + read_struct(cfg,stru,key,src,len); + return 1; +} + + + + + +const struct cw_Type cw_type_struct = { + "Byte", /* name */ + NULL, /* del */ + NULL, /* put */ + NULL, /* get */ + NULL, /* to_str */ + NULL, /* from_str */ + NULL, /* len */ + NULL, /* data */ + NULL, /* get_type_name */ + NULL, + bread, + NULL + +}; diff --git a/src/cw/val.h b/src/cw/val.h index 2bb29404..edd5591b 100644 --- a/src/cw/val.h +++ b/src/cw/val.h @@ -8,6 +8,7 @@ #include "mavltypes.h" #include "bstr.h" +#include "cfg.h" /** @@ -53,7 +54,7 @@ typedef struct cw_Val cw_Val_t; /** * @class cw_Type * @author 7u83 - * @file ktv.h + * @file val.h * @brief Representation of a cw_Type objetc */ struct cw_Type { @@ -90,6 +91,9 @@ struct cw_Type { const char * (*get_type_name)(cw_Val_t*); int (*cast)(cw_Val_t *); + + int (*read)(cw_Cfg_t *cfg, const char * key, const uint8_t *src, int len, const void * param); + int (*write)(cw_Cfg_t *cfg, const char *key, const uint8_t *dst, const void * param); }; typedef struct cw_Type cw_Type_t; @@ -117,8 +121,12 @@ typedef struct cw_ValStruct cw_ValStruct_t; -#define CW_KTVSTRUCT_L16 -2 -#define CW_KTVSTRUCT_L8 -3 +#define CW_STRUCT_LEN_WORD -2 +#define CW_STRUCT_LEN_BYTE -3 + + +#define CW_KTVSTRUCT_L8 -2 +#define CW_KTVSTRUCT_L16 -3 struct cw_ValEnum{ int value; @@ -152,6 +160,7 @@ extern const struct cw_Type cw_type_str; extern const struct cw_Type cw_type_ipaddress; extern const struct cw_Type cw_type_sysptr; extern const struct cw_Type cw_type_bool; +extern const struct cw_Type cw_type_struct; #define CW_TYPE_BYTE (&cw_type_byte) #define CW_TYPE_WORD (&cw_type_word) @@ -162,6 +171,8 @@ extern const struct cw_Type cw_type_bool; #define CW_TYPE_SYSPTR (&cw_type_sysptr) #define CW_TYPE_STR (&cw_type_str) #define CW_TYPE_BOOL (&cw_type_bool) +#define CW_TYPE_STRUCT (&cw_type_struct) + /* void cw_kvstore_mavl_delete(const void *data); */