summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLV2ProtocolMessage.m
diff options
context:
space:
mode:
authorJoel Fischer <joeljfischer@gmail.com>2016-05-25 08:44:23 -0400
committerJoel Fischer <joeljfischer@gmail.com>2016-05-25 08:44:23 -0400
commitfb8e9903a323acaf5fc78819bb3c203567542ab2 (patch)
treee40665103ac7db492e0a40e34cd92f3390defa55 /SmartDeviceLink/SDLV2ProtocolMessage.m
parentf7540a02262832e34c67b0953dd8a1804a046fea (diff)
downloadsdl_ios-fb8e9903a323acaf5fc78819bb3c203567542ab2.tar.gz
Shift files into root directory
Diffstat (limited to 'SmartDeviceLink/SDLV2ProtocolMessage.m')
-rw-r--r--SmartDeviceLink/SDLV2ProtocolMessage.m60
1 files changed, 60 insertions, 0 deletions
diff --git a/SmartDeviceLink/SDLV2ProtocolMessage.m b/SmartDeviceLink/SDLV2ProtocolMessage.m
new file mode 100644
index 000000000..683c10335
--- /dev/null
+++ b/SmartDeviceLink/SDLV2ProtocolMessage.m
@@ -0,0 +1,60 @@
+// SDLSmartDeviceLinkV2ProtocolMessage.m
+//
+
+#import "SDLV2ProtocolMessage.h"
+#import "SDLFunctionID.h"
+#import "SDLJsonDecoder.h"
+#import "SDLNames.h"
+#import "SDLProtocolHeader.h"
+#import "SDLRPCPayload.h"
+
+
+@implementation SDLV2ProtocolMessage
+
+- (instancetype)initWithHeader:(SDLProtocolHeader *)header andPayload:(NSData *)payload {
+ if (self = [self init]) {
+ self.header = header;
+ self.payload = payload;
+ }
+ return self;
+}
+
+// Convert RPC payload to dictionary (for consumption by RPC layer)
+- (NSDictionary *)rpcDictionary {
+ // Only applicable to RPCs
+ if ((self.header.serviceType != SDLServiceType_RPC) && (self.header.serviceType != SDLServiceType_BulkData)) {
+ return nil;
+ }
+
+ NSMutableDictionary *rpcMessageAsDictionary = [[NSMutableDictionary alloc] init];
+
+ // Parse the payload as RPC struct
+ SDLRPCPayload *rpcPayload = [SDLRPCPayload rpcPayloadWithData:self.payload];
+
+ // Create the inner dictionary with the RPC properties
+ NSMutableDictionary *innerDictionary = [[NSMutableDictionary alloc] init];
+ NSString *functionName = [[[SDLFunctionID alloc] init] getFunctionName:rpcPayload.functionID];
+ [innerDictionary setObject:functionName forKey:NAMES_operation_name];
+ [innerDictionary setObject:[NSNumber numberWithInt:rpcPayload.correlationID] forKey:NAMES_correlationID];
+
+ // Get the json data from the struct
+ if (rpcPayload.jsonData) {
+ NSDictionary *jsonDictionary = [[SDLJsonDecoder instance] decode:rpcPayload.jsonData];
+ if (jsonDictionary) {
+ [innerDictionary setObject:jsonDictionary forKey:NAMES_parameters];
+ }
+ }
+
+ // Store it in the containing dictionary
+ UInt8 rpcType = rpcPayload.rpcType;
+ NSArray *rpcTypeNames = @[ NAMES_request, NAMES_response, NAMES_notification ];
+ [rpcMessageAsDictionary setObject:innerDictionary forKey:rpcTypeNames[rpcType]];
+
+ // The bulk data also goes in the dictionary
+ if (rpcPayload.binaryData) {
+ [rpcMessageAsDictionary setObject:rpcPayload.binaryData forKey:NAMES_bulkData];
+ }
+
+ return rpcMessageAsDictionary;
+}
+@end