@@ -69,13 +69,14 @@ def find_network(device):
69
69
while ('routing_table' not in state ):
70
70
if ('route_table' in state ):
71
71
print ('version of luos not supported' )
72
- return
72
+ return None
73
73
state = device ._poll_once ()
74
74
if (time .time ()- startTime > 1 ):
75
75
retry = retry + 1
76
76
if retry > 5 :
77
77
# detection is not working
78
- sys .exit ("Detection failed." )
78
+ print ('Detection failed.' )
79
+ return None
79
80
device ._send ({'detection' : {}})
80
81
startTime = time .time ()
81
82
@@ -243,7 +244,7 @@ def erase_flash(device, topic, nodes_to_program):
243
244
return_value = False
244
245
break
245
246
# check if it is a response message
246
- if 'bootloader' in state :
247
+ if 'bootloader' in state :
247
248
for response in state ['bootloader' ]:
248
249
if (response ['response' ] == BOOTLOADER_ERASE_RESP ):
249
250
# this node responded, delete it from the failed nodes list
@@ -265,7 +266,7 @@ def erase_flash(device, topic, nodes_to_program):
265
266
break
266
267
267
268
# check if it is a response message
268
- if 'bootloader' in state :
269
+ if 'bootloader' in state :
269
270
for response in state ['bootloader' ]:
270
271
if (response ['response' ] == BOOTLOADER_ERASE_RESP ):
271
272
# this node responded, delete it from the failed nodes list
@@ -540,7 +541,7 @@ def check_crc(device, topic, nodes_to_program):
540
541
541
542
# send crc command
542
543
send_topic_command (device , topic , BOOTLOADER_CRC_TEST )
543
-
544
+
544
545
state = device ._poll_once ()
545
546
# wait bin_end response
546
547
init_time = time .time ()
@@ -556,7 +557,7 @@ def check_crc(device, topic, nodes_to_program):
556
557
source_crc = int .from_bytes (compute_crc (), byteorder = 'big' )
557
558
node_crc = response ['crc_value' ]
558
559
node_id = response ['node' ]
559
- # crc properly received
560
+ # crc properly received
560
561
if (source_crc == node_crc ):
561
562
print (u" ╰> CRC test for node" , node_id , " : OK." )
562
563
if node_id in failed_nodes :
@@ -588,7 +589,7 @@ def check_crc(device, topic, nodes_to_program):
588
589
source_crc = int .from_bytes (compute_crc (), byteorder = 'big' )
589
590
node_crc = response ['crc_value' ]
590
591
node_id = response ['node' ]
591
- # crc properly received
592
+ # crc properly received
592
593
if (source_crc == node_crc ):
593
594
print (u" ╰> CRC test for node" , node_id , " : OK." )
594
595
if node_id in failed_nodes :
@@ -613,30 +614,32 @@ def check_crc(device, topic, nodes_to_program):
613
614
# *******************************************************************************
614
615
def reboot_network (device , topic , nodes_to_reboot ):
615
616
for node in nodes_to_reboot :
616
- send_node_command (device , node , topic , BOOTLOADER_STOP )
617
- # delay to let gate send commands
618
- time .sleep (0.01 )
617
+ send_node_command (device , node , topic , BOOTLOADER_STOP )
618
+ # delay to let gate send commands
619
+ time .sleep (0.01 )
619
620
# *******************************************************************************
620
621
# @brief command used to flash luos nodes
621
622
# @param flash function arguments : -g, -t, -b
622
623
# @return None
623
624
# *******************************************************************************
624
625
def luos_flash (args ):
625
- print ('Luos flash subcommand with parameters :' )
626
- print ('\t --baudrate : ' , args .baudrate )
627
- print ('\t --gate : ' , args .gate )
628
- print ('\t --target : ' , args .target )
629
- print ('\t --binary : ' , args .binary )
630
- print ('\t --port : ' , args .port )
631
-
632
626
topic = 1
633
627
634
628
if not (args .port ):
635
629
try :
636
630
args .port = serial_discover (os .getenv ('LUOS_BAUDRATE' , args .baudrate ))[0 ]
637
631
except :
638
- sys .exit ()
639
- return
632
+ print ('Please specify a port to access the network.' )
633
+ return BOOTLOADER_FLASH_PORT_ERROR
634
+
635
+ baudrate = os .getenv ('LUOS_BAUDRATE' , args .baudrate )
636
+
637
+ print ('Luos flash subcommand with parameters :' )
638
+ print ('\t --baudrate : ' , baudrate )
639
+ print ('\t --gate : ' , args .gate )
640
+ print ('\t --target : ' , args .target )
641
+ print ('\t --binary : ' , args .binary )
642
+ print ('\t --port : ' , args .port )
640
643
641
644
# state used to check each step
642
645
machine_state = True
@@ -648,23 +651,26 @@ def luos_flash(args):
648
651
try :
649
652
f = open (FILEPATH , mode = "rb" )
650
653
except IOError :
651
- sys . exit ("Cannot open :" , FILEPATH )
654
+ print ("Cannot open :" , FILEPATH )
652
655
return BOOTLOADER_FLASH_BINARY_ERROR
653
656
else :
654
657
f .close ()
655
658
656
659
# init device
657
- device = Device (args .port , baudrate = os . getenv ( 'LUOS_BAUDRATE' , args . baudrate ) , background_task = False )
660
+ device = Device (args .port , baudrate = baudrate , background_task = False )
658
661
659
662
# find routing table
660
663
state = find_network (device )
664
+ if state is None :
665
+ return BOOTLOADER_DETECT_ERROR
661
666
662
667
# searching nodes to program in network
663
668
(nodes_to_reboot , nodes_to_program ) = create_target_list (args , state )
664
669
665
670
# check if we have available node to program
666
671
if not nodes_to_program :
667
- sys .exit ("No target found :\n " + str (device .nodes ))
672
+ print ("No target found :\n " + str (device .nodes ))
673
+ return BOOTLOADER_DETECT_ERROR
668
674
669
675
# reboot all nodes in bootloader mode
670
676
print ("** Reboot all nodes in bootloader mode **" )
@@ -679,12 +685,14 @@ def luos_flash(args):
679
685
# find routing table in boot mode
680
686
# its necessary to give ids to bootloader services
681
687
state = find_network (device )
688
+ if state is None :
689
+ return BOOTLOADER_DETECT_ERROR
682
690
683
691
# wait before next step
684
692
time .sleep (0.4 )
685
693
686
694
print ("\n ** Programming nodes **" )
687
-
695
+
688
696
# go to header state if node is ready
689
697
for node in nodes_to_program :
690
698
print ("--> Check if node" , node , " is ready." )
@@ -715,7 +723,7 @@ def luos_flash(args):
715
723
total_fails .extend (failed_nodes )
716
724
machine_state = True
717
725
print ("Flash of node: " , failed_nodes , "failed!" )
718
-
726
+
719
727
# inform the node of the end of the loading
720
728
print ("--> Programmation finished, waiting for acknowledge." )
721
729
machine_state , failed_nodes = send_binary_end (device , topic , nodes_to_program )
@@ -759,18 +767,20 @@ def luos_flash(args):
759
767
# @return None
760
768
# *******************************************************************************
761
769
def luos_detect (args ):
762
- print ('Luos detect subcommand on port : ' , args .port )
763
- print ('\t Luos detect subcommand at baudrate : ' , args .baudrate )
764
-
765
770
if not (args .port ):
766
771
try :
767
772
args .port = serial_discover (os .getenv ('LUOS_BAUDRATE' , args .baudrate ))[0 ]
768
773
except :
769
- sys .exit ()
770
- return
774
+ print ('Please specify a port to access the network.' )
775
+ return BOOTLOADER_DETECT_ERROR
776
+
777
+ baudrate = os .getenv ('LUOS_BAUDRATE' , args .baudrate )
778
+
779
+ print ('Luos detect subcommand on port : ' , args .port )
780
+ print ('\t Luos detect subcommand at baudrate : ' , baudrate )
771
781
772
782
# detect network
773
- device = Device (args .port , baudrate = os . getenv ( 'LUOS_BAUDRATE' , args . baudrate ) )
783
+ device = Device (args .port , baudrate = baudrate )
774
784
# print network to user
775
785
print (device .nodes )
776
786
device .close ()
@@ -783,20 +793,20 @@ def luos_detect(args):
783
793
# @return None
784
794
# *******************************************************************************
785
795
def luos_reset (args ):
786
- print ('Luos discover subcommand on port : ' , args .port )
787
- print ('\t Luos discover subcommand at baudrate : ' , args .baudrate )
788
-
789
796
if not (args .port ):
790
797
try :
791
798
args .port = serial_discover (os .getenv ('LUOS_BAUDRATE' , args .baudrate ))[0 ]
792
799
except :
793
- sys .exit ()
794
- return
800
+ return BOOTLOADER_DETECT_ERROR
795
801
802
+ baudrate = os .getenv ('LUOS_BAUDRATE' , args .baudrate )
803
+
804
+ print ('Luos discover subcommand on port : ' , args .port )
805
+ print ('\t Luos discover subcommand at baudrate : ' , args .baudrate )
796
806
797
807
# send rescue command
798
808
print ('Send reset command.' )
799
- port = serial .Serial (args .port , os . getenv ( 'LUOS_BAUDRATE' , args . baudrate ) , timeout = 0.05 )
809
+ port = serial .Serial (args .port , baudrate , timeout = 0.05 )
800
810
rst_cmd = {
801
811
'bootloader' : {
802
812
'command' : {
@@ -811,7 +821,7 @@ def luos_reset(args):
811
821
port .close ()
812
822
813
823
# detect network
814
- device = Device (args .port , baudrate = os . getenv ( 'LUOS_BAUDRATE' , args . baudrate ) , background_task = False )
824
+ device = Device (args .port , baudrate = baudrate , background_task = False )
815
825
816
826
print (device .nodes )
817
827
device .close ()
0 commit comments