Skip to content

Commit

Permalink
[NXP] Add --wifi-paf commission in chip-tool and apps of Linux platform
Browse files Browse the repository at this point in the history
	* Need to enable the function by adding "chip_device_config_enable_wifipaf=true"
	* chip-tool: Add wifi-paf option for pairing.
		Example:
		$ chip-tool pairing wifi-paf 1 ap_ssid ap_pwd [setup-pin-code] [discriminator]
	* apps: Add --wifi-paf.
		Example:
		$ chip-all-clusters-app --wifi --wifi-paf
	Note: Need the usd functions of wpa_supplicant to run this option

Signed-off-by: Lo,Chin-Ran <[email protected]>
  • Loading branch information
crlonxp committed Jul 19, 2024
1 parent 2eb95ca commit c8a828a
Show file tree
Hide file tree
Showing 32 changed files with 849 additions and 5 deletions.
13 changes: 13 additions & 0 deletions examples/chip-tool/commands/pairing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ class PairSoftAP : public PairingCommand
{}
};

#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
class PairWiFiPAF : public PairingCommand
{
public:
PairWiFiPAF(CredentialIssuerCommands * credsIssuerConfig) :
PairingCommand("wifi-paf", PairingMode::WiFiPAF, PairingNetworkType::WiFi, credsIssuerConfig)
{}
};
#endif

class PairAlreadyDiscovered : public PairingCommand
{
public:
Expand Down Expand Up @@ -243,6 +253,9 @@ void registerCommandsPairing(Commands & commands, CredentialIssuerCommands * cre
make_unique<PairBleWiFi>(credsIssuerConfig),
make_unique<PairBleThread>(credsIssuerConfig),
make_unique<PairSoftAP>(credsIssuerConfig),
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
make_unique<PairWiFiPAF>(credsIssuerConfig),
#endif
make_unique<PairAlreadyDiscovered>(credsIssuerConfig),
make_unique<PairAlreadyDiscoveredByIndex>(credsIssuerConfig),
make_unique<PairAlreadyDiscoveredByIndexWithWiFi>(credsIssuerConfig),
Expand Down
5 changes: 5 additions & 0 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId)
case PairingMode::SoftAP:
err = Pair(remoteId, PeerAddress::UDP(mRemoteAddr.address, mRemotePort, mRemoteAddr.interfaceId));
break;
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
case PairingMode::WiFiPAF:
err = Pair(remoteId, PeerAddress::WiFiPAF());
break;
#endif
case PairingMode::AlreadyDiscovered:
err = Pair(remoteId, PeerAddress::UDP(mRemoteAddr.address, mRemotePort, mRemoteAddr.interfaceId));
break;
Expand Down
10 changes: 10 additions & 0 deletions examples/chip-tool/commands/pairing/PairingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ enum class PairingMode
CodePaseOnly,
Ble,
SoftAP,
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
WiFiPAF,
#endif
AlreadyDiscovered,
AlreadyDiscoveredByIndex,
AlreadyDiscoveredByIndexWithCode,
Expand Down Expand Up @@ -127,6 +130,13 @@ class PairingCommand : public CHIPCommand,
AddArgument("device-remote-port", 0, UINT16_MAX, &mRemotePort);
AddArgument("pase-only", 0, 1, &mPaseOnly);
break;
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
case PairingMode::WiFiPAF:
AddArgument("skip-commissioning-complete", 0, 1, &mSkipCommissioningComplete);
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode);
AddArgument("discriminator", 0, 4096, &mDiscriminator);
break;
#endif
case PairingMode::AlreadyDiscovered:
AddArgument("skip-commissioning-complete", 0, 1, &mSkipCommissioningComplete);
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode.emplace());
Expand Down
13 changes: 13 additions & 0 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ int ChipLinuxAppInit(int argc, char * const argv[], OptionSet * customOptions,
CHIP_ERROR err = CHIP_NO_ERROR;
#if CONFIG_NETWORK_LAYER_BLE
RendezvousInformationFlags rendezvousFlags = RendezvousInformationFlag::kBLE;
#elif CHIP_DEVICE_CONFIG_ENABLE_WPA && CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
RendezvousInformationFlags rendezvousFlags = RendezvousInformationFlag::kWiFiPAF;
#else // CONFIG_NETWORK_LAYER_BLE
RendezvousInformationFlag rendezvousFlags = RendezvousInformationFlag::kOnNetwork;
#endif // CONFIG_NETWORK_LAYER_BLE
Expand Down Expand Up @@ -465,6 +467,17 @@ int ChipLinuxAppInit(int argc, char * const argv[], OptionSet * customOptions,
}
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WPA
#if CHIP_DEVICE_CONFIG_ENABLE_WPA && CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
ChipLogProgress(NotSpecified, "WiFi-PAF: initialzing");
if (LinuxDeviceOptions::GetInstance().mWiFi)
{
if (EnsureWiFiIsStarted())
{
ChipLogProgress(NotSpecified, "Wi-Fi Management started");
DeviceLayer::ConnectivityMgr().SetWiFiPAFAdvertisingEnabled(LinuxDeviceOptions::GetInstance().mWiFiPAF);
}
}
#endif

#if CHIP_ENABLE_OPENTHREAD
if (LinuxDeviceOptions::GetInstance().mThread)
Expand Down
17 changes: 17 additions & 0 deletions examples/platform/linux/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ enum
#if CHIP_WITH_NLFAULTINJECTION
kDeviceOption_FaultInjection,
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
kDeviceOption_WiFi_PAF = 0x1028,
#endif
};

constexpr unsigned kAppUsageLength = 64;
Expand All @@ -117,6 +120,9 @@ OptionDef sDeviceOptionDefs[] = {
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
{ "wifi", kNoArgument, kDeviceOption_WiFi },
{ "wifi-supports-5g", kNoArgument, kDeviceOption_WiFiSupports5g },
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
{ "wifi-paf", kNoArgument, kDeviceOption_WiFi_PAF },
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
#endif // CHIP_DEVICE_CONFIG_ENABLE_WPA
#if CHIP_ENABLE_OPENTHREAD
{ "thread", kNoArgument, kDeviceOption_Thread },
Expand Down Expand Up @@ -189,6 +195,11 @@ const char * sDeviceOptionHelp =
" --wifi-supports-5g\n"
" Indicate that local Wi-Fi hardware should report 5GHz support.\n"
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
#if (CHIP_DEVICE_CONFIG_ENABLE_WIFI && CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF)
"\n"
" --wifi-paf\n"
" Enable Wi-Fi PAF via wpa_supplicant.\n"
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI && CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
#if CHIP_ENABLE_OPENTHREAD
"\n"
" --thread\n"
Expand Down Expand Up @@ -588,6 +599,12 @@ bool HandleOption(const char * aProgram, OptionSet * aOptions, int aIdentifier,
}
break;
}
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
case kDeviceOption_WiFi_PAF: {
LinuxDeviceOptions::GetInstance().mWiFiPAF = true;
break;
}
#endif
default:
PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", aProgram, aName);
Expand Down
3 changes: 3 additions & 0 deletions examples/platform/linux/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ struct LinuxDeviceOptions
bool wifiSupports5g = false;
bool mWiFi = false;
bool mThread = false;
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
bool mWiFiPAF = false;
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE || CHIP_DEVICE_ENABLE_PORT_PARAMS
uint16_t securedDevicePort = CHIP_PORT;
uint16_t unsecuredCommissionerPort = CHIP_UDC_PORT;
Expand Down
8 changes: 8 additions & 0 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
#include <system/TLVPacketBufferBackingStore.h>
#include <transport/SessionManager.h>

#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
#include <transport/raw/WiFiPAF.h>
#endif

#if defined(CHIP_SUPPORT_ENABLE_STORAGE_API_AUDIT) || defined(CHIP_SUPPORT_ENABLE_STORAGE_LOAD_TEST_AUDIT)
#include <lib/support/PersistentStorageAudit.h>
#endif // defined(CHIP_SUPPORT_ENABLE_STORAGE_API_AUDIT) || defined(CHIP_SUPPORT_ENABLE_STORAGE_LOAD_TEST_AUDIT)
Expand Down Expand Up @@ -214,6 +218,10 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams)
TcpListenParameters(DeviceLayer::TCPEndPointManager())
.SetAddressType(IPAddressType::kIPv6)
.SetListenPort(mOperationalServicePort)
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
,
Transport::WiFiPAFListenParameters(DeviceLayer::ConnectivityMgr().GetWiFiPAF())
#endif
);

Expand Down
7 changes: 7 additions & 0 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
#if CONFIG_NETWORK_LAYER_BLE
#include <transport/raw/BLE.h>
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
#include <transport/raw/WiFiPAF.h>
#endif
#include <app/TimerDelegates.h>
#include <app/reporting/ReportSchedulerImpl.h>
#include <transport/raw/UDP.h>
Expand Down Expand Up @@ -99,6 +102,10 @@ using ServerTransportMgr = chip::TransportMgr<chip::Transport::UDP
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
,
chip::Transport::TCP<kMaxTcpActiveConnectionCount, kMaxTcpPendingPackets>
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
,
chip::Transport::WiFiPAFBase
#endif
>;

Expand Down
9 changes: 7 additions & 2 deletions src/controller/AutoCommissioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,13 @@ CHIP_ERROR AutoCommissioner::StartCommissioning(DeviceCommissioner * commissione
mCommissioner = commissioner;
mCommissioneeDeviceProxy = proxy;
mNeedsNetworkSetup =
mCommissioneeDeviceProxy->GetSecureSession().Value()->AsSecureSession()->GetPeerAddress().GetTransportType() ==
Transport::Type::kBle;
(mCommissioneeDeviceProxy->GetSecureSession().Value()->AsSecureSession()->GetPeerAddress().GetTransportType() ==
Transport::Type::kBle)
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
|| (mCommissioneeDeviceProxy->GetSecureSession().Value()->AsSecureSession()->GetPeerAddress().GetTransportType() ==
Transport::Type::kWiFiPAF)
#endif
;
CHIP_ERROR err = CHIP_NO_ERROR;
CommissioningStage nextStage = GetNextCommissioningStage(CommissioningStage::kSecurePairing, err);
mCommissioner->PerformCommissioningStep(mCommissioneeDeviceProxy, nextStage, mParams, this, GetEndpoint(nextStage),
Expand Down
81 changes: 81 additions & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
#include <ble/Ble.h>
#include <transport/raw/BLE.h>
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
#include <transport/raw/WiFiPAF.h>
#endif


#include <errno.h>
#include <inttypes.h>
Expand Down Expand Up @@ -730,6 +734,12 @@ CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, Re
peerAddress = Transport::PeerAddress::UDP(params.GetPeerAddress().GetIPAddress(), params.GetPeerAddress().GetPort(),
params.GetPeerAddress().GetInterface());
}
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
else if (params.GetPeerAddress().GetTransportType() == Transport::Type::kWiFiPAF)
{
peerAddress = Transport::PeerAddress::WiFiPAF();
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF

current = FindCommissioneeDevice(peerAddress);
if (current != nullptr)
Expand Down Expand Up @@ -804,6 +814,39 @@ CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, Re
ExitNow(err = CHIP_ERROR_INVALID_ARGUMENT);
}
}
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
if (params.GetPeerAddress().GetTransportType() == Transport::Type::kWiFiPAF)
{
if (DeviceLayer::ConnectivityMgr().GetWiFiPAF()->GetWiFiPAFState() != Transport::WiFiPAFBase::State::kConnected) {
ChipLogProgress(Controller, "WiFi-PAF: Subscribing the NAN-USD devices");
static constexpr useconds_t kWiFiStartCheckTimeUsec = WIFI_START_CHECK_TIME_USEC;
static constexpr uint8_t kWiFiStartCheckAttempts = WIFI_START_CHECK_ATTEMPTS;

DeviceLayer::ConnectivityMgrImpl().StartWiFiManagement();
{
for (int cnt = 0; cnt < kWiFiStartCheckAttempts; cnt++)
{
if (DeviceLayer::ConnectivityMgrImpl().IsWiFiManagementStarted())
{
break;
}
usleep(kWiFiStartCheckTimeUsec);
}
}
if (!DeviceLayer::ConnectivityMgrImpl().IsWiFiManagementStarted()) {
ChipLogError(NotSpecified, "Wi-Fi Management taking too long to start - device configuration will be reset.");
}
mRendezvousParametersForDeviceDiscoveredOverWiFiPAF = params;
DeviceLayer::ConnectivityMgr().WiFiPAFConnect(
this,
OnWiFiPAFSubscribeComplete,
OnWiFiPAFSubscribeError
);
ExitNow(CHIP_NO_ERROR);
}
ChipLogProgress(Controller, "WiFi-PAF: Request to subscrib the NAN-USD device complete");
}
#endif
session = mSystemState->SessionMgr()->CreateUnauthenticatedSession(params.GetPeerAddress(), params.GetMRPConfig());
VerifyOrExit(session.HasValue(), err = CHIP_ERROR_NO_MEMORY);
Expand Down Expand Up @@ -872,6 +915,44 @@ void DeviceCommissioner::OnDiscoveredDeviceOverBleError(void * appState, CHIP_ER
}
#endif // CONFIG_NETWORK_LAYER_BLE

#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
void DeviceCommissioner::OnWiFiPAFSubscribeComplete(void * appState)
{
auto self = static_cast<DeviceCommissioner *>(appState);
auto device = self->mDeviceInPASEEstablishment;

ChipLogProgress(Controller, "WiFi-PAF: Subscription Completed!");
if (nullptr != device && device->GetDeviceTransportType() == Transport::Type::kWiFiPAF)
{
auto remoteId = device->GetDeviceId();
auto params = self->mRendezvousParametersForDeviceDiscoveredOverWiFiPAF;

self->mRendezvousParametersForDeviceDiscoveredOverWiFiPAF = RendezvousParameters();
self->ReleaseCommissioneeDevice(device);
DeviceLayer::ConnectivityMgr().GetWiFiPAF()->SetWiFiPAFState(Transport::WiFiPAFBase::State::kConnected);
chip::DeviceLayer::StackLock stackLock;
LogErrorOnFailure(self->EstablishPASEConnection(remoteId, params));
}
}

void DeviceCommissioner::OnWiFiPAFSubscribeError(void * appState, CHIP_ERROR err)
{
auto self = static_cast<DeviceCommissioner *>(appState);
auto device = self->mDeviceInPASEEstablishment;

ChipLogProgress(Controller, "WiFi-PAF: Subscription Error!");
if (nullptr != device && device->GetDeviceTransportType() == Transport::Type::kWiFiPAF)
{
self->ReleaseCommissioneeDevice(device);
self->mRendezvousParametersForDeviceDiscoveredOverWiFiPAF = RendezvousParameters();
if (self->mPairingDelegate != nullptr)
{
self->mPairingDelegate->OnPairingComplete(err);
}
}
}
#endif

CHIP_ERROR DeviceCommissioner::Commission(NodeId remoteDeviceId, CommissioningParameters & params)
{
if (mDefaultCommissioner == nullptr)
Expand Down
5 changes: 5 additions & 0 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,11 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
static void OnDiscoveredDeviceOverBleError(void * appState, CHIP_ERROR err);
RendezvousParameters mRendezvousParametersForDeviceDiscoveredOverBle;
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
static void OnWiFiPAFSubscribeComplete(void * appState);
static void OnWiFiPAFSubscribeError(void * appState, CHIP_ERROR err);
RendezvousParameters mRendezvousParametersForDeviceDiscoveredOverWiFiPAF;
#endif

static void OnBasicFailure(void * context, CHIP_ERROR err);
static void OnBasicSuccess(void * context, const chip::app::DataModel::NullObjectType &);
Expand Down
7 changes: 7 additions & 0 deletions src/controller/CHIPDeviceControllerFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ CHIP_ERROR DeviceControllerFactory::InitSystemState(FactoryInitParams params)
#else
stateParams.bleLayer = params.bleLayer;
#endif // CONFIG_DEVICE_LAYER
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
stateParams.wifipaf_layer = params.wifipaf_layer;
#endif
VerifyOrReturnError(stateParams.bleLayer != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
#endif

Expand All @@ -167,6 +170,10 @@ CHIP_ERROR DeviceControllerFactory::InitSystemState(FactoryInitParams params)
Transport::TcpListenParameters(stateParams.tcpEndPointManager)
.SetAddressType(IPAddressType::kIPv6)
.SetListenPort(params.listenPort)
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
,
Transport::WiFiPAFListenParameters()
#endif
));

Expand Down
3 changes: 3 additions & 0 deletions src/controller/CHIPDeviceControllerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ struct FactoryInitParams
#if CONFIG_NETWORK_LAYER_BLE
Ble::BleLayer * bleLayer = nullptr;
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
Transport::WiFiPAFLayer * wifipaf_layer = nullptr;
#endif

//
// Controls enabling server cluster interactions on a controller. This in turn
Expand Down
14 changes: 14 additions & 0 deletions src/controller/CHIPDeviceControllerSystemState.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,17 @@
#include <ble/Ble.h>
#include <transport/raw/BLE.h>
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
#include <transport/raw/WiFiPAF.h>
#endif

namespace chip {

inline constexpr size_t kMaxDeviceTransportBlePendingPackets = 1;
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
inline constexpr size_t kMaxDeviceTransportWiFiPAFPendingPackets = 1;
#endif


#if INET_CONFIG_ENABLE_TCP_ENDPOINT
inline constexpr size_t kMaxDeviceTransportTcpActiveConnectionCount = CHIP_CONFIG_MAX_ACTIVE_TCP_CONNECTIONS;
Expand All @@ -76,6 +83,10 @@ using DeviceTransportMgr =
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
,
Transport::TCP<kMaxDeviceTransportTcpActiveConnectionCount, kMaxDeviceTransportTcpPendingPackets>
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
,
Transport::WiFiPAF<kMaxDeviceTransportWiFiPAFPendingPackets> /* WiFiPAF */
#endif
>;

Expand All @@ -93,6 +104,9 @@ struct DeviceControllerSystemStateParams
FabricTable * fabricTable = nullptr;
#if CONFIG_NETWORK_LAYER_BLE
Ble::BleLayer * bleLayer = nullptr;
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
Transport::WiFiPAFLayer *wifipaf_layer = nullptr;
#endif
Credentials::GroupDataProvider * groupDataProvider = nullptr;
Crypto::SessionKeystore * sessionKeystore = nullptr;
Expand Down
Loading

0 comments on commit c8a828a

Please sign in to comment.