1 /* 2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_CLIENT_H 17 #define OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_CLIENT_H 18 19 #include <functional> 20 #include <iosfwd> 21 #include <list> 22 #include <map> 23 #include <memory> 24 #include <mutex> 25 #include <stdint.h> 26 #include <set> 27 #include <condition_variable> 28 #include "i_pincode_invalid_callback.h" 29 #include "i_profile_change_listener.h" 30 #include "i_distributed_device_profile.h" 31 #include "i_dp_inited_callback.h" 32 #include "iremote_object.h" 33 #include "refbase.h" 34 #include "single_instance.h" 35 #include "dp_subscribe_info.h" 36 #include "distributed_device_profile_constants.h" 37 #include "ibusiness_callback.h" 38 #include "sync_completed_callback_stub.h" 39 #include "system_ability_status_change_stub.h" 40 #include "profile_change_listener_stub.h" 41 #include "trusted_device_info.h" 42 #include "local_service_info.h" 43 44 namespace OHOS { 45 namespace DistributedDeviceProfile { 46 class DistributedDeviceProfileClient { 47 DECLARE_SINGLE_INSTANCE(DistributedDeviceProfileClient); 48 49 public: 50 int32_t PutAccessControlProfile(const AccessControlProfile& accessControlProfile); 51 int32_t UpdateAccessControlProfile(const AccessControlProfile& accessControlProfile); 52 int32_t GetTrustDeviceProfile(const std::string& deviceId, TrustDeviceProfile& trustDeviceProfile); 53 int32_t GetAllTrustDeviceProfile(std::vector<TrustDeviceProfile>& trustDeviceProfiles); 54 int32_t GetAccessControlProfile(std::map<std::string, std::string> params, 55 std::vector<AccessControlProfile>& accessControlProfiles); 56 int32_t GetAllAccessControlProfile(std::vector<AccessControlProfile>& accessControlProfiles); 57 int32_t GetAllAclIncludeLnnAcl(std::vector<AccessControlProfile>& accessControlProfiles); 58 int32_t DeleteAccessControlProfile(int32_t accessControlId); 59 int32_t PutSessionKey(uint32_t userId, const std::vector<uint8_t>& sessionKey, int32_t& sessionKeyId); 60 int32_t GetSessionKey(uint32_t userId, int32_t sessionKeyId, std::vector<uint8_t>& sessionKey); 61 int32_t UpdateSessionKey(uint32_t userId, int32_t sessionKeyId, const std::vector<uint8_t>& sessionKey); 62 int32_t DeleteSessionKey(uint32_t userId, int32_t sessionKeyId); 63 int32_t PutDeviceProfileBatch(std::vector<DeviceProfile>& deviceProfiles); 64 int32_t PutServiceProfile(const ServiceProfile& serviceProfile); 65 int32_t PutServiceProfileBatch(const std::vector<ServiceProfile>& serviceProfiles); 66 int32_t PutCharacteristicProfile(const CharacteristicProfile& characteristicProfile); 67 int32_t PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile>& characteristicProfiles); 68 int32_t GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile); 69 int32_t GetDeviceProfiles(DeviceProfileFilterOptions& options, std::vector<DeviceProfile>& deviceProfiles); 70 int32_t DeleteDeviceProfileBatch(std::vector<DeviceProfile>& deviceProfiles); 71 int32_t GetServiceProfile(const std::string& deviceId, const std::string& serviceName, 72 ServiceProfile& serviceProfile); 73 int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, 74 const std::string& characteristicId, CharacteristicProfile& characteristicProfile); 75 int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName, 76 bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID); 77 int32_t DeleteCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, 78 const std::string& characteristicKey, bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID); 79 int32_t PutProductInfoBatch(const std::vector<ProductInfo>& productInfos); 80 int32_t PutDeviceIconInfoBatch(const std::vector<DeviceIconInfo>& deviceIconInfos); 81 int32_t GetDeviceIconInfos(const DeviceIconInfoFilterOptions& filterOptions, 82 std::vector<DeviceIconInfo>& deviceIconInfos); 83 int32_t SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo); 84 int32_t UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo); 85 int32_t SyncDeviceProfile(const DpSyncOptions& syncOptions, sptr<ISyncCompletedCallback> syncCb); 86 int32_t SubscribeDeviceProfileInited(int32_t saId, sptr<IDpInitedCallback> initedCb); 87 int32_t UnSubscribeDeviceProfileInited(int32_t saId); 88 int32_t SubscribePinCodeInvalid(const std::string& bundleName, int32_t pinExchangeType, 89 sptr<IPincodeInvalidCallback> pinCodeCallback); 90 int32_t UnSubscribePinCodeInvalid(const std::string& bundleName, int32_t pinExchangeType); 91 int32_t PutAllTrustedDevices(const std::vector<TrustedDeviceInfo>& deviceInfos); 92 int32_t PutServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile); 93 int32_t DeleteServiceInfoProfile(const ServiceInfoUniqueKey& key); 94 int32_t UpdateServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile); 95 int32_t GetServiceInfoProfileByUniqueKey(const ServiceInfoUniqueKey& key, ServiceInfoProfile& serviceInfoProfile); 96 int32_t GetServiceInfoProfileListByTokenId(const ServiceInfoUniqueKey& key, 97 std::vector<ServiceInfoProfile>& serviceInfoProfiles); 98 int32_t GetAllServiceInfoProfileList(std::vector<ServiceInfoProfile>& serviceInfoProfiles); 99 int32_t GetServiceInfoProfileListByBundleName(const ServiceInfoUniqueKey& key, 100 std::vector<ServiceInfoProfile>& serviceInfoProfiles); 101 int32_t PutLocalServiceInfo(const LocalServiceInfo& localServiceInfo); 102 int32_t UpdateLocalServiceInfo(const LocalServiceInfo& localServiceInfo); 103 int32_t GetLocalServiceInfoByBundleAndPinType(const std::string& bundleName, 104 int32_t pinExchangeType, LocalServiceInfo& localServiceInfo); 105 int32_t DeleteLocalServiceInfo(const std::string& bundleName, int32_t pinExchangeType); 106 int32_t RegisterBusinessCallback(const std::string& saId, const std::string& businessKey, 107 sptr<IBusinessCallback> businessCallback); 108 int32_t UnRegisterBusinessCallback(const std::string& saId, const std::string& businessKey); 109 int32_t PutBusinessEvent(const BusinessEvent& event); 110 int32_t GetBusinessEvent(BusinessEvent& event); 111 112 void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject); 113 void LoadSystemAbilityFail(); 114 void ReleaseResource(); 115 116 public: 117 class SystemAbilityListener : public SystemAbilityStatusChangeStub { 118 public: 119 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 120 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 121 }; 122 123 private: 124 void SendSubscribeInfosToService(); 125 sptr<IDistributedDeviceProfile> LoadDeviceProfileService(); 126 sptr<IDistributedDeviceProfile> GetDeviceProfileService(); 127 void OnServiceDied(const sptr<IRemoteObject>& remote); 128 void SubscribeDeviceProfileSA(); 129 void StartThreadSendSubscribeInfos(); 130 void ReSubscribeDeviceProfileInited(); 131 void ReSubscribePinCodeInvalid(); 132 void StartThreadReSubscribePinCodeInvalid(); 133 void ReleaseSubscribeDeviceProfileSA(); 134 void ReleaseSubscribeDeviceProfileInited(); 135 void ReleaseSubscribePinCodeInvalid(); 136 void ReleaseDeathRecipient(); 137 template <typename Method, typename... Args> 138 int32_t RetryClientRequest(int32_t firesRet, Method method, Args&& ... args); 139 void ReRegisterBusinessCallback(); 140 void ReleaseRegisterBusinessCallback(); 141 void StartThreadReRegisterBusinessCallback(); 142 143 class DeviceProfileDeathRecipient : public IRemoteObject::DeathRecipient { 144 public: 145 void OnRemoteDied(const wptr<IRemoteObject>& remote); 146 }; 147 std::mutex dpInitedLock_; 148 int32_t saId_ = 0; 149 sptr<IDpInitedCallback> dpInitedCallback_ = nullptr; 150 151 std::mutex pinCodeLock_; 152 sptr<IPincodeInvalidCallback> pinCodeCallback_ = nullptr; 153 std::string bundleName_ = ""; 154 int32_t pinExchangeType_ = DEFAULT_PIN_EXCHANGE_TYPE; 155 156 std::mutex serviceLock_; 157 std::condition_variable proxyConVar_; 158 sptr<IDistributedDeviceProfile> dpProxy_ = nullptr; 159 sptr<IRemoteObject::DeathRecipient> dpDeathRecipient_ = nullptr; 160 161 std::mutex subscribeLock_; 162 std::map<std::string, SubscribeInfo> subscribeInfos_; 163 164 std::mutex saListenerLock_; 165 sptr<SystemAbilityListener> saListenerCallback_ = nullptr; 166 167 const std::set<int32_t> retryErrCodes_ = {DP_SERVICE_STOPPED, DP_LOAD_SERVICE_ERR}; 168 std::mutex businessLock_; 169 sptr<IBusinessCallback> businessCallback_ = nullptr; 170 std::string strSaId_ = ""; 171 std::string businessKey_ = ""; 172 }; 173 } // namespace DeviceProfile 174 } // namespace OHOS 175 #endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_CLIENT_H 176