• 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 #include "usb.h"
16 
17 namespace Hdc {
HdcUSBBase(const bool serverOrDaemonIn,void * ptrMainBase)18 HdcUSBBase::HdcUSBBase(const bool serverOrDaemonIn, void *ptrMainBase)
19 {
20     serverOrDaemon = serverOrDaemonIn;
21     clsMainBase = ptrMainBase;
22     modRunning = true;
23 }
24 
~HdcUSBBase()25 HdcUSBBase::~HdcUSBBase()
26 {
27 }
28 
ReadUSB(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)29 void HdcUSBBase::ReadUSB(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
30 {
31     StartTraceScope("HdcUSBBase::ReadUSB");
32     HSession hSession = (HSession)stream->data;
33     HdcSessionBase *hSessionBase = (HdcSessionBase *)hSession->classInstance;
34     if (hSessionBase->FetchIOBuf(hSession, hSession->ioBuf, nread) < 0) {
35         hSessionBase->FreeSession(hSession->sessionId);
36     }
37 }
38 
ReadyForWorkThread(HSession hSession)39 bool HdcUSBBase::ReadyForWorkThread(HSession hSession)
40 {
41     // Server-end USB IO is handed over to each sub-thread, only the daemon is still read by the main IO to distribute
42     // to each sub-thread by DataPipe.
43     if (uv_tcp_init(&hSession->childLoop, &hSession->dataPipe[STREAM_WORK]) ||
44         uv_tcp_open(&hSession->dataPipe[STREAM_WORK], hSession->dataFd[STREAM_WORK])) {
45         WRITE_LOG(LOG_FATAL, "USBBase ReadyForWorkThread init child TCP failed");
46         return false;
47     }
48     hSession->dataPipe[STREAM_WORK].data = hSession;
49     HdcSessionBase *pSession = (HdcSessionBase *)hSession->classInstance;
50     Base::SetTcpOptions(&hSession->dataPipe[STREAM_WORK]);
51     if (uv_read_start((uv_stream_t *)&hSession->dataPipe[STREAM_WORK], pSession->AllocCallback, ReadUSB)) {
52         WRITE_LOG(LOG_FATAL, "USBBase ReadyForWorkThread child TCP read failed");
53         return false;
54     }
55     WRITE_LOG(LOG_DEBUG, "USBBase ReadyForWorkThread finish dataFd[STREAM_WORK]:%d",
56         hSession->dataFd[STREAM_WORK]);
57     return true;
58 };
59 
BuildPacketHeader(uint32_t sessionId,uint8_t option,uint32_t dataSize)60 vector<uint8_t> HdcUSBBase::BuildPacketHeader(uint32_t sessionId, uint8_t option, uint32_t dataSize)
61 {
62     vector<uint8_t> vecData;
63     USBHead head;
64     head.sessionId = htonl(sessionId);
65     for (size_t i = 0; i < sizeof(head.flag); i++) {
66         head.flag[i] = USB_PACKET_FLAG.data()[i];
67     }
68     head.option = option;
69     head.dataSize = htonl(dataSize);
70     vecData.insert(vecData.end(), (uint8_t *)&head, (uint8_t *)&head + sizeof(USBHead));
71     return vecData;
72 }
73 
74 // USB big data stream, block transmission, mainly to prevent accidental data packets from writing through EP port,
75 // inserting the send queue causes the program to crash
SendUSBBlock(HSession hSession,uint8_t * data,const int length)76 int HdcUSBBase::SendUSBBlock(HSession hSession, uint8_t *data, const int length)
77 {
78     int childRet = 0;
79     int ret = ERR_IO_FAIL;
80     StartTraceScope("HdcUSBBase::SendUSBBlock");
81     std::lock_guard<std::mutex> lock(hSession->hUSB->lockSendUsbBlock);
82     auto header = BuildPacketHeader(hSession->sessionId, USB_OPTION_HEADER, length);
83     do {
84         if ((SendUSBRaw(hSession, header.data(), header.size())) <= 0) {
85             WRITE_LOG(LOG_FATAL, "SendUSBRaw index failed");
86             break;
87         }
88         if ((childRet = SendUSBRaw(hSession, data, length)) <= 0) {
89             WRITE_LOG(LOG_FATAL, "SendUSBRaw body failed");
90             break;
91         }
92         if (childRet > 0 && (childRet % hSession->hUSB->wMaxPacketSizeSend == 0)) {
93             // win32 send ZLP will block winusb driver and LIBUSB_TRANSFER_ADD_ZERO_PACKET not effect
94             // so, we send dummy packet to prevent zero packet generate
95             auto dummy = BuildPacketHeader(hSession->sessionId, 0, 0);
96             if ((SendUSBRaw(hSession, dummy.data(), dummy.size())) <= 0) {
97                 WRITE_LOG(LOG_FATAL, "SendUSBRaw dummy failed");
98                 break;
99             }
100         }
101         ret = length;
102     } while (false);
103     return ret;
104 }
105 
IsUsbPacketHeader(uint8_t * ioBuf,int ioBytes)106 bool HdcUSBBase::IsUsbPacketHeader(uint8_t *ioBuf, int ioBytes)
107 {
108     StartTraceScope("HdcUSBBase::IsUsbPacketHeader");
109     USBHead *usbPayloadHeader = reinterpret_cast<struct USBHead *>(ioBuf);
110     uint32_t maybeSize = ntohl(usbPayloadHeader->dataSize);
111     bool isHeader = false;
112     do {
113         if (memcmp(usbPayloadHeader->flag, USB_PACKET_FLAG.c_str(), USB_PACKET_FLAG.size())) {
114             break;
115         }
116         if (ioBytes != sizeof(USBHead)) {
117             break;
118         }
119         if (maybeSize == 0) {
120             isHeader = true;  // nop packet
121             break;
122         } else {  // maybeSize != 0
123             if (usbPayloadHeader->option & USB_OPTION_HEADER) {
124                 isHeader = true;
125                 break;
126             }
127         }
128     } while (false);
129     return isHeader;
130 }
131 
PreSendUsbSoftReset(HSession hSession,uint32_t sessionIdOld)132 void HdcUSBBase::PreSendUsbSoftReset(HSession hSession, uint32_t sessionIdOld)
133 {
134     StartTraceScope("HdcUSBBase::PreSendUsbSoftReset");
135     HUSB hUSB = hSession->hUSB;
136     if (hSession->serverOrDaemon && !hUSB->resetIO) {
137         hUSB->lockSendUsbBlock.lock();
138         WRITE_LOG(LOG_WARN, "SendToHdcStream check, sessionId not matched");
139         auto header = BuildPacketHeader(sessionIdOld, USB_OPTION_RESET, 0);
140         if (SendUSBRaw(hSession, header.data(), header.size()) <= 0) {
141             WRITE_LOG(LOG_FATAL, "PreSendUsbSoftReset send failed");
142         }
143         hUSB->lockSendUsbBlock.unlock();
144         hUSB->resetIO = true;
145     }
146 }
147 
CheckPacketOption(HSession hSession,uint8_t * appendData,int dataSize)148 int HdcUSBBase::CheckPacketOption(HSession hSession, uint8_t *appendData, int dataSize)
149 {
150     HUSB hUSB = hSession->hUSB;
151     // special short packet
152     USBHead *header = reinterpret_cast<USBHead *>(appendData);
153     header->sessionId = ntohl(header->sessionId);
154     header->dataSize = ntohl(header->dataSize);
155     if (header->sessionId != hSession->sessionId) {
156         // Only server do it here, daemon 'SendUsbSoftReset' no use
157         // hilog + ctrl^C to reproduction scene
158         //
159         // Because the USB-reset API does not work on all platforms, the last session IO data may be
160         // recveived, we need to ignore it.
161         WRITE_LOG(LOG_WARN, "CheckPacketOption softreset header->sessionId:%u sessionId:%u",
162             header->sessionId, hSession->sessionId);
163         PreSendUsbSoftReset(hSession, header->sessionId);
164         return 0;
165     }
166     if (header->option & USB_OPTION_HEADER) {
167         // header packet
168         hUSB->payloadSize = header->dataSize;
169     }
170     // soft ZLP
171     return hUSB->payloadSize;
172 }
173 
174 // return value: <0 error; = 0 all finish; >0 need size
SendToHdcStream(HSession hSession,uv_stream_t * stream,uint8_t * appendData,int dataSize)175 int HdcUSBBase::SendToHdcStream(HSession hSession, uv_stream_t *stream, uint8_t *appendData, int dataSize)
176 {
177     StartTraceScope("HdcUSBBase::SendToHdcStream");
178     int childRet = 0;
179     HUSB hUSB = hSession->hUSB;
180     if (IsUsbPacketHeader(appendData, dataSize)) {
181         return CheckPacketOption(hSession, appendData, dataSize);
182     }
183     if (hUSB->payloadSize <= static_cast<uint32_t>(childRet)) {
184         // last session data
185         WRITE_LOG(LOG_WARN, "SendToHdcStream softreset dataSize:%d payloadSize:%u childRet:%d",
186             dataSize, hUSB->payloadSize, childRet);
187         PreSendUsbSoftReset(hSession, 0);  // 0 == reset current
188         return 0;
189     }
190     if ((childRet = UsbToHdcProtocol(stream, appendData, dataSize)) < 0) {
191         WRITE_LOG(LOG_FATAL, "Error usb send to stream dataSize:%d", dataSize);
192         return ERR_IO_FAIL;
193     }
194     hUSB->payloadSize -= childRet;
195     return hUSB->payloadSize;
196 }
197 
198 }
199