diff --git a/hal/CMakeLists.txt b/hal/CMakeLists.txt index e1be1c6..8fda88a 100644 --- a/hal/CMakeLists.txt +++ b/hal/CMakeLists.txt @@ -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() \ No newline at end of file diff --git a/hal/abstract/IHal.cpp b/hal/abstract/IHal.cpp index cc6dcd2..681f38c 100644 --- a/hal/abstract/IHal.cpp +++ b/hal/abstract/IHal.cpp @@ -3,31 +3,19 @@ #include "StatusCode.h" // #include #include -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)); diff --git a/hal/abstract/IHalCpp.cpp b/hal/abstract/IHalCpp.cpp index a18cfd4..8e908df 100644 --- a/hal/abstract/IHalCpp.cpp +++ b/hal/abstract/IHalCpp.cpp @@ -1,13 +1,11 @@ #include "IHalCpp.h" #include "ILog.h" -// #include #include std::shared_ptr &IHalCpp::GetInstance(std::shared_ptr *impl) { static std::shared_ptr instance = std::make_shared(); 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::GetInstance(std::shared_ptr *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 tmporaryInstance = std::make_shared(); return tmporaryInstance; } diff --git a/hal/include/IHal.h b/hal/include/IHal.h index d95ad09..071d11c 100644 --- a/hal/include/IHal.h +++ b/hal/include/IHal.h @@ -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 - { - 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); +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 CreateHalModule(void); +StatusCode DestroyHalModule(void); #ifdef __cplusplus } #endif diff --git a/hal/src/Hal.c b/hal/src/Hal.c index c175032..6e6f8d5 100644 --- a/hal/src/Hal.c +++ b/hal/src/Hal.c @@ -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); diff --git a/hal/src/Hal.h b/hal/src/Hal.h index 2cb1059..90f1483 100644 --- a/hal/src/Hal.h +++ b/hal/src/Hal.h @@ -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 - { - IHal base; - } Hal; - StatusCode NewHal(Hal **hal); +typedef struct hal Hal; +typedef struct hal +{ + IHal base; +} Hal; +StatusCode NewHal(Hal **hal); #ifdef __cplusplus } #endif diff --git a/hal/src/HalMakePtr.cpp b/hal/src/HalMakePtr.cpp index 104dd73..5f8daf7 100644 --- a/hal/src/HalMakePtr.cpp +++ b/hal/src/HalMakePtr.cpp @@ -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(); 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::GetInstance(std::shared_ptr *impl) { static auto instance = std::make_shared(); - 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 &impl) { LogInfo("HalMakePtr make ptr.\n"); diff --git a/test/hal/src/IHalTest.cpp b/test/hal/src/IHalTest.cpp index fd5ba1b..b2b8ede 100644 --- a/test/hal/src/IHalTest.cpp +++ b/test/hal/src/IHalTest.cpp @@ -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(); } } \ No newline at end of file diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 83af998..5059905 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -5,3 +5,4 @@ add_subdirectory(StatusCode) add_subdirectory(Log) add_subdirectory(SharedData) add_subdirectory(UartDevice) +add_subdirectory(LinuxApi) diff --git a/utils/LinuxApi/CMakeLists.txt b/utils/LinuxApi/CMakeLists.txt new file mode 100644 index 0000000..6891146 --- /dev/null +++ b/utils/LinuxApi/CMakeLists.txt @@ -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}) \ No newline at end of file diff --git a/utils/LinuxApi/include/LinuxApi.h b/utils/LinuxApi/include/LinuxApi.h new file mode 100644 index 0000000..db9c6dc --- /dev/null +++ b/utils/LinuxApi/include/LinuxApi.h @@ -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 \ No newline at end of file diff --git a/utils/LinuxApi/src/LinuxApi.c b/utils/LinuxApi/src/LinuxApi.c new file mode 100644 index 0000000..daca257 --- /dev/null +++ b/utils/LinuxApi/src/LinuxApi.c @@ -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 +int FX_system(const char *command) { return system(command); } \ No newline at end of file diff --git a/utils/SharedData/CMakeLists.txt b/utils/SharedData/CMakeLists.txt index bae3a49..50a05d6 100644 --- a/utils/SharedData/CMakeLists.txt +++ b/utils/SharedData/CMakeLists.txt @@ -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( diff --git a/utils/SharedData/src/SharedDataImpl.h b/utils/SharedData/src/SharedDataImpl.h index 5aee3cd..41072b5 100644 --- a/utils/SharedData/src/SharedDataImpl.h +++ b/utils/SharedData/src/SharedDataImpl.h @@ -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 mReadableMemory; - // std::shared_ptr mWritableMemory; unsigned int mPrimaryReadSize; unsigned int mMinorReadSize; void *mSharedMemeory; diff --git a/utils/SharedData/src/SharedMemory.cpp b/utils/SharedData/src/SharedMemory.cpp index 9460c93..549063b 100644 --- a/utils/SharedData/src/SharedMemory.cpp +++ b/utils/SharedData/src/SharedMemory.cpp @@ -14,6 +14,7 @@ */ #include "SharedMemory.h" #include "ILog.h" +#include "LinuxApi.h" #include "SharedDataCode.h" #include #include @@ -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) {