diff options
author | Vasilii Chernov <vchernov@inr.ru> | 2016-02-12 17:50:57 +0100 |
---|---|---|
committer | Vasilii Chernov <vchernov@inr.ru> | 2016-02-12 17:50:57 +0100 |
commit | 1b3342649294c6ce99aeb82664a29eac47687ee5 (patch) | |
tree | fb34ee89e1edc4dd3e27c2a1416a435192c7ae6b | |
parent | 55eab7196d0104c71e40136b3b22e9501d234e17 (diff) | |
download | pcitool-1b3342649294c6ce99aeb82664a29eac47687ee5.tar.gz pcitool-1b3342649294c6ce99aeb82664a29eac47687ee5.tar.bz2 pcitool-1b3342649294c6ce99aeb82664a29eac47687ee5.tar.xz pcitool-1b3342649294c6ce99aeb82664a29eac47687ee5.zip |
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
-rw-r--r-- | pcilib/pci.c | 3 | ||||
-rw-r--r-- | pcilib/py.c | 12 | ||||
-rw-r--r-- | pcilib/view.c | 2 | ||||
-rw-r--r-- | pcilib/view.h | 2 | ||||
-rw-r--r-- | pcilib/xml.c | 5 | ||||
-rw-r--r-- | pcitool/cli.c | 2 | ||||
-rw-r--r-- | pywrap/pcipywrap.c | 110 | ||||
-rw-r--r-- | pywrap/pcipywrap.i | 4 | ||||
-rw-r--r-- | pywrap/server.py | 3 | ||||
-rw-r--r-- | views/transform.c | 31 |
10 files changed, 124 insertions, 50 deletions
diff --git a/pcilib/pci.c b/pcilib/pci.c index cc2a67a..c38097f 100644 --- a/pcilib/pci.c +++ b/pcilib/pci.c @@ -192,12 +192,14 @@ pcilib_t *pcilib_open(const char *device, const char *model) { return NULL; } + xmlerr = pcilib_init_xml(ctx, ctx->model); if ((xmlerr)&&(xmlerr != PCILIB_ERROR_NOTFOUND)) { pcilib_error("Error (%i) initializing XML subsystem for model %s", xmlerr, ctx->model); pcilib_close(ctx); return NULL; } + // We have found neither standard model nor XML if ((err)&&(xmlerr)) { @@ -219,7 +221,6 @@ pcilib_t *pcilib_open(const char *device, const char *model) { pcilib_close(ctx); return NULL; } - err = pcilib_init_event_engine(ctx); if (err) { pcilib_error("Error (%i) initializing event engine\n", err); diff --git a/pcilib/py.c b/pcilib/py.c index 7b1ae79..e448d21 100644 --- a/pcilib/py.c +++ b/pcilib/py.c @@ -244,11 +244,11 @@ void* pcilib_get_value_as_pyobject(pcilib_t* ctx, pcilib_value_t *val) switch(val->type) { case PCILIB_TYPE_INVALID: - pcilib_error("Invalid register output type (PCILIB_TYPE_INVALID)"); + pcilib_warning("Invalid register output type (PCILIB_TYPE_INVALID)"); return NULL; case PCILIB_TYPE_STRING: - pcilib_error("Invalid register output type (PCILIB_TYPE_STRING)"); + pcilib_warning("Invalid register output type (PCILIB_TYPE_STRING)"); return NULL; case PCILIB_TYPE_LONG: @@ -258,7 +258,7 @@ void* pcilib_get_value_as_pyobject(pcilib_t* ctx, pcilib_value_t *val) if(err) { - pcilib_error("Failed: pcilib_get_value_as_int (%i)", err); + pcilib_error("Failed: pcilib_get_value_as_int (%i)", err); return NULL; } return (PyObject*)PyInt_FromLong((long) ret); @@ -271,14 +271,14 @@ void* pcilib_get_value_as_pyobject(pcilib_t* ctx, pcilib_value_t *val) if(err) { - pcilib_error("Failed: pcilib_get_value_as_int (%i)", err); + pcilib_error("Failed: pcilib_get_value_as_int (%i)", err); return NULL; } return (PyObject*)PyFloat_FromDouble((double) ret); } default: - pcilib_error("Invalid register output type (unknown)"); + pcilib_warning("Invalid register output type (unknown)"); return NULL; } } @@ -360,7 +360,7 @@ int pcilib_py_init_script(pcilib_t *ctx, char* module_name, pcilib_access_mode_t //setting pcilib_t instance PyObject_CallMethodObjArgs(pcipywrap_module, PyUnicode_FromString("set_pcilib"), - PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*)), + PyCObject_FromVoidPtr(ctx, NULL), NULL); } diff --git a/pcilib/view.c b/pcilib/view.c index e31fdba..797f4ae 100644 --- a/pcilib/view.c +++ b/pcilib/view.c @@ -70,7 +70,7 @@ int pcilib_add_views_custom(pcilib_t *ctx, size_t n, const pcilib_view_descripti } if (v->api->init) - view_ctx = v->api->init(ctx); + view_ctx = v->api->init(ctx, v); else { view_ctx = (pcilib_view_context_t*)malloc(sizeof(pcilib_view_context_t)); if (view_ctx) memset(view_ctx, 0, sizeof(pcilib_view_context_t)); diff --git a/pcilib/view.h b/pcilib/view.h index 33d4d96..1a1d277 100644 --- a/pcilib/view.h +++ b/pcilib/view.h @@ -19,7 +19,7 @@ typedef enum { typedef struct { pcilib_version_t version; /**< Version */ size_t description_size; /**< The actual size of the description */ - pcilib_view_context_t *(*init)(pcilib_t *ctx); /**< Optional function which should allocated context used by read/write functions */ + pcilib_view_context_t *(*init)(pcilib_t *ctx, const pcilib_view_description_t *desc); /**< Optional function which should allocated context used by read/write functions */ void (*free)(pcilib_t *ctx, pcilib_view_context_t *view); /**< Optional function which should clean context */ void (*free_description)(pcilib_t *ctx, pcilib_view_description_t *view); /**< Optional function which shoud clean required parts of the extended description if non-static memory was used to initialize it */ int (*read_from_reg)(pcilib_t *ctx, pcilib_view_context_t *view, pcilib_register_value_t regval, pcilib_value_t *val); /**< Function which computes view value based on the passed the register value (view-based properties should not use register value) */ diff --git a/pcilib/xml.c b/pcilib/xml.c index f118491..4e95437 100644 --- a/pcilib/xml.c +++ b/pcilib/xml.c @@ -602,14 +602,9 @@ static int pcilib_xml_create_transform_view(pcilib_t *ctx, xmlXPathContextPtr xp } else if (!strcasecmp(name, "script")) { desc.module = malloc(strlen(value)); sprintf(desc.module, "%s", value); - - err = pcilib_py_init_script(ctx, desc.module, &mode); - if(err) return err; - mode |= PCILIB_REGISTER_INCONSISTENT; break; } } - desc.base.mode &= mode; err = pcilib_add_views_custom(ctx, 1, (pcilib_view_description_t*)&desc, &view_ctx); diff --git a/pcitool/cli.c b/pcitool/cli.c index 3676a56..0b0c98d 100644 --- a/pcitool/cli.c +++ b/pcitool/cli.c @@ -1716,8 +1716,6 @@ int WriteRegister(pcilib_t *handle, const pcilib_model_description_t *model_info printf("\n"); } } - else - printf("%s is written\n ", reg); } else { printf("%s is written\n ", reg); } diff --git a/pywrap/pcipywrap.c b/pywrap/pcipywrap.c index fc0add1..8ef1dc0 100644 --- a/pywrap/pcipywrap.c +++ b/pywrap/pcipywrap.c @@ -8,6 +8,8 @@ */ pcilib_t* __ctx = 0; +char* full_log = NULL; + /*! * \brief Wraping for vsnprintf function, that saves string to char* * \return saved from vsnprintf string @@ -52,23 +54,51 @@ char* make_str(const char* msg, ...) */ void pcilib_print_error_to_py(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *msg, - va_list va) { - char* buf_raw_msg = vmake_str(msg, va); - char* buf_wrapped_message = make_str("%s [%s:%d]\n", buf_raw_msg, file, line); - - printf("%s", buf_wrapped_message); - PyErr_SetString(PyExc_Exception, buf_wrapped_message); + va_list va) { + //wrap error message with file and line number + char* buf_raw_msg = vmake_str(msg, va); + char* buf_wrapped_message = make_str("%s [%s:%d]\n", buf_raw_msg, file, line); + + if(prio == PCILIB_LOG_ERROR) + { + if(!full_log) + full_log = make_str(""); - free(buf_wrapped_message); - free(buf_raw_msg); + if(strlen(buf_wrapped_message) >= 2 && + buf_wrapped_message[0] == '#' && + buf_wrapped_message[1] == 'E') + { + char* wrapped_exeption = make_str("%sprogramm error log:\n%s", &(buf_wrapped_message[2]), full_log); + free(full_log); + full_log = NULL; + + PyErr_SetString(PyExc_Exception, wrapped_exeption); + free(wrapped_exeption); + } + else + { + //copy received message to log + char* buf = full_log; + char* buf_start = buf; + full_log = make_str("%s%s", buf, buf_wrapped_message); + free(buf); + } + } + else + printf(buf_wrapped_message); + + free(buf_wrapped_message); + free(buf_raw_msg); } /*! - * \brief Inits pcipywrap module at importing + * \brief Redirect pcilib standart log stream to exeption text. + * Logger will accumulate errors untill get message, starts with "#E". + * After that, logger will write last error, and all accumulated errors + * to Python exeption text */ -void init_pcipywrap_module() +void __redirect_logs_to_exeption() { - printf("init_pcipywrap_module\n"); pcilib_set_logger(pcilib_get_logger_min_prio(), pcilib_print_error_to_py, pcilib_get_logger_argument()); @@ -78,17 +108,19 @@ void init_pcipywrap_module() * \brief Wraps for pcilib_open function. * \param[in] fpga_device path to the device file [/dev/fpga0] * \param[in] model specifies the model of hardware, autodetected if NULL is passed - * \return Pointer to pcilib_t, created by pcilib_open, serialized to bytearray; NULL with exeption text, if failed. + * \return Pointer to pcilib_t, created by pcilib_open; NULL with exeption text, if failed. */ PyObject* create_pcilib_instance(const char *fpga_device, const char *model) { //opening device pcilib_t* ctx = pcilib_open(fpga_device, model); if(!ctx) + { + pcilib_error("#E Failed pcilib_open(%s, %s)", fpga_device, model); return NULL; + } - //serializing object - return PyByteArray_FromStringAndSize((const char*)&ctx, sizeof(pcilib_t*)); + return PyCObject_FromVoidPtr((void*)ctx, NULL); } /*! @@ -114,23 +146,18 @@ PyObject* get_curr_pcilib_instance() /*! * \brief Sets pcilib context to wraper. - * \param[in] addr Pointer to pcilib_t, serialized to bytearray + * \param[in] addr Pointer to pcilib_t, serialized to PyCObject * \return 1, serialized to PyObject or NULL with exeption text, if failed. */ PyObject* set_pcilib(PyObject* addr) { - if(!PyByteArray_Check(addr)) + if(!PyCObject_Check(addr)) { - pcilib_error("Incorrect addr type. Only bytearray is allowed"); + pcilib_error("#E Incorrect addr type. Only PyCObject is allowed"); return NULL; } - //deserializing adress - char* pAddr = PyByteArray_AsString(addr); - - //hard copy context adress - for(int i = 0; i < sizeof(pcilib_t*) + 10; i++) - ((char*)&__ctx)[i] = pAddr[i]; + __ctx = (pcilib_t*)PyCObject_AsVoidPtr(addr); return PyInt_FromLong((long)1); } @@ -146,7 +173,7 @@ PyObject* read_register(const char *regname, const char *bank) { if(!__ctx) { - pcilib_error("pcilib_t handler not initialized"); + pcilib_error("#E pcilib_t handler not initialized"); return NULL; } @@ -157,11 +184,17 @@ PyObject* read_register(const char *regname, const char *bank) err = pcilib_read_register(__ctx, bank, regname, ®_value); if(err) + { + pcilib_error("#E Failed pcilib_read_register"); return NULL; + } err = pcilib_set_value_from_register_value(__ctx, &val, reg_value); if(err) + { + pcilib_error("#E Failed pcilib_set_value_from_register_value"); return NULL; + } return pcilib_get_value_as_pyobject(__ctx, &val); } @@ -187,15 +220,24 @@ PyObject* write_register(PyObject* val, const char *regname, const char *bank) int err; err = pcilib_set_value_from_pyobject(__ctx, val, &val_internal); if(err) + { + pcilib_error("#E Failed pcilib_set_value_from_pyobject"); return NULL; + } reg_value = pcilib_get_value_as_register_value(__ctx, &val_internal, &err); if(err) + { + pcilib_error("#E Failed pcilib_set_value_from_pyobject, (error %i)", err); return NULL; + } err = pcilib_write_register(__ctx, bank, regname, reg_value); if(err) + { + pcilib_error("#E Failed pcilib_set_value_from_pyobject, (error %i)", err); return NULL; + } return PyInt_FromLong((long)1); } @@ -208,7 +250,7 @@ PyObject* get_property(const char *prop) { if(!__ctx) { - pcilib_error("pcilib_t handler not initialized"); + pcilib_error("#E pcilib_t handler not initialized"); return NULL; } @@ -218,7 +260,10 @@ PyObject* get_property(const char *prop) err = pcilib_get_property(__ctx, prop, &val); if(err) + { + pcilib_error("#E Failed pcilib_get_property, (error %i)", err); return NULL; + } return pcilib_get_value_as_pyobject(__ctx, &val); } @@ -235,19 +280,24 @@ PyObject* set_property(PyObject* val, const char *prop) if(!__ctx) { - pcilib_error("pcilib_t handler not initialized"); + pcilib_error("#E pcilib_t handler not initialized"); return NULL; } pcilib_value_t val_internal = {0}; err = pcilib_set_value_from_pyobject(__ctx, val, &val_internal); if(err) + { + pcilib_error("#E pcilib_set_value_from_pyobject, (error %i)", err); return NULL; + } err = pcilib_set_property(__ctx, prop, &val_internal); - if(err) + { + pcilib_error("#E pcilib_set_property, (error %i)", err); return NULL; + } return PyInt_FromLong((long)1); } @@ -452,7 +502,7 @@ PyObject* get_registers_list(const char *bank) { if(!__ctx) { - pcilib_error("pcilib_t handler not initialized"); + pcilib_error("#E pcilib_t handler not initialized"); return NULL; } @@ -475,7 +525,7 @@ PyObject* get_register_info(const char* reg,const char *bank) { if(!__ctx) { - pcilib_error("pcilib_t handler not initialized"); + pcilib_error("#E pcilib_t handler not initialized"); return NULL; } @@ -497,7 +547,7 @@ PyObject* get_property_info(const char* branch) { if(!__ctx) { - pcilib_error("pcilib_t handler not initialized"); + pcilib_error("#E pcilib_t handler not initialized"); return NULL; } diff --git a/pywrap/pcipywrap.i b/pywrap/pcipywrap.i index 95045f3..4b735d2 100644 --- a/pywrap/pcipywrap.i +++ b/pywrap/pcipywrap.i @@ -1,8 +1,6 @@ %module pcipywrap -%init %{ - init_pcipywrap_module(); -%} +extern void __redirect_logs_to_exeption(); extern PyObject* create_pcilib_instance(const char *fpga_device, const char *model = NULL); extern PyObject* set_pcilib(PyObject* addr); diff --git a/pywrap/server.py b/pywrap/server.py index 0da6bc8..d2927fb 100644 --- a/pywrap/server.py +++ b/pywrap/server.py @@ -373,6 +373,9 @@ if __name__ == '__main__': if not 'LD_LIBRARY_PATH' in os.environ: os.environ['LD_LIBRARY_PATH'] = os.environ["APP_PATH"] + "/pcilib" + #redirect logs to exeption + pcipywrap.__redirect_logs_to_exeption() + pcilib_server = BaseHTTPServer.HTTPServer httpd = pcilib_server((HOST_NAME, PORT_NUMBER), PcilibServerHandler) print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER) 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 }; |