xref: /developtools/hdc/src/host/host_usb.h (revision cc290419)
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
19namespace Hdc {
20class HdcHostUSB : public HdcUSBBase {
21public:
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
31private:
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    void CheckUsbEndpoint(int& ret, HUSB hUSB, libusb_config_descriptor *descConfig);
45    int CheckActiveConfig(libusb_device *device, HUSB hUSB, libusb_device_descriptor& desc);
46    int OpenDeviceMyNeed(HUSB hUSB);
47    bool HasValidDevice(libusb_device *device);
48    int CheckDescriptor(HUSB hUSB, libusb_device_descriptor& desc);
49    bool IsDebuggableDev(const struct libusb_interface_descriptor *ifDescriptor);
50    bool ReadyForWorkThread(HSession hSession) override;
51    bool FindDeviceByID(HUSB hUSB, const char *usbMountPoint, libusb_context *ctxUSB);
52    bool DetectMyNeed(libusb_device *device, string &sn);
53    void RestoreHdcProtocol(HUSB hUsb, const uint8_t *buf, int bufSize);
54    void UpdateUSBDaemonInfo(HUSB hUSB, HSession hSession, uint8_t connStatus);
55    void BeginUsbRead(HSession hSession);
56    void ReviewUsbNodeLater(string &nodeKey);
57    void CancelUsbIo(HSession hSession) override;
58    int UsbToHdcProtocol(uv_stream_t *stream, uint8_t *appendData, int dataSize) override;
59    int SubmitUsbBio(HSession hSession, bool sendOrRecv, uint8_t *buf, int bufSize);
60    void SendSoftResetToDaemon(HSession hSession, uint32_t sessionIdOld);
61
62    libusb_context *ctxUSB;
63    uv_timer_t devListWatcher;
64    map<string, UsbCheckStatus> mapIgnoreDevice;
65
66private:
67    uv_thread_t threadUsbWork;
68};
69}  // namespace Hdc
70#endif
71