Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to use serial over network #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions Elelabs_EzspFwUtility.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def is_valid_file(parser, arg):
parser_flash.add_argument('-p','--port', type=str, required=True, help='Serial port for the EZSP Product')
parser_flash.add_argument('-b','--baudrate', type=str, required=False, default=115200, help='Serial baud rate for NCP (115200/57600)')
parser_flash.add_argument('-d','--dlevel', choices=['RAW', 'PACKET', 'DEBUG', 'INFO'], required=False, default='INFO', help='Debug verbosity level')
parser_flash.add_argument('-m','--mode', choices=['xmodem', 'autobtl'], required=False, default='autobtl', help='Required operation mode. Xmodem does not restart the device')
parser_flash.set_defaults(which='flash')

parser_ele_update = subparsers.add_parser('ele_update', help='Updates the Elelabs product to a latest available version')
Expand All @@ -68,18 +69,24 @@ class AdapterModeProbeStatus:
ERROR = 3

class SerialInterface:
def __init__(self, port, baudrate):
def __init__(self, port, baudrate, logger):
self.logger = logger
self.port = port
self.baudrate = baudrate
self.isOverNetwork = self.port.startswith("socket://")

def open(self):
try:
self.serial = serial.Serial(port=self.port,
baudrate=self.baudrate,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
xonxoff=True,
timeout=3)
if self.isOverNetwork:
self.logger.info("Connecting to serial over network on %s...", self.port)
self.serial = serial.serial_for_url(self.port)
else:
self.serial = serial.Serial(port=self.port,
baudrate=self.baudrate,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
xonxoff=True,
timeout=3)
except Exception as e:
raise Exception("PORT ERROR: %s" % str(e))

Expand Down Expand Up @@ -528,7 +535,7 @@ def __init__(self, config, logger):
self.config = config

def probe(self):
serialInterface = SerialInterface(self.config.port, self.config.baudrate)
serialInterface = SerialInterface(self.config.port, self.config.baudrate, self.logger)
serialInterface.open()

ezsp = EzspProtocolInterface(serialInterface.serial, self.config, self.logger)
Expand Down Expand Up @@ -584,7 +591,7 @@ def probe(self):
if self.config.baudrate != 115200:
serialInterface.close()
time.sleep(1)
serialInterface = SerialInterface(self.config.port, 115200)
serialInterface = SerialInterface(self.config.port, 115200, self.logger)
serialInterface.open()

# check if allready in bootloader mode
Expand All @@ -607,7 +614,7 @@ def restart(self, mode):
adapter_status, adapter_name = self.probe()
if adapter_status == AdapterModeProbeStatus.ZIGBEE or adapter_status == AdapterModeProbeStatus.THREAD:
if mode == 'btl':
serialInterface = SerialInterface(self.config.port, self.config.baudrate)
serialInterface = SerialInterface(self.config.port, self.config.baudrate, self.logger)
serialInterface.open()

self.logger.info("Launch in bootloader mode")
Expand Down Expand Up @@ -649,7 +656,7 @@ def restart(self, mode):
self.logger.info("Allready in bootloader mode. No need to restart")
return 0
else:
serialInterface = SerialInterface(self.config.port, 115200)
serialInterface = SerialInterface(self.config.port, 115200, self.logger)
serialInterface.open()

self.logger.info("Launch in normal application mode")
Expand All @@ -667,7 +674,7 @@ def restart(self, mode):
else:
return -1

def flash(self, filename):
def flash(self, filename, mode):
# STATIC FUNCTIONS
def getc(size, timeout=1):
read_data = self.serialInterface.serial.read(size)
Expand All @@ -686,11 +693,11 @@ def putc(data, timeout=1):
self.logger.critical('Aborted! Gecko bootloader accepts .gbl or .ebl images only.')
return

if self.restart("btl"):
if mode == 'autobtl' and not self.restart("btl"):
self.logger.critical("EZSP adapter not in the bootloader mode. Can't perform update procedure")
return

self.serialInterface = SerialInterface(self.config.port, 115200)
self.serialInterface = SerialInterface(self.config.port, 115200, self.logger)
self.serialInterface.open()
# Enter '1' to initialize X-MODEM mode
self.serialInterface.serial.write(b'\x0A')
Expand Down Expand Up @@ -801,11 +808,4 @@ def ele_update(self, new_version):
elelabs.ele_update(args.version)

if args.which == 'flash':
elelabs.flash(args.file)







elelabs.flash(args.file, args.mode)