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 | |
Andy Salisbury | 62e37a2 | 2022-02-04 11:00:00 -0500 | [diff] [blame] | 20 | #include <access/examples/ExampleAccessControlDelegate.h> |
| 21 | |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 22 | #include <app/EventManagement.h> |
Song Guo | a77a662 | 2021-01-29 13:13:02 +0800 | [diff] [blame] | 23 | #include <app/InteractionModelEngine.h> |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 24 | #include <app/server/Dnssd.h> |
Yufeng Wang | a47019b | 2021-03-09 11:59:12 -0800 | [diff] [blame] | 25 | #include <app/server/EchoHandler.h> |
Vivien Nicolas | 92e3683 | 2021-05-01 00:38:25 +0200 | [diff] [blame] | 26 | #include <app/util/DataModelHandler.h> |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 27 | |
Jiacheng Guo | 69d2d59 | 2020-09-18 12:19:38 +0800 | [diff] [blame] | 28 | #include <ble/BLEEndPoint.h> |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 29 | #include <inet/IPAddress.h> |
| 30 | #include <inet/InetError.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 31 | #include <lib/core/CHIPPersistentStorageDelegate.h> |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 32 | #include <lib/dnssd/Advertiser.h> |
| 33 | #include <lib/dnssd/ServiceNaming.h> |
Zang MingJie | 53dd583 | 2021-09-03 03:05:16 +0800 | [diff] [blame] | 34 | #include <lib/support/CodeUtils.h> |
| 35 | #include <lib/support/ErrorStr.h> |
| 36 | #include <lib/support/logging/CHIPLogging.h> |
Song Guo | a77a662 | 2021-01-29 13:13:02 +0800 | [diff] [blame] | 37 | #include <messaging/ExchangeMgr.h> |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 38 | #include <platform/CHIPDeviceLayer.h> |
Pankaj Garg | 9a791a8 | 2021-02-24 07:48:03 -0800 | [diff] [blame] | 39 | #include <platform/KeyValueStoreManager.h> |
Pankaj Garg | 8ced1a1 | 2021-05-14 06:55:59 -0700 | [diff] [blame] | 40 | #include <protocols/secure_channel/CASEServer.h> |
Zang MingJie | c64d9d1 | 2021-05-13 00:09:08 +0800 | [diff] [blame] | 41 | #include <protocols/secure_channel/MessageCounterManager.h> |
Vivien Nicolas | 0110e48 | 2020-12-02 19:45:41 +0100 | [diff] [blame] | 42 | #include <setup_payload/SetupPayload.h> |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 43 | #include <sys/param.h> |
| 44 | #include <system/SystemPacketBuffer.h> |
Pankaj Garg | ed28753 | 2021-02-09 14:38:31 -0800 | [diff] [blame] | 45 | #include <system/TLVPacketBufferBackingStore.h> |
Zang MingJie | eca9bff | 2021-09-23 03:19:51 +0800 | [diff] [blame] | 46 | #include <transport/SessionManager.h> |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 47 | |
Pankaj Garg | 5d4d073 | 2021-09-30 14:15:20 -0700 | [diff] [blame] | 48 | using chip::kMinValidFabricIndex; |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 49 | using chip::RendezvousInformationFlag; |
| 50 | using chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr; |
| 51 | using chip::Inet::IPAddressType; |
| 52 | using chip::Transport::BleListenParameters; |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 53 | using chip::Transport::PeerAddress; |
| 54 | using chip::Transport::UdpListenParameters; |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 55 | |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 56 | namespace { |
Jiacheng Guo | 69d2d59 | 2020-09-18 12:19:38 +0800 | [diff] [blame] | 57 | |
Damian Królik | e861a51 | 2021-02-10 20:01:07 +0100 | [diff] [blame] | 58 | constexpr bool isRendezvousBypassed() |
Vivien Nicolas | 0110e48 | 2020-12-02 19:45:41 +0100 | [diff] [blame] | 59 | { |
Damian Królik | e861a51 | 2021-02-10 20:01:07 +0100 | [diff] [blame] | 60 | #if defined(CHIP_BYPASS_RENDEZVOUS) && CHIP_BYPASS_RENDEZVOUS |
| 61 | return true; |
| 62 | #elif defined(CONFIG_RENDEZVOUS_MODE) |
cecille | 6cbb522 | 2021-04-09 14:04:30 -0400 | [diff] [blame] | 63 | return static_cast<RendezvousInformationFlag>(CONFIG_RENDEZVOUS_MODE) == RendezvousInformationFlag::kNone; |
Damian Królik | e861a51 | 2021-02-10 20:01:07 +0100 | [diff] [blame] | 64 | #else |
| 65 | return false; |
Vivien Nicolas | 0110e48 | 2020-12-02 19:45:41 +0100 | [diff] [blame] | 66 | #endif |
Damian Królik | e861a51 | 2021-02-10 20:01:07 +0100 | [diff] [blame] | 67 | } |
Vivien Nicolas | 0110e48 | 2020-12-02 19:45:41 +0100 | [diff] [blame] | 68 | |
Vivien Nicolas | 8297617 | 2022-02-16 11:47:52 +0100 | [diff] [blame] | 69 | void StopEventLoop(intptr_t arg) |
| 70 | { |
Boris Zbarsky | 980e177 | 2022-02-25 03:38:19 -0500 | [diff] [blame] | 71 | 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 Nicolas | 8297617 | 2022-02-16 11:47:52 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void 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 Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 88 | } // namespace |
| 89 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 90 | namespace chip { |
| 91 | |
| 92 | Server Server::sServer; |
| 93 | |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 94 | #if CHIP_CONFIG_ENABLE_SERVER_IM_EVENT |
| 95 | #define CHIP_NUM_EVENT_LOGGING_BUFFERS 3 |
| 96 | static uint8_t sInfoEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE]; |
| 97 | static uint8_t sDebugEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE]; |
| 98 | static uint8_t sCritEventBuffer[CHIP_DEVICE_CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE]; |
yunhanw-google | 9a74bae | 2021-12-15 13:33:05 -0800 | [diff] [blame] | 99 | static ::chip::PersistedCounter sGlobalEventIdCounter; |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 100 | static ::chip::app::CircularEventBuffer sLoggingBuffer[CHIP_NUM_EVENT_LOGGING_BUFFERS]; |
| 101 | #endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT |
| 102 | |
Jiacheng Guo | 7ead62d | 2021-12-14 23:09:38 +0800 | [diff] [blame] | 103 | Server::Server() : |
| 104 | mCASESessionManager(CASESessionManagerConfig { |
| 105 | .sessionInitParams = { |
| 106 | .sessionManager = &mSessions, |
| 107 | .exchangeMgr = &mExchangeMgr, |
| 108 | .idAllocator = &mSessionIDAllocator, |
| 109 | .fabricTable = &mFabrics, |
| 110 | .clientPool = &mCASEClientPool, |
Jiacheng Guo | 7ead62d | 2021-12-14 23:09:38 +0800 | [diff] [blame] | 111 | }, |
| 112 | .dnsCache = nullptr, |
| 113 | .devicePool = &mDevicePool, |
| 114 | .dnsResolver = nullptr, |
Jean-Francois Penven | d136add | 2022-01-25 12:24:37 -0500 | [diff] [blame] | 115 | }), mCommissioningWindowManager(this), mGroupsProvider(mDeviceStorage), |
Andy Salisbury | 62e37a2 | 2022-02-04 11:00:00 -0500 | [diff] [blame] | 116 | mAttributePersister(mDeviceStorage), mAccessControl(Access::Examples::GetAccessControlDelegate(&mDeviceStorage)) |
Jiacheng Guo | 7ead62d | 2021-12-14 23:09:38 +0800 | [diff] [blame] | 117 | {} |
| 118 | |
Vivien Nicolas | fdb8114 | 2022-02-22 17:59:26 +0100 | [diff] [blame] | 119 | CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint16_t unsecureServicePort, |
| 120 | Inet::InterfaceId interfaceId) |
Pankaj Garg | eac670e | 2021-02-02 18:36:52 -0800 | [diff] [blame] | 121 | { |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 122 | mSecuredServicePort = secureServicePort; |
| 123 | mUnsecuredServicePort = unsecureServicePort; |
Vivien Nicolas | fdb8114 | 2022-02-22 17:59:26 +0100 | [diff] [blame] | 124 | mInterfaceId = interfaceId; |
Pankaj Garg | ed28753 | 2021-02-09 14:38:31 -0800 | [diff] [blame] | 125 | |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 126 | CHIP_ERROR err = CHIP_NO_ERROR; |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 127 | |
Pankaj Garg | 9a791a8 | 2021-02-24 07:48:03 -0800 | [diff] [blame] | 128 | chip::Platform::MemoryInit(); |
| 129 | |
Pankaj Garg | 2815a10 | 2021-09-15 17:22:34 -0700 | [diff] [blame] | 130 | mCommissioningWindowManager.SetAppDelegate(delegate); |
| 131 | mCommissioningWindowManager.SetSessionIDAllocator(&mSessionIDAllocator); |
Boris Zbarsky | ec71346 | 2022-01-06 15:53:22 -0500 | [diff] [blame] | 132 | |
| 133 | // Set up attribute persistence before we try to bring up the data model |
| 134 | // handler. |
| 135 | SetAttributePersistenceProvider(&mAttributePersister); |
| 136 | |
jmartinez-silabs | 1a89a84 | 2022-01-14 15:09:07 -0500 | [diff] [blame] | 137 | InitDataModelHandler(&mExchangeMgr); |
| 138 | |
Jean-Francois Penven | d136add | 2022-01-25 12:24:37 -0500 | [diff] [blame] | 139 | err = mFabrics.Init(&mDeviceStorage); |
shana-apple | 3907f38 | 2021-05-05 08:50:42 +0200 | [diff] [blame] | 140 | SuccessOrExit(err); |
| 141 | |
Jerry Johns | 75372d8 | 2022-03-01 06:32:40 -0800 | [diff] [blame] | 142 | app::DnssdServer::Instance().SetFabricTable(&mFabrics); |
Jerry Johns | 75372d8 | 2022-03-01 06:32:40 -0800 | [diff] [blame] | 143 | |
Jean-Francois Penven | d136add | 2022-01-25 12:24:37 -0500 | [diff] [blame] | 144 | // Group data provider must be initialized after mDeviceStorage |
Ricardo Casallas | a5513c2 | 2021-12-02 11:22:19 -0500 | [diff] [blame] | 145 | err = mGroupsProvider.Init(); |
| 146 | SuccessOrExit(err); |
| 147 | SetGroupDataProvider(&mGroupsProvider); |
| 148 | |
Andy Salisbury | 62e37a2 | 2022-02-04 11:00:00 -0500 | [diff] [blame] | 149 | // Access control must be initialized after mDeviceStorage. |
| 150 | err = mAccessControl.Init(); |
| 151 | SuccessOrExit(err); |
| 152 | Access::SetAccessControl(mAccessControl); |
| 153 | |
Song Guo | fba2fdc | 2020-11-21 04:08:24 +0800 | [diff] [blame] | 154 | // Init transport before operations with secure session mgr. |
Kevin Schoedel | 8e5eb8a | 2021-12-09 11:07:45 -0500 | [diff] [blame] | 155 | err = mTransports.Init(UdpListenParameters(DeviceLayer::UDPEndPointManager()) |
| 156 | .SetAddressType(IPAddressType::kIPv6) |
| 157 | .SetListenPort(mSecuredServicePort) |
Jean-Francois Penven | 1cbec52 | 2022-01-11 11:54:40 -0500 | [diff] [blame] | 158 | #if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_UDP |
| 159 | .SetNativeParams(chip::DeviceLayer::ThreadStackMgrImpl().OTInstance()) |
| 160 | #endif // CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_UDP |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 161 | |
Vivien Nicolas | 0110e48 | 2020-12-02 19:45:41 +0100 | [diff] [blame] | 162 | #if INET_CONFIG_ENABLE_IPV4 |
Kevin Schoedel | 8e5eb8a | 2021-12-09 11:07:45 -0500 | [diff] [blame] | 163 | , |
| 164 | UdpListenParameters(DeviceLayer::UDPEndPointManager()) |
| 165 | .SetAddressType(IPAddressType::kIPv4) |
| 166 | .SetListenPort(mSecuredServicePort) |
Vivien Nicolas | 0110e48 | 2020-12-02 19:45:41 +0100 | [diff] [blame] | 167 | #endif |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 168 | #if CONFIG_NETWORK_LAYER_BLE |
Kevin Schoedel | 8e5eb8a | 2021-12-09 11:07:45 -0500 | [diff] [blame] | 169 | , |
| 170 | BleListenParameters(DeviceLayer::ConnectivityMgr().GetBleLayer()) |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 171 | #endif |
| 172 | ); |
| 173 | |
mkardous-silabs | de95567 | 2022-01-25 16:05:37 -0500 | [diff] [blame] | 174 | err = mListener.Init(&mTransports); |
| 175 | SuccessOrExit(err); |
| 176 | mGroupsProvider.SetListener(&mListener); |
| 177 | |
Sweety | c6db337 | 2021-09-30 06:55:02 +0530 | [diff] [blame] | 178 | #if CONFIG_NETWORK_LAYER_BLE |
| 179 | mBleLayer = DeviceLayer::ConnectivityMgr().GetBleLayer(); |
| 180 | #endif |
Song Guo | fba2fdc | 2020-11-21 04:08:24 +0800 | [diff] [blame] | 181 | SuccessOrExit(err); |
| 182 | |
Zang MingJie | 06be401 | 2022-03-05 02:28:33 +0800 | [diff] [blame] | 183 | err = mSessions.Init(&DeviceLayer::SystemLayer(), &mTransports, &mMessageCounterManager, &mDeviceStorage, &GetFabricTable()); |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 184 | SuccessOrExit(err); |
| 185 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 186 | err = mExchangeMgr.Init(&mSessions); |
Song Guo | a77a662 | 2021-01-29 13:13:02 +0800 | [diff] [blame] | 187 | SuccessOrExit(err); |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 188 | err = mMessageCounterManager.Init(&mExchangeMgr); |
Zang MingJie | c64d9d1 | 2021-05-13 00:09:08 +0800 | [diff] [blame] | 189 | SuccessOrExit(err); |
Pankaj Garg | fad6d05 | 2021-01-20 12:26:34 -0800 | [diff] [blame] | 190 | |
Jerry Johns | 12db26d | 2022-01-26 10:06:04 -0800 | [diff] [blame] | 191 | err = chip::app::InteractionModelEngine::GetInstance()->Init(&mExchangeMgr); |
Yufeng Wang | a47019b | 2021-03-09 11:59:12 -0800 | [diff] [blame] | 192 | SuccessOrExit(err); |
Yufeng Wang | a47019b | 2021-03-09 11:59:12 -0800 | [diff] [blame] | 193 | |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 194 | #if CHIP_CONFIG_ENABLE_SERVER_IM_EVENT |
| 195 | // Initialize event logging subsystem |
| 196 | { |
yunhanw-google | 9a74bae | 2021-12-15 13:33:05 -0800 | [diff] [blame] | 197 | ::chip::Platform::PersistedStorage::Key globalEventIdCounterStorageKey = |
| 198 | CHIP_DEVICE_CONFIG_PERSISTED_STORAGE_GLOBAL_EIDC_KEY; |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 199 | |
| 200 | ::chip::app::LogStorageResources logStorageResources[] = { |
yunhanw-google | 9a74bae | 2021-12-15 13:33:05 -0800 | [diff] [blame] | 201 | { &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-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | chip::app::EventManagement::GetInstance().Init(&mExchangeMgr, CHIP_NUM_EVENT_LOGGING_BUFFERS, &sLoggingBuffer[0], |
yunhanw-google | 9a74bae | 2021-12-15 13:33:05 -0800 | [diff] [blame] | 207 | &logStorageResources[0], &globalEventIdCounterStorageKey, |
| 208 | CHIP_DEVICE_CONFIG_EVENT_ID_COUNTER_EPOCH, &sGlobalEventIdCounter); |
yunhanw-google | 6f89b9b | 2021-12-07 14:26:21 -0800 | [diff] [blame] | 209 | } |
| 210 | #endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT |
| 211 | |
Yufeng Wang | a47019b | 2021-03-09 11:59:12 -0800 | [diff] [blame] | 212 | #if defined(CHIP_APP_USE_ECHO) |
| 213 | err = InitEchoHandler(&gExchangeMgr); |
| 214 | SuccessOrExit(err); |
| 215 | #endif |
| 216 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 217 | if (isRendezvousBypassed()) |
Vivien Nicolas | 0110e48 | 2020-12-02 19:45:41 +0100 | [diff] [blame] | 218 | { |
Damian Królik | e861a51 | 2021-02-10 20:01:07 +0100 | [diff] [blame] | 219 | ChipLogProgress(AppServer, "Rendezvous and secure pairing skipped"); |
Pankaj Garg | 6a36256 | 2021-08-11 14:48:53 -0700 | [diff] [blame] | 220 | SuccessOrExit(err = AddTestCommissioning()); |
Damian Królik | e861a51 | 2021-02-10 20:01:07 +0100 | [diff] [blame] | 221 | } |
Hui.Li-TCL | 360252c | 2022-01-05 00:04:21 +0800 | [diff] [blame] | 222 | else if (GetFabricTable().FabricCount() != 0) |
Pankaj Garg | eac670e | 2021-02-02 18:36:52 -0800 | [diff] [blame] | 223 | { |
Pankaj Garg | ccdffbc | 2021-09-24 09:10:22 -0700 | [diff] [blame] | 224 | // The device is already commissioned, proactively disable BLE advertisement. |
| 225 | ChipLogProgress(AppServer, "Fabric already commissioned. Disabling BLE advertisement"); |
Hui.Li-TCL | 360252c | 2022-01-05 00:04:21 +0800 | [diff] [blame] | 226 | #if CONFIG_NETWORK_LAYER_BLE |
Pankaj Garg | eac670e | 2021-02-02 18:36:52 -0800 | [diff] [blame] | 227 | chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(false); |
Hui.Li-TCL | 360252c | 2022-01-05 00:04:21 +0800 | [diff] [blame] | 228 | #endif |
Pankaj Garg | eac670e | 2021-02-02 18:36:52 -0800 | [diff] [blame] | 229 | } |
Vivien Nicolas | 0110e48 | 2020-12-02 19:45:41 +0100 | [diff] [blame] | 230 | else |
Jiacheng Guo | df2497d | 2020-09-30 21:16:57 +0800 | [diff] [blame] | 231 | { |
Kamil Kasperczyk | 6ad5711 | 2021-02-11 16:44:36 +0100 | [diff] [blame] | 232 | #if CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART |
Pankaj Garg | 2815a10 | 2021-09-15 17:22:34 -0700 | [diff] [blame] | 233 | GetFabricTable().DeleteAllFabrics(); |
| 234 | SuccessOrExit(err = mCommissioningWindowManager.OpenBasicCommissioningWindow()); |
Kamil Kasperczyk | 6ad5711 | 2021-02-11 16:44:36 +0100 | [diff] [blame] | 235 | #endif |
Jiacheng Guo | df2497d | 2020-09-30 21:16:57 +0800 | [diff] [blame] | 236 | } |
Jiacheng Guo | df2497d | 2020-09-30 21:16:57 +0800 | [diff] [blame] | 237 | |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 238 | app::DnssdServer::Instance().SetSecuredPort(mSecuredServicePort); |
| 239 | app::DnssdServer::Instance().SetUnsecuredPort(mUnsecuredServicePort); |
Vivien Nicolas | fdb8114 | 2022-02-22 17:59:26 +0100 | [diff] [blame] | 240 | app::DnssdServer::Instance().SetInterfaceId(mInterfaceId); |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 241 | |
| 242 | // TODO @bzbarsky-apple @cecille Move to examples |
| 243 | // 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] | 244 | #if !CHIP_DEVICE_LAYER_TARGET_ESP32 && !CHIP_DEVICE_LAYER_TARGET_MBED && \ |
pankore | bab75e6 | 2021-10-28 21:48:46 +0800 | [diff] [blame] | 245 | (!CHIP_DEVICE_LAYER_TARGET_AMEBA || !CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE) |
chrisdecenzo | 515ea65 | 2021-08-18 11:27:09 -0700 | [diff] [blame] | 246 | // StartServer only enables commissioning mode if device has not been commissioned |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 247 | app::DnssdServer::Instance().StartServer(); |
Paul Zander | 3510f14 | 2021-04-15 16:29:40 +0200 | [diff] [blame] | 248 | #endif |
Kamil Kasperczyk | 4401402 | 2021-03-12 21:18:45 +0100 | [diff] [blame] | 249 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 250 | err = mCASEServer.ListenForSessionEstablishment(&mExchangeMgr, &mTransports, chip::DeviceLayer::ConnectivityMgr().GetBleLayer(), |
Jerry Johns | 75372d8 | 2022-03-01 06:32:40 -0800 | [diff] [blame] | 251 | &mSessions, &mFabrics); |
Pankaj Garg | 8ced1a1 | 2021-05-14 06:55:59 -0700 | [diff] [blame] | 252 | SuccessOrExit(err); |
| 253 | |
Andrei Litvin | 16bdace2 | 2022-01-07 09:32:47 -0500 | [diff] [blame] | 254 | err = mCASESessionManager.Init(); |
Jiacheng Guo | 7ead62d | 2021-12-14 23:09:38 +0800 | [diff] [blame] | 255 | |
mkardous-silabs | de95567 | 2022-01-25 16:05:37 -0500 | [diff] [blame] | 256 | // 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-silabs | de95567 | 2022-01-25 16:05:37 -0500 | [diff] [blame] | 261 | #if !CHIP_DEVICE_CONFIG_ENABLE_THREAD |
mkardous-silabs | 3c31ec7 | 2022-02-24 17:37:45 -0500 | [diff] [blame] | 262 | RejoinExistingMulticastGroups(); |
mkardous-silabs | de95567 | 2022-01-25 16:05:37 -0500 | [diff] [blame] | 263 | #endif // !CHIP_DEVICE_CONFIG_ENABLE_THREAD |
mkardous-silabs | 3c31ec7 | 2022-02-24 17:37:45 -0500 | [diff] [blame] | 264 | |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 265 | exit: |
| 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 Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 274 | return err; |
| 275 | } |
| 276 | |
mkardous-silabs | 3c31ec7 | 2022-02-24 17:37:45 -0500 | [diff] [blame] | 277 | void 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 Nicolas | 8297617 | 2022-02-16 11:47:52 +0100 | [diff] [blame] | 310 | void Server::DispatchShutDownAndStopEventLoop() |
| 311 | { |
| 312 | chip::DeviceLayer::PlatformMgr().ScheduleWork(DispatchShutDownEvent); |
| 313 | chip::DeviceLayer::PlatformMgr().ScheduleWork(StopEventLoop); |
| 314 | } |
| 315 | |
Kamil Kasperczyk | 843643c | 2022-02-23 17:38:32 +0100 | [diff] [blame] | 316 | void Server::ScheduleFactoryReset() |
| 317 | { |
| 318 | chip::DeviceLayer::PlatformMgr().ScheduleWork(FactoryReset); |
| 319 | } |
| 320 | |
| 321 | void 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 Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 335 | void Server::Shutdown() |
| 336 | { |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 337 | chip::Dnssd::ServiceAdvertiser::Instance().Shutdown(); |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 338 | chip::app::InteractionModelEngine::GetInstance()->Shutdown(); |
Boris Zbarsky | 980e177 | 2022-02-25 03:38:19 -0500 | [diff] [blame] | 339 | 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 Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 344 | mSessions.Shutdown(); |
| 345 | mTransports.Close(); |
Kevin Schoedel | beb6096 | 2021-11-22 04:28:03 -0500 | [diff] [blame] | 346 | mCommissioningWindowManager.Shutdown(); |
Andrei Litvin | eb2d033 | 2022-02-25 16:10:03 -0500 | [diff] [blame] | 347 | mCASESessionManager.Shutdown(); |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 348 | chip::Platform::MemoryShutdown(); |
Song Guo | 750e2e6 | 2020-09-15 00:45:51 +0800 | [diff] [blame] | 349 | } |
Andrei Litvin | 7093fde | 2021-02-26 16:13:23 -0500 | [diff] [blame] | 350 | |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 351 | #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 Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 355 | CHIP_ERROR Server::SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress commissioner) |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 356 | { |
| 357 | ChipLogDetail(AppServer, "SendUserDirectedCommissioningRequest2"); |
| 358 | |
| 359 | CHIP_ERROR err; |
Vivien Nicolas | 8e40abc | 2021-12-16 17:12:37 +0100 | [diff] [blame] | 360 | char nameBuffer[chip::Dnssd::Commission::kInstanceNameMaxLength + 1]; |
Kamil Kasperczyk | d9e02a0 | 2021-10-12 09:19:23 +0200 | [diff] [blame] | 361 | err = app::DnssdServer::Instance().GetCommissionableInstanceName(nameBuffer, sizeof(nameBuffer)); |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 362 | 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 | |
chrisdecenzo | 31c6249 | 2021-08-10 11:47:15 -0700 | [diff] [blame] | 369 | chip::System::PacketBufferHandle payloadBuf = chip::MessagePacketBuffer::NewWithData(nameBuffer, strlen(nameBuffer)); |
| 370 | if (payloadBuf.IsNull()) |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 371 | { |
chrisdecenzo | 31c6249 | 2021-08-10 11:47:15 -0700 | [diff] [blame] | 372 | ChipLogError(AppServer, "Unable to allocate packet buffer\n"); |
| 373 | return CHIP_ERROR_NO_MEMORY; |
| 374 | } |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 375 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 376 | err = gUDCClient.SendUDCMessage(&mTransports, std::move(payloadBuf), commissioner); |
chrisdecenzo | 31c6249 | 2021-08-10 11:47:15 -0700 | [diff] [blame] | 377 | 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)); |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 384 | } |
| 385 | return err; |
| 386 | } |
chrisdecenzo | 250d5f7 | 2021-07-21 19:28:45 -0700 | [diff] [blame] | 387 | #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT |
| 388 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 389 | CHIP_ERROR Server::AddTestCommissioning() |
Damian Królik | c6db453 | 2021-03-10 14:58:58 +0100 | [diff] [blame] | 390 | { |
Pankaj Garg | 2a63309 | 2021-07-23 09:15:02 -0700 | [diff] [blame] | 391 | CHIP_ERROR err = CHIP_NO_ERROR; |
Pankaj Garg | 2a63309 | 2021-07-23 09:15:02 -0700 | [diff] [blame] | 392 | PASESession * testSession = nullptr; |
Vivien Nicolas | 7768fc4 | 2021-04-20 16:56:11 +0200 | [diff] [blame] | 393 | PASESessionSerializable serializedTestSession; |
Zang MingJie | b210aa3 | 2021-12-10 22:53:16 +0800 | [diff] [blame] | 394 | SessionHolder session; |
Damian Królik | c6db453 | 2021-03-10 14:58:58 +0100 | [diff] [blame] | 395 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 396 | mTestPairing.ToSerializable(serializedTestSession); |
Vivien Nicolas | 7768fc4 | 2021-04-20 16:56:11 +0200 | [diff] [blame] | 397 | |
| 398 | testSession = chip::Platform::New<PASESession>(); |
| 399 | testSession->FromSerializable(serializedTestSession); |
Zang MingJie | b210aa3 | 2021-12-10 22:53:16 +0800 | [diff] [blame] | 400 | SuccessOrExit(err = mSessions.NewPairing(session, Optional<PeerAddress>{ PeerAddress::Uninitialized() }, |
| 401 | chip::kTestControllerNodeId, testSession, CryptoContext::SessionRole::kResponder, |
| 402 | kMinValidFabricIndex)); |
Damian Królik | c6db453 | 2021-03-10 14:58:58 +0100 | [diff] [blame] | 403 | |
| 404 | exit: |
Vivien Nicolas | 7768fc4 | 2021-04-20 16:56:11 +0200 | [diff] [blame] | 405 | if (testSession) |
| 406 | { |
| 407 | testSession->Clear(); |
| 408 | chip::Platform::Delete(testSession); |
| 409 | } |
| 410 | |
Pankaj Garg | 6a36256 | 2021-08-11 14:48:53 -0700 | [diff] [blame] | 411 | if (err != CHIP_NO_ERROR) |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 412 | { |
| 413 | mFabrics.ReleaseFabricIndex(kMinValidFabricIndex); |
| 414 | } |
Damian Królik | c6db453 | 2021-03-10 14:58:58 +0100 | [diff] [blame] | 415 | return err; |
| 416 | } |
| 417 | |
Jiacheng Guo | 735dbb0 | 2021-09-11 04:24:07 +0800 | [diff] [blame] | 418 | } // namespace chip |