]> sigrok.org Git - libsigrok.git/blob - src/serial_hid_bu86x.c
scpi-pps: Support for the EEZ PSU series
[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 #define LOG_PREFIX "serial-bu86x"
44
45 #ifdef HAVE_SERIAL_COMM
46 #ifdef HAVE_LIBHIDAPI
47
48 /**
49  * @file
50  *
51  * Support serial-over-HID, specifically the Brymen BU-86X infrared adapter.
52  */
53
54 #define BU86X_MAX_BYTES_PER_REQUEST     8
55
56 static const struct vid_pid_item vid_pid_items_bu86x[] = {
57         { 0x0820, 0x0001, },
58         ALL_ZERO
59 };
60
61 static int bu86x_read_bytes(struct sr_serial_dev_inst *serial,
62         uint8_t *data, int space, unsigned int timeout)
63 {
64         int rc;
65
66         if (space > BU86X_MAX_BYTES_PER_REQUEST)
67                 space = BU86X_MAX_BYTES_PER_REQUEST;
68         rc = ser_hid_hidapi_get_data(serial, 0, data, space, timeout);
69         if (rc == SR_ERR_TIMEOUT)
70                 return 0;
71         if (rc < 0)
72                 return rc;
73         if (rc == 0)
74                 return 0;
75         return rc;
76 }
77
78 static int bu86x_write_bytes(struct sr_serial_dev_inst *serial,
79         const uint8_t *data, int size)
80 {
81         return ser_hid_hidapi_set_data(serial, 0, data, size, 0);
82 }
83
84 static struct ser_hid_chip_functions chip_bu86x = {
85         .chipname = "bu86x",
86         .chipdesc = "Brymen BU-86X",
87         .vid_pid_items = vid_pid_items_bu86x,
88         .max_bytes_per_request = BU86X_MAX_BYTES_PER_REQUEST,
89         /*
90          * The IR adapter's communication parameters are fixed and need
91          * not get configured. Just silently ignore the caller's spec.
92          */
93         .set_params = std_dummy_set_params,
94         .read_bytes = bu86x_read_bytes,
95         .write_bytes = bu86x_write_bytes,
96 };
97 SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x = &chip_bu86x;
98
99 #else
100
101 SR_PRIV struct ser_hid_chip_functions *ser_hid_chip_funcs_bu86x = NULL;
102
103 #endif
104 #endif