kindlebt
Bluetooth functionality for Kindle 11th gen and up
Loading...
Searching...
No Matches
compat_ace_handler.c
1#include <kindlebt/compat_ace_handler.h>
2
3#include "log.h"
4
5#include <kindlebt/compat_ace_internals.h>
6#include <kindlebt/compat_ace_shims.h>
7#include <kindlebt/compat_ace_utils.h>
8
9void pre5170_gattc_cb_handler(aceAipc_parameter_t* task) {
10 log_debug("Called into pre 5.17 %s", __func__);
11
12 if (task == NULL) {
13 log_info("[%s()]: Server handler callback: data is null", __func__);
14 return;
15 }
16
17 printf("Dumping aceAipc_parameter_t memory:\n");
18 dump_hex(task, 128);
19 dump_aceAipc_parameter_t(task);
20
21 /* In AIPC callback this runs in server side callback context. Hence use the
22 callback id to retrive the session info*/
23 sessionHandle session_handle = getSessionFromHandler(task);
24 bleGattClientCallbacks_t* p_client_callbacks =
25 getBTClientData(session_handle, CALLBACK_INDEX_BLE_GATT_CLIENT);
26 if (session_handle == NULL || p_client_callbacks == NULL) {
27 log_error(
28 "[%s()]: Error invalid handle, session %p callback %p", __func__, session_handle,
29 p_client_callbacks
30 );
31 return;
32 }
33
34 // TODO: Actually implement these callbacks
35 switch ((ipc_evt_enum_t)task->function_id) {
36 case ACE_BT_CALLBACK_GATTC_SERVICE_REGISTERED: {
37 log_debug("BLE GATTC callback handler, case ACE_BT_CALLBACK_GATTC_SERVICE_REGISTERED");
38 log_debug("Not actually implemented at all in acebt, so ignore this log");
39 } break;
40 case ACE_BT_CALLBACK_GATTC_SERVICE_DISCOVERED: {
41 log_debug("BLE GATTC callback handler, case ACE_BT_CALLBACK_GATTC_SERVICE_DISCOVERED");
42 if (p_client_callbacks->on_ble_gattc_service_discovered_cb == NULL) {
43 log_error("[%s()]: on_ble_gattc_service_discovered_cb not implemented", __func__);
44 break;
45 }
46
47 dis_req_t* data = (dis_req_t*)task->buffer;
48 p_client_callbacks->on_ble_gattc_service_discovered_cb(
49 (bleConnHandle)data->conn_handle, data->out_status
50 );
51 } break;
52 case ACE_BT_CALLBACK_GATTC_CHARS_READ_RSP: {
53 log_debug("BLE GATTC callback handler, case ACE_BT_CALLBACK_GATTC_CHARS_READ_RSP");
54 if (p_client_callbacks->on_ble_gattc_read_characteristics_cb == NULL) {
55 log_error("[%s()]: on_ble_gattc_read_characteristics_cb not implemented", __func__);
56 break;
57 }
58
59 gattc_read_chars_data_t* data = (gattc_read_chars_data_t*)task->buffer;
60
61 // TODO: Decompiled code does way more assignments. So there is a chance I'm not filling
62 // the whole struct
63 data->value.blobValue.data = data->data;
64 uint32_t len32 = data->data_len;
65 if (len32 > UINT16_MAX) {
66 log_error(
67 "[%s() ACE_BT_CALLBACK_GATTC_CHARS_READ_RSP]: data_len too big: %d", __func__,
68 data->data_len
69 );
70 len32 = UINT16_MAX;
71 }
72 data->value.blobValue.size = (uint16_t)len32;
73 data->value.blobValue.offset = 0;
74 data->value.format = BLE_FORMAT_BLOB;
75
76 p_client_callbacks->on_ble_gattc_read_characteristics_cb(
77 (bleConnHandle)data->conn_handle, data->value, data->status
78 );
79 } break;
80 case ACE_BT_CALLBACK_GATTC_CHARS_WRITE_RSP: {
81 log_debug("BLE GATTC callback handler, case ACE_BT_CALLBACK_GATTC_CHARS_WRITE_RSP");
82 if (p_client_callbacks->on_ble_gattc_write_characteristics_cb == NULL) {
83 log_error("[%s()]: on_ble_gattc_write_characteristics_cb not implemented", __func__);
84 break;
85 }
86
87 gattc_write_chars_data_t* data = (gattc_write_chars_data_t*)task->buffer;
88
89 // TODO: Decompiled code does way more assignments. This might not be feature complete
90 data->value.blobValue.data = data->data;
91 uint32_t len32 = data->data_len;
92 if (len32 > UINT16_MAX) {
93 log_error(
94 "[%s() ACE_BT_CALLBACK_GATTC_CHARS_WRITE_RSP]: data_len too big: %d", __func__,
95 data->data_len
96 );
97 len32 = UINT16_MAX;
98 }
99 data->value.blobValue.size = (uint16_t)len32;
100 data->value.blobValue.offset = 0;
101 data->value.format = BLE_FORMAT_BLOB;
102
103 p_client_callbacks->on_ble_gattc_write_characteristics_cb(
104 (bleConnHandle)data->conn_handle, data->value, data->status
105 );
106 } break;
107 case ACE_BT_CALLBACK_GATTC_EXEC_WRITE_RSP: {
108 log_debug("BLE GATTC callback handler, case ACE_BT_CALLBACK_GATTC_EXEC_WRITE_RSP");
109 } break;
110 case ACE_BT_CALLBACK_GATTC_NOTIFY_CHARS_CHANGED: {
111 log_debug("BLE GATTC callback handler, case ACE_BT_CALLBACK_GATTC_NOTIFY_CHARS_CHANGED");
112 if (p_client_callbacks->notify_characteristics_cb == NULL) {
113 log_error("[%s()]: notify_characteristics_cb not implemented", __func__);
114 break;
115 }
116
117 notify_data_t* data = (notify_data_t*)task->buffer;
118
119 // TODO: Decompiled code does way more assignments. This might not be feature complete
120 data->value.blobValue.data = data->data;
121 uint32_t len32 = data->data_len;
122 if (len32 > UINT16_MAX) {
123 log_error(
124 "[%s() ACE_BT_CALLBACK_GATTC_NOTIFY_CHARS_CHANGED]: data_len too big: %d", __func__,
125 data->data_len
126 );
127 len32 = UINT16_MAX;
128 }
129 data->value.blobValue.size = (uint16_t)len32;
130 data->value.blobValue.offset = 0;
131 data->value.format = BLE_FORMAT_BLOB;
132
133 p_client_callbacks->notify_characteristics_cb(
134 (bleConnHandle)data->conn_handle, data->value
135 );
136 } break;
137 case ACE_BT_CALLBACK_GATTC_DESC_WRITE_RSP: {
138 log_debug("BLE GATTC callback handler, case ACE_BT_CALLBACK_GATTC_DESC_WRITE_RSP");
139 if (p_client_callbacks->on_ble_gattc_write_descriptor_cb == NULL) {
140 log_error("[%s()]: on_ble_gattc_write_descriptor_cb not implemented", __func__);
141 break;
142 }
143
144 gattc_write_desc_data_t* data = (gattc_write_desc_data_t*)task->buffer;
145
146 // TODO: Decompiled code does way more assignments. This might not be feature complete
147 data->desc.blobValue.data = data->data;
148 uint32_t len32 = data->data_len;
149 if (len32 > UINT16_MAX) {
150 log_error(
151 "[%s() ACE_BT_CALLBACK_GATTC_DESC_WRITE_RSP]: data_len too big: %d", __func__,
152 data->data_len
153 );
154 len32 = UINT16_MAX;
155 }
156 data->desc.blobValue.size = (uint16_t)len32;
157 data->desc.blobValue.offset = 0;
158
159 // Callback expects a bleGattCharacteristicsValue_t? I think it might be expected that
160 // only gattDescriptor is populated
162 charac.gattDescriptor = data->desc;
163
164 p_client_callbacks->on_ble_gattc_write_descriptor_cb(
165 (bleConnHandle)data->conn_handle, charac, data->status
166 );
167 } break;
168 case ACE_BT_CALLBACK_GATTC_DESC_READ_RSP: {
169 log_debug("BLE GATTC callback handler, case ACE_BT_CALLBACK_GATTC_DESC_READ_RSP");
170 } break;
171 case ACE_BT_CALLBACK_GATTC_GET_DB_RSP: {
172 log_debug("BLE GATTC callback handler, case ACE_BT_CALLBACK_GATTC_GET_DB_RSP");
173 if (p_client_callbacks->on_ble_gattc_get_gatt_db_cb == NULL) {
174 log_error("[%s()]: on_ble_gattc_get_gatt_db_callback not implemented", __func__);
175 break;
176 }
177
178 bleGattsService_t* gatt_service;
179 uint32_t no_svc;
180 register_cback_gatts_list_t* data = (register_cback_gatts_list_t*)task->buffer;
181
182 deserealize_all_gatts_register_data(data, &gatt_service, &no_svc);
183 p_client_callbacks->on_ble_gattc_get_gatt_db_cb(
184 (bleConnHandle)data->conn_handle, gatt_service, no_svc
185 );
186 cleanup_all_service(gatt_service, no_svc);
187 } break;
188 default:
189 log_error("[%s()]: Unknown GATT Client callback type %d", __func__, task->function_id);
190 break;
191 }
192
193 return;
194}
aceBT_bleGattClientCallbacks_t bleGattClientCallbacks_t
Callback struct of GATT Client Bluetooth operations.
aceBT_bleGattsService_t bleGattsService_t
Structure for a GATT Server service.
aceBT_bleConnHandle bleConnHandle
Connection handle for the lifetime of a Bluetooth connection.
#define BLE_FORMAT_BLOB
BLOB format.
aceBT_sessionHandle sessionHandle
Session handle for the lifetime of the Bluetooth application.
aceBT_bleGattCharacteristicsValue_t bleGattCharacteristicsValue_t
BLE GATT Characteristic.