• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 COORDINATION_DEVICE_MANAGER_H
17 #define COORDINATION_DEVICE_MANAGER_H
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include "singleton.h"
23 
24 #include "i_device_manager.h"
25 
26 namespace OHOS {
27 namespace Msdp {
28 namespace DeviceStatus {
29 class CoordinationDeviceManager {
30     DECLARE_DELAYED_SINGLETON(CoordinationDeviceManager);
31 public:
32     class Device {
33     public:
34         explicit Device(std::shared_ptr<IDevice> dev);
35         int32_t GetId() const;
36         std::string GetName() const;
37         std::string GetDhid() const;
38         std::string GetNetworkId() const;
39         bool IsRemote();
40         int32_t GetProduct() const;
41         int32_t GetVendor() const;
42         std::string GetPhys() const;
43         std::string GetUniq() const;
44         bool IsPointerDevice() const;
45         bool IsKeyboard() const;
46         IDevice::KeyboardType GetKeyboardType() const;
47 
48     private:
49         void Populate();
50         std::string MakeNetworkId(const std::string &phys) const;
51         std::string GenerateDescriptor();
52         std::string Sha256(const std::string &in) const;
53         std::shared_ptr<IDevice> device_ { nullptr };
54         std::string dhid_;
55         std::string networkId_;
56     };
57 
58 private:
59     class DeviceObserver final : public IDeviceObserver {
60     public:
61         explicit DeviceObserver(CoordinationDeviceManager &cooDevMgr);
62         ~DeviceObserver() = default;
63         void OnDeviceAdded(std::shared_ptr<IDevice> device) override;
64         void OnDeviceRemoved(std::shared_ptr<IDevice> device) override;
65 
66     private:
67         CoordinationDeviceManager &cooDevMgr_;
68     };
69 
70 public:
71     void Init();
72     std::shared_ptr<CoordinationDeviceManager::Device> GetDevice(int32_t id) const;
73     bool IsRemote(int32_t id);
74     std::vector<std::string> GetCoordinationDhids(int32_t deviceId) const;
75     std::vector<std::string> GetCoordinationDhids(const std::string &dhid) const;
76     std::string GetOriginNetworkId(int32_t id) const;
77     std::string GetOriginNetworkId(const std::string &dhid) const;
78     std::string GetDhid(int32_t deviceId) const;
79     bool HasLocalPointerDevice() const;
80 
81 private:
82     void OnDeviceAdded(std::shared_ptr<IDevice> device);
83     void OnDeviceRemoved(std::shared_ptr<IDevice> device);
84 
85     std::shared_ptr<DeviceObserver> devObserver_ { nullptr };
86     std::unordered_map<int32_t, std::shared_ptr<Device>> devices_;
87 };
88 #define COOR_DEV_MGR OHOS::DelayedSingleton<CoordinationDeviceManager>::GetInstance()
89 } // namespace DeviceStatus
90 } // namespace Msdp
91 } // namespace OHOS
92 #endif // COORDINATION_DEVICE_MANAGER_H