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