From 82816c4ead8227f4228dda9473f32d7d6be64575 Mon Sep 17 00:00:00 2001 From: xiaojiazhu <258828110.@qq.com> Date: Sun, 13 Aug 2023 01:21:05 -0700 Subject: [PATCH] Add hal moduel. --- CMakeLists.txt | 2 +- doc/design.md | 10 ++++- hal/CMakeLists.txt | 29 +++++++++++++++ hal/abstract/i_hal.c | 37 +++++++++++++++++++ hal/include/i_hal.h | 37 +++++++++++++++++++ hal/src/hal.c | 53 +++++++++++++++++++++++++++ hal/src/hal.h | 21 +++++++++++ hal/src/hal_make_ptr.c | 45 +++++++++++++++++++++++ hal/src/hal_make_ptr.h | 39 ++++++++++++++++++++ test/CMakeLists.txt | 2 +- test/hal/CMakeLists.txt | 32 ++++++++++++++++ test/hal/mainTest.cpp | 9 +++++ test/hal/src/i_hal_Test.cpp | 18 +++++++++ utils/ReturnCode/include/ReturnCode.h | 34 +++++++++-------- 14 files changed, 350 insertions(+), 18 deletions(-) create mode 100644 hal/CMakeLists.txt create mode 100644 hal/abstract/i_hal.c create mode 100644 hal/include/i_hal.h create mode 100644 hal/src/hal.c create mode 100644 hal/src/hal.h create mode 100644 hal/src/hal_make_ptr.c create mode 100644 hal/src/hal_make_ptr.h create mode 100644 test/hal/CMakeLists.txt create mode 100644 test/hal/mainTest.cpp create mode 100644 test/hal/src/i_hal_Test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8fc564b..38d9811 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,7 @@ endif() #添加编译目录 # add_subdirectory(application) # add_subdirectory(component) -# add_subdirectory(hal) +add_subdirectory(hal) add_subdirectory(utils) add_subdirectory(test) diff --git a/doc/design.md b/doc/design.md index 5cb1c74..8e777ee 100644 --- a/doc/design.md +++ b/doc/design.md @@ -254,7 +254,15 @@ ipc_config -->> -User:return ###### 1.4.2.2.7.1. 状态机管理设计模式    使用多态单例设计模式,暂定使用鸿蒙状态机开源代码改造实现,后续可替换其它源码或者自研代码。 -#### 1.4.2.3. 适配层(hal) +#### 1.4.2.3. 硬件适配层(hal) + +   负责适配不同的硬件平台。 + +##### 1.4.2.3.1. 硬件适配层设计模式 + +   基于C语言接口的多态单例模式,编译时静态多态链接对应的芯片平台适配代码,实现芯片接口的标准功能定义。 + +使用C语言开发时如何解决智能指针问题? #### 1.4.2.4. 工具库(utils) diff --git a/hal/CMakeLists.txt b/hal/CMakeLists.txt new file mode 100644 index 0000000..2137851 --- /dev/null +++ b/hal/CMakeLists.txt @@ -0,0 +1,29 @@ + +include(${CMAKE_SOURCE_DIR}/build/global_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) +set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) + +include_directories( + ./abstract + ./src + ./include + ${UTILS_SOURCE_PATH}/ReturnCode/include + ${UTILS_SOURCE_PATH}/Log/include +) +#do not rely on any other library +# link_directories( +# ${EXTERNAL_SOURCE_PATH}/curl/curl-8.1.2/build/lib +# ) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +aux_source_directory(./abstract ABSTRACT_SRC_FILES) +aux_source_directory(./src IMPL_SRC_FILES) + +set(ABSTRACT_TARGET HalAbstract) +set(IMPL_TARGET Hal) +add_library(${ABSTRACT_TARGET} STATIC ${ABSTRACT_SRC_FILES}) +target_link_libraries(${ABSTRACT_TARGET} ReturnCode Log) +add_library(${IMPL_TARGET} STATIC ${IMPL_SRC_FILES}) +target_link_libraries(${IMPL_TARGET} HalAbstract Log) \ No newline at end of file diff --git a/hal/abstract/i_hal.c b/hal/abstract/i_hal.c new file mode 100644 index 0000000..508ef2d --- /dev/null +++ b/hal/abstract/i_hal.c @@ -0,0 +1,37 @@ +#include "i_hal.h" +#include +#include +static RETURN_CODE_C hal_init(IHal *object) +{ + return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); +} +static void hal_free(IHal *object) +{ +} +static RETURN_CODE_C hal_un_init(IHal *object) +{ + return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); +} +static IHal default_hal = { + .init = hal_init, + .free = hal_free, + .un_init = hal_un_init, +}; +static IHal *hal_instance = &default_hal; +IHal *get_hal_instance(void) +{ + return hal_instance; +} +RETURN_CODE_C new_i_hal(IHal **object) +{ + if (!object || !(*object)) + { + return CreateReturnCode(C_RETURN_CODE_NOT_OK); + } + memcpy(*object, &default_hal, sizeof(IHal)); + return CreateReturnCode(C_RETURN_CODE_OK); +} +void reset_hal_impl(IHal *impl) +{ + hal_instance = impl; +} \ No newline at end of file diff --git a/hal/include/i_hal.h b/hal/include/i_hal.h new file mode 100644 index 0000000..1017c4b --- /dev/null +++ b/hal/include/i_hal.h @@ -0,0 +1,37 @@ +#ifndef I_HAL_H +#define I_HAL_H +#include "ReturnCode.h" +/* +** Make sure we can call this stuff from C++. +*/ +#ifdef __cplusplus +extern "C" +{ +#endif + typedef struct i_hal IHal; + typedef struct i_hal + { + RETURN_CODE_C (*init)(IHal *); + void (*free)(IHal *); + RETURN_CODE_C (*un_init)(IHal *); + } IHal; + IHal *get_hal_instance(void); + RETURN_CODE_C new_i_hal(IHal **object); + void reset_hal_impl(IHal *impl); + static inline RETURN_CODE_C i_hal_init(void) + { + return get_hal_instance()->init(get_hal_instance()); + } + static inline void i_hal_free(void) + { + return get_hal_instance()->free(get_hal_instance()); + } + static inline RETURN_CODE_C i_hal_un_init(void) + { + return get_hal_instance()->un_init(get_hal_instance()); + } + RETURN_CODE_C create_hal_module(void); +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/hal/src/hal.c b/hal/src/hal.c new file mode 100644 index 0000000..5959e0b --- /dev/null +++ b/hal/src/hal.c @@ -0,0 +1,53 @@ +#include "hal.h" +#include "Log.h" +#include +#include +static RETURN_CODE_C hal_init(IHal *object) +{ + LogInfo("hal instance init.\n"); + return CreateReturnCode(C_RETURN_CODE_OK); +} +static void hal_free(IHal *object) +{ + LogInfo("hal instance free.\n"); + if (object) + { + free(object); + } +} +static RETURN_CODE_C hal_un_init(IHal *object) +{ + LogInfo("hal instance un_init.\n"); + return CreateReturnCode(C_RETURN_CODE_OK); +} +static void hal_init_impl(Hal *hal) +{ + LogInfo("hal_init_impl\n"); + ((IHal *)hal)->init = hal_init; + ((IHal *)hal)->free = hal_free; + ((IHal *)hal)->un_init = hal_un_init; +} +RETURN_CODE_C new_hal(Hal **hal) +{ + if (!hal) + { + LogError("C_RETURN_CODE_INVALID_PARAMENTER\n"); + return CreateReturnCode(C_RETURN_CODE_INVALID_PARAMENTER); + } + if (!(*hal)) + { + *hal = (Hal *)malloc(sizeof(Hal)); + if (*hal) + { + LogInfo("new_hal succeed.\n"); + new_i_hal((IHal **)hal); + hal_init_impl(*hal); + return CreateReturnCode(C_RETURN_CODE_OK); + } + hal_init_impl(*hal); + LogError("new_hal failed.\n"); + return CreateReturnCode(C_RETURN_CODE_NOT_OK); + } + hal_init_impl(*hal); + return CreateReturnCode(C_RETURN_CODE_OK); +} \ No newline at end of file diff --git a/hal/src/hal.h b/hal/src/hal.h new file mode 100644 index 0000000..c14a3bc --- /dev/null +++ b/hal/src/hal.h @@ -0,0 +1,21 @@ +#ifndef HAL_H +#define HAL_H +#include "ReturnCode.h" +#include "i_hal.h" +/* +** Make sure we can call this stuff from C++. +*/ +#ifdef __cplusplus +extern "C" +{ +#endif + typedef struct hal Hal; + typedef struct hal + { + IHal base; + } Hal; + RETURN_CODE_C new_hal(Hal **hal); +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/hal/src/hal_make_ptr.c b/hal/src/hal_make_ptr.c new file mode 100644 index 0000000..3985da6 --- /dev/null +++ b/hal/src/hal_make_ptr.c @@ -0,0 +1,45 @@ +#include "hal_make_ptr.h" +#include "hal.h" +#include "Log.h" +#include +#include +#include +RETURN_CODE_C create_hal_module(void) +{ + IHal *hal = NULL; + RETURN_CODE_C code = create_hal_instance(&hal); + if (C_RETURN_CODE_OK == code.mCode) + { + LogInfo("Create hal instance ok.\n"); + reset_hal_impl(hal); + return code; + } + return CreateReturnCode(C_RETURN_CODE_NOT_OK); +} +static RETURN_CODE_C create_hal_instance_ptr(HalMakePtr *object, IHal **hal) +{ + return new_hal(hal); +} +static void hal_make_ptr_free(HalMakePtr *object) +{ +} +static HalMakePtr default_hal_make_ptr = { + .init = NULL, + .create_hal_instance = create_hal_instance_ptr, + .free = hal_make_ptr_free, + .un_init = NULL, +}; +static HalMakePtr *hal_make_ptr_instance = &default_hal_make_ptr; +HalMakePtr *get_hal_make_ptr_instance(void) +{ + return hal_make_ptr_instance; +} +void hal_make_ptr_object_init(HalMakePtr *object) +{ + memcpy(object, &default_hal_make_ptr, sizeof(HalMakePtr)); +} +void reset_hal_make_ptr_impl(HalMakePtr *impl) +{ + hal_make_ptr_instance->free(hal_make_ptr_instance); + hal_make_ptr_instance = impl; +} \ No newline at end of file diff --git a/hal/src/hal_make_ptr.h b/hal/src/hal_make_ptr.h new file mode 100644 index 0000000..c6fb990 --- /dev/null +++ b/hal/src/hal_make_ptr.h @@ -0,0 +1,39 @@ +#ifndef HAL_MAKE_PTR_H +#define HAL_MAKE_PTR_H +#include "ReturnCode.h" +#include "i_hal.h" +/* +** Make sure we can call this stuff from C++. +*/ +#ifdef __cplusplus +extern "C" +{ +#endif + typedef struct hal_make_ptr HalMakePtr; + typedef struct hal_make_ptr + { + RETURN_CODE_C (*init)(HalMakePtr *); + RETURN_CODE_C (*create_hal_instance)(HalMakePtr *, IHal **); + void (*free)(HalMakePtr *); + RETURN_CODE_C (*un_init)(HalMakePtr *); + } HalMakePtr; + HalMakePtr *get_hal_make_ptr_instance(void); + void hal_make_ptr_object_init(HalMakePtr *object); + void reset_hal_make_ptr_impl(HalMakePtr *impl); + static inline RETURN_CODE_C hal_make_ptr_init(void) + { + return get_hal_make_ptr_instance()->init(get_hal_make_ptr_instance()); + } + static inline RETURN_CODE_C hal_make_ptr_un_init(void) + { + return get_hal_make_ptr_instance()->un_init(get_hal_make_ptr_instance()); + } + static inline RETURN_CODE_C create_hal_instance(IHal **hal) + { + return get_hal_make_ptr_instance()->create_hal_instance(get_hal_make_ptr_instance(), hal); + } +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a040023..4525b3c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -9,5 +9,5 @@ execute_process(COMMAND sh build_gtest.sh ${TARGET_PLATFORM} ${CMAKE_SOURCE_DIR} # add_subdirectory(application) # add_subdirectory(component) add_subdirectory(utils) -# add_subdirectory(hal) +add_subdirectory(hal) diff --git a/test/hal/CMakeLists.txt b/test/hal/CMakeLists.txt new file mode 100644 index 0000000..3baa2f1 --- /dev/null +++ b/test/hal/CMakeLists.txt @@ -0,0 +1,32 @@ +# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake) +set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin) + +include_directories( + ${UTILS_SOURCE_PATH}/ReturnCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${HAL_SOURCE_PATH}/include + ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include + ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include +) + +link_directories( + ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib + ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib + ${LIBS_OUTPUT_PATH} +) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +aux_source_directory(./ SRC_FILES) +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME IHalTest) +add_executable(${TARGET_NAME} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} gtest gmock pthread Hal) +if(${COVERAGE_ON} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() \ No newline at end of file diff --git a/test/hal/mainTest.cpp b/test/hal/mainTest.cpp new file mode 100644 index 0000000..fb1d91b --- /dev/null +++ b/test/hal/mainTest.cpp @@ -0,0 +1,9 @@ +#include +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/hal/src/i_hal_Test.cpp b/test/hal/src/i_hal_Test.cpp new file mode 100644 index 0000000..1cd016c --- /dev/null +++ b/test/hal/src/i_hal_Test.cpp @@ -0,0 +1,18 @@ + +#include "Log.h" +#include "i_hal.h" +#include +#include +namespace IHalTest +{ + // ../out/test/bin/IHalTest --gtest_filter=IHalTest.Demo + TEST(IHalTest, Demo) + { + InitLog(LOG_EASYLOGGING, nullptr); + // create_hal_module(); + i_hal_init(); + i_hal_un_init(); + i_hal_free(); + UnInitLog(); + } +} \ No newline at end of file diff --git a/utils/ReturnCode/include/ReturnCode.h b/utils/ReturnCode/include/ReturnCode.h index a75e919..7f54183 100644 --- a/utils/ReturnCode/include/ReturnCode.h +++ b/utils/ReturnCode/include/ReturnCode.h @@ -1,19 +1,5 @@ #ifndef RETURN_CODE_H #define RETURN_CODE_H -enum C_RETURN_CODE -{ - C_RETURN_CODE_OK = 0, - C_RETURN_CODE_NOT_OK, - C_RETURN_CODE_INVALID_PARAMENTER, - C_RETURN_CODE_END -}; -typedef struct returnCodeC RETURN_CODE_C; -typedef struct returnCodeC -{ - const char *(*printStringCode)(const RETURN_CODE_C); - const long int mCode; -} RETURN_CODE_C; - /* ** Make sure we can call this stuff from C++. */ @@ -21,7 +7,25 @@ typedef struct returnCodeC extern "C" { #endif -const RETURN_CODE_C CreateReturnCode(const long int code); + enum C_RETURN_CODE + { + C_RETURN_CODE_OK = 0, + C_RETURN_CODE_NOT_OK, + C_RETURN_CODE_VIRTUAL_FUNCTION, + C_RETURN_CODE_INVALID_PARAMENTER, + C_RETURN_CODE_END + }; + typedef struct returnCodeC RETURN_CODE_C; + typedef struct returnCodeC + { + const char *(*printStringCode)(const RETURN_CODE_C); + const long int mCode; + } RETURN_CODE_C; + const RETURN_CODE_C CreateReturnCode(const long int code); + static inline const char *GetStringCode(const RETURN_CODE_C code) + { + return 0; + } #ifdef __cplusplus } #endif