-
Notifications
You must be signed in to change notification settings - Fork 1
/
improv.py
executable file
·244 lines (205 loc) · 6.55 KB
/
improv.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
#!/usr/bin/env python3
import serial
import sys
import argparse
from enum import Enum
improv_header="IMPROV"
improv_version=1
verbose=False
class improv_type(Enum):
curr_state=1
error_state=2
rpc_cmd=3
rpc_result=4
class rpc_cmd(Enum):
wifi_cmd_id=1
current_state_cmd_id=2
device_info_cmd_id=3
scan_cmd_id=4
def send_improv_cmd(ser, cmd):
improv_cmd=list(improv_header)
improv_cmd.append(improv_version)
improv_cmd.append(improv_type.rpc_cmd.value)
improv_cmd.append(len(cmd))
improv_cmd += cmd
improv_cmd.append(0)
tx_cmd=[]
cksum=0
for i in improv_cmd:
if isinstance(i, int):
cksum += i
tx_cmd.append(i)
else:
cksum += ord(i)
tx_cmd.append(ord(i))
tx_cmd[-1] = cksum & 0xff
if verbose:
print(bytearray(tx_cmd))
ser.write(bytearray(tx_cmd))
ser.write(bytearray([ord('\n')]))
ser.flush()
def set_wifi(ser, ssid, passwd):
wifi_cmd=[rpc_cmd.wifi_cmd_id.value]
wifi_cmd.append(len(ssid)+len(passwd)+2)
wifi_cmd.append(len(ssid))
wifi_cmd += list(ssid)
wifi_cmd.append(len(passwd))
wifi_cmd += list(passwd)
send_improv_cmd(ser, wifi_cmd)
monitor(ser)
def scan_wifi(ser):
wifi_cmd=[rpc_cmd.scan_cmd_id.value, 0]
send_improv_cmd(ser, wifi_cmd)
monitor(ser)
def dev_info(ser):
wifi_cmd=[rpc_cmd.device_info_cmd_id.value, 0]
send_improv_cmd(ser, wifi_cmd)
monitor(ser)
def dev_state(ser):
wifi_cmd=[rpc_cmd.current_state_cmd_id.value, 0]
send_improv_cmd(ser, wifi_cmd)
monitor(ser)
def monitor(ser):
class states(Enum):
gethdr=0
getver=1
gettype=2
getlen=3
getdata=4
getrpcresp=5
getdatlen=6
getstrlen=7
getrpcdat=8
state=states.gethdr
imp_type=0
rx_header=""
while True:
bs = ser.read(1)
#if verbose > 1:
# print(bs, end="")
ch=int.from_bytes(bs, "big")
if state == states.gethdr:
try:
rx_header += chr(ch)
if len(rx_header) > len(improv_header):
rx_header = rx_header[1:]
if rx_header == improv_header:
state=states.getver
rx_header=""
except:
rx_header = ""
elif state == states.getver:
if ch==improv_version:
state=states.gettype
else:
state=states.gethdr
elif state == states.gettype:
if verbose:
print("type", ch)
imp_type=ch
state=states.getlen
elif state == states.getlen:
if verbose:
print("len", ch)
cmdlen=ch
if cmdlen > 0:
if imp_type == improv_type.rpc_result.value:
state=states.getrpcresp
else:
state=states.getdata
else:
state=states.gethdr
elif state == states.getrpcresp:
if verbose:
print("response to command", ch)
resp_cmd=ch
state=state.getdatlen
elif state == states.getdatlen:
if verbose:
print("datlen", ch)
datlen=ch
if datlen == 0:
return
state=state.getstrlen
elif state == states.getstrlen:
if verbose:
print("strlen", ch)
datlen -= 1
strlen=ch
state=state.getrpcdat
elif state == states.getrpcdat:
print(chr(ch), end="")
strlen -= 1
datlen -= 1
if strlen == 0:
if datlen <= 0:
print()
if resp_cmd == rpc_cmd.scan_cmd_id.value:
state=states.gethdr
else:
return
else:
print(" ", end="")
state=states.getstrlen
sys.stdout.flush()
elif state == states.getdata:
if imp_type == improv_type.curr_state.value:
if (ch == 0):
print("WiFi stopped")
return
elif (ch == 1):
print("Wifi awaiting authorization")
elif (ch == 2):
print("Wifi authorized")
elif (ch == 3):
print("Wifi provisioning")
elif (ch == 4):
print("Wifi provisioned")
state=states.gethdr
elif imp_type == improv_type.error_state.value:
if (ch == 0):
print("No Error")
elif (ch == 1):
print("Invalid RPC packet")
elif (ch == 2):
print("Unknown RPC command")
elif (ch == 3):
print("Unable to connect")
elif (ch == 4):
print("Unknown Error")
return
else:
state=states.gethdr
else:
if verbose:
try:
print(bs.decode(), end="")
except:
print(bs, end="")
sys.stdout.flush()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-d', dest='dev', help="USB Device")
parser.add_argument('-s', dest='ssid', help="Wifi SSID")
parser.add_argument('-p', dest='passwd', help="Wifi Password")
parser.add_argument('-S', dest='scan', default=False, action=argparse.BooleanOptionalAction, help="Scan Wifi Networks")
parser.add_argument('-i', dest='info', default=False, action=argparse.BooleanOptionalAction, help="Get device information")
parser.add_argument('-g', dest='getstate', default=False, action=argparse.BooleanOptionalAction, help="Get current state")
parser.add_argument('-v', dest='verbose', default=False, action=argparse.BooleanOptionalAction, help="Verbose")
args = parser.parse_args()
verbose = args.verbose
if (not args.dev):
print("Must select serial device")
exit()
ser = serial.Serial(args.dev, 115200)
if (not ser):
print("Invalid serial device")
exit()
if (args.scan):
scan_wifi(ser)
if (args.info):
dev_info(ser)
if (args.getstate):
dev_state(ser)
if (args.ssid and args.passwd):
set_wifi(ser, args.ssid, args.passwd)