1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "trans_link_listener.h"
16
17 #include "bus_center_manager.h"
18 #include "lnn_distributed_net_ledger.h"
19 #include "securec.h"
20 #include "softbus_common.h"
21 #include "softbus_def.h"
22 #include "softbus_errcode.h"
23 #include "trans_log.h"
24 #include "trans_session_manager.h"
25 #include "trans_tcp_direct_p2p.h"
26 #include "wifi_direct_manager.h"
27 #include "data_bus_native.h"
28 #include "softbus_conn_interface.h"
29 #include "softbus_socket.h"
30
31 #define NETWORK_ID_LEN 7
32 #define COMBINE_TYPE(routeType, connType) ((routeType) | ((uint8_t)(connType) << 8))
33
ClearIpInfo(const char * peerUuid)34 static void ClearIpInfo(const char *peerUuid)
35 {
36 if (LnnSetLocalStrInfo(STRING_KEY_P2P_IP, "") != SOFTBUS_OK) {
37 TRANS_LOGW(TRANS_SVC, "set local p2p ip fail");
38 }
39 if (LnnSetDLP2pIp(peerUuid, CATEGORY_UUID, "") != SOFTBUS_OK) {
40 TRANS_LOGW(TRANS_SVC, "set peer p2p ip fail");
41 }
42 }
43
OnWifiDirectDeviceOffLine(const char * peerMac,const char * peerIp,const char * peerUuid,const char * localIp)44 static void OnWifiDirectDeviceOffLine(const char *peerMac, const char *peerIp, const char *peerUuid,
45 const char *localIp)
46 {
47 TRANS_CHECK_AND_RETURN_LOGW(peerUuid, TRANS_SVC, "peer uuid is null");
48 TRANS_CHECK_AND_RETURN_LOGW(localIp, TRANS_SVC, "localIp is null");
49 NodeInfo nodeInfo;
50 TransConnType connType = TRANS_CONN_ALL;
51 memset_s(&nodeInfo, sizeof(nodeInfo), 0, sizeof(nodeInfo));
52 int32_t ret = LnnGetRemoteNodeInfoById(peerUuid, CATEGORY_UUID, &nodeInfo);
53 TRANS_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK, TRANS_SVC, "LnnGetRemoteNodeInfoById failed");
54
55 if (IsHmlIpAddr(localIp)) {
56 ClearHmlListenerByUuid(peerUuid);
57 connType = TRANS_CONN_HML;
58 } else {
59 StopP2pSessionListener();
60 ClearIpInfo(peerUuid);
61 connType = TRANS_CONN_P2P;
62 }
63
64 TransOnLinkDown(nodeInfo.networkId, nodeInfo.uuid, nodeInfo.masterUdid, peerIp, COMBINE_TYPE(WIFI_P2P, connType));
65 TRANS_LOGI(TRANS_SVC, "Notify Degrade MigrateEvents start");
66 ret = NotifyNearByOnMigrateEvents(nodeInfo.networkId, WIFI_P2P, false);
67 if (ret != SOFTBUS_OK) {
68 TRANS_LOGE(TRANS_SVC, "Notify Degrade MigrateEvents fail");
69 }
70 TRANS_LOGI(TRANS_SVC, "Notify Degrade MigrateEvents success");
71 }
72
OnWifiDirectRoleChange(enum WifiDirectRole oldRole,enum WifiDirectRole newRole)73 static void OnWifiDirectRoleChange(enum WifiDirectRole oldRole, enum WifiDirectRole newRole)
74 {
75 (void)oldRole;
76 if (newRole == WIFI_DIRECT_ROLE_NONE) {
77 TRANS_LOGI(TRANS_SVC, "my role change to NONE");
78 StopP2pSessionListener();
79 for (int i = DIRECT_CHANNEL_SERVER_HML_START; i <= DIRECT_CHANNEL_SERVER_HML_END; i++) {
80 StopHmlListener((ListenerModule)i);
81 }
82 }
83 }
84
OnWifiDirectDeviceOnLine(const char * peerMac,const char * peerIp,const char * peerUuid,bool isSource)85 static void OnWifiDirectDeviceOnLine(const char *peerMac, const char *peerIp, const char *peerUuid, bool isSource)
86 {
87 TRANS_CHECK_AND_RETURN_LOGW(peerMac, TRANS_SVC, "peer mac is null");
88 NodeInfo nodeInfo;
89 memset_s(&nodeInfo, sizeof(nodeInfo), 0, sizeof(nodeInfo));
90 int32_t ret = LnnGetRemoteNodeInfoById(peerUuid, CATEGORY_UUID, &nodeInfo);
91 TRANS_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK, TRANS_SVC, "LnnGetRemoteNodeInfoById failed");
92 TRANS_LOGI(TRANS_SVC, "Notify Upgrade MigrateEvents start");
93 ret = NotifyNearByOnMigrateEvents(nodeInfo.networkId, WIFI_P2P, true);
94 if (ret != SOFTBUS_OK) {
95 TRANS_LOGE(TRANS_SVC, "Notify Upgrade MigrateEvents fail");
96 }
97 TRANS_LOGI(TRANS_SVC, "Notify Upgrade MigrateEvents success");
98 }
99
ReqLinkListener(void)100 void ReqLinkListener(void)
101 {
102 struct WifiDirectStatusListener listener = {
103 .onDeviceOffLine = OnWifiDirectDeviceOffLine,
104 .onLocalRoleChange = OnWifiDirectRoleChange,
105 .onDeviceOnLine = OnWifiDirectDeviceOnLine,
106 };
107 struct WifiDirectManager *mgr = GetWifiDirectManager();
108 if (mgr != NULL && mgr->registerStatusListener != NULL) {
109 mgr->registerStatusListener(&listener);
110 }
111 }
112