summaryrefslogtreecommitdiffstats
path: root/pywrap/pcipywrap.c
blob: 77da19594304debda598040df1163e8fa204a77d (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include "pci.h"
#include "error.h"
#include <Python.h>

/*!
 * \brief Global pointer to pcilib_t context.
 * Used by set_pcilib and read_register.
 */
pcilib_t* __ctx = 0;

char* full_log = NULL;

/*!
 * \brief Wraping for vsnprintf function, that saves string to char*
 * \return saved from vsnprintf string
 */
char* vmake_str(const char* msg, va_list vl)
{
	char *buf;
	size_t sz;
	
	va_list vl_copy;
	va_copy(vl_copy, vl);
	
	sz = vsnprintf(NULL, 0, msg, vl);
	buf = (char *)malloc(sz + 1);
	
	if(!buf)
	{
		return NULL;
	}

	vsnprintf(buf, sz+1, msg, vl_copy);
	va_end(vl_copy);
	
	return buf;
}

/*!
 * \brief Wraping for vsnprintf function, that saves string to char*
 * \return saved from vsnprintf string
 */
char* make_str(const char* msg, ...)
{
	va_list vl;
    va_start(vl, msg);
	char *buf = vmake_str(msg, vl);
	va_end(vl);
	return buf;
}

/*!
 * \brief Version of pcilib_logger_t, that saves error text to Python exeption
 */
void pcilib_print_error_to_py(void *arg, const char *file, int line, 
							  pcilib_log_priority_t prio, const char *msg,
							  va_list va) {							  
	//wrap error message with file and line number
	char* buf_raw_msg = vmake_str(msg, va);
	char* buf_wrapped_message = make_str("%s [%s:%d]\n", buf_raw_msg, file, line);
	
	if(prio == PCILIB_LOG_ERROR)
	{
		if(!full_log)
			full_log = make_str("");
    
		if(strlen(buf_wrapped_message) >= 2 &&
		   buf_wrapped_message[0] == '#' &&
		   buf_wrapped_message[1] == 'E')
		{
			char* wrapped_exeption = make_str("%sprogramm error log:\n%s", &(buf_wrapped_message[2]), full_log);
			free(full_log);
			full_log = NULL;
			
			PyErr_SetString(PyExc_Exception, wrapped_exeption);
			free(wrapped_exeption);
		}
		else
		{
			//copy received message to log
			char* buf = full_log;
			full_log = make_str("%s%s", buf, buf_wrapped_message);
			free(buf);
		}
	}
	else
		printf(buf_wrapped_message);
		
	free(buf_wrapped_message);
	free(buf_raw_msg);
}

/*!
 * \brief Redirect pcilib standart log stream to exeption text.
 * Logger will accumulate errors untill get message, starts with "#E".
 * After that, logger will write last error, and all accumulated errors
 * to Python exeption text
 */
void __redirect_logs_to_exeption()
{
	pcilib_set_logger(pcilib_get_logger_min_prio(), 
					  pcilib_print_error_to_py,
					  pcilib_get_logger_argument());
}

/*!
 * Destructor for pcilib_t
 */
void close_pcilib_instance(void *ctx)
{
	if(ctx != __ctx)
		pcilib_close(ctx);
}

/*!
 * \brief Wraps for pcilib_open function.
 * \param[in] fpga_device path to the device file [/dev/fpga0]
 * \param[in] model specifies the model of hardware, autodetected if NULL is passed
 * \return Pointer to pcilib_t, created by pcilib_open; NULL with exeption text, if failed.
 */
PyObject* create_pcilib_instance(const char *fpga_device, const char *model)
{
	//opening device
    pcilib_t* ctx = pcilib_open(fpga_device, model);
    if(!ctx)
    {
		pcilib_error("#E Failed pcilib_open(%s, %s)", fpga_device, model);
		return NULL;
	}
	
	return PyCObject_FromVoidPtr((void*)ctx, close_pcilib_instance);
}

/*!
 * \brief Closes current pciliv instance, if its open.
 */
void close_curr_pcilib_instance()
{
    if(__ctx)
    {
        pcilib_close(__ctx);
        __ctx = NULL;
    }
}

/*!
 * \brief Returns current opened pcilib_t instatnce
 * \return Pointer to pcilib_t, serialized to bytearray
 */
PyObject* get_curr_pcilib_instance()
{
    return PyByteArray_FromStringAndSize((const char*)&__ctx, sizeof(pcilib_t*));
}

/*!
 * \brief Sets pcilib context to wraper.
 * \param[in] addr Pointer to pcilib_t, serialized to PyCObject
 * \return 1, serialized to PyObject or NULL with exeption text, if failed.
 */
PyObject* set_pcilib(PyObject* addr)
{
	if(!PyCObject_Check(addr))
	{
        pcilib_error("#E Incorrect addr type. Only PyCObject is allowed");
		return NULL;
	}
	
	__ctx = (pcilib_t*)PyCObject_AsVoidPtr(addr);
	
	return PyInt_FromLong((long)1);
}


/*!
 * \brief Reads register value. Wrap for pcilib_read_register function.
 * \param[in] regname the name of the register
 * \param[in] bank should specify the bank name if register with the same name may occur in multiple banks, NULL otherwise
 * \return register value, can be integer or float type; NULL with exeption text, if failed.
 */
PyObject* read_register(const char *regname, const char *bank)
{
	if(!__ctx)
	{
        pcilib_error("#E pcilib_t handler not initialized");
		return NULL;
	}

	pcilib_value_t val = {0};
    pcilib_register_value_t reg_value;
	
	int err; 
	
	err = pcilib_read_register(__ctx, bank, regname, &reg_value);
	if(err)
	{
		pcilib_error("#E Failed pcilib_read_register");
		return NULL;
	}
	
	err = pcilib_set_value_from_register_value(__ctx, &val, reg_value);
	if(err)
	{
		pcilib_error("#E Failed pcilib_set_value_from_register_value");
		return NULL;
	}

    return pcilib_get_value_as_pyobject(__ctx, &val);
}

/*!
 * \brief Writes value to register. Wrap for pcilib_write_register function.
 * \param[in] val Register value, that needs to be set. Can be int, float or string.
 * \param[in] regname the name of the register
 * \param[in] bank should specify the bank name if register with the same name may occur in multiple banks, NULL otherwise
 * \return 1, serialized to PyObject or NULL with exeption text, if failed.
 */
PyObject* write_register(PyObject* val, const char *regname, const char *bank)
{
	if(!__ctx)
	{
        pcilib_error("pcilib_t handler not initialized");
		return NULL;
	}
	
	
	pcilib_value_t val_internal = {0};
    pcilib_register_value_t reg_value;
	
	int err; 
	
	err = pcilib_set_value_from_pyobject(__ctx, val, &val_internal);
	
	if(err)
	{
		pcilib_error("#E Failed pcilib_set_value_from_pyobject");
		return NULL;
	}
	
	
	reg_value = pcilib_get_value_as_register_value(__ctx, &val_internal, &err);
	if(err)
	{
		pcilib_error("#E Failed pcilib_set_value_from_pyobject, (error %i)", err);
		return NULL;
	}
	
	err = pcilib_write_register(__ctx, bank, regname, reg_value);
	
	if(err)
	{
		pcilib_error("#E Failed pcilib_set_value_from_pyobject, (error %i)", err);
		return NULL;
	}
	
    return PyInt_FromLong((long)1);
}

/*!
 * \brief Reads propety value. Wrap for pcilib_get_property function.
 * \param[in] prop property name (full name including path)
 * \return property value, can be integer or float type; NULL with exeption text, if failed.
 */
PyObject* get_property(const char *prop)
{
	if(!__ctx)
	{
        pcilib_error("#E pcilib_t handler not initialized");
		return NULL;
	}
	
	int err;
	pcilib_value_t val = {0};
	
	err  = pcilib_get_property(__ctx, prop, &val);
	
	if(err)
	{
		pcilib_error("#E Failed pcilib_get_property, (error %i)", err);
		return NULL;
	}
	
    return pcilib_get_value_as_pyobject(__ctx, &val);
}

/*!
 * \brief Writes value to property. Wrap for pcilib_set_property function.
 * \param[in] prop property name (full name including path)
 * \param[in] val Property value, that needs to be set. Can be int, float or string.
 * \return 1, serialized to PyObject or NULL with exeption text, if failed.
 */
PyObject* set_property(PyObject* val, const char *prop)
{
	int err;
	
	if(!__ctx)
	{
        pcilib_error("#E pcilib_t handler not initialized");
		return NULL;
	}
	
	pcilib_value_t val_internal = {0};
	err = pcilib_set_value_from_pyobject(__ctx, val, &val_internal);
	if(err)
	{
		pcilib_error("#E pcilib_set_value_from_pyobject, (error %i)", err);
		return NULL;
	}
	
	err  = pcilib_set_property(__ctx, prop, &val_internal);
	if(err)
	{
		pcilib_error("#E pcilib_set_property, (error %i)", err);
		return NULL;
	}
	
	return PyInt_FromLong((long)1);
}

/*!
 * \brief Wrap for PyDict_SetItem, with decrease reference counting after set.
 */
void pcilib_pydict_set_item(PyObject* dict, PyObject* name, PyObject* value)
{
    PyDict_SetItem(dict,
                   name,
                   value);
    Py_XDECREF(name);
    Py_XDECREF(value);
}

/*!
 * \brief Wrap for PyList_Append, with decrease reference counting after append.
 */
void pcilib_pylist_append(PyObject* list, PyObject* value)
{
    PyList_Append(list, value);
    Py_XDECREF(value);
}

void add_pcilib_value_to_dict(PyObject* dict, pcilib_value_t* val, const char *name)
{
    PyObject *py_val = (PyObject*)pcilib_get_value_as_pyobject(__ctx, val);

	if(py_val)
        pcilib_pydict_set_item(dict,
                              PyString_FromString(name),
                              py_val);
	else
        pcilib_pydict_set_item(dict,
                              PyString_FromString("defvalue"),
                              PyString_FromString("invalid"));
}

PyObject * pcilib_convert_property_info_to_pyobject(pcilib_property_info_t listItem)
{
    PyObject* pylistItem = PyDict_New();

    if(listItem.name)
        pcilib_pydict_set_item(pylistItem,
                   PyString_FromString("name"),
                   PyString_FromString(listItem.name));

    if(listItem.description)
        pcilib_pydict_set_item(pylistItem,
                   PyString_FromString("description"),
                   PyString_FromString(listItem.description));

   if(listItem.path)
        pcilib_pydict_set_item(pylistItem,
                   PyString_FromString("path"),
                   PyString_FromString(listItem.path));

    //serialize types
    const char* type = "invalid";
    switch(listItem.type)
    {
        case PCILIB_TYPE_INVALID:
            type = "invalid";
            break;
        case PCILIB_TYPE_STRING:
             type = "string";
             break;
        case PCILIB_TYPE_DOUBLE:
             type = "double";
             break;
        case PCILIB_TYPE_LONG :
             type = "long";
             break;
        default:
             break;
    }
    pcilib_pydict_set_item(pylistItem,
               PyString_FromString("type"),
               PyString_FromString(type));


    //serialize modes
    PyObject* modes = PyList_New(0);

    if((listItem.mode & PCILIB_ACCESS_R ) == PCILIB_REGISTER_R)
        pcilib_pylist_append(modes, PyString_FromString("R"));
    if((listItem.mode & PCILIB_ACCESS_W ) == PCILIB_REGISTER_W)
        pcilib_pylist_append(modes, PyString_FromString("W"));
    if((listItem.mode & PCILIB_ACCESS_RW ) == PCILIB_REGISTER_RW)
        pcilib_pylist_append(modes, PyString_FromString("RW"));
    if((listItem.mode & PCILIB_REGISTER_INCONSISTENT) == PCILIB_REGISTER_INCONSISTENT)
        pcilib_pylist_append(modes, PyString_FromString("NO_CHK"));

    pcilib_pydict_set_item(pylistItem,
                   PyString_FromString("mode"),
                   modes);

    //serialize flags
    PyObject* flags = PyList_New(0);

    if((listItem.flags & PCILIB_LIST_FLAG_CHILDS ) == PCILIB_LIST_FLAG_CHILDS)
        pcilib_pylist_append(flags, PyString_FromString("childs"));

    pcilib_pydict_set_item(pylistItem,
                   PyString_FromString("flags"),
                   flags);

    if(listItem.unit)
         pcilib_pydict_set_item(pylistItem,
                    PyString_FromString("unit"),
                    PyString_FromString(listItem.unit));

    return pylistItem;
}

PyObject * pcilib_convert_register_info_to_pyobject(pcilib_register_info_t listItem)
{
    PyObject* pylistItem = PyDict_New();

    if(listItem.name)
        pcilib_pydict_set_item(pylistItem,
		                      PyString_FromString("name"),
		                      PyString_FromString(listItem.name));

    if(listItem.description)
        pcilib_pydict_set_item(pylistItem,
                              PyString_FromString("description"),
                              PyString_FromString(listItem.description));

   if(listItem.bank)
        pcilib_pydict_set_item(pylistItem,
                              PyString_FromString("bank"),
                              PyString_FromString(listItem.bank));

    //serialize modes
    PyObject* modes = PyList_New(0);

    if((listItem.mode & PCILIB_REGISTER_R) == PCILIB_REGISTER_R)
        pcilib_pylist_append(modes, PyString_FromString("R"));
    if((listItem.mode & PCILIB_REGISTER_W) == PCILIB_REGISTER_W)
        pcilib_pylist_append(modes, PyString_FromString("W"));
    if((listItem.mode & PCILIB_REGISTER_RW) == PCILIB_REGISTER_RW)
        pcilib_pylist_append(modes, PyString_FromString("RW"));
    if((listItem.mode & PCILIB_REGISTER_W1C) == PCILIB_REGISTER_W1C)
        pcilib_pylist_append(modes, PyString_FromString("W1C"));
    if((listItem.mode & PCILIB_REGISTER_RW1C) == PCILIB_REGISTER_RW1C)
        pcilib_pylist_append(modes, PyString_FromString("RW1C"));
    if((listItem.mode & PCILIB_REGISTER_W1I) == PCILIB_REGISTER_W1I)
        pcilib_pylist_append(modes, PyString_FromString("W1I"));
    if((listItem.mode & PCILIB_REGISTER_RW1I) == PCILIB_REGISTER_RW1I)
        pcilib_pylist_append(modes, PyString_FromString("RW1I"));
    if((listItem.mode & PCILIB_REGISTER_INCONSISTENT) == PCILIB_REGISTER_INCONSISTENT)
        pcilib_pylist_append(modes, PyString_FromString("NO_CHK"));

    pcilib_pydict_set_item(pylistItem,
						  PyString_FromString("mode"),
                          modes);
                  
    pcilib_value_t defval = {0};
    pcilib_set_value_from_register_value(__ctx, &defval, listItem.defvalue);
    add_pcilib_value_to_dict(pylistItem, &defval, "defvalue");

    if(listItem.range)
    {
		pcilib_value_t minval = {0};
		pcilib_set_value_from_register_value(__ctx, &minval, listItem.range->min);
		
		pcilib_value_t maxval = {0};
		pcilib_set_value_from_register_value(__ctx, &maxval, listItem.range->max);
		
        PyObject* range = PyDict_New();
        add_pcilib_value_to_dict(range, &minval, "min");
        add_pcilib_value_to_dict(range, &maxval, "max");
        pcilib_pydict_set_item(pylistItem,
                              PyString_FromString("range"),
                              range);
    }

    if(listItem.values)
    {
        PyObject* values = PyList_New(0);

        for (int j = 0; listItem.values[j].name; j++)
        {
            PyObject* valuesItem = PyDict_New();
            
            pcilib_value_t val = {0};
			pcilib_set_value_from_register_value(__ctx, &val, listItem.values[j].value);

			pcilib_value_t min = {0};
			pcilib_set_value_from_register_value(__ctx, &min, listItem.values[j].min);
		
			pcilib_value_t max = {0};
			pcilib_set_value_from_register_value(__ctx, &max, listItem.values[j].max);
            
            add_pcilib_value_to_dict(valuesItem, &val, "value");
            add_pcilib_value_to_dict(valuesItem, &min, "min");
            add_pcilib_value_to_dict(valuesItem, &max, "max");

            if(listItem.values[j].name)
                pcilib_pydict_set_item(valuesItem,
									  PyString_FromString("name"),
									  PyString_FromString(listItem.values[j].name));

            if(listItem.values[j].description)
                pcilib_pydict_set_item(valuesItem,
									  PyString_FromString("name"),
									  PyString_FromString(listItem.values[j].description));

            pcilib_pylist_append(values, valuesItem);
        }

        pcilib_pydict_set_item(pylistItem,
                              PyString_FromString("values"),
                              values);
    }

    return pylistItem;
}

PyObject* get_registers_list(const char *bank)
{
	if(!__ctx)
	{
        pcilib_error("#E pcilib_t handler not initialized");
		return NULL;
	}
	
	pcilib_register_info_t *list = pcilib_get_register_list(__ctx, bank, PCILIB_LIST_FLAGS_DEFAULT);
	
	PyObject* pyList = PyList_New(0);
    for(int i = 0; i < __ctx->num_reg; i++)
    {
		//serialize item attributes
        PyObject* pylistItem = pcilib_convert_register_info_to_pyobject(list[i]);
        pcilib_pylist_append(pyList, pylistItem);
    }
    
    pcilib_free_register_info(__ctx, list);
	
	return pyList;
}

PyObject* get_register_info(const char* reg,const char *bank)
{
    if(!__ctx)
    {
        pcilib_error("#E pcilib_t handler not initialized");
        return NULL;
    }

    pcilib_register_info_t *info = pcilib_get_register_info(__ctx, bank, reg, PCILIB_LIST_FLAGS_DEFAULT);

	if(!info)
	{
        return NULL;
	}

    PyObject* py_info = pcilib_convert_register_info_to_pyobject(info[0]);

    pcilib_free_register_info(__ctx, info);

    return py_info;
}

PyObject* get_property_info(const char* branch)
{
    if(!__ctx)
    {
        pcilib_error("#E pcilib_t handler not initialized");
        return NULL;
    }

    pcilib_property_info_t *list = pcilib_get_property_list(__ctx, branch, PCILIB_LIST_FLAGS_DEFAULT);

    PyObject* pyList = PyList_New(0);

    for(int i = 0; list[i].path; i++)
    {
        //serialize item attributes
        PyObject* pylistItem = pcilib_convert_property_info_to_pyobject(list[i]);
        pcilib_pylist_append(pyList, pylistItem);
    }

    pcilib_free_property_info(__ctx, list);

    return pyList;
}