1 /*
2 * Copyright (c) 2021 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_sync_item_info.h"
17
18 #include <securec.h>
19
20 #include "lnn_connection_addr_utils.h"
21 #include "lnn_distributed_net_ledger.h"
22 #include "lnn_local_net_ledger.h"
23 #include "lnn_log.h"
24 #include "lnn_net_builder.h"
25 #include "lnn_sync_info_manager.h"
26 #include "softbus_adapter_mem.h"
27 #include "softbus_def.h"
28 #include "softbus_errcode.h"
29 #include "softbus_wifi_api_adapter.h"
30 #include "softbus_adapter_socket.h"
31
32 #define CONN_CODE_SHIFT 16
33 #define DISCOVERY_TYPE_MASK 0x7FFF
34
FillTargetWifiConfig(const unsigned char * targetBssid,const char * ssid,const SoftBusWifiDevConf * conWifiConf,SoftBusWifiDevConf * targetWifiConf)35 static int32_t FillTargetWifiConfig(const unsigned char *targetBssid, const char *ssid,
36 const SoftBusWifiDevConf *conWifiConf, SoftBusWifiDevConf *targetWifiConf)
37 {
38 if (strcpy_s(targetWifiConf->ssid, sizeof(targetWifiConf->ssid), ssid) != EOK) {
39 LNN_LOGE(LNN_BUILDER, "str copy ssid fail");
40 return SOFTBUS_ERR;
41 }
42
43 if (memcpy_s(targetWifiConf->bssid, sizeof(targetWifiConf->bssid),
44 targetBssid, sizeof(targetWifiConf->bssid)) != EOK) {
45 LNN_LOGE(LNN_BUILDER, "mem copy bssid fail");
46 return SOFTBUS_ERR;
47 }
48
49 if (strcpy_s(targetWifiConf->preSharedKey, sizeof(targetWifiConf->preSharedKey),
50 conWifiConf->preSharedKey) != EOK) {
51 LNN_LOGE(LNN_BUILDER, "str copy ssid fail");
52 return SOFTBUS_ERR;
53 }
54
55 targetWifiConf->securityType = conWifiConf->securityType;
56 targetWifiConf->isHiddenSsid = conWifiConf->isHiddenSsid;
57 return SOFTBUS_OK;
58 }
59
ResultClean(SoftBusWifiDevConf * result)60 static void ResultClean(SoftBusWifiDevConf *result)
61 {
62 (void)memset_s(result, sizeof(SoftBusWifiDevConf) * WIFI_MAX_CONFIG_SIZE, 0,
63 sizeof(SoftBusWifiDevConf) * WIFI_MAX_CONFIG_SIZE);
64 SoftBusFree(result);
65 }
66
WifiConnectToTargetAp(const unsigned char * targetBssid,const char * ssid)67 static int32_t WifiConnectToTargetAp(const unsigned char *targetBssid, const char *ssid)
68 {
69 SoftBusWifiDevConf *result = NULL;
70 uint32_t wifiConfigSize;
71 SoftBusWifiDevConf targetDeviceConf;
72 uint32_t i;
73
74 result = (SoftBusWifiDevConf *)SoftBusMalloc(sizeof(SoftBusWifiDevConf) * WIFI_MAX_CONFIG_SIZE);
75 if (result == NULL) {
76 LNN_LOGE(LNN_BUILDER, "malloc wifi device config fail");
77 return SOFTBUS_ERR;
78 }
79 (void)memset_s(&targetDeviceConf, sizeof(SoftBusWifiDevConf), 0, sizeof(SoftBusWifiDevConf));
80 (void)memset_s(result, sizeof(SoftBusWifiDevConf) * WIFI_MAX_CONFIG_SIZE, 0,
81 sizeof(SoftBusWifiDevConf) * WIFI_MAX_CONFIG_SIZE);
82 int32_t retVal = SoftBusGetWifiDeviceConfig(result, &wifiConfigSize);
83 if (retVal != SOFTBUS_OK || wifiConfigSize > WIFI_MAX_CONFIG_SIZE) {
84 LNN_LOGE(LNN_BUILDER, "git config fail, retVal=%{public}d, wifiConfigSize=%{public}d", retVal, wifiConfigSize);
85 ResultClean(result);
86 return SOFTBUS_ERR;
87 }
88
89 for (i = 0; i < wifiConfigSize; i++) {
90 if (strcmp(ssid, (result + i)->ssid) != 0) {
91 continue;
92 }
93 if (FillTargetWifiConfig(targetBssid, ssid, result + i, &targetDeviceConf) != SOFTBUS_OK) {
94 LNN_LOGE(LNN_BUILDER, "fill device config fail");
95 (void)memset_s(&targetDeviceConf, sizeof(SoftBusWifiDevConf), 0, sizeof(SoftBusWifiDevConf));
96 ResultClean(result);
97 return SOFTBUS_ERR;
98 }
99 break;
100 }
101 if (SoftBusDisconnectDevice() != SOFTBUS_OK) {
102 LNN_LOGE(LNN_BUILDER, "dis connect device fail");
103 (void)memset_s(&targetDeviceConf, sizeof(SoftBusWifiDevConf), 0, sizeof(SoftBusWifiDevConf));
104 ResultClean(result);
105 return SOFTBUS_ERR;
106 }
107
108 if (SoftBusConnectToDevice(&targetDeviceConf) != SOFTBUS_OK) {
109 LNN_LOGE(LNN_BUILDER, "connect to target ap fail");
110 (void)memset_s(&targetDeviceConf, sizeof(SoftBusWifiDevConf), 0, sizeof(SoftBusWifiDevConf));
111 ResultClean(result);
112 return SOFTBUS_ERR;
113 }
114 (void)memset_s(&targetDeviceConf, sizeof(SoftBusWifiDevConf), 0, sizeof(SoftBusWifiDevConf));
115 ResultClean(result);
116 return SOFTBUS_OK;
117 }
118
OnReceiveDeviceName(LnnSyncInfoType type,const char * networkId,const uint8_t * msg,uint32_t len)119 void OnReceiveDeviceName(LnnSyncInfoType type, const char *networkId, const uint8_t *msg, uint32_t len)
120 {
121 char udid[UDID_BUF_LEN];
122 BssTransInfo *bssTranInfo = NULL;
123 if (msg == NULL) {
124 LNN_LOGE(LNN_BUILDER, "msg is null");
125 return;
126 }
127 if (type != LNN_INFO_TYPE_DEVICE_NAME) {
128 return;
129 }
130 if (LnnConvertDlId(networkId, CATEGORY_NETWORK_ID, CATEGORY_UDID, udid, UDID_BUF_LEN) != SOFTBUS_OK) {
131 LNN_LOGE(LNN_BUILDER, "convert networkId to udid fail");
132 return;
133 }
134 bssTranInfo = (BssTransInfo *)msg;
135 if (WifiConnectToTargetAp(bssTranInfo->targetBssid, bssTranInfo->ssid) != SOFTBUS_OK) {
136 LNN_LOGE(LNN_BUILDER, "wifi connect to target ap failed");
137 }
138 }
139
OnReceiveTransReqMsg(LnnSyncInfoType type,const char * networkId,const uint8_t * msg,uint32_t len)140 void OnReceiveTransReqMsg(LnnSyncInfoType type, const char *networkId, const uint8_t *msg, uint32_t len)
141 {
142 char udid[UDID_BUF_LEN];
143
144 LNN_LOGI(LNN_BUILDER, "recv trans req msg infoType=%{public}d, len=%{public}d", type, len);
145 if (type != LNN_INFO_TYPE_BSS_TRANS) {
146 return;
147 }
148 if (LnnConvertDlId(networkId, CATEGORY_NETWORK_ID, CATEGORY_UDID, udid, UDID_BUF_LEN) != SOFTBUS_OK) {
149 LNN_LOGE(LNN_BUILDER, "convert networkId to udid fail");
150 return;
151 }
152 if (!LnnSetDLDeviceInfoName(udid, (char *)msg)) {
153 LNN_LOGI(LNN_BUILDER, "set peer device name fail");
154 }
155 }
156
OnReceiveBrOffline(LnnSyncInfoType type,const char * networkId,const uint8_t * msg,uint32_t len)157 static void OnReceiveBrOffline(LnnSyncInfoType type, const char *networkId, const uint8_t *msg, uint32_t len)
158 {
159 uint32_t combinedInt;
160 char uuid[UUID_BUF_LEN];
161 int16_t peerCode, code;
162 DiscoveryType discType;
163
164 LNN_LOGI(LNN_BUILDER, "Recv offline info, infoType=%{public}d, len=%{public}d", type, len);
165 if (type != LNN_INFO_TYPE_OFFLINE) {
166 return;
167 }
168 if (msg == NULL || len != sizeof(int32_t)) {
169 return;
170 }
171 combinedInt = *(uint32_t *)msg;
172 combinedInt = SoftBusNtoHl(combinedInt);
173 peerCode = (int16_t)(combinedInt >> CONN_CODE_SHIFT);
174 discType = (DiscoveryType)(combinedInt & DISCOVERY_TYPE_MASK);
175 if (LnnConvertDlId(networkId, CATEGORY_NETWORK_ID, CATEGORY_UUID, uuid, UUID_BUF_LEN) != SOFTBUS_OK) {
176 LNN_LOGE(LNN_BUILDER, "covert networkId to uuid fail");
177 return;
178 }
179 code = LnnGetCnnCode(uuid, DISCOVERY_TYPE_BR);
180 if (code == INVALID_CONNECTION_CODE_VALUE) {
181 LNN_LOGE(LNN_BUILDER, "uuid not exist");
182 return;
183 }
184 if (discType != DISCOVERY_TYPE_BR || code != peerCode) {
185 LNN_LOGE(LNN_BUILDER, "info error discType=%{public}d, code=%{public}d, peerCode=%{public}d",
186 discType, code, peerCode);
187 return;
188 }
189 if (LnnRequestLeaveSpecific(networkId, LnnDiscTypeToConnAddrType(discType)) != SOFTBUS_OK) {
190 LNN_LOGE(LNN_BUILDER, "request leave specific fail");
191 }
192 }
193
LnnSendTransReq(const char * peerNetWorkId,const BssTransInfo * transInfo)194 int32_t LnnSendTransReq(const char *peerNetWorkId, const BssTransInfo *transInfo)
195 {
196 if (peerNetWorkId == NULL || transInfo == NULL) {
197 LNN_LOGE(LNN_BUILDER, "para peerNetWorkId or tansInfo is null");
198 return SOFTBUS_ERR;
199 }
200
201 if (LnnSetDLBssTransInfo(peerNetWorkId, transInfo) != SOFTBUS_OK) {
202 LNN_LOGE(LNN_BUILDER, "save bssTransinfo fail");
203 return SOFTBUS_ERR;
204 }
205
206 if (LnnSendSyncInfoMsg(LNN_INFO_TYPE_BSS_TRANS, peerNetWorkId,
207 (const uint8_t *)transInfo, sizeof(BssTransInfo), NULL) != SOFTBUS_OK) {
208 LNN_LOGE(LNN_BUILDER, "send bss info fail");
209 return SOFTBUS_ERR;
210 }
211 return SOFTBUS_OK;
212 }
213
LnnInitOffline(void)214 int32_t LnnInitOffline(void)
215 {
216 return LnnRegSyncInfoHandler(LNN_INFO_TYPE_OFFLINE, OnReceiveBrOffline);
217 }
218
LnnDeinitOffline(void)219 void LnnDeinitOffline(void)
220 {
221 (void)LnnUnregSyncInfoHandler(LNN_INFO_TYPE_OFFLINE, OnReceiveBrOffline);
222 }
223