]> sigrok.org Git - libsigrok.git/commitdiff
scpi-pps: Add basic cross-vendor PPS functionality.
authorBert Vermeulen <redacted>
Fri, 5 Sep 2014 01:20:32 +0000 (03:20 +0200)
committerBert Vermeulen <redacted>
Mon, 8 Sep 2014 10:45:21 +0000 (12:45 +0200)
src/hardware/scpi-pps/api.c
src/hardware/scpi-pps/protocol.c
src/hardware/scpi-pps/protocol.h

index 2a179ca62a365b3ca15064d858c2de289ad69781..ec84db74eabf856bcb6224bde03782a744bf937f 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <string.h>
 #include "protocol.h"
 
 SR_PRIV struct sr_dev_driver scpi_pps_driver_info;
 static struct sr_dev_driver *di = &scpi_pps_driver_info;
+SR_PRIV const struct scpi_pps pps_profiles[] = {};
+unsigned int num_pps_profiles;
+
+static const int32_t scanopts[] = {
+       SR_CONF_CONN,
+       SR_CONF_SERIALCOMM,
+};
 
 static int init(struct sr_context *sr_ctx)
 {
        return std_init(sr_ctx, di, LOG_PREFIX);
 }
 
-static GSList *scan(GSList *options)
+static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
 {
-       struct drv_context *drvc;
-       GSList *devices;
+       struct dev_context *devc;
+       struct sr_dev_inst *sdi;
+       struct sr_scpi_hw_info *hw_info;
+       struct sr_channel_group *cg;
+       struct sr_channel *ch;
+       const struct scpi_pps *device;
+       const struct channel_group_spec *cgs;
+       struct pps_channel_group *pcg;
+       uint64_t mask;
+       unsigned int i, j;
+
+       if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
+               sr_info("Couldn't get IDN response.");
+               return NULL;
+       }
+
+       device = NULL;
+       for (i = 0; i < num_pps_profiles; i++) {
+               if (!strcasecmp(hw_info->manufacturer, pps_profiles[i].idn_vendor) &&
+                               !strcmp(hw_info->model, pps_profiles[i].idn_model)) {
+                       device = &pps_profiles[i];
+                       break;
+               }
+       }
+       if (!device) {
+               sr_scpi_hw_info_free(hw_info);
+               return NULL;
+       }
+
+       sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, device->vendor, device->idn_model,
+                       hw_info->firmware_version);
+       sdi->conn = scpi;
+       sdi->driver = di;
+       sdi->inst_type = SR_INST_SCPI;
+       devc = g_malloc0(sizeof(struct dev_context));
+       devc->device = device;
+       sdi->priv = devc;
+
+       for (i = 0; i < device->num_channels; i++) {
+               ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE,
+                               device->channels[i].name);
+               sdi->channels = g_slist_append(sdi->channels, ch);
+       }
 
-       (void)options;
+       for (i = 0; i < device->num_channel_groups; i++) {
+               cgs = &device->channel_groups[i];
+               cg = g_malloc0(sizeof(struct sr_channel_group));
+               cg->name = g_strdup(cgs->name);
+               for (j = 0, mask = 1; j < 64; j++, mask <<= 1) {
+                       if (cgs->channel_index_mask & mask) {
+                               ch = g_slist_nth_data(sdi->channels, j);
+                               cg->channels = g_slist_append(cg->channels, ch);
+                       }
+               }
+               pcg = g_malloc0(sizeof(struct pps_channel_group));
+               pcg->features = cgs->features;
+               cg->priv = pcg;
+               sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
+       }
 
-       devices = NULL;
-       drvc = di->priv;
-       drvc->instances = NULL;
+       /* SCPI devices commonly lock the panel keys when accessed remotely. */
+       scpi_cmd(sdi, SCPI_CMD_KEY_UNLOCK);
+       sr_scpi_close(scpi);
 
-       /* TODO: scan for devices, either based on a SR_CONF_CONN option
-        * or on a USB scan. */
+       return sdi;
+}
 
-       return devices;
+static GSList *scan(GSList *options)
+{
+       return sr_scpi_scan(di->priv, options, probe_device);
 }
 
 static GSList *dev_list(void)
@@ -56,9 +121,14 @@ static int dev_clear(void)
 
 static int dev_open(struct sr_dev_inst *sdi)
 {
-       (void)sdi;
+       struct sr_scpi_dev_inst *scpi;
+
+       if (sdi->status != SR_ST_ACTIVE)
+               return SR_ERR;
 
-       /* TODO: get handle from sdi->conn and open it. */
+       scpi = sdi->conn;
+       if (sr_scpi_open(scpi) < 0)
+               return SR_ERR;
 
        sdi->status = SR_ST_ACTIVE;
 
@@ -67,38 +137,202 @@ static int dev_open(struct sr_dev_inst *sdi)
 
 static int dev_close(struct sr_dev_inst *sdi)
 {
-       (void)sdi;
+       struct sr_scpi_dev_inst *scpi;
 
-       /* TODO: get handle from sdi->conn and close it. */
+       if (sdi->status != SR_ST_ACTIVE)
+               return SR_ERR_DEV_CLOSED;
 
-       sdi->status = SR_ST_INACTIVE;
+       scpi = sdi->conn;
+       if (scpi) {
+               scpi_cmd(sdi, SCPI_CMD_KEY_UNLOCK);
+               sr_scpi_close(scpi);
+               sdi->status = SR_ST_INACTIVE;
+       }
 
        return SR_OK;
 }
 
 static int cleanup(void)
 {
-       dev_clear();
-
-       /* TODO: free other driver resources, if any. */
-
        return SR_OK;
 }
 
 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
+       struct dev_context *devc;
+       struct sr_scpi_dev_inst *scpi;
+       struct sr_channel *ch;
+       double d;
        int ret;
+       char *s;
 
-       (void)sdi;
-       (void)data;
-       (void)cg;
+       if (!sdi)
+               return SR_ERR_ARG;
+
+       devc = sdi->priv;
+       scpi = sdi->conn;
 
        ret = SR_OK;
-       switch (key) {
-       /* TODO */
-       default:
-               return SR_ERR_NA;
+       if (!cg) {
+               /* No channel group: global options. */
+               switch (key) {
+               case SR_CONF_OVER_TEMPERATURE_PROTECTION:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION) == SR_OK) {
+                               if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
+                                       *data = g_variant_new_boolean(!strcmp(s, "ON"));
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+                       return SR_ERR_NA;
+                       break;
+               case SR_CONF_OUTPUT_CHANNEL_CONFIG:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_OUTPUT_CHANNEL_CONFIG) == SR_OK) {
+                               if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
+                                       *data = g_variant_new_string(s);
+                                       g_free(s);
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+               default:
+                       return SR_ERR_NA;
+               }
+       } else {
+               /*
+                * These options only apply to channel groups with a single
+                * channel -- they're per-channel settings for the device.
+                */
+               if (g_slist_length(cg->channels) > 1)
+                       return SR_ERR_NA;
+               ch = cg->channels->data;
+
+               switch (key) {
+               case SR_CONF_OUTPUT_REGULATION:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_OUTPUT_REGULATION, ch->name) == SR_OK) {
+                               if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
+                                       if (strcmp(s, "CC") && strcmp(s, "CV") && strcmp(s, "UR")) {
+                                               sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
+                                       } else {
+                                               *data = g_variant_new_string(s);
+                                               g_free(s);
+                                               ret = SR_OK;
+                                       }
+                               }
+                       }
+                       break;
+               case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED,
+                                       ch->name) == SR_OK) {
+                               if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
+                                       *data = g_variant_new_boolean(!strcmp(s, "ON"));
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+               case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE,
+                                       ch->name) == SR_OK) {
+                               if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
+                                       *data = g_variant_new_boolean(!strcmp(s, "YES"));
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+               case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD,
+                                       ch->name) == SR_OK) {
+                               if (sr_scpi_get_double(scpi, NULL, &d) == SR_OK) {
+                                       *data = g_variant_new_double(d);
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+               case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED,
+                                       ch->name) == SR_OK) {
+                               if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
+                                       *data = g_variant_new_boolean(!strcmp(s, "ON"));
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+               case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE,
+                                       ch->name) == SR_OK) {
+                               if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
+                                       *data = g_variant_new_boolean(!strcmp(s, "YES"));
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+               case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD,
+                                       ch->name) == SR_OK) {
+                               if (sr_scpi_get_double(scpi, NULL, &d) == SR_OK) {
+                                       *data = g_variant_new_double(d);
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+               case SR_CONF_OUTPUT_VOLTAGE:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_MEAS_VOLTAGE, ch->name) == SR_OK) {
+                               if (sr_scpi_get_double(scpi, NULL, &d) == SR_OK) {
+                                       *data = g_variant_new_double(d);
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+               case SR_CONF_OUTPUT_VOLTAGE_MAX:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_VOLTAGE_MAX, ch->name) == SR_OK) {
+                               if (sr_scpi_get_double(scpi, NULL, &d) == SR_OK) {
+                                       *data = g_variant_new_double(d);
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+               case SR_CONF_OUTPUT_CURRENT:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_MEAS_CURRENT, ch->name) == SR_OK) {
+                               if (sr_scpi_get_double(scpi, NULL, &d) == SR_OK) {
+                                       *data = g_variant_new_double(d);
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+               case SR_CONF_OUTPUT_CURRENT_MAX:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_CURRENT_MAX, ch->name) == SR_OK) {
+                               if (sr_scpi_get_double(scpi, NULL, &d) == SR_OK) {
+                                       *data = g_variant_new_double(d);
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+               case SR_CONF_OUTPUT_ENABLED:
+                       ret = SR_ERR;
+                       if (scpi_cmd(sdi, SCPI_CMD_GET_OUTPUT_ENABLED, ch->name) == SR_OK) {
+                               if (sr_scpi_get_string(scpi, NULL, &s) == SR_OK) {
+                                       *data = g_variant_new_boolean(!strcmp(s, "ON"));
+                                       ret = SR_OK;
+                               }
+                       }
+                       break;
+               default:
+                       return SR_ERR_NA;
+               }
        }
 
        return ret;
@@ -107,19 +341,82 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
+       struct sr_channel *ch;
+       double d;
        int ret;
-
-       (void)data;
-       (void)cg;
+       const char *s;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
+
        ret = SR_OK;
-       switch (key) {
-       /* TODO */
-       default:
-               ret = SR_ERR_NA;
+       if (!cg) {
+               switch (key) {
+               /* No channel group: global options. */
+               case SR_CONF_OVER_TEMPERATURE_PROTECTION:
+                       s = g_variant_get_boolean(data) ? "ON" : "OFF";
+                       if (scpi_cmd(sdi, SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION, s) < 0)
+                               ret = SR_ERR;
+                       break;
+               case SR_CONF_OUTPUT_CHANNEL_CONFIG:
+                       s = g_variant_get_string(data, NULL);
+                       if (scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_CHANNEL_CONFIG, s) < 0)
+                               ret = SR_ERR;
+                       break;
+               default:
+                       ret = SR_ERR_NA;
+               }
+       } else {
+               /* Channel group specified. */
+               if (!sdi)
+                       return SR_ERR_ARG;
+               if (g_slist_length(cg->channels) > 1)
+                       return SR_ERR_NA;
+               ch = cg->channels->data;
+               switch (key) {
+               case SR_CONF_OUTPUT_VOLTAGE_MAX:
+                       d = g_variant_get_double(data);
+                       if (scpi_cmd(sdi, SCPI_CMD_SET_VOLTAGE_MAX, ch->name, d) < 0)
+                               ret = SR_ERR;
+                       break;
+               case SR_CONF_OUTPUT_CURRENT_MAX:
+                       d = g_variant_get_double(data);
+                       if (scpi_cmd(sdi, SCPI_CMD_SET_CURRENT_MAX, ch->name, d) < 0)
+                               ret = SR_ERR;
+                       break;
+               case SR_CONF_OUTPUT_ENABLED:
+                       s = g_variant_get_boolean(data) ? "ON" : "OFF";
+                       if (scpi_cmd(sdi, SCPI_CMD_SET_OUTPUT_ENABLED, ch->name, s) < 0)
+                               ret = SR_ERR;
+                       break;
+               case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
+                       s = g_variant_get_boolean(data) ? "ON" : "OFF";
+                       if (scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLED,
+                                       ch->name, s) < 0)
+                               ret = SR_ERR;
+                       break;
+               case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
+                       d = g_variant_get_double(data);
+                       if (scpi_cmd(sdi, SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD,
+                                       ch->name, d) < 0)
+                               ret = SR_ERR;
+                       break;
+               case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
+                       s = g_variant_get_boolean(data) ? "ON" : "OFF";
+                       if (scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLED,
+                                       ch->name, s) < 0)
+                               ret = SR_ERR;
+                       break;
+               case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
+                       d = g_variant_get_double(data);
+                       if (scpi_cmd(sdi, SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD,
+                                       ch->name, d) < 0)
+                               ret = SR_ERR;
+                       break;
+               default:
+                       ret = SR_ERR_NA;
+               }
        }
 
        return ret;
@@ -128,45 +425,156 @@ 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_channel_group *cg)
 {
-       int ret;
+       struct dev_context *devc;
+       struct sr_channel *ch;
+       struct channel_spec *ch_spec;
+       GVariant *gvar;
+       GVariantBuilder gvb;
+       int ret, i;
+       const char *s[16];
+
+       /* Always available, even without sdi. */
+       if (key == SR_CONF_SCAN_OPTIONS) {
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                               scanopts, ARRAY_SIZE(scanopts), sizeof(int32_t));
+               return SR_OK;
+       }
 
-       (void)sdi;
-       (void)data;
-       (void)cg;
+       if (!sdi)
+               return SR_ERR_ARG;
+       devc = sdi->priv;
 
        ret = SR_OK;
-       switch (key) {
-       /* TODO */
-       default:
-               return SR_ERR_NA;
+       if (!cg) {
+               /* No channel group: global options. */
+               switch (key) {
+               case SR_CONF_DEVICE_OPTIONS:
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                                       devc->device->devopts, devc->device->num_devopts,
+                                       sizeof(int32_t));
+                       break;
+               case SR_CONF_OUTPUT_CHANNEL_CONFIG:
+                       i = 0;
+                       if (devc->device->features & PPS_INDEPENDENT)
+                               s[i++] = "Independent";
+                       if (devc->device->features & PPS_SERIES)
+                               s[i++] = "Series";
+                       if (devc->device->features & PPS_PARALLEL)
+                               s[i++] = "Parallel";
+                       if (i == 0) {
+                               /*
+                                * Shouldn't happen: independent-only devices
+                                * shouldn't advertise this option at all.
+                                */
+                               return SR_ERR_NA;
+                       }
+                       *data = g_variant_new_strv(s, i);
+                       break;
+               default:
+                       return SR_ERR_NA;
+               }
+       } else {
+               /* Channel group specified. */
+               if (!sdi)
+                       return SR_ERR_ARG;
+               /*
+                * Per-channel-group options depending on a channel are actually
+                * done with the first channel. Channel groups in PPS can have
+                * more than one channel, but they will typically be of equal
+                * specification for use in series or parallel mode. Drop requests
+                * for groups with more than one channel just to make sure.
+                */
+               if (g_slist_length(cg->channels) > 1)
+                       return SR_ERR_NA;
+               ch = cg->channels->data;
+
+               switch (key) {
+               case SR_CONF_DEVICE_OPTIONS:
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                                       devc->device->devopts_cg, devc->device->num_devopts_cg,
+                                       sizeof(int32_t));
+                       break;
+               case SR_CONF_OUTPUT_VOLTAGE_MAX:
+                       ch_spec = &(devc->device->channels[ch->index]);
+                       g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
+                       /* Min, max, write resolution. */
+                       for (i = 0; i < 3; i++) {
+                               gvar = g_variant_new_double(ch_spec->voltage[i]);
+                               g_variant_builder_add_value(&gvb, gvar);
+                       }
+                       *data = g_variant_builder_end(&gvb);
+                       break;
+               case SR_CONF_OUTPUT_CURRENT_MAX:
+                       g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
+                       /* Min, max, step. */
+                       for (i = 0; i < 3; i++) {
+                               ch_spec = &(devc->device->channels[ch->index]);
+                               gvar = g_variant_new_double(ch_spec->current[i]);
+                               g_variant_builder_add_value(&gvb, gvar);
+                       }
+                       *data = g_variant_builder_end(&gvb);
+                       break;
+               default:
+                       return SR_ERR_NA;
+               }
        }
 
        return ret;
 }
 
 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
-                                   void *cb_data)
+               void *cb_data)
 {
-       (void)sdi;
-       (void)cb_data;
+       struct dev_context *devc;
+       struct sr_scpi_dev_inst *scpi;
+       struct sr_channel *ch;
+       int ret;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       /* TODO: configure hardware, reset acquisition state, set up
-        * callbacks and send header packet. */
+       devc = sdi->priv;
+       scpi = sdi->conn;
+       devc->cb_data = cb_data;
+
+       if ((ret = sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 100,
+                       scpi_pps_receive_data, (void *)sdi)) != SR_OK)
+               return ret;
+       std_session_send_df_header(sdi, LOG_PREFIX);
+
+       /* Prime the pipe. */
+       devc->state = STATE_VOLTAGE;
+       ch = sdi->channels->data;
+       devc->cur_channel = ch;
+       scpi_cmd(sdi, SCPI_CMD_GET_MEAS_VOLTAGE, ch->name);
 
        return SR_OK;
 }
 
 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
+       struct dev_context *devc;
+       struct sr_scpi_dev_inst *scpi;
+       float f;
+
        (void)cb_data;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       /* TODO: stop acquisition. */
+       devc = sdi->priv;
+       scpi = sdi->conn;
+
+       /*
+        * A requested value is certainly on the way. Retrieve it now,
+        * to avoid leaving the device in a state where it's not expecting
+        * commands.
+        */
+       sr_scpi_get_float(scpi, NULL, &f);
+       sr_scpi_source_remove(sdi->session, scpi);
+
+       /* Just in case something is queued up. */
+       devc->state = STATE_STOP;
 
        return SR_OK;
 }
index 1572a3eadd06e9ce3fe9f709c421007c5b7f6334..b6049dc3f75c925e141db3e46c2e1924b3244c6d 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <string.h>
+#include <stdarg.h>
 #include "protocol.h"
 
+SR_PRIV int scpi_cmd(const struct sr_dev_inst *sdi, int command, ...)
+{
+       va_list args;
+       struct dev_context *devc;
+       struct sr_scpi_dev_inst *scpi;
+       unsigned int i;
+       int ret;
+       char *cmd;
+
+       devc = sdi->priv;
+       cmd = NULL;
+       for (i = 0; i < devc->device->num_commands; i++) {
+               if (devc->device->commands[i].command == command) {
+                       cmd = devc->device->commands[i].string;
+                       break;
+               }
+       }
+       if (!cmd) {
+               /* Device does not implement this command, that's OK. */
+               return SR_OK_CONTINUE;
+       }
+
+       scpi = sdi->conn;
+       va_start(args, command);
+       ret = sr_scpi_send_variadic(scpi, cmd, args);
+       va_end(args);
+
+       return ret;
+}
+
 SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
 {
-       const struct sr_dev_inst *sdi;
        struct dev_context *devc;
+       struct sr_datafeed_packet packet;
+       struct sr_datafeed_analog analog;
+       const struct sr_dev_inst *sdi;
+       struct sr_scpi_dev_inst *scpi;
+       GSList *l;
+       float f;
 
        (void)fd;
+       (void)revents;
 
        if (!(sdi = cb_data))
                return TRUE;
@@ -32,8 +70,50 @@ SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
        if (!(devc = sdi->priv))
                return TRUE;
 
-       if (revents == G_IO_IN) {
-               /* TODO */
+       if (devc->state == STATE_STOP)
+               return TRUE;
+
+       scpi = sdi->conn;
+
+       /* Retrieve requested value for this state. */
+       if (sr_scpi_get_float(scpi, NULL, &f) == SR_OK) {
+               packet.type = SR_DF_ANALOG;
+               packet.payload = &analog;
+               analog.channels = g_slist_append(NULL, devc->cur_channel);
+               analog.num_samples = 1;
+               if (devc->state == STATE_VOLTAGE) {
+                       analog.mq = SR_MQ_VOLTAGE;
+                       analog.unit = SR_UNIT_VOLT;
+               } else {
+                       analog.mq = SR_MQ_CURRENT;
+                       analog.unit = SR_UNIT_AMPERE;
+               }
+               analog.mqflags = SR_MQFLAG_DC;
+               analog.data = &f;
+               sr_session_send(sdi, &packet);
+               g_slist_free(analog.channels);
+       }
+
+       if (devc->state == STATE_VOLTAGE) {
+               /* Just got voltage, request current for this channel. */
+               devc->state = STATE_CURRENT;
+               scpi_cmd(sdi, SCPI_CMD_GET_MEAS_CURRENT, devc->cur_channel->name);
+       } else if (devc->state == STATE_CURRENT) {
+               /*
+                * Done with voltage and current for this channel, switch to
+                * the next enabled channel.
+                */
+               do {
+                       l = g_slist_find(sdi->channels, devc->cur_channel);
+                       if (l->next)
+                               devc->cur_channel = l->next->data;
+                       else
+                               devc->cur_channel = sdi->channels->data;
+               } while (!devc->cur_channel->enabled);
+
+               /* Request voltage. */
+               devc->state = STATE_VOLTAGE;
+               scpi_cmd(sdi, SCPI_CMD_GET_MEAS_VOLTAGE, devc->cur_channel->name);
        }
 
        return TRUE;
index ee434473a4acfd109af6fc8f75dae6fb4cceb4ed..19902ccc0c40dfde72e9f1348b0c2809ebd531f6 100644 (file)
 
 #define LOG_PREFIX "scpi-pps"
 
+enum pps_scpi_cmds {
+       SCPI_CMD_KEY_UNLOCK,
+       SCPI_CMD_GET_MEAS_VOLTAGE,
+       SCPI_CMD_GET_MEAS_CURRENT,
+       SCPI_CMD_GET_MEAS_POWER,
+       SCPI_CMD_GET_VOLTAGE_MAX,
+       SCPI_CMD_SET_VOLTAGE_MAX,
+       SCPI_CMD_GET_CURRENT_MAX,
+       SCPI_CMD_SET_CURRENT_MAX,
+       SCPI_CMD_GET_OUTPUT_ENABLED,
+       SCPI_CMD_SET_OUTPUT_ENABLED,
+       SCPI_CMD_GET_OUTPUT_REGULATION,
+       SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION,
+       SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION,
+       SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED,
+       SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLED,
+       SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE,
+       SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD,
+       SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD,
+       SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED,
+       SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLED,
+       SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE,
+       SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD,
+       SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD,
+       SCPI_CMD_GET_OUTPUT_CHANNEL_CONFIG,
+       SCPI_CMD_SET_OUTPUT_CHANNEL_CONFIG,
+};
+
+/*
+ * These are bit values denoting features a device can have either globally,
+ * in scpi_pps.features, or on a per-channel-group basis in
+ * channel_group_spec.features.
+ */
+enum pps_features {
+       PPS_OTP           = (1 << 0),
+       PPS_OVP           = (1 << 1),
+       PPS_OCP           = (1 << 2),
+       PPS_INDEPENDENT   = (1 << 3),
+       PPS_SERIES        = (1 << 4),
+       PPS_PARALLEL      = (1 << 5),
+};
+
+struct scpi_pps {
+       int vendor_id;
+       char *vendor;
+       char *idn_vendor;
+       char *idn_model;
+       uint64_t features;
+       const int32_t *devopts;
+       unsigned int num_devopts;
+       const int32_t *devopts_cg;
+       unsigned int num_devopts_cg;
+       struct channel_spec *channels;
+       unsigned int num_channels;
+       struct channel_group_spec *channel_groups;
+       unsigned int num_channel_groups;
+       struct scpi_command *commands;
+       unsigned int num_commands;
+};
+
+struct channel_spec {
+       char *name;
+       /* Min, max, programming resolution. */
+       float voltage[3];
+       float current[3];
+};
+
+struct scpi_command {
+       int command;
+       char *string;
+};
+
+struct channel_group_spec {
+       char *name;
+       uint64_t channel_index_mask;
+       uint64_t features;
+};
+
+struct pps_channel_group {
+       uint64_t features;
+};
+
+enum acq_states {
+       STATE_VOLTAGE,
+       STATE_CURRENT,
+       STATE_STOP,
+};
+
 /** Private, per-device-instance driver context. */
 struct dev_context {
        /* Model-specific information */
+       const struct scpi_pps *device;
 
        /* Acquisition settings */
+       void *cb_data;
 
        /* Operational state */
 
        /* Temporary state across callbacks */
-
+       int state;
+       struct sr_channel *cur_channel;
 };
 
+SR_PRIV int scpi_cmd(const struct sr_dev_inst *sdi, int command, ...);
 SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data);
 
 #endif