Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update libdivecomputer from Upstream. #71

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add helper functions to convert UUID to/from strings
Add some helper functions to convert the binary UUID data structure to
and from their human readable string representation.
jefdriesen committed Oct 11, 2024
commit 88d78a323c468885965803032e195fe0c7f5b615
1 change: 1 addition & 0 deletions contrib/android/Android.mk
Original file line number Diff line number Diff line change
@@ -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 \
1 change: 1 addition & 0 deletions contrib/msvc/libdivecomputer.vcxproj
Original file line number Diff line number Diff line change
@@ -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" />
36 changes: 36 additions & 0 deletions include/libdivecomputer/ble.h
Original file line number Diff line number Diff line change
@@ -60,11 +60,47 @@ extern "C" {
#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 */
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
@@ -88,6 +88,7 @@ libdivecomputer_la_SOURCES = \
irda.c \
usb.c \
usbhid.c \
ble.c \
bluetooth.c \
custom.c

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;
}
3 changes: 3 additions & 0 deletions src/libdivecomputer.symbols
Original file line number Diff line number Diff line change
@@ -66,6 +66,9 @@ dc_bluetooth_device_free
dc_bluetooth_iterator_new
dc_bluetooth_open

dc_ble_uuid2str
dc_ble_str2uuid

dc_irda_device_get_address
dc_irda_device_get_name
dc_irda_device_free