X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Ftesto%2Fapi.c;h=43a5f2513f67ca4822356fa00e74e5b52fd5e150;hb=f6ce25ec05e8707ee3783b111ea13779f237c3b3;hp=0c37886e30c9d995e17c1a9c95eeee59c24e3679;hpb=2a8f2d41adcd0aa9e498c4eea2a5f82263039e5c;p=libsigrok.git diff --git a/src/hardware/testo/api.c b/src/hardware/testo/api.c index 0c37886e..43a5f251 100644 --- a/src/hardware/testo/api.c +++ b/src/hardware/testo/api.c @@ -23,18 +23,18 @@ #define SERIALCOMM "115200/8n1" -SR_PRIV struct sr_dev_driver testo_driver_info; -static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); - static const uint32_t scanopts[] = { SR_CONF_CONN, }; -static const uint32_t devopts[] = { +static const uint32_t drvopts[] = { SR_CONF_MULTIMETER, +}; + +static const uint32_t devopts[] = { SR_CONF_CONTINUOUS, - SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, - SR_CONF_LIMIT_MSEC | SR_CONF_SET, + SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_GET, + SR_CONF_LIMIT_MSEC | SR_CONF_SET | SR_CONF_GET, }; static const uint8_t TESTO_x35_REQUEST[] = { 0x12, 0, 0, 0, 1, 1, 0x55, 0xd1, 0xb7 }; @@ -43,11 +43,6 @@ static const struct testo_model models[] = { { "435", 9, TESTO_x35_REQUEST }, }; -static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) -{ - return std_init(sr_ctx, di, LOG_PREFIX); -} - static GSList *scan(struct sr_dev_driver *di, GSList *options) { struct drv_context *drvc; @@ -65,7 +60,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) devices = NULL; drvc = di->context; - drvc->instances = NULL; conn_devices = NULL; for (l = options; l; l = l->next) { @@ -115,9 +109,10 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) if (strncmp(manufacturer, "testo", 5)) continue; - usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); + if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0) + continue; - /* Hardcode the 435 for now.*/ + /* Hardcode the 435 for now. */ if (strcmp(product, "testo 435/635/735")) continue; @@ -125,35 +120,22 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) sdi->status = SR_ST_INACTIVE; sdi->vendor = g_strdup("Testo"); sdi->model = g_strdup("435/635/735"); - sdi->driver = di; sdi->inst_type = SR_INST_USB; sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), libusb_get_device_address(devlist[i]), NULL); sdi->connection_id = g_strdup(connection_id); devc = g_malloc(sizeof(struct dev_context)); devc->model = &models[0]; - devc->limit_msec = 0; - devc->limit_samples = 0; + sr_sw_limits_init(&devc->sw_limits); sdi->priv = devc; if (testo_probe_channels(sdi) != SR_OK) continue; - drvc->instances = g_slist_append(drvc->instances, sdi); devices = g_slist_append(devices, sdi); } libusb_free_device_list(devlist, 1); g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); - return devices; -} - -static GSList *dev_list(const struct sr_dev_driver *di) -{ - return ((struct drv_context *)(di->context))->instances; -} - -static int dev_clear(const struct sr_dev_driver *di) -{ - return std_dev_clear(di, NULL); + return std_scan_complete(di, devices); } static int dev_open(struct sr_dev_inst *sdi) @@ -161,32 +143,13 @@ static int dev_open(struct sr_dev_inst *sdi) struct sr_dev_driver *di = sdi->driver; struct drv_context *drvc = di->context; struct sr_usb_dev_inst *usb; - libusb_device **devlist; - int ret, i; - char connection_id[64]; - - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } + int ret; usb = sdi->conn; - libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); - for (i = 0; devlist[i]; i++) { - usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); - if (strcmp(sdi->connection_id, connection_id)) - continue; - if ((ret = libusb_open(devlist[i], &usb->devhdl))) { - sr_err("Failed to open device: %s.", libusb_error_name(ret)); - return SR_ERR; - } - break; - } - libusb_free_device_list(devlist, 1); - if (!devlist[i]) { - sr_err("Device not found."); - return SR_ERR; - } + + ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb); + if (ret != SR_OK) + return ret; if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) { if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { @@ -202,53 +165,31 @@ static int dev_open(struct sr_dev_inst *sdi) sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); return SR_ERR; } - sdi->status = SR_ST_ACTIVE; return SR_OK; } static int dev_close(struct sr_dev_inst *sdi) { - struct sr_dev_driver *di = sdi->driver; struct sr_usb_dev_inst *usb; - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - usb = sdi->conn; + if (!usb->devhdl) - /* Nothing to do. */ - return SR_OK; + return SR_ERR_BUG; libusb_release_interface(usb->devhdl, 0); libusb_close(usb->devhdl); usb->devhdl = NULL; - sdi->status = SR_ST_INACTIVE; return SR_OK; } -static int cleanup(const struct sr_dev_driver *di) -{ - int ret; - struct drv_context *drvc; - - if (!(drvc = di->context)) - return SR_OK; - - ret = dev_clear(di); - g_free(drvc); - - return ret; -} - -static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +static int config_get(uint32_t key, GVariant **data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { + struct dev_context *devc = sdi->priv; struct sr_usb_dev_inst *usb; - char str[128]; (void)cg; @@ -257,9 +198,11 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s if (!sdi || !sdi->conn) return SR_ERR_ARG; usb = sdi->conn; - snprintf(str, 128, "%d.%d", usb->bus, usb->address); - *data = g_variant_new_string(str); + *data = g_variant_new_printf("%d.%d", usb->bus, usb->address); break; + case SR_CONF_LIMIT_MSEC: + case SR_CONF_LIMIT_SAMPLES: + return sr_sw_limits_config_get(&devc->sw_limits, key, data); default: return SR_ERR_NA; } @@ -267,62 +210,20 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s return SR_OK; } -static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +static int config_set(uint32_t key, GVariant *data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - struct sr_dev_driver *di = sdi->driver; - struct dev_context *devc; - gint64 now; - int ret; + struct dev_context *devc = sdi->priv; (void)cg; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - - devc = sdi->priv; - ret = SR_OK; - switch (key) { - case SR_CONF_LIMIT_MSEC: - devc->limit_msec = g_variant_get_uint64(data); - now = g_get_monotonic_time() / 1000; - devc->end_time = now + devc->limit_msec; - break; - case SR_CONF_LIMIT_SAMPLES: - devc->limit_samples = g_variant_get_uint64(data); - break; - default: - ret = SR_ERR_NA; - } - - return ret; + return sr_sw_limits_config_set(&devc->sw_limits, key, data); } -static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +static int config_list(uint32_t key, GVariant **data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - (void)sdi; - (void)cg; - - switch (key) { - case SR_CONF_SCAN_OPTIONS: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); - break; - case SR_CONF_DEVICE_OPTIONS: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); - break; - default: - return SR_ERR_NA; - } - - return SR_OK; + return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); } static void receive_data(struct sr_dev_inst *sdi, unsigned char *data, int len) @@ -356,14 +257,14 @@ static void receive_data(struct sr_dev_inst *sdi, unsigned char *data, int len) crc = crc16_mcrf4xx(0xffff, devc->reply, devc->reply_size - 2); if (crc == RL16(&devc->reply[devc->reply_size - 2])) { testo_receive_packet(sdi); - devc->num_samples++; + sr_sw_limits_update_samples_read(&devc->sw_limits, 1); } else { sr_dbg("Packet has invalid CRC."); } devc->reply_size = 0; - if (devc->limit_samples && devc->num_samples >= devc->limit_samples) - dev_acquisition_stop(sdi, devc->cb_data); + if (sr_sw_limits_check(&devc->sw_limits)) + sr_dev_acquisition_stop(sdi); else testo_request_packet(sdi); @@ -383,7 +284,7 @@ SR_PRIV void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) { /* USB device was unplugged. */ - dev_acquisition_stop(sdi, devc->cb_data); + sr_dev_acquisition_stop(sdi); } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { /* First two bytes in any transfer are FTDI status bytes. */ if (transfer->actual_length > 2) @@ -398,7 +299,7 @@ SR_PRIV void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) libusb_error_name(ret)); g_free(transfer->buffer); libusb_free_transfer(transfer); - dev_acquisition_stop(sdi, devc->cb_data); + sr_dev_acquisition_stop(sdi); } } else { /* This was the last transfer we're going to receive, so @@ -413,10 +314,8 @@ static int handle_events(int fd, int revents, void *cb_data) struct sr_dev_driver *di; struct dev_context *devc; struct drv_context *drvc; - struct sr_datafeed_packet packet; struct sr_dev_inst *sdi; struct timeval tv; - gint64 now; (void)fd; (void)revents; @@ -426,19 +325,13 @@ static int handle_events(int fd, int revents, void *cb_data) di = sdi->driver; drvc = di->context; - if (devc->limit_msec) { - now = g_get_monotonic_time() / 1000; - if (now > devc->end_time) - dev_acquisition_stop(sdi, devc->cb_data); - } + if (sr_sw_limits_check(&devc->sw_limits)) + sr_dev_acquisition_stop(sdi); if (sdi->status == SR_ST_STOPPING) { usb_source_remove(sdi->session, drvc->sr_ctx); - dev_close(sdi); - - packet.type = SR_DF_END; - sr_session_send(sdi, &packet); + std_session_send_df_end(sdi); } memset(&tv, 0, sizeof(struct timeval)); @@ -448,7 +341,7 @@ static int handle_events(int fd, int revents, void *cb_data) return TRUE; } -static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) +static int dev_acquisition_start(const struct sr_dev_inst *sdi) { struct sr_dev_driver *di = sdi->driver; struct drv_context *drvc; @@ -459,23 +352,11 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) unsigned char *buf; drvc = di->context; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - devc = sdi->priv; usb = sdi->conn; - devc->cb_data = cb_data; - devc->end_time = 0; - devc->num_samples = 0; devc->reply_size = 0; - /* Send header packet to the session bus. */ - std_session_send_df_header(cb_data, LOG_PREFIX); + std_session_send_df_header(sdi); usb_source_add(sdi->session, drvc->sr_ctx, 100, handle_events, (void *)sdi); @@ -499,36 +380,27 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) } devc->reply_size = 0; + sr_sw_limits_acquisition_start(&devc->sw_limits); + return SR_OK; } -static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) +static int dev_acquisition_stop(struct sr_dev_inst *sdi) { - struct sr_dev_driver *di = sdi->driver; - (void)cb_data; - - if (!di->context) { - sr_err("Driver was not initialized."); - return SR_ERR; - } - - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - sdi->status = SR_ST_STOPPING; return SR_OK; } -SR_PRIV struct sr_dev_driver testo_driver_info = { +static struct sr_dev_driver testo_driver_info = { .name = "testo", .longname = "Testo", .api_version = 1, - .init = init, - .cleanup = cleanup, + .init = std_init, + .cleanup = std_cleanup, .scan = scan, - .dev_list = dev_list, - .dev_clear = dev_clear, + .dev_list = std_dev_list, + .dev_clear = std_dev_clear, .config_get = config_get, .config_set = config_set, .config_list = config_list, @@ -538,3 +410,4 @@ SR_PRIV struct sr_dev_driver testo_driver_info = { .dev_acquisition_stop = dev_acquisition_stop, .context = NULL, }; +SR_REGISTER_DEV_DRIVER(testo_driver_info);