Add support for the Divesoft Freedom and Liberty

The latest versions of the Divesoft Freedom (HW 4.x) and Liberty (HW
2.x) dive computers support BLE communication. Previous generations did
support only a mass storage mode, where the dives are available as DLF
files. The BLE communication protocol uses HDLC framing for the data
packets. The dives downloaded over BLE have the same data format as the
DLF files.

Co-authored-by: Jan Matoušek <jan.matousek@rekomando.cz>
Tested-by: Jakub Hečko <jakub.hecko@divesoft.com>
This commit is contained in:
Jef Driesen 2023-02-22 00:11:14 +01:00
parent b0e77fd05f
commit d4472b758f
11 changed files with 1743 additions and 0 deletions

View File

@ -34,6 +34,8 @@ LOCAL_SRC_FILES := \
src/device.c \
src/diverite_nitekq.c \
src/diverite_nitekq_parser.c \
src/divesoft_freedom.c \
src/divesoft_freedom_parser.c \
src/divesystem_idive.c \
src/divesystem_idive_parser.c \
src/hdlc.c \

View File

@ -202,6 +202,8 @@
<ClCompile Include="..\..\src\device.c" />
<ClCompile Include="..\..\src\diverite_nitekq.c" />
<ClCompile Include="..\..\src\diverite_nitekq_parser.c" />
<ClCompile Include="..\..\src\divesoft_freedom.c" />
<ClCompile Include="..\..\src\divesoft_freedom_parser.c" />
<ClCompile Include="..\..\src\divesystem_idive.c" />
<ClCompile Include="..\..\src\divesystem_idive_parser.c" />
<ClCompile Include="..\..\src\hdlc.c" />
@ -331,6 +333,7 @@
<ClInclude Include="..\..\src\descriptor-private.h" />
<ClInclude Include="..\..\src\device-private.h" />
<ClInclude Include="..\..\src\diverite_nitekq.h" />
<ClInclude Include="..\..\src\divesoft_freedom.h" />
<ClInclude Include="..\..\src\divesystem_idive.h" />
<ClInclude Include="..\..\src\hdlc.h" />
<ClInclude Include="..\..\src\hw_frog.h" />

View File

@ -98,6 +98,7 @@ static const backend_table_t g_backends[] = {
{"screen", DC_FAMILY_SEAC_SCREEN, 0},
{"cosmiq", DC_FAMILY_DEEPBLU_COSMIQ, 0},
{"s1", DC_FAMILY_OCEANS_S1, 0},
{"freedom", DC_FAMILY_DIVESOFT_FREEDOM, 19},
};
static const transport_table_t g_transports[] = {

View File

@ -118,6 +118,8 @@ typedef enum dc_family_t {
DC_FAMILY_DEEPBLU_COSMIQ = (21 << 16),
/* Oceans S1 */
DC_FAMILY_OCEANS_S1 = (22 << 16),
/* Divesoft Freedom */
DC_FAMILY_DIVESOFT_FREEDOM = (23 << 16),
} dc_family_t;
#ifdef __cplusplus

View File

@ -80,6 +80,7 @@ libdivecomputer_la_SOURCES = \
deepblu_cosmiq.h deepblu_cosmiq.c deepblu_cosmiq_parser.c \
oceans_s1_common.h oceans_s1_common.c \
oceans_s1.h oceans_s1.c oceans_s1_parser.c \
divesoft_freedom.h divesoft_freedom.c divesoft_freedom_parser.c \
hdlc.h hdlc.c \
socket.h socket.c \
irda.c \

View File

@ -65,6 +65,7 @@ static int dc_filter_atomic (dc_transport_t transport, const void *userdata, voi
static int dc_filter_deepsix (dc_transport_t transport, const void *userdata, void *params);
static int dc_filter_deepblu (dc_transport_t transport, const void *userdata, void *params);
static int dc_filter_oceans (dc_transport_t transport, const void *userdata, void *params);
static int dc_filter_divesoft (dc_transport_t transport, const void *userdata, void *params);
static dc_status_t dc_descriptor_iterator_next (dc_iterator_t *iterator, void *item);
@ -451,6 +452,9 @@ static const dc_descriptor_t g_descriptors[] = {
{"Deepblu", "Cosmiq+", DC_FAMILY_DEEPBLU_COSMIQ, 0, DC_TRANSPORT_BLE, dc_filter_deepblu},
/* Oceans S1 */
{"Oceans", "S1", DC_FAMILY_OCEANS_S1, 0, DC_TRANSPORT_BLE, dc_filter_oceans},
/* Divesoft Freedom */
{"Divesoft", "Freedom", DC_FAMILY_DIVESOFT_FREEDOM, 19, DC_TRANSPORT_BLE, dc_filter_divesoft},
{"Divesoft", "Liberty", DC_FAMILY_DIVESOFT_FREEDOM, 10, DC_TRANSPORT_BLE, dc_filter_divesoft},
};
static int
@ -798,6 +802,20 @@ static int dc_filter_oceans (dc_transport_t transport, const void *userdata, voi
return 1;
}
static int dc_filter_divesoft (dc_transport_t transport, const void *userdata, void *params)
{
static const char * const bluetooth[] = {
"Freedom",
"Liberty",
};
if (transport == DC_TRANSPORT_BLE) {
return DC_FILTER_INTERNAL (userdata, bluetooth, 0, dc_match_prefix);
}
return 1;
}
dc_status_t
dc_descriptor_iterator (dc_iterator_t **out)
{

View File

@ -64,6 +64,7 @@
#include "seac_screen.h"
#include "deepblu_cosmiq.h"
#include "oceans_s1.h"
#include "divesoft_freedom.h"
#include "device-private.h"
#include "context-private.h"
@ -239,6 +240,9 @@ dc_device_open (dc_device_t **out, dc_context_t *context, dc_descriptor_t *descr
case DC_FAMILY_OCEANS_S1:
rc = oceans_s1_device_open (&device, context, iostream);
break;
case DC_FAMILY_DIVESOFT_FREEDOM:
rc = divesoft_freedom_device_open (&device, context, iostream);
break;
default:
return DC_STATUS_INVALIDARGS;
}

595
src/divesoft_freedom.c Normal file
View File

@ -0,0 +1,595 @@
/*
* libdivecomputer
*
* Copyright (C) 2023 Jan Matoušek, Jef Driesen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "divesoft_freedom.h"
#include "context-private.h"
#include "device-private.h"
#include "platform.h"
#include "checksum.h"
#include "array.h"
#include "hdlc.h"
#define MAXDATA 256
#define HEADER_SIGNATURE_V1 0x45766944 // "DivE"
#define HEADER_SIGNATURE_V2 0x45566944 // "DiVE"
#define HEADER_SIZE_V1 32
#define HEADER_SIZE_V2 64
#define RECORD_SIZE 16
#define FINGERPRINT_SIZE 20
#define INVALID 0xFFFFFFFF
#define COMPRESSION 1
#define DIRECTION 1
#define NRECORDS 100
#define DEVICE_CCR_CU 1 // Liberty HW rev. 1.X
#define DEVICE_FREEDOM 2 // Freedom HW rev. 2.X
#define DEVICE_FREEDOM3 5 // Freedom HW rev. 3.X
#define DEVICE_CCR_CU15 10 // Liberty HW rev. 2.X, Bluetooth enabled
#define DEVICE_FREEDOM4 19 // Freedom HW rev. 4.X, Bluetooth enabled
typedef enum message_t {
MSG_ECHO = 0,
MSG_RESULT = 1,
MSG_CONNECT = 2,
MSG_CONNECTED = 3,
MSG_VERSION = 4,
MSG_VERSION_RSP = 5,
MSG_DIVE_DATA = 64,
MSG_DIVE_DATA_RSP = 65,
MSG_DIVE_LIST = 66,
MSG_DIVE_LIST_V1 = 67,
MSG_DIVE_LIST_V2 = 71,
} message_t;
typedef struct divesoft_freedom_device_t {
dc_device_t base;
dc_iostream_t *iostream;
unsigned char fingerprint[FINGERPRINT_SIZE];
unsigned int seqnum;
} divesoft_freedom_device_t;
static dc_status_t divesoft_freedom_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size);
static dc_status_t divesoft_freedom_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata);
static dc_status_t divesoft_freedom_device_close (dc_device_t *device);
static const dc_device_vtable_t divesoft_freedom_device_vtable = {
sizeof(divesoft_freedom_device_t),
DC_FAMILY_DIVESOFT_FREEDOM,
divesoft_freedom_device_set_fingerprint, /* set_fingerprint */
NULL, /* read */
NULL, /* write */
NULL, /* dump */
divesoft_freedom_device_foreach, /* foreach */
NULL, /* timesync */
divesoft_freedom_device_close, /* close */
};
static dc_status_t
divesoft_freedom_send (divesoft_freedom_device_t *device, message_t message, const unsigned char data[], size_t size)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
size_t nbytes = 0, count = 0;
while (1) {
size_t len = size - nbytes;
if (len > MAXDATA)
len = MAXDATA;
unsigned int islast = nbytes + len == size;
unsigned char packet[6 + MAXDATA + 2] = {0};
packet[0] = ((count & 0x0F) << 4) | (device->seqnum & 0x0F);
packet[1] = 0x80 | (islast << 6);
array_uint16_le_set (packet + 2, message);
array_uint16_le_set (packet + 4, len);
if (len) {
memcpy (packet + 6, data + nbytes, len);
}
unsigned short crc = checksum_crc16r_ccitt (packet, len + 6, 0xFFFF, 0xFFFF);
array_uint16_le_set (packet + 6 + len, crc);
HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "cmd", packet, 6 + len + 2);
status = dc_iostream_write (device->iostream, packet, 6 + len + 2, NULL);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the packet.");
return status;
}
nbytes += len;
count++;
if (islast)
break;
}
return status;
}
static dc_status_t
divesoft_freedom_recv (divesoft_freedom_device_t *device, dc_event_progress_t *progress, message_t *message, dc_buffer_t *buffer)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
unsigned int msg = INVALID;
unsigned int count = 0;
while (1) {
size_t len = 0;
unsigned char packet[6 + MAXDATA + 2] = {0};
status = dc_iostream_read (device->iostream, packet, sizeof(packet), &len);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive the packet.");
return status;
}
HEXDUMP (abstract->context, DC_LOGLEVEL_DEBUG, "rcv", packet, len);
if (len < 8) {
ERROR (abstract->context, "Unexpected packet length (" DC_PRINTF_SIZE ").", len);
return DC_STATUS_PROTOCOL;
}
unsigned int seqnum = packet[0];
unsigned int flags = packet[1];
unsigned int type = array_uint16_le (packet + 2);
unsigned int length = array_uint16_le (packet + 4);
unsigned int expected = ((count & 0x0F) << 4) | (device->seqnum & 0x0F);
if (seqnum != expected) {
ERROR (abstract->context, "Unexpected packet sequence number (%u %u).", seqnum, expected);
return DC_STATUS_PROTOCOL;
}
if ((flags & ~0x40) != 0) {
ERROR (abstract->context, "Unexpected packet flags (%u).", flags);
return DC_STATUS_PROTOCOL;
}
if (length != len - 8) {
ERROR (abstract->context, "Unexpected packet length (%u " DC_PRINTF_SIZE ").", length, len - 8);
return DC_STATUS_PROTOCOL;
}
if (msg == INVALID) {
msg = type;
} else if (msg != type) {
ERROR (abstract->context, "Unexpected packet type (%u).", msg);
return DC_STATUS_PROTOCOL;
}
unsigned short crc = array_uint16_le (packet + len - 2);
unsigned short ccrc = checksum_crc16r_ccitt (packet, len - 2, 0xFFFF, 0xFFFF);
if (crc != ccrc) {
ERROR (abstract->context, "Unexpected packet checksum (%04x %04x).", crc, ccrc);
return DC_STATUS_PROTOCOL;
}
// Update and emit a progress event.
if (progress) {
progress->current += len - 8;
// Limit the progress to the maximum size. This could happen if the
// dive computer sends more data than requested for some reason.
if (progress->current > progress->maximum) {
WARNING (abstract->context, "Progress exceeds the maximum size.");
progress->current = progress->maximum;
}
device_event_emit (abstract, DC_EVENT_PROGRESS, progress);
}
if (!dc_buffer_append (buffer, packet + 6, len - 8)) {
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_NOMEMORY;
}
count++;
if (flags & 0x40)
break;
}
if (message)
*message = msg;
return status;
}
static dc_status_t
divesoft_freedom_transfer (divesoft_freedom_device_t *device, dc_event_progress_t *progress, message_t cmd, const unsigned char data[], size_t size, message_t *msg, dc_buffer_t *buffer)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
if (device_is_cancelled (abstract))
return DC_STATUS_CANCELLED;
device->seqnum++;
status = divesoft_freedom_send (device, cmd, data, size);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to send the command.");
return status;
}
status = divesoft_freedom_recv (device, progress, msg, buffer);
if(status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to receive response.");
return status;
}
return status;
}
static dc_status_t
divesoft_freedom_download (divesoft_freedom_device_t *device, message_t cmd, const unsigned char cdata[], size_t csize, unsigned char rdata[], size_t rsize)
{
dc_status_t status = DC_STATUS_SUCCESS;
dc_device_t *abstract = (dc_device_t *) device;
dc_buffer_t *buffer = dc_buffer_new (rsize);
if (buffer == NULL) {
ERROR (abstract->context, "Failed to allocate memory.");
status = DC_STATUS_NOMEMORY;
goto error_exit;
}
message_t msg = MSG_ECHO;
status = divesoft_freedom_transfer (device, NULL, cmd, cdata, csize, &msg, buffer);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to transfer the packet.");
goto error_free;
}
if (msg != cmd + 1) {
ERROR (abstract->context, "Unexpected response message (%u).", msg);
status = DC_STATUS_PROTOCOL;
goto error_free;
}
size_t length = dc_buffer_get_size (buffer);
if (length != rsize) {
ERROR (abstract->context, "Unexpected response length (" DC_PRINTF_SIZE " " DC_PRINTF_SIZE ").", length, rsize);
status = DC_STATUS_PROTOCOL;
goto error_free;
}
if (rsize) {
memcpy (rdata, dc_buffer_get_data (buffer), rsize);
}
error_free:
dc_buffer_free (buffer);
error_exit:
return status;
}
dc_status_t
divesoft_freedom_device_open (dc_device_t **out, dc_context_t *context, dc_iostream_t *iostream)
{
dc_status_t status = DC_STATUS_SUCCESS;
divesoft_freedom_device_t *device = NULL;
if (out == NULL)
return DC_STATUS_INVALIDARGS;
// Allocate memory.
device = (divesoft_freedom_device_t *) dc_device_allocate (context, &divesoft_freedom_device_vtable);
if (device == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
// Set the default values.
device->iostream = NULL;
memset(device->fingerprint, 0, sizeof(device->fingerprint));
device->seqnum = 0;
// Setup the HDLC communication.
status = dc_hdlc_open (&device->iostream, context, iostream, 244, 244);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to create the HDLC stream.");
goto error_free;
}
// Set the serial communication protocol (115200 8N1).
status = dc_iostream_configure (device->iostream, 115200, 8, DC_PARITY_NONE, DC_STOPBITS_ONE, DC_FLOWCONTROL_NONE);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to set the terminal attributes.");
goto error_free_hdlc;
}
// Set the timeout for receiving data (3000ms).
status = dc_iostream_set_timeout (device->iostream, 3000);
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to set the timeout.");
goto error_free_hdlc;
}
// Initiate the connection with the dive computer.
const char client[] = "libdivecomputer";
unsigned char cmd_connect[2 + sizeof(client) - 1] = {0};
array_uint16_le_set (cmd_connect, COMPRESSION);
memcpy (cmd_connect + 2, client, sizeof(client) - 1);
unsigned char rsp_connect[36] = {0};
status = divesoft_freedom_download (device, MSG_CONNECT, cmd_connect, sizeof(cmd_connect), rsp_connect, sizeof(rsp_connect));
if (status != DC_STATUS_SUCCESS) {
ERROR (context, "Failed to connect to the device.");
goto error_free_hdlc;
}
DEBUG (context, "Connection: compression=%u, protocol=%u.%u, serial=%.16s",
array_uint16_le (rsp_connect),
rsp_connect[2], rsp_connect[3],
rsp_connect + 4);
*out = (dc_device_t *) device;
return DC_STATUS_SUCCESS;
error_free_hdlc:
dc_iostream_close (device->iostream);
error_free:
dc_device_deallocate ((dc_device_t *) device);
return status;
}
static dc_status_t
divesoft_freedom_device_close (dc_device_t *abstract)
{
divesoft_freedom_device_t *device = (divesoft_freedom_device_t *) abstract;
return dc_iostream_close (device->iostream);
}
static dc_status_t
divesoft_freedom_device_set_fingerprint (dc_device_t *abstract, const unsigned char data[], unsigned int size)
{
divesoft_freedom_device_t *device = (divesoft_freedom_device_t *) abstract;
if (size && size != sizeof (device->fingerprint))
return DC_STATUS_INVALIDARGS;
if (size)
memcpy (device->fingerprint, data, sizeof (device->fingerprint));
else
memset (device->fingerprint, 0, sizeof (device->fingerprint));
return DC_STATUS_SUCCESS;
}
static dc_status_t
divesoft_freedom_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata)
{
dc_status_t status = DC_STATUS_SUCCESS;
divesoft_freedom_device_t *device = (divesoft_freedom_device_t *) abstract;
// Enable progress notifications.
dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
// Read the device information.
unsigned char rsp_version[26] = {0};
status = divesoft_freedom_download (device, MSG_VERSION, NULL, 0, rsp_version, sizeof(rsp_version));
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to read the device information.");
goto error_exit;
}
DEBUG (abstract->context, "Device: model=%u, hw=%u.%u, sw=%u.%u.%u.%u serial=%.16s",
rsp_version[0],
rsp_version[1], rsp_version[2],
rsp_version[3], rsp_version[4], rsp_version[5],
array_uint32_le (rsp_version + 6),
rsp_version + 10);
// Emit a device info event.
dc_event_devinfo_t devinfo;
devinfo.model = rsp_version[0];
devinfo.firmware = array_uint24_be (rsp_version + 3);
devinfo.serial = array_convert_str2num (rsp_version + 10 + 5, 11);
device_event_emit(abstract, DC_EVENT_DEVINFO, &devinfo);
// Allocate memory for the dive list.
dc_buffer_t *divelist = dc_buffer_new (0);
if (divelist == NULL) {
status = DC_STATUS_NOMEMORY;
goto error_exit;
}
// Allocate memory for the download buffer.
dc_buffer_t *buffer = dc_buffer_new (NRECORDS * (4 + FINGERPRINT_SIZE + HEADER_SIZE_V2));
if (buffer == NULL) {
status = DC_STATUS_NOMEMORY;
goto error_free_divelist;
}
// Record version and size.
unsigned int version = 0;
unsigned int headersize = 0;
unsigned int recordsize = 0;
// Download the dive list.
unsigned int ndives = 0;
unsigned int total = 0;
unsigned int maxsize = 0;
unsigned int current = INVALID;
while (1) {
// Clear the buffer.
dc_buffer_clear (buffer);
// Prepare the command.
unsigned char cmd_list[6] = {0};
array_uint32_le_set (cmd_list, current);
cmd_list[4] = DIRECTION;
cmd_list[5] = NRECORDS;
// Download the dive list records.
message_t msg_list = MSG_ECHO;
status = divesoft_freedom_transfer (device, &progress, MSG_DIVE_LIST, cmd_list, sizeof(cmd_list), &msg_list, buffer);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to download the dive list.");
goto error_free_buffer;
}
// Check the response message type.
if (msg_list != MSG_DIVE_LIST_V1 && msg_list != MSG_DIVE_LIST_V2) {
ERROR (abstract->context, "Unexpected response message (%u).", msg_list);
status = DC_STATUS_PROTOCOL;
goto error_free_buffer;
}
// Store/check the version.
if (version == 0) {
version = msg_list;
headersize = version == MSG_DIVE_LIST_V1 ?
HEADER_SIZE_V1 : HEADER_SIZE_V2;
recordsize = 4 + FINGERPRINT_SIZE + headersize;
} else if (version != msg_list) {
ERROR (abstract->context, "Unexpected response message (%u).", msg_list);
status = DC_STATUS_PROTOCOL;
goto error_free_buffer;
}
const unsigned char *data = dc_buffer_get_data (buffer);
size_t size = dc_buffer_get_size (buffer);
// Process the records.
size_t offset = 0, count = 0;
while (offset + recordsize <= size) {
// Get the record data.
unsigned int handle = array_uint32_le (data + offset);
const unsigned char *fingerprint = data + offset + 4;
const unsigned char *header = data + offset + 4 + FINGERPRINT_SIZE;
// Check the fingerprint data.
if (memcmp (device->fingerprint, fingerprint, sizeof(device->fingerprint)) == 0) {
break;
}
// Get the length of the dive.
unsigned int nrecords = version == MSG_DIVE_LIST_V1 ?
array_uint32_le (header + 16) & 0x3FFFF :
array_uint32_le (header + 20);
unsigned int length = headersize + nrecords * RECORD_SIZE;
// Calculate the total and maximum size.
if (length > maxsize)
maxsize = length;
total += length;
// Set the handle for the next request.
current = handle;
offset += recordsize;
count++;
ndives++;
}
// Append the records to the dive list buffer.
if (!dc_buffer_append (divelist, data, count * recordsize)) {
ERROR (abstract->context, "Insufficient buffer space available.");
status = DC_STATUS_NOMEMORY;
goto error_free_buffer;
}
// Stop downloading if there are no more records.
if (count < NRECORDS)
break;
}
// Update and emit a progress event.
progress.maximum = progress.current + total;
device_event_emit(abstract, DC_EVENT_PROGRESS, &progress);
// Reserve memory for the largest dive.
dc_buffer_reserve (buffer, maxsize);
const unsigned char *data = dc_buffer_get_data (divelist);
size_t size = dc_buffer_get_size (divelist);
size_t offset = 0;
while (offset + recordsize <= size) {
// Get the record data.
unsigned int handle = array_uint32_le (data + offset);
const unsigned char *fingerprint = data + offset + 4;
const unsigned char *header = data + offset + 4 + FINGERPRINT_SIZE;
// Get the length of the dive.
unsigned int nrecords = version == MSG_DIVE_LIST_V1 ?
array_uint32_le (header + 16) & 0x3FFFF :
array_uint32_le (header + 20);
unsigned int length = headersize + nrecords * RECORD_SIZE;
// Clear the buffer.
dc_buffer_clear (buffer);
// Prepare the command.
unsigned char cmd_dive[12] = {0};
array_uint32_le_set (cmd_dive + 0, handle);
array_uint32_le_set (cmd_dive + 4, 0);
array_uint32_le_set (cmd_dive + 8, length);
// Download the dive.
message_t msg_dive = MSG_ECHO;
status = divesoft_freedom_transfer (device, &progress, MSG_DIVE_DATA, cmd_dive, sizeof(cmd_dive), &msg_dive, buffer);
if (status != DC_STATUS_SUCCESS) {
ERROR (abstract->context, "Failed to download the dive.");
goto error_free_buffer;
}
// Check the response message type.
if (msg_dive != MSG_DIVE_DATA_RSP) {
ERROR (abstract->context, "Unexpected response message (%u).", msg_dive);
status = DC_STATUS_PROTOCOL;
goto error_free_buffer;
}
// Verify both dive headers are identical.
if (dc_buffer_get_size (buffer) < headersize ||
memcmp (header, dc_buffer_get_data (buffer), headersize) != 0) {
ERROR (abstract->context, "Unexpected profile header.");
status = DC_STATUS_PROTOCOL;
goto error_free_buffer;
}
if (callback && !callback (dc_buffer_get_data(buffer), dc_buffer_get_size(buffer), fingerprint, sizeof (device->fingerprint), userdata)) {
break;
}
offset += recordsize;
}
error_free_buffer:
dc_buffer_free (buffer);
error_free_divelist:
dc_buffer_free (divelist);
error_exit:
return status;
}

43
src/divesoft_freedom.h Normal file
View File

@ -0,0 +1,43 @@
/*
* libdivecomputer
*
* Copyright (C) 2023 Jan Matoušek, Jef Driesen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#ifndef DIVESOFT_FREEDOM_H
#define DIVESOFT_FREEDOM_H
#include <libdivecomputer/context.h>
#include <libdivecomputer/iostream.h>
#include <libdivecomputer/device.h>
#include <libdivecomputer/parser.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
dc_status_t
divesoft_freedom_device_open (dc_device_t **device, dc_context_t *context, dc_iostream_t *iostream);
dc_status_t
divesoft_freedom_parser_create (dc_parser_t **parser, dc_context_t *context);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* DIVESOFT_FREEDOM_H */

File diff suppressed because it is too large Load Diff

View File

@ -63,6 +63,7 @@
#include "seac_screen.h"
#include "deepblu_cosmiq.h"
#include "oceans_s1.h"
#include "divesoft_freedom.h"
#include "context-private.h"
#include "parser-private.h"
@ -199,6 +200,9 @@ dc_parser_new_internal (dc_parser_t **out, dc_context_t *context, dc_family_t fa
case DC_FAMILY_OCEANS_S1:
rc = oceans_s1_parser_create (&parser, context);
break;
case DC_FAMILY_DIVESOFT_FREEDOM:
rc = divesoft_freedom_parser_create (&parser, context);
break;
default:
return DC_STATUS_INVALIDARGS;
}