From 5349163e93a107fdfea2ad58760f6149a8d749b0 Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Fri, 5 Feb 2016 12:33:48 +0100 Subject: Add support for python script properties. Correct pcilib python wrapping. Update examples. Update cmakelists for work in shadow build mode. --- views/CMakeLists.txt | 4 +- views/script.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++ views/script.h | 18 ++++++ 3 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 views/script.c create mode 100644 views/script.h (limited to 'views') diff --git a/views/CMakeLists.txt b/views/CMakeLists.txt index 0e0c20b..c060067 100644 --- a/views/CMakeLists.txt +++ b/views/CMakeLists.txt @@ -8,6 +8,6 @@ include_directories( ${UTHASH_INCLUDE_DIRS} ) -set(HEADERS ${HEADERS} enum.h transform.h register.h) +set(HEADERS ${HEADERS} enum.h transform.h register.h script.h) -add_library(views STATIC enum.c transform.c register.c) +add_library(views STATIC enum.c transform.c register.c script.c) diff --git a/views/script.c b/views/script.c new file mode 100644 index 0000000..50b31b5 --- /dev/null +++ b/views/script.c @@ -0,0 +1,157 @@ +#define _PCILIB_VIEW_TRANSFORM_C + +#include +#include + +#include "pci.h" +#include "error.h" + +#include "model.h" +#include "script.h" + +static int pcilib_script_view_read(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t regval, pcilib_value_t *val) { + + int err; + + const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); + pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(model_info->views[view_ctx->view]); + + //Initialize python script, if it has not initialized already. + if(!v->py_script_module) + { + if(!v->script_name) + { + pcilib_error("Invalid script name specified in XML property (NULL)"); + return PCILIB_ERROR_INVALID_DATA; + } + + //create path string to scripts + char* model_dir = getenv("PCILIB_MODEL_DIR"); + char* model_path = malloc(strlen(model_dir) + strlen(ctx->model) + 2); + if (!model_path) return PCILIB_ERROR_MEMORY; + sprintf(model_path, "%s/%s", model_dir, ctx->model); + + //set model path to python + PySys_SetPath(model_path); + free(model_path); + model_path = NULL; + + //create path string to pcipywrap library + char* app_dir = getenv("APP_PATH"); + char* pcipywrap_path; + if(app_dir) + { + pcipywrap_path = malloc(strlen(app_dir) + strlen("/pcilib")); + if (!pcipywrap_path) return PCILIB_ERROR_MEMORY; + sprintf(pcipywrap_path, "%s/%s", "/pcilib", ctx->model); + } + else + { + pcipywrap_path = malloc(strlen("./pcilib")); + if (!pcipywrap_path) return PCILIB_ERROR_MEMORY; + sprintf(pcipywrap_path, "%s", "./pcilib"); + + } + + //set pcipywrap library path to python + PyObject* path = PySys_GetObject("path"); + if(PyList_Append(path, PyString_FromString(pcipywrap_path)) == -1) + { + pcilib_error("Cant set pcipywrap library path to python."); + return PCILIB_ERROR_FAILED; + } + free(pcipywrap_path); + pcipywrap_path = NULL; + + + //extract module name from script name + char* py_module_name = strtok(v->script_name, "."); + + if(!py_module_name) + { + pcilib_error("Invalid script name specified in XML property (%s)." + " Seems like name doesnt contains extension", v->script_name); + return PCILIB_ERROR_INVALID_DATA; + } + + //import python script + v->py_script_module = PyImport_ImportModule(py_module_name); + + if(!v->py_script_module) + { + printf("Error in import python module: "); + PyErr_Print(); + return PCILIB_ERROR_INVALID_DATA; + } + } + + //Initializing pcipywrap module if script use it + PyObject* dict = PyModule_GetDict(v->py_script_module); + if(PyDict_Contains(dict, PyString_FromString("pcipywrap"))) + { + PyObject* pcipywrap_module = PyDict_GetItemString(dict, "pcipywrap"); + if(!pcipywrap_module) + { + pcilib_error("Cant extract pcipywrap module from script dictionary"); + return PCILIB_ERROR_FAILED; + } + + //setting pcilib_t instance + PyObject_CallMethodObjArgs(pcipywrap_module, + PyUnicode_FromString("__setPcilib"), + PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*)), + NULL); + } + + + PyObject *ret = PyObject_CallMethod(v->py_script_module, "read_from_register", "()"); + if (!ret) + { + printf("Python script error: "); + PyErr_Print(); + return PCILIB_ERROR_FAILED; + } + + if(PyInt_Check(ret)) + { + err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(ret)); + } + else + if(PyFloat_Check(ret)) + err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(ret)); + else + if(PyString_Check(ret)) + err = pcilib_set_value_from_static_string(ctx, val, PyString_AsString(ret)); + else + { + pcilib_error("Invalid return type in read_from_register() method. Return type should be int, float or string."); + return PCILIB_ERROR_NOTSUPPORTED; + } + if(err) + { + pcilib_error("Failed to convert python script return value to internal type: %i", err); + return err; + } + return 0; +} + + +void pcilib_script_view_free_description (pcilib_t *ctx, pcilib_view_description_t *view) +{ + pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(view); + + if(v->script_name) + { + free(v->script_name); + v->script_name = NULL; + } + + if(v->py_script_module) + { + PyObject_Free(v->py_script_module); + v->py_script_module = NULL; + } +} + +const pcilib_view_api_description_t pcilib_script_view_api = + { PCILIB_VERSION, sizeof(pcilib_script_view_description_t), NULL, NULL, pcilib_script_view_free_description, pcilib_script_view_read, NULL}; diff --git a/views/script.h b/views/script.h new file mode 100644 index 0000000..2929f4c --- /dev/null +++ b/views/script.h @@ -0,0 +1,18 @@ +#ifndef _PCILIB_VIEW_SCRIPT_H +#define _PCILIB_VIEW_SCRIPT_H + +#include +#include +#include + +typedef struct { + pcilib_view_description_t base; + PyObject *py_script_module; /**< PyModule object, contains script enviroment */ + char* script_name; +} pcilib_script_view_description_t; + +#ifndef _PCILIB_VIEW_SCRIPT_C +const pcilib_view_api_description_t pcilib_script_view_api; +#endif /* _PCILIB_VIEW_SCRIPT_C */ + +#endif /* _PCILIB_VIEW_SCRIPT_H */ -- cgit v1.2.3 From e10e102b8b0ff3bf35f16b5276d56059d242e1fd Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Mon, 8 Feb 2016 11:55:33 +0100 Subject: Add support for setting register value to script transfrom. Add set_property and get_property functions to pcipywrap. Cleaning cmakelists from unused dependencies --- views/script.c | 91 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 21 deletions(-) (limited to 'views') diff --git a/views/script.c b/views/script.c index 50b31b5..3d018e9 100644 --- a/views/script.c +++ b/views/script.c @@ -4,17 +4,15 @@ #include #include "pci.h" +#include "pcipywrap.h" #include "error.h" #include "model.h" #include "script.h" -static int pcilib_script_view_read(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t regval, pcilib_value_t *val) { - +static int pcilib_script_view_module_init(pcilib_t *ctx, pcilib_script_view_description_t *v) +{ int err; - - const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); - pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(model_info->views[view_ctx->view]); //Initialize python script, if it has not initialized already. if(!v->py_script_module) @@ -103,6 +101,19 @@ static int pcilib_script_view_read(pcilib_t *ctx, pcilib_view_context_t *view_ct NULL); } + return 0; +} + +static int pcilib_script_view_read(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t regval, pcilib_value_t *val) { + + int err; + + const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); + pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(model_info->views[view_ctx->view]); + + err = pcilib_script_view_module_init(ctx, v); + if(err) + return err; PyObject *ret = PyObject_CallMethod(v->py_script_module, "read_from_register", "()"); if (!ret) @@ -112,30 +123,68 @@ static int pcilib_script_view_read(pcilib_t *ctx, pcilib_view_context_t *view_ct return PCILIB_ERROR_FAILED; } - if(PyInt_Check(ret)) + err = pcilib_convert_pyobject_to_val(ctx, ret, val); + + if(err) { - err = pcilib_set_value_from_int(ctx, val, PyInt_AsLong(ret)); + pcilib_error("Failed to convert python script return value to internal type: %i", err); + return err; } - else - if(PyFloat_Check(ret)) - err = pcilib_set_value_from_float(ctx, val, PyFloat_AsDouble(ret)); - else - if(PyString_Check(ret)) - err = pcilib_set_value_from_static_string(ctx, val, PyString_AsString(ret)); - else - { - pcilib_error("Invalid return type in read_from_register() method. Return type should be int, float or string."); - return PCILIB_ERROR_NOTSUPPORTED; - } + return 0; +} + +static int pcilib_script_view_write(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t *regval, pcilib_value_t *val) { + + int err; + + const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); + pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(model_info->views[view_ctx->view]); + + err = pcilib_script_view_module_init(ctx, v); if(err) + return err; + + PyObject *input = pcilib_convert_val_to_pyobject(ctx, val, printf); + if(!input) { - pcilib_error("Failed to convert python script return value to internal type: %i", err); + printf("Failed to convert input value to Python object"); + PyErr_Print(); + return PCILIB_ERROR_FAILED; + } + + PyObject *ret = PyObject_CallMethodObjArgs(v->py_script_module, + PyUnicode_FromString("write_to_register"), + input, + NULL); + if (!ret) + { + printf("Python script error: "); + PyErr_Print(); + return PCILIB_ERROR_FAILED; + } + + //convert output value back to pcilib_value_t + //no need because it wont be used later, and the script return could be none + /* + err = pcilib_convert_pyobject_to_val(ctx, ret, val); + if(err) + { + pcilib_error("failed to convert script write_to_register function return value to internal type: %i", err); return err; } + + uint64_t output = pcilib_get_value_as_register_value(ctx, val, &err); + if(err) + { + pcilib_error("failed to convert value to register value (%i)", err); + return err; + } + regval[0] = output; + */ + return 0; } - void pcilib_script_view_free_description (pcilib_t *ctx, pcilib_view_description_t *view) { pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(view); @@ -154,4 +203,4 @@ void pcilib_script_view_free_description (pcilib_t *ctx, pcilib_view_description } const pcilib_view_api_description_t pcilib_script_view_api = - { PCILIB_VERSION, sizeof(pcilib_script_view_description_t), NULL, NULL, pcilib_script_view_free_description, pcilib_script_view_read, NULL}; + { PCILIB_VERSION, sizeof(pcilib_script_view_description_t), NULL, NULL, pcilib_script_view_free_description, pcilib_script_view_read, pcilib_script_view_write}; -- cgit v1.2.3 From d9a31945a92a76e590657dc2e65bb20b6ea95d90 Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Thu, 11 Feb 2016 10:37:24 +0100 Subject: Merge script and transform view. Add get register and properties info to python wrap. --- views/CMakeLists.txt | 4 +- views/script.c | 206 --------------------------------------------------- views/script.h | 18 ----- views/transform.c | 52 +++++++++---- views/transform.h | 2 + 5 files changed, 42 insertions(+), 240 deletions(-) delete mode 100644 views/script.c delete mode 100644 views/script.h (limited to 'views') diff --git a/views/CMakeLists.txt b/views/CMakeLists.txt index c060067..0e0c20b 100644 --- a/views/CMakeLists.txt +++ b/views/CMakeLists.txt @@ -8,6 +8,6 @@ include_directories( ${UTHASH_INCLUDE_DIRS} ) -set(HEADERS ${HEADERS} enum.h transform.h register.h script.h) +set(HEADERS ${HEADERS} enum.h transform.h register.h) -add_library(views STATIC enum.c transform.c register.c script.c) +add_library(views STATIC enum.c transform.c register.c) diff --git a/views/script.c b/views/script.c deleted file mode 100644 index 3d018e9..0000000 --- a/views/script.c +++ /dev/null @@ -1,206 +0,0 @@ -#define _PCILIB_VIEW_TRANSFORM_C - -#include -#include - -#include "pci.h" -#include "pcipywrap.h" -#include "error.h" - -#include "model.h" -#include "script.h" - -static int pcilib_script_view_module_init(pcilib_t *ctx, pcilib_script_view_description_t *v) -{ - int err; - - //Initialize python script, if it has not initialized already. - if(!v->py_script_module) - { - if(!v->script_name) - { - pcilib_error("Invalid script name specified in XML property (NULL)"); - return PCILIB_ERROR_INVALID_DATA; - } - - //create path string to scripts - char* model_dir = getenv("PCILIB_MODEL_DIR"); - char* model_path = malloc(strlen(model_dir) + strlen(ctx->model) + 2); - if (!model_path) return PCILIB_ERROR_MEMORY; - sprintf(model_path, "%s/%s", model_dir, ctx->model); - - //set model path to python - PySys_SetPath(model_path); - free(model_path); - model_path = NULL; - - //create path string to pcipywrap library - char* app_dir = getenv("APP_PATH"); - char* pcipywrap_path; - if(app_dir) - { - pcipywrap_path = malloc(strlen(app_dir) + strlen("/pcilib")); - if (!pcipywrap_path) return PCILIB_ERROR_MEMORY; - sprintf(pcipywrap_path, "%s/%s", "/pcilib", ctx->model); - } - else - { - pcipywrap_path = malloc(strlen("./pcilib")); - if (!pcipywrap_path) return PCILIB_ERROR_MEMORY; - sprintf(pcipywrap_path, "%s", "./pcilib"); - - } - - //set pcipywrap library path to python - PyObject* path = PySys_GetObject("path"); - if(PyList_Append(path, PyString_FromString(pcipywrap_path)) == -1) - { - pcilib_error("Cant set pcipywrap library path to python."); - return PCILIB_ERROR_FAILED; - } - free(pcipywrap_path); - pcipywrap_path = NULL; - - - //extract module name from script name - char* py_module_name = strtok(v->script_name, "."); - - if(!py_module_name) - { - pcilib_error("Invalid script name specified in XML property (%s)." - " Seems like name doesnt contains extension", v->script_name); - return PCILIB_ERROR_INVALID_DATA; - } - - //import python script - v->py_script_module = PyImport_ImportModule(py_module_name); - - if(!v->py_script_module) - { - printf("Error in import python module: "); - PyErr_Print(); - return PCILIB_ERROR_INVALID_DATA; - } - } - - //Initializing pcipywrap module if script use it - PyObject* dict = PyModule_GetDict(v->py_script_module); - if(PyDict_Contains(dict, PyString_FromString("pcipywrap"))) - { - PyObject* pcipywrap_module = PyDict_GetItemString(dict, "pcipywrap"); - if(!pcipywrap_module) - { - pcilib_error("Cant extract pcipywrap module from script dictionary"); - return PCILIB_ERROR_FAILED; - } - - //setting pcilib_t instance - PyObject_CallMethodObjArgs(pcipywrap_module, - PyUnicode_FromString("__setPcilib"), - PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*)), - NULL); - } - - return 0; -} - -static int pcilib_script_view_read(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t regval, pcilib_value_t *val) { - - int err; - - const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); - pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(model_info->views[view_ctx->view]); - - err = pcilib_script_view_module_init(ctx, v); - if(err) - return err; - - PyObject *ret = PyObject_CallMethod(v->py_script_module, "read_from_register", "()"); - if (!ret) - { - printf("Python script error: "); - PyErr_Print(); - return PCILIB_ERROR_FAILED; - } - - err = pcilib_convert_pyobject_to_val(ctx, ret, val); - - if(err) - { - pcilib_error("Failed to convert python script return value to internal type: %i", err); - return err; - } - return 0; -} - -static int pcilib_script_view_write(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t *regval, pcilib_value_t *val) { - - int err; - - const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); - pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(model_info->views[view_ctx->view]); - - err = pcilib_script_view_module_init(ctx, v); - if(err) - return err; - - PyObject *input = pcilib_convert_val_to_pyobject(ctx, val, printf); - if(!input) - { - printf("Failed to convert input value to Python object"); - PyErr_Print(); - return PCILIB_ERROR_FAILED; - } - - PyObject *ret = PyObject_CallMethodObjArgs(v->py_script_module, - PyUnicode_FromString("write_to_register"), - input, - NULL); - if (!ret) - { - printf("Python script error: "); - PyErr_Print(); - return PCILIB_ERROR_FAILED; - } - - //convert output value back to pcilib_value_t - //no need because it wont be used later, and the script return could be none - /* - err = pcilib_convert_pyobject_to_val(ctx, ret, val); - if(err) - { - pcilib_error("failed to convert script write_to_register function return value to internal type: %i", err); - return err; - } - - uint64_t output = pcilib_get_value_as_register_value(ctx, val, &err); - if(err) - { - pcilib_error("failed to convert value to register value (%i)", err); - return err; - } - regval[0] = output; - */ - - return 0; -} - -void pcilib_script_view_free_description (pcilib_t *ctx, pcilib_view_description_t *view) -{ - pcilib_script_view_description_t *v = (pcilib_script_view_description_t*)(view); - - if(v->script_name) - { - free(v->script_name); - v->script_name = NULL; - } - - if(v->py_script_module) - { - PyObject_Free(v->py_script_module); - v->py_script_module = NULL; - } -} - -const pcilib_view_api_description_t pcilib_script_view_api = - { PCILIB_VERSION, sizeof(pcilib_script_view_description_t), NULL, NULL, pcilib_script_view_free_description, pcilib_script_view_read, pcilib_script_view_write}; diff --git a/views/script.h b/views/script.h deleted file mode 100644 index 2929f4c..0000000 --- a/views/script.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef _PCILIB_VIEW_SCRIPT_H -#define _PCILIB_VIEW_SCRIPT_H - -#include -#include -#include - -typedef struct { - pcilib_view_description_t base; - PyObject *py_script_module; /**< PyModule object, contains script enviroment */ - char* script_name; -} pcilib_script_view_description_t; - -#ifndef _PCILIB_VIEW_SCRIPT_C -const pcilib_view_api_description_t pcilib_script_view_api; -#endif /* _PCILIB_VIEW_SCRIPT_C */ - -#endif /* _PCILIB_VIEW_SCRIPT_H */ diff --git a/views/transform.c b/views/transform.c index de7ee0e..f47e4ef 100644 --- a/views/transform.c +++ b/views/transform.c @@ -12,34 +12,58 @@ static int pcilib_transform_view_read(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t regval, pcilib_value_t *val) { - int err; - const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(model_info->views[view_ctx->view]); - err = pcilib_set_value_from_register_value(ctx, val, regval); - if (err) return err; + if(v->script) + { + return pcilib_script_read(ctx, v->script, val); + } + else + { + int err; + + err = pcilib_set_value_from_register_value(ctx, val, regval); + if (err) return err; - return pcilib_py_eval_string(ctx, v->read_from_reg, val); + return pcilib_py_eval_string(ctx, v->read_from_reg, val); + } } static int pcilib_transform_view_write(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t *regval, const pcilib_value_t *val) { - int err = 0; - pcilib_value_t val_copy = {0}; + + const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(model_info->views[view_ctx->view]); - err = pcilib_copy_value(ctx, &val_copy, val); - if (err) return err; - err = pcilib_py_eval_string(ctx, v->write_to_reg, &val_copy); - if (err) return err; + if(!v->script) + { + int err = 0; + + pcilib_value_t val_copy = {0}; + err = pcilib_copy_value(ctx, &val_copy, val); + if (err) return err; + + err = pcilib_py_eval_string(ctx, v->write_to_reg, &val_copy); + if (err) return err; + + *regval = pcilib_get_value_as_register_value(ctx, &val_copy, &err); + return err; + } + else + pcilib_script_write(ctx, v->script, val); +} - *regval = pcilib_get_value_as_register_value(ctx, &val_copy, &err); - return err; +void pcilib_transform_view_free_description (pcilib_t *ctx, pcilib_view_description_t *view) +{ + pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(view); + + if(v->script) + pcilib_free_py_script(v->script); } const pcilib_view_api_description_t pcilib_transform_view_api = - { PCILIB_VERSION, sizeof(pcilib_transform_view_description_t), NULL, NULL, NULL, pcilib_transform_view_read, pcilib_transform_view_write }; + { PCILIB_VERSION, sizeof(pcilib_transform_view_description_t), NULL, NULL, pcilib_transform_view_free_description, pcilib_transform_view_read, pcilib_transform_view_write }; diff --git a/views/transform.h b/views/transform.h index f474552..8ab4f4f 100644 --- a/views/transform.h +++ b/views/transform.h @@ -3,11 +3,13 @@ #include #include +#include typedef struct { pcilib_view_description_t base; const char *read_from_reg; /**< Formula explaining how to convert the register value to the view value */ const char *write_to_reg; /**< Formula explaining how to convert from the view value to the register value */ + pcilib_script_t *script; } pcilib_transform_view_description_t; #ifndef _PCILIB_VIEW_TRANSFORM_C -- cgit v1.2.3 From 76ee5a167add1c169a982ab59aa0544f827cb365 Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Thu, 11 Feb 2016 12:28:37 +0100 Subject: Solve errors in serialing register_info function. Clear unused code --- views/transform.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'views') diff --git a/views/transform.c b/views/transform.c index f47e4ef..986cfd7 100644 --- a/views/transform.c +++ b/views/transform.c @@ -53,7 +53,7 @@ static int pcilib_transform_view_write(pcilib_t *ctx, pcilib_view_context_t *vie return err; } else - pcilib_script_write(ctx, v->script, val); + return pcilib_script_write(ctx, v->script, (pcilib_value_t *)val); } void pcilib_transform_view_free_description (pcilib_t *ctx, pcilib_view_description_t *view) -- cgit v1.2.3 From 55eab7196d0104c71e40136b3b22e9501d234e17 Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Fri, 12 Feb 2016 14:43:20 +0100 Subject: 1. Cmakelists - move copy xml folder command to root file 2. - Move set python paths code to python module init funtction - pci.c move python module init block code after checking model to get paths before it runs. - Fix set python path code to work with PYTHONPATH - Update pci run script to work with PYTHONPATH - Fix python finalize code 3. Change pcilib_script_s interacting method. Now it stores in hash. 4. Change names of some fucntions to more unified ones 5. Remove old unused function pcilib_xml_create_script_or_transform_view 6. cli - disable reading register after set if write_verification flag is off 7. Remove uninformative error messages fro Python wrap. 8. - Server.py - add read/write property/register command handling - Add help message - Correcting paths --- views/transform.c | 53 +++++++++++++++++++++++------------------------------ views/transform.h | 2 +- 2 files changed, 24 insertions(+), 31 deletions(-) (limited to 'views') diff --git a/views/transform.c b/views/transform.c index 986cfd7..ba2f48f 100644 --- a/views/transform.c +++ b/views/transform.c @@ -15,53 +15,46 @@ static int pcilib_transform_view_read(pcilib_t *ctx, pcilib_view_context_t *view const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(model_info->views[view_ctx->view]); - if(v->script) - { - return pcilib_script_read(ctx, v->script, val); - } - else - { - int err; - - err = pcilib_set_value_from_register_value(ctx, val, regval); - if (err) return err; + int err; + err = pcilib_set_value_from_register_value(ctx, val, regval); + if (err) return err; + + if(v->module) + return pcilib_script_read(ctx, v->module, val); + else return pcilib_py_eval_string(ctx, v->read_from_reg, val); - } } static int pcilib_transform_view_write(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t *regval, const pcilib_value_t *val) { - - - + const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx); pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(model_info->views[view_ctx->view]); + int err = 0; + + pcilib_value_t val_copy = {0}; + err = pcilib_copy_value(ctx, &val_copy, val); + if (err) return err; - if(!v->script) - { - int err = 0; - - pcilib_value_t val_copy = {0}; - err = pcilib_copy_value(ctx, &val_copy, val); - if (err) return err; - - err = pcilib_py_eval_string(ctx, v->write_to_reg, &val_copy); - if (err) return err; - *regval = pcilib_get_value_as_register_value(ctx, &val_copy, &err); - return err; - } + if(v->module) + err = pcilib_script_write(ctx, v->module, &val_copy); else - return pcilib_script_write(ctx, v->script, (pcilib_value_t *)val); + err = pcilib_py_eval_string(ctx, v->write_to_reg, &val_copy); + + if (err) return err; + + *regval = pcilib_get_value_as_register_value(ctx, &val_copy, &err); + return err; } void pcilib_transform_view_free_description (pcilib_t *ctx, pcilib_view_description_t *view) { pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(view); - if(v->script) - pcilib_free_py_script(v->script); + if(v->module) + pcilib_py_free_script(v->module); } diff --git a/views/transform.h b/views/transform.h index 8ab4f4f..c2f0a98 100644 --- a/views/transform.h +++ b/views/transform.h @@ -9,7 +9,7 @@ typedef struct { pcilib_view_description_t base; const char *read_from_reg; /**< Formula explaining how to convert the register value to the view value */ const char *write_to_reg; /**< Formula explaining how to convert from the view value to the register value */ - pcilib_script_t *script; + char *module; /**< Python script module name (without extension) */ } pcilib_transform_view_description_t; #ifndef _PCILIB_VIEW_TRANSFORM_C -- cgit v1.2.3 From 1b3342649294c6ce99aeb82664a29eac47687ee5 Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Fri, 12 Feb 2016 17:50:57 +0100 Subject: Move python module init code to transfom view constructor Update python logger and python exeption messages Change serialization method in create_pcilib_instance set_pcilib functions --- views/transform.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'views') diff --git a/views/transform.c b/views/transform.c index ba2f48f..75b95b2 100644 --- a/views/transform.c +++ b/views/transform.c @@ -9,6 +9,7 @@ #include "model.h" #include "transform.h" #include "py.h" +#include "error.h" static int pcilib_transform_view_read(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t regval, pcilib_value_t *val) { @@ -57,6 +58,34 @@ void pcilib_transform_view_free_description (pcilib_t *ctx, pcilib_view_descript pcilib_py_free_script(v->module); } +pcilib_view_context_t * pcilib_transform_view_init(pcilib_t *ctx, const pcilib_view_description_t *desc) +{ + pcilib_transform_view_description_t *v_desc = (pcilib_transform_view_description_t*)desc; + + if(v_desc->module) + { + pcilib_access_mode_t mode = 0; + + int err = pcilib_py_init_script(ctx, v_desc->module, &mode); + if(err) + { + pcilib_error("Failed init script module (%s) - error %i", v_desc->module, err); + return NULL; + } + + v_desc->base.mode |= PCILIB_REGISTER_RW; + mode |= PCILIB_REGISTER_INCONSISTENT; + v_desc->base.mode &= mode; + } + + pcilib_view_context_t *view_ctx; + view_ctx = (pcilib_view_context_t*)malloc(sizeof(pcilib_view_context_t)); + if (view_ctx) memset(view_ctx, 0, sizeof(pcilib_view_context_t)); + + return view_ctx; +} + + const pcilib_view_api_description_t pcilib_transform_view_api = - { PCILIB_VERSION, sizeof(pcilib_transform_view_description_t), NULL, NULL, pcilib_transform_view_free_description, pcilib_transform_view_read, pcilib_transform_view_write }; + { PCILIB_VERSION, sizeof(pcilib_transform_view_description_t), pcilib_transform_view_init, NULL, pcilib_transform_view_free_description, pcilib_transform_view_read, pcilib_transform_view_write }; -- cgit v1.2.3 From ed9d8f285f4d81e1ce8bb5e7a5b9e471a73c1590 Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Tue, 16 Feb 2016 16:30:40 +0100 Subject: 1. Add python thread initialization to pcilib_init_py() -Fix pcilib_script_run_func() work in multithread mode 2. pcilib_close() - Move free_py() code after free views to make view destructors work properly 3. Move script hash to pcilib_py_s 4. Move pcilib_get_logger() pcilib_get_logger_min_prio() and pcilib_get_logger_argument() declarations to error.h 5. Refactor pcilib_get_value_as_pyobject pcilib_set_value_from_pyobject to more unified form 6. Add more memory checks. Fix some string memory allocations. 7. Refactor pcilib_py_s member names. 8. Merge pcilib_script_read() and pcilib_script_write() to pcilib_script_run_func() 9. Update test_pywrap views scripts --- views/transform.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'views') diff --git a/views/transform.c b/views/transform.c index 75b95b2..24434e3 100644 --- a/views/transform.c +++ b/views/transform.c @@ -22,7 +22,8 @@ static int pcilib_transform_view_read(pcilib_t *ctx, pcilib_view_context_t *view if (err) return err; if(v->module) - return pcilib_script_read(ctx, v->module, val); + return err = pcilib_script_run_func(ctx, v->module, + "read_from_register", val); else return pcilib_py_eval_string(ctx, v->read_from_reg, val); } @@ -40,7 +41,8 @@ static int pcilib_transform_view_write(pcilib_t *ctx, pcilib_view_context_t *vie if(v->module) - err = pcilib_script_write(ctx, v->module, &val_copy); + err = pcilib_script_run_func(ctx, v->module, + "write_to_register", &val_copy); else err = pcilib_py_eval_string(ctx, v->write_to_reg, &val_copy); @@ -55,7 +57,7 @@ void pcilib_transform_view_free_description (pcilib_t *ctx, pcilib_view_descript pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(view); if(v->module) - pcilib_py_free_script(v->module); + pcilib_py_free_script(ctx, v->module); } pcilib_view_context_t * pcilib_transform_view_init(pcilib_t *ctx, const pcilib_view_description_t *desc) -- cgit v1.2.3 From 3269dce32883e14b45cc490a0cc0037b4c808a68 Mon Sep 17 00:00:00 2001 From: Vasilii Chernov Date: Wed, 17 Feb 2016 11:01:22 +0100 Subject: 1. Add cmake BUILD_PYTHON_MODULES option. 2. Rename log options getting functions. 3. py: - pcilib_init_py() - extract pcilib_py_add_script_dir() - pcilib_py_init_script() - extract view-specialized code to pcilib_py_get_transform_script_properties() 3. pcilib_open(): - move pcilib_init_py() to previous position. - add extract pcilib_py_add_script_dir() 4. pcilib_script_s - change hash key. Now it is (const char*) and contains script file name with extension 5. pcipywrap: - Add pcipywrap.h to remove swig generated file complile warnings - remove -includeall swig flag to prevent crash in multi-thread scripts - change set python expetion mechanic --- views/transform.c | 17 ++++++++++++----- views/transform.h | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) (limited to 'views') diff --git a/views/transform.c b/views/transform.c index 24434e3..eb3572a 100644 --- a/views/transform.c +++ b/views/transform.c @@ -68,12 +68,21 @@ pcilib_view_context_t * pcilib_transform_view_init(pcilib_t *ctx, const pcilib_v { pcilib_access_mode_t mode = 0; - int err = pcilib_py_init_script(ctx, v_desc->module, &mode); + int err = pcilib_py_init_script(ctx, v_desc->module); if(err) { - pcilib_error("Failed init script module (%s) - error %i", v_desc->module, err); + pcilib_error("Failed init script module (%s) - error %i", + v_desc->module, err); return NULL; - } + } + err = pcilib_py_get_transform_script_properties(ctx, v_desc->module, + &mode); + if(err) + { + pcilib_error("Failed get transform script properties (%s) - error %i", + v_desc->module, err); + return NULL; + } v_desc->base.mode |= PCILIB_REGISTER_RW; mode |= PCILIB_REGISTER_INCONSISTENT; @@ -87,7 +96,5 @@ pcilib_view_context_t * pcilib_transform_view_init(pcilib_t *ctx, const pcilib_v return view_ctx; } - - const pcilib_view_api_description_t pcilib_transform_view_api = { PCILIB_VERSION, sizeof(pcilib_transform_view_description_t), pcilib_transform_view_init, NULL, pcilib_transform_view_free_description, pcilib_transform_view_read, pcilib_transform_view_write }; diff --git a/views/transform.h b/views/transform.h index c2f0a98..8c9321d 100644 --- a/views/transform.h +++ b/views/transform.h @@ -9,7 +9,7 @@ typedef struct { pcilib_view_description_t base; const char *read_from_reg; /**< Formula explaining how to convert the register value to the view value */ const char *write_to_reg; /**< Formula explaining how to convert from the view value to the register value */ - char *module; /**< Python script module name (without extension) */ + const char *module; /**< Python script module name (without extension) */ } pcilib_transform_view_description_t; #ifndef _PCILIB_VIEW_TRANSFORM_C -- cgit v1.2.3