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