1 /*
2  * Copyright (c) 2021-2024 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 "distributed_device_profile_stub_new.h"
24 #include "i_dp_inited_callback.h"
25 #include "event_handler.h"
26 #include "event_runner.h"
27 #include "single_instance.h"
28 #include "system_ability.h"
29 #include "system_ability_definition.h"
30 
31 namespace OHOS {
32 namespace DistributedDeviceProfile {
33 class DistributedDeviceProfileServiceNew : public SystemAbility, public DistributedDeviceProfileStubNew {
34     DECLARE_SYSTEM_ABILITY(DistributedDeviceProfileServiceNew);
35     DECLARE_SINGLE_INSTANCE_BASE(DistributedDeviceProfileServiceNew);
36 
37 public:
38     DistributedDeviceProfileServiceNew();
39     ~DistributedDeviceProfileServiceNew();
40 
41     int32_t Init();
42     int32_t PostInit();
43     int32_t UnInit();
44     int32_t PutAccessControlProfile(const AccessControlProfile& aclProfile) override;
45     int32_t UpdateAccessControlProfile(const AccessControlProfile& aclProfile) override;
46     int32_t GetTrustDeviceProfile(const std::string& deviceId, TrustDeviceProfile& trustDeviceProfile) override;
47     int32_t GetAllTrustDeviceProfile(std::vector<TrustDeviceProfile>& trustDeviceProfiles) override;
48     int32_t GetAccessControlProfile(std::map<std::string, std::string> queryParams,
49         std::vector<AccessControlProfile>& accessControlProfiles) override;
50     int32_t GetAllAccessControlProfile(std::vector<AccessControlProfile>& accessControlProfiles) override;
51     int32_t DeleteAccessControlProfile(int32_t accessControlId) override;
52     int32_t PutServiceProfile(const ServiceProfile& serviceProfile) override;
53     int32_t PutServiceProfileBatch(const std::vector<ServiceProfile>& serviceProfiles) override;
54     int32_t PutCharacteristicProfile(const CharacteristicProfile& charProfile) override;
55     int32_t PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile>& charProfiles) override;
56     int32_t GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile) override;
57     int32_t GetServiceProfile(const std::string& deviceId, const std::string& serviceName,
58         ServiceProfile& serviceProfile) override;
59     int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName,
60         const std::string& characteristicKey, CharacteristicProfile& charProfile) override;
61     int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName) override;
62     int32_t DeleteCharacteristicProfile(const std::string& deviceId, const std::string& serviceName,
63         const std::string& characteristicKey) override;
64     int32_t SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo) override;
65     int32_t UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo) override;
66     int32_t SyncDeviceProfile(const DistributedDeviceProfile::DpSyncOptions& syncOptions,
67         sptr<IRemoteObject> syncCompletedCallback) override;
68     int32_t SubscribeDeviceProfileInited(int32_t saId, sptr<IRemoteObject> dpInitedCallback) override;
69     int32_t UnSubscribeDeviceProfileInited(int32_t saId) override;
70     int32_t SendSubscribeInfos(std::map<std::string, SubscribeInfo> listenerMap) override;
71     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
72     void DelayUnloadTask() override;
73     bool IsInited() override;
74 
75 protected:
76     void OnStart(const SystemAbilityOnDemandReason& startReason) override;
77     void OnStop() override;
78     int32_t OnIdle(const SystemAbilityOnDemandReason& idleReason) override;
79     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
80 
81 private:
82     int32_t CreateUnloadHandler();
83     int32_t DestroyUnloadHandler();
84     int32_t AddSvrProfilesToCache(const std::vector<ServiceProfile>& serviceProfiles);
85     int32_t AddCharProfilesToCache(const std::vector<CharacteristicProfile>& charProfiles);
86     int32_t SaveSwitchProfilesFromTempCache();
87     int32_t SaveDynamicProfilesFromTempCache();
88     int32_t NotifyDeviceProfileInited();
89     void GetDynamicProfilesFromTempCache(std::map<std::string, std::string>& entries);
90     void ClearProfileCache();
91 
92 private:
93     std::shared_ptr<AppExecFwk::EventHandler> unloadHandler_;
94     std::mutex unloadMutex_;
95     std::atomic<bool> isInited_{false};
96     std::mutex dynamicProfileMapMtx_;
97     std::map<std::string, std::string> dynamicProfileMap_;
98     std::mutex dpInitedCallbackMapMtx_;
99     std::map<int32_t, sptr<IRemoteObject>> dpInitedCallbackMap_;
100     std::mutex switchProfileMapMtx_;
101     std::map<std::string, CharacteristicProfile> switchProfileMap_;
102     std::mutex depSaIdsMtx_;
103     std::unordered_set<int32_t> depSaIds_ {
104         SOFTBUS_SERVER_SA_ID,
105         DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID,
106         DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID
107     };
108 };
109 } // namespace DeviceProfile
110 } // namespace OHOS
111 #endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_SERVICE_H
112