-
Notifications
You must be signed in to change notification settings - Fork 76
/
wifi_connection.py
270 lines (223 loc) · 8.33 KB
/
wifi_connection.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
import re
import socket
import struct
from threading import Thread
import traceback
import time
from src.connection.connection import Connection
from src.connection.websocket import WebSocket
from src.helpers import websocket_helper
from src.logic.file_transfer import FileTransfer
from src.utility.exceptions import PasswordException, NewPasswordException, OperationError, HostnameResolutionError
class WifiConnection(Connection):
WEBREPL_REQ_S = "<2sBBQLH64s"
WEBREPL_PUT_FILE = 1
WEBREPL_GET_FILE = 2
WEBREPL_GET_VER = 3
def __init__(self, host, port, terminal, password_prompt):
Connection.__init__(self, terminal)
self._host = host
self._port = port
self.s = None
self.ws = None
try:
self._start_connection()
except Exception as e:
self._clear()
raise e
if not self.handle_password(password_prompt):
self._clear()
raise PasswordException()
self._reader_thread = Thread(target=self._reader_thread_routine)
self._reader_thread.start()
def _start_connection(self):
self.s = socket.socket()
self.s.settimeout(3)
try:
errno = self.s.connect_ex((self._host, self._port))
except socket.gaierror: # Failed to resolve hostname
raise HostnameResolutionError()
if errno != 0:
raise ConnectionError("Failed to connect to the device.")
self.s.settimeout(None)
# Test if connection is working
try:
websocket_helper.client_handshake(self.s)
except (ConnectionResetError, ConnectionAbortedError):
raise ConnectionError("Device refused connection.")
self.s.setblocking(0)
self.ws = WebSocket(self.s)
return True
def _clear(self):
self.ws = None
if self.s:
self.s.close()
self.s = None
def set_password(self):
password = "passw".encode("utf-8") + b"\r"
self.ws.write(password)
response = self.ws.read_all().decode("utf-8")
if response.find("Confirm password:") < 0:
return False
self.ws.write(password)
try:
response = self.ws.read_all().decode("utf-8")
return response.find("Password successfully set") >= 0
except ConnectionAbortedError:
# If connection was aborted, password was set
return True
def login(self, password):
self.ws.write(password.encode("utf-8") + b"\r")
try:
response = self.ws.read_all().decode("utf-8")
return response.find("WebREPL connected") >= 0
except ConnectionAbortedError:
return False
def handle_password(self, password_prompt):
content = self.ws.read_all().decode("utf-8")
if content.find("New password:") >= 0:
self.set_password()
raise NewPasswordException()
elif content.find("Password:") >= 0:
return self.login(password_prompt("Enter WebREPL password"))
else:
return False
def is_connected(self):
return self.ws is not None
def disconnect(self):
if self.is_connected():
if self._reader_thread.is_alive():
self._reader_running = False
self._reader_thread.join()
self.s.close()
self.s = None
def read_all(self):
x = self.ws.read_all().decode("utf-8", errors="replace")
if x and self._terminal is not None:
self._terminal.add(x)
return x
# TODO: Join with serial implementation? At least the special character handling
def read_line(self):
x = self.ws.read_all(0.2)
if x and self._terminal is not None:
if x == b'\x08\x1b[K':
x = b'\x08'
if x[:3] == b'\x1b[': # Control sequence
# TODO: regex firt match 14D on x[3:]
pass
self._terminal.add(x.decode("utf-8", errors="replace"))
return x
def read_junk(self):
self.ws.read_all(0)
def read_one_byte(self):
return self.ws.read(1)
def send_character(self, char):
assert isinstance(char, str)
self.ws.write(char)
def send_bytes(self, binary):
self.ws.write(binary)
def send_line(self, line_text, ending="\r\n"):
assert isinstance(line_text, str)
assert isinstance(ending, str)
self.ws.write(line_text + ending)
@staticmethod
def read_resp(ws):
data = ws.read(4)
sig, code = struct.unpack("<2sH", data)
assert sig == b"WB"
return code
def _read_file_job(self, file_name, transfer):
assert isinstance(transfer, FileTransfer)
try:
file_size = self.get_file_size(file_name)
except OperationError:
transfer.mark_error("Failed to determine file size.")
return
if isinstance(file_name, str):
file_name = file_name.encode("utf-8")
ret = b""
rec = struct.pack(WifiConnection.WEBREPL_REQ_S, b"WA", WifiConnection.WEBREPL_GET_FILE,
0, 0, 0, len(file_name), file_name)
self._auto_reader_lock.acquire()
self._auto_read_enabled = False
self.read_junk()
self.ws.write(rec, True)
assert self.read_resp(self.ws) == 0
while True:
# Confirm message
self.ws.write(b"\1", True)
(sz,) = struct.unpack("<H", self.ws.read(2))
if sz == 0:
break
while sz:
buf = self.ws.read(sz)
if not buf:
raise OSError()
ret += buf
sz -= len(buf)
transfer.progress = len(ret) / file_size
if self.read_resp(self.ws) == 0:
transfer.mark_finished()
transfer.read_result.binary_data = ret
else:
transfer.mark_error()
transfer.read_result.binary_data = None
self._auto_read_enabled = True
self._auto_reader_lock.release()
def _write_file_job(self, file_name, text, transfer):
def mark_error_and_release():
transfer.mark_error()
self._auto_read_enabled = True
self._auto_reader_lock.release()
assert isinstance(transfer, FileTransfer)
if isinstance(file_name, str):
file_name = file_name.encode("utf-8")
if isinstance(text, str):
text = text.encode("utf-8")
sz = len(text)
rec = struct.pack(WifiConnection.WEBREPL_REQ_S, b"WA", WifiConnection.WEBREPL_PUT_FILE, 0, 0, sz,
len(file_name), file_name)
self._auto_reader_lock.acquire()
self._auto_read_enabled = False
self.read_junk()
self.ws.write(rec[:10], file_transfer=True)
self.ws.write(rec[10:], file_transfer=True)
try:
if self.read_resp(self.ws) != 0:
mark_error_and_release()
return
except TimeoutError:
mark_error_and_release()
return
cnt = 0
# Increase timeout from default value which gives
# more time to MCU to process large files.
original_timeout = self.ws.recv_timeout
self.ws.recv_timeout = 30
try:
while True:
buf = text[cnt:cnt + 256]
if not buf:
break
self.ws.write(buf, file_transfer=True)
cnt += len(buf)
transfer.progress = cnt / sz
if self.read_resp(self.ws) == 0:
transfer.mark_finished()
else:
transfer.mark_error()
except ConnectionResetError:
transfer.mark_error("Connection was reset.")
except ConnectionError:
transfer.mark_error()
except Exception as generalException:
info = "Unexpected error, report this on project's github issues page\n{}: {}\n{}".format(
type(generalException).__name__,
str(generalException),
"".join(traceback.format_tb(generalException.__traceback__))
)
transfer.mark_error(info)
finally:
self.ws.recv_timeout = original_timeout
self._auto_read_enabled = True
self._auto_reader_lock.release()