1 /******************************************************************************
2  *
3  *  Copyright 2017 The Android Open Source Project
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  ******************************************************************************/
18 
19 #include <bluetooth/log.h>
20 #include <string.h>
21 
22 #include <array>
23 #include <queue>
24 
25 #include "common/init_flags.h"
26 #include "gap_api.h"
27 #include "gatt_api.h"
28 #include "hardware/bt_gatt_types.h"
29 #include "os/log.h"
30 #include "stack/include/bt_types.h"
31 #include "stack/include/bt_uuid16.h"
32 #include "stack/include/btm_client_interface.h"
33 #include "types/bluetooth/uuid.h"
34 #include "types/bt_transport.h"
35 #include "types/raw_address.h"
36 
37 using bluetooth::Uuid;
38 using namespace bluetooth;
39 
40 namespace {
41 
42 typedef struct {
43   uint16_t uuid;
44   tGAP_BLE_CMPL_CBACK* p_cback;
45 } tGAP_REQUEST;
46 
47 typedef struct {
48   RawAddress bda;
49   tGAP_BLE_CMPL_CBACK* p_cback;
50   uint16_t conn_id;
51   uint16_t cl_op_uuid;
52   bool connected;
53   std::queue<tGAP_REQUEST> requests;
54 } tGAP_CLCB;
55 
56 typedef struct {
57   uint16_t handle;
58   uint16_t uuid;
59   tGAP_BLE_ATTR_VALUE attr_value;
60 } tGAP_ATTR;
61 
62 void server_attr_request_cback(uint16_t, uint32_t, tGATTS_REQ_TYPE,
63                                tGATTS_DATA*);
64 void client_connect_cback(tGATT_IF, const RawAddress&, uint16_t, bool,
65                           tGATT_DISCONN_REASON, tBT_TRANSPORT);
66 void client_cmpl_cback(uint16_t, tGATTC_OPTYPE, tGATT_STATUS,
67                        tGATT_CL_COMPLETE*);
68 
69 tGATT_CBACK gap_cback = {
70     .p_conn_cb = client_connect_cback,
71     .p_cmpl_cb = client_cmpl_cback,
72     .p_disc_res_cb = nullptr,
73     .p_disc_cmpl_cb = nullptr,
74     .p_req_cb = server_attr_request_cback,
75     .p_enc_cmpl_cb = nullptr,
76     .p_congestion_cb = nullptr,
77     .p_phy_update_cb = nullptr,
78     .p_conn_update_cb = nullptr,
79     .p_subrate_chg_cb = nullptr,
80 };
81 
82 constexpr int GAP_CHAR_DEV_NAME_SIZE = BD_NAME_LEN;
83 constexpr int GAP_MAX_CHAR_NUM = 4;
84 
85 std::vector<tGAP_CLCB> gap_clcbs;
86 /* LE GAP attribute database */
87 std::array<tGAP_ATTR, GAP_MAX_CHAR_NUM> gatt_attr;
88 tGATT_IF gatt_if;
89 
90 /** returns LCB with macthing bd address, or nullptr */
find_clcb_by_bd_addr(const RawAddress & bda)91 tGAP_CLCB* find_clcb_by_bd_addr(const RawAddress& bda) {
92   for (auto& cb : gap_clcbs)
93     if (cb.bda == bda) return &cb;
94 
95   return nullptr;
96 }
97 
98 /** returns LCB with macthing connection ID, or nullptr if not found  */
ble_find_clcb_by_conn_id(uint16_t conn_id)99 tGAP_CLCB* ble_find_clcb_by_conn_id(uint16_t conn_id) {
100   for (auto& cb : gap_clcbs)
101     if (cb.connected && cb.conn_id == conn_id) return &cb;
102 
103   return nullptr;
104 }
105 
106 /** allocates a GAP connection link control block */
clcb_alloc(const RawAddress & bda)107 tGAP_CLCB* clcb_alloc(const RawAddress& bda) {
108   gap_clcbs.emplace_back();
109   tGAP_CLCB& cb = gap_clcbs.back();
110   cb.bda = bda;
111   return &cb;
112 }
113 
114 /** The function clean up the pending request queue in GAP */
clcb_dealloc(tGAP_CLCB & clcb)115 void clcb_dealloc(tGAP_CLCB& clcb) {
116   // put last element into place of current element, and remove last one - just
117   // fast remove.
118   for (auto it = gap_clcbs.begin(); it != gap_clcbs.end(); it++) {
119     if (it->conn_id == clcb.conn_id) {
120       auto last_one = std::prev(gap_clcbs.end());
121       *it = *last_one;
122       gap_clcbs.erase(last_one);
123       return;
124     }
125   }
126 }
127 
128 /** GAP Attributes Database Request callback */
read_attr_value(uint16_t handle,tGATT_VALUE * p_value,bool is_long)129 tGATT_STATUS read_attr_value(uint16_t handle, tGATT_VALUE* p_value,
130                              bool is_long) {
131   uint8_t* p = p_value->value;
132   uint16_t offset = p_value->offset;
133   uint8_t* p_dev_name = NULL;
134 
135   for (const tGAP_ATTR& db_attr : gatt_attr) {
136     const tGAP_BLE_ATTR_VALUE& attr_value = db_attr.attr_value;
137     if (handle == db_attr.handle) {
138       if (db_attr.uuid != GATT_UUID_GAP_DEVICE_NAME && is_long)
139         return GATT_NOT_LONG;
140 
141       switch (db_attr.uuid) {
142         case GATT_UUID_GAP_DEVICE_NAME:
143           if (get_btm_client_interface().local.BTM_ReadLocalDeviceName(
144                   (const char**)&p_dev_name) != BTM_SUCCESS) {
145             log::warn("Unable to read local device name");
146           };
147           if (strlen((char*)p_dev_name) > GATT_MAX_ATTR_LEN)
148             p_value->len = GATT_MAX_ATTR_LEN;
149           else
150             p_value->len = (uint16_t)strlen((char*)p_dev_name);
151 
152           if (offset > p_value->len)
153             return GATT_INVALID_OFFSET;
154           else {
155             p_value->len -= offset;
156             p_dev_name += offset;
157             ARRAY_TO_STREAM(p, p_dev_name, p_value->len);
158           }
159           break;
160 
161         case GATT_UUID_GAP_ICON:
162           UINT16_TO_STREAM(p, attr_value.icon);
163           p_value->len = 2;
164           break;
165 
166         case GATT_UUID_GAP_PREF_CONN_PARAM:
167           UINT16_TO_STREAM(p, attr_value.conn_param.int_min); /* int_min */
168           UINT16_TO_STREAM(p, attr_value.conn_param.int_max); /* int_max */
169           UINT16_TO_STREAM(p, attr_value.conn_param.latency); /* latency */
170           UINT16_TO_STREAM(p, attr_value.conn_param.sp_tout); /* sp_tout */
171           p_value->len = 8;
172           break;
173 
174         /* address resolution */
175         case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
176           UINT8_TO_STREAM(p, attr_value.addr_resolution);
177           p_value->len = 1;
178           break;
179       }
180       return GATT_SUCCESS;
181     }
182   }
183   return GATT_NOT_FOUND;
184 }
185 
186 /** GAP Attributes Database Read/Read Blob Request process */
proc_read(tGATTS_REQ_TYPE,tGATT_READ_REQ * p_data,tGATTS_RSP * p_rsp)187 tGATT_STATUS proc_read(tGATTS_REQ_TYPE, tGATT_READ_REQ* p_data,
188                        tGATTS_RSP* p_rsp) {
189   if (p_data->is_long) p_rsp->attr_value.offset = p_data->offset;
190 
191   p_rsp->attr_value.handle = p_data->handle;
192 
193   return read_attr_value(p_data->handle, &p_rsp->attr_value, p_data->is_long);
194 }
195 
196 /** GAP ATT server process a write request */
proc_write_req(tGATTS_REQ_TYPE,tGATT_WRITE_REQ * p_data)197 tGATT_STATUS proc_write_req(tGATTS_REQ_TYPE, tGATT_WRITE_REQ* p_data) {
198   for (const auto& db_addr : gatt_attr)
199     if (p_data->handle == db_addr.handle) return GATT_WRITE_NOT_PERMIT;
200 
201   return GATT_NOT_FOUND;
202 }
203 
204 /** GAP ATT server attribute access request callback */
server_attr_request_cback(uint16_t conn_id,uint32_t trans_id,tGATTS_REQ_TYPE type,tGATTS_DATA * p_data)205 void server_attr_request_cback(uint16_t conn_id, uint32_t trans_id,
206                                tGATTS_REQ_TYPE type, tGATTS_DATA* p_data) {
207   tGATT_STATUS status = GATT_INVALID_PDU;
208   bool ignore = false;
209 
210   tGATTS_RSP rsp_msg;
211   memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
212 
213   switch (type) {
214     case GATTS_REQ_TYPE_READ_CHARACTERISTIC:
215     case GATTS_REQ_TYPE_READ_DESCRIPTOR:
216       status = proc_read(type, &p_data->read_req, &rsp_msg);
217       break;
218 
219     case GATTS_REQ_TYPE_WRITE_CHARACTERISTIC:
220     case GATTS_REQ_TYPE_WRITE_DESCRIPTOR:
221       if (!p_data->write_req.need_rsp) ignore = true;
222 
223       status = proc_write_req(type, &p_data->write_req);
224       break;
225 
226     case GATTS_REQ_TYPE_WRITE_EXEC:
227       ignore = true;
228       log::verbose("Ignore GATTS_REQ_TYPE_WRITE_EXEC");
229       break;
230 
231     case GATTS_REQ_TYPE_MTU:
232       log::verbose("Get MTU exchange new mtu size: {}", p_data->mtu);
233       ignore = true;
234       break;
235 
236     default:
237       log::verbose("Unknown/unexpected LE GAP ATT request: 0x{:02x}", type);
238       break;
239   }
240 
241   if (!ignore) {
242     if (GATTS_SendRsp(conn_id, trans_id, status, &rsp_msg) != GATT_SUCCESS) {
243       log::warn("Unable to send GATT ervier response conn_id:{}", conn_id);
244     }
245   }
246 }
247 
248 /**
249  * utility function to send a read request for a GAP charactersitic.
250  * Returns true if read started, else false if GAP is busy.
251  */
send_cl_read_request(tGAP_CLCB & clcb)252 bool send_cl_read_request(tGAP_CLCB& clcb) {
253   if (!clcb.requests.size() || clcb.cl_op_uuid != 0) {
254     return false;
255   }
256 
257   tGAP_REQUEST& req = clcb.requests.front();
258   clcb.p_cback = req.p_cback;
259   uint16_t uuid = req.uuid;
260   clcb.requests.pop();
261 
262   tGATT_READ_PARAM param;
263   memset(&param, 0, sizeof(tGATT_READ_PARAM));
264 
265   param.service.uuid = Uuid::From16Bit(uuid);
266   param.service.s_handle = 1;
267   param.service.e_handle = 0xFFFF;
268   param.service.auth_req = 0;
269 
270   if (GATTC_Read(clcb.conn_id, GATT_READ_BY_TYPE, &param) == GATT_SUCCESS) {
271     clcb.cl_op_uuid = uuid;
272   }
273 
274   return true;
275 }
276 
277 /** GAP client operation complete callback */
cl_op_cmpl(tGAP_CLCB & clcb,bool status,uint16_t len,uint8_t * p_name)278 void cl_op_cmpl(tGAP_CLCB& clcb, bool status, uint16_t len, uint8_t* p_name) {
279   tGAP_BLE_CMPL_CBACK* p_cback = clcb.p_cback;
280   uint16_t op = clcb.cl_op_uuid;
281 
282   clcb.cl_op_uuid = 0;
283   clcb.p_cback = NULL;
284 
285   if (p_cback && op) {
286     (*p_cback)(status, clcb.bda, len, (char*)p_name);
287   }
288 
289   /* if no further activity is requested in callback, drop the link */
290   if (clcb.connected) {
291     if (!send_cl_read_request(clcb)) {
292       if (GATT_Disconnect(clcb.conn_id) != GATT_SUCCESS) {
293         log::warn("Unable to disconnect GATT conn_id:{}", clcb.conn_id);
294       }
295       clcb_dealloc(clcb);
296     }
297   }
298 }
299 
300 /** Client connection callback */
client_connect_cback(tGATT_IF,const RawAddress & bda,uint16_t conn_id,bool connected,tGATT_DISCONN_REASON,tBT_TRANSPORT)301 void client_connect_cback(tGATT_IF, const RawAddress& bda, uint16_t conn_id,
302                           bool connected, tGATT_DISCONN_REASON /* reason */,
303                           tBT_TRANSPORT) {
304   tGAP_CLCB* p_clcb = find_clcb_by_bd_addr(bda);
305   if (p_clcb == NULL) {
306     log::info("No active GAP service found for peer:{} callback:{}", bda,
307               (connected) ? "Connected" : "Disconnected");
308     return;
309   }
310 
311   if (connected) {
312     log::debug("Connected GAP to remote device");
313     p_clcb->conn_id = conn_id;
314     p_clcb->connected = true;
315     /* start operation is pending */
316     send_cl_read_request(*p_clcb);
317   } else {
318     log::warn("Disconnected GAP from remote device");
319     p_clcb->connected = false;
320     cl_op_cmpl(*p_clcb, false, 0, NULL);
321     /* clean up clcb */
322     clcb_dealloc(*p_clcb);
323   }
324 }
325 
326 /** Client operation complete callback */
client_cmpl_cback(uint16_t conn_id,tGATTC_OPTYPE op,tGATT_STATUS status,tGATT_CL_COMPLETE * p_data)327 void client_cmpl_cback(uint16_t conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
328                        tGATT_CL_COMPLETE* p_data) {
329   tGAP_CLCB* p_clcb = ble_find_clcb_by_conn_id(conn_id);
330   uint16_t op_type;
331   uint16_t min, max, latency, tout;
332   uint16_t len;
333   uint8_t* pp;
334 
335   if (p_clcb == NULL) return;
336 
337   op_type = p_clcb->cl_op_uuid;
338 
339   /* Currently we only issue read commands */
340   if (op != GATTC_OPTYPE_READ) return;
341 
342   if (status != GATT_SUCCESS) {
343     cl_op_cmpl(*p_clcb, false, 0, NULL);
344     return;
345   }
346 
347   pp = p_data->att_value.value;
348   switch (op_type) {
349     case GATT_UUID_GAP_PREF_CONN_PARAM:
350       /* Extract the peripheral preferred connection parameters and save them */
351       STREAM_TO_UINT16(min, pp);
352       STREAM_TO_UINT16(max, pp);
353       STREAM_TO_UINT16(latency, pp);
354       STREAM_TO_UINT16(tout, pp);
355 
356       BTM_BleSetPrefConnParams(p_clcb->bda, min, max, latency, tout);
357       /* release the connection here */
358       cl_op_cmpl(*p_clcb, true, 0, NULL);
359       break;
360 
361     case GATT_UUID_GAP_DEVICE_NAME:
362       len = (uint16_t)strlen((char*)pp);
363       if (len > GAP_CHAR_DEV_NAME_SIZE) len = GAP_CHAR_DEV_NAME_SIZE;
364       cl_op_cmpl(*p_clcb, true, len, pp);
365       break;
366 
367     case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
368       cl_op_cmpl(*p_clcb, true, 1, pp);
369       break;
370   }
371 }
372 
accept_client_operation(const RawAddress & peer_bda,uint16_t uuid,tGAP_BLE_CMPL_CBACK * p_cback)373 bool accept_client_operation(const RawAddress& peer_bda, uint16_t uuid,
374                              tGAP_BLE_CMPL_CBACK* p_cback) {
375   if (p_cback == NULL && uuid != GATT_UUID_GAP_PREF_CONN_PARAM) return false;
376 
377   tGAP_CLCB* p_clcb = find_clcb_by_bd_addr(peer_bda);
378   if (p_clcb == NULL) {
379     p_clcb = clcb_alloc(peer_bda);
380   }
381 
382   if (GATT_GetConnIdIfConnected(gatt_if, peer_bda, &p_clcb->conn_id,
383                                 BT_TRANSPORT_LE))
384     p_clcb->connected = true;
385 
386   if (!GATT_Connect(gatt_if, p_clcb->bda, BTM_BLE_DIRECT_CONNECTION,
387                     BT_TRANSPORT_LE, true))
388     return false;
389 
390   /* enqueue the request */
391   p_clcb->requests.push({.uuid = uuid, .p_cback = p_cback});
392 
393   if (p_clcb->connected && p_clcb->cl_op_uuid == 0)
394     return send_cl_read_request(*p_clcb);
395   else /* wait for connection up or pending operation to finish */
396     return true;
397 }
398 
399 }  // namespace
400 
401 /*******************************************************************************
402  *
403  * Function         btm_ble_att_db_init
404  *
405  * Description      GAP ATT database initalization.
406  *
407  * Returns          void.
408  *
409  ******************************************************************************/
gap_attr_db_init(void)410 void gap_attr_db_init(void) {
411   /* Fill our internal UUID with a fixed pattern 0x82 */
412   std::array<uint8_t, Uuid::kNumBytes128> tmp;
413   tmp.fill(0x82);
414   Uuid app_uuid = Uuid::From128BitBE(tmp);
415   gatt_attr.fill({});
416 
417   gatt_if = GATT_Register(app_uuid, "Gap", &gap_cback, false);
418 
419   GATT_StartIf(gatt_if);
420 
421   Uuid svc_uuid = Uuid::From16Bit(UUID_SERVCLASS_GAP_SERVER);
422   Uuid name_uuid = Uuid::From16Bit(GATT_UUID_GAP_DEVICE_NAME);
423   Uuid icon_uuid = Uuid::From16Bit(GATT_UUID_GAP_ICON);
424   Uuid addr_res_uuid = Uuid::From16Bit(GATT_UUID_GAP_CENTRAL_ADDR_RESOL);
425 
426   btgatt_db_element_t service[] = {
427       {
428           .uuid = svc_uuid,
429           .type = BTGATT_DB_PRIMARY_SERVICE,
430       },
431       {.uuid = name_uuid,
432        .type = BTGATT_DB_CHARACTERISTIC,
433        .properties = GATT_CHAR_PROP_BIT_READ,
434        .permissions = GATT_PERM_READ_IF_ENCRYPTED_OR_DISCOVERABLE},
435       {.uuid = icon_uuid,
436        .type = BTGATT_DB_CHARACTERISTIC,
437        .properties = GATT_CHAR_PROP_BIT_READ,
438        .permissions = GATT_PERM_READ},
439       {.uuid = addr_res_uuid,
440        .type = BTGATT_DB_CHARACTERISTIC,
441        .properties = GATT_CHAR_PROP_BIT_READ,
442        .permissions = GATT_PERM_READ}
443 #if (BTM_PERIPHERAL_ENABLED == TRUE) /* Only needed for peripheral testing */
444       ,
445       {.uuid = Uuid::From16Bit(GATT_UUID_GAP_PREF_CONN_PARAM),
446        .type = BTGATT_DB_CHARACTERISTIC,
447        .properties = GATT_CHAR_PROP_BIT_READ,
448        .permissions = GATT_PERM_READ}
449 #endif
450   };
451 
452   /* Add a GAP service */
453   if (GATTS_AddService(gatt_if, service,
454                        sizeof(service) / sizeof(btgatt_db_element_t)) !=
455       GATT_SERVICE_STARTED) {
456     log::warn("Unable to add GATT services gatt_if:{}", gatt_if);
457   }
458 
459   gatt_attr[0].uuid = GATT_UUID_GAP_DEVICE_NAME;
460   gatt_attr[0].handle = service[1].attribute_handle;
461 
462   gatt_attr[1].uuid = GATT_UUID_GAP_ICON;
463   gatt_attr[1].handle = service[2].attribute_handle;
464 
465   gatt_attr[2].uuid = GATT_UUID_GAP_CENTRAL_ADDR_RESOL;
466   gatt_attr[2].handle = service[3].attribute_handle;
467   gatt_attr[2].attr_value.addr_resolution = 0;
468 
469 #if (BTM_PERIPHERAL_ENABLED == TRUE) /*  Only needed for peripheral testing */
470 
471   gatt_attr[3].uuid = GATT_UUID_GAP_PREF_CONN_PARAM;
472   gatt_attr[3].attr_value.conn_param.int_max = GAP_PREFER_CONN_INT_MAX; /* 6 */
473   gatt_attr[3].attr_value.conn_param.int_min = GAP_PREFER_CONN_INT_MIN; /* 0 */
474   gatt_attr[3].attr_value.conn_param.latency = GAP_PREFER_CONN_LATENCY; /* 0 */
475   gatt_attr[3].attr_value.conn_param.sp_tout =
476       GAP_PREFER_CONN_SP_TOUT; /* 2000 */
477   gatt_attr[3].handle = service[4].attribute_handle;
478 #endif
479 }
480 
481 /*******************************************************************************
482  *
483  * Function         GAP_BleAttrDBUpdate
484  *
485  * Description      GAP ATT database update.
486  *
487  ******************************************************************************/
GAP_BleAttrDBUpdate(uint16_t attr_uuid,tGAP_BLE_ATTR_VALUE * p_value)488 void GAP_BleAttrDBUpdate(uint16_t attr_uuid, tGAP_BLE_ATTR_VALUE* p_value) {
489   for (tGAP_ATTR& db_attr : gatt_attr) {
490     if (db_attr.uuid == attr_uuid) {
491       switch (attr_uuid) {
492         case GATT_UUID_GAP_ICON:
493           db_attr.attr_value.icon = p_value->icon;
494           break;
495 
496         case GATT_UUID_GAP_PREF_CONN_PARAM:
497           memcpy((void*)&db_attr.attr_value.conn_param,
498                  (const void*)&p_value->conn_param,
499                  sizeof(tGAP_BLE_PREF_PARAM));
500           break;
501 
502         case GATT_UUID_GAP_DEVICE_NAME:
503           if (get_btm_client_interface().local.BTM_SetLocalDeviceName(
504                   (const char*)p_value->p_dev_name) != BTM_SUCCESS) {
505             log::warn("Unable to set local name");
506           }
507           break;
508 
509         case GATT_UUID_GAP_CENTRAL_ADDR_RESOL:
510           db_attr.attr_value.addr_resolution = p_value->addr_resolution;
511           break;
512       }
513       break;
514     }
515   }
516 
517   return;
518 }
519 
520 /*******************************************************************************
521  *
522  * Function         GAP_BleReadPeerPrefConnParams
523  *
524  * Description      Start a process to read a connected peripheral's preferred
525  *                  connection parameters
526  *
527  * Returns          true if read started, else false if GAP is busy
528  *
529  ******************************************************************************/
GAP_BleReadPeerPrefConnParams(const RawAddress & peer_bda)530 bool GAP_BleReadPeerPrefConnParams(const RawAddress& peer_bda) {
531   return accept_client_operation(peer_bda, GATT_UUID_GAP_PREF_CONN_PARAM, NULL);
532 }
533 
534 /*******************************************************************************
535  *
536  * Function         GAP_BleReadPeerDevName
537  *
538  * Description      Start a process to read a connected peripheral's device
539  *                  name.
540  *
541  * Returns          true if request accepted
542  *
543  ******************************************************************************/
GAP_BleReadPeerDevName(const RawAddress & peer_bda,tGAP_BLE_CMPL_CBACK * p_cback)544 bool GAP_BleReadPeerDevName(const RawAddress& peer_bda,
545                             tGAP_BLE_CMPL_CBACK* p_cback) {
546   return accept_client_operation(peer_bda, GATT_UUID_GAP_DEVICE_NAME, p_cback);
547 }
548 
549 /*******************************************************************************
550  *
551  * Function         GAP_BleCancelReadPeerDevName
552  *
553  * Description      Cancel reading a peripheral's device name.
554  *
555  * Returns          true if request accepted
556  *
557  ******************************************************************************/
GAP_BleCancelReadPeerDevName(const RawAddress & peer_bda)558 bool GAP_BleCancelReadPeerDevName(const RawAddress& peer_bda) {
559   tGAP_CLCB* p_clcb = find_clcb_by_bd_addr(peer_bda);
560 
561   if (p_clcb == NULL) {
562     log::error("Cannot cancel current op is not get dev name");
563     return false;
564   }
565 
566   if (!p_clcb->connected) {
567     if (!GATT_CancelConnect(gatt_if, peer_bda, true)) {
568       log::error("Cannot cancel where No connection id");
569       return false;
570     }
571   }
572 
573   cl_op_cmpl(*p_clcb, false, 0, NULL);
574 
575   return (true);
576 }
577