• 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 COOPERATE_DEVICE_MANAGER_H
17 #define COOPERATE_DEVICE_MANAGER_H
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include "nocopyable.h"
23 
24 #include "channel.h"
25 #include "cooperate_events.h"
26 #include "i_context.h"
27 
28 namespace OHOS {
29 namespace Msdp {
30 namespace DeviceStatus {
31 namespace Cooperate {
32 class InputDeviceManager final : public std::enable_shared_from_this<InputDeviceManager> {
33 public:
34     class Device {
35     public:
36         explicit Device(std::shared_ptr<IDevice> dev);
37         int32_t GetId() const;
38         std::string GetName() const;
39         std::string GetDhid() const;
40         std::string GetNetworkId() const;
41         bool IsRemote();
42         int32_t GetProduct() const;
43         int32_t GetVendor() const;
44         std::string GetPhys() const;
45         std::string GetUniq() const;
46         bool IsPointerDevice() const;
47         bool IsKeyboard() const;
48         IDevice::KeyboardType GetKeyboardType() const;
49 
50     private:
51         void Populate();
52         std::string MakeNetworkId(const std::string &phys) const;
53         std::string GenerateDescriptor();
54         std::string Sha256(const std::string &in) const;
55         std::shared_ptr<IDevice> device_ { nullptr };
56         std::string dhid_;
57         std::string networkId_;
58     };
59 
60     InputDeviceManager(IContext *env);
61     ~InputDeviceManager();
62     DISALLOW_COPY_AND_MOVE(InputDeviceManager);
63 
64     void AttachSender(Channel<CooperateEvent>::Sender sender);
65     bool Enable();
66     void Disable();
67 
68     std::shared_ptr<InputDeviceManager::Device> GetDevice(int32_t id) const;
69     bool IsRemote(int32_t id);
70     std::vector<std::string> GetCooperateDhids(int32_t deviceId) const;
71     std::vector<std::string> GetCooperateDhids(const std::string &dhid, bool isRemote) const;
72     std::string GetOriginNetworkId(int32_t id) const;
73     std::string GetOriginNetworkId(const std::string &dhid) const;
74     std::string GetDhid(int32_t deviceId) const;
75     bool HasLocalPointerDevice() const;
76 
77 private:
78     class DeviceObserver final : public IDeviceObserver {
79     public:
80         explicit DeviceObserver(std::shared_ptr<InputDeviceManager> devMgr);
81         ~DeviceObserver() = default;
82 
83         void OnDeviceAdded(std::shared_ptr<IDevice> device) override;
84         void OnDeviceRemoved(std::shared_ptr<IDevice> device) override;
85 
86     private:
87         std::weak_ptr<InputDeviceManager> devMgr_;
88     };
89 
90     void OnDeviceAdded(std::shared_ptr<IDevice> device);
91     void OnDeviceRemoved(std::shared_ptr<IDevice> device);
92 
93     IContext *env_ { nullptr };
94     Channel<CooperateEvent>::Sender sender_;
95     std::shared_ptr<IDeviceObserver> observer_ { nullptr };
96     std::unordered_map<int32_t, std::shared_ptr<Device>> devices_;
97 };
98 } // namespace Cooperate
99 } // namespace DeviceStatus
100 } // namespace Msdp
101 } // namespace OHOS
102 #endif // COOPERATE_DEVICE_MANAGER_H