/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 09:37:24 UTC
  • mto: This revision was merged to the branch mainline in revision 353.
  • Revision ID: vchernov@inr.ru-20160211093724-ipw54n5wxts1jt2i
Merge script and transform view. Add get register and properties info to python wrap.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
/*!
6
6
 * \brief Global pointer to pcilib_t context.
7
 
 * Used by __setPcilib and read_register.
 
7
 * Used by setPcilib and read_register.
8
8
 */
9
9
pcilib_t* __ctx = 0;
10
10
 
11
11
/*!
12
 
 * \brief Wraps for pcilib_open function.
13
 
 * \param[in] fpga_device path to the device file [/dev/fpga0]
14
 
 * \param[in] model specifies the model of hardware, autodetected if NULL is passed
15
 
 * \return Pointer to pcilib_t, created by pcilib_open, serialized to bytearray
16
 
 */
17
 
PyObject* __createPcilibInstance(const char *fpga_device, const char *model)
18
 
{
19
 
        //opening device
20
 
    pcilib_t* ctx = pcilib_open(fpga_device, model);
21
 
    
22
 
    //serializing object
23
 
    return PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*));
24
 
}
25
 
 
26
 
/*!
27
12
 * \brief Sets python exeption text. Function interface same as printf.
28
13
 */
29
14
void __setPyExeptionText(const char* msg, ...)
30
15
{
 
16
        if(!Py_IsInitialized())
 
17
        Py_Initialize();
 
18
        
31
19
        char *buf;
32
20
        size_t sz;
33
21
        
53
41
}
54
42
 
55
43
/*!
 
44
 * \brief Wraps for pcilib_open function.
 
45
 * \param[in] fpga_device path to the device file [/dev/fpga0]
 
46
 * \param[in] model specifies the model of hardware, autodetected if NULL is passed
 
47
 * \return Pointer to pcilib_t, created by pcilib_open, serialized to bytearray; NULL with exeption text, if failed.
 
48
 */
 
49
PyObject* createPcilibInstance(const char *fpga_device, const char *model)
 
50
{
 
51
        //opening device
 
52
    pcilib_t* ctx = pcilib_open(fpga_device, model);
 
53
    if(!ctx)
 
54
    {
 
55
                __setPyExeptionText("Failed pcilib_open(%s, %s)", fpga_device, model);
 
56
                return NULL;
 
57
        }
 
58
        
 
59
    //serializing object
 
60
    return PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*));
 
61
}
 
62
 
 
63
/*!
 
64
 * \brief Closes current pciliv instance, if its open.
 
65
 */
 
66
void closeCurrentPcilibInstance()
 
67
{
 
68
    if(__ctx)
 
69
    {
 
70
        pcilib_close(__ctx);
 
71
        __ctx = NULL;
 
72
    }
 
73
}
 
74
 
 
75
/*!
 
76
 * \brief Returns current opened pcilib_t instatnce
 
77
 * \return Pointer to pcilib_t, serialized to bytearray
 
78
 */
 
79
PyObject* getCurrentPcilibInstance()
 
80
{
 
81
    return PyByteArray_FromStringAndSize((const char*)&__ctx, sizeof(pcilib_t*));
 
82
}
 
83
 
 
84
/*!
56
85
 * \brief Sets pcilib context to wraper.
57
86
 * \param[in] addr Pointer to pcilib_t, serialized to bytearray
58
87
 * \return 1, serialized to PyObject or NULL with exeption text, if failed.
59
88
 */
60
 
PyObject* __setPcilib(PyObject* addr)
 
89
PyObject* setPcilib(PyObject* addr)
61
90
{
62
91
        if(!PyByteArray_Check(addr))
63
92
        {
64
 
        __setPyExeptionText(PyExc_Exception, "Incorrect addr type. Only bytearray is allowed");
65
 
                return;
 
93
        __setPyExeptionText("Incorrect addr type. Only bytearray is allowed");
 
94
                return NULL;
66
95
        }
67
96
        
68
97
        //deserializing adress
71
100
        //hard copy context adress
72
101
        for(int i = 0; i < sizeof(pcilib_t*) + 10; i++)
73
102
                ((char*)&__ctx)[i] = pAddr[i];
74
 
 
75
 
        free(pAddr);
76
103
        
77
104
        return PyInt_FromLong((long)1);
78
105
}
84
111
        switch(val->type)
85
112
        {
86
113
                case PCILIB_TYPE_INVALID:
87
 
            errstream("Invalid register output type (PCILIB_TYPE_INVALID)");
 
114
                        if(errstream)
 
115
                                errstream("Invalid register output type (PCILIB_TYPE_INVALID)");
88
116
                        return NULL;
89
117
                        
90
118
                case PCILIB_TYPE_STRING:
91
 
            errstream("Invalid register output type (PCILIB_TYPE_STRING)");
 
119
                        if(errstream)
 
120
                                errstream("Invalid register output type (PCILIB_TYPE_STRING)");
92
121
                        return NULL;
93
122
                
94
123
                case PCILIB_TYPE_LONG:
98
127
                        
99
128
                        if(err)
100
129
                        {
101
 
                errstream("Failed: pcilib_get_value_as_int (%i)", err);
 
130
                                if(errstream)
 
131
                                        errstream("Failed: pcilib_get_value_as_int (%i)", err);
102
132
                                return NULL;
103
133
                        }
104
134
                        return PyInt_FromLong((long) ret);
111
141
                        
112
142
                        if(err)
113
143
                        {
114
 
                errstream("Failed: pcilib_get_value_as_int (%i)", err);
 
144
                                if(errstream)
 
145
                                        errstream("Failed: pcilib_get_value_as_int (%i)", err);
115
146
                                return NULL;
116
147
                        }
117
148
                        return PyFloat_FromDouble((double) ret);
118
149
                }
119
150
                
120
151
                default:
121
 
            errstream("Invalid register output type (unknown)");
 
152
                        if(errstream)
 
153
                                errstream("Invalid register output type (unknown)");
122
154
                        return NULL;
123
155
        }
124
156
}
289
321
        return PyInt_FromLong((long)1);
290
322
}
291
323
 
 
324
void add_pcilib_value_to_dict(PyObject* dict, pcilib_value_t* val, const char *name)
 
325
{
 
326
        PyObject *py_val = pcilib_convert_val_to_pyobject(__ctx, val, NULL);
 
327
 
 
328
        if(py_val)
 
329
                PyDict_SetItem(dict,
 
330
                       PyString_FromString(name),
 
331
                       val);
 
332
        else
 
333
                PyDict_SetItem(dict,
 
334
                       PyString_FromString("defvalue"),
 
335
                       PyString_FromString("invalid"));
 
336
}
 
337
 
 
338
PyObject * pcilib_convert_property_info_to_pyobject(pcilib_property_info_t listItem)
 
339
{
 
340
    PyObject* pylistItem = PyDict_New();
 
341
 
 
342
    if(listItem.name)
 
343
        PyDict_SetItem(pylistItem,
 
344
                   PyString_FromString("name"),
 
345
                   PyString_FromString(listItem.name));
 
346
 
 
347
    if(listItem.description)
 
348
        PyDict_SetItem(pylistItem,
 
349
                   PyString_FromString("description"),
 
350
                   PyString_FromString(listItem.description));
 
351
 
 
352
   if(listItem.path)
 
353
        PyDict_SetItem(pylistItem,
 
354
                   PyString_FromString("path"),
 
355
                   PyString_FromString(listItem.path));
 
356
 
 
357
    //serialize types
 
358
    const char* type = "invalid";
 
359
    switch(listItem.type)
 
360
    {
 
361
        case PCILIB_TYPE_INVALID:
 
362
            type = "invalid";
 
363
            break;
 
364
        case PCILIB_TYPE_STRING:
 
365
             type = "string";
 
366
             break;
 
367
        case PCILIB_TYPE_DOUBLE:
 
368
             type = "double";
 
369
             break;
 
370
        case PCILIB_TYPE_LONG :
 
371
             type = "long";
 
372
             break;
 
373
        default:
 
374
             break;
 
375
    }
 
376
    PyDict_SetItem(pylistItem,
 
377
               PyString_FromString("type"),
 
378
               PyString_FromString(type));
 
379
 
 
380
 
 
381
    //serialize modes
 
382
    PyObject* modes = PyList_New(0);
 
383
 
 
384
    if((listItem.mode & PCILIB_ACCESS_R ) == PCILIB_REGISTER_R)
 
385
        PyList_Append(modes, PyString_FromString("R"));
 
386
    if((listItem.mode & PCILIB_ACCESS_W ) == PCILIB_REGISTER_W)
 
387
        PyList_Append(modes, PyString_FromString("W"));
 
388
    if((listItem.mode & PCILIB_ACCESS_RW ) == PCILIB_REGISTER_RW)
 
389
        PyList_Append(modes, PyString_FromString("RW"));
 
390
    if((listItem.mode & PCILIB_REGISTER_NO_CHK) == PCILIB_REGISTER_NO_CHK)
 
391
        PyList_Append(modes, PyString_FromString("NO_CHK"));
 
392
 
 
393
    PyDict_SetItem(pylistItem,
 
394
                   PyString_FromString("mode"),
 
395
                   modes);
 
396
 
 
397
    //serialize flags
 
398
    PyObject* flags = PyList_New(0);
 
399
 
 
400
    if((listItem.flags & PCILIB_LIST_FLAG_CHILDS ) == PCILIB_LIST_FLAG_CHILDS)
 
401
        PyList_Append(flags, PyString_FromString("childs"));
 
402
 
 
403
    PyDict_SetItem(pylistItem,
 
404
                   PyString_FromString("flags"),
 
405
                   flags);
 
406
 
 
407
    if(listItem.unit)
 
408
         PyDict_SetItem(pylistItem,
 
409
                    PyString_FromString("unit"),
 
410
                    PyString_FromString(listItem.unit));
 
411
 
 
412
    return pylistItem;
 
413
}
 
414
 
 
415
PyObject * pcilib_convert_register_info_to_pyobject(pcilib_register_info_t listItem)
 
416
{
 
417
    PyObject* pylistItem = PyDict_New();
 
418
 
 
419
    if(listItem.name)
 
420
        PyDict_SetItem(pylistItem,
 
421
                   PyString_FromString("name"),
 
422
                   PyString_FromString(listItem.name));
 
423
 
 
424
    if(listItem.description)
 
425
        PyDict_SetItem(pylistItem,
 
426
                   PyString_FromString("description"),
 
427
                   PyString_FromString(listItem.description));
 
428
 
 
429
   if(listItem.bank)
 
430
        PyDict_SetItem(pylistItem,
 
431
                   PyString_FromString("bank"),
 
432
                   PyString_FromString(listItem.bank));
 
433
 
 
434
    //serialize modes
 
435
    PyObject* modes = PyList_New(0);
 
436
 
 
437
    if((listItem.mode & PCILIB_REGISTER_R) == PCILIB_REGISTER_R)
 
438
        PyList_Append(modes, PyString_FromString("R"));
 
439
    if((listItem.mode & PCILIB_REGISTER_W) == PCILIB_REGISTER_W)
 
440
        PyList_Append(modes, PyString_FromString("W"));
 
441
    if((listItem.mode & PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW)
 
442
        PyList_Append(modes, PyString_FromString("RW"));
 
443
    if((listItem.mode & PCILIB_REGISTER_W1C) == PCILIB_REGISTER_W1C)
 
444
        PyList_Append(modes, PyString_FromString("W1C"));
 
445
    if((listItem.mode & PCILIB_REGISTER_RW1C) == PCILIB_REGISTER_RW1C)
 
446
        PyList_Append(modes, PyString_FromString("RW1C"));
 
447
    if((listItem.mode & PCILIB_REGISTER_W1I) == PCILIB_REGISTER_W1I)
 
448
        PyList_Append(modes, PyString_FromString("W1I"));
 
449
    if((listItem.mode & PCILIB_REGISTER_RW1I) == PCILIB_REGISTER_RW1I)
 
450
        PyList_Append(modes, PyString_FromString("RW1I"));
 
451
    if((listItem.mode & PCILIB_REGISTER_NO_CHK) == PCILIB_REGISTER_NO_CHK)
 
452
        PyList_Append(modes, PyString_FromString("NO_CHK"));
 
453
 
 
454
    PyDict_SetItem(pylistItem,
 
455
                   PyString_FromString("mode"),
 
456
                   modes);
 
457
    add_pcilib_value_to_dict(pylistItem, &listItem.defvalue, "defvalue");
 
458
 
 
459
    if(listItem.range)
 
460
    {
 
461
        PyObject* range = PyDict_New();
 
462
        add_pcilib_value_to_dict(range, &(listItem.range->min), "min");
 
463
        add_pcilib_value_to_dict(range, &(listItem.range->max), "max");
 
464
        PyDict_SetItem(pylistItem,
 
465
                       PyString_FromString("range"),
 
466
                       range);
 
467
    }
 
468
 
 
469
    if(listItem.values)
 
470
    {
 
471
        PyObject* values = PyList_New(0);
 
472
 
 
473
        for (int j = 0; listItem.values[j].name; j++)
 
474
        {
 
475
            PyObject* valuesItem = PyDict_New();
 
476
 
 
477
            add_pcilib_value_to_dict(valuesItem, &(listItem.values[j].value), "value");
 
478
            add_pcilib_value_to_dict(valuesItem, &(listItem.values[j].min), "min");
 
479
            add_pcilib_value_to_dict(valuesItem, &(listItem.values[j].max), "max");
 
480
 
 
481
            if(listItem.values[j].name)
 
482
                PyDict_SetItem(valuesItem,
 
483
                           PyString_FromString("name"),
 
484
                           PyString_FromString(listItem.values[j].name));
 
485
 
 
486
            if(listItem.values[j].description)
 
487
                PyDict_SetItem(valuesItem,
 
488
                           PyString_FromString("name"),
 
489
                           PyString_FromString(listItem.values[j].description));
 
490
 
 
491
            PyList_Append(values, valuesItem);
 
492
        }
 
493
 
 
494
        PyDict_SetItem(pylistItem,
 
495
                       PyString_FromString("values"),
 
496
                       values);
 
497
    }
 
498
 
 
499
    return pylistItem;
 
500
}
 
501
 
 
502
PyObject* get_register_list(const char *bank)
 
503
{
 
504
        if(!__ctx)
 
505
        {
 
506
        __setPyExeptionText("pcilib_t handler not initialized");
 
507
                return NULL;
 
508
        }
 
509
        
 
510
        pcilib_register_info_t *list = pcilib_get_register_list(__ctx, bank, PCILIB_LIST_FLAGS_DEFAULT);
 
511
        
 
512
        PyObject* pyList = PyList_New(0);
 
513
    for(int i = 0; i < __ctx->num_reg; i++)
 
514
    {
 
515
                //serialize item attributes
 
516
        PyObject* pylistItem = pcilib_convert_register_info_to_pyobject(list[i]);
 
517
        PyList_Append(pyList, pylistItem);
 
518
    }
 
519
    
 
520
    pcilib_free_register_info(__ctx, list);
 
521
        
 
522
        return pyList;
 
523
}
 
524
 
 
525
PyObject* get_register_info(const char* reg,const char *bank)
 
526
{
 
527
    if(!__ctx)
 
528
    {
 
529
        __setPyExeptionText("pcilib_t handler not initialized");
 
530
        return NULL;
 
531
    }
 
532
 
 
533
    pcilib_register_info_t *info = pcilib_get_register_info(__ctx, bank, reg, PCILIB_LIST_FLAGS_DEFAULT);
 
534
 
 
535
        if(!info)
 
536
        {
 
537
                __setPyExeptionText("Failed pcilib_get_register_info");
 
538
        return NULL;
 
539
        }
 
540
 
 
541
    PyObject* py_info = pcilib_convert_register_info_to_pyobject(info[0]);
 
542
 
 
543
    pcilib_free_register_info(__ctx, info);
 
544
 
 
545
    return py_info;
 
546
}
 
547
 
 
548
PyObject* get_property_info(const char* branch)
 
549
{
 
550
    if(!__ctx)
 
551
    {
 
552
        __setPyExeptionText("pcilib_t handler not initialized");
 
553
        return NULL;
 
554
    }
 
555
 
 
556
    pcilib_property_info_t *list = pcilib_get_property_list(__ctx, branch, PCILIB_LIST_FLAGS_DEFAULT);
 
557
 
 
558
    PyObject* pyList = PyList_New(0);
 
559
 
 
560
    for(int i = 0; i < list[i].path; i++)
 
561
    {
 
562
        //serialize item attributes
 
563
        PyObject* pylistItem = pcilib_convert_property_info_to_pyobject(list[i]);
 
564
        PyList_Append(pyList, pylistItem);
 
565
    }
 
566
 
 
567
    pcilib_free_property_info(__ctx, list);
 
568
 
 
569
    return pyList;
 
570
}