Skip to content

Commit 996331c

Browse files
committed
dcttech-usbrelay: rework scan (enumeration) and probe (identification)
Move all of the sdi creation into the probe routine which communicates to the relay card. Extend diagnostics in that code path. Which leaves pure USB enumeration in the scan routine.
1 parent e333a40 commit 996331c

File tree

1 file changed

+26
-19
lines changed
  • src/hardware/dcttech-usbrelay

1 file changed

+26
-19
lines changed

src/hardware/dcttech-usbrelay/api.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ static const uint32_t devopts_cg[] = {
4343

4444
static struct sr_dev_driver dcttech_usbrelay_driver_info;
4545

46-
static struct sr_dev_inst *probe_device(const char *path, size_t relay_count)
46+
static struct sr_dev_inst *probe_device(struct hid_device_info *dev,
47+
size_t relay_count)
4748
{
4849
hid_device *hid;
4950
int ret;
@@ -60,9 +61,11 @@ static struct sr_dev_inst *probe_device(const char *path, size_t relay_count)
6061
struct sr_channel_group *cg;
6162

6263
/* Open device, need to communicate to identify. */
63-
hid = hid_open_path(path);
64-
if (!hid)
64+
hid = hid_open_path(dev->path);
65+
if (!hid) {
66+
sr_err("Cannot open %s: %ls.", dev->path, hid_error(NULL));
6567
return NULL;
68+
}
6669

6770
/* Get an HID report. */
6871
hid_set_nonblocking(hid, 0);
@@ -75,8 +78,14 @@ static struct sr_dev_inst *probe_device(const char *path, size_t relay_count)
7578
sr_spew("raw report, rc %d, bytes %s", ret, txt->str);
7679
sr_hexdump_free(txt);
7780
}
78-
if (ret != sizeof(report))
81+
if (ret < 0) {
82+
sr_err("Cannot read %s: %ls.", dev->path, hid_error(NULL));
83+
return NULL;
84+
}
85+
if (ret != sizeof(report)) {
86+
sr_err("Unexpected HID report length: %s.", dev->path);
7987
return NULL;
88+
}
8089

8190
/*
8291
* Serial number must be all printable characters. Relay state
@@ -87,22 +96,27 @@ static struct sr_dev_inst *probe_device(const char *path, size_t relay_count)
8796
for (snr_pos = 0; snr_pos < SERNO_LENGTH; snr_pos++) {
8897
c = report[1 + snr_pos];
8998
serno[snr_pos] = c;
90-
if (c < 0x20 || c > 0x7e)
99+
if (c < 0x20 || c > 0x7e) {
100+
sr_dbg("non-printable serno");
91101
return NULL;
102+
}
92103
}
93104
curr_state = report[1 + STATE_INDEX];
94105
sr_spew("report data, serno[%s], relays 0x%02x.", serno, curr_state);
95106

96-
/*
97-
* Create a device instance, create channels (groups). The
98-
* caller fills in vendor, model, conn from USB enum details.
99-
*/
107+
/* Create a device instance. */
100108
sdi = g_malloc0(sizeof(*sdi));
109+
sdi->vendor = g_strdup_printf("%ls", dev->manufacturer_string);
110+
sdi->model = g_strdup_printf("%ls", dev->product_string);
101111
sdi->serial_num = g_strdup(serno);
102-
sdi->connection_id = g_strdup(path);
112+
sdi->connection_id = g_strdup(dev->path);
113+
sdi->driver = &dcttech_usbrelay_driver_info;
114+
sdi->inst_type = SR_INST_USB;
115+
116+
/* Create channels (groups). */
103117
devc = g_malloc0(sizeof(*devc));
104118
sdi->priv = devc;
105-
devc->hid_path = g_strdup(path);
119+
devc->hid_path = g_strdup(dev->path);
106120
devc->relay_count = relay_count;
107121
devc->relay_mask = (1U << relay_count) - 1;
108122
for (idx = 0; idx < devc->relay_count; idx++) {
@@ -195,18 +209,11 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
195209
curdev->path, relay_count);
196210

197211
/* Identify device by communicating to it. */
198-
sdi = probe_device(curdev->path, relay_count);
212+
sdi = probe_device(curdev, relay_count);
199213
if (!sdi) {
200214
sr_warn("Failed to communicate to %s.", curdev->path);
201215
continue;
202216
}
203-
204-
/* Amend driver instance from USB enumeration details. */
205-
sdi->vendor = g_strdup_printf("%ls", curdev->manufacturer_string);
206-
sdi->model = g_strdup_printf("%ls", curdev->product_string);
207-
sdi->driver = &dcttech_usbrelay_driver_info;
208-
sdi->inst_type = SR_INST_USB;
209-
210217
devices = g_slist_append(devices, sdi);
211218
}
212219
hid_free_enumeration(devs);

0 commit comments

Comments
 (0)