@@ -36,49 +36,53 @@ def __init__(self, ssid:str='', psk:str='', retry_times:int=10):
36
36
def joinThreadNetwork (dut :IdfDut , thread :thread_parameter ) -> None :
37
37
if thread .dataset != '' :
38
38
command = 'dataset set active ' + thread .dataset
39
- dut . write ( command )
39
+ execute_command ( dut , command )
40
40
dut .expect ('Done' , timeout = 5 )
41
41
else :
42
- dut . write ( 'dataset init new' )
42
+ execute_command ( dut , 'dataset init new' )
43
43
dut .expect ('Done' , timeout = 5 )
44
- dut . write ( 'dataset commit active' )
44
+ execute_command ( dut , 'dataset commit active' )
45
45
dut .expect ('Done' , timeout = 5 )
46
46
if thread .channel != '' :
47
47
command = 'channel ' + thread .channel
48
- dut . write ( command )
48
+ execute_command ( dut , command )
49
49
dut .expect ('Done' , timeout = 5 )
50
50
if thread .exaddr != '' :
51
51
command = 'extaddr ' + thread .exaddr
52
- dut . write ( command )
52
+ execute_command ( dut , command )
53
53
dut .expect ('Done' , timeout = 5 )
54
54
if thread .bbr :
55
- dut . write ( 'bbr enable' )
55
+ execute_command ( dut , 'bbr enable' )
56
56
dut .expect ('Done' , timeout = 5 )
57
- dut .write ('ifconfig up' )
57
+ if thread .deviceRole == 'router' :
58
+ execute_command (dut , 'routerselectionjitter 1' )
59
+ dut .expect ('Done' , timeout = 5 )
60
+ execute_command (dut , 'ifconfig up' )
58
61
dut .expect ('Done' , timeout = 5 )
59
- dut . write ( 'thread start' )
60
- dut . expect ( 'Role detached ->' , timeout = 20 )
61
- if thread . deviceRole == 'leader' :
62
- assert getDeviceRole ( dut ) == 'leader'
63
- elif thread . deviceRole == 'router' :
64
- if getDeviceRole ( dut ) != 'router' :
65
- changeDeviceRole (dut , 'router' )
66
- wait (dut , 10 )
67
- assert getDeviceRole ( dut ) == 'router'
68
- else :
69
- assert False
62
+ execute_command ( dut , 'thread start' )
63
+ assert wait_for_join ( dut , thread . deviceRole )
64
+
65
+
66
+ def wait_for_join ( dut : IdfDut , role : str ) -> bool :
67
+ for _ in range ( 1 , 30 ) :
68
+ if getDeviceRole (dut ) == role :
69
+ wait (dut , 5 )
70
+ return True
71
+ wait ( dut , 1 )
72
+ return False
70
73
71
74
72
75
def joinWiFiNetwork (dut :IdfDut , wifi :wifi_parameter ) -> Tuple [str , int ]:
73
76
clean_buffer (dut )
74
77
ip_address = ''
75
78
information = ''
76
79
for order in range (1 , wifi .retry_times ):
77
- dut . write ( 'wifi connect -s ' + str (wifi .ssid ) + ' -p ' + str (wifi .psk ) )
78
- tmp = dut . expect ( pexpect . TIMEOUT , timeout = 10 )
80
+ command = 'wifi connect -s ' + str (wifi .ssid ) + ' -p ' + str (wifi .psk )
81
+ tmp = get_ouput_string ( dut , command , 10 )
79
82
if 'sta ip' in str (tmp ):
80
83
ip_address = re .findall (r'sta ip: (\w+.\w+.\w+.\w+),' , str (tmp ))[0 ]
81
- information = dut .expect (r'wifi sta (\w+ \w+ \w+)\W' , timeout = 20 )[1 ].decode ()
84
+ if 'wifi sta' in str (tmp ):
85
+ information = re .findall (r'wifi sta (\w+ \w+ \w+)\W' , str (tmp ))[0 ]
82
86
if information == 'is connected successfully' :
83
87
break
84
88
assert information == 'is connected successfully'
@@ -87,38 +91,40 @@ def joinWiFiNetwork(dut:IdfDut, wifi:wifi_parameter) -> Tuple[str, int]:
87
91
88
92
def getDeviceRole (dut :IdfDut ) -> str :
89
93
clean_buffer (dut )
90
- dut . write ( 'state' )
91
- role = dut .expect (r'state \W+(\w+)\W+Done' , timeout = 5 )[1 ].decode ()
94
+ execute_command ( dut , 'state' )
95
+ role = dut .expect (r'\W+(\w+)\W+Done' , timeout = 5 )[1 ].decode ()
92
96
print (role )
93
97
return str (role )
94
98
95
99
96
100
def changeDeviceRole (dut :IdfDut , role :str ) -> None :
97
101
command = 'state ' + role
98
- dut . write ( command )
102
+ execute_command ( dut , command )
99
103
100
104
101
105
def getDataset (dut :IdfDut ) -> str :
102
106
clean_buffer (dut )
103
- dut . write ( 'dataset active -x' )
107
+ execute_command ( dut , 'dataset active -x' )
104
108
dut_data = dut .expect (r'\n(\w{212})\r' , timeout = 5 )[1 ].decode ()
105
109
return str (dut_data )
106
110
107
111
108
112
def reset_thread (dut :IdfDut ) -> None :
109
- dut .expect ('>' )
113
+ dut .expect ('>' , timeout = 10 )
110
114
wait (dut , 3 )
111
- dut .write ('factoryreset' )
115
+ clean_buffer (dut )
116
+ execute_command (dut , 'factoryreset' )
112
117
dut .expect ('OpenThread attached to netif' , timeout = 20 )
113
- dut .expect ('>' )
118
+ dut .expect ('>' , timeout = 10 )
114
119
wait (dut , 3 )
120
+ clean_buffer (dut )
115
121
116
122
117
123
# get the mleid address of the thread
118
124
def get_mleid_addr (dut :IdfDut ) -> str :
119
125
dut_adress = ''
120
126
clean_buffer (dut )
121
- dut . write ( 'ipaddr mleid' )
127
+ execute_command ( dut , 'ipaddr mleid' )
122
128
dut_adress = dut .expect (r'\n((?:\w+:){7}\w+)\r' , timeout = 5 )[1 ].decode ()
123
129
return dut_adress
124
130
@@ -127,7 +133,7 @@ def get_mleid_addr(dut:IdfDut) -> str:
127
133
def get_rloc_addr (dut :IdfDut ) -> str :
128
134
dut_adress = ''
129
135
clean_buffer (dut )
130
- dut . write ( 'ipaddr rloc' )
136
+ execute_command ( dut , 'ipaddr rloc' )
131
137
dut_adress = dut .expect (r'\n((?:\w+:){7}\w+)\r' , timeout = 5 )[1 ].decode ()
132
138
return dut_adress
133
139
@@ -136,7 +142,7 @@ def get_rloc_addr(dut:IdfDut) -> str:
136
142
def get_linklocal_addr (dut :IdfDut ) -> str :
137
143
dut_adress = ''
138
144
clean_buffer (dut )
139
- dut . write ( 'ipaddr linklocal' )
145
+ execute_command ( dut , 'ipaddr linklocal' )
140
146
dut_adress = dut .expect (r'\n((?:\w+:){7}\w+)\r' , timeout = 5 )[1 ].decode ()
141
147
return dut_adress
142
148
@@ -147,15 +153,15 @@ def get_global_unicast_addr(dut:IdfDut, br:IdfDut) -> str:
147
153
clean_buffer (br )
148
154
omrprefix = get_omrprefix (br )
149
155
clean_buffer (dut )
150
- dut . write ( 'ipaddr' )
156
+ execute_command ( dut , 'ipaddr' )
151
157
dut_adress = dut .expect (r'(%s(?:\w+:){3}\w+)\r' % str (omrprefix ), timeout = 5 )[1 ].decode ()
152
158
return dut_adress
153
159
154
160
155
161
# ping of thread
156
162
def ot_ping (dut :IdfDut , target :str , times :int ) -> Tuple [int , int ]:
157
163
command = 'ping ' + str (target ) + ' 0 ' + str (times )
158
- dut . write ( command )
164
+ execute_command ( dut , command )
159
165
transmitted = dut .expect (r'(\d+) packets transmitted' , timeout = 30 )[1 ].decode ()
160
166
tx_count = int (transmitted )
161
167
received = dut .expect (r'(\d+) packets received' , timeout = 30 )[1 ].decode ()
@@ -250,23 +256,21 @@ def is_joined_wifi_network(br:IdfDut) -> bool:
250
256
251
257
252
258
def check_ipmaddr (dut :IdfDut ) -> bool :
253
- clean_buffer (dut )
254
- dut .write ('ipmaddr' )
255
- info = dut .expect (pexpect .TIMEOUT , timeout = 2 )
259
+ info = get_ouput_string (dut , 'ipmaddr' , 2 )
256
260
if thread_ipv6_group in str (info ):
257
261
return True
258
262
return False
259
263
260
264
261
265
def thread_is_joined_group (dut :IdfDut ) -> bool :
262
266
command = 'mcast join ' + thread_ipv6_group
263
- dut . write ( command )
267
+ execute_command ( dut , command )
264
268
dut .expect ('Done' , timeout = 5 )
265
269
order = 0
266
270
while order < 3 :
267
271
if check_ipmaddr (dut ):
268
272
return True
269
- dut . write ( command )
273
+ execute_command ( dut , command )
270
274
wait (dut , 2 )
271
275
order = order + 1
272
276
return False
@@ -451,12 +455,34 @@ def decimal_to_hex(decimal_str:str) -> str:
451
455
452
456
453
457
def get_omrprefix (br :IdfDut ) -> str :
454
- br .write ('br omrprefix' )
458
+ clean_buffer (br )
459
+ execute_command (br , 'br omrprefix' )
455
460
omrprefix = br .expect (r'Local: ((?:\w+:){4}):/\d+\r' , timeout = 5 )[1 ].decode ()
456
461
return str (omrprefix )
457
462
458
463
464
+ def get_onlinkprefix (br :IdfDut ) -> str :
465
+ clean_buffer (br )
466
+ execute_command (br , 'br onlinkprefix' )
467
+ onlinkprefix = br .expect (r'Local: ((?:\w+:){4}):/\d+\r' , timeout = 5 )[1 ].decode ()
468
+ return str (onlinkprefix )
469
+
470
+
459
471
def get_nat64prefix (br :IdfDut ) -> str :
460
- br .write ('br nat64prefix' )
472
+ clean_buffer (br )
473
+ execute_command (br , 'br nat64prefix' )
461
474
nat64prefix = br .expect (r'Local: ((?:\w+:){6}):/\d+' , timeout = 5 )[1 ].decode ()
462
475
return str (nat64prefix )
476
+
477
+
478
+ def execute_command (dut :IdfDut , command :str ) -> None :
479
+ dut .write (command )
480
+ dut .expect (command , timeout = 3 )
481
+
482
+
483
+ def get_ouput_string (dut :IdfDut , command :str , wait_time :int ) -> str :
484
+ clean_buffer (dut )
485
+ execute_command (dut , command )
486
+ tmp = dut .expect (pexpect .TIMEOUT , timeout = wait_time )
487
+ clean_buffer (dut )
488
+ return str (tmp )
0 commit comments