/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef VIRTUAL_DEVICE_BUILDER_H #define VIRTUAL_DEVICE_BUILDER_H #include #include #include #include #include "nocopyable.h" #include "virtual_device.h" #include "virtual_device_defines.h" namespace OHOS { namespace Msdp { namespace DeviceStatus { using json = nlohmann::json; class VirtualDeviceBuilder { public: VirtualDeviceBuilder(const std::string &name, uint16_t bustype, uint16_t vendor, uint16_t product); VirtualDeviceBuilder(const std::string &name, std::shared_ptr vDev); virtual ~VirtualDeviceBuilder(); DISALLOW_COPY_AND_MOVE(VirtualDeviceBuilder); bool SetUp(); void Close(); protected: static void Daemonize(); static void Unmount(const char *name, const char *id); static void WaitFor(const char *path, const char *name); static void WaitFor(const char *name, int32_t timeout); static int32_t ReadFile(const char *path, json &model); static int32_t ScanFor(std::function)> pred, std::vector> &vDevs); static std::shared_ptr Select(std::vector> &vDevs, const char *name); void SetResolution(const ResolutionInfo &resolutionInfo); void SetAbsValue(const AbsInfo &absInfo); virtual const std::vector &GetAbs() const; virtual const std::vector &GetEventTypes() const; virtual const std::vector &GetKeys() const; virtual const std::vector &GetLeds() const; virtual const std::vector &GetMiscellaneous() const; virtual const std::vector &GetProperties() const; virtual const std::vector &GetRelBits() const; virtual const std::vector &GetRepeats() const; virtual const std::vector &GetSwitches() const; protected: std::vector absInit_; std::vector abs_; std::vector relBits_; std::vector switches_; std::vector repeats_; std::vector eventTypes_; std::vector keys_; std::vector properties_; std::vector leds_; std::vector miscellaneous_; private: void SetPhys(); void SetSupportedEvents(); void SetAbsResolution(); void SetIdentity(); void CopyProperties(const std::string &name, std::shared_ptr vDev); void CopyIdentity(const std::string &name, std::shared_ptr vDev); void CopyAbsInfo(std::shared_ptr vDev); void CopyEvents(std::shared_ptr vDev); static void ConcatenationName(std::string &sLine); static bool ExecuteUnmount(const char *id, const char *name, const std::string &direntName); private: int32_t fd_ { -1 }; struct uinput_user_dev uinputDev_ {}; struct uinput_abs_setup uinputAbs_ {}; std::shared_ptr vDev_ { nullptr }; }; } // namespace DeviceStatus } // namespace Msdp } // namespace OHOS #endif // VIRTUAL_DEVICE_BUILDER_H