forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOTAProviderDelegateBridge.cpp
473 lines (388 loc) · 21.4 KB
/
OTAProviderDelegateBridge.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* Copyright (c) 2023 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 "OTAProviderDelegateBridge.h"
#include <app/MessageDef/StatusIB.h>
#include <app/clusters/ota-provider/ota-provider.h>
#include <app/data-model/List.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>
#include <string>
using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
using namespace chip::app::Clusters::OtaSoftwareUpdateProvider;
using namespace chip::app::Clusters::OtaSoftwareUpdateProvider::Commands;
constexpr EndpointId kOtaProviderEndpoint = 0;
// Time in seconds after which the requestor should retry calling query image if
// busy status is receieved. The spec minimum is 2 minutes, but in practice OTA
// generally takes a lot longer than that and devices only retry a few times
// before giving up. Default to 10 minutes for now, until we have a better
// system of computing an expected completion time for the currently-running
// OTA.
constexpr uint32_t kDelayedActionTimeSeconds = 600;
OTAProviderDelegateBridge::~OTAProviderDelegateBridge()
{
mBdxOTASender->ResetState();
Clusters::OTAProvider::SetDelegate(kOtaProviderEndpoint, nullptr);
mBdxOTASender = nullptr;
}
CHIP_ERROR OTAProviderDelegateBridge::Init(chip::System::Layer * systemLayer, chip::Messaging::ExchangeManager * exchangeManager,
jobject otaProviderDelegate)
{
ReturnLogErrorOnFailure(mOtaProviderDelegate.Init(otaProviderDelegate));
Clusters::OTAProvider::SetDelegate(kOtaProviderEndpoint, this);
mBdxOTASender = std::make_unique<BdxOTASender>(mOtaProviderDelegate.ObjectRef());
return mBdxOTASender->Init(systemLayer, exchangeManager);
}
void OTAProviderDelegateBridge::Shutdown()
{
VerifyOrReturn(mBdxOTASender != nullptr, ChipLogError(Controller, "BdxOTASender is null"));
CHIP_ERROR err = mBdxOTASender->Shutdown();
if (err != CHIP_NO_ERROR)
{
ChipLogProgress(Controller, "OTAProviderDelegateBridge-Shutdown : %" CHIP_ERROR_FORMAT, err.Format());
}
}
void GenerateUpdateToken(uint8_t * buf, size_t bufSize)
{
for (size_t i = 0; i < bufSize; ++i)
{
buf[i] = chip::Crypto::GetRandU8();
}
}
void OTAProviderDelegateBridge::sendOTAQueryFailure(uint8_t status)
{
VerifyOrReturn(mOtaProviderDelegate.HasValidObjectRef(), ChipLogError(Controller, "mOtaProviderDelegate is null"));
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
jobject otaProviderDelegate = mOtaProviderDelegate.ObjectRef();
jmethodID handleOTAQueryFailureMethod;
CHIP_ERROR err = JniReferences::GetInstance().FindMethod(env, otaProviderDelegate, "handleOTAQueryFailure", "(I)V",
&handleOTAQueryFailureMethod);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find handleOTAQueryFailure method"));
env->CallVoidMethod(otaProviderDelegate, handleOTAQueryFailureMethod, static_cast<jint>(status));
if (env->ExceptionCheck())
{
ChipLogError(Support, "Exception in call java method");
env->ExceptionDescribe();
env->ExceptionClear();
return;
}
return;
}
void OTAProviderDelegateBridge::HandleQueryImage(CommandHandler * commandObj, const ConcreteCommandPath & commandPath,
const QueryImage::DecodableType & commandData)
{
assertChipStackLockedByCurrentThread();
VerifyOrReturn(mOtaProviderDelegate.HasValidObjectRef(),
commandObj->AddStatus(commandPath, Protocols::InteractionModel::Status::Failure));
VerifyOrReturn(mBdxOTASender != nullptr, commandObj->AddStatus(commandPath, Protocols::InteractionModel::Status::Failure));
CHIP_ERROR err = CHIP_NO_ERROR;
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturn(env != nullptr, ChipLogError(Controller, "Could not get JNIEnv for current thread"));
chip::JniLocalReferenceScope scope(env);
jobject otaProviderDelegate = nullptr;
jmethodID handleQueryImageMethod = nullptr;
jobject boxedHardwareVersion = nullptr;
jobject boxedLocation = nullptr;
jobject boxedRequestorCanConsent = nullptr;
jobject boxedMetadataForProvider = nullptr;
jobject jResponse = nullptr;
jobject boxedDelayedActionTime = nullptr;
jobject boxedUserConsentNeeded = nullptr;
jmethodID getSoftwareVersionMethod = nullptr;
jmethodID getSoftwareVersionStringMethod = nullptr;
jmethodID getFilePathMethod = nullptr;
jmethodID getStatushMethod = nullptr;
jmethodID getDelayedActionTimeMethod = nullptr;
jmethodID getUserConsentNeededMethod = nullptr;
jobject jSoftwareVersion = nullptr;
jstring jSoftwareVersionString = nullptr;
jstring jFilePath = nullptr;
jint jStatus = 0;
uint8_t status = 0;
bool hasUpdate = false;
Commands::QueryImageResponse::Type response;
response.status = OTAQueryStatus::kNotAvailable;
char uriBuffer[kMaxBDXURILen];
MutableCharSpan uri(uriBuffer);
NodeId nodeId = commandObj->GetSubjectDescriptor().subject;
FabricIndex fabricIndex = commandObj->GetAccessingFabricIndex();
ScopedNodeId ourNodeId = commandObj->GetExchangeContext()->GetSessionHandle()->AsSecureSession()->GetLocalScopedNodeId();
VendorId vendorId = commandData.vendorID;
uint16_t productId = commandData.productID;
uint32_t softwareVersion = commandData.softwareVersion;
Optional<uint16_t> hardwareVersion = commandData.hardwareVersion;
Optional<chip::CharSpan> location = commandData.location;
Optional<bool> requestorCanConsent = commandData.requestorCanConsent;
Optional<chip::ByteSpan> metadataForProvider = commandData.metadataForProvider;
bool isBDXProtocolSupported = false;
auto iterator = commandData.protocolsSupported.begin();
while (iterator.Next())
{
OTADownloadProtocol protocol = iterator.GetValue();
if (protocol == OTADownloadProtocol::kBDXSynchronous)
{
isBDXProtocolSupported = true;
break;
}
}
ConcreteCommandPath cachedCommandPath(commandPath.mEndpointId, commandPath.mClusterId, commandPath.mCommandId);
// The logic we are following here is if none of the protocols supported by the requestor are supported by us, we
// can't transfer the image even if we had an image available and we would return a Protocol Not Supported status.
// Assumption here is the requestor would send us a list of all the protocols it supports. If one/more of the
// protocols supported by the requestor are supported by us, we check if an image is not available due to various
// reasons - image not available, delegate reporting busy, we will respond with the status in the delegate response.
// If update is available, we try to prepare for transfer and build the uri in the response with a status of Image
// Available
// If the protocol requested is not supported, return status - Protocol Not Supported
if (!isBDXProtocolSupported)
{
response.status = OTAQueryStatus::kDownloadProtocolNotSupported;
err = CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
ExitNow();
}
otaProviderDelegate = mOtaProviderDelegate.ObjectRef();
err = JniReferences::GetInstance().FindMethod(env, otaProviderDelegate, "handleQueryImage",
"(IIJLjava/lang/Integer;Ljava/lang/String;Ljava/lang/Boolean;[B)Lchip/"
"devicecontroller/OTAProviderDelegate$QueryImageResponse;",
&handleQueryImageMethod);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find handleQueryImage method"));
if (hardwareVersion.HasValue())
{
chip::JniReferences::GetInstance().CreateBoxedObject<jint>(
"java/lang/Integer", "(I)V", static_cast<jint>(hardwareVersion.Value()), boxedHardwareVersion);
}
if (location.HasValue())
{
boxedLocation = env->NewStringUTF(std::string(location.Value().data(), location.Value().size()).c_str());
}
if (requestorCanConsent.HasValue())
{
chip::JniReferences::GetInstance().CreateBoxedObject<jboolean>(
"java/lang/Boolean", "(Z)V", requestorCanConsent.Value() ? JNI_TRUE : JNI_FALSE, boxedRequestorCanConsent);
}
if (metadataForProvider.HasValue())
{
jbyteArray boxedMetadataForProviderByteArray = env->NewByteArray(static_cast<jsize>(metadataForProvider.Value().size()));
env->SetByteArrayRegion(boxedMetadataForProviderByteArray, 0, static_cast<jsize>(metadataForProvider.Value().size()),
reinterpret_cast<const jbyte *>(metadataForProvider.Value().data()));
boxedMetadataForProvider = boxedMetadataForProviderByteArray;
}
jResponse = env->CallObjectMethod(otaProviderDelegate, handleQueryImageMethod, static_cast<jint>(vendorId),
static_cast<jint>(productId), static_cast<jlong>(softwareVersion), boxedHardwareVersion,
boxedLocation, boxedRequestorCanConsent, boxedMetadataForProvider);
if (env->ExceptionCheck())
{
ChipLogError(Support, "Exception in call java method");
env->ExceptionDescribe();
env->ExceptionClear();
err = CHIP_JNI_ERROR_EXCEPTION_THROWN;
ExitNow();
}
hasUpdate = (jResponse != nullptr);
// If update is not available, return the delegate response
if (!hasUpdate)
{
response.status = OTAQueryStatus::kNotAvailable;
err = CHIP_ERROR_INTERNAL;
ExitNow();
}
err = JniReferences::GetInstance().FindMethod(env, jResponse, "getSoftwareVersion", "()Ljava/lang/Long;",
&getSoftwareVersionMethod);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find getSoftwareVersion method"));
err = JniReferences::GetInstance().FindMethod(env, jResponse, "getSoftwareVersionString", "()Ljava/lang/String;",
&getSoftwareVersionStringMethod);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find getSoftwareVersionString method"));
err = JniReferences::GetInstance().FindMethod(env, jResponse, "getFilePath", "()Ljava/lang/String;", &getFilePathMethod);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find getFilePath method"));
err = JniReferences::GetInstance().FindMethod(env, jResponse, "getStatus", "()I", &getStatushMethod);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find getStatus method"));
err = JniReferences::GetInstance().FindMethod(env, jResponse, "getDelayedActionTime", "()Ljava/lang/Long;",
&getDelayedActionTimeMethod);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find getDelayedActionTime method"));
err = JniReferences::GetInstance().FindMethod(env, jResponse, "getUserConsentNeeded", "()Ljava/lang/Boolean;",
&getUserConsentNeededMethod);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find getUserConsentNeeded method"));
jStatus = env->CallIntMethod(jResponse, getStatushMethod);
boxedUserConsentNeeded = env->CallObjectMethod(jResponse, getUserConsentNeededMethod);
// UserConsentNeeded Field, if present, SHALL only be interpreted if the OTA Requestor had previously indicated a value of True
// in the RequestorCanConsent field of the QueryImageRequest.
if (boxedUserConsentNeeded != nullptr)
{
jboolean userConsentNeeded = JniReferences::GetInstance().BooleanToPrimitive(boxedUserConsentNeeded);
response.userConsentNeeded.SetValue(userConsentNeeded == JNI_TRUE);
}
else
{
response.userConsentNeeded.SetValue(0);
}
status = static_cast<uint8_t>(jStatus);
if (status == static_cast<uint8_t>(OTAQueryStatus::kNotAvailable))
{
err = CHIP_ERROR_INTERNAL;
response.status = OTAQueryStatus::kNotAvailable;
ExitNow();
}
else if (status == static_cast<uint8_t>(OTAQueryStatus::kBusy))
{
err = CHIP_ERROR_BUSY;
response.status = OTAQueryStatus::kBusy;
boxedDelayedActionTime = env->CallObjectMethod(jResponse, getDelayedActionTimeMethod);
response.delayedActionTime.SetValue(
static_cast<uint32_t>(JniReferences::GetInstance().LongToPrimitive(boxedDelayedActionTime)));
ExitNow();
}
jSoftwareVersion = env->CallObjectMethod(jResponse, getSoftwareVersionMethod);
jSoftwareVersionString = (jstring) env->CallObjectMethod(jResponse, getSoftwareVersionStringMethod);
jFilePath = (jstring) env->CallObjectMethod(jResponse, getFilePathMethod);
{
response.status = OTAQueryStatus::kUpdateAvailable;
if (jSoftwareVersion != nullptr)
{
response.softwareVersion.SetValue(
static_cast<uint32_t>(JniReferences::GetInstance().LongToPrimitive(jSoftwareVersion)));
}
JniUtfString jniSoftwareVersionString(env, jSoftwareVersionString);
if (jniSoftwareVersionString.c_str() != nullptr)
{
response.softwareVersionString.SetValue(jniSoftwareVersionString.charSpan());
}
JniUtfString jniFilePath(env, jFilePath);
GenerateUpdateToken(mToken, kUpdateTokenLen);
response.updateToken.SetValue(chip::ByteSpan(mToken, kUpdateTokenLen));
err = mBdxOTASender->PrepareForTransfer(fabricIndex, nodeId);
if (CHIP_NO_ERROR != err)
{
// Handle busy error separately as we have a query image response status that maps to busy
if (err == CHIP_ERROR_BUSY)
{
ChipLogError(Controller, "Responding with Busy due to being in the middle of handling another BDX transfer");
response.status = OTAQueryStatus::kBusy;
response.delayedActionTime.SetValue(kDelayedActionTimeSeconds);
// We do not reset state when we get the busy error because that means we are locked in a BDX transfer
// session with another requestor when we get this query image request. We do not want to interrupt the
// ongoing transfer instead just respond to the second requestor with a busy status and a delayedActionTime
// in which the requestor can retry.
ExitNow();
}
LogErrorOnFailure(err);
commandObj->AddStatus(cachedCommandPath, StatusIB(err).mStatus);
// We need to reset state here to clean up any initialization we might have done including starting the BDX
// timeout timer while preparing for transfer if any failure occurs afterwards.
mBdxOTASender->ResetState();
return;
}
err = bdx::MakeURI(ourNodeId.GetNodeId(), jniFilePath.c_str() != nullptr ? jniFilePath.charSpan() : CharSpan(), uri);
if (CHIP_NO_ERROR != err)
{
LogErrorOnFailure(err);
commandObj->AddStatus(cachedCommandPath, StatusIB(err).mStatus);
mBdxOTASender->ResetState();
return;
}
response.imageURI.SetValue(uri);
commandObj->AddResponse(cachedCommandPath, response);
}
return;
exit:
ChipLogError(Controller, "OTA Query Failure : %u, %" CHIP_ERROR_FORMAT, static_cast<uint8_t>(response.status), err.Format());
commandObj->AddResponse(cachedCommandPath, response);
sendOTAQueryFailure(static_cast<uint8_t>(response.status));
}
void OTAProviderDelegateBridge::HandleApplyUpdateRequest(CommandHandler * commandObj, const ConcreteCommandPath & commandPath,
const ApplyUpdateRequest::DecodableType & commandData)
{
assertChipStackLockedByCurrentThread();
CHIP_ERROR err = CHIP_NO_ERROR;
NodeId nodeId = kUndefinedNodeId;
ConcreteCommandPath cachedCommandPath(commandPath.mEndpointId, commandPath.mClusterId, commandPath.mCommandId);
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
jobject otaProviderDelegate = nullptr;
jobject jResponse = nullptr;
jmethodID handleApplyUpdateRequestMethod;
jmethodID getActionMethod;
jmethodID getDelayedActionTimeMethod;
chip::JniLocalReferenceScope scope(env);
Commands::ApplyUpdateResponse::Type response;
Commands::ApplyUpdateResponse::Type errorResponse;
errorResponse.action = ApplyUpdateActionEnum::kDiscontinue;
errorResponse.delayedActionTime = 0;
jint jAction = 0;
jlong jDelayedActionTime = 0;
VerifyOrReturn(mOtaProviderDelegate.HasValidObjectRef(),
commandObj->AddStatus(commandPath, Protocols::InteractionModel::Status::Failure));
nodeId = commandObj->GetSubjectDescriptor().subject;
otaProviderDelegate = mOtaProviderDelegate.ObjectRef();
err = JniReferences::GetInstance().FindMethod(env, otaProviderDelegate, "handleApplyUpdateRequest",
"(JJ)Lchip/devicecontroller/OTAProviderDelegate$ApplyUpdateResponse;",
&handleApplyUpdateRequestMethod);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find handleApplyUpdateRequest method"));
jResponse = env->CallObjectMethod(otaProviderDelegate, handleApplyUpdateRequestMethod, static_cast<jlong>(nodeId),
static_cast<jlong>(commandData.newVersion));
if (env->ExceptionCheck())
{
ChipLogError(Support, "Exception in call java method");
env->ExceptionDescribe();
env->ExceptionClear();
err = CHIP_JNI_ERROR_EXCEPTION_THROWN;
ExitNow();
}
err = JniReferences::GetInstance().FindMethod(env, jResponse, "getAction", "()I", &getActionMethod);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find getAction method"));
err = JniReferences::GetInstance().FindMethod(env, jResponse, "getDelayedActionTime", "()J", &getDelayedActionTimeMethod);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find getDelayedActionTime method"));
jAction = env->CallIntMethod(jResponse, getActionMethod);
jDelayedActionTime = env->CallLongMethod(jResponse, getDelayedActionTimeMethod);
response.action = static_cast<OTAApplyUpdateAction>(jAction);
response.delayedActionTime = static_cast<uint32_t>(jDelayedActionTime);
commandObj->AddResponse(cachedCommandPath, response);
return;
exit:
ChipLogError(Controller, "Apply Update Request Failure : %" CHIP_ERROR_FORMAT, err.Format());
commandObj->AddResponse(cachedCommandPath, errorResponse);
}
void OTAProviderDelegateBridge::HandleNotifyUpdateApplied(CommandHandler * commandObj, const ConcreteCommandPath & commandPath,
const NotifyUpdateApplied::DecodableType & commandData)
{
assertChipStackLockedByCurrentThread();
CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrReturn(mOtaProviderDelegate.HasValidObjectRef(),
commandObj->AddStatus(commandPath, Protocols::InteractionModel::Status::Failure));
NodeId nodeId = commandObj->GetSubjectDescriptor().subject;
ConcreteCommandPath cachedCommandPath(commandPath.mEndpointId, commandPath.mClusterId, commandPath.mCommandId);
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
jobject otaProviderDelegate = mOtaProviderDelegate.ObjectRef();
jmethodID handleNotifyUpdateAppliedMethod;
err = JniReferences::GetInstance().FindMethod(env, otaProviderDelegate, "handleNotifyUpdateApplied", "(J)V",
&handleNotifyUpdateAppliedMethod);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find handleNotifyUpdateApplied method"));
env->CallVoidMethod(otaProviderDelegate, handleNotifyUpdateAppliedMethod, static_cast<jlong>(nodeId));
if (env->ExceptionCheck())
{
ChipLogError(Support, "Exception in call java method");
env->ExceptionDescribe();
env->ExceptionClear();
err = CHIP_JNI_ERROR_EXCEPTION_THROWN;
ExitNow();
}
commandObj->AddStatus(cachedCommandPath, Protocols::InteractionModel::Status::Success);
return;
exit:
ChipLogError(Controller, "Notify Update Applied Failure : %" CHIP_ERROR_FORMAT, err.Format());
commandObj->AddStatus(commandPath, Protocols::InteractionModel::Status::Failure);
}