Commit Graph

401 Commits

Author SHA1 Message Date
André Fonseca fd20b0fc5a 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.
2023-11-02 22:10:36 +01:00
André Fonseca 323881f8a4 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.

https://developer.apple.com/documentation/iokit/kiomasterportdefault

[ gsi: tweak style to match existing code base ]
2023-11-02 22:10:36 +01:00
Karl Palsson 6f9b03e597 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.

Signed-off-by: Karl Palsson <karlp@etactica.com>
2021-07-01 18:03:31 +02:00
Jerry Jacobs 1abec20502 Apply a default baudrate when the OS does not provide one.
Handle the situation when the OS does not provide a baudrate in the code
path which opens the serial port. See bug #1632 and PR #6 for details.

  https://sigrok.org/bugzilla/show_bug.cgi?id=1632
  https://github.com/sigrokproject/libserialport/pull/6

This resolves bug #1632.

[ gsi: adjust comment style, rephrase comment and commit message ]
2021-07-01 17:55:13 +02:00
Gerhard Sittig a06a765515 doc: update IRC reference to Libera.Chat 2021-06-16 21:42:41 +02:00
Ben Gardiner 1b011060df 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.

Signed-off-by: Ben Gardiner <ben.l.gardiner@gmail.com>
2020-09-22 03:10:08 +00:00
Wolfram Sang 086a418145 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".

Fixes bug #1531.

Signed-off-by: Wolfram Sang <wsa@kernel.org>
2020-05-04 22:05:00 +02:00
Leon Varga ffbfc5c76b Open the file descriptor of a serial port on POSIX systems exclusively
This fixes bug #1510.
2020-03-26 23:57:33 +01:00
Uwe Hermann 0a24247de8 examples/send_receive: Fix receive check. 2020-03-26 22:38:41 +01:00
Martin Ling 251890e3b9 Fix use of variable length array in send_receive example, for MSVC. 2020-02-07 14:09:06 +00:00
Martin Ling ba49ee82db Add send/receive example to VS2019 examples solution. 2020-02-07 14:00:35 +00:00
Martin Ling 2913355f7e windows: Ignore fParity flag which is always 0 after GetCommState().
This is a known bug in Windows: https://stackoverflow.com/a/36650872

Reported here: https://github.com/martinling/libserialport/issues/36
2020-02-07 14:00:35 +00:00
Martin Ling 6711e43e9b README: Remove note about old MinGW, it actually works fine. 2020-02-07 14:00:35 +00:00
Martin Ling 75a280a597 configure.ac: remove broken handling for cygwin as $host_os.
I don't think these two mentions of cygwin ever did anything useful.
In fact they stop libserialport being buildable under cygwin.

The first one caused builds on cygwin to try to build windows.c,
which uses the Win32 API, not the POSIX layer provided by cygwin.

The second asserts that enumeration and port metadata are supported
on cygwin, but that isn't the case.

Without these matches for cygwin as $host_os, libserialport builds
and works just fine on old-school cygwin with the original mingw32.

The only reason that MSYS2 worked better is that it uses "msys" as
the $host_os identifier, not cygwin.
2020-02-07 14:00:35 +00:00
Martin Ling 7b0686ed58 Add remaining examples to examples/README. 2020-02-07 13:59:31 +00:00
Martin Ling 4349b1f6a2 Formatting fix for example descriptions. 2020-02-07 13:59:31 +00:00
Martin Ling cd1a7d4361 Add example of sending and receiving data. 2020-02-07 13:59:31 +00:00
Martin Ling d81a4dfdc6 unix: Fix calculation of poll() timeout in sp_wait(). 2020-02-02 09:36:52 +00:00
Uwe Hermann 78c3db9bfb Minor whitespace- and consistency fixes. 2020-01-26 21:21:34 +01:00
Martin Ling 42b3cf3b98 README: Add note on building for MSYS2/Cygwin. 2020-01-26 21:21:34 +01:00
Martin Ling fb58f12ee9 Relax timing test in the other direction.
On my test system under MSYS (Cygwin64), a period of 991ms was
measured after a sleep of 1s.
2020-01-26 21:21:34 +01:00
Martin Ling 2be41b1265 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.

This fixes bug #963.
2020-01-26 21:21:21 +01:00
Martin Ling f6abee5c78 Relax timing test because it's too tight for Windows.
On my test system, a delay of 1016ms was measured after a
sleep of 1000ms.
2020-01-26 21:11:46 +01:00
Martin Ling 6339fa04d6 README: update instructions for building with MSYS2. 2020-01-26 21:11:46 +01:00
Martin Ling 28981e0793 README: update build instructions with VS2019/MSBuild options. 2020-01-26 21:11:46 +01:00
Martin Ling 67b55d10b8 Add Visual Studio projects and solution to build example programs. 2020-01-26 21:11:46 +01:00
Martin Ling d8c4d388e8 Fix use of variable length local array in await_events example.
This prevented building with MSVC.
2020-01-26 21:11:46 +01:00
Martin Ling d6412d2801 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.
2020-01-26 21:11:46 +01:00
Martin Ling 60fc49ceab windows: Fix a warning on size_t to USHORT conversion.
Building with MSVC gave:
warning C4267: '=': conversion from 'size_t' to 'USHORT', possible loss of data

The value here is known to be safe for the sizes involved. Add an
explicit cast to suppress the warning.
2020-01-26 21:11:46 +01:00
Martin Ling 41fc921ce4 windows: Fix a warning on conversion to unsigned int.
The result should be safe because we only use this function on time
differences as part of timeout calculations, not on absolute times.

Add an explicit cast to suppress the warning.
2020-01-26 21:11:46 +01:00
Martin Ling 528e8c0002 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.
2020-01-26 21:11:37 +01:00
Martin Ling 988ace6c9f 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.
2020-01-24 05:39:16 +00:00
Martin Ling bf40b1cea9 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.
2020-01-24 05:39:16 +00:00
Martin Ling e47c7dcbff 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.
2020-01-24 05:39:16 +00:00
Martin Ling 2149db9e93 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.
2020-01-24 05:39:16 +00:00
Martin Ling 4651adb4f6 Replace some usages of int with size_t to fix overflow warnings.
On MSVC, these gave the following warning:

warning C4267: '=': conversion from 'size_t' to 'int', possible loss of data
2020-01-24 05:39:16 +00:00
Martin Ling 75f468923b Add project files for Visual Studio 2019. 2020-01-24 05:39:16 +00:00
Martin Ling e919e2efaa Adjust headers and include ordering for MSVC support. 2020-01-24 05:39:12 +00:00
Martin Ling a20ed2965b Add example of waiting for events. 2020-01-23 04:11:45 +00:00
Martin Ling 4720053160 Add an example of proper error handling. 2020-01-23 04:10:00 +00:00
Martin Ling 6dba844779 Add some more narrative docs on the configuration API. 2020-01-23 03:56:41 +00:00
Martin Ling 9ddf08588d Add example of how to configure a port. 2020-01-23 03:56:34 +00:00
Martin Ling 0838c979cc 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.
2020-01-23 03:35:47 +00:00
Martin Ling f6e32b2dfa Use a static header file, not dependent on autoconf. 2020-01-23 03:35:47 +00:00
Martin Ling 6aaf844863 windows: wc_to_utf8: use some clearer variable names. 2020-01-23 03:35:47 +00:00
Martin Ling fdbb55ae1e windows: Don't try to include <unistd.h>.
This should enable compatibility with MSVC.
2020-01-23 03:35:47 +00:00
Martin Ling a9900f8b64 windows: wc_to_utf8: Eliminate variable-length array.
This should enable compatibility with MSVC.
2020-01-23 03:35:47 +00:00
Martin Ling e9d78d82c4 windows: Use a fixed worst-case WRITEFILE_MAX_SIZE.
This saves needing to include and isolate the DDK headers.
2020-01-23 03:35:47 +00:00
Martin Ling 8488868187 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.
2020-01-23 03:35:47 +00:00
Martin Ling c79e0ac8ef windows: Handle registry lookup failures correctly.
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.
2020-01-23 03:35:47 +00:00