diff --git a/application/main/src/MainThread.cpp b/application/main/src/MainThread.cpp index 659b7529..b643b685 100644 --- a/application/main/src/MainThread.cpp +++ b/application/main/src/MainThread.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ #include "MainThread.h" -#include "IHal.h" +#include "IHalCpp.h" #include "ILog.h" #include MainThread::MainThread() { mMainThreadRuning = false; } @@ -38,11 +38,13 @@ StatusCode MainThread::Init(void) CustomizationInit(); mMainThreadRuning = true; CreateAllModules(); - IHalInit(); + // IHalInit(); + IHalCpp::GetInstance()->Init(); return CreateStatusCode(STATUS_CODE_OK); } StatusCode MainThread::UnInit(void) { + IHalCpp::GetInstance()->UnInit(); DestoryAllModules(); ILogUnInit(); return CreateStatusCode(STATUS_CODE_OK); @@ -50,7 +52,8 @@ StatusCode MainThread::UnInit(void) StatusCode MainThread::CreateAllModules(void) { // CreateLogModule(); - CreateHalModule(); + // CreateHalModule(); + CreateHalCppModule(); return CreateStatusCode(STATUS_CODE_OK); } void MainThread::DestoryAllModules(void) {} diff --git a/hal/include/IHal.h b/hal/include/IHal.h index be031b3e..2ab59dc5 100644 --- a/hal/include/IHal.h +++ b/hal/include/IHal.h @@ -18,6 +18,26 @@ #ifdef __cplusplus extern "C" { #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 { diff --git a/hal/include/IHalCpp.h b/hal/include/IHalCpp.h index 625f7a34..d7e06990 100644 --- a/hal/include/IHalCpp.h +++ b/hal/include/IHalCpp.h @@ -16,6 +16,33 @@ #define IHALCPP_H #include "StatusCode.h" #include +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 owner) {} + virtual const std::string GetKeyName(void) { return "undefine"; } +}; class IHalCpp { public: @@ -25,4 +52,5 @@ public: virtual StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } virtual StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } }; +void CreateHalCppModule(void); #endif \ No newline at end of file diff --git a/hal/src/HalCpp.h b/hal/src/HalCpp.h index 24a5b12f..1017cc2b 100644 --- a/hal/src/HalCpp.h +++ b/hal/src/HalCpp.h @@ -20,7 +20,7 @@ class HalCpp : public IHalCpp public: HalCpp() = default; virtual ~HalCpp() = default; - StatusCode Init(void); - StatusCode UnInit(void); + StatusCode Init(void) override; + StatusCode UnInit(void) override; }; #endif \ No newline at end of file diff --git a/hal/src/HalMakePtr.cpp b/hal/src/HalMakePtr.cpp index 54a5c595..d0391883 100644 --- a/hal/src/HalMakePtr.cpp +++ b/hal/src/HalMakePtr.cpp @@ -41,6 +41,15 @@ StatusCode DestroyHalModule(void) ResetHalImpl(nullptr); return CreateStatusCode(STATUS_CODE_OK); } +void CreateHalCppModule(void) +{ + auto instance = std::make_shared(); + StatusCode code2 = HalMakePtr::GetInstance()->CreateHalSharePtr(instance); + if (IsCodeOK(code2)) { + LogInfo("IHal manager instance is ok.\n"); + 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 696ede1b..5c8e96fc 100644 --- a/middleware/DeviceManager/CMakeLists.txt +++ b/middleware/DeviceManager/CMakeLists.txt @@ -8,7 +8,7 @@ include_directories( ./include ${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/Log/include - ${UTILS_SOURCE_PATH}/Config/include + ${HAL_SOURCE_PATH}/include ) #do not rely on any other library #link_directories( @@ -36,10 +36,19 @@ add_custom_target( -p ${PLATFORM_PATH}/cmake-shell 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( TARGET ${TARGET_NAME} PRE_BUILD COMMAND make DeviceManager_code_check + COMMAND make DeviceManager_code_format WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ ) endif() \ No newline at end of file diff --git a/middleware/DeviceManager/include/IDeviceManager.h b/middleware/DeviceManager/include/IDeviceManager.h index b2b930e3..f83e1e98 100644 --- a/middleware/DeviceManager/include/IDeviceManager.h +++ b/middleware/DeviceManager/include/IDeviceManager.h @@ -21,6 +21,14 @@ enum class IpcMission TEST = 0, END }; +enum class KeyAction +{ + SHORT_CLICK = 0, + // LONG_CLICK, // Do not support. + HOLD_DOWN, + HOLD_UP, + END +}; class IDeviceManager { public: diff --git a/middleware/DeviceManager/src/DeviceManagerMakePtr.cpp b/middleware/DeviceManager/src/DeviceManagerMakePtr.cpp new file mode 100644 index 00000000..8f6d0c1b --- /dev/null +++ b/middleware/DeviceManager/src/DeviceManagerMakePtr.cpp @@ -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(); + 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::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) { + instance = *impl; + } + return instance; +} +const StatusCode DeviceManagerMakePtr::CreateDeviceManager(std::shared_ptr &impl) +{ + auto tmp = std::make_shared(); + impl = tmp; + return CreateStatusCode(STATUS_CODE_OK); +} \ No newline at end of file diff --git a/middleware/DeviceManager/src/DeviceManagerMakePtr.h b/middleware/DeviceManager/src/DeviceManagerMakePtr.h new file mode 100644 index 00000000..4132b0e1 --- /dev/null +++ b/middleware/DeviceManager/src/DeviceManagerMakePtr.h @@ -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 +class DeviceManagerMakePtr +{ +public: + DeviceManagerMakePtr() = default; + virtual ~DeviceManagerMakePtr() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual const StatusCode CreateDeviceManager(std::shared_ptr &impl); +}; +#endif \ No newline at end of file diff --git a/middleware/DeviceManager/src/IDeviceManager.cpp b/middleware/DeviceManager/src/IDeviceManager.cpp index 416b30b6..88abfdfd 100644 --- a/middleware/DeviceManager/src/IDeviceManager.cpp +++ b/middleware/DeviceManager/src/IDeviceManager.cpp @@ -3,9 +3,9 @@ * 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. @@ -17,15 +17,12 @@ std::shared_ptr &IDeviceManager::GetInstance(std::shared_ptr *impl) { static auto instance = std::make_shared(); - if (impl) - { - if (instance.use_count() == 1) - { + if (impl) { + if (instance.use_count() == 1) { LogInfo("Instance changed succeed.\n"); instance = *impl; } - else - { + else { LogError("Can't changing the instance becase of using by some one.\n"); } } diff --git a/middleware/DeviceManager/src/KeyManager.cpp b/middleware/DeviceManager/src/KeyManager.cpp new file mode 100644 index 00000000..8b48d903 --- /dev/null +++ b/middleware/DeviceManager/src/KeyManager.cpp @@ -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 &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 locker(mMutex); + ActionReport(mKeyHal->GetKeyName(), event); +} +void KeyManager::TimerKeyEventTrigger(const KeyHalEvent &event) +{ + std::lock_guard 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; +} \ No newline at end of file diff --git a/middleware/DeviceManager/src/KeyManager.h b/middleware/DeviceManager/src/KeyManager.h new file mode 100644 index 00000000..2ae047c4 --- /dev/null +++ b/middleware/DeviceManager/src/KeyManager.h @@ -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 +#include +using KeyActionReport = std::function; +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 +{ +public: + KeyManager(); + KeyManager(std::shared_ptr &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 mKeyHal; + KeyActionReport mKeyActionReport; + long int mPressingTime; + long int mLongClickTime; +}; +#endif \ No newline at end of file