blob: a898bccdb0d196b0e7490be0cf58d7b45bc3c8f2 [file] [log] [blame]
Song Guo750e2e62020-09-15 00:45:51 +08001/*
2 *
Jiacheng Guo735dbb02021-09-11 04:24:07 +08003 * Copyright (c) 2021 Project CHIP Authors
Song Guo750e2e62020-09-15 00:45:51 +08004 *
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 Kasperczykd05149c2021-01-05 16:05:27 +010018#include <app/server/Server.h>
Song Guo750e2e62020-09-15 00:45:51 +080019
Andy Salisbury62e37a22022-02-04 11:00:00 -050020#include <access/examples/ExampleAccessControlDelegate.h>
21
yunhanw-google6f89b9b2021-12-07 14:26:21 -080022#include <app/EventManagement.h>
Song Guoa77a6622021-01-29 13:13:02 +080023#include <app/InteractionModelEngine.h>
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +020024#include <app/server/Dnssd.h>
Yufeng Wanga47019b2021-03-09 11:59:12 -080025#include <app/server/EchoHandler.h>
Vivien Nicolas92e36832021-05-01 00:38:25 +020026#include <app/util/DataModelHandler.h>
Song Guo750e2e62020-09-15 00:45:51 +080027
Jiacheng Guo69d2d592020-09-18 12:19:38 +080028#include <ble/BLEEndPoint.h>
Song Guo750e2e62020-09-15 00:45:51 +080029#include <inet/IPAddress.h>
30#include <inet/InetError.h>
Zang MingJie53dd5832021-09-03 03:05:16 +080031#include <lib/core/CHIPPersistentStorageDelegate.h>
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +020032#include <lib/dnssd/Advertiser.h>
33#include <lib/dnssd/ServiceNaming.h>
Zang MingJie53dd5832021-09-03 03:05:16 +080034#include <lib/support/CodeUtils.h>
35#include <lib/support/ErrorStr.h>
36#include <lib/support/logging/CHIPLogging.h>
Song Guoa77a6622021-01-29 13:13:02 +080037#include <messaging/ExchangeMgr.h>
Song Guo750e2e62020-09-15 00:45:51 +080038#include <platform/CHIPDeviceLayer.h>
Pankaj Garg9a791a82021-02-24 07:48:03 -080039#include <platform/KeyValueStoreManager.h>
Pankaj Garg8ced1a12021-05-14 06:55:59 -070040#include <protocols/secure_channel/CASEServer.h>
Zang MingJiec64d9d12021-05-13 00:09:08 +080041#include <protocols/secure_channel/MessageCounterManager.h>
Vivien Nicolas0110e482020-12-02 19:45:41 +010042#include <setup_payload/SetupPayload.h>
Song Guo750e2e62020-09-15 00:45:51 +080043#include <sys/param.h>
44#include <system/SystemPacketBuffer.h>
Pankaj Garged287532021-02-09 14:38:31 -080045#include <system/TLVPacketBufferBackingStore.h>
Zang MingJieeca9bff2021-09-23 03:19:51 +080046#include <transport/SessionManager.h>
Song Guo750e2e62020-09-15 00:45:51 +080047
Pankaj Garg5d4d0732021-09-30 14:15:20 -070048using chip::kMinValidFabricIndex;
Jiacheng Guo735dbb02021-09-11 04:24:07 +080049using chip::RendezvousInformationFlag;
50using chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr;
51using chip::Inet::IPAddressType;
52using chip::Transport::BleListenParameters;
Jiacheng Guo735dbb02021-09-11 04:24:07 +080053using chip::Transport::PeerAddress;
54using chip::Transport::UdpListenParameters;
Song Guo750e2e62020-09-15 00:45:51 +080055
Song Guo750e2e62020-09-15 00:45:51 +080056namespace {
Jiacheng Guo69d2d592020-09-18 12:19:38 +080057
Damian Królike861a512021-02-10 20:01:07 +010058constexpr bool isRendezvousBypassed()
Vivien Nicolas0110e482020-12-02 19:45:41 +010059{
Damian Królike861a512021-02-10 20:01:07 +010060#if defined(CHIP_BYPASS_RENDEZVOUS) && CHIP_BYPASS_RENDEZVOUS
61 return true;
62#elif defined(CONFIG_RENDEZVOUS_MODE)
cecille6cbb5222021-04-09 14:04:30 -040063 return static_cast<RendezvousInformationFlag>(CONFIG_RENDEZVOUS_MODE) == RendezvousInformationFlag::kNone;
Damian Królike861a512021-02-10 20:01:07 +010064#else
65 return false;
Vivien Nicolas0110e482020-12-02 19:45:41 +010066#endif
Damian Królike861a512021-02-10 20:01:07 +010067}
Vivien Nicolas0110e482020-12-02 19:45:41 +010068
Vivien Nicolas82976172022-02-16 11:47:52 +010069void StopEventLoop(intptr_t arg)
70{
Boris Zbarsky980e1772022-02-25 03:38:19 -050071 CHIP_ERROR err = chip::DeviceLayer::PlatformMgr().StopEventLoopTask();
72 if (err != CHIP_NO_ERROR)
73 {
74 ChipLogError(AppServer, "Stopping event loop: %" CHIP_ERROR_FORMAT, err.Format());
75 }
Vivien Nicolas82976172022-02-16 11:47:52 +010076}
77
78void DispatchShutDownEvent(intptr_t arg)
79{
80 // The ShutDown event SHOULD be emitted on a best-effort basis by a Node prior to any orderly shutdown sequence.
81 chip::DeviceLayer::PlatformManagerDelegate * platformManagerDelegate = chip::DeviceLayer::PlatformMgr().GetDelegate();
82 if (platformManagerDelegate != nullptr)
83 {
84 platformManagerDelegate->OnShutDown();
85 }
86}
87
Song Guo750e2e62020-09-15 00:45:51 +080088} // namespace
89
Jiacheng Guo735dbb02021-09-11 04:24:07 +080090namespace chip {
91
92Server Server::sServer;
93
yunhanw-google6f89b9b2021-12-07 14:26:21 -080094#if CHIP_CONFIG_ENABLE_SERVER_IM_EVENT
95#define CHIP_NUM_EVENT_LOGGING_BUFFERS 3
96static uint8_t sInfoEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE];
97static uint8_t sDebugEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE];
98static uint8_t sCritEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE];
yunhanw-google9a74bae2021-12-15 13:33:05 -080099static ::chip::PersistedCounter sGlobalEventIdCounter;
yunhanw-google6f89b9b2021-12-07 14:26:21 -0800100static ::chip::app::CircularEventBuffer sLoggingBuffer[CHIP_NUM_EVENT_LOGGING_BUFFERS];
101#endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT
102
Jiacheng Guo7ead62d2021-12-14 23:09:38 +0800103Server::Server() :
104 mCASESessionManager(CASESessionManagerConfig {
105 .sessionInitParams = {
106 .sessionManager = &mSessions,
107 .exchangeMgr = &mExchangeMgr,
108 .idAllocator = &mSessionIDAllocator,
109 .fabricTable = &mFabrics,
110 .clientPool = &mCASEClientPool,
Jiacheng Guo7ead62d2021-12-14 23:09:38 +0800111 },
112 .dnsCache = nullptr,
113 .devicePool = &mDevicePool,
114 .dnsResolver = nullptr,
Jean-Francois Penvend136add2022-01-25 12:24:37 -0500115 }), mCommissioningWindowManager(this), mGroupsProvider(mDeviceStorage),
Andy Salisbury62e37a22022-02-04 11:00:00 -0500116 mAttributePersister(mDeviceStorage), mAccessControl(Access::Examples::GetAccessControlDelegate(&mDeviceStorage))
Jiacheng Guo7ead62d2021-12-14 23:09:38 +0800117{}
118
Vivien Nicolasfdb81142022-02-22 17:59:26 +0100119CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint16_t unsecureServicePort,
120 Inet::InterfaceId interfaceId)
Pankaj Gargeac670e2021-02-02 18:36:52 -0800121{
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800122 mSecuredServicePort = secureServicePort;
123 mUnsecuredServicePort = unsecureServicePort;
Vivien Nicolasfdb81142022-02-22 17:59:26 +0100124 mInterfaceId = interfaceId;
Pankaj Garged287532021-02-09 14:38:31 -0800125
Song Guo750e2e62020-09-15 00:45:51 +0800126 CHIP_ERROR err = CHIP_NO_ERROR;
Song Guo750e2e62020-09-15 00:45:51 +0800127
Pankaj Garg9a791a82021-02-24 07:48:03 -0800128 chip::Platform::MemoryInit();
129
Pankaj Garg2815a102021-09-15 17:22:34 -0700130 mCommissioningWindowManager.SetAppDelegate(delegate);
131 mCommissioningWindowManager.SetSessionIDAllocator(&mSessionIDAllocator);
Boris Zbarskyec713462022-01-06 15:53:22 -0500132
133 // Set up attribute persistence before we try to bring up the data model
134 // handler.
135 SetAttributePersistenceProvider(&mAttributePersister);
136
jmartinez-silabs1a89a842022-01-14 15:09:07 -0500137 InitDataModelHandler(&mExchangeMgr);
138
Jean-Francois Penvend136add2022-01-25 12:24:37 -0500139 err = mFabrics.Init(&mDeviceStorage);
shana-apple3907f382021-05-05 08:50:42 +0200140 SuccessOrExit(err);
141
Jerry Johns75372d82022-03-01 06:32:40 -0800142 app::DnssdServer::Instance().SetFabricTable(&mFabrics);
Jerry Johns75372d82022-03-01 06:32:40 -0800143
Jean-Francois Penvend136add2022-01-25 12:24:37 -0500144 // Group data provider must be initialized after mDeviceStorage
Ricardo Casallasa5513c22021-12-02 11:22:19 -0500145 err = mGroupsProvider.Init();
146 SuccessOrExit(err);
147 SetGroupDataProvider(&mGroupsProvider);
148
Andy Salisbury62e37a22022-02-04 11:00:00 -0500149 // Access control must be initialized after mDeviceStorage.
150 err = mAccessControl.Init();
151 SuccessOrExit(err);
152 Access::SetAccessControl(mAccessControl);
153
Song Guofba2fdc2020-11-21 04:08:24 +0800154 // Init transport before operations with secure session mgr.
Kevin Schoedel8e5eb8a2021-12-09 11:07:45 -0500155 err = mTransports.Init(UdpListenParameters(DeviceLayer::UDPEndPointManager())
156 .SetAddressType(IPAddressType::kIPv6)
157 .SetListenPort(mSecuredServicePort)
Jean-Francois Penven1cbec522022-01-11 11:54:40 -0500158#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_UDP
159 .SetNativeParams(chip::DeviceLayer::ThreadStackMgrImpl().OTInstance())
160#endif // CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_UDP
Song Guo87142e02021-04-21 09:00:00 +0800161
Vivien Nicolas0110e482020-12-02 19:45:41 +0100162#if INET_CONFIG_ENABLE_IPV4
Kevin Schoedel8e5eb8a2021-12-09 11:07:45 -0500163 ,
164 UdpListenParameters(DeviceLayer::UDPEndPointManager())
165 .SetAddressType(IPAddressType::kIPv4)
166 .SetListenPort(mSecuredServicePort)
Vivien Nicolas0110e482020-12-02 19:45:41 +0100167#endif
Song Guo87142e02021-04-21 09:00:00 +0800168#if CONFIG_NETWORK_LAYER_BLE
Kevin Schoedel8e5eb8a2021-12-09 11:07:45 -0500169 ,
170 BleListenParameters(DeviceLayer::ConnectivityMgr().GetBleLayer())
Song Guo87142e02021-04-21 09:00:00 +0800171#endif
172 );
173
mkardous-silabsde955672022-01-25 16:05:37 -0500174 err = mListener.Init(&mTransports);
175 SuccessOrExit(err);
176 mGroupsProvider.SetListener(&mListener);
177
Sweetyc6db3372021-09-30 06:55:02 +0530178#if CONFIG_NETWORK_LAYER_BLE
179 mBleLayer = DeviceLayer::ConnectivityMgr().GetBleLayer();
180#endif
Song Guofba2fdc2020-11-21 04:08:24 +0800181 SuccessOrExit(err);
182
Zang MingJie06be4012022-03-05 02:28:33 +0800183 err = mSessions.Init(&DeviceLayer::SystemLayer(), &mTransports, &mMessageCounterManager, &mDeviceStorage, &GetFabricTable());
Song Guo750e2e62020-09-15 00:45:51 +0800184 SuccessOrExit(err);
185
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800186 err = mExchangeMgr.Init(&mSessions);
Song Guoa77a6622021-01-29 13:13:02 +0800187 SuccessOrExit(err);
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800188 err = mMessageCounterManager.Init(&mExchangeMgr);
Zang MingJiec64d9d12021-05-13 00:09:08 +0800189 SuccessOrExit(err);
Pankaj Gargfad6d052021-01-20 12:26:34 -0800190
Jerry Johns12db26d2022-01-26 10:06:04 -0800191 err = chip::app::InteractionModelEngine::GetInstance()->Init(&mExchangeMgr);
Yufeng Wanga47019b2021-03-09 11:59:12 -0800192 SuccessOrExit(err);
Yufeng Wanga47019b2021-03-09 11:59:12 -0800193
yunhanw-google6f89b9b2021-12-07 14:26:21 -0800194#if CHIP_CONFIG_ENABLE_SERVER_IM_EVENT
195 // Initialize event logging subsystem
196 {
yunhanw-google9a74bae2021-12-15 13:33:05 -0800197 ::chip::Platform::PersistedStorage::Key globalEventIdCounterStorageKey =
198 CHIP_DEVICE_CONFIG_PERSISTED_STORAGE_GLOBAL_EIDC_KEY;
yunhanw-google6f89b9b2021-12-07 14:26:21 -0800199
200 ::chip::app::LogStorageResources logStorageResources[] = {
yunhanw-google9a74bae2021-12-15 13:33:05 -0800201 { &sDebugEventBuffer[0], sizeof(sDebugEventBuffer), ::chip::app::PriorityLevel::Debug },
202 { &sInfoEventBuffer[0], sizeof(sInfoEventBuffer), ::chip::app::PriorityLevel::Info },
203 { &sCritEventBuffer[0], sizeof(sCritEventBuffer), ::chip::app::PriorityLevel::Critical }
yunhanw-google6f89b9b2021-12-07 14:26:21 -0800204 };
205
206 chip::app::EventManagement::GetInstance().Init(&mExchangeMgr, CHIP_NUM_EVENT_LOGGING_BUFFERS, &sLoggingBuffer[0],
yunhanw-google9a74bae2021-12-15 13:33:05 -0800207 &logStorageResources[0], &globalEventIdCounterStorageKey,
208 CHIP_DEVICE_CONFIG_EVENT_ID_COUNTER_EPOCH, &sGlobalEventIdCounter);
yunhanw-google6f89b9b2021-12-07 14:26:21 -0800209 }
210#endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT
211
Yufeng Wanga47019b2021-03-09 11:59:12 -0800212#if defined(CHIP_APP_USE_ECHO)
213 err = InitEchoHandler(&gExchangeMgr);
214 SuccessOrExit(err);
215#endif
216
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800217 if (isRendezvousBypassed())
Vivien Nicolas0110e482020-12-02 19:45:41 +0100218 {
Damian Królike861a512021-02-10 20:01:07 +0100219 ChipLogProgress(AppServer, "Rendezvous and secure pairing skipped");
Pankaj Garg6a362562021-08-11 14:48:53 -0700220 SuccessOrExit(err = AddTestCommissioning());
Damian Królike861a512021-02-10 20:01:07 +0100221 }
Hui.Li-TCL360252c2022-01-05 00:04:21 +0800222 else if (GetFabricTable().FabricCount() != 0)
Pankaj Gargeac670e2021-02-02 18:36:52 -0800223 {
Pankaj Gargccdffbc2021-09-24 09:10:22 -0700224 // The device is already commissioned, proactively disable BLE advertisement.
225 ChipLogProgress(AppServer, "Fabric already commissioned. Disabling BLE advertisement");
Hui.Li-TCL360252c2022-01-05 00:04:21 +0800226#if CONFIG_NETWORK_LAYER_BLE
Pankaj Gargeac670e2021-02-02 18:36:52 -0800227 chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(false);
Hui.Li-TCL360252c2022-01-05 00:04:21 +0800228#endif
Pankaj Gargeac670e2021-02-02 18:36:52 -0800229 }
Vivien Nicolas0110e482020-12-02 19:45:41 +0100230 else
Jiacheng Guodf2497d2020-09-30 21:16:57 +0800231 {
Kamil Kasperczyk6ad57112021-02-11 16:44:36 +0100232#if CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART
Pankaj Garg2815a102021-09-15 17:22:34 -0700233 GetFabricTable().DeleteAllFabrics();
234 SuccessOrExit(err = mCommissioningWindowManager.OpenBasicCommissioningWindow());
Kamil Kasperczyk6ad57112021-02-11 16:44:36 +0100235#endif
Jiacheng Guodf2497d2020-09-30 21:16:57 +0800236 }
Jiacheng Guodf2497d2020-09-30 21:16:57 +0800237
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200238 app::DnssdServer::Instance().SetSecuredPort(mSecuredServicePort);
239 app::DnssdServer::Instance().SetUnsecuredPort(mUnsecuredServicePort);
Vivien Nicolasfdb81142022-02-22 17:59:26 +0100240 app::DnssdServer::Instance().SetInterfaceId(mInterfaceId);
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800241
242 // TODO @bzbarsky-apple @cecille Move to examples
243 // ESP32 and Mbed OS examples have a custom logic for enabling DNS-SD
Andrei Litvinb7924632022-03-03 15:29:54 -0500244#if !CHIP_DEVICE_LAYER_TARGET_ESP32 && !CHIP_DEVICE_LAYER_TARGET_MBED && \
pankorebab75e62021-10-28 21:48:46 +0800245 (!CHIP_DEVICE_LAYER_TARGET_AMEBA || !CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE)
chrisdecenzo515ea652021-08-18 11:27:09 -0700246 // StartServer only enables commissioning mode if device has not been commissioned
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200247 app::DnssdServer::Instance().StartServer();
Paul Zander3510f142021-04-15 16:29:40 +0200248#endif
Kamil Kasperczyk44014022021-03-12 21:18:45 +0100249
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800250 err = mCASEServer.ListenForSessionEstablishment(&mExchangeMgr, &mTransports, chip::DeviceLayer::ConnectivityMgr().GetBleLayer(),
Jerry Johns75372d82022-03-01 06:32:40 -0800251 &mSessions, &mFabrics);
Pankaj Garg8ced1a12021-05-14 06:55:59 -0700252 SuccessOrExit(err);
253
Andrei Litvin16bdace22022-01-07 09:32:47 -0500254 err = mCASESessionManager.Init();
Jiacheng Guo7ead62d2021-12-14 23:09:38 +0800255
mkardous-silabsde955672022-01-25 16:05:37 -0500256 // This code is necessary to restart listening to existing groups after a reboot
257 // Each manufacturer needs to validate that they can rejoin groups by placing this code at the appropriate location for them
258 //
259 // This is disabled for thread device because the same code is already present for thread devices in
260 // src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread_LwIP.cpp
mkardous-silabsde955672022-01-25 16:05:37 -0500261#if !CHIP_DEVICE_CONFIG_ENABLE_THREAD
mkardous-silabs3c31ec72022-02-24 17:37:45 -0500262 RejoinExistingMulticastGroups();
mkardous-silabsde955672022-01-25 16:05:37 -0500263#endif // !CHIP_DEVICE_CONFIG_ENABLE_THREAD
mkardous-silabs3c31ec72022-02-24 17:37:45 -0500264
Song Guo750e2e62020-09-15 00:45:51 +0800265exit:
266 if (err != CHIP_NO_ERROR)
267 {
268 ChipLogError(AppServer, "ERROR setting up transport: %s", ErrorStr(err));
269 }
270 else
271 {
272 ChipLogProgress(AppServer, "Server Listening...");
273 }
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800274 return err;
275}
276
mkardous-silabs3c31ec72022-02-24 17:37:45 -0500277void Server::RejoinExistingMulticastGroups()
278{
279 ChipLogProgress(AppServer, "Joining Multicast groups");
280 CHIP_ERROR err = CHIP_NO_ERROR;
281 for (const FabricInfo & fabric : mFabrics)
282 {
283 Credentials::GroupDataProvider::GroupInfo groupInfo;
284
285 auto * iterator = mGroupsProvider.IterateGroupInfo(fabric.GetFabricIndex());
286 if (iterator)
287 {
288 // GroupDataProvider was able to allocate rescources for an iterator
289 while (iterator->Next(groupInfo))
290 {
291 err = mTransports.MulticastGroupJoinLeave(
292 Transport::PeerAddress::Multicast(fabric.GetFabricIndex(), groupInfo.group_id), true);
293 if (err != CHIP_NO_ERROR)
294 {
295 ChipLogError(AppServer, "Error when trying to join Group %" PRIu16 " of fabric index %u : %" CHIP_ERROR_FORMAT,
296 groupInfo.group_id, fabric.GetFabricIndex(), err.Format());
297
298 // We assume the failure is caused by a network issue or a lack of rescources; neither of which will be solved
299 // before the next join. Exit the loop to save rescources.
300 iterator->Release();
301 return;
302 }
303 }
304
305 iterator->Release();
306 }
307 }
308}
309
Vivien Nicolas82976172022-02-16 11:47:52 +0100310void Server::DispatchShutDownAndStopEventLoop()
311{
312 chip::DeviceLayer::PlatformMgr().ScheduleWork(DispatchShutDownEvent);
313 chip::DeviceLayer::PlatformMgr().ScheduleWork(StopEventLoop);
314}
315
Kamil Kasperczyk843643c2022-02-23 17:38:32 +0100316void Server::ScheduleFactoryReset()
317{
318 chip::DeviceLayer::PlatformMgr().ScheduleWork(FactoryReset);
319}
320
321void Server::FactoryReset(intptr_t arg)
322{
323 // Delete all fabrics and emit Leave event.
324 GetInstance().GetFabricTable().DeleteAllFabrics();
325
326 // Emit Shutdown event, as shutdown will come after factory reset.
327 DispatchShutDownEvent(0);
328
329 // Flush all dispatched events.
330 chip::app::InteractionModelEngine::GetInstance()->GetReportingEngine().ScheduleUrgentEventDeliverySync();
331
332 chip::DeviceLayer::ConfigurationMgr().InitiateFactoryReset();
333}
334
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800335void Server::Shutdown()
336{
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200337 chip::Dnssd::ServiceAdvertiser::Instance().Shutdown();
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800338 chip::app::InteractionModelEngine::GetInstance()->Shutdown();
Boris Zbarsky980e1772022-02-25 03:38:19 -0500339 CHIP_ERROR err = mExchangeMgr.Shutdown();
340 if (err != CHIP_NO_ERROR)
341 {
342 ChipLogError(AppServer, "Exchange Mgr shutdown: %" CHIP_ERROR_FORMAT, err.Format());
343 }
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800344 mSessions.Shutdown();
345 mTransports.Close();
Kevin Schoedelbeb60962021-11-22 04:28:03 -0500346 mCommissioningWindowManager.Shutdown();
Andrei Litvineb2d0332022-02-25 16:10:03 -0500347 mCASESessionManager.Shutdown();
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800348 chip::Platform::MemoryShutdown();
Song Guo750e2e62020-09-15 00:45:51 +0800349}
Andrei Litvin7093fde2021-02-26 16:13:23 -0500350
chrisdecenzo250d5f72021-07-21 19:28:45 -0700351#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
352// NOTE: UDC client is located in Server.cpp because it really only makes sense
353// to send UDC from a Matter device. The UDC message payload needs to include the device's
354// randomly generated service name.
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800355CHIP_ERROR Server::SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress commissioner)
chrisdecenzo250d5f72021-07-21 19:28:45 -0700356{
357 ChipLogDetail(AppServer, "SendUserDirectedCommissioningRequest2");
358
359 CHIP_ERROR err;
Vivien Nicolas8e40abc2021-12-16 17:12:37 +0100360 char nameBuffer[chip::Dnssd::Commission::kInstanceNameMaxLength + 1];
Kamil Kasperczykd9e02a02021-10-12 09:19:23 +0200361 err = app::DnssdServer::Instance().GetCommissionableInstanceName(nameBuffer, sizeof(nameBuffer));
chrisdecenzo250d5f72021-07-21 19:28:45 -0700362 if (err != CHIP_NO_ERROR)
363 {
364 ChipLogError(AppServer, "Failed to get mdns instance name error: %s", ErrorStr(err));
365 return err;
366 }
367 ChipLogDetail(AppServer, "instanceName=%s", nameBuffer);
368
chrisdecenzo31c62492021-08-10 11:47:15 -0700369 chip::System::PacketBufferHandle payloadBuf = chip::MessagePacketBuffer::NewWithData(nameBuffer, strlen(nameBuffer));
370 if (payloadBuf.IsNull())
chrisdecenzo250d5f72021-07-21 19:28:45 -0700371 {
chrisdecenzo31c62492021-08-10 11:47:15 -0700372 ChipLogError(AppServer, "Unable to allocate packet buffer\n");
373 return CHIP_ERROR_NO_MEMORY;
374 }
chrisdecenzo250d5f72021-07-21 19:28:45 -0700375
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800376 err = gUDCClient.SendUDCMessage(&mTransports, std::move(payloadBuf), commissioner);
chrisdecenzo31c62492021-08-10 11:47:15 -0700377 if (err == CHIP_NO_ERROR)
378 {
379 ChipLogDetail(AppServer, "Send UDC request success");
380 }
381 else
382 {
383 ChipLogError(AppServer, "Send UDC request failed, err: %s\n", chip::ErrorStr(err));
chrisdecenzo250d5f72021-07-21 19:28:45 -0700384 }
385 return err;
386}
chrisdecenzo250d5f72021-07-21 19:28:45 -0700387#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
388
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800389CHIP_ERROR Server::AddTestCommissioning()
Damian Królikc6db4532021-03-10 14:58:58 +0100390{
Pankaj Garg2a633092021-07-23 09:15:02 -0700391 CHIP_ERROR err = CHIP_NO_ERROR;
Pankaj Garg2a633092021-07-23 09:15:02 -0700392 PASESession * testSession = nullptr;
Vivien Nicolas7768fc42021-04-20 16:56:11 +0200393 PASESessionSerializable serializedTestSession;
Zang MingJieb210aa32021-12-10 22:53:16 +0800394 SessionHolder session;
Damian Królikc6db4532021-03-10 14:58:58 +0100395
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800396 mTestPairing.ToSerializable(serializedTestSession);
Vivien Nicolas7768fc42021-04-20 16:56:11 +0200397
398 testSession = chip::Platform::New<PASESession>();
399 testSession->FromSerializable(serializedTestSession);
Zang MingJieb210aa32021-12-10 22:53:16 +0800400 SuccessOrExit(err = mSessions.NewPairing(session, Optional<PeerAddress>{ PeerAddress::Uninitialized() },
401 chip::kTestControllerNodeId, testSession, CryptoContext::SessionRole::kResponder,
402 kMinValidFabricIndex));
Damian Królikc6db4532021-03-10 14:58:58 +0100403
404exit:
Vivien Nicolas7768fc42021-04-20 16:56:11 +0200405 if (testSession)
406 {
407 testSession->Clear();
408 chip::Platform::Delete(testSession);
409 }
410
Pankaj Garg6a362562021-08-11 14:48:53 -0700411 if (err != CHIP_NO_ERROR)
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800412 {
413 mFabrics.ReleaseFabricIndex(kMinValidFabricIndex);
414 }
Damian Królikc6db4532021-03-10 14:58:58 +0100415 return err;
416}
417
Jiacheng Guo735dbb02021-09-11 04:24:07 +0800418} // namespace chip