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_DAEMON_USB_H 16 #define HDC_DAEMON_USB_H 17 #include "usb.h" 18 19 namespace Hdc { 20 class HdcDaemonUSB : public HdcUSBBase { 21 public: 22 HdcDaemonUSB(const bool serverOrDaemonIn, void *ptrMainBase); 23 virtual ~HdcDaemonUSB(); 24 int Initial(); 25 void Stop(); 26 int SendUSBRaw(HSession hSession, uint8_t *data, const int length) override; 27 void OnNewHandshakeOK(const uint32_t sessionId); 28 void OnSessionFreeFinally(const HSession hSession); 29 30 private: 31 struct CtxUvFileCommonIo { 32 void *thisClass; 33 void *data; 34 uint8_t *buf; 35 int bufSizeMax; 36 int bufSize; 37 bool atPollQueue; 38 uv_fs_t req; 39 }; 40 static void OnUSBRead(uv_fs_t *req); 41 static void WatchEPTimer(uv_timer_t *handle); 42 static void UvWriteCallback(uv_write_t *req, int status); 43 int ConnectEPPoint(HUSB hUSB); 44 int DispatchToWorkThread(uint32_t sessionId, uint8_t *readBuf, int readBytes); 45 int AvailablePacket(uint8_t *ioBuf, int ioBytes, uint32_t *sessionId); 46 void CloseEndpoint(HUSB hUSB, bool closeCtrlEp = false); 47 string GetDevPath(const std::string &path); 48 bool ReadyForWorkThread(HSession hSession) override; 49 int LoopUSBRead(HUSB hUSB, int readMaxWanted); 50 HSession PrepareNewSession(uint32_t sessionId, uint8_t *pRecvBuf, int recvBytesIO); 51 bool JumpAntiquePacket(const uint8_t &buf, ssize_t bytes) const; 52 int SendUSBIOSync(HSession hSession, HUSB hMainUSB, const uint8_t *data, const int length); 53 int CloseBulkEp(bool bulkInOut, int bulkFd, uv_loop_t *loop); 54 void ResetOldSession(uint32_t sessionId); 55 int GetMaxPacketSize(int fdFfs); 56 int UsbToHdcProtocol(uv_stream_t *stream, uint8_t *appendData, int dataSize) override; 57 void FillUsbV2Head(struct UsbFunctionfsDescV2 &descUsbFfs); 58 int UsbToStream(uv_stream_t *stream, const uint8_t *buf, const int size); 59 60 HdcUSB usbHandle = {}; 61 string basePath; // usb device's base path 62 uint32_t currentSessionId = 0; // USB mode,limit only one session 63 uv_timer_t checkEP; // server-use 64 mutex mutexUsbFfs; 65 bool isAlive = false; 66 int controlEp = 0; // EP0 67 CtxUvFileCommonIo ctxRecv = {}; 68 int saveNextReadSize = 0; 69 CircleBuffer cirbuf; 70 }; 71 } // namespace Hdc 72 #endif 73