/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 11:00:54 UTC
  • mto: This revision was merged to the branch mainline in revision 353.
  • Revision ID: vchernov@inr.ru-20160211110054-h6x2oxdx1dqaekhe
Change error logging method in Python wrap. Move functions, that converts values between PyObject and pcilib_value_t to py.c

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "pci.h"
2
2
#include "error.h"
3
 
#include "pcipywrap.h"
 
3
#include <Python.h>
4
4
 
5
5
/*!
6
6
 * \brief Global pointer to pcilib_t context.
9
9
pcilib_t* __ctx = 0;
10
10
 
11
11
/*!
12
 
 * \brief Sets python exeption text. Function interface same as printf.
 
12
 * \brief Wraping for vsnprintf function, that saves string to char*
 
13
 * \return saved from vsnprintf string
13
14
 */
14
 
void __setPyExeptionText(const char* msg, ...)
 
15
char* vmake_str(const char* msg, va_list vl)
15
16
{
16
 
        if(!Py_IsInitialized())
17
 
        Py_Initialize();
18
 
        
19
17
        char *buf;
20
18
        size_t sz;
21
19
        
22
 
        va_list vl, vl_copy;
23
 
    va_start(vl, msg);
 
20
        va_list vl_copy;
24
21
        va_copy(vl_copy, vl);
25
22
        
26
23
        sz = vsnprintf(NULL, 0, msg, vl);
28
25
        
29
26
        if(!buf)
30
27
        {
31
 
                PyErr_SetString(PyExc_Exception, "Memory error");
32
 
                return;
 
28
                return NULL;
33
29
        }
34
30
 
35
31
        vsnprintf(buf, sz+1, msg, vl_copy);
36
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);
37
46
        va_end(vl);
38
 
        
39
 
        PyErr_SetString(PyExc_Exception, buf);
40
 
        free(buf);
 
47
}
 
48
 
 
49
/*!
 
50
 * \brief Version of pcilib_logger_t, that saves error text to Python exeption
 
51
 */
 
52
void pcilib_print_error_to_py(void *arg, const char *file, int line, 
 
53
                                                          pcilib_log_priority_t prio, const char *msg,
 
54
                                                          va_list va) {
 
55
    char* buf_raw_msg = vmake_str(msg, va);
 
56
    char* buf_wrapped_message = make_str("%s [%s:%d]\n", buf_raw_msg, file, line);
 
57
    
 
58
    printf("%s", buf_wrapped_message);
 
59
    PyErr_SetString(PyExc_Exception, buf_wrapped_message);
 
60
    
 
61
    free(buf_wrapped_message);
 
62
    free(buf_raw_msg);
 
63
}
 
64
 
 
65
/*!
 
66
 * \brief Inits pcipywrap module at importing
 
67
 */
 
68
void init_pcipywrap_module()
 
69
{
 
70
        pcilib_set_logger(pcilib_get_logger_min_prio(), 
 
71
                                          pcilib_print_error_to_py,
 
72
                                          pcilib_get_logger_argument());
41
73
}
42
74
 
43
75
/*!
51
83
        //opening device
52
84
    pcilib_t* ctx = pcilib_open(fpga_device, model);
53
85
    if(!ctx)
54
 
    {
55
 
                __setPyExeptionText("Failed pcilib_open(%s, %s)", fpga_device, model);
56
86
                return NULL;
57
 
        }
58
87
        
59
88
    //serializing object
60
89
    return PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*));
90
119
{
91
120
        if(!PyByteArray_Check(addr))
92
121
        {
93
 
        __setPyExeptionText("Incorrect addr type. Only bytearray is allowed");
 
122
        pcilib_error("Incorrect addr type. Only bytearray is allowed");
94
123
                return NULL;
95
124
        }
96
125
        
104
133
        return PyInt_FromLong((long)1);
105
134
}
106
135
 
107
 
PyObject* pcilib_convert_val_to_pyobject(pcilib_t* ctx, pcilib_value_t *val, void (*errstream)(const char* msg, ...))
108
 
{
109
 
        int err;
110
 
        
111
 
        switch(val->type)
112
 
        {
113
 
                case PCILIB_TYPE_INVALID:
114
 
                        if(errstream)
115
 
                                errstream("Invalid register output type (PCILIB_TYPE_INVALID)");
116
 
                        return NULL;
117
 
                        
118
 
                case PCILIB_TYPE_STRING:
119
 
                        if(errstream)
120
 
                                errstream("Invalid register output type (PCILIB_TYPE_STRING)");
121
 
                        return NULL;
122
 
                
123
 
                case PCILIB_TYPE_LONG:
124
 
                {
125
 
                        long ret;
126
 
                        ret = pcilib_get_value_as_int(__ctx, val, &err);
127
 
                        
128
 
                        if(err)
129
 
                        {
130
 
                                if(errstream)
131
 
                                        errstream("Failed: pcilib_get_value_as_int (%i)", err);
132
 
                                return NULL;
133
 
                        }
134
 
                        return PyInt_FromLong((long) ret);
135
 
                }
136
 
                
137
 
                case PCILIB_TYPE_DOUBLE:
138
 
                {
139
 
                        double ret;
140
 
                        ret = pcilib_get_value_as_float(__ctx, val, &err);
141
 
                        
142
 
                        if(err)
143
 
                        {
144
 
                                if(errstream)
145
 
                                        errstream("Failed: pcilib_get_value_as_int (%i)", err);
146
 
                                return NULL;
147
 
                        }
148
 
                        return PyFloat_FromDouble((double) ret);
149
 
                }
150
 
                
151
 
                default:
152
 
                        if(errstream)
153
 
                                errstream("Invalid register output type (unknown)");
154
 
                        return NULL;
155
 
        }
156
 
}
157
 
 
158
 
int pcilib_convert_pyobject_to_val(pcilib_t* ctx, PyObject* pyVal, pcilib_value_t *val)
159
 
{
160
 
        int err;
161
 
        
162
 
    if(PyInt_Check(pyVal))
163
 
    {
164
 
        err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyVal));
165
 
    }
166
 
    else
167
 
        if(PyFloat_Check(pyVal))
168
 
            err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(pyVal));
169
 
        else
170
 
            if(PyString_Check(pyVal))
171
 
                err = pcilib_set_value_from_static_string(ctx, val, PyString_AsString(pyVal));
172
 
                else
173
 
                {
174
 
                    pcilib_error("Invalid input. Input type should be int, float or string.");
175
 
                    return PCILIB_ERROR_NOTSUPPORTED;
176
 
                }
177
 
    if(err)
178
 
        return err;
179
 
        
180
 
    return 0;
181
 
}
182
 
 
183
136
 
184
137
/*!
185
138
 * \brief Reads register value. Wrap for pcilib_read_register function.
191
144
{
192
145
        if(!__ctx)
193
146
        {
194
 
        __setPyExeptionText("pcilib_t handler not initialized");
 
147
        pcilib_error("pcilib_t handler not initialized");
195
148
                return NULL;
196
149
        }
197
150
 
203
156
        err = pcilib_read_register(__ctx, bank, regname, &reg_value);
204
157
        if(err)
205
158
        {
206
 
        __setPyExeptionText("Failed: pcilib_read_register (%i)", err);
 
159
        pcilib_error("Failed: pcilib_read_register (%i)", err);
207
160
                return NULL;
208
161
        }
209
162
        
210
163
        err = pcilib_set_value_from_register_value(__ctx, &val, reg_value);
211
164
        if(err)
212
165
        {
213
 
        __setPyExeptionText("Failed: pcilib_set_value_from_register_value (%i)", err);
 
166
        pcilib_error("Failed: pcilib_set_value_from_register_value (%i)", err);
214
167
                return NULL;
215
168
        }
216
169
 
217
 
    return pcilib_convert_val_to_pyobject(__ctx, &val, __setPyExeptionText);
 
170
    return pcilib_convert_val_to_pyobject(__ctx, &val);
218
171
}
219
172
 
220
173
/*!
228
181
{
229
182
        if(!__ctx)
230
183
        {
231
 
        __setPyExeptionText("pcilib_t handler not initialized");
 
184
        pcilib_error("pcilib_t handler not initialized");
232
185
                return NULL;
233
186
        }
234
187
        
239
192
        err = pcilib_convert_pyobject_to_val(__ctx, val, &val_internal);
240
193
        if(err)
241
194
        {
242
 
        __setPyExeptionText("Failed pcilib_convert_pyobject_to_val (%i)", err);
 
195
        pcilib_error("Failed pcilib_convert_pyobject_to_val (%i)", err);
243
196
                return NULL;
244
197
        }
245
198
        
246
199
        reg_value = pcilib_get_value_as_register_value(__ctx, &val_internal, &err);
247
200
        if(err)
248
201
        {
249
 
        __setPyExeptionText("Failed: pcilib_get_value_as_register_value (%i)", err);
 
202
        pcilib_error("Failed: pcilib_get_value_as_register_value (%i)", err);
250
203
                return NULL;
251
204
        }
252
205
        
253
206
        err = pcilib_write_register(__ctx, bank, regname, reg_value);
254
207
        if(err)
255
208
        {
256
 
        __setPyExeptionText("Failed: pcilib_write_register (%i)", err);
 
209
        pcilib_error("Failed: pcilib_write_register (%i)", err);
257
210
                return NULL;
258
211
        }
259
212
    return PyInt_FromLong((long)1);
268
221
{
269
222
        if(!__ctx)
270
223
        {
271
 
        __setPyExeptionText("pcilib_t handler not initialized");
 
224
        pcilib_error("pcilib_t handler not initialized");
272
225
                return NULL;
273
226
        }
274
227
        
279
232
        
280
233
        if(err)
281
234
        {
282
 
            __setPyExeptionText("Failed pcilib_get_property (%i)", err);
 
235
            pcilib_error("Failed pcilib_get_property (%i)", err);
283
236
                        return NULL;
284
237
        }
285
238
        
286
 
    return pcilib_convert_val_to_pyobject(__ctx, &val, __setPyExeptionText);
 
239
    return pcilib_convert_val_to_pyobject(__ctx, &val);
287
240
}
288
241
 
289
242
/*!
298
251
        
299
252
        if(!__ctx)
300
253
        {
301
 
        __setPyExeptionText("pcilib_t handler not initialized");
 
254
        pcilib_error("pcilib_t handler not initialized");
302
255
                return NULL;
303
256
        }
304
257
        
306
259
        err = pcilib_convert_pyobject_to_val(__ctx, val, &val_internal);
307
260
        if(err)
308
261
        {
309
 
        __setPyExeptionText("Failed pcilib_convert_pyobject_to_val (%i)", err);
 
262
        pcilib_error("Failed pcilib_convert_pyobject_to_val (%i)", err);
310
263
                return NULL;
311
264
        }
312
265
        
314
267
        
315
268
        if(err)
316
269
        {
317
 
        __setPyExeptionText("Failed pcilib_get_property (%i)", err);
 
270
        pcilib_error("Failed pcilib_get_property (%i)", err);
318
271
                return NULL;
319
272
        }
320
273
        
323
276
 
324
277
void add_pcilib_value_to_dict(PyObject* dict, pcilib_value_t* val, const char *name)
325
278
{
326
 
        PyObject *py_val = pcilib_convert_val_to_pyobject(__ctx, val, NULL);
 
279
    PyObject *py_val = pcilib_convert_val_to_pyobject(__ctx, val);
327
280
 
328
281
        if(py_val)
329
282
                PyDict_SetItem(dict,
503
456
{
504
457
        if(!__ctx)
505
458
        {
506
 
        __setPyExeptionText("pcilib_t handler not initialized");
 
459
        pcilib_error("pcilib_t handler not initialized");
507
460
                return NULL;
508
461
        }
509
462
        
526
479
{
527
480
    if(!__ctx)
528
481
    {
529
 
        __setPyExeptionText("pcilib_t handler not initialized");
 
482
        pcilib_error("pcilib_t handler not initialized");
530
483
        return NULL;
531
484
    }
532
485
 
534
487
 
535
488
        if(!info)
536
489
        {
537
 
                __setPyExeptionText("Failed pcilib_get_register_info");
 
490
        pcilib_error("Failed pcilib_get_register_info");
538
491
        return NULL;
539
492
        }
540
493
 
549
502
{
550
503
    if(!__ctx)
551
504
    {
552
 
        __setPyExeptionText("pcilib_t handler not initialized");
 
505
        pcilib_error("pcilib_t handler not initialized");
553
506
        return NULL;
554
507
    }
555
508