1 /*
2 * Copyright (c) 2023-2024 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_monitor.h"
17
18 #include "dp_log.h"
19 #include "dps_event_report.h"
20 #include "dps.h"
21 #include "event_status_change_command.h"
22 #include "events_info.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace CameraStandard {
28 namespace DeferredProcessing {
EventsMonitor()29 EventsMonitor::EventsMonitor()
30 {
31 DP_DEBUG_LOG("entered.");
32 }
33
~EventsMonitor()34 EventsMonitor::~EventsMonitor()
35 {
36 DP_DEBUG_LOG("entered.");
37 UnSubscribeSystemAbility();
38 }
39
Initialize()40 void EventsMonitor::Initialize()
41 {
42 DP_DEBUG_LOG("entered.");
43 std::lock_guard<std::mutex> lock(mutex_);
44 DP_CHECK_RETURN(initialized_.load());
45 DP_CHECK_RETURN(SubscribeSystemAbility() != DP_OK);
46 initialized_ = true;
47 }
48
RegisterEventsListener(const int32_t userId,const std::vector<EventType> & events,const std::weak_ptr<IEventsListener> & listener)49 void EventsMonitor::RegisterEventsListener(const int32_t userId, const std::vector<EventType>& events,
50 const std::weak_ptr<IEventsListener>& listener)
51 {
52 DP_INFO_LOG("RegisterEventsListener enter.");
53 std::lock_guard<std::mutex> lock(mutex_);
54 std::map<EventType, std::vector<std::weak_ptr<IEventsListener>>> eventListeners_;
55 if (userIdToeventListeners_.count(userId) > 0) {
56 eventListeners_ = userIdToeventListeners_[userId];
57 }
58 for (const auto &event : events) {
59 eventListeners_[event].push_back(listener);
60 }
61 userIdToeventListeners_[userId] = eventListeners_;
62 }
63
NotifyThermalLevel(int32_t level)64 void EventsMonitor::NotifyThermalLevel(int32_t level)
65 {
66 std::lock_guard<std::mutex> lock(mutex_);
67 DP_INFO_LOG("DPS_EVENT: ThermalLevel: %{public}d", level);
68 DP_CHECK_ERROR_RETURN_LOG(!initialized_.load(), "uninitialized events monitor!");
69
70 for (auto it = userIdToeventListeners_.begin(); it != userIdToeventListeners_.end(); ++it) {
71 NotifyObserversUnlocked(it->first, EventType::THERMAL_LEVEL_STATUS_EVENT, level);
72 }
73 }
74
NotifyCameraSessionStatus(const int32_t userId,const std::string & cameraId,bool running,bool isSystemCamera)75 void EventsMonitor::NotifyCameraSessionStatus(const int32_t userId,
76 const std::string& cameraId, bool running, bool isSystemCamera)
77 {
78 std::lock_guard<std::mutex> lock(mutex_);
79 DP_INFO_LOG("DPS_EVENT: userId: %{public}d, cameraId: %{public}s, running: %{public}d, isSystemCamera: %{public}d",
80 userId, cameraId.c_str(), running, isSystemCamera);
81 DP_CHECK_ERROR_RETURN_LOG(!initialized_.load(), "uninitialized events monitor!");
82
83 CameraSessionStatus cameraSessionStatus;
84 running ? numActiveSessions_++ : numActiveSessions_--;
85 DP_INFO_LOG("numActiveSessions: %{public}d", static_cast<int>(numActiveSessions_.load()));
86 bool currSessionRunning = running;
87 if (currSessionRunning) {
88 cameraSessionStatus = isSystemCamera ?
89 CameraSessionStatus::SYSTEM_CAMERA_OPEN :
90 CameraSessionStatus::NORMAL_CAMERA_OPEN;
91 } else {
92 cameraSessionStatus = isSystemCamera ?
93 CameraSessionStatus::SYSTEM_CAMERA_CLOSED :
94 CameraSessionStatus::NORMAL_CAMERA_CLOSED;
95 }
96 NotifyObserversUnlocked(userId, EventType::CAMERA_SESSION_STATUS_EVENT, cameraSessionStatus);
97 auto level = EventsInfo::GetInstance().GetThermalLevel();
98 DPSEventReport::GetInstance().SetTemperatureLevel(static_cast<int>(level));
99 }
100
NotifyMediaLibraryStatus(bool available)101 void EventsMonitor::NotifyMediaLibraryStatus(bool available)
102 {
103 std::lock_guard<std::mutex> lock(mutex_);
104 DP_INFO_LOG("DPS_EVENT: MediaLibraryStatus: %{public}d", available);
105 DP_CHECK_ERROR_RETURN_LOG(!initialized_.load(), "uninitialized events monitor!");
106
107 for (auto it = userIdToeventListeners_.begin(); it != userIdToeventListeners_.end(); ++it) {
108 NotifyObserversUnlocked(it->first, EventType::MEDIA_LIBRARY_STATUS_EVENT, available);
109 }
110 }
111
NotifyImageEnhanceStatus(int32_t status)112 void EventsMonitor::NotifyImageEnhanceStatus(int32_t status)
113 {
114 std::lock_guard<std::mutex> lock(mutex_);
115 DP_INFO_LOG("DPS_EVENT: ImageEnhanceStatus: %{public}d", status);
116 DP_CHECK_ERROR_RETURN_LOG(!initialized_.load(), "uninitialized events monitor!");
117
118 for (auto it = userIdToeventListeners_.begin(); it != userIdToeventListeners_.end(); ++it) {
119 NotifyObserversUnlocked(it->first, EventType::PHOTO_HDI_STATUS_EVENT, status);
120 }
121 }
122
NotifyVideoEnhanceStatus(int32_t status)123 void EventsMonitor::NotifyVideoEnhanceStatus(int32_t status)
124 {
125 std::lock_guard<std::mutex> lock(mutex_);
126 DP_INFO_LOG("DPS_EVENT: VideoEnhanceStatus: %{public}d", status);
127 DP_CHECK_ERROR_RETURN_LOG(!initialized_.load(), "uninitialized events monitor!");
128
129 for (auto it = userIdToeventListeners_.begin(); it != userIdToeventListeners_.end(); ++it) {
130 NotifyObserversUnlocked(it->first, EventType::VIDEO_HDI_STATUS_EVENT, status);
131 }
132 }
133
NotifyScreenStatus(int32_t status)134 void EventsMonitor::NotifyScreenStatus(int32_t status)
135 {
136 std::lock_guard<std::mutex> lock(mutex_);
137 DP_INFO_LOG("DPS_EVENT: ScreenStatus: %{public}d", status);
138 DP_CHECK_ERROR_RETURN_LOG(!initialized_.load(), "uninitialized events monitor!");
139
140 for (auto it = userIdToeventListeners_.begin(); it != userIdToeventListeners_.end(); ++it) {
141 NotifyObserversUnlocked(it->first, EventType::SCREEN_STATUS_EVENT, status);
142 }
143 }
144
NotifyChargingStatus(int32_t status)145 void EventsMonitor::NotifyChargingStatus(int32_t status)
146 {
147 std::lock_guard<std::mutex> lock(mutex_);
148 DP_INFO_LOG("DPS_EVENT: ChargingStatus: %{public}d", status);
149 DP_CHECK_ERROR_RETURN_LOG(!initialized_.load(), "uninitialized events monitor!");
150
151 for (auto it = userIdToeventListeners_.begin(); it != userIdToeventListeners_.end(); ++it) {
152 NotifyObserversUnlocked(it->first, EventType::CHARGING_STATUS_EVENT, status);
153 }
154 }
155
NotifyBatteryStatus(int32_t status)156 void EventsMonitor::NotifyBatteryStatus(int32_t status)
157 {
158 std::lock_guard<std::mutex> lock(mutex_);
159 DP_INFO_LOG("DPS_EVENT: BatteryStatus: %{public}d", status);
160 DP_CHECK_ERROR_RETURN_LOG(!initialized_.load(), "uninitialized events monitor!");
161
162 for (auto it = userIdToeventListeners_.begin(); it != userIdToeventListeners_.end(); ++it) {
163 NotifyObserversUnlocked(it->first, EventType::BATTERY_STATUS_EVENT, status);
164 }
165 }
166
NotifyBatteryLevel(int32_t level)167 void EventsMonitor::NotifyBatteryLevel(int32_t level)
168 {
169 std::lock_guard<std::mutex> lock(mutex_);
170 DP_INFO_LOG("DPS_EVENT: BatteryLevel: %{public}d", level);
171 DP_CHECK_ERROR_RETURN_LOG(!initialized_.load(), "uninitialized events monitor!");
172
173 for (auto it = userIdToeventListeners_.begin(); it != userIdToeventListeners_.end(); ++it) {
174 NotifyObserversUnlocked(it->first, EventType::BATTERY_LEVEL_STATUS_EVENT, level);
175 }
176 }
177
NotifyPhotoProcessSize(int32_t offlineSize,int32_t backSize)178 void EventsMonitor::NotifyPhotoProcessSize(int32_t offlineSize, int32_t backSize)
179 {
180 std::lock_guard<std::mutex> lock(mutex_);
181 DP_INFO_LOG("DPS_EVENT: PhotoProcessSize offline: %{public}d, background: %{public}d", offlineSize, backSize);
182 DP_CHECK_ERROR_RETURN_LOG(!initialized_.load(), "uninitialized events monitor!");
183
184 for (auto it = userIdToeventListeners_.begin(); it != userIdToeventListeners_.end(); ++it) {
185 NotifyObserversUnlocked(it->first, EventType::PHOTO_PROCESS_STATUS_EVENT, offlineSize + backSize);
186 }
187 }
188
NotifyObserversUnlocked(const int32_t userId,EventType event,int32_t value)189 void EventsMonitor::NotifyObserversUnlocked(const int32_t userId, EventType event, int32_t value)
190 {
191 int32_t ret = DPS_SendCommand<EventStatusChangeCommand>(userId, event, value);
192 DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK, "send command fail, ret: %{public}d", ret);
193 }
194
NotifyEventToObervers(const int32_t userId,EventType event,int32_t value)195 void EventsMonitor::NotifyEventToObervers(const int32_t userId, EventType event, int32_t value)
196 {
197 DP_DEBUG_LOG("entered.");
198 auto eventListeners = userIdToeventListeners_.find(userId);
199 if (eventListeners != userIdToeventListeners_.end()) {
200 std::map<EventType, std::vector<std::weak_ptr<IEventsListener>>> eventListenersVect;
201 eventListenersVect = userIdToeventListeners_[userId];
202 auto &observers = eventListenersVect[event];
203 for (auto it = observers.begin(); it != observers.end();) {
204 auto observer = it->lock();
205 if (observer) {
206 observer->OnEventChange(event, value);
207 ++it;
208 } else {
209 it = observers.erase(it);
210 }
211 }
212 }
213 }
214
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)215 void CommonEventListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
216 {
217 std::lock_guard<std::mutex> lock(eventSubscriberMutex_);
218 DP_CHECK_RETURN(eventSubscriber_ != nullptr);
219 DP_DEBUG_LOG("saId: %{public}d", systemAbilityId);
220 eventSubscriber_ = EventSubscriber::Create();
221 DP_CHECK_ERROR_RETURN_LOG(eventSubscriber_ == nullptr, "RegisterEventStatus failed, eventSubscriber is nullptr");
222 eventSubscriber_->Subcribe();
223 }
224
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)225 void CommonEventListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
226 {
227 std::lock_guard<std::mutex> lock(eventSubscriberMutex_);
228 DP_CHECK_RETURN(eventSubscriber_ == nullptr);
229 DP_DEBUG_LOG("saId: %{public}d", systemAbilityId);
230 DP_CHECK_ERROR_RETURN_LOG(eventSubscriber_ == nullptr, "UnregisterEventStatus failed, eventSubscriber is nullptr");
231 eventSubscriber_->UnSubscribe();
232 }
233
SubscribeSystemAbility()234 int32_t EventsMonitor::SubscribeSystemAbility()
235 {
236 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
237 DP_CHECK_ERROR_RETURN_RET_LOG(samgr == nullptr, DP_NULL_POINTER, "failed to get system ability manager");
238
239 ceListener_ = sptr<CommonEventListener>::MakeSptr();
240 DP_CHECK_ERROR_RETURN_RET_LOG(ceListener_ == nullptr, DP_NULL_POINTER, "ceListener is nullptr.");
241
242 int32_t ret = samgr->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, ceListener_);
243 DP_INFO_LOG("SubscribeSystemAbility ret = %{public}d", ret);
244 return ret == DP_OK ? DP_OK : DP_ERR;
245 }
246
UnSubscribeSystemAbility()247 int32_t EventsMonitor::UnSubscribeSystemAbility()
248 {
249 DP_CHECK_RETURN_RET(ceListener_ == nullptr, DP_OK);
250
251 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
252 DP_CHECK_ERROR_RETURN_RET_LOG(samgr == nullptr, DP_NULL_POINTER, "failed to get System ability manager");
253
254 int32_t ret = samgr->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, ceListener_);
255 DP_INFO_LOG("UnSubscribeSystemAbility ret = %{public}d", ret);
256 ceListener_ = nullptr;
257 return ret == DP_OK ? DP_OK : DP_ERR;
258 }
259 } // namespace DeferredProcessing
260 } // namespace CameraStandard
261 } // namespace OHOS
262