kindlebt
Bluetooth functionality for Kindle 11th gen and up
Loading...
Searching...
No Matches
kindlebt_callbacks.c
1#include <inttypes.h>
2#include <pthread.h>
3#include <stdbool.h>
4#include <string.h>
5
6#include "log.h"
7
8#include <kindlebt/kindlebt.h>
10#include <kindlebt/kindlebt_log.h>
11
12#include "kindlebt_application.c"
13#include "kindlebt_utils.c"
14
15pthread_mutex_t callback_vars_lock = PTHREAD_MUTEX_INITIALIZER;
16pthread_cond_t callback_vars_cond = PTHREAD_COND_INITIALIZER;
19// GATT get DB global variables
20uint32_t gNo_svc;
22
23void adapterStateCallback(state_t state) {
24 if (state == ACEBT_STATE_ENABLED) {
25 log_debug("Callback %s(): state: STATE_ENABLED", __func__);
26 setCallbackVariable(
27 &callback_vars_lock, &callback_vars_cond, &callback_vars.bt_enabled, true
28 );
29 } else if (state == ACEBT_STATE_DISABLED) {
30 log_debug("Callback %s(): state: STATE_DISABLED", __func__);
31 setCallbackVariable(
32 &callback_vars_lock, &callback_vars_cond, &callback_vars.bt_enabled, false
33 );
34 }
35}
36
37void bondStateCallback(status_t status, bdAddr_t* p_remote_addr, aceBT_bondState_t state) {
38 char addr[MAC_ADDR_STR_LEN];
39 memset(addr, 0, MAC_ADDR_STR_LEN);
40 utilsConvertBdAddrToStr(p_remote_addr, addr);
41
42 switch (state) {
43 case ACEBT_BOND_STATE_NONE:
44 log_debug("Callback %s(): status: %d addr: %s state: NONE", __func__, status, addr);
45 break;
46 case ACEBT_BOND_STATE_BONDING:
47 log_debug("Callback %s(): status: %d addr: %s state: BONDING", __func__, status, addr);
48 break;
49 case ACEBT_BOND_STATE_BONDED:
50 log_debug("Callback %s(): status: %d addr: %s state: BONDED", __func__, status, addr);
51 break;
52 default:
53 log_debug(
54 "Callback %s(): status: %d addr: %s state: UNKNOWN(%d)", __func__, status, addr, state
55 );
56 break;
57 }
58}
59
60void bleMtuUpdatedCallback(status_t status, bleConnHandle conn_handle, int mtu) {
61 log_debug(
62 "Callback %s(): status %d, mtu %d, conn_handle %p", __func__, status, mtu, conn_handle
63 );
64
65 if (status == ACE_STATUS_OK)
66 setCallbackVariable(&callback_vars_lock, &callback_vars_cond, &callback_vars.mtu_set, true);
67}
68
69void bleRegCallback(status_t status) {
70 log_debug("Callback %s(): status: %d\n", __func__, status);
71
72 if (status == ACE_STATUS_OK)
73 setCallbackVariable(
74 &callback_vars_lock, &callback_vars_cond, &callback_vars.ble_registered, true
75 );
76}
77
78void bleConnStateChangedCallback(
79 bleConnState_t state, gattStatus_t status, const bleConnHandle conn_handle, bdAddr_t* p_addr
80) {
81 char addr[MAC_ADDR_STR_LEN];
82 memset(addr, 0, MAC_ADDR_STR_LEN);
83 utilsConvertBdAddrToStr(p_addr, addr);
84 log_debug(
85 "Callback %s(): state %d, status %d, conn_handle %p addr %s", __func__, state, status,
86 conn_handle, addr
87 );
88
89 ble_conn_handle = conn_handle;
90
91 if (status == ACEBT_GATT_STATUS_SUCCESS) {
92 if (state == ACEBT_BLE_STATE_CONNECTED) {
93 log_info("BLE device %s connected", addr);
94 setCallbackVariable(
95 &callback_vars_lock, &callback_vars_cond, &callback_vars.gattc_connected, true
96 );
97 setCallbackVariable(
98 &callback_vars_lock, &callback_vars_cond, &callback_vars.gattc_disconnected, false
99 );
100 } else if (state == ACEBT_BLE_STATE_DISCONNECTED) {
101 log_info("BLE device %s disconnected", addr);
102 setCallbackVariable(
103 &callback_vars_lock, &callback_vars_cond, &callback_vars.gattc_connected, false
104 );
105 setCallbackVariable(
106 &callback_vars_lock, &callback_vars_cond, &callback_vars.gattc_disconnected, true
107 );
108 }
109 }
110}
111
112void bleGattcServiceDiscoveredCallback(bleConnHandle conn_handle, status_t status) {
113 log_debug("Callback %s(): conn_handle %p status %d", __func__, conn_handle, status);
114
115 if (status == ACE_STATUS_OK) {
116 setCallbackVariable(
117 &callback_vars_lock, &callback_vars_cond, &callback_vars.gattc_discovered, true
118 );
119 }
120}
121
122void bleGattcGetDbCallback(
123 bleConnHandle conn_handle, bleGattsService_t* gatt_service, uint32_t no_svc
124) {
125 log_debug("Callback %s(): conn_handle %p no_svc %" PRIu32 "", __func__, conn_handle, no_svc);
126
127 gNo_svc = no_svc;
128 status_t status = bleCloneGattService(&pGgatt_service, gatt_service, gNo_svc);
129
130 if (status != ACE_STATUS_OK) {
131 log_error("Error copying GATT Database %d\n", status);
132 return;
133 }
134
135 size_t size = 1024, offset = 0;
136 char* log_buff = malloc(size);
137 if (!log_buff) return;
138
139 for (uint32_t i = 0; i < no_svc; i++) {
140 log_buff = append_to_buffer(
141 log_buff, &size, &offset, "GATT Database index :%" PRIu32 " %p\n", i, &pGgatt_service[i]
142 );
143 log_buff = utilsDumpServer(&pGgatt_service[i], log_buff, &size, &offset);
144 }
145
146 log_debug("%s", log_buff);
147 free(log_buff);
148
149 setCallbackVariable(&callback_vars_lock, &callback_vars_cond, &callback_vars.got_gatt_db, true);
150}
151
152void bleGattcNotifyCharsCallback(
153 bleConnHandle conn_handle, bleGattCharacteristicsValue_t chars_value
154) {
155 log_debug("Callback %s(): conn_handle %p", __func__, conn_handle);
156
157 char buff[256];
158 utilsPrintUuid(buff, &chars_value.gattRecord.uuid, 256);
159 log_debug("%s() UUID:: %s", __func__, buff);
160
161 size_t size = 1024, offset = 0;
162 char* log_buff = malloc(size);
163 if (!log_buff) return;
164
165 log_buff = append_to_buffer(log_buff, &size, &offset, "%s() DATA:: ", __func__);
166 for (int idx = 0; idx < chars_value.blobValue.size; idx++)
167 log_buff =
168 append_to_buffer(log_buff, &size, &offset, "%x", chars_value.blobValue.data[idx]);
169
170 log_debug("%s", log_buff);
171 free(log_buff);
172}
173
174void bleGattcReadCharsCallback(
175 bleConnHandle conn_handle, bleGattCharacteristicsValue_t chars_value, status_t status
176) {
177 log_debug("Callback %s(): status %d conn_handle %p", __func__, status, conn_handle);
178
179 char buff[256];
180 utilsPrintUuid(buff, &chars_value.gattRecord.uuid, 256);
181 log_debug("%s() UUID:: %s", __func__, buff);
182
183 size_t size = 1024, offset = 0;
184 char* log_buff = malloc(size);
185 if (!log_buff) return;
186
187 log_buff = append_to_buffer(log_buff, &size, &offset, "%s() DATA:: ", __func__);
188 for (int idx = 0; idx < chars_value.blobValue.size; idx++)
189 log_buff =
190 append_to_buffer(log_buff, &size, &offset, "%x", chars_value.blobValue.data[idx]);
191
192 log_debug("%s", log_buff);
193 free(log_buff);
194}
195
196void bleGattcWriteCharsCallback(
197 bleConnHandle conn_handle, bleGattCharacteristicsValue_t gatt_characteristics, status_t status
198) {
199 log_debug(
200 "Callback %s(): conn_handle %p GATT format %u", __func__, conn_handle,
201 gatt_characteristics.format
202 );
203}
204
205// Wrappers needed for when we need to share callbacks between library and application
206void bleGattcServiceDiscoveredCallbackWrapper(bleConnHandle conn_handle, status_t status) {
207 bleGattcServiceDiscoveredCallback(conn_handle, status);
208
209 if (application_gatt_client_callbacks.on_ble_gattc_service_discovered_cb != NULL) {
210 application_gatt_client_callbacks.on_ble_gattc_service_discovered_cb(conn_handle, status);
211 }
212}
213
214void bleGattcGetDbCallbackWrapper(
215 bleConnHandle conn_handle, bleGattsService_t* gatt_service, uint32_t no_svc
216) {
217 bleGattcGetDbCallback(conn_handle, gatt_service, no_svc);
218
219 if (application_gatt_client_callbacks.on_ble_gattc_get_gatt_db_cb != NULL) {
220 application_gatt_client_callbacks.on_ble_gattc_get_gatt_db_cb(
221 conn_handle, gatt_service, no_svc
222 );
223 }
224}
225
226void bleGattcNotifyCharsCallbackWrapper(
227 bleConnHandle conn_handle, bleGattCharacteristicsValue_t chars_value
228) {
229 // bleGattcNotifyCharsCallback(conn_handle, chars_value);
230
231 if (application_gatt_client_callbacks.notify_characteristics_cb != NULL) {
232 application_gatt_client_callbacks.notify_characteristics_cb(conn_handle, chars_value);
233 }
234}
235
236void bleGattcReadCharsCallbackWrapper(
237 bleConnHandle conn_handle, bleGattCharacteristicsValue_t chars_value, status_t status
238) {
239 // bleGattcReadCharsCallback(conn_handle, chars_value, status);
240
241 if (application_gatt_client_callbacks.on_ble_gattc_read_characteristics_cb != NULL) {
242 application_gatt_client_callbacks.on_ble_gattc_read_characteristics_cb(
243 conn_handle, chars_value, status
244 );
245 }
246}
247
248void bleGattcWriteCharsCallbackWrapper(
249 bleConnHandle conn_handle, bleGattCharacteristicsValue_t gatt_characteristics, status_t status
250) {
251 // bleGattcWriteCharsCallback(conn_handle, gatt_characteristics, status);
252
253 if (application_gatt_client_callbacks.on_ble_gattc_write_characteristics_cb != NULL) {
254 application_gatt_client_callbacks.on_ble_gattc_write_characteristics_cb(
255 conn_handle, gatt_characteristics, status
256 );
257 }
258}
aceBT_state_t state_t
Bluetooth radio state.
#define MAC_ADDR_STR_LEN
MAC address string representation length (XX:XX:XX:XX:XX:XX)
aceBT_bleConnState_t bleConnState_t
BLE connection state.
aceBT_bdAddr_t bdAddr_t
Bluetooth address.
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_bleGattCharacteristicsValue_t bleGattCharacteristicsValue_t
BLE GATT Characteristic.
aceBT_gattStatus_t gattStatus_t
BLE GATT status.
Bluetooth library for Kindles.
status_t bleCloneGattService(bleGattsService_t **dst_gatt_service, const bleGattsService_t *src_gatt_service, int no_svc)
[Internal] Clone a GATT Service
Definition kindlebt.c:117
Internal definitions.
bleConnHandle ble_conn_handle
Internal BLE connection handle.
bleGattsService_t * pGgatt_service
Internal reference to a GATT service.
bleCallbackVars_t callback_vars
Internal bleCallbackVars_t.
uint32_t gNo_svc
Internal number of GATT services, often paired with pGgatt_service.
Internal struct for certain Bluetooth conditions.