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

[controller] Add IP rendezvous feature to CHIP Device controller #4050

Merged
merged 16 commits into from
Dec 9, 2020
Merged
21 changes: 16 additions & 5 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,24 +468,35 @@ CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, RendezvousParam
VerifyOrExit(mState == State::Initialized, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mDeviceBeingPaired == kNumMaxActiveDevices, err = CHIP_ERROR_INCORRECT_STATE);

#if CONFIG_DEVICE_LAYER && CONFIG_NETWORK_LAYER_BLE
if (!params.HasBleLayer())
if (params.GetPeerAddress().GetTransportType() == Transport::Type::kBle)
{
params.SetBleLayer(DeviceLayer::ConnectivityMgr().GetBleLayer());
}
#if CONFIG_DEVICE_LAYER && CONFIG_NETWORK_LAYER_BLE
if (!params.HasBleLayer())
{
params.SetBleLayer(DeviceLayer::ConnectivityMgr().GetBleLayer());
}
#endif // CONFIG_DEVICE_LAYER && CONFIG_NETWORK_LAYER_BLE
}

mDeviceBeingPaired = GetInactiveDeviceIndex();
VerifyOrExit(mDeviceBeingPaired < kNumMaxActiveDevices, err = CHIP_ERROR_NO_MEMORY);
device = &mActiveDevices[mDeviceBeingPaired];

mIsIPRendezvous = (params.GetPeerAddress().GetTransportType() != Transport::Type::kBle);
mRendezvousSession = chip::Platform::New<RendezvousSession>(this);
VerifyOrExit(mRendezvousSession != nullptr, err = CHIP_ERROR_NO_MEMORY);
err = mRendezvousSession->Init(params.SetLocalNodeId(mLocalDeviceId).SetRemoteNodeId(remoteDeviceId), mTransportMgr);
SuccessOrExit(err);

device->Init(mTransportMgr, mSessionManager, mInetLayer, remoteDeviceId, remotePort, interfaceId);

if (params.GetPeerAddress().GetTransportType() != Transport::Type::kBle)
erjiaqing marked this conversation as resolved.
Show resolved Hide resolved
{
// IP Rendezvous
device->SetAddress(params.GetPeerAddress().GetIPAddress());
mRendezvousSession->OnRendezvousConnectionOpened();
}

exit:
if (err != CHIP_NO_ERROR)
{
Expand Down Expand Up @@ -669,7 +680,7 @@ void DeviceCommissioner::OnRendezvousStatusUpdate(RendezvousSessionDelegate::Sta
ChipLogDetail(Controller, "Remote device completed SPAKE2+ handshake\n");
mRendezvousSession->GetPairingSession().ToSerializable(device->GetPairing());

if (mPairingDelegate != nullptr)
if (!mIsIPRendezvous && mPairingDelegate != nullptr)
{
mPairingDelegate->OnNetworkCredentialsRequested(mRendezvousSession);
}
Expand Down
4 changes: 4 additions & 0 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController, public Rendezvous
If no device is currently being paired, this value will be kNumMaxPairedDevices. */
uint16_t mDeviceBeingPaired;

/* This is a temporary flag, when using IP rendezvous, we need to disable network provisioning.
In the future, network provisioning will no longer be a part of rendezvous procedure. */
bool mIsIPRendezvous;
erjiaqing marked this conversation as resolved.
Show resolved Hide resolved

/* This field is true when device pairing information changes, e.g. a new device is paired, or
the pairing for a device is removed. The DeviceCommissioner uses this to decide when to
persist the device list */
Expand Down
3 changes: 3 additions & 0 deletions src/controller/CHIPDeviceController_deprecated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ CHIP_ERROR ChipDeviceController::ConnectDevice(NodeId remoteDeviceId, Rendezvous
mOnComplete.Response = onMessageReceived;
mOnError = onError;

// TODO: Should call mOnNewConnected when rendezvous completed
mOnNewConnection(this, nullptr, mAppReqState);

exit:
return err;
}
Expand Down
1 change: 1 addition & 0 deletions src/controller/python/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ shared_library("ChipDeviceCtrl") {
"ChipDeviceController-ScriptBinding.cpp",
"ChipDeviceController-ScriptDevicePairingDelegate.cpp",
"ChipDeviceController-ScriptDevicePairingDelegate.h",
"ChipDeviceController-StorageDelegate.h",
]

public_deps = [
Expand Down
37 changes: 34 additions & 3 deletions src/controller/python/ChipDeviceController-ScriptBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
#endif /* CONFIG_NETWORK_LAYER_BLE */

#include "ChipDeviceController-ScriptDevicePairingDelegate.h"
#include "ChipDeviceController-StorageDelegate.h"

#include <controller/CHIPDeviceController_deprecated.h>
#include <support/CHIPMem.h>
#include <support/CodeUtils.h>
#include <support/DLLUtil.h>
#include <support/logging/CHIPLogging.h>

using namespace chip;
using namespace chip::Ble;
using namespace chip::DeviceController;

Expand Down Expand Up @@ -125,6 +127,7 @@ class BleDisconnectEvent : public BleEventBase

static chip::System::Layer sSystemLayer;
static chip::Inet::InetLayer sInetLayer;
static chip::Controller::PythonPersistentStorageDelegate sStorageDelegate;

// NOTE: Remote device ID is in sync with the echo server device id
// At some point, we may want to add an option to connect to a device without
Expand Down Expand Up @@ -170,6 +173,9 @@ CHIP_ERROR nl_Chip_DeviceController_DeleteDeviceController(chip::DeviceControlle
CHIP_ERROR nl_Chip_DeviceController_Connect(chip::DeviceController::ChipDeviceController * devCtrl, BLE_CONNECTION_OBJECT connObj,
uint32_t setupPinCode, OnConnectFunct onConnect, OnMessageFunct onMessage,
OnErrorFunct onError);
CHIP_ERROR nl_Chip_DeviceController_ConnectIP(chip::DeviceController::ChipDeviceController * devCtrl, const char * peerAddrStr,
uint32_t setupPINCode, OnConnectFunct onConnect, OnMessageFunct onMessage,
OnErrorFunct onError);

// Network Provisioning
CHIP_ERROR
Expand Down Expand Up @@ -204,7 +210,7 @@ CHIP_ERROR nl_Chip_DeviceController_NewDeviceController(chip::DeviceController::
*outDevCtrl = new chip::DeviceController::ChipDeviceController();
VerifyOrExit(*outDevCtrl != NULL, err = CHIP_ERROR_NO_MEMORY);

err = (*outDevCtrl)->Init(kLocalDeviceId, &sSystemLayer, &sInetLayer);
err = (*outDevCtrl)->Init(kLocalDeviceId, &sSystemLayer, &sInetLayer, nullptr, &sStorageDelegate);
SuccessOrExit(err);

exit:
Expand Down Expand Up @@ -530,10 +536,35 @@ void nl_Chip_DeviceController_SetLogFilter(uint8_t category)
CHIP_ERROR nl_Chip_DeviceController_Connect(chip::DeviceController::ChipDeviceController * devCtrl, BLE_CONNECTION_OBJECT connObj,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated question: why do we have a 'nl' prefix here? I am generally used to nl meaning 'nest labs' and then we can replace with just 'chip_DeviceController_connect' and similar and omit the nl prefix.

uint32_t setupPINCode, OnConnectFunct onConnect, OnMessageFunct onMessage,
OnErrorFunct onError)
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::RendezvousParameters params = chip::RendezvousParameters()
.SetPeerAddress(Transport::PeerAddress(Transport::Type::kBle))
.SetSetupPINCode(setupPINCode)
.SetConnectionObject(connObj)
.SetBleLayer(&sBle);
err = devCtrl->ConnectDevice(kRemoteDeviceId, params, (void *) devCtrl, onConnect, onMessage, onError);
SuccessOrExit(err);

exit:
erjiaqing marked this conversation as resolved.
Show resolved Hide resolved
return err;
}

CHIP_ERROR nl_Chip_DeviceController_ConnectIP(chip::DeviceController::ChipDeviceController * devCtrl, const char * peerAddrStr,
uint32_t setupPINCode, OnConnectFunct onConnect, OnMessageFunct onMessage,
OnErrorFunct onError)
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::RendezvousParameters params =
chip::RendezvousParameters().SetSetupPINCode(setupPINCode).SetConnectionObject(connObj).SetBleLayer(&sBle);
chip::Inet::IPAddress peerAddr;
chip::Transport::PeerAddress addr;
chip::RendezvousParameters params = chip::RendezvousParameters().SetSetupPINCode(setupPINCode);

VerifyOrExit(chip::Inet::IPAddress::FromString(peerAddrStr, peerAddr), err = CHIP_ERROR_INVALID_ARGUMENT);
addr.SetIPAddress(peerAddr);
// TODO: IP rendezvous should use TCP connection.
addr.SetTransportType(chip::Transport::Type::kUdp);
params.SetPeerAddress(addr);
params.SetDiscriminator(0);
err = devCtrl->ConnectDevice(kRemoteDeviceId, params, (void *) devCtrl, onConnect, onMessage, onError);
SuccessOrExit(err);

Expand Down
30 changes: 30 additions & 0 deletions src/controller/python/ChipDeviceController-StorageDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <controller/CHIPPersistentStorageDelegate.h>

class PythonPersistentStorageDelegate;

extern "C" {
erjiaqing marked this conversation as resolved.
Show resolved Hide resolved
typedef void (*GetKeyValueFunct)(const uint8_t * key, uint8_t * value, uint16_t * size);
typedef void (*SetKeyValueFunct)(const uint8_t * key, const uint8_t * value);
typedef void (*DeleteKeyValueFunct)(const uint8_t * key);
}

namespace chip {
namespace Controller {

// TODO: Implement this.
class PythonPersistentStorageDelegate : public PersistentStorageDelegate
{
public:
PythonPersistentStorageDelegate() {}
~PythonPersistentStorageDelegate() {}
erjiaqing marked this conversation as resolved.
Show resolved Hide resolved
void SetDelegate(PersistentStorageResultDelegate * delegate) override{};
void GetKeyValue(const char * key) override{};
erjiaqing marked this conversation as resolved.
Show resolved Hide resolved
CHIP_ERROR GetKeyValue(const char * key, char * value, uint16_t & size) override { return CHIP_NO_ERROR; };
erjiaqing marked this conversation as resolved.
Show resolved Hide resolved
void SetKeyValue(const char * key, const char * value) override{};
void DeleteKeyValue(const char * key) override{};
};

} // namespace Controller
} // namespace chip
15 changes: 10 additions & 5 deletions src/controller/python/chip-device-ctrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ def do_btpconnect(self, line):

def do_connect(self, line):
"""
connect (via BLE) <setup pin code>
connect -ip <ip address> <setup pin code>
connect -ble <setup pin code>

connect command is used for establishing a rendezvous session to the device.
currently, only connect using setupPinCode is supported.
Expand All @@ -376,14 +377,18 @@ def do_connect(self, line):

try:
args = shlex.split(line)
if not args:
if len(args) <= 1:
print("Usage:")
self.do_help("connect SetupPinCode")
return
if len(args) > 1:
print("Unexpected argument: " + args[1])
if args[0] == "-ip" and len(args) == 3:
self.devCtrl.ConnectIP(args[1].encode("utf-8"), int(args[2]))
elif args[0] == "-ble" and len(args) == 2:
self.devCtrl.Connect(FAKE_CONN_OBJ_VALUE, int(args[1]))
else:
print("Usage:")
self.do_help("connect SetupPinCode")
return
self.devCtrl.Connect(FAKE_CONN_OBJ_VALUE, int(args[0]))
except ChipStack.ChipStackException as ex:
print(str(ex))
return
Expand Down
Loading