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 int ConnectEPPoint(HUSB hUSB); 43 int DispatchToWorkThread(uint32_t sessionId, uint8_t *readBuf, int readBytes); 44 int AvailablePacket(uint8_t *ioBuf, int ioBytes, uint32_t *sessionId); 45 void CloseEndpoint(HUSB hUSB, bool closeCtrlEp = false); 46 string GetDevPath(const std::string &path); 47 bool ReadyForWorkThread(HSession hSession) override; 48 int LoopUSBRead(HUSB hUSB, int readMaxWanted); 49 HSession PrepareNewSession(uint32_t sessionId, uint8_t *pRecvBuf, int recvBytesIO); 50 bool JumpAntiquePacket(const uint8_t &buf, ssize_t bytes) const; 51 int SendUSBIOSync(HSession hSession, HUSB hMainUSB, const uint8_t *data, const int length); 52 int CloseBulkEp(bool bulkInOut, int bulkFd, uv_loop_t *loop); 53 void ResetOldSession(uint32_t sessionId); 54 int GetMaxPacketSize(int fdFfs); 55 int UsbToHdcProtocol(uv_stream_t *stream, uint8_t *appendData, int dataSize) override; 56 void FillUsbV2Head(struct usb_functionfs_desc_v2 &descUsbFfs); 57 58 HdcUSB usbHandle = {}; 59 string basePath; // usb device's base path 60 uint32_t currentSessionId = 0; // USB mode,limit only one session 61 uv_timer_t checkEP; // server-use 62 mutex mutexUsbFfs; 63 bool isAlive = false; 64 int controlEp = 0; // EP0 65 CtxUvFileCommonIo ctxRecv = {}; 66 int saveNextReadSize = 0; 67 }; 68 } // namespace Hdc 69 #endif