Skip to content

Commit

Permalink
Apply API review fixes for MTROTAHeaderParser. (#22611)
Browse files Browse the repository at this point in the history
* Move the parsing method to MTROTAHeader.
* Rename the file to MTROTAHeader.h
* Add documentation.
* Make the properties that can be missing nullable.
* Make properties copy instead of strong.

Fixes #22540

Addresses part of #22420
  • Loading branch information
bzbarsky-apple authored Sep 14, 2022
1 parent 91db05d commit 8c4675f
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
}

NSError * error;
return [MTROTAHeaderParser headerFromData:[NSData dataWithBytes:buffer.data() length:buffer.size()] error:&error];
return [MTROTAHeader headerFromData:[NSData dataWithBytes:buffer.data() length:buffer.size()] error:&error];
}

// Parses the JSON filepath and extracts DeviceSoftwareVersionModel parameters
Expand Down
111 changes: 111 additions & 0 deletions src/darwin/Framework/CHIP/MTROTAHeader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
*
* 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.
*/

#import <Foundation/Foundation.h>

/**
* A representation of an OTA image header as defined in the Matter
* specification's "Over-the-Air (OTA) Software Update File Format" section.
*/

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, MTROTAImageDigestType) {
MTROTAImageDigestTypeSha256 = 1,
MTROTAImageDigestTypeSha256_128,
MTROTAImageDigestTypeSha256_120,
MTROTAImageDigestTypeSha256_96,
MTROTAImageDigestTypeSha256_64,
MTROTAImageDigestTypeSha256_32,
MTROTAImageDigestTypeSha384,
MTROTAImageDigestTypeSha512,
MTROTAImageDigestTypeSha3_224,
MTROTAImageDigestTypeSha3_256,
MTROTAImageDigestTypeSha3_384,
MTROTAImageDigestTypeSha3_512,
};

@interface MTROTAHeader : NSObject

/**
* The identifier of the vendor whose product this image is meant for.
*
* This field can be compared to the vendor id received in the Query Image
* command to determine whether an image matches.
*
* This field may be 0, in which case the image might apply to products from
* more than one vendor. If it's nonzero, it must match the vendor id in Query
* Image for this image to be considered.
*/
@property (nonatomic, copy) NSNumber * vendorID;
/**
* The identifier of the specific product the image is meant for. May be 0, if
* the image might apply to more than one product. This is allowed, but not
* required, to be matched against the product id received in Query Image.
*/
@property (nonatomic, copy) NSNumber * productID;
/**
* The size of the actual image payload, which follows the header in the OTA
* file.
*/
@property (nonatomic, copy) NSNumber * payloadSize;
/**
* The version of the software contained in this image. This is the version the
* OTA requestor will be updated to if this image is installed. This can be
* used to determine whether this image is newer than what the requestor is
* currently running, by comparing it to the SoftwareVersion in the Query Image
* command.
*/
@property (nonatomic, copy) NSNumber * softwareVersion;
/**
* Human-readable version of softwareVersion. This must not be used for
* deciding which versions are newer or older; use softwareVersion for that.
*/
@property (nonatomic, copy) NSString * softwareVersionString;
/**
* If not nil a URL pointing to release notes for the software update
* represented by the image.
*/
@property (nonatomic, copy, nullable) NSString * releaseNotesURL;
/**
* A digest of the payload that follows the header. Can be used to verify that
* the payload is not truncated or corrupted.
*/
@property (nonatomic, copy) NSData * imageDigest;
/**
* The specific algorithm that was used to compute imageDigest.
*/
@property (nonatomic, assign) MTROTAImageDigestType imageDigestType;
/**
* If not nil, specifies the smallest software version that this update can be
* applied on top of. In that case, this value must be compared to the
* SoftwareVersion in the QueryImage command to check whether this image is
* valid for the OTA requestor.
*/
@property (nonatomic, copy, nullable) NSNumber * minApplicableVersion;
/**
* If not nil, specifies the largest software version that this update can be
* applied on top of. In that case, this value must be compared to the
* SoftwareVersion in the QueryImage command to check whether this image is
* valid for the OTA requestor.
*/
@property (nonatomic, copy, nullable) NSNumber * maxApplicableVersion;

+ (nullable MTROTAHeader *)headerFromData:(NSData *)data error:(NSError * __autoreleasing *)error;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

#import "MTROTAHeaderParser.h"
#import "MTROTAHeader.h"

#import "MTRError.h"
#import "MTRError_Internal.h"
Expand All @@ -25,25 +25,26 @@
#include <lib/core/OTAImageHeader.h>

@implementation MTROTAHeader
@end

@implementation MTROTAHeaderParser
+ (MTROTAHeader * _Nullable)headerFromData:(NSData *)data error:(NSError * __autoreleasing *)error
{
chip::OTAImageHeaderParser parser;

parser.Init();

if (!parser.IsInitialized()) {
*error = [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeGeneralError userInfo:nil];
if (error != nil) {
*error = [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeGeneralError userInfo:nil];
}
return nil;
}

chip::ByteSpan buffer = AsByteSpan(data);
chip::OTAImageHeader header;
CHIP_ERROR err = parser.AccumulateAndDecode(buffer, header);
if (err != CHIP_NO_ERROR) {
*error = [MTRError errorForCHIPErrorCode:err];
if (error != nil) {
*error = [MTRError errorForCHIPErrorCode:err];
}
parser.Clear();
return nil;
}
Expand Down
56 changes: 0 additions & 56 deletions src/darwin/Framework/CHIP/MTROTAHeaderParser.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/Matter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#import <Matter/MTRError.h>
#import <Matter/MTRKeypair.h>
#import <Matter/MTRNOCChainIssuer.h>
#import <Matter/MTROTAHeaderParser.h>
#import <Matter/MTROTAHeader.h>
#import <Matter/MTROTAProviderDelegate.h>
#import <Matter/MTRSetupPayload.h>
#import <Matter/MTRStorage.h>
Expand Down
16 changes: 8 additions & 8 deletions src/darwin/Framework/Matter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
1ED276E026C57CF000547A89 /* MTRCallbackBridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1ED276DF26C57CF000547A89 /* MTRCallbackBridge.mm */; };
1ED276E226C5812A00547A89 /* MTRCluster.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1ED276E126C5812A00547A89 /* MTRCluster.mm */; };
1ED276E426C5832500547A89 /* MTRCluster.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ED276E326C5832500547A89 /* MTRCluster.h */; settings = {ATTRIBUTES = (Public, ); }; };
1EDCE545289049A100E41EC9 /* MTROTAHeaderParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 1EDCE543289049A100E41EC9 /* MTROTAHeaderParser.h */; settings = {ATTRIBUTES = (Public, ); }; };
1EDCE546289049A100E41EC9 /* MTROTAHeaderParser.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1EDCE544289049A100E41EC9 /* MTROTAHeaderParser.mm */; };
1EDCE545289049A100E41EC9 /* MTROTAHeader.h in Headers */ = {isa = PBXBuildFile; fileRef = 1EDCE543289049A100E41EC9 /* MTROTAHeader.h */; settings = {ATTRIBUTES = (Public, ); }; };
1EDCE546289049A100E41EC9 /* MTROTAHeader.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1EDCE544289049A100E41EC9 /* MTROTAHeader.mm */; };
27A53C1727FBC6920053F131 /* MTRAttestationTrustStoreBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = 27A53C1527FBC6920053F131 /* MTRAttestationTrustStoreBridge.h */; };
27A53C1827FBC6920053F131 /* MTRAttestationTrustStoreBridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 27A53C1627FBC6920053F131 /* MTRAttestationTrustStoreBridge.mm */; };
2C1B027A2641DB4E00780EF1 /* MTROperationalCredentialsDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2C1B02782641DB4E00780EF1 /* MTROperationalCredentialsDelegate.mm */; };
Expand Down Expand Up @@ -154,8 +154,8 @@
1ED276DF26C57CF000547A89 /* MTRCallbackBridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = MTRCallbackBridge.mm; path = "zap-generated/MTRCallbackBridge.mm"; sourceTree = "<group>"; };
1ED276E126C5812A00547A89 /* MTRCluster.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRCluster.mm; sourceTree = "<group>"; };
1ED276E326C5832500547A89 /* MTRCluster.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRCluster.h; sourceTree = "<group>"; };
1EDCE543289049A100E41EC9 /* MTROTAHeaderParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTROTAHeaderParser.h; sourceTree = "<group>"; };
1EDCE544289049A100E41EC9 /* MTROTAHeaderParser.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTROTAHeaderParser.mm; sourceTree = "<group>"; };
1EDCE543289049A100E41EC9 /* MTROTAHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTROTAHeader.h; sourceTree = "<group>"; };
1EDCE544289049A100E41EC9 /* MTROTAHeader.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTROTAHeader.mm; sourceTree = "<group>"; };
27A53C1527FBC6920053F131 /* MTRAttestationTrustStoreBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRAttestationTrustStoreBridge.h; sourceTree = "<group>"; };
27A53C1627FBC6920053F131 /* MTRAttestationTrustStoreBridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRAttestationTrustStoreBridge.mm; sourceTree = "<group>"; };
2C1B02782641DB4E00780EF1 /* MTROperationalCredentialsDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTROperationalCredentialsDelegate.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -355,8 +355,8 @@
B202528F2459E34F00F97062 /* CHIP */ = {
isa = PBXGroup;
children = (
1EDCE543289049A100E41EC9 /* MTROTAHeaderParser.h */,
1EDCE544289049A100E41EC9 /* MTROTAHeaderParser.mm */,
1EDCE543289049A100E41EC9 /* MTROTAHeader.h */,
1EDCE544289049A100E41EC9 /* MTROTAHeader.mm */,
27A53C1527FBC6920053F131 /* MTRAttestationTrustStoreBridge.h */,
27A53C1627FBC6920053F131 /* MTRAttestationTrustStoreBridge.mm */,
88EBF8CB27FABDD500686BC1 /* MTRDeviceAttestationDelegate.h */,
Expand Down Expand Up @@ -556,7 +556,7 @@
7596A84828762783004DAE0E /* MTRAsyncCallbackWorkQueue.h in Headers */,
5A7947E527C0129F00434CF2 /* MTRDeviceController+XPC.h in Headers */,
B2E0D7B4245B0B5C003C5B48 /* MTRError_Internal.h in Headers */,
1EDCE545289049A100E41EC9 /* MTROTAHeaderParser.h in Headers */,
1EDCE545289049A100E41EC9 /* MTROTAHeader.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -698,7 +698,7 @@
3CF134A9289D8D800017A19E /* MTRCSRInfo.m in Sources */,
991DC0892475F47D00C13860 /* MTRDeviceController.mm in Sources */,
B2E0D7B7245B0B5C003C5B48 /* MTRQRCodeSetupPayloadParser.mm in Sources */,
1EDCE546289049A100E41EC9 /* MTROTAHeaderParser.mm in Sources */,
1EDCE546289049A100E41EC9 /* MTROTAHeader.mm in Sources */,
1EC4CE5D25CC26E900D7304F /* MTRBaseClusters.mm in Sources */,
51E0310127EA20D20083DC9C /* MTRControllerAccessControl.mm in Sources */,
1ED276E226C5812A00547A89 /* MTRCluster.mm in Sources */,
Expand Down

0 comments on commit 8c4675f

Please sign in to comment.