From 88603a92a123e68985ce277f0d85466209e5c3c5 Mon Sep 17 00:00:00 2001 From: Marc Schink Date: Wed, 1 Apr 2015 14:37:12 +0200 Subject: [PATCH] Minor cosmetics and cleanups. --- libjaylink/buffer.c | 1 - libjaylink/device.c | 7 ++-- libjaylink/jtag.c | 2 +- libjaylink/libjaylink-internal.h | 36 +++++---------------- libjaylink/libjaylink.h | 70 +++++++++++++++++++++------------------- libjaylink/swd.c | 2 +- libjaylink/transport.c | 35 ++++++++++---------- libjaylink/util.c | 2 -- 8 files changed, 67 insertions(+), 88 deletions(-) diff --git a/libjaylink/buffer.c b/libjaylink/buffer.c index 54f828e..4031a96 100644 --- a/libjaylink/buffer.c +++ b/libjaylink/buffer.c @@ -17,7 +17,6 @@ * along with this program. If not, see . */ -#include #include #include diff --git a/libjaylink/device.c b/libjaylink/device.c index 802345b..d012f5c 100644 --- a/libjaylink/device.c +++ b/libjaylink/device.c @@ -18,6 +18,7 @@ */ #include +#include #include #include @@ -702,8 +703,7 @@ JAYLINK_API int jaylink_read_raw_config(struct jaylink_device_handle *devh, ret = transport_start_write_read(devh, 1, JAYLINK_DEV_CONFIG_SIZE, 1); if (ret != JAYLINK_OK) { - log_err(ctx, "transport_start_write_read() failed: %i.", - ret); + log_err(ctx, "transport_start_write_read() failed: %i.", ret); return ret; } @@ -756,8 +756,7 @@ JAYLINK_API int jaylink_write_raw_config(struct jaylink_device_handle *devh, ret = transport_start_write(devh, 1 + JAYLINK_DEV_CONFIG_SIZE, 1); if (ret != JAYLINK_OK) { - log_err(ctx, "transport_start_write_read() failed: %i.", - ret); + log_err(ctx, "transport_start_write_read() failed: %i.", ret); return ret; } diff --git a/libjaylink/jtag.c b/libjaylink/jtag.c index c601e4b..9ea1b31 100644 --- a/libjaylink/jtag.c +++ b/libjaylink/jtag.c @@ -143,7 +143,7 @@ JAYLINK_API int jaylink_jtag_io(struct jaylink_device_handle *devh, return ret; } - if (status) { + if (status > 0) { log_err(ctx, "JTAG I/O operation failed: %02x.", status); return JAYLINK_ERR; diff --git a/libjaylink/libjaylink-internal.h b/libjaylink/libjaylink-internal.h index dad221f..661ec36 100644 --- a/libjaylink/libjaylink-internal.h +++ b/libjaylink/libjaylink-internal.h @@ -20,7 +20,6 @@ #ifndef LIBJAYLINK_LIBJAYLINK_INTERNAL_H #define LIBJAYLINK_LIBJAYLINK_INTERNAL_H -#include #include #include #include @@ -44,14 +43,12 @@ struct jaylink_context { /** libusb context. */ struct libusb_context *usb_ctx; - /** * List of allocated device instances. * * Used to prevent multiple device instances for the same device. */ struct list *devs; - /** Current log level. */ int log_level; }; @@ -59,16 +56,12 @@ struct jaylink_context { struct jaylink_device { /** libjaylink context. */ struct jaylink_context *ctx; - /** Number of references held on this device instance. */ int refcnt; - /** libusb device instance. */ struct libusb_device *usb_dev; - /** USB address of the device. */ uint8_t usb_address; - /** * Serial number of the device. * @@ -81,19 +74,14 @@ struct jaylink_device { struct jaylink_device_handle { /** Device instance. */ struct jaylink_device *dev; - /** libusb device handle. */ struct libusb_device_handle *usb_devh; - /** USB interface number of the device. */ uint8_t interface_number; - /** USB interface IN endpoint of the device. */ uint8_t endpoint_in; - /** USB interface OUT endpoint of the device. */ uint8_t endpoint_out; - /** * Buffer for write and read operations. * @@ -101,22 +89,17 @@ struct jaylink_device_handle { * consecutively and therefore the same buffer can be used for both. */ uint8_t *buffer; - /** Number of bytes left for the read operation. */ uint16_t read_length; - /** Number of bytes available in the buffer to be read. */ uint16_t bytes_available; - /** Current read position in the buffer. */ uint16_t read_pos; - /** * Number of bytes left to be written before the write operation will * be performed. */ uint16_t write_length; - /** * Current write position in the buffer. * @@ -126,6 +109,13 @@ struct jaylink_device_handle { uint16_t write_pos; }; +struct list { + void *data; + struct list *next; +}; + +typedef int (*list_compare_callback)(const void *a, const void *b); + /*--- buffer.c --------------------------------------------------------------*/ JAYLINK_PRIV void buffer_set_u16(uint8_t *buffer, uint16_t value, @@ -143,24 +133,14 @@ JAYLINK_PRIV struct jaylink_device *device_allocate( /*--- discovery.c -----------------------------------------------------------*/ JAYLINK_PRIV ssize_t discovery_get_device_list(struct jaylink_context *ctx, - struct jaylink_device ***list); + struct jaylink_device ***list); /*--- list.c ----------------------------------------------------------------*/ -struct list { - void *data; - struct list *next; -}; - -typedef int (*list_compare_callback)(const void *a, const void *b); - JAYLINK_PRIV struct list *list_prepend(struct list *list, void *data); - JAYLINK_PRIV struct list *list_remove(struct list *list, const void *data); - JAYLINK_PRIV struct list *list_find_custom(struct list *list, list_compare_callback cb, const void *cb_data); - JAYLINK_PRIV void list_free(struct list *list); /*--- log.c -----------------------------------------------------------------*/ diff --git a/libjaylink/libjaylink.h b/libjaylink/libjaylink.h index 569f8c4..1129ab0 100644 --- a/libjaylink/libjaylink.h +++ b/libjaylink/libjaylink.h @@ -20,7 +20,6 @@ #ifndef LIBJAYLINK_LIBJAYLINK_H #define LIBJAYLINK_LIBJAYLINK_H -#include #include #include @@ -255,69 +254,39 @@ struct jaylink_device_handle; #define JAYLINK_API #endif +/*--- core.c ----------------------------------------------------------------*/ + JAYLINK_API int jaylink_init(struct jaylink_context **ctx); JAYLINK_API void jaylink_exit(struct jaylink_context *ctx); -JAYLINK_API const char *jaylink_strerror(int error_code); -JAYLINK_API const char *jaylink_strerror_name(int error_code); - -JAYLINK_API int jaylink_log_set_level(struct jaylink_context *ctx, int level); -JAYLINK_API int jaylink_log_get_level(const struct jaylink_context *ctx); +/*--- device.c --------------------------------------------------------------*/ JAYLINK_API ssize_t jaylink_get_device_list(struct jaylink_context *ctx, struct jaylink_device ***devices); - JAYLINK_API void jaylink_free_device_list(struct jaylink_device **devices, int unref_devices); - JAYLINK_API int jaylink_device_get_serial_number( const struct jaylink_device *dev, uint32_t *serial_number); - JAYLINK_API int jaylink_device_get_usb_address( const struct jaylink_device *dev); - JAYLINK_API struct jaylink_device *jaylink_ref_device( struct jaylink_device *dev); JAYLINK_API void jaylink_unref_device(struct jaylink_device *dev); - JAYLINK_API int jaylink_open(struct jaylink_device *dev, struct jaylink_device_handle **devh); - JAYLINK_API void jaylink_close(struct jaylink_device_handle *devh); - JAYLINK_API int jaylink_get_firmware_version(struct jaylink_device_handle *devh, char **version); - JAYLINK_API int jaylink_get_hardware_version(struct jaylink_device_handle *devh, struct jaylink_hardware_version *version); - JAYLINK_API int jaylink_get_hardware_status(struct jaylink_device_handle *devh, struct jaylink_hardware_status *status); - JAYLINK_API int jaylink_get_caps(struct jaylink_device_handle *devh, uint8_t *caps); JAYLINK_API int jaylink_get_extended_caps(struct jaylink_device_handle *devh, uint8_t *caps); - JAYLINK_API int jaylink_get_free_memory(struct jaylink_device_handle *devh, uint32_t *size); - -JAYLINK_API int jaylink_set_speed(struct jaylink_device_handle *devh, - uint16_t speed); - -JAYLINK_API int jaylink_select_interface(struct jaylink_device_handle *devh, - uint8_t interface); -JAYLINK_API int jaylink_get_available_interfaces( - struct jaylink_device_handle *devh, uint32_t *interfaces); -JAYLINK_API int jaylink_get_selected_interface( - struct jaylink_device_handle *devh); - -JAYLINK_API int jaylink_clear_reset(struct jaylink_device_handle *devh); -JAYLINK_API int jaylink_set_reset(struct jaylink_device_handle *devh); - -JAYLINK_API int jaylink_set_target_power(struct jaylink_device_handle *devh, - int enable); - JAYLINK_API int jaylink_read_raw_config(struct jaylink_device_handle *devh, uint8_t *config); JAYLINK_API int jaylink_write_raw_config(struct jaylink_device_handle *devh, @@ -331,16 +300,32 @@ JAYLINK_API int jaylink_unregister(struct jaylink_device_handle *devh, struct jaylink_connection *connections, uint8_t *info, uint16_t *info_size); +/*--- error.c ---------------------------------------------------------------*/ + +JAYLINK_API const char *jaylink_strerror(int error_code); +JAYLINK_API const char *jaylink_strerror_name(int error_code); + +/*--- jtag.c ----------------------------------------------------------------*/ + JAYLINK_API int jaylink_jtag_io(struct jaylink_device_handle *devh, const uint8_t *tms, const uint8_t *tdi, uint8_t *tdo, uint16_t length, int version); JAYLINK_API int jaylink_jtag_clear_trst(struct jaylink_device_handle *devh); JAYLINK_API int jaylink_jtag_set_trst(struct jaylink_device_handle *devh); +/*--- log.c -----------------------------------------------------------------*/ + +JAYLINK_API int jaylink_log_set_level(struct jaylink_context *ctx, int level); +JAYLINK_API int jaylink_log_get_level(const struct jaylink_context *ctx); + +/*--- swd.c -----------------------------------------------------------------*/ + JAYLINK_API int jaylink_swd_io(struct jaylink_device_handle *devh, const uint8_t *direction, const uint8_t *out, uint8_t *in, uint16_t length); +/*--- swo.c -----------------------------------------------------------------*/ + JAYLINK_API int jaylink_swo_start(struct jaylink_device_handle *devh, enum jaylink_swo_mode mode, uint32_t baudrate, uint32_t size); JAYLINK_API int jaylink_swo_stop(struct jaylink_device_handle *devh); @@ -349,6 +334,23 @@ JAYLINK_API ssize_t jaylink_swo_read(struct jaylink_device_handle *devh, JAYLINK_API int jaylink_swo_get_speed_info(struct jaylink_device_handle *devh, enum jaylink_swo_mode mode, uint32_t *freq, uint32_t *div); +/*--- target.c --------------------------------------------------------------*/ + +JAYLINK_API int jaylink_set_speed(struct jaylink_device_handle *devh, + uint16_t speed); +JAYLINK_API int jaylink_select_interface(struct jaylink_device_handle *devh, + uint8_t interface); +JAYLINK_API int jaylink_get_available_interfaces( + struct jaylink_device_handle *devh, uint32_t *interfaces); +JAYLINK_API int jaylink_get_selected_interface( + struct jaylink_device_handle *devh); +JAYLINK_API int jaylink_clear_reset(struct jaylink_device_handle *devh); +JAYLINK_API int jaylink_set_reset(struct jaylink_device_handle *devh); +JAYLINK_API int jaylink_set_target_power(struct jaylink_device_handle *devh, + int enable); + +/*--- util.c ----------------------------------------------------------------*/ + JAYLINK_API int jaylink_has_cap(const uint8_t *caps, uint32_t cap); #endif /* LIBJAYLINK_LIBJAYLINK_H */ diff --git a/libjaylink/swd.c b/libjaylink/swd.c index 413b3d1..250fdf9 100644 --- a/libjaylink/swd.c +++ b/libjaylink/swd.c @@ -120,7 +120,7 @@ JAYLINK_API int jaylink_swd_io(struct jaylink_device_handle *devh, return ret; } - if (status) { + if (status > 0) { log_err(ctx, "SWD I/O operation failed: %02x.", status); return JAYLINK_ERR; } diff --git a/libjaylink/transport.c b/libjaylink/transport.c index 6224f35..bcbe952 100644 --- a/libjaylink/transport.c +++ b/libjaylink/transport.c @@ -17,6 +17,7 @@ * along with this program. If not, see . */ +#include #include #include @@ -30,16 +31,16 @@ */ /** Timeout of an USB transfer in milliseconds. */ -#define USB_TIMEOUT 1000 +#define USB_TIMEOUT 1000 /** * Number of consecutive timeouts before an USB transfer will be treated as * timed out. */ -#define NUM_TIMEOUTS 2 +#define NUM_TIMEOUTS 2 /** Chunk size in bytes in which data is transferred. */ -#define CHUNK_SIZE 2048 +#define CHUNK_SIZE 2048 /** * Buffer size in bytes. @@ -47,7 +48,7 @@ * Note that both write and read operations require a buffer size of at least * #CHUNK_SIZE bytes. */ -#define BUFFER_SIZE CHUNK_SIZE +#define BUFFER_SIZE CHUNK_SIZE static int initialize_handle(struct jaylink_device_handle *devh) { @@ -280,11 +281,11 @@ JAYLINK_PRIV int transport_start_write(struct jaylink_device_handle *devh, log_dbg(ctx, "Starting write operation (length = %u bytes).", length); - if (devh->write_pos) + if (devh->write_pos > 0) log_warn(ctx, "Last write operation left %u bytes in the " "buffer.", devh->write_pos); - if (devh->write_length) + if (devh->write_length > 0) log_warn(ctx, "Last write operation was not performed."); devh->write_length = length; @@ -318,11 +319,11 @@ JAYLINK_PRIV int transport_start_read(struct jaylink_device_handle *devh, log_dbg(ctx, "Starting read operation (length = %u bytes).", length); - if (devh->bytes_available) + if (devh->bytes_available > 0) log_dbg(ctx, "Last read operation left %u bytes in the " "buffer.", devh->bytes_available); - if (devh->read_length) + if (devh->read_length > 0) log_warn(ctx, "Last read operation left %u bytes.", devh->read_length); @@ -366,18 +367,18 @@ JAYLINK_PRIV int transport_start_write_read(struct jaylink_device_handle *devh, log_dbg(ctx, "Starting write / read operation (length = " "%u / %u bytes).", write_length, read_length); - if (devh->write_pos) + if (devh->write_pos > 0) log_warn(ctx, "Last write operation left %u bytes in the " "buffer.", devh->write_pos); - if (devh->write_length) + if (devh->write_length > 0) log_warn(ctx, "Last write operation was not performed."); - if (devh->bytes_available) + if (devh->bytes_available > 0) log_warn(ctx, "Last read operation left %u bytes in the " "buffer.", devh->bytes_available); - if (devh->read_length) + if (devh->read_length > 0) log_warn(ctx, "Last read operation left %u bytes.", devh->read_length); @@ -403,7 +404,7 @@ static int usb_recv(struct jaylink_device_handle *devh, uint8_t *buffer, tries = NUM_TIMEOUTS; transferred = 0; - while (tries && !transferred) { + while (tries > 0 && !transferred) { /* Always request CHUNK_SIZE bytes from the device. */ ret = libusb_bulk_transfer(devh->usb_devh, devh->endpoint_in, (unsigned char *)buffer, CHUNK_SIZE, &transferred, @@ -424,7 +425,7 @@ static int usb_recv(struct jaylink_device_handle *devh, uint8_t *buffer, } /* Ignore a possible timeout if at least one byte was received. */ - if (transferred) { + if (transferred > 0) { *length = transferred; return JAYLINK_OK; } @@ -445,7 +446,7 @@ static int usb_send(struct jaylink_device_handle *devh, const uint8_t *buffer, ctx = devh->dev->ctx; tries = NUM_TIMEOUTS; - while (tries && length) { + while (tries > 0 && length > 0) { /* Send data in chunks of CHUNK_SIZE bytes to the device. */ ret = libusb_bulk_transfer(devh->usb_devh, devh->endpoint_out, (unsigned char *)buffer, MIN(CHUNK_SIZE, length), @@ -562,7 +563,7 @@ JAYLINK_PRIV int transport_write(struct jaylink_device_handle *devh, fill_bytes = (num_chunks * CHUNK_SIZE) - devh->write_pos; tmp = MIN(length, fill_bytes); - if (tmp) { + if (tmp > 0) { memcpy(devh->buffer + devh->write_pos, buffer, tmp); length -= tmp; @@ -646,7 +647,7 @@ JAYLINK_PRIV int transport_read(struct jaylink_device_handle *devh, devh->read_pos = 0; } - while (length) { + while (length > 0) { /* * If less than CHUNK_SIZE bytes are requested from the device, * store the received data in the internal buffer instead of diff --git a/libjaylink/util.c b/libjaylink/util.c index ac2bcfb..62c7c90 100644 --- a/libjaylink/util.c +++ b/libjaylink/util.c @@ -17,8 +17,6 @@ * along with this program. If not, see . */ -#include - #include "libjaylink.h" /** -- 2.11.4.GIT