-
Notifications
You must be signed in to change notification settings - Fork 8
/
WIZMSGHandler.py
280 lines (235 loc) · 10.1 KB
/
WIZMSGHandler.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import select
import sys
import codecs
import logging
from threading import Timer
from WIZ752CMDSET import WIZ752CMDSET
OP_SEARCHALL = 1
OP_RESET = 2
OP_SETCOMMAND = 3
OP_SETFILE = 4
OP_GETFILE = 5
OP_FWUP = 6
class WIZMSGHandler:
def __init__(self, sock):
self.sock = sock
self.msg = bytearray(1024)
self.size = 0
self.inputs = [self.sock.sock]
self.outputs = []
self.errors = []
self.opcode = None
self.iter = 0
self.dest_mac = None
self.isvalid = False
self.timer1 = None
self.istimeout = False
self.reply = ""
self.mac_list = []
self.ip_list = []
self.ip_mode = []
self.mode_list = []
self.devname = []
self.version = []
self.devst = []
self.getreply = []
self.wiz752cmdObj = WIZ752CMDSET(1)
def timeout_func(self):
# print('timeout')
self.istimeout = True
def getmacaddr(self, index):
if len(self.mac_list) >= (index + 1):
mac_addr = self.mac_list[index]
for i in range(5, 1):
mac_addr[i * 2 :] = ":" + mac_addr[i * 2 :]
return mac_addr
else:
sys.stdout.write("index is out of range\r\n")
return None
def makecommands(self, cmd_list, op_code):
self.opcode = op_code
self.size = 0
for cmd in cmd_list:
# print(cmd[0], cmd[1])
self.msg[self.size :] = str.encode(cmd[0])
self.size += len(cmd[0])
if cmd[0] == "MA":
# sys.stdout.write('cmd[1]: %r\r\n' % cmd[1])
cmd[1] = cmd[1].replace(":", "")
# print(cmd[1])
# hex_string = cmd[1].decode('hex')
hex_string = codecs.decode(cmd[1], "hex")
self.msg[self.size :] = hex_string
self.dest_mac = hex_string
# self.dest_mac = (int(cmd[1], 16)).to_bytes(6, byteorder='big') # Hexadecimal string to hexadecimal binary
# self.msg[self.size:] = self.dest_mac
self.size += 6
else:
self.msg[self.size :] = str.encode(cmd[1])
self.size += len(cmd[1])
if not "\r\n" in cmd[1]:
self.msg[self.size :] = str.encode("\r\n")
self.size += 2
def sendcommands(self):
self.sock.sendto(self.msg)
def sendcommandsTCP(self):
self.sock.write(self.msg)
def check_parameter(self, cmdset):
# print('check_parameter()', cmdset, cmdset[:2], cmdset[2:])
try:
if b"MA" not in cmdset:
if self.wiz752cmdObj.isvalidparameter(cmdset[:2].decode(), cmdset[2:].decode()):
return True
else:
return False
else:
return False
except Exception as e:
print("[ERROR] WIZMSGHandler check_parameter(): %r" % e)
# Check the response
def checkresponse(self):
readready, writeready, errorready = select.select(self.inputs, self.outputs, self.errors, 1)
self.timer1 = Timer(2.0, self.timeout_func)
self.timer1.start()
self.getreply = None
while True:
if self.istimeout == True:
self.timer1.cancel()
self.istimeout = False
break
for sock in readready:
if sock == self.sock.sock:
data = self.sock.recvfrom()
self.getreply = data.splitlines()
# print('config reply:', configreply)
readready, writeready, errorready = select.select(self.inputs, self.outputs, self.errors, 1)
# if len(readready) == 0:
if self.getreply != None:
break
if self.getreply != None:
return 1
else:
return -1
def parseresponse(self):
readready, writeready, errorready = select.select(self.inputs, self.outputs, self.errors, 1)
self.timer1 = Timer(2.0, self.timeout_func)
self.timer1.start()
# t = Timer(3.0, timeout_func)
# t.start()
replylists = None
self.getreply = None
while True:
self.iter += 1
# sys.stdout.write("iter count: %r" % self.iter)
if self.istimeout == True:
self.timer1.cancel()
self.istimeout = False
break
for sock in readready:
if sock == self.sock.sock:
data = self.sock.recvfrom()
replylists = data.splitlines()
# print('replylists', replylists)
self.getreply = replylists
if self.opcode == OP_SEARCHALL:
for i in range(0, len(replylists)):
if b"MC" in replylists[i]:
if self.check_parameter(replylists[i]):
self.mac_list.append(replylists[i][2:])
if b"MN" in replylists[i]:
if self.check_parameter(replylists[i]):
self.devname.append(replylists[i][2:])
if b"VR" in replylists[i]:
if self.check_parameter(replylists[i]):
self.version.append(replylists[i][2:])
if b"ST" in replylists[i]:
if self.check_parameter(replylists[i]):
self.devst.append(replylists[i][2:])
if b"OP" in replylists[i]:
if self.check_parameter(replylists[i]):
self.mode_list.append(replylists[i][2:])
if b"LI" in replylists[i]:
if self.check_parameter(replylists[i]):
self.ip_list.append(replylists[i][2:])
if b"IM" in replylists[i]:
if self.check_parameter(replylists[i]):
self.ip_mode.append(replylists[i][2:])
elif self.opcode == OP_FWUP:
for i in range(0, len(replylists)):
# sys.stdout.write('%s\r\n' % replylists)
# sys.stdout.write("%r\r\n" % replylists[i][:2])
if b"MA" in replylists[i][:2]:
dest_mac = self.dest_mac
reply_mac = replylists[i][2:]
# self.isvalid = True
else:
self.isvalid = False
# sys.stdout.write("%r\r\n" % replylists[i][:2])
if b"FW" in replylists[i][:2]:
# sys.stdout.write('self.isvalid == True\r\n')
param = replylists[i][2:].split(b":")
self.reply = replylists[i][2:]
# for Boot update
elif b"BU" in replylists[i][:2]:
# sys.stdout.write('self.isvalid == True\r\n')
param = replylists[i][2:].split(b":")
self.reply = replylists[i][2:]
readready, writeready, errorready = select.select(self.inputs, self.outputs, self.errors, 1)
if self.opcode == OP_SEARCHALL:
return len(self.mac_list)
elif self.opcode == OP_SETCOMMAND or self.opcode == OP_SETFILE:
if replylists != None:
return True
# return self.getreply
else:
return -1
elif self.opcode == OP_FWUP:
return self.reply
# sys.stdout.write("%s\r\n" % self.mac_list)
def get_log(self, mac_addr):
if self.getreply != None:
print("[%s] Setting result: " % (mac_addr))
# print('getreply: %s' % self.getreply)
cmdsetObj = WIZ752CMDSET(logging.ERROR)
for i in range(2, len(self.getreply)):
cmd = self.getreply[i][:2].decode()
param = self.getreply[i][2:].decode()
cmd_desc = cmdsetObj.getcmddescription(cmd)
param_desc = cmdsetObj.getparamdescription(cmd, param)
conf_info = " %02d) %s: %-17s | %s: %s\r\n" % (i - 1, cmd, param, cmd_desc, param_desc)
sys.stdout.write("%s" % conf_info)
else:
pass
def get_filelog(self, macaddr):
filename = None
# print('getreply: %s' % self.getreply)
if self.getreply == None:
print("No reply from device. exit program.")
sys.exit(0)
cmdsetObj = WIZ752CMDSET(logging.ERROR)
mac_addr = macaddr.replace(":", "")
filename = "getfile_%s.log" % (mac_addr)
for i in range(0, len(self.getreply)):
f = open(filename, "w")
for i in range(2, len(self.getreply)):
getcmd = self.getreply[i][:2]
cmd = getcmd.decode("utf-8")
if cmd not in cmdsetObj.cmdset:
print("Invalid command. Check the command set")
exit(0)
getparam = self.getreply[i][2:]
param = getparam.decode("utf-8")
cmd_desc = cmdsetObj.getcmddescription(cmd)
param_desc = cmdsetObj.getparamdescription(cmd, param)
# sys.stdout.write("%s\r\n" % self.getreply[i])
info = "%02d) %s: %-17s | %s: %s\n" % (i - 1, cmd, param, cmd_desc, param_desc)
# info = "%02d) %s: %s\r\n" % (i-1, cmd_desc, param_desc)
f.write(info)
f.close()
if filename != None:
f = open(filename, "r")
readinfo = f.read()
print(readinfo)
print('@ Refer to "%s" for detail.\n' % filename)