serial: Shorten a few code snippets.

This commit is contained in:
Uwe Hermann 2019-06-15 17:35:42 +02:00
parent c87e9d26f4
commit bf6b9e7b16
3 changed files with 14 additions and 33 deletions

View File

@ -55,12 +55,10 @@
#ifdef HAVE_SERIAL_COMM #ifdef HAVE_SERIAL_COMM
/* See if a (assumed opened) serial port is of any supported type. */ /* See if an (assumed opened) serial port is of any supported type. */
static int dev_is_supported(struct sr_serial_dev_inst *serial) static int dev_is_supported(struct sr_serial_dev_inst *serial)
{ {
if (!serial) if (!serial || !serial->lib_funcs)
return 0;
if (!serial->lib_funcs)
return 0; return 0;
return 1; return 1;
@ -270,9 +268,7 @@ SR_PRIV int serial_set_read_chunk_cb(struct sr_serial_dev_inst *serial,
*/ */
SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial) SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial)
{ {
if (!serial) if (!serial || !serial->rcv_buffer)
return;
if (!serial->rcv_buffer)
return; return;
g_string_truncate(serial->rcv_buffer, 0); g_string_truncate(serial->rcv_buffer, 0);
@ -288,9 +284,7 @@ SR_PRIV void sr_ser_discard_queued_data(struct sr_serial_dev_inst *serial)
*/ */
SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial) SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial)
{ {
if (!serial) if (!serial || !serial->rcv_buffer)
return 0;
if (!serial->rcv_buffer)
return 0; return 0;
return serial->rcv_buffer->len; return serial->rcv_buffer->len;
@ -309,9 +303,7 @@ SR_PRIV size_t sr_ser_has_queued_data(struct sr_serial_dev_inst *serial)
SR_PRIV void sr_ser_queue_rx_data(struct sr_serial_dev_inst *serial, SR_PRIV void sr_ser_queue_rx_data(struct sr_serial_dev_inst *serial,
const uint8_t *data, size_t len) const uint8_t *data, size_t len)
{ {
if (!serial) if (!serial || !data || !len)
return;
if (!data || !len)
return; return;
if (serial->rx_chunk_cb_func) if (serial->rx_chunk_cb_func)
@ -336,9 +328,7 @@ SR_PRIV size_t sr_ser_unqueue_rx_data(struct sr_serial_dev_inst *serial,
size_t qlen; size_t qlen;
GString *buf; GString *buf;
if (!serial) if (!serial || !data || !len)
return 0;
if (!data || !len)
return 0; return 0;
qlen = sr_ser_has_queued_data(serial); qlen = sr_ser_has_queued_data(serial);
@ -1027,9 +1017,7 @@ SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id)
/** @private */ /** @private */
SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes) SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes)
{ {
int bits, baud; int bits, baud, ret, timeout_ms;
int ret;
int timeout_ms;
/* Get the bitrate and frame length. */ /* Get the bitrate and frame length. */
bits = baud = 0; bits = baud = 0;

View File

@ -494,10 +494,8 @@ static int ser_bt_read(struct sr_serial_dev_inst *serial,
* Immediately satisfy the caller's request from the RX buffer * Immediately satisfy the caller's request from the RX buffer
* if the requested amount of data is available already. * if the requested amount of data is available already.
*/ */
if (sr_ser_has_queued_data(serial) >= count) { if (sr_ser_has_queued_data(serial) >= count)
rc = sr_ser_unqueue_rx_data(serial, buf, count); return sr_ser_unqueue_rx_data(serial, buf, count);
return rc;
}
/* /*
* When a timeout was specified, then determine the deadline * When a timeout was specified, then determine the deadline
@ -766,9 +764,8 @@ static void scan_cb(void *cb_args, const char *addr, const char *name)
/* Check whether the device was seen before. */ /* Check whether the device was seen before. */
for (l = scan_args->addr_list; l; l = l->next) { for (l = scan_args->addr_list; l; l = l->next) {
if (strcmp(addr, l->data) == 0) { if (strcmp(addr, l->data) == 0)
return; return;
}
} }
/* Substitute colons in the address by dashes. */ /* Substitute colons in the address by dashes. */

View File

@ -635,9 +635,8 @@ SR_PRIV const char *ser_hid_chip_find_name_vid_pid(uint16_t vid, uint16_t pid)
if (!vid_pids) if (!vid_pids)
continue; continue;
while (vid_pids->vid) { while (vid_pids->vid) {
if (vid_pids->vid == vid && vid_pids->pid == pid) { if (vid_pids->vid == vid && vid_pids->pid == pid)
return desc->chipname; return desc->chipname;
}
vid_pids++; vid_pids++;
} }
} }
@ -1298,10 +1297,8 @@ static int ser_hid_read(struct sr_serial_dev_inst *serial,
* Immediately satisfy the caller's request from the RX buffer * Immediately satisfy the caller's request from the RX buffer
* if the requested amount of data is available already. * if the requested amount of data is available already.
*/ */
if (sr_ser_has_queued_data(serial) >= count) { if (sr_ser_has_queued_data(serial) >= count)
rc = sr_ser_unqueue_rx_data(serial, buf, count); return sr_ser_unqueue_rx_data(serial, buf, count);
return rc;
}
/* /*
* When a timeout was specified, then determine the deadline * When a timeout was specified, then determine the deadline
@ -1387,9 +1384,8 @@ static int ser_hid_read(struct sr_serial_dev_inst *serial,
if (got > count) if (got > count)
got = count; got = count;
sr_dbg("DBG: %s() passing %d bytes.", __func__, got); sr_dbg("DBG: %s() passing %d bytes.", __func__, got);
rc = sr_ser_unqueue_rx_data(serial, buf, count);
return rc; return sr_ser_unqueue_rx_data(serial, buf, count);
} }
static struct ser_lib_functions serlib_hid = { static struct ser_lib_functions serlib_hid = {