1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "classic_battery_observer.h"
17
18 #include "interface_adapter_classic.h"
19 #include "interface_adapter_manager.h"
20 #include "log.h"
21 #include "log_util.h"
22
23 namespace OHOS {
24 namespace bluetooth {
ClassicBatteryObserverHf(utility::Dispatcher & dispatcher)25 ClassicBatteryObserverHf::ClassicBatteryObserverHf(utility::Dispatcher &dispatcher) : dispatcher_(&dispatcher)
26 {
27 LOG_DEBUG("[ClassicBatteryObserverHf]::%{public}s", __func__);
28 }
29
~ClassicBatteryObserverHf()30 ClassicBatteryObserverHf::~ClassicBatteryObserverHf()
31 {
32 LOG_DEBUG("[ClassicBatteryObserverHf]::%{public}s", __func__);
33 }
34
OnBatteryLevelChanged(const RawAddress & device,int batteryLevel)35 void ClassicBatteryObserverHf::OnBatteryLevelChanged(const RawAddress &device, int batteryLevel)
36 {
37 HILOGI("address: %{public}s, batteryLevel: %{public}d", GET_ENCRYPT_ADDR(device), batteryLevel);
38 dispatcher_->PostTask(std::bind(&ClassicBatteryObserverHf::SetBatteryLevel, this, device, batteryLevel));
39 }
40
SetBatteryLevel(const RawAddress & device,int batteryLevel) const41 void ClassicBatteryObserverHf::SetBatteryLevel(const RawAddress &device, int batteryLevel) const
42 {
43 LOG_DEBUG("[ClassicBatteryObserverAg]::%{public}s", __func__);
44 auto classicAdapter = IAdapterManager::GetInstance()->GetClassicAdapterInterface();
45 if (classicAdapter) {
46 classicAdapter->SetDeviceBatteryLevel(device, batteryLevel);
47 }
48 }
49
ClassicBatteryObserverAg(utility::Dispatcher & dispatcher)50 ClassicBatteryObserverAg::ClassicBatteryObserverAg(utility::Dispatcher &dispatcher) : dispatcher_(&dispatcher)
51 {
52 LOG_DEBUG("[ClassicBatteryObserverAg]::%{public}s", __func__);
53 }
54
~ClassicBatteryObserverAg()55 ClassicBatteryObserverAg::~ClassicBatteryObserverAg()
56 {
57 LOG_DEBUG("[ClassicBatteryObserverAg]::%{public}s", __func__);
58 }
59
OnHfBatteryLevelChanged(const RawAddress & device,int indValue)60 void ClassicBatteryObserverAg::OnHfBatteryLevelChanged(const RawAddress &device, int indValue)
61 {
62 HILOGI("address: %{public}s, batteryLevel: %{public}d", GET_ENCRYPT_ADDR(device), indValue);
63 dispatcher_->PostTask(std::bind(&ClassicBatteryObserverAg::SetBatteryLevel, this, device, indValue));
64 }
65
SetBatteryLevel(const RawAddress & device,int batteryLevel) const66 void ClassicBatteryObserverAg::SetBatteryLevel(const RawAddress &device, int batteryLevel) const
67 {
68 LOG_DEBUG("[ClassicBatteryObserverAg]::%{public}s", __func__);
69 auto classicAdapter = IAdapterManager::GetInstance()->GetClassicAdapterInterface();
70 if (classicAdapter) {
71 classicAdapter->SetDeviceBatteryLevel(device, batteryLevel);
72 }
73 }
74 } // namespace bluetooth
75 } // namespace OHOS