1 Star 0 Fork 875

shihaojie/communication_wifi_1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
0002-callback.patch 26.16 KB
一键复制 编辑 原始数据 按行查看 历史
zhaoshenghua1 提交于 2024-08-27 10:33 . fix codecheck
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
From cfc04fdab06a83cae0737b5a1b8ca8f6dfc21fbf Mon Sep 17 00:00:00 2001
From: zhaoshenghua <zhaoshenghua1@huawei.com>
Date: Thu, 15 Aug 2024 19:27:45 +0800
Subject: [PATCH 2/4] callback
Change-Id: Ib59d9795eb4d154e06dc2615285cbd86f7683bb7
Signed-off-by: zhaoshenghua <zhaoshenghua1@huawei.com>
---
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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/shj07/communication_wifi_1.git
git@gitee.com:shj07/communication_wifi_1.git
shj07
communication_wifi_1
communication_wifi_1
master

搜索帮助