1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdint.h>
8
9 #include <string.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12
13
14 #include "common/bt_target.h"
15 #include "common/bt_trace.h"
16 #include "stack/bt_types.h"
17 #include "stack/gatt_api.h"
18 #include "bta/bta_api.h"
19 #include "bta/bta_gatt_api.h"
20 #include "bta_gatts_int.h"
21 #include "button_pro.h"
22
23 #include "prf_defs.h"
24
25 #if (BUT_PROFILE_CFG)
26
27
28 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
29
30 button_env_cb_t button_cb_env;
31
32
33
34
35 /*****************************************************************************
36 ** Constants
37 *****************************************************************************/
38 static void button_profile_cb(esp_gatts_evt_t event, esp_gatts_t *p_data);
39
40
41 /*******************************************************************************
42 **
43 ** Function button_profile_cb
44 **
45 ** Description the callback function after the profile has been register to the BTA manager module
46 **
47 ** Returns NULL
48 **
49 *******************************************************************************/
button_profile_cb(esp_gatts_evt_t event,esp_gatts_t * p_data)50 static void button_profile_cb(esp_gatts_evt_t event, esp_gatts_t *p_data)
51 {
52 esp_gatts_rsp_t rsp;
53 esp_bt_uuid_t uuid = {LEN_UUID_16, {ATT_SVC_BUTTON}};
54 but_inst_t *p_inst = &button_cb_env.button_inst;
55 uint8_t net_event = 0xff;
56 uint8_t len = 0;
57 uint8_t *p_rec_data = NULL;
58 //BTC_TRACE_ERROR("p_data->status = %x\n",p_data->status);
59 //if(p_data->status != BTA_GATT_OK){
60 // BTC_TRACE_ERROR("button profile register failed\n");
61 // return;
62 //}
63 BTC_TRACE_ERROR("button profile cb event = %x\n", event);
64 switch (event) {
65 case ESP_GATTS_REG_EVT:
66
67 BTC_TRACE_ERROR("p_data->reg_oper.status = %x\n", p_data->reg_oper.status);
68 BTC_TRACE_ERROR("(p_data->reg_oper.uuid.uu.uuid16=%x\n", p_data->reg_oper.uuid.uu.uuid16);
69 if (p_data->reg_oper.status != BTA_GATT_OK) {
70 BTC_TRACE_ERROR("button profile register failed\n");
71 }
72 button_cb_env.gatt_if = p_data->reg_oper.server_if;
73 button_cb_env.enabled = true;
74 //button_cb_env.button_inst.app_id = p_data->reg_oper.uuid;
75 //create the button service to the service data base.
76 if (p_data->reg_oper.uuid.uu.uuid16 == ATT_SVC_BUTTON) {
77 Button_CreateService();
78 }
79 break;
80 case ESP_GATTS_READ_EVT:
81 //tBTA_GATTS_RSP rsp;
82 memset(&rsp, 0, sizeof(tBTA_GATTS_API_RSP));
83 rsp.attr_value.handle = p_data->req_data.p_data->read_req.handle;
84 rsp.attr_value.len = 2;
85 esp_ble_gatts_send_rsp(p_data->req_data.conn_id, p_data->req_data.trans_id,
86 p_data->req_data.status, &rsp);
87 break;
88 case ESP_GATTS_WRITE_EVT:
89 esp_ble_gatts_send_rsp(p_data->req_data.conn_id, p_data->req_data.trans_id,
90 p_data->req_data.status, NULL);
91 BTC_TRACE_ERROR("Received button data:");
92 for (int i = 0; i < p_data->req_data.p_data->write_req.len; i++) {
93 BTC_TRACE_ERROR("%x", p_data->req_data.p_data->write_req.value[i]);
94 }
95 BTC_TRACE_ERROR("\n");
96 if (p_data->req_data.p_data->write_req.handle == button_cb_env.button_inst.but_wirt_hdl) {
97
98 p_rec_data = &p_data->req_data.p_data->write_req.value[0];
99 // button_msg_notify(len,p_rec_data);
100 (*p_inst->p_cback)(button_cb_env.button_inst.app_id, net_event, len, p_rec_data);
101
102 }
103 break;
104 case ESP_GATTS_CFM_EVT:
105
106 break;
107 case ESP_GATTS_CREATE_EVT:
108 //tBT_UUID uuid_butt_write;
109 uuid.uu.uuid16 = ATT_CHAR_BUTTON_WIT;
110 //tBTA_GATT_PERM perm = (GATT_PERM_WRITE|GATT_PERM_READ);
111 //tBTA_GATT_CHAR_PROP prop = (GATT_CHAR_PROP_BIT_READ|GATT_CHAR_PROP_BIT_WRITE);
112 //uuid = {LEN_UUID_16, {ATT_SVC_BUTTON}};
113 button_cb_env.clcb.cur_srvc_id = p_data->create.service_id;
114 button_cb_env.is_primery = p_data->create.is_primary;
115 //uuid = {LEN_UUID_16, {ATT_CHAR_BUTTON_WIT}};
116 //start the button service after created
117 esp_ble_gatts_start_srvc(p_data->create.service_id);
118 //add the frist button characteristic --> write characteristic
119 esp_ble_gatts_add_char(button_cb_env.clcb.cur_srvc_id, &uuid,
120 (GATT_PERM_WRITE | GATT_PERM_READ),
121 (GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE));
122 break;
123
124 case ESP_GATTS_ADD_CHAR_EVT:
125 if (p_data->add_result.char_uuid.uu.uuid16 == ATT_CHAR_BUTTON_WIT) {
126 uuid.uu.uuid16 = ATT_CHAR_BUTTON_NTF;
127 //tBTA_GATT_PERM perm = GATT_PERM_READ;
128 tBTA_GATT_CHAR_PROP prop = (GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_NOTIFY);
129 //save the att handle to the env
130 button_cb_env.button_inst.but_wirt_hdl = p_data->add_result.attr_id;
131 //add the frist button characteristic --> Notify characteristic
132 esp_ble_gatts_add_char(button_cb_env.clcb.cur_srvc_id, &uuid,
133 GATT_PERM_READ, (GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_NOTIFY));
134 } else if (p_data->add_result.char_uuid.uu.uuid16 == ATT_CHAR_BUTTON_NTF) { // add the gattc config descriptor to the notify charateristic
135 //tBTA_GATT_PERM perm = (GATT_PERM_WRITE|GATT_PERM_WRITE);
136 uuid.uu.uuid16 = GATT_UUID_CHAR_CLIENT_CONFIG;
137 button_cb_env.button_inst.but_ntf_hdl = p_data->add_result.attr_id;
138 esp_ble_gatts_add_char_descr (button_cb_env.clcb.cur_srvc_id,
139 (GATT_PERM_WRITE | GATT_PERM_WRITE),
140 &uuid);
141 }
142
143 break;
144 case ESP_GATTS_ADD_CHAR_DESCR_EVT:
145 if (p_data->add_result.char_uuid.uu.uuid16 == GATT_UUID_CHAR_CLIENT_CONFIG) {
146 button_cb_env.button_inst.but_cfg_hdl = p_data->add_result.attr_id;
147 }
148 ///Start advertising
149 BTC_TRACE_ERROR("\n*******Start sent the ADV.*************\n");
150 //esp_ble_start_advertising (&adv_params);
151 //BTA_GATTS_Listen(button_cb_env.gatt_if, true, NULL);
152 break;
153 case ESP_GATTS_CONNECT_EVT:
154 BTC_TRACE_ERROR("############BUTTON CONNCET EVT################\n");
155 //esp_ble_stop_advertising();
156 //set the connection flag to true
157 button_env_clcb_alloc(p_data->conn.conn_id, p_data->conn.remote_bda);
158 break;
159 case ESP_GATTS_DISCONNECT_EVT:
160 //set the connection flag to true
161 button_cb_env.clcb.connected = false;
162 break;
163 case ESP_GATTS_OPEN_EVT:
164 ///stop the advertising after connected
165
166 break;
167 case ESP_GATTS_CLOSE_EVT:
168 if (button_cb_env.clcb.connected && (button_cb_env.clcb.conn_id == p_data->conn.conn_id)) {
169 //set the connection channal congested flag to true
170 button_cb_env.clcb.congest = p_data->congest.congested;
171 }
172 break;
173 case ESP_GATTS_CONGEST_EVT:
174 break;
175 default:
176 break;
177 }
178 }
179
180
181 /*******************************************************************************
182 **
183 ** Function Button_CreateService
184 **
185 ** Description Create a Service for the button profile
186 **
187 ** Returns NULL
188 **
189 *******************************************************************************/
Button_CreateService(void)190 void Button_CreateService(void)
191 {
192 esp_gatts_if_t server_if ;
193 esp_bt_uuid_t uuid = {LEN_UUID_16, {ATT_SVC_BUTTON}};
194 uint16_t num_handle = KEY_IDX_NB;
195 uint8_t inst = 0x00;
196 server_if = button_cb_env.gatt_if;
197 button_cb_env.inst_id = inst;
198 //if(!button_cb_env.enabled)
199 //{
200 // BTC_TRACE_ERROR("button service added error.");
201 //}
202 esp_ble_gatts_create_srvc(server_if, &uuid, inst, num_handle, true);
203
204 }
205
206 /*******************************************************************************
207 **
208 ** Function button_env_clcb_alloc
209 **
210 ** Description The function allocates a GATT profile connection link control block
211 **
212 ** Returns NULL if not found. Otherwise pointer to the connection link block.
213 **
214 *******************************************************************************/
button_env_clcb_alloc(uint16_t conn_id,BD_ADDR remote_bda)215 but_clcb_t *button_env_clcb_alloc (uint16_t conn_id, BD_ADDR remote_bda)
216 {
217 but_clcb_t *p_clcb = NULL;
218 p_clcb = &button_cb_env.clcb;
219
220 if (!p_clcb->in_use) {
221 p_clcb->in_use = TRUE;
222 p_clcb->conn_id = conn_id;
223 BTC_TRACE_ERROR("p_clcb->conn_id = %x\n", conn_id);
224 p_clcb->connected = TRUE;
225 memcpy(p_clcb->remote_bda, remote_bda, BD_ADDR_LEN);
226 }
227
228 return p_clcb;
229 }
230
231 /*******************************************************************************
232 **
233 ** Function button_env_find_conn_id_by_bd_adddr
234 **
235 ** Description The function searches all LCB with macthing bd address
236 **
237 ** Returns total number of clcb found.
238 **
239 *******************************************************************************/
button_env_find_conn_id_by_bd_adddr(BD_ADDR remote_bda)240 uint16_t button_env_find_conn_id_by_bd_adddr(BD_ADDR remote_bda)
241 {
242 uint8_t i_clcb;
243 but_clcb_t *p_clcb = NULL;
244
245 for (i_clcb = 0, p_clcb = &button_cb_env.clcb; i_clcb < BUTT_MAX_APPS; i_clcb++, p_clcb++) {
246 if (p_clcb->in_use && p_clcb->connected && memcmp(p_clcb->remote_bda, remote_bda, BD_ADDR_LEN)) {
247 return p_clcb->conn_id;
248 }
249 }
250
251 return GATT_INVALID_CONN_ID;
252 }
253
254 /*******************************************************************************
255 **
256 ** Function button_env_clcb_dealloc
257 **
258 ** Description The function deallocates a GATT profile connection link control block
259 **
260 ** Returns True the deallocation is successful
261 **
262 *******************************************************************************/
263
button_env_clcb_dealloc(uint16_t conn_id)264 BOOLEAN button_env_clcb_dealloc(uint16_t conn_id)
265 {
266 uint16_t i_clcb = 0;
267 but_clcb_t *p_clcb = NULL;
268
269 for (i_clcb = 0, p_clcb = &button_cb_env.clcb; i_clcb < 1; i_clcb++, p_clcb++) {
270 if (p_clcb->in_use && p_clcb->connected && (p_clcb->conn_id == conn_id)) {
271 memset(p_clcb, 0, sizeof(but_clcb_t));
272 return TRUE;
273 }
274 }
275
276 return FALSE;
277 }
278
279 /*******************************************************************************
280 **
281 ** Function button_init
282 **
283 ** Description Initializa the GATT Service for button profiles.
284 **
285 *******************************************************************************/
button_init(but_prf_cb_t call_back)286 esp_gatt_status_t button_init (but_prf_cb_t call_back)
287 {
288 tBT_UUID app_uuid = {LEN_UUID_16, {ATT_SVC_BUTTON}};
289
290 BTC_TRACE_ERROR("\n=============================button_init==============================================\n");
291 if (button_cb_env.enabled) {
292 BTC_TRACE_ERROR("button svc already initaliezd\n");
293 return ESP_GATT_ERROR;
294 } else {
295 memset(&button_cb_env, 0, sizeof(button_env_cb_t));
296 }
297
298
299 if (call_back != NULL) {
300 button_cb_env.button_inst.p_cback = call_back;
301 }
302
303
304 /* register the button profile to the BTA_GATTS module*/
305 esp_ble_gatts_app_register(&app_uuid, button_profile_cb);
306
307 button_cb_env.enabled = TRUE;
308
309 return ESP_GATT_OK;
310 }
311
button_disable(uint16_t connid)312 void button_disable(uint16_t connid)
313 {
314 button_env_clcb_dealloc(connid);
315 }
316
317
button_msg_notify(uint16_t len,uint8_t * button_msg)318 void button_msg_notify(uint16_t len, uint8_t *button_msg)
319 {
320 BOOLEAN conn_status = button_cb_env.clcb.connected;
321 uint16_t conn_id = button_cb_env.clcb.conn_id;
322 uint16_t attr_id = button_cb_env.button_inst.but_ntf_hdl;
323 //notify rsp==false; indicate rsp==true.
324 BOOLEAN rsp = false;
325 if (!conn_status && button_cb_env.clcb.congest) {
326 BTC_TRACE_ERROR("the conneciton for button profile has been loss\n");
327 return;
328 }
329
330 esp_ble_gatts_hdl_val_indica (conn_id, attr_id, len,
331 button_msg, rsp);
332 }
333
334 #endif ///BUT_PROFILE_CFG
335