diff options
author | Frank Elias <francois.elias@livio.io> | 2021-08-25 15:25:28 -0400 |
---|---|---|
committer | Frank Elias <francois.elias@livio.io> | 2021-08-25 15:25:28 -0400 |
commit | bad3b7580aabb356d14f4ae38d82a17b59574511 (patch) | |
tree | 7ac71152c469f06bfc55835e26c36bbada6e0ecc | |
parent | 738bea504926cd91c7778075dc76fd2619b9283c (diff) | |
download | sdl_ios-bad3b7580aabb356d14f4ae38d82a17b59574511.tar.gz |
comments review
documentation update
code style update
-rw-r--r-- | SmartDeviceLink-iOS.xcodeproj/project.pbxproj | 8 | ||||
-rw-r--r-- | SmartDeviceLink/private/SDLSecurityQueryPayload.h | 30 | ||||
-rw-r--r-- | SmartDeviceLink/private/SDLSecurityQueryPayload.m | 104 |
3 files changed, 87 insertions, 55 deletions
diff --git a/SmartDeviceLink-iOS.xcodeproj/project.pbxproj b/SmartDeviceLink-iOS.xcodeproj/project.pbxproj index cfd04d1fe..c82bed27a 100644 --- a/SmartDeviceLink-iOS.xcodeproj/project.pbxproj +++ b/SmartDeviceLink-iOS.xcodeproj/project.pbxproj @@ -5582,8 +5582,6 @@ 4ABB27F624F823F20061BF55 /* SDLSamplingRate.m */, 4ABB280524F824600061BF55 /* SDLSeatMemoryActionType.h */, 4ABB280224F8245F0061BF55 /* SDLSeatMemoryActionType.m */, - C99BE00726C53E7E00DB0B54 /* SDLSecurityQueryErrorCode.h */, - C99BE00826C53E7E00DB0B54 /* SDLSecurityQueryErrorCode.m */, C9DFFE7C257AD07E00F7D57A /* SDLSeekIndicatorType.h */, C9DFFE7D257AD07E00F7D57A /* SDLSeekIndicatorType.m */, 4ABB280424F824600061BF55 /* SDLServiceUpdateReason.h */, @@ -5818,6 +5816,10 @@ 4A8BD32524F9431B000945E3 /* SDLV1ProtocolHeader.m */, 4A8BD32A24F9431B000945E3 /* SDLV2ProtocolHeader.h */, 4A8BD32624F9431B000945E3 /* SDLV2ProtocolHeader.m */, + C93193DA26B1B57B008203EC /* SDLSecurityQueryPayload.h */, + C93193DB26B1B57B008203EC /* SDLSecurityQueryPayload.m */, + C99BE00726C53E7E00DB0B54 /* SDLSecurityQueryErrorCode.h */, + C99BE00826C53E7E00DB0B54 /* SDLSecurityQueryErrorCode.m */, ); name = Header; sourceTree = "<group>"; @@ -5841,8 +5843,6 @@ children = ( 4ABB28EF24F82AFD0061BF55 /* SDLRPCPayload.h */, 4ABB28EE24F82AFD0061BF55 /* SDLRPCPayload.m */, - C93193DA26B1B57B008203EC /* SDLSecurityQueryPayload.h */, - C93193DB26B1B57B008203EC /* SDLSecurityQueryPayload.m */, ); name = Payload; sourceTree = "<group>"; diff --git a/SmartDeviceLink/private/SDLSecurityQueryPayload.h b/SmartDeviceLink/private/SDLSecurityQueryPayload.h index 29418fe47..3ed56bebc 100644 --- a/SmartDeviceLink/private/SDLSecurityQueryPayload.h +++ b/SmartDeviceLink/private/SDLSecurityQueryPayload.h @@ -9,6 +9,9 @@ #import <Foundation/Foundation.h> #import "SDLRPCMessageType.h" +/** + * Enum for different SDL security query types + */ typedef NS_ENUM(Byte, SDLSecurityQueryType) { /// A request that will require a response SDLSecurityQueryTypeRequest = 0x00, @@ -20,7 +23,10 @@ typedef NS_ENUM(Byte, SDLSecurityQueryType) { SDLSecurityQueryTypeNotification = 0x20 }; -typedef NS_ENUM(UInt32, SDLSecurityQueryId) { +/** + * Enum for each type of SDL security query IDs + */ +typedef NS_ENUM(NSUInteger, SDLSecurityQueryId) { /// Send handshake data SDLSecurityQueryIdSendHandshake = 0x000001, @@ -32,14 +38,34 @@ NS_ASSUME_NONNULL_BEGIN @interface SDLSecurityQueryPayload : NSObject +/** + The security query's type, could be of type request - response or notification + */ @property (assign, nonatomic) SDLSecurityQueryType queryType; + +/** + The security query's ID. + */ @property (assign, nonatomic) UInt32 queryID; + +/** + The message ID is set by the Mobile libraries to track security messages. + */ @property (assign, nonatomic) UInt32 sequenceNumber; + +/** + The JSON data following the binary query header. + */ @property (nullable, strong, nonatomic) NSData *jsonData; + +/** + The binary data that is after the header (96 bits) and the JSON data. + */ @property (nullable, strong, nonatomic) NSData *binaryData; -- (NSData *)data; + (nullable id)securityPayloadWithData:(NSData *)data; +- (nullable instancetype)initWithData:(NSData *)data; +- (NSData *)data; @end diff --git a/SmartDeviceLink/private/SDLSecurityQueryPayload.m b/SmartDeviceLink/private/SDLSecurityQueryPayload.m index bd84a33d1..bc6520abd 100644 --- a/SmartDeviceLink/private/SDLSecurityQueryPayload.m +++ b/SmartDeviceLink/private/SDLSecurityQueryPayload.m @@ -16,8 +16,12 @@ NS_ASSUME_NONNULL_BEGIN @implementation SDLSecurityQueryPayload ++ (nullable id)securityPayloadWithData:(NSData *)data { + return [[SDLSecurityQueryPayload alloc] initWithData:data]; +} + - (nullable instancetype)initWithData:(NSData *)data { - unsigned long dataLength = data.length; + NSUInteger dataLength = data.length; if (data == nil || dataLength == 0) { SDLLogW(@"Security Payload data is nil"); @@ -25,58 +29,64 @@ NS_ASSUME_NONNULL_BEGIN } if (dataLength < SECURITY_QUERY_HEADER_SIZE) { - SDLLogW(@"Security Payload error: not enough data to form Security Query header"); + SDLLogE(@"Security Payload error: not enough data to form Security Query header"); return nil; } - if (self = [self init]) { - @try { - // Setup our pointers for data access - UInt8 *bytePointer = (UInt8 *)data.bytes; - UInt32 *ui32Pointer = (UInt32 *)data.bytes; - - // Extract the parts - UInt8 queryType = bytePointer[0]; - - self.queryType = queryType; - - UInt32 queryID = ui32Pointer[0]; - queryID = CFSwapInt32BigToHost(queryID) & 0x00FFFFFF; - self.queryID = queryID; - - UInt32 sequenceNumber = ui32Pointer[1]; - sequenceNumber = CFSwapInt32BigToHost(sequenceNumber); - self.sequenceNumber = sequenceNumber; - - UInt32 jsonDataSize = ui32Pointer[2]; - jsonDataSize = CFSwapInt32BigToHost(jsonDataSize); - - NSData *jsonData = nil; - NSUInteger offsetOfJSONData = SECURITY_QUERY_HEADER_SIZE; - if (jsonDataSize > 0 && jsonDataSize <= dataLength - SECURITY_QUERY_HEADER_SIZE) { - jsonData = [data subdataWithRange:NSMakeRange(offsetOfJSONData, jsonDataSize)]; - } - self.jsonData = jsonData; - - NSData *binaryData = nil; - NSUInteger offsetOfBinaryData = SECURITY_QUERY_HEADER_SIZE + jsonDataSize; - NSUInteger binaryDataSize = data.length - jsonDataSize - SECURITY_QUERY_HEADER_SIZE; - if (binaryDataSize > 0) { - binaryData = [data subdataWithRange:NSMakeRange(offsetOfBinaryData, binaryDataSize)]; - } - self.binaryData = binaryData; - - } @catch (NSException *e) { - SDLLogW(@"SDLSecurityQueryPayload error: %@", e); - return nil; + self = [super init]; + if (!self) { return nil; } + + @try { + // Setup our pointers for data access + Byte *bytePointer = (UInt8 *)data.bytes; + UInt32 *ui32Pointer = (UInt32 *)data.bytes; + + // Extract the parts + UInt8 queryType = bytePointer[0]; + + self.queryType = queryType; + + // Extract the 24 bit query ID in the last 24 bits of the first 32 bits. + UInt32 queryID = ui32Pointer[0]; + queryID = CFSwapInt32BigToHost(queryID) & 0x00FFFFFF; + self.queryID = queryID; + + // Extract the 32 bit sequence number from the data after the first 32 bits. + UInt32 sequenceNumber = ui32Pointer[1]; + sequenceNumber = CFSwapInt32BigToHost(sequenceNumber); + self.sequenceNumber = sequenceNumber; + + // Extract the 32 bit json data size from the data after the first 64 bits + UInt32 jsonDataSize = ui32Pointer[2]; + jsonDataSize = CFSwapInt32BigToHost(jsonDataSize); + + // Extract the JSON data after the header (96 bits) based on the JSON data size + NSData *jsonData = nil; + NSUInteger offsetOfJSONData = SECURITY_QUERY_HEADER_SIZE; + if (jsonDataSize > 0 && jsonDataSize <= dataLength - SECURITY_QUERY_HEADER_SIZE) { + jsonData = [data subdataWithRange:NSMakeRange(offsetOfJSONData, jsonDataSize)]; } + self.jsonData = jsonData; + + // Extract the binary data after the header (96 bits) and the JSON data to the end + NSData *binaryData = nil; + NSUInteger offsetOfBinaryData = SECURITY_QUERY_HEADER_SIZE + jsonDataSize; + NSUInteger binaryDataSize = data.length - jsonDataSize - SECURITY_QUERY_HEADER_SIZE; + if (binaryDataSize > 0) { + binaryData = [data subdataWithRange:NSMakeRange(offsetOfBinaryData, binaryDataSize)]; + } + self.binaryData = binaryData; + + } @catch (NSException *e) { + SDLLogE(@"SDLSecurityQueryPayload init error: %@", e); + return nil; } return self; } - (NSData *)data { - // Header is: + // From the properties, create a data buffer // Query Type - first 8 bits // Query ID - next 24 bits // Sequence Number - next 32 bits @@ -88,7 +98,7 @@ NS_ASSUME_NONNULL_BEGIN headerBuffer[0] &= 0xFF; headerBuffer[0] |= self.queryType; - // Serialize the header, the json data then the binary data + // Serialize the header. Append the json data, then the binary data NSMutableData *dataOut = [NSMutableData dataWithCapacity:[self size]]; [dataOut appendBytes:&headerBuffer length:12]; [dataOut appendData:self.jsonData]; @@ -98,20 +108,16 @@ NS_ASSUME_NONNULL_BEGIN } - (NSUInteger)size { - NSUInteger headerSize = SECURITY_QUERY_HEADER_SIZE; NSUInteger jsonDataSize = self.jsonData.length; NSUInteger binaryDataSize = self.binaryData.length; - return (headerSize + jsonDataSize + binaryDataSize); + return (SECURITY_QUERY_HEADER_SIZE + jsonDataSize + binaryDataSize); } - (NSString *)description { return [NSString stringWithFormat:@"Security Query Header: queryType:%i, queryID:%lu, sequenceNumber:%lu, json:%lu bytes, binary:%lu bytes", self.queryType, (unsigned long)self.queryID, (unsigned long)self.sequenceNumber, (NSUInteger)self.jsonData.length, (NSUInteger)self.binaryData.length]; } -+ (nullable id)securityPayloadWithData:(NSData *)data { - return [[SDLSecurityQueryPayload alloc] initWithData:data]; -} @end NS_ASSUME_NONNULL_END |