serial: Shorten a few code snippets.
This commit is contained in:
parent
c87e9d26f4
commit
bf6b9e7b16
26
src/serial.c
26
src/serial.c
|
@ -55,12 +55,10 @@
|
|||
|
||||
#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)
|
||||
{
|
||||
if (!serial)
|
||||
return 0;
|
||||
if (!serial->lib_funcs)
|
||||
if (!serial || !serial->lib_funcs)
|
||||
return 0;
|
||||
|
||||
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)
|
||||
{
|
||||
if (!serial)
|
||||
return;
|
||||
if (!serial->rcv_buffer)
|
||||
if (!serial || !serial->rcv_buffer)
|
||||
return;
|
||||
|
||||
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)
|
||||
{
|
||||
if (!serial)
|
||||
return 0;
|
||||
if (!serial->rcv_buffer)
|
||||
if (!serial || !serial->rcv_buffer)
|
||||
return 0;
|
||||
|
||||
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,
|
||||
const uint8_t *data, size_t len)
|
||||
{
|
||||
if (!serial)
|
||||
return;
|
||||
if (!data || !len)
|
||||
if (!serial || !data || !len)
|
||||
return;
|
||||
|
||||
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;
|
||||
GString *buf;
|
||||
|
||||
if (!serial)
|
||||
return 0;
|
||||
if (!data || !len)
|
||||
if (!serial || !data || !len)
|
||||
return 0;
|
||||
|
||||
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 */
|
||||
SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes)
|
||||
{
|
||||
int bits, baud;
|
||||
int ret;
|
||||
int timeout_ms;
|
||||
int bits, baud, ret, timeout_ms;
|
||||
|
||||
/* Get the bitrate and frame length. */
|
||||
bits = baud = 0;
|
||||
|
|
|
@ -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
|
||||
* if the requested amount of data is available already.
|
||||
*/
|
||||
if (sr_ser_has_queued_data(serial) >= count) {
|
||||
rc = sr_ser_unqueue_rx_data(serial, buf, count);
|
||||
return rc;
|
||||
}
|
||||
if (sr_ser_has_queued_data(serial) >= count)
|
||||
return sr_ser_unqueue_rx_data(serial, buf, count);
|
||||
|
||||
/*
|
||||
* 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. */
|
||||
for (l = scan_args->addr_list; l; l = l->next) {
|
||||
if (strcmp(addr, l->data) == 0) {
|
||||
if (strcmp(addr, l->data) == 0)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Substitute colons in the address by dashes. */
|
||||
|
|
|
@ -635,9 +635,8 @@ SR_PRIV const char *ser_hid_chip_find_name_vid_pid(uint16_t vid, uint16_t pid)
|
|||
if (!vid_pids)
|
||||
continue;
|
||||
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;
|
||||
}
|
||||
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
|
||||
* if the requested amount of data is available already.
|
||||
*/
|
||||
if (sr_ser_has_queued_data(serial) >= count) {
|
||||
rc = sr_ser_unqueue_rx_data(serial, buf, count);
|
||||
return rc;
|
||||
}
|
||||
if (sr_ser_has_queued_data(serial) >= count)
|
||||
return sr_ser_unqueue_rx_data(serial, buf, count);
|
||||
|
||||
/*
|
||||
* 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)
|
||||
got = count;
|
||||
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 = {
|
||||
|
|
Loading…
Reference in New Issue