Commit Graph

204 Commits

Author SHA1 Message Date
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
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
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 d81a4dfdc6 unix: Fix calculation of poll() timeout in sp_wait(). 2020-02-02 09:36:52 +00: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 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 e919e2efaa Adjust headers and include ordering for MSVC support. 2020-01-24 05:39:12 +00:00
Martin Ling 060d1d8a73 windows: Loop over WriteFile() if write size exceeds limit.
Fixes #1469.
2020-01-23 03:35:47 +00:00
Martin Ling 39acdc47db Move timing routines to separate file. 2020-01-20 04:33:24 +00:00
Martin Ling bd72614f08 Move commonly used start flag into timeout helpers. 2020-01-20 04:33:23 +00:00
Martin Ling 9d1ca7c855 Move special case for poll() timeout to call site. 2020-01-20 04:31:59 +00:00
Martin Ling 3317d678de Support timing helpers on Windows. 2020-01-20 04:31:59 +00:00
Martin Ling 08eb25f53a More generic solution for limiting per-call timeout. 2020-01-20 04:31:59 +00:00
Martin Ling 32dbe2d298 Move repetitive timeout code into helper functions. 2020-01-20 04:31:59 +00:00
Martin Ling 9a7945af84 Abstract all time handling operations. 2020-01-20 04:31:59 +00:00
Martin Ling abd31fd9f9 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.

Fixes #1078.
2020-01-04 23:00:17 +00:00
Christian Seiler fa106ef155 Use O_CLOEXEC where available
Ensures that the file descriptor is (by default) not passed to
subprocesses spawned by applications using libserialport.

This fixes bug #1051.
2020-01-04 18:21:35 +01:00
Uwe Hermann bd0fb6094f 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)
               ^
2019-12-28 23:39:15 +01:00
Martin Ling 7fb9a7b0a7 Fall back to CLOCK_REALTIME if CLOCK_MONOTONIC not usable.
Sounds like this may be necessary on some older systems.
2019-12-28 22:02:19 +01:00
Martin Ling 192e77492a Use mach_absolute_time() on OSX without clock_gettime().
This should fix #759 for OSX versions below 10.12.
2019-12-28 22:02:19 +01:00
Martin Ling f40ea9d461 Use clock_gettime(CLOCK_MONOTONIC) if available.
Should fix #759 except on OSX versions below 10.12, which don't
have clock_gettime.
2019-12-28 22:02:19 +01:00
Martin Ling 573feabc63 Move all gettimeofday() calls into a wrapper function. 2019-12-28 22:02:19 +01:00
Martin Ling b457865b8f windows: Use architecture-specific size limit for WriteFile calls. 2019-12-28 20:43:22 +01:00
Martin Ling 39df7833f7 windows: Use an adaptively sized buffer for nonblocking writes. 2019-12-28 20:43:22 +01:00
Martin Ling 6bd6a8b520 windows: Await completion of previous write before changing config. 2019-12-28 20:43:22 +01:00
Martin Ling 55ab7e0b6b unix: Fix handling of EAGAIN in sp_nonblocking_write(). 2019-12-28 20:43:22 +01:00
Uwe Hermann 81243567bc Random minor whitespace fixes. 2019-12-28 19:23:30 +01:00
Martin Ling 62ed9f801a windows: Break out helper function for awaiting previous write completion. 2017-09-13 19:27:50 +02:00
Stefan Tauner 95bad38c5b Canonicalize symlinks in portnames
This allows users to supply symlinks created e.g. by udev rules instead
of the actual device names.
2017-09-13 19:27:50 +02:00
Uwe Hermann aee7d69195 Fix ERROR_SEM_TIMEOUT issue on Windows.
The sp_blocking_write() call was incorrectly returning an error upon
ERROR_SEM_TIMEOUT. It now returns 0 instead.
2016-01-27 12:35:45 +01:00
Uwe Hermann 888fb45d66 Fix sp_blocking_read_next() implementation on Windows.
A ReadFile() call needed to check the actual number of bytes read,
instead of assuming all requested bytes were read.
2016-01-22 15:32:00 +01:00
Martin Ling 8062087ab3 Change debug message to "Port not open" when a closed port is used.
Fixes bug #710.
2015-12-29 13:30:30 +01:00
Martin Ling ae2c5825e7 windows: Handle ERROR_IO_PENDING from ReadFile in sp_nonblocking_read().
This fixes bug #707.
2015-12-09 12:43:11 +01:00
Martin Ling 127d8d0ce7 sp_wait: Avoid overflow of timeout parameter to poll(). 2015-10-13 15:04:03 +01:00
Daniel Elstner 1a584c45b0 Build: Include config.h first in all source files 2015-09-13 20:04:31 +02:00
Martin Ling f77bb46df5 Update copyright notices. 2015-05-31 18:26:53 +02:00
Martin Ling 6b8eee0682 Fix possible use of uninitialised variable in poll() setup. 2015-05-30 21:38:03 +02:00
Martin Ling e5c2630eee Add new sp_blocking_read_next() function. 2015-05-27 11:09:26 +02:00
Martin Ling 0188a545c7 windows: Always check and set ReadTotalTimeoutMultiplier.
This is not currently touched, but will be by the upcoming
sp_blocking_read_next() function.
2015-05-27 11:09:25 +02:00
Martin Ling 76222ee578 posix: Make sure blocking operations can't time out without trying. 2015-05-27 11:09:25 +02:00
Martin Ling c3cee38c3b posix: Consistent debug output when blocking operations time out. 2015-05-27 11:09:25 +02:00
Martin Ling 772c586133 posix: Move FD_ZERO() and FD_SET() calls outside loops. 2015-05-27 11:09:25 +02:00
Martin Ling 27911925c1 windows: Always check return value of GetOverlappedResult(). 2015-05-27 11:09:25 +02:00
Martin Ling bbe566fe1c windows: Restructure if/else blocks with unnecessary indentation. 2015-05-27 11:09:25 +02:00