1 /*
2 * Copyright (c) 2024 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_publish_common_event.h"
17
18 #include <pthread.h>
19 #include <thread>
20 #ifdef SUPPORT_BLUETOOTH
21 #include "bluetooth_def.h"
22 #endif // SUPPORT_BLUETOOTH
23 #include "common_event_support.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26 #ifdef SUPPORT_WIFI
27 #include "wifi_msg.h"
28 #endif // SUPPORT_WIFI
29 #include "dm_log.h"
30 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
31 #include "dm_thread_manager.h"
32 #endif
33
34 namespace OHOS {
35 namespace DistributedHardware {
36 using OHOS::EventFwk::MatchingSkills;
37 using OHOS::EventFwk::CommonEventManager;
38
39 constexpr const char* DEAL_THREAD = "publish_common_event";
40 constexpr int32_t MAX_TRY_TIMES = 3;
41
GetSubscriberEventNameVec() const42 std::vector<std::string> DmPublishEventSubscriber::GetSubscriberEventNameVec() const
43 {
44 return eventNameVec_;
45 }
46
~DmPublishCommonEventManager()47 DmPublishCommonEventManager::~DmPublishCommonEventManager()
48 {
49 DmPublishCommonEventManager::UnsubscribePublishCommonEvent();
50 }
51
SubscribePublishCommonEvent(const std::vector<std::string> & eventNameVec,const PublishEventCallback & callback)52 bool DmPublishCommonEventManager::SubscribePublishCommonEvent(const std::vector<std::string> &eventNameVec,
53 const PublishEventCallback &callback)
54 {
55 if (eventNameVec.empty() || callback == nullptr) {
56 LOGE("eventNameVec is empty or callback is nullptr.");
57 return false;
58 }
59 std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
60 if (eventValidFlag_) {
61 LOGE("failed to subscribe ble/wifi/screen commom eventName size: %{public}zu", eventNameVec.size());
62 return false;
63 }
64
65 MatchingSkills matchingSkills;
66 for (auto &item : eventNameVec) {
67 matchingSkills.AddEvent(item);
68 }
69 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
70 subscriber_ = std::make_shared<DmPublishEventSubscriber>(subscriberInfo, callback, eventNameVec);
71 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
72 if (samgrProxy == nullptr) {
73 LOGE("samgrProxy is nullptr");
74 subscriber_ = nullptr;
75 return false;
76 }
77 statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_);
78 if (statusChangeListener_ == nullptr) {
79 LOGE("statusChangeListener_ is nullptr");
80 subscriber_ = nullptr;
81 return false;
82 }
83 while (counter_ != MAX_TRY_TIMES) {
84 if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) {
85 LOGI("SubscribeServiceEvent success.");
86 counter_ = 0;
87 break;
88 }
89 if (++counter_ == MAX_TRY_TIMES) {
90 LOGI("SubscribeServiceEvent failed.");
91 }
92 sleep(1);
93 }
94 eventNameVec_ = eventNameVec;
95 eventValidFlag_ = true;
96 SetSubscriber(subscriber_);
97 LOGI("success to subscribe ble/wifi/screen commom event name size: %{public}zu", eventNameVec.size());
98 return true;
99 }
100
UnsubscribePublishCommonEvent()101 bool DmPublishCommonEventManager::UnsubscribePublishCommonEvent()
102 {
103 std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
104 if (!eventValidFlag_) {
105 LOGE("failed to unsubscribe ble/wifi/screen commom event name size: %{public}zu because event is invalid.",
106 eventNameVec_.size());
107 return false;
108 }
109 if (subscriber_ != nullptr) {
110 LOGI("start to unsubscribe commom event name size: %{public}zu", eventNameVec_.size());
111 if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
112 LOGE("failed to unsubscribe ble/wifi/screen commom event name size: %{public}zu", eventNameVec_.size());
113 return false;
114 }
115 LOGI("success to unsubscribe ble/wifi/screen commom event name size: %{public}zu", eventNameVec_.size());
116 subscriber_ = nullptr;
117 }
118 if (statusChangeListener_ != nullptr) {
119 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
120 if (samgrProxy == nullptr) {
121 LOGE("samgrProxy is nullptr");
122 return false;
123 }
124 int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
125 if (ret != ERR_OK) {
126 LOGE("failed to unsubscribe system ability COMMON_EVENT_SERVICE_ID ret:%{public}d", ret);
127 return false;
128 }
129 statusChangeListener_ = nullptr;
130 }
131
132 LOGI("success to unsubscribe ble/wifi/screen commom event name size: %{public}zu", eventNameVec_.size());
133 eventValidFlag_ = false;
134 return true;
135 }
136
SetSubscriber(std::shared_ptr<DmPublishEventSubscriber> subscriber)137 void DmPublishCommonEventManager::SetSubscriber(std::shared_ptr<DmPublishEventSubscriber> subscriber)
138 {
139 std::lock_guard<std::mutex> lock(subscriberMutex_);
140 subscriber_ = subscriber;
141 }
142
GetSubscriber()143 std::shared_ptr<DmPublishEventSubscriber> DmPublishCommonEventManager::GetSubscriber()
144 {
145 std::lock_guard<std::mutex> lock(subscriberMutex_);
146 return subscriber_;
147 }
148
SetWifiState(const int32_t wifiState)149 void DmPublishEventSubscriber::SetWifiState(const int32_t wifiState)
150 {
151 std::lock_guard<std::mutex> lock(wifiStateMutex_);
152 wifiState_ = wifiState;
153 }
154
GetWifiState()155 int32_t DmPublishEventSubscriber::GetWifiState()
156 {
157 std::lock_guard<std::mutex> lock(wifiStateMutex_);
158 return wifiState_;
159 }
160
SetBluetoothState(const int32_t bluetoothState)161 void DmPublishEventSubscriber::SetBluetoothState(const int32_t bluetoothState)
162 {
163 std::lock_guard<std::mutex> lock(bluetoothStateMutex_);
164 bluetoothState_ = bluetoothState;
165 }
166
GetBluetoothState()167 int32_t DmPublishEventSubscriber::GetBluetoothState()
168 {
169 std::lock_guard<std::mutex> lock(bluetoothStateMutex_);
170 return bluetoothState_;
171 }
172
SetScreenState(const int32_t screenState)173 void DmPublishEventSubscriber::SetScreenState(const int32_t screenState)
174 {
175 std::lock_guard<std::mutex> lock(screenStateMutex_);
176 screenState_ = screenState;
177 }
178
GetScreenState()179 int32_t DmPublishEventSubscriber::GetScreenState()
180 {
181 std::lock_guard<std::mutex> lock(screenStateMutex_);
182 return screenState_;
183 }
184
SetScreenEventState(const std::string & receiveEvent)185 void DmPublishEventSubscriber::SetScreenEventState(const std::string &receiveEvent)
186 {
187 std::lock_guard<std::mutex> lock(screenStateMutex_);
188 #ifdef SUPPORT_POWER_MANAGER
189 if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
190 screenState_ = DM_SCREEN_ON;
191 } else if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
192 screenState_ = DM_SCREEN_OFF;
193 }
194 #else
195 screenState_ = DM_SCREEN_ON;
196 #endif // SUPPORT_POWER_MANAGER
197 }
198
OnReceiveEvent(const CommonEventData & data)199 void DmPublishEventSubscriber::OnReceiveEvent(const CommonEventData &data)
200 {
201 std::string receiveEvent = data.GetWant().GetAction();
202 int32_t eventState = data.GetCode();
203 LOGI("On Received receiveEvent: %{public}s, eventState: %{public}d", receiveEvent.c_str(), eventState);
204 SetScreenEventState(receiveEvent);
205
206 #ifdef SUPPORT_BLUETOOTH
207 {
208 std::lock_guard<std::mutex> lock(bluetoothStateMutex_);
209 if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE &&
210 eventState == static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON)) {
211 bluetoothState_ = static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON);
212 } else if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE &&
213 eventState == static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF)) {
214 bluetoothState_ = static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF);
215 } else if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE) {
216 LOGI("bluetooth eventState: %{public}d", eventState);
217 return;
218 }
219 }
220 #endif // SUPPORT_BLUETOOTH
221
222 #ifdef SUPPORT_WIFI
223 {
224 std::lock_guard<std::mutex> lock(wifiStateMutex_);
225 if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE &&
226 eventState == static_cast<int32_t>(OHOS::Wifi::WifiState::ENABLED)) {
227 wifiState_ = static_cast<int32_t>(OHOS::Wifi::WifiState::ENABLED);
228 } else if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE &&
229 eventState == static_cast<int32_t>(OHOS::Wifi::WifiState::DISABLED)) {
230 wifiState_ = static_cast<int32_t>(OHOS::Wifi::WifiState::DISABLED);
231 } else if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE) {
232 LOGI("wifi eventState: %{public}d", eventState);
233 return;
234 }
235 }
236 #endif // SUPPORT_WIFI
237
238 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
239 ThreadManager::GetInstance().Submit(DEAL_THREAD, [=]() { callback_(bluetoothState_, wifiState_, screenState_); });
240 #else
241 std::thread dealThread([=]() { callback_(bluetoothState_, wifiState_, screenState_); });
242 int32_t ret = pthread_setname_np(dealThread.native_handle(), DEAL_THREAD);
243 if (ret != DM_OK) {
244 LOGE("dealThread setname failed.");
245 }
246 dealThread.detach();
247 #endif
248 }
249
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)250 void DmPublishCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
251 int32_t systemAbilityId, const std::string& deviceId)
252 {
253 LOGI("systemAbility is added with said: %{public}d.", systemAbilityId);
254 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
255 return;
256 }
257 if (changeSubscriber_ == nullptr) {
258 LOGE("failed to subscribe ble/wifi/screen commom event because changeSubscriber_ is nullptr.");
259 return;
260 }
261 std::vector<std::string> eventNameVec = changeSubscriber_->GetSubscriberEventNameVec();
262 LOGI("start to subscribe ble/wifi/screen commom eventName: %{public}zu", eventNameVec.size());
263 if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) {
264 LOGE("failed to subscribe ble/wifi/screen commom event: %{public}zu", eventNameVec.size());
265 return;
266 }
267 }
268
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)269 void DmPublishCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
270 int32_t systemAbilityId, const std::string& deviceId)
271 {
272 LOGI("systemAbility is removed with said: %{public}d.", systemAbilityId);
273 }
274 } // namespace DistributedHardware
275 } // namespace OHOS
276