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")
add_custom_target(
hal_code_check
# COMMAND ${CMAKE_SOURCE_DIR_IPCSDK}/tools/clang-tidy/clang-tidy
Hal_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
# -header-filter=.*
# -system-headers
--header-filter=.*
--system-headers=false
${ABSTRACT_SRC_FILES}
${IMPL_SRC_FILES}
${CLANG_TIDY_CONFIG}
-p ${PLATFORM_PATH}/cmake-shell
# -- -I /usr/include/linux/ -X c++
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(
TARGET ${IMPL_TARGET}
TARGET ${ABSTRACT_TARGET}
PRE_BUILD
COMMAND make hal_code_check
COMMAND make Hal_code_check
COMMAND make Hal_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()

View File

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

View File

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

View File

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

View File

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

View File

@ -1,17 +1,16 @@
#ifndef HAL_H
#define HAL_H
#include "StatusCode.h"
#include "IHal.h"
#include "StatusCode.h"
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
typedef struct hal Hal;
typedef struct hal
{
typedef struct hal Hal;
typedef struct hal
{
IHal base;
} Hal;
StatusCode NewHal(Hal **hal);
} Hal;
StatusCode NewHal(Hal **hal);
#ifdef __cplusplus
}
#endif

View File

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

View File

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

View File

@ -5,3 +5,4 @@ add_subdirectory(StatusCode)
add_subdirectory(Log)
add_subdirectory(SharedData)
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
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/LinuxApi/include
)
# link_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
@ -19,7 +20,7 @@ aux_source_directory(./src SRC_FILES)
set(TARGET_NAME SharedData)
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")
add_custom_target(

View File

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

View File

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