From b190fcead7c435a638b34deb216fa33dd87597c1 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Mon, 12 Feb 2024 23:46:11 -0800 Subject: [PATCH] Add:contor infrared light and get photosensitivity value. --- doc/design.md | 6 +- middleware/McuManager/include/IMcuManager.h | 8 ++ middleware/McuManager/src/IMcuManager.cpp | 8 ++ middleware/McuManager/src/McuManagerImpl.cpp | 10 ++ middleware/McuManager/src/McuManagerImpl.h | 2 + .../src_mock/McuManager_Mock_Test.cpp | 87 ++++++++++++++ .../tool/include/McuProtocolTestTool.h | 12 +- .../tool/src/McuProtocolTestTool.cpp | 111 ++++++++++++++++++ utils/McuProtocol/include/McuProtocol.h | 4 + utils/McuProtocol/src/McuProtocol.cpp | 17 +++ utils/McuProtocol/src/ProtocolHandle.cpp | 16 ++- utils/McuProtocol/src/ProtocolHandle.h | 6 + 12 files changed, 282 insertions(+), 5 deletions(-) diff --git a/doc/design.md b/doc/design.md index b167ef4..67e770a 100644 --- a/doc/design.md +++ b/doc/design.md @@ -537,12 +537,14 @@ end    流水号用于强绑定问答型协议的发送数据和回复数据,回复者原数据回传即可。例如: ``` -unsigned char ASK_IPC_MISSION[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x0C, 0x71, 0x88}; -unsigned char REPLY_IPC_MISSION[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x0D, 0x01, 0xAA, 0x89}; +unsigned char ASK_IPC_MISSION[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x81, 0x01, 0x00, 0x0C, 0x71, 0x88}; +unsigned char REPLY_IPC_MISSION[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x0D, 0x01, 0xAA, 0x89}; ```    流水号管理发送方和接收方互相独立,各自往外发的协议中流水号存在重复的可能,接收方直接复制回传即可。 +   流水号必须大于等于1,0用于代码初始化值,代码意义上表示无效的流水号。 + **校验码算法**    校验码算法使用ModBus CRC16方法计算。 diff --git a/middleware/McuManager/include/IMcuManager.h b/middleware/McuManager/include/IMcuManager.h index c35efb9..22af235 100644 --- a/middleware/McuManager/include/IMcuManager.h +++ b/middleware/McuManager/include/IMcuManager.h @@ -34,6 +34,12 @@ enum class ASK_RESULT TIMEOUT, END }; +enum class ControlLight +{ + TRUN_OFF = 0, + TRUN_ON, + END +}; typedef struct mcu_ask_date_time { const unsigned short mYear; @@ -99,5 +105,7 @@ public: const unsigned char &min, const unsigned char &second); virtual const StatusCode SetDateTime(std::shared_ptr &ask, const McuAskDateTime &value); virtual const StatusCode SetPirSensitivity(std::shared_ptr &ask, const unsigned char &sensitivity); + virtual const StatusCode ContorlInfraredLight(std::shared_ptr &ask, const ControlLight &control); + virtual const StatusCode GetPhotosensitivityValue(std::shared_ptr &ask); }; #endif \ No newline at end of file diff --git a/middleware/McuManager/src/IMcuManager.cpp b/middleware/McuManager/src/IMcuManager.cpp index 6b508d0..f259172 100644 --- a/middleware/McuManager/src/IMcuManager.cpp +++ b/middleware/McuManager/src/IMcuManager.cpp @@ -56,6 +56,14 @@ const StatusCode IMcuManager::SetDateTime(std::shared_ptr &ask, const M return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } const StatusCode IMcuManager::SetPirSensitivity(std::shared_ptr &ask, const unsigned char &sensitivity) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +const StatusCode IMcuManager::ContorlInfraredLight(std::shared_ptr &ask, const ControlLight &control) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +const StatusCode IMcuManager::GetPhotosensitivityValue(std::shared_ptr &ask) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } \ No newline at end of file diff --git a/middleware/McuManager/src/McuManagerImpl.cpp b/middleware/McuManager/src/McuManagerImpl.cpp index f9af197..fe3ef28 100644 --- a/middleware/McuManager/src/McuManagerImpl.cpp +++ b/middleware/McuManager/src/McuManagerImpl.cpp @@ -65,6 +65,16 @@ const StatusCode McuManagerImpl::SetPirSensitivity(std::shared_ptr &ask std::shared_ptr context = std::make_shared>>(ask); return McuProtocol::SetPirSensitivity(sensitivity, context); } +const StatusCode McuManagerImpl::ContorlInfraredLight(std::shared_ptr &ask, const ControlLight &control) +{ + std::shared_ptr context = std::make_shared>>(ask); + return McuProtocol::ContorlInfraredLight(static_cast(control), context); +} +const StatusCode McuManagerImpl::GetPhotosensitivityValue(std::shared_ptr &ask) +{ + std::shared_ptr context = std::make_shared>>(ask); + return McuProtocol::GetPhotosensitivityValue(context); +} std::shared_ptr McuManagerImpl::GetMcuMonitor(void) { auto monitor = mMonitor.lock(); diff --git a/middleware/McuManager/src/McuManagerImpl.h b/middleware/McuManager/src/McuManagerImpl.h index 3f77b0b..2133177 100644 --- a/middleware/McuManager/src/McuManagerImpl.h +++ b/middleware/McuManager/src/McuManagerImpl.h @@ -33,6 +33,8 @@ public: const unsigned char &min, const unsigned char &second) override; const StatusCode SetDateTime(std::shared_ptr &ask, const McuAskDateTime &value) override; const StatusCode SetPirSensitivity(std::shared_ptr &ask, const unsigned char &sensitivity) override; + const StatusCode ContorlInfraredLight(std::shared_ptr &ask, const ControlLight &control) override; + const StatusCode GetPhotosensitivityValue(std::shared_ptr &ask) override; private: std::shared_ptr GetMcuMonitor(void); diff --git a/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp index 9b72408..7b747c7 100644 --- a/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp +++ b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp @@ -283,6 +283,68 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_SetPirSensitivity) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_ContorlInfraredLight +TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_ContorlInfraredLight) +{ + /** + * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the + * abstract interface. + */ + class McuAskTest : public McuAsk, public McuAskBase + { + public: + McuAskTest() : McuAskBase(McuAskBlock::NOT_BLOCK, McuAskReply::NEED_REPLY) {} // using McuAskBlock::NOT_BLOCK + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBase::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + IMcuManager::GetInstance()->ContorlInfraredLight(ask, ControlLight::TRUN_ON); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_GetPhotosensitivityValue +TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_GetPhotosensitivityValue) +{ + /** + * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the + * abstract interface. + */ + class McuAskTest : public McuAsk, public McuAskBase + { + public: + McuAskTest() : McuAskBase(McuAskBlock::NOT_BLOCK, McuAskReply::NEED_REPLY) {} // using McuAskBlock::NOT_BLOCK + virtual ~McuAskTest() = default; + void ReplyFinished(const bool result) override + { + McuAskBase::ReplyFinished(result); + if (result) { + LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast(mDataReply)); + // Do something here. + } + else { + LogError("Ask data falied.\n"); + } + } + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + IMcuManager::GetInstance()->GetPhotosensitivityValue(ask); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest // --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_GetIpcMission TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission) { @@ -343,6 +405,31 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission2) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission +TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission) +{ + constexpr unsigned int TEST_SERIAL_NUMBER = 99; + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvIpcMissionEvent(std::shared_ptr &recv, const IpcMission &mission) override + { + LogInfo("RecvIpcMissionEvent\n"); + std::shared_ptr> ask = std::dynamic_pointer_cast>(recv); + ask->mDataReply = ASK_RESULT::SUCCEED; + recv->ReplyFinished(true); + } + }; + IMcuManager::GetInstance()->Init(); + OtherSideAskIpcMission(mLinuxTest, TEST_SERIAL_NUMBER); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest // --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_CutOffPowerSupply TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_CutOffPowerSupply) { diff --git a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h index 39d9515..5c566be 100644 --- a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h +++ b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h @@ -52,6 +52,12 @@ private: size_t count); void SetPirSensitivityProtocolInit(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + bool ContorlInfraredLightHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + void ContorlInfraredLightInit(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + bool GetPhotosensitivityValueHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, + size_t count); + void GetPhotosensitivityValueInit(std::shared_ptr &mock, const int &uartFd, const void *buf, + size_t count); void LockProtocolHandle(void); void UnlockProtocolHandle(void); void UnlockThread(void); @@ -67,8 +73,10 @@ private: static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const int event); private: - // virtual void MonitorWriteProtocolData(const short &head, const unsigned int &serialNumber, const short &command, - // const void *data, const short &packetLength) + // virtual void MonitorWriteProtocolData(const short &head, const unsigned int + // &serialNumber, const short &command, + // const void *data, const short + // &packetLength) // { // } diff --git a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp index ae7875d..e2a85ea 100644 --- a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp +++ b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp @@ -54,6 +54,13 @@ unsigned char ASK_SET_PIR_SENSITIVITY_X[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x81, 0x08, 0x00, 0x0D, 0x09, 0xFF, 0xFF}; unsigned char REPLY_SET_PIR_SENSITIVITY_X[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x0D, 0x01, 0xFF, 0xFF}; +unsigned char ASK_CONTORL_INFRARED_LIGHT_X[] = { + 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x81, 0x0A, 0x00, 0x0D, 0x01, 0xFF, 0xFF}; +unsigned char REPLY_ASK_CONTORL_INFRARED_LIGHT_X[] = { + 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0A, 0x00, 0x0D, 0x01, 0xFF, 0xFF}; +unsigned char ASK_GET_PHOTOSENSITIVITY_X[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x81, 0x0B, 0x00, 0x0C, 0xFF, 0xFF}; +unsigned char REPLY_ASK_GET_PHOTOSENSITIVITY_X[] = { + 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0B, 0x00, 0x0D, 0x0A, 0xFF, 0xFF}; unsigned char REPLY_OTHER_SIDE_ASK_SEND_IPC_MISSION_X[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x01, 0x00, 0x0D, 0x0A, 0xFF, 0xFF}; unsigned char OTHER_SIDE_ASK_SEND_IPC_MISSION_X[] = { @@ -72,6 +79,8 @@ McuProtocolTestTool::McuProtocolTestTool() mProtocolHandle.push_back(std::bind(&McuProtocolTestTool::FeedingCycleProtocolHandle, this, _1, _2, _3, _4)); mProtocolHandle.push_back(std::bind(&McuProtocolTestTool::SetDataTimeProtocolHandle, this, _1, _2, _3, _4)); mProtocolHandle.push_back(std::bind(&McuProtocolTestTool::SetPirSensitivityProtocolHandle, this, _1, _2, _3, _4)); + mProtocolHandle.push_back(std::bind(&McuProtocolTestTool::ContorlInfraredLightHandle, this, _1, _2, _3, _4)); + mProtocolHandle.push_back(std::bind(&McuProtocolTestTool::GetPhotosensitivityValueHandle, this, _1, _2, _3, _4)); } void McuProtocolTestTool::Init(std::shared_ptr &mock, const UartInfo &uart) { @@ -97,6 +106,7 @@ void McuProtocolTestTool::Init(std::shared_ptr &mock, const UartInfo return; } } + LogWarning("Can't find protocol handle function.\n"); }; EXPECT_CALL(*mock.get(), fx_write(uartFd, _, _)) .WillRepeatedly( @@ -441,6 +451,107 @@ void McuProtocolTestTool::SetPirSensitivityProtocolInit(std::shared_ptr(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH))) .WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING))); } +bool McuProtocolTestTool::ContorlInfraredLightHandle(std::shared_ptr &mock, const int &uartFd, + const void *buf, size_t count) +{ + if (sizeof(ASK_CONTORL_INFRARED_LIGHT_X) == count && memcmp(ASK_CONTORL_INFRARED_LIGHT_X + PROTOCOL_COMMAND_OFFSET, + (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, + PROTOCOL_COMMAND_LENGTH) == 0) { + short replyCheckCode = + calculate_check_sum(REPLY_ASK_CONTORL_INFRARED_LIGHT_X, + sizeof(REPLY_ASK_CONTORL_INFRARED_LIGHT_X) - PROTOCOL_CHECK_CODE_LENGTH); + replyCheckCode = htons(replyCheckCode); + LogInfo("Set ASK_CONTORL_INFRARED_LIGHT_X, reply data check code = 0x%x\n", replyCheckCode); + short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH); + // askCheckCode = htons(askCheckCode); + int result = memcmp( + (unsigned char *)buf + count - PROTOCOL_CHECK_CODE_LENGTH, &askCheckCode, PROTOCOL_CHECK_CODE_LENGTH); + EXPECT_EQ(result, 0) << "ask protocol data errer, check code isn't right."; + auto handle = [=, &mock](McuProtocolTestTool *testTool) { + testTool->ContorlInfraredLightInit(mock, uartFd, buf, count); + }; + if (mLockThread.joinable()) { + mLockThread.join(); + } + mLockThread = std::thread(handle, this); + return PROTOCOL_HANDLED; + } + return PROTOCOL_NOT_HANDLED; +} +void McuProtocolTestTool::ContorlInfraredLightInit(std::shared_ptr &mock, const int &uartFd, const void *buf, + size_t count) +{ + LockProtocolHandle(); + memcpy(REPLY_ASK_CONTORL_INFRARED_LIGHT_X + PROTOCOL_SERIAL_NUMBER_OFFSET, + (unsigned char *)buf + PROTOCOL_SERIAL_NUMBER_OFFSET, + PROTOCOL_SERIAL_NUMBER_LENGTH); + ResetCheckCode(REPLY_ASK_CONTORL_INFRARED_LIGHT_X, sizeof(REPLY_ASK_CONTORL_INFRARED_LIGHT_X)); + ReplySelectSucceed(mock, uartFd); + constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_ASK_CONTORL_INFRARED_LIGHT_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH; + auto apiReadKeyHead = [=](int fd, void *buf, size_t count) { + memcpy(buf, REPLY_ASK_CONTORL_INFRARED_LIGHT_X, PROTOCOL_DATA_KEY_HEAD_LENGTH); + McuProtocolTestTool::PrintHexadecimalData(buf, PROTOCOL_DATA_KEY_HEAD_LENGTH, READ_PRINT); + }; + auto apiReadLeftData = [=](int fd, void *buf, size_t count) { + memcpy(buf, REPLY_ASK_CONTORL_INFRARED_LIGHT_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH); + McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, READ_PRINT); + UnlockProtocolHandle(); + }; + EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _)) + .WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH))) + .WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH))) + .WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING))); +} +bool McuProtocolTestTool::GetPhotosensitivityValueHandle(std::shared_ptr &mock, const int &uartFd, + const void *buf, size_t count) +{ + if (sizeof(ASK_GET_PHOTOSENSITIVITY_X) == count && memcmp(ASK_GET_PHOTOSENSITIVITY_X + PROTOCOL_COMMAND_OFFSET, + (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, + PROTOCOL_COMMAND_LENGTH) == 0) { + short replyCheckCode = calculate_check_sum( + REPLY_ASK_GET_PHOTOSENSITIVITY_X, sizeof(REPLY_ASK_GET_PHOTOSENSITIVITY_X) - PROTOCOL_CHECK_CODE_LENGTH); + replyCheckCode = htons(replyCheckCode); + LogInfo("Set ASK_GET_PHOTOSENSITIVITY_X, reply data check code = 0x%x\n", replyCheckCode); + short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH); + // askCheckCode = htons(askCheckCode); + int result = memcmp( + (unsigned char *)buf + count - PROTOCOL_CHECK_CODE_LENGTH, &askCheckCode, PROTOCOL_CHECK_CODE_LENGTH); + EXPECT_EQ(result, 0) << "ask protocol data errer, check code isn't right."; + auto handle = [=, &mock](McuProtocolTestTool *testTool) { + testTool->GetPhotosensitivityValueInit(mock, uartFd, buf, count); + }; + if (mLockThread.joinable()) { + mLockThread.join(); + } + mLockThread = std::thread(handle, this); + return PROTOCOL_HANDLED; + } + return PROTOCOL_NOT_HANDLED; +} +void McuProtocolTestTool::GetPhotosensitivityValueInit(std::shared_ptr &mock, const int &uartFd, + const void *buf, size_t count) +{ + LockProtocolHandle(); + memcpy(REPLY_ASK_GET_PHOTOSENSITIVITY_X + PROTOCOL_SERIAL_NUMBER_OFFSET, + (unsigned char *)buf + PROTOCOL_SERIAL_NUMBER_OFFSET, + PROTOCOL_SERIAL_NUMBER_LENGTH); + ResetCheckCode(REPLY_ASK_GET_PHOTOSENSITIVITY_X, sizeof(REPLY_ASK_GET_PHOTOSENSITIVITY_X)); + ReplySelectSucceed(mock, uartFd); + constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_ASK_GET_PHOTOSENSITIVITY_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH; + auto apiReadKeyHead = [=](int fd, void *buf, size_t count) { + memcpy(buf, REPLY_ASK_GET_PHOTOSENSITIVITY_X, PROTOCOL_DATA_KEY_HEAD_LENGTH); + McuProtocolTestTool::PrintHexadecimalData(buf, PROTOCOL_DATA_KEY_HEAD_LENGTH, READ_PRINT); + }; + auto apiReadLeftData = [=](int fd, void *buf, size_t count) { + memcpy(buf, REPLY_ASK_GET_PHOTOSENSITIVITY_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH); + McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, READ_PRINT); + UnlockProtocolHandle(); + }; + EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _)) + .WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH))) + .WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH))) + .WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING))); +} void McuProtocolTestTool::UnlockProtocolHandle(void) { auto unlockThread = [=](McuProtocolTestTool *testTool) { testTool->UnlockThread(); }; diff --git a/utils/McuProtocol/include/McuProtocol.h b/utils/McuProtocol/include/McuProtocol.h index 51e4d27..87bee72 100644 --- a/utils/McuProtocol/include/McuProtocol.h +++ b/utils/McuProtocol/include/McuProtocol.h @@ -31,6 +31,8 @@ class VProtocolContext public: VProtocolContext() = default; virtual ~VProtocolContext() = default; + unsigned int mSerialNumber; + short mCheckCode; // TODO: Need to improve }; template class ProtocolContext : public VProtocolContext @@ -122,6 +124,8 @@ protected: const unsigned char &hour, const unsigned char &min, const unsigned char &second, std::shared_ptr &context); const StatusCode SetPirSensitivity(const unsigned char &sensitivity, std::shared_ptr &context); + const StatusCode ContorlInfraredLight(const unsigned char &control, std::shared_ptr &context); + const StatusCode GetPhotosensitivityValue(std::shared_ptr &context); void DataHandleThread(void); protected: diff --git a/utils/McuProtocol/src/McuProtocol.cpp b/utils/McuProtocol/src/McuProtocol.cpp index 41b89ee..f3a0a9a 100644 --- a/utils/McuProtocol/src/McuProtocol.cpp +++ b/utils/McuProtocol/src/McuProtocol.cpp @@ -109,6 +109,23 @@ const StatusCode McuProtocol::SetPirSensitivity(const unsigned char &sensitivity return WriteProtocolData( handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), context, handle->GetSerialNumber()); } +const StatusCode McuProtocol::ContorlInfraredLight(const unsigned char &control, + std::shared_ptr &context) +{ + std::shared_ptr param = + std::make_shared>(PROTOCOL_COMMAND::ASK_CONTORL_INFRARED_LIGHT, control); + std::shared_ptr handle = ProtocolHandle::CreateProtocolData(param); + return WriteProtocolData( + handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), context, handle->GetSerialNumber()); +} +const StatusCode McuProtocol::GetPhotosensitivityValue(std::shared_ptr &context) +{ + std::shared_ptr param = + std::make_shared(PROTOCOL_COMMAND::ASK_GET_PHOTOSENSITIVITY); + std::shared_ptr handle = ProtocolHandle::CreateProtocolData(param); + return WriteProtocolData( + handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), context, handle->GetSerialNumber()); +} void McuProtocol::DataHandleThread(void) { mThreadRuning = true; diff --git a/utils/McuProtocol/src/ProtocolHandle.cpp b/utils/McuProtocol/src/ProtocolHandle.cpp index ce455f4..3897650 100644 --- a/utils/McuProtocol/src/ProtocolHandle.cpp +++ b/utils/McuProtocol/src/ProtocolHandle.cpp @@ -35,6 +35,10 @@ ProtocolHandle::ProtocolHandle(const std::shared_ptr ¶m) : m mMakePacketFunc[ASK_SET_FEEDING_CYCLE] = std::bind(&ProtocolHandle::MakeAskSetFeedingCyclePacket, this, _1); mMakePacketFunc[ASK_SET_DATE_TIME] = std::bind(&ProtocolHandle::MakeAskSetDateTimePacket, this, _1); mMakePacketFunc[ASK_SET_PIR_SENSITIVITY] = std::bind(&ProtocolHandle::MakeAskSetPirSensitivityPacket, this, _1); + mMakePacketFunc[ASK_CONTORL_INFRARED_LIGHT] = + std::bind(&ProtocolHandle::MakeAskControlInfraredLightPacket, this, _1); + mMakePacketFunc[ASK_GET_PHOTOSENSITIVITY] = std::bind(&ProtocolHandle::MakeAskGetPhotosensitivityPacket, this, _1); + /**************************************************************************************************************************/ mMakePacketFunc[REPLY_OTHER_SIDE_ASK_SEND_IPC_MISSION] = std::bind(&ProtocolHandle::MakeReplyOtherSideSendIpcMissionPacket, this, _1); } @@ -49,6 +53,9 @@ ProtocolHandle::ProtocolHandle(const void *data, const size_t &length) mAnalyzePacketFunc[REPLY_SET_FEEDING_CYCLE] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1); mAnalyzePacketFunc[REPLY_SET_DATE_TIME] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1); mAnalyzePacketFunc[REPLY_SET_PIR_SENSITIVITY] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1); + mAnalyzePacketFunc[REPLY_CONTORL_INFRARED_LIGHT] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1); + mAnalyzePacketFunc[REPLY_GET_PHOTOSENSITIVITY] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1); + /**************************************************************************************************************************/ mAnalyzePacketFunc[OTHER_SIDE_ASK_SEND_IPC_MISSION] = std::bind(&ProtocolHandle::AnalyzeOtherSideSendIpcMissionPacket, this, _1); } @@ -157,6 +164,14 @@ void ProtocolHandle::MakeReplyOtherSideSendIpcMissionPacket(const std::shared_pt { MakeProtocolData(param); } +void ProtocolHandle::MakeAskControlInfraredLightPacket(const std::shared_ptr ¶m) +{ + MakeProtocolData(param); +} +void ProtocolHandle::MakeAskGetPhotosensitivityPacket(const std::shared_ptr ¶m) +{ + MakeNoUserDataPacket(param); +} void ProtocolHandle::AnalyzeProtocolPacket(void) { ProtocolPacket packet = {0}; @@ -165,7 +180,6 @@ void ProtocolHandle::AnalyzeProtocolPacket(void) (unsigned char *)mProtocolData + mProtocolDataLength - sizeof(unsigned short), sizeof(unsigned short)); HostByteOrderConversion(packet); - PrintHexadecimalData(&packet, sizeof(ProtocolPacket), "HostByteOrderConversion="); if (CheckoutTheCheckCode(packet) == false) { LogError("CheckoutTheCheckCode failed.\n"); return; diff --git a/utils/McuProtocol/src/ProtocolHandle.h b/utils/McuProtocol/src/ProtocolHandle.h index 5cb1fac..8a1c38a 100644 --- a/utils/McuProtocol/src/ProtocolHandle.h +++ b/utils/McuProtocol/src/ProtocolHandle.h @@ -49,6 +49,10 @@ enum PROTOCOL_COMMAND REPLY_SET_DATE_TIME = 0x0107, ASK_SET_PIR_SENSITIVITY = 0x8108, REPLY_SET_PIR_SENSITIVITY = 0x0108, + ASK_CONTORL_INFRARED_LIGHT = 0x810A, + REPLY_CONTORL_INFRARED_LIGHT = 0x010A, + ASK_GET_PHOTOSENSITIVITY = 0x810B, + REPLY_GET_PHOTOSENSITIVITY = 0x010B, /** * @brief The protocol starting from here is a request sent from the other end. * @@ -141,6 +145,8 @@ private: void MakeAskSetFeedingCyclePacket(const std::shared_ptr ¶m); void MakeAskSetDateTimePacket(const std::shared_ptr ¶m); void MakeAskSetPirSensitivityPacket(const std::shared_ptr ¶m); + void MakeAskControlInfraredLightPacket(const std::shared_ptr ¶m); + void MakeAskGetPhotosensitivityPacket(const std::shared_ptr ¶m); void MakeReplyOtherSideSendIpcMissionPacket(const std::shared_ptr ¶m); template void MakeProtocolData(const std::shared_ptr ¶m)