]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/kecheng-kc-330b/api.c
drivers: Shorten some unnecessarily long arrays.
[libsigrok.git] / src / hardware / kecheng-kc-330b / api.c
index 07d71f48f06d7ce4b7d74ad9a960f5c6b05738e9..8f2bea58cefc1e195926783fd2ca215bc6dc517a 100644 (file)
 #define VENDOR "Kecheng"
 #define USB_INTERFACE 0
 
-static const uint32_t devopts[] = {
+static const uint32_t drvopts[] = {
        SR_CONF_SOUNDLEVELMETER,
+};
+
+static const uint32_t devopts[] = {
        SR_CONF_CONTINUOUS,
        SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_SAMPLE_INTERVAL | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
@@ -47,18 +50,15 @@ SR_PRIV const uint64_t kecheng_kc_330b_sample_intervals[][2] = {
 };
 
 static const char *weight_freq[] = {
-       "A",
-       "C",
+       "A", "C",
 };
 
 static const char *weight_time[] = {
-       "F",
-       "S",
+       "F", "S",
 };
 
 static const char *data_sources[] = {
-       "Live",
-       "Memory",
+       "Live", "Memory",
 };
 
 static int scan_kecheng(struct sr_dev_driver *di,
@@ -171,9 +171,8 @@ 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 ret;
+       return SR_OK;
 }
 
 static int dev_close(struct sr_dev_inst *sdi)
@@ -184,8 +183,7 @@ static int dev_close(struct sr_dev_inst *sdi)
        usb = sdi->conn;
 
        if (!usb->devhdl)
-               /* Nothing to do. */
-               return SR_OK;
+               return SR_ERR_BUG;
 
        /* This allows a frontend to configure the device without ever
         * doing an acquisition step. */
@@ -196,7 +194,6 @@ static int dev_close(struct sr_dev_inst *sdi)
        libusb_release_interface(usb->devhdl, USB_INTERFACE);
        libusb_close(usb->devhdl);
        usb->devhdl = NULL;
-       sdi->status = SR_ST_INACTIVE;
 
        return SR_OK;
 }
@@ -256,13 +253,12 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
        struct dev_context *devc;
        uint64_t p, q;
        unsigned int i;
-       int tmp, ret;
+       int tmp;
        const char *tmp_str;
 
        (void)cg;
 
        devc = sdi->priv;
-       ret = SR_OK;
        switch (key) {
        case SR_CONF_LIMIT_SAMPLES:
                devc->limit_samples = g_variant_get_uint64(data);
@@ -279,7 +275,7 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
                        break;
                }
                if (i == ARRAY_SIZE(kecheng_kc_330b_sample_intervals))
-                       ret = SR_ERR_ARG;
+                       return SR_ERR_ARG;
                break;
        case SR_CONF_SPL_WEIGHT_FREQ:
                tmp_str = g_variant_get_string(data, NULL);
@@ -316,45 +312,30 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
                devc->config_dirty = TRUE;
                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)
 {
-       GVariant *tuple, *rational[2];
-       GVariantBuilder gvb;
-       unsigned int i;
-
-       (void)sdi;
-       (void)cg;
-
        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;
+               return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts);
        case SR_CONF_SAMPLE_INTERVAL:
-               g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
-               for (i = 0; i < ARRAY_SIZE(kecheng_kc_330b_sample_intervals); i++) {
-                       rational[0] = g_variant_new_uint64(kecheng_kc_330b_sample_intervals[i][0]);
-                       rational[1] = g_variant_new_uint64(kecheng_kc_330b_sample_intervals[i][1]);
-                       tuple = g_variant_new_tuple(rational, 2);
-                       g_variant_builder_add_value(&gvb, tuple);
-               }
-               *data = g_variant_builder_end(&gvb);
+               *data = std_gvar_tuple_array(&kecheng_kc_330b_sample_intervals,
+                               ARRAY_SIZE(kecheng_kc_330b_sample_intervals));
                break;
        case SR_CONF_SPL_WEIGHT_FREQ:
-               *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
+               *data = g_variant_new_strv(ARRAY_AND_SIZE(weight_freq));
                break;
        case SR_CONF_SPL_WEIGHT_TIME:
-               *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
+               *data = g_variant_new_strv(ARRAY_AND_SIZE(weight_time));
                break;
        case SR_CONF_DATA_SOURCE:
-               *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
+               *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources));
                break;
        default:
                return SR_ERR_NA;
@@ -497,7 +478,7 @@ static struct sr_dev_driver kecheng_kc_330b_driver_info = {
        .cleanup = std_cleanup,
        .scan = scan,
        .dev_list = std_dev_list,
-       .dev_clear = NULL,
+       .dev_clear = std_dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,