1 /* 2 * Copyright (C) 2022 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 HID_HOST_UHID_H 17 #define HID_HOST_UHID_H 18 19 #include <fcntl.h> 20 #include <pthread.h> 21 #include <poll.h> 22 #include <unistd.h> 23 #include <string> 24 #include <linux/uhid.h> 25 #include "hid_host_defines.h" 26 #include "base_def.h" 27 #include "raw_address.h" 28 29 namespace OHOS { 30 namespace bluetooth { 31 static constexpr int MAX_POLLING_ATTEMPTS = 10; 32 static constexpr int POLLING_SLEEP_DURATION_US = 5000; 33 static constexpr int POLL_TIMEOUT = 50; 34 35 /** 36 * @brief Class for l2cap connection. 37 */ 38 class HidHostUhid { 39 public: 40 explicit HidHostUhid(std::string address); 41 ~HidHostUhid(); 42 43 int Destroy(); 44 int SendHidInfo(const char* devName, PnpInformation& pnpInf, HidInformation& hidInf); 45 int SendData(uint8_t* pRpt, uint16_t len); 46 int SendControlData(uint8_t* pRpt, uint16_t len); 47 int SendHandshake(uint16_t err); 48 int Close(); 49 int Open(); 50 51 static void* PollEventThread(void* arg); 52 void PollEventThread_(); 53 54 private: 55 int WriteUhid(int fd, const struct uhid_event* ev); 56 int WritePackUhid(int fd, uint8_t* rpt, uint16_t len); 57 int ClosePollThread(); 58 pthread_t CreateThread(void* (*startRoutine)(void*), void* arg); 59 void SetUhidNonBlocking(int fd); 60 int ReadUhidEvent(); 61 int SendGetReportReplyUhid(int fd, int id, uint16_t err, uint8_t* rpt, uint16_t len); 62 int SendSetReportReplyUhid(int fd, int id, uint16_t err); 63 void ReadUhidOutPut(uhid_event ev); 64 void ReadUhidFeature(uhid_event ev); 65 void ReadUhidSetReport(uhid_event ev); 66 67 pthread_t pollThreadId_ = -1; 68 int fd_ = -1; 69 bool keepPolling_ = false; 70 bool readyForData_ = false; 71 std::string address_; 72 int task_id_ = 0; 73 int task_type_ = -1; 74 75 BT_DISALLOW_COPY_AND_ASSIGN(HidHostUhid); 76 }; 77 } // namespace bluetooth 78 } // namespace OHOS 79 #endif // HID_HOST_UHID_H