blob: 6e42ad538bde074c9466d16f7a22106aa7b7237d [file] [log] [blame]
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001/******************************************************************************
2 *
3 * Copyright (C) 2009-2013 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080018#include "bt_target.h"
19
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080020#include <string.h>
Myles Watson911d1ae2016-11-28 16:44:40 -080021#include "bt_utils.h"
22#include "btcore/include/uuid.h"
23#include "btm_int.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080024#include "gap_api.h"
Myles Watson911d1ae2016-11-28 16:44:40 -080025#include "gap_int.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080026#include "gatt_api.h"
27#include "gatt_int.h"
Myles Watson911d1ae2016-11-28 16:44:40 -080028#include "gattdefs.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080029#include "hcimsgs.h"
Myles Watsond7ffd642016-10-27 10:27:36 -070030#include "osi/include/osi.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080031
Myles Watson911d1ae2016-11-28 16:44:40 -080032#define GAP_CHAR_ICON_SIZE 2
33#define GAP_CHAR_DEV_NAME_SIZE 248
34#define GAP_MAX_NUM_INC_SVR 0
35#define GAP_MAX_ATTR_NUM (2 * GAP_MAX_CHAR_NUM + GAP_MAX_NUM_INC_SVR + 1)
36#define GAP_MAX_CHAR_VALUE_SIZE (30 + GAP_CHAR_DEV_NAME_SIZE)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080037
38#ifndef GAP_ATTR_DB_SIZE
Myles Watson911d1ae2016-11-28 16:44:40 -080039#define GAP_ATTR_DB_SIZE \
40 GATT_DB_MEM_SIZE(GAP_MAX_NUM_INC_SVR, GAP_MAX_CHAR_NUM, \
41 GAP_MAX_CHAR_VALUE_SIZE)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080042#endif
43
Myles Watson911d1ae2016-11-28 16:44:40 -080044static void gap_ble_s_attr_request_cback(uint16_t conn_id, uint32_t trans_id,
45 tGATTS_REQ_TYPE op_code,
46 tGATTS_DATA* p_data);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080047
48/* client connection callback */
Myles Watson911d1ae2016-11-28 16:44:40 -080049static void gap_ble_c_connect_cback(tGATT_IF gatt_if, BD_ADDR bda,
50 uint16_t conn_id, bool connected,
51 tGATT_DISCONN_REASON reason,
52 tGATT_TRANSPORT transport);
53static void gap_ble_c_cmpl_cback(uint16_t conn_id, tGATTC_OPTYPE op,
54 tGATT_STATUS status,
55 tGATT_CL_COMPLETE* p_data);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080056
Myles Watson911d1ae2016-11-28 16:44:40 -080057static tGATT_CBACK gap_cback = {gap_ble_c_connect_cback,
58 gap_ble_c_cmpl_cback,
59 NULL,
60 NULL,
61 gap_ble_s_attr_request_cback,
62 NULL,
Jakub Pawlowskieafd45d2017-03-22 19:00:47 -070063 NULL,
Jakub Pawlowskib5ba4fd2017-03-23 18:11:04 -070064 NULL,
Myles Watson911d1ae2016-11-28 16:44:40 -080065 NULL};
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080066
67/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080068 *
69 * Function gap_find_clcb_by_bd_addr
70 *
71 * Description The function searches all LCB with macthing bd address
72 *
73 * Returns total number of clcb found.
74 *
75 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -080076tGAP_CLCB* gap_find_clcb_by_bd_addr(BD_ADDR bda) {
77 uint8_t i_clcb;
78 tGAP_CLCB* p_clcb = NULL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080079
Myles Watson911d1ae2016-11-28 16:44:40 -080080 for (i_clcb = 0, p_clcb = gap_cb.clcb; i_clcb < GAP_MAX_CL;
81 i_clcb++, p_clcb++) {
82 if (p_clcb->in_use && !memcmp(p_clcb->bda, bda, BD_ADDR_LEN)) {
83 return p_clcb;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080084 }
Myles Watson911d1ae2016-11-28 16:44:40 -080085 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080086
Myles Watson911d1ae2016-11-28 16:44:40 -080087 return NULL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080088}
89
90/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -080091 *
92 * Function gap_ble_find_clcb_by_conn_id
93 *
94 * Description The function searches all LCB with macthing connection ID
95 *
96 * Returns total number of clcb found.
97 *
98 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -080099tGAP_CLCB* gap_ble_find_clcb_by_conn_id(uint16_t conn_id) {
100 uint8_t i_clcb;
101 tGAP_CLCB* p_clcb = NULL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800102
Myles Watson911d1ae2016-11-28 16:44:40 -0800103 for (i_clcb = 0, p_clcb = gap_cb.clcb; i_clcb < GAP_MAX_CL;
104 i_clcb++, p_clcb++) {
105 if (p_clcb->in_use && p_clcb->connected && p_clcb->conn_id == conn_id) {
106 return p_clcb;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800107 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800108 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800109
Myles Watson911d1ae2016-11-28 16:44:40 -0800110 return p_clcb;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800111}
112
113/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800114 *
115 * Function gap_clcb_alloc
116 *
117 * Description The function allocates a GAP connection link control block
118 *
Myles Watson9ca07092016-11-28 16:41:53 -0800119 * Returns NULL if not found. Otherwise pointer to the connection link
120 * block.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800121 *
122 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800123tGAP_CLCB* gap_clcb_alloc(BD_ADDR bda) {
124 uint8_t i_clcb = 0;
125 tGAP_CLCB* p_clcb = NULL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800126
Myles Watson911d1ae2016-11-28 16:44:40 -0800127 for (i_clcb = 0, p_clcb = gap_cb.clcb; i_clcb < GAP_MAX_CL;
128 i_clcb++, p_clcb++) {
129 if (!p_clcb->in_use) {
130 fixed_queue_free(p_clcb->pending_req_q, NULL);
131 memset(p_clcb, 0, sizeof(tGAP_CLCB));
132 p_clcb->in_use = true;
133 memcpy(p_clcb->bda, bda, BD_ADDR_LEN);
134 p_clcb->pending_req_q = fixed_queue_new(SIZE_MAX);
135 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800136 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800137 }
138 return p_clcb;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800139}
140
141/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800142 *
143 * Function gap_ble_dealloc_clcb
144 *
145 * Description The function clean up the pending request queue in GAP
146 *
147 * Returns none
148 *
149 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800150void gap_ble_dealloc_clcb(tGAP_CLCB* p_clcb) {
151 tGAP_BLE_REQ* p_q;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800152
Myles Watson911d1ae2016-11-28 16:44:40 -0800153 while ((p_q = (tGAP_BLE_REQ*)fixed_queue_try_dequeue(
154 p_clcb->pending_req_q)) != NULL) {
155 /* send callback to all pending requests if being removed*/
156 if (p_q->p_cback != NULL) (*p_q->p_cback)(false, p_clcb->bda, 0, NULL);
Satya Calloji444a8da2015-03-06 10:38:22 -0800157
Myles Watson911d1ae2016-11-28 16:44:40 -0800158 osi_free(p_q);
159 }
160 fixed_queue_free(p_clcb->pending_req_q, NULL);
Satya Calloji444a8da2015-03-06 10:38:22 -0800161
Myles Watson911d1ae2016-11-28 16:44:40 -0800162 memset(p_clcb, 0, sizeof(tGAP_CLCB));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800163}
164
165/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800166 *
167 * Function gap_ble_enqueue_request
168 *
169 * Description The function enqueue a GAP client request
170 *
171 * Returns true is successul; false otherwise
172 *
173 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800174bool gap_ble_enqueue_request(tGAP_CLCB* p_clcb, uint16_t uuid,
175 tGAP_BLE_CMPL_CBACK* p_cback) {
176 tGAP_BLE_REQ* p_q = (tGAP_BLE_REQ*)osi_malloc(sizeof(tGAP_BLE_REQ));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800177
Myles Watson911d1ae2016-11-28 16:44:40 -0800178 p_q->p_cback = p_cback;
179 p_q->uuid = uuid;
180 fixed_queue_enqueue(p_clcb->pending_req_q, p_q);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800181
Myles Watson911d1ae2016-11-28 16:44:40 -0800182 return true;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800183}
Pavlin Radoslavov717a4a92016-02-06 08:36:06 -0800184
Satya Calloji444a8da2015-03-06 10:38:22 -0800185/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800186 *
187 * Function gap_ble_dequeue_request
188 *
189 * Description The function dequeue a GAP client request if any
190 *
191 * Returns true is successul; false otherwise
192 *
193 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800194bool gap_ble_dequeue_request(tGAP_CLCB* p_clcb, uint16_t* p_uuid,
195 tGAP_BLE_CMPL_CBACK** p_cback) {
196 tGAP_BLE_REQ* p_q =
197 (tGAP_BLE_REQ*)fixed_queue_try_dequeue(p_clcb->pending_req_q);
198 ;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800199
Myles Watson911d1ae2016-11-28 16:44:40 -0800200 if (p_q != NULL) {
201 *p_cback = p_q->p_cback;
202 *p_uuid = p_q->uuid;
203 osi_free(p_q);
204 return true;
205 }
Satya Calloji444a8da2015-03-06 10:38:22 -0800206
Myles Watson911d1ae2016-11-28 16:44:40 -0800207 return false;
Satya Calloji444a8da2015-03-06 10:38:22 -0800208}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800209
210/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800211 * GAP Attributes Database Request callback
212 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800213tGATT_STATUS gap_read_attr_value(uint16_t handle, tGATT_VALUE* p_value,
214 bool is_long) {
215 tGAP_ATTR* p_db_attr = gap_cb.gatt_attr;
216 uint8_t *p = p_value->value, i;
217 uint16_t offset = p_value->offset;
218 uint8_t* p_dev_name = NULL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800219
Myles Watson911d1ae2016-11-28 16:44:40 -0800220 for (i = 0; i < GAP_MAX_CHAR_NUM; i++, p_db_attr++) {
221 if (handle == p_db_attr->handle) {
222 if (p_db_attr->uuid != GATT_UUID_GAP_DEVICE_NAME && is_long == true)
223 return GATT_NOT_LONG;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800224
Myles Watson911d1ae2016-11-28 16:44:40 -0800225 switch (p_db_attr->uuid) {
226 case GATT_UUID_GAP_DEVICE_NAME:
227 BTM_ReadLocalDeviceName((char**)&p_dev_name);
228 if (strlen((char*)p_dev_name) > GATT_MAX_ATTR_LEN)
229 p_value->len = GATT_MAX_ATTR_LEN;
230 else
231 p_value->len = (uint16_t)strlen((char*)p_dev_name);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800232
Myles Watson911d1ae2016-11-28 16:44:40 -0800233 if (offset > p_value->len)
234 return GATT_INVALID_OFFSET;
235 else {
236 p_value->len -= offset;
237 p_dev_name += offset;
238 ARRAY_TO_STREAM(p, p_dev_name, p_value->len);
239 GAP_TRACE_EVENT("GATT_UUID_GAP_DEVICE_NAME len=0x%04x",
240 p_value->len);
241 }
242 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800243
Myles Watson911d1ae2016-11-28 16:44:40 -0800244 case GATT_UUID_GAP_ICON:
245 UINT16_TO_STREAM(p, p_db_attr->attr_value.icon);
246 p_value->len = 2;
247 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800248
Myles Watson911d1ae2016-11-28 16:44:40 -0800249 case GATT_UUID_GAP_PREF_CONN_PARAM:
250 UINT16_TO_STREAM(
251 p, p_db_attr->attr_value.conn_param.int_min); /* int_min */
252 UINT16_TO_STREAM(
253 p, p_db_attr->attr_value.conn_param.int_max); /* int_max */
254 UINT16_TO_STREAM(
255 p, p_db_attr->attr_value.conn_param.latency); /* latency */
256 UINT16_TO_STREAM(
257 p, p_db_attr->attr_value.conn_param.sp_tout); /* sp_tout */
258 p_value->len = 8;
259 break;
Satya Calloji444a8da2015-03-06 10:38:22 -0800260
Myles Watson911d1ae2016-11-28 16:44:40 -0800261 /* address resolution */
262 case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
263 UINT8_TO_STREAM(p, p_db_attr->attr_value.addr_resolution);
264 p_value->len = 1;
265 break;
266 }
267 return GATT_SUCCESS;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800268 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800269 }
270 return GATT_NOT_FOUND;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800271}
272
273/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800274 * GAP Attributes Database Read/Read Blob Request process
275 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800276tGATT_STATUS gap_proc_read(UNUSED_ATTR tGATTS_REQ_TYPE type,
277 tGATT_READ_REQ* p_data, tGATTS_RSP* p_rsp) {
278 tGATT_STATUS status = GATT_NO_RESOURCES;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800279
Myles Watson911d1ae2016-11-28 16:44:40 -0800280 if (p_data->is_long) p_rsp->attr_value.offset = p_data->offset;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800281
Myles Watson911d1ae2016-11-28 16:44:40 -0800282 p_rsp->attr_value.handle = p_data->handle;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800283
Myles Watson911d1ae2016-11-28 16:44:40 -0800284 status =
285 gap_read_attr_value(p_data->handle, &p_rsp->attr_value, p_data->is_long);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800286
Myles Watson911d1ae2016-11-28 16:44:40 -0800287 return status;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800288}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800289
290/******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800291 *
292 * Function gap_proc_write_req
293 *
294 * Description GAP ATT server process a write request.
295 *
296 * Returns void.
297 *
298 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800299uint8_t gap_proc_write_req(UNUSED_ATTR tGATTS_REQ_TYPE type,
300 tGATT_WRITE_REQ* p_data) {
301 tGAP_ATTR* p_db_attr = gap_cb.gatt_attr;
302 uint8_t i;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800303
Myles Watson911d1ae2016-11-28 16:44:40 -0800304 for (i = 0; i < GAP_MAX_CHAR_NUM; i++, p_db_attr++) {
305 if (p_data->handle == p_db_attr->handle) {
306 return GATT_WRITE_NOT_PERMIT;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800307 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800308 }
309 return GATT_NOT_FOUND;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800310}
311
312/******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800313 *
314 * Function gap_ble_s_attr_request_cback
315 *
316 * Description GAP ATT server attribute access request callback.
317 *
318 * Returns void.
319 *
320 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800321void gap_ble_s_attr_request_cback(uint16_t conn_id, uint32_t trans_id,
322 tGATTS_REQ_TYPE type, tGATTS_DATA* p_data) {
323 uint8_t status = GATT_INVALID_PDU;
324 tGATTS_RSP rsp_msg;
325 bool ignore = false;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800326
Myles Watson911d1ae2016-11-28 16:44:40 -0800327 GAP_TRACE_EVENT("gap_ble_s_attr_request_cback : recv type (0x%02x)", type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800328
Myles Watson911d1ae2016-11-28 16:44:40 -0800329 memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800330
Myles Watson911d1ae2016-11-28 16:44:40 -0800331 switch (type) {
332 case GATTS_REQ_TYPE_READ_CHARACTERISTIC:
333 case GATTS_REQ_TYPE_READ_DESCRIPTOR:
334 status = gap_proc_read(type, &p_data->read_req, &rsp_msg);
335 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800336
Myles Watson911d1ae2016-11-28 16:44:40 -0800337 case GATTS_REQ_TYPE_WRITE_CHARACTERISTIC:
338 case GATTS_REQ_TYPE_WRITE_DESCRIPTOR:
339 if (!p_data->write_req.need_rsp) ignore = true;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800340
Myles Watson911d1ae2016-11-28 16:44:40 -0800341 status = gap_proc_write_req(type, &p_data->write_req);
342 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800343
Myles Watson911d1ae2016-11-28 16:44:40 -0800344 case GATTS_REQ_TYPE_WRITE_EXEC:
345 ignore = true;
346 GAP_TRACE_EVENT("Ignore GATTS_REQ_TYPE_WRITE_EXEC");
347 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800348
Myles Watson911d1ae2016-11-28 16:44:40 -0800349 case GATTS_REQ_TYPE_MTU:
350 GAP_TRACE_EVENT("Get MTU exchange new mtu size: %d", p_data->mtu);
351 ignore = true;
352 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800353
Myles Watson911d1ae2016-11-28 16:44:40 -0800354 default:
355 GAP_TRACE_EVENT("Unknown/unexpected LE GAP ATT request: 0x%02x", type);
356 break;
357 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800358
Myles Watson911d1ae2016-11-28 16:44:40 -0800359 if (!ignore) GATTS_SendRsp(conn_id, trans_id, status, &rsp_msg);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800360}
361
362/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800363 *
364 * Function btm_ble_att_db_init
365 *
366 * Description GAP ATT database initalization.
367 *
368 * Returns void.
369 *
370 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800371void gap_attr_db_init(void) {
372 tBT_UUID app_uuid = {LEN_UUID_128, {0}};
373 uint16_t service_handle;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800374
Myles Watson911d1ae2016-11-28 16:44:40 -0800375 /* Fill our internal UUID with a fixed pattern 0x82 */
376 memset(&app_uuid.uu.uuid128, 0x82, LEN_UUID_128);
377 memset(gap_cb.gatt_attr, 0, sizeof(tGAP_ATTR) * GAP_MAX_CHAR_NUM);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800378
Myles Watson911d1ae2016-11-28 16:44:40 -0800379 gap_cb.gatt_if = GATT_Register(&app_uuid, &gap_cback);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800380
Myles Watson911d1ae2016-11-28 16:44:40 -0800381 GATT_StartIf(gap_cb.gatt_if);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800382
Myles Watson911d1ae2016-11-28 16:44:40 -0800383 bt_uuid_t svc_uuid, name_uuid, icon_uuid, pref_uuid, addr_res_uuid;
384 uuid_128_from_16(&svc_uuid, UUID_SERVCLASS_GAP_SERVER);
385 uuid_128_from_16(&name_uuid, GATT_UUID_GAP_DEVICE_NAME);
386 uuid_128_from_16(&icon_uuid, GATT_UUID_GAP_ICON);
387 uuid_128_from_16(&pref_uuid, GATT_UUID_GAP_PREF_CONN_PARAM);
388 uuid_128_from_16(&addr_res_uuid, GATT_UUID_GAP_CENTRAL_ADDR_RESOL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800389
Myles Watson911d1ae2016-11-28 16:44:40 -0800390 btgatt_db_element_t service[] = {
391 {.type = BTGATT_DB_PRIMARY_SERVICE, .uuid = svc_uuid},
392 {.type = BTGATT_DB_CHARACTERISTIC,
393 .uuid = name_uuid,
394 .properties = GATT_CHAR_PROP_BIT_READ,
395 .permissions = GATT_PERM_READ},
396 {.type = BTGATT_DB_CHARACTERISTIC,
397 .uuid = icon_uuid,
398 .properties = GATT_CHAR_PROP_BIT_READ,
399 .permissions = GATT_PERM_READ},
400 {.type = BTGATT_DB_CHARACTERISTIC,
401 .uuid = addr_res_uuid,
402 .properties = GATT_CHAR_PROP_BIT_READ,
403 .permissions = GATT_PERM_READ}
404#if (BTM_PERIPHERAL_ENABLED == TRUE) /* Only needed for peripheral testing */
405 ,
406 {.type = BTGATT_DB_CHARACTERISTIC,
407 .uuid = pref_uuid,
408 .properties = GATT_CHAR_PROP_BIT_READ,
409 .permissions = GATT_PERM_READ}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700410#endif
Myles Watson911d1ae2016-11-28 16:44:40 -0800411 };
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700412
Myles Watson911d1ae2016-11-28 16:44:40 -0800413 /* Add a GAP service */
414 GATTS_AddService(gap_cb.gatt_if, service,
415 sizeof(service) / sizeof(btgatt_db_element_t));
416 service_handle = service[0].attribute_handle;
Satya Calloji444a8da2015-03-06 10:38:22 -0800417
Myles Watson911d1ae2016-11-28 16:44:40 -0800418 GAP_TRACE_EVENT("%s: service_handle = %d", __func__, service_handle);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800419
Myles Watson911d1ae2016-11-28 16:44:40 -0800420 gap_cb.gatt_attr[0].uuid = GATT_UUID_GAP_DEVICE_NAME;
421 gap_cb.gatt_attr[0].handle = service[1].attribute_handle;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800422
Myles Watson911d1ae2016-11-28 16:44:40 -0800423 gap_cb.gatt_attr[1].uuid = GATT_UUID_GAP_ICON;
424 gap_cb.gatt_attr[1].handle = service[2].attribute_handle;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800425
Myles Watson911d1ae2016-11-28 16:44:40 -0800426 gap_cb.gatt_attr[2].uuid = GATT_UUID_GAP_CENTRAL_ADDR_RESOL;
427 gap_cb.gatt_attr[2].handle = service[3].attribute_handle;
428 gap_cb.gatt_attr[2].attr_value.addr_resolution = 0;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800429
Myles Watson911d1ae2016-11-28 16:44:40 -0800430#if (BTM_PERIPHERAL_ENABLED == TRUE) /* Only needed for peripheral testing */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800431
Myles Watson911d1ae2016-11-28 16:44:40 -0800432 gap_cb.gatt_attr[3].uuid = GATT_UUID_GAP_PREF_CONN_PARAM;
433 gap_cb.gatt_attr[3].attr_value.conn_param.int_max =
434 GAP_PREFER_CONN_INT_MAX; /* 6 */
435 gap_cb.gatt_attr[3].attr_value.conn_param.int_min =
436 GAP_PREFER_CONN_INT_MIN; /* 0 */
437 gap_cb.gatt_attr[3].attr_value.conn_param.latency =
438 GAP_PREFER_CONN_LATENCY; /* 0 */
439 gap_cb.gatt_attr[3].attr_value.conn_param.sp_tout =
440 GAP_PREFER_CONN_SP_TOUT; /* 2000 */
441 gap_cb.gatt_attr[3].handle = service[4].attribute_handle;
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700442#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800443}
444
445/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800446 *
447 * Function GAP_BleAttrDBUpdate
448 *
449 * Description GAP ATT database update.
450 *
451 * Returns void.
452 *
453 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800454void GAP_BleAttrDBUpdate(uint16_t attr_uuid, tGAP_BLE_ATTR_VALUE* p_value) {
455 tGAP_ATTR* p_db_attr = gap_cb.gatt_attr;
456 uint8_t i = 0;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800457
Myles Watson911d1ae2016-11-28 16:44:40 -0800458 GAP_TRACE_EVENT("GAP_BleAttrDBUpdate attr_uuid=0x%04x", attr_uuid);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800459
Myles Watson911d1ae2016-11-28 16:44:40 -0800460 for (i = 0; i < GAP_MAX_CHAR_NUM; i++, p_db_attr++) {
461 if (p_db_attr->uuid == attr_uuid) {
462 GAP_TRACE_EVENT("Found attr_uuid=0x%04x", attr_uuid);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800463
Myles Watson911d1ae2016-11-28 16:44:40 -0800464 switch (attr_uuid) {
465 case GATT_UUID_GAP_ICON:
466 p_db_attr->attr_value.icon = p_value->icon;
467 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800468
Myles Watson911d1ae2016-11-28 16:44:40 -0800469 case GATT_UUID_GAP_PREF_CONN_PARAM:
470 memcpy((void*)&p_db_attr->attr_value.conn_param,
471 (const void*)&p_value->conn_param,
472 sizeof(tGAP_BLE_PREF_PARAM));
473 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800474
Myles Watson911d1ae2016-11-28 16:44:40 -0800475 case GATT_UUID_GAP_DEVICE_NAME:
476 BTM_SetLocalDeviceName((char*)p_value->p_dev_name);
477 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800478
Myles Watson911d1ae2016-11-28 16:44:40 -0800479 case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
480 p_db_attr->attr_value.addr_resolution = p_value->addr_resolution;
481 break;
482 }
483 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800484 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800485 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800486
Myles Watson911d1ae2016-11-28 16:44:40 -0800487 return;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800488}
489
490/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800491 *
492 * Function gap_ble_send_cl_read_request
493 *
Myles Watson9ca07092016-11-28 16:41:53 -0800494 * Description utility function to send a read request for a GAP
495 * charactersitic
Myles Watsonee96a3c2016-11-23 14:49:54 -0800496 *
497 * Returns true if read started, else false if GAP is busy
498 *
499 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800500bool gap_ble_send_cl_read_request(tGAP_CLCB* p_clcb) {
501 tGATT_READ_PARAM param;
502 uint16_t uuid = 0;
503 bool started = false;
Satya Calloji444a8da2015-03-06 10:38:22 -0800504
Myles Watson911d1ae2016-11-28 16:44:40 -0800505 if (gap_ble_dequeue_request(p_clcb, &uuid, &p_clcb->p_cback)) {
506 memset(&param, 0, sizeof(tGATT_READ_PARAM));
Satya Calloji444a8da2015-03-06 10:38:22 -0800507
Myles Watson911d1ae2016-11-28 16:44:40 -0800508 param.service.uuid.len = LEN_UUID_16;
509 param.service.uuid.uu.uuid16 = uuid;
510 param.service.s_handle = 1;
511 param.service.e_handle = 0xFFFF;
512 param.service.auth_req = 0;
Satya Calloji444a8da2015-03-06 10:38:22 -0800513
Myles Watson911d1ae2016-11-28 16:44:40 -0800514 if (GATTC_Read(p_clcb->conn_id, GATT_READ_BY_TYPE, &param) ==
515 GATT_SUCCESS) {
516 p_clcb->cl_op_uuid = uuid;
517 started = true;
Satya Calloji444a8da2015-03-06 10:38:22 -0800518 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800519 }
Satya Calloji444a8da2015-03-06 10:38:22 -0800520
Myles Watson911d1ae2016-11-28 16:44:40 -0800521 return started;
Satya Calloji444a8da2015-03-06 10:38:22 -0800522}
523
524/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800525 *
526 * Function gap_ble_cl_op_cmpl
527 *
528 * Description GAP client operation complete callback
529 *
530 * Returns void
531 *
532 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800533void gap_ble_cl_op_cmpl(tGAP_CLCB* p_clcb, bool status, uint16_t len,
534 uint8_t* p_name) {
535 tGAP_BLE_CMPL_CBACK* p_cback = p_clcb->p_cback;
536 uint16_t op = p_clcb->cl_op_uuid;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800537
Myles Watson911d1ae2016-11-28 16:44:40 -0800538 GAP_TRACE_EVENT("gap_ble_cl_op_cmpl status: %d", status);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800539
Myles Watson911d1ae2016-11-28 16:44:40 -0800540 p_clcb->cl_op_uuid = 0;
541 p_clcb->p_cback = NULL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800542
Myles Watson911d1ae2016-11-28 16:44:40 -0800543 if (p_cback && op) {
544 GAP_TRACE_EVENT("calling gap_ble_cl_op_cmpl");
545 (*p_cback)(status, p_clcb->bda, len, (char*)p_name);
546 }
547
548 /* if no further activity is requested in callback, drop the link */
549 if (p_clcb->connected) {
550 if (!gap_ble_send_cl_read_request(p_clcb)) {
551 GATT_Disconnect(p_clcb->conn_id);
552 gap_ble_dealloc_clcb(p_clcb);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800553 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800554 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800555}
556
557/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800558 *
559 * Function gap_ble_c_connect_cback
560 *
561 * Description Client connection callback.
562 *
563 * Returns void
564 *
565 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800566static void gap_ble_c_connect_cback(UNUSED_ATTR tGATT_IF gatt_if, BD_ADDR bda,
567 uint16_t conn_id, bool connected,
568 tGATT_DISCONN_REASON reason,
569 UNUSED_ATTR tGATT_TRANSPORT transport) {
570 tGAP_CLCB* p_clcb = gap_find_clcb_by_bd_addr(bda);
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700571
Myles Watson911d1ae2016-11-28 16:44:40 -0800572 if (p_clcb != NULL) {
573 if (connected) {
574 p_clcb->conn_id = conn_id;
575 p_clcb->connected = true;
576 /* start operation is pending */
577 gap_ble_send_cl_read_request(p_clcb);
578 } else {
579 p_clcb->connected = false;
580 gap_ble_cl_op_cmpl(p_clcb, false, 0, NULL);
581 /* clean up clcb */
582 gap_ble_dealloc_clcb(p_clcb);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800583 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800584 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800585}
586
587/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800588 *
589 * Function gap_ble_c_cmpl_cback
590 *
591 * Description Client operation complete callback.
592 *
593 * Returns void
594 *
595 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800596static void gap_ble_c_cmpl_cback(uint16_t conn_id, tGATTC_OPTYPE op,
597 tGATT_STATUS status, tGATT_CL_COMPLETE* p_data)
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800598
599{
Myles Watson911d1ae2016-11-28 16:44:40 -0800600 tGAP_CLCB* p_clcb = gap_ble_find_clcb_by_conn_id(conn_id);
601 uint16_t op_type;
602 uint16_t min, max, latency, tout;
603 uint16_t len;
604 uint8_t* pp;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800605
Myles Watson911d1ae2016-11-28 16:44:40 -0800606 if (p_clcb == NULL) return;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800607
Myles Watson911d1ae2016-11-28 16:44:40 -0800608 op_type = p_clcb->cl_op_uuid;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800609
Myles Watson911d1ae2016-11-28 16:44:40 -0800610 GAP_TRACE_EVENT(
611 "gap_ble_c_cmpl_cback() - op_code: 0x%02x status: 0x%02x read_type: "
612 "0x%04x",
613 op, status, op_type);
614 /* Currently we only issue read commands */
615 if (op != GATTC_OPTYPE_READ) return;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800616
Myles Watson911d1ae2016-11-28 16:44:40 -0800617 if (status != GATT_SUCCESS) {
618 gap_ble_cl_op_cmpl(p_clcb, false, 0, NULL);
619 return;
620 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800621
Myles Watson911d1ae2016-11-28 16:44:40 -0800622 pp = p_data->att_value.value;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800623
Myles Watson911d1ae2016-11-28 16:44:40 -0800624 switch (op_type) {
625 case GATT_UUID_GAP_PREF_CONN_PARAM:
626 GAP_TRACE_EVENT("GATT_UUID_GAP_PREF_CONN_PARAM");
627 /* Extract the peripheral preferred connection parameters and save them */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800628
Myles Watson911d1ae2016-11-28 16:44:40 -0800629 STREAM_TO_UINT16(min, pp);
630 STREAM_TO_UINT16(max, pp);
631 STREAM_TO_UINT16(latency, pp);
632 STREAM_TO_UINT16(tout, pp);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800633
Myles Watson911d1ae2016-11-28 16:44:40 -0800634 BTM_BleSetPrefConnParams(p_clcb->bda, min, max, latency, tout);
635 /* release the connection here */
636 gap_ble_cl_op_cmpl(p_clcb, true, 0, NULL);
637 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800638
Myles Watson911d1ae2016-11-28 16:44:40 -0800639 case GATT_UUID_GAP_DEVICE_NAME:
640 GAP_TRACE_EVENT("GATT_UUID_GAP_DEVICE_NAME");
641 len = (uint16_t)strlen((char*)pp);
642 if (len > GAP_CHAR_DEV_NAME_SIZE) len = GAP_CHAR_DEV_NAME_SIZE;
643 gap_ble_cl_op_cmpl(p_clcb, true, len, pp);
644 break;
Satya Calloji444a8da2015-03-06 10:38:22 -0800645
Myles Watson911d1ae2016-11-28 16:44:40 -0800646 case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
647 gap_ble_cl_op_cmpl(p_clcb, true, 1, pp);
648 break;
649 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800650}
651
652/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800653 *
654 * Function gap_ble_accept_cl_operation
655 *
656 * Description Start a process to read peer address resolution capability
657 *
658 * Returns true if request accepted
659 *
660 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800661bool gap_ble_accept_cl_operation(BD_ADDR peer_bda, uint16_t uuid,
662 tGAP_BLE_CMPL_CBACK* p_cback) {
663 tGAP_CLCB* p_clcb;
664 bool started = false;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800665
Myles Watson911d1ae2016-11-28 16:44:40 -0800666 if (p_cback == NULL && uuid != GATT_UUID_GAP_PREF_CONN_PARAM)
667 return (started);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800668
Myles Watson911d1ae2016-11-28 16:44:40 -0800669 p_clcb = gap_find_clcb_by_bd_addr(peer_bda);
670 if (p_clcb == NULL) {
671 p_clcb = gap_clcb_alloc(peer_bda);
672 if (p_clcb == NULL) {
673 GAP_TRACE_ERROR("gap_ble_accept_cl_operation max connection reached");
674 return started;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800675 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800676 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800677
Myles Watson911d1ae2016-11-28 16:44:40 -0800678 GAP_TRACE_EVENT("%s() - BDA: %08x%04x cl_op_uuid: 0x%04x", __func__,
679 (peer_bda[0] << 24) + (peer_bda[1] << 16) +
680 (peer_bda[2] << 8) + peer_bda[3],
681 (peer_bda[4] << 8) + peer_bda[5], uuid);
Satya Calloji444a8da2015-03-06 10:38:22 -0800682
Myles Watson911d1ae2016-11-28 16:44:40 -0800683 if (GATT_GetConnIdIfConnected(gap_cb.gatt_if, peer_bda, &p_clcb->conn_id,
684 BT_TRANSPORT_LE))
685 p_clcb->connected = true;
Satya Calloji444a8da2015-03-06 10:38:22 -0800686
Myles Watson911d1ae2016-11-28 16:44:40 -0800687 /* hold the link here */
688 if (!GATT_Connect(gap_cb.gatt_if, p_clcb->bda, true, BT_TRANSPORT_LE, false))
689 return started;
Satya Calloji444a8da2015-03-06 10:38:22 -0800690
Myles Watson911d1ae2016-11-28 16:44:40 -0800691 /* enqueue the request */
692 gap_ble_enqueue_request(p_clcb, uuid, p_cback);
Satya Calloji444a8da2015-03-06 10:38:22 -0800693
Myles Watson911d1ae2016-11-28 16:44:40 -0800694 if (p_clcb->connected && p_clcb->cl_op_uuid == 0)
695 started = gap_ble_send_cl_read_request(p_clcb);
696 else /* wait for connection up or pending operation to finish */
697 started = true;
Satya Calloji444a8da2015-03-06 10:38:22 -0800698
Myles Watson911d1ae2016-11-28 16:44:40 -0800699 return started;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800700}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800701/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800702 *
703 * Function GAP_BleReadPeerPrefConnParams
704 *
705 * Description Start a process to read a connected peripheral's preferred
706 * connection parameters
707 *
708 * Returns true if read started, else false if GAP is busy
709 *
710 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800711bool GAP_BleReadPeerPrefConnParams(BD_ADDR peer_bda) {
712 return gap_ble_accept_cl_operation(peer_bda, GATT_UUID_GAP_PREF_CONN_PARAM,
713 NULL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800714}
715
716/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800717 *
718 * Function GAP_BleReadPeerDevName
719 *
Myles Watson9ca07092016-11-28 16:41:53 -0800720 * Description Start a process to read a connected peripheral's device
721 * name.
Myles Watsonee96a3c2016-11-23 14:49:54 -0800722 *
723 * Returns true if request accepted
724 *
725 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800726bool GAP_BleReadPeerDevName(BD_ADDR peer_bda, tGAP_BLE_CMPL_CBACK* p_cback) {
727 return gap_ble_accept_cl_operation(peer_bda, GATT_UUID_GAP_DEVICE_NAME,
728 p_cback);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800729}
730
Satya Calloji444a8da2015-03-06 10:38:22 -0800731/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800732 *
733 * Function GAP_BleReadPeerAddressResolutionCap
734 *
735 * Description Start a process to read peer address resolution capability
736 *
737 * Returns true if request accepted
738 *
739 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800740bool GAP_BleReadPeerAddressResolutionCap(BD_ADDR peer_bda,
741 tGAP_BLE_CMPL_CBACK* p_cback) {
742 return gap_ble_accept_cl_operation(peer_bda, GATT_UUID_GAP_CENTRAL_ADDR_RESOL,
743 p_cback);
Satya Calloji444a8da2015-03-06 10:38:22 -0800744}
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700745
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800746/*******************************************************************************
Myles Watsonee96a3c2016-11-23 14:49:54 -0800747 *
748 * Function GAP_BleCancelReadPeerDevName
749 *
750 * Description Cancel reading a peripheral's device name.
751 *
752 * Returns true if request accepted
753 *
754 ******************************************************************************/
Myles Watson911d1ae2016-11-28 16:44:40 -0800755bool GAP_BleCancelReadPeerDevName(BD_ADDR peer_bda) {
756 tGAP_CLCB* p_clcb = gap_find_clcb_by_bd_addr(peer_bda);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800757
Myles Watson911d1ae2016-11-28 16:44:40 -0800758 GAP_TRACE_EVENT(
759 "GAP_BleCancelReadPeerDevName() - BDA: %08x%04x cl_op_uuid: 0x%04x",
760 (peer_bda[0] << 24) + (peer_bda[1] << 16) + (peer_bda[2] << 8) +
761 peer_bda[3],
762 (peer_bda[4] << 8) + peer_bda[5],
763 (p_clcb == NULL) ? 0 : p_clcb->cl_op_uuid);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800764
Myles Watson911d1ae2016-11-28 16:44:40 -0800765 if (p_clcb == NULL) {
766 GAP_TRACE_ERROR("Cannot cancel current op is not get dev name");
767 return false;
768 }
769
770 if (!p_clcb->connected) {
771 if (!GATT_CancelConnect(gap_cb.gatt_if, peer_bda, true)) {
772 GAP_TRACE_ERROR("Cannot cancel where No connection id");
773 return false;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800774 }
Myles Watson911d1ae2016-11-28 16:44:40 -0800775 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800776
Myles Watson911d1ae2016-11-28 16:44:40 -0800777 gap_ble_cl_op_cmpl(p_clcb, false, 0, NULL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800778
Myles Watson911d1ae2016-11-28 16:44:40 -0800779 return (true);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800780}