]> sigrok.org Git - libsigrok.git/blobdiff - hardware/atten-pps3xxx/api.c
Rename sr_probe_new() to sr_channel_new().
[libsigrok.git] / hardware / atten-pps3xxx / api.c
index 1c7608de019f2e9af0cc391461919c90d6a5efe1..e74c40de179dd3acdb9a19d6d5667ce6c7c43228 100644 (file)
@@ -31,7 +31,7 @@
  */
 #define SERIALCOMM "9600/8n2"
 
-static const int32_t hwopts[] = {
+static const int32_t scanopts[] = {
        SR_CONF_CONN,
        SR_CONF_SERIALCOMM,
 };
@@ -39,18 +39,11 @@ static const int32_t hwopts[] = {
 static const int32_t devopts[] = {
        SR_CONF_POWER_SUPPLY,
        SR_CONF_CONTINUOUS,
-};
-
-static const int32_t devopts_sdi[] = {
-       SR_CONF_POWER_SUPPLY,
-       SR_CONF_CONTINUOUS,
        SR_CONF_OUTPUT_CHANNEL,
        SR_CONF_OVER_CURRENT_PROTECTION,
 };
 
-static const int32_t devopts_pg[] = {
-       SR_CONF_POWER_SUPPLY,
-       SR_CONF_CONTINUOUS,
+static const int32_t devopts_cg[] = {
        SR_CONF_OUTPUT_VOLTAGE,
        SR_CONF_OUTPUT_VOLTAGE_MAX,
        SR_CONF_OUTPUT_CURRENT,
@@ -94,8 +87,8 @@ static GSList *scan(GSList *options, int modelid)
        struct drv_context *drvc;
        struct dev_context *devc;
        struct sr_config *src;
-       struct sr_probe *probe;
-       struct sr_probe_group *pg;
+       struct sr_channel *ch;
+       struct sr_channel_group *cg;
        struct sr_serial_dev_inst *serial;
        GSList *l, *devices;
        struct pps_model *model;
@@ -133,7 +126,7 @@ static GSList *scan(GSList *options, int modelid)
                return NULL;
        serial_flush(serial);
 
-       /* This is how the vendor software probes for hardware. */
+       /* This is how the vendor software channels for hardware. */
        memset(packet, 0, PACKET_SIZE);
        packet[0] = 0xaa;
        packet[1] = 0xaa;
@@ -174,13 +167,13 @@ static GSList *scan(GSList *options, int modelid)
        sdi->conn = serial;
        for (i = 0; i < MAX_CHANNELS; i++) {
                snprintf(channel, 10, "CH%d", i + 1);
-               probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel);
-               sdi->probes = g_slist_append(sdi->probes, probe);
-               pg = g_malloc(sizeof(struct sr_probe_group));
-               pg->name = g_strdup(channel);
-               pg->probes = g_slist_append(NULL, probe);
-               pg->priv = NULL;
-               sdi->probe_groups = g_slist_append(sdi->probe_groups, pg);
+               ch = sr_channel_new(i, SR_PROBE_ANALOG, TRUE, channel);
+               sdi->channels = g_slist_append(sdi->channels, ch);
+               cg = g_malloc(sizeof(struct sr_channel_group));
+               cg->name = g_strdup(channel);
+               cg->channels = g_slist_append(NULL, ch);
+               cg->priv = NULL;
+               sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
        }
 
        devc = g_malloc0(sizeof(struct dev_context));
@@ -207,21 +200,16 @@ static GSList *dev_list(void)
        return ((struct drv_context *)(di->priv))->instances;
 }
 
-static int dev_clear(void)
-{
-       return std_dev_clear(di, NULL);
-}
-
 static int cleanup(void)
 {
-       return dev_clear();
+       return std_dev_clear(di, NULL);
 }
 
 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
-               const struct sr_probe_group *probe_group)
+               const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
-       struct sr_probe *probe;
+       struct sr_channel *ch;
        int channel, ret;
 
        if (!sdi)
@@ -230,11 +218,11 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
        devc = sdi->priv;
 
        ret = SR_OK;
-       if (!probe_group) {
-               /* No probe group: global options. */
+       if (!cg) {
+               /* No channel group: global options. */
                switch (key) {
                case SR_CONF_OUTPUT_CHANNEL:
-                       *data = g_variant_new_string(channel_modes[devc->channel_mode - 1]);
+                       *data = g_variant_new_string(channel_modes[devc->channel_mode]);
                        break;
                case SR_CONF_OVER_CURRENT_PROTECTION:
                        *data = g_variant_new_boolean(devc->over_current_protection);
@@ -243,9 +231,9 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
                        return SR_ERR_NA;
                }
        } else {
-               /* We only ever have one channel per probe group in this driver. */
-               probe = probe_group->probes->data;
-               channel = probe->index;
+               /* We only ever have one channel per channel group in this driver. */
+               ch = cg->channels->data;
+               channel = ch->index;
 
                switch (key) {
                case SR_CONF_OUTPUT_VOLTAGE:
@@ -287,10 +275,10 @@ static int find_str(const char *str, const char **strings, int array_size)
 }
 
 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
-               const struct sr_probe_group *probe_group)
+               const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
-       struct sr_probe *probe;
+       struct sr_channel *ch;
        gdouble dval;
        int channel, ret, ival;
        const char *sval;
@@ -301,8 +289,8 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
 
        ret = SR_OK;
        devc = sdi->priv;
-       if (!probe_group) {
-               /* No probe group: global options. */
+       if (!cg) {
+               /* No channel group: global options. */
                switch (key) {
                case SR_CONF_OUTPUT_CHANNEL:
                        sval = g_variant_get_string(data, NULL);
@@ -319,8 +307,7 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
                                /* Nothing to do. */
                                break;
                        devc->channel_mode_set = ival;
-                       if (devc->acquisition_running)
-                               send_config(sdi);
+                       devc->config_dirty = TRUE;
                        break;
                case SR_CONF_OVER_CURRENT_PROTECTION:
                        bval = g_variant_get_boolean(data);
@@ -328,17 +315,16 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
                                /* Nothing to do. */
                                break;
                        devc->over_current_protection_set = bval;
-                       if (devc->acquisition_running)
-                               send_config(sdi);
+                       devc->config_dirty = TRUE;
                        break;
                default:
                        return SR_ERR_NA;
                }
        } else {
-               /* Probe group specified: per-channel options. */
-               /* We only ever have one channel per probe group in this driver. */
-               probe = probe_group->probes->data;
-               channel = probe->index;
+               /* Channel group specified: per-channel options. */
+               /* We only ever have one channel per channel group in this driver. */
+               ch = cg->channels->data;
+               channel = ch->index;
 
                switch (key) {
                case SR_CONF_OUTPUT_VOLTAGE_MAX:
@@ -346,16 +332,14 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
                        if (dval < 0 || dval > devc->model->channels[channel].voltage[1])
                                ret = SR_ERR_ARG;
                        devc->config[channel].output_voltage_max = dval;
-                       if (devc->acquisition_running)
-                               send_config(sdi);
+                       devc->config_dirty = TRUE;
                        break;
                case SR_CONF_OUTPUT_CURRENT_MAX:
                        dval = g_variant_get_double(data);
                        if (dval < 0 || dval > devc->model->channels[channel].current[1])
                                ret = SR_ERR_ARG;
                        devc->config[channel].output_current_max = dval;
-                       if (devc->acquisition_running)
-                               send_config(sdi);
+                       devc->config_dirty = TRUE;
                        break;
                case SR_CONF_OUTPUT_ENABLED:
                        bval = g_variant_get_boolean(data);
@@ -363,8 +347,7 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
                                /* Nothing to do. */
                                break;
                        devc->config[channel].output_enabled_set = bval;
-                       if (devc->acquisition_running)
-                               send_config(sdi);
+                       devc->config_dirty = TRUE;
                        break;
                default:
                        ret = SR_ERR_NA;
@@ -376,10 +359,10 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
 }
 
 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
-               const struct sr_probe_group *probe_group)
+               const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
-       struct sr_probe *probe;
+       struct sr_channel *ch;
        GVariant *gvar;
        GVariantBuilder gvb;
        int channel, ret, i;
@@ -387,30 +370,21 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
        /* Always available, even without sdi. */
        if (key == SR_CONF_SCAN_OPTIONS) {
                *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                               hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
+                               scanopts, ARRAY_SIZE(scanopts), sizeof(int32_t));
                return SR_OK;
        }
 
-       if (!sdi) {
-               if (key == SR_CONF_DEVICE_OPTIONS) {
-                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                                       devopts, ARRAY_SIZE(devopts), sizeof(int32_t));
-                       return SR_OK;
-
-               } else {
-                       /* Everything else needs an sdi. */
-                       return SR_ERR_ARG;
-               }
-       }
+       if (!sdi)
+               return SR_ERR_ARG;
        devc = sdi->priv;
 
        ret = SR_OK;
-       if (!probe_group) {
-               /* No probe group: global options. */
+       if (!cg) {
+               /* No channel group: global options. */
                switch (key) {
                case SR_CONF_DEVICE_OPTIONS:
                        *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                                       devopts_sdi, ARRAY_SIZE(devopts_sdi), sizeof(int32_t));
+                                       devopts, ARRAY_SIZE(devopts), sizeof(int32_t));
                        break;
                case SR_CONF_OUTPUT_CHANNEL:
                        if (devc->model->channel_modes == CHANMODE_INDEPENDENT) {
@@ -425,17 +399,17 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
                        return SR_ERR_NA;
                }
        } else {
-               /* Probe group specified: per-channel options. */
+               /* Channel group specified: per-channel options. */
                if (!sdi)
                        return SR_ERR_ARG;
-               /* We only ever have one channel per probe group in this driver. */
-               probe = probe_group->probes->data;
-               channel = probe->index;
+               /* We only ever have one channel per channel group in this driver. */
+               ch = cg->channels->data;
+               channel = ch->index;
 
                switch (key) {
                case SR_CONF_DEVICE_OPTIONS:
                        *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                                       devopts_pg, ARRAY_SIZE(devopts_pg), sizeof(int32_t));
+                                       devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(int32_t));
                        break;
                case SR_CONF_OUTPUT_VOLTAGE_MAX:
                        g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
@@ -463,6 +437,20 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
        return ret;
 }
 
+static int dev_close(struct sr_dev_inst *sdi)
+{
+       struct dev_context *devc;
+
+       devc = sdi->priv;
+       if (devc->config_dirty)
+               /* Some configuration changes were queued up but didn't
+                * get sent to the device, likely because we were never
+                * in acquisition mode. Send them out now. */
+               send_config(sdi);
+
+       return std_serial_dev_close(sdi);
+}
+
 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
                void *cb_data)
 {
@@ -485,7 +473,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
        serial_source_add(serial, G_IO_IN, 50, atten_pps3xxx_receive_data, (void *)sdi);
        std_session_send_df_header(cb_data, LOG_PREFIX);
 
-       /* Send a "probe" configuration packet now. */
+       /* Send a "channel" configuration packet now. */
        memset(packet, 0, PACKET_SIZE);
        packet[0] = 0xaa;
        packet[1] = 0xaa;
@@ -517,12 +505,12 @@ SR_PRIV struct sr_dev_driver atten_pps3203_driver_info = {
        .cleanup = cleanup,
        .scan = scan_3203,
        .dev_list = dev_list,
-       .dev_clear = dev_clear,
+       .dev_clear = NULL,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,
        .dev_open = std_serial_dev_open,
-       .dev_close = std_serial_dev_close,
+       .dev_close = dev_close,
        .dev_acquisition_start = dev_acquisition_start,
        .dev_acquisition_stop = dev_acquisition_stop,
        .priv = NULL,