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