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