|
| 1 | +""" |
| 2 | +Copyright (c) 2018 ARM Limited |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +""" |
| 17 | +import time |
| 18 | +from mbed_host_tests import BaseHostTest |
| 19 | +from mbed_host_tests.host_tests_runner.host_test_default import DefaultTestSelector |
| 20 | + |
| 21 | +DEFAULT_CYCLE_PERIOD = 1.0 |
| 22 | + |
| 23 | +MSG_VALUE_DUMMY = '0' |
| 24 | + |
| 25 | +MSG_KEY_DEVICE_READY = 'ready' |
| 26 | +MSG_KEY_DEVICE_RESET = 'reset' |
| 27 | +MSG_KEY_SYNC = '__sync' |
| 28 | + |
| 29 | +class SystemResetTest(BaseHostTest): |
| 30 | + """Test for the system_reset API. |
| 31 | +
|
| 32 | + Given a device running code |
| 33 | + When the device is restarted using @a system_reset() |
| 34 | + Then the device is restarted |
| 35 | + """ |
| 36 | + |
| 37 | + def __init__(self): |
| 38 | + super(SystemResetTest, self).__init__() |
| 39 | + self.reset = False |
| 40 | + self.test_steps_sequence = self.test_steps() |
| 41 | + # Advance the coroutine to it's first yield statement. |
| 42 | + self.test_steps_sequence.send(None) |
| 43 | + |
| 44 | + def setup(self): |
| 45 | + self.register_callback(MSG_KEY_DEVICE_READY, self.cb_device_ready) |
| 46 | + |
| 47 | + def cb_device_ready(self, key, value, timestamp): |
| 48 | + """Acknowledge device rebooted correctly and feed the test execution |
| 49 | + """ |
| 50 | + self.reset = True |
| 51 | + |
| 52 | + try: |
| 53 | + if self.test_steps_sequence.send(value): |
| 54 | + self.notify_complete(True) |
| 55 | + except (StopIteration, RuntimeError) as exc: |
| 56 | + self.notify_complete(False) |
| 57 | + |
| 58 | + def test_steps(self): |
| 59 | + """Reset the device and check the status |
| 60 | + """ |
| 61 | + system_reset = yield |
| 62 | + self.reset = False |
| 63 | + |
| 64 | + wait_after_reset = self.get_config_item('forced_reset_timeout') |
| 65 | + wait_after_reset = wait_after_reset if wait_after_reset is not None else DEFAULT_CYCLE_PERIOD |
| 66 | + |
| 67 | + self.send_kv(MSG_KEY_DEVICE_RESET, MSG_VALUE_DUMMY) |
| 68 | + time.sleep(wait_after_reset) |
| 69 | + self.send_kv(MSG_KEY_SYNC, MSG_VALUE_DUMMY) |
| 70 | + |
| 71 | + system_reset = yield |
| 72 | + if self.reset == False: |
| 73 | + raise RuntimeError('Platform did not reset as expected.') |
| 74 | + |
| 75 | + # The sequence is correct -- test passed. |
| 76 | + yield True |
0 commit comments