hc
2024-03-22 f63cd4c03ea42695d5f9b0e1798edd196923aae6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include "wl.h"
 
#define DEFAULT_IFNAME                "wlan0"
#define MAX_SCAN_NUM                10
#define SCAN_DWELL_TIME_PER_CHANNEL    (200)
 
static void usage(void)
{
   printf("usage:\n"
          "    scan [-i <ifname>] [-t <scan_time_per_channel>]\n");
}
 
int print_scan(uint32_t time_per_channel)
{
   int ret;
 
   ret = wl_scan(NULL, NULL, NULL, 0, time_per_channel);
 
   if (ret == WL_E_OK)
   {
       int total = 0;
       printf("==================================================================\n");
       printf("| No.\t| SSID\t\t\t| BSSID\t\t\t\t| Security\t| Channel |\n");
       printf("------------------------------------------------------------------\n");
 
       while (1)
       {
           wl_ap_info_t ap_info[MAX_SCAN_NUM];
           uint32_t count = MAX_SCAN_NUM;
           bool is_completed = false;
 
           ret = wl_get_scan_results(ap_info, &count, &is_completed);
 
           if (ret < 0)
           {
               printf("wl_get_scan_results failed, result = %d\n", ret);
               break;
           }
           else
           {
               int i;
 
               for (i = 0; i < count; i ++)
               {
                   printf("| %d\t| %s\t| %02x:%02x:%02x:%02x:%02x:%02x\t| 0x%08x\t| %d\t\t|\n",
                       total,
                       ap_info[i].ssid.value,
                       ap_info[i].bssid.octet[0], ap_info[i].bssid.octet[1], ap_info[i].bssid.octet[2],
                       ap_info[i].bssid.octet[3], ap_info[i].bssid.octet[4], ap_info[i].bssid.octet[5],
                       ap_info[i].security,
                       ap_info[i].channel);
                   total++;
               }        
           }
 
           if (is_completed)
           {
               break;
           }
       }
       printf("==================================================================\n");
   }
   else
   {
       printf("wl_scan err = %d\n", ret);
   }
 
   return ret;
}
 
 
int main(int argc, char **argv)
{
   int ret;
   char *ifname = DEFAULT_IFNAME; 
   uint32_t time_per_channel = WL_SCAN_PER_CH_TIME_DEFAULT;
 
   argv++;
 
   while (*argv)
   {
       if (!strcmp(*argv, "-i"))
       {
           argv++;
 
           if (argv != NULL)
           {
               ifname = *argv;
           }
           else
           {
               usage();
               return 0;
           }
       }
       else if (!strcmp(*argv, "-t"))
       {
           argv++;
 
           if (argv != NULL)
           {
               time_per_channel = atoi(*argv);
           }
           else
           {
               usage();
               return 0;
           }
       }
       else
       {
           usage();
           return 0;
       }
 
       argv++;
   }
   
   ret = wl_init(ifname);
   if (ret < 0)
   {
       printf("wl_init failed.\n");    
       return 0;
   }
 
   ret = print_scan(time_per_channel);
 
   wl_deinit();
   
   return 0;
}