2 * This file is part of the libsigrok project.
4 * Copyright (C) 2023 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/>.
21 * Communicate to the Devantech ETH008 relay card via TCP and Ethernet.
23 * See http://www.robot-electronics.co.uk/files/eth008b.pdf for device
24 * capabilities and a protocol discussion.
25 * See https://github.com/devantech/devantech_eth_python for Python
26 * source code which is maintained by the vendor.
28 * The device provides several means of communication: HTTP requests
29 * (as well as an interactive web form). Raw TCP communication with
30 * binary requests and responses. Text requests and responses over
31 * TCP sockets. Some of these depend on the firmware version. Version
32 * checks before command transmission is essentially non-existent in
33 * this sigrok driver implementation. Binary transmission is preferred
34 * because it is assumed that this existed in all firmware versions.
35 * The firmware interestingly accepts concurrent network connections
36 * (up to five of them, all share the same password). Which means that
37 * the peripheral's state can change even while we control it.
39 * It's assumed that WLAN models differ from Ethernet devices in terms
40 * of their hardware, but TCP communication should not bother about the
41 * underlying physics, and WLAN cards can re-use model IDs and firmware
42 * implementations. Given sigrok's abstraction of the serial transport
43 * those cards could also be attached by means of COM ports.
45 * TCP communication seems to rely on network fragmentation and assumes
46 * that software stacks provide all of a request in a single receive
47 * call on the firmware side. Which works for local communication, but
48 * could become an issue when long distances and tunnels are involved.
49 * This sigrok driver also assumes complete reception within a single
50 * receive call. The short length of binary transmission helps here
51 * (the largest payloads has a length of three bytes).
53 * The lack of length specs as well as termination in the protocol
54 * (both binary as well as text variants over TCP sockets) results in
55 * the inability to synchronize to the firmware when connecting and
56 * after hiccups in an established connection. The fixed length of
57 * requests and responses for binary payloads helps a little bit,
58 * assuming that TCP connect is used to recover. The overhead of
59 * HTTP requests and responses is considered undesirable for this
60 * sigrok driver implementation. [This also means that a transport
61 * which lacks the concept of network frames cannot send passwords.]
62 * The binary transport appears to lack HELLO or NOP requests that
63 * could be used to synchronize. Firmware just would not respond to
64 * unsupported commands. Maybe a repeated sequence of identity reads
65 * combined with a read timeout could help synchronize, but only if
66 * the response is known because the model was identified before.
68 * The sigrok driver source code was phrased with the addition of more
69 * models in mind. Only few code paths require adjustment when similar
70 * variants of requests or responses are involved in the communication
71 * to relay cards that support between two and twenty channels. Chances
72 * are good, existing firmware is compatible across firmware versions,
73 * and even across hardware revisions (model upgrades). Firmware just
74 * happens to not respond to unknown requests.
77 * - Add support for other models. Currently exclusively supports the
78 * ETH008-B model which was used during implementation of the driver.
79 * - Add support for password protection?
80 * - See command 0x79 to "login" (beware of the differing return value
81 * compared to other commands), command 0x7a to check if passwords
82 * are involved and whether the login needs refreshing, command 0x7b
83 * for immediate "logout" in contrast to expiration.
84 * - Alternatively consider switching to the "text protocol" in that
85 * use case, which can send an optional password in every request
86 * that controls relays (command 0x3a).
87 * - How to specify the password in applications and how to pass them
88 * to this driver is yet another issue that needs consideration.
97 #define READ_TIMEOUT_MS 20
100 CMD_GET_MODULE_INFO = 0x10,
101 CMD_DIGITAL_ACTIVE = 0x20,
102 CMD_DIGITAL_INACTIVE = 0x21,
103 CMD_DIGITAL_SET_OUTPUTS = 0x23,
104 CMD_DIGITAL_GET_OUTPUTS = 0x24,
105 CMD_ASCII_TEXT_COMMAND = 0x3a,
106 CMD_GET_SERIAL_NUMBER = 0x77,
107 CMD_GET_SUPPLY_VOLTS = 0x78,
108 CMD_PASSWORD_ENTRY = 0x79,
109 CMD_GET_UNLOCK_TIME = 0x7a,
110 CMD_IMMEDIATE_LOGOUT = 0x7b,
114 * Transmit a request to the relay card. Checks that all bytes get sent,
115 * short writes are considered fatal.
117 static int send_request(struct sr_serial_dev_inst *ser,
118 const uint8_t *data, size_t dlen)
123 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
124 GString *txt = sr_hexdump_new(data, dlen);
125 sr_spew("TX --> %s.", txt->str);
126 sr_hexdump_free(txt);
128 ret = serial_write_blocking(ser, data, dlen, 0);
131 written = (size_t)ret;
138 * Receive a response from the relay card. Assumes fixed size payload,
139 * considers short reads fatal.
141 static int recv_response(struct sr_serial_dev_inst *ser,
142 uint8_t *data, size_t dlen)
147 ret = serial_read_blocking(ser, data, dlen, READ_TIMEOUT_MS);
151 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
152 GString *txt = sr_hexdump_new(data, got);
153 sr_spew("<-- RX %s.", txt->str);
154 sr_hexdump_free(txt);
161 /* Send a request then receive a response. Convenience routine. */
162 static int send_then_recv(struct sr_serial_dev_inst *serial,
163 const uint8_t *tx_data, size_t tx_length,
164 uint8_t *rx_data, size_t rx_length)
168 if (tx_data && tx_length) {
169 ret = send_request(serial, tx_data, tx_length);
174 if (rx_data && rx_length) {
175 ret = recv_response(serial, rx_data, rx_length);
183 /* Identify the relay card, gather version information details. */
184 SR_PRIV int devantech_eth008_get_model(struct sr_serial_dev_inst *serial,
185 uint8_t *model_code, uint8_t *hw_version, uint8_t *fw_version)
187 uint8_t req[1], *wrptr;
189 const uint8_t *rdptr;
200 write_u8_inc(&wrptr, CMD_GET_MODULE_INFO);
201 ret = send_then_recv(serial, req, wrptr - req, rsp, sizeof(rsp));
206 v8 = read_u8_inc(&rdptr);
209 v8 = read_u8_inc(&rdptr);
212 v8 = read_u8_inc(&rdptr);
219 /* Get the relay card's serial number (its MAC address). */
220 SR_PRIV int devantech_eth008_get_serno(struct sr_serial_dev_inst *serial,
221 char *text_buffer, size_t text_length)
223 uint8_t req[1], *wrptr;
225 const uint8_t *rdptr, *endptr;
229 if (text_buffer && !text_length)
232 memset(text_buffer, 0, text_length);
235 write_u8_inc(&wrptr, CMD_GET_SERIAL_NUMBER);
236 ret = send_then_recv(serial, req, wrptr - req, rsp, sizeof(rsp));
241 endptr = rsp + sizeof(rsp);
242 while (rdptr < endptr && text_buffer && text_length >= 3) {
243 b = read_u8_inc(&rdptr);
244 written = snprintf(text_buffer, text_length, "%02x", b);
245 text_buffer += written;
246 text_length -= written;
252 /* Update an internal cache from the relay card's current state. */
253 SR_PRIV int devantech_eth008_cache_state(const struct sr_dev_inst *sdi)
255 struct sr_serial_dev_inst *serial;
256 struct dev_context *devc;
258 uint8_t req[1], *wrptr;
260 const uint8_t *rdptr;
271 rx_size = devc->model->width_do;
272 if (rx_size > sizeof(rsp))
276 write_u8_inc(&wrptr, CMD_DIGITAL_GET_OUTPUTS);
277 ret = send_then_recv(serial, req, wrptr - req, rsp, rx_size);
284 have = read_u8_inc(&rdptr);
287 have = read_u16le_inc(&rdptr);
290 have = read_u24le_inc(&rdptr);
295 have &= devc->mask_do;
296 devc->curr_do = have;
301 /* Query the state of an individual relay channel. */
302 SR_PRIV int devantech_eth008_query_do(const struct sr_dev_inst *sdi,
303 const struct sr_channel_group *cg, gboolean *on)
305 struct dev_context *devc;
306 struct channel_group_context *cgc;
314 /* Unconditionally update the internal cache. */
315 ret = devantech_eth008_cache_state(sdi);
320 * Only reject unexpected requeusts after the update. Get the
321 * individual channel's state from the cache of all channels.
328 if (cgc->index >= devc->model->ch_count_do)
330 have = devc->curr_do;
334 *on = have ? TRUE : FALSE;
340 * Manipulate the state of an individual relay channel (when cg is given).
341 * Or set/clear all channels at the same time (when cg is NULL).
343 SR_PRIV int devantech_eth008_setup_do(const struct sr_dev_inst *sdi,
344 const struct sr_channel_group *cg, gboolean on)
346 struct sr_serial_dev_inst *serial;
347 struct dev_context *devc;
349 struct channel_group_context *cgc;
352 uint8_t req[4], *wrptr, cmd;
354 const uint8_t *rdptr;
363 cgc = cg ? cg->priv : NULL;
364 if (cgc && cgc->index >= devc->model->ch_count_do)
367 width_do = devc->model->width_do;
368 if (1 + width_do > sizeof(req))
373 /* Manipulate an individual channel. */
374 cmd = on ? CMD_DIGITAL_ACTIVE : CMD_DIGITAL_INACTIVE;
375 number = cgc->number;
376 write_u8_inc(&wrptr, cmd);
377 write_u8_inc(&wrptr, number & 0xff);
378 write_u8_inc(&wrptr, 0); /* Just set/clear, no pulse. */
380 /* Manipulate all channels at the same time. */
381 reg = on ? devc->mask_do : 0;
382 write_u8_inc(&wrptr, CMD_DIGITAL_SET_OUTPUTS);
385 write_u8_inc(&wrptr, reg & 0xff);
388 write_u16le_inc(&wrptr, reg & 0xffff);
391 write_u24le_inc(&wrptr, reg & 0xffffff);
397 ret = send_then_recv(serial, req, wrptr - req, rsp, sizeof(rsp));
402 v8 = read_u8_inc(&rdptr);
409 SR_PRIV int devantech_eth008_query_supply(const struct sr_dev_inst *sdi,
410 const struct sr_channel_group *cg, uint16_t *millivolts)
412 struct sr_serial_dev_inst *serial;
413 uint8_t req[1], *wrptr;
415 const uint8_t *rdptr;
426 write_u8_inc(&wrptr, CMD_GET_SUPPLY_VOLTS);
427 ret = send_then_recv(serial, req, wrptr - req, rsp, sizeof(rsp));
432 /* Gets a byte for voltage in units of 0.1V. Scale up to mV. */
433 have = read_u8_inc(&rdptr);