1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5
6#include <bcmnvram.h>
7#include <shutils.h>
8
9#include "shared.h"
10#include "web-qtn.h"
11#include "net80211/ieee80211_dfs_reentry.h"
12
13#ifdef RTCONFIG_JFFS2ND_BACKUP
14#include <sys/mount.h>
15#include <sys/statfs.h>
16#endif
17
18static int lock_qtn_apscan = -1;
19
20extern int isValidCountryCode(const char *Ccode);
21extern int isValidRegrev(char *regrev);
22extern int isValidMacAddr(const char* mac);
23extern int file_lock(char *tag);
24extern void file_unlock(int lockfd);
25extern void char_to_ascii(const char *output, const char *input);
26
27#define	WIFINAME	"wifi0"
28#if 0
29void inc_mac(char *mac, int plus);
30#endif
31
32struct txpower_ac_qtn_s {
33	uint16 min;
34	uint16 max;
35	uint8 pwr;
36};
37
38static const struct txpower_ac_qtn_s txpower_list_qtn_rtac87u[] = {
39#if !defined(RTCONFIG_RALINK)
40	/* 1 ~ 25% */
41	{ 1, 25, 14},
42	/* 26 ~ 50% */
43	{ 26, 50, 17},
44	/* 51 ~ 75% */
45	{ 51, 75, 20},
46	/* 76 ~ 100% */
47	{ 76, 100, 23},
48#endif	/* !RTCONFIG_RALINK */
49	{ 0, 0, 0x0}
50};
51
52int
53setCountryCode_5G_qtn(const char *cc)
54{
55	int ret;
56	char value[20] = {0};
57
58	if( cc==NULL || !isValidCountryCode(cc) )
59		return 0;
60
61	if (!rpc_qtn_ready()) {
62		fprintf(stderr, "ATE command error\n");
63		return -1;
64	}
65	ret = qcsapi_bootcfg_update_parameter("ccode_5g", cc);
66	if (ret < 0) {
67		fprintf(stderr, "ATE command error\n");
68		return -1;
69	}
70	ret = qcsapi_bootcfg_get_parameter("ccode_5g", value, sizeof(value));
71	if (ret < 0) {
72		fprintf(stderr, "ATE command error\n");
73		return -1;
74	}
75
76	if (!factory_debug_raw()) {
77		fprintf(stderr, "ATE command error\n");
78		return -1;
79	}
80
81	ATE_BRCM_SET("1:ccode", cc);
82	puts(nvram_safe_get("1:ccode"));
83
84	return 1;
85}
86
87int
88getCountryCode_5G_qtn(void)
89{
90	puts(cfe_nvram_safe_get_raw("1:ccode"));
91
92	return 0;
93}
94
95int setRegrev_5G_qtn(const char *regrev)
96{
97	int ret;
98	char value[20] = {0};
99
100	if( regrev==NULL || !isValidRegrev((char *)regrev) )
101		return 0;
102
103	if (!rpc_qtn_ready()) {
104		fprintf(stderr, "ATE command error\n");
105		return -1;
106	}
107	ret = qcsapi_bootcfg_update_parameter("regrev_5g", regrev);
108	if (ret < 0) {
109		fprintf(stderr, "ATE command error\n");
110		return -1;
111	}
112	ret = qcsapi_bootcfg_get_parameter("regrev_5g", value, sizeof(value));
113	if (ret < 0) {
114		fprintf(stderr, "ATE command error\n");
115		return -1;
116	}
117
118	if (!factory_debug_raw()) {
119		fprintf(stderr, "ATE command error\n");
120		return -1;
121	}
122
123	ATE_BRCM_SET("1:regrev", regrev);
124	puts(nvram_safe_get("1:regrev"));
125	return 1;
126}
127
128int
129getRegrev_5G_qtn(void)
130{
131	puts(cfe_nvram_safe_get_raw("1:regrev"));
132	return 0;
133}
134
135int setMAC_5G_qtn(const char *mac)
136{
137	int ret;
138	char value[20] = {0};
139
140	if( mac==NULL || !isValidMacAddr(mac) )
141		return 0;
142
143	if (!rpc_qtn_ready())
144	{
145		fprintf(stderr, "ATE command error\n");
146		return -1;
147	}
148	ret = qcsapi_bootcfg_update_parameter("ethaddr", mac);
149	if (ret < 0) {
150		fprintf(stderr, "ATE command error\n");
151		return -1;
152	}
153#if 0
154	inc_mac(mac, 1);
155#endif
156	ret = qcsapi_bootcfg_update_parameter("wifiaddr", mac);
157	if (ret < 0) {
158		fprintf(stderr, "ATE command error\n");
159		return -1;
160	}
161	ret = qcsapi_bootcfg_get_parameter("ethaddr", value, sizeof(value));
162	if (ret < 0) {
163		fprintf(stderr, "ATE command error\n");
164		return -1;
165	}
166
167	if (!factory_debug_raw()) {
168		fprintf(stderr, "ATE command error\n");
169		return -1;
170	}
171
172	ATE_BRCM_SET("1:macaddr", mac);
173	// puts(nvram_safe_get("1:macaddr"));
174
175	puts(value);
176	return 1;
177}
178
179int getMAC_5G_qtn(void)
180{
181	int ret;
182	char value[20] = {0};
183
184	if (!rpc_qtn_ready()) {
185		fprintf(stderr, "ATE command error\n");
186		return -1;
187	}
188	ret = qcsapi_bootcfg_get_parameter("ethaddr", value, sizeof(value));
189	if (ret < 0) {
190		fprintf(stderr, "ATE command error\n");
191		return -1;
192	}
193	puts(value);
194	return 1;
195}
196
197int setAllLedOn_qtn(void)
198{
199	int ret;
200
201	if (!rpc_qtn_ready()) {
202		fprintf(stderr, "ATE command error\n");
203		return -1;
204	}
205	ret = qcsapi_led_set(1, 1);
206	if (ret < 0) {
207		fprintf(stderr, "ATE command error\n");
208		return -1;
209	}
210	ret = qcsapi_wifi_run_script("router_command.sh", "lan4_led_ctrl on");
211	if (ret < 0) {
212		fprintf(stderr, "ATE command error\n");
213		return -1;
214	}
215	return 0;
216}
217
218int setAllLedOff_qtn(void)
219{
220	int ret;
221
222	if (!rpc_qtn_ready()) {
223		fprintf(stderr, "ATE command error\n");
224		return -1;
225	}
226	ret = qcsapi_wifi_run_script("router_command.sh", "wifi_led_off");
227	if (ret < 0) {
228		fprintf(stderr, "[led] router_command.sh: wifi_led_off error\n");
229		return -1;
230	}
231
232	ret = qcsapi_led_set(1, 0);
233	if (ret < 0) {
234		fprintf(stderr, "ATE command error\n");
235		return -1;
236	}
237
238	ret = qcsapi_wifi_run_script("router_command.sh", "lan4_led_ctrl off");
239	if (ret < 0) {
240		fprintf(stderr, "ATE command error\n");
241		return -1;
242	}
243	return 0;
244}
245
246int Get_channel_list_qtn(int unit)
247{
248	int ret;
249	string_1024 list_of_channels;
250	char cur_ccode[20] = {0};
251
252	if (!rpc_qtn_ready()) {
253		fprintf(stderr, "ATE command error\n");
254		return -1;
255	}
256	ret = qcsapi_wifi_get_regulatory_region("wifi0", cur_ccode);
257	if (ret < 0) {
258		fprintf(stderr, "ATE command error\n");
259		return -1;
260	}
261	ret = qcsapi_regulatory_get_list_regulatory_channels(cur_ccode, 20 /* bw */, list_of_channels);
262	if (ret < 0) {
263		fprintf(stderr, "ATE command error\n");
264		return -1;
265	}
266	puts(list_of_channels);
267
268	return 1;
269}
270
271int Get_ChannelList_5G_qtn(void)
272{
273	return Get_channel_list_qtn(1);
274}
275
276// format : [Band, SSID, channel, security, encryption, RSSI, MAC, 802.11xx, hidden]
277void show_ap_properties(const qcsapi_unsigned_int index, const qcsapi_ap_properties *params, char *buff)
278{
279	int channel 	= params->ap_channel;
280	int wpa_mask	= params->ap_protocol;
281	int psk_mask	= params->ap_authentication_mode;
282	int tkip_mask	= params->ap_encryption_modes;
283	int proto 	= params->ap_80211_proto;
284	int hidden;
285	char band[8], ssid[256], security[32], auth[16], crypto[32], wmode[8];
286	char mac[24];
287
288	// MAC
289	sprintf(&mac[0], "%02X:%02X:%02X:%02X:%02X:%02X",
290		params->ap_mac_addr[0],
291		params->ap_mac_addr[1],
292		params->ap_mac_addr[2],
293		params->ap_mac_addr[3],
294		params->ap_mac_addr[4],
295		params->ap_mac_addr[5]
296	);
297
298	// Band
299	if(channel > 15)
300		strcpy(band, "5G");
301	else
302		strcpy(band, "2G");
303
304	// SSID
305	if(!strcmp(params->ap_name_SSID, ""))
306		strcpy(ssid, "");
307	else{
308		memset(ssid, 0, sizeof(ssid));
309		char_to_ascii(ssid, params->ap_name_SSID);
310	}
311
312	// security and authentication : check wpa_mask and psk_mask
313	// 	wpa_mask : 0x01 = WPA, 0x02 = WPA2
314	// 	psk_mask : 0x01 = psk, 0x02 = enterprise
315	if((wpa_mask == 0x1) && (psk_mask == 0x01)){
316		strcpy(security, "WPA-Personal");
317		strcpy(auth, "PSK");
318	}
319	else if((wpa_mask == 0x2) && (psk_mask == 0x01)){
320		strcpy(security, "WPA2-Personal");
321		strcpy(auth, "PSK");
322	}
323	else if((wpa_mask == 0x3) && (psk_mask == 0x01)){
324		strcpy(security, "WPA-Auto-Personal");
325		strcpy(auth, "PSK");
326	}
327	else if((wpa_mask == 0x0) && (psk_mask == 0x0)){
328		strcpy(security, "Open System");
329		strcpy(auth, "NONE");
330	}
331	else{
332		strcpy(security, "");
333		strcpy(auth, "");
334	}
335
336	// encryption : check tkip_mask
337	// 	tkip_mask : 0x01 = tkip, 0x02 = aes, 0x03 = tkip+aes
338	if(tkip_mask == 0x01)
339		strcpy(crypto, "TKIP");
340	else if(tkip_mask == 0x02)
341		strcpy(crypto, "AES");
342	else if(tkip_mask == 0x03)
343		strcpy(crypto, "TKIP+AES");
344	else
345		strcpy(crypto, "NONE");
346
347	// Wmode : b/a/an/bg/bgn
348	// 0x01 : b
349	// 0x02 : g
350	// 0x04 : a
351	// 0x08 : n
352	if(proto == 0x01)
353		strcpy(wmode, "b");
354	else if(proto == 0x04)
355		strcpy(wmode, "a");
356	else if(proto == 0x0C)
357		strcpy(wmode, "an");
358	else if(proto == 0x03)
359		strcpy(wmode, "bg");
360	else if(proto == 0x0B)
361		strcpy(wmode, "bgn");
362	else if(proto == 0x1C)
363		strcpy(wmode, "ac");
364	else{
365		strcpy(wmode, "");
366		fprintf(stderr, "[%s][%d]dp: [%d]\n", __FUNCTION__, __LINE__, proto);
367	}
368
369	// hidden SSID : if get MAC but not get SSID, it should be a hidden SSID
370	if((&mac[0] != NULL) && !strcmp(params->ap_name_SSID, ""))
371		hidden = 1;
372	else if((&mac[0] != NULL) && !strcmp(params->ap_name_SSID, ""))
373		hidden = 0;
374	else
375		hidden = 0;
376
377#if 0
378	dbg("band=%s,SSID=%s,channel=%d,security=%s,crypto=%s,RSSI=%d,MAC=%s,wmode=%s,hidden=%d\n",
379		band, ssid, params->ap_channel, security, crypto, params->ap_RSSI, &mac[0], wmode, hidden);
380#endif
381
382	sprintf(buff, "\"%s\",\"%s\",\"%d\",\"%s\",\"%s\",\"%d\",\"%s\",\"%s\",\"%d\"",
383		band, ssid, params->ap_channel, security, crypto, params->ap_RSSI, &mac[0], wmode, hidden);
384}
385
386int wlcscan_core_qtn(char *ofile, char *ifname)
387{
388	int i;
389	int scanstatus = -1;
390	uint32_t count;
391	qcsapi_ap_properties	params;
392	char buff[256];
393	FILE *fp_apscan;
394
395	if (!rpc_qtn_ready()) {
396		dbG("5 GHz radio is not ready\n");
397		return -1;
398	}
399
400	logmessage("wlcscan", "start wlcscan scan\n");
401
402	/* clean APSCAN_INFO */
403	lock_qtn_apscan = file_lock("sitesurvey");
404	if((fp_apscan = fopen(ofile, "a")) != NULL){
405		fclose(fp_apscan);
406	}
407	file_unlock(lock_qtn_apscan);
408
409	// start scan AP
410	// if(qcsapi_wifi_start_scan(ifname)){
411	if(qcsapi_wifi_start_scan_ext(ifname, IEEE80211_PICK_ALL | IEEE80211_PICK_NOPICK_BG)){
412		dbg("fail to start AP scan\n");
413		return 0;
414	}
415	fprintf(stderr, "ok to start AP scan\n");
416
417	// loop for check scan status
418	while(1){
419		if(qcsapi_wifi_get_scan_status(ifname, &scanstatus) < 0){
420			dbg("scan error occurs\n");
421			return 0;
422		}
423
424		// if scanstatus = 0 , no scan is running
425		if(scanstatus == 0) break;
426		else{
427			dbg("scan is running...\n");
428			sleep(1);
429		}
430	}
431
432	// check AP scan
433	if(qcsapi_wifi_get_results_AP_scan(ifname, &count) < 0){
434		dbg("fail to get AP scan results, ifname=%s, count=%d\n", ifname, (int)count);
435		return 0;
436	}
437
438	if((int)count > 0){
439		if((fp_apscan = fopen(ofile, "a")) == NULL){
440			dbg("fail to write to [%s]\n", ofile);
441			return 0;
442		}
443		else{
444			// for loop
445			for(i = 0; i < (int)count; i++){
446				// get properties of AP
447				if(!qcsapi_wifi_get_properties_AP(ifname, (uint32_t)i, &params)){
448					show_ap_properties((uint32_t)i, &params, buff);
449					fprintf(fp_apscan, "%s", buff);
450				}
451				else{
452					dbg("fail to get AP properties\n");
453					fclose(fp_apscan);
454				}
455
456				if (i == (int)count - 1){
457					fprintf(fp_apscan, "\n");
458				}else{
459					fprintf(fp_apscan, "\n");
460				}
461			}
462			// for loop
463		}
464	}
465	fclose(fp_apscan);
466	return 1;
467}
468
469int GetPhyStatus_qtn(void)
470{
471	int ret;
472
473	if (!rpc_qtn_ready()) {
474		fprintf(stderr, "ATE command error\n");
475		return -1;
476	}
477	ret = qcsapi_wifi_run_script("router_command.sh", "get_eth_1000m");
478	if (ret < 0) {
479		ret = qcsapi_wifi_run_script("router_command.sh", "get_eth_100m");
480		if (ret < 0) {
481			ret = qcsapi_wifi_run_script("router_command.sh", "get_eth_10m");
482			if (ret < 0) {
483				// fprintf(stderr, "ATE command error\n");
484				return 0;
485			}else{
486				return 10;
487			}
488		}else{
489			return 100;
490		}
491		return -1;
492	}else{
493		return 1000;
494	}
495	return 0;
496}
497
498int start_ap_qtn(void)
499{
500	int ret;
501	int			 qcsapi_retval;
502	unsigned int		 iter;
503	char ssid[65];
504
505	if (!rpc_qtn_ready()) {
506		dbG("5 GHz radio is not ready\n");
507		return -1;
508	}
509
510	logmessage("start_ap", "AP is running...");
511
512#if 0
513	qcsapi_retval = qcsapi_wifi_reload_in_mode(WIFINAME, qcsapi_access_point);
514
515	if (qcsapi_retval >= 0) {
516		fprintf(stderr, "reload to AP mode successfuly\n" );
517	} else {
518		fprintf(stderr, "reload to AP mode failed\n" );
519	}
520#endif
521	sprintf(ssid, "%s", nvram_safe_get("wl1_ssid"));
522	ret = qcsapi_wifi_set_SSID(WIFINAME, ssid);
523
524	// check security
525	char auth[8];
526	char crypto[16];
527	char beacon[] = "WPAand11i";
528	char encryption[] = "TKIPandAESEncryption";
529	char key[65];
530	uint32_t index = 0;
531
532	strncpy(auth, nvram_safe_get("wl1_auth_mode_x"), sizeof(auth));
533	strncpy(crypto, nvram_safe_get("wl1_crypto"), sizeof(crypto));
534	strncpy(key, nvram_safe_get("wl1_wpa_psk"), sizeof(key));
535
536	if(!strcmp(auth, "psk2") && !strcmp(crypto, "aes")){
537		memcpy(beacon, "11i", strlen("11i") + 1);
538		memcpy(encryption, "AESEncryption", strlen("AESEncryption") + 1);
539	}
540	else if(!strcmp(auth, "pskpsk2") && !strcmp(crypto, "aes") ){
541		memcpy(beacon, "WPAand11i", strlen("WPAand11i") + 1);
542		memcpy(encryption, "AESEncryption", strlen("AESEncryption") + 1);
543	}
544	else if(!strcmp(auth, "pskpsk2") && !strcmp(crypto, "tkip+aes") ){
545		memcpy(beacon, "WPAand11i", strlen("WPAand11i") + 1);
546		memcpy(encryption, "TKIPandAESEncryption", strlen("TKIPandAESEncryption") + 1);
547	}
548	else{
549		logmessage("start_ap", "No security in use\n");
550		memcpy(beacon, "Basic", strlen("Basic") + 1);
551	}
552
553	logmessage("start_ap", "ssid=%s, auth=%s, crypto=%s, encryption=%s, key=%s\n", ssid, auth, crypto, encryption, key);
554	if(!strcmp(auth, "open")){
555		if(qcsapi_wifi_set_WPA_authentication_mode(WIFINAME, "NONE") < 0)
556			logmessage("start_ap", "fail to setup a open-none ap\n");
557		if(qcsapi_wifi_set_beacon_type(WIFINAME, beacon) < 0)
558			logmessage("start_ap", "fail to setup beacon type in ap\n");
559	}
560	else{
561		if(qcsapi_wifi_set_beacon_type(WIFINAME, beacon) < 0)
562			logmessage("start_ap", "fail to setup beacon type in ap\n");
563		if(qcsapi_wifi_set_WPA_authentication_mode(WIFINAME, "PSKAuthentication") < 0)
564			logmessage("start_ap", "fail to setup authentiocation type in ap\n");
565		if(qcsapi_wifi_set_key_passphrase(WIFINAME, index, key) < 0)
566			logmessage("start_ap", "fail to set key in ap\n");
567		if(qcsapi_wifi_set_WPA_encryption_modes(WIFINAME, encryption) < 0)
568			logmessage("start_ap", "fail to set encryption mode in ap\n");
569	}
570
571	logmessage("start_ap", "start_ap done!\n");
572
573	return 1;
574}
575
576int start_psta_qtn(void)
577{
578	static qcsapi_SSID	 array_ssids[10 /* MAX_SSID_LIST_SIZE */];
579	int			 qcsapi_retval;
580	unsigned int		 iter;
581	qcsapi_unsigned_int	 sizeof_list = 2 /* DEFAULT_SSID_LIST_SIZE */ ;
582	char			*list_ssids[10 /* MAX_SSID_LIST_SIZE */ + 1];
583	int ret;
584
585	if (!rpc_qtn_ready()) {
586		dbG("5 GHz radio is not ready\n");
587		return -1;
588	}
589
590	logmessage("start_psta", "media bridge is running...");
591
592	qcsapi_retval = qcsapi_wifi_reload_in_mode(WIFINAME, qcsapi_station);
593
594	if (qcsapi_retval >= 0) {
595		fprintf(stderr, "reload to STA mode successfuly\n" );
596	} else {
597		fprintf(stderr, "reload to STA mode failed\n" );
598	}
599
600	for (iter = 0; iter < sizeof_list; iter++) {
601		list_ssids[iter] = array_ssids[iter];
602		*(list_ssids[iter]) = '\0';
603	}
604
605	qcsapi_retval = qcsapi_SSID_get_SSID_list(WIFINAME, sizeof_list, &list_ssids[0]);
606	if (qcsapi_retval >= 0) {
607		for (iter = 0; iter < sizeof_list; iter++) {
608			if ((list_ssids[iter] == NULL) || strlen(list_ssids[iter]) < 1) {
609				break;
610			}
611			fprintf(stderr, "remove [%s]\n", list_ssids[iter]);
612			qcsapi_SSID_remove_SSID(WIFINAME, array_ssids[iter]);
613		}
614	}
615
616	// verify ssid, if not exists, create new one
617	char ssid[33];
618	strncpy(ssid, nvram_safe_get("wlc_ssid"), sizeof(ssid));
619	logmessage("start_psta", "verify ssid [%s]", ssid);
620	if(qcsapi_SSID_verify_SSID(WIFINAME, ssid) < 0){
621		logmessage("start_psta", "Not such SSID in sta mode\n");
622		if(qcsapi_SSID_create_SSID(WIFINAME, ssid) < 0)
623			logmessage("start_psta", "fail to create SSID in sta mode\n");
624	}
625
626	// check security
627	char auth[8];
628	char crypto[16];
629	char beacon[] = "WPAand11i";
630	char encryption[] = "TKIPandAESEncryption";
631	char key[65];
632	uint32_t index = 0;
633
634	strncpy(auth, nvram_safe_get("wlc_auth_mode"), sizeof(auth));
635	strncpy(crypto, nvram_safe_get("wlc_crypto"), sizeof(crypto));
636	strncpy(key, nvram_safe_get("wlc_wpa_psk"), sizeof(key));
637
638	if(!strcmp(auth, "psk2") && !strcmp(crypto, "aes")){
639		memcpy(beacon, "11i", strlen("11i") + 1);
640		memcpy(encryption, "AESEncryption", strlen("AESEncryption") + 1);
641	}
642	else if(!strcmp(auth, "pskpsk2") && !strcmp(crypto, "aes") ){
643		memcpy(beacon, "WPAand11i", strlen("WPAand11i") + 1);
644		memcpy(encryption, "AESEncryption", strlen("AESEncryption") + 1);
645	}
646	else if(!strcmp(auth, "pskpsk2") && !strcmp(crypto, "tkip+aes") ){
647		memcpy(beacon, "WPAand11i", strlen("WPAand11i") + 1);
648		memcpy(encryption, "TKIPandAESEncryption", strlen("TKIPandAESEncryption") + 1);
649	}
650	else{
651		logmessage("start_psta", "not support such authentication & encryption\n");
652	}
653
654	logmessage("start_psta", "ssid=%s, auth=%s, crypto=%s, encryption=%s, key=%s\n", ssid, auth, crypto, encryption, key);
655	if(!strcmp(auth, "open")){
656		if(qcsapi_SSID_set_authentication_mode(WIFINAME, ssid, "NONE") < 0)
657			logmessage("start_psta", "fail to setup a open-none sta\n");
658	}
659	else{
660		if(qcsapi_SSID_set_protocol(WIFINAME, ssid, beacon) < 0)
661			logmessage("start_psta", "fail to setup protocol in sta\n");
662		if(qcsapi_SSID_set_authentication_mode(WIFINAME, ssid, "PSKAuthentication") < 0)
663			logmessage("start_psta", "fail to setup authentiocation type in sta\n");
664		if(qcsapi_SSID_set_key_passphrase(WIFINAME, ssid, index, key) < 0)
665			logmessage("start_psta", "fail to set key in sta\n");
666	}
667
668	// eval("wpa_cli", "reconfigure");
669	ret = qcsapi_wifi_run_script("router_command.sh", "wpa_cli_reconfigure");
670	if (ret < 0) {
671		fprintf(stderr, "[psta] router_command.sh: wpa_cli_reconfigure error\n");
672		return -1;
673	}
674
675	logmessage("start_psta", "start_psta done!\n");
676
677	return 1;
678}
679
680int start_nodfs_scan_qtn(void)
681{
682	int		 qcsapi_retval;
683	int pick_flags = 0;
684
685	logmessage("dfs", "start dfs scan\n");
686
687	pick_flags = IEEE80211_PICK_CLEAREST;
688	pick_flags |= IEEE80211_PICK_NONDFS;
689
690	if (!rpc_qtn_ready()) {
691		dbG("5 GHz radio is not ready\n");
692		return -1;
693	}
694	qcsapi_retval = qcsapi_wifi_start_scan_ext(WIFINAME, pick_flags);
695	if (qcsapi_retval >= 0) {
696		logmessage("nodfs_scan", "complete");
697	}else{
698		logmessage("nodfs_scan", "scan not complete");
699	}
700
701	return 1;
702}
703
704int enable_qtn_telnetsrv(int enable_flag)
705{
706	int ret;
707
708	if (!rpc_qtn_ready()) {
709		fprintf(stderr, "ATE command error\n");
710		return -1;
711	}
712	if(enable_flag == 0){
713		nvram_set("QTNTELNETSRV", "0");
714		ret = qcsapi_wifi_run_script("router_command.sh", "enable_telnet_srv 0");
715	}else{
716		nvram_set("QTNTELNETSRV", "1");
717		ret = qcsapi_wifi_run_script("router_command.sh", "enable_telnet_srv 1");
718	}
719	if (ret < 0) {
720		fprintf(stderr, "[ate] set telnet server error\n");
721		return -1;
722	}
723	nvram_commit();
724	return 0;
725}
726
727int getstatus_qtn_telnetsrv(void)
728{
729	if(nvram_get_int("QTNTELNETSRV") == 1)
730		puts("1");
731	else
732		puts("0");
733
734	return 0;
735}
736
737int del_qtn_cal_files(void)
738{
739	int ret;
740
741	if (!rpc_qtn_ready()) {
742		fprintf(stderr, "ATE command error\n");
743		return -1;
744	}
745	ret = qcsapi_wifi_run_script("router_command.sh", "del_cal_files");
746	if (ret < 0) {
747		fprintf(stderr, "[ate] delete calibration files error\n");
748		return -1;
749	}
750	return 0;
751}
752
753int get_tx_power_qtn(void)
754{
755	const struct txpower_ac_qtn_s *p_to_table;
756	int txpower = 80;
757
758	p_to_table = &txpower_list_qtn_rtac87u[0];
759	txpower = nvram_get_int("wl1_txpower");
760
761	for (p_to_table; p_to_table->min != 0; ++p_to_table) {
762		if (txpower >= p_to_table->min && txpower <= p_to_table->max) {
763			dbG("txpoewr between: min:[%d] to max:[%d]\n", p_to_table->min, p_to_table->max);
764			return p_to_table->pwr;
765		}
766	}
767
768	if ( p_to_table->min == 0 )
769		dbG("no correct power offset!\n");
770
771	/* default max power */
772	return 23;
773}
774
775typedef uint16 chanspec_t;
776extern uint8 wf_chspec_ctlchan(chanspec_t chspec);
777extern chanspec_t wf_chspec_aton(const char *a);
778
779void fix_script_err(char *orig_str, char *new_str)
780{
781	unsigned i = 0, j = 0;
782	unsigned int str_len = 0;
783	str_len = strlen(orig_str);
784
785	for ( i = 0; i < str_len; i++ ){
786		if(orig_str[i] == '$' ||
787			orig_str[i] == '`' ||
788			orig_str[i] == '"' ||
789			orig_str[i] == '\\'){
790			new_str[j] = '\\';
791			new_str[j+1] = orig_str[i];
792			j = j + 2;
793		}else{
794			new_str[j] = orig_str[i];
795			j++;
796		}
797	}
798}
799
800int gen_stateless_conf(void)
801{
802	int ret;
803	FILE *fp;
804
805	int l_len;
806	// check security
807	char auth[8];
808	char crypto[16];
809	char beacon[] = "WPAand11i";
810	char encryption[] = "TKIPandAESEncryption";
811	char key[130];
812	char tmpkey[130];
813	char ssid[66];
814	char tmpssid[66];
815	char region[5] = {0};
816	int channel = wf_chspec_ctlchan(wf_chspec_aton(nvram_safe_get("wl1_chanspec")));
817	int bw = atoi(nvram_safe_get("wl1_bw"));
818	uint32_t index = 0;
819
820	sprintf(ssid, "%s", nvram_safe_get("wl1_ssid"));
821	memset(tmpssid, 0, sizeof(tmpssid));
822	fix_script_err(ssid, tmpssid);
823	strncpy(ssid, tmpssid, sizeof(ssid));
824
825	sprintf(region, "%s", nvram_safe_get("wl1_country_code"));
826	if(strlen(region) == 0)
827		sprintf(region, "%s", nvram_safe_get("1:ccode"));
828	dbg("[stateless] channel:[%d]\n", channel);
829	dbg("[stateless] bw:[%d]\n", bw);
830
831	fp = fopen("/tmp/stateless_slave_config", "w");
832
833	if(nvram_get_int("sw_mode") == SW_MODE_AP &&
834		nvram_get_int("wlc_psta") == 1 &&
835		nvram_get_int("wlc_band") == 1){
836		/* media bridge mode */
837		fprintf(fp, "wifi0_mode=sta\n");
838
839		strncpy(auth, nvram_safe_get("wlc_auth_mode"), sizeof(auth));
840		strncpy(crypto, nvram_safe_get("wlc_crypto"), sizeof(crypto));
841		strncpy(key, nvram_safe_get("wlc_wpa_psk"), sizeof(key));
842		memset(tmpkey, 0, sizeof(tmpkey));
843		fix_script_err(key, tmpkey);
844		strncpy(key, tmpkey, sizeof(key));
845
846		strncpy(ssid, nvram_safe_get("wlc_ssid"), sizeof(ssid));
847		memset(tmpssid, 0, sizeof(tmpssid));
848		fix_script_err(ssid, tmpssid);
849		strncpy(ssid, tmpssid, sizeof(ssid));
850		fprintf(fp, "wifi0_SSID=\"%s\"\n", ssid);
851
852		logmessage("start_psta", "ssid=%s, auth=%s, crypto=%s, encryption=%s, key=%s\n", ssid, auth, crypto, encryption, key);
853
854		/* convert security from nvram to qtn */
855		if(!strcmp(auth, "psk2") && !strcmp(crypto, "aes")){
856			fprintf(fp, "wifi0_auth_mode=PSKAuthentication\n");
857			fprintf(fp, "wifi0_beacon=11i\n");
858			fprintf(fp, "wifi0_encryption=AESEncryption\n");
859			fprintf(fp, "wifi0_passphrase=\"%s\"\n", key);
860		}
861		else if(!strcmp(auth, "pskpsk2") && !strcmp(crypto, "aes") ){
862			fprintf(fp, "wifi0_auth_mode=PSKAuthentication\n");
863			fprintf(fp, "wifi0_beacon=WPAand11i\n");
864			fprintf(fp, "wifi0_encryption=AESEncryption\n");
865			fprintf(fp, "wifi0_passphrase=\"%s\"\n", key);
866		}
867		else if(!strcmp(auth, "pskpsk2") && !strcmp(crypto, "tkip+aes") ){
868			fprintf(fp, "wifi0_auth_mode=PSKAuthentication\n");
869			fprintf(fp, "wifi0_beacon=WPAand11i\n");
870			fprintf(fp, "wifi0_encryption=TKIPandAESEncryption\n");
871			fprintf(fp, "wifi0_passphrase=\"%s\"\n", key);
872		}
873		else{
874			logmessage("start_psta", "No security in use\n");
875			fprintf(fp, "wifi0_auth_mode=NONE\n");
876			fprintf(fp, "wifi0_beacon=Basic\n");
877		}
878
879		/* auto channel for media bridge mode */
880		channel = 0;
881	}else{
882		/* not media bridge mode */
883		fprintf(fp, "wifi0_mode=ap\n");
884
885		strncpy(auth, nvram_safe_get("wl1_auth_mode_x"), sizeof(auth));
886		strncpy(crypto, nvram_safe_get("wl1_crypto"), sizeof(crypto));
887		strncpy(key, nvram_safe_get("wl1_wpa_psk"), sizeof(key));
888		memset(tmpkey, 0, sizeof(tmpkey));
889		fix_script_err(key, tmpkey);
890		strncpy(key, tmpkey, sizeof(key));
891
892
893		strncpy(ssid, nvram_safe_get("wl1_ssid"), sizeof(ssid));
894		memset(tmpssid, 0, sizeof(tmpssid));
895		fix_script_err(ssid, tmpssid);
896		strncpy(ssid, tmpssid, sizeof(ssid));
897		fprintf(fp, "wifi0_SSID=\"%s\"\n", ssid);
898
899		if(!strcmp(auth, "psk2") && !strcmp(crypto, "aes")){
900			fprintf(fp, "wifi0_auth_mode=PSKAuthentication\n");
901			fprintf(fp, "wifi0_beacon=11i\n");
902			fprintf(fp, "wifi0_encryption=AESEncryption\n");
903			fprintf(fp, "wifi0_passphrase=\"%s\"\n", key);
904		}
905		else if(!strcmp(auth, "pskpsk2") && !strcmp(crypto, "aes") ){
906			fprintf(fp, "wifi0_auth_mode=PSKAuthentication\n");
907			fprintf(fp, "wifi0_beacon=WPAand11i\n");
908			fprintf(fp, "wifi0_encryption=AESEncryption\n");
909			fprintf(fp, "wifi0_passphrase=\"%s\"\n", key);
910		}
911		else if(!strcmp(auth, "pskpsk2") && !strcmp(crypto, "tkip+aes") ){
912			fprintf(fp, "wifi0_auth_mode=PSKAuthentication\n");
913			fprintf(fp, "wifi0_beacon=WPAand11i\n");
914			fprintf(fp, "wifi0_encryption=TKIPandAESEncryption\n");
915			fprintf(fp, "wifi0_passphrase=\"%s\"\n", key);
916		}
917		else{
918			logmessage("start_ap", "No security in use\n");
919			fprintf(fp, "wifi0_beacon=Basic\n");
920		}
921	}
922
923	for( l_len = 0 ; l_len < strlen(region); l_len++){
924		region[l_len] = tolower(region[l_len]);
925	}
926	fprintf(fp, "wifi0_region=%s\n", region);
927	// nvram_set("wl1_country_code", nvram_safe_get("1:ccode"));
928	fprintf(fp, "wifi0_vht=1\n");
929	if(bw==1) fprintf(fp, "wifi0_bw=20\n");
930	else if(bw==2) fprintf(fp, "wifi0_bw=40\n");
931	else if(bw==3) fprintf(fp, "wifi0_bw=80\n");
932	else fprintf(fp, "wifi0_bw=80\n");
933
934	/* if media bridge mode, always auto channel */
935	fprintf(fp, "wifi0_channel=%d\n", channel);
936	fprintf(fp, "wifi0_pwr=%d\n", get_tx_power_qtn());
937	if(nvram_get_int("wl1_itxbf") == 1 || nvram_get_int("wl1_txbf") == 1){
938		fprintf(fp, "wifi0_bf=1\n");
939	}else{
940		fprintf(fp, "wifi0_bf=0\n");
941	}
942	if(nvram_get_int("wl1_mumimo") == 1){
943		fprintf(fp, "wifi0_mu=1\n");
944	}else{
945		fprintf(fp, "wifi0_mu=0\n");
946	}
947	fprintf(fp, "wifi0_staticip=1\n");
948	fprintf(fp, "slave_ipaddr=\"192.168.1.111/16\"\n");
949	fprintf(fp, "server_ipaddr=\"%s\"\n", nvram_safe_get("QTN_RPC_SERVER"));
950	fprintf(fp, "client_ipaddr=\"%s\"\n", nvram_safe_get("QTN_RPC_CLIENT"));
951
952	if(nvram_match("wl1.1_lanaccess", "off") && !nvram_match("wl1.1_lanaccess", ""))
953		fprintf(fp, "wifi1_lanaccess=off\n");
954	else
955		fprintf(fp, "wifi1_lanaccess=on\n");
956
957	if(nvram_match("wl1.2_lanaccess", "off") && !nvram_match("wl1.2_lanaccess", ""))
958		fprintf(fp, "wifi2_lanaccess=off\n");
959	else
960		fprintf(fp, "wifi2_lanaccess=on\n");
961
962	if(nvram_match("wl1.3_lanaccess", "off") && !nvram_match("wl1.3_lanaccess", ""))
963		fprintf(fp, "wifi3_lanaccess=off\n");
964	else
965		fprintf(fp, "wifi3_lanaccess=on\n");
966
967	fclose(fp);
968
969	return 1;
970}
971
972void rpc_parse_nvram_from_httpd(int unit, int subunit);
973
974int runtime_config_qtn(int unit, int subunit)
975{
976	int ret;
977
978	if (!rpc_qtn_ready()) {
979		dbG("qcsapi error\n");
980		return -1;
981	}
982	if ( unit == 1 && subunit == -1 ){
983#if 0
984		dbG("Global QTN settings\n");
985		if(nvram_get_int("wl1_itxbf") == 1 || nvram_get_int("wl1_txbf") == 1){
986			dbG("[bf] set_bf_on\n");
987			qcsapi_wifi_run_script("router_command.sh", "set_bf_on");
988			ret = qcsapi_config_update_parameter(WIFINAME, "bf", "1");
989			if (ret < 0) dbG("qcsapi error\n");
990		}else{
991			dbG("[bf] set_bf_off\n");
992			qcsapi_wifi_run_script("router_command.sh", "set_bf_off");
993			ret = qcsapi_config_update_parameter(WIFINAME, "bf", "0");
994			if (ret < 0) dbG("qcsapi error\n");
995		}
996#endif
997		gen_stateless_conf();
998	}
999	rpc_parse_nvram_from_httpd(unit, subunit);
1000	return 1;
1001}
1002
1003/* start: 169.254.39.1, 0x127fea9 */
1004/* end: 169.254.39.254, 0xfe27fea9 */
1005int gen_rpc_qcsapi_ip(void)
1006{
1007	int i;
1008	unsigned int j;
1009	unsigned char *hwaddr;
1010	char hwaddr_5g[18];
1011	struct ifreq ifr;
1012	struct in_addr start, addr;
1013	int hw_len;
1014	FILE *fp_qcsapi_conf;
1015
1016	/* BRCM */
1017	ether_atoe(nvram_safe_get("lan_hwaddr"), (unsigned char *)&ifr.ifr_hwaddr.sa_data);
1018	for (j = 0, i = 0; i < 6; i++){
1019		j += ifr.ifr_hwaddr.sa_data[i] + (j << 6) + (j << 16) - j;
1020	}
1021	start.s_addr = htonl(ntohl(0x127fea9 /* start */) +
1022		((j + 0 /* c->addr_epoch */) % (1 + ntohl(0xfe27fea9 /* end */) - ntohl(0x127fea9 /* start */))));
1023	nvram_set("QTN_RPC_CLIENT", inet_ntoa(start));
1024
1025	/* QTN */
1026	strcpy(hwaddr_5g, nvram_safe_get("lan_hwaddr"));
1027	inc_mac(hwaddr_5g, 4);
1028	ether_atoe(hwaddr_5g, (unsigned char *)&ifr.ifr_hwaddr.sa_data);
1029	for (j = 0, i = 0; i < 6; i++){
1030		j += ifr.ifr_hwaddr.sa_data[i] + (j << 6) + (j << 16) - j;
1031	}
1032	start.s_addr = htonl(ntohl(0x127fea9 /* start */) +
1033		((j + 0 /* c->addr_epoch */) % (1 + ntohl(0xfe27fea9 /* end */) - ntohl(0x127fea9 /* start */))));
1034	nvram_set("QTN_RPC_SERVER", inet_ntoa(start));
1035	if ((fp_qcsapi_conf = fopen("/etc/qcsapi_target_ip.conf", "w")) == NULL){
1036		logmessage("qcsapi", "write qcsapi conf error");
1037	}else{
1038		fprintf(fp_qcsapi_conf, "%s", nvram_safe_get("QTN_RPC_SERVER"));
1039		fclose(fp_qcsapi_conf);
1040		logmessage("qcsapi", "write qcsapi conf ok");
1041	}
1042
1043#if 0
1044	do_ping_detect(); /* refer wanduck.c */
1045#endif
1046}
1047
1048#if defined(RTCONFIG_JFFS2ND_BACKUP)
1049#define JFFS_NAME	"jffs2"
1050#define SECOND_JFFS2_PARTITION  "asus"
1051#define SECOND_JFFS2_PATH	"/asus_jffs"
1052void check_2nd_jffs(void)
1053{
1054	char s[256];
1055	int size;
1056	int part;
1057	struct statfs sf;
1058
1059	_dprintf("2nd jffs2: %s\n", SECOND_JFFS2_PARTITION);
1060
1061	if (!mtd_getinfo(SECOND_JFFS2_PARTITION, &part, &size)) {
1062		_dprintf("Can not get 2nd jffs2 information!");
1063		return;
1064	}
1065	mount_2nd_jffs2();
1066
1067	if(access("/asus_jffs/bootcfg.tgz", R_OK ) != -1 ) {
1068		logmessage("qtn", "bootcfg.tgz exists");
1069		system("rm -f /tmp/bootcfg.tgz");
1070	} else {
1071		logmessage("qtn", "bootcfg.tgz does not exist");
1072		sprintf(s, MTD_BLKDEV(%d), part);
1073		umount("/asus_jffs");
1074		if (mount(s, SECOND_JFFS2_PATH , JFFS_NAME, MS_NOATIME, "") != 0) {
1075			logmessage("qtn", "cannot store bootcfg.tgz");
1076		}else{
1077			system("cp /tmp/bootcfg.tgz /asus_jffs");
1078			system("rm -f /tmp/bootcfg.tgz");
1079			logmessage("qtn", "backup bootcfg.tgz ok");
1080		}
1081	}
1082
1083	if (umount(SECOND_JFFS2_PATH)){
1084		dbG("umount asus_jffs failed\n");
1085	}else{
1086		dbG("umount asus_jffs ok\n");
1087	}
1088
1089	// format_mount_2nd_jffs2();
1090}
1091#endif
1092