Add hal moduel.

This commit is contained in:
xiaojiazhu 2023-08-13 01:21:05 -07:00
parent c2830a4e87
commit 82816c4ead
14 changed files with 350 additions and 18 deletions

View File

@ -48,7 +48,7 @@ endif()
# #
# add_subdirectory(application) # add_subdirectory(application)
# add_subdirectory(component) # add_subdirectory(component)
# add_subdirectory(hal) add_subdirectory(hal)
add_subdirectory(utils) add_subdirectory(utils)
add_subdirectory(test) add_subdirectory(test)

View File

@ -254,7 +254,15 @@ ipc_config -->> -User:return
###### 1.4.2.2.7.1. 状态机管理设计模式 ###### 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 #### 1.4.2.4. 工具库utils

29
hal/CMakeLists.txt Normal file
View File

@ -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)

37
hal/abstract/i_hal.c Normal file
View File

@ -0,0 +1,37 @@
#include "i_hal.h"
#include <stddef.h>
#include <string.h>
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;
}

37
hal/include/i_hal.h Normal file
View File

@ -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

53
hal/src/hal.c Normal file
View File

@ -0,0 +1,53 @@
#include "hal.h"
#include "Log.h"
#include <stddef.h>
#include <stdlib.h>
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);
}

21
hal/src/hal.h Normal file
View File

@ -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

45
hal/src/hal_make_ptr.c Normal file
View File

@ -0,0 +1,45 @@
#include "hal_make_ptr.h"
#include "hal.h"
#include "Log.h"
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
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;
}

39
hal/src/hal_make_ptr.h Normal file
View File

@ -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

View File

@ -9,5 +9,5 @@ execute_process(COMMAND sh build_gtest.sh ${TARGET_PLATFORM} ${CMAKE_SOURCE_DIR}
# add_subdirectory(application) # add_subdirectory(application)
# add_subdirectory(component) # add_subdirectory(component)
add_subdirectory(utils) add_subdirectory(utils)
# add_subdirectory(hal) add_subdirectory(hal)

32
test/hal/CMakeLists.txt Normal file
View File

@ -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()

9
test/hal/mainTest.cpp Normal file
View File

@ -0,0 +1,9 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,18 @@
#include "Log.h"
#include "i_hal.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
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();
}
}

View File

@ -1,9 +1,17 @@
#ifndef RETURN_CODE_H #ifndef RETURN_CODE_H
#define RETURN_CODE_H #define RETURN_CODE_H
/*
** Make sure we can call this stuff from C++.
*/
#ifdef __cplusplus
extern "C"
{
#endif
enum C_RETURN_CODE enum C_RETURN_CODE
{ {
C_RETURN_CODE_OK = 0, C_RETURN_CODE_OK = 0,
C_RETURN_CODE_NOT_OK, C_RETURN_CODE_NOT_OK,
C_RETURN_CODE_VIRTUAL_FUNCTION,
C_RETURN_CODE_INVALID_PARAMENTER, C_RETURN_CODE_INVALID_PARAMENTER,
C_RETURN_CODE_END C_RETURN_CODE_END
}; };
@ -13,15 +21,11 @@ typedef struct returnCodeC
const char *(*printStringCode)(const RETURN_CODE_C); const char *(*printStringCode)(const RETURN_CODE_C);
const long int mCode; const long int mCode;
} RETURN_CODE_C; } RETURN_CODE_C;
/*
** Make sure we can call this stuff from C++.
*/
#ifdef __cplusplus
extern "C"
{
#endif
const RETURN_CODE_C CreateReturnCode(const long int code); const RETURN_CODE_C CreateReturnCode(const long int code);
static inline const char *GetStringCode(const RETURN_CODE_C code)
{
return 0;
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif