Home | History | Annotate | Download | only in gap
      1 /******************************************************************************
      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  ******************************************************************************/
     18 #include "bt_target.h"
     19 
     20 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
     21 
     22 #include <string.h>
     23 #include "gap_int.h"
     24 #include "gap_api.h"
     25 #include "gattdefs.h"
     26 #include "gatt_api.h"
     27 #include "gatt_int.h"
     28 #include "btm_int.h"
     29 #include "hcimsgs.h"
     30 
     31 #define GAP_CHAR_ICON_SIZE          2
     32 #define GAP_CHAR_DEV_NAME_SIZE      248
     33 #define GAP_BLE_PRIVACY_FLAG_SIZE    1
     34 
     35 #define GAP_MAX_NUM_INC_SVR       0
     36 #define GAP_MAX_ATTR_NUM          (2 * GAP_MAX_CHAR_NUM + GAP_MAX_NUM_INC_SVR + 1)
     37 #define GAP_MAX_CHAR_VALUE_SIZE   (30 + GAP_CHAR_DEV_NAME_SIZE)
     38 
     39 
     40 #ifndef GAP_ATTR_DB_SIZE
     41 #define GAP_ATTR_DB_SIZE      GATT_DB_MEM_SIZE(GAP_MAX_NUM_INC_SVR, GAP_MAX_CHAR_NUM, GAP_MAX_CHAR_VALUE_SIZE)
     42 #endif
     43 
     44 /* privacy flag readable and writable with encryption on */
     45 #ifndef GAP_BLE_PRIVACY_FLAG_PERM
     46 #define GAP_BLE_PRIVACY_FLAG_PERM       (GATT_PERM_READ|GATT_PERM_WRITE)
     47 #endif
     48 
     49 #define GATT_READ_GAP_PRIVACY_FLAG      1
     50 #define GATT_SET_GAP_PRIVACY_FLAG       2
     51 #define GATT_READ_GAP_REMOTE_NAME       3
     52 #define GATT_UPDATE_RECONN_ADDR         4
     53 
     54 #define GAP_BLE_PRIVACY_UNKNOWN         0xff
     55 
     56 static void gap_ble_s_attr_request_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYPE op_code, tGATTS_DATA *p_data);
     57 
     58 /* client connection callback */
     59 static void  gap_ble_c_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id, BOOLEAN connected, tGATT_DISCONN_REASON reason);
     60 static void  gap_ble_c_cmpl_cback (UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status, tGATT_CL_COMPLETE *p_data);
     61 
     62 static tGATT_CBACK gap_cback =
     63 {
     64     gap_ble_c_connect_cback,
     65     gap_ble_c_cmpl_cback,
     66     NULL,
     67     NULL,
     68     gap_ble_s_attr_request_cback
     69 };
     70 
     71 
     72 
     73 /*******************************************************************************
     74 **
     75 ** Function         gap_find_clcb_by_bd_addr
     76 **
     77 ** Description      The function searches all LCB with macthing bd address
     78 **
     79 ** Returns          total number of clcb found.
     80 **
     81 *******************************************************************************/
     82 tGAP_CLCB *gap_find_clcb_by_bd_addr(BD_ADDR bda)
     83 {
     84     UINT8 i_clcb;
     85     tGAP_CLCB    *p_clcb = NULL;
     86 
     87     for (i_clcb = 0, p_clcb= gap_cb.clcb; i_clcb < GAP_MAX_CL; i_clcb++, p_clcb++)
     88     {
     89         if (p_clcb->in_use && !memcmp(p_clcb->bda, bda, BD_ADDR_LEN))
     90         {
     91             return p_clcb;
     92         }
     93     }
     94 
     95     return NULL;
     96 }
     97 
     98 /*******************************************************************************
     99 **
    100 ** Function         gap_ble_find_clcb_by_conn_id
    101 **
    102 ** Description      The function searches all LCB with macthing connection ID
    103 **
    104 ** Returns          total number of clcb found.
    105 **
    106 *******************************************************************************/
    107 tGAP_CLCB *gap_ble_find_clcb_by_conn_id(UINT16 conn_id)
    108 {
    109     UINT8 i_clcb;
    110     tGAP_CLCB    *p_clcb = NULL;
    111 
    112     for (i_clcb = 0, p_clcb= gap_cb.clcb; i_clcb < GAP_MAX_CL; i_clcb++, p_clcb++)
    113     {
    114         if (p_clcb->in_use && p_clcb->connected && p_clcb->conn_id == conn_id)
    115         {
    116             return p_clcb;
    117         }
    118     }
    119 
    120     return p_clcb;
    121 }
    122 
    123 /*******************************************************************************
    124 **
    125 ** Function         gap_clcb_alloc
    126 **
    127 ** Description      The function allocates a GAP  connection link control block
    128 **
    129 ** Returns           NULL if not found. Otherwise pointer to the connection link block.
    130 **
    131 *******************************************************************************/
    132 tGAP_CLCB *gap_clcb_alloc (UINT16 conn_id, BD_ADDR bda)
    133 {
    134     UINT8         i_clcb = 0;
    135     tGAP_CLCB    *p_clcb = NULL;
    136 
    137     for (i_clcb = 0, p_clcb= gap_cb.clcb; i_clcb < GAP_MAX_CL; i_clcb++, p_clcb++)
    138     {
    139         if (!p_clcb->in_use)
    140         {
    141             p_clcb->in_use      = TRUE;
    142             p_clcb->conn_id     = conn_id;
    143             p_clcb->connected   = TRUE;
    144             memcpy (p_clcb->bda, bda, BD_ADDR_LEN);
    145             break;
    146         }
    147     }
    148     return p_clcb;
    149 }
    150 
    151 /*******************************************************************************
    152 **
    153 ** Function         gap_find_alloc_clcb
    154 **
    155 ** Description      The function find or allocates a GAP  connection link control block
    156 **
    157 ** Returns           NULL if not found. Otherwise pointer to the connection link block.
    158 **
    159 *******************************************************************************/
    160 tGAP_CLCB *gap_find_alloc_clcb (UINT16 conn_id, BD_ADDR bda)
    161 {
    162     UINT8         i_clcb = 0;
    163     tGAP_CLCB    *p_clcb = NULL;
    164 
    165     for (i_clcb = 0, p_clcb= gap_cb.clcb; i_clcb < GAP_MAX_CL; i_clcb++, p_clcb++)
    166     {
    167         if (!p_clcb->in_use)
    168         {
    169             p_clcb->in_use      = TRUE;
    170             p_clcb->conn_id     = conn_id;
    171             p_clcb->connected   = TRUE;
    172             memcpy (p_clcb->bda, bda, BD_ADDR_LEN);
    173             break;
    174         }
    175     }
    176     return p_clcb;
    177 }
    178 
    179 /*******************************************************************************
    180 **
    181 ** Function         gap_get_conn_id_if_connected
    182 **
    183 ** Description      This function returns a connecttion handle to a ATT server
    184 **                  if the server is already connected
    185 **
    186 ** Parameters       client_if: client interface.
    187 **                  bd_addr: peer device address.
    188 **
    189 ** Returns          Connection handle or invalid handle value
    190 **
    191 *******************************************************************************/
    192 UINT16 gap_get_conn_id_if_connected (BD_ADDR bd_addr)
    193 {
    194     tGAP_CLCB       *p_clcb;
    195     UINT16          i;
    196 
    197     GAP_TRACE_EVENT2 ("gap_get_conn_id_if_connected() - BDA: %08x%04x ",
    198                       (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
    199                       (bd_addr[4]<<8)+bd_addr[5]);
    200 
    201     for (i = 0, p_clcb = gap_cb.clcb; i < GAP_MAX_CL; i++, p_clcb++)
    202     {
    203         if (p_clcb->in_use && p_clcb->connected && !memcmp(p_clcb->bda, bd_addr,  BD_ADDR_LEN) )
    204         {
    205             return(p_clcb->conn_id);
    206         }
    207     }
    208 
    209     /* If here, failed to allocate a client control block */
    210     GATT_TRACE_DEBUG0 ("gap_get_conn_id_if_connected: not connected");
    211     return(GATT_INVALID_CONN_ID);
    212 }
    213 
    214 /*******************************************************************************
    215 **
    216 ** Function         gap_ble_enqueue_op
    217 **
    218 ** Description      enqueue a GAP operation when GAP client is busy
    219 **
    220 ** Returns          void
    221 **
    222 *******************************************************************************/
    223 void gap_ble_enqueue_op( tGAP_CLCB * p_clcb, UINT8 op, BD_ADDR reconn_addr, UINT8 privacy_flag, void *p_cback)
    224 {
    225     tGAP_BLE_PENDING_OP  *p_op = (tGAP_BLE_PENDING_OP *)GKI_getbuf(sizeof(tGAP_BLE_PENDING_OP));
    226 
    227     if (p_op != NULL)
    228     {
    229         p_op->op = op;
    230         p_op->p_pending_cback = p_cback;
    231 
    232         if (op == GATT_SET_GAP_PRIVACY_FLAG)
    233             p_op->pending_data.privacy_flag = privacy_flag;
    234         else if (op == GATT_UPDATE_RECONN_ADDR)
    235             memcpy(p_op->pending_data.reconn_addr, reconn_addr, BD_ADDR_LEN);
    236 
    237         GKI_enqueue(&p_clcb->pending_op_q, p_op);
    238     }
    239 }
    240 
    241 /*******************************************************************************
    242 **
    243 ** Function         gap_ble_process_pending_op
    244 **
    245 ** Description      get next pending operation and process it
    246 **
    247 ** Returns          void
    248 **
    249 *******************************************************************************/
    250 static BOOLEAN gap_ble_process_pending_op(tGAP_CLCB *p_clcb)
    251 {
    252     tGAP_BLE_PENDING_OP *p_pending_op = (tGAP_BLE_PENDING_OP *)GKI_dequeue(&p_clcb->pending_op_q);
    253     BOOLEAN         started = FALSE;
    254 
    255     if (p_pending_op != NULL)
    256     {
    257         if (p_pending_op->op == GATT_UPDATE_RECONN_ADDR)
    258         {
    259             GAP_BleUpdateReconnectAddr( p_clcb->bda,
    260                                         p_pending_op->pending_data.reconn_addr,
    261                                         (tGAP_BLE_RECONN_ADDR_CBACK *)p_pending_op->p_pending_cback);
    262             started = TRUE;
    263         }
    264         GKI_freebuf(p_pending_op);
    265     }
    266     else
    267     {
    268         GAP_TRACE_EVENT0("No pending operation");
    269     }
    270 
    271     return started;
    272 }
    273 
    274 /*******************************************************************************
    275 **   GAP Attributes Database Request callback
    276 *******************************************************************************/
    277 tGATT_STATUS gap_read_attr_value (UINT16 handle, tGATT_VALUE *p_value, BOOLEAN is_long)
    278 {
    279     tGAP_ATTR   *p_db_attr = gap_cb.gatt_attr;
    280     UINT8       *p = p_value->value, i;
    281     UINT16      offset = p_value->offset;
    282     UINT8       *p_dev_name = NULL;
    283 
    284     for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++)
    285     {
    286         if (handle == p_db_attr->handle)
    287         {
    288             if (p_db_attr->uuid != GATT_UUID_GAP_DEVICE_NAME &&
    289                 is_long == TRUE)
    290                 return GATT_NOT_LONG;
    291 
    292             switch (p_db_attr->uuid)
    293             {
    294                 case GATT_UUID_GAP_DEVICE_NAME:
    295                     BTM_ReadLocalDeviceName((char **)&p_dev_name);
    296                     if (strlen ((char *)p_dev_name) > GATT_MAX_ATTR_LEN)
    297                         p_value->len = GATT_MAX_ATTR_LEN;
    298                     else
    299                         p_value->len = (UINT16)strlen ((char *)p_dev_name);
    300 
    301                     if (offset > p_value->len)
    302                         return GATT_INVALID_OFFSET;
    303                     else
    304                     {
    305                         p_value->len -= offset;
    306                         p_dev_name += offset;
    307                         ARRAY_TO_STREAM(p, p_dev_name, p_value->len);
    308                         GAP_TRACE_EVENT1("GATT_UUID_GAP_DEVICE_NAME len=0x%04x", p_value->len);
    309                     }
    310                     break;
    311 
    312                 case GATT_UUID_GAP_ICON:
    313                     UINT16_TO_STREAM(p, p_db_attr->attr_value.icon);
    314                     p_value->len = 2;
    315                     break;
    316 
    317                 case GATT_UUID_GAP_PRIVACY_FLAG:
    318                     UINT8_TO_STREAM(p, p_db_attr->attr_value.privacy);
    319                     p_value->len = 1;
    320                     break;
    321 
    322                 case GATT_UUID_GAP_RECONN_ADDR:
    323                     p_value->len = BD_ADDR_LEN;
    324                     BDADDR_TO_STREAM(p, p_db_attr->attr_value.reconn_bda);
    325                     break;
    326 
    327                 case GATT_UUID_GAP_PREF_CONN_PARAM:
    328                     UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.int_min); /* int_min */
    329                     UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.int_max); /* int_max */
    330                     UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.latency); /* latency */
    331                     UINT16_TO_STREAM(p, p_db_attr->attr_value.conn_param.sp_tout);  /* sp_tout */
    332                     p_value->len =8;
    333                     break;
    334             }
    335             return GATT_SUCCESS;
    336         }
    337     }
    338     return GATT_NOT_FOUND;
    339 }
    340 
    341 /*******************************************************************************
    342 **   GAP Attributes Database Read/Read Blob Request process
    343 *******************************************************************************/
    344 tGATT_STATUS gap_proc_read (tGATTS_REQ_TYPE type, tGATT_READ_REQ *p_data, tGATTS_RSP *p_rsp)
    345 {
    346     tGATT_STATUS    status = GATT_NO_RESOURCES;
    347 
    348     if (p_data->is_long)
    349         p_rsp->attr_value.offset = p_data->offset;
    350 
    351     p_rsp->attr_value.handle = p_data->handle;
    352 
    353     status = gap_read_attr_value(p_data->handle, &p_rsp->attr_value, p_data->is_long);
    354 
    355     return status;
    356 }
    357 BOOLEAN gap_read_local_reconn_addr(BD_ADDR_PTR reconn_bda)
    358 {
    359     BD_ADDR dummy_bda = {0};
    360 
    361     if (memcmp(gap_cb.reconn_bda, dummy_bda, BD_ADDR_LEN) != 0)
    362     {
    363         memcpy(reconn_bda, gap_cb.reconn_bda, BD_ADDR_LEN);
    364         return TRUE;
    365     }
    366     else
    367         return FALSE;
    368 }
    369 
    370 /******************************************************************************
    371 **
    372 ** Function         gap_proc_write_req
    373 **
    374 ** Description      GAP ATT server process a write request.
    375 **
    376 ** Returns          void.
    377 **
    378 *******************************************************************************/
    379 UINT8 gap_proc_write_req( tGATTS_REQ_TYPE type, tGATT_WRITE_REQ *p_data)
    380 {
    381     tGAP_ATTR   *p_db_attr = gap_cb.gatt_attr;
    382     UINT8   i;
    383 
    384     for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++)
    385     {
    386         if (p_data-> handle == p_db_attr->handle)
    387         {
    388             if (p_data->offset != 0) return GATT_NOT_LONG;
    389             if (p_data->is_prep) return GATT_REQ_NOT_SUPPORTED;
    390 
    391 /* DO NOT SUPPORT RECONNECTION ADDRESS FOR NOW
    392 
    393             if (p_db_attr->uuid == GATT_UUID_GAP_RECONN_ADDR)
    394             {
    395                 if (!btm_cb.ble_ctr_cb.privacy)
    396                     return GATT_WRITE_NOT_PERMIT;
    397                 if (p_data->len != BD_ADDR_LEN) return GATT_INVALID_ATTR_LEN;
    398 
    399                 STREAM_TO_BDADDR(p_db_attr->attr_value.reconn_bda, p);
    400                 // write direct connection address
    401                 memcpy(&gap_cb.reconn_bda, p_db_attr->attr_value.reconn_bda, BD_ADDR_LEN);
    402 
    403                 return GATT_SUCCESS;
    404             }
    405             else
    406 */
    407             return GATT_WRITE_NOT_PERMIT;
    408         }
    409     }
    410     return GATT_NOT_FOUND;
    411 
    412 }
    413 
    414 /******************************************************************************
    415 **
    416 ** Function         gap_ble_s_attr_request_cback
    417 **
    418 ** Description      GAP ATT server attribute access request callback.
    419 **
    420 ** Returns          void.
    421 **
    422 *******************************************************************************/
    423 void gap_ble_s_attr_request_cback (UINT16 conn_id, UINT32 trans_id,
    424                                    tGATTS_REQ_TYPE type, tGATTS_DATA *p_data)
    425 {
    426     UINT8       status = GATT_INVALID_PDU;
    427     tGATTS_RSP  rsp_msg;
    428     BOOLEAN     ignore = FALSE;
    429 
    430     GAP_TRACE_EVENT1("gap_ble_s_attr_request_cback : recv type (0x%02x)", type);
    431 
    432     memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
    433 
    434     switch (type)
    435     {
    436         case GATTS_REQ_TYPE_READ:
    437             status = gap_proc_read(type, &p_data->read_req, &rsp_msg);
    438             break;
    439 
    440         case GATTS_REQ_TYPE_WRITE:
    441             if (!p_data->write_req.need_rsp)
    442                 ignore = TRUE;
    443 
    444             status = gap_proc_write_req(type, &p_data->write_req);
    445             break;
    446 
    447         case GATTS_REQ_TYPE_WRITE_EXEC:
    448             ignore = TRUE;
    449             GAP_TRACE_EVENT0("Ignore GATTS_REQ_TYPE_WRITE_EXEC"  );
    450             break;
    451 
    452         case GATTS_REQ_TYPE_MTU:
    453             GAP_TRACE_EVENT1("Get MTU exchange new mtu size: %d", p_data->mtu);
    454             ignore = TRUE;
    455             break;
    456 
    457         default:
    458             GAP_TRACE_EVENT1("Unknown/unexpected LE GAP ATT request: 0x%02x", type);
    459             break;
    460     }
    461 
    462     if (!ignore)
    463         GATTS_SendRsp (conn_id, trans_id, status, &rsp_msg);
    464 }
    465 
    466 /*******************************************************************************
    467 **
    468 ** Function         btm_ble_att_db_init
    469 **
    470 ** Description      GAP ATT database initalization.
    471 **
    472 ** Returns          void.
    473 **
    474 *******************************************************************************/
    475 void gap_attr_db_init(void)
    476 {
    477     tBT_UUID        app_uuid = {LEN_UUID_128,{0}};
    478     tBT_UUID        uuid     = {LEN_UUID_16,{UUID_SERVCLASS_GAP_SERVER}};
    479     UINT16          service_handle;
    480     tGAP_ATTR       *p_db_attr = &gap_cb.gatt_attr[0];
    481     tGATT_STATUS    status;
    482 
    483     /* Fill our internal UUID with a fixed pattern 0x82 */
    484     memset (&app_uuid.uu.uuid128, 0x82, LEN_UUID_128);
    485     memset(gap_cb.gatt_attr, 0, sizeof(tGAP_ATTR) *GAP_MAX_CHAR_NUM);
    486 
    487     gap_cb.gatt_if = GATT_Register(&app_uuid, &gap_cback);
    488 
    489     GATT_StartIf(gap_cb.gatt_if);
    490 
    491     /* Create a GAP service */
    492     service_handle = GATTS_CreateService (gap_cb.gatt_if, &uuid, 0, GAP_MAX_ATTR_NUM, TRUE);
    493 
    494     GAP_TRACE_EVENT1 ("gap_attr_db_init service_handle = %d", service_handle);
    495 
    496     /* add Device Name Characteristic
    497     */
    498     uuid.len = LEN_UUID_16;
    499     uuid.uu.uuid16 = p_db_attr->uuid = GATT_UUID_GAP_DEVICE_NAME;
    500     p_db_attr->handle = GATTS_AddCharacteristic(service_handle, &uuid, GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ);
    501     p_db_attr ++;
    502 
    503     /* add Icon characteristic
    504     */
    505     uuid.uu.uuid16   = p_db_attr->uuid = GATT_UUID_GAP_ICON;
    506     p_db_attr->handle = GATTS_AddCharacteristic(service_handle,
    507                                                 &uuid,
    508                                                 GATT_PERM_READ,
    509                                                 GATT_CHAR_PROP_BIT_READ);
    510     p_db_attr ++;
    511 
    512     /* start service now */
    513     memset (&app_uuid.uu.uuid128, 0x81, LEN_UUID_128);
    514 
    515     status = GATTS_StartService(gap_cb.gatt_if, service_handle, GAP_TRANSPORT_SUPPORTED );
    516 
    517     GAP_TRACE_EVENT3 ("GAP App gatt_if: %d  s_hdl = %d start_status=%d",
    518                       gap_cb.gatt_if, service_handle, status);
    519 
    520 
    521 
    522 }
    523 
    524 /*******************************************************************************
    525 **
    526 ** Function         GAP_BleAttrDBUpdate
    527 **
    528 ** Description      GAP ATT database update.
    529 **
    530 ** Returns          void.
    531 **
    532 *******************************************************************************/
    533 void GAP_BleAttrDBUpdate(UINT16 attr_uuid, tGAP_BLE_ATTR_VALUE *p_value)
    534 {
    535     tGAP_ATTR  *p_db_attr = gap_cb.gatt_attr;
    536     UINT8       i = 0;
    537 
    538     GAP_TRACE_EVENT1("GAP_BleAttrDBUpdate attr_uuid=0x%04x", attr_uuid);
    539 
    540     for (i = 0; i < GAP_MAX_CHAR_NUM; i ++, p_db_attr ++)
    541     {
    542         if (p_db_attr->uuid == attr_uuid)
    543         {
    544             GAP_TRACE_EVENT1("Found attr_uuid=0x%04x", attr_uuid);
    545 
    546             switch (attr_uuid)
    547             {
    548             case GATT_UUID_GAP_ICON:
    549                 p_db_attr->attr_value.icon  =  p_value->icon;
    550                 break;
    551 
    552             case GATT_UUID_GAP_PREF_CONN_PARAM:
    553                 memcpy((void *)&p_db_attr->attr_value.conn_param, (const void *)&p_value->conn_param, sizeof(tGAP_BLE_PREF_PARAM));
    554                 break;
    555 
    556             case GATT_UUID_GAP_DEVICE_NAME:
    557                 BTM_SetLocalDeviceName((char *)p_value->p_dev_name);
    558                 break;
    559 
    560             }
    561             break;
    562         }
    563     }
    564 
    565     return;
    566 }
    567 
    568 /*******************************************************************************
    569 **
    570 ** Function         gap_ble_cl_op_cmpl
    571 **
    572 ** Description      GAP client operation complete callback
    573 **
    574 ** Returns          void
    575 **
    576 *******************************************************************************/
    577 void gap_ble_cl_op_cmpl(tGAP_CLCB *p_clcb, BOOLEAN status, UINT16 len, UINT8 *p_name)
    578 {
    579     tGAP_BLE_DEV_NAME_CBACK *p_dev_name_cback = (tGAP_BLE_DEV_NAME_CBACK *)(p_clcb->p_cback);
    580     UINT16                  op = p_clcb->cl_op_uuid;
    581 
    582     GAP_TRACE_EVENT1("gap_ble_cl_op_cmpl status: %d", status);
    583 
    584     p_clcb->cl_op_uuid = 0;
    585     p_clcb->p_cback=NULL;
    586 
    587     if (p_dev_name_cback)
    588     {
    589         GAP_TRACE_EVENT0("calling gap_ble_cl_op_cmpl");
    590 
    591         if (op == GATT_UUID_GAP_DEVICE_NAME)
    592             (* p_dev_name_cback)(status, p_clcb->bda, len, (char *)p_name);
    593     }
    594 
    595     if (!gap_ble_process_pending_op(p_clcb) &&
    596         p_clcb->cl_op_uuid == 0)
    597         GATT_Disconnect(p_clcb->conn_id);
    598 
    599 }
    600 
    601 /*******************************************************************************
    602 **
    603 ** Function         gap_ble_c_connect_cback
    604 **
    605 ** Description      Client connection callback.
    606 **
    607 ** Returns          void
    608 **
    609 *******************************************************************************/
    610 static void gap_ble_c_connect_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
    611                                      BOOLEAN connected, tGATT_DISCONN_REASON reason)
    612 {
    613     tGAP_CLCB   *p_clcb = gap_find_clcb_by_bd_addr (bda);
    614     UINT16      cl_op_uuid;
    615 
    616     GAP_TRACE_EVENT5 ("gap_ble_c_connect_cback: from %08x%04x connected:%d conn_id=%d reason = 0x%04x",
    617                       (bda[0]<<24)+(bda[1]<<16)+(bda[2]<<8)+bda[3],
    618                       (bda[4]<<8)+bda[5], connected, conn_id, reason);
    619 
    620 
    621     if (connected)
    622     {
    623         if (p_clcb == NULL)
    624         {
    625             if ((p_clcb = gap_clcb_alloc(conn_id, bda))== NULL)
    626             {
    627                 GAP_TRACE_ERROR0 ("gap_ble_c_connect_cback: no_resource");
    628                 return;
    629             }
    630         }
    631         p_clcb->conn_id = conn_id;
    632         p_clcb->connected = TRUE;
    633 
    634         /* Do not use reconnection address for now -->
    635           check privacy enabled? set reconnect address
    636         btm_ble_update_reconnect_address(bda);*/
    637     }
    638     else
    639     {
    640         if (p_clcb != NULL)
    641             p_clcb->connected = FALSE;
    642     }
    643 
    644     if (p_clcb)
    645     {
    646         cl_op_uuid = p_clcb->cl_op_uuid;
    647 
    648         GAP_TRACE_EVENT1 ("cl_op_uuid=0x%04x", cl_op_uuid  );
    649 
    650         if (p_clcb->connected)
    651         {
    652             p_clcb->cl_op_uuid = 0;
    653             if (cl_op_uuid == GATT_UUID_GAP_DEVICE_NAME)
    654             {
    655                 GAP_BleReadPeerDevName (bda, (tGAP_BLE_DEV_NAME_CBACK *)p_clcb->p_cback);
    656             }
    657         }
    658         /* current link disconnect */
    659         else
    660         {
    661             gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
    662             memset(p_clcb, 0, sizeof(tGAP_CLCB));
    663         }
    664     }
    665 
    666 }
    667 
    668 /*******************************************************************************
    669 **
    670 ** Function         gap_ble_c_cmpl_cback
    671 **
    672 ** Description      Client operation complete callback.
    673 **
    674 ** Returns          void
    675 **
    676 *******************************************************************************/
    677 static void gap_ble_c_cmpl_cback (UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status, tGATT_CL_COMPLETE *p_data)
    678 
    679 {
    680     tGAP_CLCB   *p_clcb = gap_ble_find_clcb_by_conn_id(conn_id);
    681     UINT16      op_type;
    682     UINT16      min, max, latency, tout;
    683     UINT16      len;
    684     UINT8       *pp;
    685 
    686     if (p_clcb == NULL)
    687         return;
    688 
    689     op_type = p_clcb->cl_op_uuid;
    690 
    691     GAP_TRACE_EVENT3 ("gap_ble_c_cmpl_cback() - op_code: 0x%02x  status: 0x%02x  read_type: 0x%04x", op, status, op_type);
    692     /* Currently we only issue read commands */
    693     if (op != GATTC_OPTYPE_READ && op != GATTC_OPTYPE_WRITE)
    694         return;
    695 
    696     if (status != GATT_SUCCESS)
    697     {
    698         gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
    699         return;
    700     }
    701 
    702     pp = p_data->att_value.value;
    703 
    704     switch (op_type)
    705     {
    706         case GATT_UUID_GAP_PREF_CONN_PARAM:
    707             GAP_TRACE_EVENT0 ("GATT_UUID_GAP_PREF_CONN_PARAM");
    708             /* Extract the peripheral preferred connection parameters and save them */
    709 
    710             STREAM_TO_UINT16 (min, pp);
    711             STREAM_TO_UINT16 (max, pp);
    712             STREAM_TO_UINT16 (latency, pp);
    713             STREAM_TO_UINT16 (tout, pp);
    714 
    715             BTM_BleSetPrefConnParams (p_clcb->bda, min, max, latency, tout);
    716             /* release the connection here */
    717             gap_ble_cl_op_cmpl(p_clcb, TRUE, 0, NULL);
    718             break;
    719 
    720         case GATT_UUID_GAP_DEVICE_NAME:
    721             GAP_TRACE_EVENT0 ("GATT_UUID_GAP_DEVICE_NAME");
    722             len = (UINT16)strlen((char *)pp);
    723             if (len > GAP_CHAR_DEV_NAME_SIZE)
    724                 len = GAP_CHAR_DEV_NAME_SIZE;
    725             gap_ble_cl_op_cmpl(p_clcb, TRUE, len, pp);
    726             break;
    727         case GATT_UUID_GAP_ICON:
    728             break;
    729 
    730     }
    731 }
    732 
    733 /*******************************************************************************
    734 **
    735 ** Function         gap_ble_cl_read_request
    736 **
    737 ** Description      utility function to start a read request for a GAP charactersitic
    738 **
    739 ** Returns          TRUE if read started, else FALSE if GAP is busy
    740 **
    741 *******************************************************************************/
    742 BOOLEAN gap_ble_cl_read_request(tGAP_CLCB *p_clcb, UINT16 uuid, void * p_cback)
    743 {
    744     tGATT_READ_PARAM   param;
    745 
    746     memset(&param, 0, sizeof(tGATT_READ_PARAM));
    747 
    748     param.service.uuid.len       = LEN_UUID_16;
    749     param.service.uuid.uu.uuid16 = uuid;
    750     param.service.s_handle       = 1;
    751     param.service.e_handle       = 0xFFFF;
    752     param.service.auth_req       = 0;
    753 
    754     if (GATTC_Read(p_clcb->conn_id, GATT_READ_BY_TYPE, &param) != GATT_SUCCESS)
    755     {
    756         GAP_TRACE_ERROR0 ("GAP_BleReadPeerPrefConnParams: GATT_Read Failed");
    757         /* release the link here */
    758         GATT_Disconnect(p_clcb->conn_id);
    759         return(FALSE);
    760     }
    761     else
    762     {
    763         p_clcb->p_cback = p_cback;
    764         p_clcb->cl_op_uuid = uuid;
    765         return TRUE;
    766     }
    767 
    768 }
    769 
    770 /*******************************************************************************
    771 **
    772 ** Function         GAP_BleReadPeerPrefConnParams
    773 **
    774 ** Description      Start a process to read a connected peripheral's preferred
    775 **                  connection parameters
    776 **
    777 ** Returns          TRUE if read started, else FALSE if GAP is busy
    778 **
    779 *******************************************************************************/
    780 BOOLEAN GAP_BleReadPeerPrefConnParams (BD_ADDR peer_bda)
    781 {
    782 
    783     tGAP_CLCB   *p_clcb = gap_find_clcb_by_bd_addr (peer_bda);
    784 
    785     if (p_clcb == NULL)
    786     {
    787         if ((p_clcb = gap_clcb_alloc(0, peer_bda)) == NULL)
    788         {
    789             GAP_TRACE_ERROR0("GAP_BleReadPeerPrefConnParams max connection reached");
    790             return FALSE;
    791         }
    792         p_clcb->connected = FALSE;
    793     }
    794 
    795     GAP_TRACE_API3 ("GAP_BleReadPeerPrefConnParams() - BDA: %08x%04x  cl_op_uuid: 0x%04x",
    796                     (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3],
    797                     (peer_bda[4]<<8)+peer_bda[5], p_clcb->cl_op_uuid);
    798 
    799     /* For now we only handle one at a time */
    800     if (p_clcb->cl_op_uuid != 0)
    801         return(FALSE);
    802 
    803     /* hold the link here */
    804     GATT_Connect(gap_cb.gatt_if, p_clcb->bda, TRUE);
    805 
    806     if (p_clcb->connected)
    807     {
    808     return gap_ble_cl_read_request(p_clcb, GATT_UUID_GAP_PREF_CONN_PARAM, NULL);
    809     }
    810     /* Mark currently active operation */
    811     p_clcb->cl_op_uuid = GATT_UUID_GAP_PREF_CONN_PARAM;
    812 
    813     return(TRUE);
    814 
    815 
    816 }
    817 
    818 /*******************************************************************************
    819 **
    820 ** Function         GAP_BleReadPeerDevName
    821 **
    822 ** Description      Start a process to read a connected peripheral's device name.
    823 **
    824 ** Returns          TRUE if request accepted
    825 **
    826 *******************************************************************************/
    827 BOOLEAN GAP_BleReadPeerDevName (BD_ADDR peer_bda, tGAP_BLE_DEV_NAME_CBACK *p_cback)
    828 {
    829     tGAP_CLCB   *p_clcb = NULL;
    830 
    831     if (p_cback == NULL)
    832         return(FALSE);
    833 
    834     if ((p_clcb = gap_find_clcb_by_bd_addr (peer_bda)) == NULL)
    835     {
    836         if ((p_clcb = gap_clcb_alloc(0, peer_bda)) == NULL)
    837     {
    838         GAP_TRACE_ERROR0("GAP_BleReadPeerDevName max connection reached");
    839             return FALSE;
    840     }
    841         p_clcb->connected = FALSE;
    842     }
    843 
    844     GAP_TRACE_EVENT3 ("GAP_BleReadPeerDevName() - BDA: %08x%04x  cl_op_uuid: 0x%04x",
    845                       (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3],
    846                       (peer_bda[4]<<8)+peer_bda[5], p_clcb->cl_op_uuid);
    847 
    848     /* For now we only handle one at a time */
    849     if (p_clcb->cl_op_uuid != 0)
    850         return(FALSE);
    851 
    852     /* hold the link here */
    853     GATT_Connect(gap_cb.gatt_if, p_clcb->bda, TRUE);
    854 
    855     if (p_clcb->connected)
    856     {
    857         return gap_ble_cl_read_request(p_clcb, GATT_UUID_GAP_DEVICE_NAME, (void *)p_cback);
    858     }
    859 
    860     p_clcb->p_cback = (void *)p_cback;
    861     /* Mark currently active operation */
    862     p_clcb->cl_op_uuid = GATT_UUID_GAP_DEVICE_NAME;
    863 
    864 
    865     return(TRUE);
    866 }
    867 
    868 /*******************************************************************************
    869 **
    870 ** Function         GAP_BleCancelReadPeerDevName
    871 **
    872 ** Description      Cancel reading a peripheral's device name.
    873 **
    874 ** Returns          TRUE if request accepted
    875 **
    876 *******************************************************************************/
    877 BOOLEAN GAP_BleCancelReadPeerDevName (BD_ADDR peer_bda)
    878 {
    879     tGAP_CLCB *p_clcb = gap_find_clcb_by_bd_addr (peer_bda);
    880 
    881     GAP_TRACE_EVENT3 ("GAP_BleCancelReadPeerDevName() - BDA: %08x%04x  cl_op_uuid: 0x%04x",
    882                       (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3],
    883                       (peer_bda[4]<<8)+peer_bda[5], (p_clcb == NULL)? 0 : p_clcb->cl_op_uuid);
    884 
    885     if (p_clcb == NULL || p_clcb->cl_op_uuid != GATT_UUID_GAP_DEVICE_NAME)
    886     {
    887         GAP_TRACE_ERROR0 ("Cannot cancel current op is not get dev name");
    888         return FALSE;
    889     }
    890 
    891     if (!p_clcb->connected)
    892     {
    893         if (!GATT_CancelConnect(gap_cb.gatt_if, peer_bda, TRUE))
    894         {
    895             GAP_TRACE_ERROR0 ("Cannot cancel where No connection id");
    896             return FALSE;
    897         }
    898     }
    899 
    900     gap_ble_cl_op_cmpl(p_clcb, FALSE, 0, NULL);
    901 
    902     return(TRUE);
    903 }
    904 
    905 /*******************************************************************************
    906 **
    907 ** Function         GAP_BleUpdateReconnectAddr
    908 **
    909 ** Description      Start a process to udpate the reconnect address if remote devive
    910 **                  has privacy enabled.
    911 **
    912 ** Returns          TRUE if read started, else FALSE if GAP is busy
    913 **
    914 *******************************************************************************/
    915 BOOLEAN GAP_BleUpdateReconnectAddr (BD_ADDR peer_bda, BD_ADDR reconn_addr,
    916                                     tGAP_BLE_RECONN_ADDR_CBACK *p_cback)
    917 {
    918     tGAP_CLCB         *p_clcb;
    919     tGATT_DISC_PARAM   param;
    920 
    921     if (p_cback == NULL)
    922         return(FALSE);
    923 
    924     /* This function should only be called if there is a connection to  */
    925     /* the peer. Get a client handle for that connection.               */
    926     if ((p_clcb = gap_find_clcb_by_bd_addr (peer_bda)) == NULL ||
    927         !p_clcb->connected)
    928     {
    929         GAP_TRACE_ERROR0("No connection, can not update reconnect address");
    930         return(FALSE);
    931     }
    932 
    933     GAP_TRACE_API3 ("GAP_BleUpdateReconnectAddr() - BDA: %08x%04x  cl_op_uuid: 0x%04x",
    934                     (peer_bda[0]<<24)+(peer_bda[1]<<16)+(peer_bda[2]<<8)+peer_bda[3],
    935                     (peer_bda[4]<<8)+peer_bda[5], p_clcb->cl_op_uuid);
    936 
    937     /* For now we only handle one at a time */
    938     if (p_clcb->cl_op_uuid != 0)
    939     {
    940         gap_ble_enqueue_op(p_clcb, GATT_UPDATE_RECONN_ADDR, reconn_addr, 0, (void *)p_cback);
    941         return(FALSE);
    942     }
    943 
    944     /* hold the link here */
    945     GATT_Connect(gap_cb.gatt_if, p_clcb->bda, TRUE);
    946 
    947     memset(&param, 0, sizeof(tGATT_DISC_PARAM));
    948 
    949     param.service.len       = LEN_UUID_16;
    950     param.service.uu.uuid16 = GATT_UUID_GAP_RECONN_ADDR;
    951     param.s_handle          = 1;
    952     param.e_handle          = 0xFFFF;
    953 
    954     if (GATTC_Discover(p_clcb->conn_id, GATT_DISC_CHAR, &param) != GATT_SUCCESS)
    955     {
    956         GAP_TRACE_ERROR0 ("GAP_BleReadPeerPrefConnParams: GATT_Read Failed");
    957         /* release the link here */
    958         GATT_Disconnect(p_clcb->conn_id);
    959         return(FALSE);
    960     }
    961     else
    962     {
    963         p_clcb->p_cback     = (void *)p_cback;
    964         memcpy(p_clcb->reconn_addr, reconn_addr, BD_ADDR_LEN);
    965         p_clcb->cl_op_uuid  = GATT_UUID_GAP_RECONN_ADDR;
    966     }
    967 
    968     return TRUE;
    969 
    970 }
    971 
    972 #endif  /* BLE_INCLUDED */
    973 
    974 
    975 
    976 
    977 
    978