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