Tong
2021-09-06 5017131971e6ac8f31adc2ed1e25583738b420a2
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
//
//  BlufiFrameCtrlData.m
//  EspBlufi
//
//  Created by AE on 2020/6/9.
//  Copyright © 2020 espressif. All rights reserved.
//
 
#import "BlufiFrameCtrlData.h"
 
@interface BlufiFrameCtrlData()
 
@property(assign, nonatomic,)Byte value;
 
@end
 
@implementation BlufiFrameCtrlData
 
enum {
    PositionEncrypted = 0,
    PositionChecksum,
    PositionDataDirection,
    PositionRequireAck,
    PositionFrag,
};
 
- (instancetype)initWithValue:(Byte)value {
    self = [super init];
    if (self) {
        _value = value;
    }
    return self;
}
 
- (BOOL)check:(uint32_t)position {
    return (_value >> position & 1) == 1;
}
 
- (BOOL)isEncrypted {
    return [self check:PositionEncrypted];
}
 
- (BOOL)isChecksum {
    return [self check:PositionChecksum];
}
 
- (BOOL)isAckRequirement {
    return [self check:PositionRequireAck];
}
 
- (BOOL)hasFrag {
    return [self check:PositionFrag];
}
 
+ (Byte)getFrameCtrlValueWithEncrypted:(BOOL)encrypted checksum:(BOOL)checksum direction:(DataDirection)direction requireAck:(BOOL)ack hasFrag:(BOOL)frag {
    Byte frame = 0;
    if (encrypted) {
        frame |= (1 << PositionEncrypted);
    }
    if (checksum) {
        frame |= (1 << PositionChecksum);
    }
    if (direction == DataInput) {
        frame |= (1 << PositionDataDirection);
    }
    if (ack) {
        frame |= (1 << PositionRequireAck);
    }
    if (frag) {
        frame |= (1 << PositionFrag);
    }
    return frame;
}
 
@end