summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLV2ProtocolMessage.m
blob: 4e50b6df1359fb310fd104fec7b619f9157b9b5c (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
66
67
68
//  SDLSmartDeviceLinkV2ProtocolMessage.m
//

#import "SDLV2ProtocolMessage.h"
#import "SDLFunctionID.h"
#import "SDLLogMacros.h"
#import "SDLProtocolHeader.h"
#import "SDLRPCPayload.h"
#import "SDLRPCParameterNames.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;

        self.header.bytesInPayload = (UInt32)self.payload.length;
    }
    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[SDLRPCParameterNameOperationName] = functionName;
    innerDictionary[SDLRPCParameterNameCorrelationId] = @(rpcPayload.correlationID);

    // 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:SDLRPCParameterNameParameters];
        }
    }

    // Store it in the containing dictionary
    UInt8 rpcType = rpcPayload.rpcType;
    NSArray<NSString *> *rpcTypeNames = @[SDLRPCParameterNameRequest, SDLRPCParameterNameResponse, SDLRPCParameterNameNotification];
    [rpcMessageAsDictionary setObject:innerDictionary forKey:rpcTypeNames[rpcType]];

    // The bulk data also goes in the dictionary
    if (rpcPayload.binaryData) {
        [rpcMessageAsDictionary setObject:rpcPayload.binaryData forKey:SDLRPCParameterNameBulkData];
    }

    return rpcMessageAsDictionary;
}
@end

NS_ASSUME_NONNULL_END