/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-08 10:55:33 UTC
  • mto: This revision was merged to the branch mainline in revision 353.
  • Revision ID: vchernov@inr.ru-20160208105533-fl0uue3ukv6di13b
Add support for setting register value to script transfrom. Add set_property and get_property functions to pcipywrap. Cleaning cmakelists from unused dependencies

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "pcilib.h"
2
 
#include <Python.h>
3
1
#include "pci.h"
4
2
#include "error.h"
 
3
#include "pcipywrap.h"
5
4
 
6
5
/*!
7
6
 * \brief Global pointer to pcilib_t context.
25
24
}
26
25
 
27
26
/*!
 
27
 * \brief Sets python exeption text. Function interface same as printf.
 
28
 */
 
29
void setPyExeptionText(const char* msg, ...)
 
30
{
 
31
        char *buf;
 
32
        size_t sz;
 
33
        
 
34
        va_list vl, vl_copy;
 
35
    va_start(vl, msg);
 
36
        va_copy(vl_copy, vl);
 
37
        
 
38
        sz = vsnprintf(NULL, 0, msg, vl);
 
39
        buf = (char *)malloc(sz + 1);
 
40
        
 
41
        if(!buf)
 
42
        {
 
43
                PyErr_SetString(PyExc_Exception, "Memory error");
 
44
                return;
 
45
        }
 
46
 
 
47
        vsnprintf(buf, sz+1, msg, vl_copy);
 
48
        va_end(vl_copy);
 
49
        va_end(vl);
 
50
        
 
51
        PyErr_SetString(PyExc_Exception, buf);
 
52
        free(buf);
 
53
}
 
54
 
 
55
/*!
28
56
 * \brief Sets pcilib context to wraper.
29
57
 * \param[in] addr Pointer to pcilib_t, serialized to bytearray
 
58
 * \return true, serialized to PyObject, NULL with exeption text, if failed.
30
59
 */
31
 
void __setPcilib(PyObject* addr)
 
60
PyObject* __setPcilib(PyObject* addr)
32
61
{
33
62
        if(!PyByteArray_Check(addr))
34
63
        {
35
 
                PyErr_SetString(PyExc_Exception, "Incorrect addr type. Only bytearray is allowed");
36
 
                return NULL;
 
64
                setPyExeptionText(PyExc_Exception, "Incorrect addr type. Only bytearray is allowed");
 
65
                return;
37
66
        }
38
67
        
39
68
        //deserializing adress
44
73
                ((char*)&__ctx)[i] = pAddr[i];
45
74
 
46
75
        free(pAddr);
47
 
}
 
76
        
 
77
        return PyInt_FromLong((long)1);
 
78
}
 
79
 
 
80
PyObject* pcilib_convert_val_to_pyobject(pcilib_t* ctx, pcilib_value_t *val, void (*errstream)(const char* msg, ...))
 
81
{
 
82
        int err;
 
83
        
 
84
        switch(val->type)
 
85
        {
 
86
                case PCILIB_TYPE_INVALID:
 
87
            errstream("Invalid register output type (PCILIB_TYPE_INVALID)");
 
88
                        return NULL;
 
89
                        
 
90
                case PCILIB_TYPE_STRING:
 
91
            errstream("Invalid register output type (PCILIB_TYPE_STRING)");
 
92
                        return NULL;
 
93
                
 
94
                case PCILIB_TYPE_LONG:
 
95
                {
 
96
                        long ret;
 
97
                        ret = pcilib_get_value_as_int(__ctx, val, &err);
 
98
                        
 
99
                        if(err)
 
100
                        {
 
101
                errstream("Failed: pcilib_get_value_as_int (%i)", err);
 
102
                                return NULL;
 
103
                        }
 
104
                        return PyInt_FromLong((long) ret);
 
105
                }
 
106
                
 
107
                case PCILIB_TYPE_DOUBLE:
 
108
                {
 
109
                        double ret;
 
110
                        ret = pcilib_get_value_as_float(__ctx, val, &err);
 
111
                        
 
112
                        if(err)
 
113
                        {
 
114
                errstream("Failed: pcilib_get_value_as_int (%i)", err);
 
115
                                return NULL;
 
116
                        }
 
117
                        return PyFloat_FromDouble((double) ret);
 
118
                }
 
119
                
 
120
                default:
 
121
            errstream("Invalid register output type (unknown)");
 
122
                        return NULL;
 
123
        }
 
124
}
 
125
 
 
126
int pcilib_convert_pyobject_to_val(pcilib_t* ctx, PyObject* pyVal, pcilib_value_t *val)
 
127
{
 
128
        int err;
 
129
        
 
130
    if(PyInt_Check(pyVal))
 
131
    {
 
132
        err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(pyVal));
 
133
    }
 
134
    else
 
135
        if(PyFloat_Check(pyVal))
 
136
            err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(pyVal));
 
137
        else
 
138
            if(PyString_Check(pyVal))
 
139
                err = pcilib_set_value_from_static_string(ctx, val, PyString_AsString(pyVal));
 
140
                else
 
141
                {
 
142
                    pcilib_error("Invalid input. Input type should be int, float or string.");
 
143
                    return PCILIB_ERROR_NOTSUPPORTED;
 
144
                }
 
145
    if(err)
 
146
        return err;
 
147
        
 
148
    return 0;
 
149
}
 
150
 
48
151
 
49
152
/*!
50
153
 * \brief Reads register value.
53
156
 * \return register value, can be integer or float type
54
157
 */
55
158
PyObject* read_register(const char *regname, const char *bank)
56
 
{       
 
159
{
57
160
        if(!__ctx)
58
161
        {
59
 
                PyErr_SetString(PyExc_Exception, "pcilib_t handler not initialized");
 
162
                setPyExeptionText("pcilib_t handler not initialized");
60
163
                return NULL;
61
164
        }
62
165
    
70
173
        err = pcilib_read_register(__ctx, bank, regname, &reg_value);
71
174
        if(err)
72
175
        {
73
 
                PyErr_SetString(PyExc_Exception, "Failed: read_register");
 
176
                setPyExeptionText("Failed: read_register (%i)", err);
74
177
                return NULL;
75
178
        }
76
179
        
77
180
        err = pcilib_set_value_from_register_value(__ctx, &val, reg_value);
78
 
        
79
 
        if(err)
80
 
        {
81
 
                PyErr_SetString(PyExc_Exception, "Failed: pcilib_set_value_from_register_value");
82
 
                return NULL;
83
 
        }
84
 
 
85
 
        switch(val.type)
86
 
        {
87
 
                case PCILIB_TYPE_INVALID:
88
 
                        PyErr_SetString(PyExc_Exception, "Invalid register output type (PCILIB_TYPE_INVALID)");
89
 
                        return NULL;
90
 
                        
91
 
                case PCILIB_TYPE_STRING:
92
 
                        PyErr_SetString(PyExc_Exception, "Invalid register output type (PCILIB_TYPE_STRING)");
93
 
                        return NULL;
94
 
                
95
 
                case PCILIB_TYPE_LONG:
96
 
                {
97
 
                        long ret;
98
 
                        ret = pcilib_get_value_as_int(__ctx, &val, &err);
99
 
                        
100
 
                        if(err)
101
 
                        {
102
 
                                PyErr_SetString(PyExc_Exception, "Failed: pcilib_get_value_as_int");
103
 
                                return NULL;
104
 
                        }
105
 
                        return PyInt_FromLong((long) ret);
106
 
                }
107
 
                
108
 
                case PCILIB_TYPE_DOUBLE:
109
 
                {
110
 
                        double ret;
111
 
                        ret = pcilib_get_value_as_float(__ctx, &val, &err);
112
 
                        
113
 
                        if(err)
114
 
                        {
115
 
                                PyErr_SetString(PyExc_Exception, "Failed: pcilib_get_value_as_int");
116
 
                                return NULL;
117
 
                        }
118
 
                        return PyFloat_FromDouble((double) ret);
119
 
                }
120
 
                
121
 
                default:
122
 
                        PyErr_SetString(PyExc_Exception, "Invalid register output type (unknown)");
123
 
                        return NULL;
124
 
        }
125
 
}
 
181
        if(err)
 
182
        {
 
183
                setPyExeptionText("Failed: pcilib_set_value_from_register_value (%i)", err);
 
184
                return NULL;
 
185
        }
 
186
 
 
187
    return pcilib_convert_val_to_pyobject(__ctx, &val, setPyExeptionText);
 
188
}
 
189
 
 
190
 
 
191
PyObject* get_property(const char *prop)
 
192
{
 
193
        if(!__ctx)
 
194
        {
 
195
                setPyExeptionText("pcilib_t handler not initialized");
 
196
                return NULL;
 
197
        }
 
198
        
 
199
        int err;
 
200
        pcilib_value_t val = {0};
 
201
        
 
202
        err  = pcilib_get_property(__ctx, prop, &val);
 
203
        
 
204
        if(err)
 
205
        {
 
206
                        setPyExeptionText("Failed pcilib_get_property (%i)", err);
 
207
                        return NULL;
 
208
        }
 
209
        
 
210
    return pcilib_convert_val_to_pyobject(__ctx, &val, setPyExeptionText);
 
211
}
 
212
 
 
213
PyObject* set_property(const char *prop, PyObject* val)
 
214
{
 
215
        int err;
 
216
        
 
217
        if(!__ctx)
 
218
        {
 
219
                setPyExeptionText("pcilib_t handler not initialized");
 
220
                return NULL;
 
221
        }
 
222
        
 
223
        pcilib_value_t val_internal = {0};
 
224
        err = pcilib_convert_pyobject_to_val(__ctx, val, &val_internal);
 
225
        if(err)
 
226
        {
 
227
                setPyExeptionText("pcilib_convert_pyobject_to_val (%i)", err);
 
228
                return NULL;
 
229
        }
 
230
        
 
231
        err  = pcilib_set_property(__ctx, prop, &val_internal);
 
232
        
 
233
        if(err)
 
234
        {
 
235
                setPyExeptionText("Failed pcilib_get_property (%i)", err);
 
236
                return NULL;
 
237
        }
 
238
        
 
239
        return PyInt_FromLong((long)1);
 
240
}
 
241