xref: /developtools/hdc/src/common/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_USB_H
16#define HDC_USB_H
17#include "common.h"
18
19namespace Hdc {
20class HdcUSBBase {
21public:
22    HdcUSBBase(const bool serverOrDaemonIn, void *ptrMainBase);
23    virtual ~HdcUSBBase();
24    virtual bool ReadyForWorkThread(HSession hSession);
25    virtual void CancelUsbIo(HSession hSession) {};
26    int SendUSBBlock(HSession hSession, uint8_t *data, const int length);
27
28protected:
29    virtual int SendUSBRaw(HSession hSession, uint8_t *data, const int length)
30    {
31        return 0;
32    }
33    virtual int UsbToHdcProtocol(uv_stream_t *stream, uint8_t *appendData, int dataSize)
34    {
35        return 0;
36    };
37    int SendToHdcStream(HSession hSession, uv_stream_t *stream, uint8_t *appendData, int dataSize);
38    int GetSafeUsbBlockSize(uint16_t wMaxPacketSizeSend);
39    bool IsUsbPacketHeader(uint8_t *ioBuf, int ioBytes);
40    vector<uint8_t> BuildPacketHeader(uint32_t sessionId, uint8_t option, uint32_t dataSize);
41
42    void *clsMainBase;
43    bool modRunning;
44    bool serverOrDaemon;
45    const string USB_PACKET_FLAG = "UB";  // must 2bytes
46
47private:
48    static void ReadUSB(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
49    int CheckPacketOption(HSession hSession, uint8_t *appendData, int dataSize);
50    void PreSendUsbSoftReset(HSession hSession, uint32_t sessionIdOld);
51};
52}  // namespace Hdc
53
54#endif