Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 3 | * Copyright (c) 2021 Project CHIP Authors |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Kamil Kasperczyk | d05149c | 2021-01-05 16:05:27 +0100 | [diff] [blame] | 18 | #include <app/server/Server.h> |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 19 | |
Andrei Litvin | d099b0b | 2024-11-11 11:12:12 -0500 | [diff] [blame] | 20 | #include <access/ProviderDeviceTypeResolver.h> |
Andy Salisbury | 62e37a2 | 2022-02-04 11:00:00 -0500 | [diff] [blame] | 21 | #include <access/examples/ExampleAccessControlDelegate.h> |
| 22 | |
Andrei Litvin | 994c390 | 2024-10-04 12:41:54 -0400 | [diff] [blame] | 23 | #include <app/AppConfig.h> |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 24 | #include <app/EventManagement.h> |
Song Guo | a77a662 | 2021-01-29 13:13:02 +0800 | [diff] [blame] | 25 | #include <app/InteractionModelEngine.h> |
Andrei Litvin | dad97c8 | 2024-12-05 11:30:14 -0500 | [diff] [blame] | 26 | #include <app/SafeAttributePersistenceProvider.h> |
Andrei Litvin | 994c390 | 2024-10-04 12:41:54 -0400 | [diff] [blame] | 27 | #include <app/data-model-provider/Provider.h> |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 28 | #include <app/server/Dnssd.h> |
Yufeng Wang | a47019b | 2021-03-09 11:59:12 -0800 | [diff] [blame] | 29 | #include <app/server/EchoHandler.h> |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 30 | |
wendythewan | e6141d4 | 2022-03-24 05:47:29 -0700 | [diff] [blame] | 31 | #if CONFIG_NETWORK_LAYER_BLE |
Arkadiusz Bokowy | 9966940 | 2024-04-18 11:31:44 +0200 | [diff] [blame] | 32 | #include <ble/Ble.h> |
wendythewan | e6141d4 | 2022-03-24 05:47:29 -0700 | [diff] [blame] | 33 | #endif |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 34 | #include <inet/IPAddress.h> |
| 35 | #include <inet/InetError.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 36 | #include <lib/core/CHIPPersistentStorageDelegate.h> |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 37 | #include <lib/dnssd/Advertiser.h> |
| 38 | #include <lib/dnssd/ServiceNaming.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 39 | #include <lib/support/CodeUtils.h> |
Boris Zbarsky | cc3a98c | 2022-04-14 23:15:16 -0400 | [diff] [blame] | 40 | #include <lib/support/DefaultStorageKeyAllocator.h> |
Boris Zbarsky | cc3a98c | 2022-04-14 23:15:16 -0400 | [diff] [blame] | 41 | #include <lib/support/PersistedCounter.h> |
Tennessee Carmel-Veilleux | 1c03d9c | 2022-03-30 17:44:08 -0400 | [diff] [blame] | 42 | #include <lib/support/TestGroupData.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 43 | #include <lib/support/logging/CHIPLogging.h> |
Song Guo | a77a662 | 2021-01-29 13:13:02 +0800 | [diff] [blame] | 44 | #include <messaging/ExchangeMgr.h> |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 45 | #include <platform/CHIPDeviceLayer.h> |
Tennessee Carmel-Veilleux | 4e3930a | 2022-06-10 09:08:31 -0400 | [diff] [blame] | 46 | #include <platform/DeviceControlServer.h> |
Yufeng Wang | bc7f2c3 | 2022-04-12 14:13:15 -0700 | [diff] [blame] | 47 | #include <platform/DeviceInfoProvider.h> |
Pankaj Garg | 9a791a8 | 2021-02-24 07:48:03 -0800 | [diff] [blame] | 48 | #include <platform/KeyValueStoreManager.h> |
Boris Zbarsky | afa79de | 2023-02-14 09:40:13 -0500 | [diff] [blame] | 49 | #include <platform/LockTracker.h> |
Pankaj Garg | 8ced1a1 | 2021-05-14 06:55:59 -0700 | [diff] [blame] | 50 | #include <protocols/secure_channel/CASEServer.h> |
Zang MingJie | c64d9d1 | 2021-05-13 00:09:08 +0800 | [diff] [blame] | 51 | #include <protocols/secure_channel/MessageCounterManager.h> |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 52 | #if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID) |
| 53 | #include <setup_payload/AdditionalDataPayloadGenerator.h> |
| 54 | #endif |
Vivien Nicolas | 0110e48 | 2020-12-02 19:45:41 +0100 | [diff] [blame] | 55 | #include <setup_payload/SetupPayload.h> |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 56 | #include <sys/param.h> |
| 57 | #include <system/SystemPacketBuffer.h> |
Pankaj Garg | ed28753 | 2021-02-09 14:38:31 -0800 | [diff] [blame] | 58 | #include <system/TLVPacketBufferBackingStore.h> |
Zang MingJie | eca9bff | 2021-09-23 03:19:51 +0800 | [diff] [blame] | 59 | #include <transport/SessionManager.h> |
Tennessee Carmel-Veilleux | a45d571 | 2022-06-29 23:18:33 -0400 | [diff] [blame] | 60 | |
crlonxp | 085f57f | 2024-07-30 11:21:28 +0800 | [diff] [blame] | 61 | #if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF |
| 62 | #include <transport/raw/WiFiPAF.h> |
| 63 | #endif |
| 64 | |
Tennessee Carmel-Veilleux | a45d571 | 2022-06-29 23:18:33 -0400 | [diff] [blame] | 65 | #if defined(CHIP_SUPPORT_ENABLE_STORAGE_API_AUDIT) || defined(CHIP_SUPPORT_ENABLE_STORAGE_LOAD_TEST_AUDIT) |
| 66 | #include <lib/support/PersistentStorageAudit.h> |
| 67 | #endif // defined(CHIP_SUPPORT_ENABLE_STORAGE_API_AUDIT) || defined(CHIP_SUPPORT_ENABLE_STORAGE_LOAD_TEST_AUDIT) |
| 68 | |
Damian Królik | 1c74a21 | 2022-03-10 10:45:52 +0100 | [diff] [blame] | 69 | using namespace chip::DeviceLayer; |
| 70 | |
Pankaj Garg | 5d4d073 | 2021-09-30 14:15:20 -0700 | [diff] [blame] | 71 | using chip::kMinValidFabricIndex; |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 72 | using chip::RendezvousInformationFlag; |
| 73 | using chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr; |
| 74 | using chip::Inet::IPAddressType; |
wendythewan | e6141d4 | 2022-03-24 05:47:29 -0700 | [diff] [blame] | 75 | #if CONFIG_NETWORK_LAYER_BLE |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 76 | using chip::Transport::BleListenParameters; |
wendythewan | e6141d4 | 2022-03-24 05:47:29 -0700 | [diff] [blame] | 77 | #endif |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 78 | using chip::Transport::PeerAddress; |
| 79 | using chip::Transport::UdpListenParameters; |
Pradip De | a8cfbac | 2024-03-26 20:59:15 -0700 | [diff] [blame] | 80 | #if INET_CONFIG_ENABLE_TCP_ENDPOINT |
| 81 | using chip::Transport::TcpListenParameters; |
| 82 | #endif |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 83 | |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 84 | namespace { |
Jiacheng Guo | 69d2d59 | 2020-09-18 12:19:38 +0800 | [diff] [blame] | 85 | |
Andrei Litvin | d099b0b | 2024-11-11 11:12:12 -0500 | [diff] [blame] | 86 | chip::Access::DynamicProviderDeviceTypeResolver sDeviceTypeResolver([] { |
| 87 | return chip::app::InteractionModelEngine::GetInstance()->GetDataModelProvider(); |
| 88 | }); |
Marc Lepage | df68705 | 2022-03-30 15:47:56 -0400 | [diff] [blame] | 89 | |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 90 | } // namespace |
| 91 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 92 | namespace chip { |
| 93 | |
| 94 | Server Server::sServer; |
| 95 | |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 96 | #if CHIP_CONFIG_ENABLE_SERVER_IM_EVENT |
| 97 | #define CHIP_NUM_EVENT_LOGGING_BUFFERS 3 |
| 98 | static uint8_t sInfoEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE]; |
| 99 | static uint8_t sDebugEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE]; |
| 100 | static uint8_t sCritEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE]; |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 101 | static PersistedCounter<EventNumber> sGlobalEventIdCounter; |
| 102 | static app::CircularEventBuffer sLoggingBuffer[CHIP_NUM_EVENT_LOGGING_BUFFERS]; |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 103 | #endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT |
| 104 | |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 105 | CHIP_ERROR Server::Init(const ServerInitParams & initParams) |
Pankaj Garg | eac670e | 2021-02-02 18:36:52 -0800 | [diff] [blame] | 106 | { |
Marc Lepage | ad2520a | 2022-04-29 15:17:17 -0400 | [diff] [blame] | 107 | ChipLogProgress(AppServer, "Server initializing..."); |
Boris Zbarsky | afa79de | 2023-02-14 09:40:13 -0500 | [diff] [blame] | 108 | assertChipStackLockedByCurrentThread(); |
Marc Lepage | ad2520a | 2022-04-29 15:17:17 -0400 | [diff] [blame] | 109 | |
Boris Zbarsky | 1d6a958 | 2023-02-14 13:22:17 -0500 | [diff] [blame] | 110 | mInitTimestamp = System::SystemClock().GetMonotonicMicroseconds64(); |
| 111 | |
Tennessee Carmel-Veilleux | 1c03d9c | 2022-03-30 17:44:08 -0400 | [diff] [blame] | 112 | CASESessionManagerConfig caseSessionManagerConfig; |
Yufeng Wang | bc7f2c3 | 2022-04-12 14:13:15 -0700 | [diff] [blame] | 113 | DeviceLayer::DeviceInfoProvider * deviceInfoprovider = nullptr; |
Tennessee Carmel-Veilleux | 10ab541 | 2022-03-10 18:37:47 -0500 | [diff] [blame] | 114 | |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 115 | mOperationalServicePort = initParams.operationalServicePort; |
| 116 | mUserDirectedCommissioningPort = initParams.userDirectedCommissioningPort; |
| 117 | mInterfaceId = initParams.interfaceId; |
Pankaj Garg | ed28753 | 2021-02-09 14:38:31 -0800 | [diff] [blame] | 118 | |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 119 | CHIP_ERROR err = CHIP_NO_ERROR; |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 120 | |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 121 | VerifyOrExit(initParams.persistentStorageDelegate != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 122 | VerifyOrExit(initParams.accessDelegate != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); |
Marc Lepage | 53c29b5 | 2022-05-03 19:02:43 -0400 | [diff] [blame] | 123 | VerifyOrExit(initParams.aclStorage != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); |
Marc Lepage | ad2520a | 2022-04-29 15:17:17 -0400 | [diff] [blame] | 124 | VerifyOrExit(initParams.groupDataProvider != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); |
Damian Królik | b07872c | 2023-02-17 13:56:47 +0100 | [diff] [blame] | 125 | VerifyOrExit(initParams.sessionKeystore != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 126 | VerifyOrExit(initParams.operationalKeystore != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); |
| 127 | VerifyOrExit(initParams.opCertStore != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); |
lpbeliveau-silabs | a70d5b4 | 2023-08-04 17:12:49 -0400 | [diff] [blame] | 128 | VerifyOrExit(initParams.reportScheduler != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 129 | |
Andrei Litvin | 60dd784 | 2024-11-20 11:11:48 -0500 | [diff] [blame] | 130 | // Extra log since this is an incremental requirement and existing applications may not be aware |
| 131 | if (initParams.dataModelProvider == nullptr) |
| 132 | { |
| 133 | ChipLogError(AppServer, "Application Server requires a `initParams.dataModelProvider` value."); |
Andrei Litvin | dad97c8 | 2024-12-05 11:30:14 -0500 | [diff] [blame] | 134 | ChipLogError(AppServer, "For backwards compatibility, you likely can use `CodegenDataModelProviderInstance(...)`"); |
Andrei Litvin | 60dd784 | 2024-11-20 11:11:48 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | VerifyOrExit(initParams.dataModelProvider != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); |
| 138 | |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 139 | // TODO(16969): Remove Platform::MemoryInit() call from Server class, it belongs to outer code |
| 140 | Platform::MemoryInit(); |
Pankaj Garg | 9a791a8 | 2021-02-24 07:48:03 -0800 | [diff] [blame] | 141 | |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 142 | // Initialize PersistentStorageDelegate-based storage |
Jeff Tung | 37919a1 | 2023-01-20 17:58:51 -0800 | [diff] [blame] | 143 | mDeviceStorage = initParams.persistentStorageDelegate; |
| 144 | mSessionResumptionStorage = initParams.sessionResumptionStorage; |
| 145 | mSubscriptionResumptionStorage = initParams.subscriptionResumptionStorage; |
| 146 | mOperationalKeystore = initParams.operationalKeystore; |
| 147 | mOpCertStore = initParams.opCertStore; |
cbucht200 | a39e88a | 2023-06-29 14:59:11 -0400 | [diff] [blame] | 148 | mSessionKeystore = initParams.sessionKeystore; |
Boris Zbarsky | ec71346 | 2022-01-06 15:53:22 -0500 | [diff] [blame] | 149 | |
Boris Zbarsky | db47b1b | 2023-05-15 21:44:56 -0400 | [diff] [blame] | 150 | if (initParams.certificateValidityPolicy) |
| 151 | { |
| 152 | mCertificateValidityPolicy.Init(initParams.certificateValidityPolicy); |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | mCertificateValidityPolicy.Init(&sDefaultCertValidityPolicy); |
| 157 | } |
Michael Sandstedt | 8cc291d | 2022-06-07 10:02:58 -0500 | [diff] [blame] | 158 | |
Tennessee Carmel-Veilleux | a45d571 | 2022-06-29 23:18:33 -0400 | [diff] [blame] | 159 | #if defined(CHIP_SUPPORT_ENABLE_STORAGE_API_AUDIT) |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 160 | VerifyOrDie(audit::ExecutePersistentStorageApiAudit(*mDeviceStorage)); |
Tennessee Carmel-Veilleux | a45d571 | 2022-06-29 23:18:33 -0400 | [diff] [blame] | 161 | #endif |
| 162 | |
| 163 | #if defined(CHIP_SUPPORT_ENABLE_STORAGE_LOAD_TEST_AUDIT) |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 164 | VerifyOrDie(audit::ExecutePersistentStorageLoadTestAudit(*mDeviceStorage)); |
Tennessee Carmel-Veilleux | a45d571 | 2022-06-29 23:18:33 -0400 | [diff] [blame] | 165 | #endif |
| 166 | |
Boris Zbarsky | ec71346 | 2022-01-06 15:53:22 -0500 | [diff] [blame] | 167 | // Set up attribute persistence before we try to bring up the data model |
| 168 | // handler. |
Boris Zbarsky | ad5253a | 2023-05-26 02:22:37 -0400 | [diff] [blame] | 169 | SuccessOrExit(err = mAttributePersister.Init(mDeviceStorage)); |
William | 5dd4b06 | 2023-07-28 12:45:52 +0100 | [diff] [blame] | 170 | SetSafeAttributePersistenceProvider(&mAttributePersister); |
Boris Zbarsky | ec71346 | 2022-01-06 15:53:22 -0500 | [diff] [blame] | 171 | |
Tennessee Carmel-Veilleux | e5e09f5 | 2022-06-24 16:57:34 -0400 | [diff] [blame] | 172 | { |
| 173 | FabricTable::InitParams fabricTableInitParams; |
| 174 | fabricTableInitParams.storage = mDeviceStorage; |
| 175 | fabricTableInitParams.operationalKeystore = mOperationalKeystore; |
| 176 | fabricTableInitParams.opCertStore = mOpCertStore; |
| 177 | |
| 178 | err = mFabrics.Init(fabricTableInitParams); |
| 179 | SuccessOrExit(err); |
| 180 | } |
shana-apple | 3907f38 | 2021-05-05 08:50:42 +0200 | [diff] [blame] | 181 | |
Marc Lepage | ad2520a | 2022-04-29 15:17:17 -0400 | [diff] [blame] | 182 | SuccessOrExit(err = mAccessControl.Init(initParams.accessDelegate, sDeviceTypeResolver)); |
| 183 | Access::SetAccessControl(mAccessControl); |
Marc Lepage | 53c29b5 | 2022-05-03 19:02:43 -0400 | [diff] [blame] | 184 | |
Thomas Lea | 87f6277 | 2024-08-27 13:27:10 -0500 | [diff] [blame] | 185 | #if CHIP_CONFIG_USE_ACCESS_RESTRICTIONS |
| 186 | if (initParams.accessRestrictionProvider != nullptr) |
| 187 | { |
| 188 | mAccessControl.SetAccessRestrictionProvider(initParams.accessRestrictionProvider); |
| 189 | } |
| 190 | #endif |
| 191 | |
Marc Lepage | 53c29b5 | 2022-05-03 19:02:43 -0400 | [diff] [blame] | 192 | mAclStorage = initParams.aclStorage; |
| 193 | SuccessOrExit(err = mAclStorage->Init(*mDeviceStorage, mFabrics.begin(), mFabrics.end())); |
Marc Lepage | ad2520a | 2022-04-29 15:17:17 -0400 | [diff] [blame] | 194 | |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 195 | mGroupsProvider = initParams.groupDataProvider; |
| 196 | SetGroupDataProvider(mGroupsProvider); |
Ricardo Casallas | a5513c2 | 2021-12-02 11:22:19 -0500 | [diff] [blame] | 197 | |
lpbeliveau-silabs | a70d5b4 | 2023-08-04 17:12:49 -0400 | [diff] [blame] | 198 | mReportScheduler = initParams.reportScheduler; |
| 199 | |
tehampson | 64a3d3a | 2022-05-30 14:51:20 -0400 | [diff] [blame] | 200 | mTestEventTriggerDelegate = initParams.testEventTriggerDelegate; |
fesseha-eve | 522668f | 2024-07-16 14:33:24 +0200 | [diff] [blame] | 201 | if (mTestEventTriggerDelegate == nullptr) |
| 202 | { |
| 203 | ChipLogProgress(AppServer, "WARNING: mTestEventTriggerDelegate is null"); |
| 204 | } |
tehampson | 64a3d3a | 2022-05-30 14:51:20 -0400 | [diff] [blame] | 205 | |
Yufeng Wang | bc7f2c3 | 2022-04-12 14:13:15 -0700 | [diff] [blame] | 206 | deviceInfoprovider = DeviceLayer::GetDeviceInfoProvider(); |
| 207 | if (deviceInfoprovider) |
| 208 | { |
| 209 | deviceInfoprovider->SetStorageDelegate(mDeviceStorage); |
| 210 | } |
| 211 | |
Song Guo | fba2fdc | 2020-11-21 04:08:24 +0800 | [diff] [blame] | 212 | // Init transport before operations with secure session mgr. |
Kevin Schoedel | 8e5eb8a | 2021-12-09 11:07:45 -0500 | [diff] [blame] | 213 | err = mTransports.Init(UdpListenParameters(DeviceLayer::UDPEndPointManager()) |
| 214 | .SetAddressType(IPAddressType::kIPv6) |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 215 | .SetListenPort(mOperationalServicePort) |
Jean-Francois Penven | 9405c20 | 2022-05-03 15:37:44 -0400 | [diff] [blame] | 216 | .SetNativeParams(initParams.endpointNativeParams) |
Pradip De | 0129a57 | 2025-01-09 13:59:41 -0800 | [diff] [blame] | 217 | #if INET_CONFIG_ENABLE_TCP_ENDPOINT |
| 218 | , |
| 219 | TcpListenParameters(DeviceLayer::TCPEndPointManager()) |
| 220 | .SetAddressType(IPAddressType::kIPv6) |
| 221 | .SetListenPort(mOperationalServicePort) |
| 222 | #endif |
Vivien Nicolas | 0110e48 | 2020-12-02 19:45:41 +0100 | [diff] [blame] | 223 | #if INET_CONFIG_ENABLE_IPV4 |
Kevin Schoedel | 8e5eb8a | 2021-12-09 11:07:45 -0500 | [diff] [blame] | 224 | , |
| 225 | UdpListenParameters(DeviceLayer::UDPEndPointManager()) |
| 226 | .SetAddressType(IPAddressType::kIPv4) |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 227 | .SetListenPort(mOperationalServicePort) |
Vivien Nicolas | 0110e48 | 2020-12-02 19:45:41 +0100 | [diff] [blame] | 228 | #endif |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 229 | #if CONFIG_NETWORK_LAYER_BLE |
Kevin Schoedel | 8e5eb8a | 2021-12-09 11:07:45 -0500 | [diff] [blame] | 230 | , |
| 231 | BleListenParameters(DeviceLayer::ConnectivityMgr().GetBleLayer()) |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 232 | #endif |
crlonxp | 085f57f | 2024-07-30 11:21:28 +0800 | [diff] [blame] | 233 | #if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF |
| 234 | , |
| 235 | Transport::WiFiPAFListenParameters(DeviceLayer::ConnectivityMgr().GetWiFiPAF()) |
| 236 | #endif |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 237 | ); |
| 238 | |
Sam Ryan | 9cd8c0d | 2023-12-08 17:29:05 -0500 | [diff] [blame] | 239 | SuccessOrExit(err); |
Boris Zbarsky | 393b319 | 2022-06-01 10:09:08 -0400 | [diff] [blame] | 240 | err = mListener.Init(this); |
mkardous-silabs | de95567 | 2022-01-25 16:05:37 -0500 | [diff] [blame] | 241 | SuccessOrExit(err); |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 242 | mGroupsProvider->SetListener(&mListener); |
mkardous-silabs | de95567 | 2022-01-25 16:05:37 -0500 | [diff] [blame] | 243 | |
Sweety | c6db337 | 2021-09-30 06:55:02 +0530 | [diff] [blame] | 244 | #if CONFIG_NETWORK_LAYER_BLE |
| 245 | mBleLayer = DeviceLayer::ConnectivityMgr().GetBleLayer(); |
| 246 | #endif |
Song Guo | fba2fdc | 2020-11-21 04:08:24 +0800 | [diff] [blame] | 247 | SuccessOrExit(err); |
| 248 | |
Damian Królik | b07872c | 2023-02-17 13:56:47 +0100 | [diff] [blame] | 249 | err = mSessions.Init(&DeviceLayer::SystemLayer(), &mTransports, &mMessageCounterManager, mDeviceStorage, &GetFabricTable(), |
cbucht200 | a39e88a | 2023-06-29 14:59:11 -0400 | [diff] [blame] | 250 | *mSessionKeystore); |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 251 | SuccessOrExit(err); |
| 252 | |
Tennessee Carmel-Veilleux | 1c03d9c | 2022-03-30 17:44:08 -0400 | [diff] [blame] | 253 | err = mFabricDelegate.Init(this); |
Jean-Francois Penven | a69fde3 | 2022-03-08 14:57:39 -0500 | [diff] [blame] | 254 | SuccessOrExit(err); |
| 255 | mFabrics.AddFabricDelegate(&mFabricDelegate); |
| 256 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 257 | err = mExchangeMgr.Init(&mSessions); |
Song Guo | a77a662 | 2021-01-29 13:13:02 +0800 | [diff] [blame] | 258 | SuccessOrExit(err); |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 259 | err = mMessageCounterManager.Init(&mExchangeMgr); |
Zang MingJie | c64d9d1 | 2021-05-13 00:09:08 +0800 | [diff] [blame] | 260 | SuccessOrExit(err); |
Pankaj Garg | fad6d05 | 2021-01-20 12:26:34 -0800 | [diff] [blame] | 261 | |
Boris Zbarsky | 83a0863 | 2022-06-15 20:11:04 -0400 | [diff] [blame] | 262 | err = mUnsolicitedStatusHandler.Init(&mExchangeMgr); |
| 263 | SuccessOrExit(err); |
| 264 | |
Boris Zbarsky | 39e5ed2 | 2022-07-14 14:34:50 -0400 | [diff] [blame] | 265 | SuccessOrExit(err = mCommissioningWindowManager.Init(this)); |
| 266 | mCommissioningWindowManager.SetAppDelegate(initParams.appDelegate); |
| 267 | |
| 268 | app::DnssdServer::Instance().SetFabricTable(&mFabrics); |
| 269 | app::DnssdServer::Instance().SetCommissioningModeProvider(&mCommissioningWindowManager); |
| 270 | |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 271 | Dnssd::Resolver::Instance().Init(DeviceLayer::UDPEndPointManager()); |
Andrei Litvin | b4fd95f | 2022-03-15 17:10:11 -0400 | [diff] [blame] | 272 | |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 273 | #if CHIP_CONFIG_ENABLE_SERVER_IM_EVENT |
| 274 | // Initialize event logging subsystem |
Andrei Litvin | 59aa6b7 | 2022-11-03 15:53:41 -0400 | [diff] [blame] | 275 | err = sGlobalEventIdCounter.Init(mDeviceStorage, DefaultStorageKeyAllocator::IMEventNumber(), |
Boris Zbarsky | cc3a98c | 2022-04-14 23:15:16 -0400 | [diff] [blame] | 276 | CHIP_DEVICE_CONFIG_EVENT_ID_COUNTER_EPOCH); |
| 277 | SuccessOrExit(err); |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 278 | |
Boris Zbarsky | cc3a98c | 2022-04-14 23:15:16 -0400 | [diff] [blame] | 279 | { |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 280 | app::LogStorageResources logStorageResources[] = { |
| 281 | { &sDebugEventBuffer[0], sizeof(sDebugEventBuffer), app::PriorityLevel::Debug }, |
| 282 | { &sInfoEventBuffer[0], sizeof(sInfoEventBuffer), app::PriorityLevel::Info }, |
| 283 | { &sCritEventBuffer[0], sizeof(sCritEventBuffer), app::PriorityLevel::Critical } |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 284 | }; |
| 285 | |
Yuanyao Zhong | a219ff7 | 2025-01-10 14:27:33 -0500 | [diff] [blame^] | 286 | err = app::EventManagement::GetInstance().Init(&mExchangeMgr, CHIP_NUM_EVENT_LOGGING_BUFFERS, &sLoggingBuffer[0], |
| 287 | &logStorageResources[0], &sGlobalEventIdCounter, |
| 288 | std::chrono::duration_cast<System::Clock::Milliseconds64>(mInitTimestamp), |
| 289 | &app::InteractionModelEngine::GetInstance()->GetReportingEngine()); |
| 290 | |
| 291 | SuccessOrExit(err); |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 292 | } |
| 293 | #endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT |
| 294 | |
Yufeng Wang | a9bd0ce | 2024-12-17 11:26:26 -0800 | [diff] [blame] | 295 | // SetDataModelProvider() initializes and starts the provider, which in turn |
| 296 | // triggers the initialization of cluster implementations. This callsite is |
| 297 | // critical because it ensures that cluster-level initialization occurs only |
| 298 | // after all necessary low-level dependencies have been set up. |
| 299 | // |
| 300 | // Ordering guarantees: |
| 301 | // 1) Provider initialization (under SetDataModelProvider) must happen after |
| 302 | // SetSafeAttributePersistenceProvider to ensure the provider can leverage |
| 303 | // the safe persistence provider for attribute persistence logic. |
| 304 | // 2) It must occur after all low-level components that cluster implementations |
| 305 | // might depend on have been initialized, as they rely on these components |
| 306 | // during their own initialization. |
| 307 | // |
| 308 | // This remains the single point of entry to ensure that all cluster-level |
| 309 | // initialization is performed in the correct order. |
| 310 | app::InteractionModelEngine::GetInstance()->SetDataModelProvider(initParams.dataModelProvider); |
Jean-Francois Penven | b673df5 | 2023-09-06 14:39:47 -0400 | [diff] [blame] | 311 | |
Yufeng Wang | a47019b | 2021-03-09 11:59:12 -0800 | [diff] [blame] | 312 | #if defined(CHIP_APP_USE_ECHO) |
Vivien Nicolas | b58b7be | 2022-04-01 04:11:04 +0200 | [diff] [blame] | 313 | err = InitEchoHandler(&mExchangeMgr); |
Yufeng Wang | a47019b | 2021-03-09 11:59:12 -0800 | [diff] [blame] | 314 | SuccessOrExit(err); |
| 315 | #endif |
| 316 | |
Jerry Johns | b7a7788 | 2022-07-08 15:11:24 -0700 | [diff] [blame] | 317 | // |
| 318 | // We need to advertise the port that we're listening to for unsolicited messages over UDP. However, we have both a IPv4 |
| 319 | // and IPv6 endpoint to pick from. Given that the listen port passed in may be set to 0 (which then has the kernel select |
| 320 | // a valid port at bind time), that will result in two possible ports being provided back from the resultant endpoint |
| 321 | // initializations. Since IPv6 is POR for Matter, let's go ahead and pick that port. |
| 322 | // |
| 323 | app::DnssdServer::Instance().SetSecuredPort(mTransports.GetTransport().GetImplAtIndex<0>().GetBoundPort()); |
| 324 | |
Vivien Nicolas | c9350f7 | 2022-04-12 06:45:34 +0200 | [diff] [blame] | 325 | app::DnssdServer::Instance().SetUnsecuredPort(mUserDirectedCommissioningPort); |
| 326 | app::DnssdServer::Instance().SetInterfaceId(mInterfaceId); |
| 327 | |
mkardous-silabs | 6778d3e | 2023-11-28 17:39:41 -0500 | [diff] [blame] | 328 | #if CHIP_CONFIG_ENABLE_ICD_SERVER |
| 329 | // We set the ICDManager reference betfore calling the ICDManager init due to the init ordering limitations. |
| 330 | // DnssdServer will use the default value initially and will update advertisement once ICDManager |
| 331 | // init is called. |
| 332 | app::DnssdServer::Instance().SetICDManager(&mICDManager); |
| 333 | #endif // CHIP_CONFIG_ENABLE_ICD_SERVER |
| 334 | |
Pradip De | 0129a57 | 2025-01-09 13:59:41 -0800 | [diff] [blame] | 335 | #if INET_CONFIG_ENABLE_TCP_ENDPOINT |
| 336 | app::DnssdServer::Instance().SetTCPServerEnabled(mTransports.GetTransport().GetImplAtIndex<1>().IsServerListenEnabled()); |
| 337 | #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT |
| 338 | |
Boris Zbarsky | 4d6858b | 2022-03-24 08:30:14 -0400 | [diff] [blame] | 339 | if (GetFabricTable().FabricCount() != 0) |
Pankaj Garg | eac670e | 2021-02-02 18:36:52 -0800 | [diff] [blame] | 340 | { |
Yufeng Wang | 9afad66 | 2024-10-22 10:21:42 -0700 | [diff] [blame] | 341 | #if CONFIG_NETWORK_LAYER_BLE |
Pankaj Garg | ccdffbc | 2021-09-24 09:10:22 -0700 | [diff] [blame] | 342 | // The device is already commissioned, proactively disable BLE advertisement. |
| 343 | ChipLogProgress(AppServer, "Fabric already commissioned. Disabling BLE advertisement"); |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 344 | DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(false); |
Hui.Li-TCL | 360252c | 2022-01-05 00:04:21 +0800 | [diff] [blame] | 345 | #endif |
Pankaj Garg | eac670e | 2021-02-02 18:36:52 -0800 | [diff] [blame] | 346 | } |
Vivien Nicolas | 0110e48 | 2020-12-02 19:45:41 +0100 | [diff] [blame] | 347 | else |
Jiacheng Guo | df2497d | 2020-09-30 21:16:57 +0800 | [diff] [blame] | 348 | { |
Kamil Kasperczyk | 6ad5711 | 2021-02-11 16:44:36 +0100 | [diff] [blame] | 349 | #if CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART |
Pankaj Garg | 2815a10 | 2021-09-15 17:22:34 -0700 | [diff] [blame] | 350 | SuccessOrExit(err = mCommissioningWindowManager.OpenBasicCommissioningWindow()); |
Kamil Kasperczyk | 6ad5711 | 2021-02-11 16:44:36 +0100 | [diff] [blame] | 351 | #endif |
Jiacheng Guo | df2497d | 2020-09-30 21:16:57 +0800 | [diff] [blame] | 352 | } |
Jiacheng Guo | df2497d | 2020-09-30 21:16:57 +0800 | [diff] [blame] | 353 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 354 | // TODO @bzbarsky-apple @cecille Move to examples |
| 355 | // ESP32 and Mbed OS examples have a custom logic for enabling DNS-SD |
Andrei Litvin | b792463 | 2022-03-03 15:29:54 -0500 | [diff] [blame] | 356 | #if !CHIP_DEVICE_LAYER_TARGET_ESP32 && !CHIP_DEVICE_LAYER_TARGET_MBED && \ |
pankore | bab75e6 | 2021-10-28 21:48:46 +0800 | [diff] [blame] | 357 | (!CHIP_DEVICE_LAYER_TARGET_AMEBA || !CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE) |
chrisdecenzo | 515ea65 | 2021-08-18 11:27:09 -0700 | [diff] [blame] | 358 | // StartServer only enables commissioning mode if device has not been commissioned |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 359 | app::DnssdServer::Instance().StartServer(); |
Paul Zander | 3510f14 | 2021-04-15 16:29:40 +0200 | [diff] [blame] | 360 | #endif |
Kamil Kasperczyk | 4401402 | 2021-03-12 21:18:45 +0100 | [diff] [blame] | 361 | |
Tennessee Carmel-Veilleux | 1c03d9c | 2022-03-30 17:44:08 -0400 | [diff] [blame] | 362 | caseSessionManagerConfig = { |
| 363 | .sessionInitParams = { |
| 364 | .sessionManager = &mSessions, |
Zang MingJie | 7dede20 | 2022-04-06 11:54:50 +0800 | [diff] [blame] | 365 | .sessionResumptionStorage = mSessionResumptionStorage, |
Boris Zbarsky | ed8d546 | 2023-05-05 12:19:38 -0400 | [diff] [blame] | 366 | .certificateValidityPolicy = &mCertificateValidityPolicy, |
Tennessee Carmel-Veilleux | 1c03d9c | 2022-03-30 17:44:08 -0400 | [diff] [blame] | 367 | .exchangeMgr = &mExchangeMgr, |
Tennessee Carmel-Veilleux | 1c03d9c | 2022-03-30 17:44:08 -0400 | [diff] [blame] | 368 | .fabricTable = &mFabrics, |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 369 | .groupDataProvider = mGroupsProvider, |
Boris Zbarsky | ac1ebc5 | 2024-02-29 21:59:14 -0500 | [diff] [blame] | 370 | // Don't provide an MRP local config, so each CASE initiation will use |
| 371 | // the then-current value. |
| 372 | .mrpLocalConfig = NullOptional, |
Tennessee Carmel-Veilleux | 1c03d9c | 2022-03-30 17:44:08 -0400 | [diff] [blame] | 373 | }, |
Damian Królik | ffecbc1 | 2022-12-14 19:00:35 +0100 | [diff] [blame] | 374 | .clientPool = &mCASEClientPool, |
| 375 | .sessionSetupPool = &mSessionSetupPool, |
Tennessee Carmel-Veilleux | 1c03d9c | 2022-03-30 17:44:08 -0400 | [diff] [blame] | 376 | }; |
| 377 | |
| 378 | err = mCASESessionManager.Init(&DeviceLayer::SystemLayer(), caseSessionManagerConfig); |
| 379 | SuccessOrExit(err); |
| 380 | |
Michael Sandstedt | 8cc291d | 2022-06-07 10:02:58 -0500 | [diff] [blame] | 381 | err = mCASEServer.ListenForSessionEstablishment(&mExchangeMgr, &mSessions, &mFabrics, mSessionResumptionStorage, |
Boris Zbarsky | ed8d546 | 2023-05-05 12:19:38 -0400 | [diff] [blame] | 382 | &mCertificateValidityPolicy, mGroupsProvider); |
Pankaj Garg | 8ced1a1 | 2021-05-14 06:55:59 -0700 | [diff] [blame] | 383 | SuccessOrExit(err); |
| 384 | |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 385 | err = app::InteractionModelEngine::GetInstance()->Init(&mExchangeMgr, &GetFabricTable(), mReportScheduler, &mCASESessionManager, |
| 386 | mSubscriptionResumptionStorage); |
Jerry Johns | 6cd4676 | 2022-07-27 12:42:07 -0700 | [diff] [blame] | 387 | SuccessOrExit(err); |
| 388 | |
mkardous-silabs | 96329ae | 2024-04-03 22:17:11 -0400 | [diff] [blame] | 389 | #if CHIP_CONFIG_ENABLE_ICD_SERVER |
| 390 | app::InteractionModelEngine::GetInstance()->SetICDManager(&mICDManager); |
| 391 | #endif // CHIP_CONFIG_ENABLE_ICD_SERVER |
| 392 | |
Jean-Francois Penven | ed891e2 | 2023-11-28 13:18:44 -0500 | [diff] [blame] | 393 | // ICD Init needs to be after data model init and InteractionModel Init |
| 394 | #if CHIP_CONFIG_ENABLE_ICD_SERVER |
mkardous-silabs | 6778d3e | 2023-11-28 17:39:41 -0500 | [diff] [blame] | 395 | |
| 396 | // Register the ICDStateObservers. |
| 397 | // Call register before init so that observers are notified of any state change during the init. |
| 398 | // All observers are released at mICDManager.Shutdown(). They can be released individually with ReleaseObserver |
Jean-Francois Penven | ed891e2 | 2023-11-28 13:18:44 -0500 | [diff] [blame] | 399 | mICDManager.RegisterObserver(mReportScheduler); |
| 400 | mICDManager.RegisterObserver(&app::DnssdServer::Instance()); |
mkardous-silabs | 6778d3e | 2023-11-28 17:39:41 -0500 | [diff] [blame] | 401 | |
mkardous-silabs | 5eba394 | 2024-07-26 19:42:45 -0400 | [diff] [blame] | 402 | #if CHIP_CONFIG_ENABLE_ICD_CIP |
| 403 | mICDManager.SetPersistentStorageDelegate(mDeviceStorage) |
| 404 | .SetFabricTable(&GetFabricTable()) |
| 405 | .SetSymmetricKeyStore(mSessionKeystore) |
| 406 | .SetExchangeManager(&mExchangeMgr) |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 407 | .SetSubscriptionsInfoProvider(app::InteractionModelEngine::GetInstance()) |
mkardous-silabs | 5eba394 | 2024-07-26 19:42:45 -0400 | [diff] [blame] | 408 | .SetICDCheckInBackOffStrategy(initParams.icdCheckInBackOffStrategy); |
| 409 | |
| 410 | #endif // CHIP_CONFIG_ENABLE_ICD_CIP |
| 411 | mICDManager.Init(); |
mkardous-silabs | 845ce9b | 2024-04-09 20:13:38 -0400 | [diff] [blame] | 412 | |
| 413 | // Register Test Event Trigger Handler |
fesseha-eve | 522668f | 2024-07-16 14:33:24 +0200 | [diff] [blame] | 414 | if (mTestEventTriggerDelegate != nullptr) |
| 415 | { |
| 416 | mTestEventTriggerDelegate->AddHandler(&mICDManager); |
| 417 | } |
mkardous-silabs | 845ce9b | 2024-04-09 20:13:38 -0400 | [diff] [blame] | 418 | |
Jean-Francois Penven | ed891e2 | 2023-11-28 13:18:44 -0500 | [diff] [blame] | 419 | #endif // CHIP_CONFIG_ENABLE_ICD_SERVER |
| 420 | |
mkardous-silabs | de95567 | 2022-01-25 16:05:37 -0500 | [diff] [blame] | 421 | // This code is necessary to restart listening to existing groups after a reboot |
| 422 | // Each manufacturer needs to validate that they can rejoin groups by placing this code at the appropriate location for them |
| 423 | // |
Damian Królik | b80df3d | 2022-03-30 14:33:14 +0200 | [diff] [blame] | 424 | // Thread LWIP devices using dedicated Inet endpoint implementations are excluded because they call this function from: |
mkardous-silabs | de95567 | 2022-01-25 16:05:37 -0500 | [diff] [blame] | 425 | // src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread_LwIP.cpp |
Damian Królik | b80df3d | 2022-03-30 14:33:14 +0200 | [diff] [blame] | 426 | #if !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
mkardous-silabs | 3c31ec7 | 2022-02-24 17:37:45 -0500 | [diff] [blame] | 427 | RejoinExistingMulticastGroups(); |
Damian Królik | b80df3d | 2022-03-30 14:33:14 +0200 | [diff] [blame] | 428 | #endif // !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
mkardous-silabs | 3c31ec7 | 2022-02-24 17:37:45 -0500 | [diff] [blame] | 429 | |
Tennessee Carmel-Veilleux | b91727b | 2022-06-28 20:52:02 -0400 | [diff] [blame] | 430 | // Handle deferred clean-up of a previously armed fail-safe that occurred during FabricTable commit. |
| 431 | // This is done at the very end since at the earlier time above when FabricTable::Init() is called, |
| 432 | // the delegates could not have been registered, and the other systems were not initialized. By now, |
| 433 | // everything is initialized, so we can do a deferred clean-up. |
| 434 | { |
| 435 | FabricIndex fabricIndexDeletedOnInit = GetFabricTable().GetDeletedFabricFromCommitMarker(); |
| 436 | if (fabricIndexDeletedOnInit != kUndefinedFabricIndex) |
| 437 | { |
| 438 | ChipLogError(AppServer, "FabricIndex 0x%x deleted due to restart while fail-safed. Processing a clean-up!", |
| 439 | static_cast<unsigned>(fabricIndexDeletedOnInit)); |
| 440 | |
| 441 | // Always pretend it was an add, since being in the middle of an update currently breaks |
| 442 | // the validity of the fabric table. This is expected to be extremely infrequent, so |
| 443 | // this "harsher" than usual clean-up is more likely to get us in a valid state for whatever |
| 444 | // remains. |
| 445 | const bool addNocCalled = true; |
| 446 | const bool updateNocCalled = false; |
| 447 | GetFailSafeContext().ScheduleFailSafeCleanup(fabricIndexDeletedOnInit, addNocCalled, updateNocCalled); |
| 448 | |
| 449 | // Schedule clearing of the commit marker to only occur after we have processed all fail-safe clean-up. |
| 450 | // Because Matter runs a single event loop for all scheduled work, it will occur after the above has |
| 451 | // taken place. If a reset occurs before we have cleaned everything up, the next boot will still |
| 452 | // see the commit marker. |
| 453 | PlatformMgr().ScheduleWork( |
| 454 | [](intptr_t arg) { |
| 455 | Server * server = reinterpret_cast<Server *>(arg); |
| 456 | VerifyOrReturn(server != nullptr); |
| 457 | |
| 458 | server->GetFabricTable().ClearCommitMarker(); |
| 459 | ChipLogProgress(AppServer, "Cleared FabricTable pending commit marker"); |
| 460 | }, |
| 461 | reinterpret_cast<intptr_t>(this)); |
| 462 | } |
| 463 | } |
| 464 | |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 465 | #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT // support UDC port for commissioner declaration msgs |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 466 | mUdcTransportMgr = Platform::New<UdcTransportMgr>(); |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 467 | ReturnErrorOnFailure(mUdcTransportMgr->Init(Transport::UdpListenParameters(DeviceLayer::UDPEndPointManager()) |
| 468 | .SetAddressType(Inet::IPAddressType::kIPv6) |
| 469 | .SetListenPort(static_cast<uint16_t>(mCdcListenPort)) |
| 470 | #if INET_CONFIG_ENABLE_IPV4 |
| 471 | , |
| 472 | Transport::UdpListenParameters(DeviceLayer::UDPEndPointManager()) |
| 473 | .SetAddressType(Inet::IPAddressType::kIPv4) |
| 474 | .SetListenPort(static_cast<uint16_t>(mCdcListenPort)) |
| 475 | #endif // INET_CONFIG_ENABLE_IPV4 |
| 476 | )); |
| 477 | |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 478 | gUDCClient = Platform::New<Protocols::UserDirectedCommissioning::UserDirectedCommissioningClient>(); |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 479 | mUdcTransportMgr->SetSessionManager(gUDCClient); |
| 480 | #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY |
| 481 | |
Damian Królik | 5a845c6 | 2023-02-02 10:51:40 +0100 | [diff] [blame] | 482 | PlatformMgr().AddEventHandler(OnPlatformEventWrapper, reinterpret_cast<intptr_t>(this)); |
Damian Królik | 1c74a21 | 2022-03-10 10:45:52 +0100 | [diff] [blame] | 483 | PlatformMgr().HandleServerStarted(); |
| 484 | |
Damian Królik | 5a845c6 | 2023-02-02 10:51:40 +0100 | [diff] [blame] | 485 | mIsDnssdReady = Dnssd::Resolver::Instance().IsInitialized(); |
| 486 | CheckServerReadyEvent(); |
| 487 | |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 488 | exit: |
| 489 | if (err != CHIP_NO_ERROR) |
| 490 | { |
Damian Królik | 5e9d413 | 2022-06-27 09:02:15 +0200 | [diff] [blame] | 491 | ChipLogError(AppServer, "ERROR setting up transport: %" CHIP_ERROR_FORMAT, err.Format()); |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 492 | } |
| 493 | else |
| 494 | { |
Marc Lepage | ad2520a | 2022-04-29 15:17:17 -0400 | [diff] [blame] | 495 | // NOTE: this log is scraped by the test harness. |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 496 | ChipLogProgress(AppServer, "Server Listening..."); |
| 497 | } |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 498 | return err; |
| 499 | } |
| 500 | |
Damian Królik | 5a845c6 | 2023-02-02 10:51:40 +0100 | [diff] [blame] | 501 | void Server::OnPlatformEvent(const DeviceLayer::ChipDeviceEvent & event) |
| 502 | { |
| 503 | switch (event.Type) |
| 504 | { |
Damian Królik | 40f7717 | 2023-02-14 17:53:28 +0100 | [diff] [blame] | 505 | case DeviceEventType::kDnssdInitialized: |
Damian Królik | 5a845c6 | 2023-02-02 10:51:40 +0100 | [diff] [blame] | 506 | // Platform DNS-SD implementation uses kPlatformDnssdInitialized event to signal that it's ready. |
| 507 | if (!mIsDnssdReady) |
| 508 | { |
| 509 | mIsDnssdReady = true; |
| 510 | CheckServerReadyEvent(); |
| 511 | } |
| 512 | break; |
| 513 | case DeviceEventType::kServerReady: |
Kamil Kasperczyk | 91ba8b6 | 2024-02-14 10:28:02 +0100 | [diff] [blame] | 514 | #if CHIP_CONFIG_ENABLE_ICD_SERVER && CHIP_CONFIG_ENABLE_ICD_CIP |
mkardous-silabs | 3fe8ec6 | 2024-02-07 21:12:15 -0500 | [diff] [blame] | 515 | // Only Trigger Check-In messages if we are not in the middle of a commissioning. |
| 516 | // This check is only necessary for the first commissioiner since the kServerReady event |
| 517 | // is triggered once we join the network. |
| 518 | // We trigger Check-In messages before resuming subscriptions to avoid doing both. |
| 519 | if (!mFailSafeContext.IsFailSafeArmed()) |
| 520 | { |
mkardous-silabs | 96329ae | 2024-04-03 22:17:11 -0400 | [diff] [blame] | 521 | std::function<app::ICDManager::ShouldCheckInMsgsBeSentFunction> sendCheckInMessagesOnBootUp = |
| 522 | std::bind(&Server::ShouldCheckInMsgsBeSentAtBootFunction, this, std::placeholders::_1, std::placeholders::_2); |
| 523 | mICDManager.TriggerCheckInMessages(sendCheckInMessagesOnBootUp); |
mkardous-silabs | 3fe8ec6 | 2024-02-07 21:12:15 -0500 | [diff] [blame] | 524 | } |
Kamil Kasperczyk | 91ba8b6 | 2024-02-14 10:28:02 +0100 | [diff] [blame] | 525 | #endif // CHIP_CONFIG_ENABLE_ICD_SERVER && CHIP_CONFIG_ENABLE_ICD_CIP |
Damian Królik | 5a845c6 | 2023-02-02 10:51:40 +0100 | [diff] [blame] | 526 | #if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS |
| 527 | ResumeSubscriptions(); |
mkardous-silabs | 96329ae | 2024-04-03 22:17:11 -0400 | [diff] [blame] | 528 | #endif // CHIP_CONFIG_PERSIST_SUBSCRIPTIONS |
Damian Królik | 5a845c6 | 2023-02-02 10:51:40 +0100 | [diff] [blame] | 529 | break; |
Jean-Francois Penven | 41971ba | 2023-11-21 23:13:56 -0500 | [diff] [blame] | 530 | #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
| 531 | case DeviceEventType::kThreadConnectivityChange: |
| 532 | if (event.ThreadConnectivityChange.Result == kConnectivity_Established) |
| 533 | { |
| 534 | // Refresh Multicast listening |
| 535 | ChipLogDetail(DeviceLayer, "Thread Attached updating Multicast address"); |
| 536 | RejoinExistingMulticastGroups(); |
| 537 | } |
| 538 | break; |
| 539 | #endif // CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT |
Damian Królik | 5a845c6 | 2023-02-02 10:51:40 +0100 | [diff] [blame] | 540 | default: |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | void Server::CheckServerReadyEvent() |
| 546 | { |
| 547 | // Check if all asynchronously initialized server components (currently, only DNS-SD) |
| 548 | // are ready, and emit the 'server ready' event if so. |
| 549 | if (mIsDnssdReady) |
| 550 | { |
Shubham Patil | b5cd925 | 2023-06-03 03:47:41 +0530 | [diff] [blame] | 551 | ChipLogProgress(AppServer, "Server initialization complete"); |
Damian Królik | 40f7717 | 2023-02-14 17:53:28 +0100 | [diff] [blame] | 552 | |
Damian Królik | 5a845c6 | 2023-02-02 10:51:40 +0100 | [diff] [blame] | 553 | ChipDeviceEvent event = { .Type = DeviceEventType::kServerReady }; |
| 554 | PlatformMgr().PostEventOrDie(&event); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | void Server::OnPlatformEventWrapper(const DeviceLayer::ChipDeviceEvent * event, intptr_t server) |
| 559 | { |
| 560 | reinterpret_cast<Server *>(server)->OnPlatformEvent(*event); |
| 561 | } |
| 562 | |
mkardous-silabs | 3c31ec7 | 2022-02-24 17:37:45 -0500 | [diff] [blame] | 563 | void Server::RejoinExistingMulticastGroups() |
| 564 | { |
| 565 | ChipLogProgress(AppServer, "Joining Multicast groups"); |
| 566 | CHIP_ERROR err = CHIP_NO_ERROR; |
| 567 | for (const FabricInfo & fabric : mFabrics) |
| 568 | { |
| 569 | Credentials::GroupDataProvider::GroupInfo groupInfo; |
| 570 | |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 571 | auto * iterator = mGroupsProvider->IterateGroupInfo(fabric.GetFabricIndex()); |
mkardous-silabs | 3c31ec7 | 2022-02-24 17:37:45 -0500 | [diff] [blame] | 572 | if (iterator) |
| 573 | { |
| 574 | // GroupDataProvider was able to allocate rescources for an iterator |
| 575 | while (iterator->Next(groupInfo)) |
| 576 | { |
| 577 | err = mTransports.MulticastGroupJoinLeave( |
Boris Zbarsky | 393b319 | 2022-06-01 10:09:08 -0400 | [diff] [blame] | 578 | Transport::PeerAddress::Multicast(fabric.GetFabricId(), groupInfo.group_id), true); |
mkardous-silabs | 3c31ec7 | 2022-02-24 17:37:45 -0500 | [diff] [blame] | 579 | if (err != CHIP_NO_ERROR) |
| 580 | { |
andrei-menzopol | 57471cb | 2022-04-20 23:31:17 +0300 | [diff] [blame] | 581 | ChipLogError(AppServer, "Error when trying to join Group %u of fabric index %u : %" CHIP_ERROR_FORMAT, |
mkardous-silabs | 3c31ec7 | 2022-02-24 17:37:45 -0500 | [diff] [blame] | 582 | groupInfo.group_id, fabric.GetFabricIndex(), err.Format()); |
| 583 | |
| 584 | // We assume the failure is caused by a network issue or a lack of rescources; neither of which will be solved |
| 585 | // before the next join. Exit the loop to save rescources. |
| 586 | iterator->Release(); |
| 587 | return; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | iterator->Release(); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
mkardous-silabs | 96329ae | 2024-04-03 22:17:11 -0400 | [diff] [blame] | 596 | #if CHIP_CONFIG_ENABLE_ICD_CIP |
| 597 | bool Server::ShouldCheckInMsgsBeSentAtBootFunction(FabricIndex aFabricIndex, NodeId subjectID) |
| 598 | { |
| 599 | #if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS |
| 600 | // If at least one registration has a persisted entry, do not send Check-In message. |
| 601 | // The resumption of the persisted subscription will serve the same function a check-in would have served. |
| 602 | return !app::InteractionModelEngine::GetInstance()->SubjectHasPersistedSubscription(aFabricIndex, subjectID); |
| 603 | #else |
| 604 | return true; |
| 605 | #endif // CHIP_CONFIG_PERSIST_SUBSCRIPTIONS |
| 606 | } |
| 607 | #endif // CHIP_CONFIG_ENABLE_ICD_CIP |
| 608 | |
Boris Zbarsky | 336f1c5 | 2023-02-17 09:39:19 -0500 | [diff] [blame] | 609 | void Server::GenerateShutDownEvent() |
Vivien Nicolas | 8297617 | 2022-02-16 11:47:52 +0100 | [diff] [blame] | 610 | { |
Damian Królik | 1c74a21 | 2022-03-10 10:45:52 +0100 | [diff] [blame] | 611 | PlatformMgr().ScheduleWork([](intptr_t) { PlatformMgr().HandleServerShuttingDown(); }); |
Vivien Nicolas | 8297617 | 2022-02-16 11:47:52 +0100 | [diff] [blame] | 612 | } |
| 613 | |
Kamil Kasperczyk | 843643c | 2022-02-23 17:38:32 +0100 | [diff] [blame] | 614 | void Server::ScheduleFactoryReset() |
| 615 | { |
Damian Królik | 1c74a21 | 2022-03-10 10:45:52 +0100 | [diff] [blame] | 616 | PlatformMgr().ScheduleWork([](intptr_t) { |
| 617 | // Delete all fabrics and emit Leave event. |
| 618 | GetInstance().GetFabricTable().DeleteAllFabrics(); |
| 619 | PlatformMgr().HandleServerShuttingDown(); |
| 620 | ConfigurationMgr().InitiateFactoryReset(); |
| 621 | }); |
Kamil Kasperczyk | 843643c | 2022-02-23 17:38:32 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 624 | void Server::Shutdown() |
| 625 | { |
Boris Zbarsky | afa79de | 2023-02-14 09:40:13 -0500 | [diff] [blame] | 626 | assertChipStackLockedByCurrentThread(); |
Damian Królik | 5a845c6 | 2023-02-02 10:51:40 +0100 | [diff] [blame] | 627 | PlatformMgr().RemoveEventHandler(OnPlatformEventWrapper, 0); |
Jerry Johns | c2a869a | 2022-06-09 21:46:47 -0700 | [diff] [blame] | 628 | mCASEServer.Shutdown(); |
Douglas Rocha Ferraz | cbc57d6 | 2022-06-02 09:57:15 -0400 | [diff] [blame] | 629 | mCASESessionManager.Shutdown(); |
mkardous-silabs | 6778d3e | 2023-11-28 17:39:41 -0500 | [diff] [blame] | 630 | #if CHIP_CONFIG_ENABLE_ICD_SERVER |
| 631 | app::DnssdServer::Instance().SetICDManager(nullptr); |
| 632 | #endif // CHIP_CONFIG_ENABLE_ICD_SERVER |
Boris Zbarsky | 999aeae | 2022-03-07 08:44:13 -0500 | [diff] [blame] | 633 | app::DnssdServer::Instance().SetCommissioningModeProvider(nullptr); |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 634 | Dnssd::ServiceAdvertiser::Instance().Shutdown(); |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 635 | |
| 636 | #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT |
| 637 | if (mUdcTransportMgr != nullptr) |
| 638 | { |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 639 | Platform::Delete(mUdcTransportMgr); |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 640 | mUdcTransportMgr = nullptr; |
| 641 | } |
| 642 | if (gUDCClient != nullptr) |
| 643 | { |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 644 | Platform::Delete(gUDCClient); |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 645 | gUDCClient = nullptr; |
| 646 | } |
| 647 | #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY |
| 648 | |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 649 | Dnssd::Resolver::Instance().Shutdown(); |
| 650 | app::InteractionModelEngine::GetInstance()->Shutdown(); |
mkardous-silabs | 96329ae | 2024-04-03 22:17:11 -0400 | [diff] [blame] | 651 | #if CHIP_CONFIG_ENABLE_ICD_SERVER |
| 652 | app::InteractionModelEngine::GetInstance()->SetICDManager(nullptr); |
| 653 | #endif // CHIP_CONFIG_ENABLE_ICD_SERVER |
Boris Zbarsky | 674dca8 | 2024-06-21 15:01:11 -0400 | [diff] [blame] | 654 | // Shut down any remaining sessions (and hence exchanges) before we do any |
| 655 | // futher teardown. CASE handshakes have been shut down already via |
| 656 | // shutting down mCASESessionManager and mCASEServer above; shutting |
| 657 | // down mCommissioningWindowManager will shut down any PASE handshakes we |
| 658 | // have going on. |
| 659 | mSessions.ExpireAllSecureSessions(); |
Boris Zbarsky | 39e5ed2 | 2022-07-14 14:34:50 -0400 | [diff] [blame] | 660 | mCommissioningWindowManager.Shutdown(); |
Douglas Rocha Ferraz | cbc57d6 | 2022-06-02 09:57:15 -0400 | [diff] [blame] | 661 | mMessageCounterManager.Shutdown(); |
Michael Spang | 6387049 | 2022-06-28 08:41:08 -0400 | [diff] [blame] | 662 | mExchangeMgr.Shutdown(); |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 663 | mSessions.Shutdown(); |
| 664 | mTransports.Close(); |
Douglas Rocha Ferraz | cbc57d6 | 2022-06-02 09:57:15 -0400 | [diff] [blame] | 665 | mAccessControl.Finish(); |
Michael Sandstedt | 5a0e3ce | 2022-11-03 12:00:25 -0500 | [diff] [blame] | 666 | Access::ResetAccessControlToDefault(); |
Douglas Rocha Ferraz | cbc57d6 | 2022-06-02 09:57:15 -0400 | [diff] [blame] | 667 | Credentials::SetGroupDataProvider(nullptr); |
Junior Martinez | ac060dc | 2023-07-10 17:31:05 -0400 | [diff] [blame] | 668 | #if CHIP_CONFIG_ENABLE_ICD_SERVER |
mkardous-silabs | 845ce9b | 2024-04-09 20:13:38 -0400 | [diff] [blame] | 669 | // Remove Test Event Trigger Handler |
fesseha-eve | 522668f | 2024-07-16 14:33:24 +0200 | [diff] [blame] | 670 | if (mTestEventTriggerDelegate != nullptr) |
| 671 | { |
| 672 | mTestEventTriggerDelegate->RemoveHandler(&mICDManager); |
| 673 | } |
Junior Martinez | ac060dc | 2023-07-10 17:31:05 -0400 | [diff] [blame] | 674 | mICDManager.Shutdown(); |
Junior Martinez | fb5558d | 2023-06-15 09:29:02 -0400 | [diff] [blame] | 675 | #endif // CHIP_CONFIG_ENABLE_ICD_SERVER |
Andrei Litvin | dad97c8 | 2024-12-05 11:30:14 -0500 | [diff] [blame] | 676 | |
Tennessee Carmel-Veilleux | def1881 | 2022-04-04 13:12:25 -0400 | [diff] [blame] | 677 | // TODO(16969): Remove chip::Platform::MemoryInit() call from Server class, it belongs to outer code |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 678 | chip::Platform::MemoryShutdown(); |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 679 | } |
Andrei Litvin | 7093fde | 2021-02-26 16:13:23 -0500 | [diff] [blame] | 680 | |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 681 | #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT |
| 682 | // NOTE: UDC client is located in Server.cpp because it really only makes sense |
| 683 | // to send UDC from a Matter device. The UDC message payload needs to include the device's |
| 684 | // randomly generated service name. |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 685 | CHIP_ERROR Server::SendUserDirectedCommissioningRequest(Transport::PeerAddress commissioner, |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 686 | Protocols::UserDirectedCommissioning::IdentificationDeclaration & id) |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 687 | { |
Philip Gregor | a2ded0f | 2024-05-07 12:41:05 -0700 | [diff] [blame] | 688 | ChipLogDetail(AppServer, "Server::SendUserDirectedCommissioningRequest()"); |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 689 | |
| 690 | CHIP_ERROR err; |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 691 | |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 692 | // only populate fields left blank by the client |
| 693 | if (strlen(id.GetInstanceName()) == 0) |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 694 | { |
Philip Gregor | 320f83e | 2024-05-17 12:43:00 -0700 | [diff] [blame] | 695 | ChipLogDetail(AppServer, "Server::SendUserDirectedCommissioningRequest() Instance Name not known"); |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 696 | char nameBuffer[Dnssd::Commission::kInstanceNameMaxLength + 1]; |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 697 | err = app::DnssdServer::Instance().GetCommissionableInstanceName(nameBuffer, sizeof(nameBuffer)); |
| 698 | if (err != CHIP_NO_ERROR) |
| 699 | { |
Philip Gregor | 320f83e | 2024-05-17 12:43:00 -0700 | [diff] [blame] | 700 | ChipLogError( |
| 701 | AppServer, |
| 702 | "Server::SendUserDirectedCommissioningRequest() Failed to get mdns instance name error: %" CHIP_ERROR_FORMAT, |
| 703 | err.Format()); |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 704 | return err; |
| 705 | } |
| 706 | id.SetInstanceName(nameBuffer); |
Philip Gregor | 320f83e | 2024-05-17 12:43:00 -0700 | [diff] [blame] | 707 | ChipLogDetail(AppServer, "Server::SendUserDirectedCommissioningRequest() Instance Name set to %s", nameBuffer); |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 708 | } |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 709 | |
| 710 | if (id.GetVendorId() == 0) |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 711 | { |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 712 | uint16_t vendorId = 0; |
| 713 | if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetVendorId(vendorId) != CHIP_NO_ERROR) |
| 714 | { |
Philip Gregor | 320f83e | 2024-05-17 12:43:00 -0700 | [diff] [blame] | 715 | ChipLogDetail(AppServer, "Server::SendUserDirectedCommissioningRequest() Vendor ID not known"); |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 716 | } |
| 717 | else |
| 718 | { |
| 719 | id.SetVendorId(vendorId); |
| 720 | } |
chrisdecenzo | 31c6249 | 2021-08-10 11:47:15 -0700 | [diff] [blame] | 721 | } |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 722 | |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 723 | if (id.GetProductId() == 0) |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 724 | { |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 725 | uint16_t productId = 0; |
| 726 | if (DeviceLayer::GetDeviceInstanceInfoProvider()->GetProductId(productId) != CHIP_NO_ERROR) |
| 727 | { |
Philip Gregor | 320f83e | 2024-05-17 12:43:00 -0700 | [diff] [blame] | 728 | ChipLogDetail(AppServer, "Server::SendUserDirectedCommissioningRequest() Product ID not known"); |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 729 | } |
| 730 | else |
| 731 | { |
| 732 | id.SetProductId(productId); |
| 733 | } |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 734 | } |
| 735 | |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 736 | if (strlen(id.GetDeviceName()) == 0) |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 737 | { |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 738 | char deviceName[Dnssd::kKeyDeviceNameMaxLength + 1] = {}; |
| 739 | if (!DeviceLayer::ConfigurationMgr().IsCommissionableDeviceNameEnabled() || |
| 740 | DeviceLayer::ConfigurationMgr().GetCommissionableDeviceName(deviceName, sizeof(deviceName)) != CHIP_NO_ERROR) |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 741 | { |
Philip Gregor | 320f83e | 2024-05-17 12:43:00 -0700 | [diff] [blame] | 742 | ChipLogDetail(AppServer, "Server::SendUserDirectedCommissioningRequest() Device Name not known"); |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 743 | } |
| 744 | else |
| 745 | { |
| 746 | id.SetDeviceName(deviceName); |
| 747 | } |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | #if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID) |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 751 | if (id.GetRotatingIdLength() == 0) |
| 752 | { |
chrisdecenzo | 31fa02f | 2024-03-26 09:38:19 -0700 | [diff] [blame] | 753 | AdditionalDataPayloadGeneratorParams additionalDataPayloadParams; |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 754 | uint8_t rotatingDeviceIdUniqueId[DeviceLayer::ConfigurationManager::kRotatingDeviceIDUniqueIDLength]; |
chrisdecenzo | 31fa02f | 2024-03-26 09:38:19 -0700 | [diff] [blame] | 755 | MutableByteSpan rotatingDeviceIdUniqueIdSpan(rotatingDeviceIdUniqueId); |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 756 | |
chrisdecenzo | 31fa02f | 2024-03-26 09:38:19 -0700 | [diff] [blame] | 757 | ReturnErrorOnFailure( |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 758 | DeviceLayer::GetDeviceInstanceInfoProvider()->GetRotatingDeviceIdUniqueId(rotatingDeviceIdUniqueIdSpan)); |
chrisdecenzo | 31fa02f | 2024-03-26 09:38:19 -0700 | [diff] [blame] | 759 | ReturnErrorOnFailure( |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 760 | DeviceLayer::ConfigurationMgr().GetLifetimeCounter(additionalDataPayloadParams.rotatingDeviceIdLifetimeCounter)); |
chrisdecenzo | 31fa02f | 2024-03-26 09:38:19 -0700 | [diff] [blame] | 761 | additionalDataPayloadParams.rotatingDeviceIdUniqueId = rotatingDeviceIdUniqueIdSpan; |
| 762 | |
| 763 | uint8_t rotatingDeviceIdInternalBuffer[RotatingDeviceId::kMaxLength]; |
| 764 | MutableByteSpan rotatingDeviceIdBufferTemp(rotatingDeviceIdInternalBuffer); |
| 765 | ReturnErrorOnFailure(AdditionalDataPayloadGenerator().generateRotatingDeviceIdAsBinary(additionalDataPayloadParams, |
| 766 | rotatingDeviceIdBufferTemp)); |
| 767 | |
| 768 | id.SetRotatingId(rotatingDeviceIdInternalBuffer, RotatingDeviceId::kMaxLength); |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 769 | } |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 770 | #endif |
| 771 | |
chrisdecenzo | 7dbacc6 | 2024-01-18 11:57:05 -0800 | [diff] [blame] | 772 | if (id.GetCdPort() == 0) |
| 773 | { |
| 774 | id.SetCdPort(mCdcListenPort); |
| 775 | } |
chrisdecenzo | c8bea78 | 2023-11-29 11:38:48 -0800 | [diff] [blame] | 776 | |
| 777 | err = gUDCClient->SendUDCMessage(&mTransports, id, commissioner); |
| 778 | |
chrisdecenzo | 31c6249 | 2021-08-10 11:47:15 -0700 | [diff] [blame] | 779 | if (err == CHIP_NO_ERROR) |
| 780 | { |
Philip Gregor | 320f83e | 2024-05-17 12:43:00 -0700 | [diff] [blame] | 781 | ChipLogDetail(AppServer, "Server::SendUserDirectedCommissioningRequest() Send UDC request success"); |
chrisdecenzo | 31c6249 | 2021-08-10 11:47:15 -0700 | [diff] [blame] | 782 | } |
| 783 | else |
| 784 | { |
Philip Gregor | 320f83e | 2024-05-17 12:43:00 -0700 | [diff] [blame] | 785 | ChipLogError(AppServer, "Server::SendUserDirectedCommissioningRequest() Send UDC request failed, err: %" CHIP_ERROR_FORMAT, |
| 786 | err.Format()); |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 787 | } |
| 788 | return err; |
| 789 | } |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 790 | #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT |
| 791 | |
Jeff Tung | 37919a1 | 2023-01-20 17:58:51 -0800 | [diff] [blame] | 792 | #if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS |
| 793 | void Server::ResumeSubscriptions() |
| 794 | { |
Yufeng Wang | c2739ca | 2024-12-05 14:14:38 -0800 | [diff] [blame] | 795 | CHIP_ERROR err = app::InteractionModelEngine::GetInstance()->ResumeSubscriptions(); |
Jeff Tung | 37919a1 | 2023-01-20 17:58:51 -0800 | [diff] [blame] | 796 | if (err != CHIP_NO_ERROR) |
| 797 | { |
| 798 | ChipLogError(AppServer, "Error when trying to resume subscriptions : %" CHIP_ERROR_FORMAT, err.Format()); |
| 799 | } |
| 800 | } |
| 801 | #endif |
| 802 | |
Boris Zbarsky | db47b1b | 2023-05-15 21:44:56 -0400 | [diff] [blame] | 803 | Credentials::IgnoreCertificateValidityPeriodPolicy Server::sDefaultCertValidityPolicy; |
| 804 | |
Damian Królik | b07872c | 2023-02-17 13:56:47 +0100 | [diff] [blame] | 805 | KvsPersistentStorageDelegate CommonCaseDeviceServerInitParams::sKvsPersistenStorageDelegate; |
| 806 | PersistentStorageOperationalKeystore CommonCaseDeviceServerInitParams::sPersistentStorageOperationalKeystore; |
| 807 | Credentials::PersistentStorageOpCertStore CommonCaseDeviceServerInitParams::sPersistentStorageOpCertStore; |
| 808 | Credentials::GroupDataProviderImpl CommonCaseDeviceServerInitParams::sGroupDataProvider; |
lpbeliveau-silabs | a70d5b4 | 2023-08-04 17:12:49 -0400 | [diff] [blame] | 809 | app::DefaultTimerDelegate CommonCaseDeviceServerInitParams::sTimerDelegate; |
| 810 | app::reporting::ReportSchedulerImpl |
| 811 | CommonCaseDeviceServerInitParams::sReportScheduler(&CommonCaseDeviceServerInitParams::sTimerDelegate); |
Damian Królik | b07872c | 2023-02-17 13:56:47 +0100 | [diff] [blame] | 812 | #if CHIP_CONFIG_ENABLE_SESSION_RESUMPTION |
| 813 | SimpleSessionResumptionStorage CommonCaseDeviceServerInitParams::sSessionResumptionStorage; |
| 814 | #endif |
| 815 | #if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS |
| 816 | app::SimpleSubscriptionResumptionStorage CommonCaseDeviceServerInitParams::sSubscriptionResumptionStorage; |
| 817 | #endif |
| 818 | app::DefaultAclStorage CommonCaseDeviceServerInitParams::sAclStorage; |
| 819 | Crypto::DefaultSessionKeystore CommonCaseDeviceServerInitParams::sSessionKeystore; |
mkardous-silabs | 5eba394 | 2024-07-26 19:42:45 -0400 | [diff] [blame] | 820 | #if CHIP_CONFIG_ENABLE_ICD_CIP |
| 821 | app::DefaultICDCheckInBackOffStrategy CommonCaseDeviceServerInitParams::sDefaultICDCheckInBackOffStrategy; |
| 822 | #endif |
Damian Królik | b07872c | 2023-02-17 13:56:47 +0100 | [diff] [blame] | 823 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 824 | } // namespace chip |