blob: 0760d45c70bf2021c7b524b3623ed861e3faed60 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
// SDLSmartDeviceLinkV2ProtocolMessage.m
//
#import "SDLV2ProtocolMessage.h"
#import "SDLFunctionID.h"
#import "SDLLogMacros.h"
#import "SDLProtocolHeader.h"
#import "SDLRPCPayload.h"
NS_ASSUME_NONNULL_BEGIN
@implementation SDLV2ProtocolMessage
- (instancetype)initWithHeader:(SDLProtocolHeader *)header andPayload:(nullable NSData *)payload {
if (self = [self init]) {
self.header = header;
self.payload = payload;
}
return self;
}
// Convert RPC payload to dictionary (for consumption by RPC layer)
- (nullable NSDictionary<NSString *, id> *)rpcDictionary {
// Only applicable to RPCs
if ((self.header.serviceType != SDLServiceTypeRPC) && (self.header.serviceType != SDLServiceTypeBulkData)) {
return nil;
}
NSMutableDictionary<NSString *, id> *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 <NSString *, id> *innerDictionary = [[NSMutableDictionary alloc] init];
NSString *functionName = [[SDLFunctionID sharedInstance] functionNameForId:rpcPayload.functionID];
[innerDictionary setObject:functionName forKey:SDLNameOperationName];
[innerDictionary setObject:[NSNumber numberWithInt:rpcPayload.correlationID] forKey:SDLNameCorrelationId];
// Get the json data from the struct
if (rpcPayload.jsonData) {
NSError *error = nil;
NSDictionary<NSString *, id> * jsonDictionary = [NSJSONSerialization JSONObjectWithData:rpcPayload.jsonData options:kNilOptions error:&error];
if (error != nil) {
SDLLogE(@"Error decoding JSON data: %@", error);
} else if (jsonDictionary) {
[innerDictionary setObject:jsonDictionary forKey:SDLNameParameters];
}
}
// Store it in the containing dictionary
UInt8 rpcType = rpcPayload.rpcType;
NSArray<NSString *> *rpcTypeNames = @[SDLNameRequest, SDLNameResponse, SDLNameNotification];
[rpcMessageAsDictionary setObject:innerDictionary forKey:rpcTypeNames[rpcType]];
// The bulk data also goes in the dictionary
if (rpcPayload.binaryData) {
[rpcMessageAsDictionary setObject:rpcPayload.binaryData forKey:SDLNameBulkData];
}
return rpcMessageAsDictionary;
}
@end
NS_ASSUME_NONNULL_END
|