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
80 )dlsym(RTLD_DEFAULT, "aceBT_bleDeRegisterGattClient");
81 initialized = true;
82 }
83#endif
84
85 if (new_api) {
86 return new_api(session_handle);
87 } else {
88 return pre5170_bleDeregisterGattClient(session_handle);
89 }
90}
91
92typedef status_t (*aceBT_bleDiscoverAllServices_fn_t)(sessionHandle, bleConnHandle);
93
94status_t shim_bleDiscoverAllServices(sessionHandle session_handle, bleConnHandle conn_handle) {
95 static aceBT_bleDiscoverAllServices_fn_t new_api = NULL;
96
97#ifndef FORCE_OLD_API
98 static bool initialized = false;
99
100 if (!initialized) {
101 new_api =
102 (aceBT_bleDiscoverAllServices_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleDiscoverAllServices");
103 initialized = true;
104 }
105#endif
106
107 if (new_api) {
108 return new_api(session_handle, conn_handle);
109 } else {
110 return pre5170_bleDiscoverAllServices(session_handle, conn_handle);
111 }
112}
113
114typedef status_t (*aceBT_bleGetService_fn_t)(bleConnHandle);
115
116status_t shim_bleGetDatabase(bleConnHandle conn_handle) {
117 static aceBT_bleGetService_fn_t new_api = NULL;
118
119#ifndef FORCE_OLD_API
120 static bool initialized = false;
121
122 if (!initialized) {
123 new_api = (aceBT_bleGetService_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleGetService");
124 initialized = true;
125 }
126#endif
127
128 if (new_api) {
129 return new_api(conn_handle);
130 } else {
131 return pre5170_bleGetService(conn_handle);
132 }
133}
134
135typedef status_t (*aceBT_bleReadCharacteristics_fn_t)(
137);
138
139status_t shim_bleReadCharacteristic(
140 sessionHandle session_handle, bleConnHandle conn_handle,
142) {
143 static aceBT_bleReadCharacteristics_fn_t new_api = NULL;
144
145#ifndef FORCE_OLD_API
146 static bool initialized = false;
147
148 if (!initialized) {
149 new_api =
150 (aceBT_bleReadCharacteristics_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleReadCharacteristics");
151 initialized = true;
152 }
153#endif
154
155 if (new_api) {
156 return new_api(session_handle, conn_handle, chars_value);
157 } else {
158 return pre5170_bleReadCharacteristic(session_handle, conn_handle, chars_value);
159 }
160}
161
162typedef status_t (*aceBT_bleWriteCharacteristics_fn_t)(
164);
165
166status_t shim_bleWriteCharacteristic(
167 sessionHandle session_handle, bleConnHandle conn_handle,
168 bleGattCharacteristicsValue_t* chars_value, responseType_t request_type
169) {
170 static aceBT_bleWriteCharacteristics_fn_t new_api = NULL;
171
172#ifndef FORCE_OLD_API
173 static bool initialized = false;
174
175 if (!initialized) {
176 new_api = (aceBT_bleWriteCharacteristics_fn_t
177 )dlsym(RTLD_DEFAULT, "aceBT_bleWriteCharacteristics");
178 initialized = true;
179 }
180#endif
181
182 if (new_api) {
183 return new_api(session_handle, conn_handle, chars_value, request_type);
184 } else {
185 return pre5170_bleWriteCharacteristics(
186 session_handle, conn_handle, chars_value, request_type
187 );
188 }
189}
190
191typedef status_t (*aceBT_bleWriteDescriptor_fn_t)(
193);
194
195status_t shim_bleWriteDescriptor(
196 sessionHandle session_handle, bleConnHandle conn_handle,
197 bleGattCharacteristicsValue_t* chars_value, responseType_t request_type
198) {
199 static aceBT_bleWriteDescriptor_fn_t new_api = NULL;
200
201#ifndef FORCE_OLD_API
202 static bool initialized = false;
203
204 if (!initialized) {
205 new_api =
206 (aceBT_bleWriteCharacteristics_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleWriteDescriptor");
207 }
208#endif
209
210 if (new_api) {
211 return new_api(session_handle, conn_handle, chars_value, request_type);
212 } else {
213 return pre5170_bleWriteDescriptor(session_handle, conn_handle, chars_value, request_type);
214 }
215}
216
217typedef status_t (*aceBT_bleSetNotification_fn_t)(
219);
220
221status_t shim_bleSetNotification(
222 sessionHandle session_handle, bleConnHandle conn_handle,
223 bleGattCharacteristicsValue_t chars_value, bool enable
224) {
225 static aceBT_bleSetNotification_fn_t new_api = NULL;
226
227#ifndef FORCE_OLD_API
228 static bool initialized = false;
229
230 if (!initialized) {
231 new_api = (aceBT_bleSetNotification_fn_t)dlsym(RTLD_DEFAULT, "aceBT_bleSetNotification");
232 initialized = true;
233 }
234#endif
235
236 if (new_api) {
237 return new_api(session_handle, conn_handle, chars_value, enable);
238 } else {
239 return pre5170_bleSetNotification(session_handle, conn_handle, chars_value, enable);
240 }
241}
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.