/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 views/script.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:
1
 
#define _PCILIB_VIEW_TRANSFORM_C
2
 
 
3
 
#include <stdio.h>
4
 
#include <stdlib.h>
5
 
 
6
 
#include "pci.h"
7
 
#include "pcipywrap.h"
8
 
#include "error.h"
9
 
 
10
 
#include "model.h"
11
 
#include "script.h"
12
 
 
13
 
static int pcilib_script_view_module_init(pcilib_t *ctx, pcilib_script_view_description_t *v)
14
 
{
15
 
        int err;
16
 
        
17
 
        //Initialize python script, if it has not initialized already.
18
 
        if(!v->py_script_module)
19
 
        {
20
 
                if(!v->script_name)
21
 
                {
22
 
                        pcilib_error("Invalid script name specified in XML property (NULL)");
23
 
                        return PCILIB_ERROR_INVALID_DATA;
24
 
                }
25
 
                
26
 
                //create path string to scripts
27
 
                char* model_dir = getenv("PCILIB_MODEL_DIR");
28
 
                char* model_path = malloc(strlen(model_dir) + strlen(ctx->model) + 2);
29
 
                if (!model_path) return PCILIB_ERROR_MEMORY;
30
 
                sprintf(model_path, "%s/%s", model_dir, ctx->model);
31
 
                
32
 
                //set model path to python
33
 
                PySys_SetPath(model_path);
34
 
                free(model_path);
35
 
                model_path = NULL;
36
 
                
37
 
                //create path string to pcipywrap library
38
 
                char* app_dir = getenv("APP_PATH");
39
 
                char* pcipywrap_path;
40
 
                if(app_dir)
41
 
                {
42
 
                        pcipywrap_path = malloc(strlen(app_dir) + strlen("/pcilib"));
43
 
                        if (!pcipywrap_path) return PCILIB_ERROR_MEMORY;
44
 
                        sprintf(pcipywrap_path, "%s/%s", "/pcilib", ctx->model);
45
 
                }
46
 
                else
47
 
                {
48
 
                        pcipywrap_path = malloc(strlen("./pcilib"));
49
 
                        if (!pcipywrap_path) return PCILIB_ERROR_MEMORY;
50
 
                        sprintf(pcipywrap_path, "%s", "./pcilib");
51
 
        
52
 
                }
53
 
                
54
 
                //set pcipywrap library path to python
55
 
                PyObject* path = PySys_GetObject("path");
56
 
                if(PyList_Append(path, PyString_FromString(pcipywrap_path)) == -1)
57
 
                {
58
 
                        pcilib_error("Cant set pcipywrap library path to python.");
59
 
                        return PCILIB_ERROR_FAILED;
60
 
                }
61
 
                free(pcipywrap_path);
62
 
                pcipywrap_path = NULL;
63
 
                
64
 
                
65
 
                //extract module name from script name
66
 
                char* py_module_name = strtok(v->script_name, ".");
67
 
                
68
 
                if(!py_module_name)
69
 
                {
70
 
                        pcilib_error("Invalid script name specified in XML property (%s)."
71
 
                                                 " Seems like name doesnt contains extension", v->script_name);
72
 
                        return PCILIB_ERROR_INVALID_DATA;
73
 
                }
74
 
                
75
 
                //import python script
76
 
                v->py_script_module = PyImport_ImportModule(py_module_name);
77
 
                
78
 
                if(!v->py_script_module)
79
 
                {
80
 
                        printf("Error in import python module: ");
81
 
                        PyErr_Print();
82
 
                        return PCILIB_ERROR_INVALID_DATA;
83
 
                }
84
 
        }
85
 
        
86
 
        //Initializing pcipywrap module if script use it
87
 
        PyObject* dict = PyModule_GetDict(v->py_script_module);
88
 
        if(PyDict_Contains(dict, PyString_FromString("pcipywrap")))
89
 
        {
90
 
                PyObject* pcipywrap_module = PyDict_GetItemString(dict, "pcipywrap");
91
 
                if(!pcipywrap_module)
92
 
                {
93
 
                        pcilib_error("Cant extract pcipywrap module from script dictionary");
94
 
                        return PCILIB_ERROR_FAILED;
95
 
                }
96
 
       
97
 
       //setting pcilib_t instance                 
98
 
           PyObject_CallMethodObjArgs(pcipywrap_module,
99
 
                               PyUnicode_FromString("__setPcilib"),
100
 
                               PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*)),
101
 
                               NULL);
102
 
        }
103
 
        
104
 
        return 0;
105
 
}
106
 
 
107
 
static int pcilib_script_view_read(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t regval, pcilib_value_t *val) {
108
 
        
109
 
        int err;
110
 
 
111
 
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
112
 
    pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(model_info->views[view_ctx->view]);
113
 
        
114
 
        err = pcilib_script_view_module_init(ctx, v);
115
 
        if(err)
116
 
                return err;
117
 
        
118
 
        PyObject *ret = PyObject_CallMethod(v->py_script_module, "read_from_register", "()");
119
 
        if (!ret) 
120
 
        {
121
 
           printf("Python script error: ");
122
 
           PyErr_Print();
123
 
           return PCILIB_ERROR_FAILED;
124
 
        }
125
 
        
126
 
        err = pcilib_convert_pyobject_to_val(ctx, ret, val);
127
 
        
128
 
        if(err)
129
 
        {
130
 
                pcilib_error("Failed to convert python script return value to internal type: %i", err);
131
 
                return err;
132
 
        }
133
 
    return 0;
134
 
}
135
 
 
136
 
static int pcilib_script_view_write(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t *regval, pcilib_value_t *val) {
137
 
        
138
 
        int err;
139
 
 
140
 
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
141
 
    pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(model_info->views[view_ctx->view]);
142
 
        
143
 
        err = pcilib_script_view_module_init(ctx, v);
144
 
        if(err)
145
 
                return err;
146
 
                
147
 
        PyObject *input = pcilib_convert_val_to_pyobject(ctx, val, printf);
148
 
        if(!input)
149
 
        {
150
 
           printf("Failed to convert input value to Python object");
151
 
           PyErr_Print();
152
 
           return PCILIB_ERROR_FAILED;
153
 
        }
154
 
        
155
 
        PyObject *ret = PyObject_CallMethodObjArgs(v->py_script_module,
156
 
                                                                                           PyUnicode_FromString("write_to_register"),
157
 
                                                                                           input,
158
 
                                                                                           NULL);
159
 
        if (!ret) 
160
 
        {
161
 
           printf("Python script error: ");
162
 
           PyErr_Print();
163
 
           return PCILIB_ERROR_FAILED;
164
 
        }
165
 
        
166
 
        //convert output value back to pcilib_value_t
167
 
        //no need because it wont be used later, and the script return could be none
168
 
        /*
169
 
        err = pcilib_convert_pyobject_to_val(ctx, ret, val);
170
 
        if(err)
171
 
        {
172
 
                pcilib_error("failed to convert script write_to_register function return value to internal type: %i", err);
173
 
                return err;
174
 
        }
175
 
        
176
 
        uint64_t output = pcilib_get_value_as_register_value(ctx, val, &err);
177
 
        if(err)
178
 
        {
179
 
                pcilib_error("failed to convert value to register value (%i)", err);
180
 
                return err;
181
 
        }
182
 
        regval[0] = output;
183
 
        */
184
 
 
185
 
    return 0;
186
 
}
187
 
 
188
 
void pcilib_script_view_free_description (pcilib_t *ctx, pcilib_view_description_t *view)
189
 
{
190
 
        pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(view);
191
 
        
192
 
        if(v->script_name)
193
 
        {
194
 
                free(v->script_name);
195
 
                v->script_name = NULL;
196
 
        }
197
 
        
198
 
        if(v->py_script_module)
199
 
        {
200
 
                PyObject_Free(v->py_script_module);
201
 
                v->py_script_module = NULL;
202
 
        }
203
 
}
204
 
 
205
 
const pcilib_view_api_description_t pcilib_script_view_api =
206
 
  { PCILIB_VERSION, sizeof(pcilib_script_view_description_t), NULL, NULL, pcilib_script_view_free_description, pcilib_script_view_read, pcilib_script_view_write};