Skip to content

Commit

Permalink
Merge pull request #71 from mikeller/update_libdivecomputer_202412
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeller authored Jan 1, 2025
2 parents c57fcac + e54aca5 commit 34556d4
Show file tree
Hide file tree
Showing 17 changed files with 981 additions and 236 deletions.
1 change: 1 addition & 0 deletions contrib/android/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ LOCAL_SRC_FILES := \
src/array.c \
src/atomics_cobalt.c \
src/atomics_cobalt_parser.c \
src/ble.c \
src/bluetooth.c \
src/buffer.c \
src/checksum.c \
Expand Down
1 change: 1 addition & 0 deletions contrib/msvc/libdivecomputer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
<ClCompile Include="..\..\src\array.c" />
<ClCompile Include="..\..\src\atomics_cobalt.c" />
<ClCompile Include="..\..\src\atomics_cobalt_parser.c" />
<ClCompile Include="..\..\src\ble.c" />
<ClCompile Include="..\..\src\bluetooth.c" />
<ClCompile Include="..\..\src\buffer.c" />
<ClCompile Include="..\..\src\checksum.c" />
Expand Down
4 changes: 2 additions & 2 deletions examples/dctool_timesync.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ do_timesync (dc_context_t *context, dc_descriptor_t *descriptor, dc_transport_t
}

// Syncronize the device clock.
message ("Syncronize the device clock.\n");
message ("Synchronize the device clock.\n");
rc = dc_device_timesync (device, datetime);
if (rc != DC_STATUS_SUCCESS) {
ERROR ("Error syncronizing the device clock.");
ERROR ("Error synchronizing the device clock.");
goto cleanup;
}

Expand Down
53 changes: 53 additions & 0 deletions include/libdivecomputer/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,59 @@ extern "C" {
#define DC_IOCTL_BLE_GET_ACCESSCODE DC_IOCTL_IOR('b', 2, DC_IOCTL_SIZE_VARIABLE)
#define DC_IOCTL_BLE_SET_ACCESSCODE DC_IOCTL_IOW('b', 2, DC_IOCTL_SIZE_VARIABLE)

/**
* Perform a BLE characteristic read/write operation.
*
* The UUID of the characteristic must be specified as a #dc_ble_uuid_t
* data structure. If the operation requires additional data as in- or
* output, the buffer must be located immediately after the
* #dc_ble_uuid_t data structure. The size of the ioctl request is the
* total size, including the size of the #dc_ble_uuid_t structure.
*/
#define DC_IOCTL_BLE_CHARACTERISTIC_READ DC_IOCTL_IOR('b', 3, DC_IOCTL_SIZE_VARIABLE)
#define DC_IOCTL_BLE_CHARACTERISTIC_WRITE DC_IOCTL_IOW('b', 3, DC_IOCTL_SIZE_VARIABLE)

/**
* The minimum number of bytes (including the terminating null byte) for
* formatting a bluetooth UUID as a string.
*/
#define DC_BLE_UUID_SIZE 37

/**
* Bluetooth UUID (128 bits).
*/
typedef unsigned char dc_ble_uuid_t[16];

/**
* Convert a bluetooth UUID to a string.
*
* The bluetooth UUID is formatted as
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX, where each XX pair is a
* hexadecimal number specifying an octet of the UUID.
* The minimum size for the buffer is #DC_BLE_UUID_SIZE bytes.
*
* @param[in] uuid A bluetooth UUID.
* @param[in] str The memory buffer to store the result.
* @param[in] size The size of the memory buffer.
* @returns The null-terminated string on success, or NULL on failure.
*/
char *
dc_ble_uuid2str (const dc_ble_uuid_t uuid, char *str, size_t size);

/**
* Convert a string to a bluetooth UUID.
*
* The string is expected to be in the format
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX, where each XX pair is a
* hexadecimal number specifying an octet of the UUID.
*
* @param[in] str A null-terminated string.
* @param[in] uuid The memory buffer to store the result.
* @returns Non-zero on success, or zero on failure.
*/
int
dc_ble_str2uuid (const char *str, dc_ble_uuid_t uuid);

#ifdef __cplusplus
}
#endif /* __cplusplus */
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ libdivecomputer_la_SOURCES = \
irda.c \
usb.c \
usbhid.c \
ble.c \
bluetooth.c \
custom.c

Expand Down
97 changes: 97 additions & 0 deletions src/ble.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* libdivecomputer
*
* Copyright (C) 2024 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
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>

#include <libdivecomputer/ble.h>

#include "platform.h"

char *
dc_ble_uuid2str (const dc_ble_uuid_t uuid, char *str, size_t size)
{
if (str == NULL || size < DC_BLE_UUID_SIZE)
return NULL;

int n = dc_platform_snprintf(str, size,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
uuid[0], uuid[1], uuid[2], uuid[3],
uuid[4], uuid[5],
uuid[6], uuid[7],
uuid[8], uuid[9],
uuid[10], uuid[11], uuid[12],
uuid[13], uuid[14], uuid[15]);
if (n < 0 || (size_t) n >= size)
return NULL;

return str;
}

int
dc_ble_str2uuid (const char *str, dc_ble_uuid_t uuid)
{
dc_ble_uuid_t tmp = {0};

if (str == NULL || uuid == NULL)
return 0;

unsigned int i = 0;
unsigned char c = 0;
while ((c = *str++) != '\0') {
if (c == '-') {
if (i != 8 && i != 12 && i != 16 && i != 20) {
return 0; /* Invalid character! */
}
continue;
} else if (c >= '0' && c <= '9') {
c -= '0';
} else if (c >= 'A' && c <= 'F') {
c -= 'A' - 10;
} else if (c >= 'a' && c <= 'f') {
c -= 'a' - 10;
} else {
return 0; /* Invalid character! */
}

if ((i & 1) == 0) {
c <<= 4;
}

if (i >= 2 * sizeof(tmp)) {
return 0; /* Too many characters! */
}

tmp[i / 2] |= c;
i++;
}

if (i != 2 * sizeof(tmp)) {
return 0; /* Not enough characters! */
}

memcpy (uuid, tmp, sizeof(tmp));

return 1;
}
Loading

0 comments on commit 34556d4

Please sign in to comment.