Skip to content

Commit f30a680

Browse files
committed
[mex-hal][src/include/tests]extended hal, added mock and more UT...
Signed-off-by: mendax3301 <adrian.goessl@outlook.com>
1 parent 6ebdda7 commit f30a680

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3070
-343
lines changed

CMakeLists.txt

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ find_package(Threads REQUIRED)
3232
# Realtime requirements
3333
if(BUILD_RT)
3434
find_library(RT_LIBRARY rt)
35+
if(NOT RT_LIBRARY)
36+
message(FATAL_ERROR "librt not found. Install libc6-dev or disable BUILD_RT.")
37+
endif()
3538
find_package(PkgConfig REQUIRED)
3639
pkg_check_modules(LIBXML2 REQUIRED libxml-2.0)
3740
endif()
@@ -160,6 +163,22 @@ target_include_directories(hal PUBLIC
160163
$<INSTALL_INTERFACE:include>
161164
)
162165

166+
# Mock library for testing
167+
add_library(hal-mock
168+
src/mock/gpio_mock.cpp
169+
src/mock/spi_mock.cpp
170+
src/mock/i2c_mock.cpp
171+
src/mock/uart_mock.cpp
172+
src/mock/pwm_mock.cpp
173+
src/mock/adc_mock.cpp
174+
src/mock/timer_mock.cpp
175+
)
176+
target_include_directories(hal-mock PUBLIC
177+
${CMAKE_CURRENT_SOURCE_DIR}/include
178+
${CMAKE_CURRENT_SOURCE_DIR}/src
179+
)
180+
target_link_libraries(hal-mock PUBLIC Threads::Threads)
181+
163182
add_executable(hal_main src/main.cpp src/cli_utils.cpp)
164183
target_link_libraries(hal_main PRIVATE hal)
165184

@@ -183,6 +202,42 @@ else()
183202
endif()
184203

185204
# Installation
186-
install(TARGETS hal EXPORT hal-config DESTINATION lib)
205+
install(TARGETS hal EXPORT mex-hal-targets
206+
LIBRARY DESTINATION lib
207+
ARCHIVE DESTINATION lib
208+
RUNTIME DESTINATION bin
209+
)
187210
install(DIRECTORY include/hal DESTINATION include)
188-
install(EXPORT hal-config DESTINATION share/hal/cmake)
211+
install(EXPORT mex-hal-targets
212+
FILE mex-halTargets.cmake
213+
NAMESPACE mex_hal::
214+
DESTINATION lib/cmake/mex-hal
215+
)
216+
217+
# Generate CMake package config
218+
include(CMakePackageConfigHelpers)
219+
configure_package_config_file(
220+
${CMAKE_CURRENT_SOURCE_DIR}/cmake/mex-halConfig.cmake.in
221+
${CMAKE_CURRENT_BINARY_DIR}/mex-halConfig.cmake
222+
INSTALL_DESTINATION lib/cmake/mex-hal
223+
)
224+
write_basic_package_version_file(
225+
${CMAKE_CURRENT_BINARY_DIR}/mex-halConfigVersion.cmake
226+
VERSION ${HAL_VERSION_MAJOR}.${HAL_VERSION_MINOR}.${HAL_VERSION_PATCH}
227+
COMPATIBILITY SameMajorVersion
228+
)
229+
install(FILES
230+
${CMAKE_CURRENT_BINARY_DIR}/mex-halConfig.cmake
231+
${CMAKE_CURRENT_BINARY_DIR}/mex-halConfigVersion.cmake
232+
DESTINATION lib/cmake/mex-hal
233+
)
234+
235+
# Generate pkg-config file
236+
configure_file(
237+
${CMAKE_CURRENT_SOURCE_DIR}/cmake/mex-hal.pc.in
238+
${CMAKE_CURRENT_BINARY_DIR}/mex-hal.pc
239+
@ONLY
240+
)
241+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/mex-hal.pc
242+
DESTINATION lib/pkgconfig
243+
)

cmake/mex-hal.pc.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
prefix=@CMAKE_INSTALL_PREFIX@
2+
libdir=${prefix}/lib
3+
includedir=${prefix}/include
4+
5+
Name: mex-hal
6+
Description: Hardware Abstraction Layer for Embedded Linux
7+
Version: @HAL_VERSION_MAJOR@.@HAL_VERSION_MINOR@.@HAL_VERSION_PATCH@
8+
Libs: -L${libdir} -lhal -lpthread -lrt
9+
Cflags: -I${includedir}

cmake/mex-halConfig.cmake.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/mex-halTargets.cmake")
4+
check_required_components(mex-hal)

include/hal/cli_utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#define MEX_HAL_CLI_UTILS_H
33

44
#include "../include/hal/core.h"
5-
#include "../src/device_config/device_config.h"
6-
#include "../src/sys_config/sys_config.h"
5+
#include "../../src/device_config/device_config.h"
6+
#include "../../src/sys_config/sys_config.h"
77
#include <memory>
88
#include <string>
99

include/hal/device_tree.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#ifndef MEX_HAL_DEVICE_TREE_H
2+
#define MEX_HAL_DEVICE_TREE_H
3+
4+
#include <cstdint>
5+
#include <string>
6+
#include <vector>
7+
#include <optional>
8+
9+
/// @brief mex_hal Hardware Abstraction Layer - Device Tree Representation \namespace mex_hal
10+
namespace mex_hal
11+
{
12+
/// @brief Device Tree Property \struct DTProperty
13+
struct DTProperty
14+
{
15+
std::string name;
16+
std::vector<uint8_t> value;
17+
};
18+
19+
/// @brief Device Tree Node \struct DTNode
20+
struct DTNode
21+
{
22+
std::string name;
23+
std::string path;
24+
std::vector<DTProperty> properties;
25+
std::vector<DTNode> children;
26+
};
27+
28+
/// @brief Device Tree Interface \class DeviceTreeInterface
29+
class DeviceTreeInterface
30+
{
31+
public:
32+
/**
33+
* @brief Virtual descturto
34+
*/
35+
virtual ~DeviceTreeInterface() = default;
36+
37+
/**
38+
* @brief Load the device tree from a specified path
39+
* @param overlayPath The file path to the device tree overlay (e.g., .dtbo file)
40+
* @return A boolean indicating success or failure of loading the device tree
41+
*/
42+
virtual bool loadOverlay(const std::string& overlayPath) = 0;
43+
44+
/**
45+
* @brief Remove a previously loaded device tree overlay by name
46+
* @param overlayName The name of the device tree overlay to remove (e.g., "my_overlay")
47+
* @return A boolean indicating success or failure of removing the device tree overlay
48+
*/
49+
virtual bool removeOverlay(const std::string& overlayName) = 0;
50+
51+
/**
52+
* @brief List the currently loaded device tree overlays
53+
* @return A vector of strings containing the names of the currently loaded device tree overlays
54+
*/
55+
[[nodiscard]] virtual std::vector<std::string> listOverlays() const = 0;
56+
57+
/**
58+
* @brief Read a property from a device tree node
59+
* @param nodePath The path to the device tree node (e.g., "/soc/i2c@40000000")
60+
* @param propertyName The name of the property to read (e.g., "compatible")
61+
* @return An optional containing the DTProperty if found, or std::nullopt if not found
62+
*/
63+
[[nodiscard]] virtual std::optional<DTProperty> readProperty(
64+
const std::string& nodePath,
65+
const std::string& propertyName) const = 0;
66+
67+
/**
68+
* @brief Check if a device tree node exists at the specified path
69+
* @param nodePath A string representing the path to the device tree node (e.g., "/soc/i2c@40000000")
70+
* @return The boolean value indicating whether the node exists (true) or not (false)
71+
*/
72+
[[nodiscard]] virtual bool nodeExists(const std::string& nodePath) const = 0;
73+
74+
/**
75+
* @brief Get the compatible string of a device tree node
76+
* @param nodePath The path to the device tree node (e.g., "/soc/i2c@40000000")
77+
* @return A string containing the compatible value if found, or an empty string if not found
78+
*/
79+
[[nodiscard]] virtual std::string getCompatible(const std::string& nodePath) const = 0;
80+
81+
/**
82+
* @brief Get the status of a device tree node
83+
* @param nodePath The path to the device tree node (e.g., "/soc/i2c@40000000")
84+
* @return A string containing the status value if found, or an empty string if not found
85+
*/
86+
[[nodiscard]] virtual std::string getNodeStatus(const std::string& nodePath) const = 0;
87+
88+
protected:
89+
inline static const std::string DT_BASE_PATH = "/proc/device-tree";
90+
inline static const std::string DT_OVERLAY_PATH = "/sys/kernel/config/device-tree/overlays";
91+
};
92+
}
93+
94+
#endif // MEX_HAL_DEVICE_TREE_H

include/hal/logger.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ namespace mex_hal
131131
* @param message Message to log
132132
*/
133133
void fatal(const std::string& message);
134+
135+
/**
136+
* @brief Set the maximum file size for log rotation
137+
*
138+
* @param maxSize Maximum file size in bytes
139+
*/
140+
void setMaxFileSize(size_t maxSize);
141+
142+
/**
143+
* @brief Set the maximum number of backup log files to keep
144+
*
145+
* @param maxFiles Maximum number of backup files
146+
*/
147+
void setMaxBackupFiles(uint8_t maxFiles);
134148

135149
private:
136150
LogLevel currentLevel_;
@@ -139,6 +153,8 @@ namespace mex_hal
139153
std::string logFilePath_;
140154
std::ofstream logFile_;
141155
std::mutex logMutex_;
156+
size_t maxFileSize_ = 0;
157+
uint8_t maxBackupFiles_ = 0;
142158

143159
/**
144160
* @brief Construct a new Logger object
@@ -151,23 +167,23 @@ namespace mex_hal
151167
*
152168
* @return std::string
153169
*/
154-
std::string getCurrentTimestamp() const;
170+
static std::string getCurrentTimestamp() ;
155171

156172
/**
157173
* @brief Convert LogLevel to string
158174
*
159175
* @param level LogLevel to convert
160176
* @return std::string
161177
*/
162-
std::string logLevelToString(LogLevel level) const;
178+
static std::string logLevelToString(LogLevel level) ;
163179

164180
/**
165181
* @brief Get the Log Color object
166182
*
167183
* @param level LogLevel
168184
* @return std::string
169185
*/
170-
std::string getLogColor(LogLevel level) const;
186+
static std::string getLogColor(LogLevel level);
171187

172188
/**
173189
* @brief Write the log message to the appropriate outputs
@@ -176,6 +192,11 @@ namespace mex_hal
176192
* @param message Message to log
177193
*/
178194
void writeLog(LogLevel level, const std::string& message);
195+
196+
/**
197+
* @brief Rotate the log file when it exceeds the maximum file size
198+
*/
199+
void rotateLogFile();
179200
};
180201

181202
/**

include/hal/power_management.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#ifndef MEX_HAL_POWER_MANAGEMENT_H
2+
#define MEX_HAL_POWER_MANAGEMENT_H
3+
4+
#include <cstdint>
5+
#include <string>
6+
#include <functional>
7+
8+
/// @brief mex_hal Hardware Abstraction Layer - Power Management \namespace mex_hal
9+
namespace mex_hal
10+
{
11+
/// @brief Power state enumeration \enum PowerState
12+
enum class PowerState
13+
{
14+
ACTIVE,
15+
IDLE,
16+
STANDBY,
17+
SUSPEND,
18+
HIBERNATE
19+
};
20+
21+
/// @brief CPU governor enumeration \enum CPUGovernor
22+
enum class CPUGovernor
23+
{
24+
PERFORMANCE,
25+
POWERSAVE,
26+
ONDEMAND,
27+
CONSERVATIVE,
28+
SCHEDUTIL
29+
};
30+
31+
/// @brief Power management interface
32+
using PowerEventCallback = std::function<void(PowerState oldState, PowerState newState)>;
33+
34+
/// @brief Power management interface \class PowerManagementInterface
35+
class PowerManagementInterface
36+
{
37+
public:
38+
/**
39+
* @brief Virtual destructor
40+
*/
41+
virtual ~PowerManagementInterface() = default;
42+
43+
/**
44+
* @brief Get the Power State.
45+
* @return A PowerState enumeration value representing the current power state of the system.
46+
*/
47+
[[nodiscard]] virtual PowerState getPowerState() const = 0;
48+
49+
/**
50+
* @brief Request a change in power state.
51+
* @param newState The desired new power state to transition to, represented as a PowerState enumeration value.
52+
* @return A boolean indicating whether the request to change the power state was successful (true) or not (false).
53+
*/
54+
virtual bool requestPowerState(PowerState newState) = 0;
55+
56+
/**
57+
* @brief Set the CPU governor.
58+
* @param governor The desired CPU governor to set, represented as a CPUGovernor enumeration value.
59+
* @return A boolean indicating whether the request to set the CPU governor was successful (true) or not (false).
60+
*/
61+
virtual bool setCPUGovernor(CPUGovernor governor) = 0;
62+
63+
/**
64+
* @brief Get the current CPU governor.
65+
* @return A CPUGovernor enumeration value representing the current CPU governor in use.
66+
*/
67+
[[nodiscard]] virtual CPUGovernor getCPUGovernor() const = 0;
68+
69+
/**
70+
* @brief Set the CPU frequency.
71+
* @param frequencyMHz The desired CPU frequency to set, in megahertz (MHz).
72+
* @return A boolean indicating whether the request to set the CPU frequency was successful (true) or not (false).
73+
*/
74+
virtual bool setCPUFrequency(uint32_t frequencyMHz) = 0;
75+
76+
/**
77+
* @brief Get the current CPU frequency.
78+
* @return A uint32_t value representing the current CPU frequency in megahertz (MHz).
79+
*/
80+
[[nodiscard]] virtual uint32_t getCPUFrequency() const = 0;
81+
82+
/**
83+
* @brief Register a callback function to be called when the power state changes.
84+
* @param callback A PowerEventCallback function that will be called with the old and new power states whenever a power state change occurs.
85+
*/
86+
virtual void onPowerStateChange(const PowerEventCallback& callback) = 0;
87+
88+
/**
89+
* @brief Get the system uptime.
90+
* @return A uint64_t value representing the system uptime in milliseconds.
91+
*/
92+
[[nodiscard]] virtual uint64_t getUptime() const = 0;
93+
};
94+
}
95+
96+
#endif // MEX_HAL_POWER_MANAGEMENT_H

include/hal/resource_manager.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ namespace mex_hal
167167
*/
168168
void printMemoryReport() const;
169169

170+
/**
171+
* @brief Add a dependency between two resources (e.g., resourceId depends on dependsOnId)
172+
* @param resourceId The resource that has a dependency
173+
* @param dependsOnId The resource that is depended on
174+
* @return A boolean indicating whether the dependency was successfully added (true) or not (false)
175+
*/
176+
bool addDependency(uint64_t resourceId, uint64_t dependsOnId);
177+
178+
/**
179+
* @brief Get dependencies for a resource
180+
* @param resourceId A resource ID to query dependencies for
181+
* @return A vector of resource IDs that the specified resource depends on, or an empty vector if there are no dependencies or if the resource ID is invalid
182+
*/
183+
std::vector<uint64_t> getDependencies(uint64_t resourceId) const;
184+
170185
// Prevent copying and assignment
171186
ResourceManager(const ResourceManager&) = delete;
172187
ResourceManager& operator=(const ResourceManager&) = delete;
@@ -180,6 +195,7 @@ namespace mex_hal
180195
mutable std::mutex resourceMutex_;
181196
std::unordered_map<uint64_t, std::unique_ptr<ResourceInfo>> resources_;
182197
std::unordered_map<void*, uint64_t> memoryAddressToId_;
198+
std::unordered_map<uint64_t, std::vector<uint64_t>> dependencies_;
183199
std::atomic<uint64_t> nextResourceId_{1};
184200
size_t totalAllocatedMemory_{0};
185201
};

0 commit comments

Comments
 (0)