!5869 Reinforce BR/BLE reuse

Merge pull request !5869 from 吴冯山/master
This commit is contained in:
openharmony_ci 2024-05-05 12:58:03 +00:00 committed by Gitee
commit 74e7dabf5e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
20 changed files with 253 additions and 81 deletions

View File

@ -720,7 +720,7 @@ bool CheckActiveAuthConnection(const AuthConnInfo *connInfo)
{ {
connOpt.bleOption.protocol = BLE_PROTOCOL_ANY; connOpt.bleOption.protocol = BLE_PROTOCOL_ANY;
} }
return CheckActiveConnection(&connOpt); return CheckActiveConnection(&connOpt, true);
} }
int32_t AuthStartListening(AuthLinkType type, const char *ip, int32_t port) int32_t AuthStartListening(AuthLinkType type, const char *ip, int32_t port)

View File

@ -102,7 +102,7 @@ static bool HbHasActiveBrConnection(const char *networkId)
LNN_LOGE(LNN_HEART_BEAT, "HB convert bt mac err"); LNN_LOGE(LNN_HEART_BEAT, "HB convert bt mac err");
return false; return false;
} }
ret = CheckActiveConnection(&option); ret = CheckActiveConnection(&option, false);
LNN_LOGD(LNN_HEART_BEAT, "HB has active bt connection=%{public}s", ret ? "true" : "false"); LNN_LOGD(LNN_HEART_BEAT, "HB has active bt connection=%{public}s", ret ? "true" : "false");
return ret; return ret;
} }
@ -129,7 +129,7 @@ static bool HbHasActiveBleConnection(const char *networkId)
LNN_LOGE(LNN_HEART_BEAT, "HB memcpy_s udid hash err"); LNN_LOGE(LNN_HEART_BEAT, "HB memcpy_s udid hash err");
return false; return false;
} }
ret = CheckActiveConnection(&option); ret = CheckActiveConnection(&option, false);
LNN_LOGD(LNN_HEART_BEAT, "HB has active ble connection=%{public}s", ret ? "true" : "false"); LNN_LOGD(LNN_HEART_BEAT, "HB has active ble connection=%{public}s", ret ? "true" : "false");
return ret; return ret;
} }

View File

@ -1187,7 +1187,7 @@ static bool CheckHasBrConnection(const char *networkId)
connOpt.brOption.brMac[0] == '\0') { connOpt.brOption.brMac[0] == '\0') {
return false; return false;
} }
return CheckActiveConnection(&connOpt); return CheckActiveConnection(&connOpt, true);
} }
static bool IsHasAuthConnInfo(const char *networkId) static bool IsHasAuthConnInfo(const char *networkId)

View File

@ -1829,7 +1829,7 @@ static int32_t BleStopLocalListening(const LocalListenerInfo *info)
return ConnBleStopServer(); return ConnBleStopServer();
} }
static bool BleCheckActiveConnection(const ConnectOption *option) static bool BleCheckActiveConnection(const ConnectOption *option, bool needOccupy)
{ {
CONN_CHECK_AND_RETURN_RET_LOGW(option != NULL, false, CONN_BLE, "invaliad param, option is null"); CONN_CHECK_AND_RETURN_RET_LOGW(option != NULL, false, CONN_BLE, "invaliad param, option is null");
CONN_CHECK_AND_RETURN_RET_LOGW( CONN_CHECK_AND_RETURN_RET_LOGW(
@ -1845,7 +1845,9 @@ static bool BleCheckActiveConnection(const ConnectOption *option)
CONN_CHECK_AND_RETURN_RET_LOGW( CONN_CHECK_AND_RETURN_RET_LOGW(
connection != NULL, false, CONN_BLE, "ble check action connection: connection is not exist"); connection != NULL, false, CONN_BLE, "ble check action connection: connection is not exist");
bool isActive = (connection->state == BLE_CONNECTION_STATE_EXCHANGED_BASIC_INFO); bool isActive = (connection->state == BLE_CONNECTION_STATE_EXCHANGED_BASIC_INFO);
if (isActive && needOccupy) {
ConnBleRefreshIdleTimeout(connection);
}
ConnBleReturnConnection(&connection); ConnBleReturnConnection(&connection);
return isActive; return isActive;
} }

View File

@ -35,6 +35,9 @@ extern "C" {
#define WAIT_BR_NEGOTIATION_CLOSING_TIMEOUT_MILLIS (3 * 1000) #define WAIT_BR_NEGOTIATION_CLOSING_TIMEOUT_MILLIS (3 * 1000)
#define RETRY_NOTIFY_REFERENCE_DELAY_MILLIS (1 * 1000) #define RETRY_NOTIFY_REFERENCE_DELAY_MILLIS (1 * 1000)
#define WAIT_TIMEOUT_OCCUPY (3 * 1000)
#define WAIT_TIMEOUT_TRY_AGAIN (1 * 1000)
#define MAX_RETRY_COUNT (2) #define MAX_RETRY_COUNT (2)
enum ConnBrConnectionState { enum ConnBrConnectionState {
@ -64,6 +67,7 @@ typedef struct {
// reference counter that record times for memory management // reference counter that record times for memory management
int32_t objectRc; int32_t objectRc;
bool isOccupied;
// congestion control // congestion control
int32_t window; int32_t window;
int64_t sequence; int64_t sequence;
@ -93,6 +97,8 @@ int32_t ConnBrDisconnectNow(ConnBrConnection *connection);
int32_t ConnBrStartServer(void); int32_t ConnBrStartServer(void);
int32_t ConnBrStopServer(void); int32_t ConnBrStopServer(void);
void ConnBrOccupy(ConnBrConnection *connection);
int32_t ConnBrConnectionMuduleInit(SoftBusLooper *looper, SppSocketDriver *sppDriver, ConnBrEventListener *listener); int32_t ConnBrConnectionMuduleInit(SoftBusLooper *looper, SppSocketDriver *sppDriver, ConnBrEventListener *listener);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -30,6 +30,11 @@
#define UUID "8ce255c0-200a-11e0-ac64-0800200c9a66" #define UUID "8ce255c0-200a-11e0-ac64-0800200c9a66"
typedef struct {
int32_t delta;
int32_t peerRc;
} ReferenceCount;
typedef struct { typedef struct {
int32_t socketHandle; int32_t socketHandle;
} ServerServeContext; } ServerServeContext;
@ -50,6 +55,9 @@ enum BrConnectionLooperMsgType {
MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT = 100, MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT = 100,
MSG_CONNECTION_RETRY_NOTIFY_REFERENCE, MSG_CONNECTION_RETRY_NOTIFY_REFERENCE,
MSG_CONNECTION_REPORT_CONNECT_EXCEPTION, MSG_CONNECTION_REPORT_CONNECT_EXCEPTION,
MSG_CONNECTION_OCCUPY_RELEASE,
MSG_CONNECTION_UPDATE_LOCAL_RC,
MSG_CONNECTION_UPDATE_PEER_RC,
}; };
static void BrConnectionMsgHandler(SoftBusMessage *msg); static void BrConnectionMsgHandler(SoftBusMessage *msg);
@ -316,6 +324,24 @@ ConnBrConnection *ConnBrCreateConnection(const char *addr, ConnSideType side, in
return connection; return connection;
} }
void ConnBrOccupy(ConnBrConnection *connection)
{
CONN_CHECK_AND_RETURN_LOGE(connection != NULL, CONN_BR, "conn is NULL");
CONN_CHECK_AND_RETURN_LOGE(SoftBusMutexLock(&connection->lock) == SOFTBUS_OK, CONN_BR,
"lock failed, connId=%{public}u", connection->connectionId);
ConnRemoveMsgFromLooper(
&g_brConnectionAsyncHandler, MSG_CONNECTION_OCCUPY_RELEASE, connection->connectionId, 0, NULL);
int32_t res = ConnPostMsgToLooper(&g_brConnectionAsyncHandler,
MSG_CONNECTION_OCCUPY_RELEASE, connection->connectionId, 0, NULL, WAIT_TIMEOUT_OCCUPY);
if (res != SOFTBUS_OK) {
CONN_LOGW(CONN_BLE,"post msg failed, connId=%{public}u, err=%{public}d", connection->connectionId, res);
(void)SoftBusMutexUnlock(&connection->lock);
return;
}
connection->isOccupied = true;
(void)SoftBusMutexUnlock(&connection->lock);
}
void ConnBrFreeConnection(ConnBrConnection *connection) void ConnBrFreeConnection(ConnBrConnection *connection)
{ {
CONN_CHECK_AND_RETURN_LOGW(connection != NULL, CONN_BR, "br free connection: connection is NULL"); CONN_CHECK_AND_RETURN_LOGW(connection != NULL, CONN_BR, "br free connection: connection is NULL");
@ -352,24 +378,29 @@ int32_t ConnBrConnect(ConnBrConnection *connection)
return SOFTBUS_OK; return SOFTBUS_OK;
} }
int32_t ConnBrUpdateConnectionRc(ConnBrConnection *connection, int32_t delta) static int32_t NotifyUpdateConnectionRc(uint32_t connectionId, int32_t delta)
{ {
CONN_CHECK_AND_RETURN_RET_LOGE(SoftBusMutexLock(&connection->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, CONN_BR, ConnBrConnection *connection = ConnBrGetConnectionById(connectionId);
"br update connection ref: lock failed, connId=%{public}u, delta=%{public}d", connection->connectionId, delta); CONN_CHECK_AND_RETURN_RET_LOGE(
connection != NULL, SOFTBUS_INVALID_PARAM, CONN_BR, "conn not exist, id=%{public}u", connectionId);
if (SoftBusMutexLock(&connection->lock) != SOFTBUS_OK) {
CONN_LOGI(CONN_BR, "lock failed, connId=%{public}u, delta=%{public}d", connectionId, delta);
ConnBrReturnConnection(&connection);
return SOFTBUS_LOCK_ERR;
}
connection->connectionRc += delta; connection->connectionRc += delta;
int32_t localRc = connection->connectionRc; int32_t localRc = connection->connectionRc;
CONN_LOGI(CONN_BR, "connId=%{public}u, side=%{public}d, delta=%{public}d, newRef=%{public}d", CONN_LOGI(CONN_BR, "connId=%{public}u, side=%{public}d, delta=%{public}d, newRef=%{public}d", connectionId,
connection->connectionId, connection->side, delta, localRc); connection->side, delta, localRc);
if (localRc <= 0) { if (localRc <= 0) {
connection->state = BR_CONNECTION_STATE_NEGOTIATION_CLOSING; connection->state = BR_CONNECTION_STATE_NEGOTIATION_CLOSING;
ConnPostMsgToLooper(&g_brConnectionAsyncHandler, MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT, ConnPostMsgToLooper(&g_brConnectionAsyncHandler, MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT, connectionId,
connection->connectionId, 0, NULL, WAIT_BR_NEGOTIATION_CLOSING_TIMEOUT_MILLIS); 0, NULL, WAIT_BR_NEGOTIATION_CLOSING_TIMEOUT_MILLIS);
} }
(void)SoftBusMutexUnlock(&connection->lock); (void)SoftBusMutexUnlock(&connection->lock);
int32_t flag = delta >= 0 ? CONN_HIGH : CONN_LOW; int32_t flag = delta >= 0 ? CONN_HIGH : CONN_LOW;
BrCtlMessageSerializationContext ctx = { BrCtlMessageSerializationContext ctx = {
.connectionId = connection->connectionId, .connectionId = connectionId,
.flag = flag, .flag = flag,
.method = BR_METHOD_NOTIFY_REQUEST, .method = BR_METHOD_NOTIFY_REQUEST,
.referenceRequest = { .referenceRequest = {
@ -378,30 +409,64 @@ int32_t ConnBrUpdateConnectionRc(ConnBrConnection *connection, int32_t delta)
}, },
}; };
ConnEventExtra extra = { ConnEventExtra extra = {
.connectionId = (int32_t)connection->connectionId, .connectionId = (int32_t)connectionId,
.connRcDelta = delta, .connRcDelta = delta,
.connRc = localRc, .connRc = localRc,
.peerBrMac = connection->addr, .peerBrMac = connection->addr,
.linkType = CONNECT_BR .linkType = CONNECT_BR,
}; };
ConnBrReturnConnection(&connection);
uint8_t *data = NULL; uint8_t *data = NULL;
uint32_t dataLen = 0; uint32_t dataLen = 0;
int64_t seq = ConnBrPackCtlMessage(ctx, &data, &dataLen); int64_t seq = ConnBrPackCtlMessage(ctx, &data, &dataLen);
if (seq < 0) { if (seq < 0) {
CONN_LOGE(CONN_BR, CONN_LOGE(CONN_BR, "request message failed, connId=%{public}u, ret=%{public}d", connectionId, (int32_t)seq);
"connection request message failed, connectionId=%{public}u, ret=%{public}d",
connection->connectionId, (int32_t)seq);
extra.errcode = (int32_t)seq; extra.errcode = (int32_t)seq;
extra.result = EVENT_STAGE_RESULT_FAILED; extra.result = EVENT_STAGE_RESULT_FAILED;
CONN_EVENT(EVENT_SCENE_CONNECT, EVENT_STAGE_CONNECT_UPDATE_CONNECTION_RC, extra); CONN_EVENT(EVENT_SCENE_CONNECT, EVENT_STAGE_CONNECT_UPDATE_CONNECTION_RC, extra);
return (int32_t)seq; return (int32_t)seq;
} }
extra.errcode = ConnBrPostBytes(connection->connectionId, data, dataLen, 0, flag, MODULE_CONNECTION, seq); extra.errcode = ConnBrPostBytes(connectionId, data, dataLen, 0, flag, MODULE_CONNECTION, seq);
extra.result = extra.errcode == SOFTBUS_OK ? EVENT_STAGE_RESULT_OK : EVENT_STAGE_RESULT_FAILED; extra.result = extra.errcode == SOFTBUS_OK ? EVENT_STAGE_RESULT_OK : EVENT_STAGE_RESULT_FAILED;
CONN_EVENT(EVENT_SCENE_CONNECT, EVENT_STAGE_CONNECT_UPDATE_CONNECTION_RC, extra); CONN_EVENT(EVENT_SCENE_CONNECT, EVENT_STAGE_CONNECT_UPDATE_CONNECTION_RC, extra);
return extra.errcode; return extra.errcode;
} }
static int32_t BrUpdateConnectionRc(uint32_t connectionId, int32_t delta)
{
ConnBrConnection *connection = ConnBrGetConnectionById(connectionId);
CONN_CHECK_AND_RETURN_RET_LOGE(
connection != NULL, SOFTBUS_INVALID_PARAM, CONN_BR, "conn not exist, id=%{public}u", connectionId);
int32_t status = SoftBusMutexLock(&connection->lock);
if (status != SOFTBUS_OK) {
CONN_LOGE(CONN_BR, "lock failed, connectionId=%{public}u, error=%{public}d", connectionId, status);
ConnBrReturnConnection(&connection);
return SOFTBUS_LOCK_ERR;
}
bool isOccupied = connection->isOccupied;
(void)SoftBusMutexUnlock(&connection->lock);
ConnBrReturnConnection(&connection);
if (delta < 0 && isOccupied) {
CONN_LOGI(CONN_BR, "is occupied, process later, connId=%{public}u", connectionId);
status = ConnPostMsgToLooper(
&g_brConnectionAsyncHandler, MSG_CONNECTION_UPDATE_LOCAL_RC, connectionId, delta, NULL, WAIT_TIMEOUT_TRY_AGAIN);
if (status != SOFTBUS_OK) {
CONN_LOGE(CONN_BR, "post msg failed, connectionId=%{public}u, error=%{public}d", connectionId, status);
return status;
}
return SOFTBUS_OK;
}
return NotifyUpdateConnectionRc(connectionId, delta);
}
int32_t ConnBrUpdateConnectionRc(ConnBrConnection *connection, int32_t delta)
{
CONN_CHECK_AND_RETURN_RET_LOGE(connection, SOFTBUS_INVALID_PARAM, CONN_BR, "conn is null");
return BrUpdateConnectionRc(connection->connectionId, delta);
}
int32_t ConnBrDisconnectNow(ConnBrConnection *connection) int32_t ConnBrDisconnectNow(ConnBrConnection *connection)
{ {
CONN_CHECK_AND_RETURN_RET_LOGE(SoftBusMutexLock(&connection->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, CONN_BR, CONN_CHECK_AND_RETURN_RET_LOGE(SoftBusMutexLock(&connection->lock) == SOFTBUS_OK, SOFTBUS_LOCK_ERR, CONN_BR,
@ -437,9 +502,105 @@ static int32_t BrPostReplyMessage(uint32_t connectionId, int32_t localRc)
return ConnBrPostBytes(connectionId, data, dataLen, 0, flag, MODULE_CONNECTION, seq); return ConnBrPostBytes(connectionId, data, dataLen, 0, flag, MODULE_CONNECTION, seq);
} }
static int32_t NotifyReferenceRequest(uint32_t connectionId, int32_t delta, int32_t peerRc)
{
ConnBrConnection *connection = ConnBrGetConnectionById(connectionId);
CONN_CHECK_AND_RETURN_RET_LOGE(connection != NULL, SOFTBUS_INVALID_PARAM, CONN_BR, "conn not exist, id=%{public}u", connectionId);
int32_t status = SoftBusMutexLock(&connection->lock);
if (status != SOFTBUS_OK) {
CONN_LOGE(CONN_BR, "lock failed, connectionId=%{public}u, error=%{public}d", connectionId, status);
ConnBrReturnConnection(&connection);
return SOFTBUS_LOCK_ERR;
}
connection->connectionRc += delta;
int32_t localRc = connection->connectionRc;
CONN_LOGI(CONN_BR, "connId=%{public}u, delta=%{public}d, peerRc=%{public}d, localRc=%{public}d", connectionId,
delta, peerRc, localRc);
if (peerRc > 0) {
if (localRc == 0) {
ConnPostMsgToLooper(&g_brConnectionAsyncHandler, MSG_CONNECTION_RETRY_NOTIFY_REFERENCE, connectionId, 0,
NULL, RETRY_NOTIFY_REFERENCE_DELAY_MILLIS);
(void)SoftBusMutexUnlock(&connection->lock);
ConnBrReturnConnection(&connection);
return SOFTBUS_OK;
}
}
if (connection->state == BR_CONNECTION_STATE_NEGOTIATION_CLOSING) {
ConnRemoveMsgFromLooper(
&g_brConnectionAsyncHandler, MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT, connectionId, 0, NULL);
connection->state = BR_CONNECTION_STATE_CONNECTED;
g_eventListener.onConnectionResume(connectionId);
}
if (localRc <= 0) {
connection->state = BR_CONNECTION_STATE_CLOSING;
(void)SoftBusMutexUnlock(&connection->lock);
ConnBrDisconnectNow(connection);
ConnBrReturnConnection(&connection);
return SOFTBUS_OK;
}
(void)SoftBusMutexUnlock(&connection->lock);
ConnBrReturnConnection(&connection);
return BrPostReplyMessage(connectionId, localRc);
}
static void BrOnOccupyRelease(uint32_t connectionId)
{
ConnBrConnection *connection = ConnBrGetConnectionById(connectionId);
CONN_CHECK_AND_RETURN_LOGE(connection != NULL, CONN_BR, "conn not exist, id=%{public}u", connectionId);
int32_t status = SoftBusMutexLock(&connection->lock);
if (status != SOFTBUS_OK) {
CONN_LOGE(CONN_BR, "lock failed, connectionId=%{public}u, error=%{public}d", connectionId, status);
ConnBrReturnConnection(&connection);
return;
}
connection->isOccupied = false;
(void)SoftBusMutexUnlock(&connection->lock);
ConnBrReturnConnection(&connection);
}
static int32_t BrOnReferenceRequest(uint32_t connectionId, ReferenceCount *referenceCount)
{
int32_t delta = referenceCount->delta;
int32_t peerRc = referenceCount->peerRc;
ConnBrConnection *connection = ConnBrGetConnectionById(connectionId);
CONN_CHECK_AND_RETURN_RET_LOGE(connection != NULL,
SOFTBUS_INVALID_PARAM, CONN_BR, "conn not exist, id=%{public}u", connectionId);
int32_t status = SoftBusMutexLock(&connection->lock);
if (status != SOFTBUS_OK) {
CONN_LOGE(CONN_BR, "lock failed, connectionId=%{public}u, error=%{public}d", connectionId, status);
ConnBrReturnConnection(&connection);
return SOFTBUS_LOCK_ERR;
}
bool isOccupied = connection->isOccupied;
(void)SoftBusMutexUnlock(&connection->lock);
ConnBrReturnConnection(&connection);
if (delta < 0 && isOccupied) {
CONN_LOGI(CONN_BR, "is occupied, request process later, connectionId=%{public}u", connectionId);
ReferenceCount *referenceParam = (ReferenceCount *)SoftBusMalloc(sizeof(ReferenceCount));
if (referenceParam != NULL) {
CONN_LOGE(CONN_BR, "malloc buffer failed, connectionId=%{public}u", connectionId);
return SOFTBUS_MALLOC_ERR;
}
referenceParam->delta = delta;
referenceParam->peerRc = peerRc;
int32_t res = ConnPostMsgToLooper(&g_brConnectionAsyncHandler, MSG_CONNECTION_UPDATE_PEER_RC, connectionId, 0, referenceParam,
WAIT_TIMEOUT_TRY_AGAIN);
if (res != SOFTBUS_OK) {
CONN_LOGE(CONN_BR, "post msg failed, connectionId=%{public}u, error=%{public}d", connectionId, res);
SoftBusFree(referenceParam);
return status;
}
return SOFTBUS_OK;
}
return NotifyReferenceRequest(connectionId, delta, peerRc);
}
int32_t ConnBrOnReferenceRequest(ConnBrConnection *connection, const cJSON *json) int32_t ConnBrOnReferenceRequest(ConnBrConnection *connection, const cJSON *json)
{ {
CONN_CHECK_AND_RETURN_RET_LOGW(connection != NULL, SOFTBUS_INVALID_PARAM, CONN_BR, "invalid param");
int32_t delta = 0; int32_t delta = 0;
int32_t peerRc = 0; int32_t peerRc = 0;
if (!GetJsonObjectSignedNumberItem(json, KEY_DELTA, &delta) || if (!GetJsonObjectSignedNumberItem(json, KEY_DELTA, &delta) ||
@ -448,39 +609,11 @@ int32_t ConnBrOnReferenceRequest(ConnBrConnection *connection, const cJSON *json
connection->connectionId, delta, peerRc); connection->connectionId, delta, peerRc);
return SOFTBUS_PARSE_JSON_ERR; return SOFTBUS_PARSE_JSON_ERR;
} }
ReferenceCount referenceCount = {
int32_t status = SoftBusMutexLock(&connection->lock); .delta = delta,
if (status != SOFTBUS_OK) { .peerRc = peerRc,
CONN_LOGE(CONN_BR, "lock failed, connectionId=%{public}u, error=%{public}d", connection->connectionId, status); };
return SOFTBUS_LOCK_ERR; return BrOnReferenceRequest(connection->connectionId, &referenceCount);
}
connection->connectionRc += delta;
int32_t localRc = connection->connectionRc;
CONN_LOGI(CONN_BR, "connId=%{public}u, delta=%{public}d, peerRc=%{public}d, localRc=%{public}d",
connection->connectionId, delta, peerRc, localRc);
if (peerRc > 0) {
if (localRc == 0) {
ConnPostMsgToLooper(&g_brConnectionAsyncHandler, MSG_CONNECTION_RETRY_NOTIFY_REFERENCE,
connection->connectionId, 0, NULL, RETRY_NOTIFY_REFERENCE_DELAY_MILLIS);
(void)SoftBusMutexUnlock(&connection->lock);
return SOFTBUS_OK;
}
}
if (connection->state == BR_CONNECTION_STATE_NEGOTIATION_CLOSING) {
ConnRemoveMsgFromLooper(&g_brConnectionAsyncHandler, MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT,
connection->connectionId, 0, NULL);
connection->state = BR_CONNECTION_STATE_CONNECTED;
g_eventListener.onConnectionResume(connection->connectionId);
}
if (localRc <= 0) {
connection->state = BR_CONNECTION_STATE_CLOSING;
(void)SoftBusMutexUnlock(&connection->lock);
ConnBrDisconnectNow(connection);
return SOFTBUS_OK;
}
(void)SoftBusMutexUnlock(&connection->lock);
return BrPostReplyMessage(connection->connectionId, localRc);
} }
int32_t ConnBrOnReferenceResponse(ConnBrConnection *connection, const cJSON *json) int32_t ConnBrOnReferenceResponse(ConnBrConnection *connection, const cJSON *json)
@ -704,6 +837,15 @@ static void BrConnectionMsgHandler(SoftBusMessage *msg)
case MSG_CONNECTION_REPORT_CONNECT_EXCEPTION: case MSG_CONNECTION_REPORT_CONNECT_EXCEPTION:
ReportConnectExceptionHandler(msg->arg1, (int32_t)msg->arg2); ReportConnectExceptionHandler(msg->arg1, (int32_t)msg->arg2);
break; break;
case MSG_CONNECTION_OCCUPY_RELEASE:
BrOnOccupyRelease((uint32_t)msg->arg1);
break;
case MSG_CONNECTION_UPDATE_LOCAL_RC:
BrUpdateConnectionRc((uint32_t)msg->arg1, (int32_t)msg->arg2);
break;
case MSG_CONNECTION_UPDATE_PEER_RC:
BrOnReferenceRequest((uint32_t)msg->arg1, (ReferenceCount *)msg->obj);
break;
default: default:
CONN_LOGW(CONN_BR, "receive unexpected msg, what=%{public}d", msg->what); CONN_LOGW(CONN_BR, "receive unexpected msg, what=%{public}d", msg->what);
break; break;
@ -717,7 +859,10 @@ static int BrCompareConnectionLooperEventFunc(const SoftBusMessage *msg, void *a
return COMPARE_FAILED; return COMPARE_FAILED;
} }
switch (ctx->what) { switch (ctx->what) {
case MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT: { case MSG_CONNECTION_WAIT_NEGOTIATION_CLOSING_TIMEOUT:
case MSG_CONNECTION_UPDATE_PEER_RC:
case MSG_CONNECTION_OCCUPY_RELEASE:
case MSG_CONNECTION_UPDATE_LOCAL_RC: {
if (msg->arg1 == ctx->arg1) { if (msg->arg1 == ctx->arg1) {
return COMPARE_SUCCESS; return COMPARE_SUCCESS;
} }

View File

@ -1479,7 +1479,7 @@ static int32_t BrStopLocalListening(const LocalListenerInfo *info)
return ConnBrStopServer(); return ConnBrStopServer();
} }
static bool BrCheckActiveConnection(const ConnectOption *option) static bool BrCheckActiveConnection(const ConnectOption *option, bool needOccupy)
{ {
CONN_CHECK_AND_RETURN_RET_LOGW(option != NULL, false, CONN_BR, "BrCheckActiveConnection: option is null"); CONN_CHECK_AND_RETURN_RET_LOGW(option != NULL, false, CONN_BR, "BrCheckActiveConnection: option is null");
CONN_CHECK_AND_RETURN_RET_LOGW(option->type == CONNECT_BR, false, CONN_BR, CONN_CHECK_AND_RETURN_RET_LOGW(option->type == CONNECT_BR, false, CONN_BR,
@ -1489,6 +1489,9 @@ static bool BrCheckActiveConnection(const ConnectOption *option)
CONN_CHECK_AND_RETURN_RET_LOGW( CONN_CHECK_AND_RETURN_RET_LOGW(
connection != NULL, false, CONN_BR, "BrCheckActiveConnection: connection is not exist"); connection != NULL, false, CONN_BR, "BrCheckActiveConnection: connection is not exist");
bool isActive = (connection->state == BR_CONNECTION_STATE_CONNECTED); bool isActive = (connection->state == BR_CONNECTION_STATE_CONNECTED);
if (isActive && needOccupy) {
ConnBrOccupy(connection);
}
ConnBrReturnConnection(&connection); ConnBrReturnConnection(&connection);
return isActive; return isActive;
} }

View File

@ -394,7 +394,7 @@ int32_t ConnBleDirectConnectDevice(const ConnectOption *option, uint32_t request
*/ */
bool ConnBleDirectIsEnable(BleProtocolType protocol); bool ConnBleDirectIsEnable(BleProtocolType protocol);
bool CheckActiveConnection(const ConnectOption *option); bool CheckActiveConnection(const ConnectOption *option, bool needOccupy);
/** /**
* @ingroup Softbus_conn_manager * @ingroup Softbus_conn_manager

View File

@ -749,7 +749,7 @@ void ConnServerDeinit(void)
g_isInited = false; g_isInited = false;
} }
bool CheckActiveConnection(const ConnectOption *info) bool CheckActiveConnection(const ConnectOption *info, bool needOccupy)
{ {
if (info == NULL) { if (info == NULL) {
return false; return false;
@ -764,7 +764,7 @@ bool CheckActiveConnection(const ConnectOption *info)
return false; return false;
} }
return g_connManager[info->type]->CheckActiveConnection(info); return g_connManager[info->type]->CheckActiveConnection(info, needOccupy);
} }
int32_t ConnUpdateConnection(uint32_t connectionId, UpdateOption *option) int32_t ConnUpdateConnection(uint32_t connectionId, UpdateOption *option)

View File

@ -96,7 +96,7 @@ typedef struct {
* @return <b>SOFTBUS_OK</b> if local listeners start successfully. * @return <b>SOFTBUS_OK</b> if local listeners start successfully.
*/ */
int32_t (*StopLocalListening)(const LocalListenerInfo *info); int32_t (*StopLocalListening)(const LocalListenerInfo *info);
bool (*CheckActiveConnection)(const ConnectOption *info); bool (*CheckActiveConnection)(const ConnectOption *info, bool needOccupy);
int32_t (*UpdateConnection)(uint32_t connectionId, UpdateOption *option); int32_t (*UpdateConnection)(uint32_t connectionId, UpdateOption *option);
/** /**

View File

@ -746,9 +746,10 @@ static int32_t InitProperty(void)
return SOFTBUS_OK; return SOFTBUS_OK;
} }
static bool TcpCheckActiveConnection(const ConnectOption *info) static bool TcpCheckActiveConnection(const ConnectOption *info, bool needOccupy)
{ {
(void)info; (void)info;
(void)needOccupy;
return false; return false;
} }

View File

@ -80,7 +80,7 @@ public:
virtual int64_t GetAuthIdByConnInfo(const AuthConnInfo *connInfo) = 0; virtual int64_t GetAuthIdByConnInfo(const AuthConnInfo *connInfo) = 0;
virtual int32_t SoftBusGenerateStrHash(const unsigned char *str, uint32_t len, unsigned char *hash) = 0; virtual int32_t SoftBusGenerateStrHash(const unsigned char *str, uint32_t len, unsigned char *hash) = 0;
virtual int32_t StartBaseClient(ListenerModule module, const SoftbusBaseListener *listener) = 0; virtual int32_t StartBaseClient(ListenerModule module, const SoftbusBaseListener *listener) = 0;
virtual bool CheckActiveConnection(const ConnectOption *option) = 0; virtual bool CheckActiveConnection(const ConnectOption *option, bool needOccupy) = 0;
virtual int32_t ConnOpenClientSocket(const ConnectOption *option, const char *bindAddr, bool isNonBlock) = 0; virtual int32_t ConnOpenClientSocket(const ConnectOption *option, const char *bindAddr, bool isNonBlock) = 0;
virtual int32_t AddTrigger(ListenerModule module, int32_t fd, TriggerType trigger) = 0; virtual int32_t AddTrigger(ListenerModule module, int32_t fd, TriggerType trigger) = 0;
virtual int32_t QueryLaneResource(const LaneQueryInfo *queryInfo, const QosInfo *qosInfo) = 0; virtual int32_t QueryLaneResource(const LaneQueryInfo *queryInfo, const QosInfo *qosInfo) = 0;
@ -130,7 +130,7 @@ public:
MOCK_METHOD1(GetAuthIdByConnInfo, int64_t(const AuthConnInfo *)); MOCK_METHOD1(GetAuthIdByConnInfo, int64_t(const AuthConnInfo *));
MOCK_METHOD3(SoftBusGenerateStrHash, int32_t (const unsigned char *, uint32_t, unsigned char *)); MOCK_METHOD3(SoftBusGenerateStrHash, int32_t (const unsigned char *, uint32_t, unsigned char *));
MOCK_METHOD2(StartBaseClient, int32_t (ListenerModule module, const SoftbusBaseListener *listener)); MOCK_METHOD2(StartBaseClient, int32_t (ListenerModule module, const SoftbusBaseListener *listener));
MOCK_METHOD1(CheckActiveConnection, bool (const ConnectOption *)); MOCK_METHOD2(CheckActiveConnection, bool (const ConnectOption *, bool));
MOCK_METHOD3(ConnOpenClientSocket, int32_t (const ConnectOption *option, const char *bindAddr, bool isNonBlock)); MOCK_METHOD3(ConnOpenClientSocket, int32_t (const ConnectOption *option, const char *bindAddr, bool isNonBlock));
MOCK_METHOD3(AddTrigger, int32_t (ListenerModule module, int32_t fd, TriggerType trigger)); MOCK_METHOD3(AddTrigger, int32_t (ListenerModule module, int32_t fd, TriggerType trigger));
MOCK_METHOD2(QueryLaneResource, int32_t (const LaneQueryInfo *, const QosInfo *)); MOCK_METHOD2(QueryLaneResource, int32_t (const LaneQueryInfo *, const QosInfo *));

View File

@ -363,9 +363,9 @@ int32_t StartBaseClient(ListenerModule module, const SoftbusBaseListener *listen
return GetLaneDepsInterface()->StartBaseClient(module, listener); return GetLaneDepsInterface()->StartBaseClient(module, listener);
} }
bool CheckActiveConnection(const ConnectOption *option) bool CheckActiveConnection(const ConnectOption *option, bool needOccupy)
{ {
return GetLaneDepsInterface()->CheckActiveConnection(option); return GetLaneDepsInterface()->CheckActiveConnection(option, needOccupy);
} }
int32_t ConnOpenClientSocket(const ConnectOption *option, const char *bindAddr, bool isNonBlock) int32_t ConnOpenClientSocket(const ConnectOption *option, const char *bindAddr, bool isNonBlock)

View File

@ -39,7 +39,7 @@ public:
virtual int32_t ConnDisconnectDevice(uint32_t connectionId) = 0; virtual int32_t ConnDisconnectDevice(uint32_t connectionId) = 0;
virtual uint32_t ConnGetHeadSize(void) = 0; virtual uint32_t ConnGetHeadSize(void) = 0;
virtual int32_t ConnPostBytes(uint32_t connectionId, ConnPostData *data) = 0; virtual int32_t ConnPostBytes(uint32_t connectionId, ConnPostData *data) = 0;
virtual bool CheckActiveConnection(const ConnectOption *option) = 0; virtual bool CheckActiveConnection(const ConnectOption *option, bool needOccupy) = 0;
virtual int32_t ConnStartLocalListening(const LocalListenerInfo *info) = 0; virtual int32_t ConnStartLocalListening(const LocalListenerInfo *info) = 0;
virtual int32_t ConnStopLocalListening(const LocalListenerInfo *info) = 0; virtual int32_t ConnStopLocalListening(const LocalListenerInfo *info) = 0;
virtual uint32_t ConnGetNewRequestId(ConnModule moduleId) = 0; virtual uint32_t ConnGetNewRequestId(ConnModule moduleId) = 0;
@ -57,7 +57,7 @@ public:
MOCK_METHOD1(ConnDisconnectDevice, int32_t(uint32_t)); MOCK_METHOD1(ConnDisconnectDevice, int32_t(uint32_t));
MOCK_METHOD0(ConnGetHeadSize, uint32_t(void)); MOCK_METHOD0(ConnGetHeadSize, uint32_t(void));
MOCK_METHOD2(ConnPostBytes, int32_t(uint32_t, ConnPostData *)); MOCK_METHOD2(ConnPostBytes, int32_t(uint32_t, ConnPostData *));
MOCK_METHOD1(CheckActiveConnection, bool(const ConnectOption *)); MOCK_METHOD2(CheckActiveConnection, bool(const ConnectOption *, bool));
MOCK_METHOD1(ConnStartLocalListening, int32_t(const LocalListenerInfo *)); MOCK_METHOD1(ConnStartLocalListening, int32_t(const LocalListenerInfo *));
MOCK_METHOD1(ConnStopLocalListening, int32_t(const LocalListenerInfo *)); MOCK_METHOD1(ConnStopLocalListening, int32_t(const LocalListenerInfo *));
MOCK_METHOD1(ConnGetNewRequestId, uint32_t(ConnModule)); MOCK_METHOD1(ConnGetNewRequestId, uint32_t(ConnModule));

View File

@ -75,9 +75,9 @@ int32_t ConnPostBytes(uint32_t connectionId, ConnPostData *data)
return GetConnInterface()->ConnPostBytes(connectionId, data); return GetConnInterface()->ConnPostBytes(connectionId, data);
} }
bool CheckActiveConnection(const ConnectOption *option) bool CheckActiveConnection(const ConnectOption *option, bool needOccupy)
{ {
return GetConnInterface()->CheckActiveConnection(option); return GetConnInterface()->CheckActiveConnection(option, needOccupy);
} }
int32_t ConnStartLocalListening(const LocalListenerInfo *info) int32_t ConnStartLocalListening(const LocalListenerInfo *info)

View File

@ -537,7 +537,7 @@ HWTEST_F(ConnectionBleManagerTest, TestBleInterface005, TestSize.Level1)
EXPECT_EQ(ret, EOK); EXPECT_EQ(ret, EOK);
EXPECT_CALL(bleMock, LnnGetRemoteStrInfo).WillRepeatedly(Return(SOFTBUS_OK)); EXPECT_CALL(bleMock, LnnGetRemoteStrInfo).WillRepeatedly(Return(SOFTBUS_OK));
bool res = g_bleInterface->CheckActiveConnection(&option); bool res = g_bleInterface->CheckActiveConnection(&option, false);
EXPECT_EQ(res, true); EXPECT_EQ(res, true);
UpdateOption options = { UpdateOption options = {

View File

@ -700,7 +700,7 @@ HWTEST_F(ConnectionBleSwitchTest, testConnmanger0012, TestSize.Level1)
optionInfo.type = CONNECT_BLE; optionInfo.type = CONNECT_BLE;
optionInfo.bleOption.protocol = BLE_GATT; optionInfo.bleOption.protocol = BLE_GATT;
(void)memcpy_s(optionInfo.bleOption.bleMac, BT_MAC_LEN, TEST_BLE_MAC, BT_MAC_LEN); (void)memcpy_s(optionInfo.bleOption.bleMac, BT_MAC_LEN, TEST_BLE_MAC, BT_MAC_LEN);
bool isActive = CheckActiveConnection(&optionInfo); bool isActive = CheckActiveConnection(&optionInfo, false);
EXPECT_TRUE(isActive == false); EXPECT_TRUE(isActive == false);
}; };

View File

@ -554,21 +554,36 @@ HWTEST_F(ConnectionBrTest, testBrPendingPacket002, TestSize.Level1)
info.type = CONNECT_BR; info.type = CONNECT_BR;
(void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN); (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
printf("brMac: %s\n", info.brOption.brMac);
ret = ConnPreventConnection(&info, time); ret = ConnPreventConnection(&info, time);
EXPECT_EQ(SOFTBUS_OK, ret); EXPECT_EQ(SOFTBUS_OK, ret);
} }
HWTEST_F(ConnectionBrTest, testBrPendingPacket003, TestSize.Level1) HWTEST_F(ConnectionBrTest, CheckActiveConnection001, TestSize.Level1)
{ {
int ret;
ConnectOption info; ConnectOption info;
info.type = CONNECT_BR; info.type = CONNECT_BR;
info.brOption.sideType = CONN_SIDE_ANY; info.brOption.sideType = CONN_SIDE_ANY;
(void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN); (void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
printf("brMac: %s\n", info.brOption.brMac); int32_t ret = CheckActiveConnection(&info, false);
ret = CheckActiveConnection(&info); EXPECT_EQ(false, ret);
}
HWTEST_F(ConnectionBrTest, CheckActiveConnection002, TestSize.Level1)
{
ConnBrConnection *connection = (ConnBrConnection *)SoftBusCalloc(sizeof(ConnBrConnection));
ASSERT_NE(nullptr, connection);
int32_t ret = SoftBusMutexInit(&connection->lock, NULL);
ASSERT_EQ(EOK, ret);
ret = strcpy_s(connection->addr, BT_MAC_LEN, TEST_BR_MAC);
EXPECT_EQ(EOK, ret);
ret = ConnBrSaveConnection(connection);
EXPECT_EQ(EOK, ret);
ConnectOption info;
info.type = CONNECT_BR;
info.brOption.sideType = CONN_SIDE_ANY;
(void)memcpy_s(info.brOption.brMac, BT_MAC_LEN, TEST_BR_MAC, BT_MAC_LEN);
ret = CheckActiveConnection(&info, false);
EXPECT_EQ(false, ret); EXPECT_EQ(false, ret);
} }

View File

@ -41,7 +41,7 @@ public:
virtual int32_t ConnDisconnectDeviceAllConn(const ConnectOption *option) = 0; virtual int32_t ConnDisconnectDeviceAllConn(const ConnectOption *option) = 0;
virtual int32_t ConnStartLocalListening(const LocalListenerInfo *info) = 0; virtual int32_t ConnStartLocalListening(const LocalListenerInfo *info) = 0;
virtual int32_t ConnStopLocalListening(const LocalListenerInfo *info) = 0; virtual int32_t ConnStopLocalListening(const LocalListenerInfo *info) = 0;
virtual bool CheckActiveConnection(const ConnectOption *option) = 0; virtual bool CheckActiveConnection(const ConnectOption *option, bool needOccupy) = 0;
virtual int32_t ConnSetConnectCallback(ConnModule moduleId, const ConnectCallback *callback) = 0; virtual int32_t ConnSetConnectCallback(ConnModule moduleId, const ConnectCallback *callback) = 0;
virtual uint32_t ConnGetHeadSize(void) = 0; virtual uint32_t ConnGetHeadSize(void) = 0;
virtual void NipRecvDataFromBr(uint32_t connId, const char *buf) = 0; virtual void NipRecvDataFromBr(uint32_t connId, const char *buf) = 0;
@ -67,7 +67,7 @@ public:
MOCK_METHOD1(ConnDisconnectDeviceAllConn, int32_t (const ConnectOption *)); MOCK_METHOD1(ConnDisconnectDeviceAllConn, int32_t (const ConnectOption *));
MOCK_METHOD1(ConnStartLocalListening, int32_t (const LocalListenerInfo *)); MOCK_METHOD1(ConnStartLocalListening, int32_t (const LocalListenerInfo *));
MOCK_METHOD1(ConnStopLocalListening, int32_t (const LocalListenerInfo *)); MOCK_METHOD1(ConnStopLocalListening, int32_t (const LocalListenerInfo *));
MOCK_METHOD1(CheckActiveConnection, bool (const ConnectOption *)); MOCK_METHOD2(CheckActiveConnection, bool (const ConnectOption *, bool));
MOCK_METHOD0(ConnGetHeadSize, uint32_t (void)); MOCK_METHOD0(ConnGetHeadSize, uint32_t (void));
MOCK_METHOD2(ConnSetConnectCallback, int32_t (ConnModule, const ConnectCallback *)); MOCK_METHOD2(ConnSetConnectCallback, int32_t (ConnModule, const ConnectCallback *));

View File

@ -83,9 +83,9 @@ int32_t ConnStopLocalListening(const LocalListenerInfo *info)
return GetConnectInterface()->ConnStopLocalListening(info); return GetConnectInterface()->ConnStopLocalListening(info);
} }
bool CheckActiveConnection(const ConnectOption *option) bool CheckActiveConnection(const ConnectOption *option, bool needOccupy)
{ {
return GetConnectInterface()->CheckActiveConnection(option); return GetConnectInterface()->CheckActiveConnection(option, needOccupy);
} }
uint32_t ConnGetHeadSize(void) uint32_t ConnGetHeadSize(void)