1 /*
2 * Copyright (c) 2022 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
16 #include "lnn_heartbeat_utils.h"
17
18 #include <securec.h>
19 #include <string.h>
20
21 #include "bus_center_manager.h"
22 #include "lnn_heartbeat_medium_mgr.h"
23 #include "p2plink_interface.h"
24
25 #include "softbus_adapter_crypto.h"
26 #include "softbus_errcode.h"
27 #include "softbus_conn_interface.h"
28 #include "softbus_log.h"
29 #include "softbus_utils.h"
30
LnnConvertConnAddrTypeToHbType(ConnectionAddrType addrType)31 LnnHeartbeatType LnnConvertConnAddrTypeToHbType(ConnectionAddrType addrType)
32 {
33 switch (addrType) {
34 case CONNECTION_ADDR_WLAN:
35 case CONNECTION_ADDR_ETH:
36 return HEARTBEAT_TYPE_UDP;
37 case CONNECTION_ADDR_BR:
38 case CONNECTION_ADDR_BLE:
39 return HEARTBEAT_TYPE_BLE_V1;
40 default:
41 break;
42 }
43 return HEARTBEAT_TYPE_MAX;
44 }
45
LnnConvertHbTypeToConnAddrType(LnnHeartbeatType type)46 ConnectionAddrType LnnConvertHbTypeToConnAddrType(LnnHeartbeatType type)
47 {
48 switch (type) {
49 case HEARTBEAT_TYPE_UDP:
50 case HEARTBEAT_TYPE_TCP_FLUSH:
51 return CONNECTION_ADDR_WLAN;
52 case HEARTBEAT_TYPE_BLE_V1:
53 case HEARTBEAT_TYPE_BLE_V0:
54 return CONNECTION_ADDR_BLE;
55 default:
56 break;
57 }
58 return CONNECTION_ADDR_MAX;
59 }
60
LnnConvertHbTypeToId(LnnHeartbeatType type)61 int32_t LnnConvertHbTypeToId(LnnHeartbeatType type)
62 {
63 int32_t cnt = -1;
64
65 if (type < HEARTBEAT_TYPE_MIN || type >= HEARTBEAT_TYPE_MAX) {
66 return HB_INVALID_TYPE_ID;
67 }
68 do {
69 type >>= 1;
70 ++cnt;
71 } while (type >= HEARTBEAT_TYPE_MIN);
72 if (cnt < 0 || cnt > HB_MAX_TYPE_COUNT) {
73 return HB_INVALID_TYPE_ID;
74 }
75 return cnt;
76 }
77
HbHasActiveBrConnection(const char * networkId)78 static bool HbHasActiveBrConnection(const char *networkId)
79 {
80 bool ret = false;
81 ConnectOption option = {0};
82 char brMac[BT_MAC_LEN] = {0};
83
84 if (LnnGetRemoteStrInfo(networkId, STRING_KEY_BT_MAC, brMac, sizeof(brMac)) != SOFTBUS_OK) {
85 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get bt mac err");
86 return false;
87 }
88 option.type = CONNECT_BR;
89 if (strcpy_s(option.brOption.brMac, BT_MAC_LEN, brMac) != EOK) {
90 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB strcpy_s bt mac err");
91 return false;
92 }
93 ret = CheckActiveConnection(&option);
94 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "HB has active bt connection:%s", ret ? "true" : "false");
95 return ret;
96 }
97
HbHasActiveBleConnection(const char * networkId)98 static bool HbHasActiveBleConnection(const char *networkId)
99 {
100 bool ret = false;
101 ConnectOption option = {0};
102 char udid[UDID_BUF_LEN] = {0};
103 char udidHash[UDID_HASH_LEN] = {0};
104
105 if (LnnGetRemoteStrInfo(networkId, STRING_KEY_DEV_UDID, udid, sizeof(udid)) != SOFTBUS_OK) {
106 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get udid err");
107 return false;
108 }
109 if (SoftBusGenerateStrHash((const unsigned char *)udid, strlen(udid),
110 (unsigned char *)udidHash) != SOFTBUS_OK) {
111 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get udid hash err");
112 return false;
113 }
114 option.type = CONNECT_BLE;
115 if (memcpy_s(option.bleOption.deviceIdHash, UDID_HASH_LEN, udidHash, sizeof(udidHash)) != EOK) {
116 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB memcpy_s udid hash err");
117 return false;
118 }
119 ret = CheckActiveConnection(&option);
120 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "HB has active ble connection:%s", ret ? "true" : "false");
121 return ret;
122 }
123
HbHasActiveP2pConnection(const char * networkId)124 static bool HbHasActiveP2pConnection(const char *networkId)
125 {
126 int32_t ret;
127 char peerMac[P2P_MAC_LEN] = {0};
128
129 if (LnnGetRemoteStrInfo(networkId, STRING_KEY_P2P_MAC, peerMac, sizeof(peerMac)) != SOFTBUS_OK) {
130 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB get peer p2p mac err");
131 return false;
132 }
133 ret = P2pLinkQueryDevIsOnline(peerMac);
134 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "HB has active p2p connection:%s, ret=%d",
135 ret == SOFTBUS_OK ? "true" : "false", ret);
136 return ret == SOFTBUS_OK ? true : false;
137 }
138
LnnHasActiveConnection(const char * networkId,ConnectionAddrType addrType)139 bool LnnHasActiveConnection(const char *networkId, ConnectionAddrType addrType)
140 {
141 bool ret = false;
142
143 if (networkId == NULL || addrType >= CONNECTION_ADDR_MAX) {
144 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB check active connection get invalid param");
145 return SOFTBUS_INVALID_PARAM;
146 }
147 switch (addrType) {
148 case CONNECTION_ADDR_WLAN:
149 case CONNECTION_ADDR_ETH:
150 case CONNECTION_ADDR_BR:
151 break;
152 case CONNECTION_ADDR_BLE:
153 ret = HbHasActiveBrConnection(networkId) || HbHasActiveBleConnection(networkId) ||
154 HbHasActiveP2pConnection(networkId);
155 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "HB networkId:%s has active BT/BLE/P2P connection:%s",
156 AnonymizesNetworkID(networkId), ret ? "true" : "false");
157 return ret;
158 default:
159 break;
160 }
161 return false;
162 }
163
LnnVisitHbTypeSet(VisitHbTypeCb callback,LnnHeartbeatType * typeSet,void * data)164 bool LnnVisitHbTypeSet(VisitHbTypeCb callback, LnnHeartbeatType *typeSet, void *data)
165 {
166 bool isFinish = false;
167 LnnHeartbeatType i;
168
169 if (typeSet == NULL || *typeSet < HEARTBEAT_TYPE_MIN || *typeSet >= HEARTBEAT_TYPE_MAX) {
170 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB visit typeSet get invalid param");
171 return false;
172 }
173 for (i = HEARTBEAT_TYPE_MIN; i < HEARTBEAT_TYPE_MAX; i <<= 1) {
174 if ((i & *typeSet) == 0) {
175 continue;
176 }
177 isFinish = callback(typeSet, i, data);
178 if (!isFinish) {
179 return false;
180 }
181 }
182 return true;
183 }
184
VisitCheckSupportedHbType(LnnHeartbeatType * typeSet,LnnHeartbeatType eachType,void * data)185 static bool VisitCheckSupportedHbType(LnnHeartbeatType *typeSet, LnnHeartbeatType eachType, void *data)
186 {
187 (void)typeSet;
188 LnnHeartbeatType *dstType = (LnnHeartbeatType *)data;
189
190 if ((eachType & *dstType) == 0) {
191 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB not support hbType(%d) completely", *dstType);
192 return false;
193 }
194 return true;
195 }
196
LnnCheckSupportedHbType(LnnHeartbeatType * srcType,LnnHeartbeatType * dstType)197 bool LnnCheckSupportedHbType(LnnHeartbeatType *srcType, LnnHeartbeatType *dstType)
198 {
199 if (srcType == NULL || dstType == NULL) {
200 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB check supported hbType get invalid param");
201 return false;
202 }
203 return LnnVisitHbTypeSet(VisitCheckSupportedHbType, srcType, dstType);
204 }
205
LnnGenerateHexStringHash(const unsigned char * str,char * hashStr,uint32_t len)206 int32_t LnnGenerateHexStringHash(const unsigned char *str, char *hashStr, uint32_t len)
207 {
208 int32_t ret;
209 uint8_t hashResult[SHA_256_HASH_LEN] = {0};
210
211 if (str == NULL) {
212 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB generate str hash invalid param");
213 return SOFTBUS_INVALID_PARAM;
214 }
215 ret = SoftBusGenerateStrHash(str, strlen((char *)str), hashResult);
216 if (ret != SOFTBUS_OK) {
217 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB generate str hash fail, ret=%d", ret);
218 return ret;
219 }
220 ret = ConvertBytesToHexString(hashStr, len + 1, hashResult, len / HEXIFY_UNIT_LEN);
221 if (ret != SOFTBUS_OK) {
222 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "HB convert bytes to str hash fail ret=%d", ret);
223 return ret;
224 }
225 return SOFTBUS_OK;
226 }
227