-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OTA Software Update in Darwin Tool Framework (#20767)
- Loading branch information
Showing
11 changed files
with
570 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
examples/darwin-framework-tool/commands/provider/Commands.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "OTASoftwareUpdateInteractive.h" | ||
|
||
void registerClusterOtaSoftwareUpdateProviderInteractive(Commands & commands) | ||
{ | ||
|
||
const char * clusterName = "OtaSoftwareUpdateApp"; | ||
|
||
commands_list clusterCommands = { | ||
make_unique<OTASoftwareUpdateSetFilePath>(), // | ||
make_unique<OTASoftwareUpdateSetStatus>(), // | ||
|
||
}; | ||
|
||
commands.Register(clusterName, clusterCommands); | ||
} |
58 changes: 58 additions & 0 deletions
58
examples/darwin-framework-tool/commands/provider/OTAProviderDelegate.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* | ||
* Copyright (c) 2022 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. | ||
*/ | ||
#pragma once | ||
#import <Matter/Matter.h> | ||
|
||
typedef NS_ENUM(uint8_t, UserConsentState) { | ||
OTAProviderUserGranted = 0x00, | ||
OTAProviderUserObtaining = 0x01, | ||
OTAProviderUserDenied = 0x02, | ||
OTAProviderUserUnknown = 0x03, | ||
}; | ||
|
||
@interface DeviceSoftwareVersionModelData : MTROtaSoftwareUpdateProviderClusterQueryImageParams | ||
@property BOOL softwareVersionValid; | ||
@property (strong, nonatomic, nullable) NSNumber * cDVersionNumber; | ||
@property (strong, nonatomic, nullable) NSNumber * minApplicableSoftwareVersion; | ||
@property (strong, nonatomic, nullable) NSNumber * maxApplicableSoftwareVersion; | ||
@property (strong, nonatomic, nullable) NSString * otaURL; | ||
@end | ||
|
||
@interface DeviceSoftwareVersionModel : MTROtaSoftwareUpdateProviderClusterQueryImageResponseParams | ||
@property (strong, nonatomic, nullable) DeviceSoftwareVersionModelData * deviceModelData; | ||
- (NSComparisonResult)CompareSoftwareVersions:(DeviceSoftwareVersionModel * _Nullable)otherObject; | ||
@end | ||
|
||
@interface OTAProviderDelegate : NSObject <MTROTAProviderDelegate> | ||
- (void)handleQueryImage:(MTROtaSoftwareUpdateProviderClusterQueryImageParams * _Nonnull)params | ||
completionHandler:(void (^_Nonnull)(MTROtaSoftwareUpdateProviderClusterQueryImageResponseParams * _Nullable data, | ||
NSError * _Nullable error))completionHandler; | ||
|
||
- (void)handleApplyUpdateRequest:(MTROtaSoftwareUpdateProviderClusterApplyUpdateRequestParams * _Nonnull)params | ||
completionHandler:(void (^_Nonnull)(MTROtaSoftwareUpdateProviderClusterApplyUpdateResponseParams * _Nullable data, | ||
NSError * _Nullable error))completionHandler; | ||
|
||
- (void)handleNotifyUpdateApplied:(MTROtaSoftwareUpdateProviderClusterNotifyUpdateAppliedParams * _Nonnull)params | ||
completionHandler:(StatusCompletion _Nonnull)completionHandler; | ||
|
||
@property (strong, nonatomic, nullable) NSArray<DeviceSoftwareVersionModel *> * candidates; | ||
@property (strong, nonatomic, nullable) DeviceSoftwareVersionModel * selectedCandidate; | ||
@property (strong, nonatomic, nullable) NSNumber * nodeID; | ||
@property (nonatomic, readwrite) MTROtaSoftwareUpdateProviderOTAQueryStatus queryImageStatus; | ||
@property (nonatomic, readwrite) UserConsentState userConsentState; | ||
|
||
@end |
184 changes: 184 additions & 0 deletions
184
examples/darwin-framework-tool/commands/provider/OTAProviderDelegate.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* | ||
* | ||
* Copyright (c) 2022 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 "OTAProviderDelegate.h" | ||
#import <Matter/Matter.h> | ||
|
||
constexpr uint8_t kUpdateTokenLen = 32; | ||
|
||
@interface OTAProviderDelegate () | ||
@property NSString * mOTAFilePath; | ||
@property DeviceSoftwareVersionModel * candidate; | ||
@end | ||
|
||
@implementation OTAProviderDelegate | ||
|
||
- (instancetype)init | ||
{ | ||
if (self = [super init]) { | ||
_nodeID = @(0); | ||
_selectedCandidate = [[DeviceSoftwareVersionModel alloc] init]; | ||
_userConsentState = OTAProviderUserUnknown; | ||
} | ||
return self; | ||
} | ||
|
||
// TODO: When BDX is added to Matter.framework, update to initialize | ||
// it when there is an update available. | ||
- (void)handleQueryImage:(MTROtaSoftwareUpdateProviderClusterQueryImageParams * _Nonnull)params | ||
completionHandler:(void (^_Nonnull)(MTROtaSoftwareUpdateProviderClusterQueryImageResponseParams * _Nullable data, | ||
NSError * _Nullable error))completionHandler | ||
{ | ||
NSError * error; | ||
_selectedCandidate.status = @(MTROtaSoftwareUpdateProviderOTAQueryStatusNotAvailable); | ||
if (![params.protocolsSupported containsObject:@(MTROtaSoftwareUpdateProviderOTADownloadProtocolBDXSynchronous)]) { | ||
_selectedCandidate.status = @(MTROtaSoftwareUpdateProviderOTAQueryStatusDownloadProtocolNotSupported); | ||
error = | ||
[[NSError alloc] initWithDomain:@"OTAProviderDomain" | ||
code:MTRErrorCodeGeneralError | ||
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Protocol is not supported.", nil) }]; | ||
completionHandler(_selectedCandidate, error); | ||
return; | ||
} | ||
|
||
if ([self SelectOTACandidate:params.vendorId rPID:params.productId rSV:params.softwareVersion]) { | ||
_selectedCandidate.status = @(MTROtaSoftwareUpdateProviderOTAQueryStatusUpdateAvailable); | ||
_selectedCandidate.updateToken = [self generateUpdateToken]; | ||
if (params.requestorCanConsent.integerValue == 1) { | ||
_selectedCandidate.userConsentNeeded | ||
= (_userConsentState == OTAProviderUserUnknown || _userConsentState == OTAProviderUserDenied) ? @(1) : @(0); | ||
NSLog(@"User Consent Needed: %@", _selectedCandidate.userConsentNeeded); | ||
} else { | ||
NSLog(@"Requestor cannot obtain user consent. Our State: %hhu", _userConsentState); | ||
switch (_userConsentState) { | ||
case OTAProviderUserGranted: | ||
NSLog(@"User Consent Granted"); | ||
_queryImageStatus = MTROtaSoftwareUpdateProviderOTAQueryStatusUpdateAvailable; | ||
break; | ||
|
||
case OTAProviderUserObtaining: | ||
NSLog(@"User Consent Obtaining"); | ||
_queryImageStatus = MTROtaSoftwareUpdateProviderOTAQueryStatusBusy; | ||
break; | ||
|
||
case OTAProviderUserDenied: | ||
case OTAProviderUserUnknown: | ||
NSLog(@"User Consent Denied or Uknown"); | ||
_queryImageStatus = MTROtaSoftwareUpdateProviderOTAQueryStatusNotAvailable; | ||
break; | ||
} | ||
_selectedCandidate.status = @(_queryImageStatus); | ||
} | ||
} else { | ||
NSLog(@"Unable to select OTA Image."); | ||
_selectedCandidate.status = @(MTROtaSoftwareUpdateProviderOTAQueryStatusNotAvailable); | ||
error = [[NSError alloc] | ||
initWithDomain:@"OTAProviderDomain" | ||
code:MTRErrorCodeInvalidState | ||
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"Unable to select Candidate.", nil) }]; | ||
} | ||
completionHandler(_selectedCandidate, error); | ||
} | ||
|
||
- (void)handleApplyUpdateRequest:(MTROtaSoftwareUpdateProviderClusterApplyUpdateRequestParams * _Nonnull)params | ||
completionHandler:(void (^_Nonnull)(MTROtaSoftwareUpdateProviderClusterApplyUpdateResponseParams * _Nullable data, | ||
NSError * _Nullable error))completionHandler | ||
{ | ||
MTROtaSoftwareUpdateProviderClusterApplyUpdateResponseParams * applyUpdateResponsePrams = | ||
[[MTROtaSoftwareUpdateProviderClusterApplyUpdateResponseParams alloc] init]; | ||
applyUpdateResponsePrams.action = @(MTROtaSoftwareUpdateProviderOTAApplyUpdateActionProceed); | ||
completionHandler(applyUpdateResponsePrams, nil); | ||
} | ||
|
||
- (void)handleNotifyUpdateApplied:(MTROtaSoftwareUpdateProviderClusterNotifyUpdateAppliedParams * _Nonnull)params | ||
completionHandler:(StatusCompletion _Nonnull)completionHandler | ||
{ | ||
completionHandler(nil); | ||
} | ||
|
||
- (void)SetOTAFilePath:(const char *)path | ||
{ | ||
_mOTAFilePath = [NSString stringWithUTF8String:path]; | ||
} | ||
|
||
- (NSData *)generateUpdateToken | ||
{ | ||
NSMutableData * updateTokenData = [NSMutableData dataWithCapacity:kUpdateTokenLen]; | ||
for (unsigned int i = 0; i < kUpdateTokenLen / 4; ++i) { | ||
u_int32_t randomBits = arc4random(); | ||
[updateTokenData appendBytes:(void *) &randomBits length:4]; | ||
} | ||
|
||
return [NSData dataWithData:updateTokenData]; | ||
} | ||
|
||
- (bool)SelectOTACandidate:(NSNumber *)requestorVendorID | ||
rPID:(NSNumber *)requestorProductID | ||
rSV:(NSNumber *)requestorSoftwareVersion | ||
{ | ||
bool candidateFound = false; | ||
NSArray * sortedArray = [_candidates sortedArrayUsingSelector:@selector(CompareSoftwareVersions:)]; | ||
for (DeviceSoftwareVersionModel * candidate : sortedArray) { | ||
if (candidate.deviceModelData.softwareVersionValid | ||
&& ([requestorSoftwareVersion unsignedLongValue] < [candidate.softwareVersion unsignedLongValue]) | ||
&& ([requestorSoftwareVersion unsignedLongValue] >= | ||
[candidate.deviceModelData.minApplicableSoftwareVersion unsignedLongValue]) | ||
&& ([requestorSoftwareVersion unsignedLongValue] <= | ||
[candidate.deviceModelData.maxApplicableSoftwareVersion unsignedLongValue]) | ||
&& ([requestorVendorID unsignedIntValue] == [candidate.deviceModelData.vendorId unsignedIntValue]) | ||
&& ([requestorProductID unsignedIntValue] == [candidate.deviceModelData.productId unsignedIntValue])) { | ||
candidateFound = true; | ||
_selectedCandidate = candidate; | ||
_selectedCandidate.imageURI = [NSString | ||
stringWithFormat:@"bdx://%016llX/%@", [_nodeID unsignedLongLongValue], _selectedCandidate.deviceModelData.otaURL]; | ||
} | ||
} | ||
return candidateFound; | ||
} | ||
|
||
@end | ||
|
||
@implementation DeviceSoftwareVersionModelData | ||
- (instancetype)init | ||
{ | ||
if (self = [super init]) { | ||
_cDVersionNumber = nil; | ||
|
||
_minApplicableSoftwareVersion = nil; | ||
|
||
_maxApplicableSoftwareVersion = nil; | ||
|
||
_otaURL = nil; | ||
} | ||
return self; | ||
} | ||
|
||
@end | ||
|
||
@implementation DeviceSoftwareVersionModel | ||
- (instancetype)init | ||
{ | ||
if (self = [super init]) { | ||
_deviceModelData = [[DeviceSoftwareVersionModelData alloc] init]; | ||
} | ||
return self; | ||
} | ||
- (NSComparisonResult)CompareSoftwareVersions:(DeviceSoftwareVersionModel * _Nullable)otherObject | ||
{ | ||
return [self.deviceModelData.softwareVersion compare:otherObject.deviceModelData.softwareVersion]; | ||
} | ||
@end |
Oops, something went wrong.