diff --git a/src/controller/CHIPDevice.h b/src/controller/CHIPDevice.h index bf3d55c4e6c5f0..b3d494f6d746bb 100644 --- a/src/controller/CHIPDevice.h +++ b/src/controller/CHIPDevice.h @@ -55,6 +55,13 @@ using DeviceTransportMgr = TransportMgr; +struct ControllerDeviceInitParams +{ + DeviceTransportMgr * transportMgr = nullptr; + SecureSessionMgr * sessionMgr = nullptr; + Inet::InetLayer * inetLayer = nullptr; +}; + class DLL_EXPORT Device { public: @@ -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; } @@ -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; diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index 68a474c9a4b103..2525ea39b84678 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -100,6 +100,12 @@ 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; @@ -107,10 +113,10 @@ CHIP_ERROR DeviceController::Init(NodeId localDeviceId, PersistentStorageDelegat 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 { @@ -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) { @@ -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; @@ -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); } } @@ -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; @@ -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) { @@ -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. @@ -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); diff --git a/src/controller/CHIPDeviceController.h b/src/controller/CHIPDeviceController.h index 05fa3cd34b66a7..129ee96784286c 100644 --- a/src/controller/CHIPDeviceController.h +++ b/src/controller/CHIPDeviceController.h @@ -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: @@ -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); @@ -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; @@ -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); diff --git a/src/controller/python/ChipDeviceController-ScriptBinding.cpp b/src/controller/python/ChipDeviceController-ScriptBinding.cpp index 6972f1cd0174a4..47b6424d69d595 100644 --- a/src/controller/python/ChipDeviceController-ScriptBinding.cpp +++ b/src/controller/python/ChipDeviceController-ScriptBinding.cpp @@ -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: diff --git a/src/controller/python/chip/internal/CommissionerImpl.cpp b/src/controller/python/chip/internal/CommissionerImpl.cpp index 0aa1ab48c6adc0..f789045f906455 100644 --- a/src/controller/python/chip/internal/CommissionerImpl.cpp +++ b/src/controller/python/chip/internal/CommissionerImpl.cpp @@ -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)