10#include <kindlebt/kindlebt_log.h>
12#include "kindlebt_application.c"
13#include "kindlebt_utils.c"
15pthread_mutex_t callback_vars_lock = PTHREAD_MUTEX_INITIALIZER;
16pthread_cond_t callback_vars_cond = PTHREAD_COND_INITIALIZER;
25void adapterStateCallback(
state_t state) {
26 if (state == ACEBT_STATE_ENABLED) {
27 log_debug(
"Callback %s(): state: STATE_ENABLED", __func__);
29 &callback_vars_lock, &callback_vars_cond, &
callback_vars.bt_enabled,
true
31 }
else if (state == ACEBT_STATE_DISABLED) {
32 log_debug(
"Callback %s(): state: STATE_DISABLED", __func__);
34 &callback_vars_lock, &callback_vars_cond, &
callback_vars.bt_enabled,
false
39void bondStateCallback(
status_t status,
bdAddr_t* p_remote_addr, aceBT_bondState_t state) {
42 utilsConvertBdAddrToStr(p_remote_addr, addr);
45 case ACEBT_BOND_STATE_NONE:
46 log_debug(
"Callback %s(): status: %d addr: %s state: NONE", __func__, status, addr);
48 case ACEBT_BOND_STATE_BONDING:
49 log_debug(
"Callback %s(): status: %d addr: %s state: BONDING", __func__, status, addr);
51 case ACEBT_BOND_STATE_BONDED:
52 log_debug(
"Callback %s(): status: %d addr: %s state: BONDED", __func__, status, addr);
56 "Callback %s(): status: %d addr: %s state: UNKNOWN(%d)", __func__, status, addr, state
64 "Callback %s(): status %d, mtu %d, conn_handle %p", __func__, status, mtu, conn_handle
67 if (status == ACE_STATUS_OK)
68 setCallbackVariable(&callback_vars_lock, &callback_vars_cond, &
callback_vars.mtu_set,
true);
71void bleRegCallback(
status_t status) {
72 log_debug(
"Callback %s(): status: %d\n", __func__, status);
74 if (status == ACE_STATUS_OK)
76 &callback_vars_lock, &callback_vars_cond, &
callback_vars.ble_registered,
true
80void bleConnStateChangedCallback(
85 utilsConvertBdAddrToStr(p_addr, addr);
87 "Callback %s(): state %d, status %d, conn_handle %p addr %s", __func__, state, status,
93 log_error(
"Couldn't find device context for conn_handle %p", conn_handle);
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);
103 ctx->
handle = conn_handle;
104 }
else if (state == ACEBT_BLE_STATE_DISCONNECTED) {
105 log_info(
"BLE device %s disconnected", addr);
109 pthread_cond_signal(&ctx->
cond);
110 pthread_mutex_unlock(&ctx->
lock);
115 log_debug(
"Callback %s(): conn_handle %p status %d", __func__, conn_handle, status);
119 log_error(
"Couldn't find device context for conn_handle %p", conn_handle);
123 if (status == ACE_STATUS_OK) {
128void bleGattcGetDbCallback(
131 log_debug(
"Callback %s(): conn_handle %p no_svc %" PRIu32
"", __func__, conn_handle, no_svc);
135 log_error(
"Couldn't find device context for conn_handle %p", conn_handle);
139 log_error(
"GATT DB out pointers not set");
143 pthread_mutex_lock(&ctx->
lock);
145 if (clone_status != ACE_STATUS_OK) {
146 log_error(
"Error copying GATT Database %d", clone_status);
152 pthread_cond_signal(&ctx->
cond);
154 pthread_mutex_unlock(&ctx->
lock);
157void bleGattcNotifyCharsCallback(
160 log_debug(
"Callback %s(): conn_handle %p", __func__, conn_handle);
163 utilsPrintUuid(buff, &chars_value.gattRecord.uuid, 256);
164 log_debug(
"%s() UUID:: %s", __func__, buff);
166 size_t size = 1024, offset = 0;
167 char* log_buff = malloc(size);
168 if (!log_buff)
return;
170 log_buff = append_to_buffer(log_buff, &size, &offset,
"%s() DATA:: ", __func__);
171 for (
int idx = 0; idx < chars_value.blobValue.size; idx++)
173 append_to_buffer(log_buff, &size, &offset,
"%x", chars_value.blobValue.data[idx]);
175 log_debug(
"%s", log_buff);
179void bleGattcReadCharsCallback(
182 log_debug(
"Callback %s(): status %d conn_handle %p", __func__, status, conn_handle);
185 utilsPrintUuid(buff, &chars_value.gattRecord.uuid, 256);
186 log_debug(
"%s() UUID:: %s", __func__, buff);
188 size_t size = 1024, offset = 0;
189 char* log_buff = malloc(size);
190 if (!log_buff)
return;
192 log_buff = append_to_buffer(log_buff, &size, &offset,
"%s() DATA:: ", __func__);
193 for (
int idx = 0; idx < chars_value.blobValue.size; idx++)
195 append_to_buffer(log_buff, &size, &offset,
"%x", chars_value.blobValue.data[idx]);
197 log_debug(
"%s", log_buff);
201void bleGattcWriteCharsCallback(
205 "Callback %s(): conn_handle %p GATT format %u", __func__, conn_handle,
206 gatt_characteristics.format
212 bleGattcServiceDiscoveredCallback(conn_handle, status);
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);
219void bleGattcGetDbCallbackWrapper(
222 bleGattcGetDbCallback(conn_handle, gatt_service, no_svc);
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
231void bleGattcNotifyCharsCallbackWrapper(
236 if (application_gatt_client_callbacks.notify_characteristics_cb != NULL) {
237 application_gatt_client_callbacks.notify_characteristics_cb(conn_handle, chars_value);
241void bleGattcReadCharsCallbackWrapper(
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
253void bleGattcWriteCharsCallbackWrapper(
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
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.
uint32_t * gattdb_count_out
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