1 /*
2 * ard_spi.c
3 *
4 * Created on: May 27, 2010
5 * Author: mlf by Metodo2 srl
6 */
7
8 //#define _APP_DEBUG_
9
10 #include <avr32/io.h>
11 #include "board.h"
12 #include "gpio.h"
13 #include "usart.h"
14 #include "ard_spi.h"
15 #include "ard_tcp.h"
16 #include "wifi_spi.h"
17 #include "wl_cm.h"
18 #include "ard_utils.h"
19 #include "intc.h"
20 #include "spi.h"
21 #include "debug.h"
22 #include "delay.h"
23 #include "eic.h"
24 #include "timer.h"
25 #include "lwip/dns.h"
26 #include <board_init.h>
27 #include "util.h"
28 #include "lwip/udp.h"
29 #include "lwip_setup.h"
30
31 extern const char* fwVersion;
32
33 /*! \name USART Settings
34 */
35 //! @{
36 #if BOARD == EVK1105
37 # define ARD_USART_SPI (&AVR32_USART1)
38 # define ARD_USART_SPI_SCK_PIN AVR32_USART1_CLK_0_PIN
39 # define ARD_USART_SPI_SCK_FUNCTION AVR32_USART1_CLK_0_FUNCTION
40 # define ARD_USART_SPI_MISO_PIN AVR32_USART1_TXD_0_0_PIN
41 # define ARD_USART_SPI_MISO_FUNCTION AVR32_USART1_TXD_0_0_FUNCTION
42 # define ARD_USART_SPI_MOSI_PIN AVR32_USART1_RXD_0_0_PIN
43 # define ARD_USART_SPI_MOSI_FUNCTION AVR32_USART1_RXD_0_0_FUNCTION
44 # define ARD_USART_SPI_NSS_PIN AVR32_USART1_CTS_0_0_PIN
45 # define ARD_USART_SPI_NSS_FUNCTION AVR32_USART1_CTS_0_0_FUNCTION
46 # define ARD_USART_SPI_IRQ AVR32_USART1_IRQ
47 #endif
48 #if BOARD == ARDUINO
49 # define ARD_SPI (&AVR32_SPI0)
50 #define EXT_INT_PIN_LINE1 AVR32_EIC_EXTINT_5_PIN
51 #define EXT_INT_FUNCTION_LINE1 AVR32_EIC_EXTINT_5_FUNCTION
52 #define EXT_INT_LINE1 EXT_INT5
53 #define EXT_INT_IRQ_LINE1 AVR32_EIC_IRQ_5
54 #define EXT_INT_NB_LINES 1
55 #endif
56
57 /* These defines should be adjusted to match the application */
58 /*! \brief CPU core speed in Hz */
59 #define CPUHZ 60000000
60 /*! \brief Number of bytes in the receive buffer when operating in slave mode */
61 #define BUFFERSIZE 64
62 /*! \brief A adjustable delay avoiding multiple requests on the switches */
63 //#define TIMEOUT 150000
64 #define TIMEOUT CPUHZ/200
65 /*! \brief Number of bits in each SPI package*/
66 #define SPI_BITS 8
67 /*! \brief SPI slave speed in Hz */
68 #define SPI_SLAVE_SPEED 1000000
69
70
71 #ifndef CMD_MAX_LEN
72 #define CMD_MAX_LEN 1024
73 #endif
74 #ifndef REPLY_MAX_LEN
75 #define REPLY_MAX_LEN 1024
76 #endif
77
78 #define _BUFFERSIZE 100
79
80 extern void tcp_debug_print_pcbs(void);
81 extern bool ifStatus;
82 extern bool scanNetCompleted;
83
84 static char buf[CMD_MAX_LEN];
85 static char reply[REPLY_MAX_LEN];
86 static uint16_t cmdCorr = 0;
87 static uint16_t count = 0;
88 static uint16_t replyCount = 0;
89 static cmd_spi_state_t state = SPI_CMD_IDLE;
90 int receivedChars = 0;
91 static uint8_t _receiveBuffer[_BUFFERSIZE];
92 bool startReply = false;
93 bool end_write = false; //TODO only for debug
94
95 // Signal indicating a new command is coming from SPI interface
96 static volatile Bool startRecvCmdSignal = FALSE;
97
98 #define MAX_CMD_NUM 36
99 typedef struct sCmd_spi_list{
100 cmd_spi_cb_t cb;
101 char cmd_id;
102 cmd_spi_rcb_t reply_cb;
103 void* ctx;
104 char flags;
105 }tCmd_spi_list;
106
107 static tCmd_spi_list cmd_spi_list[MAX_CMD_NUM] = { {0} };
108
109 #ifdef _SPI_STATS_
110 typedef struct sStatSpi
111 {
112 int timeoutIntErr;
113 int timeoutErr;
114 int txErr;
115 int rxErr;
116 int wrongFrame;
117 int frameDisalign;
118 int overrideFrame;
119 int lastCmd;
120 int lastError;
121 unsigned long status;
122 }tStatSpi;
123
124 tStatSpi statSpi = {0};
125
initStatSpi()126 void initStatSpi()
127 {
128 statSpi.lastCmd = 0;
129 statSpi.lastError = 0;
130 statSpi.status= 0;
131 statSpi.txErr = 0;
132 statSpi.rxErr = 0;
133 statSpi.timeoutErr= 0;
134 statSpi.timeoutIntErr= 0;
135 statSpi.wrongFrame = 0;
136 statSpi.frameDisalign = 0;
137 statSpi.overrideFrame = 0;
138 }
139
printStatSpi()140 void printStatSpi()
141 {
142 printk("totSpiCmds\t: 0x%x\n", cmdCorr);
143 printk("lastCmd \t: 0x%x\n", statSpi.lastCmd);
144 printk("lastErr \t: 0x%x\n", statSpi.lastError);
145 printk("spiStatus\t: 0x%X\n", statSpi.status);
146 printk("spiTxErr \t: 0x%x\n", statSpi.txErr);
147 printk("spiRxErr \t: 0x%x\n", statSpi.rxErr);
148 printk("spiTmoErr\t: 0x%x\n", statSpi.timeoutErr);
149 printk("spiTmoIntErr\t: 0x%x\n", statSpi.timeoutIntErr);
150 printk("wrongFrame\t: 0x%x\n", statSpi.wrongFrame);
151 printk("disalFrame\t: 0x%x\n", statSpi.frameDisalign);
152 printk("overrideFrame\t: 0x%x\n", statSpi.overrideFrame);
153 }
154
155 cmd_state_t
cmd_statSpi(int argc,char * argv[],void * ctx)156 cmd_statSpi(int argc, char* argv[], void* ctx)
157 {
158 printStatSpi();
159 return CMD_DONE;
160 }
161
162 cmd_state_t
cmd_resetStatSpi(int argc,char * argv[],void * ctx)163 cmd_resetStatSpi(int argc, char* argv[], void* ctx)
164 {
165 initStatSpi();
166 return CMD_DONE;
167 }
168 #endif
169
170 #define ARRAY_SIZE(a) sizeof(a) / sizeof(a[0])
171 #define RETURN_ERR(e) return (e==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
172 #define RESET_USART_CSR(usart) usart->cr = AVR32_USART_CR_RSTSTA_MASK;
173
174 int result = WL_CONNECT_FAILED; //Store the result of the last operation
175
176 void* mapSockTCP[MAX_SOCK_NUM][MAX_MODE_NUM];
177
178 //Udp RemoteIp and remote Port
179 static tRemoteClient remoteClients[MAX_SOCK_NUM] = {{0,0}};
180
setRemoteClient(uint16_t sock,uint32_t _ipaddr,uint16_t _port)181 void setRemoteClient(uint16_t sock, uint32_t _ipaddr, uint16_t _port)
182 {
183 if (sock < MAX_SOCK_NUM)
184 {
185 remoteClients[sock].ipaddr = _ipaddr;
186 remoteClients[sock].port = _port;
187 }
188 }
189
getRemoteClient(uint16_t sock)190 tRemoteClient* getRemoteClient(uint16_t sock)
191 {
192 if (sock < MAX_SOCK_NUM)
193 {
194 return &remoteClients[sock];
195 }
196 return NULL;
197 }
198
199 struct netif* ard_netif = NULL;
200
201 // Network list retrived in the last scanNetwork
202 static struct wl_network_list_t network_list = { 0 };
203
204 struct ip_addr _hostIpAddr;
205
206 static bool hostIpAddrFound = false;
207
getTTCP(uint8_t sock,uint8_t mode)208 void* getTTCP(uint8_t sock, uint8_t mode)
209 {
210 if (sock < MAX_SOCK_NUM)
211 return mapSockTCP[sock][mode];
212 return NULL;
213 }
214
getSock(void * _ttcp)215 int getSock(void * _ttcp)
216 {
217 if (_ttcp != NULL)
218 {
219 int i = 0;
220 for (; i<MAX_SOCK_NUM; i++)
221 {
222 if (_ttcp == mapSockTCP[i][GET_TCP_MODE(_ttcp)])
223 return i;
224 }
225 }
226 return -1;
227 }
228
setMapSockMode(uint8_t sock,void * _ttcp,uint8_t _tcp_mode)229 void setMapSockMode(uint8_t sock, void* _ttcp, uint8_t _tcp_mode)
230 {
231 if ((IS_VALID_SOCK(sock))&&(_ttcp!=NULL))
232 mapSockTCP[sock][_tcp_mode]=_ttcp;
233 INFO_TCP("Map [%d, %p, %s]\n", sock, _ttcp, Mode2Str(_tcp_mode));
234 }
235
setMapSock(uint8_t sock,void * _ttcp)236 void setMapSock(uint8_t sock, void* _ttcp)
237 {
238 setMapSockMode(sock, _ttcp, GET_TCP_MODE(_ttcp));
239 }
240
clearMapSockTcp(uint8_t sock,uint8_t mode)241 void clearMapSockTcp(uint8_t sock, uint8_t mode)
242 {
243 if (sock < MAX_SOCK_NUM)
244 {
245 //printk("UnMap [%d, %p]\n", sock, mapSockTCP[sock]);
246 mapSockTCP[sock][mode] = NULL;
247 }
248
249 }
250
initMapSockTcp()251 void initMapSockTcp()
252 {
253 memset(mapSockTCP, 0, sizeof(mapSockTCP));
254 }
255
256 #if 0
257 /**
258 * Calculate bitrate based on number of bytes transmitted and elapsed time
259 */
260 static void ard_tcp_print_stats(struct ttcp *ttcp) {
261 uint32_t ms = timer_get_ms() - ttcp->start_time;
262 uint32_t bytes = ttcp->mode == TTCP_MODE_TRANSMIT ? ttcp->nbuf
263 * ttcp->buflen : ttcp->recved;
264
265 if (ttcp->verbose)
266 printk("\n");
267
268 printk("TTCP [%p]: %d bytes processed, %d.%d KB/s (%s/%s)\n", ttcp, bytes,
269 bytes / ms, bytes % ms, ProtMode2Str(ttcp->udp),
270 Mode2Str(ttcp->mode));
271 }
272 #endif
273
showTTCPstatus()274 void showTTCPstatus()
275 {
276 printk("IF status: %s\n", (ifStatus) ? "UP":"DOWN");
277 printk("CONN status: %s\n", (_connected) ? "UP":"DOWN");
278
279 int i = 0;
280 for (; i<MAX_SOCK_NUM; i++)
281 {
282 int ii=0;
283 for (; ii<MAX_MODE_NUM; ii++)
284 {
285 void* p = getTTCP(i, ii);
286 if (p)
287 {
288 ttcp_t* _ttcp = (ttcp_t* )p;
289 printk("Socket n.:%d(%d) [0x%x] %s %s addr:%s port:%d\n", i, ii, _ttcp,
290 ProtMode2Str(_ttcp->udp), Mode2Str(_ttcp->mode), ip2str(_ttcp->addr), _ttcp->port);
291 if (_ttcp->udp == TCP_MODE)
292 {
293 int j = 0;
294 for (; j<MAX_CLIENT_ACCEPTED; ++j)
295 {
296 if (_ttcp->tpcb[j]){
297 printk("[%d tpcp-%p]-Status:%d\n", j, _ttcp->tpcb[j], _ttcp->tpcb[j]->state);
298 }
299 }
300
301 if (_ttcp->lpcb){
302 printk("[tlcp-%p]-Status:%d\n", _ttcp->lpcb, _ttcp->lpcb->state);
303 }
304 }else{
305 if (_ttcp->upcb){
306 struct ip_addr loc = _ttcp->upcb->local_ip;
307 printk("[upcp-%p] flags:0x%x local:%s[0x%x]-%d\n",
308 _ttcp->upcb, _ttcp->upcb->flags,
309 ip2str(loc), loc, _ttcp->upcb->local_port);
310 tRemoteClient remote = {0,0};;
311 getRemoteData(i, ii, &remote);
312 struct ip_addr ipaddr = { remote.ipaddr };
313 printk("remote:%s(0x%x)-%d\n", ip2str(ipaddr), remote.ipaddr, remote.port);
314 }
315 }
316 //ard_tcp_print_stats(_ttcp);
317 printk("Data avail:%s\n", isAvailTcpDataByte(i)?"YES":"NO");
318 printk("------------------------------\n");
319 }
320 }
321 }
322
323 tcp_debug_print_pcbs();
324 }
325
write_stream(volatile avr32_spi_t * spi,const char * stream,uint16_t len)326 int write_stream(volatile avr32_spi_t *spi, const char *stream, uint16_t len)
327 {
328 uint16_t _len = 0;
329 unsigned short dummy=0;
330
331 do {
332 //SIGN1_DN();
333 if (spi_write(spi, *stream) == SPI_ERROR_TIMEOUT)
334 {
335 #ifdef _SPI_STATS_
336 statSpi.timeoutErr++;
337 statSpi.txErr++;
338 statSpi.lastError = SPI_ERROR_TIMEOUT;
339 statSpi.status = spi_getStatus(spi);
340 #endif
341 return SPI_ERROR_TIMEOUT;
342 }
343 else
344 {
345 stream++;
346 _len++;
347 spi_read(spi,&dummy);
348 }
349 //SIGN1_UP();
350 }while (_len < len);
351 return SPI_OK;
352 }
353
sendError()354 void sendError()
355 {
356 AVAIL_FOR_SPI();
357 if (spi_write(&AVR32_SPI, ERR_CMD) != SPI_ERROR_TIMEOUT)
358 {
359 //Wait to empty the buffer
360 while(!spi_writeRegisterEmptyCheck(&AVR32_SPI));
361 }
362 BUSY_FOR_SPI();
363 WARN("Send SPI error!\n");
364 }
365
366 #define ENABLE_SPI_INT() do { \
367 volatile avr32_spi_t *spi = ARD_SPI; \
368 Bool global_interrupt_enabled = Is_global_interrupt_enabled(); \
369 if (global_interrupt_enabled) Disable_global_interrupt(); \
370 spi->IER.rdrf = 1; spi->IER.rxbuff = 1; spi->IER.endrx = 1; \
371 if (global_interrupt_enabled) Enable_global_interrupt(); \
372 }while(0);
373
374 #define DISABLE_SPI_INT() do { \
375 volatile avr32_spi_t *spi = ARD_SPI; \
376 Bool global_interrupt_enabled = Is_global_interrupt_enabled(); \
377 if (global_interrupt_enabled) Disable_global_interrupt(); \
378 spi->IDR.rdrf = 1; spi->IDR.rxbuff = 1; spi->IDR.endrx = 1; \
379 if (global_interrupt_enabled) Enable_global_interrupt(); \
380 }while(0);
381
382 #define CLEAR_SPI_INT() do { \
383 eic_clear_interrupt_line(&AVR32_EIC, AVR32_SPI0_IRQ); \
384 }while(0);
385
spi_add_cmd(char _cmd_id,cmd_spi_cb_t cb,cmd_spi_rcb_t rcb,void * ctx,char flag)386 int spi_add_cmd(char _cmd_id, cmd_spi_cb_t cb, cmd_spi_rcb_t rcb, void* ctx,
387 char flag) {
388 U32 i;
389 for (i = 0; i < ARRAY_SIZE(cmd_spi_list); i++)
390 if (!cmd_spi_list[i].cb)
391 break;
392
393 if (i == ARRAY_SIZE(cmd_spi_list))
394 {
395 printk("List Commands full!\n");
396 return -1;
397 }
398 cmd_spi_list[i].cmd_id = _cmd_id;
399 cmd_spi_list[i].cb = cb;
400 cmd_spi_list[i].reply_cb = rcb;
401 cmd_spi_list[i].ctx = ctx;
402 cmd_spi_list[i].flags = flag;
403 return 0;
404 }
405
set_net_cmd_cb(int numParam,char * buf,void * ctx)406 int set_net_cmd_cb(int numParam, char* buf, void* ctx) {
407 struct wl_ssid_t ssid;
408 wl_err_t err = WL_FAILURE;
409 tParam* param = (tParam*) buf;
410
411 if (param->paramLen < WL_SSID_MAX_LENGTH) {
412 memcpy(ssid.ssid, ¶m->param, param->paramLen);
413 ssid.len = param->paramLen;
414 ssid.ssid[ssid.len] = 0;
415 INFO_SPI("SSID:%s\n", ssid.ssid);
416 //dump(ssid.ssid, ssid.len);
417 err = wl_cm_set_network(&ssid, NULL);
418 if (err != 1)
419 WARN("err=%d\n", err);
420 } else {
421 WARN("SSID len out of range");
422 }
423 return err;
424 }
425
426 extern uint8_t ascii_to_key(char *outp, const char *inp);
427
set_key_cmd_cb(int numParam,char * buf,void * ctx)428 int set_key_cmd_cb(int numParam, char* buf, void* ctx) {
429 struct wl_ssid_t ssid;
430 struct wl_mac_addr_t bssid;
431 uint8_t idx=0, len=0;
432 char key[13], key_hex[27];
433 char keyIdx[2];
434 wl_err_t err = WL_SUCCESS;
435 tParam* params = (tParam*) buf;
436
437 INFO_SPI("%s params=%d\n", __FUNCTION__, numParam);
438
439 // SSID
440 memset(&ssid, 0, sizeof ssid);
441
442 if (params->paramLen < WL_SSID_MAX_LENGTH) {
443 memcpy(ssid.ssid, ¶ms->param, params->paramLen);
444 ssid.len = params->paramLen;
445 INFO_SPI("%s\n", ssid.ssid);
446 } else {
447 //printk("SSID len out of range");
448 RETURN_ERR(WL_FAILURE)
449 }
450
451 params = (tParam*)((char*)buf+PARAM_LEN_SIZE+params->paramLen);
452 strncpy(keyIdx, (const char*)¶ms->param, params->paramLen);
453 keyIdx[(uint8_t)params->paramLen]='\0';
454
455 idx = (uint8_t)atoi(keyIdx);
456 // KEY IDX
457 if ((params->paramLen != 1)||(idx < 0)||(idx > 3)){
458 //printk("KEY IDX out of range %d\n", idx);
459 RETURN_ERR(WL_FAILURE)
460 }
461
462 params = (tParam*)((char*)params+PARAM_LEN_SIZE+params->paramLen);
463 strncpy(key_hex, (const char*)¶ms->param, params->paramLen);
464 key_hex[(uint8_t)params->paramLen]='\0';
465 len = ascii_to_key(key, key_hex);
466 // KEY
467 if (( len != 5)&&(len != 13))
468 {
469 //printk("KEY len out of range %d", len);
470 RETURN_ERR(WL_FAILURE)
471 }
472 #if 0
473 printk("KEY IDX = %d\n", idx);
474 dump(key, len);
475 printk("KEY len %d\n", len);
476 #endif
477 memset(&bssid.octet, 0xff, sizeof bssid.octet);
478
479 wl_add_wep_key(idx, len, key, &bssid);
480 //wl_set_auth_mode(AUTH_MODE_SHARED_KEY);
481 wl_set_default_wep_key(idx);
482
483 //Connect
484 err = wl_cm_set_network(&ssid, NULL);
485 if (err != 1)
486 WARN("err=%d\n", err);
487 RETURN_ERR(err)
488 }
489
set_passphrase_cmd_cb(int numParam,char * buf,void * ctx)490 int set_passphrase_cmd_cb(int numParam, char* buf, void* ctx) {
491 struct wl_network_t net;
492 char pass[64];
493 wl_err_t err = WL_SUCCESS;
494 tParam* params = (tParam*) buf;
495
496 INFO_SPI("%s params=%d\n", __FUNCTION__, numParam);
497
498 memset(&net, 0, sizeof net);
499 memset(net.bssid.octet, 0xFF, sizeof net.bssid.octet);
500
501 net.enc_type = ENC_TYPE_AUTO;
502
503 // SSID
504 if (params->paramLen < WL_SSID_MAX_LENGTH) {
505 memcpy(net.ssid.ssid, ¶ms->param, params->paramLen);
506 net.ssid.len = params->paramLen;
507 INFO_SPI("%s %d\n", net.ssid.ssid, net.ssid.len);
508 } else {
509 //printk("SSID len out of range");
510 RETURN_ERR(WL_FAILURE)
511 }
512 params = (tParam*)((char*)buf+PARAM_LEN_SIZE+params->paramLen);
513 // PASSPHRASE
514
515 strncpy(pass, (const char*)¶ms->param, params->paramLen);
516 pass[(uint8_t)params->paramLen]='\0';
517 INFO_SPI("Pass: %s %d\n", pass, params->paramLen);
518
519 if (wl_set_passphrase(&net,
520 pass,
521 params->paramLen,
522 ENC_TYPE_AUTO,
523 AUTH_MODE_AUTO)
524 != WL_SUCCESS) {
525 WARN("%s : Failed to add passphrase\n", __func__);
526
527 RETURN_ERR(WL_FAILURE)
528 }
529 printk("Connect to network...");
530 //Connect
531 err = wl_cm_set_network(&net.ssid, NULL);
532 if (err != 1)
533 printk("err=%d\n", err);
534 else
535 printk("OK\n");
536 RETURN_ERR(err)
537 }
538
set_ip_config_cmd_cb(int numParam,char * buf,void * ctx)539 int set_ip_config_cmd_cb(int numParam, char* buf, void* ctx) {
540 struct ip_addr lwip_addr;
541 struct ctx_server *hs = ctx;
542 struct net_cfg *ncfg = &(hs->net_cfg);
543 struct netif *nif = ncfg->netif;
544 uint8_t parmsToChange=0;
545 const uint8_t MAX_IP_CONFIG_PARAMS = 3;
546
547 wl_err_t err = WL_SUCCESS;
548 tParam* params = (tParam*) buf;
549
550 if (params->paramLen == 1)
551 {
552 GET_PARAM_NEXT(BYTE, params, _parmsToChange);
553 parmsToChange = _parmsToChange;
554 }
555 else
556 RETURN_ERR(WL_FAILURE)
557
558 INFO_SPI("%p numParam=%d parmsToChange=%d\n", ctx, numParam, parmsToChange);
559
560 if (parmsToChange <= MAX_IP_CONFIG_PARAMS)
561 {
562 int i=0;
563 for (; i<parmsToChange; ++i)
564 {
565 if (params->paramLen == 4)
566 {
567 GET_PARAM_NEXT(LONG, params, _ip_addr);
568 lwip_addr.addr = _ip_addr;
569 INFO_SPI("%d] nif:%p lwip_addr=0x%x\n", i, nif, lwip_addr.addr);
570 switch (i)
571 {
572 case 0: // local_ip
573 {
574 netif_set_ipaddr(nif, &lwip_addr);
575 break;
576 }
577 case 1: // gateway
578 {
579 netif_set_gw(nif, &lwip_addr);
580 break;
581 }
582 case 2: // subnet
583 {
584 netif_set_netmask(nif, &lwip_addr);
585 break;
586 }
587 }
588 }else{
589 RETURN_ERR(WL_FAILURE)
590 }
591
592 }
593 /* Disable DHCP */
594 ncfg->dhcp_enabled = STATIC_IP_CONFIG;
595 }else
596 RETURN_ERR(WL_FAILURE)
597
598 RETURN_ERR(err)
599 }
600
set_dns_config_cmd_cb(int numParam,char * buf,void * ctx)601 int set_dns_config_cmd_cb(int numParam, char* buf, void* ctx) {
602 struct ip_addr lwip_addr;
603 struct ctx_server *hs = ctx;
604 struct net_cfg *ncfg = &(hs->net_cfg);
605 struct netif *nif = ncfg->netif;
606 uint8_t parmsToChange=0;
607 const uint8_t MAX_DNS_CONFIG_PARAMS = 2;
608
609 wl_err_t err = WL_SUCCESS;
610 tParam* params = (tParam*) buf;
611
612 if (params->paramLen == 1)
613 {
614 GET_PARAM_NEXT(BYTE, params, _parmsToChange);
615 parmsToChange = _parmsToChange;
616 }
617 else
618 RETURN_ERR(WL_FAILURE)
619
620 INFO_SPI("%p numParam=%d parmsToChange=%d\n", ctx, numParam, parmsToChange);
621
622 if (parmsToChange <= MAX_DNS_CONFIG_PARAMS)
623 {
624 int i=0;
625 for (; i<parmsToChange; ++i)
626 {
627 if (params->paramLen == 4)
628 {
629 GET_PARAM_NEXT(LONG, params, _ip_addr);
630 lwip_addr.addr = _ip_addr;
631 INFO_SPI("%d] nif:%p lwip_addr=0x%x\n", i, nif, lwip_addr.addr);
632 dns_setserver(i, &lwip_addr);
633 }else{
634 RETURN_ERR(WL_FAILURE)
635 }
636 }
637 /* Disable DHCP */
638 ncfg->dhcp_enabled = STATIC_IP_CONFIG;
639 }else
640 RETURN_ERR(WL_FAILURE)
641
642 RETURN_ERR(err)
643 }
644
645
646
set_result(wl_status_t _status)647 void set_result(wl_status_t _status)
648 {
649 result = _status;
650 }
651
652
set_result_cmd(int err)653 void set_result_cmd(int err)
654 {
655 wl_err_t _err = (wl_err_t)err;
656 switch (_err)
657 {
658 case WL_SUCCESS:
659 set_result(WL_CONNECTED);
660 ERROR_LED_OFF();
661 break;
662 default:
663 case WL_OOM:
664 case WL_INVALID_LENGTH:
665 case WL_NOT_SUPPORTED:
666 case WL_ABSORBED:
667 case WL_RESOURCES:
668 case WL_BUSY:
669 case WL_RETRY:
670 case WL_FAILURE:
671 set_result(WL_CONNECT_FAILED);
672 ERROR_LED_ON();
673 break;
674 }
675 INFO_SPI("%s %d\n", __FUNCTION__, result);
676 }
677
678
679
680 extern int ttcp_start(struct ip_addr addr, uint16_t port, void *opaque,
681 void *done_cb, int mode, uint16_t nbuf, uint16_t buflen, int udp, int verbose);
682
683
start_server_tcp(uint16_t port,uint8_t sock,uint8_t protMode)684 int start_server_tcp(uint16_t port, uint8_t sock, uint8_t protMode)
685 {
686 struct ip_addr addr = { 0 };
687 uint16_t buflen = 1024;
688 uint16_t nbuf = 1024;
689 wl_err_t err = WL_FAILURE;
690
691 #ifdef _APP_DEBUG_
692 int verbose = 1;
693 #else
694 int verbose = 0;
695 #endif
696 int udp = protMode;
697 int mode = 1; //RECEIVE
698 void* _ttcp = NULL;
699
700 if (sock >= MAX_SOCK_NUM)
701 return WIFI_SPI_ERR;
702
703 if (_connected)
704 {
705 WARN("Still connected...wait\n");
706 return WIFI_SPI_ERR;
707 }
708
709 if (!ifStatus)
710 {
711 WARN_VER("IF down...wait\n");
712 return WIFI_SPI_ERR;
713 }
714
715
716 if (ard_tcp_start(addr, port, NULL, NULL, mode, nbuf, buflen, udp, verbose, sock, &_ttcp) == 0)
717 {
718 INFO_SPI("Start Server %s [%d, %d] OK!\n", ProtMode2Str(protMode), port, sock);
719 setMapSock(sock, _ttcp);
720 err = WL_SUCCESS;
721 }else{
722
723 WARN("Start Server %s [%d, %d] FAILED!\n", ProtMode2Str(protMode), port, sock);
724 clearMapSockTcp(sock, TTCP_MODE_RECEIVE);
725 }
726 return err;
727 }
728
729
start_server_tcp_cmd_cb(int numParam,char * buf,void * ctx)730 int start_server_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
731 wl_err_t err = WL_FAILURE;
732 tParam* params = (tParam*) buf;
733 if (numParam == 3)
734 {
735 GET_PARAM_NEXT(INT, params, port);
736 GET_PARAM_NEXT(BYTE, params, sock);
737 GET_PARAM_NEXT(BYTE, params, protMode);
738 err = start_server_tcp(port, sock, protMode);
739 }
740 return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
741 }
742
start_client_tcp(uint32_t _addr,uint16_t port,uint8_t sock,uint8_t protMode)743 int start_client_tcp(uint32_t _addr, uint16_t port, uint8_t sock, uint8_t protMode)
744 {
745 uint16_t buflen = 1024;
746 uint16_t nbuf = 1024;
747 wl_err_t err = WL_FAILURE;
748 struct ip_addr addr = { .addr = _addr};
749
750 INFO_SPI("Addr:0x%x, port:%d, sock:%d, prot:%s\n", _addr, port, sock, ProtMode2Str(protMode));
751
752 #ifdef _APP_DEBUG_
753 int verbose = 1;
754 #else
755 int verbose = 0;
756 #endif
757
758 int udp = protMode;
759 int mode = 0; //TRANSMIT
760 void* _ttcp = NULL;
761
762 if (sock >= MAX_SOCK_NUM)
763 return WIFI_SPI_ERR;
764
765 // Check previous connection
766 _ttcp = getTTCP(sock, TTCP_MODE_TRANSMIT);
767 if (_ttcp != NULL)
768 {
769 WARN("Previous client %p not stopped !\n", _ttcp);
770 ard_tcp_stop(_ttcp);
771 clearMapSockTcp(sock, TTCP_MODE_TRANSMIT);
772 }
773
774 if (ard_tcp_start(addr, port, NULL, NULL, mode, nbuf, buflen, udp, verbose, sock, &_ttcp) == 0)
775 {
776 INFO_SPI("Start Client %s %p [0x%x, %d, %d] OK!\n", ProtMode2Str(protMode),
777 _ttcp, addr, port, sock);
778 setMapSock(sock, _ttcp);
779 err = WL_SUCCESS;
780 }else{
781 INFO_SPI("Start Client %s %p [0x%x, %d, %d] FAILED!\n", ProtMode2Str(protMode),
782 _ttcp, addr, port, sock);
783 clearMapSockTcp(sock, TTCP_MODE_TRANSMIT);
784 }
785 return err;
786 }
787
788
start_client_tcp_cmd_cb(int numParam,char * buf,void * ctx)789 int start_client_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
790 wl_err_t err = WL_FAILURE;
791 tParam* params = (tParam*) buf;
792 if (numParam == 4)
793 {
794 GET_PARAM_NEXT(LONG, params, _addr);
795 GET_PARAM_NEXT(INT, params, port);
796 GET_PARAM_NEXT(BYTE, params, sock);
797 GET_PARAM_NEXT(BYTE, params, protMode);
798 err = start_client_tcp(_addr, port, sock, protMode);
799 }
800 return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
801 }
802
stop_client_tcp_cmd_cb(int numParam,char * buf,void * ctx)803 int stop_client_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
804 wl_err_t err = WL_FAILURE;
805 tParam* params = (tParam*) buf;
806 void* _ttcp = NULL;
807
808 if (numParam == 1)
809 {
810 GET_PARAM_NEXT(BYTE, params, sock);
811
812 INFO_SPI("Stop client sock:%d\n", sock);
813
814 if (sock < MAX_SOCK_NUM)
815 {
816 _ttcp = getTTCP(sock, TTCP_MODE_TRANSMIT);
817 ard_tcp_stop(_ttcp);
818 err = WL_SUCCESS;
819 }
820 }
821 return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
822 }
823
insert_data_cmd_cb(int numParam,char * buf,void * ctx)824 int insert_data_cmd_cb(int numParam, char* buf, void* ctx) {
825
826 tDataParam* msg = (tDataParam*) buf;
827 if ((numParam == 2)&&(msg->dataLen == 1))
828 {
829 GET_DATA_BYTE(sock, buf+2);
830 GET_DATA_INT(len, buf+3);
831 //printk("tcp:%p buf:%p len:%d\n", getTTCP(sock), (uint8_t*)(buf+5), len);
832 insertBuf(sock, (uint8_t*)(buf+5), len);
833 }
834 return WIFI_SPI_ACK;
835 }
836
send_data_udp_cmd_cb(int numParam,char * buf,void * ctx)837 int send_data_udp_cmd_cb(int numParam, char* buf, void* ctx) {
838 wl_err_t err = WL_FAILURE;
839
840 tParam* params = (tParam*) buf;
841 if ((numParam == 1)&&(params->paramLen == 1))
842 {
843 GET_PARAM_NEXT(BYTE, params, sock);
844 uint16_t len = 0;
845 uint8_t* p = mergeBuf(sock, NULL, &len);
846 err = sendUdpData(getTTCP(sock, TTCP_MODE_TRANSMIT), p, len);
847 clearBuf(sock);
848 free(p);
849 }
850
851 return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
852 }
853
854
send_data_tcp_cmd_cb(int numParam,char * buf,void * ctx)855 int send_data_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
856 wl_err_t err = WL_FAILURE;
857 DATA_LED_ON();
858 tDataParam* msg = (tDataParam*) buf;
859 if ((numParam == 2)&&(msg->dataLen == 1))
860 {
861 GET_DATA_BYTE(sock, buf+2);
862 GET_DATA_INT(len, buf+3);
863 //printk("tcp:%p buf:%p len:%d\n", getTTCP(sock), (uint8_t*)(buf+5), len);
864 err = sendTcpData(getTTCP(sock, TTCP_MODE_TRANSMIT), (uint8_t*)(buf+5), len);
865 }
866 DATA_LED_OFF();
867 return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
868 }
869
ack_cmd_cb(int numParam,char * buf,void * ctx)870 int ack_cmd_cb(int numParam, char* buf, void* ctx) {
871 return WIFI_SPI_ACK;
872 }
873
get_result_cmd_cb(int numParam,char * buf,void * ctx)874 int get_result_cmd_cb(int numParam, char* buf, void* ctx) {
875 INFO_SPI("ifStatus:%d result:%d\n", ifStatus, result);
876 return WIFI_SPI_ACK;
877 }
878
disconnect_cmd_cb(int numParam,char * buf,void * ctx)879 int disconnect_cmd_cb(int numParam, char* buf, void* ctx)
880 {
881 return ((wl_disconnect()==WL_SUCCESS)? WIFI_SPI_ACK : WIFI_SPI_ERR);
882 }
883
884
get_reply_cb(char * recv,char * reply,void * ctx,uint16_t * count)885 cmd_spi_state_t get_reply_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
886
887 CREATE_HEADER_REPLY(reply, recv, 1);
888
889 reply[3] = 1; // paramLen
890 if (ctx != NULL) {
891 reply[4] = (*(uint8_t*)ctx); //param
892 } else {
893 reply[4] = (ifStatus)?WL_CONNECTED:result; //param
894 }
895
896 END_HEADER_REPLY(reply, 5, *count);
897
898 //INFO_SPI("result:%d\n", result);
899 return SPI_CMD_DONE;
900 }
901
ack_reply_cb(char * recv,char * reply,void * ctx,uint16_t * count)902 cmd_spi_state_t ack_reply_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
903
904 CREATE_HEADER_REPLY(reply, recv, 1);
905
906 reply[3] = 1; // paramLen
907 if (ctx != NULL) {
908 reply[4] = (*(uint8_t*) ctx != 1) ? WIFI_SPI_ERR : WIFI_SPI_ACK; //param
909 } else {
910 reply[4] = WIFI_SPI_ACK; //param
911 }
912
913 END_HEADER_REPLY(reply, 5, *count);
914
915 return SPI_CMD_DONE;
916 }
917
get_reply_ipaddr_cb(char * recv,char * reply,void * ctx,uint16_t * count)918 cmd_spi_state_t get_reply_ipaddr_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
919
920 CHECK_ARD_NETIF(recv, reply, count);
921
922 CREATE_HEADER_REPLY(reply, recv, 3);
923
924 PUT_LONG_IN_BYTE_NO(ard_netif->ip_addr.addr, reply, 3);
925 PUT_LONG_IN_BYTE_NO(ard_netif->netmask.addr, reply, 8);
926 PUT_LONG_IN_BYTE_NO(ard_netif->gw.addr, reply, 13);
927
928 END_HEADER_REPLY(reply, 18, *count);
929
930 return SPI_CMD_DONE;
931 }
932
getRemoteData(uint8_t sock,uint8_t mode,tRemoteClient * remoteData)933 void getRemoteData(uint8_t sock, uint8_t mode, tRemoteClient* remoteData)
934 {
935 if ((sock>=0) && (sock<MAX_SOCK_NUM))
936 {
937 void* p = getTTCP(sock, mode);
938 if (p)
939 {
940 ttcp_t* _ttcp = (ttcp_t* )p;
941 if ((_ttcp->udp == UDP_MODE))
942 {
943 if (_ttcp->mode == TTCP_MODE_RECEIVE)
944 {
945 remoteData->ipaddr = getRemoteClient(sock)->ipaddr;
946 remoteData->port = getRemoteClient(sock)->port;
947 }else{
948 remoteData->ipaddr = (_ttcp->upcb) ? _ttcp->upcb->remote_ip.addr : 0;
949 remoteData->port = (_ttcp->upcb) ? _ttcp->upcb->remote_port : 0;
950 }
951 }
952 }
953 }
954 }
955
956
get_reply_remote_data_cb(char * recv,char * reply,void * ctx,uint16_t * count)957 cmd_spi_state_t get_reply_remote_data_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
958
959 CHECK_ARD_NETIF(recv, reply, count);
960 DUMP_SPI_CMD(recv);
961
962 GET_DATA_BYTE(sock, recv+4);
963
964 CREATE_HEADER_REPLY(reply, recv, 2);
965 tRemoteClient remoteData = {0,0};
966 //TODO pass the mode
967 getRemoteData(sock, TTCP_MODE_RECEIVE, &remoteData);
968
969 PUT_LONG_IN_BYTE_NO(remoteData.ipaddr, reply, 3);
970 PUT_DATA_INT(remoteData.port, reply, 8);
971
972 END_HEADER_REPLY(reply, 11, *count);
973
974 return SPI_CMD_DONE;
975 }
976
977
foundHostByName(const char * name,struct ip_addr * ipaddr,void * callback_arg)978 void foundHostByName(const char *name, struct ip_addr *ipaddr, void *callback_arg)
979 {
980 _hostIpAddr.addr = (ipaddr)?ipaddr->addr:0xffffffff;
981 INFO_SPI("foundHostByName: Found Host: name=%s ip=0x%x\n", name, _hostIpAddr.addr);
982 hostIpAddrFound = true;
983 }
984
req_reply_host_by_name_cb(int numParam,char * buf,void * ctx)985 int req_reply_host_by_name_cb(int numParam, char* buf, void* ctx) {
986
987 char hostName[DNS_MAX_NAME_LENGTH];
988 tParam* params = (tParam*) buf;
989
990 // HostName
991 if (params->paramLen < DNS_MAX_NAME_LENGTH) {
992 memcpy(hostName, ¶ms->param, params->paramLen);
993 hostName[params->paramLen]='\0';
994 } else {
995 RETURN_ERR(WL_FAILURE)
996 }
997
998 INFO_SPI("Looking for Host: name=%s\n", hostName);
999 _hostIpAddr.addr = 0;
1000 hostIpAddrFound = false;
1001 err_t err = dns_gethostbyname(hostName, &_hostIpAddr, foundHostByName, NULL);
1002 if (err == ERR_OK)
1003 {
1004 INFO_SPI("Found Host: name=%s ip=0x%x\n", hostName, _hostIpAddr.addr);
1005 hostIpAddrFound = true;
1006 RETURN_ERR(WL_SUCCESS)
1007 }
1008 RETURN_ERR(WL_FAILURE)
1009 }
1010
get_reply_host_by_name_cb(char * recv,char * reply,void * ctx,uint16_t * count)1011 cmd_spi_state_t get_reply_host_by_name_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1012
1013 u32_t addr = (hostIpAddrFound)?_hostIpAddr.addr : 0xffffffff;
1014 INFO_SPI("Searching for Host: ip=0x%x found=%d\n", addr, hostIpAddrFound);
1015
1016 CHECK_ARD_NETIF(recv, reply, count);
1017
1018 CREATE_HEADER_REPLY(reply, recv, 1);
1019
1020 PUT_LONG_IN_BYTE_NO(addr, reply, 3);
1021
1022 END_HEADER_REPLY(reply, 8, *count);
1023
1024 return SPI_CMD_DONE;
1025 }
1026
get_reply_mac_cb(char * recv,char * reply,void * ctx,uint16_t * count)1027 cmd_spi_state_t get_reply_mac_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1028
1029 CHECK_ARD_NETIF(recv, reply, count);
1030
1031 CREATE_HEADER_REPLY(reply, recv, 1);
1032
1033 reply[3] = WL_MAC_ADDR_LENGTH;
1034 uint8_t mac[WL_MAC_ADDR_LENGTH];
1035 if (wl_get_mac_addr(mac) != WL_SUCCESS) {
1036 RETURN_ERR_REPLY(recv, reply, count);
1037 }
1038 //rotate the byte order
1039 reply[4]=mac[5];
1040 reply[5]=mac[4];
1041 reply[6]=mac[3];
1042 reply[7]=mac[2];
1043 reply[8]=mac[1];
1044 reply[9]=mac[0];
1045 END_HEADER_REPLY(reply, 10, *count);
1046
1047 return SPI_CMD_DONE;
1048 }
1049
get_reply_curr_net_cb(char * recv,char * reply,void * ctx,uint16_t * count)1050 cmd_spi_state_t get_reply_curr_net_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1051
1052 uint32_t type = (uint32_t)ctx;
1053 CHECK_ARD_NETIF(recv, reply, count);
1054
1055 CREATE_HEADER_REPLY(reply, recv, 1);
1056
1057 struct wl_network_t* net = wl_get_current_network();
1058 uint8_t len = 0;
1059 if (net != NULL)
1060 {
1061 switch (type)
1062 {
1063 default:
1064 case GET_CURR_SSID_CMD:
1065 {
1066 len = net->ssid.len;
1067 PUT_BUFDATA_BYTE(net->ssid.ssid, len, reply, 3);
1068 break;
1069 }
1070 case GET_CURR_BSSID_CMD:
1071 {
1072 len = WL_MAC_ADDR_LENGTH; ;
1073 PUT_BUFDATA_BYTE_REV(net->bssid.octet, len, reply, 3);
1074 break;
1075 }
1076 case GET_CURR_RSSI_CMD:
1077 {
1078 len=sizeof(net->rssi);
1079 PUT_LONG_IN_BYTE_HO(net->rssi, reply, 3);
1080 //printk("RSSI:%d", net->rssi);
1081 break;
1082 }
1083 case GET_CURR_ENCT_CMD:
1084 {
1085 len = sizeof(net->enc_type);
1086 PUT_DATA_BYTE(net->enc_type, reply, 3);
1087 //printk("ENCT:%d", net->enc_type);
1088 break;
1089 }
1090 }
1091 }else{
1092 PUT_DATA_BYTE(0, reply, 3);
1093 }
1094
1095 END_HEADER_REPLY(reply, 3+len+1, *count);
1096
1097 //dump(reply, *count);
1098
1099 return SPI_CMD_DONE;
1100 }
1101
get_reply_idx_net_cb(char * recv,char * reply,void * ctx,uint16_t * count)1102 cmd_spi_state_t get_reply_idx_net_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1103
1104 uint32_t type = (uint32_t)ctx;
1105 CHECK_ARD_NETIF(recv, reply, count);
1106
1107 CREATE_HEADER_REPLY(reply, recv, 1);
1108
1109 DUMP_SPI_CMD(recv);
1110
1111 GET_DATA_BYTE(idx, recv+4);
1112
1113 if (idx >= WL_NETWORKS_LIST_MAXNUM)
1114 {
1115 WARN("Index out of range: %d\n", idx);
1116 return SPI_CMD_DONE;
1117 }
1118 uint8_t len = 0;
1119 switch (type)
1120 {
1121 default:
1122 case GET_IDX_SSID_CMD:
1123 {
1124 len = network_list.net[idx]->ssid.len;
1125 PUT_BUFDATA_BYTE(network_list.net[idx]->ssid.ssid, len, reply, 3);
1126 INFO_UTIL("SSID:%s\n", network_list.net[idx]->ssid.ssid);
1127 break;
1128 }
1129 case GET_IDX_RSSI_CMD:
1130 {
1131 len = 4;
1132 PUT_LONG_IN_BYTE_HO(network_list.net[idx]->rssi, reply, 3);
1133 INFO_UTIL("RSSI:%d\n", network_list.net[idx]->rssi);
1134 break;
1135 }
1136 case GET_IDX_ENCT_CMD:
1137 {
1138 len = 1;
1139 PUT_DATA_BYTE(network_list.net[idx]->enc_type, reply, 3);
1140 INFO_UTIL("ENCT:%d\n", network_list.net[idx]->enc_type);
1141 break;
1142 }
1143 }
1144
1145
1146 END_HEADER_REPLY(reply, 3+len+1, *count);
1147
1148 DUMP(reply, *count);
1149
1150 return SPI_CMD_DONE;
1151 }
1152
copy_network_list(struct wl_network_list_t * dst,struct wl_network_list_t * src)1153 static void copy_network_list(struct wl_network_list_t *dst,
1154 struct wl_network_list_t *src)
1155 {
1156 int i;
1157 for (i = 0; i < dst->cnt; i++)
1158 free(dst->net[i]);
1159 free(dst->net);
1160
1161 dst->cnt = 0;
1162
1163 if (src->cnt == 0)
1164 return;
1165 dst->net = calloc(1, src->cnt * sizeof(struct wl_network_t *));
1166 if (dst->net == NULL) {
1167 printk("could not allocate all gui net array\n");
1168 return;
1169 }
1170
1171 for (i = 0; i < src->cnt; i++) {
1172 struct wl_network_t *net = src->net[i];
1173 dst->net[i] = malloc(sizeof(*net));
1174 if (dst->net[i] == NULL) {
1175 printk("could not allocate all gui nets\n");
1176 return;
1177 }
1178
1179 memcpy(dst->net[i], net, sizeof(*net));
1180 dst->cnt++;
1181 }
1182 }
1183
start_scan_net_cmd_cb(int numParam,char * buf,void * ctx)1184 int start_scan_net_cmd_cb(int numParam, char* buf, void* ctx) {
1185 wl_err_t err = WL_FAILURE;
1186
1187 INFO_SPI("Start Network Scan %d\n", numParam);
1188 if (scanNetCompleted){
1189 scanNetCompleted = false;
1190 err = wl_scan();
1191 if (err != WL_SUCCESS)
1192 {
1193 // May be busy scanning already, no fatal error
1194 WARN("err=%d\n", err);
1195 err = WL_SUCCESS;
1196 }
1197 }
1198 return err;
1199 }
1200
get_reply_scan_networks_cb(char * recv,char * reply,void * ctx,uint16_t * count)1201 cmd_spi_state_t get_reply_scan_networks_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1202
1203 const int8_t SCAN_NOT_YET_COMPLETED = 0;
1204
1205 if (!scanNetCompleted)
1206 {
1207 //return empty list with an error to retry
1208 CREATE_HEADER_REPLY(reply, recv, SCAN_NOT_YET_COMPLETED);
1209 END_HEADER_REPLY(reply, 3, *count);
1210 INFO_SPI("Scan not completed!\n");
1211 return SPI_CMD_DONE;
1212 }
1213
1214 int network_cnt = 0;
1215 struct wl_network_list_t* wl_network_list;
1216
1217 wl_get_network_list(&wl_network_list);
1218 if (wl_network_list->cnt == 0)
1219 {
1220 CREATE_HEADER_REPLY(reply, recv, 0);
1221 END_HEADER_REPLY(reply, 3, *count);
1222 INFO_SPI("Networks not found!\n");
1223 return SPI_CMD_DONE;
1224 }
1225
1226 if (wl_network_list->cnt > WL_NETWORKS_LIST_MAXNUM)
1227 {
1228 network_cnt = WL_NETWORKS_LIST_MAXNUM ;
1229 }
1230 else{
1231 network_cnt = wl_network_list->cnt ;
1232 }
1233
1234 copy_network_list(&network_list, wl_network_list);
1235 CREATE_HEADER_REPLY(reply, recv, network_cnt);
1236
1237 uint8_t start = 3;
1238 int ii = 0;
1239 for (; ii < network_cnt; ii++)
1240 {
1241 uint8_t len = network_list.net[ii]->ssid.len+1;
1242 network_list.net[ii]->ssid.ssid[network_list.net[ii]->ssid.len]=0;
1243 PUT_BUFDATA_BYTE(network_list.net[ii]->ssid.ssid, len, reply, start);
1244 start += len+1;
1245 INFO_SPI("%d - %s [%d]- %d - %d - 0x%x\n",ii, network_list.net[ii]->ssid.ssid,
1246 len, network_list.net[ii]->enc_type,
1247 network_list.net[ii]->rssi, network_list.net[ii]->bssid);
1248 }
1249
1250 END_HEADER_REPLY(reply, start, *count);
1251 //DUMP(reply, *count);
1252
1253 return SPI_CMD_DONE;
1254 }
1255
get_state_tcp_cmd_cb(char * recv,char * reply,void * ctx,uint16_t * count)1256 cmd_spi_state_t get_state_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1257
1258 CHECK_ARD_NETIF(recv, reply, count);
1259
1260 CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
1261
1262 uint8_t _state = CLOSED;
1263 if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
1264 {
1265 _state = getStateTcp(getTTCP((uint8_t)recv[4], TTCP_MODE_RECEIVE), 0);
1266 }
1267 PUT_DATA_BYTE(_state, reply, 3);
1268 END_HEADER_REPLY(reply, 5, *count);
1269 INFO_SPI_POLL("state:%d\n", _state);
1270
1271 return SPI_CMD_DONE;
1272 }
1273
get_client_state_tcp_cmd_cb(char * recv,char * reply,void * ctx,uint16_t * count)1274 cmd_spi_state_t get_client_state_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1275
1276 CHECK_ARD_NETIF(recv, reply, count);
1277
1278 CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
1279
1280 uint8_t _state = CLOSED;
1281 uint8_t _sock = recv[4];
1282 if ((recv[3]==1)&&(_sock>=0)&&(_sock<MAX_SOCK_NUM))
1283 {
1284 void * p= getTTCP(_sock, TTCP_MODE_TRANSMIT);
1285 if (p!=NULL)
1286 {
1287 _state = getStateTcp(p, 1);
1288 }else{
1289 WARN_VER("TTCP not found for sock:%d\n", _sock);
1290 }
1291 }
1292 PUT_DATA_BYTE(_state, reply, 3);
1293 END_HEADER_REPLY(reply, 5, *count);
1294 INFO_SPI_POLL("sock:%d state:%d\n", _sock, _state);
1295
1296 return SPI_CMD_DONE;
1297 }
1298
avail_data_tcp_cmd_cb(char * recv,char * reply,void * ctx,uint16_t * count)1299 cmd_spi_state_t avail_data_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1300
1301 CHECK_ARD_NETIF(recv, reply, count);
1302
1303 CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
1304 uint16_t dataAvail = 0;
1305 if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
1306 {
1307 dataAvail = getAvailTcpDataByte((uint8_t)recv[4]);
1308 }
1309 PUT_DATA_INT_NO(dataAvail, reply, 3);
1310 END_HEADER_REPLY(reply, 6, *count);
1311
1312 INFO_SPI_POLL("dataAvail:%d\n", dataAvail);
1313
1314 return SPI_CMD_DONE;
1315 }
1316
test_cmd_cb(char * recv,char * reply,void * ctx,uint16_t * count)1317 cmd_spi_state_t test_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1318
1319 static int counter = 0;
1320 CHECK_ARD_NETIF(recv, reply, count);
1321
1322 CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
1323 PUT_DATA_BYTE(++counter, reply, 3);
1324 END_HEADER_REPLY(reply, 5, *count);
1325 return SPI_CMD_DONE;
1326 }
1327
data_sent_tcp_cmd_cb(char * recv,char * reply,void * ctx,uint16_t * count)1328 cmd_spi_state_t data_sent_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1329
1330 CHECK_ARD_NETIF(recv, reply, count);
1331 SIGN2_DN();
1332 CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
1333 uint8_t dataSent = 0;
1334 if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
1335 {
1336 dataSent = isDataSent(getTTCP((uint8_t)recv[4], TTCP_MODE_TRANSMIT));
1337 }
1338 PUT_DATA_BYTE(dataSent, reply, 3);
1339 END_HEADER_REPLY(reply, 5, *count);
1340 SIGN2_UP();
1341 return SPI_CMD_DONE;
1342 }
1343
get_data_tcp_cmd_cb(char * recv,char * reply,void * ctx,uint16_t * count)1344 cmd_spi_state_t get_data_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1345
1346 uint8_t data;
1347
1348 CHECK_ARD_NETIF(recv, reply, count);
1349
1350 tParam* params = (tParam*)&recv[3];
1351
1352 GET_PARAM_NEXT(BYTE, params, _sock);
1353 GET_PARAM_NEXT(INT, params, _peek);
1354
1355 if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
1356 {
1357 SIGN2_DN();
1358
1359 if (getTcpDataByte((uint8_t)recv[4], &data, _peek))
1360 {
1361 CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
1362 PUT_DATA_BYTE(data, reply, 3);
1363 END_HEADER_REPLY(reply, 5, *count);
1364 }else{
1365 CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_0);
1366 END_HEADER_REPLY(reply, 3, *count);
1367 }
1368 SIGN2_UP();
1369 }
1370 return SPI_CMD_DONE;
1371 }
1372
get_databuf_tcp_cmd_cb(char * recv,char * reply,void * ctx,uint16_t * count)1373 cmd_spi_state_t get_databuf_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1374
1375 uint8_t* data;
1376 uint16_t len;
1377
1378 CHECK_ARD_NETIF(recv, reply, count);
1379
1380 GET_DATA_BYTE(sock, buf+5);
1381 if ((sock>=0)&&(sock<MAX_SOCK_NUM))
1382 {
1383 if (getTcpData((uint8_t)sock, (void**)&data, &len))
1384 {
1385 CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
1386 PUT_BUFDATA_INT(data, len, reply, 3);
1387 END_HEADER_REPLY(reply, 3+len+2, *count);
1388 freeTcpData((uint8_t)sock);
1389 }else{
1390 CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_0);
1391 END_HEADER_REPLY(reply, 3, *count);
1392 }
1393 }
1394 return SPI_CMD_DONE;
1395 }
1396
get_firmware_version_cmd_cb(char * recv,char * reply,void * ctx,uint16_t * count)1397 cmd_spi_state_t get_firmware_version_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1398
1399 CHECK_ARD_NETIF(recv, reply, count);
1400
1401 CREATE_HEADER_REPLY(reply, recv, 1);
1402
1403 uint8_t len = strlen(fwVersion);
1404
1405 PUT_BUFDATA_BYTE(fwVersion, len, reply, 3);
1406
1407 END_HEADER_REPLY(reply, 3+len+1, *count);
1408
1409 return SPI_CMD_DONE;
1410 }
1411
get_test_cmd_cb(char * recv,char * reply,void * ctx,uint16_t * count)1412 cmd_spi_state_t get_test_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
1413
1414 uint8_t buffer[255] = {0};
1415
1416 CHECK_ARD_NETIF(recv, reply, count);
1417
1418 CREATE_HEADER_REPLY(reply, recv, 1);
1419 uint8_t len = 0;
1420 if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<0xFF))
1421 {
1422 len = recv[4];
1423 int i= 0;
1424 for (; i<len; ++i) buffer[i]=i;
1425 PUT_BUFDATA_BYTE(buffer, len, reply, 3);
1426 }else{
1427 len = strlen(fwVersion);
1428 PUT_BUFDATA_BYTE(fwVersion, len, reply, 3);
1429 }
1430 END_HEADER_REPLY(reply, 3+len+1, *count);
1431
1432 return SPI_CMD_DONE;
1433 }
1434
sendReply(int cmdIdx,char * recv,char * reply,void * resultCmd)1435 int sendReply(int cmdIdx, char* recv, char* reply, void* resultCmd)
1436 {
1437 uint16_t _count = 0;
1438 int _result = SPI_OK;
1439
1440 cmd_spi_list[cmdIdx].reply_cb(recv, reply, resultCmd, &_count);
1441 state = SPI_CMD_REPLING;
1442
1443 AVAIL_FOR_SPI();
1444 _result = write_stream(ARD_SPI, &reply[0], _count);
1445 #ifdef _SPI_STATS_
1446 if ( _result != SPI_OK)
1447 {
1448 statSpi.lastCmd = cmd_spi_list[cmdIdx].cmd_id;
1449 }
1450 #endif
1451 BUSY_FOR_SPI();
1452
1453 IF_SPI_DUMP(printk("==>"));
1454 DUMP_SPI(recv, count);
1455 IF_SPI_DUMP(printk("<=="));
1456 DUMP_SPI(reply, _count);
1457 replyCount = _count;
1458 return _result;
1459 }
1460
getStartCmdSeq(unsigned char * _recv,int len,int * offset)1461 unsigned char* getStartCmdSeq(unsigned char* _recv, int len, int *offset)
1462 {
1463 int i = 0;
1464 *offset = 0;
1465 //DEB_PIN_UP();
1466 for (; i<len; ++i)
1467 {
1468 if (_recv[i]==START_CMD)
1469 {
1470 if (i!=0)
1471 {
1472 DEB_PIN_TRIGGER();
1473 IF_WARN_VER(dump((char*)_recv, (uint16_t)len));
1474 WARN("%d] Disall. %d/%d cmd:%d\n", cmdCorr, i, len,_recv[i+1]);
1475 }
1476 *offset = i;
1477 return &_recv[i];
1478 }
1479 }
1480 //DEB_PIN_DN();
1481 WARN("%d] Disall. %d\n", cmdCorr, i);
1482
1483 return NULL;
1484 }
1485
spiMsg8(uint8_t cmd)1486 inline bool spiMsg8(uint8_t cmd)
1487 {
1488 return ((cmd & DATA_FLAG)==0);
1489 }
1490
call_reply_cb(char * recv,char * reply)1491 int call_reply_cb(char* recv, char* reply) {
1492
1493 // // check the start of message
1494 // //TODO CHECK if also the ,en must be resize
1495 // //char* recv = (char*)getStartCmdSeq((unsigned char*)_recv, &count);
1496 // char* recv = (char*)getStartCmdSeq((unsigned char*)_recv, count);
1497 // if (recv == NULL)
1498 // return REPLY_ERR_MSG;
1499
1500 unsigned char cmdId = (unsigned char) recv[1];
1501 uint8_t _result = REPLY_NO_ERR;
1502 U32 i;
1503 for (i = 0; i < ARRAY_SIZE(cmd_spi_list); i++) {
1504 if (cmd_spi_list[i].cmd_id == cmdId) {
1505
1506 if (cmd_spi_list[i].flags == CMD_SET_FLAG) {
1507 //Send Reply for SET commands
1508 if (sendReply(i, recv, reply, cmd_spi_list[i].ctx) != SPI_OK)
1509 return REPLY_ERR_SET;
1510 if (spiMsg8(cmdId))
1511 {
1512 tSpiMsg* spiMsg = (tSpiMsg*) recv;
1513 _result = cmd_spi_list[i].cb(spiMsg->nParam,
1514 (char*) &(spiMsg->params[0]), cmd_spi_list[i].ctx);
1515 }else
1516 {
1517 tSpiMsgData* spiMsg = (tSpiMsgData*) recv;
1518 _result = cmd_spi_list[i].cb(spiMsg->nParam,
1519 (char*) &(spiMsg->params[0]), cmd_spi_list[i].ctx);
1520 }
1521
1522 if (_result != WIFI_SPI_ACK)
1523 return REPLY_ERR_CMD;
1524 else
1525 return REPLY_NO_ERR;
1526 }else{
1527 if (spiMsg8(cmdId))
1528 {
1529 tSpiMsg* spiMsg = (tSpiMsg*) recv;
1530 _result = cmd_spi_list[i].cb(spiMsg->nParam,
1531 (char*) &(spiMsg->params[0]), NULL);
1532 }else{
1533 tSpiMsgData* spiMsg = (tSpiMsgData*) recv;
1534 _result = cmd_spi_list[i].cb(spiMsg->nParam,
1535 (char*) &(spiMsg->params[0]), NULL);
1536 }
1537 //Send Reply for GET commands or Immediate SET apply
1538 if (cmd_spi_list[i].flags == CMD_GET_FLAG) {
1539 if (sendReply(i, recv, reply, cmd_spi_list[i].ctx) != SPI_OK)
1540 return REPLY_ERR_GET;
1541 else
1542 return REPLY_NO_ERR;
1543 }else if (cmd_spi_list[i].flags == CMD_IMM_SET_FLAG)
1544 {
1545 if (sendReply(i, recv, reply, &_result) != SPI_OK)
1546 return REPLY_ERR_GET;
1547 else
1548 return REPLY_NO_ERR;
1549
1550 }
1551 }
1552 }
1553 }
1554 // Command not found
1555 if (i==ARRAY_SIZE(cmd_spi_list))
1556 {
1557 WARN("Unknown cmd 0x%x\n", cmdId);
1558 DUMP(recv, count);
1559 return REPLY_ERR_CMD;
1560 }
1561 return REPLY_NO_ERR;
1562 }
1563
init_spi_cmds(void * ctx)1564 void init_spi_cmds(void* ctx) {
1565 spi_add_cmd(SET_NET_CMD, set_net_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
1566 spi_add_cmd(SET_PASSPHRASE_CMD, set_passphrase_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
1567 spi_add_cmd(SET_KEY_CMD, set_key_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
1568 spi_add_cmd(SET_IP_CONFIG_CMD, set_ip_config_cmd_cb, ack_reply_cb, ctx, CMD_SET_FLAG);
1569 spi_add_cmd(SET_DNS_CONFIG_CMD, set_dns_config_cmd_cb, ack_reply_cb, ctx, CMD_SET_FLAG);
1570 spi_add_cmd(GET_CONN_STATUS_CMD, get_result_cmd_cb, get_reply_cb, NULL, CMD_GET_FLAG);
1571 spi_add_cmd(GET_IPADDR_CMD, ack_cmd_cb, get_reply_ipaddr_cb, NULL, CMD_GET_FLAG);
1572 spi_add_cmd(GET_MACADDR_CMD, ack_cmd_cb, get_reply_mac_cb, NULL, CMD_GET_FLAG);
1573 spi_add_cmd(GET_CURR_SSID_CMD, ack_cmd_cb, get_reply_curr_net_cb, (void*)GET_CURR_SSID_CMD, CMD_GET_FLAG);
1574 spi_add_cmd(GET_CURR_BSSID_CMD, ack_cmd_cb, get_reply_curr_net_cb, (void*)GET_CURR_BSSID_CMD, CMD_GET_FLAG);
1575 spi_add_cmd(GET_CURR_RSSI_CMD, ack_cmd_cb, get_reply_curr_net_cb, (void*)GET_CURR_RSSI_CMD, CMD_GET_FLAG);
1576 spi_add_cmd(GET_CURR_ENCT_CMD, ack_cmd_cb, get_reply_curr_net_cb, (void*)GET_CURR_ENCT_CMD, CMD_GET_FLAG);
1577 spi_add_cmd(START_SCAN_NETWORKS, start_scan_net_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
1578 spi_add_cmd(SCAN_NETWORKS, ack_cmd_cb, get_reply_scan_networks_cb, NULL, CMD_GET_FLAG);
1579 spi_add_cmd(DISCONNECT_CMD, disconnect_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
1580 spi_add_cmd(GET_IDX_ENCT_CMD, ack_cmd_cb, get_reply_idx_net_cb, (void*)GET_IDX_ENCT_CMD, CMD_GET_FLAG);
1581 spi_add_cmd(GET_IDX_SSID_CMD, ack_cmd_cb, get_reply_idx_net_cb, (void*)GET_IDX_SSID_CMD, CMD_GET_FLAG);
1582 spi_add_cmd(GET_IDX_RSSI_CMD, ack_cmd_cb, get_reply_idx_net_cb, (void*)GET_IDX_RSSI_CMD, CMD_GET_FLAG);
1583 spi_add_cmd(REQ_HOST_BY_NAME_CMD, req_reply_host_by_name_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
1584 spi_add_cmd(GET_HOST_BY_NAME_CMD, ack_cmd_cb, get_reply_host_by_name_cb, NULL, CMD_GET_FLAG);
1585 spi_add_cmd(START_SERVER_TCP_CMD, start_server_tcp_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
1586 spi_add_cmd(START_CLIENT_TCP_CMD, start_client_tcp_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
1587 spi_add_cmd(STOP_CLIENT_TCP_CMD, stop_client_tcp_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
1588 spi_add_cmd(GET_STATE_TCP_CMD, ack_cmd_cb, get_state_tcp_cmd_cb, NULL, CMD_GET_FLAG);
1589 spi_add_cmd(GET_DATA_TCP_CMD, ack_cmd_cb, get_data_tcp_cmd_cb, NULL, CMD_GET_FLAG);
1590 spi_add_cmd(AVAIL_DATA_TCP_CMD, ack_cmd_cb, avail_data_tcp_cmd_cb, NULL, CMD_GET_FLAG);
1591 spi_add_cmd(SEND_DATA_TCP_CMD, send_data_tcp_cmd_cb, ack_reply_cb, NULL, CMD_IMM_SET_FLAG);
1592 spi_add_cmd(DATA_SENT_TCP_CMD, ack_cmd_cb, data_sent_tcp_cmd_cb, NULL, CMD_GET_FLAG);
1593 spi_add_cmd(GET_DATABUF_TCP_CMD, ack_cmd_cb, get_databuf_tcp_cmd_cb, NULL, CMD_GET_FLAG);
1594 spi_add_cmd(GET_CLIENT_STATE_TCP_CMD, ack_cmd_cb, get_client_state_tcp_cmd_cb, NULL, CMD_GET_FLAG);
1595 spi_add_cmd(GET_FW_VERSION_CMD, ack_cmd_cb, get_firmware_version_cmd_cb, NULL, CMD_GET_FLAG);
1596 spi_add_cmd(GET_TEST_CMD, ack_cmd_cb, get_test_cmd_cb, NULL, CMD_GET_FLAG);
1597 spi_add_cmd(INSERT_DATABUF_CMD, insert_data_cmd_cb, ack_reply_cb, NULL, CMD_IMM_SET_FLAG);
1598 spi_add_cmd(SEND_DATA_UDP_CMD, send_data_udp_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
1599 spi_add_cmd(GET_REMOTE_DATA_CMD, ack_cmd_cb, get_reply_remote_data_cb, NULL, CMD_GET_FLAG);
1600 }
1601
1602
checkMsgParam8(unsigned char * buf)1603 int checkMsgParam8(unsigned char* buf)
1604 {
1605 int paramLenTot=0;
1606 tSpiMsg* spiMsg = (tSpiMsg*)buf;
1607 tParam *param = spiMsg->params;
1608 int i=0;
1609 for (; i<spiMsg->nParam; ++i)
1610 {
1611 uint8_t _len = param->paramLen;
1612 paramLenTot+= _len+1;
1613 //printk("%d) len:0x%x\n", i, _len);
1614 param = (tParam*)((char*)(param)+_len+1);
1615 }
1616 return paramLenTot;
1617 }
1618
checkMsgParam16(unsigned char * buf)1619 int checkMsgParam16(unsigned char* buf)
1620 {
1621 int paramLenTot=0;
1622 tSpiMsgData* spiMsg = (tSpiMsgData*)buf;
1623 tDataParam* param = (tDataParam*)spiMsg->params;
1624 int i=0;
1625 for (; i<spiMsg->nParam; ++i)
1626 {
1627 uint16_t _len = param->dataLen;
1628 paramLenTot+= _len+sizeof(param->dataLen);
1629 //printk("%d) len:0x%x\n", i, _len);
1630 param = (tDataParam*)((char*)(param)+_len+sizeof(param->dataLen));
1631 }
1632 return paramLenTot;
1633 }
1634
checkMsgFormat(uint8_t * _recv,int len,int * offset)1635 bool checkMsgFormat(uint8_t* _recv, int len, int* offset)
1636 {
1637
1638 unsigned char* recv = getStartCmdSeq(_recv, len, offset);
1639 if ((recv == NULL)||(recv!=_recv))
1640 {
1641 DEB_PIN_TRIGGER();
1642
1643 IF_WARN_VER(DUMP((char*)_recv, len));
1644
1645 STATSPI_DISALIGN_ERROR();
1646
1647 if (recv == NULL)
1648 return false;
1649 }
1650 tSpiMsg* spiMsg = (tSpiMsg*) recv;
1651 if ((spiMsg->cmd == START_CMD)&&((spiMsg->tcmd & REPLY_FLAG) == 0))
1652 {
1653 int paramLenTot = 0;
1654 if (spiMsg8(spiMsg->tcmd))
1655 paramLenTot = checkMsgParam8(recv);
1656 else
1657 {
1658 DUMP_SPI(_recv, len);
1659 paramLenTot = checkMsgParam16(recv);
1660 }
1661
1662 //INFO_SPI("cmd:0x%x TotLen:%d\n", spiMsg->tcmd, paramLenTot);
1663 char* p = (char*)recv + paramLenTot + sizeof(tSpiHdr);
1664 if (*p == END_CMD)
1665 {
1666 return true;
1667 }else{
1668 WARN("%d] Not found end cmd: 0x%x\n", cmdCorr, *p);
1669 }
1670 }
1671 return false;
1672 }
1673
1674 //#define AVR32_USART_CSR_ITERATION_MASK (UNDERRUN) 0x00000400
1675 //#define AVR32_USART_CSR_OVRE_MASK 0x00000020
1676 //#define AVR32_USART_CSR_RXRDY_MASK 0x00000001
1677
1678
spi_poll(struct netif * netif)1679 void spi_poll(struct netif* netif) {
1680
1681 ard_netif = netif;
1682
1683 if (startReply)
1684 {
1685 startReply = false;
1686 int offset = 0;
1687 DISABLE_SPI_INT();
1688 if (checkMsgFormat(_receiveBuffer, receivedChars, &offset))
1689 {
1690 state = SPI_CMD_INPROGRESS;
1691 count = receivedChars-offset;
1692 if (count >= CMD_MAX_LEN)
1693 count = CMD_MAX_LEN;
1694 memcpy(buf, &_receiveBuffer[offset], count);
1695
1696 //mark as buffer used
1697 _receiveBuffer[0] = 0;
1698
1699 int err = call_reply_cb(buf, &reply[0]);
1700 if (err != REPLY_NO_ERR)
1701 {
1702 DUMP_SPI(buf, count);
1703 DUMP_SPI(reply, replyCount);
1704 }
1705 receivedChars = 0;
1706 count = 0;
1707 state = SPI_CMD_IDLE;
1708 }
1709 else
1710 {
1711 sendError();
1712 WARN("%d] Check format msg failed!\n", cmdCorr);
1713 IF_WARN_VER(dump((char*)_receiveBuffer, receivedChars));
1714 state = SPI_CMD_IDLE;
1715 count=0;
1716 //mark as buffer used
1717 _receiveBuffer[0] = 0;
1718 }
1719 CLEAR_SPI_INT();
1720 //Enable Spi int to receive a new command
1721 ENABLE_SPI_INT();
1722 //Available for receiving a new spi data
1723 AVAIL_FOR_SPI();
1724 }
1725
1726 #ifdef _SPI_STATS_
1727 if (statSpi.lastError != 0)
1728 {
1729 WARN("%d] Errot=0x%x spiStatus:0x%x\n", cmdCorr, statSpi.lastError, statSpi.status);
1730 statSpi.lastError = 0;
1731 }
1732 #endif
1733 }
1734
spi_slaveReceiveInt(volatile avr32_spi_t * spi)1735 inline int spi_slaveReceiveInt(volatile avr32_spi_t *spi)
1736 {
1737 receivedChars=0;
1738 int index = 0;
1739 int err = SPI_OK;
1740 state = SPI_CMD_INPUT;
1741 bool endOfFrame = false;
1742
1743 do {
1744 unsigned int timeout = SPI_TIMEOUT;
1745 err = SPI_OK;
1746
1747 while ((spi->sr & (AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK)) !=
1748 (AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK)) {
1749 if ((timeout--)==0) {
1750 err=SPI_ERROR_TIMEOUT;
1751 break;
1752 }
1753 }
1754 //DEB_PIN_TG();
1755 #if 0
1756 #ifdef _SPI_STATS_
1757 if (spi->sr & AVR32_SPI_SR_OVRES_MASK)
1758 {
1759 STATSPI_OVERRIDE_ERROR();
1760 }
1761 #endif
1762 #endif
1763 if (err == SPI_OK) {
1764 _receiveBuffer[index] = (spi->rdr >> AVR32_SPI_RDR_RD_OFFSET) & 0x00ff;
1765 DEB_PIN_UP(2);
1766 if ((index==0) && (_receiveBuffer[index] != START_CMD))
1767 DEB_PIN_TRIGGER();
1768 ++index;
1769 ++receivedChars;
1770 }else{
1771 #ifdef _SPI_STATS_
1772 STATSPI_TIMEOUT_ERROR();
1773 #endif
1774 break;
1775 }
1776
1777 /* break on buffer overflow */
1778 if (receivedChars >= _BUFFERSIZE) {
1779 err = SPI_ERROR_OVERRUN_AND_MODE_FAULT;
1780 break;
1781 }
1782
1783 if (_receiveBuffer[index - 1] == END_CMD)
1784 {
1785 int8_t numParams = 0;
1786 int idx = PARAM_LEN_POS+1;
1787 bool islen16bit = ((_receiveBuffer[CMD_POS] & DATA_FLAG) == DATA_FLAG);
1788 if (index >= idx)
1789 {
1790 numParams = _receiveBuffer[PARAM_LEN_POS];
1791 while (((index-1) > idx)&&(numParams>0))
1792 {
1793 if (islen16bit)
1794 idx += (_receiveBuffer[idx]<<8) + _receiveBuffer[idx+1]+2;
1795 else
1796 idx += _receiveBuffer[idx]+1;
1797 --numParams;
1798 }
1799 if (((index-1) == idx) && (numParams == 0))
1800 endOfFrame = true;
1801 }
1802 if (!endOfFrame){
1803 WARN("Wrong termination index:%d nParam:%d idx:%d 16bit:%d\n", index, numParams, idx, islen16bit);
1804 #ifdef _DEBUG_
1805 dump((char*)_receiveBuffer, receivedChars);
1806 while(0);
1807 #endif
1808 }
1809 }
1810 } while (!endOfFrame);
1811 return err;
1812 }
1813
1814 #if defined (__GNUC__)
1815 __attribute__((__interrupt__))
1816 #elif defined (__ICCAVR32__)
1817 __interrupt
1818 #endif
spi_int_handler(void)1819 static void spi_int_handler(void)
1820 {
1821 volatile avr32_spi_t *spi = ARD_SPI;
1822 DEB_PIN_DN(2);
1823 DISABLE_SPI_INT();
1824
1825 if ((spi->sr & AVR32_SPI_SR_RDRF_MASK) != 0)
1826 {
1827 int err = spi_slaveReceiveInt(ARD_SPI);
1828 if (err == SPI_OK)
1829 {
1830 BUSY_FOR_SPI();
1831 startReply=true;
1832 ++cmdCorr;
1833 //maintain disable interrupt to send the reply command
1834 return;
1835 }
1836 }
1837 ENABLE_SPI_INT();
1838 }
1839
spi_read8(volatile avr32_spi_t * spi,unsigned char * data)1840 inline spi_status_t spi_read8(volatile avr32_spi_t *spi, unsigned char *data)
1841 {
1842 unsigned int timeout = SPI_TIMEOUT;
1843
1844 while ((spi->sr & (AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK)) !=
1845 (AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK)) {
1846 if (!timeout--) {
1847 return SPI_ERROR_TIMEOUT;
1848 }
1849 }
1850
1851 *data = (spi->rdr >> AVR32_SPI_RDR_RD_OFFSET) & 0x00ff;
1852
1853 return SPI_OK;
1854 }
1855
1856
1857 /*!
1858 * \brief Interrupt handler of the External interrupt line "1".
1859 */
1860 #if __GNUC__
1861 __attribute__((__interrupt__))
1862 #elif __ICCAVR32__
1863 __interrupt
1864 #endif
eic_int_handler1(void)1865 static void eic_int_handler1(void)
1866 {
1867 eic_clear_interrupt_line(&AVR32_EIC, EXT_INT_LINE1);
1868 startRecvCmdSignal = TRUE;
1869 }
1870
1871 //! Structure holding the configuration parameters of the EIC module.
1872 eic_options_t eic_options[EXT_INT_NB_LINES];
1873
initExtInt()1874 void initExtInt()
1875 {
1876 // Enable edge-triggered interrupt.
1877 eic_options[0].eic_mode = EIC_MODE_EDGE_TRIGGERED;
1878 // Interrupt will trigger on falling edge.
1879 eic_options[0].eic_edge = EIC_EDGE_FALLING_EDGE;
1880 // Initialize in synchronous mode : interrupt is synchronized to the clock
1881 eic_options[0].eic_async = EIC_SYNCH_MODE;
1882 // Set the interrupt line number.
1883 eic_options[0].eic_line = EXT_INT_LINE1;
1884
1885 // Disable all interrupts.
1886 Disable_global_interrupt();
1887
1888 INTC_register_interrupt(&eic_int_handler1, EXT_INT_IRQ_LINE1, AVR32_INTC_INT0);
1889
1890 // Map the interrupt lines to the GPIO pins with the right peripheral functions.
1891 gpio_enable_module_pin(EXT_INT_PIN_LINE1,EXT_INT_FUNCTION_LINE1);
1892
1893 // Init the EIC controller with the options
1894 eic_init(&AVR32_EIC, eic_options, EXT_INT_NB_LINES);
1895
1896 // Enable the chosen lines and their corresponding interrupt feature.
1897 eic_enable_line(&AVR32_EIC, eic_options[0].eic_line);
1898 eic_enable_interrupt_line(&AVR32_EIC, eic_options[0].eic_line);
1899
1900 // Enable all interrupts.
1901 Enable_global_interrupt();
1902 }
1903
initSpi(void * ctx)1904 int initSpi(void* ctx)
1905 {
1906 volatile avr32_spi_t *spi = &AVR32_SPI0;
1907 gpio_map_t spi_piomap = { \
1908 {AVR32_SPI0_SCK_0_0_PIN, AVR32_SPI0_SCK_0_0_FUNCTION}, \
1909 {AVR32_SPI0_MISO_0_0_PIN, AVR32_SPI0_MISO_0_0_FUNCTION}, \
1910 {AVR32_SPI0_MOSI_0_0_PIN, AVR32_SPI0_MOSI_0_0_FUNCTION}, \
1911 {AVR32_SPI0_NPCS_0_0_PIN, AVR32_SPI0_NPCS_0_0_FUNCTION}, \
1912 };
1913
1914 INFO_INIT("SPI init...\n");
1915
1916 /* Init PIO */
1917 gpio_enable_module(spi_piomap, ARRAY_SIZE(spi_piomap));
1918
1919 spi_options_t spiOptions;
1920
1921 spiOptions.reg = 0;
1922 spiOptions.baudrate = SPI_SLAVE_SPEED;
1923 spiOptions.bits = SPI_BITS;
1924 spiOptions.spck_delay = 0;
1925 spiOptions.trans_delay = 4;
1926 spiOptions.stay_act = 0;
1927 spiOptions.spi_mode = 0;
1928 spiOptions.modfdis = 0;
1929
1930 /* Initialize as slave; bits, spi_mode */
1931 if (spi_initSlave(spi, spiOptions.bits, spiOptions.spi_mode) != SPI_OK)
1932 {
1933 INFO_SPI("SPI initialization failed!");
1934 return 1;
1935 }
1936
1937 spi_status_t status = spi_setupChipReg(spi, &spiOptions, FPBA_HZ);
1938 if (status == SPI_ERROR_ARGUMENT)
1939 WARN("Error configuring SPI\n");
1940
1941 // Disable all interrupts.
1942 Disable_global_interrupt();
1943
1944 // Register the SPI interrupt handler to the interrupt controller.
1945 INTC_register_interrupt((__int_handler)(&spi_int_handler), AVR32_SPI0_IRQ, AVR32_INTC_INT0);
1946
1947 // Enable all interrupts.
1948 Enable_global_interrupt();
1949
1950 ENABLE_SPI_INT();
1951
1952 spi_enable(spi);
1953 #ifdef _SPI_STATS_
1954 initStatSpi();
1955 #endif
1956 init_spi_cmds(ctx);
1957
1958 memset(_receiveBuffer, 0, sizeof(_receiveBuffer));
1959 memset(buf, 0, sizeof(buf));
1960 memset(reply, 0, sizeof(reply));
1961
1962 initMapSockTcp();
1963 set_result(WL_IDLE_STATUS);
1964
1965 init_pBuf();
1966
1967 return 0;
1968 }
1969
1970