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