Improve hal module code.

This commit is contained in:
xiaojiazhu 2023-08-17 16:33:52 -07:00
parent 8fece6d8c6
commit e14eeb7ad8
5 changed files with 30 additions and 44 deletions

View File

@ -2,30 +2,28 @@
#include "IHalCpp.h" #include "IHalCpp.h"
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
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(); 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(); return IHalCpp::GetInstance()->UnInit();
} }
static IHal default_hal = { static IHal default_hal = {
.init = hal_init, .init = IHalInit,
.free = hal_free, .free = IHalFree,
.un_init = hal_un_init, .un_init = IHalUnInit,
}; };
static IHal *hal_instance = &default_hal; static IHal *hal_instance = &default_hal;
IHal *get_hal_instance(void) IHal *GetHalIntance(void)
{ {
return hal_instance; return hal_instance;
} }
RETURN_CODE_C new_i_hal(IHal **object) RETURN_CODE_C NewIHal(IHal **object)
{ {
if (!object || !(*object)) if (!object || !(*object))
{ {
@ -34,7 +32,7 @@ RETURN_CODE_C new_i_hal(IHal **object)
memcpy(*object, &default_hal, sizeof(IHal)); memcpy(*object, &default_hal, sizeof(IHal));
return CreateReturnCode(C_RETURN_CODE_OK); return CreateReturnCode(C_RETURN_CODE_OK);
} }
void reset_hal_impl(IHal *impl) void ResetHalImpl(IHal *impl)
{ {
hal_instance->free(hal_instance); hal_instance->free(hal_instance);
hal_instance = impl; hal_instance = impl;

View File

@ -15,16 +15,16 @@ extern "C"
void (*free)(IHal *); void (*free)(IHal *);
RETURN_CODE_C (*un_init)(IHal *); RETURN_CODE_C (*un_init)(IHal *);
} IHal; } IHal;
IHal *get_hal_instance(void); IHal *GetHalIntance(void);
RETURN_CODE_C new_i_hal(IHal **object); RETURN_CODE_C NewIHal(IHal **object);
void reset_hal_impl(IHal *impl); void ResetHalImpl(IHal *impl);
static inline RETURN_CODE_C i_hal_init(void) 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 create_hal_module(void);
RETURN_CODE_C destroy_hal_module(void); RETURN_CODE_C destroy_hal_module(void);

View File

@ -2,12 +2,7 @@
#include "Log.h" #include "Log.h"
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
// static RETURN_CODE_C hal_init(IHal *object) static void HalFree(IHal *object)
// {
// LogInfo("hal instance init.\n");
// return CreateReturnCode(C_RETURN_CODE_OK);
// }
static void hal_free(IHal *object)
{ {
LogInfo("hal instance free.\n"); LogInfo("hal instance free.\n");
if (object) if (object)
@ -15,19 +10,12 @@ static void hal_free(IHal *object)
free(object); free(object);
} }
} }
// static RETURN_CODE_C hal_un_init(IHal *object) static void HalImplInit(Hal *hal)
// {
// LogInfo("hal instance un_init.\n");
// return CreateReturnCode(C_RETURN_CODE_OK);
// }
static void hal_init_impl(Hal *hal)
{ {
LogInfo("hal_init_impl\n"); LogInfo("HalImplInit\n");
// ((IHal *)hal)->init = hal_init; ((IHal *)hal)->free = HalFree;
((IHal *)hal)->free = hal_free;
// ((IHal *)hal)->un_init = hal_un_init;
} }
RETURN_CODE_C new_hal(Hal **hal) RETURN_CODE_C NewHal(Hal **hal)
{ {
if (!hal) if (!hal)
{ {
@ -39,17 +27,17 @@ RETURN_CODE_C new_hal(Hal **hal)
*hal = (Hal *)malloc(sizeof(Hal)); *hal = (Hal *)malloc(sizeof(Hal));
if (*hal) if (*hal)
{ {
LogInfo("new_hal succeed.\n"); LogInfo("NewHal succeed.\n");
new_i_hal((IHal **)hal); NewIHal((IHal **)hal);
hal_init_impl(*hal); HalImplInit(*hal);
return CreateReturnCode(C_RETURN_CODE_OK); return CreateReturnCode(C_RETURN_CODE_OK);
} }
LogError("new_hal failed.\n"); LogError("NewHal failed.\n");
return CreateReturnCode(C_RETURN_CODE_NOT_OK); return CreateReturnCode(C_RETURN_CODE_NOT_OK);
} }
else else
{ {
hal_init_impl(*hal); HalImplInit(*hal);
} }
return CreateReturnCode(C_RETURN_CODE_OK); return CreateReturnCode(C_RETURN_CODE_OK);
} }

View File

@ -14,7 +14,7 @@ extern "C"
{ {
IHal base; IHal base;
} Hal; } Hal;
RETURN_CODE_C new_hal(Hal **hal); RETURN_CODE_C NewHal(Hal **hal);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -9,14 +9,14 @@ RETURN_CODE_C create_hal_module(void)
if (C_RETURN_CODE_OK == code.mCode) if (C_RETURN_CODE_OK == code.mCode)
{ {
LogInfo("Create Hal instance ok.\n"); LogInfo("Create Hal instance ok.\n");
reset_hal_impl((IHal *)hal); ResetHalImpl((IHal *)hal);
return code; return code;
} }
return CreateReturnCode(C_RETURN_CODE_NOT_OK); return CreateReturnCode(C_RETURN_CODE_NOT_OK);
} }
RETURN_CODE_C destroy_hal_module(void) RETURN_CODE_C destroy_hal_module(void)
{ {
reset_hal_impl(NULL); ResetHalImpl(nullptr);
return CreateReturnCode(C_RETURN_CODE_OK); return CreateReturnCode(C_RETURN_CODE_OK);
} }
std::shared_ptr<HalMakePtr> &HalMakePtr::GetInstance(std::shared_ptr<HalMakePtr> *impl) std::shared_ptr<HalMakePtr> &HalMakePtr::GetInstance(std::shared_ptr<HalMakePtr> *impl)
@ -30,7 +30,7 @@ std::shared_ptr<HalMakePtr> &HalMakePtr::GetInstance(std::shared_ptr<HalMakePtr>
} }
RETURN_CODE_C HalMakePtr::CreateHalPtr(IHal **hal) RETURN_CODE_C HalMakePtr::CreateHalPtr(IHal **hal)
{ {
return new_hal((Hal **)hal); return NewHal((Hal **)hal);
} }
RETURN_CODE_C HalMakePtr::CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl) RETURN_CODE_C HalMakePtr::CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl)
{ {