1 /*
2 * Copyright (C) 2025 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 "wifi_callback_taihe.h"
17
18 namespace OHOS {
19 namespace Wifi {
20 DEFINE_WIFILOG_LABEL("WifiCallbackTaihe");
21 std::vector<::taihe::optional<::taihe::callback<void(double)>>>
22 g_wifiStateChangeVec = {};
23 std::shared_mutex g_wifiStateChangeLock;
24
25 std::vector<::taihe::optional<::taihe::callback<void(double)>>>
26 g_wifiConnectionChangeVec = {};
27 std::shared_mutex g_wifiConnectionChangeLock;
28
29 std::vector<::taihe::optional<::taihe::callback<void(double)>>>
30 g_wifiRssiChangeVec = {};
31 std::shared_mutex g_wifiRssiChangeLock;
32
33 std::vector<::taihe::optional<::taihe::callback<void(double)>>>
34 g_wifiStreamChangeVec = {};
35 std::shared_mutex g_wifiStreamChangeLock;
36
37 std::vector<::taihe::optional<::taihe::callback<void(double)>>>
38 g_wifiDeviceConfigChangeVec = {};
39 std::shared_mutex g_wifiDeviceConfigChangeLock;
40
41 std::vector<::taihe::optional<::taihe::callback<void(double)>>>
42 g_wifiScanStateChangeVec = {};
43 std::shared_mutex g_wifiScanStateChangeLock;
44
45 std::vector<::taihe::optional<::taihe::callback<void(double)>>>
46 g_wifiHotspotStateChangeVec = {};
47 std::shared_mutex g_wifiHotspotStateChangeLock;
48
49 std::vector<::taihe::optional<::taihe::callback<
50 void(::ohos::wifiManager::StationInfo const&)>>>
51 g_wifiHotspotStaJoinVec = {};
52 std::shared_mutex g_wifiHotspotStaJoinLock;
53
54 std::vector<::taihe::optional<::taihe::callback<
55 void(::ohos::wifiManager::StationInfo const&)>>>
56 g_wifiHotspotStaLeaveVec = {};
57 std::shared_mutex g_wifiHotspotStaLeaveLock;
58
59 std::vector<::taihe::optional<::taihe::callback<void(double)>>>
60 g_wifiP2pStateChangeVec = {};
61 std::shared_mutex g_wifiP2pStateChangeLock;
62
63 std::vector<::taihe::optional<::taihe::callback<
64 void(::ohos::wifiManager::WifiP2pLinkedInfo const&)>>>
65 g_wifiP2pConnectionChangeVec = {};
66 std::shared_mutex g_wifiP2pConnectionChangeLock;
67
68 std::vector<::taihe::optional<::taihe::callback<
69 void(::ohos::wifiManager::WifiP2pDevice const&)>>>
70 g_wifiP2pDeviceChangeVec = {};
71 std::shared_mutex g_wifiP2pDeviceChangeLock;
72
73 std::vector<::taihe::optional<::taihe::callback<
74 void(::taihe::array_view<::ohos::wifiManager::WifiP2pDevice>)>>>
75 g_wifiP2pPeerDeviceChangeVec = {};
76 std::shared_mutex g_wifiP2pPeerDeviceChangeLock;
77
78 std::vector<::taihe::optional<::taihe::callback<
79 void(::ohos::wifiManager::UndefinedType const&)>>>
80 g_wifiP2pPersistentGroupChangeVec = {};
81 std::shared_mutex g_wifiP2pPersistentGroupChangeLock;
82
83 std::vector<::taihe::optional<::taihe::callback<void(double)>>>
84 g_wifiP2pDiscoveryChangeVec = {};
85 std::shared_mutex g_wifiP2pDiscoveryChangeLock;
86
87 // device
OnWifiStateChanged(int state)88 void WifiIdlDeviceEventCallback::OnWifiStateChanged(int state)
89 {
90 std::shared_lock<std::shared_mutex> guard(g_wifiStateChangeLock);
91 WIFI_LOGI("OnWifiStateChanged event: %{public}d [0:DISABLING, 1:DISABLED, 2:ENABLING, 3:ENABLED]",
92 state);
93 double result = static_cast<double>(state);
94 for (auto callback : g_wifiStateChangeVec) {
95 (*callback)(result);
96 }
97 }
98
OnWifiConnectionChanged(int state,const WifiLinkedInfo & info)99 void WifiIdlDeviceEventCallback::OnWifiConnectionChanged(int state, const WifiLinkedInfo &info)
100 {
101 std::shared_lock<std::shared_mutex> guard(g_wifiStateChangeLock);
102 WIFI_LOGI("OnWifiConnectionChanged event: %{public}d [4:CONNECTED, 6:DISCONNECTED, 7:SPECIAL_CONNECT]",
103 state);
104 double result = static_cast<double>(state);
105 for (auto callback : g_wifiConnectionChangeVec) {
106 (*callback)(result);
107 }
108 }
109
OnWifiRssiChanged(int rssi)110 void WifiIdlDeviceEventCallback::OnWifiRssiChanged(int rssi)
111 {
112 std::shared_lock<std::shared_mutex> guard(g_wifiRssiChangeLock);
113 WIFI_LOGI("OnWifiRssiChanged event: %{public}d", rssi);
114 double result = static_cast<double>(rssi);
115 for (auto callback : g_wifiRssiChangeVec) {
116 (*callback)(result);
117 }
118 }
119
OnWifiWpsStateChanged(int state,const std::string & pinCode)120 void WifiIdlDeviceEventCallback::OnWifiWpsStateChanged(int state, const std::string &pinCode)
121 {
122 }
123
OnStreamChanged(int direction)124 void WifiIdlDeviceEventCallback::OnStreamChanged(int direction)
125 {
126 std::shared_lock<std::shared_mutex> guard(g_wifiStreamChangeLock);
127 WIFI_LOGD("OnStreamChanged event: %{public}d [0:DATA_NONE, 1:DATA_IN, 2:DATA_OUT, 3:DATA_INOUT]",
128 direction);
129 double result = static_cast<double>(direction);
130 for (auto callback : g_wifiStreamChangeVec) {
131 (*callback)(result);
132 }
133 }
134
OnDeviceConfigChanged(ConfigChange value)135 void WifiIdlDeviceEventCallback::OnDeviceConfigChanged(ConfigChange value)
136 {
137 std::shared_lock<std::shared_mutex> guard(g_wifiDeviceConfigChangeLock);
138 WIFI_LOGI("OnDeviceConfigChanged event: %{public}d", static_cast<int>(value));
139 double result = static_cast<double>(value);
140 for (auto callback : g_wifiDeviceConfigChangeVec) {
141 (*callback)(result);
142 }
143 }
144
AsObject()145 OHOS::sptr<OHOS::IRemoteObject> WifiIdlDeviceEventCallback::AsObject()
146 {
147 return nullptr;
148 }
149
150 // scan
OnWifiScanStateChanged(int state)151 void WifiIdlScanEventCallback::OnWifiScanStateChanged(int state)
152 {
153 std::shared_lock<std::shared_mutex> guard(g_wifiScanStateChangeLock);
154 WIFI_LOGI("scan received state changed event: %{public}d", state);
155 double result = static_cast<double>(state);
156 for (auto callback : g_wifiScanStateChangeVec) {
157 (*callback)(result);
158 }
159 }
160
AsObject()161 OHOS::sptr<OHOS::IRemoteObject> WifiIdlScanEventCallback::AsObject()
162 {
163 return nullptr;
164 }
165
166
167 // hotspot
OnHotspotStateChanged(int state)168 void WifiIdlHotspotEventCallback::OnHotspotStateChanged(int state)
169 {
170 std::shared_lock<std::shared_mutex> guard(g_wifiHotspotStateChangeLock);
171 WIFI_LOGI("Hotspot received state changed event: %{public}d", state);
172 double result = static_cast<double>(state);
173 for (auto callback : g_wifiHotspotStateChangeVec) {
174 (*callback)(result);
175 }
176 }
177
OnHotspotStaJoin(const StationInfo & info)178 void WifiIdlHotspotEventCallback::OnHotspotStaJoin(const StationInfo &info)
179 {
180 // bssid -> macAddress
181 std::shared_lock<std::shared_mutex> guard(g_wifiHotspotStaJoinLock);
182 WIFI_LOGI("Hotspot received sta join event");
183 ::ohos::wifiManager::StationInfo result = {info.bssid};
184 for (auto callback : g_wifiHotspotStaJoinVec) {
185 (*callback)(result);
186 }
187 }
188
OnHotspotStaLeave(const StationInfo & info)189 void WifiIdlHotspotEventCallback::OnHotspotStaLeave(const StationInfo &info)
190 {
191 // bssid -> macAddress
192 std::shared_lock<std::shared_mutex> guard(g_wifiHotspotStaLeaveLock);
193 WIFI_LOGI("Hotspot received sta leave event");
194 ::ohos::wifiManager::StationInfo result = {info.bssid};
195 for (auto callback : g_wifiHotspotStaLeaveVec) {
196 (*callback)(result);
197 }
198 }
199
AsObject()200 OHOS::sptr<OHOS::IRemoteObject> WifiIdlHotspotEventCallback::AsObject()
201 {
202 return nullptr;
203 }
204
205 // p2p
OnP2pStateChanged(int state)206 void WifiIdlP2pEventCallback::OnP2pStateChanged(int state)
207 {
208 std::shared_lock<std::shared_mutex> guard(g_wifiP2pStateChangeLock);
209 WIFI_LOGI("received p2p state changed event: %{public}d", state);
210 double result = static_cast<double>(state);
211 for (auto callback : g_wifiP2pStateChangeVec) {
212 (*callback)(result);
213 }
214 }
215
OnP2pPersistentGroupsChanged(void)216 void WifiIdlP2pEventCallback::OnP2pPersistentGroupsChanged(void)
217 {
218 std::shared_lock<std::shared_mutex> guard(g_wifiP2pPersistentGroupChangeLock);
219 WIFI_LOGI("received persistent group changed event");
220 auto result = ::ohos::wifiManager::UndefinedType::make_undefined();
221 for (auto callback : g_wifiP2pPersistentGroupChangeVec) {
222 (*callback)(result);
223 }
224 }
225
OnP2pThisDeviceChanged(const WifiP2pDevice & device)226 void WifiIdlP2pEventCallback::OnP2pThisDeviceChanged(const WifiP2pDevice& device)
227 {
228 std::shared_lock<std::shared_mutex> guard(g_wifiP2pDeviceChangeLock);
229 WIFI_LOGI("received this device changed event");
230 ::taihe::string_view deviceName = static_cast<::taihe::string_view>(device.GetDeviceName());
231 ::taihe::string_view deviceAddress = static_cast<::taihe::string_view>(device.GetDeviceAddress());
232 ::ohos::wifiManager::DeviceAddressType deviceAddressType =
233 static_cast<::ohos::wifiManager::DeviceAddressType::key_t>(device.GetDeviceAddressType());
234 ::taihe::string_view primaryDeviceType = static_cast<::taihe::string_view>(device.GetPrimaryDeviceType());
235 ::ohos::wifiManager::P2pDeviceStatus deviceStatus =
236 static_cast<::ohos::wifiManager::P2pDeviceStatus::key_t>(device.GetP2pDeviceStatus());
237 int groupCapabilities = static_cast<int>(device.GetGroupCapabilitys());
238 ::ohos::wifiManager::WifiP2pDevice result = {deviceName, deviceAddress, deviceAddressType,
239 primaryDeviceType, deviceStatus, groupCapabilities};
240 for (auto callback : g_wifiP2pDeviceChangeVec) {
241 (*callback)(result);
242 }
243 }
244
OnP2pPeersChanged(const std::vector<WifiP2pDevice> & devices)245 void WifiIdlP2pEventCallback::OnP2pPeersChanged(const std::vector<WifiP2pDevice>& devices)
246 {
247 std::shared_lock<std::shared_mutex> guard(g_wifiP2pPeerDeviceChangeLock);
248 WIFI_LOGI("received p2p peers changed event, devices count: %{public}d", static_cast<int>(devices.size()));
249 std::vector<::ohos::wifiManager::WifiP2pDevice> result;
250 for (WifiP2pDevice device : devices) {
251 ::taihe::string_view deviceName = static_cast<::taihe::string_view>(device.GetDeviceName());
252 ::taihe::string_view deviceAddress = static_cast<::taihe::string_view>(device.GetDeviceAddress());
253 ::ohos::wifiManager::DeviceAddressType deviceAddressType =
254 static_cast<::ohos::wifiManager::DeviceAddressType::key_t>(device.GetDeviceAddressType());
255 ::taihe::string_view primaryDeviceType =
256 static_cast<::taihe::string_view>(device.GetPrimaryDeviceType());
257 ::ohos::wifiManager::P2pDeviceStatus deviceStatus =
258 static_cast<::ohos::wifiManager::P2pDeviceStatus::key_t>(device.GetP2pDeviceStatus());
259 int groupCapabilities = static_cast<int>(device.GetGroupCapabilitys());
260 ::ohos::wifiManager::WifiP2pDevice tmpDevice = {deviceName, deviceAddress, deviceAddressType,
261 primaryDeviceType, deviceStatus, groupCapabilities};
262 result.emplace_back(tmpDevice);
263 }
264 ::taihe::array<::ohos::wifiManager::WifiP2pDevice> finalResult =
265 ::taihe::array<::ohos::wifiManager::WifiP2pDevice>(taihe::copy_data_t{}, result.data(), result.size());
266 for (auto callback : g_wifiP2pPeerDeviceChangeVec) {
267 (*callback)(result);
268 }
269 }
270
OnP2pServicesChanged(const std::vector<WifiP2pServiceInfo> & srvInfo)271 void WifiIdlP2pEventCallback::OnP2pServicesChanged(const std::vector<WifiP2pServiceInfo>& srvInfo)
272 {
273 }
274
OnP2pConnectionChanged(const WifiP2pLinkedInfo & info)275 void WifiIdlP2pEventCallback::OnP2pConnectionChanged(const WifiP2pLinkedInfo& info)
276 {
277 std::shared_lock<std::shared_mutex> guard(g_wifiP2pPeerDeviceChangeLock);
278 WIFI_LOGI("received p2p connection changed event, state: %{public}d",
279 static_cast<int>(info.GetConnectState()));
280 ::ohos::wifiManager::P2pConnectState state =
281 static_cast<::ohos::wifiManager::P2pConnectState::key_t>(info.GetConnectState());
282 ani_boolean isOwner = static_cast<ani_boolean>(info.IsGroupOwner());
283 ::taihe::string_view ownerAddr = static_cast<::taihe::string_view>(info.GetGroupOwnerAddress());
284 ::ohos::wifiManager::WifiP2pLinkedInfo result = {state, isOwner, ownerAddr};
285 for (auto callback : g_wifiP2pConnectionChangeVec) {
286 (*callback)(result);
287 }
288 }
289
OnP2pDiscoveryChanged(bool isChange)290 void WifiIdlP2pEventCallback::OnP2pDiscoveryChanged(bool isChange)
291 {
292 std::shared_lock<std::shared_mutex> guard(g_wifiP2pDiscoveryChangeLock);
293 WIFI_LOGI("received discovery state changed event");
294 double result = static_cast<double>(isChange);
295 for (auto callback : g_wifiP2pDiscoveryChangeVec) {
296 (*callback)(result);
297 }
298 }
299
OnP2pActionResult(P2pActionCallback action,ErrCode code)300 void WifiIdlP2pEventCallback::OnP2pActionResult(P2pActionCallback action, ErrCode code)
301 {
302 }
303
OnConfigChanged(CfgType type,char * data,int dataLen)304 void WifiIdlP2pEventCallback::OnConfigChanged(CfgType type, char* data, int dataLen)
305 {
306 }
307
OnP2pGcJoinGroup(const OHOS::Wifi::GcInfo & info)308 void WifiIdlP2pEventCallback::OnP2pGcJoinGroup(const OHOS::Wifi::GcInfo &info)
309 {
310 WIFI_LOGI("received OnP2pGcJoinGroup event");
311 }
312
OnP2pGcLeaveGroup(const OHOS::Wifi::GcInfo & info)313 void WifiIdlP2pEventCallback::OnP2pGcLeaveGroup(const OHOS::Wifi::GcInfo &info)
314 {
315 WIFI_LOGI("received OnP2pGcLeave event");
316 }
317
OnP2pPrivatePeersChanged(const std::string & priWfdInfo)318 void WifiIdlP2pEventCallback::OnP2pPrivatePeersChanged(const std::string &priWfdInfo)
319 {
320 WIFI_LOGI("received OnP2pPrivatePeersChanged event");
321 }
322
OnP2pChrErrCodeReport(const int errCode)323 void WifiIdlP2pEventCallback::OnP2pChrErrCodeReport(const int errCode)
324 {
325 WIFI_LOGI("received OnP2pChrErrCodeReport event");
326 }
327
AsObject()328 OHOS::sptr<OHOS::IRemoteObject> WifiIdlP2pEventCallback::AsObject()
329 {
330 return nullptr;
331 }
332 } // namespace Wifi
333 } // namespace OHOS