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/>.
27 static const uint32_t scanopts[] = {
31 static const uint32_t drvopts[] = {
35 static const uint32_t devopts[] = {
36 SR_CONF_CONN | SR_CONF_GET,
37 SR_CONF_ENABLED | SR_CONF_SET, /* Enable/disable all relays at once. */
40 static const uint32_t devopts_cg[] = {
41 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
44 static struct sr_dev_driver dcttech_usbrelay_driver_info;
46 static struct sr_dev_inst *probe_device(const char *path, size_t relay_count)
50 char serno[SERNO_LENGTH + 1];
52 uint8_t report[1 + REPORT_BYTECOUNT];
56 struct sr_dev_inst *sdi;
57 struct dev_context *devc;
58 struct channel_group_context *cgc;
60 struct sr_channel_group *cg;
62 /* Open device, need to communicate to identify. */
63 hid = hid_open_path(path);
67 /* Get an HID report. */
68 hid_set_nonblocking(hid, 0);
69 memset(&report, 0, sizeof(report));
70 report[0] = REPORT_NUMBER;
71 ret = hid_get_feature_report(hid, report, sizeof(report));
73 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
74 txt = sr_hexdump_new(report, sizeof(report));
75 sr_spew("raw report, rc %d, bytes %s", ret, txt->str);
78 if (ret != sizeof(report))
82 * Serial number must be all printable characters. Relay state
83 * is for information only, gets re-retrieved before configure
84 * API calls (get/set).
86 memset(serno, 0, sizeof(serno));
87 for (snr_pos = 0; snr_pos < SERNO_LENGTH; snr_pos++) {
88 c = report[1 + snr_pos];
90 if (c < 0x20 || c > 0x7e)
93 curr_state = report[1 + STATE_INDEX];
94 sr_spew("report data, serno[%s], relays 0x%02x.", serno, curr_state);
97 * Create a device instance, create channels (groups). The
98 * caller fills in vendor, model, conn from USB enum details.
100 sdi = g_malloc0(sizeof(*sdi));
101 devc = g_malloc0(sizeof(*devc));
103 devc->hid_path = g_strdup(path);
104 devc->relay_count = relay_count;
105 devc->relay_mask = (1U << relay_count) - 1;
106 for (idx = 0; idx < devc->relay_count; idx++) {
108 cg = g_malloc0(sizeof(*cg));
109 cg->name = g_strdup_printf("R%zu", nr);
110 cgc = g_malloc0(sizeof(*cgc));
113 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
119 static GSList *scan(struct sr_dev_driver *di, GSList *options)
123 struct drv_context *drvc;
124 struct hid_device_info *devs, *curdev;
129 unsigned long relay_count;
130 struct sr_dev_inst *sdi;
132 /* Get optional conn= spec when provided. */
134 (void)sr_serial_extract_options(options, &conn, NULL);
138 * TODO Accept different types of conn= specs? Either paths that
139 * hidapi(3) can open. Or bus.addr specs that we can check for
140 * during USB enumeration. Derive want_path, want_bus, want_addr
141 * here from the optional conn= spec.
146 drvc->instances = NULL;
149 * The firmware is V-USB based. The USB VID:PID identification
150 * is shared across several projects. Need to inspect the vendor
151 * and product _strings_ to actually identify the device.
153 * The USB serial number need not be present nor reliable. The
154 * HID report contains a five character string which may serve
155 * as an identification for boards (is said to differ between
156 * boards). The last byte encodes the current relays state.
158 devs = hid_enumerate(VENDOR_ID, PRODUCT_ID);
159 for (curdev = devs; curdev; curdev = curdev->next) {
160 if (!curdev->vendor_id || !curdev->product_id)
162 if (!curdev->manufacturer_string || !curdev->product_string)
164 if (!*curdev->manufacturer_string || !*curdev->product_string)
166 if (conn && strcmp(curdev->path, conn) != 0) {
167 sr_dbg("skipping %s, conn= mismatch", curdev->path);
170 sr_dbg("checking %04hx:%04hx, vendor %ls, product %ls.",
171 curdev->vendor_id, curdev->product_id,
172 curdev->manufacturer_string, curdev->product_string);
174 /* Check USB details retrieved by enumeration. */
175 ws = curdev->manufacturer_string;
176 if (!ws || !wcslen(ws))
178 snprintf(nonws, sizeof(nonws), "%ls", ws);
179 if (strcmp(nonws, VENDOR_STRING) != 0)
181 ws = curdev->product_string;
182 if (!ws || !wcslen(ws))
184 snprintf(nonws, sizeof(nonws), "%ls", ws);
186 if (!g_str_has_prefix(s, PRODUCT_STRING_PREFIX))
188 s += strlen(PRODUCT_STRING_PREFIX);
189 ret = sr_atoul_base(s, &relay_count, &endp, 10);
190 if (ret != SR_OK || !endp || *endp)
192 sr_info("Found: HID path %s, relay count %lu.",
193 curdev->path, relay_count);
195 /* Identify device by communicating to it. */
196 sdi = probe_device(curdev->path, relay_count);
198 sr_warn("Failed to communicate to %s.", curdev->path);
202 /* Amend driver instance from USB enumeration details. */
203 sdi->vendor = g_strdup_printf("%ls", curdev->manufacturer_string);
204 sdi->model = g_strdup_printf("%ls", curdev->product_string);
205 sdi->conn = g_strdup(curdev->path);
206 sdi->driver = &dcttech_usbrelay_driver_info;
207 sdi->inst_type = SR_INST_USB;
209 devices = g_slist_append(devices, sdi);
211 hid_free_enumeration(devs);
216 static int dev_open(struct sr_dev_inst *sdi)
218 struct dev_context *devc;
223 hid_close(devc->hid_dev);
224 devc->hid_dev = NULL;
227 devc->hid_dev = hid_open_path(devc->hid_path);
231 (void)dcttech_usbrelay_update_state(sdi);
236 static int dev_close(struct sr_dev_inst *sdi)
238 struct dev_context *devc;
243 hid_close(devc->hid_dev);
244 devc->hid_dev = NULL;
250 static int config_get(uint32_t key, GVariant **data,
251 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
261 *data = g_variant_new_string(sdi->conn);
269 case SR_CONF_ENABLED:
270 ret = dcttech_usbrelay_query_cg(sdi, cg, &on);
273 *data = g_variant_new_boolean(on);
280 static int config_set(uint32_t key, GVariant *data,
281 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
287 case SR_CONF_ENABLED:
288 /* Enable/disable all channels at the same time. */
289 on = g_variant_get_boolean(data);
290 return dcttech_usbrelay_switch_cg(sdi, cg, on);
296 case SR_CONF_ENABLED:
297 on = g_variant_get_boolean(data);
298 return dcttech_usbrelay_switch_cg(sdi, cg, on);
307 static int config_list(uint32_t key, GVariant **data,
308 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
313 case SR_CONF_SCAN_OPTIONS:
314 case SR_CONF_DEVICE_OPTIONS:
315 return STD_CONFIG_LIST(key, data, sdi, cg,
316 scanopts, drvopts, devopts);
323 case SR_CONF_DEVICE_OPTIONS:
324 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
331 static struct sr_dev_driver dcttech_usbrelay_driver_info = {
332 .name = "dcttech-usbrelay",
333 .longname = "dcttech usbrelay",
336 .cleanup = std_cleanup,
338 .dev_list = std_dev_list,
339 .dev_clear = std_dev_clear,
340 .config_get = config_get,
341 .config_set = config_set,
342 .config_list = config_list,
343 .dev_open = dev_open,
344 .dev_close = dev_close,
345 .dev_acquisition_start = std_dummy_dev_acquisition_start,
346 .dev_acquisition_stop = std_dummy_dev_acquisition_stop,
349 SR_REGISTER_DEV_DRIVER(dcttech_usbrelay_driver_info);