1 /*
2  * Copyright (c) 2021-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_VIRTUAL_DEVICE_H
17 #define OHOS_VIRTUAL_DEVICE_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 #include <dirent.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 
27 #include <linux/input.h>
28 #include "linux/uinput.h"
29 
30 #include "constants_dinput.h"
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 namespace DistributedInput {
35 class VirtualDevice {
36 public:
37     explicit VirtualDevice(const InputDevice &event);
38     virtual ~VirtualDevice();
39     bool DoIoctl(int32_t fd, int32_t request, const uint32_t value);
40     bool CreateKey(const InputDevice &inputDevice);
41     void SetABSInfo(struct uinput_user_dev &inputUserDev, const InputDevice &inputDevice);
42     bool SetPhys(const std::string &deviceName, const std::string &dhId);
43     bool SetUp(const InputDevice &inputDevice, const std::string &devId, const std::string &dhId);
44     bool InjectInputEvent(const input_event &event);
45     void SetNetWorkId(const std::string &netWorkId);
46     void SetPath(const std::string &path);
47     std::string GetNetWorkId();
48     std::string GetPath();
49     uint16_t GetClasses();
50 
51     int32_t GetDeviceFd();
52     uint16_t GetDeviceType();
53 
54 private:
55     int32_t fd_ = -1;
56     std::string deviceName_;
57     std::string netWorkId_;
58     std::string path_;
59     const uint16_t busType_;
60     const uint16_t vendorId_;
61     const uint16_t productId_;
62     const uint16_t version_;
63     const uint16_t classes_;
64     struct uinput_user_dev dev_ {};
65     const std::string pid_ = std::to_string(getpid());
66 
67 private:
68     void RecordEventLog(const input_event &event);
69 };
70 } // namespace DistributedInput
71 } // namespace DistributedHardware
72 } // namespace OHOS
73 
74 #endif // OHOS_VIRTUAL_DEVICE_H
75