1 /* 2 * Copyright (C) 2021 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 #ifndef HDC_HOST_USB_H 16 #define HDC_HOST_USB_H 17 #include "host_common.h" 18 19 namespace Hdc { 20 struct ClearUsbChannelWorkInfo { 21 int result = 0; 22 HSession hSession = nullptr; 23 HDaemonInfo pDaemonInfo = nullptr; 24 }; 25 using HClearUsbChannelWorkInfo = struct ClearUsbChannelWorkInfo *; 26 27 class HdcHostUSB : public HdcUSBBase { 28 public: 29 HdcHostUSB(const bool serverOrDaemonIn, void *ptrMainBase, void *ctxUSBin); 30 virtual ~HdcHostUSB(); 31 int Initial(); 32 static void InitLogging(void *ctxUSB); 33 int SendUSBRaw(HSession hSession, uint8_t *data, const int length) override; 34 HSession ConnectDetectDaemon(const HSession hSession, const HDaemonInfo pdi); 35 void Stop(); 36 void RemoveIgnoreDevice(string &mountInfo); 37 static libusb_log_level GetLibusbLogLevel(void); 38 static void SetLibusbLogLevelEnv(libusb_log_level logLevel); 39 void SendSoftResetToDaemon(HSession hSession, uint32_t sessionIdOld); 40 41 private: 42 enum UsbCheckStatus { 43 HOST_USB_IGNORE = 1, 44 HOST_USB_READY, 45 HOST_USB_REGISTER, 46 }; 47 static int LIBUSB_CALL HotplugHostUSBCallback(libusb_context *ctx, libusb_device *device, 48 libusb_hotplug_event event, void *userData); 49 static void UsbWorkThread(void *arg); // 3rd thread 50 static void WatchUsbNodeChange(uv_timer_t *handle); 51 static void KickoutZombie(HSession hSession); 52 static void LIBUSB_CALL USBBulkCallback(struct libusb_transfer *transfer); 53 static void ClearUsbChannel(uv_work_t *req); 54 static void ClearUsbChannelFinished(uv_work_t *req, int status); 55 static void SendSoftResetToDaemonSync(HSession hSession, uint32_t sessionIdOld); 56 int StartupUSBWork(); 57 void CheckUsbEndpoint(int& ret, HUSB hUSB, libusb_config_descriptor *descConfig); 58 int CheckActiveConfig(libusb_device *device, HUSB hUSB, libusb_device_descriptor& desc); 59 int OpenDeviceMyNeed(HUSB hUSB); 60 bool HasValidDevice(libusb_device *device); 61 int CheckDescriptor(HUSB hUSB, libusb_device_descriptor& desc); 62 bool IsDebuggableDev(const struct libusb_interface_descriptor *ifDescriptor); 63 bool ReadyForWorkThread(HSession hSession) override; 64 bool FindDeviceByID(HUSB hUSB, const char *usbMountPoint, libusb_context *ctxUSB); 65 bool DetectMyNeed(libusb_device *device, string &sn); 66 void RestoreHdcProtocol(HUSB hUsb, const uint8_t *buf, int bufSize); 67 void UpdateUSBDaemonInfo(HUSB hUSB, HSession hSession, uint8_t connStatus); 68 void BeginUsbRead(HSession hSession); 69 void ReviewUsbNodeLater(string &nodeKey); 70 void CancelUsbIo(HSession hSession) override; 71 int UsbToHdcProtocol(uv_stream_t *stream, uint8_t *appendData, int dataSize) override; 72 int SubmitUsbBio(HSession hSession, bool sendOrRecv, uint8_t *buf, int bufSize); 73 74 libusb_context *ctxUSB; 75 uv_timer_t devListWatcher; 76 map<string, UsbCheckStatus> mapIgnoreDevice; 77 int logRePrintTimer; 78 int logRePrintCount; 79 80 private: 81 uv_thread_t threadUsbWork; 82 }; 83 } // namespace Hdc 84 #endif 85