1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
#
# RTEMS Tools Project (http://www.rtems.org/)
# Copyright 2013, 2020 Chris Johns (chrisj@rtems.org)
# All rights reserved.
#
# This file is part of the RTEMS Tools package in 'rtems-tools'.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
# RTEMS Testing TFTP Interface
#
from __future__ import print_function
import errno
import logging
import threading
import time
import sys
from rtemstoolkit import error
from rtemstoolkit import log
from rtemstoolkit import reraise
import tester.rt.tftpserver
class tftp(object):
'''RTEMS Testing TFTP base.'''
def __init__(self, bsp_arch, bsp, trace = False):
self.trace = trace
self.lock_trace = False
self.lock = threading.RLock()
self.bsp = bsp
self.bsp_arch = bsp_arch
self._init()
def __del__(self):
self.kill()
def _init(self):
self.output_length = None
self.console = None
self.server = None
self.port = 0
self.exe = None
self.timeout = None
self.test_too_long = None
self.timer = None
self.opened = False
self.running = False
self.finished = False
self.caught = None
self.target_state = 'reset'
def _lock(self, msg):
if self.lock_trace:
log.trace('|[ LOCK:%s ]|' % (msg))
self.lock.acquire()
if self.lock_trace:
log.trace('|[ LOCK:%s done ]|' % (msg))
def _unlock(self, msg):
if self.lock_trace:
log.trace('|] UNLOCK:%s [|' % (msg))
self.lock.release()
if self.lock_trace:
log.trace('|[ UNLOCK:%s done ]|' % (msg))
def _trace(self, text):
if log.tracing:
log.trace('] tftp: ' + text)
def _console(self, text):
if self.console:
self.console(text)
def _set_target_state(self, state):
self._trace('set state: ' + state)
self.target_state = state
def _finished(self):
self.server = None
self.exe = None
def _stop(self, finished = True):
try:
if self.server is not None:
self.server.stop()
self.finished = Finished
except:
pass
def _run_finished(self):
while self.opened and (self.running or not self.finished):
self._unlock('_run_finished')
time.sleep(0.2)
self._lock('_run_finished')
def _kill(self):
self._stop()
self._run_finished()
def _timeout(self):
self._stop()
self._run_finished()
if self.timeout is not None:
self.timeout()
def _test_too_long(self):
self._stop()
self._run_finished()
if self.test_too_long is not None:
self.test_too_long()
def _exe_handle(self, req_file, raddress, rport):
self._lock('_exe_handle')
exe = self.exe
self.exe = None
self._unlock('_exe_handle')
if exe is not None:
self._console('tftp: %s' % (exe))
return open(exe, "rb")
self._console('tftp: re-requesting exe; target must have reset')
self._stop()
return None
def _listener(self, exe):
self.server = tester.rt.tftpserver.tftp_server(host = 'all',
port = self.port,
timeout = 10,
forced_file = exe,
sessions = 1)
try:
if log.tracing:
self.server.trace_packets()
self.server.start()
self.server.run()
except:
self.server.stop()
raise
def _runner(self):
self._trace('session: start')
self._lock('_runner')
exe = self.exe
self.exe = None
self._unlock('_runner')
caught = None
try:
self._lock('_runner')
state = self.target_state
self._unlock('_runner')
self._trace('runner: ' + state)
while state not in ['shutdown', 'finished']:
if state != 'running':
self._trace('listening: begin: ' + state)
self._listener(exe)
self._lock('_runner')
if self.target_state == 'booting':
self._set_target_state('loaded')
self._unlock('_runner')
self._trace('listening: end: ' + state)
else:
time.sleep(0.25)
self._lock('_runner')
state = self.target_state
self._unlock('_runner')
except:
caught = sys.exc_info()
self._trace('session: exception')
self._lock('_runner')
self._trace('runner: ' + self.target_state)
self.caught = caught
self.finished = True
self.running = False
self._unlock('_runner')
self._trace('session: end')
def open(self, executable, port, output_length, console, timeout):
self._trace('open: start')
self._lock('_open')
if self.exe is not None:
self._unlock('_open')
raise error.general('tftp: already running')
self._init()
self.output_length = output_length
self.console = console
self.port = port
self.exe = executable
self.timeout = timeout[2]
self.test_too_long = timeout[3]
self.opened = True
self.running = True
self.listener = threading.Thread(target = self._runner,
name = 'tftp-listener')
self._unlock('_open: start listner')
self._console('tftp: exe: %s' % (executable))
self.listener.start()
self._lock('_open: start listner')
step = 0.25
period = timeout[0]
seconds = timeout[1]
output_len = self.output_length()
while not self.finished and period > 0 and seconds > 0:
if not self.running and self.caught:
break
self._unlock('_open: loop')
current_length = self.output_length()
if output_length != current_length:
period = timeout[0]
output_length = current_length
if seconds < step:
seconds = 0
else:
seconds -= step
if period < step:
step = period
period = 0
else:
period -= step
time.sleep(step)
self._lock('_open: loop')
self._trace('open: finished')
if not self.finished:
if period == 0:
self._timeout()
elif seconds == 0:
self._test_too_long()
caught = self.caught
self._init()
self._unlock('_open')
if caught is not None:
reraise.reraise(*caught)
def kill(self):
self._trace('kill')
self._lock('kill')
self._set_target_state('shutdown')
self._kill()
self._unlock('kill')
def target_restart(self, started):
self._trace('target: restart: %d' % (started))
self._lock('target_restart')
try:
if not started:
if self.server is not None:
self._trace('server: stop')
self._stop(finished = False)
self._trace('server: stop done')
state = 'booting'
else:
state = 'shutdown'
self._set_target_state(state)
finally:
self._unlock('target_restart')
def target_reset(self, started):
self._trace('target: reset: %d' % (started))
self._lock('target_reset')
try:
if not started:
if self.server is not None:
self._trace('server: stop')
self.server.stop()
state = 'booting'
else:
state = 'shutdown'
self._set_target_state(state)
finally:
self._unlock('target_reset')
def target_start(self):
self._trace('target: start')
self._lock('target_start')
self._set_target_state('running')
self._unlock('target_start')
def target_end(self):
self._trace('target: end')
self._lock('target_end')
self._set_target_state('finished')
self._unlock('target_end')
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
executable = sys.argv[1]
else:
executable = None
t = tftp('arm', 'beagleboneblack')
t.open(executable)
|