diff options
author | Frank Elias <francois.elias@livio.io> | 2021-07-28 17:22:54 -0400 |
---|---|---|
committer | Frank Elias <francois.elias@livio.io> | 2021-07-28 17:22:54 -0400 |
commit | 023a821053511a7b1aee1bfdc2685a004be7d981 (patch) | |
tree | 67089919cad1b01eace9cd64355c88232bb1a509 | |
parent | 6471330364ddc3edc9b905ebda6d0624c9bd5540 (diff) | |
download | sdl_ios-023a821053511a7b1aee1bfdc2685a004be7d981.tar.gz |
SDLSecurityQuery added
-rw-r--r-- | SmartDeviceLink-iOS.xcodeproj/project.pbxproj | 8 | ||||
-rw-r--r-- | SmartDeviceLink/private/SDLSecurityQueryPayload.h | 27 | ||||
-rw-r--r-- | SmartDeviceLink/private/SDLSecurityQueryPayload.m | 121 |
3 files changed, 156 insertions, 0 deletions
diff --git a/SmartDeviceLink-iOS.xcodeproj/project.pbxproj b/SmartDeviceLink-iOS.xcodeproj/project.pbxproj index ccf58ea3e..4a5e0002d 100644 --- a/SmartDeviceLink-iOS.xcodeproj/project.pbxproj +++ b/SmartDeviceLink-iOS.xcodeproj/project.pbxproj @@ -1736,6 +1736,8 @@ B3DF19F3251225AA0090D7BA /* TestSmartConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = B3DF19F1251225A90090D7BA /* TestSmartConnection.m */; }; B3EC9E6E2579AA010039F3AA /* SDLClimateDataSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = B3EC9E6D2579AA010039F3AA /* SDLClimateDataSpec.m */; }; B3F7918324E062C200DB5CAF /* SDLGetVehicleDataSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = 162E824C1A9BDE8A00906325 /* SDLGetVehicleDataSpec.m */; }; + C93193DC26B1B57C008203EC /* SDLSecurityQueryPayload.h in Headers */ = {isa = PBXBuildFile; fileRef = C93193DA26B1B57B008203EC /* SDLSecurityQueryPayload.h */; }; + C93193DD26B1B57C008203EC /* SDLSecurityQueryPayload.m in Sources */ = {isa = PBXBuildFile; fileRef = C93193DB26B1B57B008203EC /* SDLSecurityQueryPayload.m */; }; C9707D1825DEE786009D00F2 /* NSArray+Extensions.h in Headers */ = {isa = PBXBuildFile; fileRef = C9707D1625DEE786009D00F2 /* NSArray+Extensions.h */; }; C9707D1925DEE786009D00F2 /* NSArray+Extensions.m in Sources */ = {isa = PBXBuildFile; fileRef = C9707D1725DEE786009D00F2 /* NSArray+Extensions.m */; }; C9707D3025E0444D009D00F2 /* SDLMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = C9707D2E25E0444D009D00F2 /* SDLMacros.h */; }; @@ -3649,6 +3651,8 @@ B3DF19F2251225A90090D7BA /* TestSmartConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestSmartConnection.h; sourceTree = "<group>"; }; B3EC9E6D2579AA010039F3AA /* SDLClimateDataSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDLClimateDataSpec.m; sourceTree = "<group>"; }; BB3C600D221AEF37007DD4CA /* NSMutableDictionary+StoreSpec.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = "NSMutableDictionary+StoreSpec.m"; path = "DevAPISpecs/NSMutableDictionary+StoreSpec.m"; sourceTree = "<group>"; }; + C93193DA26B1B57B008203EC /* SDLSecurityQueryPayload.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SDLSecurityQueryPayload.h; path = private/SDLSecurityQueryPayload.h; sourceTree = "<group>"; }; + C93193DB26B1B57B008203EC /* SDLSecurityQueryPayload.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = SDLSecurityQueryPayload.m; path = private/SDLSecurityQueryPayload.m; sourceTree = "<group>"; }; C9707D1625DEE786009D00F2 /* NSArray+Extensions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "NSArray+Extensions.h"; path = "private/NSArray+Extensions.h"; sourceTree = "<group>"; }; C9707D1725DEE786009D00F2 /* NSArray+Extensions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = "NSArray+Extensions.m"; path = "private/NSArray+Extensions.m"; sourceTree = "<group>"; }; C9707D2E25E0444D009D00F2 /* SDLMacros.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SDLMacros.h; path = private/SDLMacros.h; sourceTree = "<group>"; }; @@ -5828,6 +5832,8 @@ children = ( 4ABB28EF24F82AFD0061BF55 /* SDLRPCPayload.h */, 4ABB28EE24F82AFD0061BF55 /* SDLRPCPayload.m */, + C93193DA26B1B57B008203EC /* SDLSecurityQueryPayload.h */, + C93193DB26B1B57B008203EC /* SDLSecurityQueryPayload.m */, ); name = Payload; sourceTree = "<group>"; @@ -7671,6 +7677,7 @@ 4ABB2B5424F84EF50061BF55 /* SDLClimateControlData.h in Headers */, 4A8BD2B624F935BC000945E3 /* SDLSystemCapability.h in Headers */, 4ABB2AA624F847F40061BF55 /* SDLSetInteriorVehicleDataResponse.h in Headers */, + C93193DC26B1B57C008203EC /* SDLSecurityQueryPayload.h in Headers */, 4ABB264F24F7F5720061BF55 /* SDLConfiguration.h in Headers */, 4ABB252224F7E3FC0061BF55 /* SDLLifecycleSyncPDataHandler.h in Headers */, 4ABB29FD24F8477F0061BF55 /* SDLAlertResponse.h in Headers */, @@ -8363,6 +8370,7 @@ 4ABB2BA424F850AE0061BF55 /* SDLLightCapabilities.m in Sources */, B3A9DB0625D497FB00CDFD21 /* SDLAppCapabilityType.m in Sources */, 4ABB272224F7FCAE0061BF55 /* SDLDefrostZone.m in Sources */, + C93193DD26B1B57C008203EC /* SDLSecurityQueryPayload.m in Sources */, 4ABB24BB24F592620061BF55 /* NSMutableArray+Safe.m in Sources */, 4ABB25AA24F7E6E10061BF55 /* SDLMenuManager.m in Sources */, 4ABB25DD24F7E77C0061BF55 /* SDLScreenManager.m in Sources */, diff --git a/SmartDeviceLink/private/SDLSecurityQueryPayload.h b/SmartDeviceLink/private/SDLSecurityQueryPayload.h new file mode 100644 index 000000000..bb4471347 --- /dev/null +++ b/SmartDeviceLink/private/SDLSecurityQueryPayload.h @@ -0,0 +1,27 @@ +// +// SDLSecurityQueryPayload.h +// SmartDeviceLink +// +// Created by Frank Elias on 7/28/21. +// Copyright © 2021 smartdevicelink. All rights reserved. +// + +#import <Foundation/Foundation.h> +#import "SDLRPCMessageType.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface SDLSecurityQueryPayload : NSObject + +@property (assign, nonatomic) SDLRPCMessageType rpcType; +@property (assign, nonatomic) UInt32 functionID; +@property (assign, nonatomic) UInt32 correlationID; +@property (nullable, strong, nonatomic) NSData *jsonData; +@property (nullable, strong, nonatomic) NSData *binaryData; + +- (NSData *)data; ++ (nullable id)rpcPayloadWithData:(NSData *)data; + +@end + +NS_ASSUME_NONNULL_END diff --git a/SmartDeviceLink/private/SDLSecurityQueryPayload.m b/SmartDeviceLink/private/SDLSecurityQueryPayload.m new file mode 100644 index 000000000..044e8efc7 --- /dev/null +++ b/SmartDeviceLink/private/SDLSecurityQueryPayload.m @@ -0,0 +1,121 @@ +// +// SDLSecurityQueryPayload.m +// SmartDeviceLink +// +// Created by Frank Elias on 7/28/21. +// Copyright © 2021 smartdevicelink. All rights reserved. +// + +#import "SDLSecurityQueryPayload.h" + +#import "SDLLogMacros.h" + +const NSUInteger SECURITY_QUERY_HEADER_SIZE = 12; + +NS_ASSUME_NONNULL_BEGIN + +@implementation SDLSecurityQueryPayload + +- (nullable instancetype)initWithData:(NSData *)data { + unsigned long dataLength = data.length; + + if (data == nil || dataLength == 0) { + SDLLogW(@"RPC Payload data is nil"); + return nil; + } + + if (dataLength < SECURITY_QUERY_HEADER_SIZE) { + SDLLogW(@"RPC Payload error: not enough data to form RPC 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 rpcType = (bytePointer[0] & 0xFF) >> 8; + + self.rpcType = rpcType; + + UInt32 functionID = ui32Pointer[0]; + functionID = CFSwapInt32BigToHost(functionID) & 0x00FFFFF; + self.functionID = functionID; + + UInt32 correlationID = ui32Pointer[1]; + correlationID = CFSwapInt32BigToHost(correlationID); + self.correlationID = correlationID; + + 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; + } + } + + return self; +} + +- (NSData *)data { + // Header is: + // RPC Type - first 8 bits + // RPC Function ID - next 24 bits + // Correlation ID - next 32 bits + // JSON size - next 32 bits + UInt8 headerBuffer[SECURITY_QUERY_HEADER_SIZE]; + *(UInt32 *)&headerBuffer[0] = CFSwapInt32HostToBig(self.functionID); + *(UInt32 *)&headerBuffer[4] = CFSwapInt32HostToBig(self.correlationID); + *(UInt32 *)&headerBuffer[8] = CFSwapInt32HostToBig((UInt32)self.jsonData.length); + UInt8 rpcType = (Byte)((self.rpcType & 0xFF) << 8); + headerBuffer[0] &= 0xFF; + headerBuffer[0] |= rpcType; + + // Serialize the header, the json data then the binary data + NSMutableData *dataOut = [NSMutableData dataWithCapacity:[self size]]; + [dataOut appendBytes:&headerBuffer length:12]; + [dataOut appendData:self.jsonData]; + [dataOut appendData:self.binaryData]; + + return dataOut; +} + +- (NSUInteger)size { + NSUInteger headerSize = SECURITY_QUERY_HEADER_SIZE; + NSUInteger jsonDataSize = self.jsonData.length; + NSUInteger binaryDataSize = self.binaryData.length; + + return (headerSize + jsonDataSize + binaryDataSize); +} + +- (NSString *)description { + NSMutableString *description = [[NSMutableString alloc] init]; + [description appendFormat:@" rpcType:%i, functionID:%i, correlationID:%i, json:%lu bytes, binary:%lu bytes", self.rpcType, (unsigned int)self.functionID, (unsigned int)self.correlationID, (unsigned long)self.jsonData.length, (unsigned long)self.binaryData.length]; + + return description; +} + ++ (nullable id)rpcPayloadWithData:(NSData *)data { + return [[SDLSecurityQueryPayload alloc] initWithData:data]; +} +@end + +NS_ASSUME_NONNULL_END |