Skip to content

Commit

Permalink
Improve bootloader display and reduce detection number.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-rabault committed Sep 28, 2023
1 parent be521ba commit c3a5e3d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
2 changes: 2 additions & 0 deletions pyluos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ def _setup(self):
sys.exit("Detection failed.")
self._send({'detection': {}})
startTime = time.time()
# Save routing table data
self._routing_table = state
# Create nodes
self._services = []
self._nodes = []
Expand Down
62 changes: 37 additions & 25 deletions pyluos/tools/bootloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
OKGREEN = '\r\033[92m'
FAIL = '\r\033[91m'
ENDC = '\033[0m'
UNDERLINE = '\r\033[4m'
BOLD = '\r\033[1m'

FILEPATH = None
NB_SAMPLE_BY_FRAME_MAX = 127
Expand Down Expand Up @@ -262,6 +264,7 @@ def erase_flash(device, topic, nodes_to_program):
# timeout for exiting loop in case of fails
if (time.time() - init_time > timeout):
return_value = False
print(FAIL + u"\r ╰> Erase flash of node ", failed_nodes, "FAILED %" + ENDC)
break
# check if it is a response message
if 'bootloader' in state:
Expand Down Expand Up @@ -675,18 +678,18 @@ def luos_flash(args):

baudrate = os.getenv('LUOS_BAUDRATE', args.baudrate)

print('Luos flash subcommand with parameters :')
print("\n" + UNDERLINE + "Luos flash subcommand with parameters:" + ENDC)
print('\t--baudrate : ', baudrate)
print('\t--gate : ', args.gate)
print('\t--target : ', args.target)
print('\t--binary : ', args.binary)
print('\t--port : ', args.port)

# state used to check each step
# State used to check each step
machine_state = True
# list of all the nodes that may fail in each step
# List of all the nodes that may fail in each step
total_fails = []
# update firmware path
# Update firmware path
global FILEPATH
FILEPATH = args.binary
try:
Expand All @@ -697,73 +700,82 @@ def luos_flash(args):
else:
f.close()

# init device
# Init device
device = Device(args.port, baudrate=baudrate, background_task=False)

# find routing table
state = find_network(device)
# Get routing table JSON
state = device._routing_table
if state is None:
return BOOTLOADER_DETECT_ERROR

# searching nodes to program in network
# Searching nodes to program in network
(nodes_to_reboot, nodes_to_program) = create_target_list(args, state)

# check if we have available node to program
# Check if we have available node to program
if not nodes_to_program:
print(FAIL + "No target found :\n" + str(device.nodes) + ENDC)
return BOOTLOADER_DETECT_ERROR

# reboot all nodes in bootloader mode
print("\n** Reboot all nodes in bootloader mode **")
# Reboot all nodes in bootloader mode
print("\n" + BOLD + "Reboot all nodes in bootloader mode:" + ENDC)
for node in nodes_to_reboot:
send_node_command(device, node, topic, BOOTLOADER_START)
# delay to let gate send commands
# Delay to let the gate send the commands
time.sleep(0.01)

# wait before next step
# Delay to let the gate send the last command
time.sleep(0.1)

# find routing table in boot mode
# its necessary to give ids to bootloader services
# Find bootloaders in the routing table
state = find_network(device)
if state is None:
print(FAIL + " ╰> Reboot in bootloader mode failed." + ENDC)
return BOOTLOADER_DETECT_ERROR
else:
print(OKGREEN + " ╰> Reboot in bootloader mode succeed." + ENDC)

# wait before next step
# Wait before the next step
time.sleep(0.4)

print("\n** Programming nodes **")

# go to header state if node is ready
print("\n" + BOLD + "Programming nodes:" + ENDC)
# Go to header state if node is ready
for node in nodes_to_program:
print("─> Check if node", node, " is ready.")
machine_state = send_ready_cmd(device, node, topic)
if not machine_state:
total_fails.append(node)
machine_state = True
print(FAIL + "Node ", node, "failed to update!" + ENDC)
print(FAIL + " ╰> Node ", node, "programming failed." + ENDC)
time.sleep(0.01)

for node in total_fails:
nodes_to_program.remove(node)

# erase node flash memory
if not len(nodes_to_program):
print(BOLD + FAIL + "Programming failed on all targets." + ENDC)
return BOOTLOADER_FLASH_ERROR

# Erase node flash memory
print("─> Erase flash memory.")
machine_state, failed_nodes = erase_flash(device, topic, nodes_to_program)
if not machine_state:
for fail in failed_nodes:
nodes_to_program.remove(fail)
total_fails.extend(failed_nodes)
machine_state = True
print(FAIL + "Node ", failed_nodes, " flash erasing failed!" + ENDC)
print(FAIL + " ╰> Node ", failed_nodes, " flash erasing failed!" + ENDC)

if not len(nodes_to_program):
print(BOLD + FAIL + "Programming failed on all targets." + ENDC)
return BOOTLOADER_FLASH_ERROR

# send binary data
print("─> Send binary data.")
machine_state, failed_nodes = send_binary_data(device, topic, nodes_to_program)
if not machine_state:
total_fails.extend(failed_nodes)
machine_state = True
print(FAIL + "Node ", failed_nodes, "update failed!" + ENDC)
print(FAIL + "Node ", failed_nodes, "programming failed." + ENDC)

# inform the node of the end of the loading
print("─> Programmation finished, waiting for acknowledge.")
Expand Down Expand Up @@ -793,13 +805,13 @@ def luos_flash(args):
time.sleep(0.1)
# reboot all nodes in application mode
if len(total_fails) == 0:
print("\n** Reboot all nodes in application mode **")
print(OKGREEN + BOLD + "Programming succeed." + ENDC)
reboot_network(device, topic, nodes_to_reboot)
device.close()
return BOOTLOADER_SUCCESS
else:
device.close()
print(FAIL + "Nodes ", total_fails, " update failed, please reboot and retry." + ENDC)
print(FAIL + "Nodes ", total_fails, " programming failed, please reboot and retry." + ENDC)
return BOOTLOADER_FLASH_ERROR

# *******************************************************************************
Expand Down
17 changes: 11 additions & 6 deletions pyluos/tools/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
import serial
import struct
import argparse
OKGREEN = '\r\033[92m'
FAIL = '\r\033[91m'
ENDC = '\033[0m'


def serial_ports():
return Serial.available_hosts()


def serial_discover(baudrate=1000000):
serial_hosts = serial_ports()
available_serial = []
print("Searching for a gate available")
print("\n\033[4mSearching for available Gates:\033[0m")
for serial_host in serial_hosts:
print("Search a Gate on port " + str(serial_host))
print("\t- Search a Gate on port " + str(serial_host))
try:
port = serial.Serial(serial_host, baudrate, timeout=0.2)
time.sleep(0.1)
Expand Down Expand Up @@ -46,10 +51,10 @@ def serial_discover(baudrate=1000000):
port.reset_output_buffer()
port.close()

if available_serial :
if available_serial:
return available_serial
else:
print("... No gate detected")
print(FAIL + "... No gate detected" + ENDC)
return []

def main():
Expand All @@ -63,9 +68,9 @@ def main():
gate_list = serial_discover(os.getenv('LUOS_BAUDRATE', args.baudrate))

if gate_list:
print("Available Luos gate on port : " + str(gate_list))
print(OKGREEN + "Available Luos gate on port : " + str(gate_list) + ENDC)
else:
print("No gate detected")
print(FAIL + "No gate detected" + ENDC)


if __name__ == '__main__':
Expand Down

0 comments on commit c3a5e3d

Please sign in to comment.