Commit Graph

376 Commits

Author SHA1 Message Date
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
Martin Ling 060d1d8a73 windows: Loop over WriteFile() if write size exceeds limit.
Fixes #1469.
2020-01-23 03:35:47 +00:00
Martin Ling 8073f87d45 Add test program for timing functions. 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
Uwe Hermann d9cc984fe7 Makefile.am: Add example files to the tarball. 2020-01-05 18:19:32 +01:00
Uwe Hermann 44df415480 Doxygen: Fix an issue causing missing #define documentation output.
Searching for documented defines...
  [...]libserialport.h:1624: warning: documentation for unknown define SP_PACKAGE_VERSION_MAJOR found.
  [...]libserialport.h:1627: warning: documentation for unknown define SP_PACKAGE_VERSION_MINOR found.
  [...]libserialport.h:1630: warning: documentation for unknown define SP_PACKAGE_VERSION_MICRO found.
  [...]libserialport.h:1633: warning: documentation for unknown define SP_PACKAGE_VERSION_STRING found.
  [...]libserialport.h:1640: warning: documentation for unknown define SP_LIB_VERSION_CURRENT found.
  [...]libserialport.h:1643: warning: documentation for unknown define SP_LIB_VERSION_REVISION found.
  [...]libserialport.h:1646: warning: documentation for unknown define SP_LIB_VERSION_AGE found.
  [...]libserialport.h:1649: warning: documentation for unknown define SP_LIB_VERSION_STRING found.
2020-01-05 18:19:32 +01:00
Martin Ling 89c3d63e1a Update Doxyfile for doxygen 1.8.16. 2020-01-05 18:19:32 +01:00
Martin Ling ee12a01e52 Release examples as public domain. 2020-01-05 18:19:32 +01:00
Martin Ling ad19d60493 Add some additional formatting hints to Doxygen comments. 2020-01-05 03:28:58 +00:00
Martin Ling 7c8d67efdc Integrate examples into Doxygen. 2020-01-05 03:04:38 +00:00
Martin Ling 8c1a14e658 Add examples directory with two example programs. 2020-01-05 02:04:06 +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
Martin Ling 277f832a6a Define _POSIX_C_SOURCE to 199309L to get clock_gettime(). 2020-01-04 18:27:25 +01:00
Martin Ling 9118f753f4 linux: Fix compile warning on gcc 6+ for readlink() call.
Fixes #1268.
2020-01-04 18:24:00 +01: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 46bdc20c26 configure: Check whether clock_gettime is available. 2019-12-28 22:02:19 +01:00