-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
OTARequesterImpl.cpp
281 lines (237 loc) · 11.1 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
/*
*
* 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/device/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::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::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 metadataForRequester);
void OnQueryImageFailure(void * context, uint8_t status);
void OnApplyUpdateResponse(void * context, uint8_t action, uint32_t delayedActionTime);
void OnApplyUpdateRequestFailure(void * context, uint8_t status);
void OnConnected(void * context, OperationalDeviceProxy * operationalDeviceProxy);
void OnConnectionFailure(void * context, OperationalDeviceProxy * operationalDeviceProxy, CHIP_ERROR error);
void OnBlockReceived(void *context, chip::bdx::TransferSession::BlockData & blockdata);
void OnTransferComplete(void *context);
void OnTransferFailed(void *context);
// TODO: Encapsulate these globals and the callbacks in some class
OperationalDeviceProxy gOperationalDeviceProxy;
ExchangeContext * exchangeCtx = nullptr;
BdxDownloader bdxDownloader;
uint8_t operationalDeviceContext; // 0 - none, 1 - QueryImage, 2 - ApplyUpdateRequest, 3 - NotifyUpdateApplied
ByteSpan otaUpdateToken;
/* Callbacks for QueryImage response */
Callback<OtaSoftwareUpdateProviderClusterQueryImageResponseCallback> mQueryImageResponseCallback(OnQueryImageResponse, nullptr);
Callback<DefaultFailureCallback> mOnQueryImageFailureCallback(OnQueryImageFailure, nullptr);
/* Callbacks for ApplyUpdateRequest response */
Callback<OtaSoftwareUpdateProviderClusterApplyUpdateRequestResponseCallback> mApplyUpdateResponseCallback(OnApplyUpdateResponse, nullptr);
Callback<DefaultFailureCallback> mOnApplyUpdateRequestFailureCallback(OnApplyUpdateRequestFailure, nullptr);
/* Callbacks for operational device proxy connect response */
Callback<OnOperationalDeviceConnected> mOnConnectedCallback(OnConnected, &operationalDeviceContext);
Callback<OnOperationalDeviceConnectionFailure> mOnConnectionFailureCallback(OnConnectionFailure, nullptr);
/* Callbacks for BDX data transfer */
Callback<OnBdxBlockReceived> mOnBlockReceived(OnBlockReceived, nullptr);
Callback<OnBdxTransferComplete> mOnTransferComplete(OnTransferComplete, nullptr);
Callback<OnBdxTransferFailed> mOnTransferFailed(OnTransferFailed, nullptr);
FabricIndex providerFabricIndex = 1;
uint16_t setupDiscriminator = CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR;
void OnQueryImageResponse(void * context, uint8_t status, uint32_t delayedActionTime, CharSpan imageURI,
uint32_t softwareVersion, CharSpan softwareVersionString, ByteSpan updateToken,
bool userConsentNeeded, ByteSpan metadataForRequester)
{
char fileDesignator[11] = { "blinky.bin" };
TransferSession::TransferInitData initOptions;
initOptions.TransferCtlFlags = chip::bdx::TransferControlFlags::kReceiverDrive;
initOptions.MaxBlockSize = 1024;
initOptions.FileDesLength = static_cast<uint16_t>(strlen(fileDesignator));
initOptions.FileDesignator = reinterpret_cast<uint8_t *>(fileDesignator);
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);
BdxDownloaderCallbacks bdxCallbacks;
bdxCallbacks.onBlockReceived = &mOnBlockReceived;
bdxCallbacks.onTransferComplete = &mOnTransferComplete;
bdxCallbacks.onTransferFailed = &mOnTransferFailed;
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));
otaUpdateToken = updateToken;
}
void OnApplyUpdateResponse(void * context, uint8_t action, uint32_t delayedActionTime)
{
OTAUpdater::GetInstance().Apply(0);
}
void OnQueryImageFailure(void * context, uint8_t status)
{
ChipLogDetail(SoftwareUpdate, "QueryImage failure response %" PRIu8, status);
}
void OnApplyUpdateRequestFailure(void * context, uint8_t status)
{
ChipLogDetail(SoftwareUpdate, "ApplyUpdateRequest failure response %" PRIu8, status);
}
void OnConnected(void * context, OperationalDeviceProxy * operationalDeviceProxy)
{
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(&(operationalDeviceProxy->GetDevice()), kOtaProviderEndpoint);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Associate() failed: %s", chip::ErrorStr(err));
return;
}
switch (*command)
{
case 1: { // QueryImage
chip::Callback::Cancelable * successCallback = mQueryImageResponseCallback.Cancel();
chip::Callback::Cancelable * failureCallback = mOnQueryImageFailureCallback.Cancel();
// These parameters are choosen 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 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.QueryImage(successCallback, failureCallback, kExampleVendorId, kExampleProductId, kExampleHWVersion,
kExampleSoftwareVersion, kExampleProtocolsSupported, exampleLocation,
kExampleClientCanConsent, metadata);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "QueryImage() failed: %s", chip::ErrorStr(err));
}
break;
}
case 2: { // ApplyUpdateRequest
chip::Callback::Cancelable * successCallback = mApplyUpdateResponseCallback.Cancel();
chip::Callback::Cancelable * failureCallback = mOnApplyUpdateRequestFailureCallback.Cancel();
constexpr uint32_t newVersion = 1;
err = cluster.ApplyUpdateRequest(successCallback, failureCallback, otaUpdateToken, newVersion);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "ApplyUpdateRequest() failed: %s", chip::ErrorStr(err));
}
break;
}
case 3: { // NotifyUpdateApplied
constexpr uint32_t softwareVersion = 1;
err = cluster.NotifyUpdateApplied(nullptr, nullptr, otaUpdateToken, softwareVersion);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "NotifyUpdateApplied() failed %s", chip::ErrorStr(err));
}
break;
}
default:
break;
}
}
void OnConnectionFailure(void * context, OperationalDeviceProxy * operationalDeviceProxy, CHIP_ERROR error)
{
ChipLogError(SoftwareUpdate, "failed to connect to: %s", chip::ErrorStr(error));
}
void OnBlockReceived(void *context, chip::bdx::TransferSession::BlockData & blockdata)
{
if (OTAUpdater::GetInstance().IsInProgress() == false)
{
OTAUpdater::GetInstance().Begin();
}
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)
{
ESP_LOGI(TAG, "Transfer Failed");
OTAUpdater::GetInstance().Abort();
}
void ConnectToProvider(const char *ipAddress, uint32_t nodeId)
{
// Explicitly calling UpdateAddress() should not be needed once OperationalDeviceProxy can resolve IP address from node ID and
NodeId providerNodeId = nodeId;
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);
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 = 1;
ConnectToProvider(ipAddress, nodeId);
}
void OTARequesterImpl::SendApplyUpdateRequestCommand(const char *ipAddress, uint32_t nodeId)
{
operationalDeviceContext = 2;
ConnectToProvider(ipAddress, nodeId);
}
void OTARequesterImpl::SendNotifyUpdateAppliedCommand(const char *ipAddress, uint32_t nodeId)
{
operationalDeviceContext = 2;
ConnectToProvider(ipAddress, nodeId);
}