From: Gerhard Sittig Date: Wed, 15 Mar 2023 16:03:02 +0000 (+0100) Subject: rdtech-um: style nits, move assignment out of declaration blocks X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=fda34e5af6e7a11744035c1c8df2f2b2f95cf1d8 rdtech-um: style nits, move assignment out of declaration blocks Move initial assigment, subsequent updates, and checks of resulting variable content closer to each other. Don't hide code in declaration blocks. Eliminate redundant assignments that don't take effect, yet can hide programming errors. Increases awareness during maintenance. --- diff --git a/src/hardware/rdtech-um/api.c b/src/hardware/rdtech-um/api.c index 31c77071..a6c5f697 100644 --- a/src/hardware/rdtech-um/api.c +++ b/src/hardware/rdtech-um/api.c @@ -50,10 +50,10 @@ static GSList *rdtech_um_scan(struct sr_dev_driver *di, const char *conn, const char *serialcomm) { struct sr_serial_dev_inst *serial; - const struct rdtech_um_profile *p = NULL; - GSList *devices = NULL; - struct dev_context *devc = NULL; - struct sr_dev_inst *sdi = NULL; + const struct rdtech_um_profile *p; + GSList *devices; + struct dev_context *devc; + struct sr_dev_inst *sdi; size_t ch_idx; const char *name; @@ -84,7 +84,7 @@ static GSList *rdtech_um_scan(struct sr_dev_driver *di, for (ch_idx = 0; (name = p->channels[ch_idx].name); ch_idx++) sr_channel_new(sdi, ch_idx, SR_CHANNEL_ANALOG, TRUE, name); - devices = g_slist_append(devices, sdi); + devices = g_slist_append(NULL, sdi); serial_close(serial); if (!devices) sr_serial_dev_inst_free(serial); @@ -101,11 +101,14 @@ err_out: static GSList *scan(struct sr_dev_driver *di, GSList *options) { + const char *conn; + const char *serialcomm; + GSList *l; struct sr_config *src; - const char *conn = NULL; - const char *serialcomm = RDTECH_UM_SERIALCOMM; - for (GSList *l = options; l; l = l->next) { + conn = NULL; + serialcomm = RDTECH_UM_SERIALCOMM; + for (l = options; l; l = l->next) { src = l->data; switch (src->key) { case SR_CONF_CONN: @@ -142,12 +145,14 @@ static int config_list(uint32_t key, GVariant **data, static int dev_acquisition_start(const struct sr_dev_inst *sdi) { - struct dev_context *devc = sdi->priv; - struct sr_serial_dev_inst *serial = sdi->conn; + struct dev_context *devc; + struct sr_serial_dev_inst *serial; + devc = sdi->priv; sr_sw_limits_acquisition_start(&devc->limits); std_session_send_df_header(sdi); + serial = sdi->conn; serial_source_add(sdi->session, serial, G_IO_IN, 50, rdtech_um_receive_data, (void *)sdi); diff --git a/src/hardware/rdtech-um/protocol.c b/src/hardware/rdtech-um/protocol.c index 019af105..3a196c76 100644 --- a/src/hardware/rdtech-um/protocol.c +++ b/src/hardware/rdtech-um/protocol.c @@ -73,16 +73,20 @@ static int poll_csum_um34c(char buf[], int len) 55, 57, 59, 63, 67, 69, 73, 79, 83, 89, 97, 99, 109, 111, 113, 119, 121, 127, }; + unsigned int i; - uint8_t csum = 0; + uint8_t csum; if (len != UM_POLL_LEN) return 0; + csum = 0; for (i = 0; i < ARRAY_SIZE(positions); i++) csum ^= buf[positions[i]]; + if (csum != (uint8_t)buf[len - 1]) + return 0; - return csum == (uint8_t)buf[len - 1]; + return 1; } static const struct rdtech_um_profile um_profiles[] = { @@ -94,6 +98,7 @@ static const struct rdtech_um_profile um_profiles[] = { static const struct rdtech_um_profile *find_profile(uint16_t id) { unsigned int i; + for (i = 0; i < ARRAY_SIZE(um_profiles); i++) { if (um_profiles[i].model_id == id) return &um_profiles[i]; @@ -104,10 +109,11 @@ static const struct rdtech_um_profile *find_profile(uint16_t id) SR_PRIV const struct rdtech_um_profile *rdtech_um_probe(struct sr_serial_dev_inst *serial) { const struct rdtech_um_profile *p; - static const uint8_t request = UM_CMD_POLL; + uint8_t request; char buf[RDTECH_UM_BUFSIZE]; int len; + request = UM_CMD_POLL; if (serial_write_blocking(serial, &request, sizeof(request), SERIAL_WRITE_TIMEOUT_MS) < 0) { sr_err("Unable to send probe request."); @@ -136,16 +142,19 @@ SR_PRIV const struct rdtech_um_profile *rdtech_um_probe(struct sr_serial_dev_ins SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi) { - struct dev_context *devc = sdi->priv; - struct sr_serial_dev_inst *serial = sdi->conn; - static const uint8_t request = UM_CMD_POLL; + struct dev_context *devc; + struct sr_serial_dev_inst *serial; + uint8_t request; + serial = sdi->conn; + request = UM_CMD_POLL; if (serial_write_blocking(serial, &request, sizeof(request), SERIAL_WRITE_TIMEOUT_MS) < 0) { sr_err("Unable to send poll request."); return SR_ERR; } + devc = sdi->priv; devc->cmd_sent_at = g_get_monotonic_time() / 1000; return SR_OK; @@ -153,10 +162,11 @@ SR_PRIV int rdtech_um_poll(const struct sr_dev_inst *sdi) static void handle_poll_data(const struct sr_dev_inst *sdi) { - struct dev_context *devc = sdi->priv; + struct dev_context *devc; int i; GSList *ch; + devc = sdi->priv; sr_spew("Received poll packet (len: %d).", devc->buflen); if (devc->buflen != UM_POLL_LEN) { sr_err("Unexpected poll packet length: %i", devc->buflen); @@ -174,11 +184,13 @@ static void handle_poll_data(const struct sr_dev_inst *sdi) static void recv_poll_data(struct sr_dev_inst *sdi, struct sr_serial_dev_inst *serial) { - struct dev_context *devc = sdi->priv; - const struct rdtech_um_profile *p = devc->profile; + struct dev_context *devc; + const struct rdtech_um_profile *p; int len; /* Serial data arrived. */ + devc = sdi->priv; + p = devc->profile; while (devc->buflen < UM_POLL_LEN) { len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); if (len < 1)