• 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_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_INFO_LOG("entered.");
37     UnSubscribeSystemAbility();
38 }
39 
Initialize()40 void EventsMonitor::Initialize()
41 {
42     DP_DEBUG_LOG("entered.");
43     DP_CHECK_RETURN(initialized_.load());
44     DP_CHECK_RETURN(SubscribeSystemAbility() != DP_OK);
45     initialized_.store(true);
46 }
47 
RegisterEventsListener(const std::vector<EventType> & events,const std::weak_ptr<IEventsListener> & listener)48 void EventsMonitor::RegisterEventsListener(const std::vector<EventType>& events,
49     const std::weak_ptr<IEventsListener>& listener)
50 {
51     DP_INFO_LOG("RegisterEventsListener enter.");
52     std::lock_guard<std::mutex> lock(eventMutex_);
53     for (const auto& event : events) {
54         eventListenerList_[event].push_back(listener);
55     }
56 }
57 
NotifyThermalLevel(int32_t level)58 void EventsMonitor::NotifyThermalLevel(int32_t level)
59 {
60     NotifyObserversUnlocked(EventType::THERMAL_LEVEL_STATUS_EVENT, level);
61 }
62 
NotifyCameraSessionStatus(CameraSessionStatus status)63 void EventsMonitor::NotifyCameraSessionStatus(CameraSessionStatus status)
64 {
65     NotifyObserversUnlocked(EventType::CAMERA_SESSION_STATUS_EVENT, status);
66     auto level = EventsInfo::GetInstance().GetThermalLevel();
67     DPSEventReport::GetInstance().SetTemperatureLevel(static_cast<int>(level));
68 }
69 
NotifyMediaLibraryStatus(bool available)70 void EventsMonitor::NotifyMediaLibraryStatus(bool available)
71 {
72     DP_INFO_LOG("DPS_EVENT: MediaLibraryStatus: %{public}d", available);
73     NotifyObserversUnlocked(EventType::MEDIA_LIBRARY_STATUS_EVENT, available);
74 }
75 
NotifyImageEnhanceStatus(int32_t status)76 void EventsMonitor::NotifyImageEnhanceStatus(int32_t status)
77 {
78     DP_INFO_LOG("DPS_EVENT: ImageEnhanceStatus: %{public}d", status);
79     NotifyObserversUnlocked(EventType::PHOTO_HDI_STATUS_EVENT, status);
80 }
81 
NotifyVideoEnhanceStatus(int32_t status)82 void EventsMonitor::NotifyVideoEnhanceStatus(int32_t status)
83 {
84     DP_INFO_LOG("DPS_EVENT: VideoEnhanceStatus: %{public}d", status);
85     NotifyObserversUnlocked(EventType::VIDEO_HDI_STATUS_EVENT, status);
86 }
87 
NotifyScreenStatus(int32_t status)88 void EventsMonitor::NotifyScreenStatus(int32_t status)
89 {
90     DP_INFO_LOG("DPS_EVENT: ScreenStatus: %{public}d", status);
91     NotifyObserversUnlocked(EventType::SCREEN_STATUS_EVENT, status);
92 }
93 
NotifyChargingStatus(int32_t status)94 void EventsMonitor::NotifyChargingStatus(int32_t status)
95 {
96     NotifyObserversUnlocked(EventType::CHARGING_STATUS_EVENT, status);
97 }
98 
NotifyBatteryStatus(int32_t status)99 void EventsMonitor::NotifyBatteryStatus(int32_t status)
100 {
101     NotifyObserversUnlocked(EventType::BATTERY_STATUS_EVENT, status);
102 }
103 
NotifyBatteryLevel(int32_t level)104 void EventsMonitor::NotifyBatteryLevel(int32_t level)
105 {
106     NotifyObserversUnlocked(EventType::BATTERY_LEVEL_STATUS_EVENT, level);
107 }
108 
NotifyPhotoProcessSize(int32_t offlineSize,int32_t backSize)109 void EventsMonitor::NotifyPhotoProcessSize(int32_t offlineSize, int32_t backSize)
110 {
111     DP_INFO_LOG("DPS_EVENT: offline: %{public}d, background: %{public}d", offlineSize, backSize);
112     NotifyObserversUnlocked(EventType::PHOTO_PROCESS_STATUS_EVENT, offlineSize + backSize);
113 }
114 
NotifyTrailingStatus(int32_t status)115 void EventsMonitor::NotifyTrailingStatus(int32_t status)
116 {
117     DP_INFO_LOG("DPS_EVENT: TrailingStatus: %{public}d", status);
118     NotifyObserversUnlocked(EventType::TRAILING_STATUS_EVENT, status);
119 }
120 
NotifyObserversUnlocked(EventType event,int32_t value)121 void EventsMonitor::NotifyObserversUnlocked(EventType event, int32_t value)
122 {
123     int32_t ret = DPS_SendCommand<EventStatusChangeCommand>(event, value);
124     DP_CHECK_ERROR_PRINT_LOG(ret != DP_OK, "send command fail, ret: %{public}d", ret);
125 }
126 
NotifyEventToObervers(EventType event,int32_t value)127 void EventsMonitor::NotifyEventToObervers(EventType event, int32_t value)
128 {
129     DP_DEBUG_LOG("EventType: %{public}d, value: %{public}d", event, value);
130     auto iter = eventListenerList_.find(event);
131     if (iter == eventListenerList_.end()) {
132         DP_ERR_LOG("unexpect event: %{public}d", event);
133         return;
134     }
135 
136     auto& observers = iter->second;
137     for (auto it = observers.begin(); it != observers.end();) {
138         auto observer = it->lock();
139         if (observer) {
140             observer->OnEventChange(event, value);
141             ++it;
142         } else {
143             it = observers.erase(it);
144         }
145     }
146 }
147 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)148 void CommonEventListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
149 {
150     std::lock_guard<std::mutex> lock(eventSubscriberMutex_);
151     DP_CHECK_RETURN(eventSubscriber_ != nullptr);
152     DP_DEBUG_LOG("saId: %{public}d", systemAbilityId);
153     eventSubscriber_ = EventSubscriber::Create();
154     DP_CHECK_ERROR_RETURN_LOG(eventSubscriber_ == nullptr, "RegisterEventStatus failed, eventSubscriber is nullptr");
155     eventSubscriber_->Subcribe();
156 }
157 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)158 void CommonEventListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
159 {
160     std::lock_guard<std::mutex> lock(eventSubscriberMutex_);
161     DP_CHECK_RETURN(eventSubscriber_ == nullptr);
162     DP_DEBUG_LOG("saId: %{public}d", systemAbilityId);
163     DP_CHECK_ERROR_RETURN_LOG(eventSubscriber_ == nullptr, "UnregisterEventStatus failed, eventSubscriber is nullptr");
164     eventSubscriber_->UnSubscribe();
165 }
166 
SubscribeSystemAbility()167 int32_t EventsMonitor::SubscribeSystemAbility()
168 {
169     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
170     DP_CHECK_ERROR_RETURN_RET_LOG(samgr == nullptr, DP_NULL_POINTER, "failed to get system ability manager");
171 
172     ceListener_ = sptr<CommonEventListener>::MakeSptr();
173     DP_CHECK_ERROR_RETURN_RET_LOG(ceListener_ == nullptr, DP_NULL_POINTER, "ceListener is nullptr.");
174 
175     int32_t ret = samgr->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, ceListener_);
176     DP_INFO_LOG("SubscribeSystemAbility ret = %{public}d", ret);
177     return ret == DP_OK ? DP_OK : DP_ERR;
178 }
179 
UnSubscribeSystemAbility()180 int32_t EventsMonitor::UnSubscribeSystemAbility()
181 {
182     DP_CHECK_RETURN_RET(ceListener_ == nullptr, DP_OK);
183 
184     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
185     DP_CHECK_ERROR_RETURN_RET_LOG(samgr == nullptr, DP_NULL_POINTER, "failed to get System ability manager");
186 
187     int32_t ret = samgr->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, ceListener_);
188     DP_INFO_LOG("UnSubscribeSystemAbility ret = %{public}d", ret);
189     ceListener_ = nullptr;
190     return ret == DP_OK ? DP_OK : DP_ERR;
191 }
192 } // namespace DeferredProcessing
193 } // namespace CameraStandard
194 } // namespace OHOS
195