-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathOTARequesterImpl.cpp
328 lines (282 loc) · 12.6 KB
/
OTARequesterImpl.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
/*
*
* 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-common/zap-generated/cluster-objects.h>
#include <app/OperationalDeviceProxy.h>
#include <app/server/Server.h>
#include <platform/CHIPDeviceLayer.h>
#include <zap-generated/CHIPClientCallbacks.h>
#include <zap-generated/CHIPClusters.h>
#include <esp_log.h>
#include "BDXDownloader.h"
#include "OTARequesterImpl.h"
#include "OTAUpdater.h"
#define TAG "OTARequesterImpl"
using chip::ByteSpan;
using chip::CharSpan;
using chip::DeviceProxy;
using chip::EndpointId;
using chip::FabricIndex;
using chip::NodeId;
using chip::OnDeviceConnected;
using chip::OnDeviceConnectionFailure;
using chip::Optional;
using chip::PeerId;
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::Messaging;
using namespace chip::app::Clusters::OtaSoftwareUpdateProvider::Commands;
void OnQueryImageResponse(void * context, const QueryImageResponse::DecodableType & response);
void OnQueryImageFailure(void * context, EmberAfStatus status);
void OnApplyUpdateResponse(void * context, const ApplyUpdateResponse::DecodableType & response);
void OnApplyUpdateRequestFailure(void * context, EmberAfStatus status);
void OnConnected(void * context, chip::DeviceProxy * deviceProxy);
void OnConnectionFailure(void * context, NodeId deviceId, CHIP_ERROR error);
void OnBlockReceived(void * context, const chip::bdx::TransferSession::BlockData & blockdata);
void OnTransferComplete(void * context);
void OnTransferFailed(void * context, BdxDownloaderErrorTypes status);
enum OTARequestorCommands
{
kCommandQueryImage = 0,
kCommandApplyUpdateRequest,
kCommandNotifyUpdateApplied,
};
namespace {
// TODO: Encapsulate these globals and the callbacks in some class
ExchangeContext * exchangeCtx = nullptr;
BdxDownloader bdxDownloader;
enum OTARequestorCommands operationalDeviceContext;
constexpr uint8_t kMaxUpdateTokenLen = 32; // must be between 8 and 32
uint8_t otaUpdateToken[kMaxUpdateTokenLen] = { 0 };
uint8_t otaUpdateTokenLen = 0;
/* Callbacks for operational device proxy connect response */
Callback<OnDeviceConnected> onConnectedCallback(OnConnected, &operationalDeviceContext);
Callback<OnDeviceConnectionFailure> onConnectionFailureCallback(OnConnectionFailure, nullptr);
/* Callbacks for BDX data transfer */
Callback<OnBdxBlockReceived> onBlockReceivedCallback(OnBlockReceived, nullptr);
Callback<OnBdxTransferComplete> onTransferCompleteCallback(OnTransferComplete, nullptr);
Callback<OnBdxTransferFailed> onTransferFailedCallback(OnTransferFailed, nullptr);
FabricIndex providerFabricIndex = 1;
} // namespace
void OnQueryImageResponse(void * context, const QueryImageResponse::DecodableType & response)
{
ChipLogDetail(SoftwareUpdate, "QueryImageResponse responded with action %" PRIu8, response.status);
if (response.updateToken.HasValue())
{
otaUpdateTokenLen = response.updateToken.Value().size();
memcpy(otaUpdateToken, response.updateToken.Value().data(), otaUpdateTokenLen);
}
if (response.imageURI.HasValue() == false)
{
ChipLogError(BDX, "OTA image URI missing");
return;
}
// TODO: Handle image URI for protocol other than bdx
// Ignore the first 23 "bdx://<NodeId>/"
char fileDesignator[128]; // 128 is arbitrary value
memset(fileDesignator, 0, sizeof(fileDesignator));
size_t fileDesignatorLength = response.imageURI.Value().size() - 23 + 1; // + 1 for \0
if ((response.imageURI.Value().size() - 23) > sizeof(fileDesignator))
{
fileDesignatorLength = sizeof(fileDesignator);
}
strlcpy(fileDesignator, response.imageURI.Value().data() + 23, fileDesignatorLength);
TransferSession::TransferInitData initOptions;
initOptions.TransferCtlFlags = chip::bdx::TransferControlFlags::kReceiverDrive;
initOptions.MaxBlockSize = 1024;
initOptions.FileDesLength = static_cast<uint16_t>(fileDesignatorLength);
initOptions.FileDesignator = reinterpret_cast<const uint8_t *>(fileDesignator);
chip::OperationalDeviceProxy * operationalDeviceProxy = Server::GetInstance().GetOperationalDeviceProxy();
if (operationalDeviceProxy != nullptr)
{
chip::Messaging::ExchangeManager * exchangeMgr = operationalDeviceProxy->GetExchangeManager();
chip::Optional<chip::SessionHandle> session = operationalDeviceProxy->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;
}
}
else
{
ChipLogError(BDX, "Failed to get OperationalDeviceProxy");
return;
}
bdxDownloader.SetInitialExchange(exchangeCtx);
BdxDownloaderCallbacks bdxCallbacks;
bdxCallbacks.onBlockReceived = &onBlockReceivedCallback;
bdxCallbacks.onTransferComplete = &onTransferCompleteCallback;
bdxCallbacks.onTransferFailed = &onTransferFailedCallback;
bdxDownloader.SetCallbacks(bdxCallbacks);
// 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 OnApplyUpdateResponse(void * context, const ApplyUpdateResponse::DecodableType & response)
{
ChipLogDetail(SoftwareUpdate, "ApplyUpdateResponse responded with action %" PRIu8, response.action);
// Providing arbitrary value
OTAUpdater::GetInstance().Apply(3);
}
void OnQueryImageFailure(void * context, EmberAfStatus status)
{
ChipLogDetail(SoftwareUpdate, "QueryImage failure response %" PRIu8, status);
}
void OnApplyUpdateRequestFailure(void * context, EmberAfStatus status)
{
ChipLogDetail(SoftwareUpdate, "ApplyUpdateRequest failure response %" PRIu8, status);
}
void OnConnected(void * context, chip::DeviceProxy * deviceProxy)
{
ChipLogDetail(SoftwareUpdate, "Callback OnConnected");
uint8_t * command = reinterpret_cast<uint8_t *>(context);
chip::Controller::OtaSoftwareUpdateProviderCluster cluster;
constexpr EndpointId kOtaProviderEndpoint = 0;
CHIP_ERROR err = cluster.Associate(deviceProxy, kOtaProviderEndpoint);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Associate() failed: %s", chip::ErrorStr(err));
return;
}
switch (*command)
{
case kCommandQueryImage: {
// These parameters are chosen arbitrarily
constexpr VendorId kExampleVendorId = VendorId::Common;
constexpr uint16_t kExampleProductId = CONFIG_DEVICE_PRODUCT_ID;
constexpr uint16_t kExampleHWVersion = 0;
constexpr uint16_t kExampleSoftwareVersion = 0;
constexpr EmberAfOTADownloadProtocol kExampleProtocolsSupported[] = { EMBER_ZCL_OTA_DOWNLOAD_PROTOCOL_BDX_SYNCHRONOUS };
const char locationBuf[] = { 'U', 'S' };
CharSpan exampleLocation(locationBuf);
constexpr bool kExampleClientCanConsent = false;
ByteSpan metadata;
QueryImage::Type args;
args.vendorId = kExampleVendorId;
args.productId = kExampleProductId;
args.softwareVersion = kExampleSoftwareVersion;
args.protocolsSupported = kExampleProtocolsSupported;
args.hardwareVersion.Emplace(kExampleHWVersion);
args.location.Emplace(exampleLocation);
args.requestorCanConsent.Emplace(kExampleClientCanConsent);
args.metadataForProvider.Emplace(metadata);
err = cluster.InvokeCommand(args, nullptr, OnQueryImageResponse, OnQueryImageFailure);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "QueryImage() failed: %" CHIP_ERROR_FORMAT, err.Format());
}
break;
}
case kCommandApplyUpdateRequest: {
constexpr uint32_t kNewVersion = 1;
ApplyUpdateRequest::Type args;
args.updateToken = ByteSpan(otaUpdateToken, otaUpdateTokenLen);
args.newVersion = kNewVersion;
err = cluster.InvokeCommand(args, nullptr, OnApplyUpdateResponse, OnApplyUpdateRequestFailure);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "ApplyUpdateRequest() failed: %s", chip::ErrorStr(err));
}
break;
}
default:
break;
}
}
void OnConnectionFailure(void * context, NodeId deviceId, CHIP_ERROR error)
{
ChipLogError(SoftwareUpdate, "failed to connect to 0x%" PRIX64 ": %" CHIP_ERROR_FORMAT, deviceId, error.Format());
}
void OnBlockReceived(void * context, const chip::bdx::TransferSession::BlockData & blockdata)
{
if (OTAUpdater::GetInstance().IsInProgress() == false)
{
OTAUpdater::GetInstance().Begin();
}
// TODO: Process/skip the Matter OTA header
OTAUpdater::GetInstance().Write(reinterpret_cast<const void *>(blockdata.Data), blockdata.Length);
}
void OnTransferComplete(void * context)
{
ESP_LOGI(TAG, "Transfer complete!");
OTAUpdater::GetInstance().End();
}
void OnTransferFailed(void * context, BdxDownloaderErrorTypes status)
{
ESP_LOGI(TAG, "Transfer Failed, status:%x", status);
OTAUpdater::GetInstance().Abort();
}
void ConnectToProvider(const char * ipAddress, uint32_t nodeId)
{
NodeId providerNodeId = nodeId;
chip::OperationalDeviceProxy * operationalDeviceProxy = Server::GetInstance().GetOperationalDeviceProxy();
if (operationalDeviceProxy != nullptr && operationalDeviceProxy->GetDeviceId() != providerNodeId)
{
operationalDeviceProxy->Disconnect();
delete operationalDeviceProxy;
operationalDeviceProxy = nullptr;
}
if (operationalDeviceProxy == nullptr)
{
Server * server = &(Server::GetInstance());
chip::FabricInfo * fabric = server->GetFabricTable().FindFabricWithIndex(providerFabricIndex);
chip::DeviceProxyInitParams initParams = {
.sessionManager = &(server->GetSecureSessionManager()),
.exchangeMgr = &(server->GetExchangeManager()),
.idAllocator = &(server->GetSessionIDAllocator()),
.fabricInfo = fabric,
// TODO: Determine where this should be instantiated
.imDelegate = chip::Platform::New<chip::Controller::DeviceControllerInteractionModelDelegate>(),
};
PeerId peerID = fabric->GetPeerId();
peerID.SetNodeId(providerNodeId);
operationalDeviceProxy = new chip::OperationalDeviceProxy(initParams, peerID);
server->SetOperationalDeviceProxy(operationalDeviceProxy);
// Explicitly calling UpdateDeviceData() 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);
uint32_t idleInterval;
uint32_t activeInterval;
operationalDeviceProxy->GetMRPIntervals(idleInterval, activeInterval);
operationalDeviceProxy->UpdateDeviceData(addr, idleInterval, activeInterval);
}
CHIP_ERROR err = operationalDeviceProxy->Connect(&onConnectedCallback, &onConnectionFailureCallback);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Cannot establish connection to peer device: %" CHIP_ERROR_FORMAT, err.Format());
}
}
void OTARequesterImpl::SendQueryImageCommand(const char * ipAddress, uint32_t nodeId)
{
operationalDeviceContext = kCommandQueryImage;
ConnectToProvider(ipAddress, nodeId);
}
void OTARequesterImpl::SendApplyUpdateRequestCommand(const char * ipAddress, uint32_t nodeId)
{
operationalDeviceContext = kCommandApplyUpdateRequest;
ConnectToProvider(ipAddress, nodeId);
}