diff --git a/examples/wifi-echo/server/esp32/main/DeviceNetworkProvisioningDelegate.cpp b/examples/wifi-echo/server/esp32/main/DeviceNetworkProvisioningDelegate.cpp new file mode 100644 index 00000000000000..8821907e5727d0 --- /dev/null +++ b/examples/wifi-echo/server/esp32/main/DeviceNetworkProvisioningDelegate.cpp @@ -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 + +using namespace ::chip; + +void ESP32NetworkProvisioningDelegate::ProvisionNetwork(const char * ssid, const char * passwd) +{ + ChipLogProgress(NetworkProvisioning, "ESP32NetworkProvisioningDelegate: Received SSID and passwd\n"); +} diff --git a/examples/wifi-echo/server/esp32/main/RendezvousDeviceDelegate.cpp b/examples/wifi-echo/server/esp32/main/RendezvousDeviceDelegate.cpp index 50e1ef65c92b09..886ba126f73611 100644 --- a/examples/wifi-echo/server/esp32/main/RendezvousDeviceDelegate.cpp +++ b/examples/wifi-echo/server/esp32/main/RendezvousDeviceDelegate.cpp @@ -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: @@ -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) @@ -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. -} diff --git a/examples/wifi-echo/server/esp32/main/include/DeviceNetworkProvisioningDelegate.h b/examples/wifi-echo/server/esp32/main/include/DeviceNetworkProvisioningDelegate.h new file mode 100644 index 00000000000000..a3d5cc13fc7e0d --- /dev/null +++ b/examples/wifi-echo/server/esp32/main/include/DeviceNetworkProvisioningDelegate.h @@ -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 +#include + +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_ diff --git a/examples/wifi-echo/server/esp32/main/include/RendezvousDeviceDelegate.h b/examples/wifi-echo/server/esp32/main/include/RendezvousDeviceDelegate.h index c154278d0216fb..52ad4545dfdf34 100644 --- a/examples/wifi-echo/server/esp32/main/include/RendezvousDeviceDelegate.h +++ b/examples/wifi-echo/server/esp32/main/include/RendezvousDeviceDelegate.h @@ -18,6 +18,8 @@ #ifndef __RENDEZVOUSDEVICEDELEGATE_H__ #define __RENDEZVOUSDEVICEDELEGATE_H__ +#include "DeviceNetworkProvisioningDelegate.h" + #include #include @@ -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__ diff --git a/src/transport/NetworkProvisioning.cpp b/src/transport/NetworkProvisioning.cpp index 63cbdcecd24938..66434ca586b768 100644 --- a/src/transport/NetworkProvisioning.cpp +++ b/src/transport/NetworkProvisioning.cpp @@ -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(this)); +#endif + } +} + +NetworkProvisioning::~NetworkProvisioning() +{ + if (mDeviceDelegate != nullptr) + { + mDeviceDelegate->Release(); + +#if CONFIG_DEVICE_LAYER + DeviceLayer::PlatformMgr().RemoveEventHandler(ConnectivityHandler, reinterpret_cast(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) { @@ -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); @@ -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; } @@ -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); @@ -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; @@ -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)); @@ -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; diff --git a/src/transport/NetworkProvisioning.h b/src/transport/NetworkProvisioning.h index ceeea74088ac20..fed4d098ef9644 100644 --- a/src/transport/NetworkProvisioning.h +++ b/src/transport/NetworkProvisioning.h @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -36,6 +37,21 @@ namespace chip { +class DLL_EXPORT DeviceNetworkProvisioningDelegate : public ReferenceCounted +{ +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 { public: @@ -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(this)); -#endif - } - - ~NetworkProvisioning() - { -#if CONFIG_DEVICE_LAYER - DeviceLayer::PlatformMgr().RemoveEventHandler(ConnectivityHandler, reinterpret_cast(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 @@ -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; diff --git a/src/transport/RendezvousSession.cpp b/src/transport/RendezvousSession.cpp index 71da7df3b50882..ef1226eb2c6b3b 100644 --- a/src/transport/RendezvousSession.cpp +++ b/src/transport/RendezvousSession.cpp @@ -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; } @@ -251,6 +247,7 @@ void RendezvousSession::OnRendezvousError(CHIP_ERROR err) break; }; mDelegate->OnRendezvousError(err); + UpdateState(State::kInit); } void RendezvousSession::UpdateState(RendezvousSession::State newState) @@ -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); } diff --git a/src/transport/RendezvousSession.h b/src/transport/RendezvousSession.h index 93f4fee8ea5b27..4e94686669e55c 100644 --- a/src/transport/RendezvousSession.h +++ b/src/transport/RendezvousSession.h @@ -68,13 +68,15 @@ class RendezvousSession : public SecurePairingSessionDelegate, public: enum State : uint8_t { - kUnknown = 0, + kInit = 0, kSecurePairing, kNetworkProvisioning, kRendezvousComplete, }; - RendezvousSession(RendezvousSessionDelegate * delegate) : mDelegate(delegate) {} + RendezvousSession(RendezvousSessionDelegate * delegate, DeviceNetworkProvisioningDelegate * networkDelegate = nullptr) : + mDelegate(delegate), mDeviceNetworkProvisionDelegate(networkDelegate) + {} ~RendezvousSession() override; /** @@ -135,11 +137,12 @@ class RendezvousSession : public SecurePairingSessionDelegate, SecurePairingSession mPairingSession; NetworkProvisioning mNetworkProvision; + DeviceNetworkProvisioningDelegate * mDeviceNetworkProvisionDelegate = nullptr; SecureSession mSecureSession; uint32_t mSecureMessageIndex = 0; uint16_t mNextKeyId = 0; - RendezvousSession::State mCurrentState = State::kUnknown; + RendezvousSession::State mCurrentState = State::kInit; void UpdateState(RendezvousSession::State newState); }; diff --git a/src/transport/RendezvousSessionDelegate.h b/src/transport/RendezvousSessionDelegate.h index fb272f56fd8386..7125c08471c6c2 100644 --- a/src/transport/RendezvousSessionDelegate.h +++ b/src/transport/RendezvousSessionDelegate.h @@ -41,7 +41,6 @@ class RendezvousSessionDelegate virtual void OnRendezvousMessageReceived(System::PacketBuffer * buffer) = 0; virtual void OnRendezvousStatusUpdate(Status status, CHIP_ERROR err) {} - virtual void OnRendezvousProvisionNetwork(const char * ssid, const char * passwd) {} }; class DLL_EXPORT RendezvousDeviceCredentialsDelegate