Add device manager module files.

This commit is contained in:
fancy 2023-11-25 19:20:48 -08:00
parent f989e8fe2d
commit 81ce17838d
12 changed files with 305 additions and 14 deletions

View File

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
#include "MainThread.h" #include "MainThread.h"
#include "IHal.h" #include "IHalCpp.h"
#include "ILog.h" #include "ILog.h"
#include <thread> #include <thread>
MainThread::MainThread() { mMainThreadRuning = false; } MainThread::MainThread() { mMainThreadRuning = false; }
@ -38,11 +38,13 @@ StatusCode MainThread::Init(void)
CustomizationInit(); CustomizationInit();
mMainThreadRuning = true; mMainThreadRuning = true;
CreateAllModules(); CreateAllModules();
IHalInit(); // IHalInit();
IHalCpp::GetInstance()->Init();
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }
StatusCode MainThread::UnInit(void) StatusCode MainThread::UnInit(void)
{ {
IHalCpp::GetInstance()->UnInit();
DestoryAllModules(); DestoryAllModules();
ILogUnInit(); ILogUnInit();
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
@ -50,7 +52,8 @@ StatusCode MainThread::UnInit(void)
StatusCode MainThread::CreateAllModules(void) StatusCode MainThread::CreateAllModules(void)
{ {
// CreateLogModule(); // CreateLogModule();
CreateHalModule(); // CreateHalModule();
CreateHalCppModule();
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }
void MainThread::DestoryAllModules(void) {} void MainThread::DestoryAllModules(void) {}

View File

@ -18,6 +18,26 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
// enum KEY_EVENT
// {
// KEY_EVENT_NOT_PRESSING = 0,
// KEY_EVENT_PRESSING,
// KEY_EVENT_END
// };
// typedef struct v_key_owner VKeyOwner;
// typedef struct v_key_owner
// {
// // StatusCode (*init)(VKeyOwner *);
// // StatusCode (*un_init)(VKeyOwner *);
// void (*key_event_trigger)(VKeyOwner *, const enum KEY_EVENT);
// } VKeyOwner;
// typedef struct v_key_handle VKeyHandle;
// typedef struct v_key_handle
// {
// // StatusCode (*init)(VKeyHandle *);
// // StatusCode (*un_init)(VKeyHandle *);
// StatusCode (*set_owner)(VKeyHandle *, VKeyOwner *);
// } VKeyHandle;
typedef struct i_hal IHal; typedef struct i_hal IHal;
typedef struct i_hal typedef struct i_hal
{ {

View File

@ -16,6 +16,33 @@
#define IHALCPP_H #define IHALCPP_H
#include "StatusCode.h" #include "StatusCode.h"
#include <memory> #include <memory>
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; }
};
class VKeyHal
{
public:
VKeyHal() = default;
virtual ~VKeyHal() = default;
virtual void SetKeyHalOwner(std::shared_ptr<VKeyHalOwner> owner) {}
virtual const std::string GetKeyName(void) { return "undefine"; }
};
class IHalCpp class IHalCpp
{ {
public: public:
@ -25,4 +52,5 @@ public:
virtual StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } virtual StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
virtual StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } virtual StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
}; };
void CreateHalCppModule(void);
#endif #endif

View File

@ -20,7 +20,7 @@ class HalCpp : public IHalCpp
public: public:
HalCpp() = default; HalCpp() = default;
virtual ~HalCpp() = default; virtual ~HalCpp() = default;
StatusCode Init(void); StatusCode Init(void) override;
StatusCode UnInit(void); StatusCode UnInit(void) override;
}; };
#endif #endif

View File

@ -41,6 +41,15 @@ StatusCode DestroyHalModule(void)
ResetHalImpl(nullptr); ResetHalImpl(nullptr);
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }
void CreateHalCppModule(void)
{
auto instance = std::make_shared<IHalCpp>();
StatusCode code2 = HalMakePtr::GetInstance()->CreateHalSharePtr(instance);
if (IsCodeOK(code2)) {
LogInfo("IHal manager instance is ok.\n");
IHalCpp::GetInstance(&instance);
}
}
std::shared_ptr<HalMakePtr> &HalMakePtr::GetInstance(std::shared_ptr<HalMakePtr> *impl) std::shared_ptr<HalMakePtr> &HalMakePtr::GetInstance(std::shared_ptr<HalMakePtr> *impl)
{ {
static auto instance = std::make_shared<HalMakePtr>(); static auto instance = std::make_shared<HalMakePtr>();

View File

@ -8,7 +8,7 @@ include_directories(
./include ./include
${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/Config/include ${HAL_SOURCE_PATH}/include
) )
#do not rely on any other library #do not rely on any other library
#link_directories( #link_directories(
@ -36,10 +36,19 @@ add_custom_target(
-p ${PLATFORM_PATH}/cmake-shell -p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/DeviceManager WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/DeviceManager
) )
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
DeviceManager_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/DeviceManager
)
add_custom_command( add_custom_command(
TARGET ${TARGET_NAME} TARGET ${TARGET_NAME}
PRE_BUILD PRE_BUILD
COMMAND make DeviceManager_code_check COMMAND make DeviceManager_code_check
COMMAND make DeviceManager_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
) )
endif() endif()

View File

@ -21,6 +21,14 @@ enum class IpcMission
TEST = 0, TEST = 0,
END END
}; };
enum class KeyAction
{
SHORT_CLICK = 0,
// LONG_CLICK, // Do not support.
HOLD_DOWN,
HOLD_UP,
END
};
class IDeviceManager class IDeviceManager
{ {
public: public:

View File

@ -0,0 +1,41 @@
/*
* 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 "DeviceManagerMakePtr.h"
#include "ILog.h"
bool CreateDeviceManagerModule(void)
{
auto instance = std::make_shared<IDeviceManager>();
StatusCode code = DeviceManagerMakePtr::GetInstance()->CreateDeviceManager(instance);
if (IsCodeOK(code)) {
LogInfo("CreateDeviceManager is ok.\n");
IDeviceManager::GetInstance(&instance);
return true;
}
return false;
}
std::shared_ptr<DeviceManagerMakePtr> &DeviceManagerMakePtr::GetInstance(std::shared_ptr<DeviceManagerMakePtr> *impl)
{
static auto instance = std::make_shared<DeviceManagerMakePtr>();
if (impl) {
instance = *impl;
}
return instance;
}
const StatusCode DeviceManagerMakePtr::CreateDeviceManager(std::shared_ptr<IDeviceManager> &impl)
{
auto tmp = std::make_shared<IDeviceManager>();
impl = tmp;
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -0,0 +1,28 @@
/*
* 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 DEVICE_MANAGER_MAKE_PTR_H
#define DEVICE_MANAGER_MAKE_PTR_H
#include "IDeviceManager.h"
#include "StatusCode.h"
#include <memory>
class DeviceManagerMakePtr
{
public:
DeviceManagerMakePtr() = default;
virtual ~DeviceManagerMakePtr() = default;
static std::shared_ptr<DeviceManagerMakePtr> &GetInstance(std::shared_ptr<DeviceManagerMakePtr> *impl = nullptr);
virtual const StatusCode CreateDeviceManager(std::shared_ptr<IDeviceManager> &impl);
};
#endif

View File

@ -17,15 +17,12 @@
std::shared_ptr<IDeviceManager> &IDeviceManager::GetInstance(std::shared_ptr<IDeviceManager> *impl) std::shared_ptr<IDeviceManager> &IDeviceManager::GetInstance(std::shared_ptr<IDeviceManager> *impl)
{ {
static auto instance = std::make_shared<IDeviceManager>(); static auto instance = std::make_shared<IDeviceManager>();
if (impl) if (impl) {
{ if (instance.use_count() == 1) {
if (instance.use_count() == 1)
{
LogInfo("Instance changed succeed.\n"); LogInfo("Instance changed succeed.\n");
instance = *impl; instance = *impl;
} }
else else {
{
LogError("Can't changing the instance becase of using by some one.\n"); LogError("Can't changing the instance becase of using by some one.\n");
} }
} }

View File

@ -0,0 +1,96 @@
/*
* 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 "KeyManager.h"
#include "ILog.h"
constexpr long int KEY_PRESSING = 0;
constexpr unsigned int NOT_A_HOLD_KEY_ACTION = 0;
KeyManager::KeyManager()
{
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, KeyAction::SF_KEY_ACTION_LONG_CLICK, NOT_A_HOLD_KEY_ACTION);
}
}
if (mPressingTime != KEY_NOT_PRESSING && mPressingTime % KEY_ACTION_HOLD_DWON == 0) {
if (mKeyActionReport) {
mKeyActionReport(key, KeyAction::HOLD_DOWN, mPressingTime);
}
}
if (KEY_NOT_PRESSING == mPressingTime) {
mPressingTime = KEY_PRESSING;
}
}
void KeyManager::KeyNotPressingTrigger(const std::string &key)
{
if (KEY_ACTION_SHORT_CLICK <= mPressingTime && mPressingTime < KEY_ACTION_HOLD_DWON) {
if (mKeyActionReport) {
mKeyActionReport(key, KeyAction::SHORT_CLICK, NOT_A_HOLD_KEY_ACTION);
}
}
if (KEY_ACTION_HOLD_DWON <= mPressingTime) {
if (mKeyActionReport) {
mKeyActionReport(key, KeyAction::HOLD_UP, mPressingTime);
}
}
mPressingTime = KEY_NOT_PRESSING;
}

View File

@ -0,0 +1,52 @@
/*
* 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_MANAGER_H
#define KEY_MANAGER_H
#include "IDeviceManager.h"
#include "IHalCpp.h"
#include <functional>
#include <mutex>
using KeyActionReport = std::function<void(const std::string &, const KeyAction &, 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>
{
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;
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);
private:
std::mutex mMutex;
std::shared_ptr<VKeyHal> mKeyHal;
KeyActionReport mKeyActionReport;
long int mPressingTime;
long int mLongClickTime;
};
#endif