Skip to content

Commit

Permalink
[controller] Create ControllerInitParams class init params (#5747)
Browse files Browse the repository at this point in the history
* [controller] Create InitParams class

* Remove Setter and Deprecated
  • Loading branch information
erjiaqing authored Apr 1, 2021
1 parent ebc41b5 commit 3cb69ff
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 26 deletions.
30 changes: 16 additions & 14 deletions src/controller/CHIPDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ using DeviceTransportMgr = TransportMgr<Transport::UDP /* IPv6 */
#endif
>;

struct ControllerDeviceInitParams
{
DeviceTransportMgr * transportMgr = nullptr;
SecureSessionMgr * sessionMgr = nullptr;
Inet::InetLayer * inetLayer = nullptr;
};

class DLL_EXPORT Device
{
public:
Expand Down Expand Up @@ -140,18 +147,15 @@ class DLL_EXPORT Device
* that of this device object. If these objects are freed, while the device object is
* still using them, it can lead to unknown behavior and crashes.
*
* @param[in] transportMgr Transport manager object pointer
* @param[in] sessionMgr Secure session manager object pointer
* @param[in] inetLayer InetLayer object pointer
* @param[in] params Wrapper object for transport manager etc.
* @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(ControllerDeviceInitParams params, uint16_t listenPort, Transport::AdminId admin)
{
mTransportMgr = transportMgr;
mSessionManager = sessionMgr;
mInetLayer = inetLayer;
mTransportMgr = params.transportMgr;
mSessionManager = params.sessionMgr;
mInetLayer = params.inetLayer;
mListenPort = listenPort;
mAdminId = admin;
}
Expand All @@ -167,18 +171,16 @@ class DLL_EXPORT Device
* uninitialzed/unpaired device objects. The object is initialized only when the device
* is actually paired.
*
* @param[in] transportMgr Transport manager object pointer
* @param[in] sessionMgr Secure session manager object pointer
* @param[in] inetLayer InetLayer object pointer
* @param[in] params Wrapper object for transport manager etc.
* @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(ControllerDeviceInitParams params, uint16_t listenPort, NodeId deviceId, const Transport::PeerAddress & peerAddress,
Transport::AdminId admin)
{
Init(transportMgr, sessionMgr, inetLayer, mListenPort, admin);
Init(params, mListenPort, admin);
mDeviceId = deviceId;
mState = ConnectionState::Connecting;

Expand Down
36 changes: 27 additions & 9 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,23 @@ DeviceController::DeviceController()

CHIP_ERROR DeviceController::Init(NodeId localDeviceId, PersistentStorageDelegate * storageDelegate, System::Layer * systemLayer,
Inet::InetLayer * inetLayer)
{
return Init(localDeviceId,
ControllerInitParams{ .storageDelegate = storageDelegate, .systemLayer = systemLayer, .inetLayer = inetLayer });
}

CHIP_ERROR DeviceController::Init(NodeId localDeviceId, ControllerInitParams params)
{
CHIP_ERROR err = CHIP_NO_ERROR;

Transport::AdminPairingInfo * admin = nullptr;

VerifyOrExit(mState == State::NotInitialized, err = CHIP_ERROR_INCORRECT_STATE);

if (systemLayer != nullptr && inetLayer != nullptr)
if (params.systemLayer != nullptr && params.inetLayer != nullptr)
{
mSystemLayer = systemLayer;
mInetLayer = inetLayer;
mSystemLayer = params.systemLayer;
mInetLayer = params.inetLayer;
}
else
{
Expand All @@ -126,7 +132,7 @@ CHIP_ERROR DeviceController::Init(NodeId localDeviceId, PersistentStorageDelegat
VerifyOrExit(mSystemLayer != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrExit(mInetLayer != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT);

mStorageDelegate = storageDelegate;
mStorageDelegate = params.storageDelegate;

if (mStorageDelegate != nullptr)
{
Expand Down Expand Up @@ -257,7 +263,7 @@ CHIP_ERROR DeviceController::GetDevice(NodeId deviceId, const SerializedDevice &
ReturnErrorOnFailure(err);
}

device->Init(mTransportMgr, mSessionManager, mInetLayer, mListenPort, mAdminId);
device->Init(GetControllerDeviceInitParams(), mListenPort, mAdminId);
}

*out_device = device;
Expand Down Expand Up @@ -300,7 +306,7 @@ CHIP_ERROR DeviceController::GetDevice(NodeId deviceId, Device ** out_device)
err = device->Deserialize(deviceInfo);
VerifyOrExit(err == CHIP_NO_ERROR, ReleaseDevice(device));

device->Init(mTransportMgr, mSessionManager, mInetLayer, mListenPort, mAdminId);
device->Init(GetControllerDeviceInitParams(), mListenPort, mAdminId);
}
}

Expand Down Expand Up @@ -511,6 +517,11 @@ CHIP_ERROR DeviceController::SetPairedDeviceList(const char * serialized)

void DeviceController::OnPersistentStorageStatus(const char * key, Operation op, CHIP_ERROR err) {}

ControllerDeviceInitParams DeviceController::GetControllerDeviceInitParams()
{
return ControllerDeviceInitParams{ .transportMgr = mTransportMgr, .sessionMgr = mSessionManager, .inetLayer = mInetLayer };
}

DeviceCommissioner::DeviceCommissioner()
{
mPairingDelegate = nullptr;
Expand All @@ -537,7 +548,14 @@ CHIP_ERROR DeviceCommissioner::Init(NodeId localDeviceId, PersistentStorageDeleg
DevicePairingDelegate * pairingDelegate, System::Layer * systemLayer,
Inet::InetLayer * inetLayer)
{
ReturnErrorOnFailure(DeviceController::Init(localDeviceId, storageDelegate, systemLayer, inetLayer));
return Init(localDeviceId,
ControllerInitParams{ .storageDelegate = storageDelegate, .systemLayer = systemLayer, .inetLayer = inetLayer },
pairingDelegate);
}

CHIP_ERROR DeviceCommissioner::Init(NodeId localDeviceId, ControllerInitParams params, DevicePairingDelegate * pairingDelegate)
{
ReturnErrorOnFailure(DeviceController::Init(localDeviceId, params));

if (LoadKeyId(mStorageDelegate, mNextKeyId) != CHIP_NO_ERROR)
{
Expand Down Expand Up @@ -611,7 +629,7 @@ CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, RendezvousParam
mSessionManager, admin);
SuccessOrExit(err);

device->Init(mTransportMgr, mSessionManager, mInetLayer, mListenPort, remoteDeviceId, udpPeerAddress, admin->GetAdminId());
device->Init(GetControllerDeviceInitParams(), mListenPort, remoteDeviceId, udpPeerAddress, admin->GetAdminId());

// TODO: BLE rendezvous and IP rendezvous should have same logic in the future after BLE becomes a transport and network
// provisiong cluster is ready.
Expand Down Expand Up @@ -662,7 +680,7 @@ CHIP_ERROR DeviceCommissioner::PairTestDeviceWithoutSecurity(NodeId remoteDevice

testSecurePairingSecret->ToSerializable(device->GetPairing());

device->Init(mTransportMgr, mSessionManager, mInetLayer, mListenPort, remoteDeviceId, peerAddress, mAdminId);
device->Init(GetControllerDeviceInitParams(), mListenPort, remoteDeviceId, peerAddress, mAdminId);

device->Serialize(serialized);

Expand Down
14 changes: 14 additions & 0 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ namespace Controller {
constexpr uint16_t kNumMaxActiveDevices = 64;
constexpr uint16_t kNumMaxPairedDevices = 128;

struct ControllerInitParams
{
PersistentStorageDelegate * storageDelegate = nullptr;
System::Layer * systemLayer = nullptr;
Inet::InetLayer * inetLayer = nullptr;
};

class DLL_EXPORT DevicePairingDelegate
{
public:
Expand Down Expand Up @@ -116,6 +123,9 @@ class DLL_EXPORT DeviceController : public SecureSessionMgrDelegate, public Pers
* Init function to be used when there exists a device layer that takes care of initializing
* System::Layer and InetLayer.
*/
CHIP_ERROR Init(NodeId localDeviceId, ControllerInitParams params);

// Note: Future modifications should be made to ControllerInitParams
CHIP_ERROR Init(NodeId localDeviceId, PersistentStorageDelegate * storageDelegate = nullptr,
System::Layer * systemLayer = nullptr, Inet::InetLayer * inetLayer = nullptr);

Expand Down Expand Up @@ -201,6 +211,7 @@ class DLL_EXPORT DeviceController : public SecureSessionMgrDelegate, public Pers
void ReleaseDeviceById(NodeId remoteDeviceId);
CHIP_ERROR InitializePairedDeviceList();
CHIP_ERROR SetPairedDeviceList(const char * pairedDeviceSerializedSet);
ControllerDeviceInitParams GetControllerDeviceInitParams();

Transport::AdminId mAdminId = 0;
Transport::AdminPairingTable mAdmins;
Expand Down Expand Up @@ -258,6 +269,9 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController, public Rendezvous
* Init function to be used when there exists a device layer that takes care of initializing
* System::Layer and InetLayer.
*/
CHIP_ERROR Init(NodeId localDeviceId, ControllerInitParams params, DevicePairingDelegate * pairingDelegate = nullptr);

// Note: Future modifications should be made to ControllerInitParams
CHIP_ERROR Init(NodeId localDeviceId, PersistentStorageDelegate * storageDelegate = nullptr,
DevicePairingDelegate * pairingDelegate = nullptr, System::Layer * systemLayer = nullptr,
Inet::InetLayer * inetLayer = nullptr);
Expand Down
3 changes: 2 additions & 1 deletion src/controller/python/ChipDeviceController-ScriptBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ CHIP_ERROR pychip_DeviceController_NewDeviceController(chip::Controller::DeviceC
{
localDeviceId = kDefaultLocalDeviceId;
}
SuccessOrExit(err = (*outDevCtrl)->Init(localDeviceId, &sStorageDelegate, &sPairingDelegate));
SuccessOrExit(
err = (*outDevCtrl)->Init(localDeviceId, ControllerInitParams{ .storageDelegate = &sStorageDelegate }, &sPairingDelegate));
SuccessOrExit(err = (*outDevCtrl)->ServiceEvents());

exit:
Expand Down
9 changes: 7 additions & 2 deletions src/controller/python/chip/internal/CommissionerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,13 @@ extern "C" chip::Controller::DeviceCommissioner * pychip_internal_Commissioner_N

// System and Inet layers explicitly passed to indicate that the CHIP stack is
// already assumed initialized
err = result->Init(localDeviceId, &gServerStorage, &gPairingDelegate, &chip::DeviceLayer::SystemLayer,
&chip::DeviceLayer::InetLayer);
err = result->Init(localDeviceId,
chip::Controller::ControllerInitParams{
.storageDelegate = &gServerStorage,
.systemLayer = &chip::DeviceLayer::SystemLayer,
.inetLayer = &chip::DeviceLayer::InetLayer,
},
&gPairingDelegate);
});

if (err != CHIP_NO_ERROR)
Expand Down

0 comments on commit 3cb69ff

Please sign in to comment.