summaryrefslogtreecommitdiffstats
path: root/pcilib/views.c
blob: d0116db0106d29edf3dc1e7279d3450e67a08857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <string.h>
#include "pci.h"
#include "pcilib.h"
#include <Python.h>
#include "views.h"
#include "error.h"
#include <strings.h>
#include <stdlib.h>
#include "unit.h" 

/**
 * this function calls the python script and the function "evaluate" in it to evaluate the given formula
 *@param[in] the formula to be evaluated
 *@return the integer value of the evaluated formula (maybe go to float instead)
 */
static pcilib_register_value_t
pcilib_view_eval_formula(char* formula){
  
   /* initialization of python interpreter*/
   Py_Initialize();
   
   /*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();
   pcilib_register_value_t value=(pcilib_register_value_t)c;
   return value;
}


/**
 * 
 */
static char*
pcilib_view_compute_formula(pcilib_t* ctx, char* formula,char* reg_value_string){
  char *src=(char*)formula; 
  char *reg,*regend;
  char *dst=malloc(6*strlen(src)*sizeof(char));
  char temp[66];
  pcilib_register_value_t value;
  int offset=0;
  
  /*we get recursively all registers of string , and if they are not equel to '@reg', then we get their value and put it in formula*/
  while(1){
    reg = strchr(src, '@');
    if (!reg) {
      strcpy(dst, src);
      break;
    }
    regend = strchr(reg + 1, '@');
    if (!regend){
      pcilib_error("formula corresponding is malformed");
      return NULL;
    }
    strncpy(dst+offset, src, reg - src);
    offset+=reg-src;
    *regend = 0;
    /* Now (reg + 1) contains the proper register name, you can compare
it to reg/value and either get the value of current register or the
specified one. Add it to the register*/
    if(!(strcasecmp(reg,"@value")) || !(strcasecmp(reg,"@reg")) || !(strcasecmp(reg,"@self"))){
      strncpy(dst+offset,reg_value_string,strlen(reg_value_string));
      offset+=strlen(reg_value_string);
   
    }else{
      pcilib_read_register(ctx, NULL,reg+1,&value);
      sprintf(temp,"%i",value);
      strncpy(dst+offset,temp,strlen(temp));
      offset+=strlen(temp);
    }      
  src = regend + 1;
  }
  
  return dst;
}




static int
pcilib_view_apply_formula(pcilib_t* ctx, char* formula, pcilib_register_value_t* reg_value)
{
  
  char reg_value_string[66]; /* to register reg_value as a string, need to check the length*/
  sprintf(reg_value_string,"%u",*reg_value);
  
  formula=pcilib_view_compute_formula(ctx,formula,reg_value_string);
  if(!(formula)){
    pcilib_error("computing of formula failed");
    return PCILIB_ERROR_INVALID_DATA;
  }

  /* evaluation of the formula*/
  *reg_value= pcilib_view_eval_formula(formula);
  return 0;
}

/**
 * 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] unit - the requested unit in which we want to get the value
 *@param[in,out] value - the number that needs to get transformed
 */
static void
pcilib_view_apply_unit(pcilib_transform_unit_t unit_desc, const char* unit,pcilib_register_value_t* value){
  char* formula;
 
	    formula=malloc(strlen(unit_desc.transform_formula)*sizeof(char));
	    strcpy(formula,unit_desc.transform_formula);
	    pcilib_view_apply_formula(NULL,formula, value);

	    free(formula);
}



int pcilib_read_view(pcilib_t *ctx, const char *bank, const char *regname, const char *unit, size_t value_size, void *value)
{
  int i,j,err=0;
  pcilib_register_value_t temp_value;
  
  /* we get the index of the register to find the corresponding register context*/
  if((i=pcilib_find_register(ctx,bank,regname))==PCILIB_REGISTER_INVALID){
    pcilib_error("can't get the index of the register %s", regname);
    return PCILIB_ERROR_INVALID_REQUEST;
  }
  
  /* we get the value of the register, as we will apply the view on it*/
  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;
  }

    for(j=0;ctx->register_ctx[i].views[j].name;j++){
      if(!(strcasecmp(ctx->register_ctx[i].views[j].base_unit.name,unit))){/*if we asked for the unit "name"*/
	err=ctx->register_ctx[i].views[j].op(ctx,ctx->register_ctx[i].views[j].parameters,value/*the command name*/,0,&temp_value,0,&(ctx->register_ctx[i].views[j]));
	if(err){
	  pcilib_error("can't write to the register with the enum view");
	  return PCILIB_ERROR_FAILED;
	}
	break;
      }else if(!(strcasecmp(ctx->register_ctx[i].views[j].name,(char*)unit))){/*in this case we asked for the name of the view in unit*/
	err=ctx->register_ctx[i].views[j].op(ctx,ctx->register_ctx[i].views[j].parameters,(char*)unit, 0, &temp_value,0,&(ctx->register_ctx[i].views[j]));
      if(err){
	pcilib_error("can't write to the register with the formula view %s", unit);
	return PCILIB_ERROR_FAILED;
      }
      *(pcilib_register_value_t*)value=temp_value;
      break;
      }
    return 0;
    }
     pcilib_error("the view asked and the register do not correspond");
  return PCILIB_ERROR_NOTAVAILABLE;
}


/**
 * function to write to a register using a view
 */ 
int pcilib_write_view(pcilib_t *ctx, const char *bank, const char *regname, const char *unit, size_t value_size,void* value){
   int i,j;
   pcilib_register_value_t temp_value;
   int err;
   
  /* we get the index of the register to find the corresponding register context*/
  if((i=pcilib_find_register(ctx,bank,regname))==PCILIB_REGISTER_INVALID){
    pcilib_error("can't get the index of the register %s", regname);
    return PCILIB_ERROR_INVALID_REQUEST;
  }
 
  for(j=0;ctx->register_ctx[i].views[j].name;j++){
    if(!(strcasecmp(ctx->register_ctx[i].views[j].base_unit.name,unit))){/*if we asked for the unit "name"*/
      err=ctx->register_ctx[i].views[j].op(ctx,ctx->register_ctx[i].views[j].parameters,value/*the command name*/,1,&temp_value,0,&(ctx->register_ctx[i].views[j]));
      if(err){
	pcilib_error("can't write to the register with the enum view");
	return PCILIB_ERROR_FAILED;
      }
      break;
    }else if(!(strcasecmp(ctx->register_ctx[i].views[j].name,(char*)unit))){/*in this case we asked for then name of the view in unit*/
      temp_value=*(pcilib_register_value_t*)value /*the value to put in the register*/;
      err=ctx->register_ctx[i].views[j].op(ctx,ctx->register_ctx[i].views[j].parameters, (char*)unit, 1, &temp_value,0,&(ctx->register_ctx[i].views[j]));
      if(err){
	pcilib_error("can't write to the register with the formula view %s", unit);
	return PCILIB_ERROR_FAILED;
      }
      break;
    }
    pcilib_write_register(ctx,bank,regname,temp_value);
    return 0;
  }

  pcilib_error("the view asked and the register do not correspond");
  return PCILIB_ERROR_NOTAVAILABLE;
}

/**
 * always: viewval=view params=view params
 * write: name=enum command regval:the value corresponding to the command
 */
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;
	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);
	name[k]='\0';
	return 0;
      }
    }
  } 
  return PCILIB_ERROR_INVALID_REQUEST;
}

/**
 * pârams: view params unit=unit wanted regval:value before formula/after formula viewval=view
 */
int operation_formula(pcilib_t *ctx, void *params, char* unit, int view2reg, pcilib_register_value_t *regval, size_t viewval_size, void* viewval){
    int j=0;
    pcilib_register_value_t value=0;
    char* formula=NULL;

    if(view2reg==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);
	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)->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);
	  pcilib_view_apply_unit(((pcilib_view_t*)viewval)->base_unit.other_units[j],unit,&value);
	  return 0;
	}
      }
    }else if(view2reg==1){
      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);
	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);
	  /* we maybe need some error checking there , like temp_value >min and <max*/
	  return 0;
	}
      }
    }
    free(formula);
      return PCILIB_ERROR_INVALID_REQUEST;
}


/**
 * 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;
}