Skip to content

Commit

Permalink
🔀 Merge branch 'ladislas+yann/feature/cmake-generate-os_version' into…
Browse files Browse the repository at this point in the history
… develop
  • Loading branch information
ladislas committed Oct 31, 2022
2 parents 25050e2 + f857e15 commit 9e98b42
Show file tree
Hide file tree
Showing 21 changed files with 48 additions and 71 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"cmake.generator": "Ninja",
"cmake.configureSettings": {
"TARGET_BOARD": "LEKA_V1_2_DEV",
"OS_VERSION": "0.0.0",
"ENABLE_LOG_DEBUG": "ON",
"ENABLE_SYSTEM_STATS": "ON",
"MBED_PATH": "${workspaceFolder}/extern/mbed-os",
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ config_tools_target: mkdir_tools_config
config_cmake_build: mkdir_cmake_config
@echo ""
@echo "🏃 Running cmake configuration script for target $(TARGET_BOARD) 📝"
@cmake -S . -B $(TARGET_BUILD_DIR) -GNinja -DCMAKE_CONFIG_DIR="$(CMAKE_CONFIG_DIR)" -DTARGET_BOARD="$(TARGET_BOARD)" -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DENABLE_LOG_DEBUG=$(ENABLE_LOG_DEBUG) -DENABLE_SYSTEM_STATS=$(ENABLE_SYSTEM_STATS) -DBUILD_TARGETS_TO_USE_WITH_BOOTLOADER=$(BUILD_TARGETS_TO_USE_WITH_BOOTLOADER)
@cmake -S . -B $(TARGET_BUILD_DIR) -GNinja -DCMAKE_CONFIG_DIR="$(CMAKE_CONFIG_DIR)" -DTARGET_BOARD="$(TARGET_BOARD)" -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DENABLE_LOG_DEBUG=$(ENABLE_LOG_DEBUG) -DENABLE_SYSTEM_STATS=$(ENABLE_SYSTEM_STATS) -DBUILD_TARGETS_TO_USE_WITH_BOOTLOADER=$(BUILD_TARGETS_TO_USE_WITH_BOOTLOADER) -DOS_VERSION=$(OS_VERSION)

config_tools_build: mkdir_tools_config
@echo ""
@echo "🏃 Running cmake configuration script for target $(TARGET_BOARD) 📝"
@cmake -S . -B $(CMAKE_TOOLS_BUILD_DIR) -GNinja -DCMAKE_CONFIG_DIR="$(CMAKE_TOOLS_CONFIG_DIR)" -DTARGET_BOARD="$(TARGET_BOARD)" -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DENABLE_LOG_DEBUG=ON -DENABLE_SYSTEM_STATS=ON
@cmake -S . -B $(CMAKE_TOOLS_BUILD_DIR) -GNinja -DCMAKE_CONFIG_DIR="$(CMAKE_TOOLS_CONFIG_DIR)" -DTARGET_BOARD="$(TARGET_BOARD)" -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DENABLE_LOG_DEBUG=ON -DENABLE_SYSTEM_STATS=ON -DOS_VERSION=$(OS_VERSION)

#
# MARK: - Tests targets
Expand Down Expand Up @@ -219,7 +219,7 @@ run_unit_tests:
config_unit_tests: mkdir_build_unit_tests
@echo ""
@echo "🏃 Running unit tests cmake configuration script 📝"
cmake -S ./tests/unit -B $(UNIT_TESTS_BUILD_DIR) -GNinja -DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=$(COVERAGE) -DSANITIZERS=$(SANITIZERS)
cmake -S ./tests/unit -B $(UNIT_TESTS_BUILD_DIR) -GNinja -DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=$(COVERAGE) -DSANITIZERS=$(SANITIZERS) -DOS_VERSION=$(OS_VERSION)
@mkdir -p $(CMAKE_TOOLS_BUILD_DIR)/unit_tests
@ln -sf $(UNIT_TESTS_BUILD_DIR)/compile_commands.json $(CMAKE_TOOLS_BUILD_DIR)/unit_tests/compile_commands.json

Expand Down
2 changes: 1 addition & 1 deletion app/bootloader/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace sd {
namespace factory_reset {

constexpr auto default_limit = uint8_t {10};
constexpr auto firmware_version = FirmwareVersion {.major = 1, .minor = 0, .revision = 0};
constexpr auto firmware_version = Version {.major = 1, .minor = 0, .revision = 0};

namespace internal {

Expand Down
2 changes: 1 addition & 1 deletion app/os/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ namespace firmware {
internal::qspi.setFrequency(flash::is25lp016d::max_clock_frequency_in_hz);
}

auto version() -> FirmwareVersion
auto version() -> Version
{
return kit.getCurrentVersion();
}
Expand Down
6 changes: 3 additions & 3 deletions include/interface/drivers/FirmwareUpdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include "FirmwareVersion.h"
#include "Version.h"

namespace leka::interface {

Expand All @@ -13,8 +13,8 @@ class FirmwareUpdate
public:
virtual ~FirmwareUpdate() = default;

virtual auto getCurrentVersion() -> FirmwareVersion = 0;
virtual auto loadUpdate(const FirmwareVersion &version) -> bool = 0;
virtual auto getCurrentVersion() -> Version = 0;
virtual auto loadUpdate(const Version &version) -> bool = 0;
};

} // namespace leka::interface
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace leka {

struct FirmwareVersion {
struct Version {
uint8_t major;
uint8_t minor;
uint16_t revision; // ? Use uint16_t instead of uint8_t for compatibility w/ MCUBoot Image format, see
Expand Down
4 changes: 2 additions & 2 deletions libs/BLEKit/include/BLEServiceDeviceInformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#pragma once

#include "CastUtils.h"
#include "interface/drivers/FirmwareVersion.h"
#include "interface/drivers/Version.h"
#include "internal/BLEService.h"
#include "internal/ServicesCharacteristics.h"

Expand All @@ -24,7 +24,7 @@ class BLEServiceDeviceInformation : public interface::BLEService
sendData(data);
}

void setOSVersion(const FirmwareVersion &version) const
void setOSVersion(const Version &version) const
{
_os_version.fill('\0');
auto version_cstr = version.asStdArray();
Expand Down
6 changes: 3 additions & 3 deletions libs/BLEKit/include/BLEServiceUpdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include "interface/drivers/FirmwareVersion.h"
#include "interface/drivers/Version.h"
#include "internal/BLEService.h"
#include "internal/ServicesCharacteristics.h"

Expand All @@ -15,7 +15,7 @@ class BLEServiceUpdate : public interface::BLEService
public:
BLEServiceUpdate() : interface::BLEService(service::firmware_update::uuid, _characteristic_table) {}

auto getVersion() const -> FirmwareVersion { return version; }
auto getVersion() const -> Version { return version; }

void onDataReceived(const data_received_handle_t &params) final
{
Expand Down Expand Up @@ -65,7 +65,7 @@ class BLEServiceUpdate : public interface::BLEService
service::firmware_update::characteristic::request_factory_reset, &is_factory_reset_requested};
std::function<void(bool)> _on_factory_reset_notification_callback {};

FirmwareVersion version {};
Version version {};

WriteOnlyGattCharacteristic<uint8_t> version_major_characteristic {
service::firmware_update::characteristic::version_major, &version.major};
Expand Down
2 changes: 1 addition & 1 deletion libs/BLEKit/tests/BLEServiceDeviceInformation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ TEST(BLEServiceDeviceInformationTest, setOSVersion)

service_device_information.onDataReadyToSend(spy_callback);

auto os_version = FirmwareVersion {123, 234, 45678};
auto os_version = Version {123, 234, 45678};
auto expected_os_version = os_version.asStdArray(); // "123.234.45678"
service_device_information.setOSVersion(os_version);

Expand Down
2 changes: 1 addition & 1 deletion libs/BLEKit/tests/BLEServiceUpdate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BLEServiceUpdateTest : public testing::Test
BLEServiceUpdate::data_requested_handle_t data_requested_handle {};

bool default_request_update_sent {false};
FirmwareVersion default_version {0x00, 0x00, 0x0000};
Version default_version {0x00, 0x00, 0x0000};

void onDataReceivedProcess(const uint8_t *data)
{
Expand Down
4 changes: 4 additions & 0 deletions libs/FirmwareKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

add_library(FirmwareKit STATIC)

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/include/os_version.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/include/os_version.h")

target_include_directories(FirmwareKit
PUBLIC
include
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
)

target_sources(FirmwareKit
Expand Down
10 changes: 3 additions & 7 deletions libs/FirmwareKit/include/FirmwareKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,21 @@ class FirmwareKit : public interface::FirmwareUpdate
{
public:
struct Config {
const char *os_version_path;
const char *bin_path_format;
};

static constexpr auto DEFAULT_CONFIG =
Config {.os_version_path = "fs/sys/os-version", .bin_path_format = "/fs/usr/os/LekaOS-%i.%i.%i.bin"};
static constexpr auto DEFAULT_CONFIG = Config {.bin_path_format = "/fs/usr/os/LekaOS-%i.%i.%i.bin"};

explicit FirmwareKit(interface::FlashMemory &flash, Config config) : _flash(flash), _config(config)
{
// nothing do to
}

auto getCurrentVersion() -> leka::FirmwareVersion final;
auto getCurrentVersion() -> Version final;

auto loadUpdate(const leka::FirmwareVersion &version) -> bool final;
auto loadUpdate(const Version &version) -> bool final;

private:
auto getCurrentVersionFromFile() -> leka::FirmwareVersion;

auto loadUpdate(const char *path) -> bool;

interface::FlashMemory &_flash;
Expand Down
7 changes: 7 additions & 0 deletions libs/FirmwareKit/include/os_version.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#define OS_VERSION "${OS_VERSION}"
26 changes: 5 additions & 21 deletions libs/FirmwareKit/source/FirmwareKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,18 @@

#include "FirmwareKit.h"

#include "os_version.h"
#include "semver/semver.hpp"

using namespace leka;

auto FirmwareKit::getCurrentVersion() -> FirmwareVersion
auto FirmwareKit::getCurrentVersion() -> Version
{
return getCurrentVersionFromFile();
auto semversion = semver::version {OS_VERSION};
return Version {.major = semversion.major, .minor = semversion.minor, .revision = semversion.patch};
}

auto FirmwareKit::getCurrentVersionFromFile() -> FirmwareVersion
{
auto file_content = std::array<char, 16> {};

if (auto is_not_open = !_file.open(_config.os_version_path); is_not_open) {
return FirmwareVersion {.major = 1, .minor = 0, .revision = 0};
}

_file.read(file_content);
_file.close();

std::replace(std::begin(file_content), std::end(file_content), '\n', '\0');

auto semversion = semver::version {file_content.data()};

return FirmwareVersion {.major = semversion.major, .minor = semversion.minor, .revision = semversion.patch};
}

auto FirmwareKit::loadUpdate(const FirmwareVersion &version) -> bool
auto FirmwareKit::loadUpdate(const Version &version) -> bool
{
auto path = std::array<char, 64> {};
snprintf(path.data(), std::size(path), _config.bin_path_format, version.major, version.minor, version.revision);
Expand Down
25 changes: 5 additions & 20 deletions libs/FirmwareKit/tests/FirmwareKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "FirmwareKit.h"
#include "gtest/gtest.h"
#include "mocks/leka/FlashMemory.h"
#include "os_version.h"

using namespace leka;

Expand All @@ -23,10 +24,6 @@ class FirmwareKitTest : public ::testing::Test

void SetUp() override
{
std::ofstream osv_stream {config.os_version_path, std::ios::binary};
osv_stream << current_version_str;
osv_stream.close();

std::ofstream update_stream {bin_update_path.c_str(), std::ios::binary};
for (const auto &val: bin_update_content) {
update_stream << val;
Expand All @@ -38,14 +35,13 @@ class FirmwareKitTest : public ::testing::Test
std::string bin_update_path = "/tmp/update-v2.0.0.bin";
std::array<uint8_t, 6> bin_update_content = {0x61, 0x62, 0x63, 0x64, 0x65, 0x66}; // "abcdef"

FirmwareVersion default_version = FirmwareVersion {1, 0, 0};
FirmwareVersion current_version = FirmwareVersion {1, 2, 3};
FirmwareVersion update_version = FirmwareVersion {2, 0, 0};
Version current_version = Version {1, 2, 0};
Version update_version = Version {2, 0, 0};

std::string current_version_str = "1.2.3";
std::string current_version_str = OS_VERSION;

mock::FlashMemory mock_flash {};
FirmwareKit::Config config = {.os_version_path = "/tmp/os-version", .bin_path_format = "/tmp/update-v%i.%i.%i.bin"};
FirmwareKit::Config config = {.bin_path_format = "/tmp/update-v%i.%i.%i.bin"};

FirmwareKit firmwarekit = FirmwareKit {mock_flash, config};
};
Expand Down Expand Up @@ -75,17 +71,6 @@ TEST_F(FirmwareKitTest, getCurrentVersion)
EXPECT_EQ(actual_version.revision, current_version.revision);
}

TEST_F(FirmwareKitTest, getCurrentVersionFileNotFound)
{
std::filesystem::remove(config.os_version_path);

auto actual_version = firmwarekit.getCurrentVersion();

EXPECT_EQ(actual_version.major, default_version.major);
EXPECT_EQ(actual_version.minor, default_version.minor);
EXPECT_EQ(actual_version.revision, default_version.revision);
}

TEST_F(FirmwareKitTest, loadUpdate)
{
{
Expand Down
2 changes: 1 addition & 1 deletion libs/RobotKit/include/RobotController.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class RobotController : public interface::RobotController
auto _serial_number = _serialnumberkit.getSerialNumber();
_service_device_information.setSerialNumber(_serial_number);

auto _os_version = FirmwareVersion {.major = 1, .minor = 2, .revision = 0};
auto _os_version = _firmware_update.getCurrentVersion();
_service_device_information.setOSVersion(_os_version);

auto advertising_data = _ble.getAdvertisingData();
Expand Down
2 changes: 1 addition & 1 deletion libs/RobotKit/tests/RobotController_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class RobotControllerTest : public testing::Test
EXPECT_CALL(mock_mcu, getID).Times(1);
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(1);

// EXPECT_CALL(firmware_update, getCurrentVersion).Times(1);
EXPECT_CALL(firmware_update, getCurrentVersion).Times(1);
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(1);

Sequence set_serial_number_as_ble_device_name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TEST_F(RobotControllerTest, initializeComponents)
// TODO: Specify which BLE service and what is expected if necessary
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(1);

// EXPECT_CALL(firmware_update, getCurrentVersion).Times(1);
EXPECT_CALL(firmware_update, getCurrentVersion).Times(1);
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(1);

Sequence set_serial_number_as_ble_device_name;
Expand Down
2 changes: 1 addition & 1 deletion spikes/lk_ble/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ auto main() -> int
std::array<uint8_t, 33> serial_number = {"LK-2202-003300294E5350092038384B"};
service_device_information.setSerialNumber(serial_number);

auto os_version = FirmwareVersion {123, 234, 45678};
auto os_version = Version {123, 234, 45678};
service_device_information.setOSVersion(os_version);

blekit.onConnectionCallback([] { log_info("Connected !"); });
Expand Down
2 changes: 1 addition & 1 deletion spikes/lk_update_process_app_base/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ auto main() -> int
coreqspi.setFrequency(flash::is25lp016d::max_clock_frequency_in_hz);

// Load file
auto version = FirmwareVersion {.major = 1, .minor = 2, .revision = 3};
auto version = Version {.major = 1, .minor = 2, .revision = 3};
if (auto did_load = firmwarekit.loadUpdate(version); did_load) {
log_info("New update was loaded in external flash");
}
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/mocks/mocks/leka/FirmwareUpdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace leka::mock {
class FirmwareUpdate : public interface::FirmwareUpdate
{
public:
MOCK_METHOD(FirmwareVersion, getCurrentVersion, (), (override));
MOCK_METHOD(bool, loadUpdate, (const FirmwareVersion &), (override));
MOCK_METHOD(Version, getCurrentVersion, (), (override));
MOCK_METHOD(bool, loadUpdate, (const Version &), (override));
};

} // namespace leka::mock

0 comments on commit 9e98b42

Please sign in to comment.