2) libsigrok.h wasn't treated as a distributable file and hence couldn't be found. This is a result of two commits:
https://sigrok.org/gitweb/?p=libserialport.git;a=commit;h=f1c916ede191ec398cbe53d7f978078311c25785 marking libsigrok.h as non-distributable since it was build from libsigrok.h.in at the time and
https://sigrok.org/gitweb/?p=libserialport.git;a=commit;h=f6e32b2dfa322aa4bd4a279a46d588e0e73ea238 which removed libsigrok.h.in in favor of a static libsigrok.h
The second commit didn't revert the Makefile.am change that marked libsigrok.h as non-distributable, which this commit takes care of.
André Fonseca [Tue, 29 Aug 2023 08:11:10 +0000 (10:11 +0200)]
change type of result variables to ssize_t
These variables are being used to store the result of read/write calls,
which return a ssize_t value, which depending on platform can be bigger
than an int.
André Fonseca [Mon, 28 Aug 2023 13:28:22 +0000 (15:28 +0200)]
rename deprecated constant kIOMasterPortDefault
This constant is now deprecated since macOS 12.0 and a new constant
named kIOMainPortDefault is provided. There is a fallback in place
that retains compatibility for older macOS versions.
Karl Palsson [Fri, 11 Jun 2021 17:07:09 +0000 (17:07 +0000)]
HACK: don't even check for termiox
termiox was removed from linux in e0efb3168d34
Some more information available in https://www.spinics.net/lists/linux-serial/msg41926.html
Attempting to use the termiox ioctls on more modern kernels results in
"Inappropriate IOCTL" errors.
While the "right" solution might be to remove the termiox code from the
linux path, simply not checking for termiox builds a libserialport that
functions on modern linux kernels.
Ben Gardiner [Tue, 22 Sep 2020 03:10:08 +0000 (03:10 +0000)]
sp: clear HUPCL to preserve control lines on close
The comment and code are out of sync. The comments say "leave control
lines alone on close." The HUPCL bit, when set, will "Lower modem control
lines after last process closes the device (hang up)."
To match the intent captured in the comment, the HUPCL bit should be
cleared.
Wolfram Sang [Sun, 5 Apr 2020 21:24:50 +0000 (23:24 +0200)]
Return proper type when sp_get_port_transport() fails
The above function must always return an 'enum sp_transport'. So, return
NATIVE if no port is present because its effective meaning within the
API is "you shouldn't call any transport-specific functions for this
port handle".
Martin Ling [Sat, 25 Jan 2020 14:51:56 +0000 (14:51 +0000)]
Fix building on Cygwin.
There were two issues: first, feature test macros were only defined
for __linux__, so defintions we needed were not included. Enable the
same feature test macros for __CYGWIN__.
Second, the Cygwin headers do not define TIOCOUTQ, needed to use
ioctl() to get the number of bytes in the output queue. Return
SP_ERR_SUPP on Cygwin for this operation.
Martin Ling [Fri, 24 Jan 2020 05:14:47 +0000 (05:14 +0000)]
windows: Fix another CreateFile usage.
When built with MSVC with unicode enabled, this gave:
warning C4133: 'function': incompatible types - from 'char *' to 'LPCWSTR'
due to CreateFile expanding to CreateFileW which accepts UTF-16 filenames.
The device name used here is in 8-bit format, having come from a call to
wc_to_utf8() in either get_root_hub_name() or get_external_hub_name(). So
we need to use CreateFileA.
Martin Ling [Fri, 24 Jan 2020 04:55:32 +0000 (04:55 +0000)]
windows: Fix warnings for conversions in time_as_timeval().
Building with MSVC gave:
warning C4244: '=': conversion from 'LONGLONG' to 'long', possible loss of data
when assigning the results of these calculation to the long fields
of struct timeval. The result should be OK, but put an explicit
cast in to make the change clear and suppress the warning.
Martin Ling [Fri, 24 Jan 2020 04:44:47 +0000 (04:44 +0000)]
windows: Avoid leak of write buffer on realloc failure.
VS2019 IntelliSense reported:
Warning C6308: 'realloc' might return null pointer: assigning null
pointer to 'port->write_buf', which is passed as an
argument to 'realloc', will cause the original memory
block to be leaked.
This is correct, we would leak the buffer on a realloc failure.
Put the realloc result in a separate variable and handle the error
path before assigning the result to port->write_buf.
Martin Ling [Fri, 24 Jan 2020 04:30:22 +0000 (04:30 +0000)]
windows: Use correct variant of FormatMessage.
When built with MSVC and unicode enabled, using 'message' gave:
warning C4133: 'initializing': incompatible types - from 'TCHAR *' to 'char *'
FormatMessage expands to either FormatMessageA or FormatMessageW
depending if unicode is enabled, and generates either a char (8-bit)
or WCHAR (UTF-16) string accordingly.
Since sp_last_error_message() returns char *, we must use the 8-bit
variant. The message will be encoded in the current code page.
Martin Ling [Fri, 24 Jan 2020 04:00:13 +0000 (04:00 +0000)]
windows: Use correct variant of CreateFile.
When built with MSVC and unicode enabled, using CreateFile gave:
warning C4133: 'function': incompatible types - from 'char *' to 'LPCWSTR'
CreateFile is a macro expanding to either CreateFileW if unicode
mode is enabled, or CreateFileA if not.
For CreateFileW, the filename is a UTF-16 string. For CreateFileA
it is an 'ANSI' string, meaning 8-bit chars in the current Windows
code page.
We do need to stick to 8-bit strings for port names, since
sp_get_port_by_name() and sp_get_port_name() are defined with
char * types, and that is what we store in struct sp_port. So
CreateFileA is the correct version to use.
Since Windows serial port names are always just 'COM' and a digit,
with a '\\.\' prefix for higher numbers, encoding is fortunately
not an issue - ASCII, UTF-8 and all the Windows code pages seem to
be equivalent for these characters.
We should however explicitly document what the encoding of strings
accepted and returned by libserialport is.
Martin Ling [Fri, 24 Jan 2020 03:11:47 +0000 (03:11 +0000)]
Fix some warnings for size_t, DWORD and int conversions.
These cases are all in the sp_[non]blocking_{read,write} functions.
On MSVC, these conversions would generate warnings such as:
warning C4267: '=': conversion from 'size_t' to 'DWORD', possible loss of data
The warnings are genuine. There are some places where overflow is technically
possible, due to our use of size_t for sizes in function parameters (unsigned
64-bit on Windows x64), but an enum for return values (typically signed int
and 32-bit, but not guaranteed to be so by the standards), plus the Win32 API
usage of DWORD (unsigned 32-bit) for sizes in ReadFile/WriteFile.
However, overflow in practice would require reading/writing more than 2GB
over a serial port in a single call and is therefore unlikely to be a
real-world concern. I have therefore not tried to catch those cases - but the
places it is possible do now have explicit casts to the smaller types so that
they are more obvious.
We could document and test for a maximum read/write size of INT_MAX, but that
would still depend on the storage of 'enum sp_return' being at least a signed
int, which as I understand it the C standard does not require.
To be absolutely correct we would need a different API where sp_return
was only used for result codes, and the read/write functions took a
pointer to size_t for result sizes.
Martin Ling [Mon, 20 Jan 2020 05:02:27 +0000 (05:02 +0000)]
Use SP_API prefix for functions in libserialport.h.
For MSVC, we need to set the __declspec() for public symbols to
dllexport or dllimport, depending if we are building or using the
library. So, detect MSVC and define SP_API appropriately if found.
We use the LIBSERIALPORT_MSBUILD define to distinguish between
building and using the library, which will need to be set in the
project configuration when building the library using MS tools.
For normal client use of the header on other systems, we need to
define SP_API to nothing to avoid it being undefined, but we need
to avoid doing this in the case where we are including the header
whilst building the library with autotools and SP_API is already
set by autoconf. So define LIBSERIALPORT_ATBUILD in AM_CFLAGS,
and don't touch SP_API in the header if that's set.
Martin Ling [Mon, 20 Jan 2020 01:42:00 +0000 (01:42 +0000)]
windows: Handle the case where there are no serial ports at all.
It's possible for the HARDWARE\DEVICEMAP\SERIALCOMM key to not exist in
the registry if there are no serial ports at all and never have been, as
discovered on my rather minimalist gaming machine.
Handle that case gracefully and return an empty list.
RegOpenKeyEx() and RegQueryInfoKey() return system error codes directly,
not by setting the thread-local errno equivalent that is returned by
GetLastError().
When returning SP_ERR_FAIL, our API specifies that sp_last_error_code()
may be called immediately afterwards to get the system error code. In
this case that would not work, as it would call GetLastError() and miss
the directly-returned result.
We therefore need to call SetLastError() with the error code before
returning with SP_ERR_FAIL.
Martin Ling [Sat, 4 Jan 2020 23:00:17 +0000 (23:00 +0000)]
android: Fix build compatibility with NDK platform 21 and up.
In platforms 21 and higher of the NDK, linux/serial.h is available,
which it was not before. This broke the build, because the configure
script would detect the availability of 'struct serial_struct' in that
header and set HAVE_STRUCT_SERIAL_STRUCT, but the #ifndef __ANDROID__
in libserialport_internal.h stopped us actually including the header.
This change fixes things to build with all versions of the NDK, and is
tested with builds for arm from versions 9 to 24.
Version 21 also added availability of tcdrain(), so we also use that
where available, and only use the direct ioctl() method on NDK < 21.
Uwe Hermann [Sat, 28 Dec 2019 22:39:15 +0000 (23:39 +0100)]
windows: Fix a build error.
serialport.c: In function 'get_time':
serialport.c:64:6: warning: implicit declaration of function 'clock_gettime' [-Wimplicit-function-declaration]
if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
^
serialport.c:64:20: error: 'CLOCK_MONOTONIC' undeclared (first use in this function)
if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
^
serialport.c:64:20: note: each undeclared identifier is reported only once for each function it appears in
serialport.c:65:17: error: 'CLOCK_REALTIME' undeclared (first use in this function)
clock_gettime(CLOCK_REALTIME, &ts);
^
serialport.c: At top level:
serialport.c:60:13: warning: 'get_time' defined but not used [-Wunused-function]
static void get_time(struct timeval *time)
^
Martin Jackson [Sat, 9 Sep 2017 22:37:48 +0000 (00:37 +0200)]
windows: wc_to_utf8(): Fix a WCHAR related issue causing crashes.
In wc_to_utf8() in windows.c, the zero terminator is written to an invalid
array index, which results in 2 bytes being zeroed in a random place in the
stack. This sometimes causes a crash when running sp_list_ports() (depending
on string length and compiler optimisation settings).
sizeof(wc_str) returns the size in bytes, so cannot be used directly as an
index into that array, it should be divided by sizeof(WCHAR). Otherwise the
zero terminator index is approximately twice what it should be.
Aurelien Jacobs [Fri, 14 Oct 2016 21:50:08 +0000 (23:50 +0200)]
use readdir() instead of the deprecated readir_r()
readir() is threadsafe on both linux and freebsd anyway.
The rationale behind the readdir_r() deprecation is in the glibc manual:
https://www.gnu.org/software/libc/manual/html_node/Reading_002fClosing-Directory.html
This fixes the following warning with recent glibc:
linux.c: In function ‘list_ports’:
linux.c:197:2: warning: ‘readdir_r’ is deprecated [-Wdeprecated-declarations]
while (!readdir_r(dir, &entry, &result) && result) {
^~~~~