kindlebt
Bluetooth functionality for Kindle 11th gen and up
Loading...
Searching...
No Matches
compat_ace_shims.c
1#define _GNU_SOURCE
2
3#include <kindlebt/compat_ace_shims.h>
4
5#include <dlfcn.h>
6#include <stdbool.h>
7
8#include "log.h"
9
10#include <kindlebt/compat_ace_implementations.h>
11
12/* Internal typedefs matching the two getSessionFor* functions
13 * getSessionForCallback < 5.17
14 * getSessionForTask >= 5.17
15 * Our shim is getSessionFromHandler
16 */
17typedef sessionHandle (*getSessionForCallback_fn_t)(uint16_t);
18typedef sessionHandle (*getSessionForTask_fn_t)(aceAipc_parameter_t*);
19
20sessionHandle getSessionFromHandler(aceAipc_parameter_t* task) {
21 static getSessionForCallback_fn_t old_api = NULL;
22 static getSessionForTask_fn_t new_api = NULL;
23 static bool initialized = false;
24
25 if (!initialized) {
26 new_api = (getSessionForTask_fn_t)dlsym(RTLD_DEFAULT, "getSessionForTask");
27 if (!new_api) {
28 old_api = (getSessionForCallback_fn_t)dlsym(RTLD_DEFAULT, "getSessionForCallback");
29 }
30 initialized = true;
31 }
32
33 if (new_api) {
34 return new_api(task);
35 } else if (old_api) {
36 return old_api(task->callback_id);
37 } else {
38 // Nothing matched. We shouldn't reach this
39 log_error("[%s()]: couldn't match any of the expected getSessionFor* symbols", __func__);
40 return (sessionHandle)-1;
41 }
42}
43
44typedef status_t (*aceBt_bleRegisterGattClient_tn_t)(
46);
47
48status_t shim_bleRegisterGattClient(
49 sessionHandle session_handle, bleGattClientCallbacks_t* callbacks, bleAppId_t app_id
50) {
51 static aceBt_bleRegisterGattClient_tn_t new_api = NULL;
52
53#ifndef FORCE_OLD_API
54 static bool initialized = false;
55
56 if (!initialized) {
57 new_api =
58 (aceBt_bleRegisterGattClient_tn_t)dlsym(RTLD_DEFAULT, "aceBt_bleRegisterGattClient");
59 initialized = true;
60 }
61#endif
62
63 if (new_api) {
64 return new_api(session_handle, callbacks, app_id);
65 } else {
66 return pre5170_bleRegisterGattClient(session_handle, callbacks, app_id);
67 }
68}
69
70typedef status_t (*aceBT_bleDeRegisterGattClient_fn_t)(sessionHandle);
71
72status_t shim_bleDeregisterGattClient(sessionHandle session_handle) {
73 static aceBT_bleDeRegisterGattClient_fn_t new_api = NULL;
74
75#ifndef FORCE_OLD_API
76 static bool initialized = false;
77
78 if (!initialized) {
79 new_api = (aceBT_bleDeRegisterGattClient_fn_t)dlsym(
80 RTLD_DEFAULT, "aceBT_bleDeRegisterGattClient"
81 );
82 initialized = true;
83 }
84#endif
85
86 if (new_api) {
87 return new_api(session_handle);
88 } else {
89 return pre5170_bleDeregisterGattClient(session_handle);
90 }
91}
92
93typedef status_t (*aceBT_bleDiscoverAllServices_fn_t)(sessionHandle, bleConnHandle);
94
95status_t shim_bleDiscoverAllServices(sessionHandle session_handle, bleConnHandle conn_handle) {
96 static aceBT_bleDiscoverAllServices_fn_t new_api = NULL;
97
98#ifndef FORCE_OLD_API
99 static bool initialized = false;
100
101 if (!initialized) {
102 new_api =
103 (aceBT_bleDiscoverAllServices_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleDiscoverAllServices");
104 initialized = true;
105 }
106#endif
107
108 if (new_api) {
109 return new_api(session_handle, conn_handle);
110 } else {
111 return pre5170_bleDiscoverAllServices(session_handle, conn_handle);
112 }
113}
114
115typedef status_t (*aceBT_bleGetService_fn_t)(bleConnHandle);
116
117status_t shim_bleGetDatabase(bleConnHandle conn_handle) {
118 static aceBT_bleGetService_fn_t new_api = NULL;
119
120#ifndef FORCE_OLD_API
121 static bool initialized = false;
122
123 if (!initialized) {
124 new_api = (aceBT_bleGetService_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleGetService");
125 initialized = true;
126 }
127#endif
128
129 if (new_api) {
130 return new_api(conn_handle);
131 } else {
132 return pre5170_bleGetService(conn_handle);
133 }
134}
135
136typedef status_t (*aceBT_bleReadCharacteristics_fn_t)(
138);
139
140status_t shim_bleReadCharacteristic(
141 sessionHandle session_handle, bleConnHandle conn_handle,
143) {
144 static aceBT_bleReadCharacteristics_fn_t new_api = NULL;
145
146#ifndef FORCE_OLD_API
147 static bool initialized = false;
148
149 if (!initialized) {
150 new_api =
151 (aceBT_bleReadCharacteristics_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleReadCharacteristics");
152 initialized = true;
153 }
154#endif
155
156 if (new_api) {
157 return new_api(session_handle, conn_handle, chars_value);
158 } else {
159 return pre5170_bleReadCharacteristic(session_handle, conn_handle, chars_value);
160 }
161}
162
163typedef status_t (*aceBT_bleWriteCharacteristics_fn_t)(
165);
166
167status_t shim_bleWriteCharacteristic(
168 sessionHandle session_handle, bleConnHandle conn_handle,
169 bleGattCharacteristicsValue_t* chars_value, responseType_t request_type
170) {
171 static aceBT_bleWriteCharacteristics_fn_t new_api = NULL;
172
173#ifndef FORCE_OLD_API
174 static bool initialized = false;
175
176 if (!initialized) {
177 new_api = (aceBT_bleWriteCharacteristics_fn_t)dlsym(
178 RTLD_DEFAULT, "aceBT_bleWriteCharacteristics"
179 );
180 initialized = true;
181 }
182#endif
183
184 if (new_api) {
185 return new_api(session_handle, conn_handle, chars_value, request_type);
186 } else {
187 return pre5170_bleWriteCharacteristics(
188 session_handle, conn_handle, chars_value, request_type
189 );
190 }
191}
192
193typedef status_t (*aceBT_bleWriteDescriptor_fn_t)(
195);
196
197status_t shim_bleWriteDescriptor(
198 sessionHandle session_handle, bleConnHandle conn_handle,
199 bleGattCharacteristicsValue_t* chars_value, responseType_t request_type
200) {
201 static aceBT_bleWriteDescriptor_fn_t new_api = NULL;
202
203#ifndef FORCE_OLD_API
204 static bool initialized = false;
205
206 if (!initialized) {
207 new_api =
208 (aceBT_bleWriteCharacteristics_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleWriteDescriptor");
209 }
210#endif
211
212 if (new_api) {
213 return new_api(session_handle, conn_handle, chars_value, request_type);
214 } else {
215 return pre5170_bleWriteDescriptor(session_handle, conn_handle, chars_value, request_type);
216 }
217}
218
219typedef status_t (*aceBT_bleSetNotification_fn_t)(
221);
222
223status_t shim_bleSetNotification(
224 sessionHandle session_handle, bleConnHandle conn_handle,
225 bleGattCharacteristicsValue_t chars_value, bool enable
226) {
227 static aceBT_bleSetNotification_fn_t new_api = NULL;
228
229#ifndef FORCE_OLD_API
230 static bool initialized = false;
231
232 if (!initialized) {
233 new_api = (aceBT_bleSetNotification_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleSetNotification");
234 initialized = true;
235 }
236#endif
237
238 if (new_api) {
239 return new_api(session_handle, conn_handle, chars_value, enable);
240 } else {
241 return pre5170_bleSetNotification(session_handle, conn_handle, chars_value, enable);
242 }
243}
aceBT_bleGattClientCallbacks_t bleGattClientCallbacks_t
Callback struct of GATT Client Bluetooth operations.
aceBt_bleAppId_t bleAppId_t
BLE application type.
aceBT_responseType_t responseType_t
Type of write operation for a BLE characteristic.
ace_status_t status_t
Bluetooth API status codes.
aceBT_bleConnHandle bleConnHandle
Connection handle for the lifetime of a Bluetooth connection.
aceBT_sessionHandle sessionHandle
Session handle for the lifetime of the Bluetooth application.
aceBT_bleGattCharacteristicsValue_t bleGattCharacteristicsValue_t
BLE GATT Characteristic.