mirror of
https://github.com/openharmony/device_manager.git
synced 2026-07-21 06:05:22 -04:00
修复设备信任周期超期后无法重新PIN码认证问题
Signed-off-by: renguang1116 <renguang@huawei.com>
This commit is contained in:
@@ -187,8 +187,8 @@ const std::string ETH_PORT = "ETH_PORT";
|
||||
// ACE
|
||||
const int32_t ACE_X = 50;
|
||||
const int32_t ACE_Y = 300;
|
||||
const int32_t ACE_WIDTH = 600;
|
||||
const int32_t ACE_HEIGHT = 400;
|
||||
const int32_t ACE_WIDTH = 580;
|
||||
const int32_t ACE_HEIGHT = 520;
|
||||
const std::string EVENT_CONFIRM = "EVENT_CONFIRM";
|
||||
const std::string EVENT_CANCEL = "EVENT_CANCEL";
|
||||
const std::string EVENT_INIT = "EVENT_INIT";
|
||||
|
||||
@@ -153,7 +153,7 @@ public:
|
||||
int32_t SetAuthResponseState(std::shared_ptr<AuthResponseState> authResponseState);
|
||||
int32_t GetPinCode();
|
||||
std::string GenerateGroupName();
|
||||
int32_t HandleAuthenticateTimeout();
|
||||
void HandleAuthenticateTimeout(std::string name);
|
||||
void CancelDisplay();
|
||||
int32_t GeneratePincode();
|
||||
void ShowConfigDialog();
|
||||
@@ -176,7 +176,7 @@ private:
|
||||
std::shared_ptr<DmAuthRequestContext> authRequestContext_;
|
||||
std::shared_ptr<DmAuthResponseContext> authResponseContext_;
|
||||
std::shared_ptr<AuthMessageProcessor> authMessageProcessor_;
|
||||
std::map<std::string, std::shared_ptr<DmTimer>> timerMap_;
|
||||
std::shared_ptr<DmTimer> timer_;
|
||||
std::shared_ptr<DmAbilityManager> dmAbilityMgr_;
|
||||
bool isCryptoSupport_ = false;
|
||||
bool isFinishOfLocal_ = true;
|
||||
|
||||
@@ -62,7 +62,7 @@ public:
|
||||
int32_t DeleteGroup(const int32_t userId, std::string &groupId);
|
||||
bool IsDevicesInGroup(std::string hostDevice, std::string peerDevice);
|
||||
int32_t GetRelatedGroups(std::string DeviceId, std::vector<GroupInfo> &groupList);
|
||||
int32_t GetGroupInfo(std::string queryParams, std::vector<GroupInfo> &groupList);
|
||||
bool GetGroupInfo(const std::string &queryParams, std::vector<GroupInfo> &groupList);
|
||||
int32_t GetGroupInfo(const int32_t userId, std::string queryParams, std::vector<GroupInfo> &groupList);
|
||||
int32_t DeleteTimeOutGroup(const char* deviceId);
|
||||
private:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* 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
|
||||
@@ -13,67 +13,94 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TIMER_H
|
||||
#define TIMER_H
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#if !defined(__LITEOS_M__)
|
||||
#include <sys/epoll.h>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <cstdio>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#ifndef DM_TIMER_H
|
||||
#define DM_TIMER_H
|
||||
|
||||
#include "dm_log.h"
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
class DmTimer;
|
||||
typedef void (*TimeoutHandle)(void *data, DmTimer& timer);
|
||||
constexpr int32_t DELAY_TICK_MILLSECONDS = 1000;
|
||||
typedef std::chrono::steady_clock::time_point timerPoint;
|
||||
typedef std::chrono::steady_clock steadyClock;
|
||||
typedef std::chrono::duration<int64_t, std::ratio<1, 1000>> timerDuration;
|
||||
using TimerCallback = std::function<void (std::string name)>;
|
||||
const int32_t MILLISECOND_TO_SECOND = 1000;
|
||||
const int32_t MIN_TIME_OUT = 0;
|
||||
const int32_t MAX_TIME_OUT = 300;
|
||||
|
||||
#define MAX_EVENTS 255
|
||||
class Timer {
|
||||
public:
|
||||
Timer(std::string name, int32_t time, TimerCallback callback);
|
||||
~Timer() {};
|
||||
public:
|
||||
std::string timerName_;
|
||||
timerPoint expire_;
|
||||
bool state_;
|
||||
int32_t timeOut_;
|
||||
TimerCallback callback_;
|
||||
};
|
||||
|
||||
enum DmTimerStatus : int32_t {
|
||||
DM_STATUS_INIT = 0,
|
||||
DM_STATUS_RUNNING = 1,
|
||||
DM_STATUS_BUSY = 2,
|
||||
DM_STATUS_CREATE_ERROR = 3,
|
||||
DM_STATUS_FINISH = 6,
|
||||
struct TimerCmpare {
|
||||
public:
|
||||
bool operator () (std::shared_ptr<Timer> frontTimer, std::shared_ptr<Timer> timer)
|
||||
{
|
||||
int32_t frontTimerOut = frontTimer->timeOut_ - (std::chrono::duration_cast<timerDuration>(steadyClock::now()
|
||||
- frontTimer->expire_).count() / MILLISECOND_TO_SECOND);
|
||||
int32_t timerOut = timer->timeOut_ - (std::chrono::duration_cast<timerDuration>(steadyClock::now()
|
||||
- timer->expire_).count() / MILLISECOND_TO_SECOND);
|
||||
return frontTimerOut > timerOut;
|
||||
}
|
||||
};
|
||||
|
||||
class DmTimer {
|
||||
public:
|
||||
DmTimer(const std::string &name);
|
||||
DmTimer();
|
||||
~DmTimer();
|
||||
DmTimerStatus Start(uint32_t timeOut, TimeoutHandle handle, void *data);
|
||||
void Stop(int32_t code);
|
||||
void WaitForTimeout();
|
||||
std::string GetTimerName();
|
||||
private:
|
||||
int32_t CreateTimeFd();
|
||||
void Release();
|
||||
|
||||
private:
|
||||
DmTimerStatus mStatus_;
|
||||
uint32_t mTimeOutSec_;
|
||||
TimeoutHandle mHandle_;
|
||||
void *mHandleData_;
|
||||
int32_t mTimeFd_[2];
|
||||
#if defined(__LITEOS_M__)
|
||||
void *timerId = NULL;
|
||||
#else
|
||||
struct epoll_event mEv_;
|
||||
struct epoll_event mEvents_[MAX_EVENTS];
|
||||
int32_t mEpFd_;
|
||||
std::thread mThread_;
|
||||
std::mutex mTimerLock_;
|
||||
#endif
|
||||
/**
|
||||
* @tc.name: DmTimer::StartTimer
|
||||
* @tc.desc: start timer running
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
int32_t StartTimer(std::string name, int32_t time, TimerCallback callback);
|
||||
|
||||
std::string mTimerName_;
|
||||
/**
|
||||
* @tc.name: DmTimer::DeleteTimer
|
||||
* @tc.desc: delete timer
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
int32_t DeleteTimer(std::string timerName);
|
||||
|
||||
/**
|
||||
* @tc.name: DmTimer::DeleteAll
|
||||
* @tc.desc: delete all timer
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
int32_t DeleteAll();
|
||||
|
||||
/**
|
||||
* @tc.name: DmTimer::TimerRunning
|
||||
* @tc.desc: timer running
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
int32_t TimerRunning();
|
||||
private:
|
||||
mutable std::mutex timerMutex_;
|
||||
mutable std::mutex timerStateMutex_;
|
||||
std::priority_queue<std::shared_ptr<Timer>, std::vector<std::shared_ptr<Timer>>, TimerCmpare> timerQueue_;
|
||||
std::map<std::string, std::shared_ptr<Timer>> timerMap_;
|
||||
std::atomic<bool> timerState_ {false};
|
||||
std::condition_variable runTimerCondition_;
|
||||
std::condition_variable stopTimerCondition_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -58,7 +58,7 @@ public:
|
||||
int32_t RegisterSoftbusStateCallback();
|
||||
void RegisterOffLineTimer(const DmDeviceInfo &deviceInfo);
|
||||
void StartOffLineTimer(const DmDeviceInfo &deviceInfo);
|
||||
void DeleteTimeOutGroup(std::string stateTimer);
|
||||
void DeleteTimeOutGroup(std::string name);
|
||||
void RegisterDevStateCallback(const std::string &pkgName, const std::string &extra);
|
||||
void UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra);
|
||||
|
||||
@@ -74,6 +74,7 @@ private:
|
||||
std::map<std::string, DmDeviceInfo> remoteDeviceInfos_;
|
||||
std::map<std::string, std::string> decisionInfos_;
|
||||
std::map<std::string, StateTimerInfo> stateTimerInfoMap_;
|
||||
std::shared_ptr<DmTimer> timer_;
|
||||
std::shared_ptr<HiChainConnector> hiChainConnector_;
|
||||
std::string decisionSoName_;
|
||||
};
|
||||
|
||||
@@ -41,14 +41,14 @@ public:
|
||||
void OnDeviceFound(const std::string &pkgName, const DmDeviceInfo &info);
|
||||
void OnDiscoverySuccess(const std::string &pkgName, int32_t subscribeId);
|
||||
void OnDiscoveryFailed(const std::string &pkgName, int32_t subscribeId, int32_t failedReason);
|
||||
void HandleDiscoveryTimeout();
|
||||
void HandleDiscoveryTimeout(std::string name);
|
||||
|
||||
private:
|
||||
std::shared_ptr<SoftbusConnector> softbusConnector_;
|
||||
std::shared_ptr<DeviceManagerServiceListener> listener_;
|
||||
std::queue<std::string> discoveryQueue_;
|
||||
std::map<std::string, DmDiscoveryContext> discoveryContextMap_;
|
||||
std::shared_ptr<DmTimer> discoveryTimer_;
|
||||
std::shared_ptr<DmTimer> timer_;
|
||||
};
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
|
||||
@@ -41,23 +41,6 @@ const int32_t WAIT_REQUEST_TIMEOUT = 10;
|
||||
const int32_t CANCEL_PIN_CODE_DISPLAY = 1;
|
||||
const int32_t DEVICE_ID_HALF = 2;
|
||||
|
||||
static void TimeOut(void *data, DmTimer& timer)
|
||||
{
|
||||
LOGI("time out %s", timer.GetTimerName().c_str());
|
||||
if (data == nullptr || timer.GetTimerName().find(TIMER_PREFIX) != TIMER_DEFAULT) {
|
||||
LOGE("time out is not our timer");
|
||||
return;
|
||||
}
|
||||
|
||||
DmAuthManager *authMgr = (DmAuthManager *)data;
|
||||
if (authMgr == nullptr) {
|
||||
LOGE("authMgr is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
authMgr->HandleAuthenticateTimeout();
|
||||
}
|
||||
|
||||
DmAuthManager::DmAuthManager(std::shared_ptr<SoftbusConnector> softbusConnector,
|
||||
std::shared_ptr<DeviceManagerServiceListener> listener,
|
||||
std::shared_ptr<HiChainConnector> hiChainConnector)
|
||||
@@ -101,6 +84,14 @@ int32_t DmAuthManager::AuthenticateDevice(const std::string &pkgName, int32_t au
|
||||
return DM_INPUT_PARA_EMPTY;
|
||||
}
|
||||
|
||||
authPtr_ = authenticationMap_[authType];
|
||||
if (timer_ == nullptr) {
|
||||
timer_ = std::make_shared<DmTimer>();
|
||||
}
|
||||
timer_->StartTimer(AUTHENTICATE_TIMEOUT_TASK, AUTHENTICATE_TIMEOUT,
|
||||
[this] (std::string name) {
|
||||
DmAuthManager::HandleAuthenticateTimeout(name);
|
||||
});
|
||||
authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(shared_from_this());
|
||||
authResponseContext_ = std::make_shared<DmAuthResponseContext>();
|
||||
authRequestContext_ = std::make_shared<DmAuthRequestContext>();
|
||||
@@ -131,9 +122,6 @@ int32_t DmAuthManager::AuthenticateDevice(const std::string &pkgName, int32_t au
|
||||
authRequestState_->SetAuthManager(shared_from_this());
|
||||
authRequestState_->SetAuthContext(authRequestContext_);
|
||||
authRequestState_->Enter();
|
||||
std::shared_ptr<DmTimer> authenticateStartTimer = std::make_shared<DmTimer>(AUTHENTICATE_TIMEOUT_TASK);
|
||||
timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer;
|
||||
authenticateStartTimer->Start(AUTHENTICATE_TIMEOUT, TimeOut, this);
|
||||
LOGI("DmAuthManager::AuthenticateDevice complete");
|
||||
return DM_OK;
|
||||
}
|
||||
@@ -174,12 +162,11 @@ int32_t DmAuthManager::VerifyAuthentication(const std::string &authParam)
|
||||
return DM_AUTH_NOT_START;
|
||||
}
|
||||
std::shared_ptr<IAuthentication> ptr;
|
||||
if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()
|
||||
|| timerMap_.find(INPUT_TIMEOUT_TASK) == timerMap_.end()) {
|
||||
if (authenticationMap_.find(authResponseContext_->authType) == authenticationMap_.end()) {
|
||||
LOGE("DmAuthManager::authenticationMap_ is null");
|
||||
return DM_FAILED;
|
||||
}
|
||||
timerMap_[INPUT_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
|
||||
timer_->DeleteTimer(INPUT_TIMEOUT_TASK);
|
||||
ptr = authenticationMap_[authResponseContext_->authType];
|
||||
int32_t ret = ptr->VerifyAuthentication(authResponseContext_->authToken, authParam);
|
||||
switch (ret) {
|
||||
@@ -210,12 +197,15 @@ void DmAuthManager::OnSessionOpened(int32_t sessionId, int32_t sessionSide, int3
|
||||
authResponseState_->SetAuthManager(shared_from_this());
|
||||
authResponseState_->Enter();
|
||||
authResponseContext_ = std::make_shared<DmAuthResponseContext>();
|
||||
std::shared_ptr<DmTimer> waitStartTimer = std::make_shared<DmTimer>(WAIT_NEGOTIATE_TIMEOUT_TASK);
|
||||
timerMap_[WAIT_NEGOTIATE_TIMEOUT_TASK] = waitStartTimer;
|
||||
waitStartTimer->Start(WAIT_NEGOTIATE_TIMEOUT, TimeOut, this);
|
||||
std::shared_ptr<DmTimer> authenticateStartTimer = std::make_shared<DmTimer>(AUTHENTICATE_TIMEOUT_TASK);
|
||||
timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer;
|
||||
authenticateStartTimer->Start(AUTHENTICATE_TIMEOUT, TimeOut, this);
|
||||
timer_ = std::make_shared<DmTimer>();
|
||||
timer_->StartTimer(AUTHENTICATE_TIMEOUT_TASK, AUTHENTICATE_TIMEOUT,
|
||||
[this] (std::string name) {
|
||||
DmAuthManager::HandleAuthenticateTimeout(name);
|
||||
});
|
||||
timer_->StartTimer(WAIT_NEGOTIATE_TIMEOUT_TASK, WAIT_NEGOTIATE_TIMEOUT,
|
||||
[this] (std::string name) {
|
||||
DmAuthManager::HandleAuthenticateTimeout(name);
|
||||
});
|
||||
} else {
|
||||
std::shared_ptr<AuthMessageProcessor> authMessageProcessor =
|
||||
std::make_shared<AuthMessageProcessor>(shared_from_this());
|
||||
@@ -294,18 +284,16 @@ void DmAuthManager::OnDataReceived(int32_t sessionId, std::string message)
|
||||
|
||||
switch (authResponseContext_->msgType) {
|
||||
case MSG_TYPE_NEGOTIATE:
|
||||
if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_INIT
|
||||
&& timerMap_.find(WAIT_NEGOTIATE_TIMEOUT_TASK) != timerMap_.end()) {
|
||||
timerMap_[WAIT_NEGOTIATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
|
||||
if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_INIT) {
|
||||
timer_->DeleteTimer(WAIT_NEGOTIATE_TIMEOUT_TASK);
|
||||
authResponseState_->TransitionTo(std::make_shared<AuthResponseNegotiateState>());
|
||||
} else {
|
||||
LOGE("Device manager auth state error");
|
||||
}
|
||||
break;
|
||||
case MSG_TYPE_REQ_AUTH:
|
||||
if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_NEGOTIATE
|
||||
&& timerMap_.find(WAIT_REQUEST_TIMEOUT_TASK) != timerMap_.end()) {
|
||||
timerMap_[WAIT_REQUEST_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
|
||||
if (authResponseState_->GetStateType() == AuthState::AUTH_RESPONSE_NEGOTIATE) {
|
||||
timer_->DeleteTimer(WAIT_REQUEST_TIMEOUT_TASK);
|
||||
authResponseState_->TransitionTo(std::make_shared<AuthResponseConfirmState>());
|
||||
} else {
|
||||
LOGE("Device manager auth state error");
|
||||
@@ -359,8 +347,8 @@ void DmAuthManager::OnGroupCreated(int64_t requestId, const std::string &groupId
|
||||
void DmAuthManager::OnMemberJoin(int64_t requestId, int32_t status)
|
||||
{
|
||||
LOGE("DmAuthManager OnMemberJoin start");
|
||||
if (authRequestState_ != nullptr && timerMap_.find(ADD_TIMEOUT_TASK) != timerMap_.end()) {
|
||||
timerMap_[ADD_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
|
||||
if (authRequestState_ != nullptr) {
|
||||
timer_->DeleteTimer(ADD_TIMEOUT_TASK);
|
||||
if (status != DM_OK || authResponseContext_->requestId != requestId) {
|
||||
if (authRequestState_ != nullptr) {
|
||||
authResponseContext_->state = AuthState::AUTH_REQUEST_JOIN;
|
||||
@@ -373,9 +361,9 @@ void DmAuthManager::OnMemberJoin(int64_t requestId, int32_t status)
|
||||
}
|
||||
}
|
||||
|
||||
int32_t DmAuthManager::HandleAuthenticateTimeout()
|
||||
void DmAuthManager::HandleAuthenticateTimeout(std::string name)
|
||||
{
|
||||
LOGI("DmAuthManager::HandleAuthenticateTimeout start");
|
||||
LOGI("DmAuthManager::HandleAuthenticateTimeout start timer name %s", name.c_str());
|
||||
if (authRequestState_ != nullptr && authRequestState_->GetStateType() != AuthState::AUTH_REQUEST_FINISH) {
|
||||
if (authResponseContext_ == nullptr) {
|
||||
authResponseContext_ = std::make_shared<DmAuthResponseContext>();
|
||||
@@ -391,7 +379,6 @@ int32_t DmAuthManager::HandleAuthenticateTimeout()
|
||||
authResponseState_->TransitionTo(std::make_shared<AuthResponseFinishState>());
|
||||
}
|
||||
LOGI("DmAuthManager::HandleAuthenticateTimeout start complete");
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
int32_t DmAuthManager::EstablishAuthChannel(const std::string &deviceId)
|
||||
@@ -419,9 +406,10 @@ void DmAuthManager::StartNegotiate(const int32_t &sessionId)
|
||||
authMessageProcessor_->SetResponseContext(authResponseContext_);
|
||||
std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_NEGOTIATE);
|
||||
softbusConnector_->GetSoftbusSession()->SendData(sessionId, message);
|
||||
std::shared_ptr<DmTimer> negotiateStartTimer = std::make_shared<DmTimer>(NEGOTIATE_TIMEOUT_TASK);
|
||||
timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer;
|
||||
negotiateStartTimer->Start(NEGOTIATE_TIMEOUT, TimeOut, this);
|
||||
timer_->StartTimer(NEGOTIATE_TIMEOUT_TASK, NEGOTIATE_TIMEOUT,
|
||||
[this] (std::string name) {
|
||||
DmAuthManager::HandleAuthenticateTimeout(name);
|
||||
});
|
||||
}
|
||||
|
||||
void DmAuthManager::RespNegotiate(const int32_t &sessionId)
|
||||
@@ -449,7 +437,7 @@ void DmAuthManager::RespNegotiate(const int32_t &sessionId)
|
||||
softbusConnector_->GetSoftbusSession()->SendData(sessionId, message);
|
||||
}
|
||||
authResponseContext_ = authResponseState_->GetAuthContext();
|
||||
if (jsonObject[TAG_CRYPTO_SUPPORT] == "true" && authResponseContext_->cryptoSupport == true) {
|
||||
if (jsonObject[TAG_CRYPTO_SUPPORT] == true && authResponseContext_->cryptoSupport) {
|
||||
if (jsonObject[TAG_CRYPTO_NAME] == authResponseContext_->cryptoName &&
|
||||
jsonObject[TAG_CRYPTO_VERSION] == authResponseContext_->cryptoVer) {
|
||||
isCryptoSupport_ = true;
|
||||
@@ -457,22 +445,20 @@ void DmAuthManager::RespNegotiate(const int32_t &sessionId)
|
||||
return;
|
||||
}
|
||||
}
|
||||
jsonObject[TAG_CRYPTO_SUPPORT] = "false";
|
||||
jsonObject[TAG_CRYPTO_SUPPORT] = false;
|
||||
message = jsonObject.dump();
|
||||
softbusConnector_->GetSoftbusSession()->SendData(sessionId, message);
|
||||
std::shared_ptr<DmTimer> waitStartTimer = std::make_shared<DmTimer>(WAIT_REQUEST_TIMEOUT_TASK);
|
||||
timerMap_[WAIT_REQUEST_TIMEOUT_TASK] = waitStartTimer;
|
||||
waitStartTimer->Start(WAIT_REQUEST_TIMEOUT, TimeOut, this);
|
||||
timer_->StartTimer(WAIT_REQUEST_TIMEOUT_TASK, WAIT_REQUEST_TIMEOUT,
|
||||
[this] (std::string name) {
|
||||
DmAuthManager::HandleAuthenticateTimeout(name);
|
||||
});
|
||||
}
|
||||
|
||||
void DmAuthManager::SendAuthRequest(const int32_t &sessionId)
|
||||
{
|
||||
LOGE("DmAuthManager::EstablishAuthChannel session id");
|
||||
if (timerMap_.find(NEGOTIATE_TIMEOUT_TASK) == timerMap_.end()) {
|
||||
return;
|
||||
}
|
||||
timerMap_[NEGOTIATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
|
||||
if (authResponseContext_->cryptoSupport == true) {
|
||||
LOGI("DmAuthManager::EstablishAuthChannel session id");
|
||||
timer_->DeleteTimer(NEGOTIATE_TIMEOUT_TASK);
|
||||
if (authResponseContext_->cryptoSupport) {
|
||||
isCryptoSupport_ = true;
|
||||
}
|
||||
if (authResponseContext_->reply == DM_AUTH_PEER_REJECT) {
|
||||
@@ -483,9 +469,10 @@ void DmAuthManager::SendAuthRequest(const int32_t &sessionId)
|
||||
for (auto msg : messageList) {
|
||||
softbusConnector_->GetSoftbusSession()->SendData(sessionId, msg);
|
||||
}
|
||||
std::shared_ptr<DmTimer> confirmStartTimer = std::make_shared<DmTimer>(CONFIRM_TIMEOUT_TASK);
|
||||
timerMap_[CONFIRM_TIMEOUT_TASK] = confirmStartTimer;
|
||||
confirmStartTimer->Start(CONFIRM_TIMEOUT, TimeOut, this);
|
||||
timer_->StartTimer(CONFIRM_TIMEOUT_TASK, CONFIRM_TIMEOUT,
|
||||
[this] (std::string name) {
|
||||
DmAuthManager::HandleAuthenticateTimeout(name);
|
||||
});
|
||||
}
|
||||
|
||||
int32_t DmAuthManager::StartAuthProcess(const int32_t &action)
|
||||
@@ -510,16 +497,14 @@ int32_t DmAuthManager::StartAuthProcess(const int32_t &action)
|
||||
|
||||
void DmAuthManager::StartRespAuthProcess()
|
||||
{
|
||||
LOGI("DmAuthManager::StartRespAuthProcess StartRespAuthProcess", authResponseContext_->sessionId);
|
||||
if (timerMap_.find(CONFIRM_TIMEOUT_TASK) == timerMap_.end()) {
|
||||
return;
|
||||
}
|
||||
timerMap_[CONFIRM_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
|
||||
LOGI("DmAuthManager::StartRespAuthProcess", authResponseContext_->sessionId);
|
||||
timer_->DeleteTimer(CONFIRM_TIMEOUT_TASK);
|
||||
if (authResponseContext_->reply == USER_OPERATION_TYPE_ALLOW_AUTH) {
|
||||
std::shared_ptr<DmTimer> inputStartTimer = std::make_shared<DmTimer>(INPUT_TIMEOUT_TASK);
|
||||
timerMap_[INPUT_TIMEOUT_TASK] = inputStartTimer;
|
||||
inputStartTimer->Start(INPUT_TIMEOUT, TimeOut, this);
|
||||
authRequestState_->TransitionTo(std::make_shared<AuthRequestInputState>());
|
||||
timer_->StartTimer(INPUT_TIMEOUT_TASK, INPUT_TIMEOUT,
|
||||
[this] (std::string name) {
|
||||
DmAuthManager::HandleAuthenticateTimeout(name);
|
||||
});
|
||||
authRequestState_->TransitionTo(std::make_shared<AuthRequestJoinState>());
|
||||
} else {
|
||||
LOGE("do not accept");
|
||||
authResponseContext_->state = AuthState::AUTH_REQUEST_REPLY;
|
||||
@@ -553,9 +538,10 @@ int32_t DmAuthManager::AddMember(const std::string &deviceId)
|
||||
jsonObject[TAG_REQUEST_ID] = authResponseContext_->requestId;
|
||||
jsonObject[TAG_DEVICE_ID] = authResponseContext_->deviceId;
|
||||
std::string connectInfo = jsonObject.dump();
|
||||
std::shared_ptr<DmTimer> joinStartTimer = std::make_shared<DmTimer>(ADD_TIMEOUT_TASK);
|
||||
timerMap_[ADD_TIMEOUT_TASK] = joinStartTimer;
|
||||
joinStartTimer->Start(ADD_TIMEOUT, TimeOut, this);
|
||||
timer_->StartTimer(ADD_TIMEOUT_TASK, ADD_TIMEOUT,
|
||||
[this] (std::string name) {
|
||||
DmAuthManager::HandleAuthenticateTimeout(name);
|
||||
});
|
||||
int32_t ret = hiChainConnector_->AddMember(deviceId, connectInfo);
|
||||
if (ret != 0) {
|
||||
return DM_FAILED;
|
||||
@@ -581,11 +567,8 @@ std::string DmAuthManager::GetConnectAddr(std::string deviceId)
|
||||
|
||||
int32_t DmAuthManager::JoinNetwork()
|
||||
{
|
||||
LOGE("DmAuthManager JoinNetwork start");
|
||||
if (timerMap_.find(AUTHENTICATE_TIMEOUT_TASK) == timerMap_.end()) {
|
||||
return DM_FAILED;
|
||||
}
|
||||
timerMap_[AUTHENTICATE_TIMEOUT_TASK]->Stop(SESSION_CANCEL_TIMEOUT);
|
||||
LOGI("DmAuthManager JoinNetwork start");
|
||||
timer_->DeleteTimer(AUTHENTICATE_TIMEOUT_TASK);
|
||||
authResponseContext_->state = AuthState::AUTH_REQUEST_FINISH;
|
||||
authRequestContext_->reason = DM_OK;
|
||||
authRequestState_->TransitionTo(std::make_shared<AuthRequestFinishState>());
|
||||
@@ -610,12 +593,7 @@ void DmAuthManager::AuthenticateFinish()
|
||||
std::string message = authMessageProcessor_->CreateSimpleMessage(MSG_TYPE_REQ_AUTH_TERMINATE);
|
||||
softbusConnector_->GetSoftbusSession()->SendData(authResponseContext_->sessionId, message);
|
||||
}
|
||||
if (!timerMap_.empty()) {
|
||||
for (auto &iter : timerMap_) {
|
||||
iter.second->Stop(SESSION_CANCEL_TIMEOUT);
|
||||
}
|
||||
timerMap_.clear();
|
||||
}
|
||||
timer_->DeleteAll();
|
||||
isFinishOfLocal_ = true;
|
||||
authResponseContext_ = nullptr;
|
||||
authResponseState_ = nullptr;
|
||||
@@ -640,12 +618,7 @@ void DmAuthManager::AuthenticateFinish()
|
||||
listener_->OnAuthResult(authRequestContext_->hostPkgName, authRequestContext_->deviceId,
|
||||
authRequestContext_->token, authResponseContext_->state, authRequestContext_->reason);
|
||||
softbusConnector_->GetSoftbusSession()->CloseAuthSession(authRequestContext_->sessionId);
|
||||
if (!timerMap_.empty()) {
|
||||
for (auto &iter : timerMap_) {
|
||||
iter.second->Stop(SESSION_CANCEL_TIMEOUT);
|
||||
}
|
||||
timerMap_.clear();
|
||||
}
|
||||
timer_->DeleteAll();
|
||||
isFinishOfLocal_ = true;
|
||||
authRequestContext_ = nullptr;
|
||||
authResponseContext_ = nullptr;
|
||||
|
||||
@@ -139,14 +139,14 @@ bool HiChainConnector::IsGroupCreated(std::string groupName, GroupInfo &groupInf
|
||||
return false;
|
||||
}
|
||||
|
||||
int32_t HiChainConnector::GetGroupInfo(std::string queryParams, std::vector<GroupInfo> &groupList)
|
||||
bool HiChainConnector::GetGroupInfo(const std::string &queryParams, std::vector<GroupInfo> &groupList)
|
||||
{
|
||||
char *groupVec = nullptr;
|
||||
uint32_t num = 0;
|
||||
int32_t userId = MultipleUserConnector::GetCurrentAccountUserID();
|
||||
if (userId < 0) {
|
||||
LOGE("get current process account user id failed");
|
||||
return DM_FAILED;
|
||||
return false;
|
||||
}
|
||||
int32_t ret = deviceGroupManager_->getGroupInfo(userId, DM_PKG_NAME.c_str(), queryParams.c_str(), &groupVec, &num);
|
||||
if (ret != 0) {
|
||||
@@ -259,11 +259,15 @@ void HiChainConnector::onFinish(int64_t requestId, int operationCode, const char
|
||||
LOGI("HiChainConnector::onFinish reqId:%lld, operation:%d", requestId, operationCode);
|
||||
if (operationCode == GroupOperationCode::MEMBER_JOIN) {
|
||||
LOGI("Add Member To Group success");
|
||||
hiChainConnectorCallback_->OnMemberJoin(requestId, DM_OK);
|
||||
if (hiChainConnectorCallback_ != nullptr) {
|
||||
hiChainConnectorCallback_->OnMemberJoin(requestId, DM_OK);
|
||||
}
|
||||
}
|
||||
if (operationCode == GroupOperationCode::GROUP_CREATE) {
|
||||
LOGI("Create group success");
|
||||
hiChainConnectorCallback_->OnGroupCreated(requestId, data);
|
||||
if (hiChainConnectorCallback_ != nullptr) {
|
||||
hiChainConnectorCallback_->OnGroupCreated(requestId, data);
|
||||
}
|
||||
}
|
||||
if (operationCode == GroupOperationCode::MEMBER_DELETE) {
|
||||
LOGI("Delete Member from group success");
|
||||
@@ -279,11 +283,15 @@ void HiChainConnector::onError(int64_t requestId, int operationCode, int errorCo
|
||||
LOGI("HichainAuthenCallBack::onError reqId:%lld, operation:%d, errorCode:%d.", requestId, operationCode, errorCode);
|
||||
if (operationCode == GroupOperationCode::MEMBER_JOIN) {
|
||||
LOGE("Add Member To Group failed");
|
||||
hiChainConnectorCallback_->OnMemberJoin(requestId, DM_FAILED);
|
||||
if (hiChainConnectorCallback_ != nullptr) {
|
||||
hiChainConnectorCallback_->OnMemberJoin(requestId, DM_FAILED);
|
||||
}
|
||||
}
|
||||
if (operationCode == GroupOperationCode::GROUP_CREATE) {
|
||||
LOGE("Create group failed");
|
||||
hiChainConnectorCallback_->OnGroupCreated(requestId, "{}");
|
||||
if (hiChainConnectorCallback_ != nullptr) {
|
||||
hiChainConnectorCallback_->OnGroupCreated(requestId, "{}");
|
||||
}
|
||||
}
|
||||
if (operationCode == GroupOperationCode::MEMBER_DELETE) {
|
||||
LOGE("Delete Member from group failed");
|
||||
@@ -299,6 +307,10 @@ char *HiChainConnector::onRequest(int64_t requestId, int operationCode, const ch
|
||||
LOGE("HiChainAuthCallBack::onRequest operationCode %d", operationCode);
|
||||
return nullptr;
|
||||
}
|
||||
if (hiChainConnectorCallback_ == nullptr) {
|
||||
LOGE("HiChainAuthCallBack::hiChain connector callback is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
int32_t pinCode = 0;
|
||||
pinCode = hiChainConnectorCallback_->GetPinCode();
|
||||
nlohmann::json jsonObj;
|
||||
@@ -324,6 +336,10 @@ int64_t HiChainConnector::GenRequestId()
|
||||
|
||||
std::string HiChainConnector::GetConnectPara(std::string deviceId, std::string reqDeviceId)
|
||||
{
|
||||
if (hiChainConnectorCallback_ == nullptr) {
|
||||
LOGE("GetConnectPara::hiChain connector callback is nullptr");
|
||||
return "";
|
||||
}
|
||||
std::string connectAddr = "";
|
||||
connectAddr = hiChainConnectorCallback_->GetConnectAddr(deviceId);
|
||||
LOGE("HiChainConnector::GetConnectPara get addrInfo");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* 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
|
||||
@@ -13,175 +13,122 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "dm_constants.h"
|
||||
#include "dm_log.h"
|
||||
#include "dm_timer.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
#include "dm_constants.h"
|
||||
#include "securec.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
namespace {
|
||||
const int32_t MILL_SECONDS_PER_SECOND = 1000;
|
||||
}
|
||||
DmTimer::DmTimer(const std::string &name)
|
||||
{
|
||||
if (name.empty() || name.find(TIMER_PREFIX) != TIMER_DEFAULT) {
|
||||
LOGE("DmTimer name is null");
|
||||
return;
|
||||
}
|
||||
Timer::Timer(std::string name, int32_t time, TimerCallback callback)
|
||||
: timerName_(name), expire_(steadyClock::now()), state_(true), timeOut_(time), callback_(callback) {};
|
||||
|
||||
mStatus_ = DmTimerStatus::DM_STATUS_INIT;
|
||||
mTimeOutSec_ = 0;
|
||||
mHandle_ = nullptr;
|
||||
mHandleData_ = nullptr;
|
||||
(void)memset_s(mTimeFd_, sizeof(mTimeFd_), 0, sizeof(mTimeFd_));
|
||||
(void)memset_s(&mEv_, sizeof(mEv_), 0, sizeof(mEv_));
|
||||
(void)memset_s(mEvents_, sizeof(mEvents_), 0, sizeof(mEvents_));
|
||||
mEpFd_ = 0;
|
||||
mTimerName_ = name;
|
||||
DmTimer::DmTimer()
|
||||
{
|
||||
LOGI("DmTimer constructor");
|
||||
}
|
||||
|
||||
DmTimer::~DmTimer()
|
||||
{
|
||||
if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) {
|
||||
LOGE("DmTimer is not init");
|
||||
return;
|
||||
LOGI("DmTimer destructor");
|
||||
DeleteAll();
|
||||
if (timerState_) {
|
||||
std::unique_lock<std::mutex> locker(timerStateMutex_);
|
||||
stopTimerCondition_.wait(locker, [this] { return static_cast<bool>(!timerState_); });
|
||||
}
|
||||
LOGI("DmTimer %s destroy in", mTimerName_.c_str());
|
||||
Stop(0);
|
||||
|
||||
std::lock_guard<std::mutex> lock(mTimerLock_);
|
||||
Release();
|
||||
}
|
||||
|
||||
DmTimerStatus DmTimer::Start(uint32_t timeOut, TimeoutHandle handle, void *data)
|
||||
int32_t DmTimer::StartTimer(std::string name, int32_t timeOut, TimerCallback callback)
|
||||
{
|
||||
if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT
|
||||
|| handle == nullptr || data == nullptr) {
|
||||
LOGE("DmTimer is not init or param empty");
|
||||
return DmTimerStatus::DM_STATUS_FINISH;
|
||||
LOGI("DmTimer StartTimer %s", name.c_str());
|
||||
if (name.empty() || timeOut <= MIN_TIME_OUT || timeOut > MAX_TIME_OUT || callback == nullptr) {
|
||||
LOGI("DmTimer StartTimer input value invalid");
|
||||
return ERR_DM_INPUT_PARAMETER_EMPTY;
|
||||
}
|
||||
|
||||
LOGI("DmTimer %s start timeout(%d)", mTimerName_.c_str(), timeOut);
|
||||
if (mStatus_ != DmTimerStatus::DM_STATUS_INIT) {
|
||||
return DmTimerStatus::DM_STATUS_BUSY;
|
||||
std::shared_ptr<Timer> timer = std::make_shared<Timer>(name, timeOut, callback);
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(timerMutex_);
|
||||
timerQueue_.push(timer);
|
||||
timerMap_[name] = timer;
|
||||
}
|
||||
|
||||
mTimeOutSec_ = timeOut;
|
||||
mHandle_ = handle;
|
||||
mHandleData_ = data;
|
||||
|
||||
if (CreateTimeFd()) {
|
||||
return DmTimerStatus::DM_STATUS_CREATE_ERROR;
|
||||
if (timerState_) {
|
||||
LOGI("DmTimer is running");
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
mStatus_ = DmTimerStatus::DM_STATUS_RUNNING;
|
||||
mThread_ = std::thread(&DmTimer::WaitForTimeout, this);
|
||||
mThread_.detach();
|
||||
return mStatus_;
|
||||
TimerRunning();
|
||||
{
|
||||
std::unique_lock<std::mutex> locker(timerStateMutex_);
|
||||
runTimerCondition_.wait(locker, [this] { return static_cast<bool>(timerState_); });
|
||||
}
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
void DmTimer::Stop(int32_t code)
|
||||
int32_t DmTimer::DeleteTimer(std::string timerName)
|
||||
{
|
||||
if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT
|
||||
|| mHandleData_ == nullptr) {
|
||||
LOGE("DmTimer is not init");
|
||||
return;
|
||||
if (timerName.empty()) {
|
||||
LOGE("DmTimer DeleteTimer timer is null");
|
||||
return ERR_DM_INPUT_PARAMETER_EMPTY;
|
||||
}
|
||||
|
||||
if (mTimeFd_[1]) {
|
||||
char event = 'S';
|
||||
if (write(mTimeFd_[1], &event, 1) < 0) {
|
||||
return;
|
||||
LOGI("DmTimer DeleteTimer name %s", timerName.c_str());
|
||||
std::lock_guard<std::mutex> locker(timerMutex_);
|
||||
for (auto iter : timerMap_) {
|
||||
if (iter.second->timerName_ == timerName) {
|
||||
iter.second->state_ = false;
|
||||
return DM_OK;
|
||||
}
|
||||
}
|
||||
LOGE("DmTimer DeleteTimer no have this timer");
|
||||
return ERR_DM_INPUT_PARAMETER_EMPTY;
|
||||
}
|
||||
|
||||
void DmTimer::WaitForTimeout()
|
||||
int32_t DmTimer::DeleteAll()
|
||||
{
|
||||
if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) {
|
||||
LOGE("DmTimer is not init");
|
||||
return;
|
||||
LOGI("DmTimer DeleteAll start");
|
||||
std::lock_guard<std::mutex> locker(timerMutex_);
|
||||
for (auto iter : timerMap_) {
|
||||
iter.second->state_ = false;
|
||||
}
|
||||
LOGI("DmTimer %s start timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_);
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(mTimerLock_);
|
||||
int32_t nfds = epoll_wait(mEpFd_, mEvents_, MAX_EVENTS, mTimeOutSec_ * MILL_SECONDS_PER_SECOND);
|
||||
LOGI("DmTimer is triggering");
|
||||
if (nfds > 0) {
|
||||
char event = 0;
|
||||
if (mEvents_[0].events & EPOLLIN) {
|
||||
int num = read(mTimeFd_[0], &event, 1);
|
||||
LOGD("DmTimer %s exit with num=%d, event=%d, errno=%d", mTimerName_.c_str(), num, event, errno);
|
||||
int32_t DmTimer::TimerRunning()
|
||||
{
|
||||
std::thread([this] () {
|
||||
{
|
||||
timerState_ = true;
|
||||
std::unique_lock<std::mutex> locker(timerStateMutex_);
|
||||
runTimerCondition_.notify_one();
|
||||
}
|
||||
} else if (nfds == 0) {
|
||||
if (mHandleData_ != nullptr) {
|
||||
mHandle_(mHandleData_, *this);
|
||||
LOGI("DmTimer %s end timer at (%d)s", mTimerName_.c_str(), mTimeOutSec_);
|
||||
while (!timerQueue_.empty() && timerState_) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_TICK_MILLSECONDS));
|
||||
while (std::chrono::duration_cast<timerDuration>(steadyClock::now()
|
||||
- timerQueue_.top()->expire_).count() / MILLISECOND_TO_SECOND >= timerQueue_.top()->timeOut_
|
||||
|| !timerQueue_.top()->state_) {
|
||||
std::string name = timerQueue_.top()->timerName_;
|
||||
if (timerQueue_.top()->state_) {
|
||||
timerQueue_.top()->callback_(name);
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> locker(timerMutex_);
|
||||
timerQueue_.pop();
|
||||
timerMap_.erase(name);
|
||||
if (timerQueue_.empty()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LOGE("DmTimer %s epoll_wait returned n=%d, error: %d", mTimerName_.c_str(), nfds, errno);
|
||||
}
|
||||
Release();
|
||||
{
|
||||
timerState_ = false;
|
||||
std::unique_lock<std::mutex> locker(timerStateMutex_);
|
||||
stopTimerCondition_.notify_one();
|
||||
}
|
||||
}).detach();
|
||||
return DM_OK;
|
||||
}
|
||||
|
||||
int32_t DmTimer::CreateTimeFd()
|
||||
{
|
||||
if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) {
|
||||
LOGE("DmTimer is not init");
|
||||
return DM_STATUS_FINISH;
|
||||
}
|
||||
LOGI("DmTimer %s creatTimeFd", mTimerName_.c_str());
|
||||
|
||||
int ret = pipe(mTimeFd_);
|
||||
if (ret < 0) {
|
||||
LOGE("DmTimer %s CreateTimeFd fail:(%d) errno(%d)", mTimerName_.c_str(), ret, errno);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(mTimerLock_);
|
||||
mEv_.data.fd = mTimeFd_[0];
|
||||
mEv_.events = EPOLLIN | EPOLLET;
|
||||
mEpFd_ = epoll_create(MAX_EVENTS);
|
||||
ret = epoll_ctl(mEpFd_, EPOLL_CTL_ADD, mTimeFd_[0], &mEv_);
|
||||
if (ret != 0) {
|
||||
Release();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void DmTimer::Release()
|
||||
{
|
||||
if (mTimerName_.empty() || mTimerName_.find(TIMER_PREFIX) != TIMER_DEFAULT) {
|
||||
LOGE("DmTimer is not init");
|
||||
return;
|
||||
}
|
||||
LOGI("DmTimer %s release in", mTimerName_.c_str());
|
||||
if (mStatus_ == DmTimerStatus::DM_STATUS_INIT) {
|
||||
LOGE("DmTimer %s already Release", mTimerName_.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
mStatus_ = DmTimerStatus::DM_STATUS_INIT;
|
||||
close(mTimeFd_[0]);
|
||||
close(mTimeFd_[1]);
|
||||
if (mEpFd_ >= 0) {
|
||||
close(mEpFd_);
|
||||
}
|
||||
mTimerName_ = "";
|
||||
mTimeOutSec_ = 0;
|
||||
mHandle_ = nullptr;
|
||||
mHandleData_ = nullptr;
|
||||
mTimeFd_[0] = 0;
|
||||
mTimeFd_[1] = 0;
|
||||
mEpFd_ = 0;
|
||||
}
|
||||
|
||||
std::string DmTimer::GetTimerName()
|
||||
{
|
||||
return mTimerName_;
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
}
|
||||
@@ -24,23 +24,6 @@ namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
const int32_t SESSION_CANCEL_TIMEOUT = 0;
|
||||
|
||||
static void TimeOut(void *data, DmTimer& timer)
|
||||
{
|
||||
LOGI("time out %s", timer.GetTimerName().c_str());
|
||||
if (data == nullptr || timer.GetTimerName().find(TIMER_PREFIX) != TIMER_DEFAULT) {
|
||||
LOGE("time out is not our timer");
|
||||
return;
|
||||
}
|
||||
|
||||
DmDeviceStateManager *deviceStateMgr = (DmDeviceStateManager*)data;
|
||||
if (deviceStateMgr == nullptr) {
|
||||
LOGE("deviceStateMgr is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
deviceStateMgr->DeleteTimeOutGroup(timer.GetTimerName());
|
||||
}
|
||||
|
||||
DmDeviceStateManager::DmDeviceStateManager(std::shared_ptr<SoftbusConnector> softbusConnector,
|
||||
std::shared_ptr<DeviceManagerServiceListener> listener, std::shared_ptr<HiChainConnector> hiChainConnector)
|
||||
: softbusConnector_(softbusConnector), listener_(listener), hiChainConnector_(hiChainConnector)
|
||||
@@ -268,11 +251,14 @@ void DmDeviceStateManager::RegisterOffLineTimer(const DmDeviceInfo &deviceInfo)
|
||||
#endif
|
||||
for (auto &iter : stateTimerInfoMap_) {
|
||||
if (iter.second.netWorkId == deviceInfo.deviceId) {
|
||||
iter.second.timer->Stop(SESSION_CANCEL_TIMEOUT);
|
||||
timer_->DeleteTimer(iter.second.timerName);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (timer_ == nullptr) {
|
||||
timer_ = std::make_shared<DmTimer>();
|
||||
}
|
||||
std::string timerName = TIMER_PREFIX + STATE_TIMER_PREFIX + std::to_string(mCumulativeQuantity_++);
|
||||
std::shared_ptr<DmTimer> offLineTimer = std::make_shared<DmTimer>(timerName);
|
||||
if (offLineTimer != nullptr) {
|
||||
@@ -296,12 +282,15 @@ void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo)
|
||||
LOGI("start offline timer");
|
||||
for (auto &iter : stateTimerInfoMap_) {
|
||||
if (iter.second.netWorkId == deviceInfo.deviceId) {
|
||||
iter.second.timer->Start(OFFLINE_TIMEOUT, TimeOut, this);
|
||||
timer_->StartTimer(iter.second.timerName, OFFLINE_TIMEOUT,
|
||||
[this] (std::string name) {
|
||||
DmDeviceStateManager::DeleteTimeOutGroup(name);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DmDeviceStateManager::DeleteTimeOutGroup(std::string stateTimer)
|
||||
void DmDeviceStateManager::DeleteTimeOutGroup(std::string name)
|
||||
{
|
||||
#if defined(__LITEOS_M__)
|
||||
DmMutex mutexLock;
|
||||
@@ -309,14 +298,14 @@ void DmDeviceStateManager::DeleteTimeOutGroup(std::string stateTimer)
|
||||
std::lock_guard<std::mutex> mutexLock(timerMapMutex_);
|
||||
#endif
|
||||
if (hiChainConnector_ != nullptr) {
|
||||
auto iter = stateTimerInfoMap_.find(stateTimer);
|
||||
auto iter = stateTimerInfoMap_.find(name);
|
||||
if (iter != stateTimerInfoMap_.end()) {
|
||||
LOGI("remove hichain group with device: %s",
|
||||
GetAnonyString(stateTimerInfoMap_[stateTimer].deviceId).c_str());
|
||||
hiChainConnector_->DeleteTimeOutGroup(stateTimerInfoMap_[stateTimer].deviceId.c_str());
|
||||
GetAnonyString(stateTimerInfoMap_[name].deviceId).c_str());
|
||||
hiChainConnector_->DeleteTimeOutGroup(stateTimerInfoMap_[name].deviceId.c_str());
|
||||
}
|
||||
}
|
||||
stateTimerInfoMap_.erase(stateTimer);
|
||||
stateTimerInfoMap_.erase(name);
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
@@ -25,23 +25,6 @@ const std::string DISCOVERY_TIMEOUT_TASK = TIMER_PREFIX + "discovery";
|
||||
const int32_t DISCOVERY_TIMEOUT = 120;
|
||||
const int32_t SESSION_CANCEL_TIMEOUT = 0;
|
||||
|
||||
static void TimeOut(void *data, DmTimer& timer)
|
||||
{
|
||||
LOGI("time out %s", timer.GetTimerName().c_str());
|
||||
if (data == nullptr || timer.GetTimerName() != DISCOVERY_TIMEOUT_TASK) {
|
||||
LOGE("time out is not our timer");
|
||||
return;
|
||||
}
|
||||
|
||||
DmDiscoveryManager *discoveryMgr = (DmDiscoveryManager *)data;
|
||||
if (discoveryMgr == nullptr) {
|
||||
LOGE("discoveryMgr is nullptr");
|
||||
return;
|
||||
}
|
||||
|
||||
discoveryMgr->HandleDiscoveryTimeout();
|
||||
}
|
||||
|
||||
DmDiscoveryManager::DmDiscoveryManager(std::shared_ptr<SoftbusConnector> softbusConnector,
|
||||
std::shared_ptr<DeviceManagerServiceListener> listener)
|
||||
: softbusConnector_(softbusConnector), listener_(listener)
|
||||
@@ -72,8 +55,13 @@ int32_t DmDiscoveryManager::StartDeviceDiscovery(const std::string &pkgName, con
|
||||
discoveryContextMap_.emplace(pkgName, context);
|
||||
softbusConnector_->RegisterSoftbusDiscoveryCallback(pkgName,
|
||||
std::shared_ptr<ISoftbusDiscoveryCallback>(shared_from_this()));
|
||||
discoveryTimer_ = std::make_shared<DmTimer>(DISCOVERY_TIMEOUT_TASK);
|
||||
discoveryTimer_->Start(DISCOVERY_TIMEOUT, TimeOut, this);
|
||||
if (timer_ == nullptr) {
|
||||
timer_ = std::make_shared<DmTimer>();
|
||||
}
|
||||
timer_->StartTimer(DISCOVERY_TIMEOUT_TASK, DISCOVERY_TIMEOUT,
|
||||
[this] (std::string name) {
|
||||
DmDiscoveryManager::HandleDiscoveryTimeout(name);
|
||||
});
|
||||
return softbusConnector_->StartDiscovery(subscribeInfo);
|
||||
}
|
||||
|
||||
@@ -85,7 +73,7 @@ int32_t DmDiscoveryManager::StopDeviceDiscovery(const std::string &pkgName, uint
|
||||
if (!discoveryContextMap_.empty()) {
|
||||
discoveryContextMap_.erase(pkgName);
|
||||
softbusConnector_->UnRegisterSoftbusDiscoveryCallback(pkgName);
|
||||
discoveryTimer_->Stop(SESSION_CANCEL_TIMEOUT);
|
||||
timer_->DeleteTimer(DISCOVERY_TIMEOUT_TASK);
|
||||
}
|
||||
return softbusConnector_->StopDiscovery(subscribeId);
|
||||
}
|
||||
@@ -115,10 +103,21 @@ void DmDiscoveryManager::OnDiscoverySuccess(const std::string &pkgName, int32_t
|
||||
listener_->OnDiscoverySuccess(pkgName, subscribeId);
|
||||
}
|
||||
|
||||
void DmDiscoveryManager::HandleDiscoveryTimeout()
|
||||
void DmDiscoveryManager::HandleDiscoveryTimeout(std::string name)
|
||||
{
|
||||
LOGI("DmDiscoveryManager::HandleDiscoveryTimeout");
|
||||
StopDeviceDiscovery(discoveryQueue_.front(), discoveryContextMap_[discoveryQueue_.front()].subscribeId);
|
||||
if (discoveryQueue_.empty()) {
|
||||
LOGE("HandleDiscoveryTimeout: discovery queue is empty.");
|
||||
return;
|
||||
}
|
||||
|
||||
std::string pkgName = discoveryQueue_.front();
|
||||
auto iter = discoveryContextMap_.find(pkgName);
|
||||
if (iter == discoveryContextMap_.end()) {
|
||||
LOGE("HandleDiscoveryTimeout: subscribeId not found by pkgName %s", GetAnonyString(pkgName).c_str());
|
||||
return;
|
||||
}
|
||||
StopDeviceDiscovery(pkgName, discoveryContextMap_[pkgName].subscribeId);
|
||||
}
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
|
||||
@@ -110,8 +110,7 @@ HWTEST_F(AuthRequestStateTest, TransitionTo_002, testing::ext::TestSize.Level0)
|
||||
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector);
|
||||
std::shared_ptr<DmAuthRequestContext> context = std::make_shared<DmAuthRequestContext>();
|
||||
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestNegotiateDoneState>();
|
||||
std::shared_ptr<DmTimer> negotiateStartTimer = std::make_shared<DmTimer>(NEGOTIATE_TIMEOUT_TASK);
|
||||
authManager->timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer;
|
||||
authManager->timer_ = std::make_shared<DmTimer>();
|
||||
authManager->authRequestState_ = std::make_shared<AuthRequestNegotiateDoneState>();
|
||||
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
|
||||
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
|
||||
@@ -236,6 +235,7 @@ HWTEST_F(AuthRequestStateTest, Enter_004, testing::ext::TestSize.Level0)
|
||||
authManager->authRequestContext_->deviceId = "111";
|
||||
authManager->authMessageProcessor_->SetRequestContext(authManager->authRequestContext_);
|
||||
authManager->authMessageProcessor_->SetResponseContext(authManager->authResponseContext_);
|
||||
authManager->timer_ = std::make_shared<DmTimer>();
|
||||
authRequestState->SetAuthManager(authManager);
|
||||
std::shared_ptr<DmAuthRequestContext> context = std::make_shared<DmAuthRequestContext>();
|
||||
context->deviceId = "123456";
|
||||
@@ -272,8 +272,7 @@ HWTEST_F(AuthRequestStateTest, Enter_006, testing::ext::TestSize.Level0)
|
||||
std::shared_ptr<DmAuthManager> authManager =
|
||||
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector);
|
||||
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestNegotiateDoneState>();
|
||||
std::shared_ptr<DmTimer> negotiateStartTimer = std::make_shared<DmTimer>(NEGOTIATE_TIMEOUT_TASK);
|
||||
authManager->timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer;
|
||||
authManager->timer_ = std::make_shared<DmTimer>();
|
||||
authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
|
||||
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
|
||||
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
|
||||
@@ -334,8 +333,7 @@ HWTEST_F(AuthRequestStateTest, Enter_008, testing::ext::TestSize.Level0)
|
||||
std::shared_ptr<DmAuthManager> authManager =
|
||||
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector);
|
||||
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestReplyState>();
|
||||
std::shared_ptr<DmTimer> inputStartTimer = std::make_shared<DmTimer>(CONFIRM_TIMEOUT_TASK);
|
||||
authManager->timerMap_[CONFIRM_TIMEOUT_TASK] = inputStartTimer;
|
||||
authManager->timer_ = std::make_shared<DmTimer>();
|
||||
authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
|
||||
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
|
||||
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
|
||||
@@ -513,9 +511,9 @@ HWTEST_F(AuthRequestStateTest, Enter_014, testing::ext::TestSize.Level0)
|
||||
std::shared_ptr<DmAuthManager> authManager =
|
||||
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector);
|
||||
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestNetworkState>();
|
||||
std::shared_ptr<DmTimer> authenticateStartTimer = std::make_shared<DmTimer>(AUTHENTICATE_TIMEOUT_TASK);
|
||||
authManager->timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer;
|
||||
authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
|
||||
authManager->timer_ = std::make_shared<DmTimer>();
|
||||
authManager->authMessageProcessor_ = std::make_
|
||||
shared<AuthMessageProcessor>(authManager);
|
||||
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
|
||||
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
|
||||
authManager->authRequestState_ = std::make_shared<AuthRequestNetworkState>();
|
||||
@@ -573,8 +571,7 @@ HWTEST_F(AuthRequestStateTest, Enter_016, testing::ext::TestSize.Level0)
|
||||
std::shared_ptr<DmAuthManager> authManager =
|
||||
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector);
|
||||
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestFinishState>();
|
||||
std::shared_ptr<DmTimer> inputStartTimer = std::make_shared<DmTimer>(CONFIRM_TIMEOUT_TASK);
|
||||
authManager->timerMap_[CONFIRM_TIMEOUT_TASK] = inputStartTimer;
|
||||
authManager->timer_ = std::make_shared<DmTimer>();
|
||||
authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
|
||||
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
|
||||
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
|
||||
|
||||
@@ -201,6 +201,7 @@ HWTEST_F(AuthResponseStateTest, Enter_003, testing::ext::TestSize.Level0)
|
||||
authManager->authMessageProcessor_->SetResponseContext(authManager->authResponseContext_);
|
||||
authManager->authMessageProcessor_->SetRequestContext(authManager->authRequestContext_);
|
||||
authManager->softbusConnector_->GetSoftbusSession()->RegisterSessionCallback(authManager);
|
||||
authManager->timer_ = std::make_shared<DmTimer>();
|
||||
authResponseState->SetAuthManager(authManager);
|
||||
std::shared_ptr<DmAuthResponseContext> context = std::make_shared<DmAuthResponseContext>();
|
||||
context->deviceId = "123456";
|
||||
|
||||
@@ -68,16 +68,16 @@ HWTEST_F(DmAuthManagerTest, UnAuthenticateDevice_001, testing::ext::TestSize.Lev
|
||||
*/
|
||||
HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_001, testing::ext::TestSize.Level0)
|
||||
{
|
||||
std::string name = "test";
|
||||
std::shared_ptr<DmAuthManager> authManager =
|
||||
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
|
||||
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestNetworkState>();
|
||||
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
|
||||
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
|
||||
authManager->authRequestState_ = std::make_shared<AuthRequestNetworkState>();
|
||||
authManager->authResponseContext_ = nullptr;
|
||||
authManager->SetAuthRequestState(authRequestState);
|
||||
int32_t ret = authManager->HandleAuthenticateTimeout();
|
||||
ASSERT_EQ(ret, DM_OK);
|
||||
authManager->HandleAuthenticateTimeout(name);
|
||||
ASSERT_TRUE(authManager->authResponseContext_ != nullptr);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,6 +88,7 @@ HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_001, testing::ext::TestSiz
|
||||
*/
|
||||
HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_002, testing::ext::TestSize.Level0)
|
||||
{
|
||||
std::string name = "test";
|
||||
std::shared_ptr<DmAuthManager> authManager =
|
||||
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
|
||||
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestFinishState>();
|
||||
@@ -95,8 +96,8 @@ HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_002, testing::ext::TestSiz
|
||||
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
|
||||
authManager->authRequestState_ = std::make_shared<AuthRequestFinishState>();
|
||||
authManager->SetAuthRequestState(authRequestState);
|
||||
int32_t ret = authManager->HandleAuthenticateTimeout();
|
||||
ASSERT_EQ(ret, DM_OK);
|
||||
authManager->HandleAuthenticateTimeout(name);
|
||||
ASSERT_TRUE(authManager->authRequestState_ != nullptr);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -225,8 +226,7 @@ HWTEST_F(DmAuthManagerTest, JoinNetwork_001, testing::ext::TestSize.Level0)
|
||||
std::shared_ptr<DmAuthManager> authManager =
|
||||
std::make_shared<DmAuthManager>(softbusConnector, listener, hiChainConnector_);
|
||||
std::shared_ptr<AuthRequestState> authRequestState = std::make_shared<AuthRequestFinishState>();
|
||||
std::shared_ptr<DmTimer> authenticateStartTimer = std::make_shared<DmTimer>(AUTHENTICATE_TIMEOUT_TASK);
|
||||
authManager->timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer;
|
||||
authManager->timer_ = std::make_shared<DmTimer>();
|
||||
authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(authManager);
|
||||
authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
|
||||
authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
|
||||
|
||||
@@ -225,20 +225,6 @@ HWTEST_F(DmDiscoveryManagerTest, OnDiscoverySuccess_002, testing::ext::TestSize.
|
||||
std ::string ret = pReq->GetPkgName();
|
||||
EXPECT_EQ(ret, pkgName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: HandleDiscoveryTimeout_001
|
||||
* @tc.desc: set pkgName not null and return 1(true)
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: AR000GHSJK
|
||||
*/
|
||||
HWTEST_F(DmDiscoveryManagerTest, HandleDiscoveryTimeout_001, testing::ext::TestSize.Level0)
|
||||
{
|
||||
std::string pkgName = "com.ohos.helloworld";
|
||||
discoveryMgr_->HandleDiscoveryTimeout();
|
||||
int ret = discoveryMgr_->discoveryContextMap_.count(pkgName);
|
||||
EXPECT_EQ(ret, 1);
|
||||
}
|
||||
} // namespace
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
|
||||
Reference in New Issue
Block a user