From fdbe733bc7e4a8768e79a7af5d159f7217bf5303 Mon Sep 17 00:00:00 2001
From: Fancy code <258828110.@qq.com>
Date: Wed, 21 Feb 2024 02:06:27 -0800
Subject: [PATCH] Add:LedManager code.
---
doc/design.md | 5 +-
.../DeviceManager/src/DeviceManager.cpp | 6 +-
middleware/DeviceManager/src/LedManager.cpp | 2 +-
test/hal/tool/include/HalTestTool.h | 12 ++-
test/hal/tool/src/HalTestTool.cpp | 90 +++++++++++++++++--
test/hal/tool/src/LedControlMock.cpp | 25 ++++++
test/hal/tool/src/LedControlMock.h | 9 ++
test/middleware/DeviceManager/CMakeLists.txt | 1 +
.../DeviceManager/src/DeviceManager_Test.cpp | 23 +++--
.../DeviceManager/tool/CMakeLists.txt | 3 +-
.../tool/include/DeviceManagerTestTool.h | 6 ++
.../tool/src/DeviceManagerTestTool.cpp | 11 +++
.../tool/src/SingleControlMock.cpp | 32 +++++++
.../tool/src/SingleControlMock.h | 39 ++++++++
utils/LedControl/include/LedControl.h | 5 +-
utils/LedControl/src/LedControl.cpp | 24 +++++
16 files changed, 270 insertions(+), 23 deletions(-)
create mode 100644 test/middleware/DeviceManager/tool/src/SingleControlMock.cpp
create mode 100644 test/middleware/DeviceManager/tool/src/SingleControlMock.h
diff --git a/doc/design.md b/doc/design.md
index 67e770a..a6fad10 100644
--- a/doc/design.md
+++ b/doc/design.md
@@ -545,6 +545,9 @@ unsigned char REPLY_IPC_MISSION[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x01, 0
流水号必须大于等于1,0用于代码初始化值,代码意义上表示无效的流水号。
+**协议收发匹配逻辑**
+ 协议发送时,附带自管理的流水号,对端回复时,根据流水号绑定发送的协议,为了避免对端回复时,流水号错误导致的业务逻辑错乱,本端除了匹配流水号,还需要根据协议匹配回复的命令字,保证回复内容的有效匹配。如果流水号和命令字无法同时匹配,该回复的数据包被丢弃。
+
**校验码算法**
校验码算法使用ModBus CRC16方法计算。
@@ -644,7 +647,7 @@ unsigned short calculate_check_sum(const unsigned char* pData, unsigned short le
| 命令字 | CPU | MCU | 数据段 | 协议解析 | 备注 |
|----|----|----|----|----|----|
| 0x8101 | ask | - | - | 获取启动模式 | - |
-| 0x0101 | - | reply/ask | Data[0]:启动模式
0x01:PIR启动
0x02:TEST启动
0x03:连拍启动
0x04:PIR延时启动
0x05:定时(间隔一定时间)启动 | 回复启动模式 | - |
+| 0x0101 | - | reply | Data[0]:启动模式
0x01:PIR启动
0x02:TEST启动
0x03:连拍启动
0x04:PIR延时启动
0x05:定时(间隔一定时间)启动 | 回复启动模式 | - |
| 0x8102 | ask | - | - | 断电关机 | - |
| 0x8103 | ask | - | - | 喂狗 | - |
| 0x8104 | ask | - | Data[0]:Hour
0-23
Data[1]:Min
0-59
Data[2]:Sec
0-59 | 开启狗/设置喂狗周期 | - |
diff --git a/middleware/DeviceManager/src/DeviceManager.cpp b/middleware/DeviceManager/src/DeviceManager.cpp
index bd4df08..a38d5ac 100644
--- a/middleware/DeviceManager/src/DeviceManager.cpp
+++ b/middleware/DeviceManager/src/DeviceManager.cpp
@@ -23,15 +23,15 @@ const StatusCode DeviceManager::Init(void)
{
KeyManager::GetInstance()->Init();
KeyManager::GetInstance()->StartTimer();
- // LedManager::GetInstance()->Init();
- // LedManager::GetInstance()->StartTimer();
+ LedManager::GetInstance()->Init();
+ LedManager::GetInstance()->StartTimer();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode DeviceManager::UnInit(void)
{
KeyManager::GetInstance()->UnInit();
- // LedManager::GetInstance()->UnInit();
+ LedManager::GetInstance()->UnInit();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode DeviceManager::SetAllKeysMonitor(std::shared_ptr &monitor)
diff --git a/middleware/DeviceManager/src/LedManager.cpp b/middleware/DeviceManager/src/LedManager.cpp
index ac876a1..a147722 100644
--- a/middleware/DeviceManager/src/LedManager.cpp
+++ b/middleware/DeviceManager/src/LedManager.cpp
@@ -33,7 +33,6 @@ void LedManager::Init(void)
std::map> allLeds;
IHalCpp::GetInstance()->GetAllLeds(allLeds);
LedConversion(allLeds);
- StartTimer();
}
void LedManager::UnInit(void)
{
@@ -85,6 +84,7 @@ void LedManager::LedConversion(std::map> &
for (iter = ledHal.begin(); iter != ledHal.end(); ++iter) {
std::shared_ptr led = std::dynamic_pointer_cast(iter->second);
if (led) {
+ LogInfo("Get led [%s].\n", iter->first.c_str());
mAllLedHal[iter->first] = led;
}
else {
diff --git a/test/hal/tool/include/HalTestTool.h b/test/hal/tool/include/HalTestTool.h
index 9d8008c..73597d3 100644
--- a/test/hal/tool/include/HalTestTool.h
+++ b/test/hal/tool/include/HalTestTool.h
@@ -15,7 +15,7 @@
#ifndef HAL_TEST_TOOL_H
#define HAL_TEST_TOOL_H
#include "IHalCpp.h"
-// #include "KeyControl.h"
+#include "LedControl.h"
#include
#include
using ::testing::_;
@@ -56,8 +56,10 @@ public:
void Init(void);
void UnInit(void);
void SetAllKeysResult(std::map> &allKeys);
+ void SetAllLedsResult(std::map> &allLeds);
// void SetKeyEvent(const std::string keyName, const KeyHalEvent &event); // TODO: unused function?
void SetKeyClick(const std::string &keyName, const unsigned int &pressingTimeMs = 200);
+ void SetLedStateExpectations(const std::string &ledName, const LedState &state);
protected:
virtual void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) {}
@@ -72,11 +74,19 @@ private:
void InitAllKeysMock(std::map> &allKeys);
void InitKeysMock(std::shared_ptr &vMock);
+private:
+ void SetAllLedsResult(std::shared_ptr &vMock, std::map> &allLeds);
+ std::shared_ptr SearchLed(const std::string &ledName);
+ void InitAllLedsMock(std::map> &allLeds);
+ void InitLedsMock(std::shared_ptr &vMock);
+
public:
static std::shared_ptr MakeKeyHalTest(const std::string &keyName);
+ static std::shared_ptr MakeLedHalTest(const std::string &ledName);
private:
std::shared_ptr mHalMock;
std::map> mAllKeys;
+ std::map> mAllLeds;
};
#endif
\ No newline at end of file
diff --git a/test/hal/tool/src/HalTestTool.cpp b/test/hal/tool/src/HalTestTool.cpp
index e915fce..88fe20e 100644
--- a/test/hal/tool/src/HalTestTool.cpp
+++ b/test/hal/tool/src/HalTestTool.cpp
@@ -18,6 +18,7 @@
#include "ILog.h"
#include "KeyControl.h"
#include "KeyControlMock.h"
+#include "LedControlMock.h"
#include
void HalTestTool::Init(void)
{
@@ -30,6 +31,8 @@ void HalTestTool::UnInit(void)
{
mHalMock.reset();
CancelOverrideHalMakePtrObject();
+ mAllKeys.clear();
+ mAllLeds.clear();
}
void HalTestTool::SetAllKeysResult(std::map> &allKeys)
{
@@ -37,6 +40,12 @@ void HalTestTool::SetAllKeysResult(std::map> &allLeds)
+{
+ std::shared_ptr halMock = mHalMock;
+ SetAllLedsResult(halMock, allLeds);
+ mAllLeds = allLeds;
+}
// void HalTestTool::SetKeyEvent(const std::string keyName, const KeyHalEvent &event)
// {
// std::shared_ptr key = SearchKey(keyName);
@@ -69,6 +78,21 @@ void HalTestTool::SetKeyClick(const std::string &keyName, const unsigned int &pr
LogWarning("Key mock error.\n");
}
}
+void HalTestTool::SetLedStateExpectations(const std::string &ledName, const LedState &state)
+{
+ std::shared_ptr led = SearchLed(ledName);
+ if (!led) {
+ LogError("Can't set led state, led not found.\n");
+ return;
+ }
+ std::shared_ptr ledMock = std::dynamic_pointer_cast(led);
+ if (ledMock) {
+ LedControlMock::SetLedStateMock(ledMock, state);
+ }
+ else {
+ LogWarning("led mock error.\n");
+ }
+}
void HalTestTool::KeyEventHappendOnce(std::shared_ptr &vMock, const unsigned int &pressingTimeMs)
{
std::shared_ptr mock = std::dynamic_pointer_cast(vMock);
@@ -109,13 +133,6 @@ void HalTestTool::InitAllKeysMock(std::map
for (iter = allKeys.begin(); iter != allKeys.end(); ++iter) {
std::shared_ptr keyHal = iter->second;
InitKeysMock(keyHal);
- // std::shared_ptr mock = std::dynamic_pointer_cast(keyHal);
- // if (mock) {
- // InitKeysMock(mock);
- // }
- // else {
- // LogWarning("Invalid key mock.\n");
- // }
}
}
void HalTestTool::InitKeysMock(std::shared_ptr &vMock)
@@ -152,8 +169,67 @@ void HalTestTool::SetAllKeysResult(std::shared_ptr &vMock,
.WillRepeatedly(DoAll(SetArgReferee<0>(allKeys), Return(CreateStatusCode(STATUS_CODE_OK))));
InitAllKeysMock(allKeys);
}
+void HalTestTool::SetAllLedsResult(std::shared_ptr &vMock,
+ std::map> &allLeds)
+{
+ std::shared_ptr mock = std::dynamic_pointer_cast(vMock);
+ if (!mock) {
+ LogError("vMock error.\n");
+ return;
+ }
+ /**
+ * @brief
+ * Note: The SetArgReference function cannot be used here, as it will prevent the smart pointer from being released
+ * within the TEST range, resulting in an error only being reported after all test cases have finished running.
+ * Can't use:
+ * EXPECT_CALL(*mock.get(), GetAllLedsTrace(_))
+ * .WillRepeatedly(DoAll(SetArgReferee<0>(allLeds), Return(CreateStatusCode(STATUS_CODE_OK))));
+ */
+ auto getAllLeds = [=, &allLeds](std::map> &setAllLeds) {
+ setAllLeds = allLeds;
+ };
+ EXPECT_CALL(*mock.get(), GetAllLedsTrace(_))
+ .WillRepeatedly(DoAll(WithArgs<0>(Invoke(getAllLeds)), Return(CreateStatusCode(STATUS_CODE_OK))));
+ InitAllLedsMock(allLeds);
+}
+std::shared_ptr HalTestTool::SearchLed(const std::string &ledName)
+{
+ std::shared_ptr mock;
+ std::map>::iterator iter;
+ iter = mAllLeds.find(ledName);
+ if (iter != mAllLeds.end()) {
+ mock = std::dynamic_pointer_cast(mAllLeds[ledName]);
+ }
+ else {
+ LogWarning("Can't found the led control.\n");
+ }
+ return mock;
+}
+void HalTestTool::InitAllLedsMock(std::map> &allLeds)
+{
+ std::map>::iterator iter;
+ for (iter = allLeds.begin(); iter != allLeds.end(); ++iter) {
+ std::shared_ptr ledHal = iter->second;
+ InitLedsMock(ledHal);
+ }
+}
+void HalTestTool::InitLedsMock(std::shared_ptr &vMock)
+{
+ std::shared_ptr mock = std::dynamic_pointer_cast(vMock);
+ if (!mock) {
+ LogError("vMock error.\n");
+ return;
+ }
+ constexpr int LED_SHOULD_NOT_BE_CONTROLED_WHEN_NOBODY_CONTROL_IT = 0;
+ EXPECT_CALL(*mock.get(), SetLedStateTrace(_)).Times(LED_SHOULD_NOT_BE_CONTROLED_WHEN_NOBODY_CONTROL_IT);
+}
std::shared_ptr HalTestTool::MakeKeyHalTest(const std::string &keyName)
{
std::shared_ptr key = std::make_shared(keyName);
return key;
+}
+std::shared_ptr HalTestTool::MakeLedHalTest(const std::string &ledName)
+{
+ std::shared_ptr led = std::make_shared(ledName);
+ return led;
}
\ No newline at end of file
diff --git a/test/hal/tool/src/LedControlMock.cpp b/test/hal/tool/src/LedControlMock.cpp
index 18bb27a..8530c93 100644
--- a/test/hal/tool/src/LedControlMock.cpp
+++ b/test/hal/tool/src/LedControlMock.cpp
@@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "LedControlMock.h"
+#include "ILog.h"
LedControlTest::LedControlTest(const std::string &ledName) : mLedName(ledName)
{
//
@@ -20,4 +21,28 @@ LedControlTest::LedControlTest(const std::string &ledName) : mLedName(ledName)
LedControlMock::LedControlMock(const std::string &ledName) : LedControlTest(ledName)
{
//
+}
+StatusCode LedControlTest::SetLedState(const LedState &state)
+{
+ LogInfo("SetLedState mLedName = %s, state = %s\n", mLedName.c_str(), PrintLedState(state));
+ StatusCode code = SetLedStateTrace(state);
+ if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) {
+ // return SetLedState(state);
+ }
+ return code;
+}
+StatusCode LedControlTest::SetLedStateTrace(const LedState &state)
+{
+ return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
+}
+void LedControlMock::SetLedStateMock(std::shared_ptr &mock, const LedState &state)
+{
+ LogInfo("LedControlMock::SetLedState\n");
+ constexpr int SOMEBODY_CONTROL_LED = 1;
+ EXPECT_CALL(*mock.get(), SetLedStateTrace(_))
+ // .Times(SOMEBODY_CONTROL_LED)
+ .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
+ EXPECT_CALL(*mock.get(), SetLedStateTrace(state))
+ .Times(SOMEBODY_CONTROL_LED)
+ .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
}
\ No newline at end of file
diff --git a/test/hal/tool/src/LedControlMock.h b/test/hal/tool/src/LedControlMock.h
index 9bf3804..6aa4793 100644
--- a/test/hal/tool/src/LedControlMock.h
+++ b/test/hal/tool/src/LedControlMock.h
@@ -14,6 +14,7 @@
*/
#ifndef LED_CONTROL_MOCK_H
#define LED_CONTROL_MOCK_H
+#include "HalTestTool.h"
#include "IHalCpp.h"
#include "LedControl.h"
class LedControlTest : public LedControl, public VLedHal
@@ -21,6 +22,10 @@ class LedControlTest : public LedControl, public VLedHal
public:
LedControlTest(const std::string &ledName);
virtual ~LedControlTest() = default;
+ StatusCode SetLedState(const LedState &state) override;
+
+protected:
+ virtual StatusCode SetLedStateTrace(const LedState &state);
private:
const std::string mLedName;
@@ -30,5 +35,9 @@ class LedControlMock : public LedControlTest
public:
LedControlMock(const std::string &ledName);
virtual ~LedControlMock() = default;
+ MOCK_METHOD1(SetLedStateTrace, StatusCode(const LedState &));
+
+public:
+ static void SetLedStateMock(std::shared_ptr &mock, const LedState &state);
};
#endif
\ No newline at end of file
diff --git a/test/middleware/DeviceManager/CMakeLists.txt b/test/middleware/DeviceManager/CMakeLists.txt
index 5e012e6..d5747c3 100644
--- a/test/middleware/DeviceManager/CMakeLists.txt
+++ b/test/middleware/DeviceManager/CMakeLists.txt
@@ -11,6 +11,7 @@ include_directories(
# ${UTILS_SOURCE_PATH}/UartDevice/include
# ${UTILS_SOURCE_PATH}/McuProtocol/include
${UTILS_SOURCE_PATH}/KeyControl/include
+ ${UTILS_SOURCE_PATH}/LedControl/include
${HAL_SOURCE_PATH}/include
# ${HAL_SOURCE_PATH}/src
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include
diff --git a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp
index ae84858..7d691fa 100644
--- a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp
+++ b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp
@@ -21,7 +21,8 @@
#include
namespace DeviceManagerTest
{
-const char *KEY_TEST = "TEST";
+const char *KEY_TEST = "KEY_TEST";
+const char *LED_TEST = "LED_TEST";
class DeviceManagerTest : public testing::Test, virtual public HalTestTool, public DeviceManagerTestTool
{
public:
@@ -55,17 +56,21 @@ private:
{
std::shared_ptr key = HalTestTool::MakeKeyHalTest(KEY_TEST);
mAllKeysMock[KEY_TEST] = key;
+ std::shared_ptr led = HalTestTool::MakeLedHalTest(LED_TEST);
+ mAllLedsMock[LED_TEST] = led;
}
void DestroyAllKeysMock(void)
{
- //
+
mAllKeysMock.clear();
+ mAllLedsMock.clear();
}
public:
// std::shared_ptr mLinuxTest;
protected:
std::map> mAllKeysMock;
+ std::map> mAllLedsMock;
};
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyShortPress
@@ -133,11 +138,15 @@ TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_SetKeyMonitor)
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
-// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControLed
-TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControLed)
+// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed
+TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed)
{
- // IDeviceManager::GetInstance()->Init();
- // std::this_thread::sleep_for(std::chrono::milliseconds(2000));
- // IDeviceManager::GetInstance()->UnInit();
+ SetAllLedsResult(mAllLedsMock);
+ IDeviceManager::GetInstance()->Init();
+ std::shared_ptr monitor = std::make_shared();
+ IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
+ ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK);
+ std::this_thread::sleep_for(std::chrono::milliseconds(1000));
+ IDeviceManager::GetInstance()->UnInit();
}
} // namespace DeviceManagerTest
\ No newline at end of file
diff --git a/test/middleware/DeviceManager/tool/CMakeLists.txt b/test/middleware/DeviceManager/tool/CMakeLists.txt
index 53cbb1b..66a6d22 100644
--- a/test/middleware/DeviceManager/tool/CMakeLists.txt
+++ b/test/middleware/DeviceManager/tool/CMakeLists.txt
@@ -7,6 +7,7 @@ include_directories(
./include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
+ ${UTILS_SOURCE_PATH}/LedControl/include
# ${UTILS_SOURCE_PATH}/McuProtocol/include
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
@@ -18,7 +19,7 @@ include_directories(
aux_source_directory(./src TEST_TOOL_SRC_FILES)
set(TEST_TOOL_TARGET DeviceManagerTestTool)
add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES})
-target_link_libraries(${TEST_TOOL_TARGET} Log)
+target_link_libraries(${TEST_TOOL_TARGET} LedControl Log)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(
diff --git a/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h b/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h
index 71916d3..1a1749a 100644
--- a/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h
+++ b/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h
@@ -16,6 +16,7 @@
#define DEVICE_MANAGER_TEST_TOOL_H
#include "DeviceManager.h"
#include "HalTestTool.h"
+#include "LedControl.h"
#include
#include
using ::testing::_;
@@ -82,6 +83,10 @@ public:
void Init(void);
void UnInit(void);
+public:
+ std::shared_ptr ControlLed(const std::string &ledName, const LedState &state,
+ const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs);
+
protected:
void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) override;
@@ -90,6 +95,7 @@ private:
void KeyMonitorInit(std::shared_ptr &mock, const std::string &keyName,
const unsigned int &pressingTimeMs);
+public:
private:
std::shared_ptr mDeviceManagerMock;
std::shared_ptr mKeyMonitorMock;
diff --git a/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp b/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp
index 8d4dc9c..c8b176e 100644
--- a/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp
+++ b/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp
@@ -16,6 +16,7 @@
#include "DeviceManagerMakePtrTest.h"
#include "ILog.h"
#include "KeyControl.h"
+#include "SingleControlMock.h"
const StatusCode DeviceManagerTool::SetAllKeysMonitor(std::shared_ptr &monitor)
{
LogInfo("DeviceManagerTool::SetAllKeysMonitor\n");
@@ -37,6 +38,16 @@ void DeviceManagerTestTool::UnInit(void)
mKeyMonitorMock.reset();
CancelOverrideDeviceMakePtrObject();
}
+std::shared_ptr DeviceManagerTestTool::ControlLed(const std::string &ledName, const LedState &state,
+ const unsigned int &aliveTimeMs,
+ const unsigned int &blinkTimeMs)
+{
+ HalTestTool::SetLedStateExpectations(ledName, state);
+ std::shared_ptr ledControl =
+ std::make_shared(state, aliveTimeMs, blinkTimeMs);
+ IDeviceManager::GetInstance()->ControlLed(ledName, ledControl);
+ return ledControl;
+}
void DeviceManagerTestTool::DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs)
{
LogInfo("DeviceManagerTestTool::DeviceManagerNotice\n");
diff --git a/test/middleware/DeviceManager/tool/src/SingleControlMock.cpp b/test/middleware/DeviceManager/tool/src/SingleControlMock.cpp
new file mode 100644
index 0000000..0ca4cb6
--- /dev/null
+++ b/test/middleware/DeviceManager/tool/src/SingleControlMock.cpp
@@ -0,0 +1,32 @@
+/*
+ * 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 "SingleControlMock.h"
+SingleControlTool::SingleControlTool(const LedState &state, const unsigned int aliveTimeMs,
+ const unsigned int &blinkTimeMs)
+ : mState(state), mAliveTimeMs(aliveTimeMs), mBlinkTimeMs(blinkTimeMs)
+{
+ //
+}
+StatusCode SingleControlTool::GetLedState(LedState &state)
+{
+ state = mState;
+ return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
+}
+SingleControlMock::SingleControlMock(const LedState &state, const unsigned int aliveTimeMs,
+ const unsigned int &blinkTimeMs)
+ : SingleControlTool(state, aliveTimeMs, blinkTimeMs)
+{
+ //
+}
\ No newline at end of file
diff --git a/test/middleware/DeviceManager/tool/src/SingleControlMock.h b/test/middleware/DeviceManager/tool/src/SingleControlMock.h
new file mode 100644
index 0000000..b856f4c
--- /dev/null
+++ b/test/middleware/DeviceManager/tool/src/SingleControlMock.h
@@ -0,0 +1,39 @@
+/*
+ * 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 LED_CONTROL_MOCK_H
+#define LED_CONTROL_MOCK_H
+#include "IDeviceManager.h"
+#include "LedControl.h"
+class SingleControlTool : public VSingleControl, public VirtualLedControl
+{
+public:
+ SingleControlTool(const LedState &state, const unsigned int aliveTimeMs, const unsigned int &blinkTimeMs);
+ virtual ~SingleControlTool() = default;
+ virtual StatusCode GetLedState(LedState &state);
+ virtual unsigned int GetKeepAliveTimeMs(void) { return mAliveTimeMs; }
+ virtual unsigned int GetBlinkTimeMs(void) { return mBlinkTimeMs; }
+
+private:
+ const LedState mState;
+ const unsigned int mAliveTimeMs;
+ const unsigned int mBlinkTimeMs;
+};
+class SingleControlMock : public SingleControlTool
+{
+public:
+ SingleControlMock(const LedState &state, const unsigned int aliveTimeMs, const unsigned int &blinkTimeMs);
+ virtual ~SingleControlMock() = default;
+};
+#endif
\ No newline at end of file
diff --git a/utils/LedControl/include/LedControl.h b/utils/LedControl/include/LedControl.h
index 8fb3ccb..dd3b9ae 100644
--- a/utils/LedControl/include/LedControl.h
+++ b/utils/LedControl/include/LedControl.h
@@ -53,7 +53,7 @@ class VLedControl
public:
VLedControl() = default;
virtual ~VLedControl() = default;
- virtual bool SetLedState(const LedState &state) { return false; }
+ virtual StatusCode SetLedState(const LedState &state) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
virtual void AddLedState(std::shared_ptr &control) {}
virtual void CheckState(const unsigned int &period) {}
// virtual void SetHalLedState(const VirtualLedState &state) {}
@@ -65,7 +65,7 @@ public:
LedControl() = default;
virtual ~LedControl() = default;
void CheckState(const unsigned int &period) override;
- void AddLedState(std::shared_ptr &state);
+ void AddLedState(std::shared_ptr &state) override;
private:
void NewLedStateStart(void);
@@ -79,4 +79,5 @@ private:
unsigned int mStateAliveTime;
unsigned int mBlinkPeriod;
};
+const char *PrintLedState(const LedState &state);
#endif
\ No newline at end of file
diff --git a/utils/LedControl/src/LedControl.cpp b/utils/LedControl/src/LedControl.cpp
index 6acf7a8..62fea64 100644
--- a/utils/LedControl/src/LedControl.cpp
+++ b/utils/LedControl/src/LedControl.cpp
@@ -97,4 +97,28 @@ void LedControl::BlinkOn(std::shared_ptr &state, const LedState
mCurrentState = onState;
mBlinkPeriod = 0;
}
+}
+const char *PrintLedState(const LedState &state)
+{
+ switch (state) {
+ case LedState::OFF: {
+ return "OFF";
+ }
+ case LedState::ON: {
+ return "ON";
+ }
+ case LedState::GREEN: {
+ return "GREEN";
+ }
+ case LedState::RED: {
+ return "RED";
+ }
+ case LedState::YELLOW: {
+ return "YELLOW";
+ }
+
+ default: {
+ return "Unknown LedState";
+ }
+ }
}
\ No newline at end of file