diff --git a/examples/all-clusters-app/ameba/main/chipinterface.cpp b/examples/all-clusters-app/ameba/main/chipinterface.cpp index e11e9a0edbe021..d89b3498f3497c 100644 --- a/examples/all-clusters-app/ameba/main/chipinterface.cpp +++ b/examples/all-clusters-app/ameba/main/chipinterface.cpp @@ -146,7 +146,9 @@ static void InitServer(intptr_t context) #endif // Init ZCL Data Model and CHIP App Server - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 1078d37bbde5ff..bc669b34f3e6e4 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -90,14 +90,14 @@ app::Clusters::NetworkCommissioning::Instance class AppCallbacks : public AppDelegate { public: - void OnRendezvousStarted() override { bluetoothLED.Set(true); } - void OnRendezvousStopped() override + void OnCommissioningSessionStarted() override { bluetoothLED.Set(true); } + void OnCommissioningSessionStopped() override { bluetoothLED.Set(false); pairingWindowLED.Set(false); } - void OnPairingWindowOpened() override { pairingWindowLED.Set(true); } - void OnPairingWindowClosed() override { pairingWindowLED.Set(false); } + void OnCommissioningWindowOpened() override { pairingWindowLED.Set(true); } + void OnCommissioningWindowClosed() override { pairingWindowLED.Set(false); } }; AppCallbacks sCallbacks; @@ -109,7 +109,10 @@ constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE; static void InitServer(intptr_t context) { // Init ZCL Data Model and CHIP App Server - chip::Server::GetInstance().Init(&sCallbacks); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + initParams.appDelegate = &sCallbacks; + chip::Server::GetInstance().Init(initParams); // We only have network commissioning on endpoint 0. emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false); diff --git a/examples/all-clusters-app/linux/fuzzing-main.cpp b/examples/all-clusters-app/linux/fuzzing-main.cpp index 043a9cb529da5b..b1736826ef335a 100644 --- a/examples/all-clusters-app/linux/fuzzing-main.cpp +++ b/examples/all-clusters-app/linux/fuzzing-main.cpp @@ -41,7 +41,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t * aData, size_t aSize) VerifyOrDie(PlatformMgr().InitChipStack() == CHIP_NO_ERROR); // ChipLinuxAppMainLoop blocks, and we don't want that here. - VerifyOrDie(Server::GetInstance().Init() == CHIP_NO_ERROR); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + VerifyOrDie(Server::GetInstance().Init(initParams) == CHIP_NO_ERROR); ApplicationInit(); diff --git a/examples/all-clusters-app/mbed/main/AppTask.cpp b/examples/all-clusters-app/mbed/main/AppTask.cpp index 9e1905093a3b06..d011f5f5ae3cdb 100644 --- a/examples/all-clusters-app/mbed/main/AppTask.cpp +++ b/examples/all-clusters-app/mbed/main/AppTask.cpp @@ -63,7 +63,9 @@ int AppTask::Init() 0); // Init ZCL Data Model and start server - error = Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + error = Server::GetInstance().Init(initParams); if (error != CHIP_NO_ERROR) { ChipLogError(NotSpecified, "Server initialization failed: %s", error.AsString()); diff --git a/examples/all-clusters-app/nrfconnect/main/AppTask.cpp b/examples/all-clusters-app/nrfconnect/main/AppTask.cpp index 2ab79a37edd143..94693a40b99a50 100644 --- a/examples/all-clusters-app/nrfconnect/main/AppTask.cpp +++ b/examples/all-clusters-app/nrfconnect/main/AppTask.cpp @@ -130,7 +130,10 @@ CHIP_ERROR AppTask::Init() // Initialize CHIP server SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); - ReturnErrorOnFailure(chip::Server::GetInstance().Init()); + + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams)); #if CONFIG_CHIP_OTA_REQUESTOR InitBasicOTARequestor(); #endif diff --git a/examples/all-clusters-app/p6/src/AppTask.cpp b/examples/all-clusters-app/p6/src/AppTask.cpp index 927bfcf012683f..5ec2cf50dbdcbc 100644 --- a/examples/all-clusters-app/p6/src/AppTask.cpp +++ b/examples/all-clusters-app/p6/src/AppTask.cpp @@ -84,7 +84,9 @@ void NetWorkCommissioningInstInit() static void InitServer(intptr_t context) { // Init ZCL Data Model - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/bridge-app/esp32/main/main.cpp b/examples/bridge-app/esp32/main/main.cpp index 6c86a69c1af9dc..9b85c421ccab69 100644 --- a/examples/bridge-app/esp32/main/main.cpp +++ b/examples/bridge-app/esp32/main/main.cpp @@ -371,7 +371,9 @@ void HandleDeviceStatusChanged(Device * dev, Device::Changed_t itemChangedMask) static void InitServer(intptr_t context) { - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/bridge-app/linux/main.cpp b/examples/bridge-app/linux/main.cpp index 2fec367c1c9f57..80cf24d5e900e0 100644 --- a/examples/bridge-app/linux/main.cpp +++ b/examples/bridge-app/linux/main.cpp @@ -654,7 +654,9 @@ int main(int argc, char * argv[]) chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(true); // Init ZCL Data Model and CHIP App Server - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/light-switch-app/efr32/src/main.cpp b/examples/light-switch-app/efr32/src/main.cpp index 5a587b75371184..cefc50fa5c3e5c 100644 --- a/examples/light-switch-app/efr32/src/main.cpp +++ b/examples/light-switch-app/efr32/src/main.cpp @@ -167,7 +167,9 @@ int main(void) chip::DeviceLayer::PlatformMgr().LockChipStack(); // Init ZCL Data Model - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); chip::DeviceLayer::PlatformMgr().UnlockChipStack(); EFR32_LOG("Starting Platform Manager Event Loop"); diff --git a/examples/light-switch-app/nrfconnect/main/AppTask.cpp b/examples/light-switch-app/nrfconnect/main/AppTask.cpp index 40839ba477913b..9643dce1a8ef4b 100644 --- a/examples/light-switch-app/nrfconnect/main/AppTask.cpp +++ b/examples/light-switch-app/nrfconnect/main/AppTask.cpp @@ -159,7 +159,9 @@ CHIP_ERROR AppTask::Init() // Print initial configs SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); - ReturnErrorOnFailure(Server::GetInstance().Init()); + static chip::CommonCaseDeviceServerInitParams initParams; + ReturnErrorOnFailure(initParams.InitializeStaticResourcesBeforeServerInit()); + ReturnErrorOnFailure(Server::GetInstance().Init(initParams)); #if CONFIG_CHIP_OTA_REQUESTOR InitBasicOTARequestor(); #endif diff --git a/examples/lighting-app/ameba/main/chipinterface.cpp b/examples/lighting-app/ameba/main/chipinterface.cpp index 67552271f070b3..d40b0ab953197a 100644 --- a/examples/lighting-app/ameba/main/chipinterface.cpp +++ b/examples/lighting-app/ameba/main/chipinterface.cpp @@ -167,7 +167,9 @@ static void InitServer(intptr_t context) #endif // Init ZCL Data Model and CHIP App Server - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/lighting-app/cyw30739/src/main.cpp b/examples/lighting-app/cyw30739/src/main.cpp index 544819f781a876..97fbd3ede97602 100644 --- a/examples/lighting-app/cyw30739/src/main.cpp +++ b/examples/lighting-app/cyw30739/src/main.cpp @@ -136,7 +136,9 @@ void InitApp(intptr_t args) PlatformMgrImpl().AddEventHandler(EventHandler, 0); /* Start CHIP datamodel server */ - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/lighting-app/efr32/src/main.cpp b/examples/lighting-app/efr32/src/main.cpp index d521646e5f87fe..2464b715c4777f 100644 --- a/examples/lighting-app/efr32/src/main.cpp +++ b/examples/lighting-app/efr32/src/main.cpp @@ -171,7 +171,9 @@ int main(void) chip::DeviceLayer::PlatformMgr().LockChipStack(); // Init ZCL Data Model - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); chip::DeviceLayer::PlatformMgr().UnlockChipStack(); EFR32_LOG("Starting Platform Manager Event Loop"); diff --git a/examples/lighting-app/esp32/main/main.cpp b/examples/lighting-app/esp32/main/main.cpp index 930993b0d354f6..9310dc3a30449c 100644 --- a/examples/lighting-app/esp32/main/main.cpp +++ b/examples/lighting-app/esp32/main/main.cpp @@ -81,7 +81,9 @@ static void InitServer(intptr_t context) // Print QR Code URL PrintOnboardingCodes(chip::RendezvousInformationFlags(CONFIG_RENDEZVOUS_MODE)); - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/lighting-app/mbed/main/AppTask.cpp b/examples/lighting-app/mbed/main/AppTask.cpp index 95499a67efbf1b..78ec859903b205 100644 --- a/examples/lighting-app/mbed/main/AppTask.cpp +++ b/examples/lighting-app/mbed/main/AppTask.cpp @@ -115,7 +115,10 @@ int AppTask::Init() LightingMgr().SetCallbacks(ActionInitiated, ActionCompleted); // Init ZCL Data Model and start server - error = Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + + error = Server::GetInstance().Init(initParams); if (error != CHIP_NO_ERROR) { ChipLogError(NotSpecified, "Server initialization failed: %s", error.AsString()); diff --git a/examples/lighting-app/nrfconnect/main/AppTask.cpp b/examples/lighting-app/nrfconnect/main/AppTask.cpp index ccc46f3db56404..db687f1a4247ad 100644 --- a/examples/lighting-app/nrfconnect/main/AppTask.cpp +++ b/examples/lighting-app/nrfconnect/main/AppTask.cpp @@ -168,7 +168,11 @@ CHIP_ERROR AppTask::Init() // Initialize CHIP server SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); chip::app::DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(kExtDiscoveryTimeoutSecs); - ReturnErrorOnFailure(chip::Server::GetInstance().Init()); + + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + + ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams)); #if CONFIG_CHIP_OTA_REQUESTOR InitBasicOTARequestor(); #endif diff --git a/examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp b/examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp index 375cf6d8aa960d..c3f0b0ae986958 100644 --- a/examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp +++ b/examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp @@ -111,7 +111,9 @@ CHIP_ERROR AppTask::Init() CHIP_ERROR err = CHIP_NO_ERROR; // Init ZCL Data Model and start server - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config #ifdef ENABLE_HSM_DEVICE_ATTESTATION diff --git a/examples/lighting-app/p6/src/AppTask.cpp b/examples/lighting-app/p6/src/AppTask.cpp index 5bcc08727672de..4b6ca49942e8ec 100644 --- a/examples/lighting-app/p6/src/AppTask.cpp +++ b/examples/lighting-app/p6/src/AppTask.cpp @@ -88,7 +88,9 @@ void NetWorkCommissioningInstInit() static void InitServer(intptr_t context) { // Init ZCL Data Model - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp index 9756e8f97cad7a..ee522c997f7ef4 100644 --- a/examples/lighting-app/qpg/src/AppTask.cpp +++ b/examples/lighting-app/qpg/src/AppTask.cpp @@ -114,7 +114,9 @@ CHIP_ERROR AppTask::Init() #endif // Init ZCL Data Model - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Init OTA engine InitializeOTARequestor(); diff --git a/examples/lighting-app/telink/src/AppTask.cpp b/examples/lighting-app/telink/src/AppTask.cpp index e80642c59c86b3..694f4413a49d1f 100644 --- a/examples/lighting-app/telink/src/AppTask.cpp +++ b/examples/lighting-app/telink/src/AppTask.cpp @@ -98,7 +98,9 @@ CHIP_ERROR AppTask::Init() LightingMgr().SetCallbacks(ActionInitiated, ActionCompleted); // Init ZCL Data Model and start server - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/lock-app/cc13x2x7_26x2x7/main/AppTask.cpp b/examples/lock-app/cc13x2x7_26x2x7/main/AppTask.cpp index 86a07ef44f50f8..ff95b65a8a356e 100644 --- a/examples/lock-app/cc13x2x7_26x2x7/main/AppTask.cpp +++ b/examples/lock-app/cc13x2x7_26x2x7/main/AppTask.cpp @@ -156,7 +156,9 @@ int AppTask::Init() // Init ZCL Data Model and start server PLAT_LOG("Initialize Server"); - Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/lock-app/cyw30739/src/main.cpp b/examples/lock-app/cyw30739/src/main.cpp index 00c0f91f52648b..e1681600146c69 100644 --- a/examples/lock-app/cyw30739/src/main.cpp +++ b/examples/lock-app/cyw30739/src/main.cpp @@ -132,7 +132,9 @@ APPLICATION_START() PlatformMgrImpl().AddEventHandler(EventHandler, 0); /* Start CHIP datamodel server */ - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/lock-app/efr32/src/main.cpp b/examples/lock-app/efr32/src/main.cpp index 79496aba236330..a67a7b7967719c 100644 --- a/examples/lock-app/efr32/src/main.cpp +++ b/examples/lock-app/efr32/src/main.cpp @@ -167,7 +167,9 @@ int main(void) chip::DeviceLayer::PlatformMgr().LockChipStack(); // Init ZCL Data Model - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); chip::DeviceLayer::PlatformMgr().UnlockChipStack(); EFR32_LOG("Starting Platform Manager Event Loop"); diff --git a/examples/lock-app/esp32/main/main.cpp b/examples/lock-app/esp32/main/main.cpp index 1a9cfb6424ef78..20bb45062349e0 100644 --- a/examples/lock-app/esp32/main/main.cpp +++ b/examples/lock-app/esp32/main/main.cpp @@ -63,7 +63,9 @@ app::Clusters::NetworkCommissioning::Instance static void InitServer(intptr_t context) { - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/lock-app/mbed/main/AppTask.cpp b/examples/lock-app/mbed/main/AppTask.cpp index 283efadca06fc6..3386c12a0f07e9 100644 --- a/examples/lock-app/mbed/main/AppTask.cpp +++ b/examples/lock-app/mbed/main/AppTask.cpp @@ -118,7 +118,10 @@ int AppTask::Init() BoltLockMgr().SetCallbacks(ActionInitiated, ActionCompleted); // Init ZCL Data Model and start server - error = Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + + error = Server::GetInstance().Init(initParams); if (error != CHIP_NO_ERROR) { ChipLogError(NotSpecified, "Server initialization failed: %s", error.AsString()); diff --git a/examples/lock-app/nrfconnect/main/AppTask.cpp b/examples/lock-app/nrfconnect/main/AppTask.cpp index 64b8856bbc94f7..8259ae1bbf635d 100644 --- a/examples/lock-app/nrfconnect/main/AppTask.cpp +++ b/examples/lock-app/nrfconnect/main/AppTask.cpp @@ -149,7 +149,10 @@ CHIP_ERROR AppTask::Init() // Initialize CHIP server SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); - ReturnErrorOnFailure(chip::Server::GetInstance().Init()); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + + ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams)); #if CONFIG_CHIP_OTA_REQUESTOR InitBasicOTARequestor(); #endif diff --git a/examples/lock-app/nxp/k32w/k32w0/main/AppTask.cpp b/examples/lock-app/nxp/k32w/k32w0/main/AppTask.cpp index 43ec8f51af9080..1d0173d97be8dc 100644 --- a/examples/lock-app/nxp/k32w/k32w0/main/AppTask.cpp +++ b/examples/lock-app/nxp/k32w/k32w0/main/AppTask.cpp @@ -86,7 +86,9 @@ CHIP_ERROR AppTask::Init() CHIP_ERROR err = CHIP_NO_ERROR; // Init ZCL Data Model and start server - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/lock-app/p6/src/AppTask.cpp b/examples/lock-app/p6/src/AppTask.cpp index 3f0b37e2f340e5..c3ef7066f1d2c8 100644 --- a/examples/lock-app/p6/src/AppTask.cpp +++ b/examples/lock-app/p6/src/AppTask.cpp @@ -86,7 +86,9 @@ void NetWorkCommissioningInstInit() static void InitServer(intptr_t context) { // Init ZCL Data Model - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/lock-app/qpg/src/AppTask.cpp b/examples/lock-app/qpg/src/AppTask.cpp index 11e4cc0c067648..4f24d2e7e4f9f5 100644 --- a/examples/lock-app/qpg/src/AppTask.cpp +++ b/examples/lock-app/qpg/src/AppTask.cpp @@ -117,7 +117,9 @@ CHIP_ERROR AppTask::Init() #endif // Init ZCL Data Model - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Init OTA engine InitializeOTARequestor(); diff --git a/examples/log-source-app/linux/main.cpp b/examples/log-source-app/linux/main.cpp index 6d8b116859330a..ba30742d356b30 100644 --- a/examples/log-source-app/linux/main.cpp +++ b/examples/log-source-app/linux/main.cpp @@ -100,7 +100,9 @@ int main(int argc, char * argv[]) } chip::DeviceLayer::ConfigurationMgr().LogDeviceConfig(); - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider()); diff --git a/examples/ota-provider-app/esp32/main/main.cpp b/examples/ota-provider-app/esp32/main/main.cpp index fad067f2c9b0ca..23e3ef84770656 100644 --- a/examples/ota-provider-app/esp32/main/main.cpp +++ b/examples/ota-provider-app/esp32/main/main.cpp @@ -70,7 +70,9 @@ app::Clusters::NetworkCommissioning::Instance static void InitServer(intptr_t context) { - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/ota-requestor-app/ameba/main/chipinterface.cpp b/examples/ota-requestor-app/ameba/main/chipinterface.cpp index 1335c5d3da4b7c..3811b0b0c53ae6 100644 --- a/examples/ota-requestor-app/ameba/main/chipinterface.cpp +++ b/examples/ota-requestor-app/ameba/main/chipinterface.cpp @@ -127,7 +127,9 @@ static void InitServer(intptr_t context) InitOTARequestor(); // Init ZCL Data Model and CHIP App Server - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/ota-requestor-app/cyw30739/src/main.cpp b/examples/ota-requestor-app/cyw30739/src/main.cpp index b775fbc350bf7d..3ee89b67d6a7d1 100644 --- a/examples/ota-requestor-app/cyw30739/src/main.cpp +++ b/examples/ota-requestor-app/cyw30739/src/main.cpp @@ -124,7 +124,9 @@ void InitApp(intptr_t args) ConfigurationMgr().LogDeviceConfig(); /* Start CHIP datamodel server */ - Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/ota-requestor-app/efr32/src/AppTask.cpp b/examples/ota-requestor-app/efr32/src/AppTask.cpp index f2a37c63ccce0d..fa53fc6c66f852 100644 --- a/examples/ota-requestor-app/efr32/src/AppTask.cpp +++ b/examples/ota-requestor-app/efr32/src/AppTask.cpp @@ -127,7 +127,9 @@ CHIP_ERROR AppTask::Init() CHIP_ERROR err = CHIP_NO_ERROR; // Init ZCL Data Model - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); chip::DeviceLayer::PlatformMgr().LockChipStack(); // Initialize device attestation config diff --git a/examples/ota-requestor-app/esp32/main/main.cpp b/examples/ota-requestor-app/esp32/main/main.cpp index 93ce8c929f95ca..1ae4701cff6a41 100644 --- a/examples/ota-requestor-app/esp32/main/main.cpp +++ b/examples/ota-requestor-app/esp32/main/main.cpp @@ -64,7 +64,9 @@ app::Clusters::NetworkCommissioning::Instance static void InitServer(intptr_t context) { - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/ota-requestor-app/mbed/main/AppTask.cpp b/examples/ota-requestor-app/mbed/main/AppTask.cpp index f312c17f5690cd..0fa7df26f8d19c 100644 --- a/examples/ota-requestor-app/mbed/main/AppTask.cpp +++ b/examples/ota-requestor-app/mbed/main/AppTask.cpp @@ -104,7 +104,10 @@ int AppTask::Init() #endif // Init ZCL Data Model and start server - error = Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + + error = Server::GetInstance().Init(initParams); if (error != CHIP_NO_ERROR) { ChipLogError(NotSpecified, "Server initialization failed: %s", error.AsString()); diff --git a/examples/ota-requestor-app/p6/src/AppTask.cpp b/examples/ota-requestor-app/p6/src/AppTask.cpp index 1e4333037aefa6..8188ea90527035 100644 --- a/examples/ota-requestor-app/p6/src/AppTask.cpp +++ b/examples/ota-requestor-app/p6/src/AppTask.cpp @@ -115,7 +115,9 @@ void NetWorkCommissioningInstInit() static void InitServer(intptr_t context) { // Init ZCL Data Model - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/platform/linux/AppMain.cpp b/examples/platform/linux/AppMain.cpp index 0876bba0d4a831..66d361f942d4a7 100644 --- a/examples/platform/linux/AppMain.cpp +++ b/examples/platform/linux/AppMain.cpp @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -40,6 +41,7 @@ #include #include +#include #include #if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE @@ -229,6 +231,8 @@ void Cleanup() } chip::trace::DeInitTrace(); #endif // CHIP_CONFIG_TRANSPORT_TRACE_ENABLED + + // TODO(16968): Lifecycle management of storage-using components like GroupDataProvider, etc } } // namespace @@ -689,24 +693,28 @@ CommissionerDiscoveryController * GetCommissionerDiscoveryController() void ChipLinuxAppMainLoop() { + static chip::CommonCaseDeviceServerInitParams initParams; + VerifyOrDie(initParams.InitializeStaticResourcesBeforeServerInit() == CHIP_NO_ERROR); + #if defined(ENABLE_CHIP_SHELL) Engine::Root().Init(); std::thread shellThread([]() { Engine::Root().RunMainLoop(); }); Shell::RegisterCommissioneeCommands(); #endif - uint16_t securePort = CHIP_PORT; - uint16_t unsecurePort = CHIP_UDC_PORT; + initParams.operationalServicePort = CHIP_PORT; + initParams.userDirectedCommissioningPort = CHIP_UDC_PORT; #if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE || CHIP_DEVICE_ENABLE_PORT_PARAMS // use a different service port to make testing possible with other sample devices running on same host - securePort = LinuxDeviceOptions::GetInstance().securedDevicePort; - unsecurePort = LinuxDeviceOptions::GetInstance().unsecuredCommissionerPort; + initParams.operationalServicePort = LinuxDeviceOptions::GetInstance().securedDevicePort; + initParams.userDirectedCommissioningPort = LinuxDeviceOptions::GetInstance().unsecuredCommissionerPort; + ; #endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE - Inet::InterfaceId interfaceId = LinuxDeviceOptions::GetInstance().interfaceId; + initParams.interfaceId = LinuxDeviceOptions::GetInstance().interfaceId; // Init ZCL Data Model and CHIP App Server - Server::GetInstance().Init(nullptr, securePort, unsecurePort, interfaceId); + Server::GetInstance().Init(initParams); // Now that the server has started and we are done with our startup logging, // log our discovery/onboarding information again so it's not lost in the diff --git a/examples/pump-app/cc13x2x7_26x2x7/main/AppTask.cpp b/examples/pump-app/cc13x2x7_26x2x7/main/AppTask.cpp index f576fafcaa8bd1..e0db8f9a78ad14 100644 --- a/examples/pump-app/cc13x2x7_26x2x7/main/AppTask.cpp +++ b/examples/pump-app/cc13x2x7_26x2x7/main/AppTask.cpp @@ -174,7 +174,9 @@ int AppTask::Init() // Init ZCL Data Model and start server PLAT_LOG("Initialize Server"); - Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/pump-app/nrfconnect/main/AppTask.cpp b/examples/pump-app/nrfconnect/main/AppTask.cpp index 44b812aab274d8..4aa2eac013c9e8 100644 --- a/examples/pump-app/nrfconnect/main/AppTask.cpp +++ b/examples/pump-app/nrfconnect/main/AppTask.cpp @@ -146,7 +146,10 @@ CHIP_ERROR AppTask::Init() // Initialize CHIP server SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); - ReturnErrorOnFailure(chip::Server::GetInstance().Init()); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + + ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams)); #if CONFIG_CHIP_OTA_REQUESTOR InitBasicOTARequestor(); #endif diff --git a/examples/pump-controller-app/cc13x2x7_26x2x7/main/AppTask.cpp b/examples/pump-controller-app/cc13x2x7_26x2x7/main/AppTask.cpp index f96411cfa8d0de..4d410e34ffdf84 100644 --- a/examples/pump-controller-app/cc13x2x7_26x2x7/main/AppTask.cpp +++ b/examples/pump-controller-app/cc13x2x7_26x2x7/main/AppTask.cpp @@ -162,7 +162,9 @@ int AppTask::Init() // Init ZCL Data Model and start server PLAT_LOG("Initialize Server"); - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/pump-controller-app/nrfconnect/main/AppTask.cpp b/examples/pump-controller-app/nrfconnect/main/AppTask.cpp index 66c56d7eaca2a1..f47ddb9a9f8a42 100644 --- a/examples/pump-controller-app/nrfconnect/main/AppTask.cpp +++ b/examples/pump-controller-app/nrfconnect/main/AppTask.cpp @@ -143,7 +143,10 @@ CHIP_ERROR AppTask::Init() // Initialize CHIP server SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); - ReturnErrorOnFailure(chip::Server::GetInstance().Init()); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + + ReturnErrorOnFailure(chip::Server::GetInstance().Init(initParams)); #if CONFIG_CHIP_OTA_REQUESTOR InitBasicOTARequestor(); #endif diff --git a/examples/shell/nxp/k32w/k32w0/main/AppTask.cpp b/examples/shell/nxp/k32w/k32w0/main/AppTask.cpp index 33eb5e72ccb29e..383dbdb1f9b19d 100644 --- a/examples/shell/nxp/k32w/k32w0/main/AppTask.cpp +++ b/examples/shell/nxp/k32w/k32w0/main/AppTask.cpp @@ -75,7 +75,9 @@ CHIP_ERROR AppTask::Init() CHIP_ERROR err = CHIP_NO_ERROR; // Init ZCL Data Model and start server - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/shell/shell_common/cmd_server.cpp b/examples/shell/shell_common/cmd_server.cpp index 6a8cc2287994ab..a77a606bd73aea 100644 --- a/examples/shell/shell_common/cmd_server.cpp +++ b/examples/shell/shell_common/cmd_server.cpp @@ -56,7 +56,12 @@ static CHIP_ERROR CmdAppServerHelp(int argc, char ** argv) static CHIP_ERROR CmdAppServerStart(int argc, char ** argv) { // Init ZCL Data Model and CHIP App Server - chip::Server::GetInstance().Init(nullptr, sServerPortOperational, sServerPortCommissioning); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + initParams.operationalServicePort = sServerPortOperational; + initParams.userDirectedCommissioningPort = sServerPortCommissioning; + + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/temperature-measurement-app/esp32/main/main.cpp b/examples/temperature-measurement-app/esp32/main/main.cpp index 6640984a02cc6e..f48bd06fdfa28e 100644 --- a/examples/temperature-measurement-app/esp32/main/main.cpp +++ b/examples/temperature-measurement-app/esp32/main/main.cpp @@ -60,7 +60,10 @@ app::Clusters::NetworkCommissioning::Instance static void InitServer(intptr_t context) { - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); + sWiFiNetworkCommissioningInstance.Init(); } diff --git a/examples/tv-casting-app/linux/main.cpp b/examples/tv-casting-app/linux/main.cpp index 3aec560c0c06c5..3050f8ef40f5e5 100644 --- a/examples/tv-casting-app/linux/main.cpp +++ b/examples/tv-casting-app/linux/main.cpp @@ -141,7 +141,10 @@ void PrepareForCommissioning(const Dnssd::DiscoveredNodeData * selectedCommissio DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init(CHIP_CONFIG_KVS_PATH); // Enter commissioning mode, open commissioning window - Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); + Server::GetInstance().GetFabricTable().DeleteAllFabrics(); ReturnOnFailure( Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow(kCommissioningWindowTimeout)); diff --git a/examples/window-app/efr32/src/main.cpp b/examples/window-app/efr32/src/main.cpp index 4e844ccd847e34..9f87c930dfe17b 100644 --- a/examples/window-app/efr32/src/main.cpp +++ b/examples/window-app/efr32/src/main.cpp @@ -143,7 +143,9 @@ int main(void) chip::DeviceLayer::PlatformMgr().LockChipStack(); // Init ZCL Data Model - chip::Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); chip::DeviceLayer::PlatformMgr().UnlockChipStack(); EFR32_LOG("Starting Platform Manager Event Loop"); diff --git a/src/app/server/AppDelegate.h b/src/app/server/AppDelegate.h index 0610963287857f..e722e1b41b8f00 100644 --- a/src/app/server/AppDelegate.h +++ b/src/app/server/AppDelegate.h @@ -26,8 +26,8 @@ class AppDelegate { public: virtual ~AppDelegate() {} - virtual void OnRendezvousStarted() {} - virtual void OnRendezvousStopped() {} - virtual void OnPairingWindowOpened() {} - virtual void OnPairingWindowClosed() {} + virtual void OnCommissioningSessionStarted() {} + virtual void OnCommissioningSessionStopped() {} + virtual void OnCommissioningWindowOpened() {} + virtual void OnCommissioningWindowClosed() {} }; diff --git a/src/app/server/CommissioningWindowManager.cpp b/src/app/server/CommissioningWindowManager.cpp index 58b1638ba67c5d..9d9d113d26c9c3 100644 --- a/src/app/server/CommissioningWindowManager.cpp +++ b/src/app/server/CommissioningWindowManager.cpp @@ -119,7 +119,7 @@ void CommissioningWindowManager::OnSessionEstablishmentError(CHIP_ERROR err) if (mAppDelegate != nullptr) { - mAppDelegate->OnRendezvousStopped(); + mAppDelegate->OnCommissioningSessionStopped(); } } } @@ -150,7 +150,7 @@ void CommissioningWindowManager::OnSessionEstablished() ChipLogProgress(AppServer, "Commissioning completed session establishment step"); if (mAppDelegate != nullptr) { - mAppDelegate->OnRendezvousStarted(); + mAppDelegate->OnCommissioningSessionStarted(); } DeviceLayer::PlatformMgr().AddEventHandler(OnPlatformEventWrapper, reinterpret_cast(this)); @@ -325,7 +325,7 @@ CHIP_ERROR CommissioningWindowManager::StartAdvertisement() if (mAppDelegate != nullptr) { - mAppDelegate->OnPairingWindowOpened(); + mAppDelegate->OnCommissioningWindowOpened(); } if (mUseECM) @@ -374,7 +374,7 @@ CHIP_ERROR CommissioningWindowManager::StopAdvertisement(bool aShuttingDown) if (mAppDelegate != nullptr) { - mAppDelegate->OnPairingWindowClosed(); + mAppDelegate->OnCommissioningWindowClosed(); } return CHIP_NO_ERROR; diff --git a/src/app/server/Server.cpp b/src/app/server/Server.cpp index 65ad11086bd036..3ce73a3970b30c 100644 --- a/src/app/server/Server.cpp +++ b/src/app/server/Server.cpp @@ -94,55 +94,53 @@ static ::chip::PersistedCounter sGlobalEventIdCounter; static ::chip::app::CircularEventBuffer sLoggingBuffer[CHIP_NUM_EVENT_LOGGING_BUFFERS]; #endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT -CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint16_t unsecureServicePort, - Inet::InterfaceId interfaceId) +CHIP_ERROR Server::Init(const ServerInitParams & initParams) { - Access::AccessControl::Delegate * accessDelegate = nullptr; CASESessionManagerConfig caseSessionManagerConfig; - mSecuredServicePort = secureServicePort; - mUnsecuredServicePort = unsecureServicePort; - mInterfaceId = interfaceId; + mOperationalServicePort = initParams.operationalServicePort; + mUserDirectedCommissioningPort = initParams.userDirectedCommissioningPort; + mInterfaceId = initParams.interfaceId; CHIP_ERROR err = CHIP_NO_ERROR; - // TODO: Remove chip::Platform::MemoryInit() call from Server class, it belongs to outer code + VerifyOrExit(initParams.persistentStorageDelegate != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrExit(initParams.groupDataProvider != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrExit(initParams.accessDelegate != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); + + // TODO(16969): Remove chip::Platform::MemoryInit() call from Server class, it belongs to outer code chip::Platform::MemoryInit(); SuccessOrExit(err = mCommissioningWindowManager.Init(this)); - mCommissioningWindowManager.SetAppDelegate(delegate); + mCommissioningWindowManager.SetAppDelegate(initParams.appDelegate); + + // Initialize PersistentStorageDelegate-based storage + mDeviceStorage = initParams.persistentStorageDelegate; // Set up attribute persistence before we try to bring up the data model // handler. - SuccessOrExit(mAttributePersister.Init(&mDeviceStorage)); + SuccessOrExit(mAttributePersister.Init(mDeviceStorage)); SetAttributePersistenceProvider(&mAttributePersister); InitDataModelHandler(&mExchangeMgr); - err = mFabrics.Init(&mDeviceStorage); + err = mFabrics.Init(mDeviceStorage); SuccessOrExit(err); app::DnssdServer::Instance().SetFabricTable(&mFabrics); app::DnssdServer::Instance().SetCommissioningModeProvider(&mCommissioningWindowManager); - // Group data provider must be initialized after mDeviceStorage - mGroupsProvider.SetStorageDelegate(&mDeviceStorage); - err = mGroupsProvider.Init(); - SuccessOrExit(err); - SetGroupDataProvider(&mGroupsProvider); - - // Access control must be initialized after mDeviceStorage. - accessDelegate = Access::Examples::GetAccessControlDelegate(&mDeviceStorage); - VerifyOrExit(accessDelegate != nullptr, ChipLogError(AppServer, "Invalid access delegate found.")); + mGroupsProvider = initParams.groupDataProvider; + SetGroupDataProvider(mGroupsProvider); - err = mAccessControl.Init(accessDelegate, sDeviceTypeResolver); + err = mAccessControl.Init(initParams.accessDelegate, sDeviceTypeResolver); SuccessOrExit(err); Access::SetAccessControl(mAccessControl); // Init transport before operations with secure session mgr. err = mTransports.Init(UdpListenParameters(DeviceLayer::UDPEndPointManager()) .SetAddressType(IPAddressType::kIPv6) - .SetListenPort(mSecuredServicePort) + .SetListenPort(mOperationalServicePort) #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT .SetNativeParams(chip::DeviceLayer::ThreadStackMgrImpl().OTInstance()) #endif // CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT @@ -151,7 +149,7 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint , UdpListenParameters(DeviceLayer::UDPEndPointManager()) .SetAddressType(IPAddressType::kIPv4) - .SetListenPort(mSecuredServicePort) + .SetListenPort(mOperationalServicePort) #endif #if CONFIG_NETWORK_LAYER_BLE , @@ -161,14 +159,14 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint err = mListener.Init(&mTransports); SuccessOrExit(err); - mGroupsProvider.SetListener(&mListener); + mGroupsProvider->SetListener(&mListener); #if CONFIG_NETWORK_LAYER_BLE mBleLayer = DeviceLayer::ConnectivityMgr().GetBleLayer(); #endif SuccessOrExit(err); - err = mSessions.Init(&DeviceLayer::SystemLayer(), &mTransports, &mMessageCounterManager, &mDeviceStorage, &GetFabricTable()); + err = mSessions.Init(&DeviceLayer::SystemLayer(), &mTransports, &mMessageCounterManager, mDeviceStorage, &GetFabricTable()); SuccessOrExit(err); err = mFabricDelegate.Init(this); @@ -224,8 +222,8 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint #endif } - app::DnssdServer::Instance().SetSecuredPort(mSecuredServicePort); - app::DnssdServer::Instance().SetUnsecuredPort(mUnsecuredServicePort); + app::DnssdServer::Instance().SetSecuredPort(mOperationalServicePort); + app::DnssdServer::Instance().SetUnsecuredPort(mUserDirectedCommissioningPort); app::DnssdServer::Instance().SetInterfaceId(mInterfaceId); // TODO @bzbarsky-apple @cecille Move to examples @@ -242,7 +240,7 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint .exchangeMgr = &mExchangeMgr, .fabricTable = &mFabrics, .clientPool = &mCASEClientPool, - .groupDataProvider = &mGroupsProvider, + .groupDataProvider = mGroupsProvider, }, #if CHIP_CONFIG_MDNS_CACHE_SIZE > 0 .dnsCache = nullptr, @@ -257,7 +255,7 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint #if CONFIG_NETWORK_LAYER_BLE chip::DeviceLayer::ConnectivityMgr().GetBleLayer(), #endif - &mSessions, &mFabrics, &mGroupsProvider); + &mSessions, &mFabrics, mGroupsProvider); SuccessOrExit(err); // This code is necessary to restart listening to existing groups after a reboot @@ -291,7 +289,7 @@ void Server::RejoinExistingMulticastGroups() { Credentials::GroupDataProvider::GroupInfo groupInfo; - auto * iterator = mGroupsProvider.IterateGroupInfo(fabric.GetFabricIndex()); + auto * iterator = mGroupsProvider->IterateGroupInfo(fabric.GetFabricIndex()); if (iterator) { // GroupDataProvider was able to allocate rescources for an iterator @@ -349,7 +347,7 @@ void Server::Shutdown() mCommissioningWindowManager.Shutdown(); mCASESessionManager.Shutdown(); - // TODO: Remove chip::Platform::MemoryInit() call from Server class, it belongs to outer code + // TODO(16969): Remove chip::Platform::MemoryInit() call from Server class, it belongs to outer code chip::Platform::MemoryShutdown(); } diff --git a/src/app/server/Server.h b/src/app/server/Server.h index afba9c4b17d3a0..77fd34d29a7480 100644 --- a/src/app/server/Server.h +++ b/src/app/server/Server.h @@ -18,6 +18,7 @@ #pragma once #include +#include #include #include #include @@ -25,12 +26,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include #include #include @@ -59,11 +62,119 @@ using ServerTransportMgr = chip::TransportMgr; +struct ServerInitParams +{ + ServerInitParams() = default; + virtual ~ServerInitParams() = default; + + // Not copyable + ServerInitParams(const ServerInitParams &) = delete; + ServerInitParams & operator=(const ServerInitParams &) = delete; + + // Application delegate to handle some commissioning lifecycle events + AppDelegate * appDelegate = nullptr; + // Port to use for Matter commissioning/operational traffic + uint16_t operationalServicePort = CHIP_PORT; + // Port to use for UDC if supported + uint16_t userDirectedCommissioningPort = CHIP_UDC_PORT; + // Interface on which to run daemon + Inet::InterfaceId interfaceId = Inet::InterfaceId::Null(); + + // Persistent storage delegate: MUST be injected. Used to maintain storage by much common code. + // Must be initialized before being provided. + PersistentStorageDelegate * persistentStorageDelegate = nullptr; + // Group data provider: MUST be injected. Used to maintain critical keys such as the Identity + // Protection Key (IPK) for CASE. Must be initialized before being provided. + Credentials::GroupDataProvider * groupDataProvider = nullptr; + // Access control delegate: MUST be injected. Used to look up access control rules. Must be + // initialized before being provided + Access::AccessControl::Delegate * accessDelegate = nullptr; +}; + +/** + * Transitional version of ServerInitParams to assist SDK integrators in + * transitioning to injecting product/platform-owned resources. This version + * of `ServerInitParams` statically owns and initializes (via the + * `InitializeStaticResourcesBeforeServerInit()` method). This is to reduce the + * amount of copied boilerplate in all the example initializations (e.g. AppTask.cpp, + * main.cpp) for the transition. + * + * ACTION ITEM FOR TRANSITION: While this could be used indefinitely, it does not + * examplify orderly management of application-injected resources. It is recommended + * to instead: + * - Use the basic ServerInitParams in every application + * - Have every application own an instance of the resources being injected in its own + * state (e.g. an implementation of PersistentStorageDelegate and GroupDataProvider + * interfaces). + * - Initialize the injected resources prior to calling Server::Init() + * - De-initialize the injected resources after calling Server::Shutdown() + * + * WARNING: DO NOT replicate the pattern shown here of having a subclass of ServerInitParams + * own the resources. This was done to reduce the amount of change to existing + * examples, prior to updating each example to do the transition work presented above. + */ +struct CommonCaseDeviceServerInitParams : public ServerInitParams +{ + CommonCaseDeviceServerInitParams() = default; + + // Not copyable + CommonCaseDeviceServerInitParams(const CommonCaseDeviceServerInitParams &) = delete; + CommonCaseDeviceServerInitParams & operator=(const CommonCaseDeviceServerInitParams &) = delete; + + /** + * Call this before Server::Init() to initialize the internally-owned resources. + * Server::Init() will fail if this is not done, since several params required to + * be non-null will be null without calling this method. ** See the transition method + * in the outer comment of this class **. + * + * @return CHIP_NO_ERROR on success or a CHIP_ERROR value from APIs called to initialize + * resources on failure. + */ + virtual CHIP_ERROR InitializeStaticResourcesBeforeServerInit() + { + static chip::KvsPersistentStorageDelegate sKvsPersistenStorageDelegate; + static chip::Credentials::GroupDataProviderImpl sGroupDataProvider; + + // KVS-based persistent storage delegate injection + chip::DeviceLayer::PersistedStorage::KeyValueStoreManager & kvsManager = DeviceLayer::PersistedStorage::KeyValueStoreMgr(); + ReturnErrorOnFailure(sKvsPersistenStorageDelegate.Init(&kvsManager)); + this->persistentStorageDelegate = &sKvsPersistenStorageDelegate; + + // Group Data provider injection + sGroupDataProvider.SetStorageDelegate(&sKvsPersistenStorageDelegate); + ReturnErrorOnFailure(sGroupDataProvider.Init()); + this->groupDataProvider = &sGroupDataProvider; + + // Inject access control delegate + this->accessDelegate = Access::Examples::GetAccessControlDelegate(&sKvsPersistenStorageDelegate); + + return CHIP_NO_ERROR; + } +}; + +/** + * The `Server` singleton class is an aggregate for all the resources needed to run a + * Node that is both Commissionable and mainly used as an end-node with server clusters. + * In other words, it aggregates the state needed for the type of Node used for most + * products that are not mainly controller/administrator role. + * + * This sington class expects `ServerInitParams` initialization parameters but does not + * own the resources injected from `ServerInitParams`. Any object pointers/references + * passed in ServerInitParams must be pre-initialized externally, and shutdown/finalized + * after `Server::Shutdown()` is called. + * + * TODO: Separate lifecycle ownership for some more capabilities that should not belong to + * common logic, such as `DispatchShutDownAndStopEventLoop`. + * + * TODO: Replace all uses of GetInstance() to "reach in" to this state from all cluster + * server common logic that deal with global node state with either a common NodeState + * compatible with OperationalDeviceProxy/DeviceProxy, or with injection at common + * SDK logic init. + */ class Server { public: - CHIP_ERROR Init(AppDelegate * delegate = nullptr, uint16_t secureServicePort = CHIP_PORT, - uint16_t unsecureServicePort = CHIP_UDC_PORT, Inet::InterfaceId interfaceId = Inet::InterfaceId::Null()); + CHIP_ERROR Init(const ServerInitParams & initParams); #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT CHIP_ERROR SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress commissioner); @@ -84,7 +195,7 @@ class Server TransportMgrBase & GetTransportManager() { return mTransports; } - Credentials::GroupDataProvider * GetGroupDataProvider() { return &mGroupsProvider; } + Credentials::GroupDataProvider * GetGroupDataProvider() { return mGroupsProvider; } #if CONFIG_NETWORK_LAYER_BLE Ble::BleLayer * GetBleLayerObject() { return mBleLayer; } @@ -92,7 +203,7 @@ class Server CommissioningWindowManager & GetCommissioningWindowManager() { return mCommissioningWindowManager; } - PersistentStorageDelegate & GetPersistentStorage() { return mDeviceStorage; } + PersistentStorageDelegate & GetPersistentStorage() { return *mDeviceStorage; } /** * This function send the ShutDown event before stopping @@ -111,62 +222,6 @@ class Server static Server sServer; - class DeviceStorageDelegate : public PersistentStorageDelegate - { - CHIP_ERROR SyncGetKeyValue(const char * key, void * buffer, uint16_t & size) override - { - uint8_t emptyPlaceholder = 0; - if (buffer == nullptr) - { - if (size != 0) - { - return CHIP_ERROR_INVALID_ARGUMENT; - } - - // When size is zero, let's give a non-nullptr to the KVS backend - buffer = &emptyPlaceholder; - } - - size_t bytesRead = 0; - CHIP_ERROR err = DeviceLayer::PersistedStorage::KeyValueStoreMgr().Get(key, buffer, size, &bytesRead); - - // Update size only if it made sense - if ((CHIP_ERROR_BUFFER_TOO_SMALL == err) || (CHIP_NO_ERROR == err)) - { - size = CanCastTo(bytesRead) ? static_cast(bytesRead) : 0; - } - - if (err == CHIP_NO_ERROR) - { - ChipLogProgress(AppServer, "Retrieved from server storage: %s", key); - } - - return err; - } - - CHIP_ERROR SyncSetKeyValue(const char * key, const void * value, uint16_t size) override - { - uint8_t placeholderForEmpty = 0; - if (value == nullptr) - { - if (size == 0) - { - value = &placeholderForEmpty; - } - else - { - return CHIP_ERROR_INVALID_ARGUMENT; - } - } - return DeviceLayer::PersistedStorage::KeyValueStoreMgr().Put(key, value, size); - } - - CHIP_ERROR SyncDeleteKeyValue(const char * key) override - { - return DeviceLayer::PersistedStorage::KeyValueStoreMgr().Delete(key); - } - }; - class GroupDataProviderListener final : public Credentials::GroupDataProvider::GroupListener { public: @@ -252,19 +307,16 @@ class Server #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT CommissioningWindowManager mCommissioningWindowManager; - // Both PersistentStorageDelegate, and GroupDataProvider should be injected by the applications - // See: https://github.com/project-chip/connectedhomeip/issues/12276 - DeviceStorageDelegate mDeviceStorage; - Credentials::GroupDataProviderImpl mGroupsProvider; + PersistentStorageDelegate * mDeviceStorage; + Credentials::GroupDataProvider * mGroupsProvider; app::DefaultAttributePersistenceProvider mAttributePersister; GroupDataProviderListener mListener; ServerFabricDelegate mFabricDelegate; Access::AccessControl mAccessControl; - // TODO @ceille: Maybe use OperationalServicePort and CommissionableServicePort - uint16_t mSecuredServicePort; - uint16_t mUnsecuredServicePort; + uint16_t mOperationalServicePort; + uint16_t mUserDirectedCommissioningPort; Inet::InterfaceId mInterfaceId; }; diff --git a/src/app/server/java/AndroidAppServerWrapper.cpp b/src/app/server/java/AndroidAppServerWrapper.cpp index 81781b35a62086..cd3a224e48a6e8 100644 --- a/src/app/server/java/AndroidAppServerWrapper.cpp +++ b/src/app/server/java/AndroidAppServerWrapper.cpp @@ -48,7 +48,12 @@ CHIP_ERROR ChipAndroidAppInit(void) ConfigurationMgr().LogDeviceConfig(); // Init ZCL Data Model and CHIP App Server - err = chip::Server::GetInstance().Init(nullptr, CHIP_PORT, CHIP_UDC_PORT); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + initParams.operationalServicePort = CHIP_PORT; + initParams.userDirectedCommissioningPort = CHIP_UDC_PORT; + + err = chip::Server::GetInstance().Init(initParams); SuccessOrExit(err); if (!IsDeviceAttestationCredentialsProviderSet()) diff --git a/src/app/tests/TestCommissionManager.cpp b/src/app/tests/TestCommissionManager.cpp index 6afc7818ea99dc..cda96445bc9516 100644 --- a/src/app/tests/TestCommissionManager.cpp +++ b/src/app/tests/TestCommissionManager.cpp @@ -52,7 +52,10 @@ void InitializeChip(nlTestSuite * suite) static chip::DeviceLayer::TestOnlyCommissionableDataProvider commissionableDataProvider; chip::DeviceLayer::SetCommissionableDataProvider(&commissionableDataProvider); - err = Server::GetInstance().Init(); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + err = chip::Server::GetInstance().Init(initParams); + NL_TEST_ASSERT(suite, err == CHIP_NO_ERROR); Server::GetInstance().GetCommissioningWindowManager().CloseCommissioningWindow(); diff --git a/src/controller/python/chip/server/ServerInit.cpp b/src/controller/python/chip/server/ServerInit.cpp index a0239c66ebdbb9..b40cd59c73da51 100644 --- a/src/controller/python/chip/server/ServerInit.cpp +++ b/src/controller/python/chip/server/ServerInit.cpp @@ -172,7 +172,12 @@ void pychip_server_native_init() uint16_t unsecurePort = CHIP_UDC_PORT; // Init ZCL Data Model and CHIP App Server - chip::Server::GetInstance().Init(nullptr, securePort, unsecurePort); + static chip::CommonCaseDeviceServerInitParams initParams; + (void) initParams.InitializeStaticResourcesBeforeServerInit(); + initParams.operationalServicePort = CHIP_PORT; + initParams.userDirectedCommissioningPort = CHIP_UDC_PORT; + + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config // SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/src/include/platform/KvsPersistentStorageDelegate.h b/src/include/platform/KvsPersistentStorageDelegate.h new file mode 100644 index 00000000000000..0142e55745c4f3 --- /dev/null +++ b/src/include/platform/KvsPersistentStorageDelegate.h @@ -0,0 +1,111 @@ +/* + * + * Copyright (c) 2022 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. + */ + +#pragma once + +#include + +#include +#include +#include +#include + +namespace chip { + +// Sample version of PersistentStorageDelegate that relies only on a KeyValueStoreManager +class KvsPersistentStorageDelegate : public PersistentStorageDelegate +{ +public: + KvsPersistentStorageDelegate() = default; + virtual ~KvsPersistentStorageDelegate() = default; + + // No copy, move or assignment. + KvsPersistentStorageDelegate(const KvsPersistentStorageDelegate &) = delete; + KvsPersistentStorageDelegate(const KvsPersistentStorageDelegate &&) = delete; + KvsPersistentStorageDelegate & operator=(const KvsPersistentStorageDelegate &) = delete; + + CHIP_ERROR Init(DeviceLayer::PersistedStorage::KeyValueStoreManager * kvsManager) + { + if (kvsManager == nullptr) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + mKvsManager = kvsManager; + + return CHIP_NO_ERROR; + } + + CHIP_ERROR SyncGetKeyValue(const char * key, void * buffer, uint16_t & size) override + { + VerifyOrReturnError(mKvsManager != nullptr, CHIP_ERROR_INCORRECT_STATE); + + uint8_t emptyPlaceholder = 0; + if (buffer == nullptr) + { + if (size != 0) + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + else + { + // When size is zero, let's give a non-nullptr to the KVS backend + buffer = &emptyPlaceholder; + } + } + + size_t bytesRead = 0; + CHIP_ERROR err = mKvsManager->Get(key, buffer, size, &bytesRead); + + // Update size only if it made sense + if ((CHIP_ERROR_BUFFER_TOO_SMALL == err) || (CHIP_NO_ERROR == err)) + { + size = CanCastTo(bytesRead) ? static_cast(bytesRead) : 0; + } + + return err; + } + + CHIP_ERROR SyncSetKeyValue(const char * key, const void * value, uint16_t size) override + { + VerifyOrReturnError(mKvsManager != nullptr, CHIP_ERROR_INCORRECT_STATE); + + uint8_t placeholderForEmpty = 0; + if (value == nullptr) + { + if (size == 0) + { + value = &placeholderForEmpty; + } + else + { + return CHIP_ERROR_INVALID_ARGUMENT; + } + } + return mKvsManager->Put(key, value, size); + } + + CHIP_ERROR SyncDeleteKeyValue(const char * key) override + { + VerifyOrReturnError(mKvsManager != nullptr, CHIP_ERROR_INCORRECT_STATE); + return mKvsManager->Delete(key); + } + +protected: + DeviceLayer::PersistedStorage::KeyValueStoreManager * mKvsManager = nullptr; +}; + +} // namespace chip diff --git a/src/platform/BUILD.gn b/src/platform/BUILD.gn index 27bd7dd608ca27..f5e87a8b13deb7 100644 --- a/src/platform/BUILD.gn +++ b/src/platform/BUILD.gn @@ -295,6 +295,7 @@ if (chip_device_platform != "none") { "../include/platform/FailSafeContext.h", "../include/platform/GeneralUtils.h", "../include/platform/KeyValueStoreManager.h", + "../include/platform/KvsPersistentStorageDelegate.h", "../include/platform/PersistedStorage.h", "../include/platform/PlatformManager.h", "../include/platform/TestOnlyCommissionableDataProvider.h",