diff --git a/hal/include/IHalCpp.h b/hal/include/IHalCpp.h index 66a49936..6c4e10ec 100644 --- a/hal/include/IHalCpp.h +++ b/hal/include/IHalCpp.h @@ -17,6 +17,7 @@ #include "StatusCode.h" #include #include +#include constexpr int INVALID_PERIOD = -1; constexpr int PERIPHERAL_CHECK_PERIOD_MS = 100; constexpr int IMEI_LEN = 15; @@ -44,6 +45,54 @@ public: virtual void SetKeyHalOwner(std::shared_ptr owner) {} virtual const std::string GetKeyName(void) { return "undefine"; } }; + +enum HalLedState +{ + HAL_LED_STATE_OFF = 0, + HAL_LED_STATE_ON, + HAL_LED_STATE_GREEN, + HAL_LED_STATE_RED, + HAL_LED_STATE_YELLOW, + HAL_LED_STATE_LEVEL_0, + HAL_LED_STATE_LEVEL_1, + HAL_LED_STATE_LEVEL_2, + HAL_LED_STATE_LEVEL_3, + HAL_LED_STATE_LEVEL_4, + HAL_LED_STATE_LEVEL_END, + HAL_LED_STATE_END +}; + +class VLedHal +{ +public: + VLedHal() = default; + ~VLedHal() = default; + virtual StatusCode SetHalLedState(const HalLedState &state) + { + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); + } + virtual std::string GetLedName(void) { return "undefine"; } +}; + +#if 0 +class WifiLed : public VLedHalOwner, public VLedHal +{ +public: + WifiLed() = default; + ~WifiLed() = default; + + // StatusCode SetLedHalOwner(std::shared_ptr owner) override; + StatusCode SetHalLedState(const HalLedState &state) override; + std::string GetLedName(void) override; + +private: + // VLedHalOwner m_LedHalOwner; + std::string m_LedName; + SF_LED_GPIO_IDX_E mPinRed; + SF_LED_GPIO_IDX_E mPinGreen; +} +#endif + class IHalCpp { public: @@ -52,6 +101,11 @@ public: static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); virtual StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } virtual StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } + virtual StatusCode GetLedHals(std::vector> &ledHals) + { + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); + } }; void CreateHalCppModule(void); -#endif \ No newline at end of file + +#endif diff --git a/hal/src/HalCpp.cpp b/hal/src/HalCpp.cpp index ba77430d..d6ee8d05 100644 --- a/hal/src/HalCpp.cpp +++ b/hal/src/HalCpp.cpp @@ -24,3 +24,8 @@ StatusCode HalCpp::UnInit(void) LogInfo("HalCpp::UnInit\n"); return CreateStatusCode(STATUS_CODE_OK); } +StatusCode HalCpp::GetLedHals(std::vector> &ledHals) +{ + ledHals = mLedHals; + return CreateStatusCode(STATUS_CODE_OK); +} diff --git a/hal/src/HalCpp.h b/hal/src/HalCpp.h index 1017cc2b..4185a94a 100644 --- a/hal/src/HalCpp.h +++ b/hal/src/HalCpp.h @@ -22,5 +22,9 @@ public: virtual ~HalCpp() = default; StatusCode Init(void) override; StatusCode UnInit(void) override; + StatusCode GetLedHals(std::vector> &ledHals) override; + +private: + std::vector> mLedHals; }; #endif \ No newline at end of file diff --git a/middleware/DeviceManager/include/IDeviceManager.h b/middleware/DeviceManager/include/IDeviceManager.h index f83e1e98..4ba75384 100644 --- a/middleware/DeviceManager/include/IDeviceManager.h +++ b/middleware/DeviceManager/include/IDeviceManager.h @@ -15,7 +15,9 @@ #ifndef IDEVICEMANAGER_H #define IDEVICEMANAGER_H #include "StatusCode.h" +#include #include +#include enum class IpcMission { TEST = 0, @@ -29,6 +31,23 @@ enum class KeyAction HOLD_UP, END }; + +enum LedState +{ + LED_STATE_OFF = 0, + LED_STATE_ON, + LED_STATE_GREEN, + LED_STATE_RED, + LED_STATE_YELLOW, + LED_STATE_LEVEL_0, + LED_STATE_LEVEL_1, + LED_STATE_LEVEL_2, + LED_STATE_LEVEL_3, + LED_STATE_LEVEL_4, + LED_STATE_LEVEL_END, + LED_STATE_END +}; + class IDeviceManager { public: @@ -38,6 +57,11 @@ public: virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } virtual const IpcMission GetIpcMissiony(void) { return IpcMission::END; } + virtual const StatusCode ISetLedState(std::string ledName, LedState &CurrentState, + const unsigned int &KeepAliveTime, const unsigned int &BlinkPeriod) + { + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); + } }; bool CreateDeviceManagerModule(void); #endif \ No newline at end of file diff --git a/middleware/DeviceManager/src/DeviceManager.cpp b/middleware/DeviceManager/src/DeviceManager.cpp new file mode 100644 index 00000000..ab97471e --- /dev/null +++ b/middleware/DeviceManager/src/DeviceManager.cpp @@ -0,0 +1,55 @@ +/* + * 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 "DeviceManager.h" +#include + +const StatusCode DeviceManager::Init(void) +{ + std::vector> ledHals; + IHalCpp::GetInstance()->GetLedHals(ledHals); + + for (auto it = ledHals.begin(); it != ledHals.end(); ++it) { + std::shared_ptr ledHal = *it; + std::shared_ptr ledOut = + std::make_shared(ledHal, NEW_LED_STATE, DEFAULT_KEEP_ALIVE_TIME, LED_NOT_BLINK); + mLedManagers.push_back(ledOut); + } + return CreateStatusCode(STATUS_CODE_OK); +} + +const StatusCode DeviceManager::UnInit(void) +{ + if (!mLedManagers.empty()) { + mLedManagers.clear(); + } + + return CreateStatusCode(STATUS_CODE_OK); +} + +const IpcMission DeviceManager::GetIpcMissiony(void) { return IpcMission::TEST; } + +const StatusCode DeviceManager::ISetLedState(std::string ledName, LedState &CurrentState, + const unsigned int &KeepAliveTime, const unsigned int &BlinkPeriod) +{ + for (auto it = mLedManagers.begin(); it != mLedManagers.end(); ++it) { + std::shared_ptr ledOut = *it; + if (ledOut->GetLedHal()->GetLedName() == ledName) { + ledOut->SetLedState(CurrentState, KeepAliveTime, BlinkPeriod); + return CreateStatusCode(STATUS_CODE_OK); + } + } + return CreateStatusCode(STATUS_CODE_NOT_OK); +} diff --git a/middleware/DeviceManager/src/DeviceManager.h b/middleware/DeviceManager/src/DeviceManager.h new file mode 100644 index 00000000..0e53ad77 --- /dev/null +++ b/middleware/DeviceManager/src/DeviceManager.h @@ -0,0 +1,37 @@ +/* + * 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 DEVICEMANAGER_H +#define DEVICEMANAGER_H +#include "IDeviceManager.h" +#include "LedManager.h" + +class DeviceManager : public IDeviceManager +{ +public: + DeviceManager() = default; + virtual ~DeviceManager() = default; + + const StatusCode Init(void) override; + const StatusCode UnInit(void) override; + const IpcMission GetIpcMissiony(void) override; + const StatusCode ISetLedState(std::string ledName, LedState &CurrentState, + const unsigned int &KeepAliveTime = DEFAULT_KEEP_ALIVE_TIME, + const unsigned int &BlinkPeriod = LED_NOT_BLINK) override; + +private: + std::vector> mLedManagers; +}; + +#endif diff --git a/middleware/DeviceManager/src/LedManager.cpp b/middleware/DeviceManager/src/LedManager.cpp new file mode 100644 index 00000000..eceb2ccc --- /dev/null +++ b/middleware/DeviceManager/src/LedManager.cpp @@ -0,0 +1,65 @@ +/* + * 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" + +LedManager::LedManager() +{ + mLedHal = nullptr; + mCurrentState = NEW_LED_STATE; + mBlinkPeriod = LED_NOT_BLINK; + mKeepAliveTime = DEFAULT_KEEP_ALIVE_TIME; + mStateAliveTime = 0; +} + +LedManager::LedManager(std::shared_ptr &LedHal, const LedState &CurrentState, + const unsigned int &KeepAliveTime, const unsigned int &BlinkPeriod) +{ + mLedHal = LedHal; + mCurrentState = CurrentState; + mKeepAliveTime = KeepAliveTime; + mBlinkPeriod = BlinkPeriod; + mStateAliveTime = 0; +} + +std::shared_ptr LedManager::GetLedHal(void) { return mLedHal; } + +StatusCode LedManager::SetLedState(LedState &CurrentState, const unsigned int &KeepAliveTime, + const unsigned int &BlinkPeriod) +{ + mCurrentState = CurrentState; + mKeepAliveTime = KeepAliveTime; + mBlinkPeriod = BlinkPeriod; + return CreateStatusCode(STATUS_CODE_OK); +} + +StatusCode LedManager::GetLedState(LedState &CurrentState) +{ + CurrentState = mCurrentState; + return CreateStatusCode(STATUS_CODE_OK); +} + +StatusCode LedManager::BlinkOn(LedState CurrentState, unsigned int KeepAliveTime) +{ + mKeepAliveTime = KeepAliveTime; + return CreateStatusCode(STATUS_CODE_OK); +} + +StatusCode LedManager::BlinkOff(void) +{ + mCurrentState = LedState::LED_STATE_OFF; + return CreateStatusCode(STATUS_CODE_OK); +} diff --git a/middleware/DeviceManager/src/LedManager.h b/middleware/DeviceManager/src/LedManager.h new file mode 100644 index 00000000..e9caf07a --- /dev/null +++ b/middleware/DeviceManager/src/LedManager.h @@ -0,0 +1,58 @@ +/* + * 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 +#include + +constexpr LedState NEW_LED_STATE = LedState::LED_STATE_OFF; +constexpr unsigned int LED_NOT_BLINK = 0; +constexpr unsigned int BLINKING_FAST_MS = 500; +constexpr unsigned int BLINKING_SLOW_MS = 1000; +constexpr long int DEFAULT_KEEP_ALIVE_TIME = 1500; +constexpr unsigned int DELETED_LED_STATE = -1; +constexpr unsigned int DO_NOT_KEEP_ALIVE = -2; + +class LedManager +{ +public: + LedManager(); + LedManager(std::shared_ptr &LedHal, const LedState &CurrentState, + const unsigned int &KeepAliveTime = DEFAULT_KEEP_ALIVE_TIME, + const unsigned int &BlinkPeriod = LED_NOT_BLINK); + ~LedManager() = default; + StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_NOT_OK); } + StatusCode Unit(void) { return CreateStatusCode(STATUS_CODE_NOT_OK); } + +public: + std::shared_ptr GetLedHal(void); + StatusCode SetLedState(LedState &CurrentState, const unsigned int &KeepAliveTime = DEFAULT_KEEP_ALIVE_TIME, + const unsigned int &BlinkPeriod = LED_NOT_BLINK); + StatusCode GetLedState(LedState &CurrentState); + StatusCode BlinkOn(LedState CurrentState, unsigned int KeepAliveTime); + StatusCode BlinkOff(void); + +private: + std::shared_ptr mLedHal; + LedState mCurrentState; // 当前状态 + unsigned int mBlinkPeriod; // 闪烁频率 + unsigned int mKeepAliveTime; // 保持存活时间 + unsigned int mStateAliveTime; // 状态保持时间 +}; + +#endif