54 Commits

Author SHA1 Message Date
Linus Torvalds
8a34d822ff Merge remote-tracking branch 'jef/master' into Subsurface-branch
Rough merge of upstream libdivecomputer.

This is mainly about making the new iostream code upstream, although we
don't actually use it.

It abstracts out the the old serial and usbhid code, but we end up still
using our own 'custom_io' interface because the iostream code doesn't do
it right.

* jef/master:
  Correctly determine git SHA if libdivecomputer is a git submodule
  Don't accept a NULL pointer as parameter
  Add support for semi-closed circuit diving
  Detect dives with invalid profile data
  Implement the serial communication functions as no-ops
  Move the socket code to a common file
  Add support for a custom I/O implementation
  Port the USB HID code to the new I/O interface
  Port the bluetooth code to the new I/O interface
  Port the IrDA code to the new I/O interface
  Port the serial code to the new I/O interface
  Add a new abstract I/O interface
  Post release version bump to 0.7.0
2017-12-12 13:59:29 -08:00
Linus Torvalds
54f6bff929 Merge tag 'v0.6.0' of git://git.libdivecomputer.org/libdivecomputer into Subsurface-branch
Sync up with upstream cersion 0.6.0.

Annoying merge, mainly because a lof of the changes Jef had done are
actually changes that came from our Subsurface branch, but in a
different form, because Jef doesn't actually take patches directly from
us.

Why? I don't know.

* tag 'v0.6.0' of git://git.libdivecomputer.org/libdivecomputer:
  Release version 0.6.0
  Fix some potential buffer overflows
  Fix some casts with constant pointers
  Enable some useful compiler warnings by default
  Generate html documentation from the manpages
  Fix the decoding of the ndl/deco information
  Decode the firmware version for the iDive series
  Add support for the Suunto Eon Core
  Locate the most recent dive
  Add EON Steel time sync capability
  Improve the progress events
  Detect the model number using the hardware type
  Shearwater: add support for remaining gas time
  Shearwater: extract tank sensor data for log version 7
  Shearwater: extract log version from header
2017-12-04 10:18:23 -08:00
Jef Driesen
42f8e012b7 Don't accept a NULL pointer as parameter
Immediately return an error instead of trying to pass the NULL pointer
to the underlying open system call.
2017-12-01 15:03:17 +01:00
Jef Driesen
7cd1656d1d Port the serial code to the new I/O interface 2017-11-25 10:26:49 +01:00
Jef Driesen
68380b2ec0 Fix some casts with constant pointers
Casting away the const qualifier generates a compiler warning which can
easily be avoided by preserving the const qualifier.
2017-11-24 23:45:11 +01:00
Linus Torvalds
208807180a Update the operations in 'custom_io' to consistent calling conventions
The serial ops used a legacy calling convention that passed in just the
pointer to the userdata pointer (ie the first argument to the functions
was "void **userdata").

That's actually very inconvenient, because the custom IO data can not
only contain other interesting information that was filled in by the
custom IO provider, it also made it harder to chain these things
together, as exemplified by the core to emulate serial over the packet
interface in the subsurface bluetooth code.

This also adds the 'dc_context_t' field that is passed to the packet
routine open.  That can allow the open routine to override the
'custom_io' details of the context at open time (to allow nested
custom_io operation).

Note that callers of the open function need to be aware that the
'custom_io' can be changed by the act of opening a custom_io, and the
value shouldn't be cached in some local variable.

Finally, this adds a new user-supplied opaque pointer

	dc_user_device_t *user_device;

to the custom_io descriptor.

The 'user_device' data is filled in when registering the custom_io with
data that the custom IO open() routines can use.  This is different from
the existing 'userdata' in that the 'user_device' is filled in before
dc_open_device() is called (and "open" can then use it to limit what
kinds of devices it looks for, for example).

In contrast, the existing 'userdata' field is filled in by the
"xyz_open()" routines, and contains the data necessary for the IO
itself.

The SSRF_CUSTOM_IO define is updated to v2 to indicate the new
interfaces.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-06-27 13:38:45 -07:00
Linus Torvalds
b04f393b97 Merge git://git.libdivecomputer.org/libdivecomputer into Subsurface-branch
Merge upstream libdivecomputer changes from Jef Driesen.

No major changes and no conflicts except for a trivial one where Jef had
marked a couple of private functions 'static' and our versions had
extended the argument lists of those functions.

This just updates to the current upstream state of libdivecomputer,
where the bulk of the changes come mainly from the new ringbuffer helper
code, but there are various other small misc fixes and cleanups.

* git://git.libdivecomputer.org/libdivecomputer:
  Disable the deco events
  Replace the deco events with a deco sample
  Report errors from the close function
  Mark the private function as static
  Fix a bug in the tank pressure samples
  Disable freedive mode for the Uwatec Aladin Tec 2G
  Mark the private function as static
  Fix some compiler warnings
  Fix some more null pointer dereferences
  Use a more efficient download algorithm
  Use the new ringbuffer stream
  Add a common ringbuffer reading algorithm
  Improve the robustness of the IrDA I/O code
  Fix a few null pointer dereferences
  Fix the number of gas mixes
  Always use the sample timestamp as the base value
2017-04-12 08:24:04 -07:00
Jef Driesen
24cbff9a9f Fix a few null pointer dereferences 2017-02-16 12:00:41 +01:00
Anton Lundin
19b560d9b2 Fix logging of Custom Write
This way we actually trace print the written buffer, even if the caller
doesn't care about how much data he/she actually wrote.

Signed-off-by: Anton Lundin <glance@acc.umu.se>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2017-01-08 11:54:49 -08:00
Anton Lundin
94e7a77d01 Re-implement custom serial in a new way
This re-implements the custom serial concept in a new way. This way
doesn't touch any of the backend code, it just introduces a optional
redirection layer in the existing serial backends.

This implementation supports more serial operations to, so we can
support more backends this way.

Hooking into the existing serial backends might look ugly but its
probably the best way to make sure this patch conflicts as little as
possible with upstream.

Signed-off-by: Anton Lundin <glance@acc.umu.se>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-09-17 14:09:37 -07:00
Linus Torvalds
f9db4ca97c Merge git://git.libdivecomputer.org/libdivecomputer into Subsurface-branch
This is a rough merge of the upstream libdivecomputer changes.

I say "rough", because this disables the custom serial code as it
clashes very badly with Jef's new dc_serial_t abstraction.

Anton Lundin has patches on top of this to re-introduce the custom code
in a way that integrates better with the upstream libdivecomputer state.

* git://git.libdivecomputer.org/libdivecomputer: (42 commits)
  Add support for the Sherwood Vision.
  Fix the decoding of the maximum depth.
  Improve the default layout detection.
  Add a warning for unsupported devices.
  Fix the temperature for the Tusa Zen Air.
  Add support for the Aqualung i550T.
  Use the new settings field for the salinity.
  Fix the parsing of freedives.
  Detect the gauge and freedive mode correctly.
  Add the salinity field for the Aladin Tec.
  Add support for the Scubapro Mantis 2.
  Fix the decoding of the dive time.
  Add support for the Scubapro Mantis.
  Fix the Aeris 500AI serial number.
  Add the serial number encoding to the layout.
  Add salinity and timezone fields to Aladin Tec 2G
  Add NDL and RBT for the ATOM31 and I450T
  Add support for the new extended hardware descriptor.
  Update the OSTC device descriptors.
  Add a workaround for an OSTC4 firmware bug.
  ...
2016-09-17 14:08:32 -07:00
Jef Driesen
f8417037c6 Convert operating system errors.
Errors reported by system calls are now converted to the corresponding
libdivecomputer status code. This results in a more descriptive and
meaningfull return value.
2016-05-10 11:52:32 +02:00
Jef Driesen
72bd82f0a3 Add support for space and mark parity. 2016-05-10 11:35:03 +02:00
Jef Driesen
84563c6303 Refactor the internal serial and IrDA api.
The low level serial and IrDA functions are modified to:

 - Use the libdivecomputer namespace prefix.

 - Return a more detailed status code instead of the zero on success and
   negative on error return value. This will allow to return more
   fine-grained error codes.

 - The read and write functions have an additional output parameter to
   return the actual number of bytes transferred. Since these functions
   are not atomic, some data might still be transferred successfully if
   an error occurs.

The dive computer backends are updated to use the new api.
2016-05-10 11:34:57 +02:00
Dirk Hohndel
7c33c633fb Merge branch 'master' into Subsurface-branch
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2016-02-27 22:17:36 +01:00
Jef Driesen
ade8619ea6 Don't assume malloc sets errno to ENOMEM.
Although many implementations (including glibc) set errno on failure,
this is not required by the C standard.
2016-01-26 20:32:22 +01:00
Jef Driesen
f1c0249053 Simplify the error handling in the close function.
When the close function returns, all resources should be freed,
regardless of whether an error has occured or not. The error code is
purely informative.

However, in order to return the first error code, which is usually the
most interesting one, the current implementation is unnecessary
complicated. If an error occurs, there is no need to exit immediately.
Simply store the error code unless there is already a previous one, and
then continue.
2016-01-05 20:40:21 +01:00
Jef Driesen
18d4d5dcc0 Switch to goto style error handling.
With the goto error handling, all cleanup code can be moved to a central
location and no longer needs to be duplicated multiple times.
2016-01-05 20:40:21 +01:00
Dirk Hohndel
e227a4c5a7 Merge branch 'master' into Subsurface-branch 2015-12-14 09:33:53 -08:00
Jef Driesen
d516376ce7 Improve the support for pseudo terminals.
Apparantly Fedora applies a custom patch to glibc's tcsetattr()
function, which adds an extra check to verify the PARENB/CREAD/CSIZE
bits in the termios c_cflag field.

However, in commit 197b9f09421111e03588c94d55a72aa6ec624c63 we already
discovered that for pty's, some of the termios settings make no sense at
all, and therefore the Linux kernel always does:

    tty->termios.c_cflag &= ~(CSIZE | PARENB);
    tty->termios.c_cflag |= (CS8 | CREAD);

Thus, instead of ignoring such nonsense termios settings, the kernel
changes the termios structure to reflect what pty's actually do. The
consequence is that these settings will not stick, and cause the extra
check in the Fedora specific patch to fail.

To workaround this problem, we ignore the error when building
libdivecomputer with pty support enabled.
2015-12-10 19:41:41 +01:00
Dirk Hohndel
fb15ef18f6 Don't report errors if we can't set serial mode
That's just silly and breaks using the simulator.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2015-08-26 10:47:33 -07:00
Jef Driesen
60665ec633 Disable exclusive access mode during close.
When closing the slave side of a pseudo terminal, the exclusive access
mode will persists on the master side. The result is that re-opening the
slave side will fail with EBUSY, unless the process has root priviliges.
To workaround this problem, we already introduced an option that enables
better compatibility with pseudo terminals. See commmit
fab606b00a44ea2114a4029ad09b70c66c3049f7 for details.

In my development environment, I always have this option enabled. But
occasionally I also need to test release builds. And then I usually end
up with inaccessible pty's again, because the pty support is disabled by
default for release build.

This problem can easily be avoided by disabling the exclusive access
mode, just before closing the file descriptor.
2014-11-07 09:07:41 +01:00
Jef Driesen
197b9f0942 Remove the extra check of the termios structure.
The extra memcmp check after the tcsetattr call is intended to verify
whether all the changes to the termios structure have been applied
correctly.

But for pty's, some of the termios settings make no sense at all, and
therefore the Linux kernel always does:

    tty->termios.c_cflag &= ~(CSIZE | PARENB);
    tty->termios.c_cflag |= (CS8 | CREAD);

Thus, instead of ignoring such nonsense termios settings, the kernel
changes the termios structure to reflect what pty's actually do. The
consequence is that these settings will not stick, and cause the memcmp
check to fail.

An example where this affects libdivecomputer, are the two backends that
require odd or even parity (e.g. vyper and iconhd). Here, the kernel
will clear the PARENB flag, and thus cause the memcmp check to fail.

Since this check appears to causes more trouble than it solves, let's
just remove it completely!
2014-05-28 11:11:35 +02:00
Jef Driesen
5f1a18653d Initialize the termios structure.
The previous commit exposed another issue. The termios structure may
contain padding bytes. Because the content of those padding bytes is
unspecified, they may contain some random data, which causes the memcmp
to fail.

Explicitly initializing the termios structure with memset, will also set
the padding bytes to zero.
2014-05-28 11:10:23 +02:00
Venkatesh Shukla
3001dda198 Minor error in serial_configure
Due to a minor mistake, only the first byte was being checked for
equality. Fixed it by changing position of parenthesis.

Signed-off-by: Venkatesh Shukla <venkatesh.shukla.eee11@iitbhu.ac.in>
2014-05-20 21:48:57 +02:00
Anton Lundin
6d4893a146 Fix building for Android
This makes libdivecomputer build via Android NDK. Its currently unusable
due to the fact that Android usually doesn't provide any kernel serial
drivers.

Signed-off-by: Anton Lundin <glance@acc.umu.se>
2014-03-28 12:38:24 +01:00
Jef Driesen
bfeab10515 Add support for modifying the receive latency.
Currently this isn't used or needed anywhere, but the research has been
done, and it would be silly to drop the knowledge. We may need it in the
future.
2014-03-28 09:39:26 +01:00
Florian Klink
fc008312b5 Remove trailing whitespaces
Most of them appear in the disclaimers, but some are in the code as
well.
2014-03-19 09:16:07 +01:00
Jef Driesen
9ce8888e37 Log the device name of the serial port. 2013-08-05 22:42:24 +02:00
Jef Driesen
d44053a994 Add internal support for enumerating serial ports.
For the time being, the serial port enumeration code is of very limited
use. It's not used anywhere in the library, and as an internal api it's
also not available to applications. It serves mainly as a reference
implementation for future use.
2013-08-05 22:40:07 +02:00
Jef Driesen
ea3f833d8d Add additional messages for diagnostic purposes. 2012-09-15 13:54:48 +02:00
Jef Driesen
fab606b00a Add an option to enable support for pseudo terminals.
Pseudo terminals are very convenient for testing purposes, but they are
not fully compatible with real serial (or even usb-serial) hardware.
With the new option, some workarounds can be enabled to hide the
differences and increase compatibility. Although these workarounds
shouldn't cause any problems in production builds, the advise is to
disable this feature.

A few ioctl's are not supported for pseudo terminals. They fail with
EINVAL (Linux) or ENOTTY (Mac OS X). Since these specific error codes
should not occur under normal conditions, they are simply ignored when
pseudo terminal support is enabled.

The TIOCEXCL ioctl (exclusive access) is also problematic. The TIOCEXCL
setting is shared between the master and slave side of the pty. When the
setting is applied on the slave side, it persists for as long as the
master side remains open. The result is that re-opening the slave side
will fail with EBUSY, unless the process has root priviliges. Since this
is very inconvenient, the TIOCEXCL setting is not used when pseudo
terminal support is enabled.
2012-08-28 23:30:43 +02:00
Jef Driesen
6cb4bc6a06 Enable exclusive access mode.
With exclusive access mode, no further open() operations on the terminal
are permitted, except for a process with root priviliges. Non-root
processes will fail with EBUSY. This change will prevent other processes
from accidentally messing up the communication. It also makes the
behaviour similar to Windows, where serial ports are always opened with
exclusive access.
2012-08-28 23:30:26 +02:00
Jef Driesen
30feb3b3ed Remove the obsolete error functions. 2012-08-27 23:08:38 +02:00
Jef Driesen
38c7b022cd Integrate the context object in the serial code. 2012-08-27 23:08:38 +02:00
Jef Driesen
3df5cb09d7 Require a serial handle for the sleep function. 2012-08-27 23:02:44 +02:00
Jef Driesen
83f742080b Remove some unnecessary helper functions. 2012-08-27 23:02:44 +02:00
Jef Driesen
2095a2d10e Merge branch 'release-0.1'
Integrate the bugfixes from the stable branch.
2012-08-27 22:58:52 +02:00
Pascal Manchon
0e1809aac9 Fix some ioctl calls on 64bit Mac OS X.
On Mac OS X (and probably the other BSD's too), the ioctl() syscall
takes an 'unsigned long' integer as the request parameter. On 64bit
systems this is a 64bit type, while on 32bit systems it's a 32bit type.

Some of the request constants are defined as 32 bit negative numbers.
Casting it to a 64bit value will perform a sign extension operation to
preserve the negative value. Because this results in a different request
code when interpreted as an unsigned integer, the ioctl() call fails
with ENOTTY. For example TIOCMBIS is defined as 0x8004746c and becomes
0xffffffff8004746 after the sign extension.

Linux 64bit is unaffected by this problem. None of the request constants
has the sign bit set, and thus the sign extension has no effect. For
example TIOCMBIS is defined as 0x5416.

By using an unsigned integer type, the sign extension can be avoided. We
use the 'unsigned long' type in case one of the request constants
happens to be defined as a 64bit number.
2012-07-08 13:42:13 +02:00
Jef Driesen
e65025b501 Separate private and public headers.
The public header files are moved to a new subdirectory, to separate
the definition of the public interface from the actual implementation.
Using an identical directory layout as the final installation has the
advantage that the example code can be build outside the project tree
without any modifications to the #include statements.
2012-06-21 21:39:24 +02:00
Jef Driesen
b6d24e72e2 Add support for half-duplex emulation.
When using half-duplex communication (e.g. only a single wire for both
Tx and Rx) a data packet needs to be transmitted entirely before
attempting to switch into receiving mode.

For legacy serial hardware, the tcdrain() probably works as advertised,
and waits until the data has been transmitted. However for common
usb-serial converters, the hardware doesn't provide any feedback to the
driver, and the tcdrain() function can only wait until the data has been
transmitted to the usb-serial chip. There is no guarantee that the data
has actually been transmitted by the usb-serial chip.

As a workaround, we wait at least the minimum amount of time required to
transmit the data packet over a serial line, taking into account the
current configuration.
2012-04-10 21:31:31 +02:00
Jef Driesen
3c07a3017f Integrate the tcdrain() call into the write function. 2012-04-10 21:30:39 +02:00
Jef Driesen
088ea6d054 Add support for non standard baudrates. 2011-08-12 22:47:07 +02:00
Jef Driesen
c1602ef506 Simplify the timeout calculation. 2010-11-07 23:22:11 +01:00
Jef Driesen
72ae3b9a86 Add a function to query the state of the serial lines. 2010-10-04 23:08:52 +02:00
Jef Driesen
887d744e6d Use a more consistent typedef in the serial and irda backends. 2010-10-04 23:08:39 +02:00
Jef Driesen
fe511a774c Recalculate the timeout using the correct variable. 2010-03-23 13:41:13 +00:00
Jef Driesen
c55bbede1d Fixed some warnings related to missing initializers. 2009-01-06 21:44:40 +00:00
Jef Driesen
5cde8bdb52 Declare functions with no arguments as void. 2009-01-06 13:09:19 +00:00
Jef Driesen
596af5a17b Add LGPL license headers. 2008-10-05 20:14:18 +00:00