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_bleCleanupGattService_fn_t)(bleGattsService_t*, int);
137
138status_t shim_bleCleanupGattService(bleGattsService_t* service, int no_svc) {
139 static aceBT_bleCleanupGattService_fn_t new_api = NULL;
140
141#ifndef FORCE_OLD_API
142 static bool initialized = false;
143
144 if (!initialized) {
145 new_api =
146 (aceBT_bleCleanupGattService_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleCleanupGattService");
147 initialized = true;
148 }
149#endif
150
151 if (new_api) {
152 return new_api(service, no_svc);
153 } else {
154 return pre5170_bleCleanupGattService(service, no_svc);
155 }
156}
157
158typedef status_t (*aceBT_bleReadCharacteristics_fn_t)(
160);
161
162status_t shim_bleReadCharacteristic(
163 sessionHandle session_handle, bleConnHandle conn_handle,
165) {
166 static aceBT_bleReadCharacteristics_fn_t new_api = NULL;
167
168#ifndef FORCE_OLD_API
169 static bool initialized = false;
170
171 if (!initialized) {
172 new_api =
173 (aceBT_bleReadCharacteristics_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleReadCharacteristics");
174 initialized = true;
175 }
176#endif
177
178 if (new_api) {
179 return new_api(session_handle, conn_handle, chars_value);
180 } else {
181 return pre5170_bleReadCharacteristic(session_handle, conn_handle, chars_value);
182 }
183}
184
185typedef status_t (*aceBT_bleWriteCharacteristics_fn_t)(
187);
188
189status_t shim_bleWriteCharacteristic(
190 sessionHandle session_handle, bleConnHandle conn_handle,
191 bleGattCharacteristicsValue_t* chars_value, responseType_t request_type
192) {
193 static aceBT_bleWriteCharacteristics_fn_t new_api = NULL;
194
195#ifndef FORCE_OLD_API
196 static bool initialized = false;
197
198 if (!initialized) {
199 new_api = (aceBT_bleWriteCharacteristics_fn_t)dlsym(
200 RTLD_DEFAULT, "aceBT_bleWriteCharacteristics"
201 );
202 initialized = true;
203 }
204#endif
205
206 if (new_api) {
207 return new_api(session_handle, conn_handle, chars_value, request_type);
208 } else {
209 return pre5170_bleWriteCharacteristics(
210 session_handle, conn_handle, chars_value, request_type
211 );
212 }
213}
214
215typedef status_t (*aceBT_bleWriteDescriptor_fn_t)(
217);
218
219status_t shim_bleWriteDescriptor(
220 sessionHandle session_handle, bleConnHandle conn_handle,
221 bleGattCharacteristicsValue_t* chars_value, responseType_t request_type
222) {
223 static aceBT_bleWriteDescriptor_fn_t new_api = NULL;
224
225#ifndef FORCE_OLD_API
226 static bool initialized = false;
227
228 if (!initialized) {
229 new_api =
230 (aceBT_bleWriteCharacteristics_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleWriteDescriptor");
231 }
232#endif
233
234 if (new_api) {
235 return new_api(session_handle, conn_handle, chars_value, request_type);
236 } else {
237 return pre5170_bleWriteDescriptor(session_handle, conn_handle, chars_value, request_type);
238 }
239}
240
241typedef status_t (*aceBT_bleSetNotification_fn_t)(
243);
244
245status_t shim_bleSetNotification(
246 sessionHandle session_handle, bleConnHandle conn_handle,
247 bleGattCharacteristicsValue_t chars_value, bool enable
248) {
249 static aceBT_bleSetNotification_fn_t new_api = NULL;
250
251#ifndef FORCE_OLD_API
252 static bool initialized = false;
253
254 if (!initialized) {
255 new_api = (aceBT_bleSetNotification_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleSetNotification");
256 initialized = true;
257 }
258#endif
259
260 if (new_api) {
261 return new_api(session_handle, conn_handle, chars_value, enable);
262 } else {
263 return pre5170_bleSetNotification(session_handle, conn_handle, chars_value, enable);
264 }
265}
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_bleGattsService_t bleGattsService_t
Structure for a GATT Server service.
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.