X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fcem-dt-885x%2Fapi.c;h=6db50a90d1b9cf38b9d879f03c03213ba9dca929;hb=refs%2Fheads%2Flibsigrok-0.5.x;hp=ebeebcf2b3aebab1462a564299624b0e6d1a3461;hpb=41812aca436805b0614f2a8f31cf2f8ce494aea0;p=libsigrok.git
diff --git a/src/hardware/cem-dt-885x/api.c b/src/hardware/cem-dt-885x/api.c
index ebeebcf2..6db50a90 100644
--- a/src/hardware/cem-dt-885x/api.c
+++ b/src/hardware/cem-dt-885x/api.c
@@ -17,6 +17,7 @@
* along with this program. If not, see .
*/
+#include
#include
#include "protocol.h"
@@ -34,7 +35,7 @@ static const uint32_t drvopts[] = {
};
static const uint32_t devopts[] = {
- SR_CONF_CONTINUOUS | SR_CONF_SET,
+ SR_CONF_CONTINUOUS,
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,
@@ -68,16 +69,8 @@ static const char *data_sources[] = {
"Memory",
};
-SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info;
-
-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;
struct dev_context *devc;
struct sr_config *src;
struct sr_serial_dev_inst *serial;
@@ -102,7 +95,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
return NULL;
devices = NULL;
- drvc = di->context;
start = g_get_monotonic_time();
while (g_get_monotonic_time() - start < MAX_SCAN_TIME_US) {
if (serial_read_nonblocking(serial, &c, 1) == 1 && c == 0xa5) {
@@ -120,9 +112,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM);
sdi->inst_type = SR_INST_SERIAL;
sdi->priv = devc;
- sdi->driver = di;
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;
}
@@ -132,37 +122,13 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
serial_close(serial);
- return devices;
-}
-
-static GSList *dev_list(const struct sr_dev_driver *di)
-{
- return ((struct drv_context *)(di->context))->instances;
-}
-
-static int dev_open(struct sr_dev_inst *sdi)
-{
- struct sr_serial_dev_inst *serial;
-
- serial = sdi->conn;
- if (serial_open(serial, SERIAL_RDWR) != SR_OK)
- return SR_ERR;
-
- sdi->status = SR_ST_ACTIVE;
-
- return SR_OK;
+ return std_scan_complete(di, devices);
}
-static int cleanup(const struct sr_dev_driver *di)
-{
- return std_dev_clear(di, NULL);
-}
-
-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;
- GVariant *range[2];
uint64_t low, high;
int tmp, ret;
@@ -172,7 +138,9 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
return SR_ERR_ARG;
devc = sdi->priv;
+
ret = SR_OK;
+
switch (key) {
case SR_CONF_LIMIT_SAMPLES:
*data = g_variant_new_uint64(devc->limit_samples);
@@ -208,11 +176,8 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
*data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN);
break;
case SR_CONF_SPL_MEASUREMENT_RANGE:
- if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) {
- range[0] = g_variant_new_uint64(low);
- range[1] = g_variant_new_uint64(high);
- *data = g_variant_new_tuple(range, 2);
- }
+ if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK)
+ *data = std_gvar_tuple_u64(low, high);
break;
case SR_CONF_POWER_OFF:
*data = g_variant_new_boolean(FALSE);
@@ -230,175 +195,97 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
return ret;
}
-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 dev_context *devc;
- uint64_t tmp_u64, low, high;
- unsigned int i;
- int tmp, ret;
- const char *tmp_str;
+ int tmp, idx;
(void)cg;
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR_DEV_CLOSED;
-
- if (!(devc = sdi->priv)) {
- sr_err("sdi->priv was NULL.");
- return SR_ERR_BUG;
- }
+ devc = sdi->priv;
- ret = SR_OK;
switch (key) {
case SR_CONF_LIMIT_SAMPLES:
- tmp_u64 = g_variant_get_uint64(data);
- devc->limit_samples = tmp_u64;
- ret = SR_OK;
+ devc->limit_samples = g_variant_get_uint64(data);
break;
case SR_CONF_DATALOG:
- ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data));
- break;
+ return cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data));
case SR_CONF_SPL_WEIGHT_FREQ:
- tmp_str = g_variant_get_string(data, NULL);
- if (!strcmp(tmp_str, "A"))
- ret = cem_dt_885x_weight_freq_set(sdi,
- SR_MQFLAG_SPL_FREQ_WEIGHT_A);
- else if (!strcmp(tmp_str, "C"))
- ret = cem_dt_885x_weight_freq_set(sdi,
- SR_MQFLAG_SPL_FREQ_WEIGHT_C);
- else
+ if ((idx = std_str_idx(data, ARRAY_AND_SIZE(weight_freq))) < 0)
return SR_ERR_ARG;
- break;
+ return cem_dt_885x_weight_freq_set(sdi, (weight_freq[idx][0] == 'A') ?
+ SR_MQFLAG_SPL_FREQ_WEIGHT_A : SR_MQFLAG_SPL_FREQ_WEIGHT_C);
case SR_CONF_SPL_WEIGHT_TIME:
- tmp_str = g_variant_get_string(data, NULL);
- if (!strcmp(tmp_str, "F"))
- ret = cem_dt_885x_weight_time_set(sdi,
- SR_MQFLAG_SPL_TIME_WEIGHT_F);
- else if (!strcmp(tmp_str, "S"))
- ret = cem_dt_885x_weight_time_set(sdi,
- SR_MQFLAG_SPL_TIME_WEIGHT_S);
- else
+ if ((idx = std_str_idx(data, ARRAY_AND_SIZE(weight_time))) < 0)
return SR_ERR_ARG;
- break;
+ return cem_dt_885x_weight_time_set(sdi, (weight_time[idx][0] == 'F') ?
+ SR_MQFLAG_SPL_TIME_WEIGHT_F : SR_MQFLAG_SPL_TIME_WEIGHT_S);
case SR_CONF_HOLD_MAX:
tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0;
- ret = cem_dt_885x_holdmode_set(sdi, tmp);
- break;
+ return cem_dt_885x_holdmode_set(sdi, tmp);
case SR_CONF_HOLD_MIN:
tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0;
- ret = cem_dt_885x_holdmode_set(sdi, tmp);
- break;
+ return cem_dt_885x_holdmode_set(sdi, tmp);
case SR_CONF_SPL_MEASUREMENT_RANGE:
- g_variant_get(data, "(tt)", &low, &high);
- ret = SR_ERR_ARG;
- for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
- if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) {
- ret = cem_dt_885x_meas_range_set(sdi, low, high);
- break;
- }
- }
- break;
+ if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(meas_ranges))) < 0)
+ return SR_ERR_ARG;
+ return cem_dt_885x_meas_range_set(sdi, meas_ranges[idx][0], meas_ranges[idx][1]);
case SR_CONF_POWER_OFF:
if (g_variant_get_boolean(data))
- ret = cem_dt_885x_power_off(sdi);
+ return cem_dt_885x_power_off(sdi);
break;
case SR_CONF_DATA_SOURCE:
- tmp_str = g_variant_get_string(data, NULL);
- if (!strcmp(tmp_str, "Live"))
- devc->cur_data_source = DATA_SOURCE_LIVE;
- else if (!strcmp(tmp_str, "Memory"))
- devc->cur_data_source = DATA_SOURCE_MEMORY;
- else
- return SR_ERR;
- devc->enable_data_source_memory = devc->cur_data_source == DATA_SOURCE_MEMORY;
+ if ((idx = std_str_idx(data, ARRAY_AND_SIZE(data_sources))) < 0)
+ return SR_ERR_ARG;
+ devc->cur_data_source = idx;
+ devc->enable_data_source_memory = (idx == DATA_SOURCE_MEMORY);
break;
default:
- ret = SR_ERR_NA;
+ return SR_ERR_NA;
}
- return ret;
+ return SR_OK;
}
-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)
{
- GVariant *tuple, *range[2];
- GVariantBuilder gvb;
- unsigned int i;
- int ret;
-
- (void)cg;
-
- ret = SR_OK;
- 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;
- }
+ switch (key) {
+ case SR_CONF_SCAN_OPTIONS:
+ case SR_CONF_DEVICE_OPTIONS:
+ return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
+ case SR_CONF_SPL_WEIGHT_FREQ:
+ *data = g_variant_new_strv(ARRAY_AND_SIZE(weight_freq));
+ break;
+ case SR_CONF_SPL_WEIGHT_TIME:
+ *data = g_variant_new_strv(ARRAY_AND_SIZE(weight_time));
+ break;
+ case SR_CONF_SPL_MEASUREMENT_RANGE:
+ *data = std_gvar_tuple_array(ARRAY_AND_SIZE(meas_ranges));
+ break;
+ case SR_CONF_DATA_SOURCE:
+ *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources));
+ break;
+ default:
+ return SR_ERR_NA;
}
- return ret;
+ return SR_OK;
}
-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 dev_context *devc;
struct sr_serial_dev_inst *serial;
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR_DEV_CLOSED;
-
- if (!(devc = sdi->priv)) {
- sr_err("sdi->priv was NULL.");
- return SR_ERR_BUG;
- }
-
- devc->cb_data = cb_data;
+ devc = sdi->priv;
devc->state = ST_INIT;
devc->num_samples = 0;
devc->buf_len = 0;
- /* Send header packet to the session bus. */
- std_session_send_df_header(cb_data, LOG_PREFIX);
+ std_session_send_df_header(sdi);
- /* Poll every 100ms, or whenever some data comes in. */
serial = sdi->conn;
serial_source_add(sdi->session, serial, G_IO_IN, 150,
cem_dt_885x_receive_data, (void *)sdi);
@@ -406,30 +293,22 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
return SR_OK;
}
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
-{
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR_DEV_CLOSED;
-
- return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
- sdi->conn, LOG_PREFIX);
-}
-
-SR_PRIV struct sr_dev_driver cem_dt_885x_driver_info = {
+static struct sr_dev_driver cem_dt_885x_driver_info = {
.name = "cem-dt-885x",
.longname = "CEM DT-885x",
.api_version = 1,
- .init = init,
- .cleanup = cleanup,
+ .init = std_init,
+ .cleanup = std_cleanup,
.scan = scan,
- .dev_list = dev_list,
- .dev_clear = NULL,
+ .dev_list = std_dev_list,
+ .dev_clear = std_dev_clear,
.config_get = config_get,
.config_set = config_set,
.config_list = config_list,
- .dev_open = dev_open,
+ .dev_open = std_serial_dev_open,
.dev_close = std_serial_dev_close,
.dev_acquisition_start = dev_acquisition_start,
- .dev_acquisition_stop = dev_acquisition_stop,
+ .dev_acquisition_stop = std_serial_dev_acquisition_stop,
.context = NULL,
};
+SR_REGISTER_DEV_DRIVER(cem_dt_885x_driver_info);