forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopeniotsdk_platform.cpp
323 lines (267 loc) · 9.5 KB
/
openiotsdk_platform.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
/*
* Copyright (c) 2022 Project CHIP Authors
* 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.
*/
/**
* @file
* This file provides the common Open IoT SDK platform functions.
* It can be used in Matter examples implementation.
*/
#include "openiotsdk_platform.h"
#include "cmsis_os2.h"
#include "iotsdk/ip_network_api.h"
#include "mbedtls/platform.h"
#ifdef CONFIG_CHIP_CRYPTO_PSA
#include "psa/crypto.h"
#endif
#include <DeviceInfoProviderImpl.h>
#include <app/util/endpoint-config-api.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/logging/CHIPLogging.h>
#include <lib/support/logging/Constants.h>
#include <platform/openiotsdk/OpenIoTSDKArchUtils.h>
#include <lib/core/CHIPConfig.h>
#include <platform/CHIPDeviceLayer.h>
#ifdef USE_CHIP_DATA_MODEL
#include <app/server/Server.h>
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <data-model-providers/codegen/Instance.h>
#include <setup_payload/OnboardingCodesUtil.h>
#endif // USE_CHIP_DATA_MODEL
#ifdef CHIP_OPEN_IOT_SDK_OTA_ENABLE
#include "openiotsdk_dfu_manager.h"
#endif // CHIP_OPEN_IOT_SDK_OTA_ENABLE
#include "psa/fwu_config.h"
#include "psa/update.h"
#include "tfm_ns_interface.h"
using namespace ::chip;
using namespace ::chip::Platform;
using namespace ::chip::DeviceLayer;
using namespace ::chip::Logging;
constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE;
#define NETWORK_UP_FLAG 0x00000001U
#define NETWORK_DOWN_FLAG 0x00000002U
#define ALL_EVENTS_FLAGS (NETWORK_UP_FLAG | NETWORK_DOWN_FLAG)
#define EVENT_TIMEOUT 5000
static osEventFlagsId_t event_flags_id;
static DeviceLayer::DeviceInfoProviderImpl gDeviceInfoProvider;
extern "C" {
// RTOS-specific initialization that is not declared in any header file
uint32_t tfm_ns_interface_init(void);
}
/** Wait for specific event and check error */
static int wait_for_event(uint32_t event)
{
int res = EXIT_SUCCESS;
int ret = osEventFlagsWait(event_flags_id, ALL_EVENTS_FLAGS, osFlagsWaitAny, ms2tick(EVENT_TIMEOUT));
if (ret < 0)
{
ChipLogError(NotSpecified, "osEventFlagsWait failed %d", ret);
return EXIT_FAILURE;
}
if (!(ret & event))
{
res = EXIT_FAILURE;
}
ret = osEventFlagsClear(event_flags_id, ALL_EVENTS_FLAGS);
if (ret < 0)
{
ChipLogError(NotSpecified, "osEventFlagsClear failed %d", ret);
return EXIT_FAILURE;
}
return res;
}
static void post_network_connect()
{
// Iterate on the network interface to see if we already have beed assigned addresses.
for (chip::Inet::InterfaceAddressIterator it; it.HasCurrent(); it.Next())
{
char ifName[Inet::InterfaceId::kMaxIfNameLength];
if (it.IsUp() && CHIP_NO_ERROR == it.GetInterfaceName(ifName, sizeof(ifName)))
{
chip::Inet::IPAddress addr;
if ((it.GetAddress(addr) == CHIP_NO_ERROR))
{
char ipStrBuf[chip::Inet::IPAddress::kMaxStringLength] = { 0 };
addr.ToString(ipStrBuf);
ChipLogProgress(DeviceLayer, "Got IP address on interface: %s IP: %s", ifName, ipStrBuf);
}
}
}
}
/** This callback is called by the ip network task. It translates from a network event code
* to platform event and sends it.
*
* @param event network up or down event.
*/
static void network_state_callback(network_state_callback_event_t event)
{
uint32_t event_flag = (event == NETWORK_UP) ? NETWORK_UP_FLAG : NETWORK_DOWN_FLAG;
ChipLogDetail(NotSpecified, "Network %s", (event == NETWORK_UP) ? "UP" : "DOWN");
int res = osEventFlagsSet(event_flags_id, event_flag);
if (res < 0)
{
ChipLogError(NotSpecified, "osEventFlagsSet failed %d", res);
}
}
static int get_psa_images_details()
{
psa_status_t status;
psa_fwu_component_info_t image_info;
status = psa_fwu_query(FWU_COMPONENT_ID_SECURE, &image_info);
if (status != PSA_SUCCESS)
{
ChipLogError(NotSpecified, "Failed to query secure firmware information. Error %ld", status);
return EXIT_FAILURE;
}
ChipLogProgress(NotSpecified, "Secure firmware version: %u.%u.%u-%lu\r\n", image_info.version.major, image_info.version.minor,
image_info.version.patch, image_info.version.build);
status = psa_fwu_query(FWU_COMPONENT_ID_NONSECURE, &image_info);
if (status != PSA_SUCCESS)
{
ChipLogError(NotSpecified, "Failed to query non-secure firmware information. Error %ld", status);
return EXIT_FAILURE;
}
ChipLogProgress(NotSpecified, "Non-secure firmware version: %u.%u.%u-%lu\r\n", image_info.version.major,
image_info.version.minor, image_info.version.patch, image_info.version.build);
return EXIT_SUCCESS;
}
int openiotsdk_platform_init(void)
{
int ret;
#if defined(NDEBUG) && CHIP_CONFIG_TEST == 0
SetLogFilter(LogCategory::kLogCategory_Progress);
#endif
ret = mbedtls_platform_setup(NULL);
if (ret)
{
ChipLogError(NotSpecified, "Mbed TLS platform initialization failed: %d", ret);
return EXIT_FAILURE;
}
#ifdef CONFIG_CHIP_CRYPTO_PSA
ret = psa_crypto_init();
if (ret)
{
ChipLogError(NotSpecified, "PSA crypto initialization failed: %d", ret);
return EXIT_FAILURE;
}
#endif
ret = get_psa_images_details();
if (ret != 0)
{
ChipLogError(NotSpecified, "Get PSA image details failed: %d", ret);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int openiotsdk_chip_init(void)
{
CHIP_ERROR err;
err = MemoryInit();
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Memory initialization failed: %s", err.AsString());
return EXIT_FAILURE;
}
err = PlatformMgr().InitChipStack();
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Chip stack initialization failed: %s", err.AsString());
return EXIT_FAILURE;
}
err = PlatformMgr().StartEventLoopTask();
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Chip stack start failed: %s", err.AsString());
return EXIT_FAILURE;
}
DeviceLayer::SetDeviceInfoProvider(&gDeviceInfoProvider);
return EXIT_SUCCESS;
}
int openiotsdk_network_init(bool wait)
{
int ret;
event_flags_id = osEventFlagsNew(NULL);
if (event_flags_id == NULL)
{
ChipLogError(NotSpecified, "Create event flags failed");
return EXIT_FAILURE;
}
ret = start_network_task(network_state_callback, NETWORK_THREAD_STACK_SIZE_DEFAULT);
if (ret != osOK)
{
ChipLogError(NotSpecified, "start_network_task failed %d", ret);
return EXIT_FAILURE;
}
if (wait)
{
if (wait_for_event(NETWORK_UP_FLAG) != EXIT_SUCCESS)
{
ChipLogError(NotSpecified, "Network initialization failed");
return EXIT_FAILURE;
}
post_network_connect();
}
return EXIT_SUCCESS;
}
int openiotsdk_chip_run(void)
{
#ifdef USE_CHIP_DATA_MODEL
// Init ZCL Data Model and start server
static chip::CommonCaseDeviceServerInitParams initParams;
CHIP_ERROR err = initParams.InitializeStaticResourcesBeforeServerInit();
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Initialize static resources before server init failed: %s", err.AsString());
return EXIT_FAILURE;
}
initParams.dataModelProvider = app::CodegenDataModelProviderInstance(initParams.persistentStorageDelegate);
initParams.operationalServicePort = CHIP_PORT;
initParams.userDirectedCommissioningPort = CHIP_UDC_PORT;
err = Server::GetInstance().Init(initParams);
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Initialize server failed: %s", err.AsString());
return EXIT_FAILURE;
}
// Now that the server has started and we are done with our startup logging,
// log our discovery/onboarding information again so it's not lost in the
// noise.
ConfigurationMgr().LogDeviceConfig();
PrintOnboardingCodes(RendezvousInformationFlags(RendezvousInformationFlag::kOnNetwork));
// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(Credentials::Examples::GetExampleDACProvider());
// We only have network commissioning on endpoint 0.
emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false);
#endif // USE_CHIP_DATA_MODEL
ChipLogProgress(NotSpecified, "Current software version: [%ld] %s", uint32_t(CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION),
CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING);
#ifdef CHIP_OPEN_IOT_SDK_OTA_ENABLE
err = GetDFUManager().Init();
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "DFU manager initialization failed: %s", err.AsString());
return EXIT_FAILURE;
}
#endif
return EXIT_SUCCESS;
}
void openiotsdk_chip_shutdown()
{
#ifdef USE_CHIP_DATA_MODEL
Server::GetInstance().Shutdown();
#endif // USE_CHIP_DATA_MODEL
}