From: Gerhard Sittig Date: Sun, 9 Jun 2019 06:25:54 +0000 (+0200) Subject: serial-hid: introduce support for Brymen BU-86X IR adapters X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=a10284cd180fb482fe4974f54b1c0d98c5f29680;p=libsigrok.git serial-hid: introduce support for Brymen BU-86X IR adapters The Brymen BU-86X infrared adapters are sold with BM869s meters. Raw streams of data bytes get communicated by means of HID reports with report number 0 and up to 8 data bytes each. Communication parameters are fixed and need no configuration. --- diff --git a/Makefile.am b/Makefile.am index 96542f62..cf0136f9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -125,6 +125,7 @@ libsigrok_la_SOURCES += \ src/serial.c \ src/serial_bt.c \ src/serial_hid.c \ + src/serial_hid_bu86x.c \ src/serial_hid_ch9325.c \ src/serial_hid_cp2110.c \ src/serial_libsp.c \ diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index 0dfacbce..a6c113a5 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -756,6 +756,7 @@ struct sr_serial_dev_inst { #ifdef HAVE_LIBHIDAPI enum ser_hid_chip_t { SER_HID_CHIP_UNKNOWN, /**!< place holder */ + SER_HID_CHIP_BTC_BU86X, /**!< Brymen BU86x */ SER_HID_CHIP_SIL_CP2110, /**!< SiLabs CP2110 */ SER_HID_CHIP_WCH_CH9325, /**!< WCH CH9325 */ SER_HID_CHIP_LAST, /**!< sentinel */ @@ -1247,6 +1248,7 @@ struct ser_hid_chip_functions { int (*flush)(struct sr_serial_dev_inst *serial); int (*drain)(struct sr_serial_dev_inst *serial); }; +extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x; extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_ch9325; extern SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_cp2110; SR_PRIV const char *ser_hid_chip_find_name_vid_pid(uint16_t vid, uint16_t pid); diff --git a/src/serial_hid.c b/src/serial_hid.c index b4b82f16..1aca73e6 100644 --- a/src/serial_hid.c +++ b/src/serial_hid.c @@ -550,6 +550,7 @@ SR_PRIV int ser_hid_hidapi_set_data(struct sr_serial_dev_inst *serial, static struct ser_hid_chip_functions **chips[SER_HID_CHIP_LAST] = { [SER_HID_CHIP_UNKNOWN] = NULL, + [SER_HID_CHIP_BTC_BU86X] = &ser_hid_chip_funcs_bu86x, [SER_HID_CHIP_SIL_CP2110] = &ser_hid_chip_funcs_cp2110, [SER_HID_CHIP_WCH_CH9325] = &ser_hid_chip_funcs_ch9325, }; diff --git a/src/serial_hid_bu86x.c b/src/serial_hid_bu86x.c new file mode 100644 index 00000000..3697e5b6 --- /dev/null +++ b/src/serial_hid_bu86x.c @@ -0,0 +1,122 @@ +/* + * This file is part of the libsigrok project. + * + * Copyright (C) 2019 Gerhard Sittig + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* + * This implements serial communication primitives for the Brymen BU-86X + * infrared adapter for handheld multimeters. The vendor's protocol spec + * http://brymen.com/product-html/images/DownloadList/ProtocolList/BM860-BM860s_List/BM860-BM860s-500000-count-dual-display-DMMs-protocol.pdf + * suggests that HID reports get communicated, but only report number 0 + * is involved, which carries a mere byte stream in 8 byte chunks each. + * The frame format and bitrate are fixed, and need not get configured. + * + * The meter's packet consists of 24 bytes which get received in three + * HID reports. Packet reception gets initiated by sending a short HID + * report to the meter. It's uncertain which parts of this exchange are + * specific to the adapter and to the meter. Using the IR adapter with + * other devices, or using the meter with other cables/adapters may need + * a little more adjustment with respect to layering. + */ + +#include +#include +#include +#include "libsigrok-internal.h" +#include "serial_hid.h" +#include + +/** @cond PRIVATE */ +#define LOG_PREFIX "serial-bu86x" +/** @endcond */ + +#ifdef HAVE_SERIAL_COMM +#ifdef HAVE_LIBHIDAPI + +/** + * @file + * + * Support serial-over-HID, specifically the Brymen BU-86X infrared adapter. + */ + +#define BU86X_MAX_BYTES_PER_REQUEST 8 + +static const struct vid_pid_item vid_pid_items_bu86x[] = { + { 0x0820, 0x0001, }, + VID_PID_TERM, +}; + +static int bu86x_set_params(struct sr_serial_dev_inst *serial, + int baudrate, int bits, int parity, int stopbits, + int flowcontrol, int rts, int dtr) +{ + /* + * The IR adapter's communication parameters are fixed and need + * not get configured. Just silently ignore the caller's spec. + */ + (void)serial; + (void)baudrate; + (void)bits; + (void)parity; + (void)stopbits; + (void)flowcontrol; + (void)rts; + (void)dtr; + + return SR_OK; +} + +static int bu86x_read_bytes(struct sr_serial_dev_inst *serial, + uint8_t *data, int space, unsigned int timeout) +{ + int rc; + + if (space > BU86X_MAX_BYTES_PER_REQUEST) + space = BU86X_MAX_BYTES_PER_REQUEST; + rc = ser_hid_hidapi_get_data(serial, 0, data, space, timeout); + if (rc == SR_ERR_TIMEOUT) + return 0; + if (rc < 0) + return rc; + if (rc == 0) + return 0; + return rc; +} + +static int bu86x_write_bytes(struct sr_serial_dev_inst *serial, + const uint8_t *data, int size) +{ + return ser_hid_hidapi_set_data(serial, 0, data, size, 0); +} + +static struct ser_hid_chip_functions chip_bu86x = { + .chipname = "bu86x", + .chipdesc = "Brymen BU-86X", + .vid_pid_items = vid_pid_items_bu86x, + .max_bytes_per_request = BU86X_MAX_BYTES_PER_REQUEST, + .set_params = bu86x_set_params, + .read_bytes = bu86x_read_bytes, + .write_bytes = bu86x_write_bytes, +}; +SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x = &chip_bu86x; + +#else + +SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x = NULL; + +#endif +#endif