Skip to content

Commit

Permalink
#2
Browse files Browse the repository at this point in the history
Support serial baudrate up to 460.8kbps
  • Loading branch information
renakim committed Mar 19, 2018
1 parent 2cf7886 commit be47c2d
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 118 deletions.
129 changes: 70 additions & 59 deletions FWUploadThread.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import re
import sys

# sys.path.append('./TCPClient/')
import io
import time
import logging
Expand Down Expand Up @@ -33,6 +31,22 @@
idle_state = 1
datasent_state = 2

def jumpToApp(mac_addr):
cmd_list = []

conf_sock = WIZUDPSock(5000, 50001)
conf_sock.open()
wizmsghangler = WIZMSGHandler(conf_sock)

# boot mode change: App boot mode
print('[%s] Jump to app boot mode' % mac_addr)

cmd_list.append(["MA", mac_addr])
cmd_list.append(["PW", " "])
cmd_list.append(["AB", ""])
wizmsghangler.makecommands(cmd_list, OP_FWUP)
wizmsghangler.sendcommands()

class FWUploadThread(threading.Thread):
# initialization
# def __init__(self, log_level):
Expand All @@ -51,6 +65,10 @@ def __init__(self):

self.sentbyte = 0

conf_sock = WIZUDPSock(5000, 50001)
conf_sock.open()
self.wizmsghangler = WIZMSGHandler(conf_sock)

def setparam(self, dest_mac, binaryfile):
self.dest_mac = dest_mac
self.bin_filename = binaryfile
Expand All @@ -59,58 +77,80 @@ def setparam(self, dest_mac, binaryfile):
self.remainbytes = len(self.data)
self.curr_ptr = 0

sys.stdout.write("Firmware file size: %r\n\n" % len(self.data))
sys.stdout.write("\nFirmware file size: %r\n\n" % len(self.data))

def myTimer(self):
# sys.stdout.write('timer1 timeout\r\n')
self.istimeout = 1

def run(self):
conf_sock = WIZUDPSock(5000, 50001)
conf_sock.open()
# wizethObj = WIZEthMsgGen(conf_sock)
wizmsghangler = WIZMSGHandler(conf_sock)
def jumpToApp(self):
cmd_list = []

# boot mode change: App boot mode
print('[%s] Jump to app boot mode' % self.dest_mac)

cmd_list.append(["MA", self.dest_mac])
cmd_list.append(["PW", " "])
cmd_list.append(["AB", ""])
self.wizmsghangler.makecommands(cmd_list, OP_FWUP)
self.wizmsghangler.sendcommands()
# print('jumpToApp cmd_list: %s' % cmd_list)

# def run(self):
def sendCmd(self, command):
cmd_list = []
self.resp = None

###################################
# Send FW UPload request message
cmd_list.append(["MA", self.dest_mac])
cmd_list.append(["PW", " "])
cmd_list.append(["FW", str(len(self.data))])
cmd_list.append([command, str(len(self.data))])
# sys.stdout.write("cmd_list: %s\r\n" % cmd_list)
wizmsghangler.makecommands(cmd_list, OP_FWUP)
wizmsghangler.sendcommands()
resp = wizmsghangler.parseresponse()
self.wizmsghangler.makecommands(cmd_list, OP_FWUP)

# if no reponse from device, retry for several times.
for i in range(3):
self.wizmsghangler.sendcommands()
self.resp = self.wizmsghangler.parseresponse()
if self.resp is not '':
break
time.sleep(1)

if resp is not '':
resp = resp.decode('utf-8')
def run(self):
if self.resp is not '':
resp = self.resp.decode('utf-8')
# print('resp', resp)
params = resp.split(':')
sys.stdout.write('Dest IP: %s, Dest Port num: %r\r\n' % (params[0], int(params[1])))
self.serverip = params[0]
self.serverport = int(params[1])

# network reachable check
# os.system("ping " + ("-n 1 " if sys.platform.lower()=="win32" else "-c 1 ") + self.serverip)
os.system("ping " + ("-n 1 " if sys.platform.lower()=="win32" else "-c 1 ") + self.serverip)
ping_reponse = os.system("ping " + ("-n 1 " if sys.platform.lower()=="win32" else "-c 1 ") + self.serverip)
# ping_reponse = os.system('ping -n 1 ' + params[0])
if ping_reponse == 0:
print('Device[%s] network OK' % self.dest_mac)
else:
print('<ERROR>: Device[%s]: %s is unreachable.\n\tRefer --multiset or --ip options to set IP address.' % (self.dest_mac, self.serverip))
print('<Ping Error>: Device[%s]: %s is unreachable.\n\tRefer --multiset or --ip options to set IP address.' % (self.dest_mac, self.serverip))
sys.exit(0)
else:
print('No response from device. Check the network or device status.')
print('@@@@@ Device[%s]: No response from device. Check the network or device status.' % (self.dest_mac))
sys.exit(0)

try:
self.client = TCPClient(2, params[0], int(params[1]))
except:
pass

self.retrycheck = 0
try:
# sys.stdout.write("%r\r\n" % self.client.state)
while True:
if self.retrycheck > 20:
break

self.retrycheck += 1

if self.client.state is SOCK_CLOSE_STATE:
if self.timer1 is not None:
Expand All @@ -121,9 +161,10 @@ def run(self):
# sys.stdout.write('1 : %r\r\n' % self.client.getsockstate())
# sys.stdout.write("%r\r\n" % self.client.state)
if self.client.state is SOCK_OPEN_STATE:
sys.stdout.write('[%r] is OPEN\r\n' % (self.serverip))
# sys.stdout.write('[%r] is OPEN\r\n' % (self.serverip))
sys.stdout.write('[%r] is OPEN | %s\r\n' % (self.serverip, self.bin_filename))
# sys.stdout.write('[%r] client.working_state is %r\r\n' % (self.serverip, self.client.working_state))
time.sleep(0.5)
time.sleep(0.1)
except Exception as e:
sys.stdout.write('%r\r\n' % e)

Expand All @@ -134,7 +175,8 @@ def run(self):
self.client.connect()
# sys.stdout.write('2 : %r' % self.client.getsockstate())
if self.client.state is SOCK_CONNECT_STATE:
sys.stdout.write('[%r] is CONNECTED\r\n' % (self.serverip))
# sys.stdout.write('[%r] is CONNECTED\r\n' % (self.serverip))
sys.stdout.write('[%r] is CONNECTED | %s\r\n' % (self.serverip, self.bin_filename))
# sys.stdout.write('[%r] client.working_state is %r\r\n' % (self.serverip, self.client.working_state))
# time.sleep(1)
except Exception as e:
Expand Down Expand Up @@ -172,64 +214,33 @@ def run(self):
elif self.client.working_state == datasent_state:
# sys.stdout.write('4 : %r' % self.client.getsockstate())
response = self.client.readbytes(2)
# print('response: %r' % response)
if response is not None:
if int(binascii.hexlify(response), 16):
self.client.working_state = idle_state
self.timer1.cancel()
self.istimeout = 0
else:
print('ERROR: No response from device. Stop FW upload...')
print('>>>>>>>>> ERROR: Device[%s]: No response from device. Stop FW upload...' % self.dest_mac)
self.client.close()
sys.exit(0)

# if (response != ""):
# self.timer1.cancel()
# self.istimeout = 0

# time.sleep(0.1)
# self.client.working_state = idle_state
# self.client.close()

if self.istimeout is 1:
# self.timer1.cancel()
self.istimeout = 0
self.client.working_state = idle_state
self.client.close()
sys.exit(0)

# sys.stdout.write('Device [%s] firmware upload success!\r\n' % (self.dest_mac))


# self.client.working_state = datasent_state

# self.timer1 = threading.Timer(2.0, self.myTimer)
# self.timer1.start()
# sys.stdout.write('timer 1 started\r\n')
except Exception as e:
sys.stdout.write('%r\r\n' % e)
# elif self.client.working_state == datasent_state:
# sys.stdout.write('4 : %r' % self.client.getsockstate())
# response = self.client.read()
# if (response != ""):
# self.timer1.cancel()
# self.istimeout = 0

# time.sleep(0.1)
# self.client.working_state = idle_state
# self.client.close()

# if self.istimeout is 1:
# # self.timer1.cancel()
# self.istimeout = 0
# self.client.working_state = idle_state
# self.client.close()
# self.client.close()
response = ""
break
sys.stdout.write('Device [%s] firmware upload success!\r\n' % (self.dest_mac))

if self.retrycheck > 20:
sys.stdout.write('Device [%s] firmware upload fail. (file: %s)\r\n' % (self.dest_mac, self.bin_filename))
else:
sys.stdout.write('Device [%s] firmware upload success! (file: %s)\r\n' % (self.dest_mac, self.bin_filename))
# for send FIN packet
time.sleep(2.5)
time.sleep(1)
self.client.shutdown()
except (KeyboardInterrupt, SystemExit):
sys.stdout.write('%r\r\n' % e)
Expand Down
5 changes: 2 additions & 3 deletions WIZArgParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def loopback_arg(self):
parser.add_argument('-t', '--targetip', help='Target IP address')
parser.add_argument('-r', '--retry', type=int, default=5, help='Test retry number (default: 5)')


args = parser.parse_args()
return args

Expand All @@ -55,6 +54,8 @@ def config_arg(self):
group.add_argument('-m', '--multiset', metavar='ipaddr', help='Set IP address for all devices in \'mac_list.txt\'. Parameter is first address.')
# F/W upload
group.add_argument('-u', '--upload', dest='fwfile', help='Firmware upload from file')
group.add_argument('--ab', action='store_true', help='Jump to app boot mode')


## Network config
group = parser.add_argument_group('General Options')
Expand Down Expand Up @@ -133,8 +134,6 @@ def config_arg(self):
group.add_argument('--dg', choices=['0','1'], help='Serial debug message enable (Debug UART port)')

## Config from file
group = parser.add_argument_group('\nConfiguration from File')

group.add_argument('--setfile', help='File name to Set')
group.add_argument('--getfile', help='File name to Get info. Refer default command(cmd_oneport.txt or cmd_twoport.txt).')

Expand Down
11 changes: 11 additions & 0 deletions WIZMSGHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, udpsock):
self.mode_list = []
self.devname = []
self.version = []
self.devst = []

self.getreply = []

Expand Down Expand Up @@ -182,6 +183,10 @@ def parseresponse(self):
# sys.stdout.write("iter count: %r, %r\r\n" % (self.iter, replylists[i][2:]))
if b'VR' in replylists[i]:
self.version.append(replylists[i][2:])

if b'ST' in replylists[i]:
self.devst.append(replylists[i][2:])

# if b'VR' in replylists[i] and b"1.1.2dev" in replylists[i][2:] :
# self.mac_list.pop()
# sys.stdout.write("iter count: %r, %r\r\n" % (self.iter, replylists[i][2:]))
Expand Down Expand Up @@ -233,6 +238,12 @@ def parseresponse(self):
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 is True\r\n')
param = replylists[i][2:].split(b':')
self.reply = replylists[i][2:]

# sys.stdout.write("%r\r\n" % replylists[i])

readready, writeready, errorready = select.select(self.inputs, self.outputs, self.errors, 1)
Expand Down
Loading

0 comments on commit be47c2d

Please sign in to comment.