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