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