Skip to content

Commit

Permalink
🔀 Merge branch 'ladislas/feature/deep-sleep-logkit-compatibility' int…
Browse files Browse the repository at this point in the history
…o develop
  • Loading branch information
ladislas committed Nov 17, 2022
2 parents 932f111 + 8799580 commit 9e04f87
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 3 deletions.
27 changes: 26 additions & 1 deletion libs/LogKit/include/LogKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,32 @@ namespace internal {
internal::filehandle->write(data, size);
}

inline void disable_filehandle_input()

{
if (filehandle == nullptr) {
return;
}

internal::filehandle->enable_input(false);
}

inline void enable_filehandle_input()
{
if (filehandle == nullptr) {
return;
}

internal::filehandle->enable_input(true);
}

} // namespace internal

[[maybe_unused]] inline void set_filehandle_pointer(filehandle_ptr fh)
{
internal::filehandle = fh;

internal::disable_filehandle_input();
}

inline void process_fifo()
Expand Down Expand Up @@ -230,7 +251,11 @@ inline void set_print_function(...) {} // NOSONAR
inline void set_filehandle_pointer(...) {} // NOSONAR

namespace internal {
inline void default_sink_function(...) {} // NOSONAR

inline void default_sink_function(...) {} // NOSONAR
inline void disable_filehandle_input(...) {} // NOSONAR
inline void enable_filehandle_input(...) {} // NOSONAR

} // namespace internal

#endif // ENABLE_LOG_DEBUG
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ endfunction()

add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/boost_ut)

add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/deep_sleep_log_kit)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/deep_sleep_mbed_hal)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/imu)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/io_expander)
add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/file_manager)
Expand Down
5 changes: 3 additions & 2 deletions tests/functional/include/tests/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

#include "rtos/ThisThread.h"

#include "FATFileSystem.h"
#include "storage/blockdevice/COMPONENT_SD/include/SD/SDBlockDevice.h"
#include "storage/filesystem/fat/include/fat/FATFileSystem.h"

#include "LogKit.h"
#include "SDBlockDevice.h"

namespace utils {

Expand Down
97 changes: 97 additions & 0 deletions tests/functional/include/tests/utils_sleep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Leka - LekaOS
// Copyright 2017 ARM Limited
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <chrono>

#include "hal/lp_ticker_api.h"
#include "hal/ticker_api.h"
#include "hal/us_ticker_api.h"
#include "platform/mbed_power_mgmt.h"
#include "platform/mbed_wait_api.h"
#include "rtos/ThisThread.h"

// ? utilities taken from https://github.com/ARMmbed/mbed-os
// ? https://github.com/ARMmbed/mbed-os/blob/master/hal/tests/TESTS/mbed_hal/sleep/sleep_test_utils.h

using namespace std::chrono;

namespace utils::sleep {

constexpr inline auto SERIAL_FLUSH_TIME_MS = 150ms;
constexpr inline auto US_PER_S = 1'000'000;
constexpr inline auto DEEPSLEEP_MODE_DELTA_US = uint32_t {10000 + 125 + 5};

inline auto ticks_to_us(unsigned int ticks, unsigned int freq) -> unsigned int
{
return static_cast<unsigned int>(static_cast<unsigned long long>(ticks) * US_PER_S / freq);
}

inline auto us_to_ticks(unsigned int us, unsigned int freq) -> unsigned int
{
return static_cast<unsigned int>(static_cast<unsigned long long>(us) * freq / US_PER_S);
}

inline auto overflow_protect(unsigned int timestamp, unsigned int ticker_width) -> unsigned int
{
unsigned int counter_mask = ((1 << ticker_width) - 1);

return (timestamp & counter_mask);
}

inline auto compare_timestamps(unsigned int delta_ticks, unsigned int ticker_width, unsigned int expected,
unsigned int actual) -> bool
{
const unsigned int counter_mask = ((1 << ticker_width) - 1);

const unsigned int lower_bound = ((expected - delta_ticks) & counter_mask);
const unsigned int upper_bound = ((expected + delta_ticks) & counter_mask);

// NOLINTBEGIN
if (lower_bound < upper_bound) {
if (actual >= lower_bound && actual <= upper_bound) {
return true;
} else {
return false;
}
} else {
if ((actual >= lower_bound && actual <= counter_mask) || (actual >= 0 && actual <= upper_bound)) {
return true;
} else {
return false;
}
}
// NOLINTEND
}

inline void busy_wait(std::chrono::milliseconds ms)
{
const auto *info = us_ticker_get_info();
auto mask = static_cast<uint32_t>((1 << info->bits) - 1);

auto delay = ms.count() * info->frequency / 1000;
auto prev = us_ticker_read(); // NOLINT

while (delay > 0) {
auto next = us_ticker_read(); // NOLINT
delay -= (next - prev) & mask;
prev = next;
}
}

inline void us_ticker_isr(const ticker_data_t *const ticker_data)
{
us_ticker_clear_interrupt();
}

#if DEVICE_LPTICKER
inline void lp_ticker_isr(const ticker_data_t *const ticker_data)
{
lp_ticker_clear_interrupt();
}
#endif

} // namespace utils::sleep
15 changes: 15 additions & 0 deletions tests/functional/tests/deep_sleep_log_kit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Leka - LekaOS
# Copyright 2022 APF France handicap
# SPDX-License-Identifier: Apache-2.0

register_functional_test(
TARGET
functional_ut_deep_sleep_log_kit

INCLUDE_DIRECTORIES

SOURCES
suite_log_kit.cpp

LINK_LIBRARIES
)
94 changes: 94 additions & 0 deletions tests/functional/tests/deep_sleep_log_kit/suite_log_kit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include <cstddef>

#include "tests/config.h"
#include "tests/utils.h"
#include "tests/utils_sleep.h"

using namespace boost::ut;
using namespace std::chrono;
using namespace boost::ut::bdd;

// ? tests inspired from https://github.com/ARMmbed/mbed-os
// ? https://github.com/ARMmbed/mbed-os/blob/master/hal/tests/TESTS/mbed_hal/sleep/main.cpp

suite suite_log_kit = [] {
rtos::ThisThread::sleep_for(2000ms);

scenario("(default) LogKit w/ input_enable(false)") = [] {
given("I use the default logger") = [] {
log_info("using default logger");
rtos::ThisThread::sleep_for(500ms);

then("I expect deep sleep to be possible") = [] {
const ticker_data_t *lp_ticker = get_lp_ticker_data();
const unsigned int lp_ticker_freq = lp_ticker->interface->get_info()->frequency;

// ? Give time to test to finish UART transmission before entering deep sleep mode
utils::sleep::busy_wait(utils::sleep::SERIAL_FLUSH_TIME_MS);

auto can_deep_sleep = sleep_manager_can_deep_sleep();
expect(can_deep_sleep) << "deep sleep not possible";

const timestamp_t wakeup_time = lp_ticker_read() + utils::sleep::us_to_ticks(20000, lp_ticker_freq);
lp_ticker_set_interrupt(wakeup_time);

auto can_deep_sleep_test_check = sleep_manager_can_deep_sleep_test_check();
expect(can_deep_sleep_test_check);
};
};
};

scenario("LogKit w/ input_enable(true)") = [] {
given("I set input_enable(true) and use the default logger") = [] {
leka::logger::internal::enable_filehandle_input();
log_info("using default logger");
rtos::ThisThread::sleep_for(500ms);

then("I expect deep sleep to NOT be possible") = [] {
const ticker_data_t *lp_ticker = get_lp_ticker_data();
const unsigned int lp_ticker_freq = lp_ticker->interface->get_info()->frequency;

// ? Give time to test to finish UART transmission before entering deep sleep mode
utils::sleep::busy_wait(utils::sleep::SERIAL_FLUSH_TIME_MS);

auto can_deep_sleep = sleep_manager_can_deep_sleep();
expect(not can_deep_sleep) << "deep sleep STILL possible";

const timestamp_t wakeup_time = lp_ticker_read() + utils::sleep::us_to_ticks(20000, lp_ticker_freq);
lp_ticker_set_interrupt(wakeup_time);

auto can_deep_sleep_test_check = sleep_manager_can_deep_sleep_test_check();
expect(not can_deep_sleep_test_check);
};
};
};

scenario("LogKit w/ disable_filehandle_input()") = [] {
given("I set input_enable(true) and use the default logger") = [] {
leka::logger::internal::disable_filehandle_input();
log_info("using default logger");
rtos::ThisThread::sleep_for(500ms);

then("I expect deep sleep to NOT be possible") = [] {
const ticker_data_t *lp_ticker = get_lp_ticker_data();
const unsigned int lp_ticker_freq = lp_ticker->interface->get_info()->frequency;

// ? Give time to test to finish UART transmission before entering deep sleep mode
utils::sleep::busy_wait(utils::sleep::SERIAL_FLUSH_TIME_MS);

auto can_deep_sleep = sleep_manager_can_deep_sleep();
expect(can_deep_sleep) << "deep sleep not possible";

const timestamp_t wakeup_time = lp_ticker_read() + utils::sleep::us_to_ticks(20000, lp_ticker_freq);
lp_ticker_set_interrupt(wakeup_time);

auto can_deep_sleep_test_check = sleep_manager_can_deep_sleep_test_check();
expect(can_deep_sleep_test_check);
};
};
};
};
15 changes: 15 additions & 0 deletions tests/functional/tests/deep_sleep_mbed_hal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Leka - LekaOS
# Copyright 2022 APF France handicap
# SPDX-License-Identifier: Apache-2.0

register_functional_test(
TARGET
functional_ut_deep_sleep_mbed_hal

INCLUDE_DIRECTORIES

SOURCES
suite_mbed_hal.cpp

LINK_LIBRARIES
)
Loading

0 comments on commit 9e04f87

Please sign in to comment.