Skip to content

Commit 7d96d9e

Browse files
committed
Merge branch 'backport/fix_openthread_ci' into 'release/v5.1'
OpenThread CI: add a function for executing commands(backport v5.1) See merge request espressif/esp-idf!23602
2 parents 1111fd2 + 938bcc0 commit 7d96d9e

File tree

2 files changed

+117
-89
lines changed

2 files changed

+117
-89
lines changed

examples/openthread/ot_ci_function.py

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,49 +36,53 @@ def __init__(self, ssid:str='', psk:str='', retry_times:int=10):
3636
def joinThreadNetwork(dut:IdfDut, thread:thread_parameter) -> None:
3737
if thread.dataset != '':
3838
command = 'dataset set active ' + thread.dataset
39-
dut.write(command)
39+
execute_command(dut, command)
4040
dut.expect('Done', timeout=5)
4141
else:
42-
dut.write('dataset init new')
42+
execute_command(dut, 'dataset init new')
4343
dut.expect('Done', timeout=5)
44-
dut.write('dataset commit active')
44+
execute_command(dut, 'dataset commit active')
4545
dut.expect('Done', timeout=5)
4646
if thread.channel != '':
4747
command = 'channel ' + thread.channel
48-
dut.write(command)
48+
execute_command(dut, command)
4949
dut.expect('Done', timeout=5)
5050
if thread.exaddr != '':
5151
command = 'extaddr ' + thread.exaddr
52-
dut.write(command)
52+
execute_command(dut, command)
5353
dut.expect('Done', timeout=5)
5454
if thread.bbr:
55-
dut.write('bbr enable')
55+
execute_command(dut, 'bbr enable')
5656
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')
5861
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
7073

7174

7275
def joinWiFiNetwork(dut:IdfDut, wifi:wifi_parameter) -> Tuple[str, int]:
7376
clean_buffer(dut)
7477
ip_address = ''
7578
information = ''
7679
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)
7982
if 'sta ip' in str(tmp):
8083
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]
8286
if information == 'is connected successfully':
8387
break
8488
assert information == 'is connected successfully'
@@ -87,38 +91,40 @@ def joinWiFiNetwork(dut:IdfDut, wifi:wifi_parameter) -> Tuple[str, int]:
8791

8892
def getDeviceRole(dut:IdfDut) -> str:
8993
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()
9296
print(role)
9397
return str(role)
9498

9599

96100
def changeDeviceRole(dut:IdfDut, role:str) -> None:
97101
command = 'state ' + role
98-
dut.write(command)
102+
execute_command(dut, command)
99103

100104

101105
def getDataset(dut:IdfDut) -> str:
102106
clean_buffer(dut)
103-
dut.write('dataset active -x')
107+
execute_command(dut, 'dataset active -x')
104108
dut_data = dut.expect(r'\n(\w{212})\r', timeout=5)[1].decode()
105109
return str(dut_data)
106110

107111

108112
def reset_thread(dut:IdfDut) -> None:
109-
dut.expect('>')
113+
dut.expect('>', timeout=10)
110114
wait(dut, 3)
111-
dut.write('factoryreset')
115+
clean_buffer(dut)
116+
execute_command(dut, 'factoryreset')
112117
dut.expect('OpenThread attached to netif', timeout=20)
113-
dut.expect('>')
118+
dut.expect('>', timeout=10)
114119
wait(dut, 3)
120+
clean_buffer(dut)
115121

116122

117123
# get the mleid address of the thread
118124
def get_mleid_addr(dut:IdfDut) -> str:
119125
dut_adress = ''
120126
clean_buffer(dut)
121-
dut.write('ipaddr mleid')
127+
execute_command(dut, 'ipaddr mleid')
122128
dut_adress = dut.expect(r'\n((?:\w+:){7}\w+)\r', timeout=5)[1].decode()
123129
return dut_adress
124130

@@ -127,7 +133,7 @@ def get_mleid_addr(dut:IdfDut) -> str:
127133
def get_rloc_addr(dut:IdfDut) -> str:
128134
dut_adress = ''
129135
clean_buffer(dut)
130-
dut.write('ipaddr rloc')
136+
execute_command(dut, 'ipaddr rloc')
131137
dut_adress = dut.expect(r'\n((?:\w+:){7}\w+)\r', timeout=5)[1].decode()
132138
return dut_adress
133139

@@ -136,7 +142,7 @@ def get_rloc_addr(dut:IdfDut) -> str:
136142
def get_linklocal_addr(dut:IdfDut) -> str:
137143
dut_adress = ''
138144
clean_buffer(dut)
139-
dut.write('ipaddr linklocal')
145+
execute_command(dut, 'ipaddr linklocal')
140146
dut_adress = dut.expect(r'\n((?:\w+:){7}\w+)\r', timeout=5)[1].decode()
141147
return dut_adress
142148

@@ -147,15 +153,15 @@ def get_global_unicast_addr(dut:IdfDut, br:IdfDut) -> str:
147153
clean_buffer(br)
148154
omrprefix = get_omrprefix(br)
149155
clean_buffer(dut)
150-
dut.write('ipaddr')
156+
execute_command(dut, 'ipaddr')
151157
dut_adress = dut.expect(r'(%s(?:\w+:){3}\w+)\r' % str(omrprefix), timeout=5)[1].decode()
152158
return dut_adress
153159

154160

155161
# ping of thread
156162
def ot_ping(dut:IdfDut, target:str, times:int) -> Tuple[int, int]:
157163
command = 'ping ' + str(target) + ' 0 ' + str(times)
158-
dut.write(command)
164+
execute_command(dut, command)
159165
transmitted = dut.expect(r'(\d+) packets transmitted', timeout=30)[1].decode()
160166
tx_count = int(transmitted)
161167
received = dut.expect(r'(\d+) packets received', timeout=30)[1].decode()
@@ -250,23 +256,21 @@ def is_joined_wifi_network(br:IdfDut) -> bool:
250256

251257

252258
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)
256260
if thread_ipv6_group in str(info):
257261
return True
258262
return False
259263

260264

261265
def thread_is_joined_group(dut:IdfDut) -> bool:
262266
command = 'mcast join ' + thread_ipv6_group
263-
dut.write(command)
267+
execute_command(dut, command)
264268
dut.expect('Done', timeout=5)
265269
order = 0
266270
while order < 3:
267271
if check_ipmaddr(dut):
268272
return True
269-
dut.write(command)
273+
execute_command(dut, command)
270274
wait(dut, 2)
271275
order = order + 1
272276
return False
@@ -451,12 +455,34 @@ def decimal_to_hex(decimal_str:str) -> str:
451455

452456

453457
def get_omrprefix(br:IdfDut) -> str:
454-
br.write('br omrprefix')
458+
clean_buffer(br)
459+
execute_command(br, 'br omrprefix')
455460
omrprefix = br.expect(r'Local: ((?:\w+:){4}):/\d+\r', timeout=5)[1].decode()
456461
return str(omrprefix)
457462

458463

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+
459471
def get_nat64prefix(br:IdfDut) -> str:
460-
br.write('br nat64prefix')
472+
clean_buffer(br)
473+
execute_command(br, 'br nat64prefix')
461474
nat64prefix = br.expect(r'Local: ((?:\w+:){6}):/\d+', timeout=5)[1].decode()
462475
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

Comments
 (0)