1 #include <sys/unistd.h>
2 #include <sys/errno.h>
3 
4 #include <stdarg.h>
5 
6 #include "aos/hal/flash.h"
7 #include "aos/hal/uart.h"
8 #include "aos/hal/wifi.h"
9 #include "CheckSumUtils.h"
10 #include <wifi/wifi_conf.h>
11 #include <wifi/wifi_util.h>
12 #include <wifi/wifi_ind.h>
13 
14 static uart_dev_t qc_uart;
15 extern uart_dev_t uart_0;
16 
17 #ifndef SERIAL_NUMBER
18 #define SERIAL_NUMBER       "UNDF.0000.0000"
19 #endif
20 
21 #ifndef BOOT_VERSION
22 #define BOOT_VERSION "1.0.0"
23 #endif
24 
25 #define WEAK             __attribute__((weak))
26 
get_sn(void)27 WEAK char *get_sn(void)
28 {
29     return SERIAL_NUMBER;
30 }
31 
qc_printf(const char * fmt,...)32 void qc_printf(const char *fmt, ...)
33 {
34     va_list ap;
35     char string[128];
36 
37     va_start(ap, fmt);
38     vsprintf(string, fmt, ap);
39     string[127] = 0;
40     //hal_uart_send(&qc_uart, string, strlen(string), 0);
41     hal_uart_send(&uart_0, string, strlen(string), 0);
42     va_end(ap);
43 }
44 
qc_scan_completed_event(hal_wifi_module_t * m,hal_wifi_scan_result_t * result,void * arg)45 static void qc_scan_completed_event(hal_wifi_module_t *m,
46                                         hal_wifi_scan_result_t *result,
47                                         void *arg)
48 {
49     int i;
50     char ssid[33];
51 
52     ssid[32] = 0;
53     if (result->ap_num <= 0) {
54         qc_printf("Scan AP Fail");
55         return;
56     }
57     qc_printf( "Scan AP Success:\r\n" );
58     for (i = 0; i < (result->ap_num); i++) {
59         memcpy(ssid, result->ap_list[i].ssid, 32);// fix ssid len is 32 bytes.
60         qc_printf("  SSID: %s, RSSI: %d\r\n", ssid, result->ap_list[i].ap_power);
61     }
62 }
63 
64 
65 
66 typedef struct _scan_rst_t_ {
67 	rtw_scan_result_t result;
68 
69 	struct _scan_rst_t_ *next;
70 }mxchip_scan_rst_t;
71 
72 static mxchip_scan_rst_t *p_result_head = NULL;
73 
free_ap_list(void)74 static void free_ap_list(void)
75 {
76     mxchip_scan_rst_t* p, *q;
77 
78     p = p_result_head;
79     p_result_head = NULL;
80 
81     while(p != NULL) {
82         q = p->next;
83         aos_free(p);
84         p = q;
85     }
86 }
87 
insert_result_by_rssi(rtw_scan_result_t * result)88 static void insert_result_by_rssi(rtw_scan_result_t* result)
89 {
90     mxchip_scan_rst_t *p, *next, *prev;
91 
92     /* New BSSID - add it to the list */
93     p = (mxchip_scan_rst_t*)aos_malloc(sizeof(mxchip_scan_rst_t));
94     if (p == NULL) {
95 		return;
96     }
97     memcpy(&p->result, result, sizeof(rtw_scan_result_t));
98     p->next = NULL;
99     if (p_result_head == NULL) {
100         p->next = p_result_head;
101         p_result_head = p;
102         goto DONE;
103     }
104 
105     prev = p_result_head;
106     while(prev->next != NULL) {
107         prev = prev->next;
108     }
109     prev->next = p;
110 DONE:
111     return;
112 }
113 
scan_result_handler(rtw_scan_handler_result_t * malloced_scan_result)114 static rtw_result_t scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result )
115 {
116 	if (malloced_scan_result->scan_complete != RTW_TRUE) {
117 		rtw_scan_result_t* record = &malloced_scan_result->ap_details;
118 		record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
119         insert_result_by_rssi(record);
120 	} else{
121 	    mxchip_scan_rst_t *p;
122 	    rtw_scan_result_t* result;
123 	    p = p_result_head;
124         if (p == NULL) {
125             qc_printf("Scan AP Fail\r\n");
126             return RTW_SUCCESS;
127         }
128 	    while (p!=NULL) {
129 	        result = &p->result;
130 	        qc_printf("  SSID: %s, RSSI: %d\r\n", result->SSID.val, result->signal_strength);
131 			p = p->next;
132 	    }
133         free_ap_list();
134 	}
135 	return RTW_SUCCESS;
136 }
137 
qc_scan(void)138 static void qc_scan(void)
139 {
140     int ret;
141     hal_wifi_module_t *module;
142     uint8_t mac[6];
143 
144     module = hal_wifi_get_default_module();
145     hal_wifi_get_mac_addr(module, mac );
146     qc_printf( "MAC:%02X-%02X-%02X-%02X-%02X-%02X\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] );
147 
148     ret = wifi_scan_networks(scan_result_handler, NULL);
149 
150     if (ret != RTW_SUCCESS) {
151         qc_printf("Scan AP Fail\r\n");
152     }
153 
154 }
155 
get_bootloader_ver(void)156 static char *get_bootloader_ver(void)
157 {
158     return BOOT_VERSION;
159 }
160 
qc_crc(void)161 static uint16_t qc_crc(void)
162 {
163     hal_logic_partition_t* part;
164     int total_len, len, offset=0;
165     uint8_t data[1024];
166     CRC16_Context contex;
167     uint16_t crc = 0;
168 
169     CRC16_Init( &contex );
170     part = hal_flash_get_info((hal_partition_t)HAL_PARTITION_APPLICATION);
171     total_len = part->partition_length;
172 
173     while(total_len > 0){
174         if( 1024 < total_len ){
175             len = 1024;
176         } else {
177             len = total_len;
178         }
179         hal_flash_read( HAL_PARTITION_APPLICATION, &offset, data , len);
180 
181         total_len -= len;
182 
183         CRC16_Update( &contex, data, len );
184     }
185     CRC16_Final( &contex, &crc );
186 }
187 
qc_test(void)188 void qc_test(void)
189 {
190     uint8_t mac[6];
191     uint32_t prov_res, ret;
192 
193     printf( "\r\n" );
194     qc_uart.port                = 4;
195     qc_uart.config.baud_rate    = 921600;
196     qc_uart.config.data_width   = DATA_WIDTH_8BIT;
197     qc_uart.config.parity       = NO_PARITY;
198     qc_uart.config.stop_bits    = STOP_BITS_1;
199     qc_uart.config.flow_control = FLOW_CONTROL_DISABLED;
200 
201     //hal_uart_init(&qc_uart);
202     qc_printf( "==== MXCHIP Manufacture Test ====\r\n" );
203     qc_printf( "Serial Number: %s\r\n", get_sn() );
204     qc_printf( "App CRC: %04X\r\n", qc_crc() );
205     qc_printf( "Bootloader Version: %s\r\n", get_bootloader_ver() );
206 // GPIO test
207     qc_test_gpio();
208 // BLE test
209     bt_get_mac_address(mac);
210     qc_printf( "Local Bluetooth Address: %02X-%02X-%02X-%02X-%02X-%02X\r\n",
211         mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] );
212 
213     qc_scan();
214 
215     while(1) {// dead loop, DONOT exit QC mode
216         aos_msleep(1000);
217         hal_wdg_reload(NULL);
218     }
219 }
220 
221