]> sigrok.org Git - libsigrok.git/commitdiff
hameg-hmo: Add initial working driver version.
authorpoljar (Damir Jelić) <redacted>
Fri, 25 Oct 2013 16:15:00 +0000 (18:15 +0200)
committerUwe Hermann <redacted>
Tue, 3 Dec 2013 14:13:30 +0000 (15:13 +0100)
This patch adds initial support for Hameg's HMO oscilloscopes. It currently
supports only the HMO compact series (70MHz-200MHz).

hardware/hameg-hmo/api.c
hardware/hameg-hmo/protocol.c
hardware/hameg-hmo/protocol.h

index 8f76e65a8d29644e65bf91a16bdd12860ecb5a4d..70d91a5a9ca19c45b08f495adf07f6648bd9742e 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <stdlib.h>
+#include <glib/gstdio.h>
+
 #include "protocol.h"
 
-SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
-static struct sr_dev_driver *di = &hameg_hmo_driver_info;
+#define SERIALCOMM "115200/8n1/flow=1"
+
+static const int32_t hwopts[] = {
+       SR_CONF_CONN,
+       SR_CONF_SERIALCOMM,
+};
+
+struct usb_id_info {
+       uint16_t vendor_id;
+       uint16_t product_id;
+} usb_id_info;
+
+static struct usb_id_info ho_models[] = {
+       {
+               .vendor_id  = 0x0403,
+               .product_id = 0xed72, /* HO720 */
+       },
+       {
+               .vendor_id  = 0x0403,
+               .product_id = 0xed73, /* HO730 */
+       },
+};
 
 static int init(struct sr_context *sr_ctx)
 {
        return std_init(sr_ctx, di, LOG_PREFIX);
 }
 
+/**
+ * Find USB serial devices via the USB vendor ID and product ID.
+ *
+ * @param vendor_id vendor ID of the USB device.
+ * @param product_id product ID of the USB device.
+ *
+ * @return A GSList of strings containing the path of the serial device or null
+ * if no serial device is found. The returned list must be freed by the caller.
+ */
+static GSList *auto_find_usb(unsigned long vendor_id,
+                            unsigned long product_id)
+{
+#ifdef __linux__
+       const gchar *usb_dev;
+       const char device_tree[] = "/sys/bus/usb/devices/";
+
+       GDir *devices_dir;
+
+       GSList *l = NULL;
+       GSList *tty_devices;
+       GSList *matched_paths;
+
+       l = NULL;
+       tty_devices = NULL;
+       matched_paths = NULL;
+
+       if (!(devices_dir = g_dir_open(device_tree, 0, NULL)))
+               return NULL;
+
+       /*
+        * Find potential candidates using the vendor ID and product ID
+        * and store them in matched_paths
+        */
+       while ((usb_dev = g_dir_read_name(devices_dir))) {
+               FILE *fd;
+               char tmp[5];
+
+               gchar *vendor_path;
+               gchar *product_path;
+
+               unsigned long read_vendor_id;
+               unsigned long read_product_id;
+
+               vendor_path = g_strconcat(device_tree,
+                                         usb_dev, "/idVendor", NULL);
+               product_path = g_strconcat(device_tree,
+                                          usb_dev, "/idProduct", NULL);
+
+               if (!g_file_test(vendor_path, G_FILE_TEST_EXISTS) ||
+                   !g_file_test(product_path, G_FILE_TEST_EXISTS))
+                       goto skip_device;
+
+               if ((fd = g_fopen(vendor_path, "r")) == NULL)
+                       goto skip_device;
+
+               if (fgets(tmp, sizeof(tmp), fd) == NULL) {
+                       fclose(fd);
+                       goto skip_device;
+               }
+               read_vendor_id = strtoul(tmp, NULL, 16);
+
+               fclose(fd);
+
+               if ((fd = g_fopen(product_path, "r")) == NULL)
+                       goto skip_device;
+
+               if (fgets(tmp, sizeof(tmp), fd) == NULL) {
+                       fclose(fd);
+                       goto skip_device;
+               }
+               read_product_id = strtoul(tmp, NULL, 16);
+
+               fclose(fd);
+
+               if (vendor_id == read_vendor_id &&
+                   product_id == read_product_id) {
+                       gchar *path_copy;
+
+                       path_copy = g_strdup(usb_dev);
+                       matched_paths = g_slist_prepend(matched_paths,
+                                                       path_copy);
+               }
+
+       skip_device:
+               g_free(vendor_path);
+               g_free(product_path);
+       }
+       g_dir_close(devices_dir);
+
+       /* For every matched device try to find a ttyUSBX subfolder */
+       for (l = matched_paths; l; l = l->next) {
+               const char *file;
+
+               GDir *device_dir;
+
+               gchar *prefix;
+               gchar *subdir_path;
+               gchar *device_path;
+
+               subdir_path = NULL;
+
+               device_path = g_strconcat(device_tree, l->data, NULL);
+
+               if (!(device_dir = g_dir_open(device_path, 0, NULL))) {
+                       g_free(device_path);
+                       continue;
+               }
+
+               prefix = g_strconcat(l->data, ":", NULL);
+
+               while ((file = g_dir_read_name(device_dir))) {
+                       if (g_str_has_prefix(file, prefix)) {
+                               subdir_path = g_strconcat(device_path,
+                                                         "/", file,
+                                                         NULL);
+                               break;
+                       }
+               }
+               g_dir_close(device_dir);
+
+               g_free(prefix);
+               g_free(device_path);
+
+               if (subdir_path) {
+                       if (!(device_dir = g_dir_open(subdir_path, 0, NULL))) {
+                               g_free(subdir_path);
+                               continue;
+                       }
+                       g_free(subdir_path);
+
+                       while ((file = g_dir_read_name(device_dir))) {
+                               if (g_str_has_prefix(file, "ttyUSB")) {
+                                       gchar *tty_path;
+
+                                       tty_path = g_strconcat("/dev/",
+                                                              file, NULL);
+                                       sr_dbg("Found USB device %04x:%04x attached to %s",
+                                              vendor_id, product_id, tty_path);
+                                       tty_devices = g_slist_prepend(tty_devices,
+                                                                     tty_path);
+                                       break;
+                               }
+                       }
+                       g_dir_close(device_dir);
+               }
+       }
+       g_slist_free_full(matched_paths, g_free);
+
+       return tty_devices;
+#else
+       return NULL;
+#endif
+}
+
 static GSList *scan(GSList *options)
 {
-       struct drv_context *drvc;
        GSList *devices;
 
-       (void)options;
+       struct drv_context *drvc;
+       struct sr_dev_inst *sdi;
+
+       const char *serial_device;
+       const char *serial_options;
 
+       serial_device = NULL;
+       serial_options = SERIALCOMM;
+
+       sdi = NULL;
        devices = NULL;
        drvc = di->priv;
        drvc->instances = NULL;
 
-       /* TODO: scan for devices, either based on a SR_CONF_CONN option
-        * or on a USB scan. */
+       if (sr_serial_extract_options(options, &serial_device,
+                                     &serial_options) == SR_OK) {
+               sdi = hameg_probe_serial_device(serial_device, serial_options);
+
+               if (sdi != NULL) {
+                       devices = g_slist_append(devices, sdi);
+                       drvc->instances = g_slist_append(drvc->instances, sdi);
+               }
+
+       } else {
+               GSList *l;
+               GSList *tty_devices;
+
+               unsigned int i;
+
+               tty_devices = NULL;
+
+               for (i = 0; i < ARRAY_SIZE(ho_models); i++) {
+                       if ((l = auto_find_usb(ho_models[i].vendor_id,
+                                          ho_models[i].product_id)) == NULL)
+                               continue;
+
+                       tty_devices = g_slist_concat(tty_devices, l);
+               }
+
+               for (l = tty_devices; l; l = l->next) {
+                       sdi = hameg_probe_serial_device(l->data, serial_options);
+                       if (sdi != NULL) {
+                               devices = g_slist_append(devices, sdi);
+                               drvc->instances = g_slist_append(drvc->instances, sdi);
+                       }
+               }
+
+               g_slist_free_full(tty_devices, g_free);
+       }
 
        return devices;
 }
@@ -49,16 +266,46 @@ static GSList *dev_list(void)
        return ((struct drv_context *)(di->priv))->instances;
 }
 
+static void clear_helper(void *priv)
+{
+       unsigned int i;
+
+       struct dev_context *devc;
+       struct scope_config *model;
+
+       devc = priv;
+       model = devc->model_config;
+
+       scope_state_free(devc->model_state);
+
+       for (i = 0; i < model->analog_channels; ++i) {
+               g_slist_free(devc->analog_groups[i].probes);
+       }
+
+       for (i = 0; i < model->digital_pods; ++i) {
+               g_slist_free(devc->digital_groups[i].probes);
+               g_free(devc->digital_groups[i].name);
+       }
+
+       g_free(devc->analog_groups);
+       g_free(devc->digital_groups);
+
+       g_free(devc);
+}
+
 static int dev_clear(void)
 {
-       return std_dev_clear(di, NULL);
+       return std_dev_clear(di, clear_helper);
 }
 
 static int dev_open(struct sr_dev_inst *sdi)
 {
-       (void)sdi;
+       if (sdi->status != SR_ST_ACTIVE &&
+           serial_open(sdi->conn, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
+               return SR_ERR;
 
-       /* TODO: get handle from sdi->conn and open it. */
+       if (scope_state_get(sdi) != SR_OK)
+               return SR_ERR;
 
        sdi->status = SR_ST_ACTIVE;
 
@@ -67,9 +314,10 @@ static int dev_open(struct sr_dev_inst *sdi)
 
 static int dev_close(struct sr_dev_inst *sdi)
 {
-       (void)sdi;
+       if (sdi->status == SR_ST_INACTIVE)
+               return SR_OK;
 
-       /* TODO: get handle from sdi->conn and close it. */
+       serial_close(sdi->conn);
 
        sdi->status = SR_ST_INACTIVE;
 
@@ -80,87 +328,590 @@ 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)
+static int check_probe_group(struct dev_context *devc,
+                            const struct sr_probe_group *probe_group)
+{
+       unsigned int i;
+       struct scope_config *model;
+
+       model = devc->model_config;
+
+       if (!probe_group)
+               return PG_NONE;
+
+       for (i = 0; i < model->analog_channels; ++i)
+               if (probe_group == &devc->analog_groups[i])
+                       return PG_ANALOG;
+
+       for (i = 0; i < model->digital_pods; ++i)
+               if (probe_group == &devc->digital_groups[i])
+                       return PG_DIGITAL;
+
+       sr_err("Invalid probe group specified.");
+       return PG_INVALID;
+}
+
+static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
+                     const struct sr_probe_group *probe_group)
 {
        int ret;
+       int pg_type;
+       unsigned int i;
+
+       struct dev_context *devc;
+       struct scope_config *model;
+
+       if (!sdi || !(devc = sdi->priv))
+               return SR_ERR_ARG;
+
+       if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
+               return SR_ERR;
 
-       (void)sdi;
-       (void)data;
+       ret = SR_ERR_NA;
+       model = devc->model_config;
 
-       ret = SR_OK;
        switch (key) {
-       /* TODO */
+       case SR_CONF_NUM_TIMEBASE:
+               *data = g_variant_new_int32(model->num_xdivs);
+               ret = SR_OK;
+               break;
+
+       case SR_CONF_NUM_VDIV:
+               if (pg_type == PG_NONE) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+
+               } else if (pg_type == PG_ANALOG) {
+                       for (i = 0; i < model->analog_channels; ++i) {
+                               if (probe_group == &devc->analog_groups[i]) {
+                                       *data = g_variant_new_int32(model->num_ydivs);
+                                       ret = SR_OK;
+                                       break;
+                               }
+                       }
+
+               } else {
+                       ret = SR_ERR_NA;
+               }
+               break;
+
        default:
-               return SR_ERR_NA;
+               ret = SR_ERR_NA;
        }
 
        return ret;
 }
 
-static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
+static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
+{
+       unsigned int i;
+
+       GVariant *rational[2];
+       GVariantBuilder gvb;
+
+       g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
+
+       for (i = 0; i < n; i++) {
+               rational[0] = g_variant_new_uint64((*array)[i][0]);
+               rational[1] = g_variant_new_uint64((*array)[i][1]);
+
+               /* FIXME valgrind reports a memory leak here */
+               g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
+       }
+
+       return g_variant_builder_end(&gvb);
+}
+
+static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
+                     const struct sr_probe_group *probe_group)
 {
        int ret;
+       int pg_type;
+       unsigned int i;
+       char command[MAX_COMMAND_SIZE];
 
-       (void)data;
+       struct dev_context *devc;
+       struct scope_config *model;
+       struct scope_state *state;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
+       if (!sdi || !(devc = sdi->priv))
+               return SR_ERR_ARG;
+
+       if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
+               return SR_ERR;
+
+       model = devc->model_config;
+       state = devc->model_state;
+
+       ret = SR_ERR_NA;
 
-       ret = SR_OK;
        switch (key) {
-       /* TODO */
+       case SR_CONF_LIMIT_FRAMES:
+               devc->frame_limit = g_variant_get_uint64(data);
+               ret = SR_OK;
+               break;
+
+       case SR_CONF_TRIGGER_SOURCE:
+       {
+               const char *tmp;
+
+               tmp = g_variant_get_string(data, NULL);
+
+               for (i = 0; (*model->trigger_sources)[i]; i++) {
+                       if (!g_strcmp0(tmp, (*model->trigger_sources)[i])) {
+                               state->trigger_source = i;
+
+                               g_snprintf(command, sizeof(command),
+                                          (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
+                                          (*model->trigger_sources)[i]);
+
+                               ret = sr_scpi_send(sdi->conn, command);
+                               break;
+                       }
+               }
+       }
+       break;
+
+       case SR_CONF_VDIV:
+       {
+               unsigned int j;
+               uint64_t p, q;
+
+               if (pg_type == PG_NONE) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
+
+               g_variant_get(data, "(tt)", &p, &q);
+
+               for (i = 0; i < model->num_vdivs; i++) {
+                       if (p == (*model->vdivs)[i][0] &&
+                           q == (*model->vdivs)[i][1]){
+                               for (j = 1; j <= model->analog_channels; ++j) {
+                                       if (probe_group == &devc->analog_groups[j - 1]) {
+                                               state->analog_channels[j-1].vdiv = (float) p / q;
+                                               g_snprintf(command, sizeof(command),
+                                                          (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
+                                                          j, state->analog_channels[j-1].vdiv);
+
+                                               if (sr_scpi_send(sdi->conn, command) != SR_OK ||
+                                                   sr_scpi_get_opc(sdi->conn) != SR_OK)
+                                                       return SR_ERR;
+
+                                               break;
+                                       }
+                               }
+
+                               ret = SR_OK;
+                               break;
+                       }
+               }
+       }
+       break;
+
+       case SR_CONF_TIMEBASE:
+       {
+               uint64_t p, q;
+
+               g_variant_get(data, "(tt)", &p, &q);
+
+               for (i = 0; i < model->num_timebases; i++) {
+                       if (p == (*model->timebases)[i][0] &&
+                           q == (*model->timebases)[i][1]){
+                               state->timebase = (float) p / q;
+                               g_snprintf(command, sizeof(command),
+                                          (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
+                                          state->timebase);
+
+                               ret = sr_scpi_send(sdi->conn, command);
+                               break;
+                       }
+               }
+       }
+       break;
+
+       case SR_CONF_HORIZ_TRIGGERPOS:
+       {
+               double tmp;
+
+               tmp = g_variant_get_double(data);
+
+               if (tmp < 0.0 || tmp > 1.0)
+                       return SR_ERR;
+
+               state->horiz_triggerpos = -(tmp - 0.5) * state->timebase * model->num_xdivs;
+               g_snprintf(command, sizeof(command),
+                          (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
+                          state->horiz_triggerpos);
+
+               ret = sr_scpi_send(sdi->conn, command);
+       }
+       break;
+
+       case SR_CONF_TRIGGER_SLOPE:
+       {
+               uint64_t tmp;
+
+               tmp = g_variant_get_uint64(data);
+
+               if (tmp != 0 && tmp != 1)
+                       return SR_ERR;
+
+               state->trigger_slope = tmp;
+
+               g_snprintf(command, sizeof(command),
+                          (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
+                          tmp ? "POS" : "NEG");
+
+               ret = sr_scpi_send(sdi->conn, command);
+       }
+       break;
+
+       case SR_CONF_COUPLING:
+       {
+               unsigned int j;
+               const char *tmp;
+
+               if (pg_type == PG_NONE) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
+
+               tmp = g_variant_get_string(data, NULL);
+
+               for (i = 0; (*model->coupling_options)[i]; i++) {
+                       if (!strcmp(tmp, (*model->coupling_options)[i])) {
+                               for (j = 1; j <= model->analog_channels; ++j) {
+                                       if (probe_group == &devc->analog_groups[j - 1]) {
+                                               state->analog_channels[j-1].coupling = i;
+
+                                               g_snprintf(command, sizeof(command),
+                                                          (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
+                                                          j, tmp);
+
+                                               if (sr_scpi_send(sdi->conn, command) != SR_OK ||
+                                                   sr_scpi_get_opc(sdi->conn) != SR_OK)
+                                                       return SR_ERR;
+                                               break;
+                                       }
+                               }
+
+                               ret = SR_OK;
+                               break;
+                       }
+               }
+       }
+       break;
+
        default:
                ret = SR_ERR_NA;
+               break;
        }
 
+       if (ret == SR_OK)
+               ret = sr_scpi_get_opc(sdi->conn);
+
        return ret;
 }
 
-static int config_list(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)
 {
-       int ret;
+       int pg_type;
+
+       struct dev_context *devc;
+       struct scope_config *model;
+
+       if (!sdi || !(devc = sdi->priv))
+               return SR_ERR_ARG;
 
-       (void)sdi;
-       (void)data;
+       if ((pg_type = check_probe_group(devc, probe_group)) == PG_INVALID)
+               return SR_ERR;
+
+       model = devc->model_config;
 
-       ret = SR_OK;
        switch (key) {
-       /* TODO */
+       case SR_CONF_DEVICE_OPTIONS:
+               if (pg_type == PG_NONE) {
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                                                         model->hw_caps,
+                                                         model->num_hwcaps,
+                                                         sizeof(int32_t));
+               } else if (pg_type == PG_ANALOG) {
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                                                         model->analog_hwcaps,
+                                                         model->num_analog_hwcaps,
+                                                         sizeof(int32_t));
+               } else {
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                                                         NULL, 0, sizeof(int32_t));
+               }
+               break;
+
+       case SR_CONF_COUPLING:
+               if (pg_type == PG_NONE)
+                       return SR_ERR_PROBE_GROUP;
+
+               *data = g_variant_new_strv(*model->coupling_options,
+                                          g_strv_length((char **) *model->coupling_options));
+               break;
+
+       case SR_CONF_TRIGGER_SOURCE:
+               *data = g_variant_new_strv(*model->trigger_sources,
+                                          g_strv_length((char **) *model->trigger_sources));
+               break;
+
+       case SR_CONF_TIMEBASE:
+               *data = build_tuples(model->timebases, model->num_timebases);
+               break;
+
+       case SR_CONF_VDIV:
+               if (pg_type == PG_NONE)
+                       return SR_ERR_PROBE_GROUP;
+
+               *data = build_tuples(model->vdivs, model->num_vdivs);
+               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)
+SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
 {
-       (void)sdi;
-       (void)cb_data;
+       char command[MAX_COMMAND_SIZE];
+
+       struct sr_probe *probe;
+       struct dev_context *devc;
+       struct scope_config *model;
+
+       devc = sdi->priv;
+       model = devc->model_config;
+
+       probe = devc->current_probe->data;
+
+       switch (probe->type) {
+       case SR_PROBE_ANALOG:
+               g_snprintf(command, sizeof(command),
+                          (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
+                          probe->index + 1);
+               break;
+       case SR_PROBE_LOGIC:
+               g_snprintf(command, sizeof(command),
+                          (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
+                          probe->index < 8 ? 1 : 2);
+               break;
+       default:
+               sr_err("Invalid probe type");
+               break;
+       }
+
+       return sr_scpi_send(sdi->conn, command);
+}
+
+static int hmo_check_probes(GSList *probes)
+{
+       GSList *l;
+       gboolean enabled_pod1;
+       gboolean enabled_pod2;
+       gboolean enabled_chan3;
+       gboolean enabled_chan4;
+
+       struct sr_probe *probe;
+
+       enabled_pod1 = FALSE;
+       enabled_pod2 = FALSE;
+       enabled_chan3 = FALSE;
+       enabled_chan4 = FALSE;
+
+       for (l = probes; l; l = l->next) {
+               probe = l->data;
+
+               switch (probe->type) {
+               case SR_PROBE_ANALOG:
+                       if (probe->index == 2)
+                               enabled_chan3 = TRUE;
+                       else if (probe->index == 3)
+                               enabled_chan4 = TRUE;
+                       break;
+
+               case SR_PROBE_LOGIC:
+                       if (probe->index < 8)
+                               enabled_pod1 = TRUE;
+                       else
+                               enabled_pod2 = TRUE;
+                       break;
+
+               default:
+                       return SR_ERR;
+               }
+       }
+
+       if ((enabled_pod1 && enabled_chan3) ||
+           (enabled_pod2 && enabled_chan4))
+               return SR_ERR;
+
+       return SR_OK;
+}
+
+static int hmo_setup_probes(const struct sr_dev_inst *sdi)
+{
+       GSList *l;
+       unsigned int i;
+       gboolean *pod_enabled;
+       char command[MAX_COMMAND_SIZE];
+
+       struct scope_state *state;
+       struct scope_config *model;
+
+       struct sr_probe *probe;
+       struct dev_context *devc;
+       struct sr_serial_dev_inst *serial;
+
+       devc = sdi->priv;
+       serial = sdi->conn;
+       state = devc->model_state;
+       model = devc->model_config;
+
+       pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
+
+       for (l = sdi->probes; l; l = l->next) {
+               probe = l->data;
+
+               switch (probe->type) {
+               case SR_PROBE_ANALOG:
+               {
+                       if (probe->enabled != state->analog_channels[probe->index].state) {
+                               g_snprintf(command, sizeof(command),
+                                          (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
+                                          probe->index + 1, probe->enabled);
+
+                               if (sr_scpi_send(serial, command) != SR_OK)
+                                       return SR_ERR;
+                               state->analog_channels[probe->index].state = probe->enabled;
+                       }
+               }
+               break;
+
+               case SR_PROBE_LOGIC:
+               {
+                       /*
+                        * A digital POD needs to be enabled for every group of
+                        * 8 probes.
+                        */
+                       if (probe->enabled)
+                               pod_enabled[probe->index < 8 ? 0 : 1] = TRUE;
+
+                       if (probe->enabled != state->digital_channels[probe->index]) {
+                               g_snprintf(command, sizeof(command),
+                                          (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
+                                          probe->index, probe->enabled);
+
+                               if (sr_scpi_send(serial, command) != SR_OK)
+                                       return SR_ERR;
+
+                               state->digital_channels[probe->index] = probe->enabled;
+                       }
+               }
+               break;
+
+               default:
+                       return SR_ERR;
+               }
+       }
+
+       for (i = 1; i <= model->digital_pods; ++i) {
+               if (state->digital_pods[i-1] != pod_enabled[i-1]) {
+                       g_snprintf(command, sizeof(command),
+                                  (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
+                                  i, pod_enabled[i-1]);
+
+                       if (sr_scpi_send(serial, command) != SR_OK)
+                               return SR_ERR;
+
+                       state->digital_pods[i-1] = pod_enabled[i-1];
+               }
+       }
+
+       g_free(pod_enabled);
+
+       return SR_OK;
+}
+
+static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
+{
+       GSList *l;
+       gboolean digital_added;
+
+       struct sr_probe *probe;
+       struct dev_context *devc;
+       struct sr_serial_dev_inst *serial;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       /* TODO: configure hardware, reset acquisition state, set up
-        * callbacks and send header packet. */
+       serial = sdi->conn;
+       devc = sdi->priv;
+       digital_added = FALSE;
+
+       for (l = sdi->probes; l; l = l->next) {
+               probe = l->data;
+
+               if (probe->enabled) {
+                       /* Only add a single digital probe */
+                       if (probe->type != SR_PROBE_LOGIC  || !digital_added) {
+                               devc->enabled_probes = g_slist_append(devc->enabled_probes,
+                                                                     probe);
+                               if (probe->type == SR_PROBE_LOGIC)
+                                       digital_added = TRUE;
+                       }
+               }
+       }
 
-       return SR_OK;
+       if (!devc->enabled_probes)
+               return SR_ERR;
+
+       if (hmo_check_probes(devc->enabled_probes) != SR_OK) {
+               sr_err("Invalid probe configuration specified!");
+               return SR_ERR_NA;
+       }
+
+       if (hmo_setup_probes(sdi) != SR_OK) {
+               sr_err("Failed to setup probe configuration!");
+               return SR_ERR;
+       }
+
+       sr_source_add(serial->fd, G_IO_IN, 50, hameg_hmo_receive_data, (void *)sdi);
+
+       /* Send header packet to the session bus. */
+       std_session_send_df_header(cb_data, LOG_PREFIX);
+
+       devc->current_probe = devc->enabled_probes;
+
+       return hmo_request_data(sdi);
 }
 
 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
+       struct dev_context *devc;
+       struct sr_serial_dev_inst *serial;
+
        (void)cb_data;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       /* TODO: stop acquisition. */
+       devc = sdi->priv;
+
+       g_slist_free(devc->enabled_probes);
+       devc->enabled_probes = NULL;
+       serial = sdi->conn;
+       sr_source_remove(serial->fd);
 
        return SR_OK;
 }
index 64b35e98df87c381ee4dfdeabae03961a7613ef4..cd94f0ca1bb9393fec76f0065bf0805a9c914cd6 100644 (file)
 
 #include "protocol.h"
 
+static const char *manufacturers[] = {
+       "HAMEG",
+};
+
+static const char *hameg_scpi_dialect[] = {
+       [SCPI_CMD_GET_DIG_DATA]             = ":POD%d:DATA?",
+       [SCPI_CMD_GET_TIMEBASE]             = ":TIM:SCAL?",
+       [SCPI_CMD_SET_TIMEBASE]             = ":TIM:SCAL %E",
+       [SCPI_CMD_GET_COUPLING]             = ":CHAN%d:COUP?",
+       [SCPI_CMD_SET_COUPLING]             = ":CHAN%d:COUP %s",
+       [SCPI_CMD_GET_ANALOG_DATA]          = ":CHAN%d:DATA?",
+       [SCPI_CMD_GET_VERTICAL_DIV]         = ":CHAN%d:SCAL?",
+       [SCPI_CMD_SET_VERTICAL_DIV]         = ":CHAN%d:SCAL %E",
+       [SCPI_CMD_GET_DIG_POD_STATE]        = ":POD%d:STAT?",
+       [SCPI_CMD_SET_DIG_POD_STATE]        = ":POD%d:STAT %d",
+       [SCPI_CMD_GET_TRIGGER_SLOPE]        = ":TRIG:A:EDGE:SLOP?",
+       [SCPI_CMD_SET_TRIGGER_SLOPE]        = ":TRIG:A:EDGE:SLOP %s",
+       [SCPI_CMD_GET_TRIGGER_SOURCE]       = ":TRIG:A:SOUR?",
+       [SCPI_CMD_SET_TRIGGER_SOURCE]       = ":TRIG:A:SOUR %s",
+       [SCPI_CMD_GET_DIG_CHAN_STATE]       = ":LOG%d:STAT?",
+       [SCPI_CMD_SET_DIG_CHAN_STATE]       = ":LOG%d:STAT %d",
+       [SCPI_CMD_GET_VERTICAL_OFFSET]      = ":CHAN%d:POS?",
+       [SCPI_CMD_GET_HORIZ_TRIGGERPOS]     = ":TIM:POS?",
+       [SCPI_CMD_SET_HORIZ_TRIGGERPOS]     = ":TIM:POS %E",
+       [SCPI_CMD_GET_ANALOG_CHAN_STATE]    = ":CHAN%d:STAT?",
+       [SCPI_CMD_SET_ANALOG_CHAN_STATE]    = ":CHAN%d:STAT %d",
+};
+
+static const int32_t hmo_hwcaps[] = {
+       SR_CONF_OSCILLOSCOPE,
+       SR_CONF_TRIGGER_SOURCE,
+       SR_CONF_TIMEBASE,
+       SR_CONF_NUM_TIMEBASE,
+       SR_CONF_TRIGGER_SLOPE,
+       SR_CONF_HORIZ_TRIGGERPOS,
+};
+
+static const int32_t hmo_analog_caps[] = {
+       SR_CONF_NUM_VDIV,
+       SR_CONF_COUPLING,
+       SR_CONF_VDIV,
+};
+
+static const char *hmo_coupling_options[] = {
+       "AC",
+       "ACL",
+       "DC",
+       "GND",
+       NULL,
+};
+
+static const char *scope_trigger_slopes[] = {
+       "POS",
+       "NEG",
+       NULL,
+};
+
+static const char *hmo_compact2_trigger_sources[] = {
+       "CH1",
+       "CH2",
+       "LINE",
+       "EXT",
+       "D0",
+       "D1",
+       "D2",
+       "D3",
+       "D4",
+       "D5",
+       "D6",
+       "D7",
+       NULL,
+};
+
+static const char *hmo_compact4_trigger_sources[] = {
+       "CH1",
+       "CH2",
+       "CH3",
+       "CH4",
+       "LINE",
+       "EXT",
+       "D0",
+       "D1",
+       "D2",
+       "D3",
+       "D4",
+       "D5",
+       "D6",
+       "D7",
+       NULL,
+};
+
+static const uint64_t hmo_timebases[][2] = {
+       /* nanoseconds */
+       { 2, 1000000000 },
+       { 5, 1000000000 },
+       { 10, 1000000000 },
+       { 20, 1000000000 },
+       { 50, 1000000000 },
+       { 100, 1000000000 },
+       { 200, 1000000000 },
+       { 500, 1000000000 },
+       /* microseconds */
+       { 1, 1000000 },
+       { 2, 1000000 },
+       { 5, 1000000 },
+       { 10, 1000000 },
+       { 20, 1000000 },
+       { 50, 1000000 },
+       { 100, 1000000 },
+       { 200, 1000000 },
+       { 500, 1000000 },
+       /* milliseconds */
+       { 1, 1000 },
+       { 2, 1000 },
+       { 5, 1000 },
+       { 10, 1000 },
+       { 20, 1000 },
+       { 50, 1000 },
+       { 100, 1000 },
+       { 200, 1000 },
+       { 500, 1000 },
+       /* seconds */
+       { 1, 1 },
+       { 2, 1 },
+       { 5, 1 },
+       { 10, 1 },
+       { 20, 1 },
+       { 50, 1 },
+};
+
+static const uint64_t hmo_vdivs[][2] = {
+       /* millivolts */
+       { 1, 1000 },
+       { 2, 1000 },
+       { 5, 1000 },
+       { 10, 1000 },
+       { 20, 1000 },
+       { 50, 1000 },
+       { 100, 1000 },
+       { 200, 1000 },
+       { 500, 1000 },
+       /* volts */
+       { 1, 1 },
+       { 2, 1 },
+       { 5, 1 },
+       { 10, 1 },
+};
+
+static const char *scope_analog_probe_names[] = {
+       "CH1",
+       "CH2",
+       "CH3",
+       "CH4",
+};
+
+static const char *scope_digital_probe_names[] = {
+       "D0",
+       "D1",
+       "D2",
+       "D3",
+       "D4",
+       "D5",
+       "D6",
+       "D7",
+       "D8",
+       "D9",
+       "D10",
+       "D11",
+       "D12",
+       "D13",
+       "D14",
+       "D15",
+};
+
+static struct scope_config scope_models[] = {
+       {
+               .name = {"HMO722", "HMO1022", "HMO1522", "HMO2022", NULL},
+               .analog_channels = 2,
+               .digital_channels = 8,
+               .digital_pods = 1,
+
+               .analog_names = &scope_analog_probe_names,
+               .digital_names = &scope_digital_probe_names,
+
+               .hw_caps = &hmo_hwcaps,
+               .num_hwcaps = ARRAY_SIZE(hmo_hwcaps),
+
+               .analog_hwcaps = &hmo_analog_caps,
+               .num_analog_hwcaps = ARRAY_SIZE(hmo_analog_caps),
+
+               .coupling_options = &hmo_coupling_options,
+               .trigger_sources = &hmo_compact2_trigger_sources,
+               .trigger_slopes = &scope_trigger_slopes,
+
+               .timebases = &hmo_timebases,
+               .num_timebases = ARRAY_SIZE(hmo_timebases),
+
+               .vdivs = &hmo_vdivs,
+               .num_vdivs = ARRAY_SIZE(hmo_vdivs),
+
+               .num_xdivs = 12,
+               .num_ydivs = 8,
+
+               .scpi_dialect = &hameg_scpi_dialect,
+       },
+       {
+               .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
+               .analog_channels = 4,
+               .digital_channels = 8,
+               .digital_pods = 1,
+
+               .analog_names = &scope_analog_probe_names,
+               .digital_names = &scope_digital_probe_names,
+
+               .hw_caps = &hmo_hwcaps,
+               .num_hwcaps = ARRAY_SIZE(hmo_hwcaps),
+
+               .analog_hwcaps = &hmo_analog_caps,
+               .num_analog_hwcaps = ARRAY_SIZE(hmo_analog_caps),
+
+               .coupling_options = &hmo_coupling_options,
+               .trigger_sources = &hmo_compact4_trigger_sources,
+               .trigger_slopes = &scope_trigger_slopes,
+
+               .timebases = &hmo_timebases,
+               .num_timebases = ARRAY_SIZE(hmo_timebases),
+
+               .vdivs = &hmo_vdivs,
+               .num_vdivs = ARRAY_SIZE(hmo_vdivs),
+
+               .num_xdivs = 12,
+               .num_ydivs = 8,
+
+               .scpi_dialect = &hameg_scpi_dialect,
+       },
+};
+
+static int check_manufacturer(const char *manufacturer)
+{
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(manufacturers); ++i)
+               if (strcmp(manufacturer, manufacturers[i]) == 0)
+                       return SR_OK;
+
+       return SR_ERR;
+}
+
+static void scope_state_dump(struct scope_config *config,
+                            struct scope_state *state)
+{
+       unsigned int i;
+
+       for (i = 0; i < config->analog_channels; ++i) {
+               sr_info("State of analog channel %d -> %s : %s %.3eV %.3e offset", i+1,
+                       state->analog_channels[i].state ? "On" : "Off",
+                       (*config->coupling_options)[state->analog_channels[i].coupling],
+                       state->analog_channels[i].vdiv, state->analog_channels[i].vertical_offset);
+       }
+
+       for (i = 0; i < config->digital_channels; ++i) {
+               sr_info("State of digital channel %d -> %s", i,
+                       state->digital_channels[i] ? "On" : "Off");
+       }
+
+       for (i = 0; i < config->digital_pods; ++i) {
+               sr_info("State of digital POD %d -> %s", i,
+                       state->digital_pods[i] ? "On" : "Off");
+       }
+
+       sr_info("Current timebase: %.2es", state->timebase);
+       sr_info("Current trigger: %s (source), %s (slope) %.2e (offset)",
+               (*config->trigger_sources)[state->trigger_source],
+               (*config->trigger_slopes)[state->trigger_slope],
+               state->horiz_triggerpos);
+}
+
+static int scope_state_get_array_option(struct sr_serial_dev_inst *serial,
+                                       const char *command, const char *(*array)[],
+                                       int *result)
+{
+       char *tmp;
+       unsigned int i;
+
+       if (sr_scpi_get_string(serial, command, &tmp) != SR_OK) {
+               if (tmp)
+                       g_free(tmp);
+               return SR_ERR;
+       }
+
+       for (i = 0; (*array)[i]; ++i) {
+               if (!g_strcmp0(tmp, (*array)[i])) {
+                       *result = i;
+                       g_free(tmp);
+                       tmp = NULL;
+                       break;
+               }
+       }
+
+       if (tmp) {
+               g_free(tmp);
+               return SR_ERR;
+       }
+
+       return SR_OK;
+}
+
+static int analog_channel_state_get(struct sr_serial_dev_inst *serial,
+                                   struct scope_config *config,
+                                   struct scope_state *state)
+{
+       unsigned int i;
+       char command[MAX_COMMAND_SIZE];
+
+       for (i = 0; i < config->analog_channels; ++i) {
+               g_snprintf(command, sizeof(command),
+                          (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
+                          i + 1);
+
+               if (sr_scpi_get_bool(serial, command,
+                                    &state->analog_channels[i].state) != SR_OK)
+                       return SR_ERR;
+
+               g_snprintf(command, sizeof(command),
+                          (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_DIV],
+                          i + 1);
+
+               if (sr_scpi_get_float(serial, command,
+                                    &state->analog_channels[i].vdiv) != SR_OK)
+                       return SR_ERR;
+
+               g_snprintf(command, sizeof(command),
+                          (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
+                          i + 1);
+
+               if (sr_scpi_get_float(serial, command,
+                                    &state->analog_channels[i].vertical_offset) != SR_OK)
+                       return SR_ERR;
+
+               g_snprintf(command, sizeof(command),
+                          (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
+                          i + 1);
+
+               if (scope_state_get_array_option(serial, command, config->coupling_options,
+                                        &state->analog_channels[i].coupling) != SR_OK)
+                       return SR_ERR;
+       }
+
+       return SR_OK;
+}
+
+static int digital_channel_state_get(struct sr_serial_dev_inst *serial,
+                                    struct scope_config *config,
+                                    struct scope_state *state)
+{
+       unsigned int i;
+       char command[MAX_COMMAND_SIZE];
+
+       for (i = 0; i < config->digital_channels; ++i) {
+               g_snprintf(command, sizeof(command),
+                          (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
+                          i);
+
+               if (sr_scpi_get_bool(serial, command,
+                                    &state->digital_channels[i]) != SR_OK)
+                       return SR_ERR;
+       }
+
+       for (i = 0; i < config->digital_pods; ++i) {
+               g_snprintf(command, sizeof(command),
+                          (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
+                          i + 1);
+
+               if (sr_scpi_get_bool(serial, command,
+                                    &state->digital_pods[i]) != SR_OK)
+                       return SR_ERR;
+       }
+
+       return SR_OK;
+}
+
+SR_PRIV int scope_state_get(struct sr_dev_inst *sdi)
+{
+       struct dev_context *devc;
+       struct scope_state *state;
+       struct scope_config *config;
+
+       devc = sdi->priv;
+       config = devc->model_config;
+       state = devc->model_state;
+
+       if (analog_channel_state_get(sdi->conn, config, state) != SR_OK)
+               return SR_ERR;
+
+       if (digital_channel_state_get(sdi->conn, config, state) != SR_OK)
+               return SR_ERR;
+
+       /* TODO check if value is sensible */
+       if (sr_scpi_get_float(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
+                             &state->timebase) != SR_OK)
+               return SR_ERR;
+
+       if (sr_scpi_get_float(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
+                             &state->horiz_triggerpos) != SR_OK)
+               return SR_ERR;
+
+       if (scope_state_get_array_option(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
+                                config->trigger_sources, &state->trigger_source) != SR_OK)
+               return SR_ERR;
+
+       if (scope_state_get_array_option(sdi->conn, (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
+                                config->trigger_slopes, &state->trigger_slope) != SR_OK)
+               return SR_ERR;
+
+       scope_state_dump(config, state);
+
+       return SR_OK;
+}
+
+SR_PRIV struct scope_state *scope_state_new(struct scope_config *config)
+{
+       struct scope_state *state;
+
+       if (!(state = g_try_malloc0(sizeof(struct scope_state))))
+               return NULL;
+
+       if (!(state->analog_channels = g_try_malloc0_n(config->analog_channels,
+                                                           sizeof(struct analog_channel_state))))
+           goto fail;
+
+       if (!(state->digital_channels = g_try_malloc0_n(config->digital_channels,
+                                                            sizeof(gboolean))))
+           goto fail;
+
+       if (!(state->digital_pods = g_try_malloc0_n(config->digital_pods,
+                                                            sizeof(gboolean))))
+           goto fail;
+
+       return state;
+
+fail:
+       if (state->analog_channels)
+               g_free(state->analog_channels);
+       if (state->digital_channels)
+               g_free(state->digital_channels);
+       if (state->digital_pods)
+               g_free(state->digital_pods);
+       g_free(state);
+
+       return NULL;
+}
+
+SR_PRIV void scope_state_free(struct scope_state *state)
+{
+       g_free(state->analog_channels);
+       g_free(state->digital_channels);
+       g_free(state->digital_pods);
+       g_free(state);
+}
+
+SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
+{
+       char tmp[25];
+       int model_index;
+       unsigned int i;
+       unsigned int j;
+
+       struct sr_probe *probe;
+       struct dev_context *devc;
+
+       devc = sdi->priv;
+       model_index = -1;
+
+       /* Find the exact model */
+       for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
+               for (j = 0; scope_models[i].name[j]; j++) {
+                       if (!strcmp(sdi->model, scope_models[i].name[j])) {
+                               model_index = i;
+                               break;
+                       }
+               }
+               if (model_index != -1)
+                       break;
+       }
+
+       if (model_index == -1) {
+               sr_dbg("Unsupported HMO device");
+               return SR_ERR_NA;
+       }
+
+       if (!(devc->analog_groups = g_try_malloc0(sizeof(struct sr_probe_group) *
+                                                 scope_models[model_index].analog_channels)))
+                       return SR_ERR_MALLOC;
+
+       if (!(devc->digital_groups = g_try_malloc0(sizeof(struct sr_probe_group) *
+                                                  scope_models[model_index].digital_pods)))
+                       return SR_ERR_MALLOC;
+
+       /* Add analog channels */
+       for (i = 0; i < scope_models[model_index].analog_channels; i++) {
+               if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
+                                          (*scope_models[model_index].analog_names)[i])))
+                       return SR_ERR_MALLOC;
+               sdi->probes = g_slist_append(sdi->probes, probe);
+
+               devc->analog_groups[i].name = (char *) (*scope_models[model_index].analog_names)[i];
+               devc->analog_groups[i].probes = g_slist_append(NULL, probe);
+
+               sdi->probe_groups = g_slist_append(sdi->probe_groups,
+                                                  &devc->analog_groups[i]);
+       }
+
+       /* Add digital probe groups */
+       for (i = 0; i < scope_models[model_index].digital_pods; ++i) {
+               g_snprintf(tmp, 25, "POD%d", i);
+               devc->digital_groups[i].name = g_strdup(tmp);
+               sdi->probe_groups = g_slist_append(sdi->probe_groups,
+                                                  &devc->digital_groups[i < 8 ? 0 : 1]);
+       }
+
+       /* Add digital channels */
+       for (i = 0; i < scope_models[model_index].digital_channels; i++) {
+               if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
+                                          (*scope_models[model_index].digital_names)[i])))
+                       return SR_ERR_MALLOC;
+               sdi->probes = g_slist_append(sdi->probes, probe);
+
+               devc->digital_groups[i < 8 ? 0 : 1].probes = g_slist_append(devc->digital_groups[i < 8 ? 0 : 1].probes,
+                                                                           probe);
+       }
+
+       devc->model_config = &scope_models[model_index];
+       devc->frame_limit = 0;
+
+       if (!(devc->model_state = scope_state_new(devc->model_config)))
+               return SR_ERR_MALLOC;
+
+       return SR_OK;
+}
+
+SR_PRIV struct sr_dev_inst *hameg_probe_serial_device(const char *serial_device,
+                                                     const char *serial_options)
+{
+       struct sr_dev_inst *sdi;
+       struct dev_context *devc;
+       struct sr_scpi_hw_info *hw_info;
+       struct sr_serial_dev_inst *serial;
+
+       sdi = NULL;
+       devc = NULL;
+       serial = NULL;
+       hw_info = NULL;
+
+       if (!(serial = sr_serial_dev_inst_new(serial_device, serial_options)))
+               goto fail;
+
+       sr_info("Probing %s.", serial_device);
+       if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
+               goto fail;
+
+       if (sr_scpi_get_hw_id(serial, &hw_info) != SR_OK) {
+               sr_info("Couldn't get IDN response");
+               goto fail;
+       }
+
+       if (check_manufacturer(hw_info->manufacturer) != SR_OK)
+               goto fail;
+
+       if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
+                                   hw_info->manufacturer, hw_info->model,
+                                   hw_info->firmware_version))) {
+               goto fail;
+       }
+       sr_scpi_hw_info_free(hw_info);
+       hw_info = NULL;
+
+       if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
+               goto fail;
+
+       sdi->driver = di;
+       sdi->priv = devc;
+       sdi->inst_type = SR_INST_SERIAL;
+       sdi->conn = serial;
+
+       if (hmo_init_device(sdi) != SR_OK)
+               goto fail;
+
+       return sdi;
+
+fail:
+       if (hw_info)
+               sr_scpi_hw_info_free(hw_info);
+       if (serial)
+               sr_serial_dev_inst_free(serial);
+       if (sdi)
+               sr_dev_inst_free(sdi);
+       if (devc)
+               g_free(devc);
+
+       return NULL;
+}
+
 SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data)
 {
-       const struct sr_dev_inst *sdi;
+       struct sr_probe *probe;
+       struct sr_dev_inst *sdi;
        struct dev_context *devc;
+       struct sr_datafeed_packet packet;
 
        (void)fd;
 
@@ -33,7 +637,81 @@ SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data)
                return TRUE;
 
        if (revents == G_IO_IN) {
-               /* TODO */
+               probe = devc->current_probe->data;
+
+               switch (probe->type) {
+               case SR_PROBE_ANALOG:
+               {
+                       GArray *data;
+                       struct sr_datafeed_analog analog;
+
+                       if (sr_scpi_get_floatv(sdi->conn, NULL, &data) != SR_OK) {
+                               if (data)
+                                       g_array_free(data, TRUE);
+
+                               return TRUE;
+                       }
+
+                       packet.type = SR_DF_FRAME_BEGIN;
+                       sr_session_send(sdi, &packet);
+
+                       analog.probes = g_slist_append(NULL, probe);
+                       analog.num_samples = data->len;
+                       analog.data = (float *) data->data;
+                       analog.mq = SR_MQ_VOLTAGE;
+                       analog.unit = SR_UNIT_VOLT;
+                       analog.mqflags = 0;
+                       packet.type = SR_DF_ANALOG;
+                       packet.payload = &analog;
+                       sr_session_send(cb_data, &packet);
+                       g_slist_free(analog.probes);
+                       g_array_free(data, TRUE);
+               }
+               break;
+
+               case SR_PROBE_LOGIC:
+               {
+                       GArray *data;
+                       struct sr_datafeed_logic logic;
+
+                       if (sr_scpi_get_uint8v(sdi->conn, NULL, &data) != SR_OK) {
+                               if (data)
+                                       g_free(data);
+                               return TRUE;
+                       }
+
+                       packet.type = SR_DF_FRAME_BEGIN;
+                       sr_session_send(sdi, &packet);
+
+                       logic.length = data->len;
+                       logic.unitsize = 1;
+                       logic.data = data->data;
+                       packet.type = SR_DF_LOGIC;
+                       packet.payload = &logic;
+                       sr_session_send(cb_data, &packet);
+                       g_array_free(data, TRUE);
+               }
+               break;
+
+               default:
+                       sr_err("Invalid probe type");
+                       break;
+               }
+
+               packet.type = SR_DF_FRAME_END;
+               sr_session_send(sdi, &packet);
+
+               if (devc->current_probe->next) {
+                       devc->current_probe = devc->current_probe->next;
+                       hmo_request_data(sdi);
+               } else if (++devc->num_frames == devc->frame_limit) {
+                       packet.type = SR_DF_END;
+                       sr_session_send(sdi, &packet);
+                       sdi->driver->dev_acquisition_stop(sdi, cb_data);
+               } else {
+                       devc->current_probe = devc->enabled_probes;
+                       hmo_request_data(sdi);
+               }
        }
 
        return TRUE;
index 0c06a938a6a29db1fdc83638716fb9abafef8280..9f27f01b899e3b6c296c1d5d1bbffce147dabab1 100644 (file)
@@ -20,8 +20,9 @@
 #ifndef LIBSIGROK_HARDWARE_HAMEG_HMO_PROTOCOL_H
 #define LIBSIGROK_HARDWARE_HAMEG_HMO_PROTOCOL_H
 
-#include <stdint.h>
 #include <glib.h>
+#include <stdint.h>
+#include <string.h>
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 
 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
 #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
 
+#define MAX_INSTRUMENT_VERSIONS 10
+#define MAX_COMMAND_SIZE 31
+
+SR_PRIV struct sr_dev_driver hameg_hmo_driver_info;
+static struct sr_dev_driver *di = &hameg_hmo_driver_info;
+
+enum {
+       PG_INVALID = -1,
+       PG_NONE,
+       PG_ANALOG,
+       PG_DIGITAL,
+};
+
+struct scope_config {
+       const char *name[MAX_INSTRUMENT_VERSIONS];
+       const uint8_t analog_channels;
+       const uint8_t digital_channels;
+       const uint8_t digital_pods;
+
+       const char *(*analog_names)[];
+       const char *(*digital_names)[];
+
+       const int32_t (*hw_caps)[];
+       const uint8_t num_hwcaps;
+
+       const int32_t (*analog_hwcaps)[];
+       const uint8_t num_analog_hwcaps;
+
+       const char *(*coupling_options)[];
+       const uint8_t num_coupling_options;
+
+       const char *(*trigger_sources)[];
+       const uint8_t num_trigger_sources;
+
+       const char *(*trigger_slopes)[];
+
+       const uint64_t (*timebases)[][2];
+       const uint8_t num_timebases;
+
+       const uint64_t (*vdivs)[][2];
+       const uint8_t num_vdivs;
+
+       const uint8_t num_xdivs;
+       const uint8_t num_ydivs;
+
+       const char *(*scpi_dialect)[];
+};
+
+struct analog_channel_state {
+       int coupling;
+
+       float vdiv;
+       float vertical_offset;
+
+       gboolean state;
+};
+
+struct scope_state {
+       struct analog_channel_state *analog_channels;
+       gboolean *digital_channels;
+       gboolean *digital_pods;
+
+       float timebase;
+       float horiz_triggerpos;
+
+       int trigger_source;
+       int trigger_slope;
+};
+
 /** Private, per-device-instance driver context. */
 struct dev_context {
-       /* Model-specific information */
-
-       /* Acquisition settings */
+       void *model_config;
+       void *model_state;
 
-       /* Operational state */
+       struct sr_probe_group *analog_groups;
+       struct sr_probe_group *digital_groups;
 
-       /* Temporary state across callbacks */
+       GSList *enabled_probes;
+       GSList *current_probe;
+       uint64_t num_frames;
 
+       uint64_t frame_limit;
 };
 
+SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi);
+SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi);
 SR_PRIV int hameg_hmo_receive_data(int fd, int revents, void *cb_data);
+SR_PRIV struct sr_dev_inst *hameg_probe_serial_device(const char *serial_device,
+                                                     const char *serial_options);
+
+SR_PRIV struct scope_state *scope_state_new(struct scope_config *config);
+SR_PRIV void scope_state_free(struct scope_state *state);
+SR_PRIV int scope_state_get(struct sr_dev_inst *sdi);
 
 #endif