• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 "events_info.h"
17 
18 #include "basic_definitions.h"
19 #include "dp_log.h"
20 #ifdef CAMERA_USE_BATTERY
21 #include "battery_srv_client.h"
22 #include "battery_info.h"
23 #endif
24 #ifdef CAMERA_USE_THERMAL
25 #include "thermal_mgr_client.h"
26 #endif
27 #ifdef CAMERA_USE_POWER
28 #include "power_mgr_client.h"
29 #endif
30 
31 namespace OHOS {
32 namespace CameraStandard {
33 namespace DeferredProcessing {
34 namespace {
35     constexpr int32_t BATTERY_THRESHOLD = 50;
36 }
37 
EventsInfo()38 EventsInfo::EventsInfo()
39 {
40     DP_DEBUG_LOG("entered.");
41 }
42 
~EventsInfo()43 EventsInfo::~EventsInfo()
44 {
45     DP_DEBUG_LOG("entered.");
46 }
47 
GetScreenState()48 ScreenStatus EventsInfo::GetScreenState()
49 {
50 #ifdef CAMERA_USE_POWER
51     auto& power = PowerMgr::PowerMgrClient::GetInstance();
52     if (power.IsScreenOn()) {
53         screenState_ = ScreenStatus::SCREEN_ON;
54     } else {
55         screenState_ = ScreenStatus::SCREEN_OFF;
56     }
57 #endif
58     return screenState_;
59 }
60 
GetBatteryState()61 BatteryStatus EventsInfo::GetBatteryState()
62 {
63 #ifdef CAMERA_USE_BATTERY
64     auto& battery = PowerMgr::BatterySrvClient::GetInstance();
65     auto level = battery.GetCapacityLevel();
66     DP_INFO_LOG("GetBatteryState: %{public}d", level);
67     switch (level) {
68         case PowerMgr::BatteryCapacityLevel::LEVEL_NORMAL:
69         case PowerMgr::BatteryCapacityLevel::LEVEL_HIGH:
70         case PowerMgr::BatteryCapacityLevel::LEVEL_FULL:
71             batteryState_ = BatteryStatus::BATTERY_OKAY;
72             break;
73         default:
74             batteryState_ = BatteryStatus::BATTERY_LOW;
75             break;
76     }
77 #endif
78     return batteryState_;
79 }
80 
GetChargingState()81 ChargingStatus EventsInfo::GetChargingState()
82 {
83 #ifdef CAMERA_USE_BATTERY
84     auto& battery = PowerMgr::BatterySrvClient::GetInstance();
85     auto status = battery.GetChargingStatus();
86     DP_INFO_LOG("GetChargingState: %{public}d", status);
87     switch (status) {
88         case PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE:
89         case PowerMgr::BatteryChargeState::CHARGE_STATE_FULL:
90             chargingState_ = ChargingStatus::CHARGING;
91             break;
92         default:
93             chargingState_ = ChargingStatus::DISCHARGING;
94             break;
95     }
96 #endif
97     return chargingState_;
98 }
99 
GetBatteryLevel()100 BatteryLevel EventsInfo::GetBatteryLevel()
101 {
102 #ifdef CAMERA_USE_BATTERY
103     auto& battery = PowerMgr::BatterySrvClient::GetInstance();
104     auto capacity = battery.GetCapacity();
105     DP_INFO_LOG("GetBatteryLevel: %{public}d", capacity);
106     if (capacity <= BATTERY_THRESHOLD) {
107         batteryLevel_ = BatteryLevel::BATTERY_LEVEL_LOW;
108     } else {
109         batteryLevel_ = BatteryLevel::BATTERY_LEVEL_OKAY;
110     }
111 #endif
112     return batteryLevel_;
113 }
114 
GetThermalLevel()115 ThermalLevel EventsInfo::GetThermalLevel()
116 {
117 #ifdef CAMERA_USE_THERMAL
118     auto& thermal = OHOS::PowerMgr::ThermalMgrClient::GetInstance();
119     thermalLevel_ = static_cast<ThermalLevel>(thermal.GetThermalLevel());
120     DP_INFO_LOG("GetThermalLevel: %{public}d", thermalLevel_);
121 #endif
122     return thermalLevel_;
123 }
124 
GetCameraStatus()125 CameraSessionStatus EventsInfo::GetCameraStatus()
126 {
127     std::lock_guard lock(mutex_);
128     return cameraState_;
129 }
130 
SetCameraState(CameraSessionStatus state)131 void EventsInfo::SetCameraState(CameraSessionStatus state)
132 {
133     std::lock_guard lock(mutex_);
134     cameraState_ = state;
135 }
136 
IsCameraOpen()137 bool EventsInfo::IsCameraOpen()
138 {
139     std::lock_guard lock(mutex_);
140     DP_INFO_LOG("IsCameraOpen: %{public}d", cameraState_);
141     return cameraState_ == CameraSessionStatus::NORMAL_CAMERA_OPEN
142         || cameraState_ == CameraSessionStatus::SYSTEM_CAMERA_OPEN;
143 }
144 } // namespace DeferredProcessing
145 } // namespace CameraStandard
146 } // namespace OHOS