-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyeenet_router.py
executable file
·351 lines (297 loc) · 11.6 KB
/
yeenet_router.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import serial
from cobs import cobs
from serial.serialposix import Serial
from packet_record import packet_record
import struct
from enum import IntEnum
import math
class BadModulation(Exception):
pass
class loraBW(IntEnum):
BW_7800 = 0
BW_10400 = 1
BW_15600 = 2
BW_20800 = 3
BW_31250 = 4
BW_41700 = 5
BW_62500 = 6
BW_125000 = 7
BW_250000 = 8
BW_500000 = 9
def get_bandwidth(bw):
if(bw==loraBW.BW_7800):
return 7800
if(bw==loraBW.BW_10400):
return 10400
if(bw==loraBW.BW_15600):
return 15600
if(bw==loraBW.BW_20800):
return 20800
if(bw==loraBW.BW_31250):
return 31250
if(bw==loraBW.BW_41700):
return 41700
if(bw==loraBW.BW_62500):
return 62500
if(bw==loraBW.BW_125000):
return 125000
if(bw==loraBW.BW_250000):
return 250000
if(bw==loraBW.BW_500000):
return 500000
class lora_modulation():
def __init__(self,sf : int,bw : loraBW,cr : int,header_enabled : bool,crc_enabled : bool,preamble_length : int,payload_length : int,frequency : int):
if(sf>=6 or sf<=12):
sf_byte = int.to_bytes(sf-6,length=1,byteorder='little')
else:
raise BadModulation("spreading factor isn't the right value")
#print("sf byte: " + str(sf_byte.hex()))
if(type(bw)==loraBW):
bw_byte = int.to_bytes(int(bw),length=1,byteorder='little')
else:
raise BadModulation("bandwidth isn't the right type")
#print("bw byte: " + str(bw_byte.hex()))
if(cr>=5 or cr<=8):
cr_byte = int.to_bytes(int(cr-5),length=1,byteorder='little')
else:
raise BadModulation("coding rate needs to be 4/X where X is 5,6,7,8")
#print("cr byte: " + str(cr_byte.hex()))
if(type(header_enabled)==bool):
header_enabled_byte = bytes.fromhex('00')
if(header_enabled):
header_enabled_byte = bytes.fromhex('01')
else:
raise BadModulation("coding rate needs to be boolean")
#print("header_enabled byte: " + str(header_enabled_byte.hex()))
if(type(crc_enabled)==bool):
crc_enabled_byte = bytes.fromhex('00')
if(crc_enabled):
crc_enabled_byte = bytes.fromhex('01')
else:
raise BadModulation("coding rate needs to be boolean")
#print("crc_enabled byte: " + str(crc_enabled_byte.hex()))
if(type(preamble_length) == int and preamble_length>=0 and preamble_length<2**16):
preamable_bytes = int.to_bytes(preamble_length,length=2,byteorder='little')
else:
raise BadModulation("preamable length is nonnegative 2 bytes")
#print("preamable_length bytes: " + str(preamable_bytes.hex()))
if(type(payload_length) == int and payload_length>=0 and payload_length<2**8):
payload_length_byte = int.to_bytes(payload_length,length=1,byteorder='little')
else:
raise BadModulation("payload length is 1 byte")
#print("payload_length byte: " + str(payload_length_byte.hex()))
if(type(frequency) == int and frequency>=0 and frequency<2**32):
frequency_bytes = int.to_bytes(frequency,length=4,byteorder='little')
else:
raise BadModulation("frequency is 4 bytes")
#print("frequency bytes: " + str(frequency_bytes.hex()))
self.command = sf_byte + bw_byte + cr_byte + header_enabled_byte + crc_enabled_byte + preamable_bytes + payload_length_byte + frequency_bytes
class RouterCOBSError(Exception):
pass
class RouterCommandParseError(Exception):
pass
class InvalidFrameHeaderError(Exception):
pass
class InvalidResponseLength(Exception):
pass
class InputLengthError(Exception):
pass
class InvalidResponse(Exception):
pass
default_baudrate = 115200
import socket
class remote_serial:
def __init__(self,addr,port):
# create an INET, STREAMing socket
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# now connect to the web server on port 80 - the normal http port
self.s.connect((addr,port))
def close(self):
self.s.close()
def exchange(self,command):
self.s.send(command)
return self.s.recv(1024)
class yeenet_router:
def __init__(self,serial_device):
self.iface = serial_device
if(type(self.iface)==remote_serial):
self.is_remote = True
elif(type(self.iface==Serial)):
self.is_remote = False
else:
raise Exception("need Serial or remote_serial")
ROUTER_BUF_SIZE = 300
FRAME_DELIMETER = bytes.fromhex('00')
RDY_FRAME = bytes.fromhex('00')
COBSERR_FRAME = bytes.fromhex('01')
PARSEERR_FRAME = bytes.fromhex('02')
RESET = bytes.fromhex('00')
ECHO = bytes.fromhex('01')
BUFFER_POP = bytes.fromhex('02')
BUFFER_CAP = bytes.fromhex('03')
MODEM_SETUP = bytes.fromhex('04')
MODEM_LISTEN = bytes.fromhex('05')
MODEM_LOAD_PAYLOAD = bytes.fromhex('06')
MODEM_LOAD_AND_TRANSMIT = bytes.fromhex('07')
MODEM_TRANSMIT = bytes.fromhex('08')
MODEM_STANDBY = bytes.fromhex('09')
MODEM_IS_CLEAR = bytes.fromhex('0A')
MODEM_GET_LAST_PAYLOAD_RSSI = bytes.fromhex('0B')
MODEM_GET_LAST_PAYLOAD_SNR = bytes.fromhex('0C')
MODEM_GET_AIRTIME_USEC = bytes.fromhex('0D')
MODEM_SET_MODULATION = bytes.fromhex('0E')
MODEM_GET_LOCAL_ADDRESS = bytes.fromhex('0F')
BUFFER_GET_N_OVERFLOW = bytes.fromhex('10')
BUFFER_RESET_N_OVERFLOW = bytes.fromhex('11')
def close(self):
#reset router
self.iface.close()
def exchange_blocking(self,command: bytes):
if(self.is_remote):
decoded = self.iface.exchange(command)
else:
encoded = cobs.encode(command) + self.FRAME_DELIMETER
self.iface.write(encoded)
incoming = self.iface.read_until(self.FRAME_DELIMETER)
decoded = cobs.decode(incoming[:-1])
if(decoded[:1] == self.RDY_FRAME):
pass
elif(decoded[:1] == self.COBSERR_FRAME):
raise RouterCOBSError(decoded)
elif(decoded[:1] == self.PARSEERR_FRAME):
raise RouterCommandParseError(decoded)
else:
raise InvalidFrameHeaderError(decoded)
data_present = len(decoded)>1
if(data_present):
return decoded[1:]
else:
return
#takes bytes input
def echo(self,echo_bytes: bytes) -> bytes:
command = self.ECHO + echo_bytes
return self.exchange_blocking(command)
def reset(self):
command = self.RESET
self.exchange_blocking(command)
def buffer_pop(self):
command = self.BUFFER_POP
retval = self.exchange_blocking(command)
if(len(retval)==0):
raise InvalidResponseLength(retval)
packets_waiting = int(retval[0])
if(packets_waiting==0):
return [0,None]
if(len(retval)<10):
raise InvalidResponseLength(retval)
rssi = int.from_bytes(retval[1:5],byteorder='little',signed=True)
snr = struct.unpack('f',retval[5:9])[0]
data = retval[9:]
return [packets_waiting, packet_record(rssi,snr,data)]
def buffer_cap(self):
command = self.BUFFER_CAP
retval = self.exchange_blocking(command)
if(len(retval)!=1):
raise InvalidResponseLength(retval)
return int(retval[0])
def modem_setup(self):
command = self.MODEM_SETUP
self.exchange_blocking(command)
def modem_listen(self):
command = self.MODEM_LISTEN
self.exchange_blocking(command)
def modem_load_payload(self, data : bytes):
command = self.MODEM_LOAD_PAYLOAD
command = command + data
retval = self.exchange_blocking(command)
if(len(retval)!=1):
raise InvalidResponseLength(retval)
if(retval[0]==bytes.fromhex('00')):
return
if(retval[0]==bytes.fromhex('01')):
raise InputLengthError("packet data cannot be empty!")
if(retval[0]==bytes.fromhex('02')):
raise InputLengthError("packet data is too long!")
def modem_load_and_transmit(self, data : bytes):
command = self.MODEM_LOAD_AND_TRANSMIT
command = command + data
#print(command)
retval = self.exchange_blocking(command)
if(len(retval)!=1):
raise InvalidResponseLength(retval)
if(retval[:1]==bytes.fromhex('00')):
return
if(retval[:1]==bytes.fromhex('01')):
raise InputLengthError("packet data cannot be empty!")
if(retval[:1]==bytes.fromhex('02')):
raise InputLengthError("packet data is too long!")
raise InvalidResponse()
def modem_transmit(self):
command = self.MODEM_TRANSMIT
self.exchange_blocking(command)
def modem_standby(self):
command = self.MODEM_STANDBY
self.exchange_blocking(command)
def modem_is_clear(self):
command = self.MODEM_IS_CLEAR
retval = self.exchange_blocking(command)
if(len(retval)!=1):
raise InvalidResponseLength(retval)
if(retval[:1]==bytes.fromhex('00')):
return False
if(retval[:1]==bytes.fromhex('01')):
return True
raise InvalidResponse()
def modem_get_last_payload_rssi(self):
command = self.MODEM_GET_LAST_PAYLOAD_RSSI
retval = self.exchange_blocking(command)
if(len(retval != 4)):
raise InvalidResponseLength(retval)
return retval
def modem_get_last_payload_snr(self):
command = self.MODEM_GET_LAST_PAYLOAD_SNR
retval = self.exchange_blocking(command)
if(len(retval != 4)):
raise InvalidResponseLength(retval)
return retval
def modem_get_airtime_usec(self,length : int):
command = self.MODEM_GET_AIRTIME_USEC
command = command + int.to_bytes(length,length=1,byteorder='little')
retval = self.exchange_blocking(command)
if(len(retval)==0):
raise InvalidResponseLength
elif(retval[:1] == bytes.fromhex('00')):
if(len(retval)==5):
return int.from_bytes(retval[1:],"little")
else:
raise InvalidResponseLength(retval)
elif(retval[:1]==bytes.fromhex('01')):
raise InputLengthError('length of packet must be a one byte value')
else:
raise InvalidResponse()
def modem_set_modulation(self,modulation : lora_modulation):
command = self.MODEM_SET_MODULATION + modulation.command
retval = self.exchange_blocking(command)
#print("got back:" + str(retval))
if(len(retval)!=1):
raise InvalidResponseLength
if(retval[:1] == bytes.fromhex('00')):
return
if(retval[:1] == bytes.fromhex('01')):
raise BadModulation("modem reports bad command")
raise InvalidResponse()
def modem_get_local_address(self):
command = self.MODEM_GET_LOCAL_ADDRESS
retval = self.exchange_blocking(command)
#print("got back:" + str(retval))
if(len(retval)!=1):
raise InvalidResponseLength
return int.from_bytes(retval[0])
def buffer_get_n_overflow(self):
command = self.BUFFER_GET_N_OVERFLOW
retval = self.exchange_blocking(command)
return int.from_bytes(retval,byteorder='little')
def buffer_reset_n_overflow(self):
command = self.BUFFER_RESET_N_OVERFLOW
self.exchange_blocking(command)
return