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
24
25void adapterStateCallback(state_t state) {
26 if (state == ACEBT_STATE_ENABLED) {
27 log_debug("Callback %s(): state: STATE_ENABLED", __func__);
28 setCallbackVariable(
29 &callback_vars_lock, &callback_vars_cond, &callback_vars.bt_enabled, true
30 );
31 } else if (state == ACEBT_STATE_DISABLED) {
32 log_debug("Callback %s(): state: STATE_DISABLED", __func__);
33 setCallbackVariable(
34 &callback_vars_lock, &callback_vars_cond, &callback_vars.bt_enabled, false
35 );
36 }
37}
38
39void bondStateCallback(status_t status, bdAddr_t* p_remote_addr, aceBT_bondState_t state) {
40 char addr[MAC_ADDR_STR_LEN];
41 memset(addr, 0, MAC_ADDR_STR_LEN);
42 utilsConvertBdAddrToStr(p_remote_addr, addr);
43
44 switch (state) {
45 case ACEBT_BOND_STATE_NONE:
46 log_debug("Callback %s(): status: %d addr: %s state: NONE", __func__, status, addr);
47 break;
48 case ACEBT_BOND_STATE_BONDING:
49 log_debug("Callback %s(): status: %d addr: %s state: BONDING", __func__, status, addr);
50 break;
51 case ACEBT_BOND_STATE_BONDED:
52 log_debug("Callback %s(): status: %d addr: %s state: BONDED", __func__, status, addr);
53 break;
54 default:
55 log_debug(
56 "Callback %s(): status: %d addr: %s state: UNKNOWN(%d)", __func__, status, addr, state
57 );
58 break;
59 }
60}
61
62void bleMtuUpdatedCallback(status_t status, bleConnHandle conn_handle, int mtu) {
63 log_debug(
64 "Callback %s(): status %d, mtu %d, conn_handle %p", __func__, status, mtu, conn_handle
65 );
66
67 if (status == ACE_STATUS_OK)
68 setCallbackVariable(&callback_vars_lock, &callback_vars_cond, &callback_vars.mtu_set, true);
69}
70
71void bleRegCallback(status_t status) {
72 log_debug("Callback %s(): status: %d\n", __func__, status);
73
74 if (status == ACE_STATUS_OK)
75 setCallbackVariable(
76 &callback_vars_lock, &callback_vars_cond, &callback_vars.ble_registered, true
77 );
78}
79
80void bleConnStateChangedCallback(
81 bleConnState_t state, gattStatus_t status, const bleConnHandle conn_handle, bdAddr_t* p_addr
82) {
83 char addr[MAC_ADDR_STR_LEN];
84 memset(addr, 0, MAC_ADDR_STR_LEN);
85 utilsConvertBdAddrToStr(p_addr, addr);
86 log_debug(
87 "Callback %s(): state %d, status %d, conn_handle %p addr %s", __func__, state, status,
88 conn_handle, addr
89 );
90
91 device_context_t* ctx = dca_find_by_addr(&devices_context, p_addr);
92 if (!ctx) {
93 log_error("Couldn't find device context for conn_handle %p", conn_handle);
94 return;
95 }
96
97 if (status == ACEBT_GATT_STATUS_SUCCESS) {
98 pthread_mutex_lock(&ctx->lock);
99 if (state == ACEBT_BLE_STATE_CONNECTED) {
100 log_info("BLE device %s connected", addr);
101 ctx->gattc_connected = true;
102 ctx->gattc_disconnected = false;
103 ctx->handle = conn_handle;
104 } else if (state == ACEBT_BLE_STATE_DISCONNECTED) {
105 log_info("BLE device %s disconnected", addr);
106 ctx->gattc_connected = false;
107 ctx->gattc_disconnected = true;
108 }
109 pthread_cond_signal(&ctx->cond);
110 pthread_mutex_unlock(&ctx->lock);
111 }
112}
113
114void bleGattcServiceDiscoveredCallback(bleConnHandle conn_handle, status_t status) {
115 log_debug("Callback %s(): conn_handle %p status %d", __func__, conn_handle, status);
116
117 device_context_t* ctx = dca_find_by_handle(&devices_context, conn_handle);
118 if (!ctx) {
119 log_error("Couldn't find device context for conn_handle %p", conn_handle);
120 return;
121 }
122
123 if (status == ACE_STATUS_OK) {
124 setCallbackVariable(&ctx->lock, &ctx->cond, &ctx->gattc_discovered, true);
125 }
126}
127
128void bleGattcGetDbCallback(
129 bleConnHandle conn_handle, bleGattsService_t* gatt_service, uint32_t no_svc
130) {
131 log_debug("Callback %s(): conn_handle %p no_svc %" PRIu32 "", __func__, conn_handle, no_svc);
132
133 device_context_t* ctx = dca_find_by_handle(&devices_context, conn_handle);
134 if (!ctx) {
135 log_error("Couldn't find device context for conn_handle %p", conn_handle);
136 return;
137 }
138 if (!ctx->gattdb_out || !ctx->gattdb_count_out) {
139 log_error("GATT DB out pointers not set");
140 return;
141 }
142
143 pthread_mutex_lock(&ctx->lock);
144 status_t clone_status = bleCloneGattService(ctx->gattdb_out, gatt_service, no_svc);
145 if (clone_status != ACE_STATUS_OK) {
146 log_error("Error copying GATT Database %d", clone_status);
147 return;
148 }
149 *(ctx->gattdb_count_out) = no_svc;
150
151 ctx->gattdb_retrieved = true;
152 pthread_cond_signal(&ctx->cond);
153
154 pthread_mutex_unlock(&ctx->lock);
155}
156
157void bleGattcNotifyCharsCallback(
158 bleConnHandle conn_handle, bleGattCharacteristicsValue_t chars_value
159) {
160 log_debug("Callback %s(): conn_handle %p", __func__, conn_handle);
161
162 char buff[256];
163 utilsPrintUuid(buff, &chars_value.gattRecord.uuid, 256);
164 log_debug("%s() UUID:: %s", __func__, buff);
165
166 size_t size = 1024, offset = 0;
167 char* log_buff = malloc(size);
168 if (!log_buff) return;
169
170 log_buff = append_to_buffer(log_buff, &size, &offset, "%s() DATA:: ", __func__);
171 for (int idx = 0; idx < chars_value.blobValue.size; idx++)
172 log_buff =
173 append_to_buffer(log_buff, &size, &offset, "%x", chars_value.blobValue.data[idx]);
174
175 log_debug("%s", log_buff);
176 free(log_buff);
177}
178
179void bleGattcReadCharsCallback(
180 bleConnHandle conn_handle, bleGattCharacteristicsValue_t chars_value, status_t status
181) {
182 log_debug("Callback %s(): status %d conn_handle %p", __func__, status, conn_handle);
183
184 char buff[256];
185 utilsPrintUuid(buff, &chars_value.gattRecord.uuid, 256);
186 log_debug("%s() UUID:: %s", __func__, buff);
187
188 size_t size = 1024, offset = 0;
189 char* log_buff = malloc(size);
190 if (!log_buff) return;
191
192 log_buff = append_to_buffer(log_buff, &size, &offset, "%s() DATA:: ", __func__);
193 for (int idx = 0; idx < chars_value.blobValue.size; idx++)
194 log_buff =
195 append_to_buffer(log_buff, &size, &offset, "%x", chars_value.blobValue.data[idx]);
196
197 log_debug("%s", log_buff);
198 free(log_buff);
199}
200
201void bleGattcWriteCharsCallback(
202 bleConnHandle conn_handle, bleGattCharacteristicsValue_t gatt_characteristics, status_t status
203) {
204 log_debug(
205 "Callback %s(): conn_handle %p GATT format %u", __func__, conn_handle,
206 gatt_characteristics.format
207 );
208}
209
210// Wrappers needed for when we need to share callbacks between library and application
211void bleGattcServiceDiscoveredCallbackWrapper(bleConnHandle conn_handle, status_t status) {
212 bleGattcServiceDiscoveredCallback(conn_handle, status);
213
214 if (application_gatt_client_callbacks.on_ble_gattc_service_discovered_cb != NULL) {
215 application_gatt_client_callbacks.on_ble_gattc_service_discovered_cb(conn_handle, status);
216 }
217}
218
219void bleGattcGetDbCallbackWrapper(
220 bleConnHandle conn_handle, bleGattsService_t* gatt_service, uint32_t no_svc
221) {
222 bleGattcGetDbCallback(conn_handle, gatt_service, no_svc);
223
224 if (application_gatt_client_callbacks.on_ble_gattc_get_gatt_db_cb != NULL) {
225 application_gatt_client_callbacks.on_ble_gattc_get_gatt_db_cb(
226 conn_handle, gatt_service, no_svc
227 );
228 }
229}
230
231void bleGattcNotifyCharsCallbackWrapper(
232 bleConnHandle conn_handle, bleGattCharacteristicsValue_t chars_value
233) {
234 // bleGattcNotifyCharsCallback(conn_handle, chars_value);
235
236 if (application_gatt_client_callbacks.notify_characteristics_cb != NULL) {
237 application_gatt_client_callbacks.notify_characteristics_cb(conn_handle, chars_value);
238 }
239}
240
241void bleGattcReadCharsCallbackWrapper(
242 bleConnHandle conn_handle, bleGattCharacteristicsValue_t chars_value, status_t status
243) {
244 // bleGattcReadCharsCallback(conn_handle, chars_value, status);
245
246 if (application_gatt_client_callbacks.on_ble_gattc_read_characteristics_cb != NULL) {
247 application_gatt_client_callbacks.on_ble_gattc_read_characteristics_cb(
248 conn_handle, chars_value, status
249 );
250 }
251}
252
253void bleGattcWriteCharsCallbackWrapper(
254 bleConnHandle conn_handle, bleGattCharacteristicsValue_t gatt_characteristics, status_t status
255) {
256 // bleGattcWriteCharsCallback(conn_handle, gatt_characteristics, status);
257
258 if (application_gatt_client_callbacks.on_ble_gattc_write_characteristics_cb != NULL) {
259 application_gatt_client_callbacks.on_ble_gattc_write_characteristics_cb(
260 conn_handle, gatt_characteristics, status
261 );
262 }
263}
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.
pthread_cond_t cond
pthread_mutex_t lock
uint32_t * gattdb_count_out
bleConnHandle handle
bleGattsService_t ** gattdb_out
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.
device_context_array_t devices_context
Internal device_context_array_t.
uint32_t gNo_svc
Internal number of GATT services, often paired with pGgatt_service.
Internal struct for certain Bluetooth conditions.
Internal dynamic array struct for devices context during a BLE connection.
Internal struct for certain Bluetooth conditions during a BLE connection.
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:128
Internal definitions.