• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_error_type.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_ = sptr<SystemAbilityStatusChangeListener>(
70         new SystemAbilityStatusChangeListener(subscriber_));
71     if (statusChangeListener_ == nullptr) {
72         LOGE("statusChangeListener_ is nullptr");
73         subscriber_ = nullptr;
74         return false;
75     }
76     int32_t counter = 0;
77     while (counter <= MAX_TRY_TIMES) {
78         if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) {
79             LOGI("SubscribeServiceEvent success.");
80             break;
81         }
82         if (++counter == MAX_TRY_TIMES) {
83             LOGE("SubscribeServiceEvent failed.");
84         }
85         sleep(1);
86     }
87     eventNameVec_ = eventNameVec;
88     eventValidFlag_ = true;
89     LOGI("success to subscribe commom event name size: %{public}zu", eventNameVec.size());
90     return true;
91 }
92 
UnsubscribeServiceEvent()93 bool DmCommonEventManager::UnsubscribeServiceEvent()
94 {
95     std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
96     if (!eventValidFlag_) {
97         LOGE("failed to unsubscribe commom event name size: %{public}zu because event is invalid.",
98             eventNameVec_.size());
99         return false;
100     }
101     if (subscriber_ != nullptr) {
102         LOGI("start to unsubscribe commom event name size: %{public}zu", eventNameVec_.size());
103         if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
104             LOGE("failed to unsubscribe commom event name size: %{public}zu", eventNameVec_.size());
105             return false;
106         }
107         LOGI("success to unsubscribe commom event name size: %{public}zu", eventNameVec_.size());
108         subscriber_ = nullptr;
109     }
110     if (statusChangeListener_ != nullptr) {
111         auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
112         if (samgrProxy == nullptr) {
113             LOGE("samgrProxy is nullptr");
114             return false;
115         }
116         int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
117         if (ret != ERR_OK) {
118             LOGE("failed to unsubscribe system ability COMMON_EVENT_SERVICE_ID ret:%{public}d", ret);
119             return false;
120         }
121         statusChangeListener_ = nullptr;
122     }
123 
124     LOGI("success to unsubscribe commom event name size: %{public}zu", eventNameVec_.size());
125     eventValidFlag_ = false;
126     return true;
127 }
128 
OnReceiveEvent(const CommonEventData & data)129 void DmEventSubscriber::OnReceiveEvent(const CommonEventData &data)
130 {
131     std::string receiveEvent = data.GetWant().GetAction();
132     LOGI("Received event: %{public}s", receiveEvent.c_str());
133     int32_t userId = data.GetCode();
134     if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
135         userId = data.GetCode();
136     } else if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT) {
137         userId = data.GetWant().GetIntParam("userId", 0);
138     }
139     if (userId <= 0) {
140         LOGE("userId is less zero");
141         return;
142     }
143     std::thread dealThread([=]() { callback_(userId); });
144     int32_t ret = pthread_setname_np(dealThread.native_handle(), DEAL_THREAD);
145     if (ret != DM_OK) {
146         LOGE("dealThread setname failed.");
147     }
148     dealThread.detach();
149 }
150 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)151 void DmCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
152     int32_t systemAbilityId, const std::string& deviceId)
153 {
154     LOGI("systemAbility is added with said: %{public}d.", systemAbilityId);
155     if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
156         return;
157     }
158     if (changeSubscriber_ == nullptr) {
159         LOGE("failed to subscribe commom event because changeSubscriber_ is nullptr.");
160         return;
161     }
162     std::vector<std::string> eventNameVec = changeSubscriber_->GetSubscriberEventNameVec();
163     LOGI("start to subscribe commom eventName: %{public}zu", eventNameVec.size());
164     if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) {
165         LOGE("failed to subscribe commom event: %{public}zu", eventNameVec.size());
166         return;
167     }
168 }
169 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)170 void DmCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
171     int32_t systemAbilityId, const std::string& deviceId)
172 {
173     LOGI("systemAbility is removed with said: %{public}d.", systemAbilityId);
174 }
175 } // namespace DistributedHardware
176 } // namespace OHOS
177