diff --git a/CMakeLists.txt b/CMakeLists.txt index 38d98113..cc1ccd6b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,8 +46,6 @@ if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) endif() #添加编译目录 -# add_subdirectory(application) -# add_subdirectory(component) add_subdirectory(hal) add_subdirectory(utils) add_subdirectory(test) diff --git a/hal/CMakeLists.txt b/hal/CMakeLists.txt index 21378513..afccbeaf 100644 --- a/hal/CMakeLists.txt +++ b/hal/CMakeLists.txt @@ -26,4 +26,4 @@ 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 +target_link_libraries(${IMPL_TARGET} ${ABSTRACT_TARGET} Log) \ No newline at end of file diff --git a/hal/abstract/i_hal.c b/hal/abstract/IHal.cpp similarity index 70% rename from hal/abstract/i_hal.c rename to hal/abstract/IHal.cpp index 7143dc04..a1c82994 100644 --- a/hal/abstract/i_hal.c +++ b/hal/abstract/IHal.cpp @@ -1,16 +1,19 @@ -#include "i_hal.h" +#include "IHal.h" +#include "IHalCpp.h" #include #include static RETURN_CODE_C hal_init(IHal *object) { - return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); + // return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); + return IHalCpp::GetInstance()->Init(); } static void hal_free(IHal *object) { } static RETURN_CODE_C hal_un_init(IHal *object) { - return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); + // return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); + return IHalCpp::GetInstance()->UnInit(); } static IHal default_hal = { .init = hal_init, diff --git a/hal/abstract/IHalCpp.cpp b/hal/abstract/IHalCpp.cpp new file mode 100644 index 00000000..9cda7cfb --- /dev/null +++ b/hal/abstract/IHalCpp.cpp @@ -0,0 +1,33 @@ +#include "IHalCpp.h" +#include "Log.h" +#include +std::shared_ptr &IHalCpp::GetInstance(std::shared_ptr *impl) +{ + static std::shared_ptr instance = std::make_shared(); + static bool instanceChanging = false; + if (impl && false == instanceChanging) + { + // Don't use std::mutex for runing faster. + // Sleep for difference thread to release instance. + instanceChanging = true; + // std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Don't sleep and make sure that start fast. + if (instance.use_count() == 1) // bug? + { + LogInfo("Instance change succeed.\n"); + // instance->UnInit(); + // (*impl)->Init(); + instance = *impl; + } + else + { + LogError("[ error ] instance change failed, using by some one.\n"); + } + instanceChanging = false; + } + if (instanceChanging) + { + static std::shared_ptr tmporaryInstance = std::make_shared(); + return tmporaryInstance; + } + return instance; +} \ No newline at end of file diff --git a/hal/include/i_hal.h b/hal/include/IHal.h similarity index 100% rename from hal/include/i_hal.h rename to hal/include/IHal.h diff --git a/hal/include/IHalCpp.h b/hal/include/IHalCpp.h new file mode 100644 index 00000000..eec5f819 --- /dev/null +++ b/hal/include/IHalCpp.h @@ -0,0 +1,14 @@ +#ifndef IHAL_CPP_H +#define IHAL_CPP_H +#include "ReturnCode.h" +#include +class IHalCpp +{ +public: + IHalCpp() = default; + virtual ~IHalCpp() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual RETURN_CODE_C Init(void) { return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); } + virtual RETURN_CODE_C UnInit(void) { return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); } +}; +#endif \ No newline at end of file diff --git a/hal/src/hal.c b/hal/src/Hal.c similarity index 67% rename from hal/src/hal.c rename to hal/src/Hal.c index b8087688..29c5b885 100644 --- a/hal/src/hal.c +++ b/hal/src/Hal.c @@ -1,12 +1,12 @@ -#include "hal.h" +#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 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"); @@ -15,17 +15,17 @@ static void hal_free(IHal *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 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)->init = hal_init; ((IHal *)hal)->free = hal_free; - ((IHal *)hal)->un_init = hal_un_init; + // ((IHal *)hal)->un_init = hal_un_init; } RETURN_CODE_C new_hal(Hal **hal) { diff --git a/hal/src/hal.h b/hal/src/Hal.h similarity index 86% rename from hal/src/hal.h rename to hal/src/Hal.h index c14a3bca..323a3a88 100644 --- a/hal/src/hal.h +++ b/hal/src/Hal.h @@ -1,7 +1,7 @@ #ifndef HAL_H #define HAL_H #include "ReturnCode.h" -#include "i_hal.h" +#include "IHal.h" /* ** Make sure we can call this stuff from C++. */ diff --git a/hal/src/HalCpp.cpp b/hal/src/HalCpp.cpp new file mode 100644 index 00000000..e69de29b diff --git a/hal/src/HalCpp.h b/hal/src/HalCpp.h new file mode 100644 index 00000000..c81575e4 --- /dev/null +++ b/hal/src/HalCpp.h @@ -0,0 +1,10 @@ +#ifndef HALCPP_H +#define HALCPP_H +#include "IHalCpp.h" +class HalCpp : public IHalCpp +{ +public: + HalCpp() = default; + virtual ~HalCpp() = default; +}; +#endif \ No newline at end of file diff --git a/hal/src/HalMakePtr.cpp b/hal/src/HalMakePtr.cpp new file mode 100644 index 00000000..c93255e4 --- /dev/null +++ b/hal/src/HalMakePtr.cpp @@ -0,0 +1,35 @@ +#include "HalMakePtr.h" +#include "Log.h" +#include "Hal.h" +#include "HalCpp.h" +RETURN_CODE_C create_hal_module(void) +{ + IHal *hal = NULL; + RETURN_CODE_C code = HalMakePtr::GetInstance()->CreateHalPtr(&hal); + if (C_RETURN_CODE_OK == code.mCode) + { + LogInfo("Create Hal instance ok.\n"); + reset_hal_impl((IHal *)hal); + return code; + } + return CreateReturnCode(C_RETURN_CODE_NOT_OK); +} +std::shared_ptr &HalMakePtr::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) + { + instance = *impl; + } + return instance; +} +RETURN_CODE_C HalMakePtr::CreateHalPtr(IHal **hal) +{ + return new_hal((Hal **)hal); +} +RETURN_CODE_C HalMakePtr::CreateHalSharePtr(std::shared_ptr &impl) +{ + LogInfo("HalMakePtr make ptr.\n"); + impl = std::make_shared(); + return CreateReturnCode(C_RETURN_CODE_NOT_OK); +} \ No newline at end of file diff --git a/hal/src/HalMakePtr.h b/hal/src/HalMakePtr.h new file mode 100644 index 00000000..bced40c8 --- /dev/null +++ b/hal/src/HalMakePtr.h @@ -0,0 +1,18 @@ +#ifndef HALMAKEPTR_H +#define HALMAKEPTR_H +#include "IHal.h" +#include "IHalCpp.h" +#include "ReturnCode.h" +#include +class HalMakePtr +{ +public: + HalMakePtr() = default; + virtual ~HalMakePtr() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual RETURN_CODE_C Init() { return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); } + virtual RETURN_CODE_C UnInit() { return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); } + virtual RETURN_CODE_C CreateHalPtr(IHal **hal); + virtual RETURN_CODE_C CreateHalSharePtr(std::shared_ptr &impl); +}; +#endif \ No newline at end of file diff --git a/hal/src/hal_make_ptr.c b/hal/src/hal_make_ptr.c deleted file mode 100644 index 148a0551..00000000 --- a/hal/src/hal_make_ptr.c +++ /dev/null @@ -1,50 +0,0 @@ -#include "hal_make_ptr.h" -#include "hal.h" -#include "Log.h" -#include -#include -#include -RETURN_CODE_C create_hal_module(void) -{ - Hal *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((IHal *)hal); - return code; - } - return CreateReturnCode(C_RETURN_CODE_NOT_OK); -} -RETURN_CODE_C destroy_hal_module(void) -{ - reset_hal_impl(NULL); - return CreateReturnCode(C_RETURN_CODE_OK); -} -static RETURN_CODE_C create_hal_instance_ptr(HalMakePtr *object, Hal **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 deleted file mode 100644 index 5d0fe5f3..00000000 --- a/hal/src/hal_make_ptr.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef HAL_MAKE_PTR_H -#define HAL_MAKE_PTR_H -#include "ReturnCode.h" -#include "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 *, Hal **); - 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(Hal **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