From 3ef6582d2b14cc3fb91deb80406b942ef4a9c45e Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 18 Jan 2025 10:15:01 +0100 Subject: [PATCH 1/4] src/modbus-rtu-usb.c: fix ssize_t printing (in a blunt manner) Just cast from `ssize_t` to a more portable `long long int` type; here even a `short int` should be big enough for expected values. If a more diligent solution is needed in the future, see e.g. https://github.com/networkupstools/nut/blob/master/include/nut_stdint.h Signed-off-by: Jim Klimov --- src/modbus-rtu-usb.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/modbus-rtu-usb.c b/src/modbus-rtu-usb.c index 4ef08e19..0d4d738e 100644 --- a/src/modbus-rtu-usb.c +++ b/src/modbus-rtu-usb.c @@ -418,7 +418,9 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx) } if (ctx->debug) { - printf("Number of USB devices: %ld\n", devs_len); + // Here and below: cast from ssize_t to a portable type big enough for expected + // values + printf("Number of USB devices: %lld\n", (long long int) devs_len); } for (i = 0; i < devs_len; i++) { @@ -433,16 +435,16 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx) if (r != LIBUSB_SUCCESS) { if (ctx->debug) { fprintf(stderr, - "libusb_get_device_descriptor for device #%ld failed: %s\n", - i, + "libusb_get_device_descriptor for device #%lld failed: %s\n", + (long long int) i, libusb_strerror(r)); } continue; } if (ctx->debug) { - printf("Considering device #%ld (%04x:%04x)\n", - i, + printf("Considering device #%lld (%04x:%04x)\n", + (long long int) i, dev_desc.idVendor, dev_desc.idProduct); } @@ -461,7 +463,9 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx) r = libusb_get_port_numbers(d, ud.port_path, sizeof(ud.port_path)); if (r < 1) { if (ctx->debug) { - fprintf(stderr, "libusb_get_port_numbers for device #%ld failed\n", i); + fprintf(stderr, + "libusb_get_port_numbers for device #%lld failed\n", + (long long int) i); } continue; } @@ -477,8 +481,8 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx) if ((r = libusb_open(d, &dev_handle)) != LIBUSB_SUCCESS) { if (ctx->debug) { fprintf(stderr, - "libusb_open for device #%ld failed: %s\n", - i, + "libusb_open for device #%lld failed: %s\n", + (long long int) i, libusb_strerror(r)); } continue; @@ -540,7 +544,7 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx) if (is_match) { if (ctx->debug) { - printf("Found Device %ld (Path %s):\n", i, path_buffer); + printf("Found Device %lld (Path %s):\n", (long long int) i, path_buffer); printf(" Vendor ID: 0x%04x\n", ud.vid); printf(" Product ID: 0x%04x\n", ud.pid); } From 713f900f5e7b0514d74411d45439c4d1d8d5ae09 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 18 Jan 2025 10:53:49 +0100 Subject: [PATCH 2/4] src/modbus-rtu-usb.c: subject to clang-format Signed-off-by: Jim Klimov --- src/modbus-rtu-usb.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/modbus-rtu-usb.c b/src/modbus-rtu-usb.c index 0d4d738e..5b781a6f 100644 --- a/src/modbus-rtu-usb.c +++ b/src/modbus-rtu-usb.c @@ -552,10 +552,11 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx) ctx_rtu_usb->device_handle = dev_handle; #if defined HAVE_LIBUSB_POLLFD && HAVE_LIBUSB_POLLFD if (usb_ctx) { - const struct libusb_pollfd** pollfds = libusb_get_pollfds(usb_ctx); + const struct libusb_pollfd **pollfds = libusb_get_pollfds(usb_ctx); if (pollfds) { - unsigned j; - for (j=0; pollfds[j] != NULL; j++) {} + unsigned j; + for (j = 0; pollfds[j] != NULL; j++) { + } if (ctx->debug) { printf("Got a list of %u libusb file descriptors to poll\n", j); } @@ -576,9 +577,10 @@ static int _modbus_rtu_usb_connect(modbus_t *ctx) } #else if (ctx->debug) { - printf("Can not get a list of libusb file descriptors to poll from this libusb version\n"); + printf("Can not get a list of libusb file descriptors to poll from this " + "libusb version\n"); } -#endif /* HAVE_LIBUSB_POLLFD */ +#endif /* HAVE_LIBUSB_POLLFD */ break; } From 93d79d4a37ddce278de236c27025926783e0e606 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 18 Jan 2025 11:10:50 +0000 Subject: [PATCH 3/4] tests/Makefile.am: fix test build on systems without libusb-1.0 installed Signed-off-by: Jim Klimov --- tests/Makefile.am | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/Makefile.am b/tests/Makefile.am index 0daee73d..61e88332 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -16,10 +16,12 @@ noinst_PROGRAMS += \ endif common_ldflags = \ - $(top_builddir)/src/libmodbus.la \ - -lusb-1.0 + $(top_builddir)/src/libmodbus.la if WITH_LIBUSB +common_ldflags += \ + -lusb-1.0 + apc_test_client_SOURCES = apc-test-client.c apc_test_client_LDADD = $(common_ldflags) endif From dcbea2571e5698f6b3c3e2365395ad80894ec944 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 18 Jan 2025 20:04:22 +0000 Subject: [PATCH 4/4] src/modbus.c: _modbus_receive_msg(): we use a *backend* select(), not the standard one; do return from method if that implementation fails Note that the primary "weird" use-case is about libusb backend; its _modbus_rtu_usb_select() ignores "rset" anyway, and talks to the libusb context. Signed-off-by: Jim Klimov --- src/modbus.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/modbus.c b/src/modbus.c index b735359e..af3e9781 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -388,7 +388,7 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) if (ctx->s < 0) { if (ctx->debug) { /* we may not have an FD with e.g. libusb usage */ - fprintf(stderr, "Using a backend without a file descriptor, will not select() on it.\n"); + fprintf(stderr, "Using a backend without a file descriptor, will not natively select() on it.\n"); } } else { FD_SET(ctx->s, &rset); @@ -444,12 +444,7 @@ int _modbus_receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) errno = saved_errno; #endif } - if (ctx->s >= 0) { - return -1; - } - // else: We have at most tried some default FD's but not - // the (lacking) one for the backend, so fall through for - // its recv method anyway (e.g. query libusb directly). + return -1; } rc = ctx->backend->recv(ctx, msg + msg_length, length_to_read);