diff --git a/examples/all-clusters-app/ameba/main/chipinterface.cpp b/examples/all-clusters-app/ameba/main/chipinterface.cpp index e11e9a0edbe021..59f9a1f95f1683 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.InitBeforeServerInit(); + 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..fd2e42d2d2331a 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -109,7 +109,9 @@ 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.InitBeforeServerInit(); + 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..a3af54cbd1f9c8 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.InitBeforeServerInit(); + 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..e0e53e64f1e909 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.InitBeforeServerInit(); + 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..a66d64b969d9d6 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.InitBeforeServerInit(); + 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..12e233c9f0af93 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.InitBeforeServerInit(); + 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..a2e8cc595c199d 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.InitBeforeServerInit(); + 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..cf475446aa412a 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.InitBeforeServerInit(); + 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..c7c46bdbcec5e8 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.InitBeforeServerInit(); + 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..a59b9b04db8867 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.InitBeforeServerInit()); + 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..fb31cd945a39f3 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.InitBeforeServerInit(); + 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..9e15fa33b090cc 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.InitBeforeServerInit(); + 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..c58d24b9a8edef 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.InitBeforeServerInit(); + 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..00264e6110202e 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.InitBeforeServerInit(); + 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..6f4942ab1bf2fc 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.InitBeforeServerInit(); + + 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..44baa5523c89cf 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.InitBeforeServerInit(); + + 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..b1ef5b5a7bf500 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.InitBeforeServerInit(); + 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..2fd02a6b35f97d 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.InitBeforeServerInit(); + 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..a99dd017dae44d 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.InitBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Init OTA engine InitializeOTARequestor(); diff --git a/examples/lock-app/cc13x2x7_26x2x7/main/AppTask.cpp b/examples/lock-app/cc13x2x7_26x2x7/main/AppTask.cpp index 86a07ef44f50f8..4616bad6654676 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.InitBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); // Initialize device attestation config SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); diff --git a/examples/lock-app/efr32/src/main.cpp b/examples/lock-app/efr32/src/main.cpp index 79496aba236330..76f8bc746ee889 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.InitBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); chip::DeviceLayer::PlatformMgr().UnlockChipStack(); EFR32_LOG("Starting Platform Manager Event Loop"); diff --git a/examples/lock-app/mbed/main/AppTask.cpp b/examples/lock-app/mbed/main/AppTask.cpp index 283efadca06fc6..441a4fb375285a 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.InitBeforeServerInit(); + + 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..aa5c7f92278de7 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.InitBeforeServerInit(); + + 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..d2395a8ce52629 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.InitBeforeServerInit(); + 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..e0582b36724bb2 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.InitBeforeServerInit(); + 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..0135ff99fb0c0e 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.InitBeforeServerInit(); + 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..67c048b14ee05a 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.InitBeforeServerInit(); + 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..49aab7efaa7b43 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.InitBeforeServerInit(); + 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..b660ad4ae77191 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.InitBeforeServerInit(); + 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..10b43a94f5c326 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.InitBeforeServerInit(); + 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..60226685665aa1 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.InitBeforeServerInit(); + 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..054be6b71395c4 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.InitBeforeServerInit(); + + 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..7f328244a781b8 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.InitBeforeServerInit(); + 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..4ada9c970b3a51 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 @@ -689,24 +691,28 @@ CommissionerDiscoveryController * GetCommissionerDiscoveryController() void ChipLinuxAppMainLoop() { + static chip::CommonCaseDeviceServerInitParams initParams; + VerifyOrDie(initParams.InitBeforeServerInit() == 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..c8597daebea071 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.InitBeforeServerInit(); + 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..81b19d773691f0 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.InitBeforeServerInit(); + + 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..39632d2e332881 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.InitBeforeServerInit(); + 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..696963b3587db1 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.InitBeforeServerInit(); + + 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..16c947e65d34d8 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.InitBeforeServerInit(); + 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..f8744d9712b43d 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.InitBeforeServerInit(); + 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 820b5c73857eb3..367a994d24aaa8 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.InitBeforeServerInit(); + 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..53acbe0d657c9a 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.InitBeforeServerInit(); + chip::Server::GetInstance().Init(initParams); chip::DeviceLayer::PlatformMgr().UnlockChipStack(); EFR32_LOG("Starting Platform Manager Event Loop"); diff --git a/src/app/server/Server.cpp b/src/app/server/Server.cpp index 43a8eeae53ae2a..fb3c1b738306fe 100644 --- a/src/app/server/Server.cpp +++ b/src/app/server/Server.cpp @@ -94,56 +94,54 @@ 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; + 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: 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); mCommissioningWindowManager.SetSessionIDAllocator(&mSessionIDAllocator); + // 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 @@ -152,7 +150,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 , @@ -162,14 +160,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); @@ -225,8 +223,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 @@ -244,7 +242,7 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint .idAllocator = &mSessionIDAllocator, .fabricTable = &mFabrics, .clientPool = &mCASEClientPool, - .groupDataProvider = &mGroupsProvider, + .groupDataProvider = mGroupsProvider, }, #if CHIP_CONFIG_MDNS_CACHE_SIZE > 0 .dnsCache = nullptr, @@ -259,7 +257,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 @@ -293,7 +291,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 diff --git a/src/app/server/Server.h b/src/app/server/Server.h index 78adc35bfb292e..786dc164778fde 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,80 @@ using ServerTransportMgr = chip::TransportMgr; +struct ServerInitParams +{ + ServerInitParams() = default; + virtual ~ServerInitParams() = default; + + // Not copyable + ServerInitParams(const ServerInitParams &) = delete; + ServerInitParams & operator=(const ServerInitParams &) = delete; + + // App delegate to handle some 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. + PersistentStorageDelegate * persistentStorageDelegate = nullptr; + // Group data provider: MUST be injected. Used to maintain critical keys such as the Identity + // Protection Key (IPK) for case + Credentials::GroupDataProvider * groupDataProvider = nullptr; + // Access control delegate: MUST be injected. Used to look-up access control rules + Access::AccessControl::Delegate * accessDelegate = nullptr; +}; + +// Transitional pre-injection version of ServierInitParams +struct CommonCaseDeviceServerInitParams : public ServerInitParams +{ + CommonCaseDeviceServerInitParams() = default; + + // Not copyable + CommonCaseDeviceServerInitParams(const CommonCaseDeviceServerInitParams &) = delete; + CommonCaseDeviceServerInitParams & operator=(const CommonCaseDeviceServerInitParams &) = delete; + + virtual CHIP_ERROR InitBeforeServerInit() + { + 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; + } + + virtual void Shutdown() + { + if (accessDelegate != nullptr) + { + accessDelegate->Finish(); + } + if (groupDataProvider != nullptr) + { + groupDataProvider->Finish(); + } + } +}; + 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); @@ -86,7 +158,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; } @@ -94,7 +166,7 @@ class Server CommissioningWindowManager & GetCommissioningWindowManager() { return mCommissioningWindowManager; } - PersistentStorageDelegate & GetPersistentStorage() { return mDeviceStorage; } + PersistentStorageDelegate & GetPersistentStorage() { return *mDeviceStorage; } /** * This function send the ShutDown event before stopping @@ -113,62 +185,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: @@ -256,19 +272,16 @@ class Server SecurePairingUsingTestSecret mTestPairing; 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..6644e6855dbe60 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.InitBeforeServerInit(); + 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..c6ed6f2d891bbb 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.InitBeforeServerInit(); + 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..881b206afafe69 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.InitBeforeServerInit(); + 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 73d4cabe87c1cf..fd3046af217479 100644 --- a/src/platform/BUILD.gn +++ b/src/platform/BUILD.gn @@ -290,6 +290,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",