Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move WiFi association processing to Rendezvous StateMachine #3019

Merged
merged 7 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
*
* 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 "DeviceNetworkProvisioningDelegate.h"
#include <support/logging/CHIPLogging.h>

using namespace ::chip;

void ESP32NetworkProvisioningDelegate::ProvisionNetwork(const char * ssid, const char * passwd)
{
ChipLogProgress(NetworkProvisioning, "ESP32NetworkProvisioningDelegate: Received SSID and passwd\n");
}
35 changes: 21 additions & 14 deletions examples/wifi-echo/server/esp32/main/RendezvousDeviceDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ RendezvousDeviceDelegate::RendezvousDeviceDelegate()

params.SetSetupPINCode(setupPINCode).SetLocalNodeId(kLocalNodeId).SetBleLayer(DeviceLayer::ConnectivityMgr().GetBleLayer());

mRendezvousSession = new RendezvousSession(this);
mRendezvousSession = new RendezvousSession(this, &mDeviceNetworkProvisioningDelegate);
err = mRendezvousSession->Init(params);

exit:
Expand All @@ -56,23 +56,30 @@ RendezvousDeviceDelegate::RendezvousDeviceDelegate()
}
}

void RendezvousDeviceDelegate::OnRendezvousError(CHIP_ERROR err)
void RendezvousDeviceDelegate::OnRendezvousStatusUpdate(RendezvousSessionDelegate::Status status, CHIP_ERROR err)
{
ESP_LOGI(TAG, "OnRendezvousError: %s", ErrorStr(err));
}
if (err != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "OnRendezvousStatusUpdate: %s, status %d", ErrorStr(err), status);
}

void RendezvousDeviceDelegate::OnRendezvousConnectionOpened()
{
ESP_LOGI(TAG, "OnRendezvousConnectionOpened");
switch (status)
{
case RendezvousSessionDelegate::SecurePairingSuccess:
ESP_LOGI(TAG, "Device completed SPAKE2+ handshake\n");
PairingComplete(&mRendezvousSession->GetPairingSession());
bluetoothLED.Set(true);
break;

PairingComplete(&mRendezvousSession->GetPairingSession());
bluetoothLED.Set(true);
}
case RendezvousSessionDelegate::NetworkProvisioningSuccess:

void RendezvousDeviceDelegate::OnRendezvousConnectionClosed()
{
ESP_LOGI(TAG, "OnRendezvousConnectionClosed");
bluetoothLED.Set(false);
ESP_LOGI(TAG, "Device was assigned an ip address\n");
bluetoothLED.Set(false);
break;

default:
break;
};
}

void RendezvousDeviceDelegate::OnRendezvousMessageReceived(PacketBuffer * buffer)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
*
* 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.
*/

#ifndef _DEVICE_NETWORK_PROVISIONING_DELEGATE_H_
#define _DEVICE_NETWORK_PROVISIONING_DELEGATE_H_

#include <core/CHIPError.h>
#include <transport/NetworkProvisioning.h>

using namespace ::chip;

class ESP32NetworkProvisioningDelegate : public DeviceNetworkProvisioningDelegate
{
public:
/**
* @brief
* Called to provision WiFi credentials in a device
*
* @param ssid WiFi SSID
* @param passwd WiFi password
*/
void ProvisionNetwork(const char * ssid, const char * passwd) override;
};

#endif // _DEVICE_NETWORK_PROVISIONING_DELEGATE_H_
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef __RENDEZVOUSDEVICEDELEGATE_H__
#define __RENDEZVOUSDEVICEDELEGATE_H__

#include "DeviceNetworkProvisioningDelegate.h"

#include <platform/CHIPDeviceLayer.h>
#include <transport/RendezvousSession.h>

Expand All @@ -27,13 +29,12 @@ class RendezvousDeviceDelegate : public chip::RendezvousSessionDelegate
RendezvousDeviceDelegate();

//////////// RendezvousSession callback Implementation ///////////////
void OnRendezvousConnectionOpened() override;
void OnRendezvousConnectionClosed() override;
void OnRendezvousError(CHIP_ERROR err) override;
void OnRendezvousMessageReceived(chip::System::PacketBuffer * buffer) override;
void OnRendezvousStatusUpdate(RendezvousSessionDelegate::Status status, CHIP_ERROR err) override;

private:
chip::RendezvousSession * mRendezvousSession;
ESP32NetworkProvisioningDelegate mDeviceNetworkProvisioningDelegate;
};

#endif // __RENDEZVOUSDEVICEDELEGATE_H__
64 changes: 41 additions & 23 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ ChipDeviceController::ChipDeviceController()
mCurReqMsg = nullptr;
mOnError = nullptr;
mOnNewConnection = nullptr;
mPairingDelegate = nullptr;
mDeviceAddr = IPAddress::Any;
mDevicePort = CHIP_PORT;
mLocalDeviceId = 0;
Expand Down Expand Up @@ -106,6 +107,17 @@ CHIP_ERROR ChipDeviceController::Init(NodeId localNodeId, System::Layer * system
return err;
}

CHIP_ERROR ChipDeviceController::Init(NodeId localNodeId, DevicePairingDelegate * pairingDelegate)
{
CHIP_ERROR err = Init(localNodeId);
SuccessOrExit(err);

mPairingDelegate = pairingDelegate;

exit:
return err;
}

CHIP_ERROR ChipDeviceController::Shutdown()
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down Expand Up @@ -401,42 +413,48 @@ void ChipDeviceController::OnMessageReceived(const PacketHeader & header, Transp
}
}

void ChipDeviceController::OnRendezvousError(CHIP_ERROR err)
void ChipDeviceController::OnRendezvousMessageReceived(PacketBuffer * buffer)
{
if (mOnError)
if (mOnComplete.Response)
{
mOnError(this, mAppReqState, err, nullptr);
mOnComplete.Response(this, mAppReqState, buffer);
}
}

void ChipDeviceController::OnRendezvousConnectionOpened()
void ChipDeviceController::OnRendezvousStatusUpdate(RendezvousSessionDelegate::Status status, CHIP_ERROR err)
{
mPairingSession = mRendezvousSession->GetPairingSession();
mConState = kConnectionState_SecureConnected;

if (mOnNewConnection)
if (mOnError != nullptr && err != CHIP_NO_ERROR)
{
mOnNewConnection(this, nullptr, mAppReqState);
mOnError(this, mAppReqState, err, nullptr);
}
}

void ChipDeviceController::OnRendezvousConnectionClosed() {}

void ChipDeviceController::OnRendezvousMessageReceived(PacketBuffer * buffer)
{
if (mOnComplete.Response)
switch (status)
{
mOnComplete.Response(this, mAppReqState, buffer);
}
}
case RendezvousSessionDelegate::SecurePairingSuccess:
ChipLogProgress(Controller, "Remote device completed SPAKE2+ handshake\n");
mPairingSession = mRendezvousSession->GetPairingSession();
mConState = kConnectionState_SecureConnected;

if (mOnNewConnection)
{
mOnNewConnection(this, nullptr, mAppReqState);
}

if (mPairingDelegate != nullptr)
{
mPairingDelegate->OnNetworkCredentialsRequested(mRendezvousSession);
}
break;

case RendezvousSessionDelegate::NetworkProvisioningSuccess:

void ChipDeviceController::OnRendezvousStatusUpdate(RendezvousSessionDelegate::Status status)
{
if (status == RendezvousSessionDelegate::NetworkProvisioningSuccess)
{
ChipLogProgress(Controller, "Remote device was assigned an ip address\n");
mDeviceAddr = mRendezvousSession->GetIPAddress();
}
break;

default:
break;
};
}

} // namespace DeviceController
Expand Down
56 changes: 52 additions & 4 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <core/CHIPTLV.h>
#include <support/DLLUtil.h>
#include <transport/RendezvousSession.h>
#include <transport/RendezvousSessionDelegate.h>
#include <transport/SecureSessionMgr.h>
#include <transport/raw/UDP.h>

Expand All @@ -48,6 +49,53 @@ typedef void (*ErrorHandler)(ChipDeviceController * deviceController, void * app
const Inet::IPPacketInfo * pktInfo);
typedef void (*MessageReceiveHandler)(ChipDeviceController * deviceController, void * appReqState, System::PacketBuffer * payload);

class DLL_EXPORT DevicePairingDelegate
{
public:
/**
* @brief
* Called when the pairing reaches a certain stage.
*
* @param status Current status of pairing
*/
virtual void OnStatusUpdate(RendezvousSessionDelegate::Status status) {}

/**
* @brief
* Called when the network credentials are needed for the remote device
*
* @param callback Callback delegate that provisions the network credentials
*/
virtual void OnNetworkCredentialsRequested(RendezvousDeviceCredentialsDelegate * callback) = 0;

/**
* @brief
* Called when the operational credentials are needed for the remote device
*
* @param csr Certificate signing request from the device
* @param csr_length The length of CSR
* @param callback Callback delegate that provisions the operational credentials
*/
virtual void OnOperationalCredentialsRequested(const char * csr, size_t csr_length,
RendezvousDeviceCredentialsDelegate * callback) = 0;

/**
* @brief
* Called when the pairing is complete (with success or error)
*
* @param error Error cause, if any
*/
virtual void OnPairingComplete(CHIP_ERROR error) {}

/**
* @brief
* Called when the pairing is deleted (with success or error)
*
* @param error Error cause, if any
*/
virtual void OnPairingDeleted(CHIP_ERROR error) {}
};

class DLL_EXPORT ChipDeviceController : public SecureSessionMgrCallback, public RendezvousSessionDelegate
{
friend class ChipDeviceControllerCallback;
Expand All @@ -66,6 +114,7 @@ class DLL_EXPORT ChipDeviceController : public SecureSessionMgrCallback, public
* Init function to be used when already-initialized System::Layer and InetLayer are available.
*/
CHIP_ERROR Init(NodeId localDeviceId, System::Layer * systemLayer, Inet::InetLayer * inetLayer);
CHIP_ERROR Init(NodeId localDeviceId, DevicePairingDelegate * pairingDelegate);
CHIP_ERROR Shutdown();

// ----- Connection Management -----
Expand Down Expand Up @@ -190,11 +239,8 @@ class DLL_EXPORT ChipDeviceController : public SecureSessionMgrCallback, public
void OnNewConnection(Transport::PeerConnectionState * state, SecureSessionMgrBase * mgr) override;

//////////// RendezvousSessionDelegate Implementation ///////////////
void OnRendezvousConnectionOpened() override;
void OnRendezvousConnectionClosed() override;
void OnRendezvousError(CHIP_ERROR err) override;
void OnRendezvousMessageReceived(System::PacketBuffer * buffer) override;
void OnRendezvousStatusUpdate(RendezvousSessionDelegate::Status status) override;
void OnRendezvousStatusUpdate(RendezvousSessionDelegate::Status status, CHIP_ERROR err) override;

private:
enum
Expand Down Expand Up @@ -237,6 +283,8 @@ class DLL_EXPORT ChipDeviceController : public SecureSessionMgrCallback, public

SecurePairingSession mPairingSession;

DevicePairingDelegate * mPairingDelegate;

void ClearRequestState();
void ClearOpState();

Expand Down
4 changes: 2 additions & 2 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static void onMessageReceived(
}

static void onInternalError(chip::DeviceController::ChipDeviceController * deviceController, void * appReqState, CHIP_ERROR error,
const chip::IPPacketInfo * pi)
const chip::Inet::IPPacketInfo * pi)
{
CHIPDeviceController * controller = (__bridge CHIPDeviceController *) appReqState;
[controller _dispatchAsyncErrorBlock:[CHIPError errorForCHIPErrorCode:error]];
Expand Down Expand Up @@ -264,7 +264,7 @@ - (AddressInfo *)getAddressInfo
[self.lock lock];
err = self.cppController->PopulatePeerAddress(peerAddr);
[self.lock unlock];
chip::IPAddress ipAddr = peerAddr.GetIPAddress();
chip::Inet::IPAddress ipAddr = peerAddr.GetIPAddress();
uint16_t port = peerAddr.GetPort();

if (err != CHIP_NO_ERROR) {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Darwin/BleApplicationDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace chip {
namespace DeviceLayer {
namespace Internal {

class BleApplicationDelegateImpl : public BleApplicationDelegate
class BleApplicationDelegateImpl : public Ble::BleApplicationDelegate
{
public:
virtual void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT connObj);
Expand Down
2 changes: 2 additions & 0 deletions src/transport/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ static_library("transport") {
output_name = "libTransportLayer"

sources = [
"NetworkProvisioning.cpp",
"NetworkProvisioning.h",
"PeerConnectionState.h",
"PeerConnections.h",
"RendezvousParameters.h",
Expand Down
Loading