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 <stddef.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();
}
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;

View File

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

View File

@ -2,12 +2,7 @@
#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)
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);
}

View File

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

View File

@ -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> &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 new_hal((Hal **)hal);
return NewHal((Hal **)hal);
}
RETURN_CODE_C HalMakePtr::CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl)
{