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 DEVICE_H
17 #define DEVICE_H
18
19 #include <bitset>
20 #include <string>
21 #include <vector>
22
23 #include <linux/input.h>
24
25 #include "nocopyable.h"
26
27 #include "i_device.h"
28 #include "i_epoll_event_source.h"
29
30 namespace OHOS {
31 namespace Msdp {
32 namespace DeviceStatus {
33 inline constexpr size_t BITS_PER_UINT8 { 8 };
34
OFFSET(size_t bit)35 inline constexpr size_t OFFSET(size_t bit)
36 {
37 return (bit % BITS_PER_UINT8);
38 }
39
BYTE(size_t bit)40 inline constexpr size_t BYTE(size_t bit)
41 {
42 return (bit / BITS_PER_UINT8);
43 }
44
TestBit(size_t bit,const uint8_t * array)45 inline bool TestBit(size_t bit, const uint8_t *array)
46 {
47 return ((array)[BYTE(bit)] & (1 << OFFSET(bit)));
48 }
49
NBYTES(size_t nbits)50 inline constexpr size_t NBYTES(size_t nbits)
51 {
52 return (nbits + BITS_PER_UINT8 - 1) / BITS_PER_UINT8;
53 }
54
55 class Device final : public IDevice,
56 public IEpollEventSource {
57 public:
58 enum Capability {
59 DEVICE_CAP_KEYBOARD = 0,
60 DEVICE_CAP_TOUCH,
61 DEVICE_CAP_POINTER,
62 DEVICE_CAP_TABLET_TOOL,
63 DEVICE_CAP_TABLET_PAD,
64 DEVICE_CAP_GESTURE,
65 DEVICE_CAP_SWITCH,
66 DEVICE_CAP_JOYSTICK,
67 DEVICE_CAP_MAX
68 };
69
70 explicit Device(int32_t deviceId);
71 DISALLOW_COPY_AND_MOVE(Device);
72 ~Device();
73
74 int32_t Open() override;
75 void Close() override;
76 int32_t GetFd() const override;
77 void Dispatch(const struct epoll_event &ev) override;
78 void SetDevPath(const std::string &devPath) override;
79 void SetSysPath(const std::string &sysPath) override;
80 int32_t GetId() const override;
81 std::string GetDevPath() const override;
82 std::string GetSysPath() const override;
83 std::string GetName() const override;
84 int32_t GetBus() const override;
85 int32_t GetVersion() const override;
86 int32_t GetProduct() const override;
87 int32_t GetVendor() const override;
88 std::string GetPhys() const override;
89 std::string GetUniq() const override;
90 IDevice::KeyboardType GetKeyboardType() const override;
91 bool IsPointerDevice() const override;
92 bool IsKeyboard() const override;
93
94 private:
95 void QueryDeviceInfo();
96 void QuerySupportedEvents();
97 void UpdateCapability();
98 bool HasMouseButton() const;
99 bool HasJoystickAxesOrButtons() const;
100 void CheckPointers();
101 void CheckPencilMouse();
102 void CheckKeys();
103 std::string MakeConfigFileName() const;
104 int32_t ReadConfigFile(const std::string &filePath);
105 int32_t ConfigItemSwitch(const std::string &configItem, const std::string &value);
106 int32_t ReadTomlFile(const std::string &filePath);
107 void JudgeKeyboardType();
108 void LoadDeviceConfig();
109 void PrintCapsDevice() const;
110
111 int32_t fd_ { -1 };
112 int32_t deviceId_ { -1 };
113 int32_t bus_ { 0 };
114 int32_t version_ { 0 };
115 int32_t product_ { 0 };
116 int32_t vendor_ { 0 };
117 std::string devPath_;
118 std::string sysPath_;
119 std::string name_;
120 std::string phys_;
121 std::string uniq_;
122 std::string dhid_;
123 std::string networkId_;
124 std::bitset<DEVICE_CAP_MAX> caps_;
125 uint8_t evBitmask_[NBYTES(EV_MAX)] {};
126 uint8_t keyBitmask_[NBYTES(KEY_MAX)] {};
127 uint8_t absBitmask_[NBYTES(ABS_MAX)] {};
128 uint8_t relBitmask_[NBYTES(REL_MAX)] {};
129 uint8_t propBitmask_[NBYTES(INPUT_PROP_MAX)] {};
130 IDevice::KeyboardType keyboardType_ { IDevice::KEYBOARD_TYPE_NONE };
131 };
132
GetFd()133 inline int32_t Device::GetFd() const
134 {
135 return fd_;
136 }
137
SetDevPath(const std::string & devPath)138 inline void Device::SetDevPath(const std::string &devPath)
139 {
140 devPath_ = devPath;
141 }
142
SetSysPath(const std::string & sysPath)143 inline void Device::SetSysPath(const std::string &sysPath)
144 {
145 sysPath_ = sysPath;
146 }
147
GetId()148 inline int32_t Device::GetId() const
149 {
150 return deviceId_;
151 }
152
GetDevPath()153 inline std::string Device::GetDevPath() const
154 {
155 return devPath_;
156 }
157
GetSysPath()158 inline std::string Device::GetSysPath() const
159 {
160 return sysPath_;
161 }
162
GetName()163 inline std::string Device::GetName() const
164 {
165 return name_;
166 }
167
GetBus()168 inline int32_t Device::GetBus() const
169 {
170 return bus_;
171 }
172
GetVersion()173 inline int32_t Device::GetVersion() const
174 {
175 return version_;
176 }
177
GetProduct()178 inline int32_t Device::GetProduct() const
179 {
180 return product_;
181 }
182
GetVendor()183 inline int32_t Device::GetVendor() const
184 {
185 return vendor_;
186 }
187
GetPhys()188 inline std::string Device::GetPhys() const
189 {
190 return phys_;
191 }
192
GetUniq()193 inline std::string Device::GetUniq() const
194 {
195 return uniq_;
196 }
197
GetKeyboardType()198 inline IDevice::KeyboardType Device::GetKeyboardType() const
199 {
200 return keyboardType_;
201 }
202
IsPointerDevice()203 inline bool Device::IsPointerDevice() const
204 {
205 return caps_.test(DEVICE_CAP_POINTER);
206 }
207
IsKeyboard()208 inline bool Device::IsKeyboard() const
209 {
210 return caps_.test(DEVICE_CAP_KEYBOARD);
211 }
212 } // namespace DeviceStatus
213 } // namespace Msdp
214 } // namespace OHOS
215 #endif // DEVICE_H