-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackets.py
293 lines (223 loc) · 7.38 KB
/
packets.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
"""
The WolfWare packet definitions
Jens Dede <[email protected]>
"""
import struct
import binascii
import random
# The base and most generic packet type
class BasePacket:
TYPE_UNDEFINED = 0x0
TYPE_ACK = 0x1
TYPE_STATUS = 0x2
TYPE_CONFIG = 0x3
TYPE_BEACON = 0x4
TYPE_ACTOR_UNIVERSAL = 0x10
BIT_BROADCAST = 0
BIT_IS_ACK = 1
BIT_ACK_REQUEST = 2
version = 0x0
sender = None
config = 0x0
receiver = None
msg_type = TYPE_UNDEFINED
data = b""
seq = None
rssi = None
snr = None
def __init__(self, sender=None):
self.sender = sender
def __str__(self, additional=None):
ret = ""
ret += "Version: " + str(self.get_version()) + "\n"
ret += "Sequence: " + str(self.get_sequence()) + "\n"
ret += "Sender: " + str(self.get_sender()) + "\n"
ret += "Receiver: " + str(self.get_receiver()) + "\n"
ret += "Type: " + str(self.get_type()) + "\n"
ret += "Is Broadcast: " + str(self.get_broadcast()) + "\n"
ret += "ACK request: " + str(self.get_ack_request()) + "\n"
ret += "Is it an ACK: " + str(self.get_is_ack()) + "\n"
ret += "SNR: " + str(self.get_snr()) + "\n"
ret += "RSSI: " + str(self.get_rssi()) + "\n"
if additional:
ret += additional
else:
ret += "Data: " + str(self.data)
return ret
def get_version(self):
return self.version
def set_version(self, version):
self.version = version
def set_sequence(self, seq):
self.seq = seq
def get_sequence(self):
return self.seq
def get_sender(self):
return self.sender
def set_sender(self, sender):
self.sender = sender
def get_receiver(self):
return self.receiver
def set_receiver(self, receiver):
self.set_broadcast(receiver==None)
self.receiver = receiver
def get_type(self):
return self.msg_type
def get_snr(self):
return self.snr
def set_snr(self, snr):
self.snr = snr
def get_rssi(self):
return self.rssi
def set_rssi(self, rssi):
self.rssi = rssi
def set_type(self, t):
self.msg_type = t
def get_data(self):
return self.data
def set_data(self, data):
if type(data) != bytes:
raise ValueError("data should be bytes")
self.data = data
def get_data_len(self):
return len(self.data)
def set_broadcast(self, bc=True):
if bc:
self.config = self.config | (1<<self.BIT_BROADCAST)
else:
self.config = self.config & ~(1<<self.BIT_BROADCAST)
def get_broadcast(self):
return bool(self.config & (1<<self.BIT_BROADCAST))
def set_ack_request(self, ack=True):
if ack:
self.config = self.config | (1<<self.BIT_ACK_REQUEST)
else:
self.config = self.config & ~(1<<self.BIT_ACK_REQUEST)
def get_ack_request(self):
return bool(self.config & (1<<self.BIT_ACK_REQUEST))
def set_is_ack(self, ack=True):
if ack:
self.config = self.config | (1<<self.BIT_IS_ACK)
else:
self.config = self.config & ~(1<<self.BIT_IS_ACK)
def get_is_ack(self):
return bool(self.config & (1<<self.BIT_IS_ACK))
def get_config(self):
return self.config
def create_packet(self):
if self.sender == None:
raise ValueError("Sender not set")
if not self.seq:
self.seq = random.randint(0, 65535)
p = struct.pack("!BHIB", self.version, self.seq, self.sender, self.config)
if not self.get_broadcast():
if not self.receiver:
raise ValueError("Recevier not set")
p = p + struct.pack("!I", self.receiver)
p = p + struct.pack("!BB", self.msg_type, len(self.data))
p = p + self.data
p = p + struct.pack("!L", binascii.crc32(p))
return p
def parse_packet(self, packet):
if len(packet) < 11:
print("Packet too short")
return False
if struct.unpack("!L", packet[-4:])[0] != binascii.crc32(packet[:-4]):
print("Invalid CRC32")
return False
p = packet[:-4] # rm crc
if len(p) < 8:
print("Packet too short")
return False
self.version, self.seq, self.sender, self.config = struct.unpack("!BHIB", p[:8])
p = p[8:]
if not self.get_broadcast():
self.receiver, = struct.unpack("!I", p[:4])
p = p[4:]
if len(p) < 2:
print("Packet too short")
return False
self.msg_type, datalen = struct.unpack("!BB", p[:2])
p = p[2:]
if len(p) != datalen:
print("Invalid len")
return False
self.data = p
return True
def create_ack(self):
if not self.get_ack_request() or self.get_broadcast():
# No ACK requested or is broadcast
return None
return AckPacket(self.get_receiver(), self.get_sender(), self.get_sequence()).create_packet()
# Actor packet type
class UniversalPacket(BasePacket):
def __init__(self, sender = None):
super().__init__(sender)
self.msg_type = self.TYPE_ACTOR_UNIVERSAL
def __str__(self):
data = self.get_params()
ret = ""
ret += "Cancel prev: " + str(data[0]) + "\n"
return super().__str__(ret)
def set_params(self, cancel_prev):
self.data = struct.pack("!b", bool(cancel_prev))
def get_params(self):
if len(self.data) == 1:
return struct.unpack("!b", self.data)
else:
raise ValueError("Wong Packet format")
# The ACK packet type
class AckPacket(BasePacket):
def __init__(self, sender=None, receiver=None, sequence=None):
super().__init__(sender)
self.set_receiver(receiver)
self.set_sequence(sequence)
self.set_is_ack()
self.set_type(self.TYPE_ACK)
class BeaconPacket(BasePacket):
def __init__(self, sender=None, bat=(None, None)):
super().__init__(sender)
self.set_broadcast()
self.set_type(self.TYPE_BEACON)
if bat != (None, None):
self.data = struct.pack("!II", bat[0], bat[1])
def getBattery(self):
if len(self.data) == 8:
return struct.unpack("!II", self.data)
return None
def __str__(self):
data = self.getBattery()
ret = ""
if data:
ret += "Percent: " + str(data[0]) + "\n"
ret += "Volt: " + str(data[1]/1000) + "\n"
return super().__str__(ret)
if __name__ == "__main__":
# Just a little bit of testing
p = BasePacket(123)
print(p)
p.set_ack_request()
#p.set_broadcast()
p.set_receiver(332)
#p.set_is_ack()
p.set_data(b"TEST")
print(p)
print(p.get_config())
bin_packet = p.create_packet()
print("LEN", len(bin_packet))
p2 = BasePacket()
if p2.parse_packet(bin_packet):
print("Packet successfully parsed")
print(p2)
f = UniversalPacket(321)
f.set_ack_request()
bp = f.create_packet()
f2 = UniversalPacket()
f2.parse_packet(bp)
print(f2)
ack = f2.create_ack()
print(ack)
p3 = BasePacket()
p3.parse_packet(ack)
print(p3)