1
|
|
|
from libAnt.constants import * |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class Message: |
5
|
|
|
def __init__(self, type: int, content: bytes): |
6
|
|
|
self._type = type |
7
|
|
|
self._content = content |
8
|
|
|
|
9
|
|
|
def __len__(self): |
10
|
|
|
return len(self._content) |
11
|
|
|
|
12
|
|
|
def __iter__(self): |
13
|
|
|
return self._content |
14
|
|
|
|
15
|
|
|
def __str__(self): |
16
|
|
|
return '({:02X}): '.format(self._type) + ' '.join('{:02X}'.format(x) for x in self._content) |
17
|
|
|
|
18
|
|
|
def checksum(self) -> int: |
19
|
|
|
chk = MESSAGE_TX_SYNC ^ len(self) ^ self._type |
20
|
|
|
for b in self._content: |
21
|
|
|
chk ^= b |
22
|
|
|
return chk |
23
|
|
|
|
24
|
|
|
def encode(self) -> bytes: |
25
|
|
|
b = bytearray([MESSAGE_TX_SYNC, len(self), self._type]) |
26
|
|
|
b.extend(self._content) |
27
|
|
|
b.append(self.checksum()) |
28
|
|
|
return bytes(b) |
29
|
|
|
|
30
|
|
|
@property |
31
|
|
|
def type(self) -> int: |
32
|
|
|
return self._type |
33
|
|
|
|
34
|
|
|
@property |
35
|
|
|
def content(self) -> bytes: |
36
|
|
|
return self._content |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
class BroadcastMessage(Message): |
40
|
|
|
def __init__(self, channel: int, content: bytes): |
41
|
|
|
c = bytearray([channel]) |
42
|
|
|
c.extend(content[:8]) |
43
|
|
|
if len(content) > 8: # Extended message |
44
|
|
|
self._flag = content[8] |
45
|
|
|
self._extendedContent = content[len(content) - 9:] |
46
|
|
|
offset = 0 |
47
|
|
|
if self._flag & EXT_FLAG_CHANNEL_ID: |
48
|
|
|
self._deviceNumber = int.from_bytes(self._extendedContent[:2], byteorder='little', signed=False) |
49
|
|
|
self._deviceType = self._extendedContent[2] |
50
|
|
|
self._transType = self._extendedContent[3] |
51
|
|
|
offset += 4 |
52
|
|
|
if self._flag & EXT_FLAG_RSSI: |
53
|
|
|
rssi = self._extendedContent[len(self._extendedContent) - offset:] |
54
|
|
|
self._rssiMeasurementType = rssi[0] |
55
|
|
|
self._rssi = rssi[1] |
56
|
|
|
self._rssiThreshold = rssi[2] |
57
|
|
|
offset += 3 |
58
|
|
|
if self._flag & EXT_FLAG_TIMESTAMP: |
59
|
|
|
self._rxTimestamp = int.from_bytes(self._extendedContent[len(self._extendedContent) - offset:], |
60
|
|
|
byteorder='little', signed=False) |
61
|
|
|
|
62
|
|
|
super().__init__(MESSAGE_CHANNEL_BROADCAST_DATA, bytes(c)) |
63
|
|
|
|
64
|
|
|
def __str__(self): |
65
|
|
|
pass |
66
|
|
|
|
67
|
|
|
def checksum(self) -> int: |
68
|
|
|
pass |
69
|
|
|
|
70
|
|
|
def encode(self) -> bytes: |
71
|
|
|
pass |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
class SystemResetMessage(Message): |
75
|
|
|
def __init__(self): |
76
|
|
|
super().__init__(MESSAGE_SYSTEM_RESET, b'0') |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
class SetNetworkKeyMessage(Message): |
80
|
|
|
def __init__(self, channel: int, key: bytes): |
81
|
|
|
content = bytearray([channel]) |
82
|
|
|
content.extend(key) |
83
|
|
|
super().__init__(MESSAGE_NETWORK_KEY, bytes(content)) |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
class AssignChannelMessage(Message): |
87
|
|
|
def __init__(self, channel: int, type: int, network: int = 0, extended: int = None): |
88
|
|
|
content = bytearray([channel, type, network]) |
89
|
|
|
if extended is not None: |
90
|
|
|
content.append(extended) |
91
|
|
|
super().__init__(MESSAGE_CHANNEL_ASSIGN, bytes(content)) |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
class SetChannelIdMessage(Message): |
95
|
|
|
def __init__(self, channel: int, deviceNumber: int = 0, deviceType: int = 0, transType: int = 0): |
96
|
|
|
content = bytearray([channel]) |
97
|
|
|
content.extend(deviceNumber.to_bytes(2, byteorder='big')) |
98
|
|
|
content.append(deviceType) |
99
|
|
|
content.append(transType) |
100
|
|
|
super().__init__(MESSAGE_CHANNEL_ID, bytes(content)) |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
class SetChannelRfFrequencyMessage(Message): |
104
|
|
|
def __init__(self, channel: int, frequency: int = 2457): |
105
|
|
|
content = bytes([channel, frequency - 2400]) |
106
|
|
|
super().__init__(MESSAGE_CHANNEL_FREQUENCY, content) |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
class OpenRxScanModeMessage(Message): |
110
|
|
|
def __init__(self): |
111
|
|
|
super().__init__(OPEN_RX_SCAN_MODE, bytes([0])) |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
class EnableExtendedMessagesMessage(Message): |
115
|
|
|
def __init__(self, enable: bool = True): |
116
|
|
|
content = bytes([0, int(enable)]) |
117
|
|
|
super().__init__(MESSAGE_ENABLE_EXT_RX_MESSAGES, content) |
118
|
|
|
|
119
|
|
|
class LibConfigMessage(Message): |
120
|
|
|
def __init__(self, rxTimestamp: bool = True, rssi: bool = True, channelId: bool = True): |
121
|
|
|
config = 0 |
122
|
|
|
if rxTimestamp: |
123
|
|
|
config |= EXT_FLAG_TIMESTAMP |
124
|
|
|
if rssi: |
125
|
|
|
config |= EXT_FLAG_RSSI |
126
|
|
|
if channelId: |
127
|
|
|
config |= EXT_FLAG_CHANNEL_ID |
128
|
|
|
super().__init__(MESSAGE_LIB_CONFIG, bytes([0, config])) |