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_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_profile_change_listener.h"
29 #include "i_distributed_device_profile.h"
30 #include "i_dp_inited_callback.h"
31 #include "iremote_object.h"
32 #include "refbase.h"
33 #include "single_instance.h"
34 #include "dp_subscribe_info.h"
35 #include "distributed_device_profile_constants.h"
36 #include "sync_completed_callback_stub.h"
37 #include "system_ability_status_change_stub.h"
38 #include "profile_change_listener_stub.h"
39 
40 namespace OHOS {
41 namespace DistributedDeviceProfile {
42 class DistributedDeviceProfileClient {
43 DECLARE_SINGLE_INSTANCE(DistributedDeviceProfileClient);
44 
45 public:
46     int32_t PutAccessControlProfile(const AccessControlProfile& accessControlProfile);
47     int32_t UpdateAccessControlProfile(const AccessControlProfile& accessControlProfile);
48     int32_t GetTrustDeviceProfile(const std::string& deviceId, TrustDeviceProfile& trustDeviceProfile);
49     int32_t GetAllTrustDeviceProfile(std::vector<TrustDeviceProfile>& trustDeviceProfiles);
50     int32_t GetAccessControlProfile(std::map<std::string, std::string> params,
51         std::vector<AccessControlProfile>& accessControlProfiles);
52     int32_t GetAllAccessControlProfile(std::vector<AccessControlProfile>& accessControlProfiles);
53     int32_t DeleteAccessControlProfile(int32_t accessControlId);
54     int32_t PutServiceProfile(const ServiceProfile& serviceProfile);
55     int32_t PutServiceProfileBatch(const std::vector<ServiceProfile>& serviceProfiles);
56     int32_t PutCharacteristicProfile(const CharacteristicProfile& characteristicProfile);
57     int32_t PutCharacteristicProfileBatch(const std::vector<CharacteristicProfile>& characteristicProfiles);
58     int32_t GetDeviceProfile(const std::string& deviceId, DeviceProfile& deviceProfile);
59     int32_t GetServiceProfile(const std::string& deviceId, const std::string& serviceName,
60         ServiceProfile& serviceProfile);
61     int32_t GetCharacteristicProfile(const std::string& deviceId, const std::string& serviceName,
62         const std::string& characteristicId, CharacteristicProfile& characteristicProfile);
63     int32_t DeleteServiceProfile(const std::string& deviceId, const std::string& serviceName);
64     int32_t DeleteCharacteristicProfile(const std::string& deviceId, const std::string& serviceName,
65         const std::string& characteristicKey);
66     int32_t SubscribeDeviceProfile(const SubscribeInfo& subscribeInfo);
67     int32_t UnSubscribeDeviceProfile(const SubscribeInfo& subscribeInfo);
68     int32_t SyncDeviceProfile(const DpSyncOptions& syncOptions, sptr<ISyncCompletedCallback> syncCb);
69     int32_t SubscribeDeviceProfileInited(int32_t saId, sptr<IDpInitedCallback> initedCb);
70     int32_t UnSubscribeDeviceProfileInited(int32_t saId);
71 
72     void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
73     void LoadSystemAbilityFail();
74 
75 public:
76     class SystemAbilityListener : public SystemAbilityStatusChangeStub {
77     public:
78         void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
79         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
80     };
81 
82 private:
83     void SendSubscribeInfosToService();
84     sptr<IDistributedDeviceProfile> LoadDeviceProfileService();
85     sptr<IDistributedDeviceProfile> GetDeviceProfileService();
86     void OnServiceDied(const sptr<IRemoteObject>& remote);
87     void SubscribeDeviceProfileSA();
88     void StartThreadSendSubscribeInfos();
89     void ReSubscribeDeviceProfileInited();
90 
91     class DeviceProfileDeathRecipient : public IRemoteObject::DeathRecipient {
92     public:
93         void OnRemoteDied(const wptr<IRemoteObject>& remote);
94     };
95     int32_t saId_ = 0;
96     sptr<IDpInitedCallback> dpInitedCallback_ = nullptr;
97     std::condition_variable proxyConVar_;
98     std::mutex serviceLock_;
99     sptr<IDistributedDeviceProfile> dpProxy_ = nullptr;
100     sptr<IRemoteObject::DeathRecipient> dpDeathRecipient_ = nullptr;
101     std::map<std::string, SubscribeInfo> subscribeInfos_;
102     sptr<SystemAbilityListener> saListenerCallback_ = nullptr;
103 };
104 } // namespace DeviceProfile
105 } // namespace OHOS
106 #endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_CLIENT_H
107