]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/lascar-el-usb/protocol.c
drivers: Eliminate some unnecessary vendor/model #defines.
[libsigrok.git] / src / hardware / lascar-el-usb / protocol.c
index 49ed649b2707f463260db29b23077902e250043e..bda67b33eacfe21a851683722ccc80693e9a8177 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <stdlib.h>
 #include <sys/time.h>
 #include <string.h>
 #include <math.h>
 #include <glib.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 #include "protocol.h"
 
-extern struct sr_dev_driver lascar_el_usb_driver_info;
-static struct sr_dev_driver *di = &lascar_el_usb_driver_info;
+extern SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info;
+struct sr_dev_driver *di = &lascar_el_usb_driver_info;
 
 static const struct elusb_profile profiles[] = {
        { 1, "EL-USB-1", LOG_UNSUPPORTED },
@@ -51,10 +52,9 @@ static const struct elusb_profile profiles[] = {
        { 19, "EL-USB-1-LCD", LOG_UNSUPPORTED },
        { 20, "EL-OEM-3", LOG_UNSUPPORTED },
        { 21, "EL-USB-1-LCD", LOG_UNSUPPORTED },
-       { 0, NULL, 0 }
+       ALL_ZERO
 };
 
-
 static libusb_device_handle *lascar_open(struct libusb_device *dev)
 {
        libusb_device_handle *dev_hdl;
@@ -67,7 +67,7 @@ static libusb_device_handle *lascar_open(struct libusb_device *dev)
        }
 
        /* Some of these fail, but it needs doing -- some sort of mode
-        * setup for the SILabs F32x. */
+        * setup for the SiLabs F32x. */
        libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR,
                        0x00, 0xffff, 0x00, NULL, 0, 50);
        libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR,
@@ -78,7 +78,7 @@ static libusb_device_handle *lascar_open(struct libusb_device *dev)
        return dev_hdl;
 }
 
-static void mark_xfer(struct libusb_transfer *xfer)
+static void LIBUSB_CALL mark_xfer(struct libusb_transfer *xfer)
 {
 
        xfer->user_data = GINT_TO_POINTER(1);
@@ -97,7 +97,7 @@ SR_PRIV int lascar_get_config(libusb_device_handle *dev_hdl,
 
        sr_spew("Reading config block.");
 
-       drvc = di->priv;
+       drvc = di->context;
        *configlen = 0;
 
        if (!(xfer_in = libusb_alloc_transfer(0)) ||
@@ -112,7 +112,7 @@ SR_PRIV int lascar_get_config(libusb_device_handle *dev_hdl,
        /* Keep a read request waiting in the wings, ready to pounce
         * the moment the device sends something. */
        libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN,
-                       buf, 256, mark_xfer, 0, 10000);
+                       buf, 256, mark_xfer, 0, BULK_XFER_TIMEOUT);
        if (libusb_submit_transfer(xfer_in) != 0)
                goto cleanup;
 
@@ -133,7 +133,7 @@ SR_PRIV int lascar_get_config(libusb_device_handle *dev_hdl,
                        start = 0;
                        break;
                }
-               g_usleep(5000);
+               g_usleep(SLEEP_US_LONG);
                libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
        }
        if (!start) {
@@ -166,7 +166,7 @@ SR_PRIV int lascar_get_config(libusb_device_handle *dev_hdl,
                        start = 0;
                        break;
                }
-               g_usleep(5000);
+               g_usleep(SLEEP_US_LONG);
                libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
        }
        if (!start) {
@@ -183,16 +183,16 @@ SR_PRIV int lascar_get_config(libusb_device_handle *dev_hdl,
        *configlen = buflen;
 
 cleanup:
-       if (!xfer_in->user_data || !xfer_in->user_data) {
+       if (!xfer_in->user_data || !xfer_out->user_data) {
                if (!xfer_in->user_data)
                        libusb_cancel_transfer(xfer_in);
                if (!xfer_out->user_data)
                        libusb_cancel_transfer(xfer_out);
                start = g_get_monotonic_time();
                while (!xfer_in->user_data || !xfer_out->user_data) {
-                       if (g_get_monotonic_time() - start > 10000)
+                       if (g_get_monotonic_time() - start > EVENTS_TIMEOUT)
                                break;
-                       g_usleep(1000);
+                       g_usleep(SLEEP_US_SHORT);
                        libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
                }
        }
@@ -214,7 +214,7 @@ static int lascar_save_config(libusb_device_handle *dev_hdl,
 
        sr_spew("Writing config block.");
 
-       drvc = di->priv;
+       drvc = di->context;
 
        if (!(xfer_in = libusb_alloc_transfer(0)) ||
                        !(xfer_out = libusb_alloc_transfer(0)))
@@ -229,7 +229,7 @@ static int lascar_save_config(libusb_device_handle *dev_hdl,
        /* Keep a read request waiting in the wings, ready to pounce
         * the moment the device sends something. */
        libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN,
-                       buf, 256, mark_xfer, 0, 10000);
+                       buf, 256, mark_xfer, 0, BULK_XFER_TIMEOUT);
        if (libusb_submit_transfer(xfer_in) != 0) {
                ret = SR_ERR;
                goto cleanup;
@@ -248,7 +248,7 @@ static int lascar_save_config(libusb_device_handle *dev_hdl,
        tv.tv_sec = 0;
        tv.tv_usec = 0;
        while (!xfer_out->user_data) {
-               g_usleep(5000);
+               g_usleep(SLEEP_US_LONG);
                libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
        }
 
@@ -259,7 +259,7 @@ static int lascar_save_config(libusb_device_handle *dev_hdl,
                goto cleanup;
        }
        while (!xfer_in->user_data || !xfer_out->user_data) {
-               g_usleep(5000);
+               g_usleep(SLEEP_US_LONG);
                libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
        }
 
@@ -269,16 +269,16 @@ static int lascar_save_config(libusb_device_handle *dev_hdl,
        }
 
 cleanup:
-       if (!xfer_in->user_data || !xfer_in->user_data) {
+       if (!xfer_in->user_data || !xfer_out->user_data) {
                if (!xfer_in->user_data)
                        libusb_cancel_transfer(xfer_in);
                if (!xfer_out->user_data)
                        libusb_cancel_transfer(xfer_out);
                start = g_get_monotonic_time();
                while (!xfer_in->user_data || !xfer_out->user_data) {
-                       if (g_get_monotonic_time() - start > 10000)
+                       if (g_get_monotonic_time() - start > EVENTS_TIMEOUT)
                                break;
-                       g_usleep(1000);
+                       g_usleep(SLEEP_US_SHORT);
                        libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
                }
        }
@@ -293,7 +293,6 @@ static struct sr_dev_inst *lascar_identify(unsigned char *config)
        struct dev_context *devc;
        const struct elusb_profile *profile;
        struct sr_dev_inst *sdi;
-       struct sr_channel *ch;
        int modelid, i;
        char firmware[5];
 
@@ -325,31 +324,21 @@ static struct sr_dev_inst *lascar_identify(unsigned char *config)
 
                sdi = g_malloc0(sizeof(struct sr_dev_inst));
                sdi->status = SR_ST_INACTIVE;
-               sdi->vendor = g_strdup(LASCAR_VENDOR);
+               sdi->vendor = g_strdup("Lascar");
                sdi->model = g_strdup(profile->modelname);
                sdi->version = g_strdup(firmware);
-               sdi->driver = di;
 
                if (profile->logformat == LOG_TEMP_RH) {
                        /* Model this as two channels: temperature and humidity. */
-                       if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "Temp")))
-                               return NULL;
-                       sdi->channels = g_slist_append(NULL, ch);
-                       if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "Hum")))
-                               return NULL;
-                       sdi->channels = g_slist_append(sdi->channels, ch);
+                       sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Temp");
+                       sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Hum");
                } else if (profile->logformat == LOG_CO) {
-                       if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "CO")))
-                               return NULL;
-                       sdi->channels = g_slist_append(NULL, ch);
+                       sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CO");
                } else {
-                       if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1")))
-                               return NULL;
-                       sdi->channels = g_slist_append(NULL, ch);
+                       sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
                }
 
-               if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
-                       return NULL;
+               devc = g_malloc0(sizeof(struct dev_context));
                sdi->priv = devc;
                devc->profile = profile;
        }
@@ -362,21 +351,19 @@ SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address)
        struct drv_context *drvc;
        struct sr_dev_inst *sdi;
        struct libusb_device **devlist;
-       struct libusb_device_descriptor des;
        libusb_device_handle *dev_hdl;
-       int dummy, ret, i;
+       int dummy, i;
+       int ret;
        unsigned char config[MAX_CONFIGBLOCK_SIZE];
 
-       drvc = di->priv;
+       drvc = di->context;
        sdi = NULL;
 
-       libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
-       for (i = 0; devlist[i]; i++) {
-               if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
-                       sr_err("Failed to get device descriptor: %d.", ret);
-                       continue;
-               }
+       ret = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
+       if (ret < 0)
+               return NULL;
 
+       for (i = 0; devlist[i]; i++) {
                if (libusb_get_bus_number(devlist[i]) != bus ||
                                libusb_get_device_address(devlist[i]) != address)
                        continue;
@@ -390,6 +377,7 @@ SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address)
                libusb_close(dev_hdl);
                sdi = lascar_identify(config);
        }
+       libusb_free_device_list(devlist, 1);
 
        return sdi;
 }
@@ -400,6 +388,9 @@ static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf,
        struct dev_context *devc;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
+       struct sr_analog_encoding encoding;
+       struct sr_analog_meaning meaning;
+       struct sr_analog_spec spec;
        struct sr_channel *ch;
        float *temp, *rh;
        uint16_t s;
@@ -407,6 +398,9 @@ static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf,
 
        devc = sdi->priv;
 
+       /* Note: digits/spec_digits will be overridden later. */
+       sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
+
        samples = buflen / devc->sample_size;
        samples_left = devc->logged_samples - devc->rcvd_samples;
        if (samples_left < samples)
@@ -415,15 +409,15 @@ static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf,
        case LOG_TEMP_RH:
                packet.type = SR_DF_ANALOG;
                packet.payload = &analog;
-               analog.mqflags = 0;
+               analog.meaning->mqflags = 0;
                if (!(temp = g_try_malloc(sizeof(float) * samples)))
                        break;
                if (!(rh = g_try_malloc(sizeof(float) * samples)))
                        break;
                for (i = 0, j = 0; i < samples; i++) {
-                       /* Both Celcius and Fahrenheit stored at base -40. */
+                       /* Both Celsius and Fahrenheit stored at base -40. */
                        if (devc->temp_unit == 0)
-                               /* Celcius is stored in half-degree increments. */
+                               /* Celsius is stored in half-degree increments. */
                                temp[j] = buf[i * 2] / 2 - 40;
                        else
                                temp[j] = buf[i * 2] - 40;
@@ -439,23 +433,32 @@ static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf,
 
                ch = sdi->channels->data;
                if (ch->enabled) {
-                       analog.channels = g_slist_append(NULL, ch);
-                       analog.mq = SR_MQ_TEMPERATURE;
-                       if (devc->temp_unit == 1)
-                               analog.unit = SR_UNIT_FAHRENHEIT;
-                       else
-                               analog.unit = SR_UNIT_CELSIUS;
-                       analog.data = temp;
-                       sr_session_send(devc->cb_data, &packet);
+                       analog.meaning->channels = g_slist_append(NULL, ch);
+                       analog.meaning->mq = SR_MQ_TEMPERATURE;
+                       if (devc->temp_unit == 1) {
+                               analog.meaning->unit = SR_UNIT_FAHRENHEIT;
+                               analog.encoding->digits = 0;
+                               analog.spec->spec_digits = 0;
+                       } else {
+                               analog.meaning->unit = SR_UNIT_CELSIUS;
+                               analog.encoding->digits = 1;
+                               analog.spec->spec_digits = 1;
+                       }
+                       analog.data = (void *)temp;
+                       sr_session_send(sdi, &packet);
+                       g_slist_free(analog.meaning->channels);
                }
 
                ch = sdi->channels->next->data;
                if (ch->enabled) {
-                       analog.channels = g_slist_append(NULL, ch);
-                       analog.mq = SR_MQ_RELATIVE_HUMIDITY;
-                       analog.unit = SR_UNIT_PERCENTAGE;
-                       analog.data = rh;
-                       sr_session_send(devc->cb_data, &packet);
+                       analog.meaning->channels = g_slist_append(NULL, ch);
+                       analog.meaning->mq = SR_MQ_RELATIVE_HUMIDITY;
+                       analog.meaning->unit = SR_UNIT_PERCENTAGE;
+                       analog.encoding->digits = 1;
+                       analog.spec->spec_digits = 1;
+                       analog.data = (void *)rh;
+                       sr_session_send(sdi, &packet);
+                       g_slist_free(analog.meaning->channels);
                }
 
                g_free(temp);
@@ -464,20 +467,20 @@ static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf,
        case LOG_CO:
                packet.type = SR_DF_ANALOG;
                packet.payload = &analog;
-               analog.channels = sdi->channels;
+               analog.meaning->channels = sdi->channels;
                analog.num_samples = samples;
-               analog.mq = SR_MQ_CARBON_MONOXIDE;
-               analog.unit = SR_UNIT_CONCENTRATION;
-               analog.mqflags = 0;
+               analog.meaning->mq = SR_MQ_CARBON_MONOXIDE;
+               analog.meaning->unit = SR_UNIT_CONCENTRATION;
+               analog.meaning->mqflags = 0;
                if (!(analog.data = g_try_malloc(sizeof(float) * samples)))
                        break;
                for (i = 0; i < samples; i++) {
                        s = (buf[i * 2] << 8) | buf[i * 2 + 1];
-                       analog.data[i] = (s * devc->co_high + devc->co_low) / 1000000;
-                       if (analog.data[i] < 0.0)
-                               analog.data[i] = 0.0;
+                       ((float *)analog.data)[i] = (s * devc->co_high + devc->co_low) / (1000 * 1000);
+                       if (((float *)analog.data)[i] < 0.0)
+                               ((float *)analog.data)[i] = 0.0;
                }
-               sr_session_send(devc->cb_data, &packet);
+               sr_session_send(sdi, &packet);
                g_free(analog.data);
                break;
        default:
@@ -490,8 +493,7 @@ static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf,
 
 SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data)
 {
-       struct drv_context *drvc = di->priv;
-       struct sr_datafeed_packet packet;
+       struct drv_context *drvc = di->context;
        struct sr_dev_inst *sdi;
        struct timeval tv;
 
@@ -502,9 +504,7 @@ SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data)
 
        if (sdi->status == SR_ST_STOPPING) {
                usb_source_remove(sdi->session, drvc->sr_ctx);
-
-               packet.type = SR_DF_END;
-               sr_session_send(cb_data, &packet);
+               std_session_send_df_end(sdi);
        }
 
        memset(&tv, 0, sizeof(struct timeval));
@@ -514,7 +514,7 @@ SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data)
        return TRUE;
 }
 
-SR_PRIV void lascar_el_usb_receive_transfer(struct libusb_transfer *transfer)
+SR_PRIV void LIBUSB_CALL lascar_el_usb_receive_transfer(struct libusb_transfer *transfer)
 {
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
@@ -528,7 +528,7 @@ SR_PRIV void lascar_el_usb_receive_transfer(struct libusb_transfer *transfer)
        switch (transfer->status) {
        case LIBUSB_TRANSFER_NO_DEVICE:
                /* USB device was unplugged. */
-               dev_acquisition_stop(sdi, sdi);
+               sr_dev_acquisition_stop(sdi);
                return;
        case LIBUSB_TRANSFER_COMPLETED:
        case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
@@ -547,7 +547,7 @@ SR_PRIV void lascar_el_usb_receive_transfer(struct libusb_transfer *transfer)
                                devc->rcvd_bytes, devc->log_size,
                                devc->rcvd_samples, devc->logged_samples);
                if (devc->rcvd_bytes >= devc->log_size)
-                       dev_acquisition_stop(sdi, sdi);
+                       sr_dev_acquisition_stop(sdi);
        }
 
        if (sdi->status == SR_ST_ACTIVE) {
@@ -557,7 +557,7 @@ SR_PRIV void lascar_el_usb_receive_transfer(struct libusb_transfer *transfer)
                               libusb_error_name(ret));
                        g_free(transfer->buffer);
                        libusb_free_transfer(transfer);
-                       dev_acquisition_stop(sdi, sdi);
+                       sr_dev_acquisition_stop(sdi);
                }
        } else {
                /* This was the last transfer we're going to receive, so