设备上线增加类型判断

Signed-off-by: li-tiangang4 <litiangang4@huawei.com>
This commit is contained in:
li-tiangang4 2024-09-25 17:20:07 +08:00
parent b5391216fe
commit 1a39a3b8a3
14 changed files with 152 additions and 56 deletions

View File

@ -31,6 +31,7 @@ namespace DistributedHardware {
constexpr int32_t ERR_DH_FWK_TYPE_NOT_EXIST = -10003;
constexpr int32_t ERR_DH_FWK_JSON_PARSE_FAILED = -10004;
constexpr int32_t ERR_DH_FWK_POINTER_IS_NULL = -10005;
constexpr int32_t ERR_DH_FWK_INVALID_OSTYPE = -10006;
/* VersionManager errno, range: [-10200, -10299] */
constexpr int32_t ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST = -10200;

View File

@ -32,11 +32,12 @@ public:
bool InitLocalDevInfo();
bool IsInit();
int32_t SendOnLineEvent(const std::string &networkId, const std::string &uuid, const std::string &udid,
uint16_t deviceType);
uint16_t deviceType, int32_t osType);
int32_t SendOffLineEvent(const std::string &networkId, const std::string &uuid, const std::string &udid,
uint16_t deviceType);
int32_t GetComponentVersion(std::unordered_map<DHType, std::string> &versionMap);
void ClearDataWhenPeerLogout(const std::string &peerudid, const std::string &peeruuid);
void ClearRemoteDeviceMetaInfoData(const std::string &peerudid, const std::string &peeruuid);
void ClearRemoteDeviceLocalInfoData(const std::string &peeruuid);
int Dump(const std::vector<std::string> &argsStr, std::string &result);
void UnInit();

View File

@ -54,7 +54,7 @@ public:
int32_t RemoveDataByKey(const std::string &key);
std::vector<DistributedKv::Entry> GetEntriesByKeys(const std::vector<std::string> &keys);
bool SyncDataByNetworkId(const std::string &networkId);
bool ClearDataWhenPeerLogout(const std::string &peerudid, const std::string &peeruuid);
bool ClearDataByPrefix(const std::string &prefix);
private:
int32_t RegisterChangeListener();

View File

@ -57,6 +57,8 @@ public:
int32_t GetDataByDHType(const DHType dhType, CapabilityInfoMap &capabilityMap);
/* Queries batch records in the database based on the prefix of the key. */
int32_t GetDataByKeyPrefix(const std::string &keyPrefix, CapabilityInfoMap &capabilityMap);
int32_t RemoveLocalInfoInMemByUuid(const std::string &peeruuid);
int32_t ClearRemoteDeviceLocalInfoData(const std::string &peeruuid);
private:
mutable std::mutex capInfoMgrMutex_;

View File

@ -45,13 +45,14 @@ public:
int32_t SyncRemoteMetaInfos();
int32_t GetDataByKeyPrefix(const std::string &keyPrefix, MetaCapInfoMap &metaCapMap);
int32_t RemoveMetaInfoByKey(const std::string &key);
int32_t RemoveMetaInfoInMemByUdid(const std::string &peerudid);
int32_t GetMetaCapInfo(const std::string &udidHash, const std::string &dhId,
std::shared_ptr<MetaCapabilityInfo> &metaCapPtr);
void GetMetaCapInfosByUdidHash(const std::string &udidHash,
std::vector<std::shared_ptr<MetaCapabilityInfo>> &metaCapInfos);
int32_t GetMetaDataByDHType(const DHType dhType, MetaCapInfoMap &metaInfoMap);
int32_t SyncDataByNetworkId(const std::string &networkId);
int32_t ClearDataWhenPeerLogout(const std::string &peerudid, const std::string &peeruuid);
int32_t ClearRemoteDeviceMetaInfoData(const std::string &peerudid, const std::string &peeruuid);
/* Database data changes callback */
virtual void OnChange(const DistributedKv::ChangeNotification &changeNotification) override;
/* Cloud data changes callback */

View File

@ -36,6 +36,7 @@ namespace DistributedHardware {
constexpr int32_t DH_RETRY_INIT_DM_COUNT = 6;
constexpr int32_t DH_RETRY_INIT_DM_INTERVAL_US = 1000 * 500;
AccessManager::~AccessManager()
{
UnInit();
@ -123,8 +124,29 @@ void AccessManager::OnRemoteDied()
void AccessManager::OnDeviceOnline(const DmDeviceInfo &deviceInfo)
{
(void)deviceInfo;
return;
std::lock_guard<std::mutex> lock(accessMutex_);
DHLOGI("AccessManager online, networkId: %{public}s, deviceName: %{public}s, deviceTypeId: %{public}d",
GetAnonyString(deviceInfo.networkId).c_str(), GetAnonyString(deviceInfo.deviceName).c_str(),
deviceInfo.deviceTypeId);
auto networkId = std::string(deviceInfo.networkId);
if (!IsIdLengthValid(networkId)) {
return;
}
auto uuid = GetUUIDByDm(networkId);
if (!IsIdLengthValid(uuid)) {
return;
}
auto udid = GetUDIDByDm(networkId);
if (!IsIdLengthValid(udid)) {
return;
}
int32_t osType = GetDeviceSystemType(deviceInfo.extraData);
auto ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, udid,
deviceInfo.deviceTypeId, osType);
DHLOGI("AccessManager online result: %{public}d, networkId: %{public}s, uuid: %{public}s, udid: %{public}s,"
"osType = %{public}d", ret, GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(),
GetAnonyString(udid).c_str(), osType);
}
void AccessManager::OnDeviceOffline(const DmDeviceInfo &deviceInfo)
@ -155,28 +177,8 @@ void AccessManager::OnDeviceOffline(const DmDeviceInfo &deviceInfo)
void AccessManager::OnDeviceReady(const DmDeviceInfo &deviceInfo)
{
std::lock_guard<std::mutex> lock(accessMutex_);
DHLOGI("AccessManager online, networkId: %{public}s, deviceName: %{public}s, deviceTypeId: %{public}d",
GetAnonyString(deviceInfo.networkId).c_str(), GetAnonyString(deviceInfo.deviceName).c_str(),
deviceInfo.deviceTypeId);
auto networkId = std::string(deviceInfo.networkId);
if (!IsIdLengthValid(networkId)) {
return;
}
auto uuid = GetUUIDByDm(networkId);
if (!IsIdLengthValid(uuid)) {
return;
}
auto udid = GetUDIDByDm(networkId);
if (!IsIdLengthValid(udid)) {
return;
}
auto ret =
DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, udid,
deviceInfo.deviceTypeId);
DHLOGI("AccessManager online result: %{public}d, networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
ret, GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(udid).c_str());
(void)deviceInfo;
return;
}
void AccessManager::OnDeviceChanged(const DmDeviceInfo &deviceInfo)
@ -197,7 +199,7 @@ void AccessManager::OnDeviceTrustChange(const std::string &peerudid, const std::
DHLOGE("Peer is not same account");
return;
}
DistributedHardwareManagerFactory::GetInstance().ClearDataWhenPeerLogout(peerudid, peeruuid);
DistributedHardwareManagerFactory::GetInstance().ClearRemoteDeviceMetaInfoData(peerudid, peeruuid);
}
void AccessManager::CheckTrustedDeviceOnline()
@ -212,10 +214,12 @@ void AccessManager::CheckTrustedDeviceOnline()
const auto networkId = std::string(deviceInfo.networkId);
const auto uuid = GetUUIDByDm(networkId);
const auto udid = GetUDIDByDm(networkId);
DHLOGI("Send trusted device online, networkId = %{public}s, uuid = %{public}s, udid = %{public}s",
GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(udid).c_str());
int32_t osType = GetDeviceSystemType(deviceInfo.extraData);
DHLOGI("Send trusted device online, networkId = %{public}s, uuid = %{public}s, udid = %{public}s,"
"osType = %{public}d", GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(),
GetAnonyString(udid).c_str(), osType);
DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, udid,
deviceInfo.deviceTypeId);
deviceInfo.deviceTypeId, osType);
}
}

View File

@ -38,10 +38,14 @@
#include "distributed_hardware_log.h"
#include "distributed_hardware_manager.h"
#include "device_param_mgr.h"
#include "local_capability_info_manager.h"
#include "meta_info_manager.h"
namespace OHOS {
namespace DistributedHardware {
namespace {
constexpr int32_t DOUBLE_FRAME_DEVICE_TYPE = -1;
}
#undef DH_LOG_TAG
#define DH_LOG_TAG "DistributedHardwareManagerFactory"
@ -135,11 +139,11 @@ void DistributedHardwareManagerFactory::CheckExitSAOrNot()
const auto uuid = GetUUIDByDm(networkId);
const auto udid = GetUDIDByDm(networkId);
DHLOGI("Send trusted device online, networkId = %{public}s, uuid = %{public}s",
GetAnonyString(networkId).c_str(),
GetAnonyString(uuid).c_str());
GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str());
uint16_t deviceType = deviceInfo.deviceTypeId;
std::thread([this, networkId, uuid, udid, deviceType]() {
this->SendOnLineEvent(networkId, uuid, udid, deviceType);
int32_t osType = GetDeviceSystemType(deviceInfo.extraData);
std::thread([this, networkId, uuid, udid, deviceType, osType]() {
this->SendOnLineEvent(networkId, uuid, udid, deviceType, osType);
}).detach();
}
}
@ -150,7 +154,7 @@ bool DistributedHardwareManagerFactory::IsInit()
}
int32_t DistributedHardwareManagerFactory::SendOnLineEvent(const std::string &networkId, const std::string &uuid,
const std::string &udid, uint16_t deviceType)
const std::string &udid, uint16_t deviceType, int32_t osType)
{
if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(udid)) {
return ERR_DH_FWK_PARA_INVALID;
@ -182,6 +186,13 @@ int32_t DistributedHardwareManagerFactory::SendOnLineEvent(const std::string &ne
DHLOGI("e2e device, need initiative sync data.");
MetaInfoManager::GetInstance()->SyncDataByNetworkId(networkId);
}
if (osType == DOUBLE_FRAME_DEVICE_TYPE) {
DHLOGE("double frame device, networkId = %{public}s, uuid = %{public}s, udid = %{public}s, need clear data.",
GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(udid).c_str());
ClearRemoteDeviceMetaInfoData(udid, uuid);
ClearRemoteDeviceLocalInfoData(uuid);
}
auto onlineResult = DistributedHardwareManager::GetInstance().SendOnLineEvent(networkId, uuid, udid, deviceType);
if (onlineResult != DH_FWK_SUCCESS) {
DHLOGE("online failed, errCode = %{public}d", onlineResult);
@ -237,10 +248,15 @@ bool DistributedHardwareManagerFactory::GetUnInitFlag()
return flagUnInit_.load();
}
void DistributedHardwareManagerFactory::ClearDataWhenPeerLogout(const std::string &peerudid,
void DistributedHardwareManagerFactory::ClearRemoteDeviceMetaInfoData(const std::string &peerudid,
const std::string &peeruuid)
{
MetaInfoManager::GetInstance()->ClearDataWhenPeerLogout(peerudid, peeruuid);
MetaInfoManager::GetInstance()->ClearRemoteDeviceMetaInfoData(peerudid, peeruuid);
}
void DistributedHardwareManagerFactory::ClearRemoteDeviceLocalInfoData(const std::string &peeruuid)
{
LocalCapabilityInfoManager::GetInstance()->ClearRemoteDeviceLocalInfoData(peeruuid);
}
} // namespace DistributedHardware
} // namespace OHOS

View File

@ -565,16 +565,16 @@ bool DBAdapter::SyncDataByNetworkId(const std::string &networkId)
return true;
}
bool DBAdapter::ClearDataWhenPeerLogout(const std::string &peerudid, const std::string &peeruuid)
bool DBAdapter::ClearDataByPrefix(const std::string &prefix)
{
DHLOGI("Clear cloudData start.");
DHLOGI("Clear data by prefix: %{public}s.", GetAnonyString(prefix).c_str());
std::lock_guard<std::mutex> lock(dbAdapterMutex_);
if (kvStoragePtr_ == nullptr) {
DHLOGE("kvStoragePtr_ is nullptr!");
return false;
}
std::string udIdHash = Sha256(peerudid);
DistributedKv::Key allEntryKeyPrefix(udIdHash);
std::string keyPrefix = Sha256(prefix);
DistributedKv::Key allEntryKeyPrefix(keyPrefix);
std::vector<DistributedKv::Entry> peerEntries;
DistributedKv::Status status = kvStoragePtr_->GetEntries(allEntryKeyPrefix, peerEntries);
if (status != DistributedKv::Status::SUCCESS || peerEntries.size() == 0) {
@ -590,11 +590,6 @@ bool DBAdapter::ClearDataWhenPeerLogout(const std::string &peerudid, const std::
DHLOGE("DeleteBatch failed, error: %{public}d", status);
return false;
}
if (kvStoragePtr_->RemoveDeviceData(peeruuid) != DistributedKv::Status::SUCCESS) {
DHLOGE("RemoveDeviceData failed, peeruuid=%{public}s", GetAnonyString(peeruuid).c_str());
return false;
}
return true;
}
} // namespace DistributedHardware

View File

@ -259,5 +259,32 @@ int32_t LocalCapabilityInfoManager::GetDataByKeyPrefix(const std::string &keyPre
}
return DH_FWK_SUCCESS;
}
int32_t LocalCapabilityInfoManager::RemoveLocalInfoInMemByUuid(const std::string &peeruuid)
{
DHLOGI("remove device localinfo in memory, peerudid: %{public}s", GetAnonyString(peeruuid).c_str());
std::string deviceId = Sha256(peeruuid);
for (auto iter = globalCapInfoMap_.begin(); iter != globalCapInfoMap_.end();) {
if (!IsCapKeyMatchDeviceId(iter->first, deviceId)) {
DHLOGI("not find deviceId: %{public}s", GetAnonyString(deviceId).c_str());
iter++;
continue;
}
globalCapInfoMap_.erase(iter++);
}
return DH_FWK_SUCCESS;
}
int32_t LocalCapabilityInfoManager::ClearRemoteDeviceLocalInfoData(const std::string &peeruuid)
{
std::lock_guard<std::mutex> lock(capInfoMgrMutex_);
if (dbAdapterPtr_ == nullptr) {
DHLOGE("dbAdapterPtr is null");
return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
}
dbAdapterPtr_->ClearDataByPrefix(peeruuid);
RemoveLocalInfoInMemByUuid(peeruuid);
return DH_FWK_SUCCESS;
}
} // namespace DistributedHardware
} // namespace OHOS

View File

@ -337,13 +337,34 @@ int32_t MetaInfoManager::SyncDataByNetworkId(const std::string &networkId)
return DH_FWK_SUCCESS;
}
int32_t MetaInfoManager::ClearDataWhenPeerLogout(const std::string &peerudid, const std::string &peeruuid)
int32_t MetaInfoManager::RemoveMetaInfoInMemByUdid(const std::string &peerudid)
{
DHLOGI("remove device metainfo in memory, peerudid: %{public}s", GetAnonyString(peerudid).c_str());
std::lock_guard<std::mutex> lock(metaInfoMgrMutex_);
std::string udIdHash = Sha256(peerudid);
for (auto iter = globalMetaInfoMap_.begin(); iter != globalMetaInfoMap_.end();) {
if (!IsCapKeyMatchDeviceId(iter->first, udIdHash)) {
DHLOGI("not find udIdHash: %{public}s", GetAnonyString(udIdHash).c_str());
iter++;
continue;
}
globalMetaInfoMap_.erase(iter++);
}
return DH_FWK_SUCCESS;
}
int32_t MetaInfoManager::ClearRemoteDeviceMetaInfoData(const std::string &peerudid, const std::string &peeruuid)
{
if (dbAdapterPtr_ == nullptr) {
DHLOGE("dbAdapterPtr is null");
return ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL;
}
dbAdapterPtr_->ClearDataWhenPeerLogout(peerudid, peeruuid);
dbAdapterPtr_->ClearDataByPrefix(peerudid);
if (dbAdapterPtr_->RemoveDeviceData(peeruuid) != DH_FWK_SUCCESS) {
DHLOGE("Remove Device Data failed, peeruuid: %{public}s", GetAnonyString(peeruuid).c_str());
return ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL;
}
RemoveMetaInfoInMemByUdid(peerudid);
return DH_FWK_SUCCESS;
}

View File

@ -31,6 +31,7 @@ enum class Status : uint32_t {
};
constexpr uint16_t TEST_DEV_TYPE_PAD = 0x11;
constexpr int32_t INVALID_OSTYPE = 10;
const std::string TEST_NETWORKID = "111111";
const std::string TEST_UUID = "222222";
const std::string TEST_UDID = "333333";
@ -63,14 +64,20 @@ void AccessManagerTest::TearDownTestCase() {}
*/
HWTEST_F(AccessManagerTest, SendOnLineEvent_001, TestSize.Level1)
{
int32_t ostype = -1;
DHContext::GetInstance().AddOnlineDevice(TEST_UDID, TEST_UUID, TEST_NETWORKID);
auto ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(TEST_NETWORKID, TEST_UUID, TEST_UDID,
TEST_DEV_TYPE_PAD);
TEST_DEV_TYPE_PAD, ostype);
EXPECT_EQ(ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_ONLINE, ret);
DHContext::GetInstance().devIdEntrySet_.clear();
ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(TEST_NETWORKID, TEST_UUID, TEST_UDID,
TEST_DEV_TYPE_PAD);
TEST_DEV_TYPE_PAD, ostype);
EXPECT_EQ(DH_FWK_SUCCESS, ret);
DHContext::GetInstance().devIdEntrySet_.clear();
ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(TEST_NETWORKID, TEST_UUID, TEST_UDID,
TEST_DEV_TYPE_PAD, INVALID_OSTYPE);
EXPECT_EQ(DH_FWK_SUCCESS, ret);
}

View File

@ -1220,16 +1220,16 @@ HWTEST_F(ResourceManagerTest, SyncDataByNetworkId_001, TestSize.Level0)
EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret);
}
HWTEST_F(ResourceManagerTest, ClearDataWhenPeerLogout_001, TestSize.Level0)
HWTEST_F(ResourceManagerTest, ClearRemoteDeviceMetaInfoData_001, TestSize.Level0)
{
MetaInfoManager::GetInstance()->Init();
std::string peerudid = "peerudid_test";
std::string peeruuid = "peeruuid_test";
auto ret = MetaInfoManager::GetInstance()->ClearDataWhenPeerLogout(peerudid, peeruuid);
EXPECT_EQ(DH_FWK_SUCCESS, ret);
auto ret = MetaInfoManager::GetInstance()->ClearRemoteDeviceMetaInfoData(peerudid, peeruuid);
EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_OPERATION_FAIL, ret);
MetaInfoManager::GetInstance()->dbAdapterPtr_ = nullptr;
ret = MetaInfoManager::GetInstance()->ClearDataWhenPeerLogout(peerudid, peeruuid);
ret = MetaInfoManager::GetInstance()->ClearRemoteDeviceMetaInfoData(peerudid, peeruuid);
EXPECT_EQ(ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL, ret);
}
} // namespace DistributedHardware

View File

@ -77,6 +77,8 @@ bool IsArrayLengthValid(const std::vector<std::string> &array);
bool IsKeySizeValid(const std::string &key);
bool IsHashSizeValid(const std::string &hashValue);
int32_t GetDeviceSystemType(const std::string &extraData);
} // namespace DistributedHardware
} // namespace OHOS
#endif

View File

@ -45,6 +45,7 @@ namespace {
constexpr unsigned char MASK = 0x0F;
constexpr int32_t DOUBLE_TIMES = 2;
constexpr int32_t COMPRESS_SLICE_SIZE = 1024;
const std::string PARAM_KEY_OS_TYPE = "OS_TYPE";
}
int64_t GetCurrentTime()
@ -344,5 +345,23 @@ bool IsHashSizeValid(const std::string &inputHashValue)
}
return true;
}
int32_t GetDeviceSystemType(const std::string &extraData)
{
cJSON *jsonObj = cJSON_Parse(extraData.c_str());
if (jsonObj == NULL) {
DHLOGE("jsonStr parse failed");
return ERR_DH_FWK_INVALID_OSTYPE;
}
cJSON *paramKey = cJSON_GetObjectItem(jsonObj, PARAM_KEY_OS_TYPE.c_str());
if (paramKey == NULL || !cJSON_IsNumber(paramKey)) {
DHLOGE("paramKey is null or paramKey is invaild type!");
cJSON_Delete(jsonObj);
return ERR_DH_FWK_INVALID_OSTYPE;
}
int32_t osType = paramKey->valueint;
cJSON_Delete(jsonObj);
return osType;
}
} // namespace DistributedHardware
} // namespace OHOS