Skip to content

Commit

Permalink
Split WiFi association from RendezvousSessionDelegate to its own dele…
Browse files Browse the repository at this point in the history
…gate
  • Loading branch information
pan-apple committed Oct 2, 2020
1 parent f607806 commit a156794
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 66 deletions.
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");
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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 @@ -58,7 +58,7 @@ void RendezvousDeviceDelegate::OnRendezvousStatusUpdate(RendezvousSessionDelegat
{
if (err != CHIP_NO_ERROR)
{
ESP_LOGI(TAG, "OnRendezvousStatusUpdate: %s", ErrorStr(err));
ESP_LOGE(TAG, "OnRendezvousStatusUpdate: %s, status %d", ErrorStr(err), status);
}

switch (status)
Expand Down Expand Up @@ -92,9 +92,3 @@ void RendezvousDeviceDelegate::OnRendezvousMessageReceived(PacketBuffer * buffer
mRendezvousSession->SendMessage(buffer);
}
}

void RendezvousDeviceDelegate::OnRendezvousProvisionNetwork(const char * ssid, const char * passwd)
{
ESP_LOGI(TAG, "OnRendezvousProvisionNetwork");
// Call device network configuration method here.
}
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 @@ -32,10 +34,9 @@ class RendezvousDeviceDelegate : public RendezvousSessionDelegate
void OnRendezvousMessageReceived(PacketBuffer * buffer) override;
void OnRendezvousStatusUpdate(RendezvousSessionDelegate::Status status, CHIP_ERROR err) override;

void OnRendezvousProvisionNetwork(const char * ssid, const char * passwd) override;

private:
RendezvousSession * mRendezvousSession;
ESP32NetworkProvisioningDelegate mDeviceNetworkProvisioningDelegate;
};

#endif // __RENDEZVOUSDEVICEDELEGATE_H__
85 changes: 70 additions & 15 deletions src/transport/NetworkProvisioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,52 @@

namespace chip {

CHIP_ERROR NetworkProvisioning::HandleNetworkProvisioningMessage(uint8_t msgType, PacketBuffer * msgBuf,
RendezvousSessionDelegate * delegate)
void NetworkProvisioning::Init(NetworkProvisioningDelegate * delegate, DeviceNetworkProvisioningDelegate * deviceDelegate)
{
if (mDelegate != nullptr)
{
mDelegate->Release();
}

if (delegate != nullptr)
{
mDelegate = delegate->Retain();
}

if (mDeviceDelegate != nullptr)
{
mDeviceDelegate->Release();
}

if (deviceDelegate != nullptr)
{
mDeviceDelegate = deviceDelegate->Retain();
#if CONFIG_DEVICE_LAYER
DeviceLayer::PlatformMgr().AddEventHandler(ConnectivityHandler, reinterpret_cast<intptr_t>(this));
#endif
}
}

NetworkProvisioning::~NetworkProvisioning()
{
if (mDeviceDelegate != nullptr)
{
mDeviceDelegate->Release();

#if CONFIG_DEVICE_LAYER
DeviceLayer::PlatformMgr().RemoveEventHandler(ConnectivityHandler, reinterpret_cast<intptr_t>(this));
#endif
}

if (mDelegate != nullptr)
{
mDelegate->Release();
}
}

CHIP_ERROR NetworkProvisioning::HandleNetworkProvisioningMessage(uint8_t msgType, PacketBuffer * msgBuf)
{
CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrExit(delegate != nullptr, err = CHIP_ERROR_INCORRECT_STATE);

switch (msgType)
{
Expand All @@ -48,17 +89,23 @@ CHIP_ERROR NetworkProvisioning::HandleNetworkProvisioningMessage(uint8_t msgType
size_t len = msgBuf->DataLength();
size_t offset = 0;

ChipLogProgress(NetworkProvisioning, "Received kWiFiAssociationRequest. DeviceDelegate %p\n", mDeviceDelegate);

VerifyOrExit(mDeviceDelegate != nullptr, err = CHIP_ERROR_INCORRECT_STATE);

err = DecodeString(&buffer[offset], len - offset, bbufSSID, offset);
SuccessOrExit(err);
// TODO: Check for the error once network provisioning is moved to delegate calls

err = DecodeString(&buffer[offset], len - offset, bbufPW, offset);
SuccessOrExit(err);
// TODO: Check for the error once network provisioning is moved to delegate calls

delegate->OnRendezvousProvisionNetwork(SSID, passwd);
mDeviceDelegate->ProvisionNetwork(SSID, passwd);
err = CHIP_NO_ERROR;
}
break;

case NetworkProvisioning::MsgTypes::kIPAddressAssigned: {
ChipLogProgress(NetworkProvisioning, "Received kIPAddressAssigned\n");
if (!IPAddress::FromString(Uint8::to_const_char(msgBuf->Start()), msgBuf->DataLength(), mDeviceAddress))
{
ExitNow(err = CHIP_ERROR_INVALID_ADDRESS);
Expand All @@ -72,16 +119,20 @@ CHIP_ERROR NetworkProvisioning::HandleNetworkProvisioningMessage(uint8_t msgType
};

exit:
if (err != CHIP_NO_ERROR)
{
mDelegate->OnNetworkProvisioningError(err);
}
else
if (mDelegate != nullptr)
{
// Network provisioning handshake requires only one message exchange in either direction.
// If the current message handling did not result in an error, network provisioning is
// complete.
mDelegate->OnNetworkProvisioningComplete();
if (err != CHIP_NO_ERROR)
{
ChipLogError(NetworkProvisioning, "Failed in HandleNetworkProvisioningMessage. error %s\n", ErrorStr(err));
mDelegate->OnNetworkProvisioningError(err);
}
else
{
// Network provisioning handshake requires only one message exchange in either direction.
// If the current message handling did not result in an error, network provisioning is
// complete.
mDelegate->OnNetworkProvisioningComplete();
}
}
return err;
}
Expand Down Expand Up @@ -128,6 +179,7 @@ CHIP_ERROR NetworkProvisioning::SendIPAddress(const IPAddress & addr)
char * addrStr = addr.ToString(Uint8::to_char(buffer->Start()), buffer->AvailableDataLength());
size_t addrLen = 0;

ChipLogProgress(NetworkProvisioning, "Sending IP Address. Delegate %p\n", mDelegate);
VerifyOrExit(mDelegate != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(addrStr != nullptr, err = CHIP_ERROR_INVALID_ADDRESS);

Expand All @@ -143,6 +195,7 @@ CHIP_ERROR NetworkProvisioning::SendIPAddress(const IPAddress & addr)
exit:
if (CHIP_NO_ERROR != err)
{
ChipLogError(NetworkProvisioning, "Failed in sending IP address. error %s\n", ErrorStr(err));
PacketBuffer::Free(buffer);
}
return err;
Expand All @@ -154,6 +207,7 @@ CHIP_ERROR NetworkProvisioning::SendNetworkCredentials(const char * ssid, const
System::PacketBuffer * buffer = System::PacketBuffer::New();
BufBound bbuf(buffer->Start(), buffer->TotalLength());

ChipLogProgress(NetworkProvisioning, "Sending Network Creds. Delegate %p\n", mDelegate);
VerifyOrExit(mDelegate != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
SuccessOrExit(EncodeString(ssid, bbuf));
SuccessOrExit(EncodeString(passwd, bbuf));
Expand All @@ -169,6 +223,7 @@ CHIP_ERROR NetworkProvisioning::SendNetworkCredentials(const char * ssid, const
exit:
if (CHIP_NO_ERROR != err)
{
ChipLogError(NetworkProvisioning, "Failed in sending Network Creds. error %s\n", ErrorStr(err));
PacketBuffer::Free(buffer);
}
return err;
Expand Down
49 changes: 21 additions & 28 deletions src/transport/NetworkProvisioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <core/CHIPCore.h>
#include <core/ReferenceCounted.h>
#include <protocols/CHIPProtocols.h>
#include <support/BufBound.h>
#include <system/SystemPacketBuffer.h>
#include <transport/RendezvousSessionDelegate.h>
Expand All @@ -36,6 +37,21 @@

namespace chip {

class DLL_EXPORT DeviceNetworkProvisioningDelegate : public ReferenceCounted<DeviceNetworkProvisioningDelegate>
{
public:
/**
* @brief
* Called to provision WiFi credentials in a device
*
* @param ssid WiFi SSID
* @param passwd WiFi password
*/
virtual void ProvisionNetwork(const char * ssid, const char * passwd) {}

~DeviceNetworkProvisioningDelegate() override {}
};

class DLL_EXPORT NetworkProvisioningDelegate : public ReferenceCounted<NetworkProvisioningDelegate>
{
public:
Expand Down Expand Up @@ -77,37 +93,13 @@ class DLL_EXPORT NetworkProvisioning
kIPAddressAssigned = 1,
};

void Init(NetworkProvisioningDelegate * delegate, bool monitorNetIf)
{
if (mDelegate != nullptr)
{
mDelegate->Release();
}
void Init(NetworkProvisioningDelegate * delegate, DeviceNetworkProvisioningDelegate * deviceDelegate);

if (delegate != nullptr)
{
mDelegate = delegate->Retain();
}

#if CONFIG_DEVICE_LAYER
DeviceLayer::PlatformMgr().AddEventHandler(ConnectivityHandler, reinterpret_cast<intptr_t>(this));
#endif
}

~NetworkProvisioning()
{
#if CONFIG_DEVICE_LAYER
DeviceLayer::PlatformMgr().RemoveEventHandler(ConnectivityHandler, reinterpret_cast<intptr_t>(this));
#endif
if (mDelegate != nullptr)
{
mDelegate->Release();
}
}
~NetworkProvisioning();

CHIP_ERROR SendNetworkCredentials(const char * ssid, const char * passwd);

CHIP_ERROR HandleNetworkProvisioningMessage(uint8_t msgType, PacketBuffer * msgBuf, RendezvousSessionDelegate * delegate);
CHIP_ERROR HandleNetworkProvisioningMessage(uint8_t msgType, PacketBuffer * msgBuf);

/**
* @brief
Expand All @@ -119,7 +111,8 @@ class DLL_EXPORT NetworkProvisioning
const IPAddress & GetIPAddress() const { return mDeviceAddress; }

private:
NetworkProvisioningDelegate * mDelegate = nullptr;
NetworkProvisioningDelegate * mDelegate = nullptr;
DeviceNetworkProvisioningDelegate * mDeviceDelegate = nullptr;

IPAddress mDeviceAddress = IPAddress::Any;

Expand Down
15 changes: 6 additions & 9 deletions src/transport/RendezvousSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,10 @@ CHIP_ERROR RendezvousSession::Init(const RendezvousParameters & params)
{
err = WaitForPairing(mParams.GetLocalNodeId(), mParams.GetSetupPINCode());
SuccessOrExit(err);

mNetworkProvision.Init(this, true);
}
else
{
mNetworkProvision.Init(this, false);
}

mNetworkProvision.Init(this, mDeviceNetworkProvisionDelegate);

exit:
return err;
}
Expand Down Expand Up @@ -251,6 +247,7 @@ void RendezvousSession::OnRendezvousError(CHIP_ERROR err)
break;
};
mDelegate->OnRendezvousError(err);
UpdateState(State::kInit);
}

void RendezvousSession::UpdateState(RendezvousSession::State newState)
Expand Down Expand Up @@ -367,10 +364,10 @@ CHIP_ERROR RendezvousSession::HandleSecureMessage(PacketBuffer * msgBuf)

if (payloadHeader.GetProtocolID() == Protocols::kChipProtocol_NetworkProvisioning)
{
err = mNetworkProvision.HandleNetworkProvisioningMessage(payloadHeader.GetMessageType(), msgBuf, mDelegate);
// Ignoring error for time being, as the message is getting routed via OnRendezvousMessageReceived()
err = mNetworkProvision.HandleNetworkProvisioningMessage(payloadHeader.GetMessageType(), msgBuf);
SuccessOrExit(err);
}
// else .. TBD once application dependency on this message has been removed, enable the else condition
// else .. TODO once application dependency on this message has been removed, enable the else condition
{
mDelegate->OnRendezvousMessageReceived(msgBuf);
}
Expand Down
Loading

0 comments on commit a156794

Please sign in to comment.