-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path0002-callback.patch
742 lines (729 loc) · 26.2 KB
/
0002-callback.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
From cfc04fdab06a83cae0737b5a1b8ca8f6dfc21fbf Mon Sep 17 00:00:00 2001
From: zhaoshenghua <[email protected]>
Date: Thu, 15 Aug 2024 19:27:45 +0800
Subject: [PATCH 2/4] callback
Change-Id: Ib59d9795eb4d154e06dc2615285cbd86f7683bb7
Signed-off-by: zhaoshenghua <[email protected]>
---
wifi/frameworks/cj/BUILD.gn | 1 +
wifi/frameworks/cj/include/ffi_structs.h | 6 +-
wifi/frameworks/cj/include/wifi_callback.h | 80 +++
wifi/frameworks/cj/include/wifi_ffi.h | 2 +-
wifi/frameworks/cj/src/wifi_callback.cpp | 534 +++++++++++++++++++++
wifi/frameworks/cj/src/wifi_ffi.cpp | 15 +-
6 files changed, 631 insertions(+), 7 deletions(-)
create mode 100644 wifi/frameworks/cj/include/wifi_callback.h
create mode 100644 wifi/frameworks/cj/src/wifi_callback.cpp
diff --git a/wifi/frameworks/cj/BUILD.gn b/wifi/frameworks/cj/BUILD.gn
index 71fed17cc..a83ae447e 100644
--- a/wifi/frameworks/cj/BUILD.gn
+++ b/wifi/frameworks/cj/BUILD.gn
@@ -39,6 +39,7 @@ ohos_shared_library("cj_wifi_ffi") {
sources = [
"src/wifi_ffi.cpp",
+ "src/wifi_callback.cpp",
]
deps = [ "$WIFI_ROOT_DIR/frameworks/native:wifi_sdk" ]
diff --git a/wifi/frameworks/cj/include/ffi_structs.h b/wifi/frameworks/cj/include/ffi_structs.h
index ca174fecf..e1f0d9b23 100644
--- a/wifi/frameworks/cj/include/ffi_structs.h
+++ b/wifi/frameworks/cj/include/ffi_structs.h
@@ -45,14 +45,14 @@ extern "C" {
int32_t supportedWifiCategory;
bool isHiLinkNetwork;
};
-
+
struct WifiScanInfoArr
{
CWifiScanInfo *head;
int64_t size;
};
- // TODO figure out clientCert
+ // TODO figure out clientCertAlias
struct CWifiEapConfig
{
int32_t eapMethod; /* EAP authentication mode:PEAP/TLS/TTLS/PWD/SIM/AKA/AKA' */
@@ -112,7 +112,7 @@ extern "C" {
char *groupName;
int32_t netId;
int32_t goBand;
- int32_t deviceAddressType;
+ int32_t deviceAddressType;
};
struct CWifiP2PLinkedInfo
diff --git a/wifi/frameworks/cj/include/wifi_callback.h b/wifi/frameworks/cj/include/wifi_callback.h
new file mode 100644
index 000000000..7039f9605
--- /dev/null
+++ b/wifi/frameworks/cj/include/wifi_callback.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2024 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <shared_mutex>
+
+#include "ffi_structs.h"
+#include "wifi_errcode.h"
+#include "wifi_p2p.h"
+#include "wifi_hotspot.h"
+#include "wifi_logger.h"
+#include "wifi_sa_event.h"
+
+namespace OHOS::Wifi {
+
+class CjWifiAbilityStatusChange : public WifiAbilityStatusChange {
+public:
+ void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
+};
+
+class CjEventRegister {
+public:
+ CjEventRegister()
+ {
+ int32_t ret;
+ auto samgrProxy = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
+ if (samgrProxy == nullptr) {
+ return;
+ }
+ mSaStatusListener = new OHOS::Wifi::CjWifiAbilityStatusChange();
+ if (mSaStatusListener == nullptr) {
+ return;
+ }
+ ret = samgrProxy->SubscribeSystemAbility((int32_t)WIFI_DEVICE_ABILITY_ID, mSaStatusListener);
+ samgrProxy->SubscribeSystemAbility((int32_t)WIFI_SCAN_ABILITY_ID, mSaStatusListener);
+ samgrProxy->SubscribeSystemAbility((int32_t)WIFI_HOTSPOT_ABILITY_ID, mSaStatusListener);
+ samgrProxy->SubscribeSystemAbility((int32_t)WIFI_P2P_ABILITY_ID, mSaStatusListener);
+ }
+ ~CjEventRegister()
+ {}
+
+ static CjEventRegister& GetInstance();
+
+ int32_t Register(const std::string& type, void (* callback)());
+ int32_t UnRegister(const std::string& type);
+ ErrCode RegisterDeviceEvents(const std::vector<std::string> &event);
+ ErrCode RegisterScanEvents(const std::vector<std::string> &event);
+ ErrCode RegisterHotspotEvents(const std::vector<std::string> &event);
+ ErrCode RegisterP2PEvents(const std::vector<std::string> &event);
+
+private:
+ // std::function<void(int32_t)> wifiStateChange{nullptr};
+ // std::function<void(int32_t)> wifiConnectionChange{nullptr};
+ // std::function<void(int32_t)> wifiRssiChange{nullptr};
+ // std::function<void(int32_t)> wifiScanStateChange{nullptr};
+ // std::function<void(int32_t)> hotspotStateChange{nullptr};
+ // std::function<void(int32_t)> p2pStateChange{nullptr};
+ // std::function<void(CWifiP2PLinkedInfo)> p2pConnectionChange{nullptr};
+ // std::function<void(CWifiP2pDevice)> p2pDeviceChange{nullptr};
+ // std::function<void(WifiP2pDeviceArr)> p2pPeerDeviceChange{nullptr};
+ // std::function<void()> p2pPersistentGroupChange{nullptr};
+ // std::function<void(int32_t)> p2pDiscoveryChange{nullptr};
+ OHOS::sptr<OHOS::ISystemAbilityStatusChange> mSaStatusListener = nullptr;
+};
+
+
+
+
+}
\ No newline at end of file
diff --git a/wifi/frameworks/cj/include/wifi_ffi.h b/wifi/frameworks/cj/include/wifi_ffi.h
index 5525813ed..f7f59dcb9 100644
--- a/wifi/frameworks/cj/include/wifi_ffi.h
+++ b/wifi/frameworks/cj/include/wifi_ffi.h
@@ -72,7 +72,7 @@ FFI_EXPORT int32_t CJ_CreateGroup(CWifiP2PConfig &cfg);
FFI_EXPORT int32_t CJ_GetLinkedInfo(CWifiLinkedInfo &info);
FFI_EXPORT int32_t CJ_AddCandidateConfig(CWifiDeviceConfig cfg, int32_t &ret);
FFI_EXPORT WifiDeviceConfigArr CJ_GetCandidateConfigs(int32_t &code);
-FFI_EXPORT int32_t CJ_WifiOn(char *type, int64_t id);
+FFI_EXPORT int32_t CJ_WifiOn(char *type, void (*callback)());
FFI_EXPORT int32_t CJ_WifiOff(char* type);
}
diff --git a/wifi/frameworks/cj/src/wifi_callback.cpp b/wifi/frameworks/cj/src/wifi_callback.cpp
new file mode 100644
index 000000000..cbd564eb0
--- /dev/null
+++ b/wifi/frameworks/cj/src/wifi_callback.cpp
@@ -0,0 +1,534 @@
+/*
+ * Copyright (c) 2024 Huawei Device Co., Ltd.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "wifi_callback.h"
+
+#include "ffi_structs.h"
+#include "accesstoken_kit.h"
+#include "ipc_skeleton.h"
+#include "wifi_device.h"
+#include "wifi_logger.h"
+#include "wifi_scan.h"
+#include "cj_lambda.h"
+
+DEFINE_WIFILOG_LABEL("CJ_Wifi_Callback");
+
+namespace OHOS::Wifi {
+
+std::shared_ptr<WifiDevice> g_cjWifiStaPtr = WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID);
+std::shared_ptr<WifiScan> g_cjWifiScanPtr = WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID);
+std::shared_ptr<WifiHotspot> g_cjWifiHotspotPtr = WifiHotspot::GetInstance(WIFI_HOTSPOT_ABILITY_ID);
+std::shared_ptr<WifiP2p> g_cjWifiP2pPtr = WifiP2p::GetInstance(WIFI_P2P_ABILITY_ID);
+
+CjEventRegister& CjEventRegister::GetInstance()
+{
+ static CjEventRegister inst;
+ return inst;
+}
+
+class CjWifiDeviceEventCallback : public IWifiDeviceCallBack {
+public:
+ CjWifiDeviceEventCallback() {
+ }
+
+ virtual ~CjWifiDeviceEventCallback() {
+ }
+
+public:
+ void OnWifiStateChanged(int state) override
+ {
+ WIFI_LOGI("OnWifiStateChanged event: %{public}d [0:DISABLING, 1:DISABLED, 2:ENABLING, 3:ENABLED]",
+ state);
+ if (wifiStateChange == nullptr) {
+ WIFI_LOGI("OnWifiStateChanged not registered");
+ return;
+ }
+ if (m_wifiStateConvertMap.find(state) == m_wifiStateConvertMap.end()) {
+ WIFI_LOGW("not find state.");
+ return;
+ }
+ wifiStateChange(m_wifiStateConvertMap[state]);
+ }
+
+ void OnWifiConnectionChanged(int state, const WifiLinkedInfo &info) override
+ {
+ WIFI_LOGI("OnWifiConnectionChanged event: %{public}d [4:CONNECTED, 6:DISCONNECTED, 7:SPECIAL_CONNECT]", state);
+ if (wifiConnectionChange == nullptr) {
+ WIFI_LOGI("OnWifiConnectionChanged not registered");
+ return;
+ }
+ if (m_connectStateConvertMap.find(state) == m_connectStateConvertMap.end()) {
+ WIFI_LOGW("not find connect state.");
+ return;
+ }
+ wifiConnectionChange(m_connectStateConvertMap[state]);
+ }
+
+ void OnWifiRssiChanged(int rssi) override
+ {
+ WIFI_LOGI("OnWifiRssiChanged event: %{public}d", rssi);
+ if (wifiRssiChange == nullptr) {
+ WIFI_LOGI("OnWifiConnectionChanged not registered");
+ return;
+ }
+ wifiRssiChange(rssi);
+ }
+
+ void OnWifiWpsStateChanged(int state, const std::string &pinCode) override
+ {
+ }
+
+ void OnStreamChanged(int direction) override
+ {
+ }
+
+ void OnDeviceConfigChanged(ConfigChange value) override
+ {
+ }
+
+ OHOS::sptr<OHOS::IRemoteObject> AsObject() override
+ {
+ return nullptr;
+ }
+
+ void SetCallback(const std::string &type, void (* callback)(int32_t))
+ {
+ if (type == EVENT_STA_POWER_STATE_CHANGE) {
+ wifiStateChange = CJLambda::Create(callback);
+ }
+ if (type == EVENT_STA_CONN_STATE_CHANGE) {
+ wifiConnectionChange = CJLambda::Create(callback);
+ }
+ if (type == EVENT_STA_RSSI_STATE_CHANGE) {
+ wifiRssiChange = CJLambda::Create(callback);
+ }
+ }
+private:
+
+ std::function<void(int32_t)> wifiStateChange{nullptr};
+ std::function<void(int32_t)> wifiConnectionChange{nullptr};
+ std::function<void(int32_t)> wifiRssiChange{nullptr};
+
+ enum class JsLayerWifiState {
+ DISABLED = 0,
+ ENABLED = 1,
+ ENABLING = 2,
+ DISABLING = 3
+ };
+
+ enum class JsLayerConnectStatus {
+ DISCONNECTED = 0,
+ CONNECTED = 1,
+ SPECIAL_CONNECT = 2,
+ };
+
+ enum class JsLayerStreamDirection {
+ STREAM_DIRECTION_NONE = 0,
+ STREAM_DIRECTION_DOWN = 1,
+ STREAM_DIRECTION_UP = 2,
+ STREAM_DIRECTION_UPDOWN = 3
+ };
+
+ std::map<int, int> m_wifiStateConvertMap = {
+ { static_cast<int>(WifiState::DISABLING), static_cast<int>(JsLayerWifiState::DISABLING) },
+ { static_cast<int>(WifiState::DISABLED), static_cast<int>(JsLayerWifiState::DISABLED) },
+ { static_cast<int>(WifiState::ENABLING), static_cast<int>(JsLayerWifiState::ENABLING) },
+ { static_cast<int>(WifiState::ENABLED), static_cast<int>(JsLayerWifiState::ENABLED) },
+ };
+
+ std::map<int, int> m_connectStateConvertMap = {
+ { static_cast<int>(ConnState::CONNECTED), static_cast<int>(JsLayerConnectStatus::CONNECTED) },
+ { static_cast<int>(ConnState::DISCONNECTED), static_cast<int>(JsLayerConnectStatus::DISCONNECTED) },
+ { static_cast<int>(ConnState::SPECIAL_CONNECT), static_cast<int>(JsLayerConnectStatus::SPECIAL_CONNECT) },
+ };
+
+ std::map<int, int> m_streamDirectionConvertMap = {
+ { static_cast<int>(StreamDirection::STREAM_DIRECTION_NONE),
+ static_cast<int>(JsLayerStreamDirection::STREAM_DIRECTION_NONE) },
+ { static_cast<int>(StreamDirection::STREAM_DIRECTION_DOWN),
+ static_cast<int>(JsLayerStreamDirection::STREAM_DIRECTION_DOWN) },
+ { static_cast<int>(StreamDirection::STREAM_DIRECTION_UP),
+ static_cast<int>(JsLayerStreamDirection::STREAM_DIRECTION_UP) },
+ { static_cast<int>(StreamDirection::STREAM_DIRECTION_UPDOWN),
+ static_cast<int>(JsLayerStreamDirection::STREAM_DIRECTION_UPDOWN) },
+ };
+};
+
+class CjWifiScanEventCallback : public IWifiScanCallback {
+public:
+ CjWifiScanEventCallback() {
+ }
+
+ virtual ~CjWifiScanEventCallback() {
+ }
+
+public:
+ void OnWifiScanStateChanged(int state) override
+ {
+ WIFI_LOGI("scan received state changed event: %{public}d", state);
+ if (wifiScanStateChange == nullptr) {
+ WIFI_LOGI("OnWifiScanStateChanged not registered");
+ return;
+ }
+ wifiScanStateChange(state);
+ }
+
+ OHOS::sptr<OHOS::IRemoteObject> AsObject() override
+ {
+ return nullptr;
+ }
+
+ void SetScanStateChange(void (* callback)(int32_t))
+ {
+ wifiScanStateChange = CJLambda::Create(callback);
+ }
+private:
+ std::function<void(int32_t)> wifiScanStateChange{nullptr};
+};
+
+class CjWifiHotspotEventCallback : public IWifiHotspotCallback {
+public:
+ CjWifiHotspotEventCallback() {
+ }
+
+ virtual ~CjWifiHotspotEventCallback() {
+ }
+
+public:
+ void OnHotspotStateChanged(int state) override
+ {
+ WIFI_LOGI("Hotspot received state changed event: %{public}d", state);
+ if (hotspotStateChange == nullptr) {
+ WIFI_LOGI("OnHotspotStateChanged not registered");
+ return;
+ }
+ if (m_apStateConvertMap.find(state) == m_apStateConvertMap.end()) {
+ return;
+ }
+ hotspotStateChange(m_apStateConvertMap[state]);
+ }
+
+ void OnHotspotStaJoin(const StationInfo &info) override
+ {
+ }
+
+ void OnHotspotStaLeave(const StationInfo &info) override
+ {
+ }
+
+ void SetHotspotStateChanged(void (* callback)(int32_t))
+ {
+ hotspotStateChange = CJLambda::Create(callback);
+ }
+
+ OHOS::sptr<OHOS::IRemoteObject> AsObject() override
+ {
+ return nullptr;
+ }
+private:
+ std::function<void(int32_t)> hotspotStateChange{nullptr};
+
+ enum class JsLayerApState {
+ DISABLED = 0,
+ ENABLED = 1,
+ ENABLING = 2,
+ DISABLING = 3
+ };
+
+ std::map<int, int> m_apStateConvertMap = {
+ { static_cast<int>(ApState::AP_STATE_STARTING), static_cast<int>(JsLayerApState::ENABLING) },
+ { static_cast<int>(ApState::AP_STATE_STARTED), static_cast<int>(JsLayerApState::ENABLED) },
+ { static_cast<int>(ApState::AP_STATE_CLOSING), static_cast<int>(JsLayerApState::DISABLING) },
+ { static_cast<int>(ApState::AP_STATE_CLOSED), static_cast<int>(JsLayerApState::DISABLED) },
+ };
+};
+
+class CjWifiP2pEventCallback : public IWifiP2pCallback {
+public:
+ CjWifiP2pEventCallback() {
+ }
+
+ virtual ~CjWifiP2pEventCallback() {
+ }
+
+public:
+ void OnP2pStateChanged(int state) override
+ {
+ WIFI_LOGI("received p2p state changed event: %{public}d", state);
+ if (p2pStateChange == nullptr) {
+ WIFI_LOGI("OnP2pStateChanged not registered");
+ return;
+ }
+ p2pStateChange(state);
+ }
+
+ void OnP2pPersistentGroupsChanged(void) override
+ {
+ WIFI_LOGI("received persistent group changed event");
+ if (p2pPersistentGroupChange == nullptr) {
+ WIFI_LOGI("OnP2pPersistentGroupsChanged not registered");
+ return;
+ }
+ p2pPersistentGroupChange();
+ }
+
+ void OnP2pThisDeviceChanged(const WifiP2pDevice& device) override
+ {
+ WIFI_LOGI("received this device changed event");
+ if (p2pDeviceChange == nullptr) {
+ WIFI_LOGI("OnP2pThisDeviceChanged not registered");
+ return;
+ }
+ CWifiP2pDevice cdevice;
+ cdevice.deviceName = const_cast<char*>(device.GetDeviceName().c_str());
+ cdevice.deviceAddress = const_cast<char*>(device.GetDeviceAddress().c_str());
+ cdevice.primaryDeviceType = const_cast<char*>(device.GetPrimaryDeviceType().c_str());
+ cdevice.deviceStatus = static_cast<int32_t>(device.GetP2pDeviceStatus());
+ cdevice.groupCapabilities = device.GetGroupCapabilitys();
+ cdevice.deviceAddressType = device.GetDeviceAddressType();
+ p2pDeviceChange(cdevice);
+ }
+
+ void OnP2pPeersChanged(const std::vector<WifiP2pDevice>& devices) override
+ {
+ WIFI_LOGI("received p2p peers changed event, devices count: %{public}d", static_cast<int>(devices.size()));
+ if (p2pPeerDeviceChange == nullptr) {
+ WIFI_LOGI("OnP2pPeersChanged not registered");
+ return;
+ }
+ int64_t size = static_cast<int64_t>(devices.size());
+ if (size <= 0) {
+ return;
+ }
+ CWifiP2pDevice cdevices[size];
+ WifiP2pDeviceArr arr{.head = cdevices, .size = size};
+ uint32_t idx = 0;
+ for (auto& each : devices) {
+ cdevices[idx].deviceName = const_cast<char*>(each.GetDeviceName().c_str());
+ cdevices[idx].deviceAddress = const_cast<char*>(each.GetDeviceAddress().c_str());
+ cdevices[idx].primaryDeviceType = const_cast<char*>(each.GetPrimaryDeviceType().c_str());
+ cdevices[idx].deviceStatus = static_cast<int32_t>(each.GetP2pDeviceStatus());
+ cdevices[idx].groupCapabilities = each.GetGroupCapabilitys();
+ cdevices[idx].deviceAddressType = each.GetDeviceAddressType();
+ idx++;
+ }
+ p2pPeerDeviceChange(arr);
+ }
+
+ void OnP2pServicesChanged(const std::vector<WifiP2pServiceInfo>& srvInfo) override
+ {
+ }
+
+ void OnP2pConnectionChanged(const WifiP2pLinkedInfo& info) override
+ {
+ WIFI_LOGI("received p2p connection changed event, state: %{public}d",
+ static_cast<int>(info.GetConnectState()));
+ if (p2pConnectionChange == nullptr) {
+ WIFI_LOGI("OnP2pConnectionChanged not registered");
+ return;
+ }
+ CWifiP2PLinkedInfo cinfo;
+ cinfo.connectState = static_cast<int32_t>(info.GetConnectState());
+ cinfo.isGroupOwner = info.IsGroupOwner();
+ cinfo.groupOwnerAddr = const_cast<char*>(info.GetGroupOwnerAddress().c_str());
+ p2pConnectionChange(cinfo);
+ }
+
+ void OnP2pDiscoveryChanged(bool isChange) override
+ {
+ WIFI_LOGI("received discovery state changed event");
+ if (p2pDiscoveryChange == nullptr) {
+ WIFI_LOGI("OnP2pDiscoveryChanged not registered");
+ return;
+ }
+ p2pDiscoveryChange(static_cast<int32_t>(isChange));
+ }
+
+ void OnP2pActionResult(P2pActionCallback action, ErrCode code) override
+ {
+ }
+
+ void OnConfigChanged(CfgType type, char* data, int dataLen) override
+ {
+ }
+
+ void OnP2pGcJoinGroup(const OHOS::Wifi::GcInfo &info) override
+ {
+ }
+
+ void OnP2pGcLeaveGroup(const OHOS::Wifi::GcInfo &info) override
+ {
+ }
+
+ void OnP2pPrivatePeersChanged(const std::string &priWfdInfo) override
+ {
+ }
+
+ OHOS::sptr<OHOS::IRemoteObject> AsObject() override
+ {
+ return nullptr;
+ }
+
+ void SetCallback(const std::string &type, void (* callback)())
+ {
+ if (type == EVENT_P2P_STATE_CHANGE) {
+ p2pStateChange = CJLambda::Create(reinterpret_cast<void (*)(int32_t)>(callback));
+ }
+ if (type == EVENT_P2P_PERSISTENT_GROUP_CHANGE) {
+ p2pPersistentGroupChange = CJLambda::Create(callback);
+ }
+ if (type == EVENT_P2P_DEVICE_STATE_CHANGE) {
+ p2pDeviceChange = CJLambda::Create(reinterpret_cast<void (*)(CWifiP2pDevice)>(callback));
+ }
+ if (type == EVENT_P2P_PEER_DEVICE_CHANGE) {
+ p2pPeerDeviceChange = CJLambda::Create(reinterpret_cast<void (*)(WifiP2pDeviceArr)>(callback));
+ }
+ if (type == EVENT_P2P_CONN_STATE_CHANGE) {
+ p2pConnectionChange = CJLambda::Create(reinterpret_cast<void (*)(CWifiP2PLinkedInfo)>(callback));
+ }
+ if (type == EVENT_P2P_DISCOVERY_CHANGE) {
+ p2pDiscoveryChange = CJLambda::Create(reinterpret_cast<void (*)(int32_t)>(callback));
+ }
+ }
+private:
+ std::function<void(int32_t)> p2pStateChange{nullptr};
+ std::function<void(CWifiP2PLinkedInfo)> p2pConnectionChange{nullptr};
+ std::function<void(CWifiP2pDevice)> p2pDeviceChange{nullptr};
+ std::function<void(WifiP2pDeviceArr)> p2pPeerDeviceChange{nullptr};
+ std::function<void()> p2pPersistentGroupChange{nullptr};
+ std::function<void(int32_t)> p2pDiscoveryChange{nullptr};
+};
+
+sptr<CjWifiDeviceEventCallback> cjWifiDeviceCallback =
+ sptr<CjWifiDeviceEventCallback>(new (std::nothrow) CjWifiDeviceEventCallback());
+
+sptr<CjWifiScanEventCallback> cjWifiScanCallback =
+ sptr<CjWifiScanEventCallback>(new (std::nothrow) CjWifiScanEventCallback());
+
+sptr<CjWifiHotspotEventCallback> cjWifiHotspotCallback =
+ sptr<CjWifiHotspotEventCallback>(new (std::nothrow) CjWifiHotspotEventCallback());
+
+sptr<CjWifiP2pEventCallback> cjWifiP2pCallback =
+ sptr<CjWifiP2pEventCallback>(new (std::nothrow) CjWifiP2pEventCallback());
+
+int32_t CjEventRegister::Register(const std::string& type, void (* callback)())
+{
+ WIFI_LOGI("Register event: %{public}s", type.c_str());
+ std::vector<std::string> event = {type};
+
+ if (type == EVENT_STA_POWER_STATE_CHANGE || type == EVENT_STA_CONN_STATE_CHANGE
+ || type == EVENT_STA_RSSI_STATE_CHANGE) {
+ cjWifiDeviceCallback->SetCallback(type, reinterpret_cast<void (*)(int32_t)>(callback));
+ CjEventRegister::GetInstance().RegisterDeviceEvents(event);
+ }
+
+ if (type == EVENT_STA_SCAN_STATE_CHANGE) {
+ cjWifiScanCallback->SetScanStateChange(reinterpret_cast<void (*)(int32_t)>(callback));
+ CjEventRegister::GetInstance().RegisterScanEvents(event);
+ }
+
+ if (type == EVENT_HOTSPOT_STATE_CHANGE) {
+ cjWifiHotspotCallback->SetHotspotStateChanged(reinterpret_cast<void (*)(int32_t)>(callback));
+ CjEventRegister::GetInstance().RegisterHotspotEvents(event);
+ }
+
+ if (type == EVENT_P2P_STATE_CHANGE || type == EVENT_P2P_PERSISTENT_GROUP_CHANGE ||
+ type == EVENT_P2P_DEVICE_STATE_CHANGE || type == EVENT_P2P_PEER_DEVICE_CHANGE ||
+ type == EVENT_P2P_CONN_STATE_CHANGE || type == EVENT_P2P_DISCOVERY_CHANGE) {
+ cjWifiP2pCallback->SetCallback(type, callback);
+ CjEventRegister::GetInstance().RegisterP2PEvents(event);
+ }
+ return WIFI_OPT_SUCCESS;
+}
+
+int32_t CjEventRegister::UnRegister(const std::string& type)
+{
+ return WIFI_OPT_SUCCESS;
+}
+
+ErrCode CjEventRegister::RegisterDeviceEvents(const std::vector<std::string> &event)
+{
+ if (g_cjWifiStaPtr == nullptr) {
+ WIFI_LOGE("Register sta event get instance failed!");
+ return WIFI_OPT_FAILED;
+ }
+ return g_cjWifiStaPtr->RegisterCallBack(cjWifiDeviceCallback, event);
+}
+
+ErrCode CjEventRegister::RegisterScanEvents(const std::vector<std::string> &event)
+{
+ if (g_cjWifiScanPtr == nullptr) {
+ WIFI_LOGE("Register scan event get instance failed!");
+ return WIFI_OPT_FAILED;
+ }
+ return g_cjWifiScanPtr->RegisterCallBack(cjWifiScanCallback, event);
+}
+
+ErrCode CjEventRegister::RegisterHotspotEvents(const std::vector<std::string> &event)
+{
+ if (g_cjWifiHotspotPtr == nullptr) {
+ WIFI_LOGE("Register hotspot event get instance failed!");
+ return WIFI_OPT_FAILED;
+ }
+ return g_cjWifiHotspotPtr->RegisterCallBack(cjWifiHotspotCallback, event);
+}
+
+ErrCode CjEventRegister::RegisterP2PEvents(const std::vector<std::string> &event)
+{
+ if (g_cjWifiP2pPtr == nullptr) {
+ WIFI_LOGE("Register p2p event get instance failed!");
+ return WIFI_OPT_FAILED;
+ }
+ return g_cjWifiP2pPtr->RegisterCallBack(cjWifiP2pCallback, event);
+}
+
+void CjWifiAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
+{
+ WIFI_LOGI("OnAddSystemAbility systemAbilityId:%{public}d", systemAbilityId);
+ std::vector<std::string> event;
+ switch (systemAbilityId) {
+ case WIFI_DEVICE_ABILITY_ID: {
+ event.push_back(EVENT_STA_POWER_STATE_CHANGE);
+ event.push_back(EVENT_STA_CONN_STATE_CHANGE);
+ event.push_back(EVENT_STA_RSSI_STATE_CHANGE);
+ CjEventRegister::GetInstance().RegisterDeviceEvents(event);
+ break;
+ }
+ case WIFI_SCAN_ABILITY_ID: {
+ event.push_back(EVENT_STA_SCAN_STATE_CHANGE);
+ CjEventRegister::GetInstance().RegisterScanEvents(event);
+ break;
+ }
+ case WIFI_HOTSPOT_ABILITY_ID: {
+ event.push_back(EVENT_HOTSPOT_STATE_CHANGE);
+ CjEventRegister::GetInstance().RegisterHotspotEvents(event);
+ break;
+ }
+ case WIFI_P2P_ABILITY_ID: {
+ event.push_back(EVENT_P2P_STATE_CHANGE);
+ event.push_back(EVENT_P2P_PERSISTENT_GROUP_CHANGE);
+ event.push_back(EVENT_P2P_DEVICE_STATE_CHANGE);
+ event.push_back(EVENT_P2P_PEER_DEVICE_CHANGE);
+ event.push_back(EVENT_P2P_CONN_STATE_CHANGE);
+ event.push_back(EVENT_P2P_DISCOVERY_CHANGE);
+ CjEventRegister::GetInstance().RegisterP2PEvents(event);
+ break;
+ }
+ default:
+ WIFI_LOGI("OnAddSystemAbility unhandled sysabilityId:%{public}d", systemAbilityId);
+ return;
+ }
+}
+}
\ No newline at end of file
diff --git a/wifi/frameworks/cj/src/wifi_ffi.cpp b/wifi/frameworks/cj/src/wifi_ffi.cpp
index 2673fee55..3b1346526 100644
--- a/wifi/frameworks/cj/src/wifi_ffi.cpp
+++ b/wifi/frameworks/cj/src/wifi_ffi.cpp
@@ -22,6 +22,7 @@
#include "wifi_p2p.h"
#include "wifi_common_util.h"
#include "wifi_logger.h"
+#include "wifi_callback.h"
DEFINE_WIFILOG_LABEL("CJ_WIFI_FFI");
@@ -721,14 +722,22 @@ WifiDeviceConfigArr CJ_GetCandidateConfigs(int32_t &code)
return arr;
}
-int32_t CJ_WifiOn(char *type, int64_t id)
+int32_t CJ_WifiOn(char *type, void (*callback)())
{
- return 0;
+ std::string eventType(type);
+ if (eventType.empty()) {
+ return WIFI_OPT_FAILED;
+ }
+ return CjEventRegister::GetInstance().Register(eventType, callback);
}
int32_t CJ_WifiOff(char* type)
{
- return 0;
+ std::string eventType(type);
+ if (eventType.empty()) {
+ return WIFI_OPT_FAILED;
+ }
+ return CjEventRegister::GetInstance().UnRegister(eventType);
}
}
--
2.34.1