From 18620f17932418fd6cbaf03e21f87c01611797af Mon Sep 17 00:00:00 2001 From: caochao Date: Tue, 1 Mar 2022 04:52:38 -0800 Subject: [PATCH] the device crashed online and offline Signed-off-by: cao_liu_chao --- common/include/dm_constants.h | 4 + ext/profile/include/device_profile_adapter.h | 21 +-- ext/profile/include/profile_connector.h | 25 ++-- ext/profile/src/device_profile_adapter.cpp | 58 +++++--- ext/profile/src/profile_connector.cpp | 99 +++++++------ services/devicemanagerservice/BUILD.gn | 1 + .../devicestate/dm_device_state_manager.h | 14 +- .../devicestate/dm_device_state_manager.cpp | 136 +++++++++++------- 8 files changed, 214 insertions(+), 144 deletions(-) mode change 100644 => 100755 common/include/dm_constants.h mode change 100644 => 100755 ext/profile/include/device_profile_adapter.h mode change 100644 => 100755 ext/profile/include/profile_connector.h mode change 100644 => 100755 ext/profile/src/device_profile_adapter.cpp mode change 100644 => 100755 ext/profile/src/profile_connector.cpp mode change 100644 => 100755 services/devicemanagerservice/BUILD.gn mode change 100644 => 100755 services/devicemanagerservice/include/devicestate/dm_device_state_manager.h mode change 100644 => 100755 services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h old mode 100644 new mode 100755 index 184e2e1fc..2cf9531cc --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -62,6 +62,10 @@ enum { DM_MAKE_SHARED_FAIL, DM_SERVICE_NOT_READY, DM_DEVICE_ALREADY_TRUSTED, + ERR_DM_KEY_ALREADY_EXISTS, + ERR_DM_INPUT_PARA_INVALID, + ERR_DM_SUBSCRIBE_DP_EVENTS, + ERR_DM_UNSUBSCRIBE_DP_EVENTS, DM_IPC_FAILED = 2000, DM_IPC_TRANSACTION_FAILED, DM_IPC_FLATTEN_OBJECT, diff --git a/ext/profile/include/device_profile_adapter.h b/ext/profile/include/device_profile_adapter.h old mode 100644 new mode 100755 index 3703a619a..c87fd9906 --- a/ext/profile/include/device_profile_adapter.h +++ b/ext/profile/include/device_profile_adapter.h @@ -17,6 +17,7 @@ #define OHOS_DM_ADAPTER_DEVICE_PROFILE_H #include +#include #include #include "profile_adapter.h" @@ -24,20 +25,20 @@ namespace OHOS { namespace DistributedHardware { -class DeviceProfileAdapter : public IProfileAdapter, public IProfileConnectorCallback { +class DeviceProfileAdapter final : public IProfileAdapter, public IProfileConnectorCallback, + public std::enable_shared_from_this { public: DeviceProfileAdapter(); - ~DeviceProfileAdapter(); + virtual ~DeviceProfileAdapter(); int32_t RegisterProfileListener(const std::string &pkgName, const std::string &deviceId, - std::shared_ptr callback); - int32_t UnRegisterProfileListener(const std::string &pkgName); - int32_t OnProfileClientDeviceReady(const std::string &pkgName, const std::string &deviceId); - void OnProfileChanged(const std::string &pkgName, const std::string &deviceId); - void OnProfileComplete(const std::string &pkgName, const std::string &deviceId); - + std::shared_ptr callback) override; + int32_t UnRegisterProfileListener(const std::string &pkgName) override; + void OnProfileChanged(const std::string &pkgName, const std::string &deviceId) override; + void OnProfileComplete(const std::string &pkgName, const std::string &deviceId) override; private: - std::shared_ptr deviceProfileAdapterCallback_; - static std::shared_ptr profileConnector_; + std::mutex deviceProfileAdapterMutex_; + std::shared_ptr deviceStateManager_; + std::shared_ptr profileConnector_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/ext/profile/include/profile_connector.h b/ext/profile/include/profile_connector.h old mode 100644 new mode 100755 index d83f90749..b9ec0a351 --- a/ext/profile/include/profile_connector.h +++ b/ext/profile/include/profile_connector.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -28,28 +29,20 @@ namespace OHOS { namespace DistributedHardware { -using namespace OHOS::DeviceProfile; - -class ProfileEventCallback : public IProfileEventCallback { -public: - void OnSyncCompleted(const SyncResult &syncResults); - int32_t RegisterProfileCallback(const std::string &pkgName, std::shared_ptr callback); - int32_t UnRegisterProfileCallback(const std::string &pkgName); - -public: - static std::map> profileConnectorCallback_; -}; - -class ProfileConnector : public IProfileEventCallback { +class ProfileConnector final : public OHOS::DeviceProfile::IProfileEventCallback, + public std::enable_shared_from_this { public: + ProfileConnector(); + virtual ~ProfileConnector(); int32_t RegisterProfileCallback(const std::string &pkgName, const std::string &deviceId, - std::shared_ptr callback); + IProfileConnectorCallback* callback); int32_t UnRegisterProfileCallback(const std::string &pkgName); int32_t SubscribeProfileEvents(const std::list &serviceIds, const std::string &deviceId); int32_t UnSubscribeProfileEvents(); - + void OnSyncCompleted(const OHOS::DeviceProfile::SyncResult &syncResults) override; private: - static std::shared_ptr profileEventCallback_; + std::mutex callbackMapMutex_; + std::map callbackMap_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/ext/profile/src/device_profile_adapter.cpp b/ext/profile/src/device_profile_adapter.cpp old mode 100644 new mode 100755 index 5d6165a01..d94136ab2 --- a/ext/profile/src/device_profile_adapter.cpp +++ b/ext/profile/src/device_profile_adapter.cpp @@ -21,52 +21,70 @@ namespace OHOS { namespace DistributedHardware { -std::shared_ptr DeviceProfileAdapter::profileConnector_ = std::make_shared(); - DeviceProfileAdapter::DeviceProfileAdapter() { + LOGI("DeviceProfileAdapter construct"); } DeviceProfileAdapter::~DeviceProfileAdapter() { + LOGI("DeviceProfileAdapter Destructor"); } int32_t DeviceProfileAdapter::RegisterProfileListener(const std::string &pkgName, const std::string &deviceId, std::shared_ptr callback) { - LOGI("DeviceProfileAdapter::RegisterProfileListener"); - deviceProfileAdapterCallback_ = callback; - profileConnector_->RegisterProfileCallback(pkgName, deviceId, std::shared_ptr(this)); + if (pkgName.empty() || deviceId.empty() || callback == nullptr) { + LOGE("Not a reasonable function argument"); + return ERR_DM_INPUT_PARA_INVALID; + } + LOGI("register profile listener with pkgName: %s", pkgName.c_str()); + std::lock_guard mutexLock(deviceProfileAdapterMutex_); + deviceStateManager_ = callback; + if (profileConnector_ == nullptr) { + profileConnector_ = std::make_shared(); + } + if (profileConnector_ != nullptr) { + profileConnector_->RegisterProfileCallback(pkgName, deviceId, this); + } return DM_OK; } int32_t DeviceProfileAdapter::UnRegisterProfileListener(const std::string &pkgName) { - LOGI("DeviceProfileAdapter::RegisterProfileListener"); - deviceProfileAdapterCallback_ = nullptr; - profileConnector_->UnRegisterProfileCallback(pkgName); - return DM_OK; -} - -int32_t DeviceProfileAdapter::OnProfileClientDeviceReady(const std::string &pkgName, const std::string &deviceId) -{ - LOGI("DeviceProfileAdapter::OnProfileClientDeviceReady"); - if (deviceProfileAdapterCallback_ != nullptr) { - deviceProfileAdapterCallback_->OnProfileReady(pkgName, deviceId); - } else { - LOGI("deviceProfileAdapterCallback_ is nullptr"); + if (pkgName.empty()) { + LOGE("not a reasonable function argument"); + return ERR_DM_INPUT_PARA_INVALID; + } + LOGI("unregister profile listener with pkgName: %s", pkgName.c_str()); + std::lock_guard mutexLock(deviceProfileAdapterMutex_); + deviceStateManager_ = nullptr; + if (profileConnector_ != nullptr) { + profileConnector_->UnRegisterProfileCallback(pkgName); } return DM_OK; } void DeviceProfileAdapter::OnProfileChanged(const std::string &pkgName, const std::string &deviceId) { - OnProfileClientDeviceReady(pkgName, deviceId); + LOGI("on profile changed with pkgName: %s", pkgName.c_str()); + std::lock_guard mutexLock(deviceProfileAdapterMutex_); + if (deviceStateManager_ == nullptr) { + LOGE("deviceStateManager_ is nullptr"); + return; + } + deviceStateManager_->OnProfileReady(pkgName, deviceId); } void DeviceProfileAdapter::OnProfileComplete(const std::string &pkgName, const std::string &deviceId) { - OnProfileClientDeviceReady(pkgName, deviceId); + LOGI("on profile complete with pkgName: %s", pkgName.c_str()); + std::lock_guard mutexLock(deviceProfileAdapterMutex_); + if (deviceStateManager_ == nullptr) { + LOGE("deviceStateManager_ is nullptr"); + return; + } + deviceStateManager_->OnProfileReady(pkgName, deviceId); } extern "C" IProfileAdapter *CreateDeviceProfileObject(void) diff --git a/ext/profile/src/profile_connector.cpp b/ext/profile/src/profile_connector.cpp old mode 100644 new mode 100755 index fe46916b1..af66904bc --- a/ext/profile/src/profile_connector.cpp +++ b/ext/profile/src/profile_connector.cpp @@ -18,75 +18,91 @@ #include "dm_constants.h" #include "dm_log.h" +using namespace OHOS::DeviceProfile; + namespace OHOS { namespace DistributedHardware { -std::map> ProfileEventCallback::profileConnectorCallback_ = {}; -std::shared_ptr ProfileConnector::profileEventCallback_ = - std::make_shared(); +ProfileConnector::ProfileConnector() +{ + LOGI("ProfileConnector construct"); +} + +ProfileConnector::~ProfileConnector() +{ + LOGI("ProfileConnector Destructor"); +} int32_t ProfileConnector::RegisterProfileCallback(const std::string &pkgName, const std::string &deviceId, - std::shared_ptr callback) + IProfileConnectorCallback* callback) { - LOGI("ProfileConnector::RegisterProfileCallback"); - profileEventCallback_->RegisterProfileCallback(pkgName, callback); - SubscribeProfileEvents({ "system", "device", "fakeStorage", "fakeSystem" }, deviceId); - return DM_OK; + if (pkgName.empty() || deviceId.empty() || callback == nullptr) { + LOGE("Not a reasonable function argument"); + return ERR_DM_INPUT_PARA_INVALID; + } + + LOGI("register profile callback with pkgName: %s", pkgName.c_str()); + { + std::lock_guard mutexLock(callbackMapMutex_); + if (callbackMap_.find(pkgName) != callbackMap_.end()) { + LOGE("pkgName: %s already exists in the map", pkgName.c_str()); + return ERR_DM_KEY_ALREADY_EXISTS; + } + LOGI("register profile callback pkgName: %s", pkgName.c_str()); + callbackMap_[pkgName] = callback; + } + int32_t ret = SubscribeProfileEvents({ "system", "device", "fakeStorage", "fakeSystem" }, deviceId); + if (ret != DM_OK){ + LOGE("fail to subscribe profile events"); + } + return ret; } int32_t ProfileConnector::UnRegisterProfileCallback(const std::string &pkgName) { - LOGI("ProfileConnector::UnRegisterProfileCallback"); - profileEventCallback_->UnRegisterProfileCallback(pkgName); + if (pkgName.empty()) { + LOGE("Not a reasonable function argument"); + return ERR_DM_INPUT_PARA_INVALID; + } + + LOGI("unregister profile callback with pkgName: %s", pkgName.c_str() ); + std::lock_guard mutexLock(callbackMapMutex_); + if (callbackMap_.find(pkgName) != callbackMap_.end()) { + callbackMap_.erase(pkgName); + } return DM_OK; } int32_t ProfileConnector::SubscribeProfileEvents(const std::list &serviceIds, const std::string &deviceId) { - ExtraInfo extraInfo; - extraInfo["deviceId"] = deviceId; - extraInfo["serviceIds"] = serviceIds; - - std::list subscribeInfos; - SubscribeInfo eventSync; + std::list failedEvents; + std::list subscribeInfos; eventSync.profileEvent = ProfileEvent::EVENT_SYNC_COMPLETED; subscribeInfos.emplace_back(eventSync); - - std::list failedEvents; int32_t errCode = DistributedDeviceProfileClient::GetInstance().SubscribeProfileEvents( - subscribeInfos, profileEventCallback_, failedEvents); - LOGI("ProfileConnector::SubscribeProfileEvents result=%d", errCode); + subscribeInfos, shared_from_this(), failedEvents); + if (errCode != ERR_OK) { + LOGI("subscribe profile events result£º %ud", errCode); + return ERR_DM_UNSUBSCRIBE_DP_EVENTS; + } return DM_OK; } int32_t ProfileConnector::UnSubscribeProfileEvents() { std::list profileEvents; - profileEvents.emplace_back(ProfileEvent::EVENT_PROFILE_CHANGED); - profileEvents.emplace_back(ProfileEvent::EVENT_SYNC_COMPLETED); std::list failedEvents; + profileEvents.emplace_back(ProfileEvent::EVENT_SYNC_COMPLETED); int32_t errCode = DistributedDeviceProfileClient::GetInstance().UnsubscribeProfileEvents( - profileEvents, profileEventCallback_, failedEvents); - LOGI("ProfileConnector::UnSubscribeProfileEvents result=%d", errCode); - return DM_OK; -} - -int32_t ProfileEventCallback::RegisterProfileCallback(const std::string &pkgName, - std::shared_ptr callback) -{ - LOGI("ProfileEventCallback::RegisterProfileCallback"); - profileConnectorCallback_.emplace(pkgName, callback); - return DM_OK; -} - -int32_t ProfileEventCallback::UnRegisterProfileCallback(const std::string &pkgName) -{ - LOGI("ProfileEventCallback::UnRegisterProfileCallback"); - profileConnectorCallback_.erase(pkgName); + profileEvents, shared_from_this(), failedEvents); + if (errCode != ERR_OK) { + LOGI("unSubscribe profile events result£º%ud", errCode); + return ERR_DM_UNSUBSCRIBE_DP_EVENTS; + } return DM_OK; } -void ProfileEventCallback::OnSyncCompleted(const SyncResult &syncResults) +void ProfileConnector::OnSyncCompleted(const SyncResult &syncResults) { std::string deviceId; u_int32_t SyncStatus; @@ -95,7 +111,8 @@ void ProfileEventCallback::OnSyncCompleted(const SyncResult &syncResults) SyncStatus = iterResult.second; } LOGI("ProfileEventCallback::OnSyncCompleted, deviceId = %s", deviceId.c_str()); - for (auto &iter : profileConnectorCallback_) { + std::lock_guard mutexLock(callbackMapMutex_); + for (auto &iter : callbackMap_) { iter.second->OnProfileComplete(iter.first, deviceId); } } diff --git a/services/devicemanagerservice/BUILD.gn b/services/devicemanagerservice/BUILD.gn old mode 100644 new mode 100755 index 12ddf9c64..ba6da08e0 --- a/services/devicemanagerservice/BUILD.gn +++ b/services/devicemanagerservice/BUILD.gn @@ -49,6 +49,7 @@ if (defined(ohos_lite)) { "${common_path}/include", "${common_path}/include/ipc", "${common_path}/include/ipc/model", + "${utils_path}/include", "${utils_path}/include/permission/standard", "//base/security/deviceauth/interfaces/innerkits", "//third_party/json/include", diff --git a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h old mode 100644 new mode 100755 index 7e7ccb82f..332976cdc --- a/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h +++ b/services/devicemanagerservice/include/devicestate/dm_device_state_manager.h @@ -16,11 +16,16 @@ #ifndef OHOS_DM_DEVICE_STATE_MANAGER_H #define OHOS_DM_DEVICE_STATE_MANAGER_H +#include +#include +#include + #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 { #define OFFLINE_TIMEOUT 300 @@ -35,21 +40,20 @@ public: void OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info); void OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &info); void OnDeviceReady(const std::string &pkgName, const DmDeviceInfo &info); - void OnProfileReady(const std::string &pkgName, const std::string deviceId); + void OnProfileReady(const std::string &pkgName, const std::string &deviceId); int32_t RegisterSoftbusStateCallback(); void RegisterOffLineTimer(const DmDeviceInfo &deviceInfo); void StartOffLineTimer(const DmDeviceInfo &deviceInfo); void DeleteTimeOutGroup(std::string deviceId); - private: + std::string profileSoName_; + std::mutex timerMapMutex_; + std::mutex remoteDeviceInfosMutex_; std::shared_ptr softbusConnector_; - std::shared_ptr adapterMgr_; std::shared_ptr listener_; - std::map deviceStateMap_; std::map remoteDeviceInfos_; std::map> timerMap_; std::shared_ptr hiChainConnector_; - std::string profileSoName_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp old mode 100644 new mode 100755 index 9318bd0ab..fd7bd0be3 --- a/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp +++ b/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp @@ -16,8 +16,10 @@ #include "dm_device_state_manager.h" #include "dm_adapter_manager.h" +#include "dm_anonymous.h" #include "dm_constants.h" #include "dm_log.h" + namespace OHOS { namespace DistributedHardware { const int32_t SESSION_CANCEL_TIMEOUT = 0; @@ -34,121 +36,149 @@ static void TimeOut(void *data, DmTimer& timer) } DmDeviceStateManager::DmDeviceStateManager(std::shared_ptr softbusConnector, - std::shared_ptr listener, - std::shared_ptr hiChainConnector) + std::shared_ptr listener, std::shared_ptr hiChainConnector) : softbusConnector_(softbusConnector), listener_(listener), hiChainConnector_(hiChainConnector) { - LOGI("DmDeviceStateManager constructor"); profileSoName_ = "libdevicemanagerext_profile.z.so"; + LOGI("DmDeviceStateManager constructor"); } DmDeviceStateManager::~DmDeviceStateManager() { LOGI("DmDeviceStateManager destructor"); - softbusConnector_->UnRegisterSoftbusStateCallback("DM_PKG_NAME"); + if (softbusConnector_ != nullptr) { + softbusConnector_->UnRegisterSoftbusStateCallback("DM_PKG_NAME"); + } } void DmDeviceStateManager::OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &info) { - LOGI("DmDeviceStateManager::OnDeviceOnline in"); + LOGI("OnDeviceOnline function is called back with pkgName: %s", pkgName.c_str()); RegisterOffLineTimer(info); DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); std::shared_ptr profileAdapter = adapterMgrPtr.GetProfileAdapter(profileSoName_); - if (profileAdapter == nullptr) { - LOGE("OnDeviceOnline profile adapter is null"); - } else { - uint8_t udid[UDID_BUF_LEN] = {0}; - int32_t ret = SoftbusConnector::GetNodeKeyInfoByNetworkId(info.deviceId, NodeDeviceInfoKey::NODE_KEY_UDID, udid, - sizeof(udid)); - if (ret != DM_OK) { - LOGE("DmDeviceStateManager::OnDeviceOnline GetNodeKeyInfo failed"); - } else { - std::string deviceUdid = (char *)udid; - DmDeviceInfo saveInfo = info; + if (profileAdapter != nullptr) { + uint8_t udid[UDID_BUF_LEN + 1] = {0}; + int32_t ret = SoftbusConnector::GetNodeKeyInfoByNetworkId(info.deviceId, + NodeDeviceInfoKey::NODE_KEY_UDID, udid, sizeof(udid)); + if (ret == DM_OK) { std::string uuid; + DmDeviceInfo saveInfo = info; SoftbusConnector::GetUuidByNetworkId(info.deviceId, uuid); - remoteDeviceInfos_[uuid] = saveInfo; + { + std::lock_guard mutexLock(remoteDeviceInfosMutex_); + remoteDeviceInfos_[uuid] = saveInfo; + } + std::string deviceUdid = (char *)udid; LOGI("RegisterProfileListener in, deviceId = %s, deviceUdid = %s, uuid = %s", info.deviceId, deviceUdid.c_str(), uuid.c_str()); - LOGI("RegisterProfileListener out"); + profileAdapter->RegisterProfileListener(pkgName, deviceUdid, shared_from_this()); } } - DmDeviceState state = DEVICE_STATE_ONLINE; - deviceStateMap_[info.deviceId] = DEVICE_STATE_ONLINE; - listener_->OnDeviceStateChange(pkgName, state, info); - LOGI("DmDeviceStateManager::OnDeviceOnline out"); + if (listener_ != nullptr) { + DmDeviceState state = DEVICE_STATE_ONLINE; + listener_->OnDeviceStateChange(pkgName, state, info); + } } void DmDeviceStateManager::OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &info) { + LOGI("OnDeviceOnline function is called with pkgName: %s", pkgName.c_str()); StartOffLineTimer(info); DmAdapterManager &adapterMgrPtr = DmAdapterManager::GetInstance(); std::shared_ptr profileAdapter = adapterMgrPtr.GetProfileAdapter(profileSoName_); - if (profileAdapter == nullptr) { - LOGE("OnDeviceOffline profile adapter is null"); - } else { - auto iter = remoteDeviceInfos_.find(std::string(info.deviceId)); - if (iter == remoteDeviceInfos_.end()) { - } else { + if (profileAdapter != nullptr) { + LOGI("UnRegister Profile Listener"); + profileAdapter->UnRegisterProfileListener(pkgName); + } + { + std::lock_guard mutexLock(remoteDeviceInfosMutex_); + if (remoteDeviceInfos_.find(std::string(info.deviceId)) != remoteDeviceInfos_.end()) { remoteDeviceInfos_.erase(std::string(info.deviceId)); - } + } + } + if (listener_ != nullptr) { + DmDeviceState state = DEVICE_STATE_OFFLINE; + listener_->OnDeviceStateChange(pkgName, state, info); } - DmDeviceState state = DEVICE_STATE_OFFLINE; - deviceStateMap_[info.deviceId] = DEVICE_STATE_OFFLINE; - listener_->OnDeviceStateChange(pkgName, state, info); } void DmDeviceStateManager::OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &info) { - deviceStateMap_[info.deviceId] = DEVICE_INFO_CHANGED; + LOGI("OnDeviceChanged function is called back with pkgName: %s", pkgName.c_str()); } void DmDeviceStateManager::OnDeviceReady(const std::string &pkgName, const DmDeviceInfo &info) { - deviceStateMap_[info.deviceId] = DEVICE_INFO_READY; + LOGI("OnDeviceReady function is called back with pkgName: %s", pkgName.c_str()); } -void DmDeviceStateManager::OnProfileReady(const std::string &pkgName, const std::string deviceId) +void DmDeviceStateManager::OnProfileReady(const std::string &pkgName, const std::string &deviceId) { + LOGI("OnProfileReady function is called back"); + if (pkgName.empty() || deviceId.empty() ) { + LOGE("On profile ready pkgName is empty or deviceId is deviceId"); + return; + } DmDeviceInfo saveInfo; - auto iter = remoteDeviceInfos_.find(deviceId); - if (iter == remoteDeviceInfos_.end()) { - LOGE("DmDeviceStateManager::OnProfileReady complete not find deviceID = %s", deviceId.c_str()); - } else { - saveInfo = iter->second; + { + std::lock_guard mutexLock(remoteDeviceInfosMutex_); + auto iter = remoteDeviceInfos_.find(deviceId); + if (iter == remoteDeviceInfos_.end()) { + LOGE("OnProfileReady complete not find deviceId: %s", GetAnonyString(deviceId).c_str()); + return; + } + saveInfo = iter->second; + } + if (listener_ != nullptr) { + DmDeviceState state = DEVICE_INFO_READY; + listener_->OnDeviceStateChange(pkgName, state, saveInfo); } - DmDeviceState state = DEVICE_INFO_READY; - listener_->OnDeviceStateChange(pkgName, state, saveInfo); } int32_t DmDeviceStateManager::RegisterSoftbusStateCallback() { - softbusConnector_->RegisterSoftbusStateCallback(DM_PKG_NAME, - std::shared_ptr(shared_from_this())); + if (softbusConnector_ != nullptr) { + return softbusConnector_->RegisterSoftbusStateCallback(DM_PKG_NAME, shared_from_this()); + } return DM_OK; } void DmDeviceStateManager::RegisterOffLineTimer(const DmDeviceInfo &deviceInfo) { std::string deviceId; - softbusConnector_->GetUdidByNetworkId(deviceInfo.deviceId, deviceId); - LOGI("Device<%s>Online", deviceId.c_str()); + int32_t ret = softbusConnector_->GetUdidByNetworkId(deviceInfo.deviceId, deviceId); + if (ret != DM_OK) { + LOGE("fail to get udid by networkId"); + return; + } + LOGI("Register OffLine Timer with device: %s", GetAnonyString(deviceId).c_str()); + + std::lock_guard mutexLock(timerMapMutex_); auto iter = timerMap_.find(deviceId); if (iter != timerMap_.end()) { iter->second->Stop(SESSION_CANCEL_TIMEOUT); return; } std::shared_ptr offLineTimer = std::make_shared(deviceId); - timerMap_[deviceId] = offLineTimer; + if (offLineTimer != nullptr) { + timerMap_[deviceId] = offLineTimer; + } } void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo) { std::string deviceId; - softbusConnector_->GetUdidByNetworkId(deviceInfo.deviceId, deviceId); - LOGI("Device<%s>Offline", deviceId.c_str()); + int32_t ret = softbusConnector_->GetUdidByNetworkId(deviceInfo.deviceId, deviceId); + if (ret != DM_OK) { + LOGE("fail to get udid by networkId"); + return; + } + + LOGI("start offline timer with device: %s", GetAnonyString(deviceId).c_str()); + std::lock_guard mutexLock(timerMapMutex_); for (auto &iter : timerMap_) { - if (iter.first.compare(deviceId) == 0) { + if (iter.first == deviceId) { iter.second->Start(OFFLINE_TIMEOUT, TimeOut, this); } } @@ -156,8 +186,10 @@ void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo) void DmDeviceStateManager::DeleteTimeOutGroup(std::string deviceId) { - LOGI("Remove DmDevice<%s> Hichain Group", deviceId.c_str()); - hiChainConnector_->DeleteTimeOutGroup(deviceId.c_str()); + LOGI("remove hichain group with device: %s", GetAnonyString(deviceId).c_str()); + if (hiChainConnector_ != nullptr) { + hiChainConnector_->DeleteTimeOutGroup(deviceId.c_str()); + } } } // namespace DistributedHardware } // namespace OHOS -- Gitee