-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathServer.cpp
288 lines (244 loc) · 9.88 KB
/
Server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <app/server/Server.h>
#include <app/InteractionModelEngine.h>
#include <app/server/EchoHandler.h>
#include <app/server/Mdns.h>
#include <app/server/RendezvousServer.h>
#include <app/util/DataModelHandler.h>
#include <ble/BLEEndPoint.h>
#include <inet/IPAddress.h>
#include <inet/InetError.h>
#include <inet/InetLayer.h>
#include <lib/core/CHIPPersistentStorageDelegate.h>
#include <lib/mdns/Advertiser.h>
#include <lib/mdns/ServiceNaming.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/ErrorStr.h>
#include <lib/support/logging/CHIPLogging.h>
#include <messaging/ExchangeMgr.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/KeyValueStoreManager.h>
#include <protocols/secure_channel/CASEServer.h>
#include <protocols/secure_channel/MessageCounterManager.h>
#include <setup_payload/SetupPayload.h>
#include <sys/param.h>
#include <system/SystemPacketBuffer.h>
#include <system/TLVPacketBufferBackingStore.h>
#include <transport/FabricTable.h>
#include <transport/SecureSessionMgr.h>
using chip::RendezvousInformationFlag;
using chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr;
using chip::Inet::IPAddressType;
using chip::Transport::BleListenParameters;
using chip::Transport::kMinValidFabricIndex;
using chip::Transport::PeerAddress;
using chip::Transport::UdpListenParameters;
namespace {
constexpr bool isRendezvousBypassed()
{
#if defined(CHIP_BYPASS_RENDEZVOUS) && CHIP_BYPASS_RENDEZVOUS
return true;
#elif defined(CONFIG_RENDEZVOUS_MODE)
return static_cast<RendezvousInformationFlag>(CONFIG_RENDEZVOUS_MODE) == RendezvousInformationFlag::kNone;
#else
return false;
#endif
}
} // namespace
namespace chip {
Server Server::sServer;
CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint16_t unsecureServicePort)
{
mAppDelegate = delegate;
mSecuredServicePort = secureServicePort;
mUnsecuredServicePort = unsecureServicePort;
CHIP_ERROR err = CHIP_NO_ERROR;
chip::Platform::MemoryInit();
mCommissionManager.SetAppDelegate(delegate);
InitDataModelHandler(&mExchangeMgr);
#if CHIP_DEVICE_LAYER_TARGET_DARWIN
err = DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init("chip.store");
SuccessOrExit(err);
#elif CHIP_DEVICE_LAYER_TARGET_LINUX
DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init(CHIP_CONFIG_KVS_PATH);
#endif
err = mRendezvousServer.Init(mAppDelegate, &mSessionIDAllocator);
SuccessOrExit(err);
err = mFabrics.Init(&mServerStorage);
SuccessOrExit(err);
// Init transport before operations with secure session mgr.
err = mTransports.Init(UdpListenParameters(&DeviceLayer::InetLayer)
.SetAddressType(IPAddressType::kIPAddressType_IPv6)
.SetListenPort(mSecuredServicePort)
#if INET_CONFIG_ENABLE_IPV4
,
UdpListenParameters(&DeviceLayer::InetLayer)
.SetAddressType(IPAddressType::kIPAddressType_IPv4)
.SetListenPort(mSecuredServicePort)
#endif
#if CONFIG_NETWORK_LAYER_BLE
,
BleListenParameters(DeviceLayer::ConnectivityMgr().GetBleLayer())
#endif
);
SuccessOrExit(err);
err = mSessions.Init(&DeviceLayer::SystemLayer(), &mTransports, &mFabrics, &mMessageCounterManager);
SuccessOrExit(err);
err = mExchangeMgr.Init(&mSessions);
SuccessOrExit(err);
err = mMessageCounterManager.Init(&mExchangeMgr);
SuccessOrExit(err);
err = chip::app::InteractionModelEngine::GetInstance()->Init(&mExchangeMgr, nullptr);
SuccessOrExit(err);
#if defined(CHIP_APP_USE_ECHO)
err = InitEchoHandler(&gExchangeMgr);
SuccessOrExit(err);
#endif
if (isRendezvousBypassed())
{
ChipLogProgress(AppServer, "Rendezvous and secure pairing skipped");
SuccessOrExit(err = AddTestCommissioning());
}
else if (DeviceLayer::ConnectivityMgr().IsWiFiStationProvisioned() || DeviceLayer::ConnectivityMgr().IsThreadProvisioned())
{
// If the network is already provisioned, proactively disable BLE advertisement.
ChipLogProgress(AppServer, "Network already provisioned. Disabling BLE advertisement");
chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(false);
}
else
{
#if CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART
SuccessOrExit(err = mCommissionManager.OpenBasicCommissioningWindow(ResetFabrics::kYes));
#endif
}
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS
app::Mdns::SetSecuredPort(mSecuredServicePort);
app::Mdns::SetUnsecuredPort(mUnsecuredServicePort);
#endif // CHIP_DEVICE_CONFIG_ENABLE_MDNS
// TODO @bzbarsky-apple @cecille Move to examples
// ESP32 and Mbed OS examples have a custom logic for enabling DNS-SD
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS && !CHIP_DEVICE_LAYER_TARGET_ESP32 && !CHIP_DEVICE_LAYER_TARGET_MBED
// StartServer only enables commissioning mode if device has not been commissioned
app::Mdns::StartServer();
#endif
// TODO @pan-apple Use IM protocol ID.
// Register to receive unsolicited legacy ZCL messages from the exchange manager.
err = mExchangeMgr.RegisterUnsolicitedMessageHandlerForProtocol(Protocols::TempZCL::Id, this);
SuccessOrExit(err);
// TODO @pan-apple Remove service provisioniong, maybe multi-admin?
// Register to receive unsolicited Service Provisioning messages from the exchange manager.
err = mExchangeMgr.RegisterUnsolicitedMessageHandlerForProtocol(Protocols::ServiceProvisioning::Id, this);
SuccessOrExit(err);
err = mCASEServer.ListenForSessionEstablishment(&mExchangeMgr, &mTransports, chip::DeviceLayer::ConnectivityMgr().GetBleLayer(),
&mSessions, &mFabrics, &mSessionIDAllocator);
SuccessOrExit(err);
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "ERROR setting up transport: %s", ErrorStr(err));
}
else
{
ChipLogProgress(AppServer, "Server Listening...");
}
return err;
}
void Server::Shutdown()
{
chip::Mdns::ServiceAdvertiser::Instance().StopPublishDevice();
chip::app::InteractionModelEngine::GetInstance()->Shutdown();
mExchangeMgr.Shutdown();
mSessions.Shutdown();
mTransports.Close();
mRendezvousServer.Cleanup();
chip::Platform::MemoryShutdown();
}
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
// NOTE: UDC client is located in Server.cpp because it really only makes sense
// to send UDC from a Matter device. The UDC message payload needs to include the device's
// randomly generated service name.
CHIP_ERROR Server::SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress commissioner)
{
ChipLogDetail(AppServer, "SendUserDirectedCommissioningRequest2");
CHIP_ERROR err;
char nameBuffer[chip::Mdns::kMaxInstanceNameSize + 1];
err = app::Mdns::GetCommissionableInstanceName(nameBuffer, sizeof(nameBuffer));
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "Failed to get mdns instance name error: %s", ErrorStr(err));
return err;
}
ChipLogDetail(AppServer, "instanceName=%s", nameBuffer);
chip::System::PacketBufferHandle payloadBuf = chip::MessagePacketBuffer::NewWithData(nameBuffer, strlen(nameBuffer));
if (payloadBuf.IsNull())
{
ChipLogError(AppServer, "Unable to allocate packet buffer\n");
return CHIP_ERROR_NO_MEMORY;
}
err = gUDCClient.SendUDCMessage(&mTransports, std::move(payloadBuf), commissioner);
if (err == CHIP_NO_ERROR)
{
ChipLogDetail(AppServer, "Send UDC request success");
}
else
{
ChipLogError(AppServer, "Send UDC request failed, err: %s\n", chip::ErrorStr(err));
}
return err;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
CHIP_ERROR Server::AddTestCommissioning()
{
CHIP_ERROR err = CHIP_NO_ERROR;
PASESession * testSession = nullptr;
PASESessionSerializable serializedTestSession;
mTestPairing.ToSerializable(serializedTestSession);
testSession = chip::Platform::New<PASESession>();
testSession->FromSerializable(serializedTestSession);
SuccessOrExit(err = mSessions.NewPairing(Optional<PeerAddress>{ PeerAddress::Uninitialized() }, chip::kTestControllerNodeId,
testSession, SecureSession::SessionRole::kResponder, kMinValidFabricIndex));
exit:
if (testSession)
{
testSession->Clear();
chip::Platform::Delete(testSession);
}
if (err != CHIP_NO_ERROR)
{
mFabrics.ReleaseFabricIndex(kMinValidFabricIndex);
}
return err;
}
CHIP_ERROR Server::OnMessageReceived(Messaging::ExchangeContext * exchangeContext, const PayloadHeader & payloadHeader,
System::PacketBufferHandle && buffer)
{
CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrReturnError(!buffer.IsNull(), err = CHIP_ERROR_INVALID_ARGUMENT);
// TODO: BDX messages will also be possible in the future.
HandleDataModelMessage(exchangeContext, std::move(buffer));
return err;
}
void Server::OnResponseTimeout(Messaging::ExchangeContext * ec)
{
ChipLogProgress(AppServer, "Failed to receive response");
if (mAppDelegate != nullptr)
{
mAppDelegate->OnReceiveError();
}
}
} // namespace chip