add interfaces

Signed-off-by: tangfan <tangfan5@huawei.com>
This commit is contained in:
tangfan 2023-11-08 22:10:20 +08:00
parent 8dcdf90ad7
commit 09642153c0
7 changed files with 1039 additions and 0 deletions

View File

@ -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"
}

View File

@ -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 <cstdint>
#include <string>
#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<IRemoteObject> &remoteObject) override;
void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
};
}
}
#endif

View File

@ -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 <functional>
#include <iosfwd>
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <stdint.h>
#include <set>
#include <condition_variable>
#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<TrustDeviceProfile>& trustDeviceProfiles);
int32_t GetAccessControlProfile(std::map<std::string, std::string> params,
std::vector<AccessControlProfile>& accessControlProfiles);
int32_t GetAllAccessControlProfile(std::vector<AccessControlProfile>& accessControlProfiles);
int32_t DeleteAccessControlProfile(int32_t accessControlId);
int32_t PutServiceProfile(const ServiceProfile& serviceProfile);
int32_t PutServiceProfileBatch(const std::vector<ServiceProfile>& serviceProfiles);
int32_t PutCharacteristicProfile(const CharacteristicProfile& characteristicProfile);
int32_t PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile>& 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<ISyncCompletedCallback> syncCb);
void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
void LoadSystemAbilityFail();
private:
void SendSubscribeInfosToService();
sptr<IDistributedDeviceProfile> LoadDeviceProfileService();
sptr<IDistributedDeviceProfile> GetDeviceProfileService();
void OnServiceDied(const sptr<IRemoteObject>& remote);
class DeviceProfileDeathRecipient : public IRemoteObject::DeathRecipient {
public:
void OnRemoteDied(const wptr<IRemoteObject>& remote);
};
std::condition_variable proxyConVar_;
std::mutex serviceLock_;
sptr<IDistributedDeviceProfile> dpProxy_ = nullptr;
sptr<IRemoteObject::DeathRecipient> dpDeathRecipient_ = nullptr;
std::map<std::string, SubscribeInfo> subscribeInfos_;
};
} // namespace DeviceProfile
} // namespace OHOS
#endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_CLIENT_H

View File

@ -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 <functional>
#include <iosfwd>
#include <list>
#include <stdint.h>
#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<IDistributedDeviceProfile> {
public:
explicit DistributedDeviceProfileProxy(const sptr<IRemoteObject>& impl)
: IRemoteProxy<IDistributedDeviceProfile>(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<TrustDeviceProfile>& trustDeviceProfiles) override;
int32_t GetAccessControlProfile(std::map<std::string, std::string> queryParams,
std::vector<AccessControlProfile>& accessControlProfiles) override;
int32_t GetAllAccessControlProfile(std::vector<AccessControlProfile>& accessControlProfiles) override;
int32_t DeleteAccessControlProfile(int32_t accessControlId) override;
int32_t PutServiceProfile(const ServiceProfile& serviceProfile) override;
int32_t PutServiceProfileBatch(const std::vector<ServiceProfile>& serviceProfiles) override;
int32_t PutCharacteristicProfile(const CharacteristicProfile& charProfile) override;
int32_t PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile>& 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<IRemoteObject> syncCompletedCallback) override;
int32_t SendSubscribeInfos(std::map<std::string, SubscribeInfo> listenerMap) override;
private:
static inline BrokerDelegator<DistributedDeviceProfileProxy> delegator_;
};
} // namespace DeviceProfile
} // namespace OHOS
#endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_PROXY_H

View File

@ -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<IRemoteObject> &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!");
}
}
}

View File

@ -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 <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <new>
#include <string>
#include <thread>
#include <type_traits>
#include <unistd.h>
#include <utility>
#include <profile_utils.h>
#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<IDistributedDeviceProfile> DistributedDeviceProfileClient::LoadDeviceProfileService()
{
sptr<DeviceProfileLoadCallback> 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<std::mutex> 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<IRemoteObject> &remoteObject)
{
HILOGI("DistributedDeviceProfileClient FinishStartSA");
std::lock_guard<std::mutex> lock(serviceLock_);
if (dpDeathRecipient_ == nullptr) {
dpDeathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new DeviceProfileDeathRecipient);
}
if (remoteObject != nullptr) {
remoteObject->AddDeathRecipient(dpDeathRecipient_);
dpProxy_ = iface_cast<IDistributedDeviceProfile>(remoteObject);
proxyConVar_.notify_one();
}
HILOGI("Send SubscribeInfos cache in proxy to service!");
std::thread(&DistributedDeviceProfileClient::SendSubscribeInfosToService, this).detach();
}
void DistributedDeviceProfileClient::LoadSystemAbilityFail()
{
std::lock_guard<std::mutex> lock(serviceLock_);
dpProxy_ = nullptr;
}
void DistributedDeviceProfileClient::SendSubscribeInfosToService()
{
auto dpService = GetDeviceProfileService();
if (dpService == nullptr) {
HILOGE("dpService is nullptr!");
return;
}
{
std::lock_guard<std::mutex> 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<TrustDeviceProfile>& 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<std::string, std::string> params,
std::vector<AccessControlProfile>& 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<AccessControlProfile>& 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<ServiceProfile>& 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<CharacteristicProfile>& 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<std::mutex> 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<std::mutex> lock(serviceLock_);
subscribeInfos_.erase(subscribeInfo.GetSubscribeKey() + SEPARATOR + std::to_string(subscribeInfo.GetSaId()));
}
return dpService->UnSubscribeDeviceProfile(subscribeInfo);
}
int32_t DistributedDeviceProfileClient::SyncDeviceProfile(const SyncOptions& syncOptions,
sptr<ISyncCompletedCallback> 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<IRemoteObject> syncCompletedCallback = syncCb->AsObject();
if (syncCompletedCallback == nullptr) {
HILOGE("SyncCb ipc cast fail!");
return DP_SYNC_DEVICE_FAIL;
}
return dpService->SyncDeviceProfile(syncOptions, syncCompletedCallback);
}
sptr<IDistributedDeviceProfile> DistributedDeviceProfileClient::GetDeviceProfileService()
{
{
std::lock_guard<std::mutex> 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<IRemoteObject::DeathRecipient>(new DeviceProfileDeathRecipient);
}
object->AddDeathRecipient(dpDeathRecipient_);
dpProxy_ = iface_cast<IDistributedDeviceProfile>(object);
return dpProxy_;
}
}
HILOGW("remoteObject is null!");
if (LoadDeviceProfileService()) {
std::lock_guard<std::mutex> 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<IRemoteObject>& remote)
{
HILOGI("called");
std::lock_guard<std::mutex> lock(serviceLock_);
dpProxy_ = nullptr;
}
void DistributedDeviceProfileClient::DeviceProfileDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
{
DistributedDeviceProfileClient::GetInstance().OnServiceDied(remote.promote());
}
} // namespace DeviceProfile
} // namespace OHOS

View File

@ -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 <memory>
#include "distributed_device_profile_proxy.h"
namespace OHOS {
namespace DistributedDeviceProfile {
namespace {
const std::string TAG = "DistributedDeviceProfileProxy";
}
int32_t DistributedDeviceProfileProxy::PutAccessControlProfile(const AccessControlProfile& aclProfile)
{
sptr<IRemoteObject> 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<uint32_t>(DPInterfaceCode::PUT_ACL_PROFILE), data, reply);
return DP_SUCCESS;
}
int32_t DistributedDeviceProfileProxy::UpdateAccessControlProfile(const AccessControlProfile& aclProfile)
{
sptr<IRemoteObject> 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<uint32_t>(DPInterfaceCode::UPDATE_ACL_PROFILE), data, reply);
return DP_SUCCESS;
}
int32_t DistributedDeviceProfileProxy::GetTrustDeviceProfile(const std::string& deviceId,
TrustDeviceProfile& trustDeviceProfile)
{
sptr<IRemoteObject> remote = nullptr;
GET_REMOTE_OBJECT(remote);
MessageParcel data;
WRITE_INTERFACE_TOKEN(data);
WRITE_HELPER(data, String, deviceId);
MessageParcel reply;
SEND_REQUEST(remote, static_cast<uint32_t>(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<TrustDeviceProfile>& trustDeviceProfiles)
{
sptr<IRemoteObject> remote = nullptr;
GET_REMOTE_OBJECT(remote);
MessageParcel data;
WRITE_INTERFACE_TOKEN(data);
MessageParcel reply;
SEND_REQUEST(remote, static_cast<uint32_t>(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<std::string, std::string> queryParams,
std::vector<AccessControlProfile>& accessControlProfiles)
{
sptr<IRemoteObject> 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<uint32_t>(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<AccessControlProfile>& accessControlProfiles)
{
sptr<IRemoteObject> remote = nullptr;
GET_REMOTE_OBJECT(remote);
MessageParcel data;
WRITE_INTERFACE_TOKEN(data);
MessageParcel reply;
SEND_REQUEST(remote, static_cast<uint32_t>(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<IRemoteObject> remote = nullptr;
GET_REMOTE_OBJECT(remote);
MessageParcel data;
WRITE_INTERFACE_TOKEN(data);
WRITE_HELPER(data, Int32, accessControlId);
MessageParcel reply;
SEND_REQUEST(remote, static_cast<uint32_t>(DPInterfaceCode::DELETE_ACL_PROFILE), data, reply);
return DP_SUCCESS;
}
int32_t DistributedDeviceProfileProxy::PutServiceProfile(const ServiceProfile& serviceProfile)
{
sptr<IRemoteObject> 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<uint32_t>(DPInterfaceCode::PUT_SERVICE_PROFILE), data, reply);
return DP_SUCCESS;
}
int32_t DistributedDeviceProfileProxy::PutServiceProfileBatch(const std::vector<ServiceProfile>& serviceProfiles)
{
sptr<IRemoteObject> 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<uint32_t>(DPInterfaceCode::PUT_SERVICE_PROFILE_BATCH), data, reply);
return DP_SUCCESS;
}
int32_t DistributedDeviceProfileProxy::PutCharacteristicProfile(const CharacteristicProfile& charProfile)
{
sptr<IRemoteObject> 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<uint32_t>(DPInterfaceCode::PUT_CHAR_PROFILE), data, reply);
return DP_SUCCESS;
}
int32_t DistributedDeviceProfileProxy::PutCharacteristicProfileBatch(
const std::vector<CharacteristicProfile>& charProfiles)
{
sptr<IRemoteObject> 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<uint32_t>(DPInterfaceCode::PUT_CHAR_PROFILE_BATCH), data, reply);
return DP_SUCCESS;
}
int32_t DistributedDeviceProfileProxy::GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile)
{
sptr<IRemoteObject> remote = nullptr;
GET_REMOTE_OBJECT(remote);
MessageParcel data;
WRITE_INTERFACE_TOKEN(data);
WRITE_HELPER(data, String, deviceId);
MessageParcel reply;
SEND_REQUEST(remote, static_cast<uint32_t>(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<IRemoteObject> 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<uint32_t>(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<IRemoteObject> 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<uint32_t>(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<IRemoteObject> 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<uint32_t>(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<IRemoteObject> 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<uint32_t>(DPInterfaceCode::DEL_CHAR_PROFILE), data, reply);
return DP_SUCCESS;
}
int32_t DistributedDeviceProfileProxy::SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo)
{
sptr<IRemoteObject> 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<uint32_t>(DPInterfaceCode::SUBSCRIBE_DEVICE_PROFILE), data, reply);
return DP_SUCCESS;
}
int32_t DistributedDeviceProfileProxy::UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo)
{
sptr<IRemoteObject> 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<uint32_t>(DPInterfaceCode::UNSUBSCRIBE_DEVICE_PROFILE), data, reply);
return DP_SUCCESS;
}
int32_t DistributedDeviceProfileProxy::SyncDeviceProfile(const SyncOptions& syncOptions,
const sptr<IRemoteObject> syncCompletedCallback)
{
sptr<IRemoteObject> 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<uint32_t>(DPInterfaceCode::SYNC_DEVICE_PROFILE_NEW), data, reply);
return DP_SUCCESS;
}
int32_t DistributedDeviceProfileProxy::SendSubscribeInfos(std::map<std::string, SubscribeInfo> listenerMap)
{
sptr<IRemoteObject> 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<uint32_t>(DPInterfaceCode::SEND_SUBSCRIBE_INFOS), data, reply);
return DP_SUCCESS;
}
} // namespace DeviceProfile
} // namespace OHOS