• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_PASTEBOARD_SERVICES_DEVICE_DM_ADAPTER_H
17 #define OHOS_PASTEBOARD_SERVICES_DEVICE_DM_ADAPTER_H
18 #include <shared_mutex>
19 
20 #include "api/visibility.h"
21 #include "common/concurrent_map.h"
22 #ifdef PB_DEVICE_MANAGER_ENABLE
23 #include "device_manager.h"
24 #include "device_manager_callback.h"
25 #include "dm_device_info.h"
26 #endif
27 
28 namespace OHOS::MiscServices {
29 #ifdef PB_DEVICE_MANAGER_ENABLE
30 using namespace OHOS::DistributedHardware;
31 #endif
32 
33 #ifdef PB_DEVICE_MANAGER_ENABLE
34 class DmStateObserver : public DeviceStateCallback {
35 public:
36     DmStateObserver(const std::function<void(const DmDeviceInfo &)> online,
37         const std::function<void(const DmDeviceInfo &)> onReady,
38         const std::function<void(const DmDeviceInfo &)> offline);
39     void OnDeviceOnline(const DmDeviceInfo &deviceInfo) override;
40     void OnDeviceOffline(const DmDeviceInfo &deviceInfo) override;
41     void OnDeviceChanged(const DmDeviceInfo &deviceInfo) override;
42     void OnDeviceReady(const DmDeviceInfo &deviceInfo) override;
43 
44 private:
45     std::function<void(const DmDeviceInfo &)> online_;
46     std::function<void(const DmDeviceInfo &)> onReady_;
47     std::function<void(const DmDeviceInfo &)> offline_;
48 };
49 
50 class DmDeath : public DmInitCallback, public std::enable_shared_from_this<DmDeath> {
51 public:
52     DmDeath(std::shared_ptr<DmStateObserver> observer, std::string pkgName);
53     void OnRemoteDied() override;
54 
55 private:
56     std::shared_ptr<DmStateObserver> observer_;
57     std::string pkgName_;
58 };
59 #endif
60 
61 class API_EXPORT DMAdapter {
62 public:
63     static constexpr size_t MAX_ID_LEN = 64;
64     static constexpr size_t RESULT_OK = 0;
65     class DMObserver {
66     public:
67         virtual void Online(const std::string &device) = 0;
68         virtual void Offline(const std::string &device) = 0;
69         virtual void OnReady(const std::string &device) = 0;
70     };
71     static DMAdapter &GetInstance();
72     bool Initialize(const std::string &pkgName);
73     void DeInitialize();
74     const std::string &GetLocalDeviceUdid();
75     const std::string GetLocalNetworkId();
76     std::string GetUdidByNetworkId(const std::string &networkId);
77     std::vector<std::string> GetNetworkIds();
78     int32_t GetLocalDeviceType();
79     bool IsSameAccount(const std::string &networkId);
80     void SetDevices();
81 
82 #ifdef PB_DEVICE_MANAGER_ENABLE
83     int32_t GetRemoteDeviceInfo(const std::string &networkId, DmDeviceInfo &remoteDevice);
84     std::vector<DmDeviceInfo> GetDevices();
85 #endif
86     std::string GetDeviceName(const std::string &networkId);
87     void Register(DMObserver *observer);
88     void Unregister(DMObserver *observer);
89 
90 private:
91     static constexpr const char *NAME_EX = "dm_adapter";
92     static constexpr const char *DEVICE_INVALID_NAME = "unknown";
93     DMAdapter();
94     ~DMAdapter();
95     DMAdapter(const DMAdapter& other) = delete;
96     DMAdapter& operator=(const DMAdapter& other) = delete;
97 
98     std::string pkgName_;
99     const std::string invalidDeviceUdid_{};
100     const std::string invalidNetworkId_{};
101     const std::string invalidUdid_{};
102     mutable std::mutex mutex_{};
103     std::string localDeviceUdid_{};
104     ConcurrentMap<DMObserver *, DMObserver *> observers_;
105 #ifdef PB_DEVICE_MANAGER_ENABLE
106     std::shared_mutex dmMutex_;
107     std::vector<DmDeviceInfo> devices_;
108     std::atomic<int32_t> deviceType_ = DmDeviceType::DEVICE_TYPE_UNKNOWN;
109 #endif
110 };
111 } // namespace OHOS::MiscServices
112 #endif // OHOS_PASTEBOARD_SERVICES_DEVICE_DM_ADAPTER_H
113