1 /*
2 * Copyright (c) 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 *e
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 "device_switch_collect.h"
17
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "matching_skills.h"
21 #include "sa_profiles.h"
22 #include "sam_log.h"
23 #include "system_ability_manager.h"
24
25 using namespace std;
26 using namespace OHOS::AppExecFwk;
27
28 namespace OHOS {
29 namespace {
30 const std::string BLUETOOTH_NAME = "bluetooth_status";
31 const std::string WIFI_NAME = "wifi_status";
32 constexpr int32_t WIFI_ON = 3;
33 constexpr int32_t WIFI_OFF = 1;
34 constexpr int32_t BLUETOOTH_STATE_TURN_ON = 1;
35 constexpr int32_t BLUETOOTH_STATE_TURN_OFF = 3;
36 constexpr int32_t COMMON_EVENT_SERVICE_ID = 3299;
37 }
38
DeviceSwitchCollect(const sptr<IReport> & report)39 DeviceSwitchCollect::DeviceSwitchCollect(const sptr<IReport>& report)
40 : ICollectPlugin(report)
41 {
42 }
43
InitCommonEventSubscriber()44 void DeviceSwitchCollect::InitCommonEventSubscriber()
45 {
46 EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
47 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE);
48 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE);
49 EventFwk::CommonEventSubscribeInfo info(skill);
50 switchEventSubscriber_ = std::make_shared<SwitchEventSubscriber>(info, this);
51 cesStateListener_ = new CesStateListener(this);
52 }
53
SubscribeSwitchEvent()54 int32_t DeviceSwitchCollect::SubscribeSwitchEvent()
55 {
56 if (switchEventSubscriber_ == nullptr) {
57 HILOGE("DeviceSwitchCollect switchEventSubscriber is nullptr");
58 return ERR_INVALID_VALUE;
59 }
60 return switchEventSubscriber_->SubscribeSwitchEvent();
61 }
62
CheckSwitchEvent(const OnDemandEvent & onDemandEvent)63 int32_t DeviceSwitchCollect::CheckSwitchEvent(const OnDemandEvent& onDemandEvent)
64 {
65 if (onDemandEvent.eventId != SETTING_SWITCH) {
66 return ERR_INVALID_VALUE;
67 }
68 if (onDemandEvent.name != WIFI_NAME && onDemandEvent.name != BLUETOOTH_NAME) {
69 return ERR_INVALID_VALUE;
70 }
71 return ERR_OK;
72 }
73
Init(const std::list<SaProfile> & saProfiles)74 void DeviceSwitchCollect::Init(const std::list<SaProfile>& saProfiles)
75 {
76 HILOGI("DeviceSwitchCollect Init called");
77 InitCommonEventSubscriber();
78 for (auto saProfile : saProfiles) {
79 for (auto onDemandEvent : saProfile.startOnDemand.onDemandEvents) {
80 if (CheckSwitchEvent(onDemandEvent) == ERR_OK) {
81 needListenSwitchEvent_ = true;
82 return;
83 }
84 }
85 for (auto onDemandEvent : saProfile.stopOnDemand.onDemandEvents) {
86 if (CheckSwitchEvent(onDemandEvent) == ERR_OK) {
87 needListenSwitchEvent_ = true;
88 return;
89 }
90 }
91 }
92 }
93
OnStart()94 int32_t DeviceSwitchCollect::OnStart()
95 {
96 HILOGI("DeviceSwitchCollect OnStart called");
97 if (!needListenSwitchEvent_) {
98 return ERR_OK;
99 }
100 return SystemAbilityManager::GetInstance()->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID,
101 cesStateListener_);
102 }
103
OnStop()104 int32_t DeviceSwitchCollect::OnStop()
105 {
106 HILOGI("DeviceSwitchCollect OnStop called");
107 if (cesStateListener_ != nullptr) {
108 SystemAbilityManager::GetInstance()->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, cesStateListener_);
109 }
110 if (switchEventSubscriber_ != nullptr) {
111 switchEventSubscriber_->UnSubscribeSwitchEvent();
112 }
113 return ERR_OK;
114 }
115
AddCollectEvent(const OnDemandEvent & event)116 int32_t DeviceSwitchCollect::AddCollectEvent(const OnDemandEvent& event)
117 {
118 if (CheckSwitchEvent(event) != ERR_OK) {
119 HILOGE("DeviceSwitchCollect invalid event name %{public}s!", event.name.c_str());
120 return ERR_INVALID_VALUE;
121 }
122 return SubscribeSwitchEvent();
123 }
124
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)125 void CesStateListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
126 {
127 HILOGI("DeviceSwitchCollect OnAddSystemAbility systemAbilityId:%{public}d", systemAbilityId);
128 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
129 HILOGE("DeviceSwitchCollect OnAddSystemAbility unhandled sysabilityId:%{public}d", systemAbilityId);
130 return;
131 }
132 auto deviceSwitchCollect = deviceSwitchCollect_.promote();
133 if (deviceSwitchCollect == nullptr) {
134 HILOGE("DeviceSwitchCollect switchEventSubscriber is nullptr");
135 return;
136 }
137 auto task = [this] () {
138 auto deviceSwitchCollect = deviceSwitchCollect_.promote();
139 if (deviceSwitchCollect == nullptr) {
140 HILOGE("DeviceSwitchCollect switchEventSubscriber is nullptr");
141 return;
142 }
143 deviceSwitchCollect->SubscribeSwitchEvent();
144 };
145 deviceSwitchCollect->PostDelayTask(task, 0);
146 }
147
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)148 void CesStateListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
149 {
150 HILOGI("DeviceSwitchCollect OnRemoveSystemAbility systemAbilityId:%{public}d", systemAbilityId);
151 }
152
SubscribeSwitchEvent()153 int32_t SwitchEventSubscriber::SubscribeSwitchEvent()
154 {
155 HILOGI("DeviceSwitchCollect Subscribe Switch State");
156 std::lock_guard<std::mutex> autoLock(isListenEventLock_);
157 if (isListenSwitchEvent_) {
158 return ERR_OK;
159 }
160 if (EventFwk::CommonEventManager::SubscribeCommonEvent(shared_from_this())) {
161 isListenSwitchEvent_ = true;
162 return ERR_OK;
163 }
164 return ERR_INVALID_VALUE;
165 }
166
UnSubscribeSwitchEvent()167 int32_t SwitchEventSubscriber::UnSubscribeSwitchEvent()
168 {
169 HILOGI("DeviceSwitchCollect UnSubscribe Switch State");
170 std::lock_guard<std::mutex> autoLock(isListenEventLock_);
171 if (!isListenSwitchEvent_) {
172 return ERR_OK;
173 }
174 if (EventFwk::CommonEventManager::UnSubscribeCommonEvent(shared_from_this())) {
175 isListenSwitchEvent_ = false;
176 return ERR_OK;
177 }
178 return ERR_INVALID_VALUE;
179 }
180
ReportEvent(const OnDemandEvent & event)181 void SwitchEventSubscriber::ReportEvent(const OnDemandEvent& event)
182 {
183 auto deviceSwitchCollect = deviceSwitchCollect_.promote();
184 if (deviceSwitchCollect == nullptr) {
185 HILOGE("DeviceSwitchCollect is nullptr");
186 return;
187 }
188 deviceSwitchCollect->ReportEvent(event);
189 }
190
OnReceiveWifiEvent(const EventFwk::CommonEventData & data)191 void SwitchEventSubscriber::OnReceiveWifiEvent(const EventFwk::CommonEventData& data)
192 {
193 std::string eventValue;
194 int32_t code = data.GetCode();
195 HILOGI("DeviceSwitchCollect wifi state changed, code %{public}d", code);
196 if (code == WIFI_ON) {
197 eventValue = "on";
198 } else if (code == WIFI_OFF) {
199 eventValue = "off";
200 } else {
201 return;
202 }
203 OnDemandEvent event = {SETTING_SWITCH, WIFI_NAME, eventValue};
204 ReportEvent(event);
205 }
206
OnReceiveBluetoothEvent(const EventFwk::CommonEventData & data)207 void SwitchEventSubscriber::OnReceiveBluetoothEvent(const EventFwk::CommonEventData& data)
208 {
209 std::string eventValue;
210 int32_t code = data.GetCode();
211 HILOGI("DeviceSwitchCollect bluetooth state changed, code %{public}d", code);
212 if (code == BLUETOOTH_STATE_TURN_ON) {
213 eventValue = "on";
214 } else if (code == BLUETOOTH_STATE_TURN_OFF) {
215 eventValue = "off";
216 } else {
217 return;
218 }
219 OnDemandEvent event = {SETTING_SWITCH, BLUETOOTH_NAME, eventValue};
220 ReportEvent(event);
221 }
222
OnReceiveEvent(const EventFwk::CommonEventData & data)223 void SwitchEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data)
224 {
225 std::string action = data.GetWant().GetAction();
226 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE) {
227 OnReceiveWifiEvent(data);
228 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE) {
229 OnReceiveBluetoothEvent(data);
230 } else {
231 HILOGE("DeviceSwitchCollect invalid action: %{public}s", action.c_str());
232 }
233 }
234 }