• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "native_c/wifi_event.h"
17 #include "native_c/wifi_device.h"
18 #include "native_c/wifi_scan_info.h"
19 #include "wifi_logger.h"
20 #include "common_event_manager.h"
21 #include <set>
22 
23 DEFINE_WIFILOG_LABEL("WifiCEvent");
24 
25 const std::string WIFI_USUAL_EVENT_CONN_STATE = "usual.event.wifi.CONN_STATE";
26 const std::string WIFI_USUAL_EVENT_SCAN_STATE = "usual.event.wifi.SCAN_FINISHED";
27 const std::string WIFI_USUAL_EVENT_HOTSPOT_STATE = "usual.event.wifi.HOTSPOT_STATE";
28 const std::string WIFI_USUAL_EVENT_STA_JOIN = "usual.event.wifi.WIFI_HS_STA_JOIN";
29 const std::string WIFI_USUAL_EVENT_STA_LEAVE = "usual.event.wifi.WIFI_HS_STA_LEAVE";
30 
31 using ConnectionChangeCb = void (*)(int, WifiLinkedInfo*);
32 using ScanStateChangeCb = void (*)(int, int);
33 using HotspotStateChangeCb = void (*)(int);
34 using HotspotJoinCb = void (*)(StationInfo*);
35 using HotspotLeaveCb = void (*)(StationInfo*);
36 
37 using namespace OHOS::EventFwk;
38 class EventManager {
39 public:
EventManager()40     EventManager() {
41     }
42 
~EventManager()43     virtual ~EventManager() {
44     }
45 
AddConnectionChangeCb(const ConnectionChangeCb cb)46     bool AddConnectionChangeCb(const ConnectionChangeCb cb) {
47         if (m_setConnectionChangeCb.empty()) {
48             if (!SubscribeServiceEvent(WIFI_USUAL_EVENT_CONN_STATE)) {
49                 return false;
50             }
51         }
52         return m_setConnectionChangeCb.insert(cb).second;
53     }
54 
RemoveConnectionChangeCb(const ConnectionChangeCb cb)55     void RemoveConnectionChangeCb(const ConnectionChangeCb cb) {
56         m_setConnectionChangeCb.erase(cb);
57         if (m_setConnectionChangeCb.empty()) {
58             UnsubscribeServiceEvent(WIFI_USUAL_EVENT_CONN_STATE);
59         }
60     }
61 
GetConnectionChangeCb()62     static std::set<ConnectionChangeCb> GetConnectionChangeCb() {
63         return m_setConnectionChangeCb;
64     }
65 
AddScanStateChangeCb(const ScanStateChangeCb cb)66     bool AddScanStateChangeCb(const ScanStateChangeCb cb) {
67         if (m_setScanStateChangeCb.empty()) {
68             if (!SubscribeServiceEvent(WIFI_USUAL_EVENT_SCAN_STATE)) {
69                 return false;
70             }
71         }
72         return m_setScanStateChangeCb.insert(cb).second;
73     }
74 
RemoveScanStateChangeCb(const ScanStateChangeCb cb)75     void RemoveScanStateChangeCb(const ScanStateChangeCb cb) {
76         m_setScanStateChangeCb.erase(cb);
77         if (m_setScanStateChangeCb.empty()) {
78             UnsubscribeServiceEvent(WIFI_USUAL_EVENT_SCAN_STATE);
79         }
80     }
81 
GetScanStateChangeCb()82     static std::set<ScanStateChangeCb> GetScanStateChangeCb() {
83         return m_setScanStateChangeCb;
84     }
85 
AddHotspotChangeCb(const HotspotStateChangeCb cb)86     bool AddHotspotChangeCb(const HotspotStateChangeCb cb) {
87         if (m_setHotspotChangeCb.empty()) {
88             if (!SubscribeServiceEvent(WIFI_USUAL_EVENT_HOTSPOT_STATE)) {
89                 return false;
90             }
91         }
92         return m_setHotspotChangeCb.insert(cb).second;
93     }
94 
RemoveHotspotChangeCb(const HotspotStateChangeCb cb)95     void RemoveHotspotChangeCb(const HotspotStateChangeCb cb) {
96         m_setHotspotChangeCb.erase(cb);
97         if (m_setHotspotChangeCb.empty()) {
98             UnsubscribeServiceEvent(WIFI_USUAL_EVENT_HOTSPOT_STATE);
99         }
100     }
101 
GetHotspotChangeCb()102     static std::set<HotspotStateChangeCb> GetHotspotChangeCb() {
103         return m_setHotspotChangeCb;
104     }
105 
AddHotspotJoinCb(const HotspotJoinCb cb)106     bool AddHotspotJoinCb(const HotspotJoinCb cb) {
107         if (m_setHotspotJoinCb.empty()) {
108             if (!SubscribeServiceEvent(WIFI_USUAL_EVENT_STA_JOIN)) {
109                 return false;
110             }
111         }
112         return m_setHotspotJoinCb.insert(cb).second;
113     }
114 
RemoveHotspotJoinCb(const HotspotJoinCb cb)115     void RemoveHotspotJoinCb(const HotspotJoinCb cb) {
116         m_setHotspotJoinCb.erase(cb);
117         if (m_setHotspotJoinCb.empty()) {
118             UnsubscribeServiceEvent(WIFI_USUAL_EVENT_STA_JOIN);
119         }
120     }
121 
GetHotspotJoinCb()122     static std::set<HotspotJoinCb> GetHotspotJoinCb() {
123         return m_setHotspotJoinCb;
124     }
125 
AddHotspotLeaveCb(const HotspotLeaveCb cb)126     bool AddHotspotLeaveCb(const HotspotLeaveCb cb) {
127         if (m_setHotspotLeaveCb.empty()) {
128             if (!SubscribeServiceEvent(WIFI_USUAL_EVENT_STA_LEAVE)) {
129                 return false;
130             }
131         }
132         return m_setHotspotLeaveCb.insert(cb).second;
133     }
134 
RemoveHotspotLeaveCb(const HotspotLeaveCb cb)135     void RemoveHotspotLeaveCb(const HotspotLeaveCb cb) {
136         m_setHotspotLeaveCb.erase(cb);
137         if (m_setHotspotLeaveCb.empty()) {
138             UnsubscribeServiceEvent(WIFI_USUAL_EVENT_STA_LEAVE);
139         }
140     }
141 
GetHotspotLeaveCb()142     static std::set<HotspotLeaveCb> GetHotspotLeaveCb() {
143         return m_setHotspotLeaveCb;
144     }
145 
146     class WifiEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
147     public:
WifiEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & subscribeInfo)148         explicit WifiEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo)
149             : CommonEventSubscriber(subscribeInfo) {
150         }
151 
~WifiEventSubscriber()152         virtual ~WifiEventSubscriber() {
153         }
154 
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)155         virtual void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override {
156             std::string event = data.GetWant().GetAction();
157             int code = data.GetCode();
158             WIFI_LOGI("Received event: %{public}s, value: %{public}d", event.c_str(), code);
159             if (event == WIFI_USUAL_EVENT_CONN_STATE && !GetConnectionChangeCb().empty()) {
160                 WifiLinkedInfo linkInfo;
161                 WifiErrorCode ret = GetLinkedInfo(&linkInfo);
162                 if (ret != WIFI_SUCCESS) {
163                     WIFI_LOGE("Received event get linked info failed");
164                     return;
165                 }
166                 for (auto& cb : GetConnectionChangeCb()) {
167                     cb(code, &linkInfo);
168                 }
169             }
170 
171             if (event == WIFI_USUAL_EVENT_SCAN_STATE && !EventManager::m_setScanStateChangeCb.empty()) {
172                 for (auto& cb : EventManager::m_setScanStateChangeCb) {
173                     cb(code, WIFI_SCAN_HOTSPOT_LIMIT);
174                 }
175             }
176 
177             if (event == WIFI_USUAL_EVENT_HOTSPOT_STATE && !EventManager::m_setHotspotChangeCb.empty()) {
178                 for (auto& cb : EventManager::m_setHotspotChangeCb) {
179                     cb(code);
180                 }
181             }
182 
183             if (event == WIFI_USUAL_EVENT_STA_JOIN && !EventManager::m_setHotspotJoinCb.empty()) {
184             }
185 
186             if (event == WIFI_USUAL_EVENT_STA_LEAVE && !EventManager::m_setHotspotLeaveCb.empty()) {
187             }
188         }
189     };
190 
SubscribeServiceEvent(const std::string & event)191     bool SubscribeServiceEvent(const std::string& event) {
192         MatchingSkills matchingSkills;
193         matchingSkills.AddEvent(event);
194         CommonEventSubscribeInfo subscriberInfo(matchingSkills);
195         std::shared_ptr<WifiEventSubscriber> subscriber = std::make_shared<WifiEventSubscriber>(subscriberInfo);
196         WIFI_LOGI("Subscribe event: %{public}s", event.c_str());
197         bool subscribeResult = CommonEventManager::SubscribeCommonEvent(subscriber);
198         if (subscribeResult) {
199             m_mapEventSubscriber[event] = subscriber;
200         } else {
201             WIFI_LOGE("Subscribe service event fail: %{public}s", event.c_str());
202         }
203         return subscribeResult;
204     }
205 
UnsubscribeServiceEvent(const std::string & event)206     bool UnsubscribeServiceEvent(const std::string& event) {
207         std::map<std::string, std::shared_ptr<WifiEventSubscriber>>::iterator iter;
208         iter = m_mapEventSubscriber.find(event);
209         if (iter == m_mapEventSubscriber.end()) {
210             return false;
211         }
212 
213         bool unsubscribeResult = CommonEventManager::SubscribeCommonEvent(iter->second);
214         if (!unsubscribeResult) {
215             WIFI_LOGE("Unsubscribe event fail: %{public}s", event.c_str());
216         }
217         return unsubscribeResult;
218     }
219 
220 private:
221     static std::set<ConnectionChangeCb> m_setConnectionChangeCb;
222     static std::set<ScanStateChangeCb> m_setScanStateChangeCb;
223     static std::set<HotspotStateChangeCb> m_setHotspotChangeCb;
224     static std::set<HotspotJoinCb> m_setHotspotJoinCb;
225     static std::set<HotspotLeaveCb> m_setHotspotLeaveCb;
226     std::map<std::string, std::shared_ptr<WifiEventSubscriber> > m_mapEventSubscriber;
227 };
228 std::set<ConnectionChangeCb> EventManager::m_setConnectionChangeCb;
229 std::set<ScanStateChangeCb> EventManager::m_setScanStateChangeCb;
230 std::set<HotspotStateChangeCb> EventManager::m_setHotspotChangeCb;
231 std::set<HotspotJoinCb> EventManager::m_setHotspotJoinCb;
232 std::set<HotspotLeaveCb> EventManager::m_setHotspotLeaveCb;
233 
234 static EventManager g_eventManager;
235 
RegisterWifiEvent(WifiEvent * event)236 WifiErrorCode RegisterWifiEvent(WifiEvent *event)
237 {
238     WIFI_LOGI("Register wifi event");
239     if (event == nullptr) {
240         return ERROR_WIFI_INVALID_ARGS;
241     }
242 
243     if (event->OnWifiConnectionChanged != nullptr) {
244         return g_eventManager.AddConnectionChangeCb(event->OnWifiConnectionChanged)
245             ? WIFI_SUCCESS : ERROR_WIFI_INVALID_ARGS;
246     }
247     if (event->OnWifiScanStateChanged != nullptr) {
248         return g_eventManager.AddScanStateChangeCb(event->OnWifiScanStateChanged)
249             ? WIFI_SUCCESS : ERROR_WIFI_INVALID_ARGS;
250     }
251     if (event->OnHotspotStateChanged != nullptr) {
252         return g_eventManager.AddHotspotChangeCb(event->OnHotspotStateChanged)
253             ? WIFI_SUCCESS : ERROR_WIFI_INVALID_ARGS;
254     }
255     if (event->OnHotspotStaJoin != nullptr) {
256         return g_eventManager.AddHotspotJoinCb(event->OnHotspotStaJoin)
257             ? WIFI_SUCCESS : ERROR_WIFI_INVALID_ARGS;
258     }
259     if (event->OnHotspotStaLeave != nullptr) {
260         return g_eventManager.AddHotspotLeaveCb(event->OnHotspotStaLeave)
261             ? WIFI_SUCCESS : ERROR_WIFI_INVALID_ARGS;
262     }
263     return ERROR_WIFI_INVALID_ARGS;
264 }
265 
UnRegisterWifiEvent(const WifiEvent * event)266 WifiErrorCode UnRegisterWifiEvent(const WifiEvent *event)
267 {
268     WIFI_LOGI("Unregister wifi event");
269     if (event == nullptr) {
270         return ERROR_WIFI_INVALID_ARGS;
271     }
272 
273     if (event->OnWifiConnectionChanged != nullptr) {
274         g_eventManager.RemoveConnectionChangeCb(event->OnWifiConnectionChanged);
275     }
276     if (event->OnWifiScanStateChanged != nullptr) {
277         g_eventManager.RemoveScanStateChangeCb(event->OnWifiScanStateChanged);
278     }
279     if (event->OnHotspotStateChanged != nullptr) {
280         g_eventManager.RemoveHotspotChangeCb(event->OnHotspotStateChanged);
281     }
282     if (event->OnHotspotStaJoin != nullptr) {
283         g_eventManager.RemoveHotspotJoinCb(event->OnHotspotStaJoin);
284     }
285     if (event->OnHotspotStaLeave != nullptr) {
286         g_eventManager.RemoveHotspotLeaveCb(event->OnHotspotStaLeave);
287     }
288     return WIFI_SUCCESS;
289 }
290