Skip to content

Commit 8c4675f

Browse files
Apply API review fixes for MTROTAHeaderParser. (#22611)
* 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
1 parent 91db05d commit 8c4675f

File tree

6 files changed

+128
-72
lines changed

6 files changed

+128
-72
lines changed

examples/darwin-framework-tool/commands/provider/OTASoftwareUpdateInteractive.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
}
4040

4141
NSError * error;
42-
return [MTROTAHeaderParser headerFromData:[NSData dataWithBytes:buffer.data() length:buffer.size()] error:&error];
42+
return [MTROTAHeader headerFromData:[NSData dataWithBytes:buffer.data() length:buffer.size()] error:&error];
4343
}
4444

4545
// Parses the JSON filepath and extracts DeviceSoftwareVersionModel parameters
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
*
3+
* Copyright (c) 2022 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#import <Foundation/Foundation.h>
19+
20+
/**
21+
* A representation of an OTA image header as defined in the Matter
22+
* specification's "Over-the-Air (OTA) Software Update File Format" section.
23+
*/
24+
25+
NS_ASSUME_NONNULL_BEGIN
26+
27+
typedef NS_ENUM(NSUInteger, MTROTAImageDigestType) {
28+
MTROTAImageDigestTypeSha256 = 1,
29+
MTROTAImageDigestTypeSha256_128,
30+
MTROTAImageDigestTypeSha256_120,
31+
MTROTAImageDigestTypeSha256_96,
32+
MTROTAImageDigestTypeSha256_64,
33+
MTROTAImageDigestTypeSha256_32,
34+
MTROTAImageDigestTypeSha384,
35+
MTROTAImageDigestTypeSha512,
36+
MTROTAImageDigestTypeSha3_224,
37+
MTROTAImageDigestTypeSha3_256,
38+
MTROTAImageDigestTypeSha3_384,
39+
MTROTAImageDigestTypeSha3_512,
40+
};
41+
42+
@interface MTROTAHeader : NSObject
43+
44+
/**
45+
* The identifier of the vendor whose product this image is meant for.
46+
*
47+
* This field can be compared to the vendor id received in the Query Image
48+
* command to determine whether an image matches.
49+
*
50+
* This field may be 0, in which case the image might apply to products from
51+
* more than one vendor. If it's nonzero, it must match the vendor id in Query
52+
* Image for this image to be considered.
53+
*/
54+
@property (nonatomic, copy) NSNumber * vendorID;
55+
/**
56+
* The identifier of the specific product the image is meant for. May be 0, if
57+
* the image might apply to more than one product. This is allowed, but not
58+
* required, to be matched against the product id received in Query Image.
59+
*/
60+
@property (nonatomic, copy) NSNumber * productID;
61+
/**
62+
* The size of the actual image payload, which follows the header in the OTA
63+
* file.
64+
*/
65+
@property (nonatomic, copy) NSNumber * payloadSize;
66+
/**
67+
* The version of the software contained in this image. This is the version the
68+
* OTA requestor will be updated to if this image is installed. This can be
69+
* used to determine whether this image is newer than what the requestor is
70+
* currently running, by comparing it to the SoftwareVersion in the Query Image
71+
* command.
72+
*/
73+
@property (nonatomic, copy) NSNumber * softwareVersion;
74+
/**
75+
* Human-readable version of softwareVersion. This must not be used for
76+
* deciding which versions are newer or older; use softwareVersion for that.
77+
*/
78+
@property (nonatomic, copy) NSString * softwareVersionString;
79+
/**
80+
* If not nil a URL pointing to release notes for the software update
81+
* represented by the image.
82+
*/
83+
@property (nonatomic, copy, nullable) NSString * releaseNotesURL;
84+
/**
85+
* A digest of the payload that follows the header. Can be used to verify that
86+
* the payload is not truncated or corrupted.
87+
*/
88+
@property (nonatomic, copy) NSData * imageDigest;
89+
/**
90+
* The specific algorithm that was used to compute imageDigest.
91+
*/
92+
@property (nonatomic, assign) MTROTAImageDigestType imageDigestType;
93+
/**
94+
* If not nil, specifies the smallest software version that this update can be
95+
* applied on top of. In that case, this value must be compared to the
96+
* SoftwareVersion in the QueryImage command to check whether this image is
97+
* valid for the OTA requestor.
98+
*/
99+
@property (nonatomic, copy, nullable) NSNumber * minApplicableVersion;
100+
/**
101+
* If not nil, specifies the largest software version that this update can be
102+
* applied on top of. In that case, this value must be compared to the
103+
* SoftwareVersion in the QueryImage command to check whether this image is
104+
* valid for the OTA requestor.
105+
*/
106+
@property (nonatomic, copy, nullable) NSNumber * maxApplicableVersion;
107+
108+
+ (nullable MTROTAHeader *)headerFromData:(NSData *)data error:(NSError * __autoreleasing *)error;
109+
@end
110+
111+
NS_ASSUME_NONNULL_END

src/darwin/Framework/CHIP/MTROTAHeaderParser.mm src/darwin/Framework/CHIP/MTROTAHeader.mm

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
#import "MTROTAHeaderParser.h"
18+
#import "MTROTAHeader.h"
1919

2020
#import "MTRError.h"
2121
#import "MTRError_Internal.h"
@@ -25,25 +25,26 @@
2525
#include <lib/core/OTAImageHeader.h>
2626

2727
@implementation MTROTAHeader
28-
@end
29-
30-
@implementation MTROTAHeaderParser
3128
+ (MTROTAHeader * _Nullable)headerFromData:(NSData *)data error:(NSError * __autoreleasing *)error
3229
{
3330
chip::OTAImageHeaderParser parser;
3431

3532
parser.Init();
3633

3734
if (!parser.IsInitialized()) {
38-
*error = [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeGeneralError userInfo:nil];
35+
if (error != nil) {
36+
*error = [NSError errorWithDomain:MTRErrorDomain code:MTRErrorCodeGeneralError userInfo:nil];
37+
}
3938
return nil;
4039
}
4140

4241
chip::ByteSpan buffer = AsByteSpan(data);
4342
chip::OTAImageHeader header;
4443
CHIP_ERROR err = parser.AccumulateAndDecode(buffer, header);
4544
if (err != CHIP_NO_ERROR) {
46-
*error = [MTRError errorForCHIPErrorCode:err];
45+
if (error != nil) {
46+
*error = [MTRError errorForCHIPErrorCode:err];
47+
}
4748
parser.Clear();
4849
return nil;
4950
}

src/darwin/Framework/CHIP/MTROTAHeaderParser.h

-56
This file was deleted.

src/darwin/Framework/CHIP/Matter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#import <Matter/MTRError.h>
4040
#import <Matter/MTRKeypair.h>
4141
#import <Matter/MTRNOCChainIssuer.h>
42-
#import <Matter/MTROTAHeaderParser.h>
42+
#import <Matter/MTROTAHeader.h>
4343
#import <Matter/MTROTAProviderDelegate.h>
4444
#import <Matter/MTRSetupPayload.h>
4545
#import <Matter/MTRStorage.h>

src/darwin/Framework/Matter.xcodeproj/project.pbxproj

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
1ED276E026C57CF000547A89 /* MTRCallbackBridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1ED276DF26C57CF000547A89 /* MTRCallbackBridge.mm */; };
1616
1ED276E226C5812A00547A89 /* MTRCluster.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1ED276E126C5812A00547A89 /* MTRCluster.mm */; };
1717
1ED276E426C5832500547A89 /* MTRCluster.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ED276E326C5832500547A89 /* MTRCluster.h */; settings = {ATTRIBUTES = (Public, ); }; };
18-
1EDCE545289049A100E41EC9 /* MTROTAHeaderParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 1EDCE543289049A100E41EC9 /* MTROTAHeaderParser.h */; settings = {ATTRIBUTES = (Public, ); }; };
19-
1EDCE546289049A100E41EC9 /* MTROTAHeaderParser.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1EDCE544289049A100E41EC9 /* MTROTAHeaderParser.mm */; };
18+
1EDCE545289049A100E41EC9 /* MTROTAHeader.h in Headers */ = {isa = PBXBuildFile; fileRef = 1EDCE543289049A100E41EC9 /* MTROTAHeader.h */; settings = {ATTRIBUTES = (Public, ); }; };
19+
1EDCE546289049A100E41EC9 /* MTROTAHeader.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1EDCE544289049A100E41EC9 /* MTROTAHeader.mm */; };
2020
27A53C1727FBC6920053F131 /* MTRAttestationTrustStoreBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = 27A53C1527FBC6920053F131 /* MTRAttestationTrustStoreBridge.h */; };
2121
27A53C1827FBC6920053F131 /* MTRAttestationTrustStoreBridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 27A53C1627FBC6920053F131 /* MTRAttestationTrustStoreBridge.mm */; };
2222
2C1B027A2641DB4E00780EF1 /* MTROperationalCredentialsDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2C1B02782641DB4E00780EF1 /* MTROperationalCredentialsDelegate.mm */; };
@@ -154,8 +154,8 @@
154154
1ED276DF26C57CF000547A89 /* MTRCallbackBridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = MTRCallbackBridge.mm; path = "zap-generated/MTRCallbackBridge.mm"; sourceTree = "<group>"; };
155155
1ED276E126C5812A00547A89 /* MTRCluster.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRCluster.mm; sourceTree = "<group>"; };
156156
1ED276E326C5832500547A89 /* MTRCluster.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRCluster.h; sourceTree = "<group>"; };
157-
1EDCE543289049A100E41EC9 /* MTROTAHeaderParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTROTAHeaderParser.h; sourceTree = "<group>"; };
158-
1EDCE544289049A100E41EC9 /* MTROTAHeaderParser.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTROTAHeaderParser.mm; sourceTree = "<group>"; };
157+
1EDCE543289049A100E41EC9 /* MTROTAHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTROTAHeader.h; sourceTree = "<group>"; };
158+
1EDCE544289049A100E41EC9 /* MTROTAHeader.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTROTAHeader.mm; sourceTree = "<group>"; };
159159
27A53C1527FBC6920053F131 /* MTRAttestationTrustStoreBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRAttestationTrustStoreBridge.h; sourceTree = "<group>"; };
160160
27A53C1627FBC6920053F131 /* MTRAttestationTrustStoreBridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRAttestationTrustStoreBridge.mm; sourceTree = "<group>"; };
161161
2C1B02782641DB4E00780EF1 /* MTROperationalCredentialsDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTROperationalCredentialsDelegate.mm; sourceTree = "<group>"; };
@@ -355,8 +355,8 @@
355355
B202528F2459E34F00F97062 /* CHIP */ = {
356356
isa = PBXGroup;
357357
children = (
358-
1EDCE543289049A100E41EC9 /* MTROTAHeaderParser.h */,
359-
1EDCE544289049A100E41EC9 /* MTROTAHeaderParser.mm */,
358+
1EDCE543289049A100E41EC9 /* MTROTAHeader.h */,
359+
1EDCE544289049A100E41EC9 /* MTROTAHeader.mm */,
360360
27A53C1527FBC6920053F131 /* MTRAttestationTrustStoreBridge.h */,
361361
27A53C1627FBC6920053F131 /* MTRAttestationTrustStoreBridge.mm */,
362362
88EBF8CB27FABDD500686BC1 /* MTRDeviceAttestationDelegate.h */,
@@ -556,7 +556,7 @@
556556
7596A84828762783004DAE0E /* MTRAsyncCallbackWorkQueue.h in Headers */,
557557
5A7947E527C0129F00434CF2 /* MTRDeviceController+XPC.h in Headers */,
558558
B2E0D7B4245B0B5C003C5B48 /* MTRError_Internal.h in Headers */,
559-
1EDCE545289049A100E41EC9 /* MTROTAHeaderParser.h in Headers */,
559+
1EDCE545289049A100E41EC9 /* MTROTAHeader.h in Headers */,
560560
);
561561
runOnlyForDeploymentPostprocessing = 0;
562562
};
@@ -698,7 +698,7 @@
698698
3CF134A9289D8D800017A19E /* MTRCSRInfo.m in Sources */,
699699
991DC0892475F47D00C13860 /* MTRDeviceController.mm in Sources */,
700700
B2E0D7B7245B0B5C003C5B48 /* MTRQRCodeSetupPayloadParser.mm in Sources */,
701-
1EDCE546289049A100E41EC9 /* MTROTAHeaderParser.mm in Sources */,
701+
1EDCE546289049A100E41EC9 /* MTROTAHeader.mm in Sources */,
702702
1EC4CE5D25CC26E900D7304F /* MTRBaseClusters.mm in Sources */,
703703
51E0310127EA20D20083DC9C /* MTRControllerAccessControl.mm in Sources */,
704704
1ED276E226C5812A00547A89 /* MTRCluster.mm in Sources */,

0 commit comments

Comments
 (0)