1.Delete hal.

2.Add new hal.
This commit is contained in:
xiaojiazhu 2023-08-17 08:20:31 -07:00
parent e45d061399
commit 080c1f6c64
14 changed files with 131 additions and 109 deletions

View File

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

View File

@ -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)
target_link_libraries(${IMPL_TARGET} ${ABSTRACT_TARGET} Log)

View File

@ -1,16 +1,19 @@
#include "i_hal.h"
#include "IHal.h"
#include "IHalCpp.h"
#include <stddef.h>
#include <string.h>
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,

33
hal/abstract/IHalCpp.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "IHalCpp.h"
#include "Log.h"
#include <thread>
std::shared_ptr<IHalCpp> &IHalCpp::GetInstance(std::shared_ptr<IHalCpp> *impl)
{
static std::shared_ptr<IHalCpp> instance = std::make_shared<IHalCpp>();
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<IHalCpp> tmporaryInstance = std::make_shared<IHalCpp>();
return tmporaryInstance;
}
return instance;
}

14
hal/include/IHalCpp.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef IHAL_CPP_H
#define IHAL_CPP_H
#include "ReturnCode.h"
#include <memory>
class IHalCpp
{
public:
IHalCpp() = default;
virtual ~IHalCpp() = default;
static std::shared_ptr<IHalCpp> &GetInstance(std::shared_ptr<IHalCpp> *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

View File

@ -1,12 +1,12 @@
#include "hal.h"
#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 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)
{

View File

@ -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++.
*/

0
hal/src/HalCpp.cpp Normal file
View File

10
hal/src/HalCpp.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef HALCPP_H
#define HALCPP_H
#include "IHalCpp.h"
class HalCpp : public IHalCpp
{
public:
HalCpp() = default;
virtual ~HalCpp() = default;
};
#endif

35
hal/src/HalMakePtr.cpp Normal file
View File

@ -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> &HalMakePtr::GetInstance(std::shared_ptr<HalMakePtr> *impl)
{
static auto instance = std::make_shared<HalMakePtr>();
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<IHalCpp> &impl)
{
LogInfo("HalMakePtr make ptr.\n");
impl = std::make_shared<HalCpp>();
return CreateReturnCode(C_RETURN_CODE_NOT_OK);
}

18
hal/src/HalMakePtr.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef HALMAKEPTR_H
#define HALMAKEPTR_H
#include "IHal.h"
#include "IHalCpp.h"
#include "ReturnCode.h"
#include <memory>
class HalMakePtr
{
public:
HalMakePtr() = default;
virtual ~HalMakePtr() = default;
static std::shared_ptr<HalMakePtr> &GetInstance(std::shared_ptr<HalMakePtr> *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<IHalCpp> &impl);
};
#endif

View File

@ -1,50 +0,0 @@
#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)
{
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;
}

View File

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