summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornicolas.zilio@hotmail.fr <>2015-09-14 18:18:00 +0200
committernicolas.zilio@hotmail.fr <>2015-09-14 18:18:00 +0200
commitde589562bd91cc60ee3e2d739bdd7a03063d38f7 (patch)
tree0ceb314dd240c1780d7cd0adf4fad99d69445ba3
parenta1bf5e300e2345b642d0a13e7e26d22c56156e47 (diff)
downloadpcitool-de589562bd91cc60ee3e2d739bdd7a03063d38f7.tar.gz
pcitool-de589562bd91cc60ee3e2d739bdd7a03063d38f7.tar.bz2
pcitool-de589562bd91cc60ee3e2d739bdd7a03063d38f7.tar.xz
pcitool-de589562bd91cc60ee3e2d739bdd7a03063d38f7.zip
first try with pcilib_operation_t
-rw-r--r--pcilib/pci.c7
-rw-r--r--pcilib/pci.h7
-rw-r--r--pcilib/views.c107
-rw-r--r--pcilib/views.h19
4 files changed, 132 insertions, 8 deletions
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 <max*/
+ return 0;
+ }
+ }
+ }
+ return -1;
+}
+
+
/**
* function to populate ctx enum views, as we could do for registers or banks
*/
@@ -421,7 +499,34 @@ int pcilib_add_views_formula(pcilib_t *ctx, size_t n, const pcilib_view_formula_
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
+ */
+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
*/