Skip to content

Commit

Permalink
[ble/rendezvous] Decouple BLE transport from Rendezvous
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing committed Mar 12, 2021
1 parent 0b19aa2 commit 68c3f72
Show file tree
Hide file tree
Showing 15 changed files with 500 additions and 124 deletions.
15 changes: 11 additions & 4 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,19 @@ void InitServer(AppDelegate * delegate)
gAdvDelegate.SetDelegate(delegate);

// Init transport before operations with secure session mgr.
err = gTransports.Init(UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(kIPAddressType_IPv6)

#if INET_CONFIG_ENABLE_IPV4
err = gTransports.Init(UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(kIPAddressType_IPv6),
UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(kIPAddressType_IPv4));
#else
err = gTransports.Init(UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(kIPAddressType_IPv6));
,
UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(kIPAddressType_IPv4)
#endif
#if CONFIG_NETWORK_LAYER_BLE
,
BleListenParameters(DeviceLayer::ConnectivityMgr().GetBleLayer())
#endif
);

// DeviceLayer::ConnectivityMgr().GetBleLayer())
SuccessOrExit(err);

err = gSessions.Init(chip::kTestDeviceNodeId, &DeviceLayer::SystemLayer, &gTransports, &gAdminPairings);
Expand Down
12 changes: 9 additions & 3 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
#include <transport/AdminPairingTable.h>
#include <transport/TransportMgr.h>
#include <transport/raw/UDP.h>
#include <transport/raw/BLE.h>

using DemoTransportMgr = chip::TransportMgr<chip::Transport::UDP
#if INET_CONFIG_ENABLE_IPV4
using DemoTransportMgr = chip::TransportMgr<chip::Transport::UDP, chip::Transport::UDP>;
#else
using DemoTransportMgr = chip::TransportMgr<chip::Transport::UDP>;
,
chip::Transport::UDP
#endif
#if CONFIG_NETWORK_LAYER_BLE
,
chip::Transport::BLE<4>
#endif
>;

/**
* Initialize DataModelHandler and start CHIP datamodel server, the server
Expand Down
16 changes: 8 additions & 8 deletions src/ble/BLEEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ BLE_ERROR BLEEndPoint::HandleConnectComplete()
StopConnectTimer();

// We've successfully completed the BLE transport protocol handshake, so let the application know we're open for business.
if (OnConnectComplete != nullptr)
if (mBleTransport != nullptr)
{
// Indicate connect complete to next-higher layer.
OnConnectComplete(this, BLE_NO_ERROR);
mBleTransport->OnEndPointConnectComplete(this, BLE_NO_ERROR);
}
else
{
Expand Down Expand Up @@ -434,16 +434,16 @@ void BLEEndPoint::DoCloseCallback(uint8_t state, uint8_t flags, BLE_ERROR err)
{
if (state == kState_Connecting)
{
if (OnConnectComplete != nullptr)
if (mBleTransport != nullptr)
{
OnConnectComplete(this, err);
mBleTransport->OnEndPointConnectComplete(this, err);
}
}
else
{
if (OnConnectionClosed != nullptr)
if (mBleTransport != nullptr)
{
OnConnectionClosed(this, err);
mBleTransport->OnEndPointConnectionClosed(this, err);
}
}

Expand Down Expand Up @@ -1427,10 +1427,10 @@ BLE_ERROR BLEEndPoint::Receive(PacketBufferHandle data)
else
#endif
// If we have a message received callback, and end point is not closing...
if (OnMessageReceived && mState != kState_Closing)
if (mBleTransport != nullptr && mState != kState_Closing)
{
// Pass received message up the stack.
OnMessageReceived(this, std::move(full_packet));
mBleTransport->OnEndPointMessageReceived(this, std::move(full_packet));
}
}

Expand Down
52 changes: 51 additions & 1 deletion src/ble/BleLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

#include <core/CHIPEncoding.h>
#include <support/CodeUtils.h>
#include <support/ReturnMacros.h>
#include <support/logging/CHIPLogging.h>

// Magic values expected in first 2 bytes of valid BLE transport capabilities request or response:
Expand Down Expand Up @@ -368,6 +369,31 @@ BLE_ERROR BleLayer::NewBleConnection(void * appState, const uint16_t connDiscrim
return err;
}

BLE_ERROR BleLayer::NewBleConnectionById(uint16_t connDiscriminator)
{

VerifyOrReturnError(mState == kState_Initialized, BLE_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mConnectionDelegate != nullptr, BLE_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mBleTransport != nullptr, BLE_ERROR_INCORRECT_STATE);

mConnectionDelegate->OnConnectionComplete = OnConnectionComplete;
mConnectionDelegate->OnConnectionError = OnConnectionError;
// TODO: Passing BleLayer two times is not clean, this should be fixed in follow-ups.
mConnectionDelegate->NewConnection(this, this, connDiscriminator);

return BLE_NO_ERROR;
}

BLE_ERROR BleLayer::NewBleConnectionByObject(BLE_CONNECTION_OBJECT connObj)
{
VerifyOrReturnError(mState == kState_Initialized, BLE_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mBleTransport != nullptr, BLE_ERROR_INCORRECT_STATE);

OnConnectionComplete(this, connObj);

return BLE_NO_ERROR;
}

BLE_ERROR BleLayer::NewBleEndPoint(BLEEndPoint ** retEndPoint, BLE_CONNECTION_OBJECT connObj, BleRole role, bool autoClose)
{
*retEndPoint = nullptr;
Expand All @@ -390,6 +416,7 @@ BLE_ERROR BleLayer::NewBleEndPoint(BLEEndPoint ** retEndPoint, BLE_CONNECTION_OB
}

(*retEndPoint)->Init(this, connObj, role, autoClose);
(*retEndPoint)->mBleTransport = mBleTransport;

#if CHIP_ENABLE_CHIPOBLE_TEST
mTestBleEndPoint = *retEndPoint;
Expand All @@ -409,7 +436,8 @@ BLE_ERROR BleLayer::HandleBleTransportConnectionInitiated(BLE_CONNECTION_OBJECT
err = NewBleEndPoint(&newEndPoint, connObj, kBleRole_Peripheral, false);
SuccessOrExit(err);

newEndPoint->mAppState = mAppState;
newEndPoint->mAppState = mBleTransport;
newEndPoint->mBleTransport = mBleTransport;

err = newEndPoint->Receive(std::move(pBuf));
SuccessOrExit(err); // If we fail here, end point will have already released connection and freed itself.
Expand Down Expand Up @@ -721,6 +749,28 @@ BleTransportProtocolVersion BleLayer::GetHighestSupportedProtocolVersion(const B
return retVersion;
}

void BleLayer::OnConnectionComplete(void * appState, BLE_CONNECTION_OBJECT connObj)
{
BleLayer * layer = reinterpret_cast<BleLayer *>(appState);
BLEEndPoint * endPoint = nullptr;
BLE_ERROR err = layer->NewBleEndPoint(&endPoint, connObj, kBleRole_Central, true);

SuccessOrExit(err);
layer->mBleTransport->OnBleConnectionComplete(endPoint);

exit:
if (err != CHIP_NO_ERROR)
{
OnConnectionError(layer, err);
}
}

void BleLayer::OnConnectionError(void * appState, BLE_ERROR err)
{
BleLayer * layer = reinterpret_cast<BleLayer *>(appState);
layer->mBleTransport->OnBleConnectionError(err);
}

} /* namespace Ble */
} /* namespace chip */

Expand Down
23 changes: 21 additions & 2 deletions src/ble/BleLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,28 @@ constexpr size_t kCapabilitiesResponseWindowSizeLength = 1;
constexpr size_t kCapabilitiesResponseLength(kCapabilitiesResponseMagicnumLength + kCapabilitiesResponseL2capMtuLength +
kCapabilitiesResponseSelectedProtocolVersionLength +
kCapabilitiesResponseWindowSizeLength);
class BleTransportDelegate
{
public:
virtual ~BleTransportDelegate() = default;
virtual void OnBleConnectionComplete(BLEEndPoint * endpoint) = 0;
virtual void OnBleConnectionError(BLE_ERROR err) = 0;

virtual void OnEndPointConnectComplete(BLEEndPoint * endPoint, BLE_ERROR err) = 0;
virtual void OnEndPointMessageReceived(BLEEndPoint * endPoint, PacketBufferHandle msg) = 0;
virtual void OnEndPointConnectionClosed(BLEEndPoint * endPoint, BLE_ERROR err) = 0;

virtual CHIP_ERROR SetEndPoint(BLEEndPoint * endPoint) = 0;
};

class BleLayerObject
{
friend class BleLayer;

public:
// Public data members:
BleLayer * mBle; ///< [READ-ONLY] Pointer to the BleLayer object that owns this object.
BleLayer * mBle; ///< [READ-ONLY] Pointer to the BleLayer object that owns this object.
BleTransportDelegate * mBleTransport;
void * mAppState; ///< Generic pointer to app-specific data associated with the object.

protected:
Expand Down Expand Up @@ -251,7 +265,7 @@ class DLL_EXPORT BleLayer
kState_Initialized = 1
} mState; ///< [READ-ONLY] Current state

void * mAppState;
BleTransportDelegate * mBleTransport;

typedef void (*BleConnectionReceivedFunct)(BLEEndPoint * newEndPoint);
BleConnectionReceivedFunct OnChipBleConnectReceived;
Expand All @@ -267,6 +281,8 @@ class DLL_EXPORT BleLayer
BLE_ERROR NewBleConnection(void * appState, uint16_t connDiscriminator,
BleConnectionDelegate::OnConnectionCompleteFunct onConnectionComplete,
BleConnectionDelegate::OnConnectionErrorFunct onConnectionError);
BLE_ERROR NewBleConnectionById(uint16_t connDiscriminator);
BLE_ERROR NewBleConnectionByObject(BLE_CONNECTION_OBJECT connObj);
BLE_ERROR NewBleEndPoint(BLEEndPoint ** retEndPoint, BLE_CONNECTION_OBJECT connObj, BleRole role, bool autoClose);

chip::System::Error ScheduleWork(chip::System::Layer::TimerCompleteFunct aComplete, void * aAppState)
Expand Down Expand Up @@ -361,6 +377,9 @@ class DLL_EXPORT BleLayer
BLE_ERROR HandleBleTransportConnectionInitiated(BLE_CONNECTION_OBJECT connObj, System::PacketBufferHandle pBuf);

static BleTransportProtocolVersion GetHighestSupportedProtocolVersion(const BleTransportCapabilitiesRequestMessage & reqMsg);

static void OnConnectionComplete(void * appState, BLE_CONNECTION_OBJECT connObj);
static void OnConnectionError(void * appState, BLE_ERROR err);
};

} /* namespace Ble */
Expand Down
4 changes: 4 additions & 0 deletions src/controller/CHIPDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ CHIP_ERROR Device::LoadSecureSessionParameters(ResetTransport resetNeeded)
#if INET_CONFIG_ENABLE_IPV4
,
Transport::UdpListenParameters(mInetLayer).SetAddressType(kIPAddressType_IPv4).SetListenPort(mListenPort)
#endif
#if CONFIG_NETWORK_LAYER_BLE
,
Transport::BleListenParameters(mBleLayer)
#endif
);
SuccessOrExit(err);
Expand Down
40 changes: 35 additions & 5 deletions src/controller/CHIPDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <app/InteractionModelEngine.h>
#include <app/util/CHIPDeviceCallbacksMgr.h>
#include <app/util/basic-types.h>
#include <ble/BleLayer.h>
#include <core/CHIPCallback.h>
#include <core/CHIPCore.h>
#include <setup_payload/SetupPayload.h>
Expand All @@ -38,6 +39,7 @@
#include <transport/PASESession.h>
#include <transport/SecureSessionMgr.h>
#include <transport/TransportMgr.h>
#include <transport/raw/BLE.h>
#include <transport/raw/MessageHeader.h>
#include <transport/raw/UDP.h>

Expand All @@ -52,6 +54,10 @@ using DeviceTransportMgr = TransportMgr<Transport::UDP /* IPv6 */
#if INET_CONFIG_ENABLE_IPV4
,
Transport::UDP /* IPv4 */
#endif
#if CONFIG_NETWORK_LAYER_BLE
,
Transport::BLE<4> /* BLE */
#endif
>;

Expand Down Expand Up @@ -143,17 +149,24 @@ class DLL_EXPORT Device
* @param[in] transportMgr Transport manager object pointer
* @param[in] sessionMgr Secure session manager object pointer
* @param[in] inetLayer InetLayer object pointer
* @param[in] bleLayer BleLayer object pointer
* @param[in] listenPort Port on which controller is listening (typically CHIP_PORT)
* @param[in] admin Local administrator that's initializing this device object
*/
void Init(DeviceTransportMgr * transportMgr, SecureSessionMgr * sessionMgr, Inet::InetLayer * inetLayer, uint16_t listenPort,
Transport::AdminId admin)
void Init(DeviceTransportMgr * transportMgr, SecureSessionMgr * sessionMgr, Inet::InetLayer * inetLayer
#if CONFIG_NETWORK_LAYER_BLE
,
Ble::BleLayer * bleLayer
#endif
,
uint16_t listenPort, Transport::AdminId admin)
{
mTransportMgr = transportMgr;
mSessionManager = sessionMgr;
mInetLayer = inetLayer;
mListenPort = listenPort;
mAdminId = admin;
mBleLayer = bleLayer;
}

/**
Expand All @@ -170,15 +183,27 @@ class DLL_EXPORT Device
* @param[in] transportMgr Transport manager object pointer
* @param[in] sessionMgr Secure session manager object pointer
* @param[in] inetLayer InetLayer object pointer
* @param[in] bleLayer BleLayer object pointer
* @param[in] listenPort Port on which controller is listening (typically CHIP_PORT)
* @param[in] deviceId Node ID of the device
* @param[in] peerAddress The location of the peer. MUST be of type Transport::Type::kUdp
* @param[in] admin Local administrator that's initializing this device object
*/
void Init(DeviceTransportMgr * transportMgr, SecureSessionMgr * sessionMgr, Inet::InetLayer * inetLayer, uint16_t listenPort,
NodeId deviceId, const Transport::PeerAddress & peerAddress, Transport::AdminId admin)
void Init(DeviceTransportMgr * transportMgr, SecureSessionMgr * sessionMgr, Inet::InetLayer * inetLayer
#if CONFIG_NETWORK_LAYER_BLE
,
Ble::BleLayer * bleLayer
#endif
,
uint16_t listenPort, NodeId deviceId, const Transport::PeerAddress & peerAddress, Transport::AdminId admin)
{
Init(transportMgr, sessionMgr, inetLayer, mListenPort, admin);
Init(transportMgr, sessionMgr, inetLayer
#if CONFIG_NETWORK_LAYER_BLE
,
bleLayer
#endif
,
mListenPort, admin);
mDeviceId = deviceId;
mState = ConnectionState::Connecting;

Expand Down Expand Up @@ -292,6 +317,7 @@ class DLL_EXPORT Device
mSessionManager = nullptr;
mStatusDelegate = nullptr;
mInetLayer = nullptr;
mBleLayer = nullptr;
}

NodeId GetDeviceId() const { return mDeviceId; }
Expand Down Expand Up @@ -332,6 +358,10 @@ class DLL_EXPORT Device
bool mActive = false;
ConnectionState mState = ConnectionState::NotConnected;

#if CONFIG_NETWORK_LAYER_BLE
Ble::BleLayer * mBleLayer;
#endif

PASESessionSerializable mPairing;

DeviceStatusDelegate * mStatusDelegate = nullptr;
Expand Down
Loading

0 comments on commit 68c3f72

Please sign in to comment.