After creating a new session in HDC, read and discard USB data, and then send a handshake message

Signed-off-by: wenlong_12 <wenlong12@huawei.com>
Change-Id: Ib3b2f2762df1917d49617dda7a47742568d53234
This commit is contained in:
wenlong_12 2024-08-26 15:30:42 +08:00
parent e5b1c3ef94
commit 39fd5073d5
9 changed files with 296 additions and 218 deletions

View File

@ -87,6 +87,7 @@ constexpr uint16_t MAX_DELETED_SESSION_ID_RECORD_COUNT = 10;
#endif
constexpr uint16_t TCP_CONNECT_MAX_RETRY_COUNT = 6;
constexpr uint16_t TCP_CONNECT_RETRY_TIME_MS = 500;
constexpr uint16_t NEW_SESSION_DROP_USB_DATA_TIME_MS = 1000;
// double-word(hex)=[0]major[1][2]minor[3][4]version[5]fix(a-p)[6][7]reserve
// |----------------------------------------------------------------|

228
src/common/define_enum.h Normal file
View File

@ -0,0 +1,228 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DEFINE_ENUM_H
#define DEFINE_ENUM_H
namespace Hdc {
enum HdcLogLevel {
LOG_OFF,
LOG_FATAL,
LOG_WARN,
LOG_INFO, // default
LOG_DEBUG,
LOG_ALL,
LOG_VERBOSE,
LOG_LAST = LOG_VERBOSE, // tail, not use
};
enum MessageLevel {
MSG_FAIL,
MSG_INFO,
MSG_OK,
};
enum ConnType { CONN_USB = 0, CONN_TCP, CONN_SERIAL, CONN_BT, CONN_UNKNOWN };
const string conTypeDetail[] = { "USB", "TCP", "UART", "BT", "UNKNOWN" };
#ifdef HDC_SUPPORT_UART
enum UartTimeConst {
UV_TIMEOUT = 10,
UV_REPEAT = 100,
TIMEOUTS_R_INTERALTIMEOUT = 1000,
TIMEOUTS_R_TOALTIMEOUTMULTIPLIER = 500,
TIMEOUTS_R_TIMEOUTCONSTANT = 5000
};
enum UartSetSerialNBits {
UART_BIT1 = 7,
UART_BIT2 = 8
};
enum UartSetSerialNSpeed {
UART_SPEED2400 = 2400,
UART_SPEED4800 = 4800,
UART_SPEED9600 = 9600,
UART_SPEED115200 = 115200,
UART_SPEED921600 = 921600,
UART_SPEED1500000 = 1500000
};
enum UartSetSerialNStop {
UART_STOP1 = 1,
UART_STOP2 = 2
};
#endif
enum ConnStatus { STATUS_UNKNOW = 0, STATUS_READY, STATUS_CONNECTED, STATUS_OFFLINE, STATUS_UNAUTH };
const string conStatusDetail[] = { "Unknown", "Ready", "Connected", "Offline", "Unauthorized" };
enum AuthVerifyType { RSA_ENCRYPT = 0, RSA_3072_SHA512 = 1, UNKNOWN = 100 };
enum OperateID {
OP_ADD,
OP_REMOVE,
OP_QUERY,
OP_QUERY_REF, // crossthread query, manually reduce ref
OP_GET_STRLIST,
OP_GET_STRLIST_FULL,
OP_GET_ANY,
OP_UPDATE,
OP_CLEAR,
OP_INIT,
OP_GET_ONLY,
OP_VOTE_RESET,
OP_WAIT_FOR_ANY
};
enum RetErrCode {
RET_SUCCESS = 0,
ERR_GENERIC = -1,
ERR_NO_SUPPORT = -2,
ERR_PARAM_NULLPTR = -5,
ERR_LIBUSB_OPEN = -100,
ERR_BUF_SIZE = -10000,
ERR_BUF_ALLOC,
ERR_BUF_OVERFLOW,
ERR_BUF_CHECK,
ERR_BUF_RESET,
ERR_BUF_COPY,
ERR_CHECK_VERSION,
ERR_FILE_OPEN = -11000,
ERR_FILE_READ,
ERR_FILE_WRITE,
ERR_FILE_STAT,
ERR_FILE_PATH_CHECK,
ERR_PARM_FORMAT = -12000,
ERR_PARM_SIZE,
ERR_PARM_FAIL,
ERR_API_FAIL = -13000,
ERR_IO_FAIL = -14000,
ERR_IO_TIMEOUT,
ERR_IO_SOFT_RESET,
ERR_SESSION_NOFOUND = -15000,
ERR_SESSION_OFFLINE,
ERR_SESSION_DEAD,
ERR_HANDSHAKE_NOTMATCH = -16000,
ERR_HANDSHAKE_CONNECTKEY_FAILED,
ERR_HANDSHAKE_HANGUP_CHILD,
ERR_SOCKET_FAIL = -17000,
ERR_SOCKET_DUPLICATE,
ERR_MODULE_JDWP_FAILED = -18000,
ERR_UT_MODULE_NOTREADY = -19000,
ERR_UT_MODULE_WAITMAX,
ERR_THREAD_MUTEX_FAIL = -20000,
ERR_PROCESS_SUB_FAIL = -21000,
ERR_PRIVELEGE_NEED = -22000,
};
// Flags shared by multiple modules
enum AsyncEvent {
ASYNC_STOP_MAINLOOP = 0,
ASYNC_FREE_SESSION,
ASYNC_FREE_CHANNEL,
};
enum InnerCtrlCommand {
SP_START_SESSION = 0,
SP_STOP_SESSION,
SP_ATTACH_CHANNEL,
SP_DEATCH_CHANNEL,
SP_JDWP_NEWFD,
SP_ARK_NEWFD,
};
enum HdcCommand {
// core commands types
CMD_KERNEL_HELP = 0,
CMD_KERNEL_HANDSHAKE,
CMD_KERNEL_CHANNEL_CLOSE,
CMD_KERNEL_SERVER_KILL,
CMD_KERNEL_TARGET_DISCOVER,
CMD_KERNEL_TARGET_LIST,
CMD_KERNEL_TARGET_ANY,
CMD_KERNEL_TARGET_CONNECT,
CMD_KERNEL_TARGET_DISCONNECT,
CMD_KERNEL_ECHO,
CMD_KERNEL_ECHO_RAW,
CMD_KERNEL_ENABLE_KEEPALIVE,
CMD_KERNEL_WAKEUP_SLAVETASK,
CMD_CHECK_SERVER,
CMD_CHECK_DEVICE,
CMD_WAIT_FOR,
// One-pass simple commands
CMD_UNITY_COMMAND_HEAD = 1000, // not use
CMD_UNITY_EXECUTE,
CMD_UNITY_REMOUNT,
CMD_UNITY_REBOOT,
CMD_UNITY_RUNMODE,
CMD_UNITY_HILOG,
CMD_UNITY_TERMINATE,
CMD_UNITY_ROOTRUN,
CMD_JDWP_LIST,
CMD_JDWP_TRACK,
CMD_UNITY_COMMAND_TAIL, // not use
// It will be separated from unity in the near future
CMD_UNITY_BUGREPORT_INIT,
CMD_UNITY_BUGREPORT_DATA,
// Shell commands types
CMD_SHELL_INIT = 2000,
CMD_SHELL_DATA,
// Forward commands types
CMD_FORWARD_INIT = 2500,
CMD_FORWARD_CHECK,
CMD_FORWARD_CHECK_RESULT,
CMD_FORWARD_ACTIVE_SLAVE,
CMD_FORWARD_ACTIVE_MASTER,
CMD_FORWARD_DATA,
CMD_FORWARD_FREE_CONTEXT,
CMD_FORWARD_LIST,
CMD_FORWARD_REMOVE,
CMD_FORWARD_SUCCESS,
// File commands
CMD_FILE_INIT = 3000,
CMD_FILE_CHECK,
CMD_FILE_BEGIN,
CMD_FILE_DATA,
CMD_FILE_FINISH,
CMD_APP_SIDELOAD,
CMD_FILE_MODE,
CMD_DIR_MODE,
// App commands
CMD_APP_INIT = 3500,
CMD_APP_CHECK,
CMD_APP_BEGIN,
CMD_APP_DATA,
CMD_APP_FINISH,
CMD_APP_UNINSTALL,
// deprecated, remove later
CMD_UNITY_JPID = CMD_JDWP_LIST,
// Flashd commands
CMD_FLASHD_UPDATE_INIT = 4000,
CMD_FLASHD_FLASH_INIT,
CMD_FLASHD_CHECK,
CMD_FLASHD_BEGIN,
CMD_FLASHD_DATA,
CMD_FLASHD_FINISH,
CMD_FLASHD_ERASE,
CMD_FLASHD_FORMAT,
CMD_FLASHD_PROGRESS,
};
enum UsbProtocolOption {
USB_OPTION_HEADER = 1,
USB_OPTION_RESET = 2,
USB_OPTION_RESERVE4 = 4,
USB_OPTION_RESERVE8 = 8,
USB_OPTION_RESERVE16 = 16,
};
}
#endif

View File

@ -20,6 +20,7 @@
#ifdef HDC_TRACE
#include "hitrace_meter.h"
#endif
#include "define_enum.h"
namespace Hdc {
@ -37,18 +38,6 @@ static string MaskString(const string &str)
}
}
// ############################# enum define ###################################
enum HdcLogLevel {
LOG_OFF,
LOG_FATAL,
LOG_WARN,
LOG_INFO, // default
LOG_DEBUG,
LOG_ALL,
LOG_VERBOSE,
LOG_LAST = LOG_VERBOSE, // tail, not use
};
#define WRITE_LOG(level, fmt, ...) Base::PrintLogEx(__FILE_NAME__, __LINE__, level, fmt, ##__VA_ARGS__)
#ifndef TEMP_FAILURE_RETRY
@ -72,203 +61,6 @@ enum HdcLogLevel {
#define StartTraceScope(value)
#endif
enum MessageLevel {
MSG_FAIL,
MSG_INFO,
MSG_OK,
};
enum ConnType { CONN_USB = 0, CONN_TCP, CONN_SERIAL, CONN_BT, CONN_UNKNOWN };
const string conTypeDetail[] = { "USB", "TCP", "UART", "BT", "UNKNOWN" };
#ifdef HDC_SUPPORT_UART
enum UartTimeConst {
UV_TIMEOUT = 10,
UV_REPEAT = 100,
TIMEOUTS_R_INTERALTIMEOUT = 1000,
TIMEOUTS_R_TOALTIMEOUTMULTIPLIER = 500,
TIMEOUTS_R_TIMEOUTCONSTANT = 5000
};
enum UartSetSerialNBits {
UART_BIT1 = 7,
UART_BIT2 = 8
};
enum UartSetSerialNSpeed {
UART_SPEED2400 = 2400,
UART_SPEED4800 = 4800,
UART_SPEED9600 = 9600,
UART_SPEED115200 = 115200,
UART_SPEED921600 = 921600,
UART_SPEED1500000 = 1500000
};
enum UartSetSerialNStop {
UART_STOP1 = 1,
UART_STOP2 = 2
};
#endif
enum ConnStatus { STATUS_UNKNOW = 0, STATUS_READY, STATUS_CONNECTED, STATUS_OFFLINE, STATUS_UNAUTH };
const string conStatusDetail[] = { "Unknown", "Ready", "Connected", "Offline", "Unauthorized" };
enum AuthVerifyType { RSA_ENCRYPT = 0, RSA_3072_SHA512 = 1, UNKNOWN = 100 };
enum OperateID {
OP_ADD,
OP_REMOVE,
OP_QUERY,
OP_QUERY_REF, // crossthread query, manually reduce ref
OP_GET_STRLIST,
OP_GET_STRLIST_FULL,
OP_GET_ANY,
OP_UPDATE,
OP_CLEAR,
OP_INIT,
OP_GET_ONLY,
OP_VOTE_RESET,
OP_WAIT_FOR_ANY
};
enum RetErrCode {
RET_SUCCESS = 0,
ERR_GENERIC = -1,
ERR_NO_SUPPORT = -2,
ERR_PARAM_NULLPTR = -5,
ERR_LIBUSB_OPEN = -100,
ERR_BUF_SIZE = -10000,
ERR_BUF_ALLOC,
ERR_BUF_OVERFLOW,
ERR_BUF_CHECK,
ERR_BUF_RESET,
ERR_BUF_COPY,
ERR_CHECK_VERSION,
ERR_FILE_OPEN = -11000,
ERR_FILE_READ,
ERR_FILE_WRITE,
ERR_FILE_STAT,
ERR_FILE_PATH_CHECK,
ERR_PARM_FORMAT = -12000,
ERR_PARM_SIZE,
ERR_PARM_FAIL,
ERR_API_FAIL = -13000,
ERR_IO_FAIL = -14000,
ERR_IO_TIMEOUT,
ERR_IO_SOFT_RESET,
ERR_SESSION_NOFOUND = -15000,
ERR_SESSION_OFFLINE,
ERR_SESSION_DEAD,
ERR_HANDSHAKE_NOTMATCH = -16000,
ERR_HANDSHAKE_CONNECTKEY_FAILED,
ERR_HANDSHAKE_HANGUP_CHILD,
ERR_SOCKET_FAIL = -17000,
ERR_SOCKET_DUPLICATE,
ERR_MODULE_JDWP_FAILED = -18000,
ERR_UT_MODULE_NOTREADY = -19000,
ERR_UT_MODULE_WAITMAX,
ERR_THREAD_MUTEX_FAIL = -20000,
ERR_PROCESS_SUB_FAIL = -21000,
ERR_PRIVELEGE_NEED = -22000,
};
// Flags shared by multiple modules
enum AsyncEvent {
ASYNC_STOP_MAINLOOP = 0,
ASYNC_FREE_SESSION,
ASYNC_FREE_CHANNEL,
};
enum InnerCtrlCommand {
SP_START_SESSION = 0,
SP_STOP_SESSION,
SP_ATTACH_CHANNEL,
SP_DEATCH_CHANNEL,
SP_JDWP_NEWFD,
SP_ARK_NEWFD,
};
enum HdcCommand {
// core commands types
CMD_KERNEL_HELP = 0,
CMD_KERNEL_HANDSHAKE,
CMD_KERNEL_CHANNEL_CLOSE,
CMD_KERNEL_SERVER_KILL,
CMD_KERNEL_TARGET_DISCOVER,
CMD_KERNEL_TARGET_LIST,
CMD_KERNEL_TARGET_ANY,
CMD_KERNEL_TARGET_CONNECT,
CMD_KERNEL_TARGET_DISCONNECT,
CMD_KERNEL_ECHO,
CMD_KERNEL_ECHO_RAW,
CMD_KERNEL_ENABLE_KEEPALIVE,
CMD_KERNEL_WAKEUP_SLAVETASK,
CMD_CHECK_SERVER,
CMD_CHECK_DEVICE,
CMD_WAIT_FOR,
// One-pass simple commands
CMD_UNITY_COMMAND_HEAD = 1000, // not use
CMD_UNITY_EXECUTE,
CMD_UNITY_REMOUNT,
CMD_UNITY_REBOOT,
CMD_UNITY_RUNMODE,
CMD_UNITY_HILOG,
CMD_UNITY_TERMINATE,
CMD_UNITY_ROOTRUN,
CMD_JDWP_LIST,
CMD_JDWP_TRACK,
CMD_UNITY_COMMAND_TAIL, // not use
// It will be separated from unity in the near future
CMD_UNITY_BUGREPORT_INIT,
CMD_UNITY_BUGREPORT_DATA,
// Shell commands types
CMD_SHELL_INIT = 2000,
CMD_SHELL_DATA,
// Forward commands types
CMD_FORWARD_INIT = 2500,
CMD_FORWARD_CHECK,
CMD_FORWARD_CHECK_RESULT,
CMD_FORWARD_ACTIVE_SLAVE,
CMD_FORWARD_ACTIVE_MASTER,
CMD_FORWARD_DATA,
CMD_FORWARD_FREE_CONTEXT,
CMD_FORWARD_LIST,
CMD_FORWARD_REMOVE,
CMD_FORWARD_SUCCESS,
// File commands
CMD_FILE_INIT = 3000,
CMD_FILE_CHECK,
CMD_FILE_BEGIN,
CMD_FILE_DATA,
CMD_FILE_FINISH,
CMD_APP_SIDELOAD,
CMD_FILE_MODE,
CMD_DIR_MODE,
// App commands
CMD_APP_INIT = 3500,
CMD_APP_CHECK,
CMD_APP_BEGIN,
CMD_APP_DATA,
CMD_APP_FINISH,
CMD_APP_UNINSTALL,
// deprecated, remove later
CMD_UNITY_JPID = CMD_JDWP_LIST,
// Flashd commands
CMD_FLASHD_UPDATE_INIT = 4000,
CMD_FLASHD_FLASH_INIT,
CMD_FLASHD_CHECK,
CMD_FLASHD_BEGIN,
CMD_FLASHD_DATA,
CMD_FLASHD_FINISH,
CMD_FLASHD_ERASE,
CMD_FLASHD_FORMAT,
CMD_FLASHD_PROGRESS,
};
enum UsbProtocolOption {
USB_OPTION_HEADER = 1,
USB_OPTION_RESET = 2,
USB_OPTION_RESERVE4 = 4,
USB_OPTION_RESERVE8 = 8,
USB_OPTION_RESERVE16 = 16,
};
// ################################### struct define ###################################
#pragma pack(push)
#pragma pack(1)
@ -446,6 +238,9 @@ struct HdcSession {
uv_thread_t hWorkChildThread;
std::mutex mapTaskMutex;
AuthVerifyType verifyType;
std::atomic<bool> isNeedDropData; // host: Whether to discard the USB data after it is read
bool isSoftReset; // for daemon, Used to record whether a reset command has been received
std::string ToDebugString()
{
std::ostringstream oss;
@ -489,6 +284,8 @@ struct HdcSession {
hUART = nullptr;
#endif
verifyType = AuthVerifyType::RSA_3072_SHA512;
isNeedDropData = true;
isSoftReset = false;
}
~HdcSession()

View File

@ -37,6 +37,7 @@ protected:
int SendToHdcStream(HSession hSession, uv_stream_t *stream, uint8_t *appendData, int dataSize);
int GetSafeUsbBlockSize(uint16_t wMaxPacketSizeSend);
bool IsUsbPacketHeader(uint8_t *ioBuf, int ioBytes);
vector<uint8_t> BuildPacketHeader(uint32_t sessionId, uint8_t option, uint32_t dataSize);
void *clsMainBase;
bool modRunning;
@ -45,7 +46,6 @@ protected:
private:
static void ReadUSB(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
vector<uint8_t> BuildPacketHeader(uint32_t sessionId, uint8_t option, uint32_t dataSize);
int CheckPacketOption(HSession hSession, uint8_t *appendData, int dataSize);
void PreSendUsbSoftReset(HSession hSession, uint32_t sessionIdOld);
};

View File

@ -214,7 +214,7 @@ void HdcDaemonUSB::CloseEndpoint(HUSB hUSB, bool closeCtrlEp)
WRITE_LOG(LOG_FATAL, "DaemonUSB close endpoint");
}
void HdcDaemonUSB::ResetOldSession(uint32_t sessionId)
void HdcDaemonUSB::ResetOldSession(uint32_t sessionId, bool isSoftReset)
{
HdcDaemon *daemon = reinterpret_cast<HdcDaemon *>(clsMainBase);
if (sessionId == 0) {
@ -226,7 +226,9 @@ void HdcDaemonUSB::ResetOldSession(uint32_t sessionId)
return;
}
// The Host side is restarted, but the USB cable is still connected
WRITE_LOG(LOG_WARN, "Hostside softreset to restart daemon, old sessionId:%u", sessionId);
hSession->isSoftReset = isSoftReset;
WRITE_LOG(LOG_WARN, "Hostside softreset to restart daemon, old sessionId:%u isSoftReset:%d",
sessionId, isSoftReset);
daemon->FreeSession(sessionId);
}
@ -242,8 +244,9 @@ int HdcDaemonUSB::AvailablePacket(uint8_t *ioBuf, int ioBytes, uint32_t *session
USBHead *usbPayloadHeader = reinterpret_cast<struct USBHead *>(ioBuf);
uint32_t inSessionId = ntohl(usbPayloadHeader->sessionId);
if ((usbPayloadHeader->option & USB_OPTION_RESET)) {
WRITE_LOG(LOG_FATAL, "USB_OPTION_RESET sessionId:%u", inSessionId);
ResetOldSession(inSessionId);
WRITE_LOG(LOG_INFO, "USB_OPTION_RESET inSessionId:%u, currentSessionId:%u",
inSessionId, currentSessionId);
ResetOldSession(inSessionId, true);
ret = ERR_IO_SOFT_RESET;
break;
}
@ -356,7 +359,13 @@ void HdcDaemonUSB::OnNewHandshakeOK(const uint32_t sessionId)
// MainThreadCall, when seession was freed
void HdcDaemonUSB::OnSessionFreeFinally(const HSession hSession)
{
WRITE_LOG(LOG_DEBUG, "OnSessionFreeFinally sid:%u currentsid:%u", hSession->sessionId, currentSessionId);
if (hSession->isSoftReset) {
WRITE_LOG(LOG_INFO, "OnSessionFreeFinally sid:%u softreset", hSession->sessionId);
return;
}
if (currentSessionId == hSession->sessionId) {
WRITE_LOG(LOG_DEBUG, "OnSessionFreeFinally set isAlive false");
isAlive = false;
// uv_cancel ctxRecv.req == UV_EBUSY, not effect immediately. It must be close by logic
}
@ -609,6 +618,7 @@ void HdcDaemonUSB::OnUSBRead(uv_fs_t *req)
break;
}
if (!ret) {
WRITE_LOG(LOG_INFO, "OnUSBRead ret false, set isAlive = false");
thisClass->isAlive = false;
thisClass->ctxRecv.atPollQueue = false;
}

View File

@ -51,7 +51,7 @@ private:
bool JumpAntiquePacket(const uint8_t &buf, ssize_t bytes) const;
int SendUSBIOSync(HSession hSession, HUSB hMainUSB, const uint8_t *data, const int length);
int CloseBulkEp(bool bulkInOut, int bulkFd, uv_loop_t *loop);
void ResetOldSession(uint32_t sessionId);
void ResetOldSession(uint32_t sessionId, bool isSoftReset = false);
int GetMaxPacketSize(int fdFfs);
int UsbToHdcProtocol(uv_stream_t *stream, uint8_t *appendData, int dataSize) override;
void FillUsbV2Head(struct UsbFunctionfsDescV2 &descUsbFfs);

View File

@ -595,6 +595,14 @@ void HdcHostUSB::BeginUsbRead(HSession hSession)
WRITE_LOG(LOG_FATAL, "Read usb failed, sid:%u ret:%d", hSession->sessionId, childRet);
break;
}
// when a session is set up for a period of time, the read data is discarded to empty the USB channel.
if (hSession->isNeedDropData) {
WRITE_LOG(LOG_WARN, "Read usb thread sid:%u read size:%d, isNeedDropData:%d drop data",
hSession->sessionId, childRet, hSession->isNeedDropData.load());
childRet = 0;
continue;
}
if (childRet == 0) {
WRITE_LOG(LOG_WARN, "Read usb return 0, continue read, sid:%u", hSession->sessionId);
childRet = nextReadSize;
@ -738,17 +746,42 @@ HSession HdcHostUSB::ConnectDetectDaemon(const HSession hSession, const HDaemonI
return nullptr;
}
UpdateUSBDaemonInfo(hUSB, hSession, STATUS_CONNECTED);
hSession->isNeedDropData = true;
WRITE_LOG(LOG_INFO, "ConnectDetectDaemon set isNeedDropData true, sid:%u", hSession->sessionId);
BeginUsbRead(hSession);
hUSB->usbMountPoint = pdi->usbMountPoint;
WRITE_LOG(LOG_DEBUG, "HSession HdcHostUSB::ConnectDaemon");
WRITE_LOG(LOG_DEBUG, "HSession HdcHostUSB::ConnectDaemon, sid:%u", hSession->sessionId);
Base::StartWorkThread(&pServer->loopMain, pServer->SessionWorkThread, Base::FinishWorkThread, hSession);
// wait for thread up
while (hSession->childLoop.active_handles == 0) {
uv_sleep(1);
}
auto funcDelayStartSessionNotify = [hSession](const uint8_t flag, string &msg, const void *p) -> void {
HdcServer *pServer = (HdcServer *)hSession->classInstance;
auto ctrl = pServer->BuildCtrlString(SP_START_SESSION, 0, nullptr, 0);
hSession->isNeedDropData = false;
WRITE_LOG(LOG_INFO, "funcDelayStartSessionNotify set isNeedDropData false, sid:%u", hSession->sessionId);
Base::SendToPollFd(hSession->ctrlFd[STREAM_MAIN], ctrl.data(), ctrl.size());
};
// delay NEW_SESSION_DROP_USB_DATA_TIME_MS to start session
SendSoftResetToDaemon(hSession, 0);
Base::DelayDoSimple(&(pServer->loopMain), NEW_SESSION_DROP_USB_DATA_TIME_MS, funcDelayStartSessionNotify);
return hSession;
}
void HdcHostUSB::SendSoftResetToDaemon(HSession hSession, uint32_t sessionIdOld)
{
HUSB hUSB = hSession->hUSB;
hUSB->lockSendUsbBlock.lock();
WRITE_LOG(LOG_INFO, "SendSoftResetToDaemon sid:%u sidOld:%u", hSession->sessionId, sessionIdOld);
auto header = BuildPacketHeader(sessionIdOld, USB_OPTION_RESET, 0);
if (SendUSBRaw(hSession, header.data(), header.size()) <= 0) {
WRITE_LOG(LOG_FATAL, "SendSoftResetToDaemon send failed");
}
hUSB->lockSendUsbBlock.unlock();
WRITE_LOG(LOG_INFO, "SendSoftResetToDaemon sid:%u finished", hSession->sessionId);
}
} // namespace Hdc

View File

@ -57,6 +57,7 @@ private:
void CancelUsbIo(HSession hSession) override;
int UsbToHdcProtocol(uv_stream_t *stream, uint8_t *appendData, int dataSize) override;
int SubmitUsbBio(HSession hSession, bool sendOrRecv, uint8_t *buf, int bufSize);
void SendSoftResetToDaemon(HSession hSession, uint32_t sessionIdOld);
libusb_context *ctxUSB;
uv_timer_t devListWatcher;

View File

@ -720,6 +720,14 @@ HSession HdcServerForClient::FindAliveSessionFromDaemonMap(const HChannel hChann
EchoClient(hChannel, MSG_FAIL, "Bind tartget session is dead");
return nullptr;
}
if (!hdi->hSession->handshakeOK) {
WRITE_LOG(LOG_WARN, "hSession handShake is false sid:%u cid:%u",
hdi->hSession->sessionId, hChannel->channelId);
const string errMsg = "[E000004]:The communication channel is being established.\r\n"\
"Please wait for several seconds and try again.";
EchoClient(hChannel, MSG_FAIL, errMsg.c_str());
return nullptr;
}
hSession = reinterpret_cast<HSession>(hdi->hSession);
return hSession;
}