]> sigrok.org Git - libsigrok.git/blobdiff - hardware/uni-t-dmm/protocol.c
hardware: Call libusb_error_name() in all USB-related error messages
[libsigrok.git] / hardware / uni-t-dmm / protocol.c
index 086a254aab3c35f2eaf1635a1877e3818f96f103..a4b69403f878e5c625295a04ce08fd3d72d0d513 100644 (file)
  *  - ...
  */
 
-static void decode_packet(struct dev_context *devc, const uint8_t *buf)
+static void decode_packet(struct dev_context *devc, int dmm, const uint8_t *buf)
 {
        struct sr_datafeed_packet packet;
        struct sr_datafeed_analog analog;
+       struct fs9721_info info;
        float floatval;
        int ret;
 
        memset(&analog, 0, sizeof(struct sr_datafeed_analog));
 
        /* Parse the protocol packet. */
-       if ((ret = sr_dmm_parse_fs9922(buf, &floatval, &analog)) != SR_OK) {
+       if (dmm == UNI_T_UT61D)
+               ret = sr_dmm_parse_fs9922(buf, &floatval, &analog);
+       else if (dmm == VOLTCRAFT_VC820)
+               ret = sr_fs9721_parse(buf, &floatval, &analog, &info);
+       if (ret != SR_OK) {
                sr_err("Invalid DMM packet, ignoring.");
                return;
        }
@@ -101,7 +106,8 @@ static int hid_chip_init(struct dev_context *devc, uint16_t baudrate)
        if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) {
                ret = libusb_detach_kernel_driver(devc->usb->devhdl, 0);
                if (ret < 0) {
-                       sr_err("Failed to detach kernel driver: %d.", ret);
+                       sr_err("Failed to detach kernel driver: %s.",
+                              libusb_error_name(ret));
                        return SR_ERR;
                }
                sr_dbg("Successfully detached kernel driver.");
@@ -111,7 +117,8 @@ static int hid_chip_init(struct dev_context *devc, uint16_t baudrate)
 
        /* Claim interface 0. */
        if ((ret = libusb_claim_interface(devc->usb->devhdl, 0)) < 0) {
-               sr_err("Failed to claim interface 0: %d.", ret);
+               sr_err("Failed to claim interface 0: %s.",
+                      libusb_error_name(ret));
                return SR_ERR;
        }
        sr_dbg("Successfully claimed interface 0.");
@@ -140,7 +147,7 @@ static int hid_chip_init(struct dev_context *devc, uint16_t baudrate)
                1000 /* timeout (ms) */);
 
        if (ret < 0) {
-               sr_err("HID feature report error: %d.", ret);
+               sr_err("HID feature report error: %s.", libusb_error_name(ret));
                return SR_ERR;
        }
 
@@ -170,7 +177,7 @@ static void log_dmm_packet(const uint8_t *buf)
               buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
 }
 
-SR_PRIV int uni_t_dmm_receive_data(int fd, int revents, void *cb_data)
+static int uni_t_dmm_receive_data(int fd, int revents, int dmm, void *cb_data)
 {
        struct sr_dev_inst *sdi;
        struct dev_context *devc;
@@ -211,7 +218,7 @@ SR_PRIV int uni_t_dmm_receive_data(int fd, int revents, void *cb_data)
                1000 /* timeout (ms) */);
 
        if (ret < 0) {
-               sr_err("USB receive error: %d.", ret);
+               sr_err("USB receive error: %s.", libusb_error_name(ret));
                return FALSE;
        }
 
@@ -226,9 +233,15 @@ SR_PRIV int uni_t_dmm_receive_data(int fd, int revents, void *cb_data)
        if (buf[0] != 0xf0) {
                /* First time: Synchronize to the start of a packet. */
                if (!synced_on_first_packet) {
-                       /* Valid packets start with '+' or '-'. */
-                       if ((buf[1] != '+') && buf[1] != '-')
-                               return TRUE;
+                       if (dmm == UNI_T_UT61D) {
+                               /* Valid packets start with '+' or '-'. */
+                               if ((buf[1] != '+') && buf[1] != '-')
+                                       return TRUE;
+                       } else if (dmm == VOLTCRAFT_VC820) {
+                               /* Valid packets have 0x1 as high nibble. */
+                               if (!sr_fs9721_is_packet_start(buf[1]))
+                                       return TRUE;
+                       }
                        synced_on_first_packet = TRUE;
                        sr_spew("Successfully synchronized on first packet.");
                }
@@ -241,7 +254,11 @@ SR_PRIV int uni_t_dmm_receive_data(int fd, int revents, void *cb_data)
                if (data_byte_counter == NUM_DATA_BYTES) {
                        log_dmm_packet(pbuf);
                        data_byte_counter = 0;
-                       decode_packet(devc, pbuf);
+                       if (!sr_fs9721_packet_valid(pbuf)) {
+                               sr_err("Invalid packet.");
+                               return TRUE;
+                       }
+                       decode_packet(devc, dmm, pbuf);
                        memset(pbuf, 0x00, NUM_DATA_BYTES);
                }
        }
@@ -254,3 +271,13 @@ SR_PRIV int uni_t_dmm_receive_data(int fd, int revents, void *cb_data)
 
        return TRUE;
 }
+
+SR_PRIV int uni_t_ut61d_receive_data(int fd, int revents, void *cb_data)
+{
+       return uni_t_dmm_receive_data(fd, revents, UNI_T_UT61D, cb_data);
+}
+
+SR_PRIV int voltcraft_vc820_receive_data(int fd, int revents, void *cb_data)
+{
+       return uni_t_dmm_receive_data(fd, revents, VOLTCRAFT_VC820, cb_data);
+}