diff options
-rw-r--r-- | CMakeLists.txt | 5 | ||||
-rw-r--r-- | pcilib/CMakeLists.txt | 8 | ||||
-rw-r--r-- | pcilib/py.c | 389 | ||||
-rw-r--r-- | pcilib/xml.c | 6 | ||||
-rw-r--r-- | pywrap/CMakeLists.txt | 18 | ||||
-rw-r--r-- | pywrap/pcipywrap.c | 158 | ||||
-rw-r--r-- | views/transform.c | 48 | ||||
-rw-r--r-- | xml/test/props.xml | 2 |
8 files changed, 317 insertions, 317 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 22a685b..ee73a1a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,11 +121,6 @@ if (NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) DESTINATION ${CMAKE_BINARY_DIR} FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE ) - - file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/xml DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/pywrap/templates DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/pywrap) - file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/pywrap/static DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/pywrap) - configure_file(${CMAKE_CURRENT_SOURCE_DIR}/pywrap/html_server.py ${CMAKE_CURRENT_BINARY_DIR}/pywrap/html_server.py) endif(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) install(FILES diff --git a/pcilib/CMakeLists.txt b/pcilib/CMakeLists.txt index 4ea50fd..27543e4 100644 --- a/pcilib/CMakeLists.txt +++ b/pcilib/CMakeLists.txt @@ -8,7 +8,7 @@ include_directories( ${UTHASH_INCLUDE_DIRS} ) -set(HEADERS pcilib.h pci.h datacpy.h memcpy.h pagecpy.h cpu.h timing.h export.h value.h bar.h fifo.h model.h bank.h register.h view.h property.h unit.h xml.h py.h kmem.h irq.h locking.h lock.h dma.h event.h plugin.h tools.h error.h debug.h env.h version.h config.h build.h) +set(HEADERS pcilib.h pci.h datacpy.h memcpy.h pagecpy.h cpu.h timing.h export.h value.h bar.h fifo.h model.h bank.h register.h view.h property.h unit.h xml.h py.h kmem.h irq.h locking.h lock.h dma.h event.h plugin.h tools.h error.h debug.h env.h config.h version.h build.h) add_library(pcilib SHARED pci.c datacpy.c memcpy.c pagecpy.c cpu.c timing.c export.c value.c bar.c fifo.c model.c bank.c register.c view.c unit.c property.c xml.c py.c kmem.c irq.c locking.c lock.c dma.c event.c plugin.c tools.c error.c debug.c env.c) target_link_libraries(pcilib dma protocols views ${CMAKE_THREAD_LIBS_INIT} ${UFODECODE_LIBRARIES} ${CMAKE_DL_LIBS} ${EXTRA_SYSTEM_LIBS} ${LIBXML2_LIBRARIES} ${PYTHON_LIBRARIES}) add_dependencies(pcilib build dma protocols views) @@ -21,10 +21,6 @@ install(FILES pcilib.h DESTINATION include ) -install(FILES bar.h kmem.h locking.h lock.h bank.h register.h xml.h dma.h event.h model.h error.h debug.h env.h tools.h timing.h cpu.h datacpy.h pagecpy.h memcpy.h export.h view.h unit.h +install(FILES bar.h kmem.h locking.h lock.h bank.h register.h xml.h dma.h event.h model.h error.h debug.h env.h tools.h timing.h cpu.h datacpy.h pagecpy.h memcpy.h export.h version.h view.h unit.h DESTINATION include/pcilib ) - -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/version.h - DESTINATION include/pcilib -)
\ No newline at end of file diff --git a/pcilib/py.c b/pcilib/py.c index 271a3fd..3e1f38d 100644 --- a/pcilib/py.c +++ b/pcilib/py.c @@ -5,6 +5,7 @@ #endif /* HAVE_PYTHON */ #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <strings.h> #include <alloca.h> @@ -47,9 +48,9 @@ void pcilib_log_python_error(const char *file, int line, pcilib_log_flags_t flag gstate = PyGILState_Ensure(); if (PyErr_Occurred()) { - PyErr_Fetch(&pytype, &pyval, &pytraceback); - type = PyString_AsString(pytype); - val = PyString_AsString(pyval); + PyErr_Fetch(&pytype, &pyval, &pytraceback); + type = PyString_AsString(pytype); + val = PyString_AsString(pyval); } PyGILState_Release(gstate); #endif /* HAVE_PYTHON */ @@ -82,9 +83,9 @@ void pcilib_log_python_error(const char *file, int line, pcilib_log_flags_t flag va_end(va); #ifdef HAVE_PYTHON - if (pytype) Py_XDECREF(pytype); - if (pyval) Py_XDECREF(pyval); - if (pytraceback) Py_XDECREF(pytraceback); + if (pytype) Py_DECREF(pytype); + if (pyval) Py_DECREF(pyval); + if (pytraceback) Py_DECREF(pytraceback); #endif /* HAVE_PYTHON */ } @@ -97,101 +98,109 @@ int pcilib_init_py(pcilib_t *ctx) { memset(ctx->py, 0, sizeof(pcilib_py_t)); - if(Py_IsInitialized()) - ctx->py->finalyze = 0; - else { + if(!Py_IsInitialized()) { Py_Initialize(); - // Since python is being initializing from c programm, it needs to initialize threads to work properly with c threads + // Since python is being initializing from c programm, it needs to initialize threads to work properly with c threads PyEval_InitThreads(); PyEval_ReleaseLock(); + ctx->py->finalyze = 1; } ctx->py->main_module = PyImport_AddModule("__parser__"); if (!ctx->py->main_module) { - pcilib_python_error("Error importing python parser"); - return PCILIB_ERROR_FAILED; + pcilib_python_error("Error importing python parser"); + return PCILIB_ERROR_FAILED; } ctx->py->global_dict = PyModule_GetDict(ctx->py->main_module); if (!ctx->py->global_dict) { - pcilib_python_error("Error locating global python dictionary"); - return PCILIB_ERROR_FAILED; + pcilib_python_error("Error locating global python dictionary"); + return PCILIB_ERROR_FAILED; } PyObject *pywrap = PyImport_ImportModule("pcipywrap"); if (!pywrap) { - pcilib_python_error("Error importing pcilib python wrapper"); - return PCILIB_ERROR_FAILED; + pcilib_python_error("Error importing pcilib python wrapper"); + return PCILIB_ERROR_FAILED; } PyObject *mod_name = PyString_FromString("Pcipywrap"); - PyObject *ctx_py_ptr = PyCObject_FromVoidPtr(ctx, NULL); - ctx->py->pcilib_pywrap = PyObject_CallMethodObjArgs(pywrap, mod_name, ctx_py_ptr, NULL); - Py_DECREF(ctx_py_ptr); - Py_DECREF(mod_name); + PyObject *pyctx = PyCObject_FromVoidPtr(ctx, NULL); + ctx->py->pcilib_pywrap = PyObject_CallMethodObjArgs(pywrap, mod_name, pyctx, NULL); + Py_XDECREF(pyctx); + Py_XDECREF(mod_name); if (!ctx->py->pcilib_pywrap) { - pcilib_python_error("Error initializing python wrapper"); - return PCILIB_ERROR_FAILED; + pcilib_python_error("Error initializing python wrapper"); + return PCILIB_ERROR_FAILED; } #endif /* HAVE_PYTHON */ + return 0; } int pcilib_py_add_script_dir(pcilib_t *ctx, const char *dir) { #ifdef HAVE_PYTHON - PyObject* pypath; - char *script_dir; - - const char *model_dir = getenv("PCILIB_MODEL_DIR"); - if (!model_dir) model_dir = PCILIB_MODEL_DIR; - - if (!dir) dir = ctx->model; - - if (*dir == '/') { - script_dir = (char*)dir; - } else { - script_dir = alloca(strlen(model_dir) + strlen(dir) + 2); - if (!script_dir) return PCILIB_ERROR_MEMORY; - sprintf(script_dir, "%s/%s", model_dir, dir); - } - - pypath = PySys_GetObject("path"); - if (!pypath) { - pcilib_python_error("Can't get python path"); - return PCILIB_ERROR_FAILED; - } - - PyObject* new_path = PyString_FromString(script_dir); - - //Check if sys.path already contains our path - PyObject* dict = PyDict_New(); - PyObject* cur_py = PyString_FromString("cur"); - PyObject* path_py = PyString_FromString("path"); - PyDict_SetItem(dict, cur_py, new_path); - PyDict_SetItem(dict, path_py, pypath); - Py_DECREF(cur_py); - Py_DECREF(path_py); - - PyObject* pyret = PyRun_String("cur in path", Py_eval_input, - ctx->py->global_dict, - dict); - Py_DECREF(dict); - - if(pyret == Py_False) { - if(PyList_Append(pypath, new_path) == -1) { - pcilib_python_error("Can't add directory (%s) to python path", script_dir); - return PCILIB_ERROR_FAILED; - } - } - else if(pyret == Py_True) - { - printf("1\n"); - Py_DECREF(new_path); - return 0; - } - + int err = 0; + PyObject *pypath, *pynewdir; + PyObject *pydict, *pystr, *pyret = NULL; + char *script_dir; + + const char *model_dir = getenv("PCILIB_MODEL_DIR"); + if (!model_dir) model_dir = PCILIB_MODEL_DIR; + + if (!dir) dir = ctx->model; + + if (*dir == '/') { + script_dir = (char*)dir; + } else { + script_dir = alloca(strlen(model_dir) + strlen(dir) + 2); + if (!script_dir) return PCILIB_ERROR_MEMORY; + sprintf(script_dir, "%s/%s", model_dir, dir); + } + + pypath = PySys_GetObject("path"); + if (!pypath) { + pcilib_python_error("Can't get python path"); + return PCILIB_ERROR_FAILED; + } + + pynewdir = PyString_FromString(script_dir); + if (!pynewdir) { + pcilib_python_error("Can't create python string"); + return PCILIB_ERROR_MEMORY; + } + + // Checking if the directory already in the path? + pydict = PyDict_New(); + if (pydict) { + pystr = PyString_FromString("cur"); + if (pystr) { + PyDict_SetItem(pydict, pystr, pynewdir); + Py_DECREF(pystr); + } + + pystr = PyString_FromString("path"); + if (pystr) { + PyDict_SetItem(pydict, pystr, pypath); + Py_DECREF(pystr); + } + + pyret = PyRun_String("cur in path", Py_eval_input, ctx->py->global_dict, pydict); + Py_DECREF(pydict); + } + + if ((pyret == Py_False)&&(PyList_Append(pypath, pynewdir) == -1)) + err = PCILIB_ERROR_FAILED; + + if (pyret) Py_DECREF(pyret); + Py_DECREF(pynewdir); + + if (err) { + pcilib_python_error("Can't add directory (%s) to python path", script_dir); + return err; + } #endif /* HAVE_PYTHON */ return 0; @@ -199,65 +208,65 @@ int pcilib_py_add_script_dir(pcilib_t *ctx, const char *dir) { void pcilib_free_py(pcilib_t *ctx) { #ifdef HAVE_PYTHON - int finalyze = 0; + int finalyze = 0; + + if (ctx->py) { + if (ctx->py->finalyze) finalyze = 1; + + if (ctx->py->script_hash) { + pcilib_script_t *script, *script_tmp; + + HASH_ITER(hh, ctx->py->script_hash, script, script_tmp) { + Py_DECREF(script->module); + HASH_DEL(ctx->py->script_hash, script); + free(script); + } + ctx->py->script_hash = NULL; + } + + if (ctx->py->pcilib_pywrap) + Py_DECREF(ctx->py->pcilib_pywrap); - if (ctx->py) { - - if(ctx->py->finalyze) finalyze = 1; - if (ctx->py->script_hash) { - pcilib_script_t *script, *script_tmp; - - HASH_ITER(hh, ctx->py->script_hash, script, script_tmp) { - Py_DECREF(script->module); - - HASH_DEL(ctx->py->script_hash, script); - free(script); - } - ctx->py->script_hash = NULL; - } - - Py_DECREF(ctx->py->pcilib_pywrap); - - free(ctx->py); - ctx->py = NULL; - } + free(ctx->py); + ctx->py = NULL; + } if (finalyze) - Py_Finalize(); + Py_Finalize(); #endif /* HAVE_PYTHON */ } int pcilib_py_load_script(pcilib_t *ctx, const char *script_name) { #ifdef HAVE_PYTHON - PyObject* pymodule; - pcilib_script_t *module = NULL; + PyObject* pymodule; + pcilib_script_t *module = NULL; - char *module_name = strdupa(script_name); - if (!module_name) return PCILIB_ERROR_MEMORY; + char *module_name = strdupa(script_name); + if (!module_name) return PCILIB_ERROR_MEMORY; - char *py = strrchr(module_name, '.'); - if ((!py)||(strcasecmp(py, ".py"))) { - pcilib_error("Invalid script name (%s) is specified", script_name); - return PCILIB_ERROR_INVALID_ARGUMENT; - } - *py = 0; + char *py = strrchr(module_name, '.'); + if ((!py)||(strcasecmp(py, ".py"))) { + pcilib_error("Invalid script name (%s) is specified", script_name); + return PCILIB_ERROR_INVALID_ARGUMENT; + } + *py = 0; - HASH_FIND_STR(ctx->py->script_hash, script_name, module); - if (module) return 0; + HASH_FIND_STR(ctx->py->script_hash, script_name, module); + if (module) return 0; - pymodule = PyImport_ImportModule(module_name); - if (!pymodule) { - pcilib_python_error("Error importing script (%s)", script_name); - return PCILIB_ERROR_FAILED; - } + pymodule = PyImport_ImportModule(module_name); + if (!pymodule) { + pcilib_python_error("Error importing script (%s)", script_name); + return PCILIB_ERROR_FAILED; + } - module = (pcilib_script_t*)malloc(sizeof(pcilib_script_t)); - if (!module) return PCILIB_ERROR_MEMORY; + module = (pcilib_script_t*)malloc(sizeof(pcilib_script_t)); + if (!module) return PCILIB_ERROR_MEMORY; - module->module = pymodule; - module->name = script_name; - HASH_ADD_KEYPTR(hh, ctx->py->script_hash, module->name, strlen(module->name), module); + module->module = pymodule; + module->name = script_name; + HASH_ADD_KEYPTR(hh, ctx->py->script_hash, module->name, strlen(module->name), module); #endif /* HAVE_PYTHON */ return 0; } @@ -266,34 +275,34 @@ int pcilib_py_get_transform_script_properties(pcilib_t *ctx, const char *script_ pcilib_access_mode_t mode = 0; #ifdef HAVE_PYTHON - PyObject *dict; - PyObject *pystr; - pcilib_script_t *module; - - HASH_FIND_STR(ctx->py->script_hash, script_name, module); - - if(!module) { - pcilib_error("Script (%s) is not loaded yet", script_name); - return PCILIB_ERROR_NOTFOUND; - } - - dict = PyModule_GetDict(module->module); - if (!dict) { - pcilib_python_error("Error getting dictionary for script (%s)", script_name); - return PCILIB_ERROR_FAILED; - } - - pystr = PyString_FromString("read_from_register"); - if (pystr) { - if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_R; - Py_XDECREF(pystr); - } - - pystr = PyString_FromString("write_to_register"); - if (pystr) { - if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_W; - Py_XDECREF(pystr); - } + PyObject *dict; + PyObject *pystr; + pcilib_script_t *module; + + HASH_FIND_STR(ctx->py->script_hash, script_name, module); + + if(!module) { + pcilib_error("Script (%s) is not loaded yet", script_name); + return PCILIB_ERROR_NOTFOUND; + } + + dict = PyModule_GetDict(module->module); + if (!dict) { + pcilib_python_error("Error getting dictionary for script (%s)", script_name); + return PCILIB_ERROR_FAILED; + } + + pystr = PyString_FromString("read_from_register"); + if (pystr) { + if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_R; + Py_DECREF(pystr); + } + + pystr = PyString_FromString("write_to_register"); + if (pystr) { + if (PyDict_Contains(dict, pystr)) mode |= PCILIB_ACCESS_W; + Py_DECREF(pystr); + } #endif /* HAVE_PYTHON */ if (mode_ret) *mode_ret = mode; @@ -309,31 +318,31 @@ pcilib_py_object *pcilib_get_value_as_pyobject(pcilib_t* ctx, pcilib_value_t *va long ival; double fval; - gstate = PyGILState_Ensure(); - switch(val->type) { - case PCILIB_TYPE_LONG: - ival = pcilib_get_value_as_int(ctx, val, &err); - if (!err) res = (PyObject*)PyInt_FromLong(ival); - break; - case PCILIB_TYPE_DOUBLE: - fval = pcilib_get_value_as_float(ctx, val, &err); - if (!err) res = (PyObject*)PyFloat_FromDouble(fval); - break; - default: - PyGILState_Release(gstate); - pcilib_error("Can't convert pcilib value of type (%lu) to PyObject", val->type); - if (ret) *ret = PCILIB_ERROR_NOTSUPPORTED; - return NULL; - } - PyGILState_Release(gstate); - - if (err) { - if (ret) *ret = err; - return NULL; - } else if (!res) { - if (ret) *ret = PCILIB_ERROR_MEMORY; - return res; - } + gstate = PyGILState_Ensure(); + switch(val->type) { + case PCILIB_TYPE_LONG: + ival = pcilib_get_value_as_int(ctx, val, &err); + if (!err) res = (PyObject*)PyInt_FromLong(ival); + break; + case PCILIB_TYPE_DOUBLE: + fval = pcilib_get_value_as_float(ctx, val, &err); + if (!err) res = (PyObject*)PyFloat_FromDouble(fval); + break; + default: + PyGILState_Release(gstate); + pcilib_error("Can't convert pcilib value of type (%lu) to PyObject", val->type); + if (ret) *ret = PCILIB_ERROR_NOTSUPPORTED; + return NULL; + } + PyGILState_Release(gstate); + + if (err) { + if (ret) *ret = err; + return NULL; + } else if (!res) { + if (ret) *ret = PCILIB_ERROR_MEMORY; + return res; + } if (ret) *ret = 0; return res; @@ -481,36 +490,36 @@ static char *pcilib_py_parse_string(pcilib_t *ctx, const char *codestr, pcilib_v int pcilib_py_eval_string(pcilib_t *ctx, const char *codestr, pcilib_value_t *value) { #ifdef HAVE_PYTHON int err; - + PyGILState_STATE gstate; char *code; PyObject* obj; - code = pcilib_py_parse_string(ctx, codestr, value); if (!code) { pcilib_error("Failed to parse registers in the code: %s", codestr); return PCILIB_ERROR_FAILED; } - + gstate = PyGILState_Ensure(); obj = PyRun_String(code, Py_eval_input, ctx->py->global_dict, ctx->py->global_dict); PyGILState_Release(gstate); - - free(code); if (!obj) { pcilib_error("Failed to run the Python code: %s", code); + free(code); return PCILIB_ERROR_FAILED; } pcilib_debug(VIEWS, "Evaluating a Python string \'%s\' to %lf=\'%s\'", codestr, PyFloat_AsDouble(obj), code); err = pcilib_set_value_from_float(ctx, value, PyFloat_AsDouble(obj)); + Py_DECREF(obj); + free(code); return err; #else /* HAVE_PYTHON */ - pcilib_error("Current build not support python."); + pcilib_error("Current build not support python."); return PCILIB_ERROR_NOTAVAILABLE; #endif /* HAVE_PYTHON */ } @@ -525,39 +534,39 @@ int pcilib_py_eval_func(pcilib_t *ctx, const char *script_name, const char *func HASH_FIND_STR(ctx->py->script_hash, script_name, module); if (!module) { - pcilib_error("Script (%s) is not loaded", script_name); - return PCILIB_ERROR_NOTFOUND; + pcilib_error("Script (%s) is not loaded", script_name); + return PCILIB_ERROR_NOTFOUND; } if (val) { - pyval = pcilib_get_value_as_pyobject(ctx, val, &err); - if (err) return err; + pyval = pcilib_get_value_as_pyobject(ctx, val, &err); + if (err) return err; } PyGILState_STATE gstate = PyGILState_Ensure(); pyfunc = PyUnicode_FromString(func_name); if (!pyfunc) { - if (pyval) Py_XDECREF(pyval); - PyGILState_Release(gstate); - return PCILIB_ERROR_MEMORY; + if (pyval) Py_DECREF(pyval); + PyGILState_Release(gstate); + return PCILIB_ERROR_MEMORY; } pyret = PyObject_CallMethodObjArgs(module->module, pyfunc, ctx->py->pcilib_pywrap, pyval, NULL); - Py_XDECREF(pyfunc); - Py_XDECREF(pyval); + Py_DECREF(pyfunc); + Py_DECREF(pyval); if (!pyret) { - PyGILState_Release(gstate); - pcilib_python_error("Error executing function (%s) of python script (%s)", func_name, script_name); - return PCILIB_ERROR_FAILED; + PyGILState_Release(gstate); + pcilib_python_error("Error executing function (%s) of python script (%s)", func_name, script_name); + return PCILIB_ERROR_FAILED; } if ((val)&&(pyret != Py_None)) - err = pcilib_set_value_from_pyobject(ctx, val, pyret); + err = pcilib_set_value_from_pyobject(ctx, val, pyret); - Py_XDECREF(pyret); + Py_DECREF(pyret); PyGILState_Release(gstate); return err; diff --git a/pcilib/xml.c b/pcilib/xml.c index 0463a40..b980a83 100644 --- a/pcilib/xml.c +++ b/pcilib/xml.c @@ -616,8 +616,6 @@ static int pcilib_xml_parse_value_name(pcilib_t *ctx, xmlXPathContextPtr xpath, int min_set = 0, max_set = 0; pcilib_register_value_t val; - memset(desc, 0, sizeof(pcilib_register_value_name_t)); - for (cur = node->properties; cur != NULL; cur = cur->next) { if(!cur->children) continue; if(!xmlNodeIsText(cur->children)) continue; @@ -715,7 +713,7 @@ static int pcilib_xml_create_enum_view(pcilib_t *ctx, xmlXPathContextPtr xpath, pcilib_error("No names is defined for enum view (%s)", desc.base.name); return PCILIB_ERROR_INVALID_DATA; } - + memset(desc.names, 0, (nodeset->nodeNr + 1) * sizeof(pcilib_register_value_name_t)); for (i = 0; i < nodeset->nodeNr; i++) { err = pcilib_xml_parse_value_name(ctx, xpath, doc, nodeset->nodeTab[i], &desc.names[i]); @@ -725,11 +723,9 @@ static int pcilib_xml_create_enum_view(pcilib_t *ctx, xmlXPathContextPtr xpath, return err; } } - memset(&desc.names[nodeset->nodeNr], 0, sizeof(pcilib_register_value_name_t)); xmlXPathFreeObject(nodes); - err = pcilib_add_views_custom(ctx, 1, (pcilib_view_description_t*)&desc, &view_ctx); if (err) { free(desc.names); diff --git a/pywrap/CMakeLists.txt b/pywrap/CMakeLists.txt index f1c909e..988546b 100644 --- a/pywrap/CMakeLists.txt +++ b/pywrap/CMakeLists.txt @@ -2,26 +2,30 @@ include_directories( ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/pcilib + ${CMAKE_SOURCE_DIR}/pywrap ${CMAKE_BINARY_DIR}/pcilib ${LIBXML2_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIR} ${UTHASH_INCLUDE_DIRS} ) -set(HEADERS pcipywrap.h) - -#Creating python wrapping include(${SWIG_USE_FILE}) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + +set(HEADERS pcipywrap.h) set(CMAKE_SWIG_FLAGS "") swig_add_module(pcipywrap python pcipywrap.i pcipywrap.c) swig_link_libraries(pcipywrap ${PYTHON_LIBRARIES} pcilib) -#install pcilib python wrapper into Python site packages folder execute_process ( COMMAND python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE) install(TARGETS ${SWIG_MODULE_pcipywrap_REAL_NAME} DESTINATION ${PYTHON_SITE_PACKAGES}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/pcipywrap.py DESTINATION ${PYTHON_SITE_PACKAGES}) -configure_file(server.py server.py) -configure_file(test_pcipywrap.py test_pcipywrap.py) +if (NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) + file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/server.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) + file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test_pcipywrap.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) + + file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/templates DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) + file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/static DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) + file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/html_server.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) +endif(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) diff --git a/pywrap/pcipywrap.c b/pywrap/pcipywrap.c index 64e059a..ceb0835 100644 --- a/pywrap/pcipywrap.c +++ b/pywrap/pcipywrap.c @@ -281,7 +281,7 @@ PyObject * pcilib_convert_register_info_to_pyobject(pcilib_t* ctx, pcilib_regist { PyObject* values = PyList_New(0); - + for (int j = 0; listItem.values[j].name; j++) { PyObject* valuesItem = PyDict_New(); @@ -306,9 +306,9 @@ PyObject * pcilib_convert_register_info_to_pyobject(pcilib_t* ctx, pcilib_regist if(listItem.values[j].description) { pcilib_pydict_set_item(valuesItem, - PyString_FromString("description"), - PyString_FromString(listItem.values[j].description)); - + PyString_FromString("description"), + PyString_FromString(listItem.values[j].description)); + } pcilib_pylist_append(values, valuesItem); } @@ -319,7 +319,7 @@ PyObject * pcilib_convert_register_info_to_pyobject(pcilib_t* ctx, pcilib_regist } return pylistItem; - + } Pcipywrap *new_Pcipywrap(const char* fpga_device, const char* model) @@ -507,101 +507,101 @@ PyObject* Pcipywrap_get_property_list(Pcipywrap *self, const char* branch) PyObject* Pcipywrap_read_dma(Pcipywrap *self, unsigned char dma, size_t size) { - int err; - void* buf = NULL; - size_t real_size; - - err = pcilib_read_dma(self->ctx, dma, (uintptr_t)NULL, size, buf, &real_size); - if(err) - { - set_python_exception("Failed pcilib_read_dma", err); - return NULL; - } - - - PyObject* py_buf = PyByteArray_FromStringAndSize((const char*)buf, real_size); - if(buf) - free(buf); - - return py_buf; + int err; + void* buf = NULL; + size_t real_size; + + err = pcilib_read_dma(self->ctx, dma, (uintptr_t)NULL, size, buf, &real_size); + if(err) + { + set_python_exception("Failed pcilib_read_dma", err); + return NULL; + } + + + PyObject* py_buf = PyByteArray_FromStringAndSize((const char*)buf, real_size); + if(buf) + free(buf); + + return py_buf; } PyObject* Pcipywrap_lock_global(Pcipywrap *self) { - int err; - - err = pcilib_lock_global(self->ctx); - if(err) - { - set_python_exception("Failed pcilib_lock_global"); - return NULL; - } - - return PyInt_FromLong((long)1); + int err; + + err = pcilib_lock_global(self->ctx); + if(err) + { + set_python_exception("Failed pcilib_lock_global"); + return NULL; + } + + return PyInt_FromLong((long)1); } void Pcipywrap_unlock_global(Pcipywrap *self) { - pcilib_unlock_global(self->ctx); - return; + pcilib_unlock_global(self->ctx); + return; } -PyObject* Pcipywrap_lock(Pcipywrap *self, const char *lock_id) -{ - pcilib_lock_t* lock = pcilib_get_lock(self->ctx, - PCILIB_LOCK_FLAGS_DEFAULT, - lock_id); +PyObject* Pcipywrap_lock(Pcipywrap *self, const char *lock_id) +{ + pcilib_lock_t* lock = pcilib_get_lock(self->ctx, + PCILIB_LOCK_FLAGS_DEFAULT, + lock_id); if(!lock) - { - set_python_exception("Failed pcilib_get_lock"); - return NULL; - } - - - int err = pcilib_lock(lock); - if(err) - { - set_python_exception("Failed pcilib_lock"); - return NULL; - } - - return PyInt_FromLong((long)1); + { + set_python_exception("Failed pcilib_get_lock"); + return NULL; + } + + + int err = pcilib_lock(lock); + if(err) + { + set_python_exception("Failed pcilib_lock"); + return NULL; + } + + return PyInt_FromLong((long)1); } PyObject* Pcipywrap_try_lock(Pcipywrap *self, const char *lock_id) { - pcilib_lock_t* lock = pcilib_get_lock(self->ctx, - PCILIB_LOCK_FLAGS_DEFAULT, - lock_id); + pcilib_lock_t* lock = pcilib_get_lock(self->ctx, + PCILIB_LOCK_FLAGS_DEFAULT, + lock_id); if(!lock) - { - set_python_exception("Failed pcilib_get_lock"); - return NULL; - } - - int err = pcilib_try_lock(lock); - if(err) - { - set_python_exception("Failed pcilib_try_lock"); - return NULL; - } - - return PyInt_FromLong((long)1); + { + set_python_exception("Failed pcilib_get_lock"); + return NULL; + } + + int err = pcilib_try_lock(lock); + if(err) + { + set_python_exception("Failed pcilib_try_lock"); + return NULL; + } + + return PyInt_FromLong((long)1); } PyObject* Pcipywrap_unlock(Pcipywrap *self, const char *lock_id) { - pcilib_lock_t* lock = pcilib_get_lock(self->ctx, - PCILIB_LOCK_FLAGS_DEFAULT, - lock_id); + pcilib_lock_t* lock = pcilib_get_lock(self->ctx, + PCILIB_LOCK_FLAGS_DEFAULT, + lock_id); if(!lock) - { - set_python_exception("Failed pcilib_get_lock"); - return NULL; - } - - pcilib_unlock(lock); - return PyInt_FromLong((long)1); + { + set_python_exception("Failed pcilib_get_lock"); + return NULL; + } + + pcilib_unlock(lock); + return PyInt_FromLong((long)1); } diff --git a/views/transform.c b/views/transform.c index 7906743..25f30d1 100644 --- a/views/transform.c +++ b/views/transform.c @@ -19,26 +19,26 @@ static pcilib_view_context_t * pcilib_transform_view_init(pcilib_t *ctx, pcilib_ pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(ctx->views[view]); if(v->script) { - pcilib_access_mode_t mode = 0; - - err = pcilib_py_load_script(ctx, v->script); - if(err) { - pcilib_error("Error (%i), loading script %s", err, v->script); - return NULL; - } - - err = pcilib_py_get_transform_script_properties(ctx, v->script, &mode); - if(err) { - pcilib_error("Error (%i) obtaining properties of transform script %s", err, v->script); - return NULL; - } - - if ((v->base.mode&PCILIB_REGISTER_RW) == 0) - v->base.mode |= PCILIB_REGISTER_RW; - v->base.mode &= (~PCILIB_REGISTER_RW)|mode; - - if (!v->read_from_reg) v->read_from_reg = "read_from_register"; - if (!v->write_to_reg) v->write_to_reg = "write_to_register"; + pcilib_access_mode_t mode = 0; + + err = pcilib_py_load_script(ctx, v->script); + if(err) { + pcilib_error("Error (%i), loading script %s", err, v->script); + return NULL; + } + + err = pcilib_py_get_transform_script_properties(ctx, v->script, &mode); + if(err) { + pcilib_error("Error (%i) obtaining properties of transform script %s", err, v->script); + return NULL; + } + + if ((v->base.mode&PCILIB_REGISTER_RW) == 0) + v->base.mode |= PCILIB_REGISTER_RW; + v->base.mode &= (~PCILIB_REGISTER_RW)|mode; + + if (!v->read_from_reg) v->read_from_reg = "read_from_register"; + if (!v->write_to_reg) v->write_to_reg = "write_to_register"; } view_ctx = (pcilib_view_context_t*)malloc(sizeof(pcilib_view_context_t)); @@ -57,9 +57,9 @@ static int pcilib_transform_view_read(pcilib_t *ctx, pcilib_view_context_t *view if (err) return err; if (v->script) - err = pcilib_py_eval_func(ctx, v->script, v->read_from_reg, val); + err = pcilib_py_eval_func(ctx, v->script, v->read_from_reg, val); else - err = pcilib_py_eval_string(ctx, v->read_from_reg, val); + err = pcilib_py_eval_string(ctx, v->read_from_reg, val); return err; } @@ -75,9 +75,9 @@ static int pcilib_transform_view_write(pcilib_t *ctx, pcilib_view_context_t *vie if (err) return err; if (v->script) - err = pcilib_py_eval_func(ctx, v->script, v->write_to_reg, &val_copy); + err = pcilib_py_eval_func(ctx, v->script, v->write_to_reg, &val_copy); else - err = pcilib_py_eval_string(ctx, v->write_to_reg, &val_copy); + err = pcilib_py_eval_string(ctx, v->write_to_reg, &val_copy); if (err) return err; diff --git a/xml/test/props.xml b/xml/test/props.xml index 650c781..57702e2 100644 --- a/xml/test/props.xml +++ b/xml/test/props.xml @@ -1,6 +1,6 @@ <?xml version="1.0"?> <model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> - <transform path="/test/prop1" register="test_prop1" unit="C" read_from_register="(503975./1024000) - 27315./100" description="formula to get real fpga temperature from the fpga_temperature register in decimal"/> + <transform path="/test/prop1" register="test_prop1" unit="C" read_from_register="(503975./1024000)*${/registers/fpga/sensor_temperature:C} - 27315./100" description="formula to get real fpga temperature from the fpga_temperature register in decimal"/> <transform path="/test/prop2" register="test_prop2" unit="C" script="test_prop2.py" description="test python script #1" write_verification="0" /> <transform path="/test/prop3" register="test_prop3" unit="C" script="test_prop3.py" description="test python script #2" /> </model> |