/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to pcilib/pcipywrap.c

  • Committer: Vasilii Chernov
  • Date: 2016-02-11 13:24:01 UTC
  • mto: This revision was merged to the branch mainline in revision 353.
  • Revision ID: vchernov@inr.ru-20160211132401-w37eyhdwxj87vvmv
Change no_set_check parameter name. Move Python wrap to separate directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "pci.h"
2
 
#include "error.h"
3
 
#include <Python.h>
4
 
 
5
 
/*!
6
 
 * \brief Global pointer to pcilib_t context.
7
 
 * Used by setPcilib and read_register.
8
 
 */
9
 
pcilib_t* __ctx = 0;
10
 
 
11
 
/*!
12
 
 * \brief Wraping for vsnprintf function, that saves string to char*
13
 
 * \return saved from vsnprintf string
14
 
 */
15
 
char* vmake_str(const char* msg, va_list vl)
16
 
{
17
 
        char *buf;
18
 
        size_t sz;
19
 
        
20
 
        va_list vl_copy;
21
 
        va_copy(vl_copy, vl);
22
 
        
23
 
        sz = vsnprintf(NULL, 0, msg, vl);
24
 
        buf = (char *)malloc(sz + 1);
25
 
        
26
 
        if(!buf)
27
 
        {
28
 
                return NULL;
29
 
        }
30
 
 
31
 
        vsnprintf(buf, sz+1, msg, vl_copy);
32
 
        va_end(vl_copy);
33
 
        
34
 
        return buf;
35
 
}
36
 
 
37
 
/*!
38
 
 * \brief Wraping for vsnprintf function, that saves string to char*
39
 
 * \return saved from vsnprintf string
40
 
 */
41
 
char* make_str(const char* msg, ...)
42
 
{
43
 
        va_list vl;
44
 
    va_start(vl, msg);
45
 
        char *buf = vmake_str(msg, vl);
46
 
        va_end(vl);
47
 
        return buf;
48
 
}
49
 
 
50
 
/*!
51
 
 * \brief Version of pcilib_logger_t, that saves error text to Python exeption
52
 
 */
53
 
void pcilib_print_error_to_py(void *arg, const char *file, int line, 
54
 
                                                          pcilib_log_priority_t prio, const char *msg,
55
 
                                                          va_list va) {
56
 
    char* buf_raw_msg = vmake_str(msg, va);
57
 
    char* buf_wrapped_message = make_str("%s [%s:%d]\n", buf_raw_msg, file, line);
58
 
    
59
 
    printf("%s", buf_wrapped_message);
60
 
    PyErr_SetString(PyExc_Exception, buf_wrapped_message);
61
 
    
62
 
    free(buf_wrapped_message);
63
 
    free(buf_raw_msg);
64
 
}
65
 
 
66
 
/*!
67
 
 * \brief Inits pcipywrap module at importing
68
 
 */
69
 
void init_pcipywrap_module()
70
 
{
71
 
        pcilib_set_logger(pcilib_get_logger_min_prio(), 
72
 
                                          pcilib_print_error_to_py,
73
 
                                          pcilib_get_logger_argument());
74
 
}
75
 
 
76
 
/*!
77
 
 * \brief Wraps for pcilib_open function.
78
 
 * \param[in] fpga_device path to the device file [/dev/fpga0]
79
 
 * \param[in] model specifies the model of hardware, autodetected if NULL is passed
80
 
 * \return Pointer to pcilib_t, created by pcilib_open, serialized to bytearray; NULL with exeption text, if failed.
81
 
 */
82
 
PyObject* createPcilibInstance(const char *fpga_device, const char *model)
83
 
{
84
 
        //opening device
85
 
    pcilib_t* ctx = pcilib_open(fpga_device, model);
86
 
    if(!ctx)
87
 
                return NULL;
88
 
        
89
 
    //serializing object
90
 
    return PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*));
91
 
}
92
 
 
93
 
/*!
94
 
 * \brief Closes current pciliv instance, if its open.
95
 
 */
96
 
void closeCurrentPcilibInstance()
97
 
{
98
 
    if(__ctx)
99
 
    {
100
 
        pcilib_close(__ctx);
101
 
        __ctx = NULL;
102
 
    }
103
 
}
104
 
 
105
 
/*!
106
 
 * \brief Returns current opened pcilib_t instatnce
107
 
 * \return Pointer to pcilib_t, serialized to bytearray
108
 
 */
109
 
PyObject* getCurrentPcilibInstance()
110
 
{
111
 
    return PyByteArray_FromStringAndSize((const char*)&__ctx, sizeof(pcilib_t*));
112
 
}
113
 
 
114
 
/*!
115
 
 * \brief Sets pcilib context to wraper.
116
 
 * \param[in] addr Pointer to pcilib_t, serialized to bytearray
117
 
 * \return 1, serialized to PyObject or NULL with exeption text, if failed.
118
 
 */
119
 
PyObject* setPcilib(PyObject* addr)
120
 
{
121
 
        if(!PyByteArray_Check(addr))
122
 
        {
123
 
        pcilib_error("Incorrect addr type. Only bytearray is allowed");
124
 
                return NULL;
125
 
        }
126
 
        
127
 
        //deserializing adress
128
 
        char* pAddr = PyByteArray_AsString(addr);
129
 
        
130
 
        //hard copy context adress
131
 
        for(int i = 0; i < sizeof(pcilib_t*) + 10; i++)
132
 
                ((char*)&__ctx)[i] = pAddr[i];
133
 
        
134
 
        return PyInt_FromLong((long)1);
135
 
}
136
 
 
137
 
 
138
 
/*!
139
 
 * \brief Reads register value. Wrap for pcilib_read_register function.
140
 
 * \param[in] regname the name of the register
141
 
 * \param[in] bank should specify the bank name if register with the same name may occur in multiple banks, NULL otherwise
142
 
 * \return register value, can be integer or float type; NULL with exeption text, if failed.
143
 
 */
144
 
PyObject* read_register(const char *regname, const char *bank)
145
 
{
146
 
        if(!__ctx)
147
 
        {
148
 
        pcilib_error("pcilib_t handler not initialized");
149
 
                return NULL;
150
 
        }
151
 
 
152
 
        pcilib_value_t val = {0};
153
 
    pcilib_register_value_t reg_value;
154
 
        
155
 
        int err; 
156
 
        
157
 
        err = pcilib_read_register(__ctx, bank, regname, &reg_value);
158
 
        if(err)
159
 
        {
160
 
        pcilib_error("Failed: pcilib_read_register (%i)", err);
161
 
                return NULL;
162
 
        }
163
 
        
164
 
        err = pcilib_set_value_from_register_value(__ctx, &val, reg_value);
165
 
        if(err)
166
 
        {
167
 
        pcilib_error("Failed: pcilib_set_value_from_register_value (%i)", err);
168
 
                return NULL;
169
 
        }
170
 
 
171
 
    return pcilib_convert_val_to_pyobject(__ctx, &val);
172
 
}
173
 
 
174
 
/*!
175
 
 * \brief Writes value to register. Wrap for pcilib_write_register function.
176
 
 * \param[in] val Register value, that needs to be set. Can be int, float or string.
177
 
 * \param[in] regname the name of the register
178
 
 * \param[in] bank should specify the bank name if register with the same name may occur in multiple banks, NULL otherwise
179
 
 * \return 1, serialized to PyObject or NULL with exeption text, if failed.
180
 
 */
181
 
PyObject* write_register(PyObject* val, const char *regname, const char *bank)
182
 
{
183
 
        if(!__ctx)
184
 
        {
185
 
        pcilib_error("pcilib_t handler not initialized");
186
 
                return NULL;
187
 
        }
188
 
        
189
 
        pcilib_value_t val_internal = {0};
190
 
    pcilib_register_value_t reg_value;
191
 
        
192
 
        int err; 
193
 
        err = pcilib_convert_pyobject_to_val(__ctx, val, &val_internal);
194
 
        if(err)
195
 
        {
196
 
        pcilib_error("Failed pcilib_convert_pyobject_to_val (%i)", err);
197
 
                return NULL;
198
 
        }
199
 
        
200
 
        reg_value = pcilib_get_value_as_register_value(__ctx, &val_internal, &err);
201
 
        if(err)
202
 
        {
203
 
        pcilib_error("Failed: pcilib_get_value_as_register_value (%i)", err);
204
 
                return NULL;
205
 
        }
206
 
        
207
 
        err = pcilib_write_register(__ctx, bank, regname, reg_value);
208
 
        if(err)
209
 
        {
210
 
        pcilib_error("Failed: pcilib_write_register (%i)", err);
211
 
                return NULL;
212
 
        }
213
 
    return PyInt_FromLong((long)1);
214
 
}
215
 
 
216
 
/*!
217
 
 * \brief Reads propety value. Wrap for pcilib_get_property function.
218
 
 * \param[in] prop property name (full name including path)
219
 
 * \return property value, can be integer or float type; NULL with exeption text, if failed.
220
 
 */
221
 
PyObject* get_property(const char *prop)
222
 
{
223
 
        if(!__ctx)
224
 
        {
225
 
        pcilib_error("pcilib_t handler not initialized");
226
 
                return NULL;
227
 
        }
228
 
        
229
 
        int err;
230
 
        pcilib_value_t val = {0};
231
 
        
232
 
        err  = pcilib_get_property(__ctx, prop, &val);
233
 
        
234
 
        if(err)
235
 
        {
236
 
            pcilib_error("Failed pcilib_get_property (%i)", err);
237
 
                        return NULL;
238
 
        }
239
 
        
240
 
    return pcilib_convert_val_to_pyobject(__ctx, &val);
241
 
}
242
 
 
243
 
/*!
244
 
 * \brief Writes value to property. Wrap for pcilib_set_property function.
245
 
 * \param[in] prop property name (full name including path)
246
 
 * \param[in] val Property value, that needs to be set. Can be int, float or string.
247
 
 * \return 1, serialized to PyObject or NULL with exeption text, if failed.
248
 
 */
249
 
PyObject* set_property(const char *prop, PyObject* val)
250
 
{
251
 
        int err;
252
 
        
253
 
        if(!__ctx)
254
 
        {
255
 
        pcilib_error("pcilib_t handler not initialized");
256
 
                return NULL;
257
 
        }
258
 
        
259
 
        pcilib_value_t val_internal = {0};
260
 
        err = pcilib_convert_pyobject_to_val(__ctx, val, &val_internal);
261
 
        if(err)
262
 
        {
263
 
        pcilib_error("Failed pcilib_convert_pyobject_to_val (%i)", err);
264
 
                return NULL;
265
 
        }
266
 
        
267
 
        err  = pcilib_set_property(__ctx, prop, &val_internal);
268
 
        
269
 
        if(err)
270
 
        {
271
 
        pcilib_error("Failed pcilib_get_property (%i)", err);
272
 
                return NULL;
273
 
        }
274
 
        
275
 
        return PyInt_FromLong((long)1);
276
 
}
277
 
 
278
 
void add_pcilib_value_to_dict(PyObject* dict, pcilib_value_t* val, const char *name)
279
 
{
280
 
    PyObject *py_val = (PyObject*)pcilib_convert_val_to_pyobject(__ctx, val);
281
 
 
282
 
        if(py_val)
283
 
                PyDict_SetItem(dict,
284
 
                       PyString_FromString(name),
285
 
                       py_val);
286
 
        else
287
 
                PyDict_SetItem(dict,
288
 
                       PyString_FromString("defvalue"),
289
 
                       PyString_FromString("invalid"));
290
 
}
291
 
 
292
 
PyObject * pcilib_convert_property_info_to_pyobject(pcilib_property_info_t listItem)
293
 
{
294
 
    PyObject* pylistItem = PyDict_New();
295
 
 
296
 
    if(listItem.name)
297
 
        PyDict_SetItem(pylistItem,
298
 
                   PyString_FromString("name"),
299
 
                   PyString_FromString(listItem.name));
300
 
 
301
 
    if(listItem.description)
302
 
        PyDict_SetItem(pylistItem,
303
 
                   PyString_FromString("description"),
304
 
                   PyString_FromString(listItem.description));
305
 
 
306
 
   if(listItem.path)
307
 
        PyDict_SetItem(pylistItem,
308
 
                   PyString_FromString("path"),
309
 
                   PyString_FromString(listItem.path));
310
 
 
311
 
    //serialize types
312
 
    const char* type = "invalid";
313
 
    switch(listItem.type)
314
 
    {
315
 
        case PCILIB_TYPE_INVALID:
316
 
            type = "invalid";
317
 
            break;
318
 
        case PCILIB_TYPE_STRING:
319
 
             type = "string";
320
 
             break;
321
 
        case PCILIB_TYPE_DOUBLE:
322
 
             type = "double";
323
 
             break;
324
 
        case PCILIB_TYPE_LONG :
325
 
             type = "long";
326
 
             break;
327
 
        default:
328
 
             break;
329
 
    }
330
 
    PyDict_SetItem(pylistItem,
331
 
               PyString_FromString("type"),
332
 
               PyString_FromString(type));
333
 
 
334
 
 
335
 
    //serialize modes
336
 
    PyObject* modes = PyList_New(0);
337
 
 
338
 
    if((listItem.mode & PCILIB_ACCESS_R ) == PCILIB_REGISTER_R)
339
 
        PyList_Append(modes, PyString_FromString("R"));
340
 
    if((listItem.mode & PCILIB_ACCESS_W ) == PCILIB_REGISTER_W)
341
 
        PyList_Append(modes, PyString_FromString("W"));
342
 
    if((listItem.mode & PCILIB_ACCESS_RW ) == PCILIB_REGISTER_RW)
343
 
        PyList_Append(modes, PyString_FromString("RW"));
344
 
    if((listItem.mode & PCILIB_REGISTER_NO_CHK) == PCILIB_REGISTER_NO_CHK)
345
 
        PyList_Append(modes, PyString_FromString("NO_CHK"));
346
 
 
347
 
    PyDict_SetItem(pylistItem,
348
 
                   PyString_FromString("mode"),
349
 
                   modes);
350
 
 
351
 
    //serialize flags
352
 
    PyObject* flags = PyList_New(0);
353
 
 
354
 
    if((listItem.flags & PCILIB_LIST_FLAG_CHILDS ) == PCILIB_LIST_FLAG_CHILDS)
355
 
        PyList_Append(flags, PyString_FromString("childs"));
356
 
 
357
 
    PyDict_SetItem(pylistItem,
358
 
                   PyString_FromString("flags"),
359
 
                   flags);
360
 
 
361
 
    if(listItem.unit)
362
 
         PyDict_SetItem(pylistItem,
363
 
                    PyString_FromString("unit"),
364
 
                    PyString_FromString(listItem.unit));
365
 
 
366
 
    return pylistItem;
367
 
}
368
 
 
369
 
PyObject * pcilib_convert_register_info_to_pyobject(pcilib_register_info_t listItem)
370
 
{
371
 
    PyObject* pylistItem = PyDict_New();
372
 
 
373
 
    if(listItem.name)
374
 
        PyDict_SetItem(pylistItem,
375
 
                   PyString_FromString("name"),
376
 
                   PyString_FromString(listItem.name));
377
 
 
378
 
    if(listItem.description)
379
 
        PyDict_SetItem(pylistItem,
380
 
                   PyString_FromString("description"),
381
 
                   PyString_FromString(listItem.description));
382
 
 
383
 
   if(listItem.bank)
384
 
        PyDict_SetItem(pylistItem,
385
 
                   PyString_FromString("bank"),
386
 
                   PyString_FromString(listItem.bank));
387
 
 
388
 
    //serialize modes
389
 
    PyObject* modes = PyList_New(0);
390
 
 
391
 
    if((listItem.mode & PCILIB_REGISTER_R) == PCILIB_REGISTER_R)
392
 
        PyList_Append(modes, PyString_FromString("R"));
393
 
    if((listItem.mode & PCILIB_REGISTER_W) == PCILIB_REGISTER_W)
394
 
        PyList_Append(modes, PyString_FromString("W"));
395
 
    if((listItem.mode & PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW)
396
 
        PyList_Append(modes, PyString_FromString("RW"));
397
 
    if((listItem.mode & PCILIB_REGISTER_W1C) == PCILIB_REGISTER_W1C)
398
 
        PyList_Append(modes, PyString_FromString("W1C"));
399
 
    if((listItem.mode & PCILIB_REGISTER_RW1C) == PCILIB_REGISTER_RW1C)
400
 
        PyList_Append(modes, PyString_FromString("RW1C"));
401
 
    if((listItem.mode & PCILIB_REGISTER_W1I) == PCILIB_REGISTER_W1I)
402
 
        PyList_Append(modes, PyString_FromString("W1I"));
403
 
    if((listItem.mode & PCILIB_REGISTER_RW1I) == PCILIB_REGISTER_RW1I)
404
 
        PyList_Append(modes, PyString_FromString("RW1I"));
405
 
    if((listItem.mode & PCILIB_REGISTER_NO_CHK) == PCILIB_REGISTER_NO_CHK)
406
 
        PyList_Append(modes, PyString_FromString("NO_CHK"));
407
 
 
408
 
    PyDict_SetItem(pylistItem,
409
 
                   PyString_FromString("mode"),
410
 
                   modes);
411
 
                   
412
 
    pcilib_value_t defval = {0};
413
 
    pcilib_set_value_from_register_value(__ctx, &defval, listItem.defvalue);
414
 
    add_pcilib_value_to_dict(pylistItem, &defval, "defvalue");
415
 
 
416
 
    if(listItem.range)
417
 
    {
418
 
                pcilib_value_t minval = {0};
419
 
                pcilib_set_value_from_register_value(__ctx, &minval, listItem.range->min);
420
 
                
421
 
                pcilib_value_t maxval = {0};
422
 
                pcilib_set_value_from_register_value(__ctx, &maxval, listItem.range->max);
423
 
                
424
 
        PyObject* range = PyDict_New();
425
 
        add_pcilib_value_to_dict(range, &minval, "min");
426
 
        add_pcilib_value_to_dict(range, &maxval, "max");
427
 
        PyDict_SetItem(pylistItem,
428
 
                       PyString_FromString("range"),
429
 
                       range);
430
 
    }
431
 
 
432
 
    if(listItem.values)
433
 
    {
434
 
        PyObject* values = PyList_New(0);
435
 
 
436
 
        for (int j = 0; listItem.values[j].name; j++)
437
 
        {
438
 
            PyObject* valuesItem = PyDict_New();
439
 
            
440
 
            pcilib_value_t val = {0};
441
 
                        pcilib_set_value_from_register_value(__ctx, &val, listItem.values[j].value);
442
 
 
443
 
                        pcilib_value_t min = {0};
444
 
                        pcilib_set_value_from_register_value(__ctx, &min, listItem.values[j].min);
445
 
                
446
 
                        pcilib_value_t max = {0};
447
 
                        pcilib_set_value_from_register_value(__ctx, &max, listItem.values[j].max);
448
 
            
449
 
            add_pcilib_value_to_dict(valuesItem, &val, "value");
450
 
            add_pcilib_value_to_dict(valuesItem, &min, "min");
451
 
            add_pcilib_value_to_dict(valuesItem, &max, "max");
452
 
 
453
 
            if(listItem.values[j].name)
454
 
                PyDict_SetItem(valuesItem,
455
 
                           PyString_FromString("name"),
456
 
                           PyString_FromString(listItem.values[j].name));
457
 
 
458
 
            if(listItem.values[j].description)
459
 
                PyDict_SetItem(valuesItem,
460
 
                           PyString_FromString("name"),
461
 
                           PyString_FromString(listItem.values[j].description));
462
 
 
463
 
            PyList_Append(values, valuesItem);
464
 
        }
465
 
 
466
 
        PyDict_SetItem(pylistItem,
467
 
                       PyString_FromString("values"),
468
 
                       values);
469
 
    }
470
 
 
471
 
    return pylistItem;
472
 
}
473
 
 
474
 
PyObject* get_register_list(const char *bank)
475
 
{
476
 
        if(!__ctx)
477
 
        {
478
 
        pcilib_error("pcilib_t handler not initialized");
479
 
                return NULL;
480
 
        }
481
 
        
482
 
        pcilib_register_info_t *list = pcilib_get_register_list(__ctx, bank, PCILIB_LIST_FLAGS_DEFAULT);
483
 
        
484
 
        PyObject* pyList = PyList_New(0);
485
 
    for(int i = 0; i < __ctx->num_reg; i++)
486
 
    {
487
 
                //serialize item attributes
488
 
        PyObject* pylistItem = pcilib_convert_register_info_to_pyobject(list[i]);
489
 
        PyList_Append(pyList, pylistItem);
490
 
    }
491
 
    
492
 
    pcilib_free_register_info(__ctx, list);
493
 
        
494
 
        return pyList;
495
 
}
496
 
 
497
 
PyObject* get_register_info(const char* reg,const char *bank)
498
 
{
499
 
    if(!__ctx)
500
 
    {
501
 
        pcilib_error("pcilib_t handler not initialized");
502
 
        return NULL;
503
 
    }
504
 
 
505
 
    pcilib_register_info_t *info = pcilib_get_register_info(__ctx, bank, reg, PCILIB_LIST_FLAGS_DEFAULT);
506
 
 
507
 
        if(!info)
508
 
        {
509
 
        pcilib_error("Failed pcilib_get_register_info");
510
 
        return NULL;
511
 
        }
512
 
 
513
 
    PyObject* py_info = pcilib_convert_register_info_to_pyobject(info[0]);
514
 
 
515
 
    pcilib_free_register_info(__ctx, info);
516
 
 
517
 
    return py_info;
518
 
}
519
 
 
520
 
PyObject* get_property_info(const char* branch)
521
 
{
522
 
    if(!__ctx)
523
 
    {
524
 
        pcilib_error("pcilib_t handler not initialized");
525
 
        return NULL;
526
 
    }
527
 
 
528
 
    pcilib_property_info_t *list = pcilib_get_property_list(__ctx, branch, PCILIB_LIST_FLAGS_DEFAULT);
529
 
 
530
 
    PyObject* pyList = PyList_New(0);
531
 
 
532
 
    for(int i = 0; list[i].path; i++)
533
 
    {
534
 
        //serialize item attributes
535
 
        PyObject* pylistItem = pcilib_convert_property_info_to_pyobject(list[i]);
536
 
        PyList_Append(pyList, pylistItem);
537
 
    }
538
 
 
539
 
    pcilib_free_property_info(__ctx, list);
540
 
 
541
 
    return pyList;
542
 
}