• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
52 
SubscribeSwitchEvent()53 int32_t DeviceSwitchCollect::SubscribeSwitchEvent()
54 {
55     if (switchEventSubscriber_ == nullptr) {
56         HILOGE("DeviceSwitchCollect switchEventSubscriber is nullptr");
57         return ERR_INVALID_VALUE;
58     }
59     return switchEventSubscriber_->SubscribeSwitchEvent();
60 }
61 
CheckSwitchEvent(const OnDemandEvent & onDemandEvent)62 int32_t DeviceSwitchCollect::CheckSwitchEvent(const OnDemandEvent& onDemandEvent)
63 {
64     if (onDemandEvent.eventId != SETTING_SWITCH) {
65         return ERR_INVALID_VALUE;
66     }
67     if (onDemandEvent.name != WIFI_NAME && onDemandEvent.name != BLUETOOTH_NAME) {
68         return ERR_INVALID_VALUE;
69     }
70     return ERR_OK;
71 }
72 
Init(const std::list<SaProfile> & saProfiles)73 void DeviceSwitchCollect::Init(const std::list<SaProfile>& saProfiles)
74 {
75     HILOGI("DeviceSwitchCollect Init called");
76     InitCommonEventSubscriber();
77     for (auto saProfile : saProfiles) {
78         for (auto onDemandEvent : saProfile.startOnDemand.onDemandEvents) {
79             if (CheckSwitchEvent(onDemandEvent) == ERR_OK) {
80                 needListenSwitchEvent_ = true;
81                 return;
82             }
83         }
84         for (auto onDemandEvent : saProfile.stopOnDemand.onDemandEvents) {
85             if (CheckSwitchEvent(onDemandEvent) == ERR_OK) {
86                 needListenSwitchEvent_ = true;
87                 return;
88             }
89         }
90     }
91 }
92 
OnStart()93 int32_t DeviceSwitchCollect::OnStart()
94 {
95     HILOGI("DeviceSwitchCollect OnStart called");
96     if (!needListenSwitchEvent_) {
97         return ERR_OK;
98     }
99     sptr<CesStateListener> cesStateListener = new CesStateListener(this);
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 (switchEventSubscriber_ != nullptr) {
108         switchEventSubscriber_->UnSubscribeSwitchEvent();
109     }
110     return ERR_OK;
111 }
112 
AddCollectEvent(const std::vector<OnDemandEvent> & events)113 int32_t DeviceSwitchCollect::AddCollectEvent(const std::vector<OnDemandEvent>& events)
114 {
115     for (auto& event : events) {
116         if (CheckSwitchEvent(event) != ERR_OK) {
117             HILOGE("DeviceSwitchCollect invalid event name %{public}s!", event.name.c_str());
118             return ERR_INVALID_VALUE;
119         }
120         auto ret = SubscribeSwitchEvent();
121         if (ret != ERR_OK) {
122             HILOGE("DeviceSwitchCollect SubscribeSwitchEvent err:%{public}d", ret);
123             return ret;
124         }
125     }
126     return ERR_OK;
127 }
128 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)129 void CesStateListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
130 {
131     HILOGI("DeviceSwitchCollect OnAddSystemAbility systemAbilityId:%{public}d", systemAbilityId);
132     if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
133         HILOGE("DeviceSwitchCollect OnAddSystemAbility unhandled sysabilityId:%{public}d", systemAbilityId);
134         return;
135     }
136     auto deviceSwitchCollect = deviceSwitchCollect_.promote();
137     if (deviceSwitchCollect == nullptr) {
138         HILOGE("DeviceSwitchCollect switchEventSubscriber is nullptr");
139         return;
140     }
141     auto task = [this] () {
142         auto deviceSwitchCollect = deviceSwitchCollect_.promote();
143         if (deviceSwitchCollect == nullptr) {
144             HILOGE("DeviceSwitchCollect switchEventSubscriber is nullptr");
145             return;
146         }
147         deviceSwitchCollect->SubscribeSwitchEvent();
148     };
149     deviceSwitchCollect->PostDelayTask(task, 0);
150 }
151 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)152 void CesStateListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
153 {
154     HILOGI("DeviceSwitchCollect OnRemoveSystemAbility systemAbilityId:%{public}d", systemAbilityId);
155 }
156 
SubscribeSwitchEvent()157 int32_t SwitchEventSubscriber::SubscribeSwitchEvent()
158 {
159     HILOGI("DeviceSwitchCollect Subscribe Switch State");
160     std::lock_guard<std::mutex> autoLock(isListenEventLock_);
161     if (isListenSwitchEvent_) {
162         return ERR_OK;
163     }
164     if (EventFwk::CommonEventManager::SubscribeCommonEvent(shared_from_this())) {
165         isListenSwitchEvent_ = true;
166         return ERR_OK;
167     }
168     return ERR_INVALID_VALUE;
169 }
170 
UnSubscribeSwitchEvent()171 int32_t SwitchEventSubscriber::UnSubscribeSwitchEvent()
172 {
173     HILOGI("DeviceSwitchCollect UnSubscribe Switch State");
174     std::lock_guard<std::mutex> autoLock(isListenEventLock_);
175     if (!isListenSwitchEvent_) {
176         return ERR_OK;
177     }
178     if (EventFwk::CommonEventManager::UnSubscribeCommonEvent(shared_from_this())) {
179         isListenSwitchEvent_ = false;
180         return ERR_OK;
181     }
182     return ERR_INVALID_VALUE;
183 }
184 
ReportEvent(const OnDemandEvent & event)185 void SwitchEventSubscriber::ReportEvent(const OnDemandEvent& event)
186 {
187     auto deviceSwitchCollect = deviceSwitchCollect_.promote();
188     if (deviceSwitchCollect == nullptr) {
189         HILOGE("DeviceSwitchCollect is nullptr");
190         return;
191     }
192     deviceSwitchCollect->ReportEvent(event);
193 }
194 
OnReceiveWifiEvent(const EventFwk::CommonEventData & data)195 void SwitchEventSubscriber::OnReceiveWifiEvent(const EventFwk::CommonEventData& data)
196 {
197     std::string eventValue;
198     int32_t code = data.GetCode();
199     HILOGI("DeviceSwitchCollect wifi state changed, code %{public}d", code);
200     if (code == WIFI_ON) {
201         eventValue = "on";
202     } else if (code == WIFI_OFF) {
203         eventValue = "off";
204     } else {
205         return;
206     }
207     OnDemandEvent event = {SETTING_SWITCH, WIFI_NAME, eventValue};
208     ReportEvent(event);
209 }
210 
OnReceiveBluetoothEvent(const EventFwk::CommonEventData & data)211 void SwitchEventSubscriber::OnReceiveBluetoothEvent(const EventFwk::CommonEventData& data)
212 {
213     std::string eventValue;
214     int32_t code = data.GetCode();
215     HILOGI("DeviceSwitchCollect bluetooth state changed, code %{public}d", code);
216     if (code == BLUETOOTH_STATE_TURN_ON) {
217         eventValue = "on";
218     } else if (code == BLUETOOTH_STATE_TURN_OFF) {
219         eventValue = "off";
220     } else {
221         return;
222     }
223     OnDemandEvent event = {SETTING_SWITCH, BLUETOOTH_NAME, eventValue};
224     ReportEvent(event);
225 }
226 
OnReceiveEvent(const EventFwk::CommonEventData & data)227 void SwitchEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data)
228 {
229     std::string action = data.GetWant().GetAction();
230     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE) {
231         OnReceiveWifiEvent(data);
232     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE) {
233         OnReceiveBluetoothEvent(data);
234     } else {
235         HILOGE("DeviceSwitchCollect invalid action: %{public}s", action.c_str());
236     }
237 }
238 }