From: Gerhard Sittig Date: Sun, 19 Mar 2023 15:02:45 +0000 (+0100) Subject: rdtech-tc: request keyword differs between CDC and BLE transports X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=37b689533bc9655600c23773d06307a4ab63bb3b;p=libsigrok.git rdtech-tc: request keyword differs between CDC and BLE transports The TC66C firmware implements different keywords and different request terminators across the USB CDC and the Bluetooth LE transports. Move the string literal to the probe routine, construct the request text at the time when the device is probed, then re-use it for the remaining session. Check the receive buffer size and maximum reponse length for consistency at compile time. Declarations are spread across source files. --- diff --git a/src/hardware/rdtech-tc/protocol.c b/src/hardware/rdtech-tc/protocol.c index ef1cd34b..9320b84f 100644 --- a/src/hardware/rdtech-tc/protocol.c +++ b/src/hardware/rdtech-tc/protocol.c @@ -33,8 +33,6 @@ #define WRITE_TO_MS 1 #define POLL_PERIOD_MS 100 -static const char *poll_cmd = "getva"; - /* * Response data (raw sample data) consists of three adjacent chunks * of 64 bytes each. These chunks start with their magic string, and @@ -55,6 +53,9 @@ static const char *poll_cmd = "getva"; #define OFF_PAC2 (1 * PAC_LEN) #define OFF_PAC3 (2 * PAC_LEN) #define TC_POLL_LEN (3 * PAC_LEN) +#if TC_POLL_LEN > RDTECH_TC_RSPBUFSIZE +# error "response length exceeds receive buffer space" +#endif #define OFF_MODEL 4 #define LEN_MODEL 4 @@ -126,16 +127,28 @@ static int process_poll_pkt(struct dev_context *devc, uint8_t *dst) SR_PRIV int rdtech_tc_probe(struct sr_serial_dev_inst *serial, struct dev_context *devc) { + static const char *poll_cmd_cdc = "getva"; + static const char *poll_cmd_ble = "bgetva\r\n"; + int len; uint8_t poll_pkt[TC_POLL_LEN]; + /* Construct the request text. Which differs across transports. */ + devc->is_bluetooth = ser_name_is_bt(serial); + snprintf(devc->req_text, sizeof(devc->req_text), "%s", + devc->is_bluetooth ? poll_cmd_ble : poll_cmd_cdc); + sr_dbg("is bluetooth %d -> poll request '%s'.", + devc->is_bluetooth, devc->req_text); + + /* Transmit the request. */ len = serial_write_blocking(serial, - poll_cmd, strlen(poll_cmd), WRITE_TO_MS); + devc->req_text, strlen(devc->req_text), WRITE_TO_MS); if (len < 0) { sr_err("Failed to send probe request."); return SR_ERR; } + /* Receive a response. */ len = serial_read_blocking(serial, devc->buf, TC_POLL_LEN, PROBE_TO_MS); if (len != TC_POLL_LEN) { sr_err("Failed to read probe response."); @@ -185,7 +198,7 @@ SR_PRIV int rdtech_tc_poll(const struct sr_dev_inst *sdi, gboolean force) */ serial = sdi->conn; len = serial_write_blocking(serial, - poll_cmd, strlen(poll_cmd), WRITE_TO_MS); + devc->req_text, strlen(devc->req_text), WRITE_TO_MS); if (len < 0) { sr_err("Unable to send poll request."); return SR_ERR; diff --git a/src/hardware/rdtech-tc/protocol.h b/src/hardware/rdtech-tc/protocol.h index fb025906..789b18c2 100644 --- a/src/hardware/rdtech-tc/protocol.h +++ b/src/hardware/rdtech-tc/protocol.h @@ -25,7 +25,16 @@ #define LOG_PREFIX "rdtech-tc" -#define RDTECH_TC_BUFSIZE 256 +/* + * Keep request and response buffers of sufficient size. The maximum + * request text currently involved is "bgetva\r\n" which translates + * to 9 bytes. The poll response (a measurement, the largest amount + * of data that is currently received) is 192 bytes in length. Add + * some slack for alignment, and for in-flight messages or adjacent + * data during synchronization to the data stream. + */ +#define RDTECH_TC_MAXREQLEN 12 +#define RDTECH_TC_RSPBUFSIZE 256 struct rdtech_dev_info { char *model_name; @@ -43,12 +52,14 @@ struct rdtech_tc_channel_desc { }; struct dev_context { + gboolean is_bluetooth; + char req_text[RDTECH_TC_MAXREQLEN]; struct rdtech_dev_info dev_info; const struct rdtech_tc_channel_desc *channels; size_t channel_count; struct feed_queue_analog **feeds; struct sr_sw_limits limits; - uint8_t buf[RDTECH_TC_BUFSIZE]; + uint8_t buf[RDTECH_TC_RSPBUFSIZE]; size_t rdlen; int64_t cmd_sent_at; };