]> sigrok.org Git - libsigrok.git/blobdiff - hardware/lascar-el-usb/api.c
s/sr_config_make/sr_config_new/.
[libsigrok.git] / hardware / lascar-el-usb / api.c
index f0b69aee438f499408126a1281163c85fd5baaf0..901af0c6c105a3ebaf23473b5abf43047f1aabec 100644 (file)
@@ -36,10 +36,15 @@ static const int hwopts[] = {
 static const int hwcaps[] = {
        SR_CONF_THERMOMETER,
        SR_CONF_HYGROMETER,
+       SR_CONF_DATALOG,
        SR_CONF_LIMIT_SAMPLES,
        0
 };
 
+/* Terrible hack */
+static int BOOL_TRUE = TRUE;
+static int BOOL_FALSE = FALSE;
+
 /* Properly close and free all devices. */
 static int clear_instances(void)
 {
@@ -70,17 +75,7 @@ static int clear_instances(void)
 
 static int hw_init(struct sr_context *sr_ctx)
 {
-       struct drv_context *drvc;
-
-       if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
-               sr_err("Driver context malloc failed.");
-               return SR_ERR_MALLOC;
-       }
-
-       drvc->sr_ctx = sr_ctx;
-       di->priv = drvc;
-
-       return SR_OK;
+       return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
 }
 
 static GSList *hw_scan(GSList *options)
@@ -95,10 +90,7 @@ static GSList *hw_scan(GSList *options)
 
        (void)options;
 
-       if (!(drvc = di->priv)) {
-               sr_err("Driver was not initialized.");
-               return NULL;
-       }
+       drvc = di->priv;
 
        /* USB scan is always authoritative. */
        clear_instances();
@@ -140,14 +132,7 @@ static GSList *hw_scan(GSList *options)
 
 static GSList *hw_dev_list(void)
 {
-       struct drv_context *drvc;
-
-       if (!(drvc = di->priv)) {
-               sr_err("Driver was not initialized.");
-               return NULL;
-       }
-
-       return drvc->instances;
+       return ((struct drv_context *)(di->priv))->instances;
 }
 
 static int hw_dev_open(struct sr_dev_inst *sdi)
@@ -191,7 +176,6 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
        libusb_release_interface(devc->usb->devhdl, LASCAR_INTERFACE);
        libusb_close(devc->usb->devhdl);
        devc->usb->devhdl = NULL;
-       g_free(devc->config);
        sdi->status = SR_ST_INACTIVE;
 
 
@@ -213,17 +197,22 @@ static int hw_cleanup(void)
        return SR_OK;
 }
 
-static int hw_info_get(int info_id, const void **data,
-               const struct sr_dev_inst *sdi)
+static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
 {
-       (void)sdi;
+       struct dev_context *devc;
+       int ret;
 
-       switch (info_id) {
-       case SR_DI_HWOPTS:
-               *data = hwopts;
+       devc = sdi->priv;
+       switch (id) {
+       case SR_CONF_DATALOG:
+               if (!sdi)
+                       return SR_ERR_ARG;
+               if ((ret = lascar_is_logging(sdi)) == -1)
+                       return SR_ERR;
+               *data = ret ? &BOOL_TRUE : &BOOL_FALSE;
                break;
-       case SR_DI_HWCAPS:
-               *data = hwcaps;
+       case SR_CONF_LIMIT_SAMPLES:
+               *data = &devc->limit_samples;
                break;
        default:
                return SR_ERR_ARG;
@@ -232,8 +221,7 @@ static int hw_info_get(int info_id, const void **data,
        return SR_OK;
 }
 
-static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
-               const void *value)
+static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        int ret;
@@ -249,20 +237,48 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
 
        devc = sdi->priv;
        ret = SR_OK;
-       switch (hwcap) {
+       switch (id) {
+       case SR_CONF_DATALOG:
+               if (*(int *)value) {
+                       /* Start logging. */
+                       ret = lascar_start_logging(sdi);
+               } else {
+                       /* Stop logging. */
+                       ret = lascar_stop_logging(sdi);
+               }
+               break;
        case SR_CONF_LIMIT_SAMPLES:
                devc->limit_samples = *(const uint64_t *)value;
                sr_dbg("Setting sample limit to %" PRIu64 ".",
                       devc->limit_samples);
                break;
        default:
-               sr_err("Unknown hardware capability: %d.", hwcap);
+               sr_err("Unknown hardware capability: %d.", id);
                ret = SR_ERR_ARG;
        }
 
        return ret;
 }
 
+static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
+{
+
+       (void)sdi;
+
+       switch (key) {
+       case SR_CONF_SCAN_OPTIONS:
+               *data = hwopts;
+               break;
+       case SR_CONF_DEVICE_OPTIONS:
+               *data = hwcaps;
+               break;
+       default:
+               return SR_ERR_ARG;
+       }
+
+       return SR_OK;
+}
+
 static void mark_xfer(struct libusb_transfer *xfer)
 {
 
@@ -295,10 +311,10 @@ static float binary32_le_to_float(unsigned char *buf)
 static int lascar_proc_config(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
-       int ret;
+       int dummy, ret;
 
        devc = sdi->priv;
-       if (!(devc->config = lascar_get_config(devc->usb->devhdl)))
+       if (lascar_get_config(devc->usb->devhdl, devc->config, &dummy) != SR_OK)
                return SR_ERR;
 
        ret = SR_OK;
@@ -334,12 +350,14 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                void *cb_data)
 {
        struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
+       struct sr_datafeed_meta meta;
+       struct sr_config *src;
        struct dev_context *devc;
-       struct drv_context *drvc = di->priv;
+       struct drv_context *drvc;
        struct libusb_transfer *xfer_in, *xfer_out;
        const struct libusb_pollfd **pfd;
        struct timeval tv;
+       uint64_t interval;
        int ret, i;
        unsigned char cmd[3], resp[4], *buf;
 
@@ -348,6 +366,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR;
        }
 
+       drvc = di->priv;
        devc = sdi->priv;
        devc->cb_data = cb_data;
 
@@ -357,11 +376,15 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        sr_dbg("Starting log retrieval.");
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
+
+       interval = (devc->config[0x1c] | (devc->config[0x1d] << 8)) * 1000;
+       packet.type = SR_DF_META;
+       packet.payload = &meta;
+       src = sr_config_new(SR_CONF_SAMPLE_INTERVAL, (const void *)&interval);
+       meta.config = g_slist_append(NULL, src);
        sr_session_send(devc->cb_data, &packet);
+       g_free(src);
 
        if (devc->logged_samples == 0) {
                /* This ensures the frontend knows the session is done. */
@@ -480,10 +503,11 @@ SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info = {
        .scan = hw_scan,
        .dev_list = hw_dev_list,
        .dev_clear = clear_instances,
+       .config_get = config_get,
+       .config_set = config_set,
+       .config_list = config_list,
        .dev_open = hw_dev_open,
        .dev_close = hw_dev_close,
-       .info_get = hw_info_get,
-       .dev_config_set = hw_dev_config_set,
        .dev_acquisition_start = hw_dev_acquisition_start,
        .dev_acquisition_stop = hw_dev_acquisition_stop,
        .priv = NULL,