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_SERVICE_H 17 #define OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_SERVICE_H 18 19 #include <atomic> 20 #include <map> 21 #include <mutex> 22 #include <unordered_set> 23 #include "device_icon_info.h" 24 #include "device_icon_info_filter_options.h" 25 #include "device_profile_filter_options.h" 26 #include "distributed_device_profile_stub_new.h" 27 #include "dp_account_common_event.h" 28 #include "event_handler.h" 29 #include "event_runner.h" 30 #include "i_dp_inited_callback.h" 31 #include "local_service_info.h" 32 #include "product_info.h" 33 #include "local_service_info.h" 34 #include "service_info_profile.h" 35 #include "service_info_unique_key.h" 36 #include "single_instance.h" 37 #include "system_ability.h" 38 #include "system_ability_definition.h" 39 40 namespace OHOS { 41 namespace DistributedDeviceProfile { 42 class DistributedDeviceProfileServiceNew : public SystemAbility, public DistributedDeviceProfileStubNew { 43 DECLARE_SYSTEM_ABILITY(DistributedDeviceProfileServiceNew); 44 DECLARE_SINGLE_INSTANCE_BASE(DistributedDeviceProfileServiceNew); 45 46 public: 47 DistributedDeviceProfileServiceNew(); 48 ~DistributedDeviceProfileServiceNew() = default; 49 50 int32_t Init(); 51 int32_t PostInit(); 52 int32_t PostInitNext(); 53 int32_t UnInit(); 54 int32_t PutAccessControlProfile(const AccessControlProfile& aclProfile) override; 55 int32_t UpdateAccessControlProfile(const AccessControlProfile& aclProfile) override; 56 int32_t GetTrustDeviceProfile(const std::string& deviceId, TrustDeviceProfile& trustDeviceProfile) override; 57 int32_t GetAllTrustDeviceProfile(std::vector<TrustDeviceProfile>& trustDeviceProfiles) override; 58 int32_t GetAccessControlProfile(std::map<std::string, std::string> queryParams, 59 std::vector<AccessControlProfile>& accessControlProfiles) override; 60 int32_t GetAllAccessControlProfile(std::vector<AccessControlProfile>& accessControlProfiles) override; 61 int32_t DeleteAccessControlProfile(int32_t accessControlId) override; 62 int32_t PutSessionKey(uint32_t userId, const std::vector<uint8_t>& sessionKey, int32_t& sessionKeyId) override; 63 int32_t GetSessionKey(uint32_t userId, int32_t sessionKeyId, std::vector<uint8_t>& sessionKey) override; 64 int32_t UpdateSessionKey(uint32_t userId, int32_t sessionKeyId, const std::vector<uint8_t>& sessionKey) override; 65 int32_t DeleteSessionKey(uint32_t userId, int32_t sessionKeyId) override; 66 int32_t PutDeviceProfileBatch(std::vector<DeviceProfile>& deviceProfiles) override; 67 int32_t PutServiceProfile(const ServiceProfile& serviceProfile) override; 68 int32_t PutServiceProfileBatch(const std::vector<ServiceProfile>& serviceProfiles) override; 69 int32_t PutCharacteristicProfile(const CharacteristicProfile& charProfile) override; 70 int32_t PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile>& charProfiles) override; 71 int32_t GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile) override; 72 int32_t GetDeviceProfiles(DeviceProfileFilterOptions& options, std::vector<DeviceProfile>& deviceProfiles) override; 73 int32_t DeleteDeviceProfileBatch(std::vector<DeviceProfile>& deviceProfiles) override; 74 int32_t GetServiceProfile(const std::string& deviceId, const std::string& serviceName, 75 ServiceProfile& serviceProfile) override; 76 int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, 77 const std::string& characteristicKey, CharacteristicProfile& charProfile) override; 78 int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName, bool isMultiUser = false, 79 int32_t userId = DEFAULT_USER_ID) override; 80 int32_t DeleteCharacteristicProfile(const std::string& deviceId, const std::string& serviceName, 81 const std::string& characteristicKey, bool isMultiUser = false, int32_t userId = DEFAULT_USER_ID) override; 82 int32_t PutProductInfoBatch(const std::vector<ProductInfo>& productInfos) override; 83 int32_t PutDeviceIconInfoBatch(const std::vector<DeviceIconInfo>& deviceIconInfos) override; 84 int32_t GetDeviceIconInfos(const DeviceIconInfoFilterOptions& filterOptions, 85 std::vector<DeviceIconInfo>& deviceIconInfos) override; 86 int32_t PutServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile) override; 87 int32_t DeleteServiceInfoProfile(const ServiceInfoUniqueKey& key) override; 88 int32_t UpdateServiceInfoProfile(const ServiceInfoProfile& serviceInfoProfile) override; 89 int32_t GetServiceInfoProfileByUniqueKey(const ServiceInfoUniqueKey& key, 90 ServiceInfoProfile& serviceInfoProfile) override; 91 int32_t GetServiceInfoProfileListByTokenId(const ServiceInfoUniqueKey& key, 92 std::vector<ServiceInfoProfile>& serviceInfoProfiles) override; 93 int32_t GetAllServiceInfoProfileList(std::vector<ServiceInfoProfile>& serviceInfoProfiles) override; 94 int32_t GetServiceInfoProfileListByBundleName(const ServiceInfoUniqueKey& key, 95 std::vector<ServiceInfoProfile>& serviceInfoProfiles) override; 96 int32_t SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo) override; 97 int32_t UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo) override; 98 int32_t SyncDeviceProfile(const DistributedDeviceProfile::DpSyncOptions& syncOptions, 99 sptr<IRemoteObject> syncCompletedCallback) override; 100 int32_t SubscribeDeviceProfileInited(int32_t saId, sptr<IRemoteObject> dpInitedCallback) override; 101 int32_t UnSubscribeDeviceProfileInited(int32_t saId) override; 102 int32_t SubscribePinCodeInvalid(const std::string& bundleName, int32_t pinExchangeType, 103 sptr<IRemoteObject> pinCodeCallback) override; 104 int32_t UnSubscribePinCodeInvalid(const std::string& bundleName, int32_t pinExchangeType) override; 105 int32_t PutAllTrustedDevices(const std::vector<TrustedDeviceInfo> deviceInfos) override; 106 int32_t PutLocalServiceInfo(const LocalServiceInfo& localServiceInfo)override; 107 int32_t UpdateLocalServiceInfo(const LocalServiceInfo& localServiceInfo)override; 108 int32_t GetLocalServiceInfoByBundleAndPinType(const std::string& bundleName, 109 int32_t pinExchangeType, LocalServiceInfo& localServiceInfo)override; 110 int32_t DeleteLocalServiceInfo(const std::string& bundleName, int32_t pinExchangeType)override; 111 int32_t SendSubscribeInfos(std::map<std::string, SubscribeInfo> listenerMap) override; 112 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 113 void DelayUnloadTask() override; 114 bool IsInited() override; 115 void SubscribeAccountCommonEvent(); 116 117 protected: 118 void OnStart(const SystemAbilityOnDemandReason& startReason) override; 119 void OnStop() override; 120 int32_t OnIdle(const SystemAbilityOnDemandReason& idleReason) override; 121 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 122 123 private: 124 void AccountCommonEventCallback(int32_t userId, const std::string commonEventType); 125 int32_t CreateUnloadHandler(); 126 int32_t DestroyUnloadHandler(); 127 int32_t AddSvrProfilesToCache(const std::vector<ServiceProfile>& serviceProfiles); 128 int32_t AddCharProfilesToCache(const std::vector<CharacteristicProfile>& charProfiles); 129 int32_t SaveSwitchProfilesFromTempCache(); 130 int32_t SaveDynamicProfilesFromTempCache(); 131 int32_t NotifyDeviceProfileInited(); 132 int32_t NotifyPinCodeInvalid(const LocalServiceInfo& localServiceInfo); 133 void GetDynamicProfilesFromTempCache(std::map<std::string, std::string>& entries); 134 void ClearProfileCache(); 135 int32_t UnInitNext(); 136 137 private: 138 std::mutex accountCommonEventManagerMtx_; 139 std::shared_ptr<DpAccountCommonEventManager> accountCommonEventManager_; 140 std::shared_ptr<AppExecFwk::EventHandler> unloadHandler_; 141 std::mutex unloadMutex_; 142 std::atomic<bool> isInited_{false}; 143 std::mutex dynamicProfileMapMtx_; 144 std::map<std::string, std::string> dynamicProfileMap_; 145 std::mutex dpInitedCallbackMapMtx_; 146 std::map<int32_t, sptr<IRemoteObject>> dpInitedCallbackMap_; 147 std::mutex pinCodeCallbackMapMtx_; 148 std::map<std::pair<std::string, int32_t>, sptr<IRemoteObject>> pinCodeCallbackMap_; 149 std::mutex switchProfileMapMtx_; 150 std::map<std::string, CharacteristicProfile> switchProfileMap_; 151 std::mutex depSaIdsMtx_; 152 std::unordered_set<int32_t> depSaIds_ { 153 SOFTBUS_SERVER_SA_ID, 154 DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID, 155 DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID, 156 SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN 157 }; 158 }; 159 } // namespace DeviceProfile 160 } // namespace OHOS 161 #endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_SERVICE_H 162