-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathus2n.py
417 lines (367 loc) · 13.6 KB
/
us2n.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# us2n.py
import json
import time
import select
import socket
import machine
import network
print_ = print
VERBOSE = 1
def print(*args, **kwargs):
if VERBOSE:
print_(*args, **kwargs)
def read_config(filename='us2n.json', obj=None, default=None):
with open(filename, 'r') as f:
config = json.load(f)
if obj is None:
return config
return config.get(obj, default)
def parse_bind_address(addr, default=None):
if addr is None:
return default
args = addr
if not isinstance(args, (list, tuple)):
args = addr.rsplit(':', 1)
host = '' if len(args) == 1 or args[0] == '0' else args[0]
port = int(args[1])
return host, port
class RINGBUFFER:
def __init__(self, size):
self.data = bytearray(size)
self.size = size
self.index_put = 0
self.index_get = 0
self.index_rewind = 0
self.wrapped = False
def put(self, data):
cur_idx = 0
while cur_idx < len(data):
min_idx = min(self.index_put+len(data)-cur_idx, self.size)
self.data[self.index_put:min_idx] = data[cur_idx:min_idx-self.index_put+cur_idx]
cur_idx += min_idx-self.index_put
if self.index_get > self.index_put:
self.index_get = max(min_idx+1, self.index_get)
if self.index_get >= self.size:
self.index_get -= self.size
self.index_put = min_idx
if self.index_put == self.size:
self.index_put = 0
self.wrapped = True
if self.index_get == 0:
self.index_get = 1
def putc(self, value):
next_index = (self.index_put + 1) % self.size
self.data[self.index_put] = value
self.index_put = next_index
# check for overflow
if self.index_get == self.index_put:
self.index_get = (self.index_get + 1) % self.size
return value
def get(self, numbytes):
data = bytearray()
while len(data) < numbytes:
start = self.index_get
min_idx = min(self.index_get+numbytes-len(data), self.size)
if self.index_put >= self.index_get:
min_idx = min(min_idx, self.index_put)
data.extend(self.data[start:min_idx])
self.index_get = min_idx
if self.index_get == self.size:
self.index_get = 0
if self.index_get == self.index_put:
break
return data
def getc(self):
if not self.has_data():
return None ## buffer empty
else:
value = self.data[self.index_get]
self.index_get = (self.index_get + 1) % self.size
return value
def has_data(self):
return self.index_get != self.index_put
def rewind(self):
if self.wrapped:
self.index_get = (self.index_put+1) % self.size
else:
self.index_get = 0
def UART(config):
config = dict(config)
uart_type = config.pop('type') if 'type' in config.keys() else 'hw'
port = config.pop('port')
if uart_type == 'SoftUART':
print('Using SoftUART...')
uart = machine.SoftUART(machine.Pin(config.pop('tx')),machine.Pin(config.pop('rx')),timeout=config.pop('timeout'),timeout_char=config.pop('timeout_char'),baudrate=config.pop('baudrate'))
else:
print('Using HW UART...')
uart = machine.UART(port)
uart.init(**config)
return uart
class Bridge:
def __init__(self, config):
super().__init__()
self.config = config
self.uart = None
self.uart_port = config['uart']['port']
self.tcp = None
self.address = parse_bind_address(config['tcp']['bind'])
self.bind_port = self.address[1]
self.client = None
self.client_address = None
self.ring_buffer = RINGBUFFER(16 * 1024)
self.cur_line = bytearray()
self.state = 'listening'
self.uart = UART(self.config['uart'])
print('UART opened ', self.uart)
print(self.config)
def bind(self):
tcp = socket.socket()
tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# tcp.setblocking(False)
tcp.bind(self.address)
tcp.listen(5)
print('Bridge listening at TCP({0}) for UART({1})'
.format(self.bind_port, self.uart_port))
self.tcp = tcp
if 'ssl' in self.config:
import ntptime
ntptime.host = "pool.ntp.org"
while True:
try:
ntptime.settime()
except OSError as e:
print(f"NTP synchronization failed, {e}")
time.sleep(15)
continue
print(f"NTP synchronization succeeded, {time.time()}")
print(time.gmtime())
break
return tcp
def fill(self, fds):
if self.uart is not None:
fds.append(self.uart)
if self.tcp is not None:
fds.append(self.tcp)
if self.client is not None:
fds.append(self.client)
return fds
def recv(self, sock, n):
if hasattr(sock, 'recv'):
return sock.recv(n)
else:
# SSL-wrapped sockets don't have recv(), use read() instead
# TODO: Read more than 1 byte? Probably needs non-blocking sockets
return sock.read(1)
def sendall(self, sock, bytes):
if hasattr(sock, 'sendall'):
return sock.sendall(bytes)
else:
# SSL-wrapped sockets don't have sendall(), use write() instead
return sock.write(bytes)
def handle(self, fd):
if fd == self.tcp:
self.close_client()
self.open_client()
elif fd == self.client:
data = self.recv(self.client, 4096)
if data:
if self.state == 'enterpassword':
while len(data):
c = data[0:1]
data = data[1:]
if c == b'\n' or c == b'\r':
print("Received password {0}".format(self.password))
if self.password.decode('utf-8') == self.config['auth']['password']:
self.sendall(self.client, "\r\nAuthentication succeeded\r\n")
self.state = 'authenticated'
self.ring_buffer.rewind()
fd = self.uart # Send all uart data
break
else:
self.password = b""
self.sendall(self.client, "\r\nAuthentication failed\r\npassword: ")
else:
self.password += c
if self.state == 'authenticated':
print('TCP({0})->UART({1}) {2}'.format(self.bind_port,
self.uart_port, data))
self.uart.write(data)
else:
print('Client ', self.client_address, ' disconnected')
self.close_client()
if fd == self.uart:
data = self.uart.read(64)
if data is not None:
self.ring_buffer.put(data)
if self.state == 'authenticated' and self.ring_buffer.has_data():
data = self.ring_buffer.get(4096)
print('UART({0})->TCP({1}) {2}'.format(self.uart_port,
self.bind_port, data))
self.sendall(self.client, data)
def close_client(self):
if self.client is not None:
print('Closing client ', self.client_address)
self.client.close()
self.client = None
self.client_address = None
self.state = 'listening'
def open_client(self):
self.client, self.client_address = self.tcp.accept()
print('Accepted connection from ', self.client_address)
if 'ssl' in self.config:
import ussl
import ubinascii
print(time.gmtime())
sslconf = self.config['ssl'].copy()
for key in ['cadata', 'key', 'cert']:
if key in sslconf:
with open(sslconf[key], "rb") as file:
sslconf[key] = file.read()
# TODO: Setting CERT_REQUIRED produces MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
sslconf['cert_reqs'] = ussl.CERT_OPTIONAL
self.client = ussl.wrap_socket(self.client, server_side=True, **sslconf)
self.state = 'enterpassword' if 'auth' in self.config else 'authenticated'
self.password = b""
if self.state == 'enterpassword':
self.sendall(self.client, "password: ")
print("Prompting for password")
def close(self):
self.close_client()
if self.tcp is not None:
print('Closing TCP server {0}...'.format(self.address))
self.tcp.close()
self.tcp = None
class S2NServer:
def __init__(self, config):
self.config = config
def report_exception(self, e):
if 'syslog' in self.config:
try:
import usyslog
import io
import sys
stringio = io.StringIO()
sys.print_exception(e, stringio)
stringio.seek(0)
e_string = stringio.read()
s = usyslog.UDPClient(**self.config['syslog'])
s.error(e_string)
s.close()
except BaseException as e2:
sys.print_exception(e2)
def serve_forever(self):
while True:
config_network(self.config.get('wlan'), self.config.get('name'))
try:
self._serve_forever()
except KeyboardInterrupt:
print('Ctrl-C pressed. Bailing out')
break
except BaseException as e:
import sys
sys.print_exception(e)
self.report_exception(e)
time.sleep(1)
print("Restarting")
def bind(self):
bridges = []
for config in self.config['bridges']:
bridge = Bridge(config)
bridge.bind()
bridges.append(bridge)
return bridges
def _serve_forever(self):
bridges = self.bind()
try:
while True:
fds = []
for bridge in bridges:
bridge.fill(fds)
rlist, _, xlist = select.select(fds, (), fds)
if xlist:
print('Errors. bailing out')
break
for fd in rlist:
for bridge in bridges:
bridge.handle(fd)
finally:
for bridge in bridges:
bridge.close()
def config_lan(config, name):
# For a board which has LAN
pass
def config_wlan(config, name):
if config is None:
return None, None
return (WLANStation(config.get('sta'), name),
WLANAccessPoint(config.get('ap'), name))
def WLANStation(config, name):
if config is None:
return
config.setdefault('connection_attempts', -1)
essid = config['essid']
password = config['password']
attempts_left = config['connection_attempts']
sta = network.WLAN(network.STA_IF)
if not sta.isconnected():
while not sta.isconnected() and attempts_left != 0:
attempts_left -= 1
sta.disconnect()
sta.active(False)
sta.active(True)
sta.connect(essid, password)
print('Connecting to WiFi...')
n, ms = 20, 250
t = n*ms
while not sta.isconnected() and n > 0:
time.sleep_ms(ms)
n -= 1
if not sta.isconnected():
print('Failed to connect wifi station after {0}ms. I give up'
.format(t))
return sta
print('Wifi station connected as {0}'.format(sta.ifconfig()))
return sta
def WLANAccessPoint(config, name):
if config is None:
return
config.setdefault('essid', name)
config.setdefault('channel', 11)
config.setdefault('authmode',
getattr(network,'AUTH_' +
config.get('authmode', 'OPEN').upper()))
config.setdefault('hidden', False)
# config.setdefault('dhcp_hostname', name)
ap = network.WLAN(network.AP_IF)
if not ap.isconnected():
ap.active(True)
n, ms = 20, 250
t = n * ms
while not ap.active() and n > 0:
time.sleep_ms(ms)
n -= 1
if not ap.active():
print('Failed to activate wifi access point after {0}ms. ' \
'I give up'.format(t))
return ap
# ap.config(**config)
print('Wifi {0!r} connected as {1}'.format(ap.config('essid'),
ap.ifconfig()))
return ap
def config_network(config, name):
config_lan(config, name)
config_wlan(config, name)
def config_verbosity(config):
global VERBOSE
VERBOSE = config.setdefault('verbose', 1)
for bridge in config.get('bridges'):
if bridge.get('uart', {}).get('port', None) == 0:
VERBOSE = 0
def server(config_filename='us2n.json'):
config = read_config(config_filename)
VERBOSE = config.setdefault('verbose', 1)
name = config.setdefault('name', 'Tiago-ESP32')
config_verbosity(config)
print(50*'=')
print('Welcome to ESP8266/32 serial <-> tcp bridge\n')
return S2NServer(config)