• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_DISTRIBUTED_DEVICE_PROFILE_CLIENT_H
17 #define OHOS_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 
27 #include "event_handler.h"
28 #include "idistributed_device_profile.h"
29 #include "iprofile_event_callback.h"
30 #include "iremote_object.h"
31 #include "profile_event.h"
32 #include "profile_event_notifier_stub.h"
33 #include "refbase.h"
34 #include "single_instance.h"
35 #include "subscribe_info.h"
36 
37 namespace OHOS {
38 namespace DeviceProfile {
39 class DistributedDeviceProfileClient {
40     DECLARE_SINGLE_INSTANCE(DistributedDeviceProfileClient);
41 
42 public:
43     int32_t PutDeviceProfile(const ServiceCharacteristicProfile& profile);
44     int32_t GetDeviceProfile(const std::string& udid, const std::string& serviceId,
45         ServiceCharacteristicProfile& profile);
46     int32_t DeleteDeviceProfile(const std::string& serviceId);
47     int32_t SubscribeProfileEvent(const SubscribeInfo& subscribeInfo,
48         const std::shared_ptr<IProfileEventCallback>& eventCb);
49     int32_t UnsubscribeProfileEvent(ProfileEvent profileEvent,
50         const std::shared_ptr<IProfileEventCallback>& eventCb);
51     int32_t SubscribeProfileEvents(const std::list<SubscribeInfo>& subscribeInfos,
52         const std::shared_ptr<IProfileEventCallback>& eventCb,
53         std::list<ProfileEvent>& failedEvents);
54     int32_t UnsubscribeProfileEvents(const std::list<ProfileEvent>& profileEvents,
55         const std::shared_ptr<IProfileEventCallback>& eventCb,
56         std::list<ProfileEvent>& failedEvents);
57     int32_t SyncDeviceProfile(const SyncOptions& syncOptions,
58         const std::shared_ptr<IProfileEventCallback>& syncCb);
59     void LoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
60     void LoadSystemAbilityFail();
61 
62 private:
63     class DeviceProfileDeathRecipient : public IRemoteObject::DeathRecipient {
64     public:
65         void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
66     };
67 
68     struct SubscribeRecord {
69         std::list<SubscribeInfo> subscribeInfos;
70         sptr<IRemoteObject> notifier;
71         ProfileEvents profileEvents;
72     };
73 
74     sptr<IDistributedDeviceProfile> GetDeviceProfileService();
75     bool LoadDeviceProfileService();
76     bool CheckProfileInvalidity(const ServiceCharacteristicProfile& profile);
77     void OnServiceDied(const sptr<IRemoteObject>& remote);
78     void MergeSubscribeInfoLocked(std::list<SubscribeInfo>& subscribeInfos,
79         const std::list<SubscribeInfo>& newSubscribeInfos);
80 
81     std::condition_variable proxyConVar_;
82     std::mutex serviceLock_;
83     std::mutex subscribeLock_;
84     sptr<IDistributedDeviceProfile> dpProxy_;
85     sptr<IRemoteObject::DeathRecipient> dpDeathRecipient_;
86     std::shared_ptr<AppExecFwk::EventHandler> dpClientHandler_;
87     std::map<std::shared_ptr<IProfileEventCallback>, SubscribeRecord> subscribeRecords_;
88 };
89 } // namespace DeviceProfile
90 } // namespace OHOS
91 #endif // OHOS_DISTRIBUTED_DEVICE_PROFILE_CLIENT_H