• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
60 private:
61     class DeviceProfileDeathRecipient : public IRemoteObject::DeathRecipient {
62     public:
63         void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
64     };
65 
66     struct SubscribeRecord {
67         std::list<SubscribeInfo> subscribeInfos;
68         sptr<IRemoteObject> notifier;
69         ProfileEvents profileEvents;
70     };
71 
72     sptr<IDistributedDeviceProfile> GetDeviceProfileService();
73     bool CheckProfileInvalidity(const ServiceCharacteristicProfile& profile);
74     void OnServiceDied(const sptr<IRemoteObject>& remote);
75     void MergeSubscribeInfoLocked(std::list<SubscribeInfo>& subscribeInfos,
76         const std::list<SubscribeInfo>& newSubscribeInfos);
77 
78     std::mutex serviceLock_;
79     std::mutex subscribeLock_;
80     sptr<IDistributedDeviceProfile> dpProxy_;
81     sptr<IRemoteObject::DeathRecipient> dpDeathRecipient_;
82     std::shared_ptr<AppExecFwk::EventHandler> dpClientHandler_;
83     std::map<std::shared_ptr<IProfileEventCallback>, SubscribeRecord> subscribeRecords_;
84 };
85 } // namespace DeviceProfile
86 } // namespace OHOS
87 #endif // OHOS_DISTRIBUTED_DEVICE_PROFILE_CLIENT_H