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