|
| 1 | +/* Mbed Microcontroller Library |
| 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 | +#if !DEVICE_WATCHDOG |
| 18 | +#error [NOT_SUPPORTED] Watchdog not supported for this target |
| 19 | +#endif |
| 20 | + |
| 21 | +#include "greentea-client/test_env.h" |
| 22 | +#include "utest/utest.h" |
| 23 | +#include "unity/unity.h" |
| 24 | +#include "platform/mbed_watchdog_mgr.h" |
| 25 | +#include "watchdog_api.h" |
| 26 | +#include "watchdog_mgr_reset_tests.h" |
| 27 | +#include "mbed.h" |
| 28 | + |
| 29 | +#define TIMEOUT_DELTA_MS 50UL |
| 30 | + |
| 31 | +#define MSG_VALUE_DUMMY "0" |
| 32 | +#define CASE_DATA_INVALID 0xffffffffUL |
| 33 | +#define CASE_DATA_PHASE2_OK 0xfffffffeUL |
| 34 | + |
| 35 | +#define MSG_VALUE_LEN 24 |
| 36 | +#define MSG_KEY_LEN 24 |
| 37 | + |
| 38 | +#define MSG_KEY_DEVICE_READY "ready" |
| 39 | +#define MSG_KEY_START_CASE "start_case" |
| 40 | +#define MSG_KEY_DEVICE_RESET "dev_reset" |
| 41 | + |
| 42 | +using utest::v1::Case; |
| 43 | +using utest::v1::Specification; |
| 44 | +using utest::v1::Harness; |
| 45 | + |
| 46 | +struct testcase_data { |
| 47 | + int index; |
| 48 | + int start_index; |
| 49 | + uint32_t received_data; |
| 50 | +}; |
| 51 | + |
| 52 | +void release_sem(Semaphore *sem) |
| 53 | +{ |
| 54 | + sem->release(); |
| 55 | +} |
| 56 | + |
| 57 | +testcase_data current_case; |
| 58 | + |
| 59 | +bool send_reset_notification(testcase_data *tcdata, uint32_t delay_ms) |
| 60 | +{ |
| 61 | + char msg_value[12]; |
| 62 | + int str_len = snprintf(msg_value, sizeof msg_value, "%02x,%08lx", tcdata->start_index + tcdata->index, delay_ms); |
| 63 | + if (str_len != (sizeof msg_value) - 1) { |
| 64 | + utest_printf("Failed to compose a value string to be sent to host."); |
| 65 | + return false; |
| 66 | + } |
| 67 | + greentea_send_kv(MSG_KEY_DEVICE_RESET, msg_value); |
| 68 | + return true; |
| 69 | +} |
| 70 | + |
| 71 | +void test_simple_reset() |
| 72 | +{ |
| 73 | + // Phase 2. -- verify the test results. |
| 74 | + // Verify if this test case passed based on data received from host. |
| 75 | + if (current_case.received_data != CASE_DATA_INVALID) { |
| 76 | + TEST_ASSERT_EQUAL(CASE_DATA_PHASE2_OK, current_case.received_data); |
| 77 | + current_case.received_data = CASE_DATA_INVALID; |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Phase 1. -- run the test code. |
| 82 | + // Init the watchdog and wait for a device reset. |
| 83 | + if (send_reset_notification(¤t_case, HW_WATCHDOG_TIMEOUT + TIMEOUT_DELTA_MS) == false) { |
| 84 | + TEST_ASSERT_MESSAGE(0, "Dev-host communication error."); |
| 85 | + return; |
| 86 | + } |
| 87 | + TEST_ASSERT_TRUE(mbed_wdog_manager_start()); |
| 88 | + // Block interrupts, including the one from the wdog_manager maintenance ticker. |
| 89 | + core_util_critical_section_enter(); |
| 90 | + wait((HW_WATCHDOG_TIMEOUT + TIMEOUT_DELTA_MS) / 1000.0); // Device reset expected. |
| 91 | + |
| 92 | + // Watchdog reset should have occurred during wait() above; |
| 93 | + |
| 94 | + core_util_critical_section_exit(); |
| 95 | + TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected."); |
| 96 | +} |
| 97 | + |
| 98 | +void test_restart_reset() |
| 99 | +{ |
| 100 | + watchdog_features_t features = hal_watchdog_get_platform_features(); |
| 101 | + if (!features.disable_watchdog) { |
| 102 | + TEST_IGNORE_MESSAGE("Disabling Watchdog not supported for this platform"); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + // Phase 2. -- verify the test results. |
| 107 | + if (current_case.received_data != CASE_DATA_INVALID) { |
| 108 | + TEST_ASSERT_EQUAL(CASE_DATA_PHASE2_OK, current_case.received_data); |
| 109 | + current_case.received_data = CASE_DATA_INVALID; |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + // Phase 1. -- run the test code. |
| 114 | + TEST_ASSERT_TRUE(mbed_wdog_manager_start()); |
| 115 | + // The Watchdog Manager maintenance ticker has a period equal to a half of |
| 116 | + // Watchdog timeout. Wait shorter than that and stop the Watchdog Manager |
| 117 | + // before the Watchdog is kicked by the ticker callback. |
| 118 | + wait((HW_WATCHDOG_TIMEOUT / 4UL) / 1000.0); |
| 119 | + TEST_ASSERT_TRUE(mbed_wdog_manager_stop()); |
| 120 | + // Block interrupts, including the one from the wdog_manager maintenance ticker. |
| 121 | + core_util_critical_section_enter(); |
| 122 | + // Check that stopping the Watchdog Manager prevents a device reset. |
| 123 | + wait((HW_WATCHDOG_TIMEOUT + TIMEOUT_DELTA_MS) / 1000.0); |
| 124 | + core_util_critical_section_exit(); |
| 125 | + |
| 126 | + if (send_reset_notification(¤t_case, HW_WATCHDOG_TIMEOUT + TIMEOUT_DELTA_MS) == false) { |
| 127 | + TEST_ASSERT_MESSAGE(0, "Dev-host communication error."); |
| 128 | + return; |
| 129 | + } |
| 130 | + TEST_ASSERT_TRUE(mbed_wdog_manager_start()); |
| 131 | + // Block interrupts, including the one from the wdog_manager maintenance ticker. |
| 132 | + core_util_critical_section_enter(); |
| 133 | + wait((HW_WATCHDOG_TIMEOUT + TIMEOUT_DELTA_MS) / 1000.0); // Device reset expected. |
| 134 | + |
| 135 | + // Watchdog reset should have occurred during wait() above; |
| 136 | + |
| 137 | + core_util_critical_section_exit(); |
| 138 | + TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected."); |
| 139 | +} |
| 140 | + |
| 141 | +void test_kick_reset() |
| 142 | +{ |
| 143 | + // Phase 2. -- verify the test results. |
| 144 | + if (current_case.received_data != CASE_DATA_INVALID) { |
| 145 | + TEST_ASSERT_EQUAL(CASE_DATA_PHASE2_OK, current_case.received_data); |
| 146 | + current_case.received_data = CASE_DATA_INVALID; |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + // Phase 1. -- run the test code. |
| 151 | + TEST_ASSERT_TRUE(mbed_wdog_manager_start()); |
| 152 | + wait((HW_WATCHDOG_TIMEOUT + TIMEOUT_DELTA_MS) / 1000.0); // Device reset expected. |
| 153 | + |
| 154 | + if (send_reset_notification(¤t_case, HW_WATCHDOG_TIMEOUT + TIMEOUT_DELTA_MS) == false) { |
| 155 | + TEST_ASSERT_MESSAGE(0, "Dev-host communication error."); |
| 156 | + return; |
| 157 | + } |
| 158 | + // Block interrupts, including the one from the wdog_manager maintenance ticker. |
| 159 | + core_util_critical_section_enter(); |
| 160 | + |
| 161 | + wait((HW_WATCHDOG_TIMEOUT + TIMEOUT_DELTA_MS) / 1000.0); // Device reset expected. |
| 162 | + |
| 163 | + // Watchdog reset should have occurred during wait() above; |
| 164 | + |
| 165 | + core_util_critical_section_exit(); |
| 166 | + TEST_ASSERT_MESSAGE(0, "Watchdog did not reset the device as expected."); |
| 167 | +} |
| 168 | + |
| 169 | +utest::v1::status_t case_setup(const Case *const source, const size_t index_of_case) |
| 170 | +{ |
| 171 | + current_case.index = index_of_case; |
| 172 | + return utest::v1::greentea_case_setup_handler(source, index_of_case); |
| 173 | +} |
| 174 | + |
| 175 | +int testsuite_setup(const size_t number_of_cases) |
| 176 | +{ |
| 177 | + GREENTEA_SETUP(90, "watchdog_reset"); |
| 178 | + utest::v1::status_t status = utest::v1::greentea_test_setup_handler(number_of_cases); |
| 179 | + if (status != utest::v1::STATUS_CONTINUE) { |
| 180 | + return status; |
| 181 | + } |
| 182 | + |
| 183 | + char key[MSG_KEY_LEN + 1] = { }; |
| 184 | + char value[MSG_VALUE_LEN + 1] = { }; |
| 185 | + |
| 186 | + greentea_send_kv(MSG_KEY_DEVICE_READY, MSG_VALUE_DUMMY); |
| 187 | + greentea_parse_kv(key, value, MSG_KEY_LEN, MSG_VALUE_LEN); |
| 188 | + |
| 189 | + if (strcmp(key, MSG_KEY_START_CASE) != 0) { |
| 190 | + utest_printf("Invalid message key.\n"); |
| 191 | + return utest::v1::STATUS_ABORT; |
| 192 | + } |
| 193 | + |
| 194 | + int num_args = sscanf(value, "%02x,%08lx", &(current_case.start_index), &(current_case.received_data)); |
| 195 | + if (num_args == 0 || num_args == EOF) { |
| 196 | + utest_printf("Invalid data received from host\n"); |
| 197 | + return utest::v1::STATUS_ABORT; |
| 198 | + } |
| 199 | + |
| 200 | + utest_printf("This test suite is composed of %i test cases. Starting at index %i.\n", number_of_cases, |
| 201 | + current_case.start_index); |
| 202 | + return current_case.start_index; |
| 203 | +} |
| 204 | + |
| 205 | +Case cases[] = { |
| 206 | + Case("Watchdog Manager reset", case_setup, test_simple_reset), |
| 207 | + Case("Watchdog Manager started again", case_setup, test_restart_reset), |
| 208 | + Case("Watchdog Manager's ticker prevents reset", case_setup, test_kick_reset), |
| 209 | +}; |
| 210 | + |
| 211 | +Specification specification((utest::v1::test_setup_handler_t) testsuite_setup, cases); |
| 212 | + |
| 213 | +int main() |
| 214 | +{ |
| 215 | + // Harness will start with a test case index provided by host script. |
| 216 | + return !Harness::run(specification); |
| 217 | +} |
0 commit comments