diff --git a/data_object/frameworks/innerkitsimpl/include/communicator/app_device_handler.h b/data_object/frameworks/innerkitsimpl/include/communicator/app_device_handler.h index 99c72dae8aa1908dbb662eaaec665163546945b3..0bca19bb8de289f6f3b0b0c20bdb7f6db4c2a348 100644 --- a/data_object/frameworks/innerkitsimpl/include/communicator/app_device_handler.h +++ b/data_object/frameworks/innerkitsimpl/include/communicator/app_device_handler.h @@ -16,6 +16,7 @@ #ifndef DISTRIBUTEDDATAFWK_SRC_DEVICE_HANDLER_H #define DISTRIBUTEDDATAFWK_SRC_DEVICE_HANDLER_H #include "softbus_adapter.h" +#include "dev_manager.h" namespace OHOS { namespace ObjectStore { @@ -41,6 +42,7 @@ public: private: std::shared_ptr softbusAdapter_{}; + DevManager *devManager_ = nullptr; }; } // namespace ObjectStore } // namespace OHOS diff --git a/data_object/frameworks/innerkitsimpl/include/communicator/dev_manager.h b/data_object/frameworks/innerkitsimpl/include/communicator/dev_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..166e7f15ff6d33a372982db315c6eaddeb7dd1c9 --- /dev/null +++ b/data_object/frameworks/innerkitsimpl/include/communicator/dev_manager.h @@ -0,0 +1,36 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef DATA_OBJECT_DEV_MANAGER_H +#define DATA_OBJECT_DEV_MANAGER_H +#include +namespace OHOS { +namespace ObjectStore { +class DevManager { +public: + static DevManager *GetInstance() + { + static DevManager *instance = new DevManager(); + return instance; + } + void RegisterDevCallback(); + +private: + DevManager(); + ~DevManager(); + int32_t Init(); +}; +} // namespace ObjectStore +} // namespace OHOS +#endif // DATA_OBJECT_DEV_MANAGER_H \ No newline at end of file diff --git a/data_object/frameworks/innerkitsimpl/src/adaptor/flat_object_store.cpp b/data_object/frameworks/innerkitsimpl/src/adaptor/flat_object_store.cpp index f23a4721184f0a8202b576bad84e44b3641aeee8..40ab137b43cb242ef317aff7c2c879e4416af922 100644 --- a/data_object/frameworks/innerkitsimpl/src/adaptor/flat_object_store.cpp +++ b/data_object/frameworks/innerkitsimpl/src/adaptor/flat_object_store.cpp @@ -47,7 +47,7 @@ FlatObjectStore::~FlatObjectStore() uint32_t FlatObjectStore::CreateObject(const std::string &sessionId) { - if (!storageEngine_->isOpened_) { + if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) { LOG_ERROR("FlatObjectStore::DB has not inited"); return ERR_DB_NOT_INIT; } @@ -97,7 +97,7 @@ uint32_t FlatObjectStore::CreateObject(const std::string &sessionId) uint32_t FlatObjectStore::Delete(const std::string &sessionId) { - if (!storageEngine_->isOpened_) { + if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) { LOG_ERROR("FlatObjectStore::DB has not inited"); return ERR_DB_NOT_INIT; } @@ -112,7 +112,7 @@ uint32_t FlatObjectStore::Delete(const std::string &sessionId) uint32_t FlatObjectStore::Watch(const std::string &sessionId, std::shared_ptr watcher) { - if (!storageEngine_->isOpened_) { + if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) { LOG_ERROR("FlatObjectStore::DB has not inited"); return ERR_DB_NOT_INIT; } @@ -125,7 +125,7 @@ uint32_t FlatObjectStore::Watch(const std::string &sessionId, std::shared_ptrisOpened_) { + if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) { LOG_ERROR("FlatObjectStore::DB has not inited"); return ERR_DB_NOT_INIT; } @@ -138,7 +138,7 @@ uint32_t FlatObjectStore::UnWatch(const std::string &sessionId) uint32_t FlatObjectStore::Put(const std::string &sessionId, const std::string &key, std::vector value) { - if (!storageEngine_->isOpened_) { + if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) { LOG_ERROR("FlatObjectStore::DB has not inited"); return ERR_DB_NOT_INIT; } @@ -147,7 +147,7 @@ uint32_t FlatObjectStore::Put(const std::string &sessionId, const std::string &k uint32_t FlatObjectStore::Get(std::string &sessionId, const std::string &key, Bytes &value) { - if (!storageEngine_->isOpened_) { + if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) { LOG_ERROR("FlatObjectStore::DB has not inited"); return ERR_DB_NOT_INIT; } @@ -156,7 +156,7 @@ uint32_t FlatObjectStore::Get(std::string &sessionId, const std::string &key, By uint32_t FlatObjectStore::SetStatusNotifier(std::shared_ptr notifier) { - if (!storageEngine_->isOpened_) { + if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) { LOG_ERROR("FlatObjectStore::DB has not inited"); return ERR_DB_NOT_INIT; } @@ -166,7 +166,7 @@ uint32_t FlatObjectStore::SetStatusNotifier(std::shared_ptr notif uint32_t FlatObjectStore::SyncAllData(const std::string &sessionId, const std::function &)> &onComplete) { - if (!storageEngine_->isOpened_) { + if (!storageEngine_->isOpened_ && storageEngine_->Open(bundleName_) != SUCCESS) { LOG_ERROR("FlatObjectStore::DB has not inited"); return ERR_DB_NOT_INIT; } diff --git a/data_object/frameworks/innerkitsimpl/src/communicator/app_device_handler.cpp b/data_object/frameworks/innerkitsimpl/src/communicator/app_device_handler.cpp index 2838d3e71d16af138720c543fa92c85ea6e1f101..2a9eaf3fe13f3299871182204d3183f728ad3d26 100644 --- a/data_object/frameworks/innerkitsimpl/src/communicator/app_device_handler.cpp +++ b/data_object/frameworks/innerkitsimpl/src/communicator/app_device_handler.cpp @@ -22,6 +22,7 @@ namespace ObjectStore { AppDeviceHandler::AppDeviceHandler() { softbusAdapter_ = SoftBusAdapter::GetInstance(); + devManager_ = DevManager::GetInstance(); } AppDeviceHandler::~AppDeviceHandler() @@ -31,6 +32,7 @@ AppDeviceHandler::~AppDeviceHandler() void AppDeviceHandler::Init() { softbusAdapter_->Init(); + devManager_->RegisterDevCallback(); } Status AppDeviceHandler::StartWatchDeviceChange( diff --git a/data_object/frameworks/innerkitsimpl/src/communicator/dev_manager.cpp b/data_object/frameworks/innerkitsimpl/src/communicator/dev_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8cb66136067525dbe6e819dbdc790144bf9121a --- /dev/null +++ b/data_object/frameworks/innerkitsimpl/src/communicator/dev_manager.cpp @@ -0,0 +1,124 @@ +/* + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "dev_manager.h" + +#include +#include + +#include "device_manager.h" +#include "device_manager_callback.h" +#include "dm_device_info.h" +#include "softbus_adapter.h" +#include "app_types.h" + +namespace OHOS { +namespace ObjectStore { +using namespace OHOS::DistributedHardware; +constexpr int32_t DM_OK = 0; +constexpr int32_t DM_ERROR = -1; +constexpr const char *PKG_NAME = "ohos.objectstore"; +class DMStateCallback : public DeviceStateCallback { +public: + explicit DMStateCallback(std::shared_ptr softBusAdapter) : softBusAdapter_(softBusAdapter){}; + void OnDeviceOnline(const DmDeviceInfo &deviceInfo) override; + void OnDeviceOffline(const DmDeviceInfo &deviceInfo) override; + void OnDeviceChanged(const DmDeviceInfo &deviceInfo) override; + void OnDeviceReady(const DmDeviceInfo &deviceInfo) override; + +private: + std::shared_ptr softBusAdapter_; + void NotifyAll(const DmDeviceInfo &deviceInfo, DeviceChangeType type); +}; + +void DMStateCallback::OnDeviceOnline(const DmDeviceInfo &deviceInfo) +{ + std::string udid = softBusAdapter_->GetUdidByNodeId(std::string(deviceInfo.networkId)); + LOG_INFO("[Online] id:%{public}s, name:%{public}s, typeId:%{public}d", SoftBusAdapter::ToBeAnonymous(udid).c_str(), + deviceInfo.deviceName, deviceInfo.deviceTypeId); + NotifyAll(deviceInfo, DeviceChangeType::DEVICE_ONLINE); +} + +void DMStateCallback::OnDeviceOffline(const DmDeviceInfo &deviceInfo) +{ + std::string udid = softBusAdapter_->GetUdidByNodeId(std::string(deviceInfo.networkId)); + LOG_INFO("[Offline] id:%{public}s, name:%{public}s, typeId:%{public}d", + SoftBusAdapter::ToBeAnonymous(udid).c_str(), deviceInfo.deviceName, deviceInfo.deviceTypeId); + NotifyAll(deviceInfo, DeviceChangeType::DEVICE_OFFLINE); +} + +void DMStateCallback::OnDeviceChanged(const DmDeviceInfo &deviceInfo) +{ + std::string udid = softBusAdapter_->GetUdidByNodeId(std::string(deviceInfo.networkId)); + LOG_INFO("[InfoChange] id:%{public}s, name:%{public}s", SoftBusAdapter::ToBeAnonymous(udid).c_str(), + deviceInfo.deviceName); +} + +void DMStateCallback::OnDeviceReady(const DmDeviceInfo &deviceInfo) +{ +} + +void DMStateCallback::NotifyAll(const DmDeviceInfo &deviceInfo, DeviceChangeType type) +{ + DeviceInfo di = { std::string(deviceInfo.networkId), std::string(deviceInfo.deviceName), + std::to_string(deviceInfo.deviceTypeId) }; + softBusAdapter_->NotifyAll(di, type); +} + +DevManager::DevManager() +{ +} + +DevManager::~DevManager() +{ +} + +int32_t DevManager::Init() +{ + auto &deviceManager = DeviceManager::GetInstance(); + std::shared_ptr deviceInitCallback = nullptr; + auto deviceStateCallback = std::make_shared(SoftBusAdapter::GetInstance()); + int32_t status = deviceManager.InitDeviceManager(PKG_NAME, deviceInitCallback); + if (status != DM_OK) { + return status; + } + status = deviceManager.RegisterDevStateCallback(PKG_NAME, "", deviceStateCallback); + return status; +} + +void DevManager::RegisterDevCallback() +{ + int32_t status = Init(); + if (status == DM_OK) { + return; + } + LOG_INFO("register device callback failed, try again."); + std::thread th = std::thread([this]() { + constexpr int RETRY_TIMES = 300; + int i = 0; + int32_t status = DM_ERROR; + while (i++ < RETRY_TIMES) { + status = Init(); + if (status == DM_OK) { + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + LOG_INFO("register device callback exit now: %{public}d times, status: %{public}d", i, status); + }); + th.detach(); +} + +} // namespace ObjectStore +} // namespace OHOS \ No newline at end of file diff --git a/data_object/frameworks/innerkitsimpl/src/communicator/softbus_adapter_standard.cpp b/data_object/frameworks/innerkitsimpl/src/communicator/softbus_adapter_standard.cpp index 00b05690b6e106950708e8ee53a90b73a7464d1f..8c1d9fba5dd8f4c25cb933f2623eb5ca911093a5 100644 --- a/data_object/frameworks/innerkitsimpl/src/communicator/softbus_adapter_standard.cpp +++ b/data_object/frameworks/innerkitsimpl/src/communicator/softbus_adapter_standard.cpp @@ -40,25 +40,6 @@ constexpr int32_t DEVICE_ID_SIZE_MAX = 65; constexpr int32_t ID_BUF_LEN = 65; using namespace std; -class AppDeviceListenerWrap { -public: - explicit AppDeviceListenerWrap() - { - } - ~AppDeviceListenerWrap() - { - } - static void SetDeviceHandler(SoftBusAdapter *handler); - static void OnDeviceOnline(NodeBasicInfo *info); - static void OnDeviceOffline(NodeBasicInfo *info); - static void OnDeviceInfoChanged(NodeBasicInfoType type, NodeBasicInfo *info); - -private: - static SoftBusAdapter *softBusAdapter_; - static void NotifyAll(NodeBasicInfo *info, DeviceChangeType type); -}; -SoftBusAdapter *AppDeviceListenerWrap::softBusAdapter_; - class AppDataListenerWrap { public: static void SetDataHandler(SoftBusAdapter *handler); @@ -76,52 +57,11 @@ public: SoftBusAdapter *AppDataListenerWrap::softBusAdapter_; std::shared_ptr SoftBusAdapter::instance_; -void AppDeviceListenerWrap::OnDeviceInfoChanged(NodeBasicInfoType type, NodeBasicInfo *info) -{ - std::string udid = softBusAdapter_->GetUdidByNodeId(std::string(info->networkId)); - LOG_INFO("[InfoChange] type:%{public}d, id:%{public}s, name:%{public}s", type, - SoftBusAdapter::ToBeAnonymous(udid).c_str(), info->deviceName); -} - -void AppDeviceListenerWrap::OnDeviceOffline(NodeBasicInfo *info) -{ - std::string udid = softBusAdapter_->GetUdidByNodeId(std::string(info->networkId)); - LOG_INFO("[Offline] id:%{public}s, name:%{public}s, typeId:%{public}d", - SoftBusAdapter::ToBeAnonymous(udid).c_str(), info->deviceName, info->deviceTypeId); - NotifyAll(info, DeviceChangeType::DEVICE_OFFLINE); -} - -void AppDeviceListenerWrap::OnDeviceOnline(NodeBasicInfo *info) -{ - std::string udid = softBusAdapter_->GetUdidByNodeId(std::string(info->networkId)); - LOG_INFO("[Online] id:%{public}s, name:%{public}s, typeId:%{public}d", SoftBusAdapter::ToBeAnonymous(udid).c_str(), - info->deviceName, info->deviceTypeId); - NotifyAll(info, DeviceChangeType::DEVICE_ONLINE); -} - -void AppDeviceListenerWrap::SetDeviceHandler(SoftBusAdapter *handler) -{ - LOG_INFO("SetDeviceHandler."); - softBusAdapter_ = handler; -} - -void AppDeviceListenerWrap::NotifyAll(NodeBasicInfo *info, DeviceChangeType type) -{ - DeviceInfo di = { std::string(info->networkId), std::string(info->deviceName), std::to_string(info->deviceTypeId) }; - softBusAdapter_->NotifyAll(di, type); -} - SoftBusAdapter::SoftBusAdapter() { LOG_INFO("begin"); - AppDeviceListenerWrap::SetDeviceHandler(this); AppDataListenerWrap::SetDataHandler(this); - nodeStateCb_.events = EVENT_NODE_STATE_MASK; - nodeStateCb_.onNodeOnline = AppDeviceListenerWrap::OnDeviceOnline; - nodeStateCb_.onNodeOffline = AppDeviceListenerWrap::OnDeviceOffline; - nodeStateCb_.onNodeBasicInfoChanged = AppDeviceListenerWrap::OnDeviceInfoChanged; - sessionListener_.OnSessionOpened = AppDataListenerWrap::OnSessionOpened; sessionListener_.OnSessionClosed = AppDataListenerWrap::OnSessionClosed; sessionListener_.OnBytesReceived = AppDataListenerWrap::OnBytesReceived; diff --git a/data_object/interfaces/innerkits/BUILD.gn b/data_object/interfaces/innerkits/BUILD.gn index 13308b2ca7d954fd4e337ae2e2d6653827ed1c5c..eea8889201800ae541822ee1dba47ea86dadf7d2 100644 --- a/data_object/interfaces/innerkits/BUILD.gn +++ b/data_object/interfaces/innerkits/BUILD.gn @@ -48,6 +48,7 @@ ohos_shared_library("distributeddataobject_impl") { "../../frameworks/innerkitsimpl/src/communicator/ark_communication_provider.cpp", "../../frameworks/innerkitsimpl/src/communicator/communication_provider.cpp", "../../frameworks/innerkitsimpl/src/communicator/communication_provider_impl.cpp", + "../../frameworks/innerkitsimpl/src/communicator/dev_manager.cpp", "../../frameworks/innerkitsimpl/src/communicator/process_communicator_impl.cpp", "../../frameworks/innerkitsimpl/src/communicator/softbus_adapter_standard.cpp", ] @@ -59,6 +60,7 @@ ohos_shared_library("distributeddataobject_impl") { deps = [ "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/dfx:distributeddata_dfx_static", "//foundation/distributeddatamgr/kv_store/frameworks/libs/distributeddb:distributeddb", + "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp:devicemanagersdk", "//third_party/bounds_checking_function:libsec_static", "//third_party/libuv:uv", ]