!2275 bugfix: br send control message when closing and ble gatt server adapter do not cleanup callback when unregister

Merge pull request !2275 from songzhengteng/bugfix_gatts_cleanup
This commit is contained in:
openharmony_ci 2022-09-02 10:28:29 +00:00 committed by Gitee
commit 57791dcbf1
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 34 additions and 27 deletions

View File

@ -35,8 +35,8 @@ static const char SOFTBUS_APP_UUID[BT_UUID_LEN] = {
SoftBusGattsCallback *g_gattsCallback = NULL;
static BtGattServerCallbacks g_bleGattsHalCallback = {0};
static int g_halServerId = -1;
static int g_halRegFlag = -1; // -1:not registered or register failed; 0:registerring; 1:registered
static volatile int g_halServerId = -1;
static volatile int g_halRegFlag = -1; // -1:not registered or register failed; 0:registerring; 1:registered
int CheckGattsStatus(void)
{
@ -410,8 +410,7 @@ int SoftBusRegisterGattsCallbacks(SoftBusGattsCallback *callback)
return SOFTBUS_BLECONNECTION_REG_GATTS_CALLBACK_FAIL;
}
if (g_gattsCallback != NULL) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "SoftBusRegisterGattsCallbacks fail:callback exist");
return SOFTBUS_BLECONNECTION_REG_GATTS_CALLBACK_FAIL;
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_WARN, "SoftBusRegisterGattsCallbacks register again");
}
int ret = GattsRegisterHalCallback();
if (ret != SOFTBUS_OK) {
@ -423,8 +422,10 @@ int SoftBusRegisterGattsCallbacks(SoftBusGattsCallback *callback)
BtUuid uuid;
uuid.uuid = (char *)SOFTBUS_APP_UUID;
uuid.uuidLen = sizeof(SOFTBUS_APP_UUID);
g_halRegFlag = 0;
ret = BleGattsRegister(uuid);
if (ret != SOFTBUS_OK) {
g_halRegFlag = -1;
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleGattsRegister failed%d", ret);
return SOFTBUS_BLECONNECTION_REG_GATTS_CALLBACK_FAIL;
}
@ -447,6 +448,6 @@ void SoftBusUnRegisterGattsCallbacks(void)
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "BleGattsUnRegister error.");
return;
}
g_gattsCallback = NULL;
g_halServerId = -1;
g_halRegFlag = -1;
}

View File

@ -136,7 +136,7 @@ bool HasDiffMacDeviceExit(const ConnectOption *option);
int32_t GetBrConnStateByConnOption(const ConnectOption *option, uint32_t *outCountId, uint32_t *connectingReqId);
bool IsBrDeviceReady(uint32_t connId);
int32_t GetBrConnStateByConnectionId(uint32_t connId);
int32_t BrClosingByConnOption(const ConnectOption *option, int32_t *socketFd, int32_t *sideType);

View File

@ -138,8 +138,11 @@ static int32_t PostBytesInner(uint32_t connectionId, int32_t module, const char
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"PostBytesInner connectionId=%u,len=%u,module=%d", connectionId, len, module);
if (!IsBrDeviceReady(connectionId)) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "connectionId(%u) device is not ready", connectionId);
// control message need be sent when closing
int32_t state = GetBrConnStateByConnectionId(connectionId);
if (state != BR_CONNECTION_STATE_CLOSING && state != BR_CONNECTION_STATE_CONNECTED) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "connectionId(%u) device is not ready, state: %d",
connectionId, state);
SoftBusFree((void *)data);
return SOFTBUS_BRCONNECTION_POSTBYTES_ERROR;
}
@ -384,6 +387,8 @@ static void BrDisconnect(int32_t socketFd, int32_t value)
ListNode *itNext = NULL;
LIST_FOR_EACH_SAFE(it, itNext, &pendingList) {
RequestInfo *request = LIST_ENTRY(it, RequestInfo, node);
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "connection %u closed, restore connect request: %u",
connectionId, request->requestId);
ConnConnectDevice(&option, request->requestId, &request->callback);
ListDelete(&request->node);
SoftBusFree(request);
@ -611,8 +616,10 @@ static int32_t PostBytes(uint32_t connectionId, const char *data, int32_t len, i
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO,
"PostBytes connectionId=%u,pid=%d,len=%d flag=%d", connectionId, pid, len, flag);
if (!IsBrDeviceReady(connectionId)) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "connectionId(%u) device is not ready", connectionId);
int32_t state = GetBrConnStateByConnectionId(connectionId);
if (state != BR_CONNECTION_STATE_CONNECTED) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "connectionId(%u) device is not ready, state: %d",
connectionId, state);
SoftBusFree((void *)data);
return SOFTBUS_BRCONNECTION_POSTBYTES_ERROR;
}

View File

@ -552,6 +552,7 @@ int32_t ResumeConnection(uint32_t connId, ListNode *pendings)
(void)pthread_mutex_unlock(&g_connectionLock);
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_ERROR, "resume connection %u to 'BR_CONNECTION_STATE_CONNECTED'", connId);
target->state = BR_CONNECTION_STATE_CONNECTED;
ListNode *item = NULL;
ListNode *itemNext = NULL;
@ -633,26 +634,24 @@ int32_t GetBrConnStateByConnOption(const ConnectOption *option, uint32_t *outCon
return BR_CONNECTION_STATE_CLOSED;
}
bool IsBrDeviceReady(uint32_t connId)
int32_t GetBrConnStateByConnectionId(uint32_t connId)
{
(void)pthread_mutex_lock(&g_connectionLock);
int32_t state = -1;
BrConnectionInfo *target = NULL;
ListNode *item = NULL;
BrConnectionInfo *itemNode = NULL;
LIST_FOR_EACH(item, &g_connection_list) {
itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
if (itemNode->connectionId != connId) {
continue;
BrConnectionInfo *itemNode = LIST_ENTRY(item, BrConnectionInfo, node);
if (itemNode->connectionId == connId) {
target = itemNode;
break;
}
if (itemNode->state != BR_CONNECTION_STATE_CONNECTED) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "br state not connected, state: %d", itemNode->state);
(void)pthread_mutex_unlock(&g_connectionLock);
return false;
}
(void)pthread_mutex_unlock(&g_connectionLock);
return true;
}
if (target != NULL) {
state = target->state;
}
(void)pthread_mutex_unlock(&g_connectionLock);
return false;
return state;
}
int32_t BrClosingByConnOption(const ConnectOption *option, int32_t *socketFd, int32_t *sideType)

View File

@ -130,7 +130,7 @@ int32_t BrTransSend(const BrConnectionInfo *brConnInfo, const SppSocketDriver *s
if (brConnInfo == NULL) {
return SOFTBUS_ERR;
}
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "BrTransSend");
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "BrTransSend, %d", brConnInfo->connectionId);
int32_t socketFd = brConnInfo->socketFd;
if (socketFd == -1) {
return SOFTBUS_ERR;
@ -142,10 +142,10 @@ int32_t BrTransSend(const BrConnectionInfo *brConnInfo, const SppSocketDriver *s
while (tempLen > 0) {
(void)pthread_mutex_lock(&brConnInfo->lock);
while (brConnInfo->conGestState == BT_RFCOM_CONGEST_ON &&
brConnInfo->state == BR_CONNECTION_STATE_CONNECTED) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "wait congest");
(brConnInfo->state == BR_CONNECTION_STATE_CONNECTED || brConnInfo->state == BR_CONNECTION_STATE_CLOSING)) {
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "wait congest condition, %d", brConnInfo->connectionId);
pthread_cond_wait(&brConnInfo->congestCond, &brConnInfo->lock);
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "free congest");
SoftBusLog(SOFTBUS_LOG_CONN, SOFTBUS_LOG_INFO, "free congest condition, %d", brConnInfo->connectionId);
break;
}
(void)pthread_mutex_unlock(&brConnInfo->lock);