From 2722e76f513ba7be80e63f1f49f2095d39759f09 Mon Sep 17 00:00:00 2001 From: zilio nicolas Date: Thu, 10 Sep 2015 20:40:15 +0200 Subject: more towards views --- pcilib/pci.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'pcilib/pci.h') diff --git a/pcilib/pci.h b/pcilib/pci.h index 00528e1..657c335 100644 --- a/pcilib/pci.h +++ b/pcilib/pci.h @@ -62,6 +62,8 @@ struct pcilib_s { size_t num_banks, num_protocols, num_ranges; /**< Number of registered banks, protocols, and register ranges */ size_t num_engines; /**< Number of configured DMA engines */ size_t dyn_banks; /**< Number of configured dynamic banks */ + size_t num_enum_views,alloc_enum_views; /**< Number of configured and allocated views of type enum*/ + size_t num_formula_views,alloc_formula_views; /**< Number of configured and allocated views of type formula*/ pcilib_register_description_t *registers; /**< List of currently defined registers (from all sources) */ pcilib_register_bank_description_t banks[PCILIB_MAX_REGISTER_BANKS + 1]; /**< List of currently defined register banks (from all sources) */ @@ -81,6 +83,9 @@ struct pcilib_s { struct pcilib_locking_s locks; /**< Context of locking subsystem */ struct pcilib_xml_s xml; /**< XML context */ + pcilib_view_enum2_t* enum_views; /**< list of currently defined views of type enum*/ + pcilib_view_formula_t* formula_views; /**< list of currently defined views of type formula*/ + #ifdef PCILIB_FILE_IO int file_io_handle; #endif /* PCILIB_FILE_IO */ -- cgit v1.2.3 From 60c6bf9f6916b6ae2c05a499675ff54480256ece Mon Sep 17 00:00:00 2001 From: "nicolas.zilio@hotmail.fr" <> Date: Fri, 11 Sep 2015 19:40:33 +0200 Subject: more towards views --- CMakeLists.txt | 2 +- pcilib/pci.c | 12 ++++++++ pcilib/pci.h | 1 + pcilib/views.c | 6 ++-- pcilib/views.h | 6 ++-- pcilib/xml.c | 80 ++++++++++++++++++++++++++++++++--------------------- pcitool/cli.c | 5 ++++ xml/test/camera.xml | 6 ++-- 8 files changed, 77 insertions(+), 41 deletions(-) (limited to 'pcilib/pci.h') diff --git a/CMakeLists.txt b/CMakeLists.txt index d9e637f..c5cc12a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,7 +46,7 @@ if (NOT DISABLE_PCITOOL) pkg_check_modules(FASTWRITER fastwriter REQUIRED) endif (NOT DISABLE_PCITOOL) -add_definitions("-fPIC --std=c99 -Wall -O2 -gdwarf-2 -g3 -fno-omit-frame-pointer") +add_definitions("-fPIC --std=c99 -Wall -O2 -gdwarf-2 -g3 -fno-omit-frame-pointer -g") #add_definitions("-fPIC --std=c99 -Wall -O2") include(cmake/version.cmake) diff --git a/pcilib/pci.c b/pcilib/pci.c index b7dcbcd..8c178f6 100644 --- a/pcilib/pci.c +++ b/pcilib/pci.c @@ -139,8 +139,12 @@ pcilib_t *pcilib_open(const char *device, const char *model) { } ctx->alloc_reg = PCILIB_DEFAULT_REGISTER_SPACE; + ctx->alloc_formula_views=PCILIB_DEFAULT_VIEW_SPACE; + ctx->alloc_enum_views=PCILIB_DEFAULT_VIEW_SPACE; ctx->registers = (pcilib_register_description_t *)malloc(PCILIB_DEFAULT_REGISTER_SPACE * sizeof(pcilib_register_description_t)); ctx->register_ctx = (pcilib_register_context_t *)malloc(PCILIB_DEFAULT_REGISTER_SPACE * sizeof(pcilib_register_context_t)); + ctx->enum_views = (pcilib_view_enum2_t *)malloc(PCILIB_DEFAULT_VIEW_SPACE * sizeof(pcilib_view_enum2_t)); + ctx->formula_views = (pcilib_view_formula_t*)malloc(PCILIB_DEFAULT_VIEW_SPACE * sizeof(pcilib_view_formula_t)); if ((!ctx->registers)||(!ctx->register_ctx)) { pcilib_error("Error allocating memory for register model"); @@ -148,12 +152,20 @@ pcilib_t *pcilib_open(const char *device, const char *model) { return NULL; } + if((!ctx->enum_views)||(!ctx->formula_views)){ + pcilib_error("Error allocating memory for views"); + pcilib_close(ctx); + return NULL; + } + memset(ctx->registers, 0, sizeof(pcilib_register_description_t)); memset(ctx->banks, 0, sizeof(pcilib_register_bank_description_t)); memset(ctx->ranges, 0, sizeof(pcilib_register_range_t)); memset(ctx->register_ctx, 0, PCILIB_DEFAULT_REGISTER_SPACE * sizeof(pcilib_register_context_t)); + memset(ctx->enum_views,0,sizeof(pcilib_view_enum2_t)); + memset(ctx->formula_views,0,sizeof(pcilib_view_formula_t)); for (i = 0; pcilib_protocols[i].api; i++); memcpy(ctx->protocols, pcilib_protocols, i * sizeof(pcilib_register_protocol_description_t)); diff --git a/pcilib/pci.h b/pcilib/pci.h index 657c335..3b1c0e8 100644 --- a/pcilib/pci.h +++ b/pcilib/pci.h @@ -8,6 +8,7 @@ #define PCILIB_DMA_SKIP_TIMEOUT 1000000 /**< us */ #define PCILIB_MAX_BARS 6 /**< this is defined by PCI specification */ #define PCILIB_DEFAULT_REGISTER_SPACE 1024 /**< number of registers to allocate on init */ +#define PCILIB_DEFAULT_VIEW_SPACE 128 /**< number of views to allocate on init */ #define PCILIB_MAX_REGISTER_BANKS 32 /**< maximum number of register banks to allocate space for */ #define PCILIB_MAX_REGISTER_RANGES 32 /**< maximum number of register ranges to allocate space for */ #define PCILIB_MAX_REGISTER_PROTOCOLS 32 /**< maximum number of register protocols to support */ diff --git a/pcilib/views.c b/pcilib/views.c index 44392d9..038b48d 100644 --- a/pcilib/views.c +++ b/pcilib/views.c @@ -2,7 +2,7 @@ #include "pci.h" #include "pcilib.h" #include -//#include "views.h" +#include "views.h" #include "error.h" #include #include @@ -349,7 +349,6 @@ int pcilib_add_views_enum(pcilib_t *ctx, size_t n, const pcilib_view_enum2_t* vi if (!views_enum) return PCILIB_ERROR_MEMORY; ctx->enum_views = views_enum; - /* context + model part ????*/ ctx->alloc_enum_views = size; } @@ -376,13 +375,12 @@ int pcilib_add_views_formula(pcilib_t *ctx, size_t n, const pcilib_view_formula_ } if ((ctx->num_formula_views + n + 1) > ctx->alloc_formula_views) { - for (size = ctx->alloc_formula_views; size < 2 * (n + ctx->num_formula_views + 1); size<<=1); + for (size = ctx->alloc_formula_views; size < 2 * (n + ctx->num_formula_views + 1); size<<=1); views_formula = (pcilib_view_formula_t*)realloc(ctx->formula_views, size * sizeof(pcilib_view_formula_t)); if (!views_formula) return PCILIB_ERROR_MEMORY; ctx->formula_views = views_formula; - /* context + model part?????*/ ctx->alloc_formula_views = size; } diff --git a/pcilib/views.h b/pcilib/views.h index 98e3dcd..dfc9f70 100644 --- a/pcilib/views.h +++ b/pcilib/views.h @@ -24,6 +24,7 @@ struct pcilib_view_enum_s { struct pcilib_view_enum2_s { const char* name; pcilib_view_enum_t* enums_list; + const char* description; }; @@ -35,6 +36,7 @@ struct pcilib_view_formula_s { const char *read_formula; /**< formula to parse to read from a register*/ const char *write_formula; /**nodesetval; - if (!xmlXPathNodeSetIsEmpty(nodeset)) { int i,k,l; /*if we correctly get a nodeset, then we iterate through the nodeset to get all views, using their names*/ for (i = 0; i < nodeset->nodeNr; i++) { view_name=(char*)nodeset->nodeTab[i]->children->content; - + /* if the view name obtained is for an enum view, we get all pcilib_view_enum_t corresponding to the register*/ - for(k=0; ctx->enum_views[k].enums_list[0].value;k++){ + for(k=0; ctx->enum_views[k].enums_list[0].value; k++){ if(!(strcasecmp(view_name, ctx->enum_views[k].name))){ ctx->register_ctx[id].enums=malloc(sizeof(pcilib_view_enum_t)); @@ -109,29 +113,34 @@ pcilib_get_associated_views(pcilib_t* ctx, const char* reg_name,xmlXPathContextP return PCILIB_ERROR_MEMORY; } + /*!!!!!!!!!!this loop here is buggy*/ for(l=0; ctx->enum_views[k].enums_list[l].value;l++){ ctx->register_ctx[id].enums=realloc(ctx->register_ctx[id].enums,(l+1)*sizeof(pcilib_view_enum_t)); ctx->register_ctx[id].enums[l]=ctx->enum_views[k].enums_list[l]; + // printf("names %s %s\n",ctx->register_ctx[id].enums[l].name,ctx->enum_views[k].enums_list[l].name); } + return 0; } } /*here it is for formula, i assume we have only one formula view per register*/ for(k=0; ctx->formula_views[k].name[0];k++){ if(!(strcasecmp(view_name,ctx->formula_views[k].name))){ + ctx->register_ctx[id].formulas=malloc(sizeof(pcilib_view_formula_t)); if(!(ctx->register_ctx[id].formulas)){ pcilib_error("error allocating memory for formula views in register context %i",id); return PCILIB_ERROR_MEMORY; } - ctx->register_ctx[id].formulas=&(ctx->formula_views[k]); + ctx->register_ctx[id].formulas[0]=ctx->formula_views[k]; + break; } } } } - + xmlXPathFreeObject(nodes); return 0; } @@ -247,7 +256,8 @@ static int pcilib_xml_parse_register(pcilib_t *ctx, pcilib_xml_register_descript static int pcilib_xml_create_register(pcilib_t *ctx, pcilib_register_bank_t bank, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node) { int err; int views_ok=0; - + int h; + xmlXPathObjectPtr nodes; xmlNodeSetPtr nodeset; @@ -255,7 +265,7 @@ static int pcilib_xml_create_register(pcilib_t *ctx, pcilib_register_bank_t bank pcilib_xml_register_description_t fdesc; pcilib_register_t reg; - + desc.base.bank = ctx->banks[bank].addr; desc.base.rwmask = PCILIB_REGISTER_ALL_BITS; desc.base.mode = PCILIB_REGISTER_R; @@ -279,7 +289,7 @@ static int pcilib_xml_create_register(pcilib_t *ctx, pcilib_register_bank_t bank /* if the register had a node of type views, then we compute its associated registers. I do that here as i need the index for register context*/ if(views_ok){ - err=pcilib_get_associated_views(ctx,desc.base.name,xpath,reg); + err=pcilib_get_associated_views(ctx,desc.base.name,xpath,reg,1); if(err) pcilib_warning("can't get correctly the associated views of the register %s",desc.base.name); } @@ -324,7 +334,7 @@ static int pcilib_xml_create_register(pcilib_t *ctx, pcilib_register_bank_t bank ctx->register_ctx[reg].min = fdesc.min; ctx->register_ctx[reg].max = fdesc.max; if(views_ok){ - err=pcilib_get_associated_views(ctx, desc.base.name,xpath,reg); + err=pcilib_get_associated_views(ctx, desc.base.name,xpath,reg,0); if(err) pcilib_warning("can't get correctly the associated views of the register %s",fdesc.base.name); } } @@ -503,6 +513,7 @@ static int pcilib_xml_create_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo enum_desc.max=0; complete_enum_desc.name="default enum"; + complete_enum_desc.description="default description"; complete_enum_desc.enums_list=malloc(sizeof(pcilib_view_enum_t)); if(!(complete_enum_desc.enums_list)){ pcilib_error("can't allocate memory for the complete enum type"); @@ -513,6 +524,7 @@ static int pcilib_xml_create_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo formula_desc.name="formula_default"; formula_desc.read_formula="@reg"; formula_desc.write_formula="@reg"; + formula_desc.description="default description"; /* we get the attribute type of the view node*/ attr=node->properties; @@ -530,6 +542,10 @@ static int pcilib_xml_create_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo if (!(strcasecmp((char*)name,"name"))) { complete_enum_desc.name = value; + + }else if (!(strcasecmp((char*)name,"description"))) { + complete_enum_desc.description = value; + }else if (!(strcasecmp((char*)name,"enum"))) { complete_enum_desc.enums_list=realloc(complete_enum_desc.enums_list,(i+1)*sizeof(pcilib_view_enum_t)); @@ -567,7 +583,7 @@ static int pcilib_xml_create_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo complete_enum_desc.enums_list[i].max=dat_max; } } - i++; + i++; } } err=pcilib_add_views_enum(ctx,1,&complete_enum_desc); @@ -584,6 +600,7 @@ static int pcilib_xml_create_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo name = (char*)cur->name; value = (char*)cur->children->content; + if (!value) continue; if (!(strcasecmp((char*)name,"name"))) { @@ -592,6 +609,8 @@ static int pcilib_xml_create_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo formula_desc.read_formula=value; }else if (!(strcasecmp((char*)name,"write_to_register"))) { formula_desc.write_formula=value; + }else if (!(strcasecmp((char*)name,"description"))) { + formula_desc.description=value; } } err=pcilib_add_views_formula(ctx,1,&formula_desc); @@ -619,6 +638,22 @@ static int pcilib_xml_process_document(pcilib_t *ctx, xmlDocPtr doc, xmlXPathCon int i; xmlErrorPtr xmlerr; + views_nodes=xmlXPathEvalExpression(VIEWS_PATH,xpath); + if(!views_nodes){ + xmlerr = xmlGetLastError(); + if (xmlerr) pcilib_error("Failed to parse XPath expression %s, xmlXPathEvalExpression reported error %d - %s", BANKS_PATH, xmlerr->code, xmlerr->message); + else pcilib_error("Failed to parse XPath expression %s", BANKS_PATH); + return PCILIB_ERROR_FAILED; + } + + nodeset=views_nodes->nodesetval; + if(!xmlXPathNodeSetIsEmpty(nodeset)){ + for(i=0;i < nodeset->nodeNr; i++){ + pcilib_xml_create_view(ctx,xpath,doc,nodeset->nodeTab[i]); + } + } + xmlXPathFreeObject(views_nodes); + bank_nodes = xmlXPathEvalExpression(BANKS_PATH, xpath); if (!bank_nodes) { xmlerr = xmlGetLastError(); @@ -636,23 +671,6 @@ static int pcilib_xml_process_document(pcilib_t *ctx, xmlDocPtr doc, xmlXPathCon } xmlXPathFreeObject(bank_nodes); - - views_nodes=xmlXPathEvalExpression(VIEWS_PATH,xpath); - if(!views_nodes){ - xmlerr = xmlGetLastError(); - if (xmlerr) pcilib_error("Failed to parse XPath expression %s, xmlXPathEvalExpression reported error %d - %s", BANKS_PATH, xmlerr->code, xmlerr->message); - else pcilib_error("Failed to parse XPath expression %s", BANKS_PATH); - return PCILIB_ERROR_FAILED; - } - - nodeset=views_nodes->nodesetval; - if(!xmlXPathNodeSetIsEmpty(nodeset)){ - for(i=0;i < nodeset->nodeNr; i++){ - pcilib_xml_create_view(ctx,xpath,doc,nodeset->nodeTab[i]); - } - } - xmlXPathFreeObject(views_nodes); - return 0; } diff --git a/pcitool/cli.c b/pcitool/cli.c index c3663d2..7014190 100644 --- a/pcitool/cli.c +++ b/pcitool/cli.c @@ -2720,6 +2720,11 @@ int main(int argc, char **argv) { mode = MODE_READ; if (optarg) addr = optarg; else if ((optind < argc)&&(argv[optind][0] != '-')) addr = argv[optind++]; + + /* char* s; + if(!(s=strchr(addr,'/'))){ + mode=MODE_READ_VIEW;*/ + break; case OPT_WRITE: if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported"); diff --git a/xml/test/camera.xml b/xml/test/camera.xml index 5b486ee..815a50e 100644 --- a/xml/test/camera.xml +++ b/xml/test/camera.xml @@ -277,9 +277,9 @@ 16 R sensor_temperature - - formuu1 - formuu2 + + formuu1 + formuu2 enumm2 -- cgit v1.2.3 From 2dfb23016c39a331bf5ed4111b630dffa330edbb Mon Sep 17 00:00:00 2001 From: "nicolas.zilio@hotmail.fr" <> Date: Mon, 14 Sep 2015 11:56:38 +0200 Subject: views working fine, units in progress --- pcilib/CMakeLists.txt | 4 +-- pcilib/pci.c | 14 +++++--- pcilib/pci.h | 5 ++- pcilib/unit.c | 34 ++++++++++++++++++ pcilib/unit.h | 30 ++++++++++++++++ pcilib/views.c | 99 +++++++++++++++++++++++++++++---------------------- pcilib/views.h | 3 ++ pcilib/xml.c | 71 ++++++++++++++++++++++++++++++++++-- pcitool/cli.c | 67 ++++++++++++++++++++++++++++------ xml/test/camera.xml | 4 +-- 10 files changed, 267 insertions(+), 64 deletions(-) create mode 100644 pcilib/unit.c create mode 100644 pcilib/unit.h (limited to 'pcilib/pci.h') diff --git a/pcilib/CMakeLists.txt b/pcilib/CMakeLists.txt index 48363f6..1f3e646 100644 --- a/pcilib/CMakeLists.txt +++ b/pcilib/CMakeLists.txt @@ -9,9 +9,9 @@ include_directories( set(HEADERS pcilib.h pci.h export.h bar.h fifo.h model.h bank.h register.h views.h xml.h kmem.h irq.h locking.h lock.h dma.h event.h plugin.h tools.h error.h -debug.h env.h version.h config.h ) +debug.h env.h version.h config.h unit.h) add_library(pcilib SHARED pci.c export.c bar.c fifo.c model.c bank.c -register.c views.c xml.c kmem.c irq.c locking.c lock.c dma.c event.c plugin.c tools.c error.c debug.c env.c ) +register.c views.c xml.c kmem.c irq.c locking.c lock.c dma.c event.c plugin.c tools.c error.c debug.c env.c unit.c) target_link_libraries(pcilib dma protocols ${CMAKE_THREAD_LIBS_INIT} ${UFODECODE_LIBRARIES} ${CMAKE_DL_LIBS} ${EXTRA_SYSTEM_LIBS} ${LIBXML2_LIBRARIES} ${PYTHON_LIBRARIES}) diff --git a/pcilib/pci.c b/pcilib/pci.c index 8c178f6..2742240 100644 --- a/pcilib/pci.c +++ b/pcilib/pci.c @@ -145,19 +145,24 @@ pcilib_t *pcilib_open(const char *device, const char *model) { ctx->register_ctx = (pcilib_register_context_t *)malloc(PCILIB_DEFAULT_REGISTER_SPACE * sizeof(pcilib_register_context_t)); ctx->enum_views = (pcilib_view_enum2_t *)malloc(PCILIB_DEFAULT_VIEW_SPACE * sizeof(pcilib_view_enum2_t)); ctx->formula_views = (pcilib_view_formula_t*)malloc(PCILIB_DEFAULT_VIEW_SPACE * sizeof(pcilib_view_formula_t)); + ctx->alloc_units=PCILIB_DEFAULT_UNIT_SPACE; + ctx->units=(pcilib_unit_t*)malloc(PCILIB_DEFAULT_UNIT_SPACE * sizeof(pcilib_unit_t)); + + if ((!ctx->registers)||(!ctx->register_ctx)) { pcilib_error("Error allocating memory for register model"); pcilib_close(ctx); return NULL; } - if((!ctx->enum_views)||(!ctx->formula_views)){ - pcilib_error("Error allocating memory for views"); - pcilib_close(ctx); - return NULL; + /* i think we need a better error handling here, because, it's not that a problem to not have views working, but how to block the use if the memory here was not good?, and we could have only one type of views that is working*/ + if((!ctx->enum_views)||(!ctx->formula_views) || (!ctx->units)){ + pcilib_warning("Error allocating memory for views"); } + + memset(ctx->registers, 0, sizeof(pcilib_register_description_t)); memset(ctx->banks, 0, sizeof(pcilib_register_bank_description_t)); memset(ctx->ranges, 0, sizeof(pcilib_register_range_t)); @@ -166,6 +171,7 @@ pcilib_t *pcilib_open(const char *device, const char *model) { memset(ctx->enum_views,0,sizeof(pcilib_view_enum2_t)); memset(ctx->formula_views,0,sizeof(pcilib_view_formula_t)); + memset(ctx->units,0,sizeof(pcilib_unit_t)); for (i = 0; pcilib_protocols[i].api; i++); memcpy(ctx->protocols, pcilib_protocols, i * sizeof(pcilib_register_protocol_description_t)); diff --git a/pcilib/pci.h b/pcilib/pci.h index 3b1c0e8..a62e1cc 100644 --- a/pcilib/pci.h +++ b/pcilib/pci.h @@ -9,6 +9,7 @@ #define PCILIB_MAX_BARS 6 /**< this is defined by PCI specification */ #define PCILIB_DEFAULT_REGISTER_SPACE 1024 /**< number of registers to allocate on init */ #define PCILIB_DEFAULT_VIEW_SPACE 128 /**< number of views to allocate on init */ +#define PCILIB_DEFAULT_UNIT_SPACE 128 /** number of units to allocate on init*/ #define PCILIB_MAX_REGISTER_BANKS 32 /**< maximum number of register banks to allocate space for */ #define PCILIB_MAX_REGISTER_RANGES 32 /**< maximum number of register ranges to allocate space for */ #define PCILIB_MAX_REGISTER_PROTOCOLS 32 /**< maximum number of register protocols to support */ @@ -27,6 +28,7 @@ #include "export.h" #include "locking.h" #include "xml.h" +#include "unit.h" typedef struct { uint8_t max_link_speed, link_speed; @@ -65,7 +67,7 @@ struct pcilib_s { size_t dyn_banks; /**< Number of configured dynamic banks */ size_t num_enum_views,alloc_enum_views; /**< Number of configured and allocated views of type enum*/ size_t num_formula_views,alloc_formula_views; /**< Number of configured and allocated views of type formula*/ - + size_t num_units,alloc_units; /**< Number of configured and allocated units*/ pcilib_register_description_t *registers; /**< List of currently defined registers (from all sources) */ pcilib_register_bank_description_t banks[PCILIB_MAX_REGISTER_BANKS + 1]; /**< List of currently defined register banks (from all sources) */ pcilib_register_range_t ranges[PCILIB_MAX_REGISTER_RANGES + 1]; /**< List of currently defined register ranges (from all sources) */ @@ -87,6 +89,7 @@ struct pcilib_s { pcilib_view_enum2_t* enum_views; /**< list of currently defined views of type enum*/ pcilib_view_formula_t* formula_views; /**< list of currently defined views of type formula*/ + pcilib_unit_t* units; /** list of currently defined units*/ #ifdef PCILIB_FILE_IO int file_io_handle; #endif /* PCILIB_FILE_IO */ diff --git a/pcilib/unit.c b/pcilib/unit.c new file mode 100644 index 0000000..a9766ed --- /dev/null +++ b/pcilib/unit.c @@ -0,0 +1,34 @@ +#include "pcilib.h" +#include "pci.h" +#include "stdio.h" +#include +#include "error.h" +#include "unit.h" + +int pcilib_add_units(pcilib_t *ctx, size_t n, const pcilib_unit_t* units) { + + pcilib_unit_t *units2; + size_t size; + + if (!n) { + for (n = 0; units[n].name[0]; n++); + } + + if ((ctx->num_units + n + 1) > ctx->alloc_units) { + for (size = ctx->alloc_units; size < 2 * (n + ctx->num_units + 1); size<<=1); + + units2 = (pcilib_unit_t*)realloc(ctx->units, size * sizeof(pcilib_unit_t)); + if (!units2) return PCILIB_ERROR_MEMORY; + + ctx->units = units2; + ctx->alloc_units = size; + } + + memcpy(ctx->units + ctx->num_units, units, n * sizeof(pcilib_unit_t)); + memset(ctx->units + ctx->num_units + n, 0, sizeof(pcilib_unit_t)); + + ctx->num_units += n; + + return 0; +} + diff --git a/pcilib/unit.h b/pcilib/unit.h new file mode 100644 index 0000000..f9991f1 --- /dev/null +++ b/pcilib/unit.h @@ -0,0 +1,30 @@ +#ifndef _PCILIB_UNITS_H +#define _PCILIB_UNITS_H + +#include "pcilib.h" + +typedef struct pcilib_unit_s pcilib_unit_t; +typedef struct pcilib_transform_unit_s pcilib_transform_unit_t; + +/** + * type to save a transformation unit in the pcitool program + */ +struct pcilib_transform_unit_s{ + char *name; + char *transform_formula; +}; + +/** + * type to save a unit in the pcitool programm + */ +struct pcilib_unit_s{ + char* name; + pcilib_transform_unit_t* other_units; +}; + +/** + * function to populate the ctx with units + */ +int pcilib_add_units(pcilib_t* ctx, size_t n, const pcilib_unit_t* units); + +#endif diff --git a/pcilib/views.c b/pcilib/views.c index 038b48d..c9ad4d3 100644 --- a/pcilib/views.c +++ b/pcilib/views.c @@ -6,6 +6,7 @@ #include "error.h" #include #include +#include "unit.h" /** * @@ -81,7 +82,7 @@ pcilib_view_formula_replace (const char *txt, const char *before, const char *af * @param[in] end the ending index of the substring. * @return the extracted substring. */ -static char* +char* pcilib_view_str_sub (const char *s, unsigned int start, unsigned int end) { char *new_s = NULL; @@ -108,6 +109,39 @@ pcilib_view_str_sub (const char *s, unsigned int start, unsigned int end) return new_s; } +/** + * function to apply a unit for the views of type formula + *@param[in] view - the view we want to get the units supported + *@param[in] base_unit - the base unit of the formulas of the view + *@param[in] unit - the requested unit in which we want to get the value + *@param[in,out] value - the number that needs to get transformed + * +static int +pcilib_view_apply_unit(pcilib_view_formula_t* view, char* base_unit, char* unit,int* value){ + char* formula; + char temp[66]; + int i,j,k; + k=1; + /*we iterate through all the units of the given view to find the corresponding unit, and so the formula to transform it; then we evaluate value with the formula* + for(i=0;view->units[i].name[0];i++){ + if(!(strcasecmp(base_unit,view->units[i].name))){ + for(j=0;view->units[i].other_units[j].name[0];j++){ + if(!(strcasecmp(unit,view->units[i].other_units[j].name))){ + formula=malloc(strlen(units[i].other_units[j].transform_formula)*sizeof(char)); + strcpy(formula,units[i].other_units[j].transform_formula); + sprintf(temp,"%i",*value); + formula=pcilib_view_formula_replace(formula,"@self",temp); + *value=(int)pcilib_view_eval_formula(formula); + return 0; + } + } + } + } + + pcilib_error("no unit corresponds to the base unit asked"); + return PCILIB_ERROR_INVALID_REQUEST; +}*/ + /** * get the bank name associated with a register name */ @@ -162,46 +196,25 @@ pcilib_view_compute_plain_registers(pcilib_t* ctx, char* formula){ */ static pcilib_register_value_t pcilib_view_eval_formula(char* formula){ - setenv("PYTHONPATH",".",1); - PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *presult=NULL; - char* pythonfile; + + // setenv("PYTHONPATH",".",1); - /* path to the python file, we may need a way to set it, maybe with a variable like PCILIB_PYTHON_PATH*/ - pythonfile="pythonscripts.py"; - /* initialization of python interpreter*/ Py_Initialize(); - - pName = PyUnicode_FromString(pythonfile); - pModule = PyImport_Import(pName); /* get the python file*/ - if(!pModule) { - pcilib_error("no python file found for python evaluation of formulas\n"); - PyErr_Print(); - return -1; - } - pDict = PyModule_GetDict(pModule); /*useless but needed*/ - - pFunc = PyDict_GetItemString(pDict, (char*)"evaluate"); /*getting of the function "evaluate" in the script*/ - /* if the function is ok, then call it*/ - if (PyCallable_Check(pFunc)) - { - /* execution of the function*/ - pValue=Py_BuildValue("(z)",formula); - PyErr_Print(); - presult=PyObject_CallObject(pFunc,pValue); - PyErr_Print(); - } else - { - PyErr_Print(); - } - /* remove memory*/ - Py_DECREF(pModule); - Py_DECREF(pName); + /*compilation of the formula as python string*/ + PyCodeObject* code=(PyCodeObject*)Py_CompileString(formula,"test",Py_eval_input); + PyObject* main_module = PyImport_AddModule("__parser__"); + PyObject* global_dict = PyModule_GetDict(main_module); + PyObject* local_dict = PyDict_New(); + /*evaluation of formula*/ + PyObject* obj = PyEval_EvalCode(code, global_dict, local_dict); + double c=PyFloat_AsDouble(obj); + /* close interpreter*/ Py_Finalize(); - - return (pcilib_register_value_t)PyLong_AsUnsignedLong(presult); /*this function is due to python 3.3, as PyLong_AsInt was there in python 2.7 and is no more there, resulting in using cast futhermore*/ + pcilib_register_value_t value=(pcilib_register_value_t)c; + return value; } @@ -227,7 +240,6 @@ pcilib_view_apply_formula(pcilib_t* ctx, char* formula, pcilib_register_value_t formula=pcilib_view_compute_plain_registers(ctx,formula); /* computation of @reg with register value*/ formula=pcilib_view_formula_replace(formula,"@reg",reg_value_string); - /* evaluation of the formula*/ *out_value= pcilib_view_eval_formula(formula); @@ -246,8 +258,10 @@ int pcilib_read_view(pcilib_t *ctx, const char *bank, const char *regname, const } /* we get the value of the register, as we will apply the view on it*/ - if((err==pcilib_read_register_by_id(ctx,i,&temp_value))>0){ - pcilib_error("can't read the register %s value before applying views",regname); + err=pcilib_read_register_by_id(ctx,i,&temp_value); + if(err){ + pcilib_error("can't read the register %s value before applying views : error %i",regname); + return PCILIB_ERROR_INVALID_REQUEST; } /*in the case we don't ask for a view's name, we know it will be for views of type enum. Especially, it's faster to search directly on the values of those views instead of a given name. we iterate so through the views of type enum to verify if the value we have corresponds to an enum command*/ @@ -255,13 +269,15 @@ int pcilib_read_view(pcilib_t *ctx, const char *bank, const char *regname, const for(j=0; ctx->register_ctx[i].enums[j].value;j++){ if((temp_value >= ctx->register_ctx[i].enums[j].min) && (temp_value <= ctx->register_ctx[i].enums[j].max)){ value_size=strlen(ctx->register_ctx[i].enums[j].name)*sizeof(char); - value=malloc(sizeof(value_size)); + value=(char*)realloc(value,sizeof(value_size)); if(!(value)){ pcilib_error("can't allocate memory for the returning value of the view %s",view); return PCILIB_ERROR_MEMORY; } /* in the case the value of register is between min and max, then we return the correponding enum command*/ strncpy((char*)value,ctx->register_ctx[i].enums[j].name, strlen(ctx->register_ctx[i].enums[j].name)); + /* make sure the string is correctly terminated*/ + ((char*)value)[value_size]='\0'; return 0; } } @@ -271,8 +287,7 @@ int pcilib_read_view(pcilib_t *ctx, const char *bank, const char *regname, const /** in the other case we ask for a view of type formula. Indeed, wa can't directly ask for a formula, so we have to provide a name for those views*/ j=0; - while((ctx->register_ctx[i].formulas[j].name)){ - if(!(strcasecmp(ctx->register_ctx[i].formulas[j].name,view))){ + if(!(strcasecmp(ctx->register_ctx[i].formulas[0].name,view))){ /* when we have found the correct view of type formula, we apply the formula, that get the good value for return*/ formula=malloc(sizeof(char)*strlen(ctx->register_ctx[i].formulas[j].read_formula)); strncpy(formula,ctx->register_ctx[i].formulas[j].read_formula,strlen(ctx->register_ctx[i].formulas[j].read_formula)); @@ -281,8 +296,6 @@ int pcilib_read_view(pcilib_t *ctx, const char *bank, const char *regname, const value_size=sizeof(int); return 0; } - j++; - } pcilib_warning("the view asked and the register do not correspond"); return PCILIB_ERROR_NOTAVAILABLE; diff --git a/pcilib/views.h b/pcilib/views.h index dfc9f70..f186336 100644 --- a/pcilib/views.h +++ b/pcilib/views.h @@ -2,6 +2,7 @@ #define _PCILIB_VIEWS_H #include "pcilib.h" +#include "unit.h" typedef struct pcilib_view_enum_s pcilib_view_enum_t; @@ -37,6 +38,7 @@ struct pcilib_view_formula_s { const char *write_formula; /**units[0].other_units.name[0];i++){ + for(j=0;ctx->units[j].name[0];i++){ + if(!(strcasecmp(myview->units[0].other_units.name,ctx->units[i].name))){ + myview.units=realloc(myview.units,k*sizeof(pcilib_unit_t)); + myview.units[k-1]=ctx->units[i]; + k++; + } + } + } + }*/ + /** * get the associated views of a register, to fill its register context */ @@ -490,7 +507,52 @@ static int pcilib_xml_create_bank(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo return 0; } +/*static int pcilib_xml_create_unit(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node) { + int err; + + int override = 0; + pcilib_unit_t desc = {0}; + xmlNodePtr cur; + char *value, *name, *value2; + char *endptr; + xmlXPathObjectPtr nodes; + xmlNodeSetPtr nodeset; + xmlAttr *attr; + int i=0; + + /* we get the attribute type of the view node* + attr=node->properties; + value=(char*)attr->children->content; + desc.name=value; + desc.other_units=malloc(sizeof(pcilib_transform_unit_t)); + + for (cur = node->children; cur != NULL; cur = cur->next) { + if (!cur->children) continue; + if (!xmlNodeIsText(cur->children)) continue; + + name = (char*)cur->name; + value = (char*)cur->children->content; + attr= cur->properties; + value2=(char*)attr->children->content; + if (!value || !attr) continue; + + if (!strcasecmp(name, "convert_unit")) { + desc.other_units=realloc(des.other_units,sizeof((i+1)*sizeof(pcilib_transform_unit_t))); + desc.other_units[i].name=value2; + desc.other_units[i].transform_formula=value; + } + } + + err = pcilib_add_units(ctx, 1, &desc); + if (err) { + pcilib_error("Error adding unit (%s) specified in the XML", desc.name); + return err; + } + + return 0; +} +*/ /** * function that create a view from a view node, and populate ctx views list */ @@ -505,6 +567,7 @@ static int pcilib_xml_create_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo char *endptr; xmlAttr *attr; int i=0; + int ok_min=0, ok_max=0; /*must i initialize? i think it's only needed if we want to include a description property*/ enum_desc.name="default"; @@ -573,15 +636,19 @@ static int pcilib_xml_create_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo return PCILIB_ERROR_INVALID_DATA; } complete_enum_desc.enums_list[i].min=dat_min; + ok_min=1; }else if(!(strcasecmp(name,"max"))){ pcilib_register_value_t dat_max = strtol(value, &endptr, 0); if ((strlen(endptr) > 0)) { pcilib_error("Invalid max (%s) is specified in the XML enum node", value); return PCILIB_ERROR_INVALID_DATA; } - complete_enum_desc.enums_list[i].max=dat_max; + ok_max=1; } + if(ok_min==0) complete_enum_desc.enums_list[i].min=complete_enum_desc.enums_list[i].value; + if(ok_max==0) complete_enum_desc.enums_list[i].max=complete_enum_desc.enums_list[i].value; + } i++; } diff --git a/pcitool/cli.c b/pcitool/cli.c index 7014190..6af70e4 100644 --- a/pcitool/cli.c +++ b/pcitool/cli.c @@ -1024,13 +1024,14 @@ int ReadRegister(pcilib_t *handle, const pcilib_model_description_t *model_info, int i; int err; const char *format; + char *s1,*s2,*s3; + pcilib_register_bank_t bank_id; pcilib_register_bank_addr_t bank_addr = 0; pcilib_register_value_t value; - - if (reg) { + if (reg && !(strchr(reg,'/'))) { pcilib_register_t regid = pcilib_find_register(handle, bank, reg); bank_id = pcilib_find_register_bank_by_addr(handle, model_info->registers[regid].bank); format = model_info->banks[bank_id].format; @@ -1044,7 +1045,36 @@ int ReadRegister(pcilib_t *handle, const pcilib_model_description_t *model_info, printf(format, value); printf("\n"); } + }else if(reg && (s1=strchr(reg,'/'))){ + char* enum_command=malloc(sizeof(char*)); + if(!enum_command){ + printf("Error allocating memory for the result\n"); + return PCILIB_ERROR_MEMORY; + } + s2=pcilib_view_str_sub(reg,0,s1-reg-1); + s3=pcilib_view_str_sub(reg,s1-reg+1,strlen(reg)); + if(!(strcasecmp(s3,"name"))){ + err = pcilib_read_view(handle,bank,s2,NULL,sizeof(char*),enum_command); + if (err) printf("Error reading register %s\n", reg); + else { + printf("%s = %s\n", reg, (char*)enum_command); + } + }else{ + pcilib_register_t regid = pcilib_find_register(handle, bank, s2); + bank_id = pcilib_find_register_bank_by_addr(handle, model_info->registers[regid].bank); + format = model_info->banks[bank_id].format; + if (!format) format = "%lu"; + + err = pcilib_read_view(handle,bank,s2,s3,sizeof(pcilib_register_value_t),&value); + if (err) printf("Error reading register %s\n", reg); + else { + printf("%s = ", reg); + printf(format, value); + printf("\n"); + } + } } else { + printf("da\n"); // Adding DMA registers pcilib_get_dma_description(handle); @@ -1254,6 +1284,7 @@ int WriteRegister(pcilib_t *handle, const pcilib_model_description_t *model_info pcilib_register_value_t value; const char *format = NULL; + char *s1; pcilib_register_t regid = pcilib_find_register(handle, bank, reg); if (regid == PCILIB_REGISTER_INVALID) Error("Can't find register (%s) from bank (%s)", reg, bank?bank:"autodetected"); @@ -1281,14 +1312,27 @@ int WriteRegister(pcilib_t *handle, const pcilib_model_description_t *model_info format = "0x%lx"; } else { - Error("Can't parse data value (%s) is not valid decimal number", *data); + err = pcilib_write_view(handle,bank,reg,*data,0,NULL); + if(err) Error("can't write to the register using an enum view"); + else return 0; } + /* } else { + Error("Can't parse data value (%s) is not valid decimal number", *data); + }*/ value = val; - - err = pcilib_write_register(handle, bank, reg, value); - if (err) Error("Error writting register %s\n", reg); + if((s1=strchr(reg,'/'))){ + char *s3,*s2; + s2=pcilib_view_str_sub(reg,0,s1-reg-1); + s3=pcilib_view_str_sub(reg,s1-reg+1,strlen(reg)); + err = pcilib_write_view(handle,bank,s2,s3,sizeof(pcilib_register_value_t),&value); + if (err) printf("Error writing register %s using view %s\n",s2,s3); + }else{ + err = pcilib_write_register(handle, bank, reg, value); + if (err) Error("Error writting register %s\n", reg); + } + if ((model_info->registers[regid].mode&PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW) { err = pcilib_read_register(handle, bank, reg, &value); if (err) Error("Error reading back register %s for verification\n", reg); @@ -3249,7 +3293,10 @@ int main(int argc, char **argv) { ++mode; } } - } else { + } else if(strchr(addr,'/')) { + reg=addr; + ++mode; + }else { if (pcilib_find_register(handle, bank, addr) == PCILIB_REGISTER_INVALID) { Usage(argc, argv, "Invalid address (%s) is specified", addr); } else { @@ -3349,12 +3396,12 @@ int main(int argc, char **argv) { } else if (addr) { err = ReadData(handle, amode, flags, dma, bar, start, size, access, endianess, (size_t)-1, ofile); } else { - Error("Address to read is not specified"); + Error("Address to read is not specified"); } break; case MODE_READ_REGISTER: - if ((reg)||(!addr)) ReadRegister(handle, model_info, bank, reg); - else ReadRegisterRange(handle, model_info, bank, start, addr_shift, size, ofile); + if ((reg)||(!addr)) ReadRegister(handle, model_info, bank, reg); + else ReadRegisterRange(handle, model_info, bank, start, addr_shift, size, ofile); break; case MODE_WRITE: WriteData(handle, amode, dma, bar, start, size, access, endianess, data, verify); diff --git a/xml/test/camera.xml b/xml/test/camera.xml index 815a50e..bf26d80 100644 --- a/xml/test/camera.xml +++ b/xml/test/camera.xml @@ -466,14 +466,14 @@ formuu2 C - ((1./4)*(@reg - 1200)) if @freq==0 else ((3./10)*(@reg - 1000)) + ((1./4)*(@reg + 1200)) if @freq==0 else ((3./10)*(@reg + 1000)) 4*@value + 1200 if @freq==0 else (10./3)*@value + 1000 formula to get real sensor temperature from the sensor_temperature register in decimal enumm2 high - low + low enum towards sensor_temperature register -- cgit v1.2.3 From de589562bd91cc60ee3e2d739bdd7a03063d38f7 Mon Sep 17 00:00:00 2001 From: "nicolas.zilio@hotmail.fr" <> Date: Mon, 14 Sep 2015 18:18:00 +0200 Subject: first try with pcilib_operation_t --- pcilib/pci.c | 7 ++-- pcilib/pci.h | 7 ++-- pcilib/views.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- pcilib/views.h | 19 +++++++++- 4 files changed, 132 insertions(+), 8 deletions(-) (limited to 'pcilib/pci.h') diff --git a/pcilib/pci.c b/pcilib/pci.c index 2742240..946b003 100644 --- a/pcilib/pci.c +++ b/pcilib/pci.c @@ -140,16 +140,16 @@ pcilib_t *pcilib_open(const char *device, const char *model) { ctx->alloc_reg = PCILIB_DEFAULT_REGISTER_SPACE; ctx->alloc_formula_views=PCILIB_DEFAULT_VIEW_SPACE; + ctx->alloc_views=PCILIB_DEFAULT_VIEW_SPACE; ctx->alloc_enum_views=PCILIB_DEFAULT_VIEW_SPACE; ctx->registers = (pcilib_register_description_t *)malloc(PCILIB_DEFAULT_REGISTER_SPACE * sizeof(pcilib_register_description_t)); ctx->register_ctx = (pcilib_register_context_t *)malloc(PCILIB_DEFAULT_REGISTER_SPACE * sizeof(pcilib_register_context_t)); ctx->enum_views = (pcilib_view_enum2_t *)malloc(PCILIB_DEFAULT_VIEW_SPACE * sizeof(pcilib_view_enum2_t)); ctx->formula_views = (pcilib_view_formula_t*)malloc(PCILIB_DEFAULT_VIEW_SPACE * sizeof(pcilib_view_formula_t)); + ctx->views = (pcilib_view_t*)malloc(PCILIB_DEFAULT_VIEW_SPACE * sizeof(pcilib_view_t)); ctx->alloc_units=PCILIB_DEFAULT_UNIT_SPACE; ctx->units=(pcilib_unit_t*)malloc(PCILIB_DEFAULT_UNIT_SPACE * sizeof(pcilib_unit_t)); - - if ((!ctx->registers)||(!ctx->register_ctx)) { pcilib_error("Error allocating memory for register model"); pcilib_close(ctx); @@ -157,7 +157,7 @@ pcilib_t *pcilib_open(const char *device, const char *model) { } /* i think we need a better error handling here, because, it's not that a problem to not have views working, but how to block the use if the memory here was not good?, and we could have only one type of views that is working*/ - if((!ctx->enum_views)||(!ctx->formula_views) || (!ctx->units)){ + if((!ctx->enum_views)||(!ctx->formula_views) || (!ctx->units) || !(ctx->views)){ pcilib_warning("Error allocating memory for views"); } @@ -172,6 +172,7 @@ pcilib_t *pcilib_open(const char *device, const char *model) { memset(ctx->enum_views,0,sizeof(pcilib_view_enum2_t)); memset(ctx->formula_views,0,sizeof(pcilib_view_formula_t)); memset(ctx->units,0,sizeof(pcilib_unit_t)); + memset(ctx->views,0,sizeof(pcilib_view_t)); for (i = 0; pcilib_protocols[i].api; i++); memcpy(ctx->protocols, pcilib_protocols, i * sizeof(pcilib_register_protocol_description_t)); diff --git a/pcilib/pci.h b/pcilib/pci.h index a62e1cc..d5f1811 100644 --- a/pcilib/pci.h +++ b/pcilib/pci.h @@ -67,6 +67,7 @@ struct pcilib_s { size_t dyn_banks; /**< Number of configured dynamic banks */ size_t num_enum_views,alloc_enum_views; /**< Number of configured and allocated views of type enum*/ size_t num_formula_views,alloc_formula_views; /**< Number of configured and allocated views of type formula*/ + size_t num_views,alloc_views; /**< Number of configured and allocated views*/ size_t num_units,alloc_units; /**< Number of configured and allocated units*/ pcilib_register_description_t *registers; /**< List of currently defined registers (from all sources) */ pcilib_register_bank_description_t banks[PCILIB_MAX_REGISTER_BANKS + 1]; /**< List of currently defined register banks (from all sources) */ @@ -86,9 +87,9 @@ struct pcilib_s { struct pcilib_locking_s locks; /**< Context of locking subsystem */ struct pcilib_xml_s xml; /**< XML context */ - pcilib_view_enum2_t* enum_views; /**< list of currently defined views of type enum*/ - pcilib_view_formula_t* formula_views; /**< list of currently defined views of type formula*/ - + pcilib_view_enum2_t* enum_views; /**< list of currently defined views of type enum*/ + pcilib_view_formula_t* formula_views; /**< list of currently defined views of type formula*/ + pcilib_view_t* views; /**< list of currently defined views*/ pcilib_unit_t* units; /** list of currently defined units*/ #ifdef PCILIB_FILE_IO int file_io_handle; diff --git a/pcilib/views.c b/pcilib/views.c index 1f26b52..9dde46b 100644 --- a/pcilib/views.c +++ b/pcilib/views.c @@ -363,6 +363,84 @@ int pcilib_write_view(pcilib_t *ctx, const char *bank, const char *regname, cons return PCILIB_ERROR_NOTAVAILABLE; } +int operation_enum(pcilib_t *ctx, void *params/*name*/, char* name, int read_or_write, pcilib_register_value_t *regval, size_t viewval_size, void* viewval){ + int j,k; + if(read_or_write==1){ + for(j=0; ((pcilib_view_enum_t*)(params))[j].name;j++){ + if(!(strcasecmp(((pcilib_view_enum_t*)(params))[j].name,(char*)viewval))){ + return j; + } + } + }else if (read_or_write==0){ + for(j=0; ((pcilib_view_enum_t*)(params))[j].name;j++){ + if (*regval<((pcilib_view_enum_t*)(params))[j].max && *regval>((pcilib_view_enum_t*)(params))[j].min){ + viewval=(char*)realloc(viewval,strlen(((pcilib_view_enum_t*)(params))[j].name)*sizeof(char)); + strncpy((char*)viewval,((pcilib_view_enum_t*)(params))[j].name, strlen(((pcilib_view_enum_t*)(params))[j].name)); + k=strlen(((pcilib_view_enum_t*)(params))[j].name); + ((char*)regval)[k]='\0'; + return 0; + } + } + } + return -1; +} + +int operation_formula(pcilib_t *ctx, void *params/*name*/, char* unit, int read_or_write, pcilib_register_value_t *regval, size_t viewval_size, void* viewval){ + int j=0; + char* formula; + pcilib_register_value_t value=0; + + if(read_or_write==0){ + if(!(strcasecmp(unit, ((pcilib_view_t*)viewval)->base_unit.name))){ + formula=malloc(sizeof(char)*strlen(((pcilib_formula_t*)params)->read_formula)); + if(!(formula)){ + pcilib_error("can't allocate memory for the formula"); + return PCILIB_ERROR_MEMORY; + } + strncpy(formula,((pcilib_formula_t*)params)->read_formula,strlen(((pcilib_formula_t*)params)->read_formula)); + pcilib_view_apply_formula(ctx,formula,*regval,&value,0); + return value; + } + + for(j=0; ((pcilib_view_t*)viewval)->base_unit.other_units[j].name;j++){ + if(!(strcasecmp(((pcilib_view_t*)viewval)->base_unit.other_units[j].name,unit))){ + /* when we have found the correct view of type formula, we apply the formula, that get the good value for return*/ + formula=malloc(sizeof(char)*strlen(((pcilib_formula_t*)params)->read_formula)); + if(!(formula)){ + pcilib_error("can't allocate memory for the formula"); + return PCILIB_ERROR_MEMORY; + } + strncpy(formula,((pcilib_formula_t*)params)->read_formula,strlen(((pcilib_formula_t*)params)->read_formula)); + pcilib_view_apply_formula(ctx,formula, *regval,&value,0); + pcilib_view_apply_unit(((pcilib_view_t*)viewval)->base_unit.other_units[j],unit,&value); + return value; + } + } + }else if(read_or_write==1){ + j=0; + if(!(strcasecmp(unit, ((pcilib_view_t*)viewval)->base_unit.name))){ + formula=malloc(sizeof(char)*strlen(((pcilib_formula_t*)params)->write_formula)); + strncpy(formula,((pcilib_formula_t*)params)->write_formula,strlen(((pcilib_formula_t*)params)->write_formula)); + pcilib_view_apply_formula(ctx,formula,*regval,&value,1); + return 0; + } + + for(j=0;((pcilib_view_t*)viewval)->base_unit.other_units[j].name;j++){ + if(!(strcasecmp(((pcilib_view_t*)viewval)->base_unit.other_units[j].name,unit))){ + /* when we have found the correct view of type formula, we apply the formula, that get the good value for return*/ + formula=malloc(sizeof(char)*strlen(((pcilib_formula_t*)params)->write_formula)); + strncpy(formula,((pcilib_formula_t*)params)->write_formula,strlen((( pcilib_formula_t*)params)->write_formula)); + pcilib_view_apply_unit(((pcilib_view_t*)viewval)->base_unit.other_units[j],unit,&value); + pcilib_view_apply_formula(ctx,formula,*regval,&value,1); + /* we maybe need some error checking there , like temp_value >min and formula_views + ctx->num_formula_views + n, 0, sizeof(pcilib_view_formula_t)); ctx->num_formula_views += n; - + return 0; +} + +/** + * function to populate ctx views, as we could do for registers or banks + */ +int pcilib_add_views(pcilib_t *ctx, size_t n, const pcilib_view_t* views) { + + pcilib_view_t *views2; + size_t size; + + if (!n) { + for (n = 0; views[n].name; n++); + } + + if ((ctx->num_views + n + 1) > ctx->alloc_views) { + for (size = ctx->alloc_views; size < 2 * (n + ctx->num_views + 1); size<<=1); + + views2 = (pcilib_view_t*)realloc(ctx->views, size * sizeof(pcilib_view_t)); + if (!views2) return PCILIB_ERROR_MEMORY; + + ctx->views = views2; + ctx->alloc_views = size; + } + + memcpy(ctx->views + ctx->num_views, views, n * sizeof(pcilib_view_t)); + memset(ctx->views + ctx->num_views + n, 0, sizeof(pcilib_view_t)); + ctx->num_views += n; return 0; } diff --git a/pcilib/views.h b/pcilib/views.h index 33bcf4c..c7dee6b 100644 --- a/pcilib/views.h +++ b/pcilib/views.h @@ -10,6 +10,12 @@ typedef struct pcilib_view_formula_s pcilib_view_formula_t; typedef struct pcilib_view_enum2_s pcilib_view_enum2_t; +typedef struct pcilib_view_s pcilib_view_t; + +typedef struct pcilib_formula_s pcilib_formula_t; + +typedef int (*pcilib_view_operation_t)(pcilib_t *ctx, void *params, char* string, int read_or_write, pcilib_register_value_t *regval, size_t viewval_size, void* viewval); + /** * new type to define an enum view */ @@ -18,6 +24,10 @@ struct pcilib_view_enum_s { pcilib_register_value_t value, min, max; }; +struct pcilib_formula_s{ + char* read_formula; + char* write_formula; +}; /** * complete type for an enum view : name will be changed after with the previous one @@ -28,7 +38,14 @@ struct pcilib_view_enum2_s { const char* description; }; - +struct pcilib_view_s{ + const char* name; + const char* description; + pcilib_view_operation_t op; + void* parameters; + pcilib_unit_t base_unit; +}; + /** * new type to define a formula view */ -- cgit v1.2.3 From 6c47064466d8484813741f347e41af1d4ac90f85 Mon Sep 17 00:00:00 2001 From: "nicolas.zilio@hotmail.fr" <> Date: Tue, 15 Sep 2015 11:45:37 +0200 Subject: cleaning --- pcilib/pci.c | 8 +--- pcilib/pci.h | 5 +-- pcilib/register.h | 8 ++-- pcilib/views.c | 107 ------------------------------------------------------ pcilib/views.h | 30 --------------- 5 files changed, 5 insertions(+), 153 deletions(-) (limited to 'pcilib/pci.h') diff --git a/pcilib/pci.c b/pcilib/pci.c index 946b003..b37e817 100644 --- a/pcilib/pci.c +++ b/pcilib/pci.c @@ -139,13 +139,9 @@ pcilib_t *pcilib_open(const char *device, const char *model) { } ctx->alloc_reg = PCILIB_DEFAULT_REGISTER_SPACE; - ctx->alloc_formula_views=PCILIB_DEFAULT_VIEW_SPACE; ctx->alloc_views=PCILIB_DEFAULT_VIEW_SPACE; - ctx->alloc_enum_views=PCILIB_DEFAULT_VIEW_SPACE; ctx->registers = (pcilib_register_description_t *)malloc(PCILIB_DEFAULT_REGISTER_SPACE * sizeof(pcilib_register_description_t)); ctx->register_ctx = (pcilib_register_context_t *)malloc(PCILIB_DEFAULT_REGISTER_SPACE * sizeof(pcilib_register_context_t)); - ctx->enum_views = (pcilib_view_enum2_t *)malloc(PCILIB_DEFAULT_VIEW_SPACE * sizeof(pcilib_view_enum2_t)); - ctx->formula_views = (pcilib_view_formula_t*)malloc(PCILIB_DEFAULT_VIEW_SPACE * sizeof(pcilib_view_formula_t)); ctx->views = (pcilib_view_t*)malloc(PCILIB_DEFAULT_VIEW_SPACE * sizeof(pcilib_view_t)); ctx->alloc_units=PCILIB_DEFAULT_UNIT_SPACE; ctx->units=(pcilib_unit_t*)malloc(PCILIB_DEFAULT_UNIT_SPACE * sizeof(pcilib_unit_t)); @@ -157,7 +153,7 @@ pcilib_t *pcilib_open(const char *device, const char *model) { } /* i think we need a better error handling here, because, it's not that a problem to not have views working, but how to block the use if the memory here was not good?, and we could have only one type of views that is working*/ - if((!ctx->enum_views)||(!ctx->formula_views) || (!ctx->units) || !(ctx->views)){ + if((!ctx->units) || !(ctx->views)){ pcilib_warning("Error allocating memory for views"); } @@ -169,8 +165,6 @@ pcilib_t *pcilib_open(const char *device, const char *model) { memset(ctx->register_ctx, 0, PCILIB_DEFAULT_REGISTER_SPACE * sizeof(pcilib_register_context_t)); - memset(ctx->enum_views,0,sizeof(pcilib_view_enum2_t)); - memset(ctx->formula_views,0,sizeof(pcilib_view_formula_t)); memset(ctx->units,0,sizeof(pcilib_unit_t)); memset(ctx->views,0,sizeof(pcilib_view_t)); diff --git a/pcilib/pci.h b/pcilib/pci.h index d5f1811..8805fc2 100644 --- a/pcilib/pci.h +++ b/pcilib/pci.h @@ -65,8 +65,6 @@ struct pcilib_s { size_t num_banks, num_protocols, num_ranges; /**< Number of registered banks, protocols, and register ranges */ size_t num_engines; /**< Number of configured DMA engines */ size_t dyn_banks; /**< Number of configured dynamic banks */ - size_t num_enum_views,alloc_enum_views; /**< Number of configured and allocated views of type enum*/ - size_t num_formula_views,alloc_formula_views; /**< Number of configured and allocated views of type formula*/ size_t num_views,alloc_views; /**< Number of configured and allocated views*/ size_t num_units,alloc_units; /**< Number of configured and allocated units*/ pcilib_register_description_t *registers; /**< List of currently defined registers (from all sources) */ @@ -87,10 +85,9 @@ struct pcilib_s { struct pcilib_locking_s locks; /**< Context of locking subsystem */ struct pcilib_xml_s xml; /**< XML context */ - pcilib_view_enum2_t* enum_views; /**< list of currently defined views of type enum*/ - pcilib_view_formula_t* formula_views; /**< list of currently defined views of type formula*/ pcilib_view_t* views; /**< list of currently defined views*/ pcilib_unit_t* units; /** list of currently defined units*/ + #ifdef PCILIB_FILE_IO int file_io_handle; #endif /* PCILIB_FILE_IO */ diff --git a/pcilib/register.h b/pcilib/register.h index 2f6334c..13d4c9b 100644 --- a/pcilib/register.h +++ b/pcilib/register.h @@ -45,11 +45,9 @@ typedef struct { typedef struct { - pcilib_register_bank_t bank; /**< Reference to bank containing the register */ - pcilib_register_value_t min, max; /**< Minimum & maximum allowed values */ - pcilib_xml_node_t *xml; /**< Additional XML properties */ - pcilib_view_formula_t *formulas; /**< list of views of type formula linked to this register*/ - pcilib_view_enum_t *enums; /**< list of views of type enum linked to this register*/ + pcilib_register_bank_t bank; /**< Reference to bank containing the register */ + pcilib_register_value_t min, max; /**< Minimum & maximum allowed values */ + pcilib_xml_node_t *xml; /**< Additional XML properties */ pcilib_view_t *views; /** list of views linked to this register*/ } pcilib_register_context_t; diff --git a/pcilib/views.c b/pcilib/views.c index 86675f5..d0116db 100644 --- a/pcilib/views.c +++ b/pcilib/views.c @@ -8,41 +8,6 @@ #include #include "unit.h" -/** - * function used to get the substring of a string s, from the starting and ending indexes - * @param[in] s string containing the substring we want to extract. - * @param[in] start the start index of the substring. - * @param[in] end the ending index of the substring. - * @return the extracted substring. - */ -char* -pcilib_view_str_sub (const char *s, unsigned int start, unsigned int end) -{ - char *new_s = NULL; - - if (s != NULL && start < end) - { - new_s = malloc (sizeof (*new_s) * (end - start + 2)); - if (new_s != NULL) - { - int i; - - for (i = start; i <= end; i++) - { - new_s[i-start] = s[i]; - } - new_s[i-start] = '\0'; - } - else - { - pcilib_error("insufficient memory for string manipulation\n"); - return NULL; - } - } - return new_s; -} - - /** * this function calls the python script and the function "evaluate" in it to evaluate the given formula *@param[in] the formula to be evaluated @@ -122,17 +87,6 @@ specified one. Add it to the register*/ static int pcilib_view_apply_formula(pcilib_t* ctx, char* formula, pcilib_register_value_t* reg_value) { - /* when applying a formula, we need to: - 1) compute the values of all registers present in plain name in the formulas and replace their name with their value : for example, if we have the formula" ((1./4)*(@reg - 1200)) if @freq==0 else ((3./10)*(@reg - 1000)) " we need to get the value of the register "freq" - 2) compute the "@reg" component, corresponding to the raw value of the register - 3) pass the formula to python interpreter for evaluation - 4) return the final evaluation - - remark: it might be worth to use python regular expression interpreter (the following return all words following @ in formula : - >>> import re - >>> m = re.search('(?<=@)\w+', formula) - >>> m.group(0) - */ char reg_value_string[66]; /* to register reg_value as a string, need to check the length*/ sprintf(reg_value_string,"%u",*reg_value); @@ -334,67 +288,6 @@ int operation_formula(pcilib_t *ctx, void *params, char* unit, int view2reg, pci } -/** - * function to populate ctx enum views, as we could do for registers or banks - */ -int pcilib_add_views_enum(pcilib_t *ctx, size_t n, const pcilib_view_enum2_t* views) { - - pcilib_view_enum2_t *views_enum; - size_t size; - - if (!n) { - for (n = 0; views[n].enums_list[0].value; n++); - } - - if ((ctx->num_enum_views + n + 1) > ctx->alloc_enum_views) { - for (size = ctx->alloc_enum_views; size < 2 * (n + ctx->num_enum_views + 1); size<<=1); - - views_enum = (pcilib_view_enum2_t*)realloc(ctx->enum_views, size * sizeof(pcilib_view_enum2_t)); - if (!views_enum) return PCILIB_ERROR_MEMORY; - - ctx->enum_views = views_enum; - ctx->alloc_enum_views = size; - } - - memcpy(ctx->enum_views + ctx->num_enum_views, views, n * sizeof(pcilib_view_enum2_t)); - memset(ctx->enum_views + ctx->num_enum_views + n, 0, sizeof(pcilib_view_enum2_t)); - - ctx->num_enum_views += n; - - - return 0; -} - - -/** - * function to populate ctx formula views, as we could do for registers or banks - */ -int pcilib_add_views_formula(pcilib_t *ctx, size_t n, const pcilib_view_formula_t* views) { - - pcilib_view_formula_t *views_formula; - size_t size; - - if (!n) { - for (n = 0; views[n].name; n++); - } - - if ((ctx->num_formula_views + n + 1) > ctx->alloc_formula_views) { - for (size = ctx->alloc_formula_views; size < 2 * (n + ctx->num_formula_views + 1); size<<=1); - - views_formula = (pcilib_view_formula_t*)realloc(ctx->formula_views, size * sizeof(pcilib_view_formula_t)); - if (!views_formula) return PCILIB_ERROR_MEMORY; - - ctx->formula_views = views_formula; - ctx->alloc_formula_views = size; - } - - memcpy(ctx->formula_views + ctx->num_formula_views, views, n * sizeof(pcilib_view_formula_t)); - memset(ctx->formula_views + ctx->num_formula_views + n, 0, sizeof(pcilib_view_formula_t)); - - ctx->num_formula_views += n; - return 0; -} - /** * function to populate ctx views, as we could do for registers or banks */ diff --git a/pcilib/views.h b/pcilib/views.h index 5d9100e..ae92d0d 100644 --- a/pcilib/views.h +++ b/pcilib/views.h @@ -6,10 +6,6 @@ typedef struct pcilib_view_enum_s pcilib_view_enum_t; -typedef struct pcilib_view_formula_s pcilib_view_formula_t; - -typedef struct pcilib_view_enum2_s pcilib_view_enum2_t; - typedef struct pcilib_view_s pcilib_view_t; typedef struct pcilib_formula_s pcilib_formula_t; @@ -29,15 +25,6 @@ struct pcilib_formula_s{ char* write_formula; }; -/** - * complete type for an enum view : name will be changed after with the previous one - */ -struct pcilib_view_enum2_s { - const char* name; - pcilib_view_enum_t* enums_list; - const char* description; -}; - struct pcilib_view_s{ const char* name; const char* description; @@ -45,17 +32,6 @@ struct pcilib_view_s{ void* parameters; pcilib_unit_t base_unit; }; -/** - * new type to define a formula view - */ -struct pcilib_view_formula_s { - const char *name; /** Date: Tue, 15 Sep 2015 12:07:04 +0200 Subject: merge views and units, some name homogeneisation --- pcilib/CMakeLists.txt | 4 ++-- pcilib/pci.h | 2 +- pcilib/unit.c | 34 ------------------------------- pcilib/unit.h | 32 ----------------------------- pcilib/views.c | 56 +++++++++++++++++++++++++++++++++++++-------------- pcilib/views.h | 35 +++++++++++++++++++++++++++++--- pcilib/xml.c | 24 +++++++++++----------- 7 files changed, 88 insertions(+), 99 deletions(-) delete mode 100644 pcilib/unit.c delete mode 100644 pcilib/unit.h (limited to 'pcilib/pci.h') diff --git a/pcilib/CMakeLists.txt b/pcilib/CMakeLists.txt index 1f3e646..9688065 100644 --- a/pcilib/CMakeLists.txt +++ b/pcilib/CMakeLists.txt @@ -9,9 +9,9 @@ include_directories( set(HEADERS pcilib.h pci.h export.h bar.h fifo.h model.h bank.h register.h views.h xml.h kmem.h irq.h locking.h lock.h dma.h event.h plugin.h tools.h error.h -debug.h env.h version.h config.h unit.h) +debug.h env.h version.h config.h) add_library(pcilib SHARED pci.c export.c bar.c fifo.c model.c bank.c -register.c views.c xml.c kmem.c irq.c locking.c lock.c dma.c event.c plugin.c tools.c error.c debug.c env.c unit.c) +register.c views.c xml.c kmem.c irq.c locking.c lock.c dma.c event.c plugin.c tools.c error.c debug.c env.c) target_link_libraries(pcilib dma protocols ${CMAKE_THREAD_LIBS_INIT} ${UFODECODE_LIBRARIES} ${CMAKE_DL_LIBS} ${EXTRA_SYSTEM_LIBS} ${LIBXML2_LIBRARIES} ${PYTHON_LIBRARIES}) diff --git a/pcilib/pci.h b/pcilib/pci.h index 8805fc2..f364890 100644 --- a/pcilib/pci.h +++ b/pcilib/pci.h @@ -28,7 +28,7 @@ #include "export.h" #include "locking.h" #include "xml.h" -#include "unit.h" +#include "views.h" typedef struct { uint8_t max_link_speed, link_speed; diff --git a/pcilib/unit.c b/pcilib/unit.c deleted file mode 100644 index a9766ed..0000000 --- a/pcilib/unit.c +++ /dev/null @@ -1,34 +0,0 @@ -#include "pcilib.h" -#include "pci.h" -#include "stdio.h" -#include -#include "error.h" -#include "unit.h" - -int pcilib_add_units(pcilib_t *ctx, size_t n, const pcilib_unit_t* units) { - - pcilib_unit_t *units2; - size_t size; - - if (!n) { - for (n = 0; units[n].name[0]; n++); - } - - if ((ctx->num_units + n + 1) > ctx->alloc_units) { - for (size = ctx->alloc_units; size < 2 * (n + ctx->num_units + 1); size<<=1); - - units2 = (pcilib_unit_t*)realloc(ctx->units, size * sizeof(pcilib_unit_t)); - if (!units2) return PCILIB_ERROR_MEMORY; - - ctx->units = units2; - ctx->alloc_units = size; - } - - memcpy(ctx->units + ctx->num_units, units, n * sizeof(pcilib_unit_t)); - memset(ctx->units + ctx->num_units + n, 0, sizeof(pcilib_unit_t)); - - ctx->num_units += n; - - return 0; -} - diff --git a/pcilib/unit.h b/pcilib/unit.h deleted file mode 100644 index 4a99f5b..0000000 --- a/pcilib/unit.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef _PCILIB_UNITS_H -#define _PCILIB_UNITS_H - -#include "pcilib.h" - -#define PCILIB_MAX_TRANSFORMS_PER_UNIT 16 - -typedef struct pcilib_unit_s pcilib_unit_t; -typedef struct pcilib_transform_unit_s pcilib_transform_unit_t; - -/** - * type to save a transformation unit in the pcitool program - */ -struct pcilib_transform_unit_s{ - char *name; - char *transform_formula; -}; - -/** - * type to save a unit in the pcitool programm - */ -struct pcilib_unit_s{ - char* name; - pcilib_transform_unit_t other_units[PCILIB_MAX_TRANSFORMS_PER_UNIT]; -}; - -/** - * function to populate the ctx with units - */ -int pcilib_add_units(pcilib_t* ctx, size_t n, const pcilib_unit_t* units); - -#endif diff --git a/pcilib/views.c b/pcilib/views.c index d0116db..9fe8810 100644 --- a/pcilib/views.c +++ b/pcilib/views.c @@ -6,7 +6,6 @@ #include "error.h" #include #include -#include "unit.h" /** * this function calls the python script and the function "evaluate" in it to evaluate the given formula @@ -209,18 +208,18 @@ int pcilib_write_view(pcilib_t *ctx, const char *bank, const char *regname, cons int operation_enum(pcilib_t *ctx, void *params, char* name, int view2reg, pcilib_register_value_t *regval, size_t viewval_size, void* viewval){ int j,k; if(view2reg==1){ - for(j=0; ((pcilib_view_enum_t*)(params))[j].name;j++){ - if(!(strcasecmp(((pcilib_view_enum_t*)(params))[j].name,name))){ - *regval=((pcilib_view_enum_t*)(params))[j].value; + for(j=0; ((pcilib_enum_t*)(params))[j].name;j++){ + if(!(strcasecmp(((pcilib_enum_t*)(params))[j].name,name))){ + *regval=((pcilib_enum_t*)(params))[j].value; return 0; } } }else if (view2reg==0){ - for(j=0; ((pcilib_view_enum_t*)(params))[j].name;j++){ - if (*regval<((pcilib_view_enum_t*)(params))[j].max && *regval>((pcilib_view_enum_t*)(params))[j].min){ - name=(char*)realloc(name,strlen(((pcilib_view_enum_t*)(params))[j].name)*sizeof(char)); - strncpy(name,((pcilib_view_enum_t*)(params))[j].name, strlen(((pcilib_view_enum_t*)(params))[j].name)); - k=strlen(((pcilib_view_enum_t*)(params))[j].name); + for(j=0; ((pcilib_enum_t*)(params))[j].name;j++){ + if (*regval<((pcilib_enum_t*)(params))[j].max && *regval>((pcilib_enum_t*)(params))[j].min){ + name=(char*)realloc(name,strlen(((pcilib_enum_t*)(params))[j].name)*sizeof(char)); + strncpy(name,((pcilib_enum_t*)(params))[j].name, strlen(((pcilib_enum_t*)(params))[j].name)); + k=strlen(((pcilib_enum_t*)(params))[j].name); name[k]='\0'; return 0; } @@ -249,8 +248,8 @@ int operation_formula(pcilib_t *ctx, void *params, char* unit, int view2reg, pci return 0; } - for(j=0; ((pcilib_view_t*)viewval)->base_unit.other_units[j].name;j++){ - if(!(strcasecmp(((pcilib_view_t*)viewval)->base_unit.other_units[j].name,unit))){ + for(j=0; ((pcilib_view_t*)viewval)->base_unit.transforms[j].name;j++){ + if(!(strcasecmp(((pcilib_view_t*)viewval)->base_unit.transforms[j].name,unit))){ /* when we have found the correct view of type formula, we apply the formula, that get the good value for return*/ formula=malloc(sizeof(char)*strlen(((pcilib_formula_t*)params)->read_formula)); if(!(formula)){ @@ -259,7 +258,7 @@ int operation_formula(pcilib_t *ctx, void *params, char* unit, int view2reg, pci } strncpy(formula,((pcilib_formula_t*)params)->read_formula,strlen(((pcilib_formula_t*)params)->read_formula)); pcilib_view_apply_formula(ctx,formula, regval); - pcilib_view_apply_unit(((pcilib_view_t*)viewval)->base_unit.other_units[j],unit,&value); + pcilib_view_apply_unit(((pcilib_view_t*)viewval)->base_unit.transforms[j],unit,&value); return 0; } } @@ -271,12 +270,12 @@ int operation_formula(pcilib_t *ctx, void *params, char* unit, int view2reg, pci return 0; } - for(j=0;((pcilib_view_t*)viewval)->base_unit.other_units[j].name;j++){ - if(!(strcasecmp(((pcilib_view_t*)viewval)->base_unit.other_units[j].name,unit))){ + for(j=0;((pcilib_view_t*)viewval)->base_unit.transforms[j].name;j++){ + if(!(strcasecmp(((pcilib_view_t*)viewval)->base_unit.transforms[j].name,unit))){ /* when we have found the correct view of type formula, we apply the formula, that get the good value for return*/ formula=malloc(sizeof(char)*strlen(((pcilib_formula_t*)params)->write_formula)); strncpy(formula,((pcilib_formula_t*)params)->write_formula,strlen((( pcilib_formula_t*)params)->write_formula)); - pcilib_view_apply_unit(((pcilib_view_t*)viewval)->base_unit.other_units[j],unit,&value); + pcilib_view_apply_unit(((pcilib_view_t*)viewval)->base_unit.transforms[j],unit,&value); pcilib_view_apply_formula(ctx,formula,regval); /* we maybe need some error checking there , like temp_value >min and num_views += n; return 0; } + +int pcilib_add_units(pcilib_t *ctx, size_t n, const pcilib_unit_t* units) { + + pcilib_unit_t *units2; + size_t size; + + if (!n) { + for (n = 0; units[n].name[0]; n++); + } + + if ((ctx->num_units + n + 1) > ctx->alloc_units) { + for (size = ctx->alloc_units; size < 2 * (n + ctx->num_units + 1); size<<=1); + + units2 = (pcilib_unit_t*)realloc(ctx->units, size * sizeof(pcilib_unit_t)); + if (!units2) return PCILIB_ERROR_MEMORY; + + ctx->units = units2; + ctx->alloc_units = size; + } + + memcpy(ctx->units + ctx->num_units, units, n * sizeof(pcilib_unit_t)); + memset(ctx->units + ctx->num_units + n, 0, sizeof(pcilib_unit_t)); + + ctx->num_units += n; + + return 0; +} diff --git a/pcilib/views.h b/pcilib/views.h index ae92d0d..c44393d 100644 --- a/pcilib/views.h +++ b/pcilib/views.h @@ -2,9 +2,14 @@ #define _PCILIB_VIEWS_H #include "pcilib.h" -#include "unit.h" -typedef struct pcilib_view_enum_s pcilib_view_enum_t; +#define PCILIB_MAX_TRANSFORMS_PER_UNIT 16 + +typedef struct pcilib_transform_unit_s pcilib_transform_unit_t; + +typedef struct pcilib_unit_s pcilib_unit_t; + +typedef struct pcilib_enum_s pcilib_enum_t; typedef struct pcilib_view_s pcilib_view_t; @@ -12,10 +17,26 @@ typedef struct pcilib_formula_s pcilib_formula_t; typedef int (*pcilib_view_operation_t)(pcilib_t *ctx, void *params, char* string, int read_or_write, pcilib_register_value_t *regval, size_t viewval_size, void* viewval); +/** + * type to save a transformation unit in the pcitool program + */ +struct pcilib_transform_unit_s{ + char *name; + char *transform_formula; +}; + +/** + * type to save a unit in the pcitool programm + */ +struct pcilib_unit_s{ + char* name; + pcilib_transform_unit_t transforms[PCILIB_MAX_TRANSFORMS_PER_UNIT]; +}; + /** * new type to define an enum view */ -struct pcilib_view_enum_s { +struct pcilib_enum_s { const char *name; /**nodeNr; i++) { view_name=(char*)nodeset->nodeTab[i]->children->content; - /* if the view name obtained is for an enum view, we get all pcilib_view_enum_t corresponding to the register*/ + /* if the view name obtained is for an enum view, we get all pcilib_enum_t corresponding to the register*/ for(k=0; ctx->views[k].name; k++){ if(!(strcasecmp(view_name, ctx->views[k].name))){ - ctx->register_ctx[id].views=realloc(ctx->register_ctx[id].views,(k+1)*sizeof(pcilib_view_enum_t)); + ctx->register_ctx[id].views=realloc(ctx->register_ctx[id].views,(k+1)*sizeof(pcilib_enum_t)); ctx->register_ctx[id].views[k]=ctx->views[k]; } } @@ -520,8 +520,8 @@ pcilib_xml_create_unit(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDocPtr doc, x if (!value || !attr) continue; if (!strcasecmp(name, "convert_unit")) { - desc.other_units[i].name=value2; - desc.other_units[i].transform_formula=value; + desc.transforms[i].name=value2; + desc.transforms[i].transform_formula=value; i++; } } @@ -558,7 +558,7 @@ static int pcilib_xml_create_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo /*if the view is of type enum, we get recursively its properties and then populate ctx enum views*/ if(!(strcasecmp(value,"enum"))){ desc.op=&operation_enum; - desc.parameters=malloc(sizeof(pcilib_view_enum_t)); + desc.parameters=malloc(sizeof(pcilib_enum_t)); desc.base_unit.name="name"; for (cur = node->children; cur != NULL; cur = cur->next) { if (!cur->children) continue; @@ -576,8 +576,8 @@ static int pcilib_xml_create_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo }else if (!(strcasecmp((char*)name,"enum"))) { - desc.parameters=realloc(desc.parameters,(i+1)*sizeof(pcilib_view_enum_t)); - ((pcilib_view_enum_t*)(desc.parameters))[i].name=value; + desc.parameters=realloc(desc.parameters,(i+1)*sizeof(pcilib_enum_t)); + ((pcilib_enum_t*)(desc.parameters))[i].name=value; /* we need to iterate through the different attributes of an enum node to get all properties*/ for(attr=cur->properties; attr!=NULL;attr=attr->next){ @@ -593,14 +593,14 @@ static int pcilib_xml_create_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo pcilib_error("Invalid value (%s) is specified in the XML enum node", value); return PCILIB_ERROR_INVALID_DATA; } - ((pcilib_view_enum_t*)(desc.parameters))[i].value=dat_value; + ((pcilib_enum_t*)(desc.parameters))[i].value=dat_value; }else if(!(strcasecmp(name,"min"))){ pcilib_register_value_t dat_min = strtol(value, &endptr, 0); if ((strlen(endptr) > 0)) { pcilib_error("Invalid min (%s) is specified in the XML enum node", value); return PCILIB_ERROR_INVALID_DATA; } - ((pcilib_view_enum_t*)(desc.parameters))[i].min=dat_min; + ((pcilib_enum_t*)(desc.parameters))[i].min=dat_min; ok_min=1; }else if(!(strcasecmp(name,"max"))){ pcilib_register_value_t dat_max = strtol(value, &endptr, 0); @@ -608,11 +608,11 @@ static int pcilib_xml_create_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDo pcilib_error("Invalid max (%s) is specified in the XML enum node", value); return PCILIB_ERROR_INVALID_DATA; } - ((pcilib_view_enum_t*)(desc.parameters))[i].max=dat_max; + ((pcilib_enum_t*)(desc.parameters))[i].max=dat_max; ok_max=1; } - if(ok_min==0) ((pcilib_view_enum_t*)(desc.parameters))[i].min=((pcilib_view_enum_t*)(desc.parameters))[i].value; - if(ok_max==0) ((pcilib_view_enum_t*)(desc.parameters))[i].max=((pcilib_view_enum_t*)(desc.parameters))[i].value; + if(ok_min==0) ((pcilib_enum_t*)(desc.parameters))[i].min=((pcilib_enum_t*)(desc.parameters))[i].value; + if(ok_max==0) ((pcilib_enum_t*)(desc.parameters))[i].max=((pcilib_enum_t*)(desc.parameters))[i].value; } i++; -- cgit v1.2.3