Skip to content

Commit

Permalink
Adds RendezvousSession inside src/transport
Browse files Browse the repository at this point in the history
RendezvousSession manages the initial connection between a commissioner and a device.
During rendezvous a key pair is generated by SecurePairingSession under the hood.

Embedders can listen to the different states of Rendezvous by implementing
the RendezvousSessionCallback interface. The differents events are:
 * OnRendezvousConnectionOpened();
 * OnRendezvousConnectionClosed();
 * OnRendezvousError(CHIP_ERROR err);
 * OnRendezvousMessageReceived(System::PacketBuffer * buffer);
  • Loading branch information
vivien-apple committed Sep 8, 2020
1 parent 1ace193 commit 44c9814
Show file tree
Hide file tree
Showing 29 changed files with 757 additions and 670 deletions.
13 changes: 8 additions & 5 deletions examples/chip-tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,28 +471,31 @@ CHIP_ERROR ExecuteCommand(DeviceController::ChipDeviceController * controller, C
CHIP_ERROR err = CHIP_NO_ERROR;
switch (command)
{
case Command::EchoBle:
err = controller->ConnectDevice(kRemoteDeviceId, commandArgs.discriminator, commandArgs.setupPINCode, NULL, OnConnect,
OnMessage, OnError);
case Command::EchoBle: {
RendezvousParameters params = RendezvousParameters(commandArgs.setupPINCode).SetDiscriminator(commandArgs.discriminator);
err = controller->ConnectDevice(kRemoteDeviceId, params, NULL, OnConnect, OnMessage, OnError);
VerifyOrExit(err == CHIP_NO_ERROR, fprintf(stderr, "Failed to connect to the device"));
DoEchoBle(controller, commandArgs.discriminator);
break;
}

case Command::Echo:
case Command::Echo: {
err = controller->ConnectDeviceWithoutSecurePairing(kRemoteDeviceId, commandArgs.hostAddr, NULL, OnConnect, OnMessage,
OnError, commandArgs.port);
VerifyOrExit(err == CHIP_NO_ERROR, fprintf(stderr, "Failed to connect to the device"));
DoEchoIP(controller, commandArgs.hostAddr, commandArgs.port);
break;
}

default:
default: {
err = controller->ConnectDeviceWithoutSecurePairing(kRemoteDeviceId, commandArgs.hostAddr, NULL, OnConnect, OnMessage,
OnError, commandArgs.port);
VerifyOrExit(err == CHIP_NO_ERROR, fprintf(stderr, "Failed to connect to the device"));
DoOnOff(controller, command, commandArgs);
controller->ServiceEventSignal();
break;
}
}

exit:
return err;
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void StartServer(chip::SecureSessionMgr<chip::Transport::UDP> * sessions)
UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(kIPAddressType_IPv6));
SuccessOrExit(err);

err = sessions->NewPairing(Optional<NodeId>::Value(kUndefinedNodeId), peer, 0, 0, &gTestPairing);
err = sessions->NewPairing(peer, &gTestPairing);
SuccessOrExit(err);

sessions->SetDelegate(&gCallbacks);
Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/efr32/src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void StartServer(DemoSessionManager * sessions)
UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(kIPAddressType_IPv6));
SuccessOrExit(err);

err = sessions->NewPairing(Optional<NodeId>::Value(kUndefinedNodeId), peer, 0, 0, &gTestPairing);
err = sessions->NewPairing(peer, &gTestPairing);
SuccessOrExit(err);

sessions->SetDelegate(&gCallbacks);
Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/nrfconnect/main/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void StartServer(DemoSessionManager * sessions)
UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(kIPAddressType_IPv6));
SuccessOrExit(err);

err = sessions->NewPairing(Optional<NodeId>::Value(kUndefinedNodeId), peer, 0, 0, &gTestPairing);
err = sessions->NewPairing(peer, &gTestPairing);
SuccessOrExit(err);

sessions->SetDelegate(&gCallbacks);
Expand Down
2 changes: 1 addition & 1 deletion examples/platform/nrf528xx/app/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void StartServer(DemoSessionManager * sessions)
UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(kIPAddressType_IPv6));
SuccessOrExit(err);

err = sessions->NewPairing(Optional<NodeId>::Value(kUndefinedNodeId), peer, 0, 0, &gTestPairing);
err = sessions->NewPairing(peer, &gTestPairing);
SuccessOrExit(err);

sessions->SetDelegate(&gCallbacks);
Expand Down
8 changes: 4 additions & 4 deletions examples/wifi-echo/server/esp32/main/EchoDeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
**/

#include "EchoDeviceCallbacks.h"
#include "RendezvousSession.h"
#include "RendezvousDeviceDelegate.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include <platform/CHIPDeviceLayer.h>
Expand All @@ -47,7 +47,7 @@ using namespace ::chip::DeviceLayer;
extern LEDWidget statusLED1;
extern LEDWidget statusLED2;
extern WiFiWidget wifiLED;
extern RendezvousSession * rendezvousSession;
extern RendezvousDeviceDelegate * rendezvousDelegate;

void EchoDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg)
{
Expand All @@ -65,9 +65,9 @@ void EchoDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, int

// Since the commissioner device does not yet have a mechanism to discover the IP address
// of the peripheral, the following code send it over the current Rendezvous session.
if (rendezvousSession != NULL)
if (rendezvousDelegate != NULL)
{
rendezvousSession->Send(ipAddrStr);
rendezvousDelegate->Send(ipAddrStr);
}
}
wifiLED.Set(true);
Expand Down
4 changes: 2 additions & 2 deletions examples/wifi-echo/server/esp32/main/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ SecureSessionMgr<Transport::UDP, // IPV6

} // namespace

void PairingComplete(Optional<NodeId> peerNodeId, uint16_t peerKeyId, uint16_t localKeyId, SecurePairingSession * pairing)
void PairingComplete(SecurePairingSession * pairing)
{
Optional<Transport::PeerAddress> peer(Transport::Type::kUndefined);
sessions.NewPairing(peerNodeId, peer, peerKeyId, localKeyId, pairing);
sessions.NewPairing(peer, pairing);
}

// The echo server assumes the platform's networking has been setup already
Expand Down
102 changes: 102 additions & 0 deletions examples/wifi-echo/server/esp32/main/RendezvousDeviceDelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
*
* 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 "RendezvousDeviceDelegate.h"
#include "BluetoothWidget.h"
#include "RendezvousMessageHandler.h"
#include "esp_log.h"
#include <platform/ConfigurationManager.h>
#include <support/CodeUtils.h>
#include <support/ErrorStr.h>
#include <system/SystemPacketBuffer.h>

extern BluetoothWidget bluetoothLED;
extern NodeId kLocalNodeId;
extern void PairingComplete(SecurePairingSession * pairing);

static const char * TAG = "rendezvous-devicedelegate";

using namespace ::chip;

RendezvousDeviceDelegate::RendezvousDeviceDelegate()
{
CHIP_ERROR err = CHIP_NO_ERROR;

uint32_t setupPINCode;
err = DeviceLayer::ConfigurationMgr().GetSetupPinCode(setupPINCode);
SuccessOrExit(err);

mRendezvousSession = new RendezvousSession(this, RendezvousParameters(setupPINCode).SetLocalNodeId(kLocalNodeId));
err = mRendezvousSession->Init();
SuccessOrExit(err);

exit:
if (err != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "RendezvousDeviceDelegate Init failure: %s", ErrorStr(err));
}
}

CHIP_ERROR RendezvousDeviceDelegate::Send(const char * msg)
{
CHIP_ERROR err = CHIP_NO_ERROR;
System::PacketBuffer * buffer;
const size_t msgLen = strlen(msg);

VerifyOrExit(mRendezvousSession, err = CHIP_ERROR_INCORRECT_STATE);

buffer = System::PacketBuffer::NewWithAvailableSize(msgLen);
memcpy(buffer->Start(), msg, msgLen);
buffer->SetDataLength(msgLen);

err = mRendezvousSession->SendMessage(buffer);

exit:
return err;
}

void RendezvousDeviceDelegate::OnRendezvousError(CHIP_ERROR err)
{
ESP_LOGI(TAG, "OnRendezvousError: %s", ErrorStr(err));
}

void RendezvousDeviceDelegate::OnRendezvousConnectionOpened()
{
ESP_LOGI(TAG, "OnRendezvousConnectionOpened");

PairingComplete(&mRendezvousSession->GetPairingSession());
bluetoothLED.Set(true);
}

void RendezvousDeviceDelegate::OnRendezvousConnectionClosed()
{
ESP_LOGI(TAG, "OnRendezvousConnectionClosed");
bluetoothLED.Set(false);
}

void RendezvousDeviceDelegate::OnRendezvousMessageReceived(PacketBuffer * buffer)
{
ESP_LOGI(TAG, "OnRendezvousMessageReceived");

// When paired, offer the RendezvousMessageHandler a chance to process the message.
CHIP_ERROR err = RendezvousMessageHandler::HandleMessageReceived(buffer);
if (err == CHIP_ERROR_INVALID_MESSAGE_TYPE)
{
// If the handler did not recognize the message, treat it as an echo request.
mRendezvousSession->SendMessage(buffer);
}
}
144 changes: 0 additions & 144 deletions examples/wifi-echo/server/esp32/main/RendezvousSession.cpp

This file was deleted.

Loading

0 comments on commit 44c9814

Please sign in to comment.