-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathmain.cpp
324 lines (279 loc) · 13.8 KB
/
main.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
/*
*
* Copyright (c) 2021 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.
*/
#include <app/device/OperationalDeviceProxy.h>
#include <app/server/Server.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <lib/support/CHIPArgParser.hpp>
#include <platform/CHIPDeviceLayer.h>
#include <zap-generated/CHIPClientCallbacks.h>
#include <zap-generated/CHIPClusters.h>
#include "BDXDownloader.h"
using chip::ByteSpan;
using chip::CharSpan;
using chip::EndpointId;
using chip::FabricIndex;
using chip::NodeId;
using chip::Server;
using chip::VendorId;
using chip::bdx::TransferSession;
using chip::Callback::Callback;
using chip::Inet::IPAddress;
using chip::System::Layer;
using chip::Transport::PeerAddress;
using namespace chip::ArgParser;
using namespace chip::Messaging;
using namespace chip::app::device;
void OnQueryImageResponse(void * context, uint8_t status, uint32_t delayedActionTime, CharSpan imageURI, uint32_t softwareVersion,
CharSpan softwareVersionString, ByteSpan updateToken, bool userConsentNeeded,
ByteSpan metadataForRequestor);
void OnQueryImageFailure(void * context, uint8_t status);
void OnConnected(void * context, OperationalDeviceProxy * operationalDeviceProxy);
void OnConnectionFailure(void * context, OperationalDeviceProxy * operationalDeviceProxy, CHIP_ERROR error);
bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue);
// TODO: would be nicer to encapsulate these globals and the callbacks in some sort of class
OperationalDeviceProxy gOperationalDeviceProxy;
ExchangeContext * exchangeCtx = nullptr;
BdxDownloader bdxDownloader;
Callback<OtaSoftwareUpdateProviderClusterQueryImageResponseCallback> mQueryImageResponseCallback(OnQueryImageResponse, nullptr);
Callback<DefaultFailureCallback> mOnQueryFailureCallback(OnQueryImageFailure, nullptr);
Callback<OnOperationalDeviceConnected> mOnConnectedCallback(OnConnected, nullptr);
Callback<OnOperationalDeviceConnectionFailure> mOnConnectionFailureCallback(OnConnectionFailure, nullptr);
constexpr uint16_t kOptionProviderNodeId = 'n';
constexpr uint16_t kOptionProviderFabricIndex = 'f';
constexpr uint16_t kOptionUdpPort = 'u';
constexpr uint16_t kOptionDiscriminator = 'd';
constexpr uint16_t kOptionIPAddress = 'i';
constexpr uint16_t kOptionDelayQuery = 'q';
const char * ipAddress = NULL;
NodeId providerNodeId = 0x0;
FabricIndex providerFabricIndex = 1;
uint16_t requestorSecurePort = 0;
uint16_t setupDiscriminator = CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR;
uint16_t delayQueryTimeInSec = 0;
OptionDef cmdLineOptionsDef[] = {
{ "providerNodeId", chip::ArgParser::kArgumentRequired, kOptionProviderNodeId },
{ "providerFabricIndex", chip::ArgParser::kArgumentRequired, kOptionProviderFabricIndex },
{ "udpPort", chip::ArgParser::kArgumentRequired, kOptionUdpPort },
{ "discriminator", chip::ArgParser::kArgumentRequired, kOptionDiscriminator },
// TODO: This can be removed once OperationalDeviceProxy can resolve the IP Address from Node ID
{ "ipaddress", chip::ArgParser::kArgumentRequired, kOptionIPAddress },
{ "delayQuery", chip::ArgParser::kArgumentRequired, kOptionDelayQuery },
{},
};
OptionSet cmdLineOptions = { HandleOptions, cmdLineOptionsDef, "PROGRAM OPTIONS",
" -n/--providerNodeId <node ID>\n"
" Node ID of the OTA Provider to connect to (hex format)\n\n"
" This assumes that you've already commissioned the OTA Provider node with chip-tool.\n"
" -f/--providerFabricIndex <fabric index>\n"
" Fabric index of the OTA Provider to connect to. If none is specified, default value is 1.\n\n"
" This assumes that you've already commissioned the OTA Provider node with chip-tool.\n"
" -u/--udpPort <UDP port number>\n"
" UDP Port that the OTA Requestor listens on for secure connections.\n"
" -d/--discriminator <discriminator>\n"
" A 12-bit value used to discern between multiple commissionable CHIP device\n"
" advertisements. If none is specified, default value is 3840.\n"
" -i/--ipaddress <IP Address>\n"
" The IP Address of the OTA Provider to connect to. This value must be supplied.\n"
" -q/--delayQuery <Time in seconds>\n"
" From boot up, the amount of time to wait before triggering the QueryImage\n"
" command. If none or zero is supplied, QueryImage will not be triggered.\n" };
HelpOptions helpOptions("ota-requestor-app", "Usage: ota-requestor-app [options]", "1.0");
OptionSet * allOptions[] = { &cmdLineOptions, &helpOptions, nullptr };
void OnQueryImageResponse(void * context, uint8_t status, uint32_t delayedActionTime, CharSpan imageURI, uint32_t softwareVersion,
CharSpan softwareVersionString, ByteSpan updateToken, bool userConsentNeeded,
ByteSpan metadataForRequestor)
{
ChipLogDetail(SoftwareUpdate, "%s", __FUNCTION__);
TransferSession::TransferInitData initOptions;
initOptions.TransferCtlFlags = chip::bdx::TransferControlFlags::kReceiverDrive;
initOptions.MaxBlockSize = 1024;
char testFileDes[9] = { "test.txt" };
initOptions.FileDesLength = static_cast<uint16_t>(strlen(testFileDes));
initOptions.FileDesignator = reinterpret_cast<uint8_t *>(testFileDes);
{
chip::Messaging::ExchangeManager * exchangeMgr = gOperationalDeviceProxy.GetDevice().GetExchangeManager();
chip::Optional<chip::SessionHandle> session = gOperationalDeviceProxy.GetDevice().GetSecureSession();
if (exchangeMgr != nullptr && session.HasValue())
{
exchangeCtx = exchangeMgr->NewContext(session.Value(), &bdxDownloader);
}
if (exchangeCtx == nullptr)
{
ChipLogError(BDX, "unable to allocate ec: exchangeMgr=%p sessionExists? %u", exchangeMgr, session.HasValue());
return;
}
}
bdxDownloader.SetInitialExchange(exchangeCtx);
// This will kick of a timer which will regularly check for updates to the bdx::TransferSession state machine.
bdxDownloader.InitiateTransfer(&chip::DeviceLayer::SystemLayer(), chip::bdx::TransferRole::kReceiver, initOptions,
chip::System::Clock::Seconds16(20));
}
void OnQueryImageFailure(void * context, uint8_t status)
{
ChipLogDetail(SoftwareUpdate, "QueryImage failure response %" PRIu8, status);
}
void OnConnected(void * context, OperationalDeviceProxy * operationalDeviceProxy)
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::Controller::OtaSoftwareUpdateProviderCluster cluster;
constexpr EndpointId kOtaProviderEndpoint = 0;
chip::Callback::Cancelable * successCallback = mQueryImageResponseCallback.Cancel();
chip::Callback::Cancelable * failureCallback = mOnQueryFailureCallback.Cancel();
// These QueryImage params have been chosen arbitrarily
constexpr VendorId kExampleVendorId = VendorId::Common;
constexpr uint16_t kExampleProductId = 77;
constexpr uint16_t kExampleHWVersion = 3;
constexpr uint16_t kExampleSoftwareVersion = 0;
constexpr uint8_t kExampleProtocolsSupported =
EMBER_ZCL_OTA_DOWNLOAD_PROTOCOL_BDX_SYNCHRONOUS; // TODO: support this as a list once ember adds list support
const char locationBuf[] = { 'U', 'S' };
CharSpan exampleLocation(locationBuf);
constexpr bool kExampleClientCanConsent = false;
ByteSpan metadata;
err = cluster.Associate(&(operationalDeviceProxy->GetDevice()), kOtaProviderEndpoint);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Associate() failed: %s", chip::ErrorStr(err));
return;
}
err = cluster.QueryImage(successCallback, failureCallback, kExampleVendorId, kExampleProductId, kExampleHWVersion,
kExampleSoftwareVersion, kExampleProtocolsSupported, exampleLocation, kExampleClientCanConsent,
metadata);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "QueryImage() failed: %s", chip::ErrorStr(err));
}
}
void OnConnectionFailure(void * context, OperationalDeviceProxy * operationalDeviceProxy, CHIP_ERROR error)
{
ChipLogError(SoftwareUpdate, "failed to connect to: %s", chip::ErrorStr(error));
}
bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue)
{
bool retval = true;
switch (aIdentifier)
{
case kOptionProviderNodeId:
if (1 != sscanf(aValue, "%" PRIX64, &providerNodeId))
{
PrintArgError("%s: unable to parse Node ID: %s\n", aProgram, aValue);
}
break;
case kOptionProviderFabricIndex:
providerFabricIndex = static_cast<uint8_t>(strtol(aValue, NULL, 0));
if (kOptionProviderFabricIndex == 0)
{
PrintArgError("%s: Input ERROR: Fabric Index may not be zero\n", aProgram);
retval = false;
}
break;
case kOptionUdpPort:
requestorSecurePort = static_cast<uint16_t>(strtol(aValue, NULL, 0));
if (requestorSecurePort == 0)
{
PrintArgError("%s: Input ERROR: udpPort may not be zero\n", aProgram);
retval = false;
}
break;
case kOptionDiscriminator:
setupDiscriminator = static_cast<uint16_t>(strtol(aValue, NULL, 0));
if (setupDiscriminator > 0xFFF)
{
PrintArgError("%s: Input ERROR: setupDiscriminator value %s is out of range \n", aProgram, aValue);
retval = false;
}
break;
case kOptionIPAddress:
ipAddress = aValue;
ChipLogError(SoftwareUpdate, "IP Address = %s", aValue);
break;
case kOptionDelayQuery:
delayQueryTimeInSec = static_cast<uint16_t>(strtol(aValue, NULL, 0));
break;
default:
PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", aProgram, aName);
retval = false;
break;
}
return (retval);
}
void SendQueryImageCommand()
{
// Explicitly calling UpdateAddress() should not be needed once OperationalDeviceProxy can resolve IP address from node ID and
// fabric index
IPAddress ipAddr;
IPAddress::FromString(ipAddress, ipAddr);
PeerAddress addr = PeerAddress::UDP(ipAddr, CHIP_PORT);
gOperationalDeviceProxy.UpdateAddress(addr);
Server * server = &(Server::GetInstance());
OperationalDeviceProxyInitParams initParams = {
.sessionManager = &(server->GetSecureSessionManager()),
.exchangeMgr = &(server->GetExchangeManager()),
.idAllocator = &(server->GetSessionIDAllocator()),
.fabricsTable = &(server->GetFabricTable()),
};
CHIP_ERROR err = CHIP_NO_ERROR;
FabricIndex peerFabricIndex = providerFabricIndex;
gOperationalDeviceProxy.Init(providerNodeId, peerFabricIndex, initParams);
err = gOperationalDeviceProxy.Connect(&mOnConnectedCallback, &mOnConnectionFailureCallback);
}
void OnStartDelayTimerHandler(Layer * systemLayer, void * appState)
{
SendQueryImageCommand();
}
int main(int argc, char * argv[])
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint16_t unsecurePort = CHIP_UDC_PORT;
if (chip::Platform::MemoryInit() != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "FAILED to initialize memory");
return 1;
}
if (chip::DeviceLayer::PlatformMgr().InitChipStack() != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "FAILED to initialize chip stack");
return 1;
}
if (!chip::ArgParser::ParseArgs(argv[0], argc, argv, allOptions))
{
return 1;
}
chip::DeviceLayer::ConfigurationMgr().LogDeviceConfig();
// Set discriminator to user specified value
ChipLogProgress(SoftwareUpdate, "Setting discriminator to: %d", setupDiscriminator);
err = chip::DeviceLayer::ConfigurationMgr().StoreSetupDiscriminator(setupDiscriminator);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Setup discriminator setting failed with code: %s \n", chip::ErrorStr(err));
return 1;
}
// Init Data Model and CHIP App Server with user specified UDP port
Server::GetInstance().Init(nullptr, requestorSecurePort, unsecurePort);
ChipLogProgress(SoftwareUpdate, "Initializing the Application Server. Listening on UDP port %d", requestorSecurePort);
// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(chip::Credentials::Examples::GetExampleDACProvider());
// If a delay is provided, QueryImage after the timer expires
if (delayQueryTimeInSec > 0)
{
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(delayQueryTimeInSec * 1000),
OnStartDelayTimerHandler, nullptr);
}
chip::DeviceLayer::PlatformMgr().RunEventLoop();
return 0;
}