已合并
add reset proxy channel reconnect event #11416
add reset proxy channel reconnect event #11416
已合并
wangJE创建于 1月4日
8 个文件变更+187-64
@@ -179,7 +179,7 @@ static int32_t CloseAllBrProxy()
179 BrProxyInfo *nodeNext = NULL;179 BrProxyInfo *nodeNext = NULL;
180 LIST_FOR_EACH_ENTRY_SAFE(nodeInfo, nodeNext, &(g_proxyList->list), BrProxyInfo, node) {180 LIST_FOR_EACH_ENTRY_SAFE(nodeInfo, nodeNext, &(g_proxyList->list), BrProxyInfo, node) {
181 if (nodeInfo->isEnable && nodeInfo->channel.close != NULL) {181 if (nodeInfo->isEnable && nodeInfo->channel.close != NULL) {
182- nodeInfo->channel.close(&nodeInfo->channel);182+ nodeInfo->channel.close(&nodeInfo->channel, true);
183 }183 }
184 ListDelete(&nodeInfo->node);184 ListDelete(&nodeInfo->node);
185 SoftBusFree(nodeInfo);185 SoftBusFree(nodeInfo);
@@ -1004,7 +1004,7 @@ static int32_t GetChannelId(const char *mac, const char *uuid, int32_t *channelI
1004 continue;1004 continue;
1005 }1005 }
1006 if (nodeInfo->channel.close != NULL) {1006 if (nodeInfo->channel.close != NULL) {
1007- nodeInfo->channel.close(&nodeInfo->channel);1007+ nodeInfo->channel.close(&nodeInfo->channel, true);
1008 TRANS_LOGI(TRANS_SVC, "[br_proxy] appIndex:%{public}d close channel!", nodeInfo->appIndex);1008 TRANS_LOGI(TRANS_SVC, "[br_proxy] appIndex:%{public}d close channel!", nodeInfo->appIndex);
1009 }1009 }
1010 }1010 }
@@ -1080,7 +1080,7 @@ int32_t TransCloseBrProxy(int32_t channelId, bool isInnerCall)
1080 }1080 }
1081 }1081 }
1082 if (info.channel.close != NULL) {1082 if (info.channel.close != NULL) {
1083- info.channel.close(&info.channel);1083+ info.channel.close(&info.channel, true);
1084 TRANS_LOGE(TRANS_SVC, "[br_proxy] close channel");1084 TRANS_LOGE(TRANS_SVC, "[br_proxy] close channel");
1085 }1085 }
1086 TransBrProxyRemoveObject();1086 TransBrProxyRemoveObject();
@@ -28,6 +28,7 @@
28#define INNER_RECONNECT_RETRY_WAIT_MS 100028#define INNER_RECONNECT_RETRY_WAIT_MS 1000
29#define RECONNECT_AFTER_DISCONNECT_WAIT_MS 50029#define RECONNECT_AFTER_DISCONNECT_WAIT_MS 500
30#define OPEN_PROXY_CHANNEL_WAIT_MS 20030#define OPEN_PROXY_CHANNEL_WAIT_MS 200
31+#define ACL_WAIT_HFP_DELAY_MS (10 * 1000)
31 32 
32typedef struct {33typedef struct {
33 bool isSuccess;34 bool isSuccess;
@@ -150,34 +151,58 @@ int32_t ProxyChannelSend(struct ProxyChannel *channel, const uint8_t *data, uint
150 return ret;151 return ret;
151}152}
152 153 
153-static void ProxyChannelCloseHandler(char *brAddr)154+static void ResetReconnectEvent(uint32_t requestId, const char *brMac)
154{155{
156+ ConnRemoveMsgFromLooper(&g_proxyChannelAsyncHandler, MSG_OPEN_PROXY_CHANNEL, requestId, 0, NULL);
157+ 
158+ ProxyConnectInfo *connectingChannel = GetProxyChannelManager()->proxyChannelRequestInfo;
159+ if (connectingChannel != NULL && connectingChannel->requestId == requestId &&
160+ StrCmpIgnoreCase(brMac, connectingChannel->brMac) == 0) {
161+ CONN_LOGW(CONN_PROXY, "reset connectingChannel, reqId=%{public}u", requestId);
162+ DestoryProxyConnectInfo(&GetProxyChannelManager()->proxyChannelRequestInfo);
163+ }
164+}
165+ 
166+static void ProxyChannelCloseHandler(uint32_t requestId, char *brAddr)
167+{
168+ ResetReconnectEvent(requestId, brAddr);
155 RemoveReconnectDeviceInfoByAddrUnsafe(brAddr);169 RemoveReconnectDeviceInfoByAddrUnsafe(brAddr);
156}170}
157 171 
158-static void ProxyChannelClose(struct ProxyChannel *channel)172+static void AttemptPostChannelCloseEvent(struct ProxyChannel *channel, bool isClearReconnectEvent)
159{173{
160- CONN_CHECK_AND_RETURN_LOGE(channel != NULL, CONN_PROXY, "channel is null");174+ CONN_CHECK_AND_RETURN_LOGD(isClearReconnectEvent, CONN_PROXY, "no need clear reconnect event");
161 char *copyAddr = (char *)SoftBusCalloc(BT_MAC_MAX_LEN);175 char *copyAddr = (char *)SoftBusCalloc(BT_MAC_MAX_LEN);
162 if (copyAddr == NULL || strcpy_s(copyAddr, BT_MAC_MAX_LEN, channel->brMac) != EOK) {176 if (copyAddr == NULL || strcpy_s(copyAddr, BT_MAC_MAX_LEN, channel->brMac) != EOK) {
163 CONN_LOGE(CONN_PROXY, "copyAddr fail");177 CONN_LOGE(CONN_PROXY, "copyAddr fail");
164 SoftBusFree(copyAddr);178 SoftBusFree(copyAddr);
165 return;179 return;
166 }180 }
167- int32_t ret = ConnPostMsgToLooper(&g_proxyChannelAsyncHandler, MSG_CLOSE_PROXY_CHANNEL, 0, 0, copyAddr, 0);181+ int32_t ret = ConnPostMsgToLooper(&g_proxyChannelAsyncHandler, MSG_CLOSE_PROXY_CHANNEL,
182+ channel->requestId, 0, copyAddr, 0);
168 if (ret < 0) {183 if (ret < 0) {
169 // fall-through184 // fall-through
S

这里的fall-throut是多余的,改了代码没有删除。

likedislike
170 CONN_LOGE(CONN_PROXY, "send msg fail, error=%{public}d", ret);185 CONN_LOGE(CONN_PROXY, "send msg fail, error=%{public}d", ret);
171 SoftBusFree(copyAddr);186 SoftBusFree(copyAddr);
172 }187 }
188+}
189+ 
190+static void ProxyChannelClose(struct ProxyChannel *channel, bool isClearReconnectEvent)
191+{
192+ CONN_CHECK_AND_RETURN_LOGE(channel != NULL, CONN_PROXY, "channel is null");
193+ AttemptPostChannelCloseEvent(channel, isClearReconnectEvent);
173 194 
174 struct ProxyConnection *proxyConnection = GetProxyChannelByChannelId(channel->channelId);195 struct ProxyConnection *proxyConnection = GetProxyChannelByChannelId(channel->channelId);
175 CONN_CHECK_AND_RETURN_LOGE(proxyConnection != NULL, CONN_PROXY,196 CONN_CHECK_AND_RETURN_LOGE(proxyConnection != NULL, CONN_PROXY,
176 "get proxyConnection fail, channelId=%{public}u", channel->channelId);197 "get proxyConnection fail, channelId=%{public}u", channel->channelId);
177 SetProxyChannelState(proxyConnection, PROXY_CHANNEL_DISCONNECTING);198 SetProxyChannelState(proxyConnection, PROXY_CHANNEL_DISCONNECTING);
178- ret = GetProxyBrConnectionManager()->disconnect(proxyConnection);199+ int32_t ret = GetProxyBrConnectionManager()->disconnect(proxyConnection);
179- CONN_LOGW(CONN_PROXY, "close proxy channel=%{public}u, error=%{public}d", channel->channelId, ret);200+ CONN_LOGW(CONN_PROXY, "channel=%{public}u, reqId=%{public}u, error=%{public}d, isClear=%{public}d",
201+ channel->channelId, channel->requestId, ret, isClearReconnectEvent);
180 proxyConnection->dereference(proxyConnection);202 proxyConnection->dereference(proxyConnection);
203+#define WAIT_CLOSE_END_TIME_MS 1000
204+ // add 10 ms after close, because of the remote device will refresh service after disconnected
205+ SoftBusSleepMs(WAIT_CLOSE_END_TIME_MS);
181}206}
182 207 
183static struct ProxyConnection *CreateProxyConnection(ProxyConnectInfo *connectInfo)208static struct ProxyConnection *CreateProxyConnection(ProxyConnectInfo *connectInfo)
@@ -263,6 +288,21 @@ static void DestoryProxyConnection(struct ProxyConnection *proxyConnection)
263 SoftBusFree(proxyConnection);288 SoftBusFree(proxyConnection);
264}289}
265 290 
291+static void PostEventByAddr(enum BrProxyLooperMsgType msgType, const char *brMac, uint64_t dalayTimeMs)
292+{
293+ char *copyAddr = (char *)SoftBusCalloc(BT_MAC_LEN);
294+ if (copyAddr == NULL || strcpy_s(copyAddr, BT_MAC_LEN, brMac) != EOK) {
295+ CONN_LOGE(CONN_PROXY, "copyAddr fail");
296+ SoftBusFree(copyAddr);
297+ return;
298+ }
299+ int32_t ret = ConnPostMsgToLooper(&g_proxyChannelAsyncHandler, msgType, 0, 0, copyAddr, dalayTimeMs);
300+ if (ret != SOFTBUS_OK) {
301+ CONN_LOGE(CONN_PROXY, "post msg err");
302+ SoftBusFree(copyAddr);
303+ }
304+}
305+ 
266static void ProcessConnectFailed(ProxyConnectInfo *connectingChannel, const char *brMac, int32_t reason)306static void ProcessConnectFailed(ProxyConnectInfo *connectingChannel, const char *brMac, int32_t reason)
267{307{
268 CONN_LOGE(CONN_PROXY, "notify open fail reqId=%{public}u, reason=%{public}d", connectingChannel->requestId, reason);308 CONN_LOGE(CONN_PROXY, "notify open fail reqId=%{public}u, reason=%{public}d", connectingChannel->requestId, reason);
@@ -274,17 +314,7 @@ static void ProcessConnectFailed(ProxyConnectInfo *connectingChannel, const char
274 314 
275 // inner request retry connect after fail315 // inner request retry connect after fail
276 CONN_LOGI(CONN_PROXY, "inner reconnect fail, retry reqId=%{public}u", requestId);316 CONN_LOGI(CONN_PROXY, "inner reconnect fail, retry reqId=%{public}u", requestId);
277- char *copyAddr = (char *)SoftBusCalloc(BT_MAC_LEN);317+ PostEventByAddr(MSG_OPEN_PROXY_CHANNEL_RETRY, brMac, 0);
278- if (copyAddr == NULL || strcpy_s(copyAddr, BT_MAC_LEN, brMac) != EOK) {
279- CONN_LOGE(CONN_PROXY, "copyAddr fail");
280- SoftBusFree(copyAddr);
281- return;
282- }
283- int32_t ret = ConnPostMsgToLooper(&g_proxyChannelAsyncHandler, MSG_OPEN_PROXY_CHANNEL_RETRY, 0, 0, copyAddr, 0);
284- if (ret != SOFTBUS_OK) {
285- CONN_LOGE(CONN_PROXY, "post msg err");
286- SoftBusFree(copyAddr);
287- }
288}318}
289 319 
290static void NotifyOpenProxyChannelResult(struct ProxyConnection *proxyConnection, bool isSuccess, int32_t status)320static void NotifyOpenProxyChannelResult(struct ProxyConnection *proxyConnection, bool isSuccess, int32_t status)
@@ -497,6 +527,8 @@ static void AddReconnectDeviceInfoUnsafe(ProxyConnectInfo *connectInfo)
497 CONN_CHECK_AND_RETURN_LOGE(info != NULL, CONN_PROXY, "CopyProxyConnectInfo fail");527 CONN_CHECK_AND_RETURN_LOGE(info != NULL, CONN_PROXY, "CopyProxyConnectInfo fail");
498 info->isAclConnected = true;528 info->isAclConnected = true;
499 info->innerRetryNum = 0;529 info->innerRetryNum = 0;
530+ (void)IsPairedDevice(info->brMac, true, &info->isSupportHfp);
531+ CONN_LOGI(CONN_PROXY, "isSupportHfp=%{public}d", info->isSupportHfp);
500 ListAdd(&GetProxyChannelManager()->reconnectDeviceInfos, &info->node);532 ListAdd(&GetProxyChannelManager()->reconnectDeviceInfos, &info->node);
501}533}
502 534 
@@ -531,6 +563,10 @@ static void RemoveReconnectDeviceInfoByAddrUnsafe(const char *addr)
531 563 
532static void OpenProxyChannelHandler(ProxyConnectInfo *connectInfo)564static void OpenProxyChannelHandler(ProxyConnectInfo *connectInfo)
533{565{
566+ ProxyConnectInfo *reconnectDeviceInfo = GetReconnectDeviceInfoByAddrUnsafe(connectInfo->brMac);
567+ if (!connectInfo->isInnerRequest && reconnectDeviceInfo != NULL) {
568+ reconnectDeviceInfo->requestId = connectInfo->requestId;
569+ }
534 if (IsNeedReuseOrWait(connectInfo)) {570 if (IsNeedReuseOrWait(connectInfo)) {
535 return;571 return;
536 }572 }
@@ -656,7 +692,7 @@ static int32_t OpenProxyChannel(ProxyChannelParam *param, const OpenProxyChannel
656 CONN_CHECK_AND_RETURN_RET_LOGE(SoftBusGetBrState() == BR_ENABLE, SOFTBUS_CONN_BR_DISABLE_ERR,692 CONN_CHECK_AND_RETURN_RET_LOGE(SoftBusGetBrState() == BR_ENABLE, SOFTBUS_CONN_BR_DISABLE_ERR,
657 CONN_PROXY, "br disable");693 CONN_PROXY, "br disable");
658 bool isRealMac = IsRealMac(param->brMac);694 bool isRealMac = IsRealMac(param->brMac);
659- bool isPairedDevice = IsPairedDevice(param->brMac, isRealMac);695+ bool isPairedDevice = IsPairedDevice(param->brMac, isRealMac, NULL);
660 CONN_CHECK_AND_RETURN_RET_LOGE(isPairedDevice, SOFTBUS_CONN_BR_UNPAIRED,696 CONN_CHECK_AND_RETURN_RET_LOGE(isPairedDevice, SOFTBUS_CONN_BR_UNPAIRED,
661 CONN_PROXY, "is not paired device");697 CONN_PROXY, "is not paired device");
662 char anomizeAddress[BT_MAC_MAX_LEN] = { 0 };698 char anomizeAddress[BT_MAC_MAX_LEN] = { 0 };
@@ -705,25 +741,13 @@ static void ProxyChannelDisconnectHandler(ProxyChannelNotifyContext *ctx)
705 struct ProxyConnection *proxyConnection = GetProxyChannelByChannelId(channelId);741 struct ProxyConnection *proxyConnection = GetProxyChannelByChannelId(channelId);
706 CONN_CHECK_AND_RETURN_LOGE(proxyConnection != NULL, CONN_PROXY,742 CONN_CHECK_AND_RETURN_LOGE(proxyConnection != NULL, CONN_PROXY,
707 "get proxyConnection fail, channelId=%{public}u", channelId);743 "get proxyConnection fail, channelId=%{public}u", channelId);
708- CONN_LOGE(CONN_PROXY, "channelId=%{public}u, disconnected err=%{public}d", channelId, reason);744+ CONN_LOGE(CONN_PROXY, "reqId=%{public}u, channelId=%{public}u, err=%{public}d",
745+ proxyConnection->proxyChannel.requestId, channelId, reason);
709 746 
710 SetProxyChannelState(proxyConnection, PROXY_CHANNEL_DISCONNECTED);747 SetProxyChannelState(proxyConnection, PROXY_CHANNEL_DISCONNECTED);
711 NotifyDisconnected(&proxyConnection->proxyChannel, reason);748 NotifyDisconnected(&proxyConnection->proxyChannel, reason);
712 RemoveProxyChannelByChannelId(channelId);749 RemoveProxyChannelByChannelId(channelId);
713- char *copyAddr = (char *)SoftBusCalloc(BT_MAC_LEN);750+ PostEventByAddr(MSG_OPEN_PROXY_CHANNEL_RETRY, proxyConnection->brMac, RECONNECT_AFTER_DISCONNECT_WAIT_MS);
714- if (copyAddr == NULL || strcpy_s(copyAddr, BT_MAC_LEN, proxyConnection->brMac) != EOK) {
715- CONN_LOGE(CONN_PROXY, "copyAddr fail");
716- SoftBusFree(copyAddr);
717- proxyConnection->dereference(proxyConnection);
718- return;
719- }
720- int32_t ret = ConnPostMsgToLooper(&g_proxyChannelAsyncHandler, MSG_OPEN_PROXY_CHANNEL_RETRY,
721- 0, 0, copyAddr, RECONNECT_AFTER_DISCONNECT_WAIT_MS);
722- if (ret != SOFTBUS_OK) {
723- CONN_LOGE(CONN_PROXY, "post msg err");
724- SoftBusFree(copyAddr);
725- }
726- proxyConnection->dereference(proxyConnection);
727}751}
728 752 
729static void OnProxyChannelDisconnected(uint32_t channelId, int32_t reason)753static void OnProxyChannelDisconnected(uint32_t channelId, int32_t reason)
@@ -755,6 +779,8 @@ static int32_t RegisterProxyChannelListener(ProxyConnectListener *listener)
755static void OnInnerReConnectSuccess(uint32_t requestId, struct ProxyChannel *channel)779static void OnInnerReConnectSuccess(uint32_t requestId, struct ProxyChannel *channel)
756{780{
757 CONN_CHECK_AND_RETURN_LOGE(g_listener.onProxyChannelReconnected != NULL, CONN_PROXY, "Reconnected is NULL");781 CONN_CHECK_AND_RETURN_LOGE(g_listener.onProxyChannelReconnected != NULL, CONN_PROXY, "Reconnected is NULL");
782+ ResetReconnectEvent(requestId, channel->brMac);
783+ CONN_LOGI(CONN_PROXY, "reqId=%{public}u, channelId=%{public}u", requestId, channel->channelId);
758 // notify upper reconnect success784 // notify upper reconnect success
759 g_listener.onProxyChannelReconnected(channel->brMac, channel);785 g_listener.onProxyChannelReconnected(channel->brMac, channel);
760}786}
@@ -782,7 +808,7 @@ static bool CheckNeedToRetry(char *brAddr, ProxyConnectInfo *reconnectDeviceInfo
782 bool isAlreadyConnected = IsTargetDeviceAlreadyConnected(brAddr);808 bool isAlreadyConnected = IsTargetDeviceAlreadyConnected(brAddr);
783 CONN_CHECK_AND_RETURN_RET_LOGW(!isAlreadyConnected, false, CONN_PROXY, "exist already connection");809 CONN_CHECK_AND_RETURN_RET_LOGW(!isAlreadyConnected, false, CONN_PROXY, "exist already connection");
784 810 
785- bool isPairedDevice = IsPairedDevice(brAddr, true);811+ bool isPairedDevice = IsPairedDevice(brAddr, true, NULL);
786 CONN_CHECK_AND_RETURN_RET_LOGE(isPairedDevice, false, CONN_PROXY, "is not paired device");812 CONN_CHECK_AND_RETURN_RET_LOGE(isPairedDevice, false, CONN_PROXY, "is not paired device");
787 return true;813 return true;
788}814}
@@ -810,7 +836,8 @@ static void AttemptReconnectDevice(char *brAddr)
810 NotifyDisconnected(&proxyChannel, SOFTBUS_CONN_PROXY_RETRY_FAILED);836 NotifyDisconnected(&proxyChannel, SOFTBUS_CONN_PROXY_RETRY_FAILED);
811 return;837 return;
812 }838 }
813- 839+ ConnRemoveMsgFromLooper(&g_proxyChannelAsyncHandler, MSG_OPEN_PROXY_CHANNEL,
840+ reconnectDeviceInfo->requestId, 0, NULL);
814 ProxyConnectInfo *proxyChannelRequestInfo = CopyProxyConnectInfo(reconnectDeviceInfo);841 ProxyConnectInfo *proxyChannelRequestInfo = CopyProxyConnectInfo(reconnectDeviceInfo);
815 CONN_CHECK_AND_RETURN_LOGW(proxyChannelRequestInfo != NULL, CONN_PROXY, "CopyProxyConnectInfo fail");842 CONN_CHECK_AND_RETURN_LOGW(proxyChannelRequestInfo != NULL, CONN_PROXY, "CopyProxyConnectInfo fail");
816 uint64_t delayMillis = g_retryIntervalMillis[innerRetryNum];843 uint64_t delayMillis = g_retryIntervalMillis[innerRetryNum];
@@ -834,6 +861,14 @@ static void AclStateChangedHandler(ProxyChannelAclStateContext *context)
834 ProxyConnectInfo *reconnectDeviceInfo = GetReconnectDeviceInfoByAddrUnsafe(context->brMac);861 ProxyConnectInfo *reconnectDeviceInfo = GetReconnectDeviceInfoByAddrUnsafe(context->brMac);
835 CONN_CHECK_AND_RETURN_LOGW(reconnectDeviceInfo != NULL, CONN_PROXY, "no reconnect device");862 CONN_CHECK_AND_RETURN_LOGW(reconnectDeviceInfo != NULL, CONN_PROXY, "no reconnect device");
836 reconnectDeviceInfo->isAclConnected = (context->state == SOFTBUS_ACL_STATE_CONNECTED) ? true : false;863 reconnectDeviceInfo->isAclConnected = (context->state == SOFTBUS_ACL_STATE_CONNECTED) ? true : false;
864+ if (!reconnectDeviceInfo->isAclConnected) {
865+ ConnRemoveMsgFromLooper(&g_proxyChannelAsyncHandler, MSG_OPEN_PROXY_CHANNEL_RETRY,
866+ 0, 0, reconnectDeviceInfo->brMac);
867+ return;
868+ }
869+ CONN_LOGI(CONN_PROXY, "isSupportHfp=%{public}d", reconnectDeviceInfo->isSupportHfp);
870+ uint64_t delayMillis = reconnectDeviceInfo->isSupportHfp ? ACL_WAIT_HFP_DELAY_MS : 0;
871+ PostEventByAddr(MSG_OPEN_PROXY_CHANNEL_RETRY, reconnectDeviceInfo->brMac, delayMillis);
837}872}
838 873 
839static void ProxyResetHandler(void)874static void ProxyResetHandler(void)
@@ -883,13 +918,18 @@ static void ProxyDeviceUnpaired(const char *brAddr)
883 918 
884 ProxyConnectInfo *target = GetReconnectDeviceInfoByAddrUnsafe(brAddr);919 ProxyConnectInfo *target = GetReconnectDeviceInfoByAddrUnsafe(brAddr);
885 CONN_CHECK_AND_RETURN_LOGE(target != NULL, CONN_PROXY, "ignore addr=%{public}s", anomizeAddress);920 CONN_CHECK_AND_RETURN_LOGE(target != NULL, CONN_PROXY, "ignore addr=%{public}s", anomizeAddress);
886- RemoveReconnectDeviceInfoByAddrUnsafe(brAddr);
887 struct ProxyChannel proxyChannel = { 0 };921 struct ProxyChannel proxyChannel = { 0 };
922+ proxyChannel.requestId = target->requestId;
888 int32_t ret = target->isRealMac ? strcpy_s(proxyChannel.brMac, BT_MAC_MAX_LEN, target->brMac) :923 int32_t ret = target->isRealMac ? strcpy_s(proxyChannel.brMac, BT_MAC_MAX_LEN, target->brMac) :
889 strcpy_s(proxyChannel.brMac, BT_MAC_MAX_LEN, target->brHashMac);924 strcpy_s(proxyChannel.brMac, BT_MAC_MAX_LEN, target->brHashMac);
890- CONN_CHECK_AND_RETURN_LOGE(ret == EOK, CONN_PROXY, "cpy mac err");925+ if (ret != EOK) {
926+ CONN_LOGW(CONN_PROXY, "cpy brMac err!");
927+ }
891 ret = strcpy_s(proxyChannel.uuid, UUID_STRING_LEN, target->uuid);928 ret = strcpy_s(proxyChannel.uuid, UUID_STRING_LEN, target->uuid);
892- CONN_CHECK_AND_RETURN_LOGE(ret == EOK, CONN_PROXY, "cpy uuid err");929+ if (ret != EOK) {
930+ CONN_LOGW(CONN_PROXY, "cpy uuid err!");
931+ }
932+ RemoveReconnectDeviceInfoByAddrUnsafe(brAddr);
893 NotifyDisconnected(&proxyChannel, SOFTBUS_CONN_BR_UNPAIRED);933 NotifyDisconnected(&proxyChannel, SOFTBUS_CONN_BR_UNPAIRED);
894}934}
895 935 
@@ -926,21 +966,13 @@ static void OnObserverStateChanged(const char *addr, int32_t state)
926 CONN_PROXY, "ignore state=%{public}d", state);966 CONN_PROXY, "ignore state=%{public}d", state);
927 CONN_LOGI(CONN_PROXY, "state=%{public}d", state);967 CONN_LOGI(CONN_PROXY, "state=%{public}d", state);
928 CONN_CHECK_AND_RETURN_LOGW(addr != NULL, CONN_PROXY, "addr is NULL");968 CONN_CHECK_AND_RETURN_LOGW(addr != NULL, CONN_PROXY, "addr is NULL");
929- char *copyAddr = (char *)SoftBusCalloc(BT_MAC_LEN);969+ if (state == SOFTBUS_HFP_CONNECTED) {
930- if (copyAddr == NULL || strncpy_s(copyAddr, BT_MAC_LEN, addr, BT_MAC_LEN - 1) != EOK) {970+ ConnRemoveMsgFromLooper(&g_proxyChannelAsyncHandler, MSG_OPEN_PROXY_CHANNEL_RETRY, 0, 0, (void *)addr);
931- CONN_LOGE(CONN_PROXY, "copyAddr fail");
932- SoftBusFree(copyAddr);
933- return;
934 }971 }
935 972 
936 enum BrProxyLooperMsgType msgType = state == SOFTBUS_HFP_CONNECTED ?973 enum BrProxyLooperMsgType msgType = state == SOFTBUS_HFP_CONNECTED ?
937 MSG_OPEN_PROXY_CHANNEL_RETRY : MSG_PROXY_UNPAIRED;974 MSG_OPEN_PROXY_CHANNEL_RETRY : MSG_PROXY_UNPAIRED;
938- int32_t ret = ConnPostMsgToLooper(&g_proxyChannelAsyncHandler, msgType, 0, 0, copyAddr, 0);975+ PostEventByAddr(msgType, addr, 0);
939- if (ret != SOFTBUS_OK) {
940- CONN_LOGE(CONN_PROXY, "post msg fail, error=%{public}d", ret);
941- SoftBusFree(copyAddr);
942- return;
943- }
944}976}
945 977 
946static void OnProxyBtStateChanged(int listenerId, int state)978static void OnProxyBtStateChanged(int listenerId, int state)
@@ -972,7 +1004,7 @@ static void ProxyChannelMsgHandler(SoftBusMessage *msg)
972 break;1004 break;
973 case MSG_CLOSE_PROXY_CHANNEL:1005 case MSG_CLOSE_PROXY_CHANNEL:
974 CONN_CHECK_AND_RETURN_LOGW(msg->obj != NULL, CONN_PROXY, "msg->obj is NULL");1006 CONN_CHECK_AND_RETURN_LOGW(msg->obj != NULL, CONN_PROXY, "msg->obj is NULL");
975- ProxyChannelCloseHandler((char *)msg->obj);1007+ ProxyChannelCloseHandler(msg->arg1, (char *)msg->obj);
976 break;1008 break;
977 case MSG_CLOSE_PROXY_DISCONNECT:1009 case MSG_CLOSE_PROXY_DISCONNECT:
978 CONN_CHECK_AND_RETURN_LOGW(msg->obj != NULL, CONN_PROXY, "msg->obj is NULL");1010 CONN_CHECK_AND_RETURN_LOGW(msg->obj != NULL, CONN_PROXY, "msg->obj is NULL");
@@ -1008,6 +1040,20 @@ static int ProxyChannelLooperEventFunc(const SoftBusMessage *msg, void *args)
1008 }1040 }
1009 return COMPARE_FAILED;1041 return COMPARE_FAILED;
1010 }1042 }
1043+ case MSG_OPEN_PROXY_CHANNEL: {
1044+ if (msg->obj == NULL) {
1045+ return COMPARE_FAILED;
1046+ }
1047+ ProxyConnectInfo *info = (ProxyConnectInfo *)msg->obj;
1048+ return (uint64_t)(info->requestId) == ctx->arg1 ? COMPARE_SUCCESS : COMPARE_FAILED;
1049+ }
1050+ case MSG_OPEN_PROXY_CHANNEL_RETRY: {
1051+ if (msg->obj == NULL || ctx->obj == NULL) {
1052+ return COMPARE_FAILED;
1053+ }
1054+ bool isSameDevice = (StrCmpIgnoreCase((char *)msg->obj, (char *)ctx->obj) == 0);
1055+ return isSameDevice ? COMPARE_SUCCESS : COMPARE_FAILED;
1056+ }
1011 default:1057 default:
1012 break;1058 break;
1013 }1059 }
@@ -50,7 +50,7 @@ struct ProxyChannel {
50 char brMac[BT_MAC_MAX_LEN];50 char brMac[BT_MAC_MAX_LEN];
51 char uuid[UUID_STRING_LEN];51 char uuid[UUID_STRING_LEN];
52 int32_t (*send)(struct ProxyChannel *channel, const uint8_t *data, uint32_t dataLen);52 int32_t (*send)(struct ProxyChannel *channel, const uint8_t *data, uint32_t dataLen);
53- void (*close)(struct ProxyChannel *channel);53+ void (*close)(struct ProxyChannel *channel, bool isClearReconnectEvent);
54};54};
55 55 
56struct ProxyConnection {56struct ProxyConnection {
@@ -88,6 +88,7 @@ typedef struct {
88 uint64_t timeoutMs;88 uint64_t timeoutMs;
89 OpenProxyChannelCallback result;89 OpenProxyChannelCallback result;
90 bool isAclConnected;90 bool isAclConnected;
91+ bool isSupportHfp;
91 ListNode node;92 ListNode node;
92} ProxyConnectInfo;93} ProxyConnectInfo;
93 94 
@@ -123,7 +123,29 @@ static std::string ConvertRealMacToHashMac(const std::string addr)
123 return std::string(hashAddrStr).substr(SHA_256_HASH_LEN, SHA_256_HASH_LEN);123 return std::string(hashAddrStr).substr(SHA_256_HASH_LEN, SHA_256_HASH_LEN);
124}124}
125 125 
126-bool IsPairedDevice(const char *addr, bool isRealMac)126+static bool CompareIgnoreCase(const std::string &left, const std::string &right)
127+{
128+ std::string leftLower = left;
129+ std::transform(left.begin(), left.end(), leftLower.begin(), ::tolower);
130+ 
131+ std::string rightLower = right;
132+ std::transform(right.begin(), right.end(), rightLower.begin(), ::tolower);
133+ return leftLower.compare(rightLower) == 0;
134+}
135+ 
136+static bool IsSupportHfp(const OHOS::Bluetooth::BluetoothRemoteDevice& device)
137+{
138+ std::vector<std::string> uuids;
139+ device.GetDeviceUuids(uuids);
140+ for (const std::string& uuid : uuids) {
141+ if (CompareIgnoreCase(uuid, OHOS::Bluetooth::BLUETOOTH_UUID_HFP_HF)) {
142+ return true;
143+ }
144+ }
145+ return false;
146+}
147+ 
148+bool IsPairedDevice(const char *addr, bool isRealMac, bool *isSupportHfp)
127{149{
128 CONN_CHECK_AND_RETURN_RET_LOGE(addr != nullptr, false, CONN_PROXY, "addr is null");150 CONN_CHECK_AND_RETURN_RET_LOGE(addr != nullptr, false, CONN_PROXY, "addr is null");
129 std::vector<OHOS::Bluetooth::BluetoothRemoteDevice> remoteDeviceLists;151 std::vector<OHOS::Bluetooth::BluetoothRemoteDevice> remoteDeviceLists;
@@ -137,6 +159,9 @@ bool IsPairedDevice(const char *addr, bool isRealMac)
137 if (state == OHOS::Bluetooth::PAIR_PAIRED &&159 if (state == OHOS::Bluetooth::PAIR_PAIRED &&
138 ((isRealMac && StrCmpIgnoreCase(device.GetDeviceAddr().c_str(), addr) == 0) ||160 ((isRealMac && StrCmpIgnoreCase(device.GetDeviceAddr().c_str(), addr) == 0) ||
139 (!isRealMac && StrCmpIgnoreCase(ConvertRealMacToHashMac(device.GetDeviceAddr()).c_str(), addr) == 0))) {161 (!isRealMac && StrCmpIgnoreCase(ConvertRealMacToHashMac(device.GetDeviceAddr()).c_str(), addr) == 0))) {
162+ if (isSupportHfp != nullptr) {
163+ *isSupportHfp = IsSupportHfp(device);
164+ }
140 return true;165 return true;
141 }166 }
142 }167 }
@@ -27,7 +27,7 @@ extern "C" {
27typedef void (*ProxyListener)(const char *addr, int32_t state);27typedef void (*ProxyListener)(const char *addr, int32_t state);
28 28 
29int32_t RegisterHfpListener(const ProxyListener listener);29int32_t RegisterHfpListener(const ProxyListener listener);
30-bool IsPairedDevice(const char *addr, bool isRealMac);30+bool IsPairedDevice(const char *addr, bool isRealMac, bool *isSupportHfp);
31int32_t GetRealMac(char *realAddr, uint32_t realAddrLen, const char *hashAddr);31int32_t GetRealMac(char *realAddr, uint32_t realAddrLen, const char *hashAddr);
32#ifdef __cplusplus32#ifdef __cplusplus
33}33}
@@ -44,9 +44,9 @@ void InitProxyChannelManagerWrapper(void)
44 return ProxyChannelMock::GetMock()->InitProxyChannelManagerWrapper();44 return ProxyChannelMock::GetMock()->InitProxyChannelManagerWrapper();
45}45}
46 46 
47-bool IsPairedDevice(const char *addr, bool isRealMac)47+bool IsPairedDevice(const char *addr, bool isRealMac, bool *isSupportHfp)
48{48{
49- return ProxyChannelMock::GetMock()->IsPairedDevice(addr, isRealMac);49+ return ProxyChannelMock::GetMock()->IsPairedDevice(addr, isRealMac, isSupportHfp);
50}50}
51 51 
52int32_t GetRealMac(char *realAddr, uint32_t realAddrLen, const char *hashAddr)52int32_t GetRealMac(char *realAddr, uint32_t realAddrLen, const char *hashAddr)
@@ -124,6 +124,14 @@ int32_t ProxyChannelMock::ActionOfConnect2(const char *uuid, const BT_ADDR mac,
124 return UNDERLAYER_HANDLE;124 return UNDERLAYER_HANDLE;
125}125}
126 126 
127+bool ProxyChannelMock::ActionOfIsPairedDevice(const char *addr, bool isRealMac, bool *isSupportHfp)
128+{
129+ if (isSupportHfp != nullptr) {
130+ *isSupportHfp = true;
131+ }
132+ return true;
133+}
134+ 
127int32_t SoftBusGetBrState(void)135int32_t SoftBusGetBrState(void)
128{136{
129 return BR_ENABLE;137 return BR_ENABLE;
@@ -54,7 +54,7 @@ public:
54 virtual int32_t Connect(const char *uuid, const BT_ADDR mac, void *connectCallback) = 0;54 virtual int32_t Connect(const char *uuid, const BT_ADDR mac, void *connectCallback) = 0;
55 virtual int32_t Write(int32_t clientFd, const uint8_t *buf, const int32_t len) = 0;55 virtual int32_t Write(int32_t clientFd, const uint8_t *buf, const int32_t len) = 0;
56 virtual int32_t Read(int32_t clientFd, uint8_t *buf, const int32_t len) = 0;56 virtual int32_t Read(int32_t clientFd, uint8_t *buf, const int32_t len) = 0;
57- virtual bool IsPairedDevice(const char *addr, bool isRealMac) = 0;57+ virtual bool IsPairedDevice(const char *addr, bool isRealMac, bool *isSupportHfp) = 0;
58 virtual int32_t GetRealMac(char *realAddr, uint32_t realAddrLen, const char *hashAddr) = 0;58 virtual int32_t GetRealMac(char *realAddr, uint32_t realAddrLen, const char *hashAddr) = 0;
59};59};
60class ProxyChannelMock : public ProxyChannelInterface {60class ProxyChannelMock : public ProxyChannelInterface {
@@ -73,7 +73,7 @@ public:
73 MOCK_METHOD(int32_t, Connect, (const char *uuid, const BT_ADDR mac, void *connectCallback), (override));73 MOCK_METHOD(int32_t, Connect, (const char *uuid, const BT_ADDR mac, void *connectCallback), (override));
74 MOCK_METHOD(int32_t, Write, (int32_t clientFd, const uint8_t *buf, const int32_t len), (override));74 MOCK_METHOD(int32_t, Write, (int32_t clientFd, const uint8_t *buf, const int32_t len), (override));
75 MOCK_METHOD(int32_t, Read, (int32_t clientFd, uint8_t *buf, const int32_t len), (override));75 MOCK_METHOD(int32_t, Read, (int32_t clientFd, uint8_t *buf, const int32_t len), (override));
76- MOCK_METHOD(bool, IsPairedDevice, (const char *addr, bool isRealMac), (override));76+ MOCK_METHOD(bool, IsPairedDevice, (const char *addr, bool isRealMac, bool *isSupportHfp), (override));
77 MOCK_METHOD(int32_t, GetRealMac, (char *realAddr, uint32_t realAddrLen, const char *hashAddr), (override));77 MOCK_METHOD(int32_t, GetRealMac, (char *realAddr, uint32_t realAddrLen, const char *hashAddr), (override));
78 78 
79 static int32_t ActionOfAddBtStateListener(const SoftBusBtStateListener *listener, int *listenerId);79 static int32_t ActionOfAddBtStateListener(const SoftBusBtStateListener *listener, int *listenerId);
@@ -85,6 +85,7 @@ public:
85 static int32_t ActionOfConnect(const char *uuid, const BT_ADDR mac, void *connectCallback);85 static int32_t ActionOfConnect(const char *uuid, const BT_ADDR mac, void *connectCallback);
86 static int32_t ActionOfConnect1(const char *uuid, const BT_ADDR mac, void *connectCallback);86 static int32_t ActionOfConnect1(const char *uuid, const BT_ADDR mac, void *connectCallback);
87 static int32_t ActionOfConnect2(const char *uuid, const BT_ADDR mac, void *connectCallback);87 static int32_t ActionOfConnect2(const char *uuid, const BT_ADDR mac, void *connectCallback);
88+ static bool ActionOfIsPairedDevice(const char *addr, bool isRealMac, bool *isSupportHfp);
88 static void InjectHfpConnectionChanged(std::string addr, int32_t state);89 static void InjectHfpConnectionChanged(std::string addr, int32_t state);
89 static void InjectBtAclStateChanged(90 static void InjectBtAclStateChanged(
90 int32_t listenerId, const SoftBusBtAddr *btAddr, int32_t aclState, int32_t hciReason);91 int32_t listenerId, const SoftBusBtAddr *btAddr, int32_t aclState, int32_t hciReason);
@@ -303,12 +303,12 @@ HWTEST_F(ProxyManagerTest, ProxyChannelManagerTest005, TestSize.Level1)
303 CONN_LOGI(CONN_PROXY, "=================");303 CONN_LOGI(CONN_PROXY, "=================");
304 ret = g_channel->send(&proxyChannel, data, sizeof(data));304 ret = g_channel->send(&proxyChannel, data, sizeof(data));
305 EXPECT_EQ(ret, SOFTBUS_NOT_FIND);305 EXPECT_EQ(ret, SOFTBUS_NOT_FIND);
306- g_channel->close(&proxyChannel);306+ g_channel->close(&proxyChannel, true);
307 sleep(1);307 sleep(1);
308 proxyChannel.channelId = g_channelId;308 proxyChannel.channelId = g_channelId;
309 ret = g_channel->send(&proxyChannel, data, sizeof(data));309 ret = g_channel->send(&proxyChannel, data, sizeof(data));
310 EXPECT_EQ(ret, SOFTBUS_OK);310 EXPECT_EQ(ret, SOFTBUS_OK);
311- g_channel->close(&proxyChannel);311+ g_channel->close(&proxyChannel, true);
312 sleep(2);312 sleep(2);
313 // test reconnect device is null313 // test reconnect device is null
314 ProxyChannelMock::InjectHfpConnectionChanged(addr, SOFTBUS_HFP_CONNECTED);314 ProxyChannelMock::InjectHfpConnectionChanged(addr, SOFTBUS_HFP_CONNECTED);
@@ -330,7 +330,7 @@ HWTEST_F(ProxyManagerTest, ProxyChannelManagerTest006, TestSize.Level1)
330 ProxyChannelMock mock;330 ProxyChannelMock mock;
331 EXPECT_CALL(mock, Connect).WillRepeatedly(Return(UNDERLAYER_HANDLE));331 EXPECT_CALL(mock, Connect).WillRepeatedly(Return(UNDERLAYER_HANDLE));
332 EXPECT_CALL(mock, Read).WillRepeatedly(Return(-1));332 EXPECT_CALL(mock, Read).WillRepeatedly(Return(-1));
333- EXPECT_CALL(mock, IsPairedDevice).WillRepeatedly(Return(true));333+ EXPECT_CALL(mock, IsPairedDevice).WillRepeatedly(ProxyChannelMock::ActionOfIsPairedDevice);
334 int32_t ret = ConstructParamAndOpenProxyChannel(1, CONNECT_TIMEOUT);334 int32_t ret = ConstructParamAndOpenProxyChannel(1, CONNECT_TIMEOUT);
335 EXPECT_EQ(ret, SOFTBUS_OK);335 EXPECT_EQ(ret, SOFTBUS_OK);
336 sleep(1);336 sleep(1);
@@ -364,7 +364,7 @@ HWTEST_F(ProxyManagerTest, ProxyChannelManagerTest007, TestSize.Level1)
364 ProxyChannelMock mock;364 ProxyChannelMock mock;
365 EXPECT_CALL(mock, Connect).WillRepeatedly(Return(UNDERLAYER_HANDLE));365 EXPECT_CALL(mock, Connect).WillRepeatedly(Return(UNDERLAYER_HANDLE));
366 EXPECT_CALL(mock, Read).WillRepeatedly(Return(-1));366 EXPECT_CALL(mock, Read).WillRepeatedly(Return(-1));
367- EXPECT_CALL(mock, IsPairedDevice).WillRepeatedly(Return(true));367+ EXPECT_CALL(mock, IsPairedDevice).WillRepeatedly(ProxyChannelMock::ActionOfIsPairedDevice);
368 368 
369 // open new proxy channel369 // open new proxy channel
370 int32_t ret = ConstructParamAndOpenProxyChannel(1, CONNECT_TIMEOUT);370 int32_t ret = ConstructParamAndOpenProxyChannel(1, CONNECT_TIMEOUT);
@@ -647,4 +647,46 @@ HWTEST_F(ProxyManagerTest, ProxyChannelManagerTest012, TestSize.Level1)
647 EXPECT_NE(g_channelId, 0);647 EXPECT_NE(g_channelId, 0);
648 CONN_LOGI(CONN_PROXY, "ProxyChannelManagerTest012 out");648 CONN_LOGI(CONN_PROXY, "ProxyChannelManagerTest012 out");
649}649}
650+ 
651+/*
652+ * @tc.name: ProxyChannelManagerTest012
653+ * @tc.desc: test close not reset reconnect event
654+ * @tc.type: FUNC
655+ * @tc.require:
656+ */
657+HWTEST_F(ProxyManagerTest, ProxyChannelManagerTest013, TestSize.Level1)
658+{
659+ CONN_LOGI(CONN_PROXY, "ProxyChannelManagerTest013 in");
660+ ProxyChannelMock mock;
661+ EXPECT_CALL(mock, Connect).WillRepeatedly(Return(UNDERLAYER_HANDLE));
662+ EXPECT_CALL(mock, Read).WillRepeatedly(Return(-1));
663+ EXPECT_CALL(mock, IsPairedDevice).WillRepeatedly(Return(true));
664+ 
665+ // open new proxy channel
666+ int32_t ret = ConstructParamAndOpenProxyChannel(1, CONNECT_TIMEOUT);
667+ EXPECT_EQ(ret, SOFTBUS_OK);
668+ sleep(1);
669+ EXPECT_NE(g_channelId, 0);
670+ 
671+ struct ProxyChannel proxyChannel = {
672+ .channelId = g_channelId,
673+ };
674+ g_channelId = 0;
675+ g_channel->close(&proxyChannel, false);
676+ sleep(1);
677+ bool reconnectDeviceExist = false;
678+ ProxyConnectInfo *it = NULL;
679+ LIST_FOR_EACH_ENTRY(it, &GetProxyChannelManager()->reconnectDeviceInfos, ProxyConnectInfo, node) {
680+ reconnectDeviceExist = true;
681+ }
682+ EXPECT_EQ(reconnectDeviceExist, true);
683+ 
684+ SoftBusBtAddr btAddr = {
685+ .addr = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
686+ };
687+ ProxyChannelMock::InjectBtAclStateChanged(1, &btAddr, SOFTBUS_ACL_STATE_CONNECTED, 0);
688+ sleep(1);
689+ EXPECT_NE(g_channelId, 0);
690+ CONN_LOGI(CONN_PROXY, "ProxyChannelManagerTest013 out");
691+}
650}692}