diff --git a/hal/abstract/IHalCpp.cpp b/hal/abstract/IHalCpp.cpp index 7f92ce3..ae01756 100644 --- a/hal/abstract/IHalCpp.cpp +++ b/hal/abstract/IHalCpp.cpp @@ -31,7 +31,7 @@ std::shared_ptr &IHalCpp::GetInstance(std::shared_ptr *impl) } StatusCode IHalCpp::Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } StatusCode IHalCpp::UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } -StatusCode IHalCpp::GetLedHals(std::vector> &ledHals) +StatusCode IHalCpp::GetAllLeds(std::map> &allLeds) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } diff --git a/hal/include/IHalCpp.h b/hal/include/IHalCpp.h index d21a778..da82e9e 100644 --- a/hal/include/IHalCpp.h +++ b/hal/include/IHalCpp.h @@ -25,6 +25,7 @@ constexpr int INVALID_PERIOD = -1; constexpr int PERIPHERAL_CHECK_PERIOD_MS = 100; constexpr int IMEI_LEN = 15; void CreateHalCppModule(void); +void DestroyHalCppModule(void); class VKeyHalMonitor { public: @@ -47,12 +48,7 @@ class VLedHal { public: VLedHal() = default; - ~VLedHal() = default; - virtual StatusCode SetHalLedState(const VirtualLedState &state) - { - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); - } - virtual std::string GetLedName(void) { return "undefine"; } + virtual ~VLedHal() = default; }; class IHalCpp { @@ -62,7 +58,7 @@ public: static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); virtual StatusCode Init(void); virtual StatusCode UnInit(void); - virtual StatusCode GetLedHals(std::vector> &ledHals); + virtual StatusCode GetAllLeds(std::map> &allLeds); virtual StatusCode GetAllKeys(std::map> &allKeys); }; #endif diff --git a/hal/src/HalMakePtr.cpp b/hal/src/HalMakePtr.cpp index ec3d6ec..13c3e1c 100644 --- a/hal/src/HalMakePtr.cpp +++ b/hal/src/HalMakePtr.cpp @@ -50,6 +50,11 @@ void CreateHalCppModule(void) IHalCpp::GetInstance(&instance); } } +void DestroyHalCppModule(void) +{ + auto instance = std::make_shared(); + IHalCpp::GetInstance(&instance); +} std::shared_ptr &HalMakePtr::GetInstance(std::shared_ptr *impl) { static auto instance = std::make_shared(); diff --git a/middleware/DeviceManager/CMakeLists.txt b/middleware/DeviceManager/CMakeLists.txt index 58469da..2531853 100644 --- a/middleware/DeviceManager/CMakeLists.txt +++ b/middleware/DeviceManager/CMakeLists.txt @@ -8,6 +8,7 @@ include_directories( ./include ${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/LedControl/include ${HAL_SOURCE_PATH}/include ) #do not rely on any other library @@ -21,7 +22,7 @@ aux_source_directory(./src SRC_FILES) set(TARGET_NAME DeviceManager) add_library(${TARGET_NAME} STATIC ${SRC_FILES}) -target_link_libraries(${TARGET_NAME} Hal StatusCode Log) +target_link_libraries(${TARGET_NAME} LedControl Hal StatusCode Log) if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") add_custom_target( diff --git a/middleware/DeviceManager/include/IDeviceManager.h b/middleware/DeviceManager/include/IDeviceManager.h index 7b8bffd..5a34807 100644 --- a/middleware/DeviceManager/include/IDeviceManager.h +++ b/middleware/DeviceManager/include/IDeviceManager.h @@ -38,6 +38,12 @@ public: virtual ~VKeyMonitor() = default; virtual void KeyEventReport(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs) {} }; +class VirtualLedControl +{ +public: + VirtualLedControl() = default; + virtual ~VirtualLedControl() = default; +}; class IDeviceManager { public: @@ -46,8 +52,7 @@ public: static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); virtual const StatusCode Init(void); virtual const StatusCode UnInit(void); - virtual const StatusCode SetLedState(const std::string &ledName, const VirtualLedState ¤tState, - const unsigned int &KeepAliveTime, const unsigned int &blinkPeriod); + virtual const StatusCode ControlLed(const std::string &ledName, std::shared_ptr &control); virtual const StatusCode SetAllKeysMonitor(std::shared_ptr &monitor); }; #endif \ No newline at end of file diff --git a/middleware/DeviceManager/src/DeviceManager.cpp b/middleware/DeviceManager/src/DeviceManager.cpp index f4193c5..bd4df08 100644 --- a/middleware/DeviceManager/src/DeviceManager.cpp +++ b/middleware/DeviceManager/src/DeviceManager.cpp @@ -16,18 +16,22 @@ #include "DeviceManager.h" #include "ILog.h" #include "KeyManager.h" +#include "LedManager.h" #include const StatusCode DeviceManager::Init(void) { KeyManager::GetInstance()->Init(); KeyManager::GetInstance()->StartTimer(); + // LedManager::GetInstance()->Init(); + // LedManager::GetInstance()->StartTimer(); return CreateStatusCode(STATUS_CODE_OK); } const StatusCode DeviceManager::UnInit(void) { KeyManager::GetInstance()->UnInit(); + // LedManager::GetInstance()->UnInit(); return CreateStatusCode(STATUS_CODE_OK); } const StatusCode DeviceManager::SetAllKeysMonitor(std::shared_ptr &monitor) @@ -35,3 +39,8 @@ const StatusCode DeviceManager::SetAllKeysMonitor(std::shared_ptr & LogInfo("DeviceManager::SetAllKeysMonitor\n"); return KeyManager::GetInstance()->SetKeyMonitor(monitor); } +const StatusCode DeviceManager::ControlLed(const std::string &ledName, std::shared_ptr &control) +{ + LedManager::GetInstance()->ControlLed(ledName, control); + return CreateStatusCode(STATUS_CODE_OK); +} diff --git a/middleware/DeviceManager/src/DeviceManager.h b/middleware/DeviceManager/src/DeviceManager.h index b07a4dd..2e43326 100644 --- a/middleware/DeviceManager/src/DeviceManager.h +++ b/middleware/DeviceManager/src/DeviceManager.h @@ -25,10 +25,8 @@ public: const StatusCode Init(void) override; const StatusCode UnInit(void) override; - // const StatusCode SetLedState(const std::string &ledName, const VirtualLedState ¤tState, - // const unsigned int &keepAliveTime = DEFAULT_KEEP_ALIVE_TIME, - // const unsigned int &blinkPeriod = LED_NOT_BLINK) override; const StatusCode SetAllKeysMonitor(std::shared_ptr &monitor) override; + const StatusCode ControlLed(const std::string &ledName, std::shared_ptr &control) override; private: // std::vector> mLedManagers; diff --git a/middleware/DeviceManager/src/IDeviceManager.cpp b/middleware/DeviceManager/src/IDeviceManager.cpp index 89dbbf0..3d893d0 100644 --- a/middleware/DeviceManager/src/IDeviceManager.cpp +++ b/middleware/DeviceManager/src/IDeviceManager.cpp @@ -30,8 +30,7 @@ std::shared_ptr &IDeviceManager::GetInstance(std::shared_ptr &control) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } diff --git a/middleware/DeviceManager/src/KeyManager.h b/middleware/DeviceManager/src/KeyManager.h index dea867f..b2f24fc 100644 --- a/middleware/DeviceManager/src/KeyManager.h +++ b/middleware/DeviceManager/src/KeyManager.h @@ -30,11 +30,11 @@ public: void UnInit(void); void StartTimer(void); void StopTimer(void); - void Timer(void); void GetAllKeysState(std::map &status); const StatusCode SetKeyMonitor(std::shared_ptr &monitor); private: + void Timer(void); void SetKeyHalMonitor(void); private: diff --git a/middleware/DeviceManager/src/LedManager.cpp b/middleware/DeviceManager/src/LedManager.cpp new file mode 100644 index 0000000..ac876a1 --- /dev/null +++ b/middleware/DeviceManager/src/LedManager.cpp @@ -0,0 +1,94 @@ +/* + * 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 "LedManager.h" +#include "ILog.h" +std::shared_ptr &LedManager::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) { + if (instance.use_count() == 1) { + LogInfo("Instance changed succeed.\n"); + instance = *impl; + } + else { + LogError("Can't changing the instance becase of using by some one.\n"); + } + } + return instance; +} +void LedManager::Init(void) +{ + std::map> allLeds; + IHalCpp::GetInstance()->GetAllLeds(allLeds); + LedConversion(allLeds); + StartTimer(); +} +void LedManager::UnInit(void) +{ + StopTimer(); + mAllLedHal.clear(); +} +void LedManager::Timer(void) +{ + mTimerRuning = true; + std::map>::iterator iter; + while (mTimerRuning) { + mMutex.lock(); + for (iter = mAllLedHal.begin(); iter != mAllLedHal.end(); ++iter) { + std::shared_ptr ledHal = iter->second; + ledHal->CheckState(LED_STATE_CHECK_PERIOD_MS); + } + mMutex.unlock(); + std::this_thread::sleep_for(std::chrono::milliseconds(LED_STATE_CHECK_PERIOD_MS)); + } +} +void LedManager::ControlLed(const std::string &ledName, std::shared_ptr &control) +{ + std::lock_guard locker(mMutex); + std::shared_ptr singleControl = std::dynamic_pointer_cast(control); + if (!singleControl) { + LogError("led can't be controled.\n"); + return; + } + mAllLedHal[ledName]->AddLedState(singleControl); +} +void LedManager::StartTimer(void) +{ + auto timerThread = [](std::shared_ptr timer) { + LogInfo("Led timer started.\n"); + timer->Timer(); + }; + mTimer = std::thread(timerThread, shared_from_this()); +} +void LedManager::StopTimer(void) +{ + mTimerRuning = false; + if (mTimer.joinable()) { + mTimer.join(); + } +} +void LedManager::LedConversion(std::map> &ledHal) +{ + std::map>::iterator iter; + for (iter = ledHal.begin(); iter != ledHal.end(); ++iter) { + std::shared_ptr led = std::dynamic_pointer_cast(iter->second); + if (led) { + mAllLedHal[iter->first] = led; + } + else { + LogWarning("Missing something.\n"); + } + } +} \ No newline at end of file diff --git a/middleware/DeviceManager/src/LedManager.h b/middleware/DeviceManager/src/LedManager.h new file mode 100644 index 0000000..07d1ca8 --- /dev/null +++ b/middleware/DeviceManager/src/LedManager.h @@ -0,0 +1,46 @@ +/* + * 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_MANAGER_H +#define LED_MANAGER_H +#include "IDeviceManager.h" +#include "IHalCpp.h" +#include "LedControl.h" +#include +#include +#include +constexpr int LED_STATE_CHECK_PERIOD_MS = 100; +class LedManager : public std::enable_shared_from_this +{ +public: + LedManager() = default; + virtual ~LedManager() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + void Init(void); + void UnInit(void); + void StartTimer(void); + void StopTimer(void); + void ControlLed(const std::string &ledName, std::shared_ptr &control); + +private: + void Timer(void); + void LedConversion(std::map> &ledHal); + +private: + std::mutex mMutex; + std::map> mAllLedHal; + bool mTimerRuning; + std::thread mTimer; +}; +#endif \ No newline at end of file diff --git a/test/hal/tool/CMakeLists.txt b/test/hal/tool/CMakeLists.txt index 5eb2636..942fc82 100644 --- a/test/hal/tool/CMakeLists.txt +++ b/test/hal/tool/CMakeLists.txt @@ -8,6 +8,7 @@ include_directories( ${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/KeyControl/include + ${UTILS_SOURCE_PATH}/LedControl/include # ${UTILS_SOURCE_PATH}/McuProtocol/include ${HAL_SOURCE_PATH}/src # /home/xiaojiazhu/project/rkipc/battery/ipc-rk1106/ipc-sdk/hal/src/HalCpp.h @@ -21,7 +22,7 @@ include_directories( aux_source_directory(./src TEST_TOOL_SRC_FILES) set(TEST_TOOL_TARGET HalTestTool) add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES}) -target_link_libraries(${TEST_TOOL_TARGET} KeyControl Log) +target_link_libraries(${TEST_TOOL_TARGET} KeyControl LedControl Log) if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") add_custom_target( diff --git a/test/hal/tool/include/HalTestTool.h b/test/hal/tool/include/HalTestTool.h index 1e482ff..4006ab0 100644 --- a/test/hal/tool/include/HalTestTool.h +++ b/test/hal/tool/include/HalTestTool.h @@ -53,11 +53,11 @@ class HalCppTest : public HalCpp public: HalCppTest() = default; virtual ~HalCppTest() = default; - StatusCode GetLedHals(std::vector> &ledHals) override; + StatusCode GetAllLeds(std::map> &allLeds) override; StatusCode GetAllKeys(std::map> &allKeys) override; protected: - virtual StatusCode GetLedHalsTrace(std::vector> &ledHals); + virtual StatusCode GetAllLedsTrace(std::map> &allLeds); virtual StatusCode GetAllKeysTrace(std::map> &allKeys); }; class HalCppMock : public HalCppTest @@ -65,47 +65,9 @@ class HalCppMock : public HalCppTest public: HalCppMock() = default; virtual ~HalCppMock() = default; - MOCK_METHOD1(GetLedHalsTrace, StatusCode(std::vector> &)); + MOCK_METHOD1(GetAllLedsTrace, StatusCode(std::map> &)); MOCK_METHOD1(GetAllKeysTrace, StatusCode(std::map> &)); }; -class KeyControlTest : public KeyControl, public VKeyHal -{ -public: - KeyControlTest(const std::string &keyName); - virtual ~KeyControlTest() = default; - unsigned int GetStatusCheckPeriodMs(void) override { return PERIPHERAL_CHECK_PERIOD_MS; } - void SetKeyMonitor(std::shared_ptr &monitor) override; - void CheckKeyStatus(void) override; - void KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs) override; - const std::string GetKeyName(void) override { return mKeyName; } - -private: - virtual StatusCode KeyEventTriggerTrace(const std::string &keyName, const KeyEvent &event, - const unsigned int &timeMs) - { - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); - } - -private: - const std::string mKeyName; - std::weak_ptr mMonitor; -}; -class KeyControlMock : public KeyControlTest -{ -public: - KeyControlMock(const std::string &keyName); - virtual ~KeyControlMock() = default; - // void KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs) override; - void SetKeyEvent(const KeyHalEvent &event); - bool SetKeyClick(const unsigned int &pressingTimeMs = 200); - MOCK_METHOD3(KeyEventTriggerTrace, StatusCode(const std::string &, const KeyEvent &, const unsigned int &)); - -private: - void KeyEventHappendOnce(std::shared_ptr &mock, const unsigned int &pressingTimeMs); - -private: - std::mutex mMutex; -}; class HalTestTool { public: @@ -121,14 +83,17 @@ protected: virtual void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) {} private: - void KeyEventHappendOnce(std::shared_ptr &mock, const unsigned int &pressingTimeMs); + void KeyEventHappendOnce(std::shared_ptr &vMock, const unsigned int &pressingTimeMs); private: void HalMockInit(std::shared_ptr &mock); void SetAllKeysResult(std::shared_ptr &mock, std::map> &allKeys); - std::shared_ptr SearchKey(const std::string &keyName); + std::shared_ptr SearchKey(const std::string &keyName); void InitAllKeysMock(std::map> &allKeys); - void InitKeysMock(std::shared_ptr &mock); + void InitKeysMock(std::shared_ptr &vMock); + +public: + static std::shared_ptr MakeKeyHalTest(const std::string &keyName); private: std::shared_ptr mHalMock; diff --git a/test/hal/tool/src/HalMakePtrTest.h b/test/hal/tool/src/HalMakePtrTest.h index 1fc7ed1..4c99d88 100644 --- a/test/hal/tool/src/HalMakePtrTest.h +++ b/test/hal/tool/src/HalMakePtrTest.h @@ -16,6 +16,7 @@ #define HAL_MAKE_PTR_TEST_H #include "HalMakePtr.h" #include "HalTestTool.h" +#include "KeyControlMock.h" void OverrideHalMakePtrObject(std::shared_ptr &halMock); void CancelOverrideHalMakePtrObject(void); class HalMakePtrTest : public HalMakePtr diff --git a/test/hal/tool/src/HalTestTool.cpp b/test/hal/tool/src/HalTestTool.cpp index 9f9e933..f0db8f3 100644 --- a/test/hal/tool/src/HalTestTool.cpp +++ b/test/hal/tool/src/HalTestTool.cpp @@ -15,13 +15,14 @@ #include "HalTestTool.h" #include "HalMakePtrTest.h" #include "ILog.h" +#include "KeyControlMock.h" #include -StatusCode HalCppTest::GetLedHals(std::vector> &ledHals) +StatusCode HalCppTest::GetAllLeds(std::map> &allLeds) { - LogInfo("HalCppTest::GetLedHals\n"); - StatusCode code = GetLedHalsTrace(ledHals); + LogInfo("HalCppTest::GetAllLeds\n"); + StatusCode code = GetAllLedsTrace(allLeds); if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) { - return HalCpp::GetLedHals(ledHals); + return HalCpp::GetAllLeds(allLeds); } return code; } @@ -34,9 +35,9 @@ StatusCode HalCppTest::GetAllKeys(std::map } return code; } -StatusCode HalCppTest::GetLedHalsTrace(std::vector> &ledHals) +StatusCode HalCppTest::GetAllLedsTrace(std::map> &allLeds) { - LogInfo("HalCppTest::GetLedHalsTrace\n"); + LogInfo("HalCppTest::GetAllLedsTrace\n"); return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } StatusCode HalCppTest::GetAllKeysTrace(std::map> &allKeys) @@ -69,54 +70,6 @@ void KeyControlTest::KeyEventTrigger(const std::string &keyName, const KeyEvent } monitor->KeyEventHappened(keyName, static_cast(event), timeMs); } -KeyControlMock::KeyControlMock(const std::string &keyName) : KeyControlTest(keyName) -{ - // -} -void KeyControlMock::SetKeyEvent(const KeyHalEvent &event) -{ - // - KeyHalEventTrigger(event); -} -bool KeyControlMock::SetKeyClick(const unsigned int &pressingTimeMs) -{ - if (mMutex.try_lock()) { - auto keyClickThread = [=](std::shared_ptr key) { - KeyEventHappendOnce(key, pressingTimeMs); - key->KeyHalEventTrigger(KeyHalEvent::PRESSING); - std::this_thread::sleep_for(std::chrono::milliseconds(pressingTimeMs)); - key->KeyHalEventTrigger(KeyHalEvent::NOT_PRESSING); - mMutex.unlock(); - }; - std::shared_ptr tmp = KeyControl::shared_from_this(); - std::shared_ptr key = std::dynamic_pointer_cast(tmp); - std::thread clickThread = std::thread(keyClickThread, key); - clickThread.detach(); - return true; - } - LogWarning("SetKeyClick failed, becase key was lock.\n"); - return false; -} -void KeyControlMock::KeyEventHappendOnce(std::shared_ptr &mock, const unsigned int &pressingTimeMs) -{ - EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(_, _, _)) - .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); - constexpr int CLICK_EVENT_HAPPENED_ONLY_ONCE = 1; - constexpr int HOLD_UP_EVENT_HAPPENED_ONLY_ONCE = 1; - if (KEY_ACTION_SHORT_CLICK <= pressingTimeMs && pressingTimeMs < KEY_ACTION_HOLD_DWON) { - EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), KeyEvent::SHORT_CLICK, _)) - .Times(CLICK_EVENT_HAPPENED_ONLY_ONCE) - .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); - } - if (KEY_ACTION_HOLD_DWON <= pressingTimeMs) { - EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), KeyEvent::HOLD_DOWN, _)) - .Times(AtLeast(1)) - .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); - EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), KeyEvent::HOLD_UP, _)) - .Times(HOLD_UP_EVENT_HAPPENED_ONLY_ONCE) - .WillOnce(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); - } -} void HalTestTool::Init(void) { mHalMock = std::make_shared(); @@ -135,27 +88,43 @@ void HalTestTool::SetAllKeysResult(std::map key = SearchKey(keyName); + std::shared_ptr key = SearchKey(keyName); if (!key) { LogError("Can't set key event, key not found.\n"); return; } - key->SetKeyEvent(event); + std::shared_ptr keyMock = std::dynamic_pointer_cast(key); + if (keyMock) { + keyMock->SetKeyEvent(event); + } + else { + LogWarning("Key mock error.\n"); + } } void HalTestTool::SetKeyClick(const std::string &keyName, const unsigned int &pressingTimeMs) { - std::shared_ptr key = SearchKey(keyName); + std::shared_ptr key = SearchKey(keyName); if (!key) { LogError("Can't set key event, key not found.\n"); return; } - if (key->SetKeyClick(pressingTimeMs)) { - // KeyEventHappendOnce(key, pressingTimeMs); - DeviceManagerNotice(keyName, pressingTimeMs); + std::shared_ptr keyMock = std::dynamic_pointer_cast(key); + if (keyMock) { + if (keyMock->SetKeyClick(pressingTimeMs)) { + DeviceManagerNotice(keyName, pressingTimeMs); + } + } + else { + LogWarning("Key mock error.\n"); } } -void HalTestTool::KeyEventHappendOnce(std::shared_ptr &mock, const unsigned int &pressingTimeMs) +void HalTestTool::KeyEventHappendOnce(std::shared_ptr &vMock, const unsigned int &pressingTimeMs) { + std::shared_ptr mock = std::dynamic_pointer_cast(vMock); + if (!mock) { + LogError("vMock error.\n"); + return; + } EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(_, _, _)) .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); constexpr int CLICK_EVENT_HAPPENED_ONLY_ONCE = 1; @@ -177,7 +146,7 @@ void HalTestTool::SetAllKeysResult(std::shared_ptr &mock, .WillRepeatedly(DoAll(SetArgReferee<0>(allKeys), Return(CreateStatusCode(STATUS_CODE_OK)))); InitAllKeysMock(allKeys); } -std::shared_ptr HalTestTool::SearchKey(const std::string &keyName) +std::shared_ptr HalTestTool::SearchKey(const std::string &keyName) { std::shared_ptr mock; std::map>::iterator iter; @@ -195,24 +164,35 @@ void HalTestTool::InitAllKeysMock(std::map std::map>::iterator iter; for (iter = allKeys.begin(); iter != allKeys.end(); ++iter) { std::shared_ptr keyHal = iter->second; - std::shared_ptr mock = std::dynamic_pointer_cast(keyHal); - if (mock) { - InitKeysMock(mock); - } - else { - LogWarning("Invalid key mock.\n"); - } + 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 &mock) +void HalTestTool::InitKeysMock(std::shared_ptr &vMock) { + std::shared_ptr mock = std::dynamic_pointer_cast(vMock); + if (!mock) { + LogError("vMock error.\n"); + return; + } constexpr int KEY_SHOULD_NOT_TRIGGER_WHEN_NOBODY_SET_KEY_RESULT = 0; EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(_, _, _)).Times(KEY_SHOULD_NOT_TRIGGER_WHEN_NOBODY_SET_KEY_RESULT); } void HalTestTool::HalMockInit(std::shared_ptr &mock) { - EXPECT_CALL(*mock.get(), GetLedHalsTrace(_)) + EXPECT_CALL(*mock.get(), GetAllLedsTrace(_)) .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); EXPECT_CALL(*mock.get(), GetAllKeysTrace(_)) .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); +} +std::shared_ptr HalTestTool::MakeKeyHalTest(const std::string &keyName) +{ + std::shared_ptr key = std::make_shared(keyName); + return key; } \ No newline at end of file diff --git a/test/hal/tool/src/KeyControlMock.cpp b/test/hal/tool/src/KeyControlMock.cpp new file mode 100644 index 0000000..71d4e97 --- /dev/null +++ b/test/hal/tool/src/KeyControlMock.cpp @@ -0,0 +1,69 @@ +/* + * 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 "KeyControlMock.h" +#include "ILog.h" +KeyControlMock::KeyControlMock(const std::string &keyName) : KeyControlTest(keyName) +{ + // +} +void KeyControlMock::SetKeyEvent(const KeyHalEvent &event) +{ + // + KeyHalEventTrigger(event); +} +bool KeyControlMock::SetKeyClick(const unsigned int &pressingTimeMs) +{ + if (mMutex.try_lock()) { + auto keyClickThread = [=](std::shared_ptr key) { + KeyEventHappendOnce(key, pressingTimeMs); + key->KeyHalEventTrigger(KeyHalEvent::PRESSING); + std::this_thread::sleep_for(std::chrono::milliseconds(pressingTimeMs)); + key->KeyHalEventTrigger(KeyHalEvent::NOT_PRESSING); + mMutex.unlock(); + }; + std::shared_ptr tmp = KeyControl::shared_from_this(); + std::shared_ptr key = std::dynamic_pointer_cast(tmp); + std::thread clickThread = std::thread(keyClickThread, key); + clickThread.detach(); + return true; + } + LogWarning("SetKeyClick failed, becase key was lock.\n"); + return false; +} +void KeyControlMock::KeyEventHappendOnce(std::shared_ptr &mock, const unsigned int &pressingTimeMs) +{ + EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(_, _, _)) + .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); + constexpr int CLICK_EVENT_HAPPENED_ONLY_ONCE = 1; + constexpr int HOLD_UP_EVENT_HAPPENED_ONLY_ONCE = 1; + if (KEY_ACTION_SHORT_CLICK <= pressingTimeMs && pressingTimeMs < KEY_ACTION_HOLD_DWON) { + EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), KeyEvent::SHORT_CLICK, _)) + .Times(CLICK_EVENT_HAPPENED_ONLY_ONCE) + .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); + return; + } + if (KEY_ACTION_HOLD_DWON <= pressingTimeMs) { + EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), KeyEvent::HOLD_DOWN, _)) + .Times(AtLeast(1)) + .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); + EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), KeyEvent::HOLD_UP, _)) + .Times(HOLD_UP_EVENT_HAPPENED_ONLY_ONCE) + .WillOnce(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); + return; + } + constexpr int KEY_SHOULD_NOT_TRIGGER_WHEN_NOBODY_SET_KEY_RESULT = 0; + EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), _, _)) + .Times(KEY_SHOULD_NOT_TRIGGER_WHEN_NOBODY_SET_KEY_RESULT); +} \ No newline at end of file diff --git a/test/hal/tool/src/KeyControlMock.h b/test/hal/tool/src/KeyControlMock.h new file mode 100644 index 0000000..4b148c2 --- /dev/null +++ b/test/hal/tool/src/KeyControlMock.h @@ -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. + */ +#ifndef KEY_CONTROL_MOCK_H +#define KEY_CONTROL_MOCK_H +#include "HalTestTool.h" +#include "IHalCpp.h" +#include "KeyControl.h" +#include +class KeyControlTest : public KeyControl, public VKeyHal +{ +public: + KeyControlTest(const std::string &keyName); + virtual ~KeyControlTest() = default; + unsigned int GetStatusCheckPeriodMs(void) override { return PERIPHERAL_CHECK_PERIOD_MS; } + void SetKeyMonitor(std::shared_ptr &monitor) override; + void CheckKeyStatus(void) override; + void KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs) override; + const std::string GetKeyName(void) override { return mKeyName; } + +private: + virtual StatusCode KeyEventTriggerTrace(const std::string &keyName, const KeyEvent &event, + const unsigned int &timeMs) + { + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); + } + +private: + const std::string mKeyName; + std::weak_ptr mMonitor; +}; +class KeyControlMock : public KeyControlTest +{ +public: + KeyControlMock(const std::string &keyName); + virtual ~KeyControlMock() = default; + // void KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs) override; + void SetKeyEvent(const KeyHalEvent &event); + bool SetKeyClick(const unsigned int &pressingTimeMs = 200); + MOCK_METHOD3(KeyEventTriggerTrace, StatusCode(const std::string &, const KeyEvent &, const unsigned int &)); + +private: + void KeyEventHappendOnce(std::shared_ptr &mock, const unsigned int &pressingTimeMs); + +private: + std::mutex mMutex; +}; +#endif \ No newline at end of file diff --git a/test/hal/tool/src/LedControlMock.cpp b/test/hal/tool/src/LedControlMock.cpp new file mode 100644 index 0000000..18bb27a --- /dev/null +++ b/test/hal/tool/src/LedControlMock.cpp @@ -0,0 +1,23 @@ +/* + * 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 "LedControlMock.h" +LedControlTest::LedControlTest(const std::string &ledName) : mLedName(ledName) +{ + // +} +LedControlMock::LedControlMock(const std::string &ledName) : LedControlTest(ledName) +{ + // +} \ No newline at end of file diff --git a/test/hal/tool/src/LedControlMock.h b/test/hal/tool/src/LedControlMock.h new file mode 100644 index 0000000..9bf3804 --- /dev/null +++ b/test/hal/tool/src/LedControlMock.h @@ -0,0 +1,34 @@ +/* + * 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 "IHalCpp.h" +#include "LedControl.h" +class LedControlTest : public LedControl, public VLedHal +{ +public: + LedControlTest(const std::string &ledName); + virtual ~LedControlTest() = default; + +private: + const std::string mLedName; +}; +class LedControlMock : public LedControlTest +{ +public: + LedControlMock(const std::string &ledName); + virtual ~LedControlMock() = default; +}; +#endif \ No newline at end of file diff --git a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp index 724d831..ae84858 100644 --- a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp +++ b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp @@ -53,7 +53,7 @@ public: private: void CreateAllKeysMcok(void) { - std::shared_ptr key = std::make_shared(KEY_TEST); + std::shared_ptr key = HalTestTool::MakeKeyHalTest(KEY_TEST); mAllKeysMock[KEY_TEST] = key; } void DestroyAllKeysMock(void) @@ -85,6 +85,23 @@ TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyShortPress) IDeviceManager::GetInstance()->UnInit(); } // ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyInvalidShort +/** + * @brief Construct a new test f object + * This test case demonstrates whether DeviceManager can reprocess key events and report them to the application when + * they are triggered. + */ +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyInvalidShort) +{ + SetAllKeysResult(mAllKeysMock); + IDeviceManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor); + SetKeyClick(KEY_TEST, 50); // Simulate pressing a button. + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IDeviceManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/DeviceManagerTest // --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyLongPress /** * @brief Construct a new test f object @@ -115,4 +132,12 @@ TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_SetKeyMonitor) std::this_thread::sleep_for(std::chrono::milliseconds(2000)); IDeviceManager::GetInstance()->UnInit(); } +// ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControLed +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControLed) +{ + // IDeviceManager::GetInstance()->Init(); + // std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + // IDeviceManager::GetInstance()->UnInit(); +} } // namespace DeviceManagerTest \ No newline at end of file diff --git a/utils/LedControl/include/LedControl.h b/utils/LedControl/include/LedControl.h index b5a3c39..8fb3ccb 100644 --- a/utils/LedControl/include/LedControl.h +++ b/utils/LedControl/include/LedControl.h @@ -54,14 +54,18 @@ public: VLedControl() = default; virtual ~VLedControl() = default; virtual bool SetLedState(const LedState &state) { return false; } + virtual void AddLedState(std::shared_ptr &control) {} + virtual void CheckState(const unsigned int &period) {} + // virtual void SetHalLedState(const VirtualLedState &state) {} + virtual std::string GetLedName(void) { return "undefine"; } }; class LedControl : virtual public VLedControl { public: LedControl() = default; virtual ~LedControl() = default; + void CheckState(const unsigned int &period) override; void AddLedState(std::shared_ptr &state); - void CheckState(const unsigned int &period); private: void NewLedStateStart(void);