X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=src%2Fhardware%2Fcem-dt-885x%2Fapi.c;h=ff2edb1292c07e2e862f0b2b5f5e29c29e4fb754;hp=9aed547362c59bd5933c45e56cff2e24a7fb43f8;hb=1c47e0da8f2571bc34dbdc368c3c1f55318c3aa0;hpb=155b680da482cea2381becb73c51cfb838bff31e diff --git a/src/hardware/cem-dt-885x/api.c b/src/hardware/cem-dt-885x/api.c index 9aed5473..ff2edb12 100644 --- a/src/hardware/cem-dt-885x/api.c +++ b/src/hardware/cem-dt-885x/api.c @@ -17,29 +17,34 @@ * along with this program. If not, see . */ +#include #include #include "protocol.h" #define SERIALCOMM "9600/8n1" + /* 23ms is the longest interval between tokens. */ -#define MAX_SCAN_TIME 25 * 1000 +#define MAX_SCAN_TIME_US (25 * 1000) -static const int32_t hwopts[] = { +static const uint32_t scanopts[] = { SR_CONF_CONN, }; -static const int32_t hwcaps[] = { +static const uint32_t drvopts[] = { SR_CONF_SOUNDLEVELMETER, - SR_CONF_LIMIT_SAMPLES, - SR_CONF_CONTINUOUS, - SR_CONF_SPL_WEIGHT_FREQ, - SR_CONF_SPL_WEIGHT_TIME, - SR_CONF_SPL_MEASUREMENT_RANGE, - SR_CONF_DATALOG, - SR_CONF_HOLD_MAX, - SR_CONF_HOLD_MIN, - SR_CONF_POWER_OFF, - SR_CONF_DATA_SOURCE, +}; + +static const uint32_t devopts[] = { + SR_CONF_CONTINUOUS | SR_CONF_SET, + SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, + SR_CONF_SPL_WEIGHT_FREQ | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, + SR_CONF_SPL_WEIGHT_TIME | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, + SR_CONF_SPL_MEASUREMENT_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, + SR_CONF_DATALOG | SR_CONF_GET | SR_CONF_SET, + SR_CONF_HOLD_MAX | SR_CONF_GET | SR_CONF_SET, + SR_CONF_HOLD_MIN | SR_CONF_GET | SR_CONF_SET, + SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET, + SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, }; static const char *weight_freq[] = { @@ -63,23 +68,21 @@ static const char *data_sources[] = { "Live", "Memory", }; -SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info; -static struct sr_dev_driver *di = &cem_dt_885x_driver_info; +SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info; -static int init(struct sr_context *sr_ctx) +static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) { return std_init(sr_ctx, di, LOG_PREFIX); } -static GSList *scan(GSList *options) +static GSList *scan(struct sr_dev_driver *di, GSList *options) { struct drv_context *drvc; struct dev_context *devc; struct sr_config *src; struct sr_serial_dev_inst *serial; struct sr_dev_inst *sdi; - struct sr_channel *ch; GSList *l, *devices; gint64 start; const char *conn; @@ -94,41 +97,32 @@ static GSList *scan(GSList *options) if (!conn) return NULL; - if (!(serial = sr_serial_dev_inst_new(conn, SERIALCOMM))) - return NULL; + serial = sr_serial_dev_inst_new(conn, SERIALCOMM); - if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK) + if (serial_open(serial, SERIAL_RDONLY) != SR_OK) return NULL; devices = NULL; - drvc = di->priv; + drvc = di->context; start = g_get_monotonic_time(); - while (g_get_monotonic_time() - start < MAX_SCAN_TIME) { - if (serial_read(serial, &c, 1) == 1 && c == 0xa5) { + while (g_get_monotonic_time() - start < MAX_SCAN_TIME_US) { + if (serial_read_nonblocking(serial, &c, 1) == 1 && c == 0xa5) { /* Found one. */ - if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "CEM", - "DT-885x", NULL))) - return NULL; - - if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { - sr_dbg("Device context malloc failed."); - return NULL; - } + sdi = g_malloc0(sizeof(struct sr_dev_inst)); + sdi->status = SR_ST_INACTIVE; + sdi->vendor = g_strdup("CEM"); + sdi->model = g_strdup("DT-885x"); + devc = g_malloc0(sizeof(struct dev_context)); devc->cur_mqflags = 0; devc->recording = -1; devc->cur_meas_range = 0; devc->cur_data_source = DATA_SOURCE_LIVE; devc->enable_data_source_memory = FALSE; - - if (!(sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM))) - return NULL; - + sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM); sdi->inst_type = SR_INST_SERIAL; sdi->priv = devc; sdi->driver = di; - if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "SPL"))) - return NULL; - sdi->channels = g_slist_append(sdi->channels, ch); + sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL"); drvc->instances = g_slist_append(drvc->instances, sdi); devices = g_slist_append(devices, sdi); break; @@ -142,9 +136,9 @@ static GSList *scan(GSList *options) return devices; } -static GSList *dev_list(void) +static GSList *dev_list(const struct sr_dev_driver *di) { - return ((struct drv_context *)(di->priv))->instances; + return ((struct drv_context *)(di->context))->instances; } static int dev_open(struct sr_dev_inst *sdi) @@ -160,12 +154,12 @@ static int dev_open(struct sr_dev_inst *sdi) return SR_OK; } -static int cleanup(void) +static int cleanup(const struct sr_dev_driver *di) { return std_dev_clear(di, NULL); } -static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, +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; @@ -237,7 +231,7 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, return ret; } -static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, +static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { struct dev_context *devc; @@ -327,7 +321,7 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, return ret; } -static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, +static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { GVariant *tuple, *range[2]; @@ -335,40 +329,50 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, unsigned int i; int ret; - (void)sdi; (void)cg; ret = SR_OK; - switch (key) { - case SR_CONF_SCAN_OPTIONS: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, - hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); - break; - case SR_CONF_DEVICE_OPTIONS: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, - hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); - break; - case SR_CONF_SPL_WEIGHT_FREQ: - *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq)); - break; - case SR_CONF_SPL_WEIGHT_TIME: - *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time)); - break; - case SR_CONF_SPL_MEASUREMENT_RANGE: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { - range[0] = g_variant_new_uint64(meas_ranges[i][0]); - range[1] = g_variant_new_uint64(meas_ranges[i][1]); - tuple = g_variant_new_tuple(range, 2); - g_variant_builder_add_value(&gvb, tuple); + if (!sdi) { + 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, + drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); + break; + default: + return SR_ERR_NA; + } + } else { + switch (key) { + case SR_CONF_DEVICE_OPTIONS: + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, + devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); + break; + case SR_CONF_SPL_WEIGHT_FREQ: + *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq)); + break; + case SR_CONF_SPL_WEIGHT_TIME: + *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time)); + break; + case SR_CONF_SPL_MEASUREMENT_RANGE: + g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); + for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { + range[0] = g_variant_new_uint64(meas_ranges[i][0]); + range[1] = g_variant_new_uint64(meas_ranges[i][1]); + tuple = g_variant_new_tuple(range, 2); + g_variant_builder_add_value(&gvb, tuple); + } + *data = g_variant_builder_end(&gvb); + break; + case SR_CONF_DATA_SOURCE: + *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); + break; + default: + return SR_ERR_NA; } - *data = g_variant_builder_end(&gvb); - break; - case SR_CONF_DATA_SOURCE: - *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); - break; - default: - return SR_ERR_NA; } return ret; @@ -392,7 +396,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) devc->num_samples = 0; devc->buf_len = 0; - /* Send header packet to the session bus. */ std_session_send_df_header(cb_data, LOG_PREFIX); /* Poll every 100ms, or whenever some data comes in. */ @@ -428,5 +431,5 @@ SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = { .dev_close = std_serial_dev_close, .dev_acquisition_start = dev_acquisition_start, .dev_acquisition_stop = dev_acquisition_stop, - .priv = NULL, + .context = NULL, };