]> sigrok.org Git - libsigrok.git/blob - src/serial_hid_bu86x.c
3697e5b6cea1c5b9035804060b6f6f724f3e5297
[libsigrok.git] / src / serial_hid_bu86x.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2019 Gerhard Sittig <gerhard.sittig@gmx.net>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /*
21  * This implements serial communication primitives for the Brymen BU-86X
22  * infrared adapter for handheld multimeters. The vendor's protocol spec
23  * http://brymen.com/product-html/images/DownloadList/ProtocolList/BM860-BM860s_List/BM860-BM860s-500000-count-dual-display-DMMs-protocol.pdf
24  * suggests that HID reports get communicated, but only report number 0
25  * is involved, which carries a mere byte stream in 8 byte chunks each.
26  * The frame format and bitrate are fixed, and need not get configured.
27  *
28  * The meter's packet consists of 24 bytes which get received in three
29  * HID reports. Packet reception gets initiated by sending a short HID
30  * report to the meter. It's uncertain which parts of this exchange are
31  * specific to the adapter and to the meter. Using the IR adapter with
32  * other devices, or using the meter with other cables/adapters may need
33  * a little more adjustment with respect to layering.
34  */
35
36 #include <config.h>
37 #include <glib.h>
38 #include <libsigrok/libsigrok.h>
39 #include "libsigrok-internal.h"
40 #include "serial_hid.h"
41 #include <string.h>
42
43 /** @cond PRIVATE */
44 #define LOG_PREFIX "serial-bu86x"
45 /** @endcond */
46
47 #ifdef HAVE_SERIAL_COMM
48 #ifdef HAVE_LIBHIDAPI
49
50 /**
51  * @file
52  *
53  * Support serial-over-HID, specifically the Brymen BU-86X infrared adapter.
54  */
55
56 #define BU86X_MAX_BYTES_PER_REQUEST     8
57
58 static const struct vid_pid_item vid_pid_items_bu86x[] = {
59         { 0x0820, 0x0001, },
60         VID_PID_TERM,
61 };
62
63 static int bu86x_set_params(struct sr_serial_dev_inst *serial,
64         int baudrate, int bits, int parity, int stopbits,
65         int flowcontrol, int rts, int dtr)
66 {
67         /*
68          * The IR adapter's communication parameters are fixed and need
69          * not get configured. Just silently ignore the caller's spec.
70          */
71         (void)serial;
72         (void)baudrate;
73         (void)bits;
74         (void)parity;
75         (void)stopbits;
76         (void)flowcontrol;
77         (void)rts;
78         (void)dtr;
79
80         return SR_OK;
81 }
82
83 static int bu86x_read_bytes(struct sr_serial_dev_inst *serial,
84         uint8_t *data, int space, unsigned int timeout)
85 {
86         int rc;
87
88         if (space > BU86X_MAX_BYTES_PER_REQUEST)
89                 space = BU86X_MAX_BYTES_PER_REQUEST;
90         rc = ser_hid_hidapi_get_data(serial, 0, data, space, timeout);
91         if (rc == SR_ERR_TIMEOUT)
92                 return 0;
93         if (rc < 0)
94                 return rc;
95         if (rc == 0)
96                 return 0;
97         return rc;
98 }
99
100 static int bu86x_write_bytes(struct sr_serial_dev_inst *serial,
101         const uint8_t *data, int size)
102 {
103         return ser_hid_hidapi_set_data(serial, 0, data, size, 0);
104 }
105
106 static struct ser_hid_chip_functions chip_bu86x = {
107         .chipname = "bu86x",
108         .chipdesc = "Brymen BU-86X",
109         .vid_pid_items = vid_pid_items_bu86x,
110         .max_bytes_per_request = BU86X_MAX_BYTES_PER_REQUEST,
111         .set_params = bu86x_set_params,
112         .read_bytes = bu86x_read_bytes,
113         .write_bytes = bu86x_write_bytes,
114 };
115 SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x = &chip_bu86x;
116
117 #else
118
119 SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x = NULL;
120
121 #endif
122 #endif