LE: Add peripheral role support (2/4)

Initial stack support for the LE peripheral role.

Change-Id: I261d751b43b7020760bff345b472b6f60caa60be
diff --git a/bta/dm/bta_dm_act.c b/bta/dm/bta_dm_act.c
index 68bae03..b3e4e72 100644
--- a/bta/dm/bta_dm_act.c
+++ b/bta/dm/bta_dm_act.c
@@ -4960,6 +4960,37 @@
         BTM_BleObserve(FALSE, 0, NULL,NULL );
     }
 }
+/*******************************************************************************
+**
+** Function         bta_dm_ble_set_scan_params
+**
+** Description      This function set the adv parameters.
+**
+** Parameters:
+**
+*******************************************************************************/
+void bta_dm_ble_set_adv_params (tBTA_DM_MSG *p_data)
+{
+    BTM_BleSetAdvParams(p_data->ble_set_adv_params.adv_int_min,
+                        p_data->ble_set_adv_params.adv_int_max,
+                        p_data->ble_set_adv_params.p_dir_bda,
+                        BTA_DM_BLE_ADV_CHNL_MAP);
+}
+/*******************************************************************************
+**
+** Function         bta_dm_ble_set_adv_config
+**
+** Description      This function set the customized ADV data configuration
+**
+** Parameters:
+**
+*******************************************************************************/
+void bta_dm_ble_set_adv_config (tBTA_DM_MSG *p_data)
+{
+    BTM_BleWriteAdvData(p_data->ble_set_adv_data.data_mask,
+                        (tBTM_BLE_ADV_DATA *)p_data->ble_set_adv_data.p_adv_cfg);
+}
+
 
 #if ((defined BTA_GATT_INCLUDED) &&  (BTA_GATT_INCLUDED == TRUE))
 #ifndef BTA_DM_GATT_CLOSE_DELAY_TOUT
diff --git a/bta/dm/bta_dm_api.c b/bta/dm/bta_dm_api.c
index a81618d..941213a 100644
--- a/bta/dm/bta_dm_api.c
+++ b/bta/dm/bta_dm_api.c
@@ -1447,6 +1447,72 @@
 
 /*******************************************************************************
 **
+** Function         BTA_DmSetBleAdvParams
+**
+** Description      This function sets the advertising parameters BLE functionality.
+**                  It is to be called when device act in peripheral or broadcaster
+**                  role.
+**
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_DmSetBleAdvParams (UINT16 adv_int_min, UINT16 adv_int_max,
+                           tBLE_BD_ADDR *p_dir_bda)
+{
+#if BLE_INCLUDED == TRUE
+    tBTA_DM_API_BLE_ADV_PARAMS    *p_msg;
+
+    APPL_TRACE_API2 ("BTA_DmSetBleAdvParam: %d, %d", adv_int_min, adv_int_max);
+
+    if ((p_msg = (tBTA_DM_API_BLE_ADV_PARAMS *) GKI_getbuf(sizeof(tBTA_DM_API_BLE_ADV_PARAMS))) != NULL)
+    {
+        memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_ADV_PARAMS));
+
+        p_msg->hdr.event = BTA_DM_API_BLE_ADV_PARAM_EVT;
+
+        p_msg->adv_int_min      = adv_int_min;
+        p_msg->adv_int_max      = adv_int_max;
+
+        if (p_dir_bda != NULL)
+        {
+            p_msg->p_dir_bda = (tBLE_BD_ADDR *)(p_msg + 1);
+            memcpy(p_msg->p_dir_bda, p_dir_bda, sizeof(tBLE_BD_ADDR));
+        }
+
+        bta_sys_sendmsg(p_msg);
+    }
+#endif
+}
+
+#if BLE_INCLUDED == TRUE
+/*******************************************************************************
+**
+** Function         BTA_DmBleSetAdvConfig
+**
+** Description      This function is called to override the BTA default ADV parameters.
+**
+** Parameters       Pointer to User defined ADV data structure
+**
+** Returns          None
+**
+*******************************************************************************/
+void BTA_DmBleSetAdvConfig (tBTA_BLE_AD_MASK data_mask, tBTA_BLE_ADV_DATA *p_adv_cfg)
+{
+    tBTA_DM_API_SET_ADV_CONFIG  *p_msg;
+
+    if ((p_msg = (tBTA_DM_API_SET_ADV_CONFIG *) GKI_getbuf(sizeof(tBTA_DM_API_SET_ADV_CONFIG))) != NULL)
+    {
+        p_msg->hdr.event = BTA_DM_API_BLE_SET_ADV_CONFIG_EVT;
+		p_msg->data_mask = data_mask;
+        p_msg->p_adv_cfg = p_adv_cfg;
+
+        bta_sys_sendmsg(p_msg);
+    }
+}
+#endif
+/*******************************************************************************
+**
 ** Function         BTA_DmBleSetBgConnType
 **
 ** Description      This function is called to set BLE connectable mode for a
diff --git a/bta/dm/bta_dm_int.h b/bta/dm/bta_dm_int.h
index 9b3069d..d87dbf3 100644
--- a/bta/dm/bta_dm_int.h
+++ b/bta/dm/bta_dm_int.h
@@ -99,6 +99,8 @@
     BTA_DM_API_BLE_CONN_PARAM_EVT,
     BTA_DM_API_BLE_SCAN_PARAM_EVT,
     BTA_DM_API_BLE_OBSERVE_EVT,
+    BTA_DM_API_BLE_ADV_PARAM_EVT,
+    BTA_DM_API_BLE_SET_ADV_CONFIG_EVT,
 #endif
 
 #if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
@@ -862,9 +864,6 @@
 extern const tBTA_DM_CFG bta_dm_cfg;
 
 
-
-#define BTA_ALL_APP_ID 0xff
-
 typedef struct
 {
     UINT8   id;
@@ -995,6 +994,9 @@
 extern void bta_dm_ble_set_scan_params (tBTA_DM_MSG *p_data);
 extern void bta_dm_close_gatt_conn(tBTA_DM_MSG *p_data);
 extern void bta_dm_ble_observe (tBTA_DM_MSG *p_data);
+extern void bta_dm_ble_set_adv_params (tBTA_DM_MSG *p_data);
+extern void bta_dm_ble_set_adv_config (tBTA_DM_MSG *p_data);
+
 #endif
 extern void bta_dm_set_encryption(tBTA_DM_MSG *p_data);
 extern void bta_dm_confirm(tBTA_DM_MSG *p_data);
diff --git a/bta/dm/bta_dm_main.c b/bta/dm/bta_dm_main.c
index c68f315..50f871f 100644
--- a/bta/dm/bta_dm_main.c
+++ b/bta/dm/bta_dm_main.c
@@ -96,6 +96,8 @@
     bta_dm_ble_set_conn_params,      /* BTA_DM_API_BLE_CONN_PARAM_EVT */
     bta_dm_ble_set_scan_params,      /* BTA_DM_API_BLE_SCAN_PARAM_EVT */
     bta_dm_ble_observe,
+    bta_dm_ble_set_adv_params,     /* BTA_DM_API_BLE_SCAN_PARAM_EVT */
+    bta_dm_ble_set_adv_config,     /* BTA_DM_API_BLE_SET_ADV_CONFIG_EVT */
 #endif
 
 #if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
diff --git a/bta/gatt/bta_gattc_act.c b/bta/gatt/bta_gattc_act.c
index 211ec34..8ab7992 100644
--- a/bta/gatt/bta_gattc_act.c
+++ b/bta/gatt/bta_gattc_act.c
@@ -1948,6 +1948,99 @@
         APPL_TRACE_ERROR0("No resources");
     }
 }
+/*******************************************************************************
+**
+** Function         bta_gattc_process_listen_all
+**
+** Description      process listen all, send open callback to application for all
+**                  connected slave LE link.
+**
+** Returns          void
+**
+********************************************************************************/
+void bta_gattc_process_listen_all(UINT8 cif)
+{
+    UINT8               i_conn = 0;
+    tBTA_GATTC_CONN     *p_conn = &bta_gattc_cb.conn_track[0];
 
-#endif /* #if BLE_INCLUDED == TRUE */
-#endif /* BTA_GATT_INCLUDED */
+    for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
+    {
+        if (p_conn->in_use )
+        {
+            if (bta_gattc_find_clcb_by_cif(cif, p_conn->remote_bda) == NULL)
+            {
+                bta_gattc_init_clcb_conn(cif, p_conn->remote_bda);
+            }
+            /* else already connected */
+        }
+    }
+}
+/*******************************************************************************
+**
+** Function         bta_gattc_listen
+**
+** Description      Start or stop a listen for connection
+**
+** Returns          void
+**
+********************************************************************************/
+void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
+{
+    tBTA_GATTC_RCB      *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
+    tBTA_GATTC          cb_data;
+    cb_data.reg_oper.status = BTA_GATT_ERROR;
+    cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
+
+    if (p_clreg == NULL)
+    {
+        APPL_TRACE_ERROR1("bta_gattc_listen failed, unknown client_if: %d",
+                            p_msg->api_listen.client_if);
+        return;
+    }
+    /* mark bg conn record */
+    if (bta_gattc_mark_bg_conn(p_msg->api_listen.client_if,
+                               (BD_ADDR_PTR) p_msg->api_listen.remote_bda,
+                               p_msg->api_listen.start,
+                               TRUE))
+    {
+        if (!GATT_Listen(p_msg->api_listen.client_if,
+                         p_msg->api_listen.start,
+                         p_msg->api_listen.remote_bda))
+        {
+            APPL_TRACE_ERROR0("Listen failure");
+            (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
+        }
+        else
+        {
+            cb_data.status = BTA_GATT_OK;
+
+            (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
+
+            if (p_msg->api_listen.start)
+            {
+                /* if listen to a specific target */
+                if (p_msg->api_listen.remote_bda != NULL)
+                {
+
+                    /* if is a connected remote device */
+                    if (L2CA_GetBleConnRole(p_msg->api_listen.remote_bda) == HCI_ROLE_SLAVE &&
+                        bta_gattc_find_clcb_by_cif(p_msg->api_listen.client_if, p_msg->api_listen.remote_bda) == NULL)
+                    {
+
+                        bta_gattc_init_clcb_conn(p_msg->api_listen.client_if,
+                                                p_msg->api_listen.remote_bda);
+                    }
+                }
+                /* if listen to all */
+                else
+                {
+                    APPL_TRACE_ERROR0("Listen For All now");
+                    /* go through all connected device and send callback for all connected slave connection */
+                    bta_gattc_process_listen_all(p_msg->api_listen.client_if);
+                }
+            }
+        }
+    }
+}
+#endif
+#endif
diff --git a/bta/gatt/bta_gattc_api.c b/bta/gatt/bta_gattc_api.c
index a57fa90..fff1a55 100644
--- a/bta/gatt/bta_gattc_api.c
+++ b/bta/gatt/bta_gattc_api.c
@@ -1017,5 +1017,44 @@
     }
     return;
 }
+
+/*******************************************************************************
+**
+** Function         BTA_GATTC_Listen
+**
+** Description      Start advertisement to listen for connection request for a GATT
+**                  client application.
+**
+** Parameters       client_if: server interface.
+**                  start: to start or stop listening for connection
+**                  remote_bda: remote device BD address, if listen to all device
+**                              use NULL.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_GATTC_Listen(tBTA_GATTC_IF client_if, BOOLEAN start, BD_ADDR_PTR target_bda)
+{
+    tBTA_GATTC_API_LISTEN  *p_buf;
+
+    if ((p_buf = (tBTA_GATTC_API_LISTEN *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN))) != NULL)
+    {
+        p_buf->hdr.event = BTA_GATTC_API_LISTEN_EVT;
+
+        p_buf->client_if = client_if;
+        p_buf->start = start;
+        if (target_bda)
+        {
+            p_buf->remote_bda = (UINT8*)(p_buf + 1);
+            memcpy(p_buf->remote_bda, target_bda, BD_ADDR_LEN);
+        }
+        else
+            p_buf->remote_bda = NULL;
+
+        bta_sys_sendmsg(p_buf);
+    }
+    return;
+}
+
 #endif /* BTA_GATT_INCLUDED */
 
diff --git a/bta/gatt/bta_gattc_int.h b/bta/gatt/bta_gattc_int.h
index 7730113..87210fc 100644
--- a/bta/gatt/bta_gattc_int.h
+++ b/bta/gatt/bta_gattc_int.h
@@ -68,8 +68,8 @@
     BTA_GATTC_INT_START_IF_EVT,
     BTA_GATTC_API_REG_EVT,
     BTA_GATTC_API_DEREG_EVT,
+    BTA_GATTC_API_LISTEN_EVT,
     BTA_GATTC_API_DISABLE_EVT
-
 };
 typedef UINT16 tBTA_GATTC_INT_EVT;
 
@@ -181,6 +181,14 @@
 typedef struct
 {
     BT_HDR                  hdr;
+    BD_ADDR_PTR             remote_bda;
+    tBTA_GATTC_IF           client_if;
+    BOOLEAN                 start;
+} tBTA_GATTC_API_LISTEN;
+
+typedef struct
+{
+    BT_HDR                  hdr;
     BD_ADDR                 remote_bda;
     tBTA_GATTC_IF           client_if;
     UINT8                   role;
@@ -208,6 +216,8 @@
 
     tBTA_GATTC_INT_START_IF     int_start_if;
     tBTA_GATTC_INT_DEREG        int_dereg;
+    /* if peripheral role is supported */
+    tBTA_GATTC_API_LISTEN       api_listen;
 
 } tBTA_GATTC_DATA;
 
@@ -462,7 +472,9 @@
 extern void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status,
                                        BD_ADDR remote_bda, UINT16 conn_id);
 extern void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
-
+#if BLE_INCLUDED == TRUE
+extern void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
+#endif
 /* utility functions */
 extern tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_cif (UINT8 client_if, BD_ADDR remote_bda);
 extern tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_conn_id (UINT16 conn_id);
diff --git a/bta/gatt/bta_gattc_main.c b/bta/gatt/bta_gattc_main.c
index 0479546..9427f89 100644
--- a/bta/gatt/bta_gattc_main.c
+++ b/bta/gatt/bta_gattc_main.c
@@ -381,6 +381,11 @@
             bta_gattc_process_api_refresh(p_cb, (tBTA_GATTC_DATA *) p_msg);
             break;
 
+#if BLE_INCLUDED == TRUE
+        case BTA_GATTC_API_LISTEN_EVT:
+            bta_gattc_listen(p_cb, (tBTA_GATTC_DATA *) p_msg);
+            break;
+#endif
         default:
             if (p_msg->event == BTA_GATTC_INT_CONN_EVT)
                 p_clcb = bta_gattc_find_int_conn_clcb((tBTA_GATTC_DATA *) p_msg);
@@ -472,6 +477,8 @@
             return "BTA_GATTC_API_DEREG_EVT";
         case BTA_GATTC_API_REFRESH_EVT:
             return "BTA_GATTC_API_REFRESH_EVT";
+        case BTA_GATTC_API_LISTEN_EVT:
+            return "BTA_GATTC_API_LISTEN_EVT";
         case BTA_GATTC_API_DISABLE_EVT:
             return "BTA_GATTC_API_DISABLE_EVT";
         default:
diff --git a/bta/gatt/bta_gatts_act.c b/bta/gatt/bta_gatts_act.c
index dd7e1fc..f59b593 100644
--- a/bta/gatt/bta_gatts_act.c
+++ b/bta/gatt/bta_gatts_act.c
@@ -781,6 +781,39 @@
     }
 
 }
+/*******************************************************************************
+**
+** Function         bta_gatts_listen
+**
+** Description      Start or stop listening for LE connection on a GATT server
+**
+** Returns          none.
+**
+*******************************************************************************/
+void bta_gatts_listen(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA * p_msg)
+{
+    tBTA_GATTS_RCB     *p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_listen.server_if);
+    tBTA_GATTS          cb_data;
+    cb_data.reg_oper.status = BTA_GATT_OK;
+    cb_data.reg_oper.server_if = p_msg->api_listen.server_if;
+
+    if (p_rcb == NULL)
+    {
+        APPL_TRACE_ERROR0("Unknown GATTS application");
+        return;
+    }
+
+    if (!GATT_Listen(p_msg->api_listen.server_if,
+                     p_msg->api_listen.start,
+                     p_msg->api_listen.remote_bda))
+    {
+        cb_data.status = BTA_GATT_ERROR;
+        APPL_TRACE_ERROR0("bta_gatts_listen Listen failed");
+    }
+
+    if (p_rcb->p_cback)
+        (*p_rcb->p_cback)(BTA_GATTS_LISTEN_EVT, &cb_data);
+}
 
 /*******************************************************************************
 **
diff --git a/bta/gatt/bta_gatts_api.c b/bta/gatt/bta_gatts_api.c
index 7d5d541..ad30d73 100644
--- a/bta/gatt/bta_gatts_api.c
+++ b/bta/gatt/bta_gatts_api.c
@@ -538,5 +538,43 @@
     return;
 
 }
+/*******************************************************************************
+**
+** Function         BTA_GATTS_Listen
+**
+** Description      Start advertisement to listen for connection request for a
+**                  GATT server
+**
+** Parameters       server_if: server interface.
+**                  start: to start or stop listening for connection
+**                  remote_bda: remote device BD address, if listen to all device
+**                              use NULL.
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_GATTS_Listen(tBTA_GATTS_IF server_if, BOOLEAN start, BD_ADDR_PTR target_bda)
+{
+    tBTA_GATTS_API_LISTEN  *p_buf;
+
+    if ((p_buf = (tBTA_GATTS_API_LISTEN *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTS_API_LISTEN) + BD_ADDR_LEN))) != NULL)
+    {
+        p_buf->hdr.event = BTA_GATTS_API_LISTEN_EVT;
+
+        p_buf->server_if    = server_if;
+        p_buf->start        = start;
+
+        if (target_bda)
+        {
+            p_buf->remote_bda = (UINT8*)(p_buf + 1);
+            memcpy(p_buf->remote_bda, target_bda, BD_ADDR_LEN);
+        }
+        else
+            p_buf->remote_bda = NULL;
+
+        bta_sys_sendmsg(p_buf);
+    }
+    return;
+}
 
 #endif /* BTA_GATT_INCLUDED */
diff --git a/bta/gatt/bta_gatts_main.c b/bta/gatt/bta_gatts_main.c
index f763766..02810fc 100644
--- a/bta/gatt/bta_gatts_main.c
+++ b/bta/gatt/bta_gatts_main.c
@@ -107,6 +107,11 @@
             bta_gatts_send_rsp(p_cb,(tBTA_GATTS_DATA *) p_msg);
             break;
 
+        case BTA_GATTS_API_LISTEN_EVT:
+            bta_gatts_listen(p_cb,(tBTA_GATTS_DATA *) p_msg);
+            break;
+
+
         case BTA_GATTS_API_ADD_INCL_SRVC_EVT:
         case BTA_GATTS_API_ADD_CHAR_EVT:
         case BTA_GATTS_API_ADD_DESCR_EVT:
diff --git a/bta/include/bta_api.h b/bta/include/bta_api.h
index cf6c065..dd1d298 100644
--- a/bta/include/bta_api.h
+++ b/bta/include/bta_api.h
@@ -1767,6 +1767,23 @@
 
 /*******************************************************************************
 **
+** Function         BTA_DmSetBleAdvParams
+**
+** Description      This function sets the advertising parameters BLE functionality.
+**                  It is to be called when device act in peripheral or broadcaster
+**                  role.
+**
+** Parameters:      adv_int_min    - adv interval minimum
+**                  adv_int_max    - adv interval max
+**                  p_dir_bda      - directed adv initator address
+**
+** Returns          void
+**
+*******************************************************************************/
+BTA_API extern void BTA_DmSetBleAdvParams (UINT16 adv_int_min, UINT16 adv_int_max,
+                                           tBLE_BD_ADDR *p_dir_bda);
+/*******************************************************************************
+**
 ** Function         BTA_DmSearchExt
 **
 ** Description      This function searches for peer Bluetooth devices. It performs
diff --git a/bta/include/bta_gatt_api.h b/bta/include/bta_gatt_api.h
index f052323..93eadd5 100644
--- a/bta/include/bta_gatt_api.h
+++ b/bta/include/bta_gatt_api.h
@@ -114,6 +114,7 @@
 #define BTA_GATTC_ACL_EVT           13  /* ACL up event */
 #define BTA_GATTC_CANCEL_OPEN_EVT   14  /* cancel open event */
 #define BTA_GATTC_SRVC_CHG_EVT      15  /* service change event */
+#define BTA_GATTC_LISTEN_EVT        16  /* listen event */
 
 typedef UINT8 tBTA_GATTC_EVT;
 
@@ -392,6 +393,7 @@
 #define BTA_GATTS_OPEN_EVT                              16
 #define BTA_GATTS_CANCEL_OPEN_EVT                       17
 #define BTA_GATTS_CLOSE_EVT                             18
+#define BTA_GATTS_LISTEN_EVT                            19
 
 typedef UINT8  tBTA_GATTS_EVT;
 typedef tGATT_IF tBTA_GATTS_IF;
@@ -1005,6 +1007,22 @@
 
 
 /*******************************************************************************
+**
+** Function         BTA_GATTC_Listen
+**
+** Description      Start advertisement to listen for connection request.
+**
+** Parameters       client_if: server interface.
+**                  start: to start or stop listening for connection
+**                  remote_bda: remote device BD address, if listen to all device
+**                              use NULL.
+**
+** Returns          void
+**
+*******************************************************************************/
+BTA_API extern void BTA_GATTC_Listen(tBTA_GATTC_IF client_if, BOOLEAN start, BD_ADDR_PTR target_bda);
+
+/*******************************************************************************
 **  BTA GATT Server API
 ********************************************************************************/
 
diff --git a/btif/src/btif_dm.c b/btif/src/btif_dm.c
index 30949e2..21512b5 100644
--- a/btif/src/btif_dm.c
+++ b/btif/src/btif_dm.c
@@ -961,6 +961,7 @@
             {
                 bt_property_t properties[5];
                 bt_device_type_t dev_type;
+                UINT8 addr_type;
                 uint32_t num_properties = 0;
                 bt_status_t status;
 
@@ -971,7 +972,8 @@
                 num_properties++;
                 /* BD_NAME */
                 /* Don't send BDNAME if it is empty */
-                if (bdname.name[0]) {
+                if (bdname.name[0])
+                {
                     BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
                                                BT_PROPERTY_BDNAME,
                                                strlen((char *)bdname.name), &bdname);
@@ -983,9 +985,10 @@
                                     BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
                 num_properties++;
                 /* DEV_TYPE */
-#if (BLE_INCLUDED == TRUE)
+#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
                 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
                 dev_type = p_search_data->inq_res.device_type;
+                addr_type = p_search_data->inq_res.ble_addr_type;
 #else
                 dev_type = BT_DEVICE_TYPE_BREDR;
 #endif
@@ -1000,7 +1003,10 @@
 
                 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
                 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
-
+#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
+                status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
+                ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
+#endif
                 /* Callback to notify upper layer of device */
                 HAL_CBACK(bt_hal_cbacks, device_found_cb,
                                  num_properties, properties);
diff --git a/btif/src/btif_gatt_client.c b/btif/src/btif_gatt_client.c
index 10fc2dc..ad33c51 100644
--- a/btif/src/btif_gatt_client.c
+++ b/btif/src/btif_gatt_client.c
@@ -55,6 +55,8 @@
 **  Constants & Macros
 ********************************************************************************/
 
+#define ADV_FLAGS 0x02
+
 #define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\
     {\
         ALOGW("%s: BTGATT not initialized", __FUNCTION__);\
@@ -86,7 +88,9 @@
     BTIF_GATTC_REG_FOR_NOTIFICATION,
     BTIF_GATTC_DEREG_FOR_NOTIFICATION,
     BTIF_GATTC_REFRESH,
-    BTIF_GATTC_READ_RSSI
+    BTIF_GATTC_READ_RSSI,
+    BTIF_GATTC_LISTEN,
+    BTIF_GATTC_SET_ADV_DATA
 } btif_gattc_event_t;
 
 #define BTIF_GATT_MAX_OBSERVED_DEV 40
@@ -100,7 +104,14 @@
 
 typedef struct
 {
+    tBTM_BLE_AD_MASK mask;
+    tBTM_BLE_ADV_DATA data;
+} btgatt_adv_data;
+
+typedef struct
+{
     uint8_t     value[BTGATT_MAX_ATTR_LEN];
+    btgatt_adv_data adv_data;
     bt_bdaddr_t bd_addr;
     btgatt_srvc_id_t srvc_id;
     btgatt_srvc_id_t incl_srvc_id;
@@ -117,6 +128,7 @@
     uint8_t     write_type;
     uint8_t     status;
     uint8_t     addr_type;
+    uint8_t     start;
     int8_t      rssi;
     tBT_DEVICE_TYPE device_type;
 } __attribute__((packed)) btif_gattc_cb_t;
@@ -469,6 +481,14 @@
             break;
         }
 
+        case BTA_GATTC_LISTEN_EVT:
+        {
+            HAL_CBACK(bt_gatt_callbacks, client->listen_cb
+                , p_data->reg_oper.status
+                , p_data->reg_oper.client_if
+            );
+            break;
+        }
         default:
             ALOGE("%s: Unhandled event (%d)!", __FUNCTION__, event);
             break;
@@ -786,6 +806,21 @@
             BTM_ReadRSSI (p_cb->bd_addr.address, (tBTM_CMPL_CB *)btm_read_rssi_cb);
             break;
 
+        case BTIF_GATTC_LISTEN:
+            BTA_GATTC_Listen(p_cb->client_if, p_cb->start, NULL);
+            break;
+
+        case BTIF_GATTC_SET_ADV_DATA:
+        {
+            if (p_cb->start == 0)
+                BTM_BleWriteAdvData(p_cb->adv_data.mask, &p_cb->adv_data.data);
+            else
+                BTM_BleWriteScanRsp(p_cb->adv_data.mask, &p_cb->adv_data.data);
+            if (p_cb->adv_data.data.manu.p_val != NULL)
+                GKI_freebuf(p_cb->adv_data.data.manu.p_val);
+            break;
+        }
+
         default:
             ALOGE("%s: Unknown event (%d)!", __FUNCTION__, event);
             break;
@@ -845,6 +880,68 @@
                                  (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
 }
 
+static bt_status_t btif_gattc_listen(int client_if, bool start)
+{
+    CHECK_BTGATT_INIT();
+    btif_gattc_cb_t btif_cb;
+    btif_cb.client_if = (uint8_t) client_if;
+    btif_cb.start = start ? 1 : 0;
+    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_LISTEN,
+                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
+}
+
+static bt_status_t btif_gattc_set_adv_data(int client_if, bool set_scan_rsp, bool include_name,
+                bool include_txpower, int min_interval, int max_interval, int appearance,
+                uint16_t manufacturer_len, char* manufacturer_data)
+{
+    CHECK_BTGATT_INIT();
+    btif_gattc_cb_t btif_cb;
+    memset(&btif_cb, 0, sizeof(btif_gattc_cb_t));
+    memset(&btif_cb.adv_data, 0, sizeof(btgatt_adv_data));
+
+    btif_cb.client_if = (uint8_t) client_if;
+    btif_cb.start = set_scan_rsp ? 1 : 0;
+
+    if (!set_scan_rsp)
+    {
+        btif_cb.adv_data.mask = BTM_BLE_AD_BIT_FLAGS;
+        btif_cb.adv_data.data.flag = ADV_FLAGS;
+    }
+
+    if (include_name)
+        btif_cb.adv_data.mask |= BTM_BLE_AD_BIT_DEV_NAME;
+
+    if (include_txpower)
+        btif_cb.adv_data.mask |= BTM_BLE_AD_BIT_TX_PWR;
+
+    if (min_interval > 0 && max_interval > 0 && max_interval > min_interval)
+    {
+        btif_cb.adv_data.mask |= BTM_BLE_AD_BIT_INT_RANGE;
+        btif_cb.adv_data.data.int_range.low = min_interval;
+        btif_cb.adv_data.data.int_range.hi = max_interval;
+    }
+
+    if (appearance != 0)
+    {
+        btif_cb.adv_data.mask |= BTM_BLE_AD_BIT_APPEARANCE;
+        btif_cb.adv_data.data.appearance = appearance;
+    }
+
+    if (manufacturer_len > 0 && manufacturer_data != NULL)
+    {
+        btif_cb.adv_data.data.manu.p_val = GKI_getbuf(manufacturer_len);
+        if (btif_cb.adv_data.data.manu.p_val != NULL)
+        {
+            btif_cb.adv_data.mask |= BTM_BLE_AD_BIT_MANU;
+            btif_cb.adv_data.data.manu.len = manufacturer_len;
+            memcpy(btif_cb.adv_data.data.manu.p_val, manufacturer_data, manufacturer_len);
+        }
+    }
+
+    return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SET_ADV_DATA,
+                                 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL);
+}
+
 static bt_status_t btif_gattc_refresh( int client_if, const bt_bdaddr_t *bd_addr )
 {
     CHECK_BTGATT_INIT();
@@ -1060,6 +1157,7 @@
     btif_gattc_scan,
     btif_gattc_open,
     btif_gattc_close,
+    btif_gattc_listen,
     btif_gattc_refresh,
     btif_gattc_search_service,
     btif_gattc_get_included_service,
@@ -1074,6 +1172,7 @@
     btif_gattc_dereg_for_notification,
     btif_gattc_read_remote_rssi,
     btif_gattc_get_device_type,
+    btif_gattc_set_adv_data,
     btif_gattc_test_command
 };