X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Frdtech-um%2Fprotocol.c;h=3a196c76070ce14202c49e306b3482672c70ff2f;hb=fda34e5af6e7a11744035c1c8df2f2b2f95cf1d8;hp=019af105db9d5bacbee82e714e442361c2624145;hpb=dca972f8ba1eca12fd462bd97788b18c8953ce64;p=libsigrok.git 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)