• 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 "dm_device_info.h"
24 #endif
25 
26 namespace OHOS::MiscServices {
27 #ifdef PB_DEVICE_MANAGER_ENABLE
28 using namespace OHOS::DistributedHardware;
29 #endif
30 class API_EXPORT DMAdapter {
31 public:
32     static constexpr size_t MAX_ID_LEN = 64;
33     static constexpr size_t RESULT_OK = 0;
34     class DMObserver {
35     public:
36         virtual void Online(const std::string &device) = 0;
37         virtual void Offline(const std::string &device) = 0;
38         virtual void OnReady(const std::string &device) = 0;
39     };
40     static DMAdapter &GetInstance();
41     bool Initialize(const std::string &pkgName);
42     void DeInitialize();
43     const std::string &GetLocalDeviceUdid();
44     const std::string GetLocalNetworkId();
45     std::string GetUdidByNetworkId(const std::string &networkId);
46     std::vector<std::string> GetNetworkIds();
47     int32_t GetLocalDeviceType();
48     bool IsSameAccount(const std::string &networkId);
49     void SetDevices();
50 
51 #ifdef PB_DEVICE_MANAGER_ENABLE
52     int32_t GetRemoteDeviceInfo(const std::string &networkId, DmDeviceInfo &remoteDevice);
53     std::vector<DmDeviceInfo> GetDevices();
54 #endif
55     std::string GetDeviceName(const std::string &networkId);
56     void Register(DMObserver *observer);
57     void Unregister(DMObserver *observer);
58 
59 private:
60     static constexpr const char *NAME_EX = "dm_adapter";
61     static constexpr const char *DEVICE_INVALID_NAME = "unknown";
62     DMAdapter();
63     ~DMAdapter();
64     DMAdapter(const DMAdapter& other) = delete;
65     DMAdapter& operator=(const DMAdapter& other) = delete;
66 
67     std::string pkgName_;
68     const std::string invalidDeviceUdid_{};
69     const std::string invalidNetworkId_{};
70     const std::string invalidUdid_{};
71     mutable std::mutex mutex_{};
72     std::string localDeviceUdid_{};
73     ConcurrentMap<DMObserver *, DMObserver *> observers_;
74 #ifdef PB_DEVICE_MANAGER_ENABLE
75     std::shared_mutex dmMutex_;
76     std::vector<DmDeviceInfo> devices_;
77 #endif
78 };
79 } // namespace OHOS::MiscServices
80 #endif // OHOS_PASTEBOARD_SERVICES_DEVICE_DM_ADAPTER_H
81