|
9 | 9 | */
|
10 | 10 |
|
11 | 11 | #include <rtthread.h>
|
| 12 | +#include <rthw.h> |
12 | 13 | #include <wlan_mgnt.h>
|
13 | 14 | #include <wlan_cfg.h>
|
14 | 15 | #include <wlan_prot.h>
|
15 | 16 |
|
| 17 | +#define DBG_TAG "WLAN.cmd" |
| 18 | +#ifdef RT_WLAN_MGNT_DEBUG |
| 19 | +#define DBG_LVL DBG_LOG |
| 20 | +#else |
| 21 | +#define DBG_LVL DBG_INFO |
| 22 | +#endif /* RT_WLAN_MGNT_DEBUG */ |
| 23 | +#include <rtdbg.h> |
| 24 | + |
| 25 | +static struct rt_wlan_scan_result scan_result; |
| 26 | +static struct rt_wlan_info *scan_filter = RT_NULL; |
| 27 | + |
16 | 28 | #if defined(RT_WLAN_MANAGE_ENABLE) && defined(RT_WLAN_MSH_CMD_ENABLE)
|
17 | 29 |
|
18 | 30 | struct wifi_cmd_des
|
@@ -142,46 +154,192 @@ static int wifi_status(int argc, char *argv[])
|
142 | 154 | return 0;
|
143 | 155 | }
|
144 | 156 |
|
145 |
| -static int wifi_scan(int argc, char *argv[]) |
| 157 | + |
| 158 | +static rt_bool_t wifi_info_isequ(struct rt_wlan_info *info1, struct rt_wlan_info *info2) |
146 | 159 | {
|
147 |
| - struct rt_wlan_scan_result *scan_result = RT_NULL; |
148 |
| - struct rt_wlan_info *info = RT_NULL; |
149 |
| - struct rt_wlan_info filter; |
| 160 | + rt_bool_t is_equ = 1; |
| 161 | + rt_uint8_t bssid_zero[RT_WLAN_BSSID_MAX_LENGTH] = { 0 }; |
150 | 162 |
|
151 |
| - if (argc > 3) |
152 |
| - return -1; |
| 163 | + if (is_equ && (info1->security != SECURITY_UNKNOWN) && (info2->security != SECURITY_UNKNOWN)) |
| 164 | + { |
| 165 | + is_equ &= info2->security == info1->security; |
| 166 | + } |
| 167 | + if (is_equ && ((info1->ssid.len > 0) && (info2->ssid.len > 0))) |
| 168 | + { |
| 169 | + is_equ &= info1->ssid.len == info2->ssid.len; |
| 170 | + is_equ &= rt_memcmp(&info2->ssid.val[0], &info1->ssid.val[0], info1->ssid.len) == 0; |
| 171 | + } |
| 172 | + if (is_equ && (rt_memcmp(&info1->bssid[0], bssid_zero, RT_WLAN_BSSID_MAX_LENGTH)) && |
| 173 | + (rt_memcmp(&info2->bssid[0], bssid_zero, RT_WLAN_BSSID_MAX_LENGTH))) |
| 174 | + { |
| 175 | + is_equ &= rt_memcmp(&info1->bssid[0], &info2->bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0; |
| 176 | + } |
| 177 | + if (is_equ && info1->datarate && info2->datarate) |
| 178 | + { |
| 179 | + is_equ &= info1->datarate == info2->datarate; |
| 180 | + } |
| 181 | + if (is_equ && (info1->channel >= 0) && (info2->channel >= 0)) |
| 182 | + { |
| 183 | + is_equ &= info1->channel == info2->channel; |
| 184 | + } |
| 185 | + if (is_equ && (info1->rssi < 0) && (info2->rssi < 0)) |
| 186 | + { |
| 187 | + is_equ &= info1->rssi == info2->rssi; |
| 188 | + } |
| 189 | + return is_equ; |
| 190 | +} |
153 | 191 |
|
154 |
| - if (argc == 3) |
| 192 | +static rt_err_t wifi_scan_result_cache(struct rt_wlan_info *info) |
| 193 | +{ |
| 194 | + struct rt_wlan_info *ptable; |
| 195 | + rt_err_t err = RT_EOK; |
| 196 | + int i, insert = -1; |
| 197 | + rt_base_t level; |
| 198 | + |
| 199 | + if (_sta_is_null() || (info == RT_NULL) || (info->ssid.len == 0)) return -RT_EINVAL; |
| 200 | + |
| 201 | + LOG_D("ssid:%s len:%d mac:%02x:%02x:%02x:%02x:%02x:%02x", info->ssid.val, info->ssid.len, |
| 202 | + info->bssid[0], info->bssid[1], info->bssid[2], info->bssid[3], info->bssid[4], info->bssid[5]); |
| 203 | + |
| 204 | + /* scanning result filtering */ |
| 205 | + level = rt_hw_interrupt_disable(); |
| 206 | + if (scan_filter) |
155 | 207 | {
|
156 |
| - INVALID_INFO(&filter); |
157 |
| - SSID_SET(&filter, argv[2]); |
158 |
| - info = &filter; |
| 208 | + struct rt_wlan_info _tmp_info = *scan_filter; |
| 209 | + rt_hw_interrupt_enable(level); |
| 210 | + if (wifi_info_isequ(&_tmp_info, info) != RT_TRUE) |
| 211 | + { |
| 212 | + return RT_EOK; |
| 213 | + } |
| 214 | + } |
| 215 | + else |
| 216 | + { |
| 217 | + rt_hw_interrupt_enable(level); |
159 | 218 | }
|
160 | 219 |
|
161 |
| - /* clean scan result */ |
162 |
| - rt_wlan_scan_result_clean(); |
163 |
| - /* scan ap info */ |
164 |
| - scan_result = rt_wlan_scan_with_info(info); |
165 |
| - if (scan_result) |
| 220 | + /* de-duplicatio */ |
| 221 | + for (i = 0; i < scan_result.num; i++) |
166 | 222 | {
|
167 |
| - int index, num; |
| 223 | + if ((info->ssid.len == scan_result.info[i].ssid.len) && |
| 224 | + (rt_memcmp(&info->bssid[0], &scan_result.info[i].bssid[0], RT_WLAN_BSSID_MAX_LENGTH) == 0)) |
| 225 | + { |
| 226 | + return RT_EOK; |
| 227 | + } |
| 228 | +#ifdef RT_WLAN_SCAN_SORT |
| 229 | + if (insert >= 0) |
| 230 | + { |
| 231 | + continue; |
| 232 | + } |
| 233 | + /* Signal intensity comparison */ |
| 234 | + if ((info->rssi < 0) && (scan_result.info[i].rssi < 0)) |
| 235 | + { |
| 236 | + if (info->rssi > scan_result.info[i].rssi) |
| 237 | + { |
| 238 | + insert = i; |
| 239 | + continue; |
| 240 | + } |
| 241 | + else if (info->rssi < scan_result.info[i].rssi) |
| 242 | + { |
| 243 | + continue; |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + /* Channel comparison */ |
| 248 | + if (info->channel < scan_result.info[i].channel) |
| 249 | + { |
| 250 | + insert = i; |
| 251 | + continue; |
| 252 | + } |
| 253 | + else if (info->channel > scan_result.info[i].channel) |
| 254 | + { |
| 255 | + continue; |
| 256 | + } |
| 257 | + |
| 258 | + /* data rate comparison */ |
| 259 | + if ((info->datarate > scan_result.info[i].datarate)) |
| 260 | + { |
| 261 | + insert = i; |
| 262 | + continue; |
| 263 | + } |
| 264 | + else if (info->datarate < scan_result.info[i].datarate) |
| 265 | + { |
| 266 | + continue; |
| 267 | + } |
| 268 | +#endif |
| 269 | + } |
| 270 | + |
| 271 | + /* Insert the end */ |
| 272 | + if (insert == -1) |
| 273 | + insert = scan_result.num; |
| 274 | + |
| 275 | + if (scan_result.num >= RT_WLAN_SCAN_CACHE_NUM) |
| 276 | + return RT_EOK; |
| 277 | + |
| 278 | + /* malloc memory */ |
| 279 | + ptable = rt_malloc(sizeof(struct rt_wlan_info) * (scan_result.num + 1)); |
| 280 | + if (ptable == RT_NULL) |
| 281 | + { |
| 282 | + LOG_E("wlan info malloc failed!"); |
| 283 | + return -RT_ENOMEM; |
| 284 | + } |
| 285 | + scan_result.num ++; |
| 286 | + |
| 287 | + /* copy info */ |
| 288 | + for (i = 0; i < scan_result.num; i++) |
| 289 | + { |
| 290 | + if (i < insert) |
| 291 | + { |
| 292 | + ptable[i] = scan_result.info[i]; |
| 293 | + } |
| 294 | + else if (i > insert) |
| 295 | + { |
| 296 | + ptable[i] = scan_result.info[i - 1]; |
| 297 | + } |
| 298 | + else if (i == insert) |
| 299 | + { |
| 300 | + ptable[i] = *info; |
| 301 | + } |
| 302 | + } |
| 303 | + rt_free(scan_result.info); |
| 304 | + scan_result.info = ptable; |
| 305 | + return err; |
| 306 | +} |
| 307 | + |
| 308 | + |
| 309 | + |
| 310 | +static void wifi_scan_result_clean(void) |
| 311 | +{ |
| 312 | + |
| 313 | + /* If there is data */ |
| 314 | + if (scan_result.num) |
| 315 | + { |
| 316 | + scan_result.num = 0; |
| 317 | + rt_free(scan_result.info); |
| 318 | + scan_result.info = RT_NULL; |
| 319 | + } |
| 320 | +} |
| 321 | + |
| 322 | +static void print_ap_info(struct rt_wlan_info *info,int index) |
| 323 | +{ |
168 | 324 | char *security;
|
| 325 | + |
| 326 | + if(index == 0) |
| 327 | + { |
| 328 | + rt_kprintf(" SSID MAC security rssi chn Mbps\n"); |
| 329 | + rt_kprintf("------------------------------- ----------------- -------------- ---- --- ----\n"); |
| 330 | + } |
169 | 331 |
|
170 |
| - num = scan_result->num; |
171 |
| - rt_kprintf(" SSID MAC security rssi chn Mbps\n"); |
172 |
| - rt_kprintf("------------------------------- ----------------- -------------- ---- --- ----\n"); |
173 |
| - for (index = 0; index < num; index ++) |
174 | 332 | {
|
175 |
| - rt_kprintf("%-32.32s", &scan_result->info[index].ssid.val[0]); |
| 333 | + rt_kprintf("%-32.32s", &(info->ssid.val[0])); |
176 | 334 | rt_kprintf("%02x:%02x:%02x:%02x:%02x:%02x ",
|
177 |
| - scan_result->info[index].bssid[0], |
178 |
| - scan_result->info[index].bssid[1], |
179 |
| - scan_result->info[index].bssid[2], |
180 |
| - scan_result->info[index].bssid[3], |
181 |
| - scan_result->info[index].bssid[4], |
182 |
| - scan_result->info[index].bssid[5] |
| 335 | + info->bssid[0], |
| 336 | + info->bssid[1], |
| 337 | + info->bssid[2], |
| 338 | + info->bssid[3], |
| 339 | + info->bssid[4], |
| 340 | + info->bssid[5] |
183 | 341 | );
|
184 |
| - switch (scan_result->info[index].security) |
| 342 | + switch (info->security) |
185 | 343 | {
|
186 | 344 | case SECURITY_OPEN:
|
187 | 345 | security = "OPEN";
|
@@ -218,15 +376,85 @@ static int wifi_scan(int argc, char *argv[])
|
218 | 376 | break;
|
219 | 377 | }
|
220 | 378 | rt_kprintf("%-14.14s ", security);
|
221 |
| - rt_kprintf("%-4d ", scan_result->info[index].rssi); |
222 |
| - rt_kprintf("%3d ", scan_result->info[index].channel); |
223 |
| - rt_kprintf("%4d\n", scan_result->info[index].datarate / 1000000); |
| 379 | + rt_kprintf("%-4d ", info->rssi); |
| 380 | + rt_kprintf("%3d ", info->channel); |
| 381 | + rt_kprintf("%4d\n", info->datarate / 1000000); |
| 382 | + } |
| 383 | + |
| 384 | +} |
| 385 | + |
| 386 | +static void user_ap_info_callback(int event, struct rt_wlan_buff *buff, void *parameter) |
| 387 | +{ |
| 388 | + struct rt_wlan_info *info = RT_NULL; |
| 389 | + int index = 0; |
| 390 | + int ret = RT_EOK; |
| 391 | + |
| 392 | + RT_ASSERT(event == RT_WLAN_EVT_SCAN_REPORT); |
| 393 | + RT_ASSERT(buff != NULL); |
| 394 | + RT_ASSERT(parameter != NULL); |
| 395 | + |
| 396 | + info = (struct rt_wlan_info *)buff->data; |
| 397 | + index = *((int *)(parameter)); |
| 398 | + |
| 399 | + ret = wifi_scan_result_cache(info); |
| 400 | + if(ret == RT_EOK) |
| 401 | + { |
| 402 | + if(scan_filter == RT_NULL || |
| 403 | + (scan_filter != RT_NULL && |
| 404 | + scan_filter->ssid.len == info->ssid.len && |
| 405 | + rt_memcmp(&scan_filter->ssid.val[0], &info->ssid.val[0], scan_filter->ssid.len) == 0)) |
| 406 | + { |
| 407 | + /*Print the info*/ |
| 408 | + print_ap_info(info,index); |
| 409 | + |
| 410 | + index++; |
| 411 | + *((int *)(parameter)) = index; |
224 | 412 | }
|
225 |
| - rt_wlan_scan_result_clean(); |
226 | 413 | }
|
227 |
| - else |
| 414 | + |
| 415 | +} |
| 416 | +static int wifi_scan(int argc, char *argv[]) |
| 417 | +{ |
| 418 | + struct rt_wlan_info *info = RT_NULL; |
| 419 | + struct rt_wlan_info filter; |
| 420 | + int ret = 0; |
| 421 | + int i = 0; |
| 422 | + |
| 423 | + if (argc > 3) |
| 424 | + return -1; |
| 425 | + |
| 426 | + if (argc == 3) |
| 427 | + { |
| 428 | + INVALID_INFO(&filter); |
| 429 | + SSID_SET(&filter, argv[2]); |
| 430 | + info = &filter; |
| 431 | + } |
| 432 | + |
| 433 | + ret = rt_wlan_register_event_handler(RT_WLAN_EVT_SCAN_REPORT,user_ap_info_callback,&i); |
| 434 | + if(ret != RT_EOK) |
| 435 | + { |
| 436 | + LOG_E("Scan register user callback error:%d!\n",ret); |
| 437 | + return 0; |
| 438 | + } |
| 439 | + |
| 440 | + if(info) |
| 441 | + { |
| 442 | + scan_filter = info; |
| 443 | + } |
| 444 | + |
| 445 | + |
| 446 | + /*Todo: what can i do for it return val */ |
| 447 | + ret = rt_wlan_scan_with_info(info); |
| 448 | + if(ret != RT_EOK) |
| 449 | + { |
| 450 | + LOG_E("Scan with info error:%d!\n",ret); |
| 451 | + } |
| 452 | + |
| 453 | + /* clean scan result */ |
| 454 | + wifi_scan_result_clean(); |
| 455 | + if(info) |
228 | 456 | {
|
229 |
| - rt_kprintf("wifi scan result is null\n"); |
| 457 | + scan_filter = RT_NULL; |
230 | 458 | }
|
231 | 459 | return 0;
|
232 | 460 | }
|
|
0 commit comments