Rename the unfortunately named poll_csum() callback. It neither polled
for receive data nor calculated checksums for the caller. It verifies
that a received packet's checksum is plausible, and returns a boolean.
Address coding style nits in the process. Adjust data types. Use size
types where appropriate. Don't use characters when bytes are meant. Use
booleans for the checksum test result. Shorten identifier names to trim
text line length. No need to "take the address" of a routine. Remove
dead code, model dependent checksum approaches are handled by registered
callbacks in profiles. Eliminate else after return. Prefer read_u16be()
endianess routines over RB16() preprocessor macros. Prefer lower case
hex digits. Make the code reflect that the last 16bit or 8bit entity in
the received data packet gets checked. Use "calc" and "recv" names for
awareness which variable holds which value.
-static int poll_csum_fff1(char buf[], int len)
+static gboolean csum_ok_fff1(const uint8_t *buf, size_t len)
- return 0;
- else
- return RB16(&buf[len - 2]) == 0xFFF1;
+ return FALSE;
+
+ csum_recv = read_u16be(&buf[len - sizeof(uint16_t)]);
+ if (csum_recv != 0xfff1)
+ return FALSE;
+
+ return TRUE;
-static int poll_csum_um34c(char buf[], int len)
+static gboolean csum_ok_um34c(const uint8_t *buf, size_t len)
{
static const int positions[] = {
1, 3, 7, 9, 15, 17, 19, 23, 31, 39, 41, 45, 49, 53,
{
static const int positions[] = {
1, 3, 7, 9, 15, 17, 19, 23, 31, 39, 41, 45, 49, 53,
111, 113, 119, 121, 127,
};
111, 113, 119, 121, 127,
};
- unsigned int i;
- uint8_t csum;
+ size_t i;
+ uint8_t csum_calc, csum_recv;
for (i = 0; i < ARRAY_SIZE(positions); i++)
for (i = 0; i < ARRAY_SIZE(positions); i++)
- csum ^= buf[positions[i]];
- if (csum != (uint8_t)buf[len - 1])
- return 0;
+ csum_calc ^= buf[positions[i]];
+ csum_recv = read_u8(&buf[len - sizeof(uint8_t)]);
+ if (csum_recv != csum_calc)
+ return FALSE;
}
static const struct rdtech_um_profile um_profiles[] = {
}
static const struct rdtech_um_profile um_profiles[] = {
- { "UM24C", RDTECH_UM24C, rdtech_default_channels, &poll_csum_fff1, },
- { "UM25C", RDTECH_UM25C, rdtech_um25c_channels, &poll_csum_fff1, },
- { "UM34C", RDTECH_UM34C, rdtech_default_channels, &poll_csum_um34c, },
+ { "UM24C", RDTECH_UM24C, rdtech_default_channels, csum_ok_fff1, },
+ { "UM25C", RDTECH_UM25C, rdtech_um25c_channels, csum_ok_fff1, },
+ { "UM34C", RDTECH_UM34C, rdtech_default_channels, csum_ok_um34c, },
};
static const struct rdtech_um_profile *find_profile(uint16_t id)
};
static const struct rdtech_um_profile *find_profile(uint16_t id)
{
const struct rdtech_um_profile *p;
uint8_t request;
{
const struct rdtech_um_profile *p;
uint8_t request;
- char buf[RDTECH_UM_BUFSIZE];
+ uint8_t buf[RDTECH_UM_BUFSIZE];
int len;
request = UM_CMD_POLL;
int len;
request = UM_CMD_POLL;
- if (!p->poll_csum(buf, len)) {
+ if (!p->csum_ok(buf, len)) {
sr_err("Probe response contains illegal checksum or end marker.\n");
return NULL;
}
sr_err("Probe response contains illegal checksum or end marker.\n");
return NULL;
}
GSList *ch;
devc = sdi->priv;
GSList *ch;
devc = sdi->priv;
- sr_spew("Received poll packet (len: %d).", devc->buflen);
+ sr_spew("Received poll packet (len: %zu).", devc->buflen);
if (devc->buflen != UM_POLL_LEN) {
if (devc->buflen != UM_POLL_LEN) {
- sr_err("Unexpected poll packet length: %i", devc->buflen);
+ sr_err("Unexpected poll packet length: %zu", devc->buflen);
- if (devc->buflen == UM_POLL_LEN && p->poll_csum(devc->buf, devc->buflen))
+ if (devc->buflen == UM_POLL_LEN && p->csum_ok(devc->buf, devc->buflen))
handle_poll_data(sdi);
else
sr_warn("Skipping packet with illegal checksum / end marker.");
handle_poll_data(sdi);
else
sr_warn("Skipping packet with illegal checksum / end marker.");
RDTECH_UM34C = 0x0d4c,
};
RDTECH_UM34C = 0x0d4c,
};
-enum rdtech_um_checksum {
- RDTECH_CSUM_STATIC_FFF1,
- RDTECH_CSUM_UM34C,
-};
-
/* Supported device profiles */
struct rdtech_um_profile {
const char *model_name;
enum rdtech_um_model_id model_id;
const struct binary_analog_channel *channels;
/* Supported device profiles */
struct rdtech_um_profile {
const char *model_name;
enum rdtech_um_model_id model_id;
const struct binary_analog_channel *channels;
-
- /* Verify poll packet checksum; return 1 if OK, 0 otherwise. */
- int (*poll_csum)(char buf[], int len);
+ gboolean (*csum_ok)(const uint8_t *buf, size_t len);
};
struct dev_context {
const struct rdtech_um_profile *profile;
struct sr_sw_limits limits;
};
struct dev_context {
const struct rdtech_um_profile *profile;
struct sr_sw_limits limits;
-
- char buf[RDTECH_UM_BUFSIZE];
- int buflen;
+ uint8_t buf[RDTECH_UM_BUFSIZE];
+ size_t buflen;