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 "thermal_common_event_receiver.h"
17 #include <common_event_manager.h>
18 #include <common_event_support.h>
19 #include <unistd.h>
20 #include "thermal_common.h"
21 
22 using namespace OHOS::AAFwk;
23 using namespace OHOS::EventFwk;
24 
25 namespace OHOS {
26 namespace PowerMgr {
27 namespace {
28 const int32_t DELAY_TIME = 50000;
29 static const int32_t POWER_UID = 5528;
30 }
~ThermalCommonEventReceiver()31 ThermalCommonEventReceiver::~ThermalCommonEventReceiver()
32 {
33     if (subscriber_ != nullptr) {
34         CommonEventManager::UnSubscribeCommonEvent(subscriber_);
35     }
36 }
37 
AddEvent(std::string eventName,EventHandle callback)38 void ThermalCommonEventReceiver::AddEvent(std::string eventName, EventHandle callback)
39 {
40     THERMAL_HILOGI(COMP_SVC, "add common event: %{public}s", eventName.c_str());
41     InitEventHandles(eventName, callback);
42 }
43 
Register()44 bool ThermalCommonEventReceiver::Register()
45 {
46     return RegisterSubscriber(GetSubscribeInfo());
47 }
48 
InitEventHandles(std::string eventName,EventHandle callback)49 void ThermalCommonEventReceiver::InitEventHandles(std::string eventName, EventHandle callback)
50 {
51     THERMAL_HILOGD(COMP_SVC, "Add Event: %{public}s", eventName.c_str());
52     eventHandles_.insert(std::make_pair(eventName, callback));
53 }
54 
GetSubscribeInfo() const55 sptr<CesInfo> ThermalCommonEventReceiver::GetSubscribeInfo() const
56 {
57     MatchingSkills skill;
58     for (auto &eh : eventHandles_) {
59         skill.AddEvent(eh.first);
60     }
61     sptr<CesInfo> info = new CesInfo(skill);
62     info->SetThreadMode(CesInfo::COMMON);
63     info->SetPublisherUid(POWER_UID);
64     return info;
65 }
66 
RegisterSubscriber(const sptr<CesInfo> & info)67 bool ThermalCommonEventReceiver::RegisterSubscriber(const sptr<CesInfo>& info)
68 {
69     THERMAL_HILOGD(COMP_SVC, "Enter");
70     static const int32_t MAX_RETRY_TIMES = 2;
71 
72     auto succeed = false;
73     std::shared_ptr<Ces> s = std::make_shared<EventSubscriber>(info, eventHandles_);
74     for (int32_t tryTimes = 0; tryTimes < MAX_RETRY_TIMES; tryTimes++) {
75         THERMAL_HILOGI(COMP_SVC, "start subscribe");
76         succeed = CommonEventManager::SubscribeCommonEvent(s);
77         if (succeed) {
78             break;
79         }
80         THERMAL_HILOGE(COMP_SVC, "Sleep for a while and retry to register subscriber");
81         usleep(DELAY_TIME);
82     }
83     if (!succeed) {
84         THERMAL_HILOGE(COMP_SVC, "Failed to register subscriber");
85         return false;
86     }
87     subscriber_ = s;
88     THERMAL_HILOGI(COMP_SVC, "Succeed to register subscriber");
89     return true;
90 }
91 
HandleEventChanged(const CommonEventData & data) const92 void ThermalCommonEventReceiver::HandleEventChanged(const CommonEventData& __attribute__((unused))data) const
93 {
94     THERMAL_HILOGD(COMP_SVC, "Enter");
95 }
96 
HandleEvent(const OHOS::EventFwk::CommonEventData & data)97 void ThermalCommonEventReceiver::EventSubscriber::HandleEvent(const OHOS::EventFwk::CommonEventData &data)
98 {
99     auto action = data.GetWant().GetAction();
100     auto it = eventHandles_.find(action);
101     if (it == eventHandles_.end()) {
102         THERMAL_HILOGE(COMP_SVC, "Ignore event: %{public}s", action.c_str());
103         return;
104     }
105     THERMAL_HILOGD(COMP_SVC, "Handle Event: %{public}s", action.c_str());
106     auto func = it->second;
107     if (func) {
108         it->second(data);
109     }
110 }
111 } // namespace PowerMgr
112 } // namespace OHOS