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