summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLProtocolHeader.m
blob: 2c193fecbc28f1d43abf717b411e76ebecd19081 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//  SDLProtocolHeader.m
//


#import "SDLProtocolHeader.h"
#import "SDLV1ProtocolHeader.h"
#import "SDLV2ProtocolHeader.h"

NS_ASSUME_NONNULL_BEGIN

@implementation SDLProtocolHeader

@synthesize version = _version;
@synthesize size = _size;


- (instancetype)init {
    if (self = [super init]) {
        _version = 0;
        _size = 0;
    }
    return self;
}

- (BOOL)compressed {
    return _encrypted;
}

- (void)setCompressed:(BOOL)compressed {
    _encrypted = compressed;
}

- (id)copyWithZone:(nullable NSZone *)zone {
    [self doesNotRecognizeSelector:_cmd];
    return 0;
}

- (nullable NSData *)data {
    [self doesNotRecognizeSelector:_cmd];
    return nil;
}

- (void)parse:(NSData *)data {
    [self doesNotRecognizeSelector:_cmd];
}

- (NSString *)description {
    NSString *description = [NSString stringWithFormat:@"<%@: %p>", NSStringFromClass([self class]), self];
    return description;
}

+ (__kindof SDLProtocolHeader *)headerForVersion:(UInt8)version {
    // VERSION DEPENDENT CODE
    switch (version) {
        case 1: {
            return [[SDLV1ProtocolHeader alloc] init];
        } break;
        case 2: // Fallthrough
        case 3: // Fallthrough
        case 4: // Fallthrough
        case 5: {
            return [[SDLV2ProtocolHeader alloc] initWithVersion:version];
        } break;
        default: {
            // Assume V2 header for unknown header versions and hope it doesn't break
            return [[SDLV2ProtocolHeader alloc] initWithVersion:version];
        } break;
    }
}

// For use in decoding a stream of bytes.
// Pass in bytes representing message (or beginning of message)
// Looks at and parses first byte to determine version.
+ (UInt8)determineVersion:(NSData *)data {
    UInt8 firstByte = ((UInt8 *)data.bytes)[0];
    UInt8 version = firstByte >> 4;
    return version;
}

@end

NS_ASSUME_NONNULL_END