hc
2025-02-14 bbb9540dc49f70f6b703d1c8d1b85fa5f602d86e
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 *  wlan_test.c  --  wlan test application
 *
 *  Copyright (c) 2017 Rockchip Electronics Co. Ltd.
 *  Author: Panzhenzhuan Wang <randy.wang@rock-chips.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//WLAN test program
#include <stdio.h>
 
//open()Ïà¹ØÍ·Îļþ
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
//errorÏà¹ØÍ·Îļþ
#include <errno.h>
#include <string.h>
#include "wlan_test.h"
 
#define LOG_TAG "wlan_test"
#include "common.h"
#define WLAN_PROC_ERR -45
 
#define WLAN_START_UP_COMMAND "ifconfig wlan0 up"
#define WLAN_MANAGE_START "wpa_supplicant -i wlan0 -c /etc/wpa_supplicant.conf &"
#define WLAN_SCAN_COMMAND "wpa_cli -i wlan0 scan 'wlan0'"
#define WLAN_SCAN_RESULT  "wpa_cli -i wlan0 scan_r | busybox awk 'NR>=3{print $5,$3}'"
#define SCAN_RESULT_FILE "/tmp/wlan_scan_result.txt"
 
#define AP_SUPPORT_CMD "iw list | busybox grep AP > %s"
#define AP_SUPPORT_FILE "/tmp/wlan_ap_result.txt"
#define SOFTAP_MSG "dmesg | grep SSID | tail -n 6 > %s"
#define MSG_FILE "/tmp/softap_start_msg.txt"
 
#ifdef PCBA_PX3SE
#define CONNECT_AP_CMD "wpa_supplicant -i wlan0 -c /data/wpa_conf &"
#define CONNECT_AP_STATUS "wpa_cli status > %s"
#define CONNECT_AP_RESULT_FILE "/tmp/wlan_connect_result.txt"
 
#define REPORT_SSID     "Rockchip" //Ö¸¶¨É¨ÃèµÄµÄwifi ssidÃû³Æ
#endif
 
//* 1¡¢¹Ø±Õºǫ́wpa_supplicant½ø³Ì
static int close_wpa_supplicant(void)
{
    int test_flag = -1;
    char pid_buf[64];
    char cmd[64];
    FILE* pp;
    printf("====================function : %s start =================\n",__func__);
    pp = popen("ps |grep wpa_supplicant|awk 'NR==1 {print $1}'","r");
    //pp = popen("ps | grep wpa_supplicant | grep -v grep","r");
    //Èç¹ûÎļþ´ò¿ªÊ§°Ü£¬ÔòÊä³ö´íÎóÐÅÏ¢
    if (!pp)
    {
        printf("%s popen err%s\n",__func__,strerror(errno));
        return -1;
    }
    memset(pid_buf,0,sizeof(pid_buf));
    fgets(pid_buf,sizeof(pid_buf),pp);
    pclose(pp);
    printf("Get pid_buf is: \t %s\n",pid_buf);
    sprintf(cmd,"kill -9 %d",atoi(pid_buf));
    printf("cmd is: %s\n",cmd);
    system(cmd);
    printf("====================function : %s finish =================\n",__func__);
    return 0;
}
 
/*
 * RSSI Levels as used by notification icon
 *
 * Level 4  -55 <= RSSI
 * Level 3  -66 <= RSSI < -55
 * Level 2  -77 <= RSSI < -67
 * Level 1  -88 <= RSSI < -78
 * Level 0         RSSI < -88
 */
static int calc_rssi_lvl(int rssi)
{
   if (rssi >= -55)
       return 4;
   else if (rssi >= -66)
       return 3;
   else if (rssi >= -77)
       return 2;
   else if (rssi >= -88)
       return 1;
   else
       return 0;
}
 
 
/* ½«É¨Ãèµ½µÄWiFiÐÅÏ¢Êä³öµ½path¶ÔÓ¦Îļþ
 * ²¢ÏÔʾµÚÒ»¸öWiFiÃû×ÖºÍÐźÅÇ¿¶È
*/
int wlan_exec(const char *cmd, char *path)
{
    char result_buf[128];
    char ssid[128];
    int signal_level;
    int ch=0;
    int fd;
 
     printf("=================== function :%s start======================\n\n",__func__);
 
    memset(result_buf, 0, sizeof(result_buf));
    memset(ssid, 0, sizeof(ssid));
    //ÏÔʾµÚÒ»¸öWiFiÐÅÏ¢
    FILE *pp = popen(cmd, "r");
    //Èç¹ûÎļþ´ò¿ªÊ§°Ü£¬ÔòÊä³ö´íÎóÐÅÏ¢
    if (!pp)
    {
        printf("%s popen err%s\n",__func__,strerror(errno));
        return -1;
    }
 
#ifdef PCBA_PX3SE
    /*chad.ma add below*/
    if (path != NULL) {
        fd = open(path, O_CREAT | O_WRONLY    | O_TRUNC);
        if (fd <0) {
            log_err("open %s fail, errno = %d\n", path, errno);
            return errno;
        }
    }
    /*chad.ma add up*/
#endif
 
   while(!feof(pp))
   {
       fscanf(pp,"%s",ssid);
       fscanf(pp,"%d",&signal_level);
       //¸ù¾ÝÐźÅÇ¿¶ÈÅжÏWiFiÊÇ·ñÕý³£¹¤×÷
       if(signal_level< -200||signal_level >=0)
        {
            printf("wlan scan result is null\n");
            return -1;
        }
#ifdef PCBA_PX3SE
        snprintf(result_buf,sizeof(result_buf),
                    "SSID:%s Signal Level:%d RSSI:%d\n",
                    ssid, signal_level, calc_rssi_lvl(signal_level));
        int w_len = write(fd, result_buf, strlen(result_buf));
       if (w_len <= 0) {
           log_err("Write %s fail, errno = %d\n", path, errno);
           return errno;
       }
#endif
 
       printf("SSID is: %s,signal level is: %d\n",ssid,signal_level);
       printf("SSID is: %s,rssi is: %d\n",ssid,calc_rssi_lvl(signal_level));
   }
   pclose(pp);
 
#ifdef PCBA_PX3SE
    close(fd);
#endif
   printf("\n=================== function :%s finish======================\n",__func__);
   return 0;
}
 
void *wlan_test(void *argv)
{
    int test_flag=-1;
    char cmd[128];
    printf("===================Wlan test start======================\n");
    //sprintf(cmd,"aplay %s/wlan_test_start.wav",AUDIO_PATH);
    //system(cmd);
    //system("aplay /data/cfg/test/wlan_test_start.wav");
 
    //1¡¢Ïȹرպǫ́wpa_supplicant½ø³Ì£¬È»ºóÏÈÐ¶ÔØWLANÇý¶¯¹Ì¼þ£¬È»ºóÖØÐ¼ÓÔØÇý¶¯¹Ì¼þ
    test_flag = close_wpa_supplicant();
    system("echo 1 > /sys/class/rkwifi/driver");
 
    //2¡¢¿ªÆôwlan0¶Ë¿Ú
    int status = system(WLAN_START_UP_COMMAND);
 
    /* add by chad ma 20180629 below judge*/
    if(status == -1) {
        printf("system run error ....\n");
        goto fail;
    } else {
        log_info("exit status value = [0x%x]\n", status);
        if (WIFEXITED(status))
        {
            if (0 == WEXITSTATUS(status))
            {
                log_info("run WLAN_START_UP_COMMAND successfully.\n");
                //ret = 0;
            }
            else
            {
                system("rfkill unblock wifi");
            }
        }
        else
        {
            log_info("exit status = [%d]\n", WEXITSTATUS(status));
 
            //ret = -1;
        }
    }
    sleep(2);
 
    //3¡¢Æô¶¯ÎÞÏßÍø¿¨¹ÜÀí³ÌÐòwpa_supplicant
    system(WLAN_MANAGE_START);
    sleep(2);
 
    //4¡¢É¨ÃèÎÞÏßÍøÂç
    system(WLAN_SCAN_COMMAND);
    sleep(3);
 
    //5¡¢ÏÔʾ³öWiFiÐÅÏ¢
    test_flag = wlan_exec(WLAN_SCAN_RESULT,SCAN_RESULT_FILE);
    if(test_flag<0)
    {
        //µÚÒ»´Îscanʧ°Ü£¬ÔÙÖ´ÐÐÒ»±é;
        system(WLAN_SCAN_COMMAND);
        sleep(2);
        test_flag = wlan_exec(WLAN_SCAN_RESULT,SCAN_RESULT_FILE);
        if(test_flag<0)
        goto fail;
        //return -1;
    }
 
    printf("===================Wlan test success ======================\n");
    system("ifconfig wlan0 down");
    system("busybox killall wpa_supplicant");      //¹Ø±ÕÎÞÏß¹ÜÀí³ÌÐòwpa_supplicant
    return (void*)test_flag;
fail:
    printf("===================Wlan test failed======================\n");
    system("ifconfig wlan0 down");
    system("busybox killall wpa_supplicant");      //¹Ø±ÕÎÞÏß¹ÜÀí³ÌÐòwpa_supplicant
    return (void*)test_flag;
}
 
int main(int argc, char *argv[])
{
    int test_flag = 0,err_code = 0;
    char read_buf[128];
    int find_flag = 0;
    FILE *fp;
    char buf[COMMAND_VALUESIZE] = "wlan_test";
    char result[COMMAND_VALUESIZE] = RESULT_PASS;
    test_flag = (int)wlan_test(argv[0]);
    if(test_flag < 0)
    {
        strcpy(result,RESULT_FAIL);
        err_code = WLAN_PROC_ERR;
    }
 
#ifdef PCBA_PX3SE
    //´ÓɨÃè½á¹û±£´æÎļþÖУ¬É¨ÃèÖ¸¶¨ssidµÄsignal
    fp = fopen(SCAN_RESULT_FILE, "r");
    memset(read_buf, 0 ,sizeof(read_buf));
     //Èç¹ûÎļþ´ò¿ªÊ§°Ü£¬ÔòÊä³ö´íÎóÐÅÏ¢
    if (!fp)
    {
        printf("%s fopen err:%s\n",__func__,strerror(errno));
        return -1;
    }
 
    while(!feof(fp))
    {
        fgets(read_buf,sizeof(read_buf),fp);
        if(strstr(read_buf,REPORT_SSID)!= NULL)
        {
            find_flag = 1;
            //×¢Ò⣺read_buf»á½«Ò»ÐеĻسµ·ûÒ²¶Á½øÈ¥£¬ÕâÀïÒª¶Ôread_buf½øÐиÄÔì
            int len = strlen(read_buf);
            if ( len < 128) {
                //assert(len < 128);
                read_buf[len - 1] = '\0';
                printf("\n ###Found!!! %s ###\n",read_buf);
                break;
            } else {
                printf("%s ERROR!!! read to much from one line\n",__func__);
            }
        }
    }
    fclose(fp);
    //½«±£´æ½á¹û¸´ÖƵ½bufÖÐ
    if (find_flag) {
        strcat(buf, ": ");
        strcat(buf, read_buf);
    }
 
    //ɾ³ýɨÃèÎļþ
    //remove(SCAN_RESULT_FILE);
    printf("\n $$ %s :not found %s $$\n",buf , REPORT_SSID);
#endif
    send_msg_to_server(buf, result, err_code);
}