blob: 4cfa044f5ca404142cfd6559f6450262458d9156 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001//#include <message.h>
2#include <limits.h>
3#include <errno.h>
4#include <wifi.h>
5#include "wlan_interface.h"
6#include "softap_api.h"
7
8#define WIFI_SSID_LEN 33
9#define WLAN_SECURITY_LEN 20
10#define WLAN_ENCRYTYPE_LEN 10
11#define WLAN_DEFAULTID_LEN 2
12#define WLAN_WEPKEY_LEN 32
13#define WLAN_PSK_LEN 65
14#define WLAN_ENCODE_PSK_LEN 100
15
16#define WF_AU_OPEN "OPEN" //wifi auth mode?
17#define WF_AU_SHARE "SHARED"
18#define WF_AU_WEPAUTO "WEPAUTO"
19#define WF_AU_WPA "WPAPSK"
20#define WF_AU_WPA2 "WPA2PSK"
21#define WF_AU_WPA_WPA2 "WPAPSKWPA2PSK"
22#define WF_AU_WAPIPSK "WAPIPSK"
23#define WF_ENCRY_NONE "NONE" //encrypt
24#define WF_ENCRY_WEP "WEP"
25#define WF_ENCRY_TKIP "TKIP"
26#define WF_ENCRY_CCMP "CCMP"
27#define WF_ENCRY_AES "AES"
28#define WF_ENCRY_TKIP_CCMP "TKIPCCMP"
29#define WF_ENCRY_TKIP_AES "TKIPAES"
30
31#define MAX_WIFI_NUM 32
32
33static int zte_station_connect_time_calc(char *timeStr);
34static void zte_get_vap_sta_list(RT_802_11_MAC_TABLE *staList);
35//int get_vap_sta_num(char *vapIface);
36
37static int zte_wlan_ssid1_basic_set(char *ssid_in)
38{
39 char *ssid = NULL;
40 char nv_tmp[WIFI_SSID_LEN] = {0};
41
42 if (NULL == ssid_in)
43 {
44 printf("input ssid is null\n");
45 return -1;
46 }
47
48 if ((!strcmp(ssid_in, "")) || (strlen(ssid_in) <= 0) || (strlen(ssid_in) > WIFI_SSID_LEN))
49 {
50 printf("input ssid is error\n");
51 return -1;
52 }
53
54 //ssid
55 memset(nv_tmp, 0, sizeof(nv_tmp));
56 //kw 3
57 cfg_get_item("SSID1", nv_tmp, sizeof(nv_tmp));
58 if (0 != strcmp(ssid_in, nv_tmp))
59 {
60 cfg_set("SSID1", ssid_in);
61 //cfg_save();
62 }
63 else
64 {
65 printf("input ssid is same \n");
66 return 1;
67 }
68
69 return 0;
70}
71
72static int zte_wlan_ssid1_security_set(char *password)
73{
74 char authMode[WLAN_SECURITY_LEN] = {0};
75 char encrypType[WLAN_ENCRYTYPE_LEN] = {0};
76 char DefaultKeyID[WLAN_DEFAULTID_LEN] = {0};
77 int changeSecurityMode = 1;
78
79 cfg_get_item("AuthMode", authMode, sizeof(authMode));
80 cfg_get_item("EncrypType", encrypType, sizeof(encrypType));
81
82 //kw 3
83 printf("zte_wlan_ssid1_security_set authMode = [%s]\n", authMode);
84 printf("zte_wlan_ssid1_security_set encrypType = [%s]\n", encrypType);
85 if(NULL == password)//&& (!(!strcmp(authMode, WF_AU_OPEN) && !strcmp(encrypType, WF_ENCRY_NONE))))
86 {
87 printf("zte_wlan_ssid1_security_set password is null\n");
88 return -1;
89 }
90#if 0 //kw 3
91 if((!strcmp(authMode, WF_AU_OPEN)) && (!strcmp(encrypType, WF_ENCRY_NONE)))
92 {
93 if(strlen(password) == 0 || (!strcmp(password, "")))
94 {
95 printf("%s, WF_AU_OPEN password len is 0\n", __FUNCTION__);
96 return 1;
97 }
98 else
99 {
100 if((strlen(password) < 8) || (strlen(password) > 64))
101 {
102 printf("%s, password len is error\n", __FUNCTION__);
103 return -1;
104 }
105 }
106 }
107 else
108 {
109 if((strlen(password) < 8) || (strlen(password) > 64) || (!strcmp(password, "")))
110 {
111 printf("%s, not open password len is error\n", __FUNCTION__);
112 return -1;
113 }
114 }
115#endif
116 if(((!strcmp(authMode, WF_AU_WPA_WPA2)) && (!strcmp(encrypType, WF_ENCRY_TKIP_AES))))
117 {
118 changeSecurityMode = 0;
119 }
120 else
121 {
122 cfg_set("AuthMode", WF_AU_WPA_WPA2);
123 cfg_set("EncrypType", WF_ENCRY_TKIP_AES);
124 }
125 if((!strcmp(authMode, WF_AU_OPEN)) || (!strcmp(authMode, WF_AU_SHARE)) || (!strcmp(authMode, WF_AU_WEPAUTO)))
126 {
127 if(((!strcmp(authMode, WF_AU_OPEN)) && (!strcmp(encrypType, WF_ENCRY_WEP))) ||
128 (!strcmp(authMode, WF_AU_SHARE)) || (!strcmp(authMode, WF_AU_WEPAUTO)))
129 {
130 int len = 0;
131 len = strlen(password);
132 if(len != 5 && len != 10 && len != 13 && len != 26) //kw 3 ;wep 5,13(no 16)
133 {
134 printf("zte_wlan_ssid1_security_set wep psk len = [%d]\n", len);
135 return -1;
136 }
137 cfg_get_item("DefaultKeyID",DefaultKeyID,sizeof(DefaultKeyID));
138 //kw 3
139 char old_wep_key[WLAN_WEPKEY_LEN] = {0};
140
141 if(!strcmp(DefaultKeyID, "1"))
142 {
143 cfg_get_item("Key2Str1",old_wep_key,sizeof(old_wep_key));
144 if(strcmp(old_wep_key, password))
145 {
146 cfg_set("Key2Str1", password);
147 //cfg_save();
148 }
149 else
150 {
151 printf("zte_wlan_ssid1_security_set wep psk is same\n");
152 return 1;
153 }
154 }
155 else if(!strcmp(DefaultKeyID, "2"))
156 {
157 cfg_get_item("Key3Str1",old_wep_key,sizeof(old_wep_key));
158
159 if(strcmp(old_wep_key, password))
160 {
161 cfg_set("Key3Str1", password);
162 //cfg_save();
163 }
164 else
165 {
166 printf("zte_wlan_ssid1_security_set wep psk is same\n");
167 return 1;
168 }
169 }
170 else if(!strcmp(DefaultKeyID, "3"))
171 {
172 cfg_get_item("Key4Str1",old_wep_key,sizeof(old_wep_key));
173 if(strcmp(old_wep_key, password))
174 {
175 cfg_set("Key4Str1", password);
176 //cfg_save();
177 }
178 else
179 {
180 printf("zte_wlan_ssid1_security_set wep psk is same\n");
181 return 1;
182 }
183 }
184 else
185 {
186 cfg_get_item("Key1Str1",old_wep_key,sizeof(old_wep_key));
187 if(strcmp(old_wep_key, password))
188 {
189 cfg_set("Key1Str1", password);
190 //cfg_save();
191 }
192 else
193 {
194 printf("zte_wlan_ssid1_security_set wep psk is same\n");
195 return 1;
196 }
197 }
198
199 }
200 else if(!strcmp(authMode, WF_AU_OPEN) && !strcmp(encrypType, WF_ENCRY_NONE))
201 {
202 printf("authMode is open and not wep \n");
203 return 1;
204
205 }
206 }
207 else if(!strcmp(authMode, WF_AU_WPA) || !strcmp(authMode, WF_AU_WPA2) || !strcmp(authMode, WF_AU_WPA_WPA2))
208 {
209 char psk[WLAN_PSK_LEN] = {0};
210 int len = 0;
211 char encode[WLAN_ENCODE_PSK_LEN] = {0};
212 len = strlen(password);
213
214 if(len < 8 || len > 64)
215 {
216 printf("%s password len is %d \n",authMode, len);
217 return -1;
218 }
219 cfg_get_item("WPAPSK1",psk,sizeof(psk));
220 //kw 3
221 if(strcmp(psk, password))
222 {
223 cfg_set("WPAPSK1", password);
224 base64_encode(password, len, encode, WLAN_ENCODE_PSK_LEN);
225 cfg_set("WPAPSK1_encode", encode);
226 //cfg_save();
227 }
228 else
229 {
230 if(!changeSecurityMode)
231 {
232 printf("%s, wpapskwpa2psk psk is same\n", __FUNCTION__);
233 return 1;
234 }
235 }
236
237 }
238 else if(!strcmp(authMode, WF_AU_WAPIPSK))
239 {
240 char psk[WLAN_PSK_LEN] = {0};
241 int len = 0;
242 char encode[WLAN_ENCODE_PSK_LEN] = {0};
243 len = strlen(password);
244
245 if(len < 8 || len > 64)
246 {
247 printf("WF_AU_WAPIPSK password len is %d \n", len);
248 return -1;
249 }
250 cfg_get_item("WPAPSK1",psk,sizeof(psk));
251 //kw 3
252 if(strcmp(psk, password))
253 {
254 cfg_set("WPAPSK1", password);
255 base64_encode(password, len, encode, WLAN_ENCODE_PSK_LEN);
256 cfg_set("WPAPSK1_encode", encode);
257 //cfg_save();
258 }
259 else
260 {
261 if(!changeSecurityMode)
262 {
263 printf("%s, wapi psk is same\n", __FUNCTION__);
264 return 1;
265 }
266 }
267
268 }
269 else
270 {
271 // other security mode is invalid
272 printf("ERROR:security mode is invalid\n");
273 return -1;
274 }
275
276 return 0;
277}
278
279int zte_wlan_get_ssid1_settings(char* ssid, char* password, int ssid_len, int password_len)
280{
281 char authMode[WLAN_SECURITY_LEN] = {0};
282 char encrypType[WLAN_ENCRYTYPE_LEN] = {0};
283 char DefaultKeyID[WLAN_DEFAULTID_LEN] = {0};
284
285 if(NULL == ssid || NULL == password)
286 {
287 printf("ERROR:input ssid && password is null\n");
288 return -1;
289 }
290
291 //kw 3
292 char old_ssid[WIFI_SSID_LEN] = {0};
293 cfg_get_item("SSID1",old_ssid,sizeof(old_ssid));
294 strncpy(ssid, old_ssid, ssid_len - 1);
295
296 cfg_get_item("AuthMode",authMode,sizeof(authMode));
297 cfg_get_item("EncrypType",encrypType,sizeof(encrypType));
298
299 printf("zte_wlan_ssid1_security_get authMode = [%s]\n", authMode);
300 printf("zte_wlan_ssid1_security_get encrypType = [%s]\n", encrypType);
301
302 if((!strcmp(authMode, WF_AU_OPEN)) || (!strcmp(authMode, WF_AU_SHARE)) || (!strcmp(authMode, WF_AU_WEPAUTO)))
303 {
304 if(((!strcmp(authMode, WF_AU_OPEN)) && (!strcmp(encrypType, WF_ENCRY_WEP))) ||
305 (!strcmp(authMode, WF_AU_SHARE)) || (!strcmp(authMode, WF_AU_WEPAUTO)))
306 {
307 cfg_get_item("DefaultKeyID",DefaultKeyID,sizeof(DefaultKeyID));
308
309 char old_wep_key[WLAN_WEPKEY_LEN] = {0};
310 if(!strcmp(DefaultKeyID, "1"))
311 {
312 cfg_get_item("Key2Str1",old_wep_key,sizeof(old_wep_key));
313 strncpy(password, old_wep_key, password_len-1);
314 }
315 else if(!strcmp(DefaultKeyID, "2"))
316 {
317 cfg_get_item("Key3Str1",old_wep_key,sizeof(old_wep_key));
318 strncpy(password, old_wep_key, password_len-1);
319 }
320 else if(!strcmp(DefaultKeyID, "3"))
321 {
322 cfg_get_item("Key4Str1",old_wep_key,sizeof(old_wep_key));
323 strncpy(password, old_wep_key, password_len-1);
324 }
325 else
326 {
327 cfg_get_item("Key1Str1",old_wep_key,sizeof(old_wep_key));
328 strncpy(password, old_wep_key, password_len-1);
329 }
330
331 }
332 }
333 else if(!strcmp(authMode, WF_AU_WPA) || !strcmp(authMode, WF_AU_WPA2) || !strcmp(authMode, WF_AU_WPA_WPA2))
334 {
335 char psk[WLAN_PSK_LEN] = {0};
336 cfg_get_item("WPAPSK1",psk,sizeof(psk));
337 strncpy(password, psk, password_len-1);
338 }
339 else if(!strcmp(authMode, WF_AU_WAPIPSK))
340 {
341 char psk[WLAN_PSK_LEN] = {0};
342 cfg_get_item("WPAPSK1",psk,sizeof(psk));
343 strncpy(password, psk, password_len-1);
344 }
345 else
346 {
347 return -1;
348 }
349
350 return 0;
351}
352
353int zte_wlan_ssid1_set(char* ssid, char* password)
354{
355 int ret_for_basic = 0;
356 int ret_for_security = 0;
357
358 ret_for_basic = zte_wlan_ssid1_basic_set(ssid);
359 if(-1 == ret_for_basic)
360 {
361 printf("zte_wlan_ssid1_basic_set is false\n");
362 return -1;
363 }
364 ret_for_security = zte_wlan_ssid1_security_set(password);
365 if(-1 == ret_for_security)
366 {
367 printf("call zte_lan_ssid1_security_set failure .\n");
368 return -1;//kw 3
369 }
370
371 if(ret_for_basic == 0 || ret_for_security == 0)
372 {
373 if(0 == ipc_send_message(MODULE_ID_WLAN_SERVER, MODULE_ID_WIFI, MSG_CMD_WIFI_ADVANCED, 2, "1", 0))
374 {
375 //cfg_set("RadioOff", "0");
376 printf("wifi set cmd done!\n");
377 }
378 else
379 {
380 printf("wifi set cmd zte_wlan_ssid1_set error!\n");
381 return -1;
382 }
383 }
384 return 0;
385}
386
387//wifi_hal.c of realtek, in order to capture sta num of va0 and va1(if multi ssid is enabled)
388int zte_wlan_capture_sta_num()
389{
390 int staTotalNum = 0;
391 char wifi_cur_state[8] = {0};
392 char sta_count[8] = {0};
393
394 cfg_get_item("wifi_cur_state",wifi_cur_state,sizeof(wifi_cur_state));
395
396 if( !strncmp(wifi_cur_state,"0",1) )
397 {
398 printf("[%s]wifi is off, can't obtain sta info!!\n",__FUNCTION__);
399 return 0;
400 }
401 cfg_get_item("sta_count",sta_count,sizeof(sta_count));
402
403 staTotalNum = atoi(sta_count);
404
405 printf("[%s] staTotalNum = %d\n",__FUNCTION__,staTotalNum);
406 return staTotalNum;
407 }
408
409
410static int zte_station_connect_time_calc(char* timeStr)
411{
412 //timeStr : [1 hr 2 min 3 sec]
413 //char timeStr[32] = {0};
414 char *splited_val = NULL;
415 char *splited_str = NULL;
416 char *delimiter = " ";
417 char *save = NULL;
418 int time = 0;
419 int splited_int = 0;
420
421 //strncpy(timeStr, str, 31);
422 //timeStr[31] = '\0';
423 printf("timeStr %s\n", timeStr);
424 splited_val = strtok_r(timeStr, delimiter, &save);
425 if(NULL != splited_val)
426 {
427 printf("splited_val is %s\n", splited_val);
428 splited_str = strtok_r(NULL, delimiter, &save);
429 }
430 else
431 {
432 printf("splited_val is NULL\n");
433 }
434
435 while(NULL != splited_val && NULL != splited_str)
436 {
437 printf("val %s, str %s\n", splited_val, splited_str);
438 splited_int = atoi(splited_val);
439
440 if(splited_int < 0 || splited_int > INT_MAX-1) {//kw 3
441 printf("[%s]splited_int error: %d!!\n",__FUNCTION__,splited_int);
442 return 0;
443 }
444 if(0 == strncmp(splited_str, "hr", 2))
445 {
446 time += splited_int * 60 * 60;
447 }else if(0 == strncmp(splited_str, "min", 3))
448 {
449 time += splited_int * 60;
450 }else if(0 == strncmp(splited_str, "sec", 3))
451 {
452 time += splited_int;
453 }
454 splited_val = strtok_r(NULL, delimiter, &save);
455 if(NULL != splited_val)
456 {
457 splited_str = strtok_r(NULL, delimiter, &save);
458 }
459 }
460 char temp[32] = {0};
461 sprintf(temp, "%d", time);
462 cfg_set("link_time", temp);
463 return time;
464}
465
466int copyfile(const char *source, const char * target)
467{
468 FILE *fp_s = NULL;
469 FILE *fp_t = NULL;
470 char buf[1024]={0};
471 int lenR = 0;
472 int lenW=0;
473
474 if( NULL== source || NULL == target) return -1;
475 if((fp_s = fopen(source,"r"))== NULL)
476 {
477 printf("[copyfile]sunwenming %s can not be open\n", source);
478 return -1;
479 }
480
481 if((fp_t = fopen(target,"w"))== NULL)
482 {
483 printf("[copyfile]sunwenming %s can not be open\n", target);
484 fclose(fp_s); //kw 3
485 return -1;
486 }
487
488 while((lenR = fread(buf, 1, 1024, fp_s)) > 0)
489 {
490 if((lenW = fwrite(buf, 1, lenR, fp_t)) != lenR)
491 {
492 printf("[copyfile]sunwenming write to %s failed\n", target);
493 fclose(fp_s); //kw 3
494 fclose(fp_t);
495 return -1;
496 }
497 memset(buf, 0, 1024);
498 }
499 //kw 3
500 fclose(fp_s);
501 fclose(fp_t);
502 return 0;
503}
504
505
506static void zte_get_vap_sta_list(RT_802_11_MAC_TABLE *staList)
507{
508 char station_mac_tmp[MAX_NUMBER_OF_MAC*20] = {0};
509 char sta_count[8] = {0};
510 char * p = NULL;
511 int i =0;
512 char mac_tmp[18] = {0};
513 cfg_get_item("station_mac", station_mac_tmp, sizeof(station_mac_tmp));
514 cfg_get_item("sta_count", sta_count, sizeof(sta_count));
515
516 staList->Num = atoi(sta_count);
517 if (staList->Num > MAX_NUMBER_OF_MAC) {//kw
518 printf("sta_count=%s, Num=%d\n", sta_count, staList->Num);
519 return;
520 }
521
522 p = station_mac_tmp;
523 printf("station_mac=%s\n", station_mac_tmp);
524 for(i=0;i < staList->Num; i++ )
525 {
526
527 unsigned char addr[6] = {0};
528 memset(mac_tmp, 0, 18);
529 strncpy(mac_tmp, p, 17); p+= 18;
530 printf("mac_tmp=%s\n", mac_tmp);
531 sscanf(mac_tmp, "%2x:%2x:%2x:%2x:%2x:%2x", (unsigned int *)&addr[0], (unsigned int *)&addr[1],
532 (unsigned int *)&addr[2],(unsigned int *)&addr[3],(unsigned int *)&addr[4],(unsigned int *)&addr[5]);//kw
533 printf("[addr=]%2x:%2x:%2x:%2x:%2x:%2x\n", addr[0], addr[1],
534 addr[2],addr[3],addr[4],addr[5]);
535
536 memcpy((void*)(staList->Entry[i].Addr), (void*)addr, 6);
537 }
538
539
540}
541
542void zte_get_wifi_sta_list(RT_802_11_MAC_TABLE *staList)
543{
544 int i = 0;
545 char wifi_cur_state[8]={0};
546
547 cfg_get_item("wifi_cur_state",wifi_cur_state,sizeof(wifi_cur_state));
548 if(atoi(wifi_cur_state) == 0)
549 {
550 printf("wifi is Off, can not read sta_info\n");
551 return;
552 }
553
554 zte_get_vap_sta_list(staList);
555
556}
557#if 0 //kw 3
558int get_channel()
559{
560 char channel[8] = {0};
561 pipecmd("iwpriv wlan0 get_mib channel |cut -d':' -f2", channel);
562 return atoi(channel);
563}
564#endif