1d590543dSopenharmony_ci/*
2d590543dSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3d590543dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d590543dSopenharmony_ci * you may not use this file except in compliance with the License.
5d590543dSopenharmony_ci * You may obtain a copy of the License at
6d590543dSopenharmony_ci *
7d590543dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d590543dSopenharmony_ci *
9d590543dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d590543dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d590543dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d590543dSopenharmony_ci * See the License for the specific language governing permissions and
13d590543dSopenharmony_ci * limitations under the License.
14d590543dSopenharmony_ci */
15d590543dSopenharmony_ci
16d590543dSopenharmony_ci#include "entities/bluetooth_entity.h"
17d590543dSopenharmony_ci
18d590543dSopenharmony_ci#include <cinttypes>
19d590543dSopenharmony_ci#ifdef SYS_MGR_CLIENT_ENABLE
20d590543dSopenharmony_ci#include <ipc_skeleton.h>
21d590543dSopenharmony_ci#include "bundle_constants.h"
22d590543dSopenharmony_ci#include "bundle_mgr_interface.h"
23d590543dSopenharmony_ci#include "system_ability_definition.h"
24d590543dSopenharmony_ci#include "sys_mgr_client.h"
25d590543dSopenharmony_ci#endif
26d590543dSopenharmony_ci
27d590543dSopenharmony_ci#include "battery_stats_service.h"
28d590543dSopenharmony_ci#include "stats_log.h"
29d590543dSopenharmony_ci
30d590543dSopenharmony_cinamespace OHOS {
31d590543dSopenharmony_cinamespace PowerMgr {
32d590543dSopenharmony_cinamespace {
33d590543dSopenharmony_ci}
34d590543dSopenharmony_ci
35d590543dSopenharmony_ciBluetoothEntity::BluetoothEntity()
36d590543dSopenharmony_ci{
37d590543dSopenharmony_ci    consumptionType_ = BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH;
38d590543dSopenharmony_ci}
39d590543dSopenharmony_ci
40d590543dSopenharmony_civoid BluetoothEntity::Calculate(int32_t uid)
41d590543dSopenharmony_ci{
42d590543dSopenharmony_ci    if (uid > StatsUtils::INVALID_VALUE) {
43d590543dSopenharmony_ci        // Calculate Bluetooth scan caused by app
44d590543dSopenharmony_ci        CalculateBtPowerForApp(uid);
45d590543dSopenharmony_ci    } else {
46d590543dSopenharmony_ci        // Calculate Bluetooth on and Bluetooth app power consumption caused by Bluetooth hardware
47d590543dSopenharmony_ci        CalculateBtPower();
48d590543dSopenharmony_ci    }
49d590543dSopenharmony_ci}
50d590543dSopenharmony_ci
51d590543dSopenharmony_civoid BluetoothEntity::CalculateBtPower()
52d590543dSopenharmony_ci{
53d590543dSopenharmony_ci    auto bss = BatteryStatsService::GetInstance();
54d590543dSopenharmony_ci    // Calculate Bluetooth BR on power
55d590543dSopenharmony_ci    auto bluetoothBrOnAverageMa =
56d590543dSopenharmony_ci        bss->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_ON);
57d590543dSopenharmony_ci    auto bluetoothBrOnTimeMs = GetActiveTimeMs(StatsUtils::STATS_TYPE_BLUETOOTH_BR_ON);
58d590543dSopenharmony_ci    auto bluetoothBrOnPowerMah = bluetoothBrOnAverageMa * bluetoothBrOnTimeMs / StatsUtils::MS_IN_HOUR;
59d590543dSopenharmony_ci    bluetoothBrPowerMah_ += bluetoothBrOnPowerMah;
60d590543dSopenharmony_ci
61d590543dSopenharmony_ci    // Calculate Bluetooth BLE on power
62d590543dSopenharmony_ci    auto bluetoothBleOnAverageMa =
63d590543dSopenharmony_ci        bss->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_ON);
64d590543dSopenharmony_ci    auto bluetoothBleOnTimeMs = GetActiveTimeMs(StatsUtils::STATS_TYPE_BLUETOOTH_BLE_ON);
65d590543dSopenharmony_ci    auto bluetoothBleOnPowerMah = bluetoothBleOnAverageMa * bluetoothBleOnTimeMs / StatsUtils::MS_IN_HOUR;
66d590543dSopenharmony_ci    bluetoothBlePowerMah_ += bluetoothBleOnPowerMah;
67d590543dSopenharmony_ci
68d590543dSopenharmony_ci    auto bluetoothUidPowerMah = GetBluetoothUidPower();
69d590543dSopenharmony_ci
70d590543dSopenharmony_ci    bluetoothPowerMah_ = bluetoothBrOnPowerMah + bluetoothBleOnPowerMah + bluetoothUidPowerMah;
71d590543dSopenharmony_ci    totalPowerMah_ += bluetoothPowerMah_;
72d590543dSopenharmony_ci
73d590543dSopenharmony_ci    std::shared_ptr<BatteryStatsInfo> statsInfo = std::make_shared<BatteryStatsInfo>();
74d590543dSopenharmony_ci    statsInfo->SetConsumptioType(BatteryStatsInfo::CONSUMPTION_TYPE_BLUETOOTH);
75d590543dSopenharmony_ci    statsInfo->SetPower(bluetoothPowerMah_);
76d590543dSopenharmony_ci    statsInfoList_.push_back(statsInfo);
77d590543dSopenharmony_ci
78d590543dSopenharmony_ci    STATS_HILOGD(COMP_SVC, "Calculate bluetooth Br time: %{public}" PRId64 "ms, Br power average: %{public}lfma,"    \
79d590543dSopenharmony_ci        "Br power consumption: %{public}lfmAh, bluetooth Ble time: %{public}" PRId64 "ms, "                          \
80d590543dSopenharmony_ci        "Ble power average: %{public}lfma, Ble power consumption: %{public}lfmAh, "                                  \
81d590543dSopenharmony_ci        "uid power consumption: %{public}lfmAh, total power consumption: %{public}lfmAh",
82d590543dSopenharmony_ci        bluetoothBrOnTimeMs,
83d590543dSopenharmony_ci        bluetoothBrOnAverageMa,
84d590543dSopenharmony_ci        bluetoothBrOnPowerMah,
85d590543dSopenharmony_ci        bluetoothBleOnTimeMs,
86d590543dSopenharmony_ci        bluetoothBleOnAverageMa,
87d590543dSopenharmony_ci        bluetoothBleOnPowerMah,
88d590543dSopenharmony_ci        bluetoothUidPowerMah,
89d590543dSopenharmony_ci        bluetoothPowerMah_);
90d590543dSopenharmony_ci}
91d590543dSopenharmony_ci
92d590543dSopenharmony_civoid BluetoothEntity::CalculateBtPowerForApp(int32_t uid)
93d590543dSopenharmony_ci{
94d590543dSopenharmony_ci    auto bss = BatteryStatsService::GetInstance();
95d590543dSopenharmony_ci    // Calculate Bluetooth Br scan power consumption
96d590543dSopenharmony_ci    auto bluetoothBrScanAverageMa =
97d590543dSopenharmony_ci        bss->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BR_SCAN);
98d590543dSopenharmony_ci    auto bluetoothBrScanTimeMs = GetActiveTimeMs(uid, StatsUtils::STATS_TYPE_BLUETOOTH_BR_SCAN);
99d590543dSopenharmony_ci    auto bluetoothBrScanPowerMah = bluetoothBrScanTimeMs * bluetoothBrScanAverageMa / StatsUtils::MS_IN_HOUR;
100d590543dSopenharmony_ci    UpdateAppBluetoothBlePower(POWER_TYPE_BR, uid, bluetoothBrScanPowerMah);
101d590543dSopenharmony_ci
102d590543dSopenharmony_ci    // Calculate Bluetooth Ble scan power consumption
103d590543dSopenharmony_ci    auto bluetoothBleScanAverageMa =
104d590543dSopenharmony_ci        bss->GetBatteryStatsParser()->GetAveragePowerMa(StatsUtils::CURRENT_BLUETOOTH_BLE_SCAN);
105d590543dSopenharmony_ci    auto bluetoothBleScanTimeMs = GetActiveTimeMs(uid, StatsUtils::STATS_TYPE_BLUETOOTH_BLE_SCAN);
106d590543dSopenharmony_ci    auto bluetoothBleScanPowerMah = bluetoothBleScanTimeMs * bluetoothBleScanAverageMa / StatsUtils::MS_IN_HOUR;
107d590543dSopenharmony_ci    UpdateAppBluetoothBlePower(POWER_TYPE_BLE, uid, bluetoothBleScanPowerMah);
108d590543dSopenharmony_ci
109d590543dSopenharmony_ci    auto bluetoothUidPowerMah = bluetoothBrScanPowerMah + bluetoothBleScanPowerMah;
110d590543dSopenharmony_ci    UpdateAppBluetoothBlePower(POWER_TYPE_ALL, uid, bluetoothUidPowerMah);
111d590543dSopenharmony_ci
112d590543dSopenharmony_ci    STATS_HILOGD(COMP_SVC, "Calculate bluetooth Br scan time: %{public}" PRId64 "ms, "                         \
113d590543dSopenharmony_ci        "Br scan power average: %{public}lfma, Br scan power consumption: %{public}lfmAh "                     \
114d590543dSopenharmony_ci        "bluetooth Ble scan time: %{public}" PRId64 "ms, Ble scan power average: %{public}lfma, "              \
115d590543dSopenharmony_ci        "Ble scan power consumption: %{public}lfmAh, total power consumption: %{public}lfmAh, uid:%{public}d",
116d590543dSopenharmony_ci        bluetoothBrScanTimeMs,
117d590543dSopenharmony_ci        bluetoothBrScanAverageMa,
118d590543dSopenharmony_ci        bluetoothBrScanPowerMah,
119d590543dSopenharmony_ci        bluetoothBleScanTimeMs,
120d590543dSopenharmony_ci        bluetoothBleScanAverageMa,
121d590543dSopenharmony_ci        bluetoothBleScanPowerMah,
122d590543dSopenharmony_ci        bluetoothUidPowerMah,
123d590543dSopenharmony_ci        uid);
124d590543dSopenharmony_ci}
125d590543dSopenharmony_ci
126d590543dSopenharmony_civoid BluetoothEntity::UpdateAppBluetoothBlePower(PowerType type, int32_t uid, double powerMah)
127d590543dSopenharmony_ci{
128d590543dSopenharmony_ci    switch (type) {
129d590543dSopenharmony_ci        case POWER_TYPE_BR: {
130d590543dSopenharmony_ci            auto iter = appBluetoothBrPowerMap_.find(uid);
131d590543dSopenharmony_ci            if (iter != appBluetoothBrPowerMap_.end()) {
132d590543dSopenharmony_ci                iter->second = powerMah;
133d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Update app bluetooth Br power consumption: %{public}lfmAh for uid: %{public}d",
134d590543dSopenharmony_ci                    powerMah, uid);
135d590543dSopenharmony_ci                break;
136d590543dSopenharmony_ci            }
137d590543dSopenharmony_ci            appBluetoothBrPowerMap_.insert(std::pair<int32_t, double>(uid, powerMah));
138d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Create app bluetooth Br power consumption: %{public}lfmAh for uid: %{public}d",
139d590543dSopenharmony_ci                powerMah, uid);
140d590543dSopenharmony_ci            break;
141d590543dSopenharmony_ci        }
142d590543dSopenharmony_ci        case POWER_TYPE_BLE: {
143d590543dSopenharmony_ci            auto iter = appBluetoothBlePowerMap_.find(uid);
144d590543dSopenharmony_ci            if (iter != appBluetoothBlePowerMap_.end()) {
145d590543dSopenharmony_ci                iter->second = powerMah;
146d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Update app bluetooth Ble power consumption: %{public}lfmAh for uid: %{public}d",
147d590543dSopenharmony_ci                    powerMah, uid);
148d590543dSopenharmony_ci                break;
149d590543dSopenharmony_ci            }
150d590543dSopenharmony_ci            appBluetoothBlePowerMap_.insert(std::pair<int32_t, double>(uid, powerMah));
151d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Create app bluetooth Ble power consumption: %{public}lfmAh for uid: %{public}d",
152d590543dSopenharmony_ci                powerMah, uid);
153d590543dSopenharmony_ci            break;
154d590543dSopenharmony_ci        }
155d590543dSopenharmony_ci        case POWER_TYPE_ALL: {
156d590543dSopenharmony_ci            auto iter = appBluetoothPowerMap_.find(uid);
157d590543dSopenharmony_ci            if (iter != appBluetoothPowerMap_.end()) {
158d590543dSopenharmony_ci                iter->second = powerMah;
159d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Update app bluetooth power consumption: %{public}lfmAh for uid: %{public}d",
160d590543dSopenharmony_ci                    powerMah, uid);
161d590543dSopenharmony_ci                break;
162d590543dSopenharmony_ci            }
163d590543dSopenharmony_ci            appBluetoothPowerMap_.insert(std::pair<int32_t, double>(uid, powerMah));
164d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Create app bluetooth power consumption: %{public}lfmAh for uid: %{public}d",
165d590543dSopenharmony_ci                powerMah, uid);
166d590543dSopenharmony_ci            break;
167d590543dSopenharmony_ci        }
168d590543dSopenharmony_ci        default:
169d590543dSopenharmony_ci            break;
170d590543dSopenharmony_ci    }
171d590543dSopenharmony_ci}
172d590543dSopenharmony_ci
173d590543dSopenharmony_ciint64_t BluetoothEntity::GetActiveTimeMs(int32_t uid, StatsUtils::StatsType statsType, int16_t level)
174d590543dSopenharmony_ci{
175d590543dSopenharmony_ci    int64_t time = StatsUtils::DEFAULT_VALUE;
176d590543dSopenharmony_ci    switch (statsType) {
177d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BR_SCAN: {
178d590543dSopenharmony_ci            auto brScanIter = appBluetoothBrScanTimerMap_.find(uid);
179d590543dSopenharmony_ci            if (brScanIter != appBluetoothBrScanTimerMap_.end()) {
180d590543dSopenharmony_ci                time = brScanIter->second->GetRunningTimeMs();
181d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get blueooth Br scan time: %{public}" PRId64 "ms for uid: %{public}d",
182d590543dSopenharmony_ci                    time, uid);
183d590543dSopenharmony_ci                break;
184d590543dSopenharmony_ci            }
185d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "No bluetooth Br scan timer related to uid: %{public}d was found, return 0", uid);
186d590543dSopenharmony_ci            break;
187d590543dSopenharmony_ci        }
188d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BLE_SCAN: {
189d590543dSopenharmony_ci            auto bleScanIter = appBluetoothBleScanTimerMap_.find(uid);
190d590543dSopenharmony_ci            if (bleScanIter != appBluetoothBleScanTimerMap_.end()) {
191d590543dSopenharmony_ci                time = bleScanIter->second->GetRunningTimeMs();
192d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get blueooth Ble scan time: %{public}" PRId64 "ms for uid: %{public}d",
193d590543dSopenharmony_ci                    time, uid);
194d590543dSopenharmony_ci                break;
195d590543dSopenharmony_ci            }
196d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "No bluetooth Ble scan timer related to uid: %{public}d was found, return 0", uid);
197d590543dSopenharmony_ci            break;
198d590543dSopenharmony_ci        }
199d590543dSopenharmony_ci        default:
200d590543dSopenharmony_ci            break;
201d590543dSopenharmony_ci    }
202d590543dSopenharmony_ci    return time;
203d590543dSopenharmony_ci}
204d590543dSopenharmony_ci
205d590543dSopenharmony_ciint64_t BluetoothEntity::GetActiveTimeMs(StatsUtils::StatsType statsType, int16_t level)
206d590543dSopenharmony_ci{
207d590543dSopenharmony_ci    int64_t time = StatsUtils::DEFAULT_VALUE;
208d590543dSopenharmony_ci    switch (statsType) {
209d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BR_ON: {
210d590543dSopenharmony_ci            if (bluetoothBrOnTimer_) {
211d590543dSopenharmony_ci                time = bluetoothBrOnTimer_->GetRunningTimeMs();
212d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get blueooth Br on time: %{public}" PRId64 "ms", time);
213d590543dSopenharmony_ci                break;
214d590543dSopenharmony_ci            }
215d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Bluetooth Br has not been turned on yet, return 0");
216d590543dSopenharmony_ci            break;
217d590543dSopenharmony_ci        }
218d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BLE_ON: {
219d590543dSopenharmony_ci            if (bluetoothBleOnTimer_) {
220d590543dSopenharmony_ci                time = bluetoothBleOnTimer_->GetRunningTimeMs();
221d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get blueooth Ble on time: %{public}" PRId64 "ms", time);
222d590543dSopenharmony_ci                break;
223d590543dSopenharmony_ci            }
224d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Bluetooth Ble has not been turned on yet, return 0");
225d590543dSopenharmony_ci            break;
226d590543dSopenharmony_ci        }
227d590543dSopenharmony_ci        default:
228d590543dSopenharmony_ci            break;
229d590543dSopenharmony_ci    }
230d590543dSopenharmony_ci    return time;
231d590543dSopenharmony_ci}
232d590543dSopenharmony_ci
233d590543dSopenharmony_cidouble BluetoothEntity::GetEntityPowerMah(int32_t uidOrUserId)
234d590543dSopenharmony_ci{
235d590543dSopenharmony_ci    double power = StatsUtils::DEFAULT_VALUE;
236d590543dSopenharmony_ci    if (uidOrUserId > StatsUtils::INVALID_VALUE) {
237d590543dSopenharmony_ci        auto iter = appBluetoothPowerMap_.find(uidOrUserId);
238d590543dSopenharmony_ci        if (iter != appBluetoothPowerMap_.end()) {
239d590543dSopenharmony_ci            power = iter->second;
240d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Get app blueooth power consumption: %{public}lfmAh for uid: %{public}d",
241d590543dSopenharmony_ci                power, uidOrUserId);
242d590543dSopenharmony_ci        } else {
243d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC,
244d590543dSopenharmony_ci                "No app blueooth power consumption related to uid: %{public}d was found, return 0", uidOrUserId);
245d590543dSopenharmony_ci        }
246d590543dSopenharmony_ci    } else {
247d590543dSopenharmony_ci        power = bluetoothPowerMah_;
248d590543dSopenharmony_ci        STATS_HILOGD(COMP_SVC, "Get blueooth power consumption: %{public}lfmAh", power);
249d590543dSopenharmony_ci    }
250d590543dSopenharmony_ci    return power;
251d590543dSopenharmony_ci}
252d590543dSopenharmony_ci
253d590543dSopenharmony_cidouble BluetoothEntity::GetStatsPowerMah(StatsUtils::StatsType statsType, int32_t uid)
254d590543dSopenharmony_ci{
255d590543dSopenharmony_ci    double power = StatsUtils::DEFAULT_VALUE;
256d590543dSopenharmony_ci    switch (statsType) {
257d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BR_ON: {
258d590543dSopenharmony_ci            power = bluetoothBrPowerMah_;
259d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Get blueooth Br on power consumption: %{public}lfmAh", power);
260d590543dSopenharmony_ci            break;
261d590543dSopenharmony_ci        }
262d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BLE_ON: {
263d590543dSopenharmony_ci            power = bluetoothBlePowerMah_;
264d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Get blueooth Ble on power consumption: %{public}lfmAh", power);
265d590543dSopenharmony_ci            break;
266d590543dSopenharmony_ci        }
267d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BR_SCAN: {
268d590543dSopenharmony_ci            auto brIter = appBluetoothBrPowerMap_.find(uid);
269d590543dSopenharmony_ci            if (brIter != appBluetoothBrPowerMap_.end()) {
270d590543dSopenharmony_ci                power = brIter->second;
271d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get blueooth Br scan power consumption: %{public}lfmAh for uid: %{public}d",
272d590543dSopenharmony_ci                    power, uid);
273d590543dSopenharmony_ci                break;
274d590543dSopenharmony_ci            }
275d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC,
276d590543dSopenharmony_ci                "No bluetooth Br scan power consumption related to uid: %{public}d was found, return 0", uid);
277d590543dSopenharmony_ci            break;
278d590543dSopenharmony_ci        }
279d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BLE_SCAN: {
280d590543dSopenharmony_ci            auto bleIter = appBluetoothBlePowerMap_.find(uid);
281d590543dSopenharmony_ci            if (bleIter != appBluetoothBlePowerMap_.end()) {
282d590543dSopenharmony_ci                power = bleIter->second;
283d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get blueooth Ble scan power consumption: %{public}lfmAh for uid: %{public}d",
284d590543dSopenharmony_ci                    power, uid);
285d590543dSopenharmony_ci                break;
286d590543dSopenharmony_ci            }
287d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC,
288d590543dSopenharmony_ci                "No bluetooth Ble scan power consumption related to uid: %{public}d was found, return 0", uid);
289d590543dSopenharmony_ci            break;
290d590543dSopenharmony_ci        }
291d590543dSopenharmony_ci        default:
292d590543dSopenharmony_ci            break;
293d590543dSopenharmony_ci    }
294d590543dSopenharmony_ci    return power;
295d590543dSopenharmony_ci}
296d590543dSopenharmony_ci
297d590543dSopenharmony_civoid BluetoothEntity::Reset()
298d590543dSopenharmony_ci{
299d590543dSopenharmony_ci    // Reset Bluetooth on timer and power consumption
300d590543dSopenharmony_ci    bluetoothBrPowerMah_ = StatsUtils::DEFAULT_VALUE;
301d590543dSopenharmony_ci    bluetoothBlePowerMah_ = StatsUtils::DEFAULT_VALUE;
302d590543dSopenharmony_ci    bluetoothPowerMah_ = StatsUtils::DEFAULT_VALUE;
303d590543dSopenharmony_ci    if (bluetoothBrOnTimer_) {
304d590543dSopenharmony_ci        bluetoothBrOnTimer_->Reset();
305d590543dSopenharmony_ci    }
306d590543dSopenharmony_ci
307d590543dSopenharmony_ci    if (bluetoothBleOnTimer_) {
308d590543dSopenharmony_ci        bluetoothBleOnTimer_->Reset();
309d590543dSopenharmony_ci    }
310d590543dSopenharmony_ci
311d590543dSopenharmony_ci    // Reset app Bluetooth scan power consumption
312d590543dSopenharmony_ci    for (auto& iter : appBluetoothBrPowerMap_) {
313d590543dSopenharmony_ci        iter.second = StatsUtils::DEFAULT_VALUE;
314d590543dSopenharmony_ci    }
315d590543dSopenharmony_ci
316d590543dSopenharmony_ci    for (auto& iter : appBluetoothBlePowerMap_) {
317d590543dSopenharmony_ci        iter.second = StatsUtils::DEFAULT_VALUE;
318d590543dSopenharmony_ci    }
319d590543dSopenharmony_ci
320d590543dSopenharmony_ci    for (auto& iter : appBluetoothPowerMap_) {
321d590543dSopenharmony_ci        iter.second = StatsUtils::DEFAULT_VALUE;
322d590543dSopenharmony_ci    }
323d590543dSopenharmony_ci
324d590543dSopenharmony_ci    // Reset Bluetooth scan timer
325d590543dSopenharmony_ci    for (auto& iter : appBluetoothBrScanTimerMap_) {
326d590543dSopenharmony_ci        if (iter.second) {
327d590543dSopenharmony_ci            iter.second->Reset();
328d590543dSopenharmony_ci        }
329d590543dSopenharmony_ci    }
330d590543dSopenharmony_ci
331d590543dSopenharmony_ci    for (auto& iter : appBluetoothBleScanTimerMap_) {
332d590543dSopenharmony_ci        if (iter.second) {
333d590543dSopenharmony_ci            iter.second->Reset();
334d590543dSopenharmony_ci        }
335d590543dSopenharmony_ci    }
336d590543dSopenharmony_ci}
337d590543dSopenharmony_ci
338d590543dSopenharmony_cistd::shared_ptr<StatsHelper::ActiveTimer> BluetoothEntity::GetOrCreateTimer(int32_t uid,
339d590543dSopenharmony_ci    StatsUtils::StatsType statsType, int16_t level)
340d590543dSopenharmony_ci{
341d590543dSopenharmony_ci    std::shared_ptr<StatsHelper::ActiveTimer> timer = nullptr;
342d590543dSopenharmony_ci    switch (statsType) {
343d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BR_SCAN: {
344d590543dSopenharmony_ci            auto brScanIter = appBluetoothBrScanTimerMap_.find(uid);
345d590543dSopenharmony_ci            if (brScanIter != appBluetoothBrScanTimerMap_.end()) {
346d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get blueooth Br scan timer for uid: %{public}d", uid);
347d590543dSopenharmony_ci                timer = brScanIter->second;
348d590543dSopenharmony_ci                break;
349d590543dSopenharmony_ci            }
350d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Create bluetooth Br scan timer for uid: %{public}d", uid);
351d590543dSopenharmony_ci            std::shared_ptr<StatsHelper::ActiveTimer> brScanTimer = std::make_shared<StatsHelper::ActiveTimer>();
352d590543dSopenharmony_ci            appBluetoothBrScanTimerMap_.insert(
353d590543dSopenharmony_ci                std::pair<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>>(uid, brScanTimer));
354d590543dSopenharmony_ci            timer = brScanTimer;
355d590543dSopenharmony_ci            break;
356d590543dSopenharmony_ci        }
357d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BLE_SCAN: {
358d590543dSopenharmony_ci            auto bleScanIter = appBluetoothBleScanTimerMap_.find(uid);
359d590543dSopenharmony_ci            if (bleScanIter != appBluetoothBleScanTimerMap_.end()) {
360d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get blueooth Ble scan timer for uid: %{public}d", uid);
361d590543dSopenharmony_ci                timer = bleScanIter->second;
362d590543dSopenharmony_ci                break;
363d590543dSopenharmony_ci            }
364d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Create bluetooth Ble scan timer for uid: %{public}d", uid);
365d590543dSopenharmony_ci            std::shared_ptr<StatsHelper::ActiveTimer> bleScanTimer = std::make_shared<StatsHelper::ActiveTimer>();
366d590543dSopenharmony_ci            appBluetoothBleScanTimerMap_.insert(
367d590543dSopenharmony_ci                std::pair<int32_t, std::shared_ptr<StatsHelper::ActiveTimer>>(uid, bleScanTimer));
368d590543dSopenharmony_ci            timer = bleScanTimer;
369d590543dSopenharmony_ci            break;
370d590543dSopenharmony_ci        }
371d590543dSopenharmony_ci        default:
372d590543dSopenharmony_ci            STATS_HILOGW(COMP_SVC, "Create active timer failed");
373d590543dSopenharmony_ci            break;
374d590543dSopenharmony_ci    }
375d590543dSopenharmony_ci    return timer;
376d590543dSopenharmony_ci}
377d590543dSopenharmony_ci
378d590543dSopenharmony_cistd::shared_ptr<StatsHelper::ActiveTimer> BluetoothEntity::GetOrCreateTimer(StatsUtils::StatsType statsType,
379d590543dSopenharmony_ci    int16_t level)
380d590543dSopenharmony_ci{
381d590543dSopenharmony_ci    std::shared_ptr<StatsHelper::ActiveTimer> timer = nullptr;
382d590543dSopenharmony_ci    switch (statsType) {
383d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BR_ON: {
384d590543dSopenharmony_ci            if (bluetoothBrOnTimer_ != nullptr) {
385d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get blueooth Br on timer");
386d590543dSopenharmony_ci                timer = bluetoothBrOnTimer_;
387d590543dSopenharmony_ci                break;
388d590543dSopenharmony_ci            }
389d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Create blueooth Br on timer");
390d590543dSopenharmony_ci            bluetoothBrOnTimer_ = std::make_shared<StatsHelper::ActiveTimer>();
391d590543dSopenharmony_ci            timer = bluetoothBrOnTimer_;
392d590543dSopenharmony_ci            break;
393d590543dSopenharmony_ci        }
394d590543dSopenharmony_ci        case StatsUtils::STATS_TYPE_BLUETOOTH_BLE_ON: {
395d590543dSopenharmony_ci            if (bluetoothBleOnTimer_ != nullptr) {
396d590543dSopenharmony_ci                STATS_HILOGD(COMP_SVC, "Get blueooth Ble on timer");
397d590543dSopenharmony_ci                timer = bluetoothBleOnTimer_;
398d590543dSopenharmony_ci                break;
399d590543dSopenharmony_ci            }
400d590543dSopenharmony_ci            STATS_HILOGD(COMP_SVC, "Create blueooth Ble on timer");
401d590543dSopenharmony_ci            bluetoothBleOnTimer_ = std::make_shared<StatsHelper::ActiveTimer>();
402d590543dSopenharmony_ci            timer = bluetoothBleOnTimer_;
403d590543dSopenharmony_ci            break;
404d590543dSopenharmony_ci        }
405d590543dSopenharmony_ci        default:
406d590543dSopenharmony_ci            STATS_HILOGW(COMP_SVC, "Create active timer failed");
407d590543dSopenharmony_ci            break;
408d590543dSopenharmony_ci    }
409d590543dSopenharmony_ci    return timer;
410d590543dSopenharmony_ci}
411d590543dSopenharmony_ci
412d590543dSopenharmony_cidouble BluetoothEntity::GetBluetoothUidPower()
413d590543dSopenharmony_ci{
414d590543dSopenharmony_ci    double bluetoothUidPower = StatsUtils::DEFAULT_VALUE;
415d590543dSopenharmony_ci#ifdef SYS_MGR_CLIENT_ENABLE
416d590543dSopenharmony_ci    auto bundleObj =
417d590543dSopenharmony_ci        DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance()->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
418d590543dSopenharmony_ci    if (bundleObj == nullptr) {
419d590543dSopenharmony_ci        STATS_HILOGW(COMP_SVC, "Failed to get bundle manager service, return 0");
420d590543dSopenharmony_ci        return bluetoothUidPower;
421d590543dSopenharmony_ci    }
422d590543dSopenharmony_ci
423d590543dSopenharmony_ci    sptr<AppExecFwk::IBundleMgr> bmgr = iface_cast<AppExecFwk::IBundleMgr>(bundleObj);
424d590543dSopenharmony_ci    if (bmgr == nullptr) {
425d590543dSopenharmony_ci        STATS_HILOGW(COMP_SVC, "Failed to get bundle manager proxy, return 0");
426d590543dSopenharmony_ci        return bluetoothUidPower;
427d590543dSopenharmony_ci    }
428d590543dSopenharmony_ci
429d590543dSopenharmony_ci    std::string bundleName = "com.ohos.bluetooth";
430d590543dSopenharmony_ci    std::string identity = IPCSkeleton::ResetCallingIdentity();
431d590543dSopenharmony_ci    int32_t bluetoothUid = bmgr->GetUidByBundleName(bundleName, AppExecFwk::Constants::DEFAULT_USERID);
432d590543dSopenharmony_ci    IPCSkeleton::SetCallingIdentity(identity);
433d590543dSopenharmony_ci
434d590543dSopenharmony_ci    auto bss = BatteryStatsService::GetInstance();
435d590543dSopenharmony_ci    auto core = bss->GetBatteryStatsCore();
436d590543dSopenharmony_ci    auto uidEntity = core->GetEntity(BatteryStatsInfo::CONSUMPTION_TYPE_APP);
437d590543dSopenharmony_ci    if (uidEntity != nullptr) {
438d590543dSopenharmony_ci        bluetoothUidPower = uidEntity->GetEntityPowerMah(bluetoothUid);
439d590543dSopenharmony_ci    }
440d590543dSopenharmony_ci    STATS_HILOGD(COMP_SVC, "Get bluetooth uid power consumption: %{public}lfmAh", bluetoothUidPower);
441d590543dSopenharmony_ci#endif
442d590543dSopenharmony_ci    return bluetoothUidPower;
443d590543dSopenharmony_ci}
444d590543dSopenharmony_ci
445d590543dSopenharmony_civoid BluetoothEntity::DumpInfo(std::string& result, int32_t uid)
446d590543dSopenharmony_ci{
447d590543dSopenharmony_ci    int64_t brOntime = GetActiveTimeMs(StatsUtils::STATS_TYPE_BLUETOOTH_BR_ON);
448d590543dSopenharmony_ci    int64_t bleOntime = GetActiveTimeMs(StatsUtils::STATS_TYPE_BLUETOOTH_BR_ON);
449d590543dSopenharmony_ci    result.append("Bluetooth dump:\n")
450d590543dSopenharmony_ci        .append("Bluetooth Br on time: ")
451d590543dSopenharmony_ci        .append(ToString(brOntime))
452d590543dSopenharmony_ci        .append("ms")
453d590543dSopenharmony_ci        .append("\n")
454d590543dSopenharmony_ci        .append("Bluetooth Ble on time: ")
455d590543dSopenharmony_ci        .append(ToString(bleOntime))
456d590543dSopenharmony_ci        .append("ms")
457d590543dSopenharmony_ci        .append("\n");
458d590543dSopenharmony_ci}
459d590543dSopenharmony_ci} // namespace PowerMgr
460d590543dSopenharmony_ci} // namespace OHOS
461