Improve:DeviceManager module.

This commit is contained in:
Fancy code 2024-02-15 12:34:32 -08:00
parent 1f862db3de
commit d19ccfc67b
26 changed files with 421 additions and 556 deletions

View File

@ -17,26 +17,25 @@
#include <memory>
std::shared_ptr<IHalCpp> &IHalCpp::GetInstance(std::shared_ptr<IHalCpp> *impl)
{
static std::shared_ptr<IHalCpp> instance = std::make_shared<IHalCpp>();
static bool instanceChanging = false;
if (impl && false == instanceChanging) {
// Don't use std::mutex for runing faster.
// Sleep for difference thread to release instance.
instanceChanging = true;
// std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Don't sleep and make sure that start fast.
if (instance.use_count() == 1) // bug?
{
LogInfo("Instance change succeed.\n");
static auto instance = std::make_shared<IHalCpp>();
if (impl) {
if (instance.use_count() == 1) {
LogInfo("Instance changed succeed.\n");
instance = *impl;
}
else {
LogError("[ error ] instance change failed, using by some one.\n");
LogError("Can't changing the instance becase of using by some one.\n");
}
instanceChanging = false;
}
if (instanceChanging) {
static std::shared_ptr<IHalCpp> tmporaryInstance = std::make_shared<IHalCpp>();
return tmporaryInstance;
}
return instance;
}
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<std::shared_ptr<VLedHal>> &ledHals)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode IHalCpp::GetAllKeys(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}

View File

@ -16,96 +16,45 @@
#define IHALCPP_H
#include "StatusCode.h"
#include <iostream>
#include <map>
#include <memory>
#include <vector>
using VirtualLedState = unsigned char;
using VirtualKeyEvent = unsigned char;
constexpr int INVALID_PERIOD = -1;
constexpr int PERIPHERAL_CHECK_PERIOD_MS = 100;
constexpr int IMEI_LEN = 15;
constexpr long int KEY_DO_NOT_HOLD_PRESSING = -1;
enum class KeyHalEvent
{
PRESSING = 0,
NOT_PRESSING,
END
};
class VKeyHalOwner
{
public:
VKeyHalOwner() = default;
virtual ~VKeyHalOwner() = default;
virtual void KeyEventTrigger(const KeyHalEvent &event) {}
virtual void TimerKeyEventTrigger(const KeyHalEvent &event) {}
virtual long int GetHoldPressingTimeMs(void) { return KEY_DO_NOT_HOLD_PRESSING; }
};
void CreateHalCppModule(void);
class VKeyHal
{
public:
VKeyHal() = default;
virtual ~VKeyHal() = default;
virtual void SetKeyHalOwner(std::shared_ptr<VKeyHalOwner> owner) {}
virtual void CheckKeyStatus(void) {}
virtual const std::string GetKeyName(void) { return "undefine"; }
virtual void GetHoldPressingTimeMs(long int &holdTimeMs, VirtualKeyEvent &event) {}
};
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)
virtual StatusCode SetHalLedState(const VirtualLedState &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<VLedHalOwner> 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:
IHalCpp() = default;
virtual ~IHalCpp() = default;
static std::shared_ptr<IHalCpp> &GetInstance(std::shared_ptr<IHalCpp> *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<std::shared_ptr<VLedHal>> &ledHals)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
virtual StatusCode Init(void);
virtual StatusCode UnInit(void);
virtual StatusCode GetLedHals(std::vector<std::shared_ptr<VLedHal>> &ledHals);
virtual StatusCode GetAllKeys(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys);
};
void CreateHalCppModule(void);
#endif

View File

@ -19,30 +19,18 @@
#include <memory>
#include <vector>
bool CreateDeviceManagerModule(void);
enum class KeyEvent
bool DestroyDeviceManagerModule(void);
using VirtualLedState = unsigned char;
using VirtualKeyEvent = unsigned char;
typedef struct key_status
{
SHORT_CLICK = 0,
HOLD_DOWN,
HOLD_UP,
END
};
enum class LedState
{
OFF = 0,
ON,
GREEN,
RED,
YELLOW,
LEVEL_0,
LEVEL_1,
LEVEL_2,
LEVEL_3,
LEVEL_4,
LEVEL_END,
END
};
key_status(const VirtualKeyEvent &keyEvent, const long int holdTimeMs)
: mKeyEvent(keyEvent), mHoldTimeMs(holdTimeMs)
{
}
const VirtualKeyEvent mKeyEvent;
const long int mHoldTimeMs;
} KeyStatus;
class IDeviceManager
{
public:
@ -51,7 +39,7 @@ public:
static std::shared_ptr<IDeviceManager> &GetInstance(std::shared_ptr<IDeviceManager> *impl = nullptr);
virtual const StatusCode Init(void);
virtual const StatusCode UnInit(void);
virtual const StatusCode SetLedState(const std::string &ledName, const LedState &currentState,
virtual const StatusCode SetLedState(const std::string &ledName, const VirtualLedState &currentState,
const unsigned int &KeepAliveTime, const unsigned int &blinkPeriod);
};
#endif

View File

@ -15,45 +15,18 @@
#include "DeviceManager.h"
#include "ILog.h"
#include "LedTimer.h"
#include "KeyManager.h"
#include <vector>
const StatusCode DeviceManager::Init(void)
{
std::vector<std::shared_ptr<VLedHal>> ledHals;
IHalCpp::GetInstance()->GetLedHals(ledHals);
for (auto it = ledHals.begin(); it != ledHals.end(); ++it) {
std::shared_ptr<VLedHal> ledHal = *it;
std::shared_ptr<LedManager> ledOut =
std::make_shared<LedManager>(ledHal, NEW_LED_STATE, DEFAULT_KEEP_ALIVE_TIME, LED_NOT_BLINK);
mLedManagers.push_back(ledOut);
LedTimer::GetInstance()->AddTimerLedManager(ledOut);
}
LedTimer::GetInstance()->Init();
KeyManager::GetInstance()->Init();
KeyManager::GetInstance()->StartTimer();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode DeviceManager::UnInit(void)
{
if (!mLedManagers.empty()) {
mLedManagers.clear();
}
LedTimer::GetInstance()->UnInit();
KeyManager::GetInstance()->UnInit();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode DeviceManager::SetLedState(const std::string &ledName, const LedState &currentState,
const unsigned int &KeepAliveTime, const unsigned int &blinkPeriod)
{
for (auto it = mLedManagers.begin(); it != mLedManagers.end(); ++it) {
std::shared_ptr<LedManager> ledOut = *it;
if (ledOut->GetLedHal()->GetLedName() == ledName) {
ledOut->SetLedState(currentState, KeepAliveTime, blinkPeriod);
return CreateStatusCode(STATUS_CODE_OK);
}
}
LogWarning("ledName(%s) not found", ledName.c_str());
return CreateStatusCode(STATUS_CODE_NOT_OK);
}

View File

@ -15,7 +15,7 @@
#ifndef DEVICEMANAGER_H
#define DEVICEMANAGER_H
#include "IDeviceManager.h"
#include "LedManager.h"
// #include "LedManager.h"
class DeviceManager : public IDeviceManager
{
@ -25,12 +25,12 @@ public:
const StatusCode Init(void) override;
const StatusCode UnInit(void) override;
const StatusCode SetLedState(const std::string &ledName, const LedState &currentState,
const unsigned int &keepAliveTime = DEFAULT_KEEP_ALIVE_TIME,
const unsigned int &blinkPeriod = LED_NOT_BLINK) override;
// const StatusCode SetLedState(const std::string &ledName, const VirtualLedState &currentState,
// const unsigned int &keepAliveTime = DEFAULT_KEEP_ALIVE_TIME,
// const unsigned int &blinkPeriod = LED_NOT_BLINK) override;
private:
std::vector<std::shared_ptr<LedManager>> mLedManagers;
// std::vector<std::shared_ptr<LedManager>> mLedManagers;
};
#endif

View File

@ -26,6 +26,13 @@ bool CreateDeviceManagerModule(void)
}
return false;
}
bool DestroyDeviceManagerModule(void)
{
auto instance = std::make_shared<IDeviceManager>();
IDeviceManager::GetInstance()->UnInit();
IDeviceManager::GetInstance(&instance);
return true;
}
std::shared_ptr<DeviceManagerMakePtr> &DeviceManagerMakePtr::GetInstance(std::shared_ptr<DeviceManagerMakePtr> *impl)
{
static auto instance = std::make_shared<DeviceManagerMakePtr>();

View File

@ -30,7 +30,7 @@ std::shared_ptr<IDeviceManager> &IDeviceManager::GetInstance(std::shared_ptr<IDe
}
const StatusCode IDeviceManager::Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
const StatusCode IDeviceManager::UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
const StatusCode IDeviceManager::SetLedState(const std::string &ledName, const LedState &currentState,
const StatusCode IDeviceManager::SetLedState(const std::string &ledName, const VirtualLedState &currentState,
const unsigned int &KeepAliveTime, const unsigned int &blinkPeriod)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);

View File

@ -14,83 +14,75 @@
*/
#include "KeyManager.h"
#include "ILog.h"
constexpr long int KEY_PRESSING = 0;
constexpr unsigned int NOT_A_HOLD_KEY_ACTION = 0;
KeyManager::KeyManager()
std::shared_ptr<KeyManager> &KeyManager::GetInstance(std::shared_ptr<KeyManager> *impl)
{
mKeyHal = nullptr;
mKeyActionReport = nullptr;
mPressingTime = KEY_NOT_PRESSING;
mLongClickTime = 0;
}
KeyManager::KeyManager(std::shared_ptr<VKeyHal> &keyHal, const KeyActionReport &keyAction,
const long int &longClickTime)
: mKeyHal(keyHal), mKeyActionReport(keyAction), mLongClickTime(longClickTime)
{
mPressingTime = KEY_NOT_PRESSING;
}
KeyManager::~KeyManager() {}
void KeyManager::KeyEventTrigger(const KeyHalEvent &event)
{
std::lock_guard<std::mutex> locker(mMutex);
ActionReport(mKeyHal->GetKeyName(), event);
}
void KeyManager::TimerKeyEventTrigger(const KeyHalEvent &event)
{
std::lock_guard<std::mutex> locker(mMutex);
if (IsKeyPressing()) {
ActionReport(mKeyHal->GetKeyName(), event);
}
}
long int KeyManager::GetHoldPressingTimeMs(void) { return mPressingTime; }
bool KeyManager::IsKeyPressing(void) { return mPressingTime >= KEY_PRESSING ? true : false; }
void KeyManager::Init(void) { mKeyHal->SetKeyHalOwner(shared_from_this()); }
void KeyManager::UnInit(void) {}
void KeyManager::ActionReport(const std::string &key, const KeyHalEvent &keyEvent)
{
if (KEY_PRESSING <= mPressingTime) {
mPressingTime += PERIPHERAL_CHECK_PERIOD_MS;
}
switch (keyEvent) {
case KeyHalEvent::PRESSING:
KeyPressingTrigger(key);
break;
case KeyHalEvent::NOT_PRESSING:
KeyNotPressingTrigger(key);
break;
default:
break;
}
}
void KeyManager::KeyPressingTrigger(const std::string &key)
{
if (mLongClickTime <= mPressingTime) // Do not support long click, it should be count in application code.
{
if (mKeyActionReport) {
// mKeyActionReport(key, KeyEvent::SF_KEY_ACTION_LONG_CLICK, NOT_A_HOLD_KEY_ACTION);
static auto instance = std::make_shared<KeyManager>();
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");
}
}
if (mPressingTime != KEY_NOT_PRESSING && mPressingTime % KEY_ACTION_HOLD_DWON == 0) {
if (mKeyActionReport) {
mKeyActionReport(key, KeyEvent::HOLD_DOWN, mPressingTime);
}
return instance;
}
void KeyManager::Init(void)
{
//
IHalCpp::GetInstance()->GetAllKeys(mAllKeyHal);
}
void KeyManager::UnInit(void)
{
//
mAllKeyHal.clear();
}
void KeyManager::StartTimer(void)
{
if (mAllKeyHal.size() == 0) {
LogError("StartTimer failed, no key to manager.\n");
return;
}
if (KEY_NOT_PRESSING == mPressingTime) {
mPressingTime = KEY_PRESSING;
auto timerThread = [](std::shared_ptr<KeyManager> timer) {
LogInfo("Key timer started.\n");
timer->Timer();
};
mTimer = std::thread(timerThread, shared_from_this());
}
void KeyManager::StopTimer(void)
{
mTimerRuning = false;
if (mTimer.joinable()) {
mTimer.join();
}
}
void KeyManager::KeyNotPressingTrigger(const std::string &key)
void KeyManager::Timer(void)
{
if (KEY_ACTION_SHORT_CLICK <= mPressingTime && mPressingTime < KEY_ACTION_HOLD_DWON) {
if (mKeyActionReport) {
mKeyActionReport(key, KeyEvent::SHORT_CLICK, NOT_A_HOLD_KEY_ACTION);
mTimerRuning = true;
std::map<std::string, std::shared_ptr<VKeyHal>>::iterator iter;
while (mTimerRuning) {
// for (int i = 0; i < static_cast<int>(SfKeyDefine::KEY_PIN_END); i++)
// {
// mKeyManagers[static_cast<SfKeyDefine>(i)]->TimerKeyEventHappened(static_cast<SfKeyDefine>(i),
// SfKeyEvent::KEY_EVENT_PRESSED, nullptr);
// }
for (iter = mAllKeyHal.begin(); iter != mAllKeyHal.end(); ++iter) {
std::shared_ptr<VKeyHal> keyHal = iter->second;
keyHal->CheckKeyStatus();
}
std::this_thread::sleep_for(std::chrono::milliseconds(PERIPHERAL_CHECK_PERIOD_MS));
}
if (KEY_ACTION_HOLD_DWON <= mPressingTime) {
if (mKeyActionReport) {
mKeyActionReport(key, KeyEvent::HOLD_UP, mPressingTime);
}
}
void KeyManager::GetAllKeysState(std::map<std::string, KeyStatus> &status)
{
std::map<std::string, std::shared_ptr<VKeyHal>>::iterator iter;
for (iter = mAllKeyHal.begin(); iter != mAllKeyHal.end(); ++iter) {
std::shared_ptr<VKeyHal> keyHal = iter->second;
long int holdTimeMs = 0;
VirtualKeyEvent holdPressingEvent = 0x00;
keyHal->GetHoldPressingTimeMs(holdTimeMs, holdPressingEvent);
KeyStatus result(holdPressingEvent, holdTimeMs);
status.insert(std::make_pair(iter->first, result));
}
mPressingTime = KEY_NOT_PRESSING;
}

View File

@ -16,37 +16,27 @@
#define KEY_MANAGER_H
#include "IDeviceManager.h"
#include "IHalCpp.h"
#include <functional>
#include <map>
#include <memory>
#include <mutex>
using KeyActionReport = std::function<void(const std::string &, const KeyEvent &, const unsigned int &)>;
constexpr int KEY_ACTION_LONG_CLICK = 1000 * 5;
constexpr int KEY_ACTION_SHORT_CLICK = 200;
constexpr int KEY_ACTION_HOLD_DWON = 500;
constexpr long int KEY_NOT_PRESSING = -1;
class KeyManager : public VKeyHalOwner, public std::enable_shared_from_this<KeyManager>
#include <thread>
class KeyManager : public std::enable_shared_from_this<KeyManager>
{
public:
KeyManager();
KeyManager(std::shared_ptr<VKeyHal> &keyHal, const KeyActionReport &keyAction,
const long int &longClickTime = KEY_ACTION_LONG_CLICK);
~KeyManager();
void KeyEventTrigger(const KeyHalEvent &event) override;
void TimerKeyEventTrigger(const KeyHalEvent &event) override;
long int GetHoldPressingTimeMs(void) override;
KeyManager() = default;
~KeyManager() = default;
static std::shared_ptr<KeyManager> &GetInstance(std::shared_ptr<KeyManager> *impl = nullptr);
void Init(void);
void UnInit(void);
void ActionReport(const std::string &key, const KeyHalEvent &keyEvent);
private:
void KeyPressingTrigger(const std::string &key);
void KeyNotPressingTrigger(const std::string &key);
bool IsKeyPressing(void);
void StartTimer(void);
void StopTimer(void);
void Timer(void);
void GetAllKeysState(std::map<std::string, KeyStatus> &status);
private:
std::mutex mMutex;
std::shared_ptr<VKeyHal> mKeyHal;
KeyActionReport mKeyActionReport;
long int mPressingTime;
long int mLongClickTime;
std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeyHal;
bool mTimerRuning;
std::thread mTimer;
};
#endif

View File

@ -1,65 +0,0 @@
/*
* 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<VLedHal> &LedHal, const LedState &urrentState, const unsigned int &KeepAliveTime,
const unsigned int &blinkPeriod)
{
mLedHal = LedHal;
mCurrentState = urrentState;
mKeepAliveTime = KeepAliveTime;
mBlinkPeriod = blinkPeriod;
mStateAliveTime = 0;
}
std::shared_ptr<VLedHal> LedManager::GetLedHal(void) { return mLedHal; }
StatusCode LedManager::SetLedState(const 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 &urrentState)
{
urrentState = mCurrentState;
return CreateStatusCode(STATUS_CODE_OK);
}
// StatusCode LedManager::BlinkOn(LedState urrentState, unsigned int KeepAliveTime)
// {
// mKeepAliveTime = KeepAliveTime;
// return CreateStatusCode(STATUS_CODE_OK);
// }
// StatusCode LedManager::BlinkOff(void)
// {
// mCurrentState = LedState::OFF;
// return CreateStatusCode(STATUS_CODE_OK);
// }

View File

@ -1,58 +0,0 @@
/*
* 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 <functional>
#include <mutex>
constexpr LedState NEW_LED_STATE = LedState::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<VLedHal> &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<VLedHal> GetLedHal(void);
StatusCode SetLedState(const 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<VLedHal> mLedHal;
LedState mCurrentState;
unsigned int mBlinkPeriod;
unsigned int mKeepAliveTime;
unsigned int mStateAliveTime;
};
#endif

View File

@ -1,75 +0,0 @@
/*
* 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 "LedTimer.h"
#include "ILog.h"
LedTimer::LedTimer() { mTimerRuning = false; }
std::shared_ptr<LedTimer> &LedTimer::GetInstance(std::shared_ptr<LedTimer> *impl)
{
static auto instance = std::make_shared<LedTimer>();
// if (impl)
// {
// instance = *impl;
// }
return instance;
}
// void LedTimer::Init(std::vector<std::shared_ptr<LedManager>> &LedManagers)
void LedTimer::Init(void)
{
// TimerLedManagers = LedManagers;
StartTimer();
}
void LedTimer::UnInit(void)
{
StopTimer();
TimerLedManagers.clear();
}
void LedTimer::StartTimer(void)
{
auto timerThread = [](std::shared_ptr<LedTimer> timer) { timer->Timer(); };
mTimer = std::thread(timerThread, shared_from_this());
}
void LedTimer::StopTimer(void)
{
mTimerRuning = false;
if (mTimer.joinable()) {
mTimer.join();
}
}
void LedTimer::Timer(void)
{
mTimerRuning = true;
while (mTimerRuning) {
mMutex.lock();
CheckState();
mMutex.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(LED_STATE_CHECK_PERIOD_MS));
}
}
const StatusCode LedTimer::AddTimerLedManager(std::shared_ptr<LedManager> &LedManager)
{
TimerLedManagers.push_back(LedManager);
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode LedTimer::CheckState(void)
{
int count = 0;
for (auto it = TimerLedManagers.begin(); it != TimerLedManagers.end(); ++it) {
std::shared_ptr<LedManager> LedManager = *it;
}
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -1,50 +0,0 @@
/*
* 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 LEDTIMER_H
#define LEDTIMER_H
#include "LedManager.h"
#include <map>
#include <memory>
#include <mutex>
#include <thread>
constexpr int LED_STATE_CHECK_PERIOD_MS = 100;
class LedTimer : public std::enable_shared_from_this<LedTimer>
{
public:
LedTimer();
~LedTimer() = default;
static std::shared_ptr<LedTimer> &GetInstance(std::shared_ptr<LedTimer> *impl = nullptr);
// void Init(std::vector<std::shared_ptr<LedManager>> &LedManagers);
void Init(void);
void UnInit(void);
void Timer(void);
const StatusCode CheckState(void);
const StatusCode AddTimerLedManager(std::shared_ptr<LedManager> &LedManager);
private:
void StartTimer(void);
void StopTimer(void);
private:
std::vector<std::shared_ptr<LedManager>> TimerLedManagers;
std::mutex mMutex;
bool mTimerRuning;
std::thread mTimer;
};
#endif

View File

@ -20,12 +20,54 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
aux_source_directory(./ SRC_FILES)
aux_source_directory(. SRC_FILES)
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME IHalTest)
set(TARGET_NAME HalTest)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} gtest gmock pthread Hal)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
endif()
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(
HalTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/hal
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make HalTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
HalTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/hal
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make HalTest_code_check
COMMAND make HalTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
add_subdirectory(tool)

View File

@ -1,3 +1,17 @@
/*
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>

View File

@ -1,22 +1,34 @@
#include "ILog.h"
/*
* 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 "IHal.h"
#include "ILog.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace IHalTest
{
// ../output_files/test/bin/IHalTest --gtest_filter=IHalTest.Demo
TEST(IHalTest, Demo)
{
ILogInit(LOG_EASYLOGGING);
CreateHalModule();
StatusCode code = IHalInit();
if (IsCodeOK(code))
{
PrintStringCode(code);
}
IHalUnInit();
DestroyHalModule();
ILogUnInit();
// ../output_files/test/bin/IHalTest --gtest_filter=IHalTest.Demo
TEST(IHalTest, Demo)
{
ILogInit(LOG_EASYLOGGING);
CreateHalModule();
StatusCode code = IHalInit();
if (IsCodeOK(code)) {
PrintStringCode(code);
}
}
IHalUnInit();
DestroyHalModule();
ILogUnInit();
}
} // namespace IHalTest

View File

@ -0,0 +1,56 @@
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
include_directories(
./src
./include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
# ${UTILS_SOURCE_PATH}/McuProtocol/include
# ${MIDDLEWARE_SOURCE_PATH}/Hal/src
# ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
# ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
)
# link_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
# )
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
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} Log)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(
HalTestTool_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${TEST_TOOL_SRC_FILES}
${CLANG_TIDY_CONFIG}
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/hal/tool
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
HalTestTool_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${TEST_TOOL_SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/hal/tool
)
add_custom_command(
TARGET ${TEST_TOOL_TARGET}
PRE_BUILD
COMMAND make HalTestTool_code_check
COMMAND make HalTestTool_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TEST_TOOL_TARGET})

View File

@ -0,0 +1,18 @@
/*
* 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 HAL_TEST_TOOL_H
#define HAL_TEST_TOOL_H
#endif

View File

@ -0,0 +1,15 @@
/*
* 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 "HalTestTool.h"

View File

@ -3,4 +3,4 @@
add_subdirectory(IpcConfig)
add_subdirectory(McuManager)
add_subdirectory(McuAskBase)
add_subdirectory(LedTest)
add_subdirectory(DeviceManager)

View File

@ -1,23 +1,26 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
#
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
#
include_directories(
.
./src
./include
./tool/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
# ${MIDDLEWARE_SOURCE_PATH}/IpcConfig/include
${UTILS_SOURCE_PATH}/UartDevice/include
${UTILS_SOURCE_PATH}/McuProtocol/include
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src
${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}/utils/UartDevice/tool/include
${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
${TEST_SOURCE_PATH}/middleware/McuAskBase/tool/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
#
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
@ -26,59 +29,57 @@ link_directories(
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# , SRC_FILES_MAIN
aux_source_directory(. SRC_FILES_MAIN)
# ./src, SRC_FILES
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
set(TARGET_NAME Led_Test)
#
set(TARGET_NAME DeviceManagerTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} DeviceManager gtest gmock pthread)
target_link_libraries(${TARGET_NAME} DeviceManager HalTestTool gtest gmock pthread)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(
Led_Test_code_check
DeviceManagerTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/LedTest
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/DeviceManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make Led_Test_code_check
COMMAND make DeviceManagerTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
Led_Test_code_format
DeviceManagerTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/LedTest
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/DeviceManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make Led_Test_code_check
COMMAND make Led_Test_code_format
COMMAND make DeviceManagerTest_code_check
COMMAND make DeviceManagerTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
define_file_name(${TARGET_NAME})
# add_subdirectory(tool)

View File

@ -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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,64 @@
/*
* 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 "IDeviceManager.h"
#include "ILog.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace DeviceManagerTest
{
class DeviceManagerTest : public testing::Test
{
public:
DeviceManagerTest() {}
virtual ~DeviceManagerTest() {}
static void SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
static void TearDownTestCase() { ILogUnInit(); }
virtual void SetUp()
{
CreateDeviceManagerModule();
// mLinuxTest = LinuxTest::CreateLinuxTest();
// std::shared_ptr<LinuxApiMock> test = mLinuxTest;
// LinuxApiMock::GetInstance(&test);
// LinuxApiMock::GetInstance()->Init();
// McuManagerTestTool::Init(mLinuxTest);
// CreateMcuManager();
}
virtual void TearDown()
{
// LinuxApiMock::GetInstance()->UnInit();
// mLinuxTest = std::make_shared<LinuxTest>();
// std::shared_ptr<LinuxApiMock> test = std::make_shared<LinuxApiMock>();
// LinuxApiMock::GetInstance(&test);
// McuManagerTestTool::UnInit();
// DestroyMcuManager();
DestroyDeviceManagerModule();
}
public:
// std::shared_ptr<LinuxTest> mLinuxTest;
};
// ../output_files/test/bin/DeviceManagerTest --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_Demo
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_Demo)
{
IDeviceManager::GetInstance()->Init();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IDeviceManager::GetInstance()->UnInit();
}
} // namespace DeviceManagerTest

View File

@ -1,9 +0,0 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -1,21 +0,0 @@
#include "IDeviceManager.h"
#include "ILog.h"
#include <gtest/gtest.h>
namespace Led_Test
{
// ../output_files/test/bin/Led_Test --gtest_filter=Led_Test.Demo
TEST(Led_Test, Demo)
{
// 初始化Log
CreateLogModule();
CreateDeviceManagerModule();
IDeviceManager::GetInstance()->Init();
LedState _Led_State = LED_STATE_RED;
IDeviceManager::GetInstance()->SetLedState("Led_testing", _Led_State, 10, 0);
IDeviceManager::GetInstance()->UnInit();
}
} /*namespace Led_Test*/

View File

@ -167,7 +167,7 @@ TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_RecvData)
TEST_F(UartDeviceMockTest, UNIT_UartDevice_AUTO_MultiThreadTest)
{
auto openThread = [](void) {
LogInfo("===========openThread \n");
LogInfo("openThread.\n");
void *object = CreateUartDevice(gUartDevice);
IUartOpen(object);
IUartDeviceFree(object);