From e14eeb7ad8780daf78866ea0fd18ed94383a5e07 Mon Sep 17 00:00:00 2001 From: xiaojiazhu <258828110.@qq.com> Date: Thu, 17 Aug 2023 16:33:52 -0700 Subject: [PATCH] Improve hal module code. --- hal/abstract/IHal.cpp | 20 +++++++++----------- hal/include/IHal.h | 14 +++++++------- hal/src/Hal.c | 32 ++++++++++---------------------- hal/src/Hal.h | 2 +- hal/src/HalMakePtr.cpp | 6 +++--- 5 files changed, 30 insertions(+), 44 deletions(-) diff --git a/hal/abstract/IHal.cpp b/hal/abstract/IHal.cpp index a1c82994..8323ee90 100644 --- a/hal/abstract/IHal.cpp +++ b/hal/abstract/IHal.cpp @@ -2,30 +2,28 @@ #include "IHalCpp.h" #include #include -static RETURN_CODE_C hal_init(IHal *object) +static RETURN_CODE_C IHalInit(IHal *object) { - // return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); return IHalCpp::GetInstance()->Init(); } -static void hal_free(IHal *object) +static void IHalFree(IHal *object) { } -static RETURN_CODE_C hal_un_init(IHal *object) +static RETURN_CODE_C IHalUnInit(IHal *object) { - // return CreateReturnCode(C_RETURN_CODE_VIRTUAL_FUNCTION); return IHalCpp::GetInstance()->UnInit(); } static IHal default_hal = { - .init = hal_init, - .free = hal_free, - .un_init = hal_un_init, + .init = IHalInit, + .free = IHalFree, + .un_init = IHalUnInit, }; static IHal *hal_instance = &default_hal; -IHal *get_hal_instance(void) +IHal *GetHalIntance(void) { return hal_instance; } -RETURN_CODE_C new_i_hal(IHal **object) +RETURN_CODE_C NewIHal(IHal **object) { if (!object || !(*object)) { @@ -34,7 +32,7 @@ RETURN_CODE_C new_i_hal(IHal **object) memcpy(*object, &default_hal, sizeof(IHal)); return CreateReturnCode(C_RETURN_CODE_OK); } -void reset_hal_impl(IHal *impl) +void ResetHalImpl(IHal *impl) { hal_instance->free(hal_instance); hal_instance = impl; diff --git a/hal/include/IHal.h b/hal/include/IHal.h index 3dac4afb..3447639c 100644 --- a/hal/include/IHal.h +++ b/hal/include/IHal.h @@ -15,16 +15,16 @@ extern "C" 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) + IHal *GetHalIntance(void); + RETURN_CODE_C NewIHal(IHal **object); + void ResetHalImpl(IHal *impl); + static inline RETURN_CODE_C IHalInit(void) { - return get_hal_instance()->init(get_hal_instance()); + return GetHalIntance()->init(GetHalIntance()); } - static inline RETURN_CODE_C i_hal_un_init(void) + static inline RETURN_CODE_C IHalUnInit(void) { - return get_hal_instance()->un_init(get_hal_instance()); + return GetHalIntance()->un_init(GetHalIntance()); } RETURN_CODE_C create_hal_module(void); RETURN_CODE_C destroy_hal_module(void); diff --git a/hal/src/Hal.c b/hal/src/Hal.c index 29c5b885..5bd18ecc 100644 --- a/hal/src/Hal.c +++ b/hal/src/Hal.c @@ -2,12 +2,7 @@ #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) +static void HalFree(IHal *object) { LogInfo("hal instance free.\n"); if (object) @@ -15,19 +10,12 @@ 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 void hal_init_impl(Hal *hal) +static void HalImplInit(Hal *hal) { - LogInfo("hal_init_impl\n"); - // ((IHal *)hal)->init = hal_init; - ((IHal *)hal)->free = hal_free; - // ((IHal *)hal)->un_init = hal_un_init; + LogInfo("HalImplInit\n"); + ((IHal *)hal)->free = HalFree; } -RETURN_CODE_C new_hal(Hal **hal) +RETURN_CODE_C NewHal(Hal **hal) { if (!hal) { @@ -39,17 +27,17 @@ RETURN_CODE_C new_hal(Hal **hal) *hal = (Hal *)malloc(sizeof(Hal)); if (*hal) { - LogInfo("new_hal succeed.\n"); - new_i_hal((IHal **)hal); - hal_init_impl(*hal); + LogInfo("NewHal succeed.\n"); + NewIHal((IHal **)hal); + HalImplInit(*hal); return CreateReturnCode(C_RETURN_CODE_OK); } - LogError("new_hal failed.\n"); + LogError("NewHal failed.\n"); return CreateReturnCode(C_RETURN_CODE_NOT_OK); } else { - hal_init_impl(*hal); + HalImplInit(*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 index 323a3a88..e9cba263 100644 --- a/hal/src/Hal.h +++ b/hal/src/Hal.h @@ -14,7 +14,7 @@ extern "C" { IHal base; } Hal; - RETURN_CODE_C new_hal(Hal **hal); + RETURN_CODE_C NewHal(Hal **hal); #ifdef __cplusplus } #endif diff --git a/hal/src/HalMakePtr.cpp b/hal/src/HalMakePtr.cpp index ed290dea..5be863a4 100644 --- a/hal/src/HalMakePtr.cpp +++ b/hal/src/HalMakePtr.cpp @@ -9,14 +9,14 @@ RETURN_CODE_C create_hal_module(void) if (C_RETURN_CODE_OK == code.mCode) { LogInfo("Create Hal instance ok.\n"); - reset_hal_impl((IHal *)hal); + ResetHalImpl((IHal *)hal); return code; } return CreateReturnCode(C_RETURN_CODE_NOT_OK); } RETURN_CODE_C destroy_hal_module(void) { - reset_hal_impl(NULL); + ResetHalImpl(nullptr); return CreateReturnCode(C_RETURN_CODE_OK); } std::shared_ptr &HalMakePtr::GetInstance(std::shared_ptr *impl) @@ -30,7 +30,7 @@ std::shared_ptr &HalMakePtr::GetInstance(std::shared_ptr } RETURN_CODE_C HalMakePtr::CreateHalPtr(IHal **hal) { - return new_hal((Hal **)hal); + return NewHal((Hal **)hal); } RETURN_CODE_C HalMakePtr::CreateHalSharePtr(std::shared_ptr &impl) {