kindlebt
Bluetooth functionality for Kindle 11th gen and up
Loading...
Searching...
No Matches
basic_usage.c
1#include <stdio.h>
2#include <unistd.h>
3
4#include <kindlebt/kindlebt.h>
5#include <kindlebt/kindlebt_log.h>
6
7static sessionHandle bt_session = NULL;
8static bleConnHandle conn_handle = NULL;
9static bleGattsService_t* gatt_db = NULL;
10static uint32_t gatt_db_count;
11
12// For our dumb callback waits implementation
13static bool read_characteristic = false;
14static bool wrote_characteristic = false;
15
16// ===== UTILS =====
17void print_uuid(uuid_t* uuid) {
18 printf(
19 "UUID: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
20 uuid->uu[0], uuid->uu[1], uuid->uu[2], uuid->uu[3], uuid->uu[4], uuid->uu[5], uuid->uu[5],
21 uuid->uu[7], uuid->uu[8], uuid->uu[9], uuid->uu[10], uuid->uu[11], uuid->uu[12],
22 uuid->uu[13], uuid->uu[14], uuid->uu[15]
23 );
24}
25
26// This is a very simple implementation. In some cases, the response might not even be
27// a @ref bleGattBlobValue_t like we're assuming here
28// Another thing to note is that the \c data field is just a \c uint8_t* field. If you
29// expect text, you'll need to cast it with an ASCII table
30void print_data(bleGattBlobValue_t* value) {
31 printf("Data: ");
32 for (int idx = 0; idx < value->size; idx++)
33 printf("%02x ", value->data[idx]);
34 printf("\n");
35}
36
37void freeGattBlob(bleGattCharacteristicsValue_t* chars_value) {
38 if (chars_value == NULL || chars_value->blobValue.data == NULL) return;
39
40 free(chars_value->blobValue.data);
41 chars_value->blobValue.data = NULL;
42 chars_value->blobValue.size = 0;
43 chars_value->blobValue.offset = 0;
44}
45
46void setGattBlobFromBytes(
47 bleGattCharacteristicsValue_t* chars_value, const uint8_t* data, uint16_t size
48) {
49 if (chars_value == NULL || data == NULL || size == 0) return;
50
51 free(chars_value->blobValue.data);
52
53 uint8_t* blob = malloc(size);
54 if (blob == NULL) return;
55
56 memcpy(blob, data, size);
57
58 chars_value->blobValue.data = blob;
59 chars_value->blobValue.size = size;
60 chars_value->blobValue.offset = 0;
61 chars_value->format = BLE_FORMAT_BLOB;
62}
63
64// ===== CALLBACKS =====
65void bleGattcNotifyCharsCallback(
66 bleConnHandle conn_handle, bleGattCharacteristicsValue_t chars_value
67) {
68 printf("Callback %s(): conn_handle %p", __func__, conn_handle);
69 print_uuid(&chars_value.gattRecord.uuid);
70 print_data(&chars_value.blobValue);
71}
72
73void bleGattcReadCharsCallback(
74 bleConnHandle conn_handle, bleGattCharacteristicsValue_t chars_value, status_t status
75) {
76 printf("Callback %s(): status %d conn_handle %p\n", __func__, status, conn_handle);
77 print_uuid(&chars_value.gattRecord.uuid);
78 print_data(&chars_value.blobValue);
79 read_characteristic = true;
80}
81
82void bleGattcWriteCharsCallback(
83 bleConnHandle conn_handle, bleGattCharacteristicsValue_t gatt_characteristics, status_t status
84) {
85 printf("Callback %s(): conn_handle %p status %d", __func__, conn_handle, status);
86 wrote_characteristic = true;
87}
88
89static bleGattClientCallbacks_t gatt_app_callbacks = {
90 .size = sizeof(bleGattClientCallbacks_t),
91 .notify_characteristics_cb = bleGattcNotifyCharsCallback,
92 .on_ble_gattc_read_characteristics_cb = bleGattcReadCharsCallback,
93 .on_ble_gattc_write_characteristics_cb = bleGattcWriteCharsCallback,
94};
95
96int main() {
97 // (OPTIONAL) SET LOG LEVEL
98 kindlebt_set_log_level(LOG_LEVEL_DEBUG);
99
100 // USE BLUETOOTH PRIVILEGES
101 // The ace_bt stuff won't run under root user
102 if (setgid((gid_t)1003) || setuid((uid_t)1003)) {
103 fprintf(stderr, "Can't drop privileges to bluetooth user/group\n");
104 return -1;
105 }
106
107 printf("Hello World from Kindle!\n");
108
109 // CHECK FOR BLE SUPPORT
110 // Might not be necessary
111 bool isBLE = isBLESupported();
112 printf("Is BLE enabled: %d\n", isBLE);
113 if (!isBLE) {
114 fprintf(stderr, "BLE is not enabled\n");
115 return -2;
116 }
117
118 status_t status;
119
120 // OPEN SESSION
121 // Necessary, basically all API operations require a BT session
122 status = openSession(ACEBT_SESSION_TYPE_DUAL_MODE, &bt_session);
123 printf(
124 "Opened session status %d, session %p (u32 %u)\n", status, bt_session, (uint32_t)bt_session
125 );
126 if (status != ACE_STATUS_OK) {
127 fprintf(stderr, "Cannot open Bluetooth session, status: %d\n", status);
128 return -3;
129 }
130
131 // REGISTER FOR BLE USE
132 // This registers for BLE and related callbacks functionality in btmanagerd
133 status = bleRegister(bt_session);
134 printf("Registered BLE: %d\n", status);
135 if (status != ACE_STATUS_OK) {
136 fprintf(stderr, "Cannot register BLE, status: %d\n", status);
137 return -4;
138 }
139
140 // REGISTER FOR GATT CLIENT USE
141 // Likewise, same as before but specifically for GATTC events in btmanagerd
142 status = bleRegisterGattClient(bt_session, &gatt_app_callbacks);
143 printf("Registered GATT Client status: %d\n", status);
144 if (status != ACE_STATUS_OK) {
145 fprintf(stderr, "Cannot register GATT Client, status: %d\n", status);
146 return -5;
147 }
148
149 // CONNECT TO BLE DEVICE
150 // Untested whether connecting to a Classic device with this API works
151 bdAddr_t ble_addr = {.address = {0x2C, 0xCF, 0x67, 0xB8, 0xDC, 0x3F}};
152 status = bleConnect(
153 bt_session, &conn_handle, &ble_addr, ACE_BT_BLE_CONN_PARAM_BALANCED,
154 ACEBT_BLE_GATT_CLIENT_ROLE, ACE_BT_BLE_CONN_PRIO_MEDIUM
155 );
156 printf("Connected to BLE status:%d\n", status);
157 if (status != ACE_STATUS_OK) {
158 fprintf(stderr, "Cannot connect to BLE device, status: %d\n", status);
159 return -6;
160 }
161
162 // DISCOVER GATT SERVICES
163 // Necessary step to discover all the GATT services of the remote device
164 status = bleDiscoverAllServices(bt_session, conn_handle);
165 printf("Discovered all services: %d\n", status);
166 if (status != ACE_STATUS_OK) {
167 fprintf(stderr, "Cannot discover services of BLE device, status: %d\n", status);
168 return -7;
169 }
170
171 // RETRIEVE GATT SERVICES (DB)
172 // Retrieval of services is a different and also necessary step from discovery
173 // Even if you know the Characteristic UUIDs ahead of time, you still need to
174 // discover and retrieve the services
175 status = bleGetDatabase(conn_handle, &gatt_db, &gatt_db_count);
176 printf("Requested GATT DB status: %d\n", status);
177 if (status != ACE_STATUS_OK) {
178 fprintf(stderr, "Cannot retrieve services of BLE device, status: %d\n", status);
179 return -8;
180 }
181
182 // FINDING CHARACTERISTIC
183 // Again, even if you know beforehand the UUID of the characteristic you want, you
184 // still need to retrieve the DB, and then find your UUID in the local DB copy to
185 // interact with it
186 uuid_t characUuid = {
187 .uu =
188 {0xFF, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189 0x00, 0x00},
190 .type = ACEBT_UUID_TYPE_16,
191 };
192 struct aceBT_gattCharRec_t* charac = utilsFindCharRec(gatt_db, gatt_db_count, characUuid, 16);
193 if (charac == NULL) {
194 printf("Couldn't find the characteristic");
195 return -9;
196 }
197
198 // ENABLING NOTIFICATIONS ON CHARACTERISTIC
199 status = bleSetNotification(bt_session, conn_handle, charac->value, true);
200 if (status != ACE_STATUS_OK) {
201 fprintf(
202 stderr, "Cannot enable notifications on BLE device/characteristic, status: %d\n", status
203 );
204 return -10;
205 }
206
207 sleep(5);
208
209 // DISABLING NOTIFICATIONS ON CHARACTERISTIC
210 status = bleSetNotification(bt_session, conn_handle, charac->value, false);
211 if (status != ACE_STATUS_OK) {
212 fprintf(
213 stderr, "Cannot disable notifications on BLE device/characteristic, status: %d\n",
214 status
215 );
216 return -10;
217 }
218
219 sleep(2);
220
221 // Block reading and writing in a loop
222 for (int i = 0; i < 20; i++) {
223 // READING CHARACTERISTIC
224 status = bleReadCharacteristic(bt_session, conn_handle, charac->value);
225 if (status != ACE_STATUS_OK) {
226 fprintf(stderr, "Cannot read characteristic, status: %d\n", status);
227 return -11;
228 }
229 for (int j = 0; j < 10; j++) {
230 if (read_characteristic) break;
231 sleep(1);
232 }
233 if (!read_characteristic) {
234 fprintf(stderr, "Timed out waiting to read characteristic, status: %d\n", status);
235 return -11;
236 }
237 read_characteristic = false;
238 // Not sure if necessary but I always reset the blob after read/write
239 freeGattBlob(&charac->value);
240
241 // WRITING CHARACTERISTIC
242 // Values used in my characteristic as an example of how to write
243 uint8_t off[] = {'O', 'F', 'F'};
244 uint8_t on[] = {'O', 'N'};
245 uint8_t* states[] = {off, on};
246 size_t lengths[] = {sizeof(off), sizeof(on)};
247
248 // Now write the value to the characteristic struct
249 setGattBlobFromBytes(&charac->value, states[i % 2], lengths[i % 2]);
250
251 status = bleWriteCharacteristic(
252 bt_session, conn_handle, &charac->value, ACEBT_BLE_WRITE_TYPE_RESP_REQUIRED
253 );
254 if (status != ACE_STATUS_OK) {
255 fprintf(stderr, "Cannot write characteristic, status: %d\n", status);
256 return -12;
257 }
258 for (int j = 0; j < 10; j++) {
259 if (wrote_characteristic) break;
260 sleep(1);
261 }
262 if (!wrote_characteristic) {
263 fprintf(stderr, "Timed out waiting to write characteristic, status: %d\n", status);
264 return -12;
265 }
266 wrote_characteristic = false;
267 freeGattBlob(&charac->value);
268 }
269
270 sleep(2);
271
272 status = bleDisconnect(conn_handle);
273 printf("Disconnected from BLE status: %d\n", status);
274
275 status = bleDeregisterGattClient(bt_session);
276 printf("Deregistered GATT Client status: %d\n", status);
277
278 status = bleDeregister(bt_session);
279 printf("Deregistered BLE status: %d\n", status);
280
281 status = closeSession(bt_session);
282 printf("Closed session status: %d\n", status);
283
284 return 0;
285}
aceBT_bleGattClientCallbacks_t bleGattClientCallbacks_t
Callback struct of GATT Client Bluetooth operations.
aceBT_bdAddr_t bdAddr_t
Bluetooth address.
ace_status_t status_t
Bluetooth API status codes.
aceBT_bleGattBlobValue_t bleGattBlobValue_t
BLE blob value.
aceBT_bleGattsService_t bleGattsService_t
Structure for a GATT Server service.
aceBT_uuid_t uuid_t
Bluetooth UUID struct.
aceBT_bleConnHandle bleConnHandle
Connection handle for the lifetime of a Bluetooth connection.
#define BLE_FORMAT_BLOB
BLOB format.
aceBT_sessionHandle sessionHandle
Session handle for the lifetime of the Bluetooth application.
aceBT_bleGattCharacteristicsValue_t bleGattCharacteristicsValue_t
BLE GATT Characteristic.
status_t bleWriteCharacteristic(sessionHandle session_handle, bleConnHandle conn_handle, bleGattCharacteristicsValue_t *chars_value, responseType_t request_type)
Write a Characteristic from a BLE device.
Definition kindlebt.c:151
void kindlebt_set_log_level(log_level_t level)
Set the log level for log.c.
Definition kindlebt_log.c:9
status_t openSession(sessionType_t session_type, sessionHandle *session_handle)
Open a Bluetooth session.
Definition kindlebt.c:48
status_t bleRegisterGattClient(sessionHandle session_handle, bleGattClientCallbacks_t *callbacks)
Register as a BLE GATT Client.
Definition kindlebt.c:79
status_t bleGetDatabase(bleConnHandle conn_handle, bleGattsService_t **services_out, uint32_t *services_count)
Retrieve all services of a remote GATT Server.
Definition kindlebt.c:105
status_t bleRegister(sessionHandle session_handle)
Register as BLE client.
Definition kindlebt.c:54
struct aceBT_gattCharRec_t * utilsFindCharRec(bleGattsService_t *services, uint32_t noSvcs, uuid_t uuid, uint8_t uuid_len)
Find a GATT Characteristic Record by UUID.
status_t bleSetNotification(sessionHandle session_handle, bleConnHandle conn_handle, bleGattCharacteristicsValue_t chars_value, bool enable)
Set notifications on a Characteristic from a BLE device.
Definition kindlebt.c:165
status_t closeSession(sessionHandle session_handle)
Close a Bluetooth session.
Definition kindlebt.c:52
status_t bleDeregister(sessionHandle session_handle)
Deregister as BLE client.
Definition kindlebt.c:68
bool isBLESupported(void)
Is BLE supported by the hardware Bluetooth adapter.
Definition kindlebt.c:40
status_t bleReadCharacteristic(sessionHandle session_handle, bleConnHandle conn_handle, bleGattCharacteristicsValue_t chars_value)
Read a Characteristic from a BLE device.
Definition kindlebt.c:144
status_t bleDiscoverAllServices(sessionHandle session_handle, bleConnHandle conn_handle)
Discover all services of a remote GATT Server.
Definition kindlebt.c:95
status_t bleConnect(sessionHandle session_handle, bleConnHandle *conn_handle, bdAddr_t *p_device, bleConnParam_t conn_param, bleConnRole_t conn_role, bleConnPriority_t conn_priority)
Connect to a BLE device.
Definition kindlebt.c:126
status_t bleDisconnect(bleConnHandle conn_handle)
Disconnect from a BLE device.
Definition kindlebt.c:142
status_t bleDeregisterGattClient(sessionHandle session_handle)
Deregister as a BLE GATT Client.
Definition kindlebt.c:91
Bluetooth library for Kindles.