1 /*
2 * Copyright (c) 2022-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 "dm_common_event_manager.h"
17
18 #include <pthread.h>
19 #include <thread>
20
21 #include "dm_constants.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "common_event_support.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
28 using OHOS::EventFwk::MatchingSkills;
29 using OHOS::EventFwk::CommonEventManager;
30
31 constexpr const char* DEAL_THREAD = "common_event";
32 constexpr int32_t MAX_TRY_TIMES = 3;
33
GetSubscriberEventNameVec() const34 std::vector<std::string> DmEventSubscriber::GetSubscriberEventNameVec() const
35 {
36 return eventNameVec_;
37 }
38
~DmCommonEventManager()39 DmCommonEventManager::~DmCommonEventManager()
40 {
41 DmCommonEventManager::UnsubscribeServiceEvent();
42 }
43
SubscribeServiceEvent(const std::vector<std::string> & eventNameVec,const CommomEventCallback & callback)44 bool DmCommonEventManager::SubscribeServiceEvent(const std::vector<std::string> &eventNameVec,
45 const CommomEventCallback &callback)
46 {
47 if (eventNameVec.empty() || callback == nullptr) {
48 LOGE("enentNsmr is empty or callback is nullptr.");
49 return false;
50 }
51 std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
52 if (eventValidFlag_) {
53 LOGE("failed to subscribe commom eventName size: %{public}zu", eventNameVec.size());
54 return false;
55 }
56
57 MatchingSkills matchingSkills;
58 for (auto &item : eventNameVec) {
59 matchingSkills.AddEvent(item);
60 }
61 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
62 subscriber_ = std::make_shared<DmEventSubscriber>(subscriberInfo, callback, eventNameVec);
63 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
64 if (samgrProxy == nullptr) {
65 LOGE("samgrProxy is nullptr");
66 subscriber_ = nullptr;
67 return false;
68 }
69 statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_);
70 if (statusChangeListener_ == nullptr) {
71 LOGE("statusChangeListener_ is nullptr");
72 subscriber_ = nullptr;
73 return false;
74 }
75 while (counter_ != MAX_TRY_TIMES) {
76 if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) {
77 LOGI("SubscribeServiceEvent success.");
78 counter_ = 0;
79 break;
80 }
81 if (++counter_ == MAX_TRY_TIMES) {
82 LOGI("SubscribeServiceEvent failed.");
83 }
84 sleep(1);
85 }
86 eventNameVec_ = eventNameVec;
87 eventValidFlag_ = true;
88 LOGI("success to subscribe commom event name size: %{public}zu", eventNameVec.size());
89 return true;
90 }
91
UnsubscribeServiceEvent()92 bool DmCommonEventManager::UnsubscribeServiceEvent()
93 {
94 std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
95 if (!eventValidFlag_) {
96 LOGE("failed to unsubscribe commom event name size: %{public}zu because event is invalid.",
97 eventNameVec_.size());
98 return false;
99 }
100 if (subscriber_ != nullptr) {
101 LOGI("start to unsubscribe commom event name size: %{public}zu", eventNameVec_.size());
102 if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
103 LOGE("failed to unsubscribe commom event name size: %{public}zu", eventNameVec_.size());
104 return false;
105 }
106 LOGI("success to unsubscribe commom event name size: %{public}zu", eventNameVec_.size());
107 subscriber_ = nullptr;
108 }
109 if (statusChangeListener_ != nullptr) {
110 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
111 if (samgrProxy == nullptr) {
112 LOGE("samgrProxy is nullptr");
113 return false;
114 }
115 int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
116 if (ret != ERR_OK) {
117 LOGE("failed to unsubscribe system ability COMMON_EVENT_SERVICE_ID ret:%{public}d", ret);
118 return false;
119 }
120 statusChangeListener_ = nullptr;
121 }
122
123 LOGI("success to unsubscribe commom event name size: %{public}zu", eventNameVec_.size());
124 eventValidFlag_ = false;
125 return true;
126 }
127
OnReceiveEvent(const CommonEventData & data)128 void DmEventSubscriber::OnReceiveEvent(const CommonEventData &data)
129 {
130 std::string receiveEvent = data.GetWant().GetAction();
131 LOGI("Received event: %{public}s", receiveEvent.c_str());
132 int32_t userId = data.GetCode();
133 if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
134 userId = data.GetCode();
135 } else if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT) {
136 userId = data.GetWant().GetIntParam("userId", 0);
137 }
138 if (userId <= 0) {
139 LOGE("userId is less zero");
140 return;
141 }
142 std::thread dealThread([=]() { callback_(userId); });
143 int32_t ret = pthread_setname_np(dealThread.native_handle(), DEAL_THREAD);
144 if (ret != DM_OK) {
145 LOGE("dealThread setname failed.");
146 }
147 dealThread.detach();
148 }
149
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)150 void DmCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
151 int32_t systemAbilityId, const std::string& deviceId)
152 {
153 LOGI("systemAbility is added with said: %{public}d.", systemAbilityId);
154 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
155 return;
156 }
157 if (changeSubscriber_ == nullptr) {
158 LOGE("failed to subscribe commom event because changeSubscriber_ is nullptr.");
159 return;
160 }
161 std::vector<std::string> eventNameVec = changeSubscriber_->GetSubscriberEventNameVec();
162 LOGI("start to subscribe commom eventName: %{public}zu", eventNameVec.size());
163 if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) {
164 LOGE("failed to subscribe commom event: %{public}zu", eventNameVec.size());
165 return;
166 }
167 }
168
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)169 void DmCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
170 int32_t systemAbilityId, const std::string& deviceId)
171 {
172 LOGI("systemAbility is removed with said: %{public}d.", systemAbilityId);
173 }
174 } // namespace DistributedHardware
175 } // namespace OHOS
176