Backup McuManager module.
This commit is contained in:
		
							parent
							
								
									0529077864
								
							
						
					
					
						commit
						988d2a03e5
					
				|  | @ -1,4 +1,5 @@ | |||
| add_subdirectory(StateMachine) | ||||
| add_subdirectory(IpcConfig) | ||||
| add_subdirectory(DeviceManager) | ||||
| add_subdirectory(McuManager) | ||||
| add_subdirectory(McuManager) | ||||
| add_subdirectory(McuAskBase) | ||||
							
								
								
									
										70
									
								
								middleware/McuAskBase/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								middleware/McuAskBase/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,70 @@ | |||
| 
 | ||||
| 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 | ||||
|     ${MIDDLEWARE_SOURCE_PATH}/McuManager/include | ||||
|     ${UTILS_SOURCE_PATH}/StatusCode/include | ||||
|     ${UTILS_SOURCE_PATH}/Log/include | ||||
|     ${UTILS_SOURCE_PATH}/McuProtocol/include | ||||
|     ${UTILS_SOURCE_PATH}/UartDevice/include | ||||
| ) | ||||
| #do not rely on any other library | ||||
| #link_directories( | ||||
| #) | ||||
| 
 | ||||
| set(CMAKE_AUTOMOC ON) | ||||
| set(CMAKE_INCLUDE_CURRENT_DIR ON) | ||||
| 
 | ||||
| if (DEFINED MCU_UART_DEVICE) | ||||
|     add_definitions(-DMCU_UART_DEVICE=\"${MCU_UART_DEVICE}\") | ||||
| else() | ||||
|     message(FATAL_ERROR "You set define MCU_UART_DEVICE in toolchan .cmake file to tell what uart device to contrl.") | ||||
| endif() | ||||
| 
 | ||||
| aux_source_directory(./src SRC_FILES) | ||||
| 
 | ||||
| set(TARGET_NAME McuAskBase) | ||||
| add_library(${TARGET_NAME} STATIC ${SRC_FILES}) | ||||
| 
 | ||||
| target_link_libraries(${TARGET_NAME} McuManager StatusCode Log) | ||||
| 
 | ||||
| if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") | ||||
| add_custom_target( | ||||
|     McuAskBase_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 ${MIDDLEWARE_SOURCE_PATH}/McuAskBase | ||||
| ) | ||||
| add_custom_command( | ||||
|     TARGET ${TARGET_NAME} | ||||
|     PRE_BUILD | ||||
|     COMMAND make McuAskBase_code_check | ||||
|     WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ | ||||
| ) | ||||
| file(GLOB_RECURSE HEADER_FILES *.h) | ||||
| add_custom_target( | ||||
|     McuAskBase_code_format | ||||
|     COMMAND ${CLANG_FORMAT_EXE} | ||||
|     -style=file | ||||
|     -i ${SRC_FILES}  ${HEADER_FILES} | ||||
|     WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/McuAskBase | ||||
| ) | ||||
| add_custom_command( | ||||
|     TARGET ${TARGET_NAME} | ||||
|     PRE_BUILD | ||||
|     COMMAND make McuAskBase_code_check | ||||
|     COMMAND make McuAskBase_code_format | ||||
|     WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ | ||||
| ) | ||||
| endif() | ||||
| 
 | ||||
| define_file_name(${TARGET_NAME}) | ||||
							
								
								
									
										53
									
								
								middleware/McuAskBase/include/McuAskBase.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								middleware/McuAskBase/include/McuAskBase.h
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,53 @@ | |||
| /*
 | ||||
|  * 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 MCUASK_BASE_H | ||||
| #define MCUASK_BASE_H | ||||
| #include "IMcuManager.h" | ||||
| #include <semaphore.h> | ||||
| constexpr unsigned int ASK_NEVER_TIMEOUT = 0; | ||||
| constexpr unsigned int DEFAULT_ASK_TIMEOUT = 5000; | ||||
| enum class McuAskBlock | ||||
| { | ||||
|     BLOCK = 0, | ||||
|     NOT_BLOCK, | ||||
|     END | ||||
| }; | ||||
| enum class McuAskReply | ||||
| { | ||||
|     NEED_REPLY = 0, | ||||
|     NEED_NOT_REPLY, | ||||
|     END | ||||
| }; | ||||
| class McuAskBase : virtual public VMcuAsk | ||||
| { | ||||
| public: | ||||
|     McuAskBase(const McuAskBlock isBlock, const McuAskReply needReply, | ||||
|                const unsigned int timeoutMs = DEFAULT_ASK_TIMEOUT); | ||||
|     virtual ~McuAskBase() = default; | ||||
|     ASK_RESULT Blocking(void) override; | ||||
|     void StopBlocking(void) override; | ||||
|     bool NeedReply(void) override; | ||||
|     void ReplyFinished(const bool result) override; | ||||
|     bool IfTimeout(const unsigned int &integrationTimeMs) override; | ||||
| 
 | ||||
| private: | ||||
|     sem_t mSem; | ||||
|     McuAskBlock mIsBlock; | ||||
|     McuAskReply mNeedReply; | ||||
|     bool mReplyOK; | ||||
|     unsigned int mTimeout; | ||||
|     unsigned int mWaitingTimeMs; | ||||
| }; | ||||
| #endif | ||||
							
								
								
									
										59
									
								
								middleware/McuAskBase/src/McuAskBase.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								middleware/McuAskBase/src/McuAskBase.cpp
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,59 @@ | |||
| /*
 | ||||
|  * 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 "McuAskBase.h" | ||||
| #include "ILog.h" | ||||
| McuAskBase::McuAskBase(const McuAskBlock isBlock, const McuAskReply needReply, const unsigned int timeoutMs) | ||||
|     : mIsBlock(isBlock), mNeedReply(needReply), mTimeout(timeoutMs) | ||||
| { | ||||
|     constexpr int THREAD_SHARING = 0; | ||||
|     constexpr int INITIAL_VALUE_OF_SEMAPHORE = 0; | ||||
|     sem_init(&mSem, THREAD_SHARING, INITIAL_VALUE_OF_SEMAPHORE); | ||||
|     mReplyOK = false; | ||||
|     mWaitingTimeMs = 0; | ||||
| } | ||||
| ASK_RESULT McuAskBase::Blocking(void) | ||||
| { | ||||
|     if (McuAskBlock::BLOCK == mIsBlock) { | ||||
|         sem_wait(&mSem); | ||||
|     } | ||||
|     if (true == mReplyOK) { | ||||
|         return ASK_RESULT::SUCCEED; | ||||
|     } | ||||
|     return ASK_RESULT::FAILED; | ||||
| } | ||||
| void McuAskBase::StopBlocking(void) | ||||
| { | ||||
|     //
 | ||||
|     sem_post(&mSem); | ||||
| } | ||||
| bool McuAskBase::NeedReply(void) | ||||
| { | ||||
|     //
 | ||||
|     return mNeedReply == McuAskReply::NEED_REPLY ? true : false; | ||||
| } | ||||
| void McuAskBase::ReplyFinished(const bool result) | ||||
| { | ||||
|     //
 | ||||
|     mReplyOK = result; | ||||
|     sem_post(&mSem); | ||||
| } | ||||
| bool McuAskBase::IfTimeout(const unsigned int &integrationTimeMs) | ||||
| { | ||||
|     mWaitingTimeMs += integrationTimeMs; | ||||
|     if (mWaitingTimeMs >= mTimeout) { | ||||
|         return true; | ||||
|     } | ||||
|     return false; | ||||
| } | ||||
|  | @ -38,6 +38,7 @@ public: | |||
|     virtual void StopBlocking(void) {} | ||||
|     virtual bool NeedReply(void) { return false; } | ||||
|     virtual void ReplyFinished(const bool result) {} | ||||
|     virtual bool IfTimeout(const unsigned int &integrationTimeMs) { return false; } | ||||
| 
 | ||||
| public: | ||||
|     /**
 | ||||
|  | @ -47,7 +48,7 @@ public: | |||
|     unsigned int mSerialNumber; | ||||
| }; | ||||
| template <typename T> | ||||
| class McuAsk : public VMcuAsk | ||||
| class McuAsk : virtual public VMcuAsk | ||||
| { | ||||
| 
 | ||||
| public: | ||||
|  | @ -57,11 +58,11 @@ public: | |||
| public: | ||||
|     T mDataReply; | ||||
| }; | ||||
| class McuMonitor | ||||
| class VMcuMonitor | ||||
| { | ||||
| public: | ||||
|     McuMonitor() = default; | ||||
|     virtual ~McuMonitor() = default; | ||||
|     VMcuMonitor() = default; | ||||
|     virtual ~VMcuMonitor() = default; | ||||
| }; | ||||
| class IMcuManager | ||||
| { | ||||
|  | @ -71,6 +72,10 @@ public: | |||
|     static std::shared_ptr<IMcuManager> &GetInstance(std::shared_ptr<IMcuManager> *impl = nullptr); | ||||
|     virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } | ||||
|     virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } | ||||
|     virtual const StatusCode SetMcuMonitor(std::shared_ptr<VMcuMonitor> &monitor) | ||||
|     { | ||||
|         return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); | ||||
|     } | ||||
|     virtual const StatusCode GetIpcMission(std::shared_ptr<VMcuAsk> ask) | ||||
|     { | ||||
|         return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); | ||||
|  |  | |||
|  | @ -15,6 +15,7 @@ | |||
| #include "McuDevice.h" | ||||
| #include "ILog.h" | ||||
| #include <string.h> | ||||
| constexpr int SLEEP_TIME_MS = 1000; | ||||
| /**
 | ||||
|  * @brief Do not use static decoration on this constant pointer, as external test code needs to reference it. | ||||
|  * | ||||
|  | @ -46,9 +47,11 @@ const StatusCode McuDevice::Init(void) | |||
|         LogError("IUartOpen failed.\n"); | ||||
|         return code; | ||||
|     } | ||||
|     auto recvThread = [](std::shared_ptr<McuDevice> device) { device->DeviceRecvThread(); }; | ||||
|     std::shared_ptr<McuDevice> device = std::dynamic_pointer_cast<McuDevice>(SharedFromThis()); | ||||
|     auto recvThread = [](std::shared_ptr<McuDevice> device) { device->DeviceRecvThread(); }; | ||||
|     mUartRecvThread = std::thread(recvThread, device); | ||||
|     auto mcuAskHandle = [](std::shared_ptr<McuDevice> device) { device->McuAskHandleThread(); }; | ||||
|     mMcuAskHandleThread = std::thread(mcuAskHandle, device); | ||||
|     return CreateStatusCode(STATUS_CODE_OK); | ||||
| } | ||||
| const StatusCode McuDevice::UnInit(void) | ||||
|  | @ -57,11 +60,15 @@ const StatusCode McuDevice::UnInit(void) | |||
|     if (mUartRecvThread.joinable()) { | ||||
|         mUartRecvThread.join(); | ||||
|     } | ||||
|     if (mMcuAskHandleThread.joinable()) { | ||||
|         mMcuAskHandleThread.join(); | ||||
|     } | ||||
|     if (nullptr != mUartDevice) { | ||||
| 
 | ||||
|         IUartDeviceFree(mUartDevice); | ||||
|     } | ||||
|     mUartDevice = nullptr; | ||||
|     mAllAsk.clear(); | ||||
|     return CreateStatusCode(STATUS_CODE_OK); | ||||
| } | ||||
| size_t McuDevice::WriteData(const void *buff, const size_t buffLength, std::shared_ptr<VProtocolContext> &context, | ||||
|  | @ -97,7 +104,9 @@ void McuDevice::GetIpcMissionReply(const unsigned int &serialNumber, const unsig | |||
|             realAsk->mDataReply = static_cast<IpcMission>(mission); | ||||
|             ask->ReplyFinished(true); | ||||
|         } | ||||
|         ask->ReplyFinished(false); | ||||
|         else { | ||||
|             ask->ReplyFinished(false); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| void McuDevice::DeviceRecvThread(void) | ||||
|  | @ -128,7 +137,13 @@ void McuDevice::DeviceRecvThread(void) | |||
|     } | ||||
|     free(keyHeadBuf); | ||||
| } | ||||
| 
 | ||||
| void McuDevice::McuAskHandleThread(void) | ||||
| { | ||||
|     while (mThreadRuning) { | ||||
|         TraverseCheckAllAsk(); | ||||
|         std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); | ||||
|     } | ||||
| } | ||||
| void McuDevice::DeviceRecvData(const char *keyHead, const size_t headLength) | ||||
| { | ||||
|     constexpr int RECV_TIMEOUT_MS = 1000; | ||||
|  | @ -166,10 +181,11 @@ void McuDevice::AddMcuAsk(std::shared_ptr<VMcuAsk> &ask) | |||
| { | ||||
|     // std::lock_guard<std::mutex> locker(mMutex);
 | ||||
|     if (ask->NeedReply() == true) { | ||||
|         LogInfo("AddMcuAsk push back one ask serial number = %u.\n", ask->mSerialNumber); | ||||
|         mAllAsk.push_back(ask); | ||||
|     } | ||||
| } | ||||
| void McuDevice::SearchMcuAsk(unsigned int serialNumber, std::shared_ptr<VMcuAsk> &ask) | ||||
| void McuDevice::SearchMcuAsk(const unsigned int &serialNumber, std::shared_ptr<VMcuAsk> &ask) | ||||
| { | ||||
|     std::lock_guard<std::mutex> locker(mMutex); | ||||
|     for (auto iter = mAllAsk.begin(); iter != mAllAsk.end(); ++iter) { | ||||
|  | @ -189,4 +205,22 @@ void McuDevice::DeleteMcuAsk(std::shared_ptr<VMcuAsk> &ask) | |||
| { | ||||
|     //
 | ||||
|     std::lock_guard<std::mutex> locker(mMutex); | ||||
| } | ||||
| void McuDevice::TraverseCheckAllAsk(void) | ||||
| { | ||||
|     std::lock_guard<std::mutex> locker(mMutex); | ||||
|     auto ifTimeout = [](std::shared_ptr<VMcuAsk> &ask) -> bool { | ||||
|         constexpr bool REMOVE_THE_ASK = true; | ||||
|         constexpr bool KEEP_THE_ASK = false; | ||||
|         if (!ask) { | ||||
|             return REMOVE_THE_ASK; | ||||
|         } | ||||
|         if (ask->IfTimeout(SLEEP_TIME_MS) == true) { | ||||
|             LogWarning("Mcu ask time out.\n"); | ||||
|             ask->ReplyFinished(false); | ||||
|             return REMOVE_THE_ASK; | ||||
|         } | ||||
|         return KEEP_THE_ASK; | ||||
|     }; | ||||
|     mAllAsk.remove_if(ifTimeout); | ||||
| } | ||||
|  | @ -37,17 +37,20 @@ public: | |||
| 
 | ||||
| public: | ||||
|     void DeviceRecvThread(void); | ||||
|     void McuAskHandleThread(void); | ||||
| 
 | ||||
| private: | ||||
|     void DeviceRecvData(const char *keyHead, const size_t headLength); | ||||
|     void AddMcuAsk(std::shared_ptr<VMcuAsk> &ask); | ||||
|     void SearchMcuAsk(unsigned int serialNumber, std::shared_ptr<VMcuAsk> &ask); | ||||
|     void SearchMcuAsk(const unsigned int &serialNumber, std::shared_ptr<VMcuAsk> &ask); | ||||
|     void DeleteMcuAsk(std::shared_ptr<VMcuAsk> &ask); | ||||
|     void TraverseCheckAllAsk(void); | ||||
| 
 | ||||
| private: | ||||
|     std::mutex mMutex; | ||||
|     void *mUartDevice; | ||||
|     std::thread mUartRecvThread; | ||||
|     std::thread mMcuAskHandleThread; | ||||
|     bool mThreadRuning; | ||||
|     std::list<std::shared_ptr<VMcuAsk>> mAllAsk; | ||||
| }; | ||||
|  |  | |||
|  | @ -10,6 +10,7 @@ include_directories( | |||
|     ${UTILS_SOURCE_PATH}/StatusCode/include | ||||
|     ${UTILS_SOURCE_PATH}/UartDevice/include | ||||
|     ${MIDDLEWARE_SOURCE_PATH}/McuManager/include | ||||
|     ${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include | ||||
|     ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include | ||||
|     ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include | ||||
|     ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include | ||||
|  | @ -39,7 +40,7 @@ endif() | |||
| 
 | ||||
| set(TARGET_NAME McuManagerTest) | ||||
| add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) | ||||
| target_link_libraries(${TARGET_NAME} McuManager McuManagerTestTool gtest gmock pthread) | ||||
| target_link_libraries(${TARGET_NAME} McuAskBase McuManager McuManagerTestTool gtest gmock pthread) | ||||
| if(${TEST_COVERAGE} MATCHES "true") | ||||
|     target_link_libraries(${TARGET_NAME} gcov) | ||||
| endif() | ||||
|  |  | |||
|  | @ -1,6 +1,7 @@ | |||
| #include "ILog.h" | ||||
| #include "IMcuManager.h" | ||||
| #include "LinuxApiMock.h" | ||||
| #include "McuAskBase.h" | ||||
| #include "McuManagerTestTool.h" | ||||
| #include <gmock/gmock.h> | ||||
| #include <gtest/gtest.h> | ||||
|  | @ -43,11 +44,22 @@ public: | |||
| // --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_GetIpcMissiony
 | ||||
| TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_GetIpcMissiony) | ||||
| { | ||||
|     class McuAskTest : public McuAsk<IpcMission> | ||||
|     class McuAskTest : public McuAsk<IpcMission>, public McuAskBase | ||||
|     { | ||||
|     public: | ||||
|         McuAskTest() = default; | ||||
|         McuAskTest() : McuAskBase(McuAskBlock::BLOCK, McuAskReply::NEED_REPLY) {} | ||||
|         virtual ~McuAskTest() = default; | ||||
|         void ReplyFinished(const bool result) override | ||||
|         { | ||||
|             //
 | ||||
|             McuAskBase::ReplyFinished(result); | ||||
|             if (result) { | ||||
|                 LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast<int>(mDataReply)); | ||||
|             } | ||||
|             else { | ||||
|                 LogError("Ask data falied.\n"); | ||||
|             } | ||||
|         } | ||||
|     }; | ||||
|     IMcuManager::GetInstance()->Init(); | ||||
|     std::shared_ptr<VMcuAsk> ask = std::make_shared<McuAskTest>(); | ||||
|  |  | |||
|  | @ -26,6 +26,9 @@ public: | |||
|     void Init(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart); | ||||
|     void UnInit(void); | ||||
| 
 | ||||
| private: | ||||
|     void IpcMissionProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd); | ||||
| 
 | ||||
| private: | ||||
|     static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const int event); | ||||
| }; | ||||
|  |  | |||
|  | @ -17,12 +17,20 @@ | |||
| #include <string.h> | ||||
| #include <thread> | ||||
| constexpr size_t PROTOCOL_DATA_KEY_HEAD_LENGTH = 10; | ||||
| const unsigned char ASK_IPC_MISSION[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x0C, 0x3D, 0x68}; | ||||
| const unsigned char REPLY_IPC_MISSION[] = { | ||||
|     0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x0D, 0x00, 0x3D, 0x68}; | ||||
| void McuProtocolTestTool::Init(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart) | ||||
| { | ||||
|     int uartFd = GetDeviceMockFd(uart); | ||||
|     IpcMissionProtocolInit(mock, uartFd); | ||||
| } | ||||
| void McuProtocolTestTool::UnInit(void) | ||||
| { | ||||
|     //
 | ||||
| } | ||||
| void McuProtocolTestTool::IpcMissionProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd) | ||||
| { | ||||
|     const unsigned char ASK_IPC_MISSION[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x0C, 0x3D, 0x68}; | ||||
|     const unsigned char REPLY_IPC_MISSION[] = { | ||||
|         0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x0D, 0x01, 0x3D, 0x68}; | ||||
|     static size_t WRITE_COUNT = -1; | ||||
|     auto api_write = [=, &mock](int fd, const void *buf, size_t count) { | ||||
|         McuProtocolTestTool::PrintHexadecimalData(buf, count, WRITE_PRINT); | ||||
|  | @ -61,10 +69,6 @@ void McuProtocolTestTool::Init(std::shared_ptr<LinuxTest> &mock, const UartInfo | |||
|         .WillRepeatedly( | ||||
|             DoAll(SaveArg<2>(&WRITE_COUNT), WithArgs<0, 1, 2>(Invoke(api_write)), ReturnPointee(&WRITE_COUNT))); | ||||
| } | ||||
| void McuProtocolTestTool::UnInit(void) | ||||
| { | ||||
|     //
 | ||||
| } | ||||
| void McuProtocolTestTool::PrintHexadecimalData(const void *buf, const size_t &bufLength, const int event) | ||||
| { | ||||
|     if (WRITE_PRINT == event) { | ||||
|  |  | |||
|  | @ -14,6 +14,7 @@ | |||
|  */ | ||||
| #include "LittleEndianHandle.h" | ||||
| #include "ILog.h" | ||||
| #include "ModBusCRC16.h" | ||||
| #include <netinet/in.h> | ||||
| #include <string.h> | ||||
| LittleEndianHandle::LittleEndianHandle(const std::shared_ptr<VProtocolParam> ¶m) : ProtocolHandle(param) | ||||
|  | @ -39,4 +40,12 @@ void LittleEndianHandle::HostByteOrderConversion(ProtocolPacket &packet) | |||
|     packet.mLength = ntohs(packet.mLength); | ||||
|     packet.mCheckCode = ntohs(packet.mCheckCode); | ||||
|     packet.mSerialNumber = ntohl(packet.mSerialNumber); | ||||
| } | ||||
| bool LittleEndianHandle::CheckoutTheCheckCode(const ProtocolPacket &packet) | ||||
| { | ||||
|     short code = Calculate_Check_Sum(mProtocolData, mProtocolDataLength - sizeof(short)); | ||||
|     if (code == packet.mCheckCode) { | ||||
|         return true; | ||||
|     } | ||||
|     return false; | ||||
| } | ||||
|  | @ -25,5 +25,6 @@ public: | |||
| private: | ||||
|     void BigEndianConversion(ProtocolPacket &packet) override; | ||||
|     void HostByteOrderConversion(ProtocolPacket &packet) override; | ||||
|     bool CheckoutTheCheckCode(const ProtocolPacket &packet) override; | ||||
| }; | ||||
| #endif | ||||
|  | @ -16,8 +16,6 @@ | |||
| #include "ILog.h" | ||||
| #include "ProtocolHandle.h" | ||||
| #include <string.h> | ||||
| constexpr int THREAD_SHARING = 0; | ||||
| constexpr int INITIAL_VALUE_OF_SEMAPHORE = 0; | ||||
| std::shared_ptr<VProtocolRecv> &VProtocolRecv::GetInstance(std::shared_ptr<VProtocolRecv> *impl) | ||||
| { | ||||
|     static auto instance = std::make_shared<VProtocolRecv>(); | ||||
|  | @ -28,6 +26,8 @@ std::shared_ptr<VProtocolRecv> &VProtocolRecv::GetInstance(std::shared_ptr<VProt | |||
| } | ||||
| const StatusCode McuProtocol::Init(void) | ||||
| { | ||||
|     constexpr int THREAD_SHARING = 0; | ||||
|     constexpr int INITIAL_VALUE_OF_SEMAPHORE = 0; | ||||
|     sem_init(&mSem, THREAD_SHARING, INITIAL_VALUE_OF_SEMAPHORE); | ||||
|     auto dataHandleThread = [](std::shared_ptr<McuProtocol> handle) { handle->DataHandleThread(); }; | ||||
|     std::shared_ptr<McuProtocol> handle = std::dynamic_pointer_cast<McuProtocol>(SharedFromThis()); | ||||
|  |  | |||
|  | @ -75,6 +75,7 @@ void ProtocolHandle::MakeAskIpcMissionPacket(const std::shared_ptr<VProtocolPara | |||
|     packet.mLength = dataLength; | ||||
|     packet.mCheckCode = Calculate_Check_Sum(mProtocolData, dataLength - sizeof(short)); | ||||
|     packet.mSerialNumber = mSerialNumber; | ||||
|     mProtocolSerialNumber = packet.mSerialNumber; | ||||
|     mSerialNumber++; | ||||
|     BigEndianConversion(packet); | ||||
|     memcpy(mProtocolData, &packet, KEY_HEAD_LENGTH); | ||||
|  | @ -89,6 +90,10 @@ void ProtocolHandle::AnalyzeProtocolPacket(void) | |||
|            (unsigned char *)mProtocolData + mProtocolDataLength - sizeof(unsigned short), | ||||
|            sizeof(unsigned short)); | ||||
|     HostByteOrderConversion(packet); | ||||
|     if (CheckoutTheCheckCode(packet) == false) { | ||||
|         LogError("CheckoutTheCheckCode failed.\n"); | ||||
|         return; | ||||
|     } | ||||
|     LogInfo("AnalyzeProtocolPacket, command = 0x%04X\n", packet.mCommand); | ||||
|     PROTOCOL_COMMAND command = static_cast<PROTOCOL_COMMAND>(packet.mCommand); | ||||
|     std::map<PROTOCOL_COMMAND, AnalyzePacketFunc>::iterator iter; | ||||
|  | @ -108,7 +113,20 @@ void ProtocolHandle::AnalyzeReplyIpcMissionPacket(const ProtocolPacket &packet) | |||
|     unsigned char ipcMission = UNKNOWN_MISSION; | ||||
|     ipcMission = mProtocolData[PROTOCOL_DATA_START_ADDRESS]; | ||||
|     LogInfo("ipc mission = 0x%02X\n", ipcMission); | ||||
|     VProtocolRecv::GetInstance()->GetIpcMissionReply(packet.mSerialNumber, ipcMission); | ||||
|     /**
 | ||||
|      * @brief unsigned int number = packet.mSerialNumber This line of code is for | ||||
|      * avoiding errors: runtime error: reference binding to misaligned address xxxx | ||||
|      */ | ||||
|     mProtocolSerialNumber = packet.mSerialNumber; | ||||
|     VProtocolRecv::GetInstance()->GetIpcMissionReply(mProtocolSerialNumber, ipcMission); | ||||
| } | ||||
| bool ProtocolHandle::CheckoutTheCheckCode(const ProtocolPacket &packet) | ||||
| { | ||||
|     short code = Calculate_Check_Sum(mProtocolData, mProtocolDataLength - sizeof(short)); | ||||
|     if (code == packet.mCheckCode) { | ||||
|         return true; | ||||
|     } | ||||
|     return false; | ||||
| } | ||||
| std::shared_ptr<ProtocolHandle> ProtocolHandle::CreateProtocolData(const std::shared_ptr<VProtocolParam> ¶m) | ||||
| { | ||||
|  |  | |||
|  | @ -90,8 +90,9 @@ private: | |||
| private: | ||||
|     virtual void BigEndianConversion(ProtocolPacket &packet) {} | ||||
|     virtual void HostByteOrderConversion(ProtocolPacket &packet) {} | ||||
|     virtual bool CheckoutTheCheckCode(const ProtocolPacket &packet); | ||||
| 
 | ||||
| private: | ||||
| protected: | ||||
|     std::shared_ptr<VProtocolParam> mParam; | ||||
|     std::map<PROTOCOL_COMMAND, MakePacketFunc> mMakePacketFunc; | ||||
|     std::map<PROTOCOL_COMMAND, AnalyzePacketFunc> mAnalyzePacketFunc; | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Fancy code
						Fancy code