From 4a6f8cf8beccd75198b2d04e8ed15eb5e7592f70 Mon Sep 17 00:00:00 2001
From: Andre Eisenbach <eisenbach@google.com>
Date: Wed, 18 Jun 2014 12:20:37 -0700
Subject: [PATCH] LE: Add is_connected() function (2/3)

Change-Id: I73c4ea7f13982bf9c208996f9c749d2dfbe698be
---
 system/bta/dm/bta_dm_api.c     | 16 ++++++++++++++++
 system/bta/include/bta_api.h   | 11 +++++++++++
 system/btif/include/btif_api.h | 11 +++++++++++
 system/btif/src/bluetooth.c    | 10 ++++++++++
 system/btif/src/btif_dm.c      | 14 ++++++++++++++
 5 files changed, 62 insertions(+)

diff --git a/system/bta/dm/bta_dm_api.c b/system/bta/dm/bta_dm_api.c
index 92af9c7b76b..798ef9677dc 100644
--- a/system/bta/dm/bta_dm_api.c
+++ b/system/bta/dm/bta_dm_api.c
@@ -1091,6 +1091,22 @@ BOOLEAN BTA_DmUseSsr( BD_ADDR bd_addr )
     return use_ssr;
 }
 
+/*******************************************************************************
+**
+** Function         BTA_DmGetConnectionState
+**
+** Description      Returns whether the remote device is currently connected.
+**
+** Returns          0 if the device is NOT connected.
+**
+*******************************************************************************/
+UINT16 BTA_DmGetConnectionState( BD_ADDR bd_addr )
+{
+    tBTA_DM_PEER_DEVICE * p_dev = bta_dm_find_peer_device(bd_addr);
+    return (p_dev && p_dev->conn_state == BTA_DM_CONNECTED);
+}
+
+
 /*******************************************************************************
 **                   Device Identification (DI) Server Functions
 *******************************************************************************/
diff --git a/system/bta/include/bta_api.h b/system/bta/include/bta_api.h
index 0b7d960f00f..1035bac9450 100644
--- a/system/bta/include/bta_api.h
+++ b/system/bta/include/bta_api.h
@@ -1724,6 +1724,17 @@ BTA_API extern void BTA_GetEirService( UINT8 *p_eir, tBTA_SERVICE_MASK *p_servic
 *******************************************************************************/
 BTA_API extern BOOLEAN BTA_DmUseSsr( BD_ADDR bd_addr );
 
+/*******************************************************************************
+**
+** Function         BTA_DmGetConnectionState
+**
+** Description      Returns whether the remote device is currently connected.
+**
+** Returns          0 if the device is NOT connected.
+**
+*******************************************************************************/
+BTA_API extern UINT16 BTA_DmGetConnectionState( BD_ADDR bd_addr );
+
 
 /*******************************************************************************
 **
diff --git a/system/btif/include/btif_api.h b/system/btif/include/btif_api.h
index 3a5607fc477..80a74d0515f 100644
--- a/system/btif/include/btif_api.h
+++ b/system/btif/include/btif_api.h
@@ -230,6 +230,17 @@ bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr);
 *******************************************************************************/
 bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr);
 
+/*******************************************************************************
+**
+** Function         btif_dm_get_connection_state
+**
+** Description      Returns whether the remote device is currently connected
+**
+** Returns          0 if not connected
+**
+*******************************************************************************/
+uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr);
+
 /*******************************************************************************
 **
 ** Function         btif_dm_pin_reply
diff --git a/system/btif/src/bluetooth.c b/system/btif/src/bluetooth.c
index b620b941e88..a88a40c8624 100644
--- a/system/btif/src/bluetooth.c
+++ b/system/btif/src/bluetooth.c
@@ -289,6 +289,15 @@ static int remove_bond(const bt_bdaddr_t *bd_addr)
     return btif_dm_remove_bond(bd_addr);
 }
 
+static int get_connection_state(const bt_bdaddr_t *bd_addr)
+{
+    /* sanity check */
+    if (interface_ready() == FALSE)
+        return 0;
+
+    return btif_dm_get_connection_state(bd_addr);
+}
+
 static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept,
                  uint8_t pin_len, bt_pin_code_t *pin_code)
 {
@@ -426,6 +435,7 @@ static const bt_interface_t bluetoothInterface = {
     create_bond,
     remove_bond,
     cancel_bond,
+    get_connection_state,
     pin_reply,
     ssp_reply,
     get_profile_interface,
diff --git a/system/btif/src/btif_dm.c b/system/btif/src/btif_dm.c
index 8a06e188f59..fd96bb9a6c9 100644
--- a/system/btif/src/btif_dm.c
+++ b/system/btif/src/btif_dm.c
@@ -552,6 +552,20 @@ void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
     }
 }
 
+/*******************************************************************************
+**
+** Function         btif_dm_get_connection_state
+**
+** Description      Returns whether the remote device is currently connected
+**
+** Returns          0 if not connected
+**
+*******************************************************************************/
+uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
+{
+    return BTA_DmGetConnectionState((UINT8 *)bd_addr->address);
+}
+
 /*******************************************************************************
 **
 ** Function         search_devices_copy_cb
-- 
GitLab