1 /******************************************************************************
2  *
3  *  Copyright 2014  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 
19 #define LOG_TAG "btm_ble_cont_energy"
20 
21 #include <bluetooth/log.h>
22 #include <string.h>
23 
24 #include "btm_ble_api.h"
25 #include "stack/btm/btm_int_types.h"
26 #include "stack/include/bt_types.h"
27 #include "stack/include/btm_client_interface.h"
28 
29 using namespace bluetooth;
30 
31 extern tBTM_CB btm_cb;
32 
33 tBTM_BLE_ENERGY_INFO_CB ble_energy_info_cb;
34 
35 /*******************************************************************************
36  *
37  * Function         btm_ble_cont_energy_cmpl_cback
38  *
39  * Description      Controller VSC complete callback
40  *
41  * Parameters
42  *
43  * Returns          void
44  *
45  ******************************************************************************/
btm_ble_cont_energy_cmpl_cback(tBTM_VSC_CMPL * p_params)46 static void btm_ble_cont_energy_cmpl_cback(tBTM_VSC_CMPL* p_params) {
47   uint8_t* p = p_params->p_param_buf;
48   uint16_t len = p_params->param_len;
49   uint32_t total_tx_time = 0, total_rx_time = 0, total_idle_time = 0, total_energy_used = 0;
50 
51   if (len < 17) {
52     log::error("wrong length for btm_ble_cont_energy_cmpl_cback");
53     return;
54   }
55 
56   uint8_t raw_status;
57   STREAM_TO_UINT8(raw_status, p);
58   tHCI_STATUS status = to_hci_status_code(raw_status);
59   STREAM_TO_UINT32(total_tx_time, p);
60   STREAM_TO_UINT32(total_rx_time, p);
61   STREAM_TO_UINT32(total_idle_time, p);
62   STREAM_TO_UINT32(total_energy_used, p);
63 
64   log::verbose("energy_info status={},tx_t={}, rx_t={}, ener_used={}, idle_t={}", status,
65                total_tx_time, total_rx_time, total_energy_used, total_idle_time);
66 
67   if (NULL != ble_energy_info_cb.p_ener_cback) {
68     ble_energy_info_cb.p_ener_cback(total_tx_time, total_rx_time, total_idle_time,
69                                     total_energy_used, static_cast<tHCI_STATUS>(status));
70   }
71 
72   return;
73 }
74 
75 /*******************************************************************************
76  *
77  * Function         BTM_BleGetEnergyInfo
78  *
79  * Description      This function obtains the energy info
80  *
81  * Parameters      p_ener_cback - Callback pointer
82  *
83  * Returns          status
84  *
85  ******************************************************************************/
BTM_BleGetEnergyInfo(tBTM_BLE_ENERGY_INFO_CBACK * p_ener_cback)86 tBTM_STATUS BTM_BleGetEnergyInfo(tBTM_BLE_ENERGY_INFO_CBACK* p_ener_cback) {
87   tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
88 
89   BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
90 
91   log::verbose("BTM_BleGetEnergyInfo");
92 
93   if (0 == cmn_ble_vsc_cb.energy_support) {
94     log::error("Controller does not support get energy info");
95     return tBTM_STATUS::BTM_ERR_PROCESSING;
96   }
97 
98   ble_energy_info_cb.p_ener_cback = p_ener_cback;
99   get_btm_client_interface().vendor.BTM_VendorSpecificCommand(HCI_BLE_ENERGY_INFO, 0, NULL,
100                                                               btm_ble_cont_energy_cmpl_cback);
101   return tBTM_STATUS::BTM_CMD_STARTED;
102 }
103