diff --git a/interfaces/innerkits/core/BUILD.gn b/interfaces/innerkits/core/BUILD.gn new file mode 100644 index 00000000..8635ec6d --- /dev/null +++ b/interfaces/innerkits/core/BUILD.gn @@ -0,0 +1,56 @@ +# Copyright (c) 2021-2023 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. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") +import("//foundation/deviceprofile/device_info_manager/deviceprofile.gni") + +config("distributed_device_profile_sdk_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "include/callback", + "${device_profile_common}/include/constants", + "${device_profile_common}/include/interfaces", + "${device_profile_common}/include/utils", + "//third_party/json/include", + ] +} + +ohos_shared_library("distributed_device_profile_sdk") { + install_enable = true + + sources = [ + "src/callback/device_profile_load_callback.cpp", + "src/distributed_device_profile_client.cpp", + "src/distributed_device_profile_proxy.cpp", + ] + + public_configs = [ + ":distributed_device_profile_sdk_config", + ] + + deps = [ "${device_profile_common}:distributed_device_profile_common" ] + + external_deps = [ + "c_utils:utils", + "eventhandler:libeventhandler", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + "relational_store:native_rdb" + ] + + part_name = "device_info_manager" + subsystem_name = "deviceprofile" +} diff --git a/interfaces/innerkits/core/include/callback/device_profile_load_callback.h b/interfaces/innerkits/core/include/callback/device_profile_load_callback.h new file mode 100644 index 00000000..6c090685 --- /dev/null +++ b/interfaces/innerkits/core/include/callback/device_profile_load_callback.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 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 OHOS_DEVICE_PROFILE_LOAD_CALLBACK_H +#define OHOS_DEVICE_PROFILE_LOAD_CALLBACK_H + +#include +#include + +#include "refbase.h" +#include "system_ability_load_callback_stub.h" + +namespace OHOS { class IRemoteObject; } +namespace OHOS { +namespace DistributedDeviceProfile { +class DeviceProfileLoadCallback : public SystemAbilityLoadCallbackStub { +public: + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const sptr &remoteObject) override; + void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; +}; +} +} +#endif \ No newline at end of file diff --git a/interfaces/innerkits/core/include/distributed_device_profile_client.h b/interfaces/innerkits/core/include/distributed_device_profile_client.h new file mode 100644 index 00000000..27c28856 --- /dev/null +++ b/interfaces/innerkits/core/include/distributed_device_profile_client.h @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2021-2023 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 OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_CLIENT_H +#define OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_CLIENT_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "i_profile_change_listener.h" +#include "i_distributed_device_profile.h" +#include "iremote_object.h" +#include "refbase.h" +#include "single_instance.h" +#include "dp_subscribe_info.h" +#include "distributed_device_profile_constants.h" +#include "sync_completed_callback_stub.h" +#include "profile_change_listener_stub.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class DistributedDeviceProfileClient { +DECLARE_SINGLE_INSTANCE(DistributedDeviceProfileClient); + +public: + int32_t PutAccessControlProfile(const AccessControlProfile& accessControlProfile); + int32_t UpdateAccessControlProfile(const AccessControlProfile& accessControlProfile); + int32_t GetTrustDeviceProfile(const std::string& deviceId, TrustDeviceProfile& trustDeviceProfile); + int32_t GetAllTrustDeviceProfile(std::vector& trustDeviceProfiles); + int32_t GetAccessControlProfile(std::map params, + std::vector& accessControlProfiles); + int32_t GetAllAccessControlProfile(std::vector& accessControlProfiles); + int32_t DeleteAccessControlProfile(int32_t accessControlId); + int32_t PutServiceProfile(const ServiceProfile& serviceProfile); + int32_t PutServiceProfileBatch(const std::vector& serviceProfiles); + int32_t PutCharacteristicProfile(const CharacteristicProfile& characteristicProfile); + int32_t PutCharacteristicProfileBatch(const std::vector& characteristicProfiles); + int32_t GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile); + int32_t GetServiceProfile(const std::string& deviceId, const std::string& serviceName, + ServiceProfile& serviceProfile); + int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& characteristicId, CharacteristicProfile& characteristicProfile); + int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName); + int32_t DeleteCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& characteristicKey); + int32_t SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo); + int32_t UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo); + int32_t SyncDeviceProfile(const SyncOptions& syncOptions, sptr syncCb); + + void LoadSystemAbilitySuccess(const sptr &remoteObject); + void LoadSystemAbilityFail(); + +private: + void SendSubscribeInfosToService(); + sptr LoadDeviceProfileService(); + sptr GetDeviceProfileService(); + void OnServiceDied(const sptr& remote); + + class DeviceProfileDeathRecipient : public IRemoteObject::DeathRecipient { + public: + void OnRemoteDied(const wptr& remote); + }; + std::condition_variable proxyConVar_; + std::mutex serviceLock_; + sptr dpProxy_ = nullptr; + sptr dpDeathRecipient_ = nullptr; + std::map subscribeInfos_; +}; +} // namespace DeviceProfile +} // namespace OHOS +#endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_CLIENT_H \ No newline at end of file diff --git a/interfaces/innerkits/core/include/distributed_device_profile_proxy.h b/interfaces/innerkits/core/include/distributed_device_profile_proxy.h new file mode 100644 index 00000000..7e51a6a7 --- /dev/null +++ b/interfaces/innerkits/core/include/distributed_device_profile_proxy.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023 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 OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_PROXY_H +#define OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_PROXY_H + +#include +#include +#include +#include + +#include "i_distributed_device_profile.h" +#include "iremote_broker.h" +#include "iremote_proxy.h" +#include "refbase.h" +#include "distributed_device_profile_enums.h" +#include "distributed_device_profile_errors.h" +#include "ipc_utils.h" +#include "profile_change_listener_stub.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +class DistributedDeviceProfileProxy : public IRemoteProxy { +public: + explicit DistributedDeviceProfileProxy(const sptr& impl) + : IRemoteProxy(impl) {} + ~DistributedDeviceProfileProxy() {} + + int32_t PutAccessControlProfile(const AccessControlProfile& aclProfile) override; + int32_t UpdateAccessControlProfile(const AccessControlProfile& aclProfile) override; + int32_t GetTrustDeviceProfile(const std::string& deviceId, TrustDeviceProfile& trustDeviceProfile) override; + int32_t GetAllTrustDeviceProfile(std::vector& trustDeviceProfiles) override; + int32_t GetAccessControlProfile(std::map queryParams, + std::vector& accessControlProfiles) override; + int32_t GetAllAccessControlProfile(std::vector& accessControlProfiles) override; + int32_t DeleteAccessControlProfile(int32_t accessControlId) override; + int32_t PutServiceProfile(const ServiceProfile& serviceProfile) override; + int32_t PutServiceProfileBatch(const std::vector& serviceProfiles) override; + int32_t PutCharacteristicProfile(const CharacteristicProfile& charProfile) override; + int32_t PutCharacteristicProfileBatch(const std::vector& charProfiles) override; + int32_t GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile) override; + int32_t GetServiceProfile(const std::string& deviceId, const std::string& serviceName, + ServiceProfile& serviceProfile) override; + int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& characteristicId, CharacteristicProfile& charProfile) override; + int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName) override; + int32_t DeleteCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, + const std::string& characteristicId) override; + int32_t SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo) override; + int32_t UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo) override; + int32_t SyncDeviceProfile(const SyncOptions& syncOptions, sptr syncCompletedCallback) override; + int32_t SendSubscribeInfos(std::map listenerMap) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace DeviceProfile +} // namespace OHOS +#endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_PROXY_H \ No newline at end of file diff --git a/interfaces/innerkits/core/src/callback/device_profile_load_callback.cpp b/interfaces/innerkits/core/src/callback/device_profile_load_callback.cpp new file mode 100644 index 00000000..1835ca46 --- /dev/null +++ b/interfaces/innerkits/core/src/callback/device_profile_load_callback.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 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 "device_profile_load_callback.h" + +#include "distributed_device_profile_log.h" +#include "distributed_device_profile_client.h" + +namespace OHOS { class IRemoteObject; } +namespace OHOS { +namespace DistributedDeviceProfile { + +const std::string TAG = "DeviceProfileLoadCallback"; + +void DeviceProfileLoadCallback::OnLoadSystemAbilitySuccess( + int32_t systemAbilityId, const sptr &remoteObject) +{ + DistributedDeviceProfileClient::GetInstance().LoadSystemAbilitySuccess(remoteObject); + HILOGI("on load system ability success!"); +} + +void DeviceProfileLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + DistributedDeviceProfileClient::GetInstance().LoadSystemAbilityFail(); + HILOGI("on load system ability failed!"); +} +} +} \ No newline at end of file diff --git a/interfaces/innerkits/core/src/distributed_device_profile_client.cpp b/interfaces/innerkits/core/src/distributed_device_profile_client.cpp new file mode 100644 index 00000000..3785683a --- /dev/null +++ b/interfaces/innerkits/core/src/distributed_device_profile_client.cpp @@ -0,0 +1,407 @@ +/* + * Copyright (c) 2021-2023 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 "distributed_device_profile_client.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "profile_change_listener_stub.h" +#include "device_profile_load_callback.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "event_handler.h" +#include "event_runner.h" +#include "i_distributed_device_profile.h" +#include "if_system_ability_manager.h" +#include "ipc_skeleton.h" +#include "iremote_broker.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace std::chrono_literals; + +namespace { + const std::string TAG = "DistributedDeviceProfileClient"; + constexpr int32_t LOAD_SA_TIMEOUT_MS = 10000; +} + +IMPLEMENT_SINGLE_INSTANCE(DistributedDeviceProfileClient); + +sptr DistributedDeviceProfileClient::LoadDeviceProfileService() +{ + sptr loadCallback = new DeviceProfileLoadCallback(); + if (loadCallback == nullptr) { + HILOGE("loadCallback is nullptr."); + return nullptr; + } + + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + HILOGE("get samgr failed"); + return nullptr; + } + + int32_t ret = samgrProxy->LoadSystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID, loadCallback); + if (ret != ERR_OK) { + HILOGE("Failed to Load systemAbility"); + return nullptr; + } + { + std::unique_lock lock(serviceLock_); + auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS), + [this]() { return dpProxy_ != nullptr; }); + if (waitStatus && dpProxy_ != nullptr) { + HILOGE("Get profile Service success!"); + return dpProxy_; + } + } + return nullptr; +} + +void DistributedDeviceProfileClient::LoadSystemAbilitySuccess(const sptr &remoteObject) +{ + HILOGI("DistributedDeviceProfileClient FinishStartSA"); + std::lock_guard lock(serviceLock_); + if (dpDeathRecipient_ == nullptr) { + dpDeathRecipient_ = sptr(new DeviceProfileDeathRecipient); + } + if (remoteObject != nullptr) { + remoteObject->AddDeathRecipient(dpDeathRecipient_); + dpProxy_ = iface_cast(remoteObject); + proxyConVar_.notify_one(); + } + HILOGI("Send SubscribeInfos cache in proxy to service!"); + std::thread(&DistributedDeviceProfileClient::SendSubscribeInfosToService, this).detach(); +} + +void DistributedDeviceProfileClient::LoadSystemAbilityFail() +{ + std::lock_guard lock(serviceLock_); + dpProxy_ = nullptr; +} + +void DistributedDeviceProfileClient::SendSubscribeInfosToService() +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("dpService is nullptr!"); + return; + } + { + std::lock_guard lock(serviceLock_); + if (subscribeInfos_.empty() || subscribeInfos_.size() > MAX_SUBSCRIBE_INFO_SIZE) { + HILOGE("SubscribeInfos size is invalid!"); + return; + } + dpService->SendSubscribeInfos(subscribeInfos_); + } +} + +int32_t DistributedDeviceProfileClient::PutAccessControlProfile(const AccessControlProfile& accessControlProfile) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->PutAccessControlProfile(accessControlProfile); +} + +int32_t DistributedDeviceProfileClient::UpdateAccessControlProfile(const AccessControlProfile& accessControlProfile) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->PutAccessControlProfile(accessControlProfile); +} + +int32_t DistributedDeviceProfileClient::GetTrustDeviceProfile(const std::string& deviceId, + TrustDeviceProfile& trustDeviceProfile) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->GetTrustDeviceProfile(deviceId, trustDeviceProfile); +} + +int32_t DistributedDeviceProfileClient::GetAllTrustDeviceProfile(std::vector& trustDeviceProfiles) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->GetAllTrustDeviceProfile(trustDeviceProfiles); +} + +int32_t DistributedDeviceProfileClient::GetAccessControlProfile(std::map params, + std::vector& accessControlProfiles) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + if (params.empty() || params.size() > MAX_PARAM_SIZE) { + HILOGE("Params size is invalid!"); + return DP_INVALID_PARAMS; + } + return dpService->GetAccessControlProfile(params, accessControlProfiles); +} + +int32_t DistributedDeviceProfileClient::GetAllAccessControlProfile( + std::vector& accessControlProfiles) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->GetAllAccessControlProfile(accessControlProfiles); +} + +int32_t DistributedDeviceProfileClient::DeleteAccessControlProfile(int32_t accessControlId) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->DeleteAccessControlProfile(accessControlId); +} + +int32_t DistributedDeviceProfileClient::PutServiceProfile(const ServiceProfile& serviceProfile) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->PutServiceProfile(serviceProfile); +} + +int32_t DistributedDeviceProfileClient::PutServiceProfileBatch(const std::vector& serviceProfiles) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + if (serviceProfiles.empty() || serviceProfiles.size() > MAX_PROFILE_SIZE) { + HILOGE("ServiceProfiles size is invalid!"); + return DP_INVALID_PARAMS; + } + return dpService->PutServiceProfileBatch(serviceProfiles); +} + +int32_t DistributedDeviceProfileClient::PutCharacteristicProfile(const CharacteristicProfile& characteristicProfile) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->PutCharacteristicProfile(characteristicProfile); +} + +int32_t DistributedDeviceProfileClient::PutCharacteristicProfileBatch( + const std::vector& characteristicProfiles) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + if (characteristicProfiles.empty() || characteristicProfiles.size() > MAX_PROFILE_SIZE) { + HILOGE("ServiceProfiles size is invalid!"); + return DP_INVALID_PARAMS; + } + return dpService->PutCharacteristicProfileBatch(characteristicProfiles); +} + +int32_t DistributedDeviceProfileClient::GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->GetDeviceProfile(deviceId, deviceProfile); +} + +int32_t DistributedDeviceProfileClient::GetServiceProfile(const std::string& deviceId, const std::string& serviceName, + ServiceProfile& serviceProfile) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->GetServiceProfile(deviceId, serviceName, serviceProfile); +} + +int32_t DistributedDeviceProfileClient::GetCharacteristicProfile(const std::string& deviceId, + const std::string& serviceName, const std::string& characteristicId, CharacteristicProfile& characteristicProfile) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->GetCharacteristicProfile(deviceId, serviceName, characteristicId, characteristicProfile); +} + +int32_t DistributedDeviceProfileClient::DeleteServiceProfile(const std::string& deviceId, + const std::string& serviceName) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->DeleteServiceProfile(deviceId, serviceName); +} + +int32_t DistributedDeviceProfileClient::DeleteCharacteristicProfile(const std::string& deviceId, + const std::string& serviceName, const std::string& characteristicKey) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + return dpService->DeleteCharacteristicProfile(deviceId, serviceName, characteristicKey); +} + +int32_t DistributedDeviceProfileClient::SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo) +{ + auto dpService = LoadDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + { + std::lock_guard lock(serviceLock_); + if (subscribeInfos_.size() > MAX_LISTENER_SIZE) { + HILOGE("ProfileChangeListeners size is invalid!"); + return DP_EXCEED_MAX_SIZE_FAIL; + } + std::string subscribeTag = + subscribeInfo.GetSubscribeKey() + SEPARATOR + std::to_string(subscribeInfo.GetSaId()); + subscribeInfos_[subscribeTag] = subscribeInfo; + } + return dpService->SubscribeDeviceProfile(subscribeInfo); +} + +int32_t DistributedDeviceProfileClient::UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo) +{ + auto dpService = LoadDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + { + std::lock_guard lock(serviceLock_); + subscribeInfos_.erase(subscribeInfo.GetSubscribeKey() + SEPARATOR + std::to_string(subscribeInfo.GetSaId())); + } + return dpService->UnSubscribeDeviceProfile(subscribeInfo); +} + +int32_t DistributedDeviceProfileClient::SyncDeviceProfile(const SyncOptions& syncOptions, + sptr syncCb) +{ + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("Get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + if (syncCb == nullptr) { + HILOGE("SyncCb is nullptr!"); + return DP_SYNC_DEVICE_FAIL; + } + sptr syncCompletedCallback = syncCb->AsObject(); + if (syncCompletedCallback == nullptr) { + HILOGE("SyncCb ipc cast fail!"); + return DP_SYNC_DEVICE_FAIL; + } + return dpService->SyncDeviceProfile(syncOptions, syncCompletedCallback); +} + +sptr DistributedDeviceProfileClient::GetDeviceProfileService() +{ + { + std::lock_guard lock(serviceLock_); + if (dpProxy_ != nullptr) { + return dpProxy_; + } + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + HILOGE("get samgr failed"); + return nullptr; + } + auto object = samgrProxy->CheckSystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID); + if (object != nullptr) { + HILOGI("get service succeeded"); + if (dpDeathRecipient_ == nullptr) { + dpDeathRecipient_ = sptr(new DeviceProfileDeathRecipient); + } + object->AddDeathRecipient(dpDeathRecipient_); + dpProxy_ = iface_cast(object); + return dpProxy_; + } + } + + HILOGW("remoteObject is null!"); + if (LoadDeviceProfileService()) { + std::lock_guard lock(serviceLock_); + if (dpProxy_ != nullptr) { + return dpProxy_; + } else { + HILOGE("load dp service failed"); + return nullptr; + } + } + HILOGE("load dp service failed"); + return nullptr; +} + +void DistributedDeviceProfileClient::OnServiceDied(const sptr& remote) +{ + HILOGI("called"); + std::lock_guard lock(serviceLock_); + dpProxy_ = nullptr; +} + +void DistributedDeviceProfileClient::DeviceProfileDeathRecipient::OnRemoteDied(const wptr& remote) +{ + DistributedDeviceProfileClient::GetInstance().OnServiceDied(remote.promote()); +} +} // namespace DeviceProfile +} // namespace OHOS diff --git a/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp b/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp new file mode 100644 index 00000000..d8c8681b --- /dev/null +++ b/interfaces/innerkits/core/src/distributed_device_profile_proxy.cpp @@ -0,0 +1,340 @@ +/* + * Copyright (c) 2021-2023 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 +#include "distributed_device_profile_proxy.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +namespace { + const std::string TAG = "DistributedDeviceProfileProxy"; +} + +int32_t DistributedDeviceProfileProxy::PutAccessControlProfile(const AccessControlProfile& aclProfile) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + if (!aclProfile.Marshalling(data)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::PUT_ACL_PROFILE), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::UpdateAccessControlProfile(const AccessControlProfile& aclProfile) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + if (!aclProfile.Marshalling(data)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::UPDATE_ACL_PROFILE), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::GetTrustDeviceProfile(const std::string& deviceId, + TrustDeviceProfile& trustDeviceProfile) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + WRITE_HELPER(data, String, deviceId); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::GET_TRUST_DEVICE_PROFILE), data, reply); + if (!trustDeviceProfile.UnMarshalling(data)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::GetAllTrustDeviceProfile(std::vector& trustDeviceProfiles) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::GET_ALL_TRUST_DEVICE_PROFILE), data, reply); + if (!IpcUtils::UnMarshalling(reply, trustDeviceProfiles)) { + HILOGE("dp ipc read parcel fail"); + return DP_READ_PARCEL_FAIL; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::GetAccessControlProfile(std::map queryParams, + std::vector& accessControlProfiles) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + if (!IpcUtils::Marshalling(data, queryParams)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::GET_ACL_PROFILE), data, reply); + if (!IpcUtils::UnMarshalling(reply, accessControlProfiles)) { + HILOGE("dp ipc read parcel fail"); + return DP_READ_PARCEL_FAIL; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::GetAllAccessControlProfile( + std::vector& accessControlProfiles) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::GET_ALL_ACL_PROFILE), data, reply); + if (!IpcUtils::UnMarshalling(reply, accessControlProfiles)) { + HILOGE("dp ipc read parcel fail"); + return DP_READ_PARCEL_FAIL; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::DeleteAccessControlProfile(int32_t accessControlId) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + WRITE_HELPER(data, Int32, accessControlId); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::DELETE_ACL_PROFILE), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::PutServiceProfile(const ServiceProfile& serviceProfile) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + if (!serviceProfile.Marshalling(data)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::PUT_SERVICE_PROFILE), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::PutServiceProfileBatch(const std::vector& serviceProfiles) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + if (!IpcUtils::Marshalling(data, serviceProfiles)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::PUT_SERVICE_PROFILE_BATCH), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::PutCharacteristicProfile(const CharacteristicProfile& charProfile) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + if (!charProfile.Marshalling(data)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::PUT_CHAR_PROFILE), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::PutCharacteristicProfileBatch( + const std::vector& charProfiles) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + if (!IpcUtils::Marshalling(data, charProfiles)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::PUT_CHAR_PROFILE_BATCH), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + WRITE_HELPER(data, String, deviceId); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::GET_DEVICE_PROFILE_NEW), data, reply); + if (!deviceProfile.UnMarshalling(reply)) { + HILOGE("dp ipc read parcel fail"); + return DP_READ_PARCEL_FAIL; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::GetServiceProfile(const std::string& deviceId, const std::string& serviceName, + ServiceProfile& serviceProfile) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + WRITE_HELPER(data, String, deviceId); + WRITE_HELPER(data, String, serviceName); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::GET_SERVICE_PROFILE), data, reply); + if (!serviceProfile.UnMarshalling(reply)) { + HILOGE("dp ipc read parcel fail"); + return DP_READ_PARCEL_FAIL; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::GetCharacteristicProfile(const std::string& deviceId, + const std::string& serviceName, const std::string& characteristicId, CharacteristicProfile& charProfile) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + WRITE_HELPER(data, String, deviceId); + WRITE_HELPER(data, String, serviceName); + WRITE_HELPER(data, String, characteristicId); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::GET_CHAR_PROFILE), data, reply); + if (!charProfile.UnMarshalling(reply)) { + HILOGE("dp ipc read parcel fail"); + return DP_READ_PARCEL_FAIL; + } + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::DeleteServiceProfile(const std::string& deviceId, + const std::string& serviceName) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + WRITE_HELPER(data, String, deviceId); + WRITE_HELPER(data, String, serviceName); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::DEL_SERVICE_PROFILE), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::DeleteCharacteristicProfile(const std::string& deviceId, + const std::string& serviceName, const std::string& characteristicId) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + WRITE_HELPER(data, String, deviceId); + WRITE_HELPER(data, String, serviceName); + WRITE_HELPER(data, String, characteristicId); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::DEL_CHAR_PROFILE), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + if (!subscribeInfo.Marshalling(data)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + subscribeInfo.Marshalling(data); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::SUBSCRIBE_DEVICE_PROFILE), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + if (!subscribeInfo.Marshalling(data)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::UNSUBSCRIBE_DEVICE_PROFILE), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::SyncDeviceProfile(const SyncOptions& syncOptions, + const sptr syncCompletedCallback) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + if (!syncOptions.Marshalling(data)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + WRITE_HELPER(data, RemoteObject, syncCompletedCallback); + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::SYNC_DEVICE_PROFILE_NEW), data, reply); + return DP_SUCCESS; +} + +int32_t DistributedDeviceProfileProxy::SendSubscribeInfos(std::map listenerMap) +{ + sptr remote = nullptr; + GET_REMOTE_OBJECT(remote); + MessageParcel data; + WRITE_INTERFACE_TOKEN(data); + if (!IpcUtils::Marshalling(data, listenerMap)) { + HILOGE("dp ipc write parcel fail"); + return DP_WRITE_PARCEL_FAIL; + } + MessageParcel reply; + SEND_REQUEST(remote, static_cast(DPInterfaceCode::SEND_SUBSCRIBE_INFOS), data, reply); + return DP_SUCCESS; +} +} // namespace DeviceProfile +} // namespace OHOS \ No newline at end of file