forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAppTaskBase.cpp
445 lines (376 loc) · 14.2 KB
/
AppTaskBase.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
*
* Copyright (c) 2021 Project CHIP Authors
* Copyright (c) 2021 Google LLC.
* Copyright 2024 NXP
* All rights reserved.
*
* 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 "AppTaskBase.h"
#include "AppEvent.h"
#include "AppFactoryData.h"
#include "CHIPDeviceManager.h"
#include "CommonDeviceCallbacks.h"
#include <app/server/Dnssd.h>
#include <lib/dnssd/Advertiser.h>
#include <app/server/OnboardingCodesUtil.h>
#include <app/util/attribute-storage.h>
#include <app/clusters/network-commissioning/network-commissioning.h>
#include <platform/CommissionableDataProvider.h>
#include "lib/core/ErrorStr.h"
#include <platform/DeviceInstanceInfoProvider.h>
#if defined(MATTER_DM_PLUGIN_USER_LABEL) || defined(MATTER_DM_PLUGIN_FIXED_LABEL)
#ifndef CONFIG_DEVICE_INFO_PROVIDER_IMPL
#define CONFIG_DEVICE_INFO_PROVIDER_IMPL 1
#endif
#endif
#if CONFIG_DEVICE_INFO_PROVIDER_IMPL
#include <DeviceInfoProviderImpl.h>
#endif
/* Flag generated by Zap */
#ifdef MATTER_DM_PLUGIN_BINDING
#include "binding-handler.h"
#endif
#if CONFIG_NET_L2_OPENTHREAD
#include <inet/EndPointStateOpenThread.h>
#include <lib/support/ThreadOperationalDataset.h>
#endif
#if CONFIG_CHIP_TCP_DOWNLOAD
#include "TcpDownload.h"
#endif
#if CONFIG_OPERATIONAL_KEYSTORE
#include "OperationalKeystore.h"
#endif
#if CONFIG_CHIP_OTA_PROVIDER
#include <OTAProvider.h>
#endif
#if CONFIG_DIAG_LOGS_DEMO
#include "DiagnosticLogsDemo.h"
#endif
#if CONFIG_LOW_POWER
#include "LowPower.h"
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
#include "OTARequestorInitiator.h"
#endif
#if CONFIG_CHIP_TEST_EVENT && CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
#include <app/clusters/ota-requestor/OTATestEventTriggerDelegate.h>
#endif
#ifdef ENABLE_CHIP_SHELL
#include <lib/shell/commands/WiFi.h>
#endif
#ifdef SMOKE_CO_ALARM
#include <app/TestEventTriggerDelegate.h>
#include <app/clusters/smoke-co-alarm-server/SmokeCOTestEventTriggerHandler.h>
#endif
#if CHIP_CONFIG_ENABLE_ICD_SERVER
#include <app/TestEventTriggerDelegate.h>
#endif
#ifndef CONFIG_THREAD_DEVICE_TYPE
#define CONFIG_THREAD_DEVICE_TYPE kThreadDeviceType_Router
#endif
using namespace chip;
using namespace chip::TLV;
using namespace ::chip::Credentials;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceManager;
using namespace ::chip::app::Clusters;
#if CONFIG_DEVICE_INFO_PROVIDER_IMPL
chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
#endif
#if CONFIG_CHIP_WIFI || CHIP_DEVICE_CONFIG_ENABLE_WPA
app::Clusters::NetworkCommissioning::Instance sNetworkCommissioningInstance(0,
chip::NXP::App::GetAppTask().GetWifiDriverInstance());
#elif CONFIG_CHIP_ETHERNET
app::Clusters::NetworkCommissioning::Instance
sNetworkCommissioningInstance(0, chip::NXP::App::GetAppTask().GetEthernetDriverInstance());
#endif
#if CHIP_CONFIG_ENABLE_ICD_SERVER || (CONFIG_CHIP_TEST_EVENT && CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR)
static uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLength] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb,
0xcc, 0xdd, 0xee, 0xff };
#endif
#if CONFIG_NET_L2_OPENTHREAD
void LockOpenThreadTask(void)
{
chip::NXP::App::GetAppTask().AppMatter_DisallowDeviceToSleep();
chip::DeviceLayer::ThreadStackMgr().LockThreadStack();
}
void UnlockOpenThreadTask(void)
{
chip::DeviceLayer::ThreadStackMgr().UnlockThreadStack();
chip::NXP::App::GetAppTask().AppMatter_AllowDeviceToSleep();
}
#endif
void chip::NXP::App::AppTaskBase::InitServer(intptr_t arg)
{
GetAppTask().PreInitMatterServerInstance();
#if CONFIG_CHIP_TEST_EVENT && CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
static OTATestEventTriggerDelegate testEventTriggerDelegate{ ByteSpan(sTestEventTriggerEnableKey) };
initParams.testEventTriggerDelegate = &testEventTriggerDelegate;
#endif
#ifdef SMOKE_CO_ALARM
static SimpleTestEventTriggerDelegate sTestEventTriggerDelegate{};
static SmokeCOTestEventTriggerHandler sSmokeCOTestEventTriggerHandler{};
VerifyOrDie(sTestEventTriggerDelegate.Init(ByteSpan(sTestEventTriggerEnableKey)) == CHIP_NO_ERROR);
VerifyOrDie(sTestEventTriggerDelegate.AddHandler(&sSmokeCOTestEventTriggerHandler) == CHIP_NO_ERROR);
initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate;
#endif
#if CHIP_CONFIG_ENABLE_ICD_SERVER
static SimpleTestEventTriggerDelegate sTestEventTriggerDelegate{};
VerifyOrDie(sTestEventTriggerDelegate.Init(ByteSpan(sTestEventTriggerEnableKey)) == CHIP_NO_ERROR);
initParams.testEventTriggerDelegate = &sTestEventTriggerDelegate;
#endif
#if CONFIG_OPERATIONAL_KEYSTORE
initParams.operationalKeystore = chip::NXP::App::OperationalKeystore::GetInstance();
#endif
(void) initParams.InitializeStaticResourcesBeforeServerInit();
#if CONFIG_NET_L2_OPENTHREAD
// Init ZCL Data Model and start server
chip::Inet::EndPointStateOpenThread::OpenThreadEndpointInitParam nativeParams;
nativeParams.lockCb = LockOpenThreadTask;
nativeParams.unlockCb = UnlockOpenThreadTask;
nativeParams.openThreadInstancePtr = chip::DeviceLayer::ThreadStackMgrImpl().OTInstance();
initParams.endpointNativeParams = static_cast<void *>(&nativeParams);
#endif
VerifyOrDie((chip::Server::GetInstance().Init(initParams)) == CHIP_NO_ERROR);
auto * persistentStorage = &Server::GetInstance().GetPersistentStorage();
#if CONFIG_OPERATIONAL_KEYSTORE
chip::NXP::App::OperationalKeystore::Init(persistentStorage);
#endif
#if CONFIG_DEVICE_INFO_PROVIDER_IMPL
gExampleDeviceInfoProvider.SetStorageDelegate(persistentStorage);
chip::DeviceLayer::SetDeviceInfoProvider(&gExampleDeviceInfoProvider);
#endif
GetAppTask().PostInitMatterServerInstance();
#if CONFIG_DIAG_LOGS_DEMO
chip::NXP::App::DiagnosticLogsDemo::DisplayUsage();
#endif
#if CONFIG_CHIP_OTA_PROVIDER
InitOTAServer();
#endif
}
CHIP_ERROR chip::NXP::App::AppTaskBase::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;
/* Init Chip memory management before the stack */
chip::Platform::MemoryInit();
#if CONFIG_LOW_POWER
chip::NXP::App::LowPower::Init();
#endif
/* Initialize Matter factory data before initializing the Matter stack */
err = AppFactoryData_PreMatterStackInit();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Pre Factory Data Provider init failed");
goto exit;
}
/*
* Initialize the CHIP stack.
* Would also initialize all required platform modules
*/
err = PlatformMgr().InitChipStack();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "PlatformMgr().InitChipStack() failed: %s", ErrorStr(err));
goto exit;
}
/* Initialize Matter factory data after initializing the Matter stack */
err = AppFactoryData_PostMatterStackInit();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Post Factory Data Provider init failed");
goto exit;
}
/*
* Register all application callbacks allowing to be informed of stack events
*/
err = CHIPDeviceManager::GetInstance().Init(&GetDeviceCallbacks());
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "CHIPDeviceManager.Init() failed: %s", ErrorStr(err));
goto exit;
}
/* Make sure to initialize the Matter CLI which will include the ot-cli first.
* In fact it is mandatory to enable first the ot-cli before initializing the Matter openthread layer
* which would modify some contexts of the openthread instance.
*/
err = AppMatter_Register();
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "Error during APP features registration"));
#if CONFIG_NET_L2_OPENTHREAD
err = ThreadStackMgr().InitThreadStack();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Error during ThreadStackMgr().InitThreadStack()");
return err;
}
err = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::CONFIG_THREAD_DEVICE_TYPE);
if (err != CHIP_NO_ERROR)
{
return err;
}
#endif
/*
* Schedule an event to the Matter stack to initialize
* the ZCL Data Model and start server
*/
PlatformMgr().ScheduleWork(InitServer, 0);
/* Flag generated by Zap */
#ifdef MATTER_DM_PLUGIN_BINDING
/* Init binding handlers */
err = InitBindingHandlers();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "InitBindingHandlers failed: %s", ErrorStr(err));
goto exit;
}
#endif
#if CONFIG_CHIP_WIFI || CHIP_DEVICE_CONFIG_ENABLE_WPA
sNetworkCommissioningInstance.Init();
#ifdef ENABLE_CHIP_SHELL
Shell::SetWiFiDriver(chip::NXP::App::GetAppTask().GetWifiDriverInstance());
#endif
#elif CONFIG_CHIP_ETHERNET
sNetworkCommissioningInstance.Init();
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
if (err == CHIP_NO_ERROR)
{
/* If an update is under test make it permanent */
OTARequestorInitiator::Instance().HandleSelfTest();
}
#endif
ConfigurationMgr().LogDeviceConfig();
// QR code will be used with CHIP Tool
PrintOnboardingInfo();
PrintCurrentVersion();
/* Start a task to run the CHIP Device event loop. */
err = PlatformMgr().StartEventLoopTask();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Error during PlatformMgr().StartEventLoopTask()");
goto exit;
}
#if CONFIG_NET_L2_OPENTHREAD
// Start OpenThread task
err = ThreadStackMgrImpl().StartThreadTask();
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "Error during ThreadStackMgrImpl().StartThreadTask()");
}
#endif
#if CONFIG_CHIP_TCP_DOWNLOAD
EnableTcpDownloadComponent();
#endif
exit:
return err;
}
void chip::NXP::App::AppTaskBase::StartCommissioning(intptr_t arg)
{
/* Check the status of the commissioning */
if (ConfigurationMgr().IsFullyProvisioned())
{
ChipLogProgress(DeviceLayer, "Device already commissioned");
}
else if (chip::Server::GetInstance().GetCommissioningWindowManager().IsCommissioningWindowOpen())
{
ChipLogProgress(DeviceLayer, "Commissioning window already opened");
}
else
{
chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow();
}
}
void chip::NXP::App::AppTaskBase::StopCommissioning(intptr_t arg)
{
/* Check the status of the commissioning */
if (ConfigurationMgr().IsFullyProvisioned())
{
ChipLogProgress(DeviceLayer, "Device already commissioned");
}
else if (!chip::Server::GetInstance().GetCommissioningWindowManager().IsCommissioningWindowOpen())
{
ChipLogProgress(DeviceLayer, "Commissioning window not opened");
}
else
{
chip::Server::GetInstance().GetCommissioningWindowManager().CloseCommissioningWindow();
}
}
void chip::NXP::App::AppTaskBase::SwitchCommissioningState(intptr_t arg)
{
/* Check the status of the commissioning */
if (ConfigurationMgr().IsFullyProvisioned())
{
ChipLogProgress(DeviceLayer, "Device already commissioned");
}
else if (!chip::Server::GetInstance().GetCommissioningWindowManager().IsCommissioningWindowOpen())
{
chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow();
}
else
{
chip::Server::GetInstance().GetCommissioningWindowManager().CloseCommissioningWindow();
}
}
void chip::NXP::App::AppTaskBase::StartCommissioningHandler(void)
{
/* Publish an event to the Matter task to always set the commissioning state in the Matter task context */
PlatformMgr().ScheduleWork(StartCommissioning, 0);
}
void chip::NXP::App::AppTaskBase::StopCommissioningHandler(void)
{
/* Publish an event to the Matter task to always set the commissioning state in the Matter task context */
PlatformMgr().ScheduleWork(StopCommissioning, 0);
}
void chip::NXP::App::AppTaskBase::SwitchCommissioningStateHandler(void)
{
/* Publish an event to the Matter task to always set the commissioning state in the Matter task context */
PlatformMgr().ScheduleWork(SwitchCommissioningState, 0);
}
void chip::NXP::App::AppTaskBase::FactoryResetHandler(void)
{
/* Emit the ShutDown event before factory reset */
chip::Server::GetInstance().GenerateShutDownEvent();
chip::Server::GetInstance().ScheduleFactoryReset();
}
void chip::NXP::App::AppTaskBase::PrintOnboardingInfo()
{
#if CONFIG_NETWORK_LAYER_BLE
auto flags = chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE);
#else
auto flags = chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kOnNetwork);
#endif /* CONFIG_NETWORK_LAYER_BLE */
chip::PayloadContents payload;
CHIP_ERROR err = GetPayloadContents(payload, flags);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "GetPayloadContents() failed: %" CHIP_ERROR_FORMAT, err.Format());
}
#if CONFIG_USER_ACTION_REQUIRED
payload.commissioningFlow = chip::CommissioningFlow::kUserActionRequired;
#endif
PrintOnboardingCodes(payload);
}
void chip::NXP::App::AppTaskBase::PrintCurrentVersion()
{
// Print the current software version
char currentSoftwareVer[ConfigurationManager::kMaxSoftwareVersionStringLength + 1] = { 0 };
auto err = ConfigurationMgr().GetSoftwareVersionString(currentSoftwareVer, sizeof(currentSoftwareVer));
ReturnOnFailure(err);
uint32_t currentVersion;
err = ConfigurationMgr().GetSoftwareVersion(currentVersion);
ReturnOnFailure(err);
ChipLogProgress(DeviceLayer, "Current Software Version: %s, %d", currentSoftwareVer, static_cast<int>(currentVersion));
}