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 VIRTUAL_DEVICE_BUILDER_H 17 #define VIRTUAL_DEVICE_BUILDER_H 18 19 #include <memory> 20 #include <vector> 21 22 #include <linux/uinput.h> 23 24 #include <nlohmann/json.hpp> 25 #include "nocopyable.h" 26 27 #include "virtual_device.h" 28 #include "virtual_device_defines.h" 29 30 namespace OHOS { 31 namespace Msdp { 32 namespace DeviceStatus { 33 using json = nlohmann::json; 34 class VirtualDeviceBuilder { 35 public: 36 VirtualDeviceBuilder(const std::string &name, uint16_t bustype, uint16_t vendor, uint16_t product); 37 VirtualDeviceBuilder(const std::string &name, std::shared_ptr<VirtualDevice> vDev); 38 virtual ~VirtualDeviceBuilder(); 39 DISALLOW_COPY_AND_MOVE(VirtualDeviceBuilder); 40 bool SetUp(); 41 void Close(); 42 43 protected: 44 static void Daemonize(); 45 static void Unmount(const char *name, const char *id); 46 static void WaitFor(const char *path, const char *name); 47 static void WaitFor(const char *name, int32_t timeout); 48 static int32_t ReadFile(const char *path, json &model); 49 static int32_t ScanFor(std::function<bool(std::shared_ptr<VirtualDevice>)> pred, 50 std::vector<std::shared_ptr<VirtualDevice>> &vDevs); 51 static std::shared_ptr<VirtualDevice> Select(std::vector<std::shared_ptr<VirtualDevice>> &vDevs, const char *name); 52 void SetResolution(const ResolutionInfo &resolutionInfo); 53 void SetAbsValue(const AbsInfo &absInfo); 54 virtual const std::vector<uint32_t> &GetAbs() const; 55 virtual const std::vector<uint32_t> &GetEventTypes() const; 56 virtual const std::vector<uint32_t> &GetKeys() const; 57 virtual const std::vector<uint32_t> &GetLeds() const; 58 virtual const std::vector<uint32_t> &GetMiscellaneous() const; 59 virtual const std::vector<uint32_t> &GetProperties() const; 60 virtual const std::vector<uint32_t> &GetRelBits() const; 61 virtual const std::vector<uint32_t> &GetRepeats() const; 62 virtual const std::vector<uint32_t> &GetSwitches() const; 63 64 protected: 65 std::vector<uinput_abs_setup> absInit_; 66 std::vector<uint32_t> eventTypes_; 67 std::vector<uint32_t> keys_; 68 std::vector<uint32_t> properties_; 69 std::vector<uint32_t> abs_; 70 std::vector<uint32_t> relBits_; 71 std::vector<uint32_t> leds_; 72 std::vector<uint32_t> miscellaneous_; 73 std::vector<uint32_t> switches_; 74 std::vector<uint32_t> repeats_; 75 76 private: 77 void SetPhys(); 78 void SetSupportedEvents(); 79 void SetAbsResolution(); 80 void SetIdentity(); 81 void CopyProperties(const std::string &name, std::shared_ptr<VirtualDevice> vDev); 82 void CopyIdentity(const std::string &name, std::shared_ptr<VirtualDevice> vDev); 83 void CopyAbsInfo(std::shared_ptr<VirtualDevice> vDev); 84 void CopyEvents(std::shared_ptr<VirtualDevice> vDev); 85 86 private: 87 int32_t fd_ { -1 }; 88 struct uinput_user_dev uinputDev_ {}; 89 struct uinput_abs_setup uinputAbs_ {}; 90 std::shared_ptr<VirtualDevice> vDev_ { nullptr }; 91 }; 92 } // namespace DeviceStatus 93 } // namespace Msdp 94 } // namespace OHOS 95 #endif // VIRTUAL_DEVICE_BUILDER_H