From 6a106e80a37c1473b32c9d2143b09a7039fd34c8 Mon Sep 17 00:00:00 2001 From: "xiaocong.ran" Date: Thu, 17 Feb 2022 19:55:19 +0800 Subject: [PATCH] timer Signed-off-by: xiaocong.ran Change-Id: If377d186ed4e97f493083020b56fa3e83a82fd18 --- .../dependency/hichain/hichain_connector.h | 1 + .../include/dependency/timer/dm_timer.h | 38 ++++++++++- .../devicestate/dm_device_state_manager.h | 25 +++++++- .../dependency/hichain/hichain_connector.cpp | 21 ++++++ .../src/dependency/timer/dm_timer.cpp | 15 +++-- .../src/device_manager_service.cpp | 2 +- .../devicestate/dm_device_state_manager.cpp | 64 ++++++++++++++++++- 7 files changed, 153 insertions(+), 13 deletions(-) diff --git a/services/devicemanagerservice/include/dependency/hichain/hichain_connector.h b/services/devicemanagerservice/include/dependency/hichain/hichain_connector.h index 99d534f6e..530b22df7 100644 --- a/services/devicemanagerservice/include/dependency/hichain/hichain_connector.h +++ b/services/devicemanagerservice/include/dependency/hichain/hichain_connector.h @@ -64,6 +64,7 @@ public: int32_t GetRelatedGroups(std::string DeviceId, std::vector &groupList); int32_t GetGroupInfo(std::string queryParams, std::vector &groupList); int32_t GetGroupInfo(const int32_t userId, std::string queryParams, std::vector &groupList); + int32_t DeleteTimeOutGroup(const char* deviceId); private: int64_t GenRequestId(); int32_t SyncGroups(std::string deviceId, std::vector &remoteGroupIdList); diff --git a/services/devicemanagerservice/include/dependency/timer/dm_timer.h b/services/devicemanagerservice/include/dependency/timer/dm_timer.h index 4bf8896ed..f5c98f646 100644 --- a/services/devicemanagerservice/include/dependency/timer/dm_timer.h +++ b/services/devicemanagerservice/include/dependency/timer/dm_timer.h @@ -29,9 +29,11 @@ namespace OHOS { namespace DistributedHardware { +class DmTimer; typedef void (*TimeoutHandle)(void *data); +typedef void (*TimerHandle)(const DmTimer &timer); -#define MAX_EVENTS 255 +#define MAXEVENTS 255 enum DmTimerStatus : int32_t { DM_STATUS_INIT = 0, @@ -46,9 +48,39 @@ public: DmTimer(const std::string &name); ~DmTimer(); DmTimerStatus Start(uint32_t timeOut, TimeoutHandle handle, void *data); + template + DmTimerStatus Start(uint32_t timeOut, TimerHandle handle, T *data) + { + if (mTimerName_.empty() || handle == nullptr || data == nullptr) { + LOGI("DmTimer is not init or param empty"); + return DmTimerStatus::DM_STATUS_FINISH; + } + LOGI("DmTimer %s start timeout(%d)", mTimerName_.c_str(), timeOut); + if (mStatus_ != DmTimerStatus::DM_STATUS_INIT) { + return DmTimerStatus::DM_STATUS_BUSY; + } + + mTimeOutSec_ = timeOut; + mTimerHandle_ = handle; + owner = data; + + if (CreateTimeFd()) { + return DmTimerStatus::DM_STATUS_CREATE_ERROR; + } + + mStatus_ = DmTimerStatus::DM_STATUS_RUNNING; + mThread_ = std::thread(&DmTimer::WiteforTimeout, this); + mThread_.detach(); + + return mStatus_; + }; + void Stop(int32_t code); void WaitForTimeout(); +public: + void *owner; + private: int32_t CreateTimeFd(); void Release(); @@ -57,10 +89,12 @@ private: DmTimerStatus mStatus_; uint32_t mTimeOutSec_; TimeoutHandle mHandle_; + TimerHandle mTimerHandle_; + void *mHandleOutData_; void *mHandleData_; int32_t mTimeFd_[2]; struct epoll_event mEv_; - struct epoll_event mEvents_[MAX_EVENTS]; + struct epoll_event mEvents_[MAXEVENTS]; int32_t mEpFd_; std::thread mThread_; std::string mTimerName_; diff --git a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h index a54ef47ef..cdd78c915 100644 --- a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h +++ b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h @@ -19,14 +19,29 @@ #include "device_manager_service_listener.h" #include "dm_adapter_manager.h" #include "softbus_connector.h" - +#include "dm_timer.h" +#include "hichain_connector.h" namespace OHOS { namespace DistributedHardware { +class HmDevice { +public: + HmDevice(std::string deviceId, std::shared_ptr stateMgr); + ~HmDevice(); + +public: + std::string mDeviceId; + std::map mGroupMap; + std::shared_ptr mOfflineTimerPtr; + std::shared_ptr deviceStateMgr; +}; + +#define OFFLINE_TIMEOUT 300 class DmDeviceStateManager final : public ISoftbusStateCallback, public std::enable_shared_from_this { public: DmDeviceStateManager(std::shared_ptr softbusConnector, - std::shared_ptr listener); + std::shared_ptr listener, + std::shared_ptr hiChainConnector); ~DmDeviceStateManager(); void OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &info); void OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info); @@ -34,6 +49,9 @@ public: void OnDeviceReady(const std::string &pkgName, const DmDeviceInfo &info); void OnProfileReady(const std::string &pkgName, const std::string deviceId); int32_t RegisterSoftbusStateCallback(); + void OnDeviceOnline(const DmDeviceInfo &deviceInfo); + void OnDeviceOffline(const DmDeviceInfo &deviceInfo); + void RmHichainGroup(HmDevice *hmDevice); private: std::shared_ptr softbusConnector_; @@ -41,7 +59,10 @@ private: std::shared_ptr listener_; std::map deviceStateMap_; std::map remoteDeviceInfos_; + std::map> timerMap_; + std::shared_ptr hiChainConnector_; std::string profileSoName_; + std::map mHmDeviceMap_ = {}; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/dependency/hichain/hichain_connector.cpp b/services/devicemanagerservice/src/dependency/hichain/hichain_connector.cpp index 22a50c39d..f130913cc 100644 --- a/services/devicemanagerservice/src/dependency/hichain/hichain_connector.cpp +++ b/services/devicemanagerservice/src/dependency/hichain/hichain_connector.cpp @@ -490,5 +490,26 @@ int32_t HiChainConnector::DeleteGroup(const int32_t userId, std::string &groupId } return DM_OK; } + +int32_t HiChainConnector::DeleteTimeOutGroup(const char* deviceId) +{ + LOGE("HiChainConnector::DeleteTimeOutGroup start"); + int32_t userId = MultipleUserConnector::GetCurrentAccountUserID(); + if (userId < 0) { + LOGE("get current process account user id failed"); + return DM_FAILED; + } + std::vector peerGroupInfoList; + GetRelatedGroups(deviceId, peerGroupInfoList); + char localDeviceId[DEVICE_UUID_LENGTH] = {0}; + GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH); + for (auto &group : peerGroupInfoList){ + if (deviceGroupManager_->isDeviceInGroup(userId, DM_PKG_NAME.c_str(), group.groupId.c_str(), localDeviceId)){ + DeleteGroup(group.groupId); + return DM_OK; + } + } + return DM_FAILED; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp b/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp index a33034f48..739cccf27 100644 --- a/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp +++ b/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp @@ -33,6 +33,7 @@ DmTimer::DmTimer(const std::string &name) mStatus_ = DmTimerStatus::DM_STATUS_INIT; mTimeOutSec_ = 0; mHandle_ = nullptr; + mTimerHandle_ = nullptr; mHandleData_ = nullptr; (void)memset_s(mTimeFd_, sizeof(mTimeFd_), 0, sizeof(mTimeFd_)); (void)memset_s(&mEv_, sizeof(mEv_), 0, sizeof(mEv_)); @@ -64,14 +65,14 @@ DmTimerStatus DmTimer::Start(uint32_t timeOut, TimeoutHandle handle, void *data) mTimeOutSec_ = timeOut; mHandle_ = handle; - mHandleData_ = data; + mHandleOutData_ = data; if (CreateTimeFd()) { return DmTimerStatus::DM_STATUS_CREATE_ERROR; } mStatus_ = DmTimerStatus::DM_STATUS_RUNNING; - mThread_ = std::thread(&DmTimer::WaitForTimeout, this); + mThread_ = std::thread(&DmTimer::WiteforTimeout, this); mThread_.detach(); return mStatus_; @@ -103,8 +104,7 @@ void DmTimer::WaitForTimeout() return; } LOGI("DmTimer %s start timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_); - - int32_t nfds = epoll_wait(mEpFd_, mEvents_, MAX_EVENTS, mTimeOutSec_ * MILL_SECONDS_PER_SECOND); + int32_t nfds = epoll_wait(mEpFd_, mEvents_, MAXEVENTS, mTimeOutSec_ * MILL_SECONDS_PER_SECOND); if (nfds < 0) { LOGE("DmTimer %s epoll_wait returned n=%d, error: %d", mTimerName_.c_str(), nfds, errno); } @@ -123,7 +123,10 @@ void DmTimer::WaitForTimeout() return; } - mHandle_(mHandleData_); + if (mHandle_) + mHandle_(mHandleOutData_); + if (mTimerHandle_) + mTimerHandle_(*this); Release(); LOGE("DmTimer %s end timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_); @@ -147,7 +150,7 @@ int32_t DmTimer::CreateTimeFd() mEv_.data.fd = mTimeFd_[0]; mEv_.events = EPOLLIN | EPOLLET; - mEpFd_ = epoll_create(MAX_EVENTS); + mEpFd_ = epoll_create(MAXEVENTS); ret = epoll_ctl(mEpFd_, EPOLL_CTL_ADD, mTimeFd_[0], &mEv_); if (ret != 0) { Release(); diff --git a/services/devicemanagerservice/src/device_manager_service.cpp b/services/devicemanagerservice/src/device_manager_service.cpp index 8a02d7265..88541338f 100644 --- a/services/devicemanagerservice/src/device_manager_service.cpp +++ b/services/devicemanagerservice/src/device_manager_service.cpp @@ -77,7 +77,7 @@ int32_t DeviceManagerService::Init() } } if (deviceStateMgr_ == nullptr) { - deviceStateMgr_ = std::make_shared(softbusConnector_, listener_); + deviceStateMgr_ = std::make_shared(softbusConnector_, listener_, hiChainConnector_); if (deviceStateMgr_ == nullptr) { LOGE("Init failed, deviceStateMgr_ apply for failure"); return DM_MAKE_SHARED_FAIL; diff --git a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp index 8fc7214ac..1cf0753f7 100644 --- a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp +++ b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp @@ -20,9 +20,21 @@ #include "dm_log.h" namespace OHOS { namespace DistributedHardware { +static void TimeOut(const DmTimer &timer) +{ + LOGE("time out "); + if (timer.owner == nullptr) { + LOGE("OnDeviceOfflineTimeOut hmDevice owner = nullptr"); + return; + } + std::shared_ptr deviceStateMgr = ((HmDevice *)timer.owner)->deviceStateMgr; + deviceStateMgr->RmHichainGroup((HmDevice *)timer.owner); +} + DmDeviceStateManager::DmDeviceStateManager(std::shared_ptr softbusConnector, - std::shared_ptr listener) - : softbusConnector_(softbusConnector), listener_(listener) + std::shared_ptr listener, + std::shared_ptr hiChainConnector) + : softbusConnector_(softbusConnector), listener_(listener), hiChainConnector_(hiChainConnector) { LOGI("DmDeviceStateManager constructor"); profileSoName_ = "libdevicemanagerext_profile.z.so"; @@ -37,6 +49,7 @@ DmDeviceStateManager::~DmDeviceStateManager() void DmDeviceStateManager::OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &info) { LOGI("DmDeviceStateManager::OnDeviceOnline in"); + OnDeviceOnline(info); DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); std::shared_ptr profileAdapter = adapterMgrPtr.GetProfileAdapter(profileSoName_); if (profileAdapter == nullptr) { @@ -66,6 +79,7 @@ void DmDeviceStateManager::OnDeviceOnline(const std::string &pkgName, const DmDe void DmDeviceStateManager::OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info) { + OnDeviceOffline(info); DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); std::shared_ptr profileAdapter = adapterMgrPtr.GetProfileAdapter(profileSoName_); if (profileAdapter == nullptr) { @@ -113,5 +127,51 @@ int32_t DmDeviceStateManager::RegisterSoftbusStateCallback() std::shared_ptr(shared_from_this())); return DM_OK; } + +void DmDeviceStateManager::OnDeviceOnline(const DmDeviceInfo &deviceInfo) +{ + std::string deviceId; + softbusConnector_->GetUdidByNetworkId(deviceInfo.deviceId, deviceId); + LOGI("Device<%s>Online", deviceId.c_str()); + auto iter = mHmDeviceMap_.find(deviceId); + if (iter != mHmDeviceMap_.end()) { + iter->second->mOfflineTimerPtr->Stop(0); + return; + } + + HmDevice *hmDevice = new HmDevice(deviceId, shared_from_this()); + mHmDeviceMap_[hmDevice->mDeviceId] = hmDevice; +} + +void DmDeviceStateManager::OnDeviceOffline(const DmDeviceInfo &deviceInfo) +{ + std::string deviceId; + softbusConnector_->GetUdidByNetworkId(deviceInfo.deviceId, deviceId); + LOGI("Device<%s>Offline", deviceId.c_str()); + for (auto &iter : mHmDeviceMap_) { + if (iter.second->mDeviceId.compare(deviceId) == 0) { + iter.second->mOfflineTimerPtr->Start(OFFLINE_TIMEOUT, TimeOut, iter.second); + } + } +} + +void DmDeviceStateManager::RmHichainGroup(HmDevice *hmDevice) +{ + LOGI("Remove DmDevice<%s> Hichain Group", hmDevice->mDeviceId.c_str()); + hiChainConnector_->DeleteTimeOutGroup(hmDevice->mDeviceId.c_str()); +} + +HmDevice::HmDevice(std::string deviceId, std::shared_ptr stateMgr) +{ + mDeviceId = deviceId; + std::string timerName = "HmDeviceTimer"; + mOfflineTimerPtr = std::make_shared(timerName); + deviceStateMgr = stateMgr; +} + +HmDevice::~HmDevice() +{ + mOfflineTimerPtr = nullptr; +} } // namespace DistributedHardware } // namespace OHOS -- Gitee