1262 Commits

Author SHA1 Message Date
Jef Driesen
29f781f803 Fix a typo in the comments 2018-04-17 08:36:26 +02:00
Jef Driesen
56d194d377 Use a NULL pointer for the no-op implementation
For most I/O stream implementations the serial communication specific
functions are meaningless. Implementing them as no-ops allows the dive
computer backends the call the I/O stream functions unconditionally.

However, implementing the no-op with a dummy function returning
DC_STATUS_SUCCESS, does not only add some (small) overhead at runtime,
but also requires many such functions. This is inconvenient and the same
result can easily be obtained by using a NULL pointer instead.

The consequence is that the logic is reversed now. To obtain the
previous behaviour of returning the DC_STATUS_UNSUPPORTED error code
again, you'll need to implement a dummy function. But that's fine
because it's the less common case.
2018-04-17 08:18:35 +02:00
Jef Driesen
1908394af4 Add some extra logging 2018-04-12 10:07:53 +02:00
Jef Driesen
945898f8fd Always initialize the output parameters
I/O functions with output parameters, should always initialize those
output parameters, even when an error is returned. This prevents the
(accidental) use of uninitialized variables, whenever the caller forgets
to check the return code.

As a nice side effect, the use of a local variable guarantees that the
underlying I/O implementation will always receive a valid pointer.
2018-04-12 10:07:30 +02:00
Jef Driesen
8957d61f4e Add support for the Scubapro G2 Console
The G2 Console is identical to the G2, except for the new USB PID.
2018-04-06 13:15:11 +02:00
Jef Driesen
3d394c9262 Don't use the USB VID/PID for opening the device
When two or more identical (or very similar) dive computers are
connected, the USB VID/PID can be ambiguous. That's because the VID/PID
identifies the type of the USB device, and not the individual device.
But each USB HID device descriptor returned by the device discovery
represents a single connected device, and thus guarantees to open the
correct USB device.

To obtain the same behaviour as before, an application can simply open
the first discovered device.
2018-04-03 22:02:15 +02:00
Jef Driesen
9477791bfe Use a reference counted USB session
Replace the global USB library context with a reference counted session
to manage the lifetime of the USB library context. For the libusb based
implementation, this is actually a much better match for the underlying
libusb api, and allows to eliminate the global state. For the hidapi
based implementation, the global state is unavoidable because the hidapi
doesn't support multiple sessions. Therefore we use a singleton session.
2018-04-03 21:58:26 +02:00
Jef Driesen
8f7abc5a2d Add BLE support for the Shearwater devices
The main difference with the serial communication is that the BLE
communication transmits each SLIP encoded data packet as one or more BLE
data packets. The BLE packets have an extra two byte header with the
total number of packets and the current packet number.
2018-04-03 21:52:20 +02:00
Jef Driesen
0978f8c0fa Add BLE support for the HW OSTC3 devices
The main difference with the serial communication is that the BLE
communication uses data packets (with a maximum size of 20 bytes)
instead of a continuous data stream.
2018-04-03 21:52:20 +02:00
Jef Driesen
3dcf93e26e Add BLE support for the Scubapro G2 devices
The main difference with the USB HID communication is that the BLE data
packets have a variable size and are no longer padded to the full 32
(Tx) or 64 (Rx) bytes.
2018-04-03 21:52:20 +02:00
Jef Driesen
afff8b450f Add BLE support for the Suunto Eon Steel devices
The main difference with the USB HID communication is that the BLE data
stream is encoded using HDLC framing with a 32 bit CRC checksum. Due to
this encoding, the data packets can no longer be processed one by one
(as is done for the USB HID packets). The entire HDLC encoded stream
needs to be received before it can be processed. This requires some
additional buffering.
2018-04-03 21:52:20 +02:00
Jef Driesen
44eba5515c Move the I/O implementations to the public api
The I/O implementations need to be exposed in the public api, otherwise
applications won't be able to use them!
2018-04-03 21:44:08 +02:00
Jef Driesen
ef2402eff5 Integrate the new I/O interface in the public api
Currently the dive computer backends are responsible for opening (and
closing) the underlying I/O stream internally. The consequence is that
each backend is hardwired to a specific transport type (e.g. serial,
irda or usbhid). In order to remove this dependency and support more
than one transport type in the same backend, the opening (and closing)
of the I/O stream is moved to the application.

The dc_device_open() function is modified to accept a pointer to the I/O
stream, instead of a string with the device node (which only makes sense
for serial communication). The dive computer backends only depend on the
common I/O interface.
2018-04-03 21:11:06 +02:00
Jef Driesen
30ea13b36b Remove the obsolete transport function
With the support for multiple transports per device, the
dc_descriptor_get_transport() function became obsolete because it does
support only a single transport type. Applications should use the new
dc_descriptor_get_transports() function instead.
2018-04-03 21:11:06 +02:00
Jef Driesen
46608ce898 Always enable all device descriptors
With the support for multiple transports per device and the possibility
to use custom I/O implementations, libdivecomputer no longer knows which
devices are actually supported. Hence libdivecomputer needs to always
report all the devices it knows about, and it's up to the application to
filter out entries for which there is no suitable transport available
(either built-in or custom).
2018-04-03 21:11:06 +02:00
Jef Driesen
aee59a33be Add support for querying the available built-in transports
Because the list of supported built-in transports depends on the
availability of external libraries (libusb, hidapi) and the operating
system, the application needs some mechanism to retrieve this
information at runtime. Therefore, a new dc_context_get_transports()
function is added, which returns a bitmask with all the available
built-in transports.
2018-04-03 21:11:06 +02:00
Jef Driesen
eed993fd16 Add support for multiple transports per device
Several dive computers support multiple transports. For example the
Suunto Eon Steel supports both USB HID and BLE. All devices using
bluetooth classic communication support both the native bluetooth
transport and the legacy serial port emulation.

To support this feature, the values of the dc_transport_t type are
changed into bitmasks, and the dc_descriptor_t struct is extended with a
bitfield with all the supported transports.
2018-04-03 21:11:06 +02:00
Jef Driesen
3230387fff Add the transport type to the I/O stream
Add a function to query the underlying transport type. This allows the
dive computer backends to implement transport specific behaviour where
necessary.

For the built-in I/O implementations, the transport type is obviously
always hardcoded, but for a custom I/O implementation the application
needs to provide the correct type. Hence the transport type can't be
hardcoded in the vtable and needs to be passed as a parameter.
2018-04-03 21:11:06 +02:00
Jef Driesen
7d48cffc0d Simplify the packet send/receive code
Re-organize the packet sending and receiving code to eliminate the need
for different code paths for the init command and all other commands.
2018-04-03 21:10:04 +02:00
Jef Driesen
43f196b804 Remove the code to purge the input buffer
Trying to purge the input buffer by reading and discarding data packets,
results in an annoying and confusing error message if no data packet is
received. To avoid this error, the functionality should be integrated in
the USB HID code, either automatically during initialization or by
implementing the purge function.

But since there seems to be no evidence that this is actually necessary,
let's remove this code.
2018-04-03 21:10:04 +02:00
Jef Driesen
5344f3926a Abort with an error if the buffer is too small
Silently truncating the data packet if the buffer is too small will
result in a corrupt data stream.
2018-04-03 21:10:04 +02:00
Jef Driesen
38c3f289b5 Improve the error reporting
The error codes from the I/O layer are now correctly returned to the
upper layers.
2018-04-03 21:10:04 +02:00
Jef Driesen
755b52febc Improve the fingerprint matching
Check the fingerprint before downloading the dive. If a match is found,
this avoids some unnecessary communication and thus makes the download a
little bit faster.
2018-04-03 21:10:04 +02:00
Jef Driesen
01ccb7ce4b Fix a few memory leaks
The file list isn't freed when an error occurs, and the strings returned
from the lookup_enum function are dynamically allocated and thus should
be freed as well.
2018-04-03 21:10:04 +02:00
Jef Driesen
96bac1de13 Fix some compiler warnings
The descriptor strings are dynamically allocated and owned by the
struct. The const qualifiers are a bit misleading here, and result in
warnings when trying to free the pointers again.
2018-04-03 21:10:04 +02:00
Jef Driesen
ea2272d4b0 Fix a build error for msvc
The msvc build needs the platform header for the non-standard _snprintf
function.
2018-04-03 21:06:48 +02:00
Jef Driesen
2c7d1fe39f Use 64bit arithmetic to avoid overflow
The multiplication is evaluated using 32bit arithmetic, and then stored
in a 64bit integer. The 32bit integer overflow can be avoided by casting
to a 64bit type first.
2018-04-03 21:06:48 +02:00
Jef Driesen
0026fbd289 Re-write the slip encoding and decoding
The write function is modified to always fill the buffer completely
before sending out the data. For escaped characters, the previous
implementation needed to append two bytes at once. Thus, if there was
only space left for a single byte, the buffer got flushed early with one
byte still unused. This can be avoid by appending one byte at a time.

The read function is modified to use a simple state machine with only a
single read call. This is mainly preparation to support reading and
processing larger data packets instead of just single bytes.
2018-04-03 21:06:48 +02:00
Jef Driesen
b62f160dd5 Workaround for an OSTC4 issue
I received a bug report complaining that the most recent dives did not
get downloaded. It turns out that the internal dive number in the
logbook entries got reset back to zero somehow:

Logbook   0: empty
...
Logbook 209: empty
Logbook 210: 1
Logbook 211: 2
...
Logbook 235: 26
Logbook 236: 27
Logbook 237: 0
Logbook 238: 1
...
Logbook 254: 17
Logbook 255: 18

This confuses the logic to locate the most recent dive. Because that
logic assumes that the entry with the highest internal dive number is
always the most recent dive, it finds logbook entry #236 instead of the
correct entry #255. Now, when processing the logbook entries backwards,
it stops at those empty entries, and thus never reaches then newest
entries #237 to #255.

The workaround is based on the fact that the OSTC4, unlike the other
hwos based models, already re-orders the logbook entries and always
sends the most recent logbook entry last. So we can ignore the dive
number and simply use the last non-empty entry.
2018-04-03 21:06:48 +02:00
Linus Torvalds
c15d422f75 Suunto EON Steel: set date too when doing device timesync
I'd never noticed this before, since my date had always been already set
correctly, but the timesync with the EON Steel only set the time, not
the date.

The fix is trivial, since the code already filled in the datetime data,
it just didn't do the SET_DATE command.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2018-04-03 21:01:39 +02:00
Jef Driesen
5207fcd86b Add support for the dive mode
The Dive Rite NiTek Q supports OC and CC samples. For reporting the dive
mode, any dive containing at least one CC sample is considered to be a
CCR dive.
2018-03-09 21:35:51 +01:00
Jef Driesen
38ff1f75dd Remove the half-duplex emulation from the I/O api
Now that the half-duplex emulation code isn't used anymore, it can be
removed from the I/O stream api.
2018-03-05 09:08:21 +01:00
Jef Driesen
4897a8351b Handle the half-duplex emulation in the vyper2 backend
The half-duplex emulation is basically a workaround for a timing problem
in the vyper2 backend. Since no other dive computer backend uses or
needs this feature, it makes no sense to support this at the I/O layer.
It can be implemented directly in the vyper2 backend instead.
2018-03-05 09:08:21 +01:00
Jef Driesen
ee31f24cfb Use the new timer for the timestamps in the logging
Replace the platform specific code with the new timers.
2018-03-05 09:08:21 +01:00
Jef Driesen
eb4c150024 Use the new timer for the timeout calculations
Replace the gettimeofday() based implementation with the new monotonic
timers. This makes the implementation more robust against unexpected
adjustments of the clock.
2018-03-05 09:08:21 +01:00
Jef Driesen
a8adb16c0d Add a high resolution timer module
The new timer module provides an cross-platform interface for a high
resolution monotonic clock. The timestamps are always relative to the
creation of the timer and their unit is one microseconds.

The timers can be used for logging, measuring elapsed time and
implementing timeouts.
2018-03-05 08:44:14 +01:00
Jef Driesen
becaf02ab6 Add functions for converting bluetooth addresses 2018-03-04 20:49:49 +01:00
Jef Driesen
efd47cd9a1 Add support for the OSTC 2 TR
The OSTC 2 TR add supports for one or more tank pressure transmitters,
and the active transmitter is linked to the active gas mix.
2018-02-17 10:22:44 +01:00
Jef Driesen
0ac15dffd3 Add a workaround for invalid logbook begin pointers
Unfortunately there are several devices where an invalid logbook begin
pointer occurs relative frequently. Typical examples are the Oceanic VT
4.1 and the Sherwood Wisdom 2. In such cases, the strict validation of
the pointer causes the download to fail, without being able to download
any dives at all.

Since the begin pointer is only needed to detect the oldest logbook
entry, we can fall back to downloading the entire logbook ringbuffer. If
we're lucky (and we usually are), we can detect the oldest entry by
inspecting the logbook entries once they are downloaded (e.g. presence
of uninitialized entries) and then the download will finish succesfully.
In the worst case scenario, we'll be able to download at least some
dives before hitting another error.
2018-02-06 13:00:57 +01:00
Jef Driesen
05f5522210 Let the ringbuffer function handle a full ringbuffer
The ringbuffer_distance() function has a parameter to specify whether a
ringbuffer with identical begin/end pointers should be considered an
empty or a full ringbuffer. Hence there is no need to handle the case of
a full ringbuffer manually.
2018-02-06 13:00:34 +01:00
Jef Driesen
9117b593fc Suppress the warning if no O2 sensors are present
The warning about disabling the O2 sensors due to factory default
calibration values, applies only if there is at least one calibrated O2
sensor present.

This has no effect on the calibration bits, because those are already
zero if there are no calibrated O2 sensors present.
2018-02-05 19:58:22 +01:00
Jef Driesen
85eef19f8f Integrate the connect step into the open function
There is no need to expose the two step connection setup of the
underlying socket interface in the public api. Doing so may complicate
the implementation on platforms where the native api is not based on the
socket interface (e.g. Mac OS X).

Note that the function to connect based on the IrDA service name is
removed. It's not used anywhere in libdivecomputer and since IrDA is an
outdated technology nowadays, it's unlikely we'll need it in the future.
2018-02-04 21:21:42 +01:00
Jef Driesen
0ce3bd3e6d Implement some filter functions 2018-02-04 21:21:42 +01:00
Jef Driesen
30e4060817 Add suport for applying a filter function
The device descriptors are extended with a filter function. During the
device discovery, this filter function is used to return only devices
that match a known dive computer.

The filtering is optional, and can be disabled by passing a NULL pointer
for the device descriptor when creating the iterator with one of the
dc_xxx_iterator_new() functions.
2018-02-04 21:21:42 +01:00
Jef Driesen
ca91500ed5 Re-write the device discovery using the iterator api
Replacing the callback based interface with an iterator based interface,
results in a more extensible abstraction with a common interface for
each of the built-in I/O implementations (serial, usbhid, irda and
bluetooth).
2018-02-04 21:21:16 +01:00
Jef Driesen
296fad2d9d Cleanup the iterator internals 2018-01-30 22:00:41 +01:00
Jef Driesen
f992d201ad Simplify the conditional compilation
Move the global variables inside an existing conditionally compiled
block, to remove some of the #ifdef's.
2018-01-30 21:21:21 +01:00
Jef Driesen
90eed3c790 Use SDP to auto-detect the rfcomm port number
When the port number is set to zero (which is an invalid value), detect
the port number automatically. On Windows, we can simply supply the UUID
of the serial port service, and the Windows api will take care of the
discovery. On Linux (bluez), the SDP discovery needs to be performed
manually to retrieve the port number.
2018-01-30 21:21:21 +01:00
Jef Driesen
6d5ac37f99 Use the actual IrDA device names 2018-01-30 21:21:21 +01:00
Jef Driesen
9e169c9a3f Use the correct data type for the temperature
Temperatures are reported as a floating point values and not as
(unsigned) integers.
2018-01-30 21:20:53 +01:00