2 * This file is part of the libsigrok project.
4 * Copyright (C) 2021 Gerhard Sittig <gerhard.sittig@gmx.net>
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.
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.
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/>.
28 static const uint32_t scanopts[] = {
32 static const uint32_t drvopts[] = {
36 static const uint32_t devopts[] = {
37 SR_CONF_CONN | SR_CONF_GET,
38 SR_CONF_ENABLED | SR_CONF_SET, /* Enable/disable all relays at once. */
41 static const uint32_t devopts_cg[] = {
42 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
45 static struct sr_dev_driver dcttech_usbrelay_driver_info;
47 static struct sr_dev_inst *probe_device_common(const char *path,
48 const wchar_t *vendor, const wchar_t *product)
50 char nonws[16], *s, *endp;
51 unsigned long relay_count;
54 char serno[SERNO_LENGTH + 1];
56 uint8_t report[1 + REPORT_BYTECOUNT];
60 struct sr_dev_inst *sdi;
61 struct dev_context *devc;
62 struct channel_group_context *cgc;
64 struct sr_channel_group *cg;
67 * Get relay count from product string. Weak condition,
68 * accept any trailing number regardless of preceeding text.
70 snprintf(nonws, sizeof(nonws), "%ls", product);
73 while (s > nonws && isdigit((int)s[-1]))
75 ret = sr_atoul_base(s, &relay_count, &endp, 10);
76 if (ret != SR_OK || !endp || *endp)
80 sr_info("Relay count %lu from product string %s.", relay_count, nonws);
82 /* Open device, need to communicate to identify. */
83 hid = hid_open_path(path);
85 sr_err("Cannot open %s.", path);
89 /* Get an HID report. */
90 hid_set_nonblocking(hid, 0);
91 memset(&report, 0, sizeof(report));
92 report[0] = REPORT_NUMBER;
93 ret = hid_get_feature_report(hid, report, sizeof(report));
95 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
96 txt = sr_hexdump_new(report, sizeof(report));
97 sr_spew("Got report bytes: %s, rc %d.", txt->str, ret);
101 sr_err("Cannot read %s: %ls.", path, hid_error(NULL));
104 if (ret != sizeof(report)) {
105 sr_err("Unexpected HID report length %d from %s.", ret, path);
110 * Serial number must be all printable characters. Relay state
111 * is for information only, gets re-retrieved before configure
112 * API calls (get/set).
114 memset(serno, 0, sizeof(serno));
115 for (snr_pos = 0; snr_pos < SERNO_LENGTH; snr_pos++) {
116 c = report[1 + snr_pos];
118 if (c < 0x20 || c > 0x7e) {
119 sr_warn("Skipping %s, non-printable serial.", path);
123 curr_state = report[1 + STATE_INDEX];
124 sr_info("HID report data: serial number %s, relay state 0x%02x.",
127 /* Create a device instance. */
128 sdi = g_malloc0(sizeof(*sdi));
129 sdi->vendor = g_strdup_printf("%ls", vendor);
130 sdi->model = g_strdup_printf("%ls", product);
131 sdi->serial_num = g_strdup(serno);
132 sdi->connection_id = g_strdup(path);
133 sdi->driver = &dcttech_usbrelay_driver_info;
134 sdi->inst_type = SR_INST_USB;
136 /* Create channels (groups). */
137 devc = g_malloc0(sizeof(*devc));
139 devc->hid_path = g_strdup(path);
140 devc->relay_count = relay_count;
141 devc->relay_mask = (1U << relay_count) - 1;
142 for (idx = 0; idx < devc->relay_count; idx++) {
144 cg = g_malloc0(sizeof(*cg));
145 cg->name = g_strdup_printf("R%zu", nr);
146 cgc = g_malloc0(sizeof(*cgc));
149 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
155 static struct sr_dev_inst *probe_device_enum(struct hid_device_info *dev)
157 return probe_device_common(dev->path,
158 dev->manufacturer_string, dev->product_string);
161 static struct sr_dev_inst *probe_device_path(const char *path)
166 wchar_t vendor[32], product[32];
169 * TODO Accept different types of conn= specs? Either paths that
170 * hidapi(3) can open. Or bus.addr specs that we can check for
171 * during USB enumeration and derive a hidapi(3) compatible path
172 * from? Is some "unescaping" desirable for platforms which have
173 * colons in hidapi(3) paths that collide with how conn= specs
174 * are passed in sigrok? This would be the place to translate
175 * the 'path' to a canonical format.
178 dev = hid_open_path(path);
180 sr_err("Cannot open %s.", path);
185 ret = hid_get_manufacturer_string(dev, vendor, ARRAY_SIZE(vendor));
190 ret = hid_get_product_string(dev, product, ARRAY_SIZE(product));
193 if (!wcslen(product))
199 return probe_device_common(path, vendor, product);
202 static GSList *scan(struct sr_dev_driver *di, GSList *options)
206 struct drv_context *drvc;
207 struct hid_device_info *devs, *curdev;
210 struct sr_dev_inst *sdi;
212 /* Get optional conn= spec when provided. */
214 (void)sr_serial_extract_options(options, &conn, NULL);
220 drvc->instances = NULL;
223 * The firmware is V-USB based. The USB VID:PID identification
224 * is shared across several projects. Need to inspect the vendor
225 * and product _strings_ to actually identify the device. The
226 * USB serial number need not be present nor reliable. The HID
227 * report content will carry the board's serial number.
229 * When conn= was specified, then have HIDAPI open _this_ device
230 * and skip the enumeration. Which allows users to specify paths
231 * that need not match the enumeration's details.
234 sr_info("Checking HID path %s.", conn);
235 sdi = probe_device_path(conn);
237 sr_warn("Failed to communicate to %s.", conn);
239 devices = g_slist_append(devices, sdi);
241 devs = hid_enumerate(VENDOR_ID, PRODUCT_ID);
242 for (curdev = devs; curdev; curdev = curdev->next) {
245 if (!curdev->vendor_id || !curdev->product_id)
247 if (!curdev->manufacturer_string || !curdev->product_string)
249 if (!*curdev->manufacturer_string || !*curdev->product_string)
251 sr_dbg("Checking %04hx:%04hx, vendor %ls, product %ls.",
252 curdev->vendor_id, curdev->product_id,
253 curdev->manufacturer_string, curdev->product_string);
255 /* Check USB details retrieved by enumeration. */
256 ws = curdev->manufacturer_string;
257 if (!ws || !wcslen(ws))
259 snprintf(nonws, sizeof(nonws), "%ls", ws);
260 if (strcmp(nonws, VENDOR_STRING) != 0)
262 ws = curdev->product_string;
263 if (!ws || !wcslen(ws))
265 snprintf(nonws, sizeof(nonws), "%ls", ws);
266 if (!g_str_has_prefix(nonws, PRODUCT_STRING_PREFIX))
269 /* Identify device by communicating to it. */
270 sr_info("Checking HID path %s.", curdev->path);
271 sdi = probe_device_enum(curdev);
273 sr_warn("Failed to communicate to %s.", curdev->path);
276 devices = g_slist_append(devices, sdi);
278 hid_free_enumeration(devs);
283 static int dev_open(struct sr_dev_inst *sdi)
285 struct dev_context *devc;
290 hid_close(devc->hid_dev);
291 devc->hid_dev = NULL;
294 devc->hid_dev = hid_open_path(devc->hid_path);
298 (void)dcttech_usbrelay_update_state(sdi);
303 static int dev_close(struct sr_dev_inst *sdi)
305 struct dev_context *devc;
310 hid_close(devc->hid_dev);
311 devc->hid_dev = NULL;
317 static int config_get(uint32_t key, GVariant **data,
318 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
326 if (!sdi->connection_id)
328 *data = g_variant_new_string(sdi->connection_id);
336 case SR_CONF_ENABLED:
337 ret = dcttech_usbrelay_query_cg(sdi, cg, &on);
340 *data = g_variant_new_boolean(on);
347 static int config_set(uint32_t key, GVariant *data,
348 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
354 case SR_CONF_ENABLED:
355 /* Enable/disable all channels at the same time. */
356 on = g_variant_get_boolean(data);
357 return dcttech_usbrelay_switch_cg(sdi, cg, on);
363 case SR_CONF_ENABLED:
364 on = g_variant_get_boolean(data);
365 return dcttech_usbrelay_switch_cg(sdi, cg, on);
374 static int config_list(uint32_t key, GVariant **data,
375 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
380 case SR_CONF_SCAN_OPTIONS:
381 case SR_CONF_DEVICE_OPTIONS:
382 return STD_CONFIG_LIST(key, data, sdi, cg,
383 scanopts, drvopts, devopts);
390 case SR_CONF_DEVICE_OPTIONS:
391 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
398 static struct sr_dev_driver dcttech_usbrelay_driver_info = {
399 .name = "dcttech-usbrelay",
400 .longname = "dcttech usbrelay",
403 .cleanup = std_cleanup,
405 .dev_list = std_dev_list,
406 .dev_clear = std_dev_clear,
407 .config_get = config_get,
408 .config_set = config_set,
409 .config_list = config_list,
410 .dev_open = dev_open,
411 .dev_close = dev_close,
412 .dev_acquisition_start = std_dummy_dev_acquisition_start,
413 .dev_acquisition_stop = std_dummy_dev_acquisition_stop,
416 SR_REGISTER_DEV_DRIVER(dcttech_usbrelay_driver_info);