LE energy info feature

Stack and BT-IF changes for LE energy feature

Change-Id: I671e63aaead210c6352b45a5e20ad5e4bbbb77b7
diff --git a/btif/include/btif_api.h b/btif/include/btif_api.h
index 80a74d0..2369b13 100644
--- a/btif/include/btif_api.h
+++ b/btif/include/btif_api.h
@@ -349,6 +349,17 @@
 
 /*******************************************************************************
 **
+** Function         btif_dm_read_energy_info
+**
+** Description     Reads the energy info from controller
+**
+** Returns          void
+**
+*******************************************************************************/
+void btif_dm_read_energy_info();
+
+/*******************************************************************************
+**
 ** Function         btif_config_hci_snoop_log
 **
 ** Description     enable or disable HCI snoop log
diff --git a/btif/src/bluetooth.c b/btif/src/bluetooth.c
index 21b6f24..7020ffa 100644
--- a/btif/src/bluetooth.c
+++ b/btif/src/bluetooth.c
@@ -321,6 +321,14 @@
     return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
 }
 
+static int read_energy_info()
+{
+    if (interface_ready() == FALSE)
+        return BT_STATUS_NOT_READY;
+    btif_dm_read_energy_info();
+    return BT_STATUS_SUCCESS;
+}
+
 static const void* get_profile_interface (const char *profile_id)
 {
     ALOGI("get_profile_interface %s", profile_id);
@@ -454,6 +462,7 @@
 #endif
     config_hci_snoop_log,
     set_os_callouts,
+    read_energy_info,
 };
 
 const bt_interface_t* bluetooth__get_bluetooth_interface ()
diff --git a/btif/src/btif_core.c b/btif/src/btif_core.c
index c168e6c..4265808 100644
--- a/btif/src/btif_core.c
+++ b/btif/src/btif_core.c
@@ -1043,6 +1043,7 @@
                 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
                 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
                 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
+                local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
                 memcpy(prop.val, &local_le_features, prop.len);
                 #endif
             }
diff --git a/btif/src/btif_dm.c b/btif/src/btif_dm.c
index 7ec5948..c3dbe95 100644
--- a/btif/src/btif_dm.c
+++ b/btif/src/btif_dm.c
@@ -124,6 +124,17 @@
     BT_OCTET16 sp_r;
     BD_ADDR  oob_bdaddr;  /* peer bdaddr*/
 } btif_dm_oob_cb_t;
+
+typedef struct
+{
+    uint8_t  status;
+    uint8_t  ctrl_state;
+    uint64_t tx_time;
+    uint64_t rx_time;
+    uint64_t idle_time;
+    uint64_t energy_used;
+} btif_activity_energy_info_cb_t;
+
 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id)       (1 << (id))
 
 /* This flag will be true if HCI_Inquiry is in progress */
@@ -1671,6 +1682,20 @@
             HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
             break;
          }
+
+        case BTA_DM_ENER_INFO_READ:
+        {
+            btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
+            bt_activity_energy_info energy_info;
+            energy_info.status = p_ener_data->status;
+            energy_info.ctrl_state = p_ener_data->ctrl_state;
+            energy_info.rx_time = p_ener_data->rx_time;
+            energy_info.tx_time = p_ener_data->tx_time;
+            energy_info.idle_time = p_ener_data->idle_time;
+            energy_info.energy_used = p_ener_data->energy_used;
+            HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
+            break;
+        }
 #endif
 
         case BTA_DM_AUTHORIZE_EVT:
@@ -1866,6 +1891,34 @@
    btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
 }
 
+/*******************************************************************************
+**
+** Function         bta_energy_info_cb
+**
+** Description      Switches context from BTE to BTIF for DM energy info event
+**
+** Returns          void
+**
+*******************************************************************************/
+static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
+                                    tBTA_DM_BLE_IDLE_TIME_MS idle_time,
+                                    tBTA_DM_BLE_ENERGY_USED energy_used,
+                                    tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
+{
+    BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
+        status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
+
+    btif_activity_energy_info_cb_t btif_cb;
+    btif_cb.status = status;
+    btif_cb.ctrl_state = ctrl_state;
+    btif_cb.tx_time = (uint64_t) tx_time;
+    btif_cb.rx_time = (uint64_t) rx_time;
+    btif_cb.idle_time =(uint64_t) idle_time;
+    btif_cb.energy_used =(uint64_t) energy_used;
+    btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
+                          (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
+}
+
 /*****************************************************************************
 **
 **   btif api functions (no context switch)
@@ -2790,6 +2843,20 @@
     }
 }
 
+/*******************************************************************************
+**
+** Function         btif_dm_read_energy_info
+**
+** Description     Reads the energy info from controller
+**
+** Returns         void
+**
+*******************************************************************************/
+void btif_dm_read_energy_info()
+{
+    BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
+}
+
 static char* btif_get_default_local_name() {
     if (btif_default_local_name[0] == '\0')
     {
diff --git a/btif/src/btif_util.c b/btif/src/btif_util.c
index 693e768..bd376a6 100644
--- a/btif/src/btif_util.c
+++ b/btif/src/btif_util.c
@@ -282,6 +282,7 @@
         CASE_RETURN_STR(BTA_DM_BLE_AUTH_CMPL_EVT)
         CASE_RETURN_STR(BTA_DM_DEV_UNPAIRED_EVT)
         CASE_RETURN_STR(BTA_DM_HW_ERROR_EVT)
+        CASE_RETURN_STR(BTA_DM_ENER_INFO_READ)
 
         default:
             return "UNKNOWN DM EVENT";