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 class HdcHostUSB : public HdcUSBBase { 21 public: 22 HdcHostUSB(const bool serverOrDaemonIn, void *ptrMainBase, void *ctxUSBin); 23 virtual ~HdcHostUSB(); 24 int Initial(); 25 int SendUSBRaw(HSession hSession, uint8_t *data, const int length); 26 HSession ConnectDetectDaemon(const HSession hSession, const HDaemonInfo pdi); 27 void Stop(); 28 void RemoveIgnoreDevice(string &mountInfo); 29 30 private: 31 enum UsbCheckStatus { 32 HOST_USB_IGNORE = 1, 33 HOST_USB_READY, 34 HOST_USB_REGISTER, 35 }; 36 static int LIBUSB_CALL HotplugHostUSBCallback(libusb_context *ctx, libusb_device *device, 37 libusb_hotplug_event event, void *userData); 38 static void UsbWorkThread(void *arg); // 3rd thread 39 static void WatchUsbNodeChange(uv_timer_t *handle); 40 static void KickoutZombie(HSession hSession); 41 static void LIBUSB_CALL USBBulkCallback(struct libusb_transfer *transfer); 42 int StartupUSBWork(); 43 int CheckActiveConfig(libusb_device *device, HUSB hUSB, libusb_device_descriptor& desc); 44 int OpenDeviceMyNeed(HUSB hUSB); 45 int CheckDescriptor(HUSB hUSB, libusb_device_descriptor& desc); 46 bool IsDebuggableDev(const struct libusb_interface_descriptor *ifDescriptor); 47 bool ReadyForWorkThread(HSession hSession); 48 bool FindDeviceByID(HUSB hUSB, const char *usbMountPoint, libusb_context *ctxUSB); 49 bool DetectMyNeed(libusb_device *device, string &sn); 50 void RestoreHdcProtocol(HUSB hUsb, const uint8_t *buf, int bufSize); 51 void UpdateUSBDaemonInfo(HUSB hUSB, HSession hSession, uint8_t connStatus); 52 void BeginUsbRead(HSession hSession); 53 void ReviewUsbNodeLater(string &nodeKey); 54 void CancelUsbIo(HSession hSession); 55 int UsbToHdcProtocol(uv_stream_t *stream, uint8_t *appendData, int dataSize); 56 int SubmitUsbBio(HSession hSession, bool sendOrRecv, uint8_t *buf, int bufSize); 57 58 libusb_context *ctxUSB; 59 uv_timer_t devListWatcher; 60 map<string, UsbCheckStatus> mapIgnoreDevice; 61 62 private: 63 uv_thread_t threadUsbWork; 64 }; 65 } // namespace Hdc 66 #endif