SW watchdog unittest cases

-Added the mock class function to mock mbed_assert_internal
  -Added the unit test case to test start,kick,stop
  -Modified the interface api name from is_alive to process
  -added the unit test cases for process
pull/10645/head
Rajkumar Kanagaraj 2019-01-25 11:41:23 +00:00 committed by Filip Jagodzinski
parent eb98c38226
commit 47030cd23f
10 changed files with 388 additions and 64 deletions

View File

@ -0,0 +1,142 @@
/*
* Copyright (c) 2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "drivers/Watchdog.h"
class TestWatchdog : public testing::Test {
public:
static uint32_t expect_assert_count;
static uint32_t expect_reset_count;
protected:
virtual void SetUp() {}
virtual void TearDown() {}
};
uint32_t TestWatchdog::expect_assert_count = 0;
uint32_t TestWatchdog::expect_reset_count = 0;
void mbed_assert_internal(const char *expr, const char *file, int line)
{
TestWatchdog::expect_assert_count++;
}
void mock_system_reset()
{
TestWatchdog::expect_reset_count++;
}
TEST_F(TestWatchdog, wdog_constructor)
{
EXPECT_LE(sizeof(mbed::Watchdog), 1024);
mbed::Watchdog watchdog(500, "watchdog_unittest");
}
TEST_F(TestWatchdog, wdog_constructor_with_default_value)
{
mbed::Watchdog watchdog;
}
TEST_F(TestWatchdog, wdog_start_pass)
{
mbed::Watchdog watchdog(500, "watchdog_unittest");
watchdog.start();
watchdog.stop();
EXPECT_EQ(0, TestWatchdog::expect_assert_count);
}
TEST_F(TestWatchdog, wdog_kick_pass)
{
mbed::Watchdog watchdog(500, "watchdog_unittest");
watchdog.start();
watchdog.kick();
watchdog.stop();
EXPECT_EQ(0, TestWatchdog::expect_assert_count);
}
TEST_F(TestWatchdog, wdog_stop_fail)
{
mbed::Watchdog watchdog(500, "watchdog_unittest");
watchdog.start();
watchdog.stop();
watchdog.stop();
EXPECT_EQ(1, TestWatchdog::expect_assert_count);
TestWatchdog::expect_assert_count = 0;
}
TEST_F(TestWatchdog, wdog_kick_fail)
{
mbed::Watchdog watchdog(500, "watchdog_unittest");
watchdog.kick();
EXPECT_EQ(1, TestWatchdog::expect_assert_count);
TestWatchdog::expect_assert_count = 0;
}
TEST_F(TestWatchdog, wdog_start_kick_pass)
{
mbed::Watchdog watchdog(500, "watchdog_unittest");
mbed::Watchdog watchdog1(600, "watchdog_unittest_1");
mbed::Watchdog watchdog2(700, "watchdog_unittest_2");
watchdog.start();
watchdog1.start();
watchdog2.start();
watchdog1.kick();
watchdog.kick();
watchdog2.kick();
watchdog1.stop();
watchdog.stop();
watchdog2.stop();
EXPECT_EQ(0, TestWatchdog::expect_assert_count);
}
TEST_F(TestWatchdog, wdog_start_process_pass)
{
mbed::Watchdog watchdog(500, "watchdog_unittest");
watchdog.start();
watchdog.kick();
watchdog.process((HW_WATCHDOG_TIMEOUT / 2));
watchdog.stop();
EXPECT_EQ(0, TestWatchdog::expect_assert_count);
EXPECT_EQ(0, TestWatchdog::expect_reset_count);
}
TEST_F(TestWatchdog, wdog_start_process_fail)
{
mbed::Watchdog watchdog(500, "watchdog_unittest");
mbed::Watchdog watchdog1(500, "watchdog_unittest-1");
watchdog.start();
watchdog1.start();
watchdog.process((HW_WATCHDOG_TIMEOUT / 2));
watchdog.process((HW_WATCHDOG_TIMEOUT / 2));
watchdog1.kick();
watchdog.process((HW_WATCHDOG_TIMEOUT / 2));
watchdog.process((HW_WATCHDOG_TIMEOUT / 2));
watchdog.stop();
watchdog1.stop();
EXPECT_EQ(0, TestWatchdog::expect_assert_count);
EXPECT_EQ(1, TestWatchdog::expect_reset_count);
TestWatchdog::expect_reset_count = 0;
}
TEST_F(TestWatchdog, wdog_start_fail)
{
mbed::Watchdog watchdog(500, "watchdog_unittest");
watchdog.start();
watchdog.start();
watchdog.stop();
EXPECT_EQ(1, TestWatchdog::expect_assert_count);
TestWatchdog::expect_assert_count = 0;
}

View File

@ -0,0 +1,27 @@
####################
# UNIT TESTS
####################
set(TEST_SUITE_NAME "Watchdog")
# Add test specific include paths
set(unittest-includes ${unittest-includes}
.
../hal
)
# Source files
set(unittest-sources
../drivers/Watchdog.cpp
)
# Test files
set(unittest-test-sources
drivers/Watchdog/test_watchdog.cpp
stubs/mbed_critical_stub.c
)
# defines
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEVICE_WATCHDOG -DMBED_WDOG_ASSERT=1")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEVICE_WATCHDOG -DMBED_WDOG_ASSERT=1")

View File

@ -18,10 +18,14 @@
#include "Watchdog.h"
namespace mbed
namespace mbed {
Watchdog *Watchdog::_first;
Watchdog::Watchdog(uint32_t timeout, const char *const str): _name(str)
{
Watchdog *Watchdog::first;
}
void Watchdog::start()
{
@ -38,7 +42,7 @@ void Watchdog::stop()
}
void Watchdog::is_alive()
void Watchdog::process(uint32_t elapsed_ms)
{
}

View File

@ -59,12 +59,12 @@ TEST_F(TestMbedWatchdogMgr, test_mbed_watchdog_mgr_stop_fail)
TEST_F(TestMbedWatchdogMgr, test_mbed_wdog_manager_get_max_timeout)
{
EXPECT_EQ(0xFFFFFFFF,mbed_wdog_manager_get_max_timeout());
EXPECT_EQ(0xFFFFFFFF, mbed_wdog_manager_get_max_timeout());
}
TEST_F(TestMbedWatchdogMgr, mbed_wdog_manager_get_reload_value)
TEST_F(TestMbedWatchdogMgr, test_mbed_wdog_manager_get_timeout)
{
EXPECT_EQ(500,mbed_wdog_manager_get_reload_value());
EXPECT_EQ(500, mbed_wdog_manager_get_timeout());
}

View File

@ -0,0 +1,135 @@
/** \addtogroup platform */
/** @{*/
/**
* \defgroup platform_Assert Assert macros
* @{
*/
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBED_ASSERT_H
#define MBED_ASSERT_H
#include "mbed_preprocessor.h"
#include "mbed_toolchain.h"
#ifdef __cplusplus
extern "C" {
#endif
/** Internal mbed assert function which is invoked when MBED_ASSERT macro fails.
* This function is active only if NDEBUG is not defined prior to including this
* assert header file.
* In case of MBED_ASSERT failing condition, error() is called with the assertation message.
* @param expr Expression to be checked.
* @param file File where assertation failed.
* @param line Failing assertation line number.
*/
MBED_NORETURN void mbed_assert_internal(const char *expr, const char *file, int line);
#ifdef __cplusplus
}
#endif
/** MBED_ASSERT
* Declare runtime assertions: results in runtime error if condition is false
*
* @note
* Use of MBED_ASSERT is limited to Debug and Develop builds.
*
* @code
*
* int Configure(serial_t *obj) {
* MBED_ASSERT(obj);
* }
* @endcode
*/
#if defined( NDEBUG ) && !defined (MBED_WDOG_ASSERT)
#define MBED_ASSERT(expr) ((void)0)
#else
#define MBED_ASSERT(expr) \
do { \
if (!(expr)) { \
mbed_assert_internal(#expr, __FILE__, __LINE__); \
} \
} while (0)
#endif
/** MBED_STATIC_ASSERT
* Declare compile-time assertions, results in compile-time error if condition is false
*
* The assertion acts as a declaration that can be placed at file scope, in a
* code block (except after a label), or as a member of a C++ class/struct/union.
*
* @note
* Use of MBED_STATIC_ASSERT as a member of a struct/union is limited:
* - In C++, MBED_STATIC_ASSERT is valid in class/struct/union scope.
* - In C, MBED_STATIC_ASSERT is not valid in struct/union scope, and
* MBED_STRUCT_STATIC_ASSERT is provided as an alternative that is valid
* in C and C++ class/struct/union scope.
*
* @code
* MBED_STATIC_ASSERT(MBED_LIBRARY_VERSION >= 120,
* "The mbed library must be at least version 120");
*
* int main() {
* MBED_STATIC_ASSERT(sizeof(int) >= sizeof(char),
* "An int must be larger than a char");
* }
* @endcode
*/
#if defined(__cplusplus) && (__cplusplus >= 201103L || __cpp_static_assert >= 200410L)
#define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
#elif !defined(__cplusplus) && __STDC_VERSION__ >= 201112L
#define MBED_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
#elif defined(__cplusplus) && defined(__GNUC__) && defined(__GXX_EXPERIMENTAL_CXX0X__) \
&& (__GNUC__*100 + __GNUC_MINOR__) > 403L
#define MBED_STATIC_ASSERT(expr, msg) __extension__ static_assert(expr, msg)
#elif !defined(__cplusplus) && defined(__GNUC__) && !defined(__CC_ARM) \
&& (__GNUC__*100 + __GNUC_MINOR__) > 406L
#define MBED_STATIC_ASSERT(expr, msg) __extension__ _Static_assert(expr, msg)
#elif defined(__ICCARM__)
#define MBED_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
#else
#define MBED_STATIC_ASSERT(expr, msg) \
enum {MBED_CONCAT(MBED_ASSERTION_AT_, __LINE__) = sizeof(char[(expr) ? 1 : -1])}
#endif
/** MBED_STRUCT_STATIC_ASSERT
* Declare compile-time assertions, results in compile-time error if condition is false
*
* Unlike MBED_STATIC_ASSERT, MBED_STRUCT_STATIC_ASSERT can and must be used
* as a member of a C/C++ class/struct/union.
*
* @code
* struct thing {
* MBED_STATIC_ASSERT(2 + 2 == 4,
* "Hopefully the universe is mathematically consistent");
* };
* @endcode
*/
#define MBED_STRUCT_STATIC_ASSERT(expr, msg) int : (expr) ? 0 : -1
#endif
/**@}*/
/**@}*/

View File

@ -15,3 +15,12 @@
* limitations under the License.
*/
/** Resets the processor and most of the sub-system
*
* @note Does not affect the debug sub-system
*/
extern void mock_system_reset();
MBED_NORETURN static inline void system_reset(void)
{
mock_system_reset();
}

View File

@ -18,15 +18,22 @@
#include "Watchdog.h"
namespace mbed
{
namespace mbed {
Watchdog *Watchdog::first;
Watchdog *Watchdog::_first = NULL;
Watchdog::Watchdog(uint32_t timeout, const char *const str): _name(str)
{
_current_count = 0;
_is_initialized = false;
_next = NULL;
_max_timeout = timeout;
}
void Watchdog::add_to_list()
{
this->next = first;
first = this;
this->_next = _first;
_first = this;
_is_initialized = true;
}
@ -57,44 +64,45 @@ void Watchdog::stop()
void Watchdog::remove_from_list()
{
Watchdog *cur_ptr = first,
Watchdog *cur_ptr = _first,
*prev_ptr = NULL;
while(cur_ptr != NULL) {
if(cur_ptr == this) {
if(cur_ptr == first) {
prev_ptr = first;
first = cur_ptr->next;
prev_ptr->next = NULL;
while (cur_ptr != NULL) {
if (cur_ptr == this) {
if (cur_ptr == _first) {
prev_ptr = _first;
_first = cur_ptr->_next;
prev_ptr->_next = NULL;
} else {
prev_ptr->next = cur_ptr->next;
cur_ptr->next = NULL;
prev_ptr->_next = cur_ptr->_next;
cur_ptr->_next = NULL;
}
_is_initialized = false;
break;
} else {
prev_ptr = cur_ptr;
cur_ptr = cur_ptr->next;
cur_ptr = cur_ptr->_next;
}
}
}
void Watchdog::is_alive()
void Watchdog::process(uint32_t elapsed_ms)
{
Watchdog *cur_ptr = first;
while(cur_ptr != NULL) {
if(cur_ptr->_current_count > cur_ptr->_max_timeout) {
Watchdog *cur_ptr = _first;
while (cur_ptr != NULL) {
if (cur_ptr->_current_count > cur_ptr->_max_timeout) {
system_reset();
} else {
cur_ptr->_current_count++;
cur_ptr = cur_ptr->next;
cur_ptr->_current_count += elapsed_ms;
}
cur_ptr = cur_ptr->_next;
}
}
Watchdog::~Watchdog()
{
if(_is_initialized)
if (_is_initialized) {
stop();
}
}
} // namespace mbed

View File

@ -25,9 +25,7 @@
#include "rtos/ThisThread.h"
#include "platform/mbed_critical.h"
#include "platform/mbed_power_mgmt.h"
#include <string>
namespace mbed
{
namespace mbed {
/** \addtogroup drivers */
/** A system timer that will reset the system in the case of system failures or
@ -36,7 +34,7 @@ namespace mbed
* Example:
* @code
*
* Watchdog watchdog = Watchdog(300,"Software Watchdog");
* Watchdog watchdog(300,"Software Watchdog");
* watchdog.start();
*
* while (true) {
@ -47,13 +45,9 @@ namespace mbed
* @endcode
* @ingroup drivers
*/
class Watchdog
{
class Watchdog {
public:
Watchdog() {}
Watchdog(uint32_t timeout = 0,const char *str = NULL):_max_timeout(timeout),name(str),_current_count(0),_is_initialized(false),next(NULL) {}
Watchdog(uint32_t timeout = 1, const char *const str = NULL);
~Watchdog();
public:
@ -81,14 +75,17 @@ public:
*/
void kick();
/** Get the watchdog timer refresh value
/** mbed_watchdog_manager(runs by periodic call from ticker) used this API interface
* to go through all the registered user/threads of watchdog.
*
* @param elapsed_ms completed ticker callback elapsed milliseconds
*
* This function should be called from mbed_watchdog_manager_kick to monitor all the
* user/threads alive state.
*
* Otherwise, the system is reset.
*/
void is_alive();
void process(uint32_t elapsed_ms);
protected :
/** add_to_list is used to store the registered user into List.
@ -103,11 +100,11 @@ protected :
void remove_from_list();
private:
uint32_t _max_timeout; //_max_timeout initialized via constructor while creating instance of this class
const char *name;//To store the details of user
const char *_name;//To store the details of user
uint32_t _current_count;//this parameter is used to reset everytime threads/user calls kick
bool _is_initialized;//To control start and stop functionality
static Watchdog *first;//List to store the user/threads who called start
Watchdog *next;
static Watchdog *_first;//List to store the user/threads who called start
Watchdog *_next;
};
} // namespace mbed

View File

@ -21,10 +21,15 @@
static bool is_watchdog_started = false; //boolean to control watchdog start and stop
#define MS_TO_US(x) ((x) * 1000) //macro to convert millisecond to microsecond
static uint32_t elapsed_ms = (HW_WATCHDOG_TIMEOUT / 2);
MBED_STATIC_ASSERT((HW_WATCHDOG_TIMEOUT > 0), "Timeout must be greater than zero");
MBED_STATIC_ASSERT((HW_WATCHDOG_TIMEOUT >= 0),"Timeout must be greater than zero");
Watchdog watchdog(0,"Platform Watchdog");
/** mbed watchdog manager creates watchdog class instance with zero timeout
* as mbed watchdog manager is not going to register(not going to call "start" method of Watchdog class)
* its only going to call process to verify all registered users/threads in alive state
*
*/
Watchdog watchdog(0, "Platform Watchdog");
/** Create singleton instance of LowPowerTicker for watchdog periodic call back of kick.
*/
@ -45,7 +50,7 @@ static void mbed_wdog_manager_kick()
{
core_util_critical_section_enter();
hal_watchdog_kick();
watchdog.is_alive();
watchdog.process(((elapsed_ms <= 0) ? 1 : elapsed_ms));
core_util_critical_section_exit();
}
@ -57,23 +62,22 @@ uint32_t mbed_wdog_manager_get_max_timeout()
bool mbed_wdog_manager_start()
{
watchdog_status_t sts;
bool msts = true;
MBED_ASSERT(HW_WATCHDOG_TIMEOUT < mbed_wdog_manager_get_max_timeout());
core_util_critical_section_enter();
if(is_watchdog_started) {
core_util_critical_section_exit();
if (is_watchdog_started) {
core_util_critical_section_exit();
return false;
}
watchdog_config_t config;
config.timeout_ms = HW_WATCHDOG_TIMEOUT;
sts = hal_watchdog_init(&config);
if(sts != WATCHDOG_STATUS_OK) {
if (sts != WATCHDOG_STATUS_OK) {
msts = false;
} else {
us_timestamp_t timeout = (MS_TO_US(HW_WATCHDOG_TIMEOUT)/2);
get_ticker()->attach_us(callback(&mbed_wdog_manager_kick),timeout);
us_timestamp_t timeout = (MS_TO_US(((elapsed_ms <= 0) ? 1 : elapsed_ms)));
get_ticker()->attach_us(callback(&mbed_wdog_manager_kick), timeout);
is_watchdog_started = true;
}
core_util_critical_section_exit();
@ -85,9 +89,9 @@ bool mbed_wdog_manager_stop()
watchdog_status_t sts;
bool msts = true;
core_util_critical_section_enter();
if(is_watchdog_started) {
if (is_watchdog_started) {
sts = hal_watchdog_stop();
if(sts != WATCHDOG_STATUS_OK) {
if (sts != WATCHDOG_STATUS_OK) {
msts = false;
} else {
get_ticker()->detach();
@ -95,14 +99,13 @@ bool mbed_wdog_manager_stop()
}
} else {
msts = false;
msts = false;
}
core_util_critical_section_exit();
return msts;
}
uint32_t mbed_wdog_manager_get_reload_value()
uint32_t mbed_wdog_manager_get_timeout()
{
return hal_watchdog_get_reload_value();
}

View File

@ -31,7 +31,7 @@
extern "C" {
#endif
/** \addtogroup drivers */
/** \addtogroup platform */
/** A system timer that will reset the system in the case of system failures or
* malfunctions.
*
@ -45,7 +45,7 @@ extern "C" {
*
* }
* @endcode
* @ingroup drivers
* @ingroup platform
*/
/** Start an independent watchdog timer
@ -63,9 +63,8 @@ bool mbed_wdog_manager_start();
* watchdog timers if supported by the current platform.
*
* @return Returns true if the watchdog timer was succesfully
* stopped, or if the timer was never started. Returns
* false if the watchdog cannot be disabled
* on the current platform.
* stopped, Returns false if the watchdog cannot be disabled
* on the current platform or if the timer was never started.
*/
bool mbed_wdog_manager_stop();
@ -76,7 +75,7 @@ bool mbed_wdog_manager_stop();
*
* @return Reload value for the watchdog timer in milliseconds.
*/
uint32_t mbed_wdog_manager_get_reload_value();
uint32_t mbed_wdog_manager_get_timeout();
/** Get the maximum refresh value for the current platform in milliseconds