Skip to content

Commit 34556d4

Browse files
authored
Merge pull request #71 from mikeller/update_libdivecomputer_202412
2 parents c57fcac + e54aca5 commit 34556d4

17 files changed

+981
-236
lines changed

contrib/android/Android.mk

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LOCAL_SRC_FILES := \
99
src/array.c \
1010
src/atomics_cobalt.c \
1111
src/atomics_cobalt_parser.c \
12+
src/ble.c \
1213
src/bluetooth.c \
1314
src/buffer.c \
1415
src/checksum.c \

contrib/msvc/libdivecomputer.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
<ClCompile Include="..\..\src\array.c" />
178178
<ClCompile Include="..\..\src\atomics_cobalt.c" />
179179
<ClCompile Include="..\..\src\atomics_cobalt_parser.c" />
180+
<ClCompile Include="..\..\src\ble.c" />
180181
<ClCompile Include="..\..\src\bluetooth.c" />
181182
<ClCompile Include="..\..\src\buffer.c" />
182183
<ClCompile Include="..\..\src\checksum.c" />

examples/dctool_timesync.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ do_timesync (dc_context_t *context, dc_descriptor_t *descriptor, dc_transport_t
8585
}
8686

8787
// Syncronize the device clock.
88-
message ("Syncronize the device clock.\n");
88+
message ("Synchronize the device clock.\n");
8989
rc = dc_device_timesync (device, datetime);
9090
if (rc != DC_STATUS_SUCCESS) {
91-
ERROR ("Error syncronizing the device clock.");
91+
ERROR ("Error synchronizing the device clock.");
9292
goto cleanup;
9393
}
9494

include/libdivecomputer/ble.h

+53
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,59 @@ extern "C" {
4848
#define DC_IOCTL_BLE_GET_ACCESSCODE DC_IOCTL_IOR('b', 2, DC_IOCTL_SIZE_VARIABLE)
4949
#define DC_IOCTL_BLE_SET_ACCESSCODE DC_IOCTL_IOW('b', 2, DC_IOCTL_SIZE_VARIABLE)
5050

51+
/**
52+
* Perform a BLE characteristic read/write operation.
53+
*
54+
* The UUID of the characteristic must be specified as a #dc_ble_uuid_t
55+
* data structure. If the operation requires additional data as in- or
56+
* output, the buffer must be located immediately after the
57+
* #dc_ble_uuid_t data structure. The size of the ioctl request is the
58+
* total size, including the size of the #dc_ble_uuid_t structure.
59+
*/
60+
#define DC_IOCTL_BLE_CHARACTERISTIC_READ DC_IOCTL_IOR('b', 3, DC_IOCTL_SIZE_VARIABLE)
61+
#define DC_IOCTL_BLE_CHARACTERISTIC_WRITE DC_IOCTL_IOW('b', 3, DC_IOCTL_SIZE_VARIABLE)
62+
63+
/**
64+
* The minimum number of bytes (including the terminating null byte) for
65+
* formatting a bluetooth UUID as a string.
66+
*/
67+
#define DC_BLE_UUID_SIZE 37
68+
69+
/**
70+
* Bluetooth UUID (128 bits).
71+
*/
72+
typedef unsigned char dc_ble_uuid_t[16];
73+
74+
/**
75+
* Convert a bluetooth UUID to a string.
76+
*
77+
* The bluetooth UUID is formatted as
78+
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX, where each XX pair is a
79+
* hexadecimal number specifying an octet of the UUID.
80+
* The minimum size for the buffer is #DC_BLE_UUID_SIZE bytes.
81+
*
82+
* @param[in] uuid A bluetooth UUID.
83+
* @param[in] str The memory buffer to store the result.
84+
* @param[in] size The size of the memory buffer.
85+
* @returns The null-terminated string on success, or NULL on failure.
86+
*/
87+
char *
88+
dc_ble_uuid2str (const dc_ble_uuid_t uuid, char *str, size_t size);
89+
90+
/**
91+
* Convert a string to a bluetooth UUID.
92+
*
93+
* The string is expected to be in the format
94+
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX, where each XX pair is a
95+
* hexadecimal number specifying an octet of the UUID.
96+
*
97+
* @param[in] str A null-terminated string.
98+
* @param[in] uuid The memory buffer to store the result.
99+
* @returns Non-zero on success, or zero on failure.
100+
*/
101+
int
102+
dc_ble_str2uuid (const char *str, dc_ble_uuid_t uuid);
103+
51104
#ifdef __cplusplus
52105
}
53106
#endif /* __cplusplus */

src/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ libdivecomputer_la_SOURCES = \
8888
irda.c \
8989
usb.c \
9090
usbhid.c \
91+
ble.c \
9192
bluetooth.c \
9293
custom.c
9394

src/ble.c

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* libdivecomputer
3+
*
4+
* Copyright (C) 2024 Jef Driesen
5+
*
6+
* This library is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 2.1 of the License, or (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this library; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19+
* MA 02110-1301 USA
20+
*/
21+
22+
#ifdef HAVE_CONFIG_H
23+
#include "config.h"
24+
#endif
25+
26+
#include <string.h>
27+
28+
#include <libdivecomputer/ble.h>
29+
30+
#include "platform.h"
31+
32+
char *
33+
dc_ble_uuid2str (const dc_ble_uuid_t uuid, char *str, size_t size)
34+
{
35+
if (str == NULL || size < DC_BLE_UUID_SIZE)
36+
return NULL;
37+
38+
int n = dc_platform_snprintf(str, size,
39+
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
40+
uuid[0], uuid[1], uuid[2], uuid[3],
41+
uuid[4], uuid[5],
42+
uuid[6], uuid[7],
43+
uuid[8], uuid[9],
44+
uuid[10], uuid[11], uuid[12],
45+
uuid[13], uuid[14], uuid[15]);
46+
if (n < 0 || (size_t) n >= size)
47+
return NULL;
48+
49+
return str;
50+
}
51+
52+
int
53+
dc_ble_str2uuid (const char *str, dc_ble_uuid_t uuid)
54+
{
55+
dc_ble_uuid_t tmp = {0};
56+
57+
if (str == NULL || uuid == NULL)
58+
return 0;
59+
60+
unsigned int i = 0;
61+
unsigned char c = 0;
62+
while ((c = *str++) != '\0') {
63+
if (c == '-') {
64+
if (i != 8 && i != 12 && i != 16 && i != 20) {
65+
return 0; /* Invalid character! */
66+
}
67+
continue;
68+
} else if (c >= '0' && c <= '9') {
69+
c -= '0';
70+
} else if (c >= 'A' && c <= 'F') {
71+
c -= 'A' - 10;
72+
} else if (c >= 'a' && c <= 'f') {
73+
c -= 'a' - 10;
74+
} else {
75+
return 0; /* Invalid character! */
76+
}
77+
78+
if ((i & 1) == 0) {
79+
c <<= 4;
80+
}
81+
82+
if (i >= 2 * sizeof(tmp)) {
83+
return 0; /* Too many characters! */
84+
}
85+
86+
tmp[i / 2] |= c;
87+
i++;
88+
}
89+
90+
if (i != 2 * sizeof(tmp)) {
91+
return 0; /* Not enough characters! */
92+
}
93+
94+
memcpy (uuid, tmp, sizeof(tmp));
95+
96+
return 1;
97+
}

0 commit comments

Comments
 (0)