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