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