HPEXPORT int HPCALL hpcables_cable_close(cable_handle * handle) { int res; if (handle != NULL) { do { int (*close) (cable_handle *); DO_BASIC_HANDLE_CHECKS() close = handle->fncts->close; if (close != NULL) { handle->busy = 1; res = (*close)(handle); if (res == ERR_SUCCESS) { handle->open = 0; hpcables_info("%s: close succeeded", __FUNCTION__); } else { hpcables_error("%s: close failed", __FUNCTION__); } handle->busy = 0; } else { res = ERR_CABLE_INVALID_FNCTS; hpcables_error("%s: fncts->close is NULL", __FUNCTION__); } } while (0); } else { res = ERR_INVALID_HANDLE; hpcables_error("%s: handle is NULL", __FUNCTION__); } return res; }
HPEXPORT int HPCALL hpcables_cable_recv (cable_handle * handle, uint8_t * data, uint32_t * len) { int res; if (handle != NULL) { do { int (*recv) (cable_handle *, uint8_t *, uint32_t *); DO_BASIC_HANDLE_CHECKS() recv = handle->fncts->recv; if (recv != NULL) { handle->busy = 1; res = (*recv)(handle, data, len); if (res == ERR_SUCCESS) { //hpcables_info("%s: recv succeeded", __FUNCTION__); } else { hpcables_warning("%s: recv failed", __FUNCTION__); } handle->busy = 0; } else { res = ERR_CABLE_INVALID_FNCTS; hpcables_error("%s: fncts->recv is NULL", __FUNCTION__); } } while (0); } else { res = ERR_INVALID_HANDLE; hpcables_error("%s: handle is NULL", __FUNCTION__); } return res; }
HPEXPORT int HPCALL hpcables_options_set_read_timeout(cable_handle * handle, int read_timeout) { int res; if (handle != NULL) { do { int (*set_read_timeout) (cable_handle *, int); DO_BASIC_HANDLE_CHECKS2() set_read_timeout = handle->fncts->set_read_timeout; if (set_read_timeout != NULL) { handle->busy = 1; res = (*set_read_timeout)(handle, read_timeout); if (res == ERR_SUCCESS) { hpcables_info("%s: set_read_timeout succeeded", __FUNCTION__); } else { hpcables_error("%s: set_read_timeout failed", __FUNCTION__); } handle->busy = 0; } else { res = ERR_CABLE_INVALID_FNCTS; hpcables_error("%s: fncts->set_read_timeout is NULL", __FUNCTION__); } } while (0); } else { res = ERR_INVALID_HANDLE; hpcables_error("%s: handle is NULL", __FUNCTION__); } return res; }
HPEXPORT int HPCALL hpcables_options_get_read_timeout(cable_handle * handle) { int timeout = 0; if (handle != NULL) { timeout = handle->read_timeout; } else { hpcables_error("%s: handle is NULL", __FUNCTION__); } return timeout; }
HPEXPORT cable_handle * HPCALL hpcables_handle_new(cable_model model) { cable_handle * handle = NULL; if (model < CABLE_MAX) { handle = (cable_handle *)calloc(1, sizeof(*handle)); if (handle != NULL) { handle->model = model; handle->handle = NULL; handle->fncts = hpcables_all_cables[model]; hpcables_info("%s: handle allocation succeeded", __FUNCTION__); } else { hpcables_error("%s: handle allocation failed", __FUNCTION__); } } else { hpcables_error("%s: invalid model", __FUNCTION__); } return handle; }
HPEXPORT int HPCALL hpcables_cable_open(cable_handle * handle) { int res; if (handle != NULL) { do { int (*open) (cable_handle *); if (handle->open) { res = ERR_CABLE_OPEN; hpcables_error("%s: cable already open", __FUNCTION__); break; } DO_BASIC_HANDLE_CHECKS2() open = handle->fncts->open; if (open != NULL) { handle->busy = 1; res = (*open)(handle); if (res == ERR_SUCCESS) { handle->open = 1; hpcables_info("%s: open succeeded", __FUNCTION__); } else { hpcables_error("%s: open failed", __FUNCTION__); } handle->busy = 0; } else { res = ERR_CABLE_INVALID_FNCTS; hpcables_error("%s: fncts->open is NULL", __FUNCTION__); } } while (0); } else { res = ERR_INVALID_HANDLE; hpcables_error("%s: handle is NULL", __FUNCTION__); } return res; }
HPEXPORT int HPCALL hpcables_handle_display(cable_handle * handle) { int res; if (handle != NULL) { hpcables_info("Link cable handle details:"); hpcables_info("\tmodel: %s", hpcables_model_to_string(handle->model)); hpcables_info("\tread_timeout: %d", handle->read_timeout); hpcables_info("\topen: %d", handle->open); hpcables_info("\tbusy: %d", handle->busy); res = ERR_SUCCESS; } else { res = ERR_INVALID_HANDLE; hpcables_error("%s: handle is NULL", __FUNCTION__); } return res; }
HPEXPORT int HPCALL hpcables_handle_del(cable_handle * handle) { int res; if (handle != NULL) { free(handle->handle); handle->handle = NULL; free(handle); handle = NULL; res = ERR_SUCCESS; hpcables_info("%s: handle deletion succeeded", __FUNCTION__); } else { res = ERR_INVALID_HANDLE; hpcables_error("%s: handle is NULL", __FUNCTION__); } return res; }
HPEXPORT int HPCALL hpcables_error_get(int number, char **message) { int ret = number; //hpcables_debug("%s: entering", __FUNCTION__); if (message != NULL) { if (number >= ERR_CABLE_FIRST && number <= ERR_CABLE_LAST) { switch (number) { case ERR_CABLE_NOT_OPEN: *message = strdup(_("Cable is not open")); break; case ERR_CABLE_OPEN: *message = strdup(_("Cable is already open")); break; case ERR_CABLE_BUSY: *message = strdup(_("Cable is busy")); break; case ERR_CABLE_WRITE_ERROR: *message = strdup(_("Error writing to cable")); break; case ERR_CABLE_READ_ERROR: *message = strdup(_("Error reading from cable")); break; case ERR_CABLE_INVALID_FNCTS: *message = strdup(_("Invalid cable functions")); break; case ERR_CABLE_PROBE_FAILED: *message = strdup(_("Cable probing failed")); break; default: *message = strdup(_("<Unknown error code>")); break; } ret = 0; } else { *message = NULL; } } else { hpcables_error("%s: message is NULL", __FUNCTION__); } //hpcables_debug("%s: exiting %d", __FUNCTION__, ret); return ret; }
HPEXPORT int HPCALL hpcables_init(void (*log_callback)(const char *format, va_list args)) { int res; // TODO: when (if) libhpcables is split from libhpfiles, copy and adjust locale setting code from hpcalcs.c. hpcables_log_set_callback(log_callback); hpcables_info(_("hpcables library version %s compiled on %s"), hpcables_version_get(), __DATE__ " " __TIME__); res = hid_init(); if (res == 0) { res = ERR_SUCCESS; hpcables_info(_("%s: init succeeded"), __FUNCTION__); } else { res = ERR_LIBRARY_INIT; hpcables_error(_("%s: init failed"), __FUNCTION__); } return res; }