From: Uwe Hermann Date: Sat, 15 Jun 2019 15:35:42 +0000 (+0200) Subject: serial: Shorten a few code snippets. X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=bf6b9e7b16264d2a6ccb3ac2a6004cbc2cef0e43 serial: Shorten a few code snippets. --- diff --git a/src/serial.c b/src/serial.c index a2d0189f..7f9de4f6 100644 --- a/src/serial.c +++ b/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; diff --git a/src/serial_bt.c b/src/serial_bt.c index 37a1a68b..8ed4523b 100644 --- a/src/serial_bt.c +++ b/src/serial_bt.c @@ -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. */ diff --git a/src/serial_hid.c b/src/serial_hid.c index d2776fc7..ee073d1d 100644 --- a/src/serial_hid.c +++ b/src/serial_hid.c @@ -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 = {