Add linux api module.

This commit is contained in:
fancy 2023-11-17 07:46:03 -08:00
parent 8e626bc451
commit e0c4744eda
15 changed files with 158 additions and 95 deletions

View File

@ -31,26 +31,31 @@ target_link_libraries(${IMPL_TARGET} ${ABSTRACT_TARGET})
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target( add_custom_target(
hal_code_check Hal_code_check
# COMMAND ${CMAKE_SOURCE_DIR_IPCSDK}/tools/clang-tidy/clang-tidy
COMMAND ${CLANG_TIDY_EXE} COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}' -checks='${CLANG_TIDY_CHECKS}'
# -header-filter=.*
# -system-headers
--header-filter=.* --header-filter=.*
--system-headers=false --system-headers=false
${ABSTRACT_SRC_FILES} ${ABSTRACT_SRC_FILES}
${IMPL_SRC_FILES} ${IMPL_SRC_FILES}
${CLANG_TIDY_CONFIG} ${CLANG_TIDY_CONFIG}
-p ${PLATFORM_PATH}/cmake-shell -p ${PLATFORM_PATH}/cmake-shell
# -- -I /usr/include/linux/ -X c++
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR_IPCSDK}/hal WORKING_DIRECTORY ${CMAKE_SOURCE_DIR_IPCSDK}/hal
) )
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
Hal_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${ABSTRACT_SRC_FILES} ${IMPL_SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${HAL_SOURCE_PATH}
)
add_custom_command( add_custom_command(
TARGET ${IMPL_TARGET} TARGET ${IMPL_TARGET}
TARGET ${ABSTRACT_TARGET} TARGET ${ABSTRACT_TARGET}
PRE_BUILD PRE_BUILD
COMMAND make hal_code_check COMMAND make Hal_code_check
COMMAND make Hal_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
) )
endif() endif()

View File

@ -3,31 +3,19 @@
#include "StatusCode.h" #include "StatusCode.h"
// #include <stddef.h> // #include <stddef.h>
#include <string.h> #include <string.h>
static StatusCode IHalInit(IHal *object) static StatusCode IHalInit(IHal *object) { return IHalCpp::GetInstance()->Init(); }
{ static void IHalFree(IHal *object) {}
return IHalCpp::GetInstance()->Init(); static StatusCode IHalUnInit(IHal *object) { return IHalCpp::GetInstance()->UnInit(); }
}
static void IHalFree(IHal *object)
{
}
static StatusCode IHalUnInit(IHal *object)
{
return IHalCpp::GetInstance()->UnInit();
}
static IHal default_hal = { static IHal default_hal = {
.init = IHalInit, .init = IHalInit,
.un_init = IHalUnInit, .un_init = IHalUnInit,
.free = IHalFree, .free = IHalFree,
}; };
static IHal *hal_instance = &default_hal; static IHal *hal_instance = &default_hal;
IHal *GetHalIntance(void) IHal *GetHalIntance(void) { return hal_instance; }
{
return hal_instance;
}
StatusCode NewIHal(IHal **object) StatusCode NewIHal(IHal **object)
{ {
if (!object || !(*object)) if (!object || !(*object)) {
{
return CreateStatusCode(STATUS_CODE_NOT_OK); return CreateStatusCode(STATUS_CODE_NOT_OK);
} }
memcpy(*object, &default_hal, sizeof(IHal)); memcpy(*object, &default_hal, sizeof(IHal));

View File

@ -1,13 +1,11 @@
#include "IHalCpp.h" #include "IHalCpp.h"
#include "ILog.h" #include "ILog.h"
// #include <thread>
#include <memory> #include <memory>
std::shared_ptr<IHalCpp> &IHalCpp::GetInstance(std::shared_ptr<IHalCpp> *impl) std::shared_ptr<IHalCpp> &IHalCpp::GetInstance(std::shared_ptr<IHalCpp> *impl)
{ {
static std::shared_ptr<IHalCpp> instance = std::make_shared<IHalCpp>(); static std::shared_ptr<IHalCpp> instance = std::make_shared<IHalCpp>();
static bool instanceChanging = false; static bool instanceChanging = false;
if (impl && false == instanceChanging) if (impl && false == instanceChanging) {
{
// Don't use std::mutex for runing faster. // Don't use std::mutex for runing faster.
// Sleep for difference thread to release instance. // Sleep for difference thread to release instance.
instanceChanging = true; instanceChanging = true;
@ -15,18 +13,14 @@ std::shared_ptr<IHalCpp> &IHalCpp::GetInstance(std::shared_ptr<IHalCpp> *impl)
if (instance.use_count() == 1) // bug? if (instance.use_count() == 1) // bug?
{ {
LogInfo("Instance change succeed.\n"); LogInfo("Instance change succeed.\n");
// instance->UnInit();
// (*impl)->Init();
instance = *impl; instance = *impl;
} }
else else {
{
LogError("[ error ] instance change failed, using by some one.\n"); LogError("[ error ] instance change failed, using by some one.\n");
} }
instanceChanging = false; instanceChanging = false;
} }
if (instanceChanging) if (instanceChanging) {
{
static std::shared_ptr<IHalCpp> tmporaryInstance = std::make_shared<IHalCpp>(); static std::shared_ptr<IHalCpp> tmporaryInstance = std::make_shared<IHalCpp>();
return tmporaryInstance; return tmporaryInstance;
} }

View File

@ -2,8 +2,7 @@
#define IHAL_H #define IHAL_H
#include "StatusCode.h" #include "StatusCode.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C" {
{
#endif #endif
typedef struct i_hal IHal; typedef struct i_hal IHal;
typedef struct i_hal typedef struct i_hal
@ -15,16 +14,10 @@ extern "C"
IHal *GetHalIntance(void); IHal *GetHalIntance(void);
StatusCode NewIHal(IHal **object); StatusCode NewIHal(IHal **object);
void ResetHalImpl(IHal *impl); void ResetHalImpl(IHal *impl);
static inline StatusCode IHalInit(void) static inline StatusCode IHalInit(void) { return GetHalIntance()->init(GetHalIntance()); }
{ static inline StatusCode IHalUnInit(void) { return GetHalIntance()->un_init(GetHalIntance()); }
return GetHalIntance()->init(GetHalIntance()); StatusCode CreateHalModule(void);
} StatusCode DestroyHalModule(void);
static inline StatusCode IHalUnInit(void)
{
return GetHalIntance()->un_init(GetHalIntance());
}
StatusCode create_hal_module(void);
StatusCode destroy_hal_module(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -5,8 +5,7 @@
static void HalFree(IHal *object) static void HalFree(IHal *object)
{ {
LogInfo("hal instance free.\n"); LogInfo("hal instance free.\n");
if (object) if (object) {
{
free(object); free(object);
} }
} }
@ -17,16 +16,13 @@ static void HalImplInit(Hal *hal)
} }
StatusCode NewHal(Hal **hal) StatusCode NewHal(Hal **hal)
{ {
if (!hal) if (!hal) {
{
LogError("STATUS_CODE_INVALID_PARAMENTER\n"); LogError("STATUS_CODE_INVALID_PARAMENTER\n");
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
} }
if (!(*hal)) if (!(*hal)) {
{
*hal = (Hal *)malloc(sizeof(Hal)); *hal = (Hal *)malloc(sizeof(Hal));
if (*hal) if (*hal) {
{
LogInfo("NewHal succeed.\n"); LogInfo("NewHal succeed.\n");
NewIHal((IHal **)hal); NewIHal((IHal **)hal);
HalImplInit(*hal); HalImplInit(*hal);

View File

@ -1,10 +1,9 @@
#ifndef HAL_H #ifndef HAL_H
#define HAL_H #define HAL_H
#include "StatusCode.h"
#include "IHal.h" #include "IHal.h"
#include "StatusCode.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C" {
{
#endif #endif
typedef struct hal Hal; typedef struct hal Hal;
typedef struct hal typedef struct hal

View File

@ -2,29 +2,26 @@
#include "Hal.h" #include "Hal.h"
#include "HalCpp.h" #include "HalCpp.h"
#include "ILog.h" #include "ILog.h"
StatusCode create_hal_module(void) StatusCode CreateHalModule(void)
{ {
IHal *hal = NULL; IHal *hal = NULL;
StatusCode code = HalMakePtr::GetInstance()->CreateHalPtr(&hal); StatusCode code = HalMakePtr::GetInstance()->CreateHalPtr(&hal);
if (IsCodeOK(code)) if (IsCodeOK(code)) {
{
LogInfo("Create Hal instance ok.\n"); LogInfo("Create Hal instance ok.\n");
ResetHalImpl((IHal *)hal); ResetHalImpl((IHal *)hal);
} }
else else {
{
return CreateStatusCode(STATUS_CODE_NOT_OK); return CreateStatusCode(STATUS_CODE_NOT_OK);
} }
auto instance = std::make_shared<IHalCpp>(); auto instance = std::make_shared<IHalCpp>();
StatusCode code2 = HalMakePtr::GetInstance()->CreateHalSharePtr(instance); StatusCode code2 = HalMakePtr::GetInstance()->CreateHalSharePtr(instance);
if (IsCodeOK(code2)) if (IsCodeOK(code2)) {
{
LogInfo("IHal manager instance is ok.\n"); LogInfo("IHal manager instance is ok.\n");
IHalCpp::GetInstance(&instance); IHalCpp::GetInstance(&instance);
} }
return code2; return code2;
} }
StatusCode destroy_hal_module(void) StatusCode DestroyHalModule(void)
{ {
ResetHalImpl(nullptr); ResetHalImpl(nullptr);
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
@ -32,16 +29,12 @@ StatusCode destroy_hal_module(void)
std::shared_ptr<HalMakePtr> &HalMakePtr::GetInstance(std::shared_ptr<HalMakePtr> *impl) std::shared_ptr<HalMakePtr> &HalMakePtr::GetInstance(std::shared_ptr<HalMakePtr> *impl)
{ {
static auto instance = std::make_shared<HalMakePtr>(); static auto instance = std::make_shared<HalMakePtr>();
if (impl) if (impl) {
{
instance = *impl; instance = *impl;
} }
return instance; return instance;
} }
StatusCode HalMakePtr::CreateHalPtr(IHal **hal) StatusCode HalMakePtr::CreateHalPtr(IHal **hal) { return NewHal((Hal **)hal); }
{
return NewHal((Hal **)hal);
}
StatusCode HalMakePtr::CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl) StatusCode HalMakePtr::CreateHalSharePtr(std::shared_ptr<IHalCpp> &impl)
{ {
LogInfo("HalMakePtr make ptr.\n"); LogInfo("HalMakePtr make ptr.\n");

View File

@ -9,14 +9,14 @@ namespace IHalTest
TEST(IHalTest, Demo) TEST(IHalTest, Demo)
{ {
ILogInit(LOG_EASYLOGGING); ILogInit(LOG_EASYLOGGING);
create_hal_module(); CreateHalModule();
StatusCode code = IHalInit(); StatusCode code = IHalInit();
if (IsCodeOK(code)) if (IsCodeOK(code))
{ {
PrintStringCode(code); PrintStringCode(code);
} }
IHalUnInit(); IHalUnInit();
destroy_hal_module(); DestroyHalModule();
ILogUnInit(); ILogUnInit();
} }
} }

View File

@ -5,3 +5,4 @@ add_subdirectory(StatusCode)
add_subdirectory(Log) add_subdirectory(Log)
add_subdirectory(SharedData) add_subdirectory(SharedData)
add_subdirectory(UartDevice) add_subdirectory(UartDevice)
add_subdirectory(LinuxApi)

View File

@ -0,0 +1,52 @@
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
include_directories(
./src
./include
${UTILS_SOURCE_PATH}/Log/include
)
# link_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
# )
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME LinuxApi)
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
target_link_libraries(${TARGET_NAME} Log)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(
LinuxApi_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/LinuxApi
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
LinuxApi_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/LinuxApi
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make LinuxApi_code_check
COMMAND make LinuxApi_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})

View File

@ -0,0 +1,24 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LINUX_API_H
#define LINUX_API_H
#ifdef __cplusplus
extern "C" {
#endif
int FX_system(const char *command);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,17 @@
/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "LinuxApi.h"
#include <stdlib.h>
int FX_system(const char *command) { return system(command); }

View File

@ -7,6 +7,7 @@ include_directories(
./include ./include
${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/LinuxApi/include
) )
# link_directories( # link_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs # ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
@ -19,7 +20,7 @@ aux_source_directory(./src SRC_FILES)
set(TARGET_NAME SharedData) set(TARGET_NAME SharedData)
add_library(${TARGET_NAME} STATIC ${SRC_FILES}) add_library(${TARGET_NAME} STATIC ${SRC_FILES})
target_link_libraries(${TARGET_NAME} StatusCode Log) target_link_libraries(${TARGET_NAME} StatusCode Log LinuxApi)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target( add_custom_target(

View File

@ -30,10 +30,11 @@ typedef struct shared_data_header
{ {
const char *mCheckName; const char *mCheckName;
} SharedDataHeader; } SharedDataHeader;
typedef struct shared_memory_header // TODO: Use mutex to luck memory data?
{ // typedef struct shared_memory_header
std::mutex mMutex; // {
}; // std::mutex mMutex;
// };
typedef struct user_data_header typedef struct user_data_header
{ {
char mUserName[USER_NAME_BUF_LENGTH]; char mUserName[USER_NAME_BUF_LENGTH];
@ -54,8 +55,6 @@ private:
private: private:
const SHARER_NAME mSharerName; const SHARER_NAME mSharerName;
// std::shared_ptr<SharedMemory> mReadableMemory;
// std::shared_ptr<SharedMemory> mWritableMemory;
unsigned int mPrimaryReadSize; unsigned int mPrimaryReadSize;
unsigned int mMinorReadSize; unsigned int mMinorReadSize;
void *mSharedMemeory; void *mSharedMemeory;

View File

@ -14,6 +14,7 @@
*/ */
#include "SharedMemory.h" #include "SharedMemory.h"
#include "ILog.h" #include "ILog.h"
#include "LinuxApi.h"
#include "SharedDataCode.h" #include "SharedDataCode.h"
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
@ -32,7 +33,7 @@ StatusCode SharedMemory::MakeSharedMemory(const int &size)
char touchPath[128] = {0}; char touchPath[128] = {0};
if (access(mPath, F_OK) != 0) { if (access(mPath, F_OK) != 0) {
sprintf(touchPath, "%s %s", "touch", mPath); sprintf(touchPath, "%s %s", "touch", mPath);
system(touchPath); FX_system(touchPath);
} }
key_t key = ftok(mPath, mProjectId); key_t key = ftok(mPath, mProjectId);
if (key < 0) { if (key < 0) {