1 /*
2  * Copyright (C) 2021-2022 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 #ifndef OHOS_WIFI_SA_EVENT_H
17 #define OHOS_WIFI_SA_EVENT_H
18 #include <mutex>
19 #include "wifi_errcode.h"
20 #include "wifi_logger.h"
21 #include "kits/c/wifi_event.h"
22 #include "iremote_broker.h"
23 #include "iremote_object.h"
24 #include "iservice_registry.h"
25 #include "system_ability_status_change_stub.h"
26 #include "i_wifi_device_callback.h"
27 #include "i_wifi_hotspot_callback.h"
28 #include "i_wifi_scan_callback.h"
29 #include "i_wifi_p2p_callback.h"
30 #include "kits/c/wifi_p2p.h"
31 #include "kits/c/wifi_hid2d.h"
32 
33 class WifiCDeviceEventCallback : public OHOS::Wifi::IWifiDeviceCallBack {
34 public:
WifiCDeviceEventCallback()35     WifiCDeviceEventCallback() {
36     }
37 
~WifiCDeviceEventCallback()38     virtual ~WifiCDeviceEventCallback() {
39     }
40 
41 public:
42     void OnWifiStateChanged(int state) override;
43 
44     void OnWifiConnectionChanged(int state, const OHOS::Wifi::WifiLinkedInfo &info) override;
45 
46     void OnWifiRssiChanged(int rssi) override;
47 
48     void OnWifiWpsStateChanged(int state, const std::string &pinCode) override;
49 
50     void OnStreamChanged(int direction) override;
51 
52     void OnDeviceConfigChanged(OHOS::Wifi::ConfigChange value) override;
53 
54     OHOS::sptr<OHOS::IRemoteObject> AsObject() override;
55 
56     static std::vector<std::string> deviceCallbackEvent;
57 };
58 
59 class WifiCScanEventCallback : public OHOS::Wifi::IWifiScanCallback {
60 public:
WifiCScanEventCallback()61     WifiCScanEventCallback() {
62     }
63 
~WifiCScanEventCallback()64     virtual ~WifiCScanEventCallback() {
65     }
66 
67 public:
68     void OnWifiScanStateChanged(int state) override;
69 
70     OHOS::sptr<OHOS::IRemoteObject> AsObject() override;
71 
72     static std::vector<std::string> scanCallbackEvent;
73 };
74 
75 class WifiCHotspotEventCallback : public OHOS::Wifi::IWifiHotspotCallback {
76 public:
WifiCHotspotEventCallback()77     WifiCHotspotEventCallback() {
78     }
79 
~WifiCHotspotEventCallback()80     virtual ~WifiCHotspotEventCallback() {
81     }
82 
83 public:
84     void OnHotspotStateChanged(int state) override;
85 
86     void OnHotspotStaJoin(const OHOS::Wifi::StationInfo &info) override;
87 
88     void OnHotspotStaLeave(const OHOS::Wifi::StationInfo &info) override;
89 
90     OHOS::sptr<OHOS::IRemoteObject> AsObject() override;
91 
92     static std::vector<std::string> hotspotCallbackEvent;
93 };
94 
95 class WifiP2pCEventCallback : public OHOS::Wifi::IWifiP2pCallback {
96 public:
97     void OnP2pStateChanged(int state) override;
98 
99     void OnP2pPersistentGroupsChanged(void) override;
100 
101     void OnP2pThisDeviceChanged(const OHOS::Wifi::WifiP2pDevice &device) override;
102 
103     void OnP2pPeersChanged(const std::vector<OHOS::Wifi::WifiP2pDevice> &devices) override;
104 
105     void OnP2pPrivatePeersChanged(const std::string &priWfdInfo) override;
106 
107     void OnP2pServicesChanged(const std::vector<OHOS::Wifi::WifiP2pServiceInfo> &srvInfo) override;
108 
109     void OnP2pConnectionChanged(const OHOS::Wifi::WifiP2pLinkedInfo &info) override;
110 
111     void OnP2pDiscoveryChanged(bool isChange) override;
112 
113     void OnP2pActionResult(OHOS::Wifi::P2pActionCallback action, OHOS::Wifi::ErrCode code) override;
114 
115     void OnConfigChanged(OHOS::Wifi::CfgType type, char* data, int dataLen) override;
116 
117     void OnP2pGcJoinGroup(const OHOS::Wifi::GcInfo &info) override;
118 
119     void OnP2pGcLeaveGroup(const OHOS::Wifi::GcInfo &info) override;
120 
121     OHOS::sptr<OHOS::IRemoteObject> AsObject() override;
122 
123 public:
WifiP2pCEventCallback()124     WifiP2pCEventCallback()
125     {
126         stateChangeCb = nullptr;
127         groupChangeCb = nullptr;
128         connectionChangeCb = nullptr;
129         peersChangeCb = nullptr;
130         privatepeerChangeCb = nullptr;
131         cfgChangeCallback = nullptr;
132     }
133 
~WifiP2pCEventCallback()134     virtual ~WifiP2pCEventCallback() {
135     }
136 
137 public:
138     P2pStateChangedCallback stateChangeCb;
139     P2pPersistentGroupsChangedCallback groupChangeCb;
140     P2pConnectionChangedCallback connectionChangeCb;
141     P2pPeersChangedCallback peersChangeCb;
142     P2pPrivatePeersChangedCallback privatepeerChangeCb;
143     WifiCfgChangCallback cfgChangeCallback;
144 
145 private:
146     std::mutex p2pCallbackMutex;
147 
148 private:
149     WifiP2pLinkedInfo ConvertP2pLinkedInfo(const OHOS::Wifi::WifiP2pLinkedInfo& linkedInfo);
150 };
151 
152 namespace OHOS {
153 namespace Wifi {
154 
155 class WifiAbilityStatusChange : public SystemAbilityStatusChangeStub {
156 public:
157     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
158     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
159     void Init(int32_t systemAbilityId);
160 };
161 }
162 }
163 
164 class EventManager {
165 public:
166     EventManager();
167 
168     ~EventManager();
169 
170     void RemoveEventCallback(WifiEvent *cb);
171 
172     WifiErrorCode RegisterDeviceEvent(const std::vector<std::string> &event);
173     WifiErrorCode RegisterScanEvent(const std::vector<std::string> &event);
174     WifiErrorCode RegisterHotspotEvent(const std::vector<std::string> &event);
175     WifiErrorCode RegisterP2PEvent(const std::vector<std::string> &event);
176     WifiErrorCode RegisterWifiEvents();
177 
178     bool IsEventRegistered();
179 
180     void SetIsEventRegistrated(bool isEventRegistered);
181 
182     void Init();
183 
184     void SetP2PCallbackEvent(OHOS::sptr<WifiP2pCEventCallback> &sptr, const std::string &eventName);
185 
186     void RemoveP2PCallbackEvent(const std::string &eventName);
187 
188     std::set<std::string>& GetP2PCallbackEvent();
189 
190     OHOS::sptr<WifiP2pCEventCallback> GetP2PCallbackPtr();
191 
192     static EventManager& GetInstance();
193 
194     void SaveWifiCallbackInfo(WifiEvent* event);
195 public:
196     static std::mutex callbackMutex;
197     static WifiEvent g_wifiEvent;
198 private:
199     static bool m_isEventRegistered;
200     std::set<std::string> p2pRegisteredCallbackEvent;
201     OHOS::sptr<WifiP2pCEventCallback> sptrP2PCallback = nullptr;
202     OHOS::sptr<OHOS::ISystemAbilityStatusChange> mSaStatusListener = nullptr;
203 };
204 #endif