aboutsummaryrefslogtreecommitdiff
path: root/firmwares/wifishield/wifiHD/src/ard_spi.c
blob: 8bd288ba17d5c8400bfcb6ff471d817a44a009ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
/*
 * ard_spi.c
 *
 *  Created on: May 27, 2010
 *      Author: mlf by Metodo2 srl
 */

//#define _APP_DEBUG_

#include <avr32/io.h>
#include "board.h"
#include "gpio.h"
#include "usart.h"
#include "ard_spi.h"
#include "ard_tcp.h"
#include "wifi_spi.h"
#include "wl_cm.h"
#include "ard_utils.h"
#include "intc.h"
#include "spi.h"
#include "debug.h"
#include "delay.h"
#include "eic.h"
#include "timer.h"
#include "lwip/dns.h"
#include <board_init.h>
#include "util.h"
#include "lwip/udp.h"
#include "lwip_setup.h"

extern const char* fwVersion;

/*! \name USART Settings
 */
//! @{
#if BOARD == EVK1105
#  define ARD_USART_SPI                 (&AVR32_USART1)
#  define ARD_USART_SPI_SCK_PIN         AVR32_USART1_CLK_0_PIN
#  define ARD_USART_SPI_SCK_FUNCTION    AVR32_USART1_CLK_0_FUNCTION
#  define ARD_USART_SPI_MISO_PIN        AVR32_USART1_TXD_0_0_PIN
#  define ARD_USART_SPI_MISO_FUNCTION   AVR32_USART1_TXD_0_0_FUNCTION
#  define ARD_USART_SPI_MOSI_PIN        AVR32_USART1_RXD_0_0_PIN
#  define ARD_USART_SPI_MOSI_FUNCTION   AVR32_USART1_RXD_0_0_FUNCTION
#  define ARD_USART_SPI_NSS_PIN         AVR32_USART1_CTS_0_0_PIN
#  define ARD_USART_SPI_NSS_FUNCTION    AVR32_USART1_CTS_0_0_FUNCTION
#  define ARD_USART_SPI_IRQ             AVR32_USART1_IRQ 
#endif
#if BOARD == ARDUINO
#  define ARD_SPI                 (&AVR32_SPI0)
#define EXT_INT_PIN_LINE1               AVR32_EIC_EXTINT_5_PIN
#define EXT_INT_FUNCTION_LINE1          AVR32_EIC_EXTINT_5_FUNCTION
#define EXT_INT_LINE1                   EXT_INT5
#define EXT_INT_IRQ_LINE1               AVR32_EIC_IRQ_5
#define EXT_INT_NB_LINES                1
#endif

/* These defines should be adjusted to match the application */
/*! \brief CPU core speed in Hz */
#define CPUHZ      60000000
/*! \brief Number of bytes in the receive buffer when operating in slave mode */
#define BUFFERSIZE    64
/*! \brief A adjustable delay avoiding multiple requests on the switches */
//#define TIMEOUT 150000
#define TIMEOUT      CPUHZ/200
/*! \brief Number of bits in each SPI package*/
#define SPI_BITS    8
/*! \brief SPI slave speed in Hz */
#define SPI_SLAVE_SPEED    1000000


#ifndef CMD_MAX_LEN
#define CMD_MAX_LEN 1024
#endif
#ifndef REPLY_MAX_LEN
#define REPLY_MAX_LEN 1024
#endif

#define  _BUFFERSIZE 100

extern void tcp_debug_print_pcbs(void);
extern bool ifStatus;
extern bool scanNetCompleted;

static char buf[CMD_MAX_LEN];
static char reply[REPLY_MAX_LEN];
static uint16_t cmdCorr = 0;
static uint16_t count = 0;
static uint16_t replyCount = 0;
static cmd_spi_state_t state = SPI_CMD_IDLE;
int receivedChars = 0;
static uint8_t _receiveBuffer[_BUFFERSIZE];
bool startReply = false;
bool end_write = false;	//TODO only for debug

// Signal indicating a new command is coming from SPI interface
static volatile Bool startRecvCmdSignal = FALSE;

#define MAX_CMD_NUM 36
typedef struct sCmd_spi_list{
	cmd_spi_cb_t cb;
	char cmd_id;
	cmd_spi_rcb_t reply_cb;
	void* ctx;
	char flags;
}tCmd_spi_list;

static tCmd_spi_list cmd_spi_list[MAX_CMD_NUM] = { {0} };

#ifdef _SPI_STATS_
typedef struct sStatSpi
{
	int	timeoutIntErr;
	int	timeoutErr;
	int txErr;
	int	rxErr;
	int wrongFrame;
	int frameDisalign;
	int overrideFrame;
	int lastCmd;
	int lastError;
	unsigned long status;
}tStatSpi;

tStatSpi statSpi = {0};

void initStatSpi()
{
	statSpi.lastCmd = 0;
	statSpi.lastError = 0;
	statSpi.status= 0;
	statSpi.txErr = 0;
	statSpi.rxErr = 0;
	statSpi.timeoutErr= 0;
	statSpi.timeoutIntErr= 0;
	statSpi.wrongFrame = 0;
	statSpi.frameDisalign = 0;
	statSpi.overrideFrame = 0;
}

void printStatSpi()
{
	printk("totSpiCmds\t: 0x%x\n", cmdCorr);
	printk("lastCmd  \t: 0x%x\n", statSpi.lastCmd);
	printk("lastErr  \t: 0x%x\n", statSpi.lastError);
	printk("spiStatus\t: 0x%X\n", statSpi.status);
	printk("spiTxErr \t: 0x%x\n", statSpi.txErr);
	printk("spiRxErr \t: 0x%x\n", statSpi.rxErr);
	printk("spiTmoErr\t: 0x%x\n", statSpi.timeoutErr);
	printk("spiTmoIntErr\t: 0x%x\n", statSpi.timeoutIntErr);
	printk("wrongFrame\t: 0x%x\n", statSpi.wrongFrame);
	printk("disalFrame\t: 0x%x\n", statSpi.frameDisalign);
	printk("overrideFrame\t: 0x%x\n", statSpi.overrideFrame);
}

cmd_state_t
cmd_statSpi(int argc, char* argv[], void* ctx)
{
	printStatSpi();
	return CMD_DONE;
}

cmd_state_t
cmd_resetStatSpi(int argc, char* argv[], void* ctx)
{
	initStatSpi();
	return CMD_DONE;
}
#endif

#define ARRAY_SIZE(a) sizeof(a) / sizeof(a[0])
#define RETURN_ERR(e) return (e==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
#define RESET_USART_CSR(usart) usart->cr = AVR32_USART_CR_RSTSTA_MASK;

int result = WL_CONNECT_FAILED; //Store the result of the last operation

void* mapSockTCP[MAX_SOCK_NUM][MAX_MODE_NUM];

//Udp RemoteIp and remote Port
static tRemoteClient remoteClients[MAX_SOCK_NUM] = {{0,0}};

void setRemoteClient(uint16_t sock, uint32_t _ipaddr, uint16_t _port)
{
	if (sock < MAX_SOCK_NUM)
	{
		remoteClients[sock].ipaddr = _ipaddr;
		remoteClients[sock].port = _port;
	}
}

tRemoteClient* getRemoteClient(uint16_t sock)
{
	if (sock < MAX_SOCK_NUM)
	{
		return &remoteClients[sock];
	}
	return NULL;
}

struct netif* ard_netif = NULL;

// Network list retrived in the last scanNetwork
static struct wl_network_list_t network_list = { 0 };

struct ip_addr _hostIpAddr;

static bool hostIpAddrFound = false;

void* getTTCP(uint8_t sock, uint8_t mode)
{
	if (sock < MAX_SOCK_NUM)
		return mapSockTCP[sock][mode];
	return NULL;
}

int getSock(void * _ttcp)
{
	if (_ttcp != NULL)
	{
		int i = 0;
		for (; i<MAX_SOCK_NUM; i++)
		{
			if (_ttcp == mapSockTCP[i][GET_TCP_MODE(_ttcp)])
				return i;
		}
	}
	return -1;
}

void setMapSockMode(uint8_t sock, void* _ttcp, uint8_t _tcp_mode)
{
	if ((IS_VALID_SOCK(sock))&&(_ttcp!=NULL))
		mapSockTCP[sock][_tcp_mode]=_ttcp;
	INFO_TCP("Map [%d, %p, %s]\n", sock, _ttcp, Mode2Str(_tcp_mode));
}

void setMapSock(uint8_t sock, void* _ttcp)
{
	setMapSockMode(sock, _ttcp, GET_TCP_MODE(_ttcp));
}

void clearMapSockTcp(uint8_t sock, uint8_t mode)
{
	if (sock < MAX_SOCK_NUM)
	{
		//printk("UnMap [%d, %p]\n", sock, mapSockTCP[sock]);
		mapSockTCP[sock][mode] = NULL;
	}

}

void initMapSockTcp()
{
	memset(mapSockTCP, 0, sizeof(mapSockTCP));
}

#if 0
/**
 * Calculate bitrate based on number of bytes transmitted and elapsed time
 */
static void ard_tcp_print_stats(struct ttcp *ttcp) {
	uint32_t ms = timer_get_ms() - ttcp->start_time;
	uint32_t bytes = ttcp->mode == TTCP_MODE_TRANSMIT ? ttcp->nbuf
			* ttcp->buflen : ttcp->recved;

	if (ttcp->verbose)
		printk("\n");

	printk("TTCP [%p]: %d bytes processed, %d.%d KB/s (%s/%s)\n", ttcp, bytes,
			bytes / ms, bytes % ms, ProtMode2Str(ttcp->udp),
					Mode2Str(ttcp->mode));
}
#endif

void showTTCPstatus()
{
	printk("IF   status: %s\n", (ifStatus) ? "UP":"DOWN");
	printk("CONN status: %s\n", (_connected) ? "UP":"DOWN");

	int i = 0;
	for (; i<MAX_SOCK_NUM; i++)
	{
		int ii=0;
		for (; ii<MAX_MODE_NUM; ii++)
		{
			void* p = getTTCP(i, ii);
			if (p)
			{
				ttcp_t* _ttcp = (ttcp_t* )p;
				printk("Socket n.:%d(%d) [0x%x] %s %s addr:%s port:%d\n", i, ii, _ttcp, 
					ProtMode2Str(_ttcp->udp), Mode2Str(_ttcp->mode), ip2str(_ttcp->addr), _ttcp->port);
				if (_ttcp->udp == TCP_MODE)
				{
					int j = 0;
					for (; j<MAX_CLIENT_ACCEPTED; ++j)
					{
						if (_ttcp->tpcb[j]){
							printk("[%d tpcp-%p]-Status:%d\n", j, _ttcp->tpcb[j], _ttcp->tpcb[j]->state);
						}
					}

					if (_ttcp->lpcb){
						printk("[tlcp-%p]-Status:%d\n", _ttcp->lpcb, _ttcp->lpcb->state);
					}
				}else{
					if (_ttcp->upcb){
						struct ip_addr loc = _ttcp->upcb->local_ip;
						printk("[upcp-%p] flags:0x%x  local:%s[0x%x]-%d\n", 
								_ttcp->upcb, _ttcp->upcb->flags,
								ip2str(loc), loc, _ttcp->upcb->local_port);				
						tRemoteClient remote = {0,0};;
						getRemoteData(i, ii, &remote);
						struct ip_addr ipaddr = { remote.ipaddr };
						printk("remote:%s(0x%x)-%d\n", ip2str(ipaddr), remote.ipaddr, remote.port);
						}					
				}
				//ard_tcp_print_stats(_ttcp);
				printk("Data avail:%s\n", isAvailTcpDataByte(i)?"YES":"NO");
				printk("------------------------------\n");
			}
		}			
	}

	tcp_debug_print_pcbs();
}

int write_stream(volatile avr32_spi_t *spi, const char *stream, uint16_t len)
{
	uint16_t _len = 0;
	unsigned short dummy=0;

     	do {
            //SIGN1_DN();
    		if (spi_write(spi, *stream) == SPI_ERROR_TIMEOUT)
    		{
#ifdef _SPI_STATS_
    			statSpi.timeoutErr++;
    			statSpi.txErr++;
    			statSpi.lastError = SPI_ERROR_TIMEOUT;
    			statSpi.status = spi_getStatus(spi);
#endif
    			return SPI_ERROR_TIMEOUT;
    		}
    		else
    		{
    			stream++;
    			_len++;
    			spi_read(spi,&dummy);
    		}
    		//SIGN1_UP();
    }while (_len < len);
	return SPI_OK;
}

void sendError()
{
	AVAIL_FOR_SPI();
	if (spi_write(&AVR32_SPI, ERR_CMD) != SPI_ERROR_TIMEOUT)
	{
		//Wait to empty the buffer
		while(!spi_writeRegisterEmptyCheck(&AVR32_SPI));
	}
	BUSY_FOR_SPI();
	WARN("Send SPI error!\n");
}

#define ENABLE_SPI_INT() do {										\
	volatile avr32_spi_t *spi = ARD_SPI;							\
    Bool global_interrupt_enabled = Is_global_interrupt_enabled();	\
    if (global_interrupt_enabled) Disable_global_interrupt();		\
    spi->IER.rdrf = 1; spi->IER.rxbuff = 1;	spi->IER.endrx = 1;		\
    if (global_interrupt_enabled) Enable_global_interrupt();		\
}while(0);

#define DISABLE_SPI_INT() do {										\
	volatile avr32_spi_t *spi = ARD_SPI;							\
    Bool global_interrupt_enabled = Is_global_interrupt_enabled();	\
    if (global_interrupt_enabled) Disable_global_interrupt();		\
    spi->IDR.rdrf = 1; spi->IDR.rxbuff = 1;	spi->IDR.endrx = 1;										\
    if (global_interrupt_enabled) Enable_global_interrupt();		\
}while(0);

#define CLEAR_SPI_INT() do {	\
		eic_clear_interrupt_line(&AVR32_EIC, AVR32_SPI0_IRQ);	\
	}while(0);

int spi_add_cmd(char _cmd_id, cmd_spi_cb_t cb, cmd_spi_rcb_t rcb, void* ctx,
		char flag) {
	U32 i;
	for (i = 0; i < ARRAY_SIZE(cmd_spi_list); i++)
		if (!cmd_spi_list[i].cb)
			break;

	if (i == ARRAY_SIZE(cmd_spi_list))
	{
		printk("List Commands full!\n");
		return -1;
	}
	cmd_spi_list[i].cmd_id = _cmd_id;
	cmd_spi_list[i].cb = cb;
	cmd_spi_list[i].reply_cb = rcb;
	cmd_spi_list[i].ctx = ctx;
	cmd_spi_list[i].flags = flag;
	return 0;
}

int set_net_cmd_cb(int numParam, char* buf, void* ctx) {
	struct wl_ssid_t ssid;
	wl_err_t err = WL_FAILURE;
	tParam* param = (tParam*) buf;

	if (param->paramLen < WL_SSID_MAX_LENGTH) {
		memcpy(ssid.ssid, &param->param, param->paramLen);
		ssid.len = param->paramLen;
		ssid.ssid[ssid.len] = 0;
		INFO_SPI("SSID:%s\n", ssid.ssid);
		//dump(ssid.ssid, ssid.len);
		err = wl_cm_set_network(&ssid, NULL);
		if (err != 1)
			WARN("err=%d\n", err);
	} else {
		WARN("SSID len out of range");
	}
	return err;
}

extern uint8_t ascii_to_key(char *outp, const char *inp);

int set_key_cmd_cb(int numParam, char* buf, void* ctx) {
	struct wl_ssid_t ssid;
    struct wl_mac_addr_t bssid;
    uint8_t idx=0, len=0;
    char key[13], key_hex[27];
    char keyIdx[2];
	wl_err_t err = WL_SUCCESS;
	tParam* params = (tParam*) buf;

    INFO_SPI("%s params=%d\n", __FUNCTION__, numParam);

    // SSID
    memset(&ssid, 0, sizeof ssid);

	if (params->paramLen < WL_SSID_MAX_LENGTH) {
		memcpy(ssid.ssid, &params->param, params->paramLen);
		ssid.len = params->paramLen;
		INFO_SPI("%s\n", ssid.ssid);
	} else {
		//printk("SSID len out of range");
		RETURN_ERR(WL_FAILURE)
	}

    params = (tParam*)((char*)buf+PARAM_LEN_SIZE+params->paramLen);
    strncpy(keyIdx, (const char*)&params->param, params->paramLen);
    keyIdx[(uint8_t)params->paramLen]='\0';

    idx = (uint8_t)atoi(keyIdx);
    // KEY IDX
    if ((params->paramLen != 1)||(idx < 0)||(idx > 3)){
        //printk("KEY IDX out of range %d\n", idx);
        RETURN_ERR(WL_FAILURE)
    }

    params = (tParam*)((char*)params+PARAM_LEN_SIZE+params->paramLen);
    strncpy(key_hex, (const char*)&params->param, params->paramLen);
    key_hex[(uint8_t)params->paramLen]='\0';
    len = ascii_to_key(key, key_hex);
    // KEY
    if (( len != 5)&&(len != 13))
    {
        //printk("KEY len out of range %d", len);
        RETURN_ERR(WL_FAILURE)
    }
#if 0
    printk("KEY IDX = %d\n", idx);
    dump(key, len);
    printk("KEY len %d\n", len);
#endif
    memset(&bssid.octet, 0xff, sizeof bssid.octet);

    wl_add_wep_key(idx, len, key, &bssid);
    //wl_set_auth_mode(AUTH_MODE_SHARED_KEY);
    wl_set_default_wep_key(idx);

    //Connect
    err = wl_cm_set_network(&ssid, NULL);
    if (err != 1)
        WARN("err=%d\n", err);
    RETURN_ERR(err)
}

int set_passphrase_cmd_cb(int numParam, char* buf, void* ctx) {
    struct wl_network_t net;
    char pass[64];
	wl_err_t err = WL_SUCCESS;
	tParam* params = (tParam*) buf;

    INFO_SPI("%s params=%d\n", __FUNCTION__, numParam);

    memset(&net, 0, sizeof net);
    memset(net.bssid.octet, 0xFF, sizeof net.bssid.octet);

    net.enc_type = ENC_TYPE_AUTO;

    // SSID
	if (params->paramLen < WL_SSID_MAX_LENGTH) {
		memcpy(net.ssid.ssid, &params->param, params->paramLen);
		net.ssid.len = params->paramLen;
		INFO_SPI("%s %d\n", net.ssid.ssid, net.ssid.len);
	} else {
		//printk("SSID len out of range");
		RETURN_ERR(WL_FAILURE)
	}
    params = (tParam*)((char*)buf+PARAM_LEN_SIZE+params->paramLen);
    // PASSPHRASE     
   
    strncpy(pass, (const char*)&params->param, params->paramLen);
    pass[(uint8_t)params->paramLen]='\0';
    INFO_SPI("Pass: %s %d\n", pass, params->paramLen);

    if (wl_set_passphrase(&net, 
                          pass, 
                          params->paramLen, 
                          ENC_TYPE_AUTO,
                          AUTH_MODE_AUTO) 
        != WL_SUCCESS) {
            WARN("%s : Failed to add passphrase\n", __func__);

            RETURN_ERR(WL_FAILURE)
    }
    printk("Connect to network...");
    //Connect
    err = wl_cm_set_network(&net.ssid, NULL);
    if (err != 1)
        printk("err=%d\n", err);
    else
    	printk("OK\n");
    RETURN_ERR(err)
}

int set_ip_config_cmd_cb(int numParam, char* buf, void* ctx) {
    struct ip_addr lwip_addr;
    struct ctx_server *hs = ctx;
    struct net_cfg *ncfg = &(hs->net_cfg);
    struct netif *nif = ncfg->netif;
    uint8_t parmsToChange=0;
    const uint8_t MAX_IP_CONFIG_PARAMS = 3;

	wl_err_t err = WL_SUCCESS;
	tParam* params = (tParam*) buf;

    if (params->paramLen == 1)
	{
		GET_PARAM_NEXT(BYTE, params, _parmsToChange);
		parmsToChange = _parmsToChange;
	}		
    else
    	RETURN_ERR(WL_FAILURE)

    INFO_SPI("%p numParam=%d parmsToChange=%d\n", ctx, numParam, parmsToChange);

    if (parmsToChange <= MAX_IP_CONFIG_PARAMS)
    {
		int i=0;
    	for (; i<parmsToChange; ++i)
    	{
    	    if (params->paramLen == 4)
    	    {
				GET_PARAM_NEXT(LONG, params, _ip_addr);
				lwip_addr.addr = _ip_addr;
				INFO_SPI("%d] nif:%p lwip_addr=0x%x\n", i, nif, lwip_addr.addr);
    	    	switch (i)
    	    	{
    	    		case 0:	// local_ip
					{ 	    			
    	    			netif_set_ipaddr(nif, &lwip_addr);
    	    			break;
					}								
    	    		case 1:	// gateway
					{
    	    			netif_set_gw(nif, &lwip_addr);
    	    			break;
					}					
    	    		case 2:	// subnet
					{
    	    			netif_set_netmask(nif, &lwip_addr);
    	    			break;
					}					
    	    	}
    	    }else{
    	    	RETURN_ERR(WL_FAILURE)
    	    }

    	}
    	/* Disable DHCP */
    	ncfg->dhcp_enabled = STATIC_IP_CONFIG;
    }else
    	RETURN_ERR(WL_FAILURE)
		
    RETURN_ERR(err)
}

int set_dns_config_cmd_cb(int numParam, char* buf, void* ctx) {
    struct ip_addr lwip_addr;
    struct ctx_server *hs = ctx;
    struct net_cfg *ncfg = &(hs->net_cfg);
    struct netif *nif = ncfg->netif;
    uint8_t parmsToChange=0;
    const uint8_t MAX_DNS_CONFIG_PARAMS = 2;

	wl_err_t err = WL_SUCCESS;
	tParam* params = (tParam*) buf;

    if (params->paramLen == 1)
	{
		GET_PARAM_NEXT(BYTE, params, _parmsToChange);
		parmsToChange = _parmsToChange;
	}		
    else
    	RETURN_ERR(WL_FAILURE)

    INFO_SPI("%p numParam=%d parmsToChange=%d\n", ctx, numParam, parmsToChange);

    if (parmsToChange <= MAX_DNS_CONFIG_PARAMS)
    {
		int i=0;
    	for (; i<parmsToChange; ++i)
    	{
    	    if (params->paramLen == 4)
    	    {
				GET_PARAM_NEXT(LONG, params, _ip_addr);
				lwip_addr.addr = _ip_addr;
				INFO_SPI("%d] nif:%p lwip_addr=0x%x\n", i, nif, lwip_addr.addr);
				dns_setserver(i, &lwip_addr);
    	    }else{
    	    	RETURN_ERR(WL_FAILURE)
    	    }
    	}
    	/* Disable DHCP */
    	ncfg->dhcp_enabled = STATIC_IP_CONFIG;
    }else
    	RETURN_ERR(WL_FAILURE)
		
    RETURN_ERR(err)
}



void set_result(wl_status_t _status)
{
	result = _status;
}


void set_result_cmd(int err) 
{
    wl_err_t _err = (wl_err_t)err;
    switch (_err)
    {
    case WL_SUCCESS:
    	set_result(WL_CONNECTED);
    	ERROR_LED_OFF();
        break;
    default:
    case WL_OOM:
    case WL_INVALID_LENGTH:
    case WL_NOT_SUPPORTED:
    case WL_ABSORBED:
    case WL_RESOURCES:
    case WL_BUSY:
    case WL_RETRY:
    case WL_FAILURE:
    	set_result(WL_CONNECT_FAILED);
    	ERROR_LED_ON();
        break;
    }
    INFO_SPI("%s %d\n", __FUNCTION__, result);
}



extern int ttcp_start(struct ip_addr addr, uint16_t port, void *opaque,
           void *done_cb, int mode, uint16_t nbuf, uint16_t buflen, int udp, int verbose);


int start_server_tcp(uint16_t port, uint8_t sock, uint8_t protMode)
{
	struct ip_addr addr = { 0 };
    uint16_t buflen = 1024;
    uint16_t nbuf = 1024;
    wl_err_t err = WL_FAILURE;

#ifdef _APP_DEBUG_
    int verbose = 1;
#else
    int verbose = 0;
#endif
    int udp = protMode;
    int mode = 1;   //RECEIVE
    void* _ttcp = NULL;

    if (sock >= MAX_SOCK_NUM)
    	return WIFI_SPI_ERR;

    if (_connected)
    {
    	WARN("Still connected...wait\n");
    	return WIFI_SPI_ERR;
    }

    if (!ifStatus)
     {
    	WARN_VER("IF down...wait\n");
     	return WIFI_SPI_ERR;
     }


    if (ard_tcp_start(addr, port, NULL, NULL, mode, nbuf, buflen, udp, verbose, sock, &_ttcp) == 0)
    {
    	INFO_SPI("Start Server %s [%d, %d] OK!\n", ProtMode2Str(protMode), port, sock);
    	setMapSock(sock, _ttcp);
        err = WL_SUCCESS;
    }else{

    	WARN("Start Server %s [%d, %d] FAILED!\n", ProtMode2Str(protMode), port, sock);
    	clearMapSockTcp(sock, TTCP_MODE_RECEIVE);
    }
    return err;
}


int start_server_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
	wl_err_t err = WL_FAILURE;
	tParam* params = (tParam*) buf;
    if (numParam == 3)
    {
    	GET_PARAM_NEXT(INT, params, port);
    	GET_PARAM_NEXT(BYTE, params, sock);
    	GET_PARAM_NEXT(BYTE, params, protMode);
    	err = start_server_tcp(port, sock, protMode);
    }
    return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
}

int start_client_tcp(uint32_t _addr, uint16_t port, uint8_t sock, uint8_t protMode)
{
    uint16_t buflen = 1024;
    uint16_t nbuf = 1024;
    wl_err_t err = WL_FAILURE;
    struct ip_addr addr = { .addr = _addr};

    INFO_SPI("Addr:0x%x, port:%d, sock:%d, prot:%s\n", _addr, port, sock,  ProtMode2Str(protMode));

	#ifdef _APP_DEBUG_
    int verbose = 1;
	#else
    int verbose = 0;
	#endif

    int udp = protMode;
    int mode = 0;   //TRANSMIT
    void* _ttcp = NULL;

        if (sock >= MAX_SOCK_NUM)
        	return WIFI_SPI_ERR;

    // Check previous connection
	_ttcp = getTTCP(sock, TTCP_MODE_TRANSMIT);
	if (_ttcp != NULL)
	{
		WARN("Previous client %p not stopped !\n", _ttcp);
		ard_tcp_stop(_ttcp);
		clearMapSockTcp(sock, TTCP_MODE_TRANSMIT);
	}

	if (ard_tcp_start(addr, port, NULL, NULL, mode, nbuf, buflen, udp, verbose, sock, &_ttcp) == 0)
	{
		INFO_SPI("Start Client %s %p [0x%x, %d, %d] OK!\n", ProtMode2Str(protMode),
				_ttcp, addr, port, sock);
		setMapSock(sock, _ttcp);
		err = WL_SUCCESS;
	}else{
		INFO_SPI("Start Client %s %p [0x%x, %d, %d] FAILED!\n", ProtMode2Str(protMode), 
				_ttcp, addr, port, sock);
		clearMapSockTcp(sock, TTCP_MODE_TRANSMIT);
	}
	return err;
}


int start_client_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
	wl_err_t err = WL_FAILURE;
	tParam* params = (tParam*) buf;
    if (numParam == 4)
    {
    	GET_PARAM_NEXT(LONG, params, _addr);
    	GET_PARAM_NEXT(INT, params, port);
     	GET_PARAM_NEXT(BYTE, params, sock);
     	GET_PARAM_NEXT(BYTE, params, protMode);
     	err = start_client_tcp(_addr, port, sock, protMode);
    }
    return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
}

int stop_client_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
	wl_err_t err = WL_FAILURE;
	tParam* params = (tParam*) buf;
	void* _ttcp = NULL;

    if (numParam == 1)
    {
     	GET_PARAM_NEXT(BYTE, params, sock);

        INFO_SPI("Stop client sock:%d\n", sock);

        if (sock < MAX_SOCK_NUM)
        {
        	_ttcp = getTTCP(sock, TTCP_MODE_TRANSMIT);
        	ard_tcp_stop(_ttcp);
            err = WL_SUCCESS;
        }
    }
    return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
}

int insert_data_cmd_cb(int numParam, char* buf, void* ctx) {

	tDataParam* msg = (tDataParam*) buf;
    if ((numParam == 2)&&(msg->dataLen == 1))
    {
        GET_DATA_BYTE(sock, buf+2);
        GET_DATA_INT(len, buf+3);
        //printk("tcp:%p buf:%p len:%d\n", getTTCP(sock), (uint8_t*)(buf+5), len);
        insertBuf(sock, (uint8_t*)(buf+5), len);
    }
    return WIFI_SPI_ACK;
}

int send_data_udp_cmd_cb(int numParam, char* buf, void* ctx) {
	wl_err_t err = WL_FAILURE;

	tParam* params = (tParam*) buf;
    if ((numParam == 1)&&(params->paramLen == 1))
    {
    	GET_PARAM_NEXT(BYTE, params, sock);
        uint16_t len = 0;
        uint8_t* p = mergeBuf(sock, NULL, &len);
        err = sendUdpData(getTTCP(sock, TTCP_MODE_TRANSMIT), p, len);
		clearBuf(sock);
        free(p);
    }

    return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
}


int send_data_tcp_cmd_cb(int numParam, char* buf, void* ctx) {
	wl_err_t err = WL_FAILURE;
	DATA_LED_ON();
	tDataParam* msg = (tDataParam*) buf;
    if ((numParam == 2)&&(msg->dataLen == 1))
    {
        GET_DATA_BYTE(sock, buf+2);
        GET_DATA_INT(len, buf+3);
        //printk("tcp:%p buf:%p len:%d\n", getTTCP(sock), (uint8_t*)(buf+5), len);
        err = sendTcpData(getTTCP(sock, TTCP_MODE_TRANSMIT), (uint8_t*)(buf+5), len);
    }
    DATA_LED_OFF();
    return (err==WL_SUCCESS) ? WIFI_SPI_ACK : WIFI_SPI_ERR;
}

int ack_cmd_cb(int numParam, char* buf, void* ctx) {
	return WIFI_SPI_ACK;
}

int get_result_cmd_cb(int numParam, char* buf, void* ctx) {
	INFO_SPI("ifStatus:%d result:%d\n", ifStatus, result);
	return WIFI_SPI_ACK;
}

int disconnect_cmd_cb(int numParam, char* buf, void* ctx) 
{
	return ((wl_disconnect()==WL_SUCCESS)? WIFI_SPI_ACK : WIFI_SPI_ERR);
}


cmd_spi_state_t get_reply_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

    CREATE_HEADER_REPLY(reply, recv, 1);

    reply[3] = 1; // paramLen
	if (ctx != NULL) {
		reply[4] = (*(uint8_t*)ctx); //param
	} else {
		reply[4] = (ifStatus)?WL_CONNECTED:result; //param
	}

    END_HEADER_REPLY(reply, 5, *count);

    //INFO_SPI("result:%d\n", result);
	return SPI_CMD_DONE;
}

cmd_spi_state_t ack_reply_cb(char* recv, char* reply, void* ctx, uint16_t* count) {
     
    CREATE_HEADER_REPLY(reply, recv, 1);

    reply[3] = 1; // paramLen
	if (ctx != NULL) {
		reply[4] = (*(uint8_t*) ctx != 1) ? WIFI_SPI_ERR : WIFI_SPI_ACK; //param
	} else {
		reply[4] = WIFI_SPI_ACK; //param
	}

    END_HEADER_REPLY(reply, 5, *count);

    return SPI_CMD_DONE;
}

cmd_spi_state_t get_reply_ipaddr_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

    CHECK_ARD_NETIF(recv, reply, count);

    CREATE_HEADER_REPLY(reply, recv, 3);

    PUT_LONG_IN_BYTE_NO(ard_netif->ip_addr.addr, reply, 3);
    PUT_LONG_IN_BYTE_NO(ard_netif->netmask.addr, reply, 8);
    PUT_LONG_IN_BYTE_NO(ard_netif->gw.addr, reply, 13);

    END_HEADER_REPLY(reply, 18, *count);

    return SPI_CMD_DONE;
}

void getRemoteData(uint8_t sock, uint8_t mode, tRemoteClient* remoteData)
{
	if ((sock>=0) && (sock<MAX_SOCK_NUM))
	{
		void* p = getTTCP(sock, mode);
		if (p)
		{
			ttcp_t* _ttcp = (ttcp_t* )p;
			if ((_ttcp->udp == UDP_MODE))
			{
				if (_ttcp->mode == TTCP_MODE_RECEIVE)
				{
					remoteData->ipaddr = getRemoteClient(sock)->ipaddr;
					remoteData->port = getRemoteClient(sock)->port;
				}else{
					remoteData->ipaddr = (_ttcp->upcb) ? _ttcp->upcb->remote_ip.addr : 0;
					remoteData->port = (_ttcp->upcb) ? _ttcp->upcb->remote_port : 0;
				}
			}
		}
	}
}


cmd_spi_state_t get_reply_remote_data_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

    CHECK_ARD_NETIF(recv, reply, count);
    DUMP_SPI_CMD(recv);

    GET_DATA_BYTE(sock, recv+4);

    CREATE_HEADER_REPLY(reply, recv, 2);
	tRemoteClient remoteData = {0,0};
	//TODO pass the mode
	getRemoteData(sock, TTCP_MODE_RECEIVE, &remoteData);

    PUT_LONG_IN_BYTE_NO(remoteData.ipaddr, reply, 3);
    PUT_DATA_INT(remoteData.port, reply, 8);

    END_HEADER_REPLY(reply, 11, *count);

    return SPI_CMD_DONE;
}


void foundHostByName(const char *name, struct ip_addr *ipaddr, void *callback_arg)
{
	_hostIpAddr.addr = (ipaddr)?ipaddr->addr:0xffffffff;
	INFO_SPI("foundHostByName: Found Host: name=%s ip=0x%x\n", name, _hostIpAddr.addr);
	hostIpAddrFound = true;
}

int req_reply_host_by_name_cb(int numParam, char* buf, void* ctx) {

    char hostName[DNS_MAX_NAME_LENGTH];
	tParam* params = (tParam*) buf;

    // HostName
	if (params->paramLen < DNS_MAX_NAME_LENGTH) {
		memcpy(hostName, &params->param, params->paramLen);
		hostName[params->paramLen]='\0';
	} else {
		RETURN_ERR(WL_FAILURE)
	}

	INFO_SPI("Looking for Host: name=%s\n", hostName);
	_hostIpAddr.addr = 0;
	hostIpAddrFound = false;
    err_t err = dns_gethostbyname(hostName, &_hostIpAddr, foundHostByName, NULL);
    if (err == ERR_OK)
    {
    	INFO_SPI("Found Host: name=%s ip=0x%x\n", hostName, _hostIpAddr.addr);
    	hostIpAddrFound = true;
		RETURN_ERR(WL_SUCCESS)
    }
	RETURN_ERR(WL_FAILURE)
}

cmd_spi_state_t get_reply_host_by_name_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

	u32_t addr = (hostIpAddrFound)?_hostIpAddr.addr : 0xffffffff;
	INFO_SPI("Searching for Host: ip=0x%x found=%d\n", addr, hostIpAddrFound);

    CHECK_ARD_NETIF(recv, reply, count);

    CREATE_HEADER_REPLY(reply, recv, 1);

    PUT_LONG_IN_BYTE_NO(addr, reply, 3);

    END_HEADER_REPLY(reply, 8, *count);

    return SPI_CMD_DONE;
}

cmd_spi_state_t get_reply_mac_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

    CHECK_ARD_NETIF(recv, reply, count);

    CREATE_HEADER_REPLY(reply, recv, 1);

    reply[3] = WL_MAC_ADDR_LENGTH;
    uint8_t mac[WL_MAC_ADDR_LENGTH];
    if (wl_get_mac_addr(mac) != WL_SUCCESS) {
            RETURN_ERR_REPLY(recv, reply, count);
    }
    //rotate the byte order
    reply[4]=mac[5];
    reply[5]=mac[4];
    reply[6]=mac[3];
    reply[7]=mac[2];
    reply[8]=mac[1];
    reply[9]=mac[0];
    END_HEADER_REPLY(reply, 10, *count);

    return SPI_CMD_DONE;
}

cmd_spi_state_t get_reply_curr_net_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

	uint32_t type = (uint32_t)ctx;
    CHECK_ARD_NETIF(recv, reply, count);

    CREATE_HEADER_REPLY(reply, recv, 1);

    struct wl_network_t* net = wl_get_current_network();
    uint8_t len = 0;
    if (net != NULL)
    {
    	switch (type)
    	{
    	default:
    	case GET_CURR_SSID_CMD:
			{
			len = net->ssid.len;
			PUT_BUFDATA_BYTE(net->ssid.ssid, len, reply, 3);
			break;
			}
    	case GET_CURR_BSSID_CMD:
			{
			len = WL_MAC_ADDR_LENGTH; ;
			PUT_BUFDATA_BYTE_REV(net->bssid.octet, len, reply, 3);
			break;
			}
    	case GET_CURR_RSSI_CMD:
			{
				len=sizeof(net->rssi);
			PUT_LONG_IN_BYTE_HO(net->rssi, reply, 3);
			//printk("RSSI:%d", net->rssi);
			break;
			}
    	case GET_CURR_ENCT_CMD:
			{
				len = sizeof(net->enc_type);
				PUT_DATA_BYTE(net->enc_type, reply, 3);
				//printk("ENCT:%d", net->enc_type);
				break;
			}
    	}
    }else{
    	PUT_DATA_BYTE(0, reply, 3);
    }

    END_HEADER_REPLY(reply, 3+len+1, *count);

    //dump(reply, *count);

    return SPI_CMD_DONE;
}

cmd_spi_state_t get_reply_idx_net_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

	uint32_t type = (uint32_t)ctx;
    CHECK_ARD_NETIF(recv, reply, count);

    CREATE_HEADER_REPLY(reply, recv, 1);

    DUMP_SPI_CMD(recv);

    GET_DATA_BYTE(idx, recv+4);

    if (idx >= WL_NETWORKS_LIST_MAXNUM)
    {
    	WARN("Index out of range: %d\n", idx);
    	return SPI_CMD_DONE;
    }
    uint8_t len = 0;
    switch (type)
    {
    	default:
    	case GET_IDX_SSID_CMD:
			{
				len = network_list.net[idx]->ssid.len;
				PUT_BUFDATA_BYTE(network_list.net[idx]->ssid.ssid, len, reply, 3);
				INFO_UTIL("SSID:%s\n", network_list.net[idx]->ssid.ssid);
				break;
			}
    	case GET_IDX_RSSI_CMD:
			{
				len = 4;
				PUT_LONG_IN_BYTE_HO(network_list.net[idx]->rssi, reply, 3);
				INFO_UTIL("RSSI:%d\n", network_list.net[idx]->rssi);
				break;
			}
    	case GET_IDX_ENCT_CMD:
			{
				len = 1;
				PUT_DATA_BYTE(network_list.net[idx]->enc_type, reply, 3);
				INFO_UTIL("ENCT:%d\n", network_list.net[idx]->enc_type);
				break;
			}
   	}


    END_HEADER_REPLY(reply, 3+len+1, *count);

    DUMP(reply, *count);

    return SPI_CMD_DONE;
}

static void copy_network_list(struct wl_network_list_t *dst,
                              struct wl_network_list_t *src)
{
        int i;
        for (i = 0; i < dst->cnt; i++)
                free(dst->net[i]);
        free(dst->net);

        dst->cnt = 0;

        if (src->cnt == 0)
                return;
        dst->net = calloc(1, src->cnt * sizeof(struct wl_network_t *));
        if (dst->net == NULL) {
                printk("could not allocate all gui net array\n");
                return;
        }

        for (i = 0; i < src->cnt; i++) {
                struct wl_network_t *net = src->net[i];
                dst->net[i] = malloc(sizeof(*net));
                if (dst->net[i] == NULL) {
                        printk("could not allocate all gui nets\n");
                        return;
                }

                memcpy(dst->net[i], net, sizeof(*net));
                dst->cnt++;
        }
}

int start_scan_net_cmd_cb(int numParam, char* buf, void* ctx) {
	wl_err_t err = WL_FAILURE;

	INFO_SPI("Start Network Scan %d\n", numParam);
	if (scanNetCompleted){
		scanNetCompleted = false;
		err = wl_scan();
		if (err != WL_SUCCESS)
		{
			// May be busy scanning already, no fatal error
			WARN("err=%d\n", err);
			err = WL_SUCCESS;
		}
	}
	return err;
}

cmd_spi_state_t get_reply_scan_networks_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

	const int8_t SCAN_NOT_YET_COMPLETED = 0;

	if (!scanNetCompleted)
	{
		//return empty list with an error to retry
	     CREATE_HEADER_REPLY(reply, recv, SCAN_NOT_YET_COMPLETED);
	     END_HEADER_REPLY(reply, 3, *count);
	     INFO_SPI("Scan not completed!\n");
	     return SPI_CMD_DONE;
	}

    int network_cnt = 0;
    struct wl_network_list_t* wl_network_list;

    wl_get_network_list(&wl_network_list);
     if (wl_network_list->cnt == 0)
     {
    	 CREATE_HEADER_REPLY(reply, recv, 0);
    	 END_HEADER_REPLY(reply, 3, *count);
    	 INFO_SPI("Networks not found!\n");
    	 return SPI_CMD_DONE;
     }

     if (wl_network_list->cnt > WL_NETWORKS_LIST_MAXNUM)
     {
    	 network_cnt = WL_NETWORKS_LIST_MAXNUM ;
     }
     else{
    	 network_cnt = wl_network_list->cnt ;
     }

     copy_network_list(&network_list, wl_network_list);
     CREATE_HEADER_REPLY(reply, recv, network_cnt);

     uint8_t start = 3;
     int ii = 0;
     for (; ii < network_cnt; ii++)
     {
    	 uint8_t len = network_list.net[ii]->ssid.len+1;
    	 network_list.net[ii]->ssid.ssid[network_list.net[ii]->ssid.len]=0;
    	 PUT_BUFDATA_BYTE(network_list.net[ii]->ssid.ssid, len, reply, start);
    	 start += len+1;
    	 INFO_SPI("%d - %s [%d]- %d - %d - 0x%x\n",ii, network_list.net[ii]->ssid.ssid,
    			 len, network_list.net[ii]->enc_type,
    			 network_list.net[ii]->rssi, network_list.net[ii]->bssid);
     }

     END_HEADER_REPLY(reply, start, *count);
     //DUMP(reply, *count);

    return SPI_CMD_DONE;
}

cmd_spi_state_t get_state_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

    CHECK_ARD_NETIF(recv, reply, count);

    CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);

    uint8_t _state = CLOSED;
    if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
    {
    	_state = getStateTcp(getTTCP((uint8_t)recv[4], TTCP_MODE_RECEIVE), 0);
    }
    PUT_DATA_BYTE(_state, reply, 3);
    END_HEADER_REPLY(reply, 5, *count);
    INFO_SPI_POLL("state:%d\n", _state);

    return SPI_CMD_DONE;
}

cmd_spi_state_t get_client_state_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

    CHECK_ARD_NETIF(recv, reply, count);

    CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);

    uint8_t _state = CLOSED;
    uint8_t _sock = recv[4];
    if ((recv[3]==1)&&(_sock>=0)&&(_sock<MAX_SOCK_NUM))
    {
    	void * p= getTTCP(_sock, TTCP_MODE_TRANSMIT);
    	if (p!=NULL)
    	{
			_state = getStateTcp(p, 1);	
    	}else{
    		WARN_VER("TTCP not found for sock:%d\n", _sock);
    	}
    }
    PUT_DATA_BYTE(_state, reply, 3);
    END_HEADER_REPLY(reply, 5, *count);
    INFO_SPI_POLL("sock:%d state:%d\n", _sock, _state);

    return SPI_CMD_DONE;
}

cmd_spi_state_t avail_data_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

	CHECK_ARD_NETIF(recv, reply, count);

	CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
	uint16_t dataAvail = 0;
    if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
    {
    	dataAvail = getAvailTcpDataByte((uint8_t)recv[4]);
    }
	PUT_DATA_INT_NO(dataAvail, reply, 3);
	END_HEADER_REPLY(reply, 6, *count);

	INFO_SPI_POLL("dataAvail:%d\n", dataAvail);

    return SPI_CMD_DONE;
}

cmd_spi_state_t test_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

	static int counter = 0;
	CHECK_ARD_NETIF(recv, reply, count);

	CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
	PUT_DATA_BYTE(++counter, reply, 3);
	END_HEADER_REPLY(reply, 5, *count);
    return SPI_CMD_DONE;
}

cmd_spi_state_t data_sent_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

	CHECK_ARD_NETIF(recv, reply, count);
	SIGN2_DN();
	CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
	uint8_t dataSent = 0;
    if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
    {
    	dataSent = isDataSent(getTTCP((uint8_t)recv[4], TTCP_MODE_TRANSMIT));
    }
	PUT_DATA_BYTE(dataSent, reply, 3);
	END_HEADER_REPLY(reply, 5, *count);
	SIGN2_UP();
    return SPI_CMD_DONE;
}

cmd_spi_state_t get_data_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

	uint8_t data;

    CHECK_ARD_NETIF(recv, reply, count);

    tParam* params = (tParam*)&recv[3];

    GET_PARAM_NEXT(BYTE, params, _sock);
    GET_PARAM_NEXT(INT, params, _peek);

    if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<MAX_SOCK_NUM))
    {
    	SIGN2_DN();

    	if (getTcpDataByte((uint8_t)recv[4], &data, _peek))
    	{
    		CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
    		PUT_DATA_BYTE(data, reply, 3);
    		END_HEADER_REPLY(reply, 5, *count);
    	}else{
    		CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_0);
    		END_HEADER_REPLY(reply, 3, *count);
    	}
    	SIGN2_UP();
    }
    return SPI_CMD_DONE;
}

cmd_spi_state_t get_databuf_tcp_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

	uint8_t* data;
	uint16_t len;

    CHECK_ARD_NETIF(recv, reply, count);

    GET_DATA_BYTE(sock, buf+5);
    if ((sock>=0)&&(sock<MAX_SOCK_NUM))
    {
    	if (getTcpData((uint8_t)sock, (void**)&data, &len))
    	{
    		CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_1);
    		PUT_BUFDATA_INT(data, len, reply, 3);
    		END_HEADER_REPLY(reply, 3+len+2, *count);
    		freeTcpData((uint8_t)sock);
    	}else{
    		CREATE_HEADER_REPLY(reply, recv, PARAM_NUMS_0);
    		END_HEADER_REPLY(reply, 3, *count);
    	}
    }
    return SPI_CMD_DONE;
}

cmd_spi_state_t get_firmware_version_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

    CHECK_ARD_NETIF(recv, reply, count);

    CREATE_HEADER_REPLY(reply, recv, 1);

    uint8_t len = strlen(fwVersion);

    PUT_BUFDATA_BYTE(fwVersion, len, reply, 3);

    END_HEADER_REPLY(reply, 3+len+1, *count);

    return SPI_CMD_DONE;
}

cmd_spi_state_t get_test_cmd_cb(char* recv, char* reply, void* ctx, uint16_t* count) {

	uint8_t buffer[255] = {0};

    CHECK_ARD_NETIF(recv, reply, count);

    CREATE_HEADER_REPLY(reply, recv, 1);
    uint8_t len = 0;
    if ((recv[3]==1)&&(recv[4]>=0)&&(recv[4]<0xFF))
    {
    	len = recv[4];
    	int i= 0;
    	for (; i<len; ++i) buffer[i]=i;
    	PUT_BUFDATA_BYTE(buffer, len, reply, 3);
    }else{
    	len = strlen(fwVersion);
    	PUT_BUFDATA_BYTE(fwVersion, len, reply, 3);
    }
    END_HEADER_REPLY(reply, 3+len+1, *count);

    return SPI_CMD_DONE;
}

int sendReply(int cmdIdx, char* recv, char* reply, void* resultCmd)
{
	uint16_t _count = 0;
    int _result = SPI_OK;

    cmd_spi_list[cmdIdx].reply_cb(recv, reply, resultCmd, &_count);
    state = SPI_CMD_REPLING;

    AVAIL_FOR_SPI();
    _result = write_stream(ARD_SPI, &reply[0], _count);
#ifdef _SPI_STATS_
    if ( _result != SPI_OK)
    {
    	statSpi.lastCmd = cmd_spi_list[cmdIdx].cmd_id;
    }
#endif
    BUSY_FOR_SPI();

    IF_SPI_DUMP(printk("==>"));
    DUMP_SPI(recv, count);
    IF_SPI_DUMP(printk("<=="));
	DUMP_SPI(reply, _count);
    replyCount = _count;
    return _result;
}

unsigned char* getStartCmdSeq(unsigned char* _recv, int len, int *offset)
{
	int i = 0;
	*offset = 0;
	//DEB_PIN_UP();
	for (; i<len; ++i)
	{
		if (_recv[i]==START_CMD)
		{
			if (i!=0)
			{
				DEB_PIN_TRIGGER();
				IF_WARN_VER(dump((char*)_recv, (uint16_t)len));
				WARN("%d] Disall. %d/%d cmd:%d\n", cmdCorr, i, len,_recv[i+1]);
			}
			*offset = i;
			return &_recv[i];
		}
	}
	//DEB_PIN_DN();
	WARN("%d] Disall. %d\n", cmdCorr, i);

	return NULL;
}

inline bool spiMsg8(uint8_t cmd)
{
	return ((cmd & DATA_FLAG)==0);
}

int call_reply_cb(char* recv, char* reply) {

//	// check the start of message
//	//TODO CHECK if also the ,en must be resize
//	//char* recv = (char*)getStartCmdSeq((unsigned char*)_recv, &count);
//	char* recv = (char*)getStartCmdSeq((unsigned char*)_recv, count);
//	if (recv == NULL)
//		return REPLY_ERR_MSG;

	unsigned char cmdId = (unsigned char) recv[1];
    uint8_t _result = REPLY_NO_ERR;
	U32 i;
	for (i = 0; i < ARRAY_SIZE(cmd_spi_list); i++) {
		if (cmd_spi_list[i].cmd_id == cmdId) {

			if (cmd_spi_list[i].flags == CMD_SET_FLAG) {
				//Send Reply for SET commands
                 if (sendReply(i, recv, reply, cmd_spi_list[i].ctx) != SPI_OK)
                     return REPLY_ERR_SET;
                 if (spiMsg8(cmdId))
                 {
                	 tSpiMsg* spiMsg = (tSpiMsg*) recv;
                	 _result = cmd_spi_list[i].cb(spiMsg->nParam,
     					(char*) &(spiMsg->params[0]), cmd_spi_list[i].ctx);
           		 }else
           		 {
           			tSpiMsgData* spiMsg = (tSpiMsgData*) recv;
                   	 _result = cmd_spi_list[i].cb(spiMsg->nParam,
         					(char*) &(spiMsg->params[0]), cmd_spi_list[i].ctx);
           		 }

                 if (_result != WIFI_SPI_ACK)
                 	return REPLY_ERR_CMD;
                 else
                 	return REPLY_NO_ERR;
			}else{
				if (spiMsg8(cmdId))
				{
					tSpiMsg* spiMsg = (tSpiMsg*) recv;
					_result = cmd_spi_list[i].cb(spiMsg->nParam,
							(char*) &(spiMsg->params[0]), NULL);
				}else{
					tSpiMsgData* spiMsg = (tSpiMsgData*) recv;
					_result = cmd_spi_list[i].cb(spiMsg->nParam,
							(char*) &(spiMsg->params[0]), NULL);
				}
				//Send Reply for GET commands or Immediate SET apply
				if (cmd_spi_list[i].flags == CMD_GET_FLAG) {
					if (sendReply(i, recv, reply, cmd_spi_list[i].ctx)  != SPI_OK)
						return REPLY_ERR_GET;
					else
						return REPLY_NO_ERR;
				}else if (cmd_spi_list[i].flags == CMD_IMM_SET_FLAG)
				{
					if (sendReply(i, recv, reply, &_result)  != SPI_OK)
						return REPLY_ERR_GET;
					else
						return REPLY_NO_ERR;

				}
			}
		}
	}
	// Command not found
	if (i==ARRAY_SIZE(cmd_spi_list))
	{
		WARN("Unknown cmd 0x%x\n", cmdId);
		DUMP(recv, count);
		return REPLY_ERR_CMD;
	}
	return REPLY_NO_ERR;
}

void init_spi_cmds(void* ctx) {
	spi_add_cmd(SET_NET_CMD, set_net_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
	spi_add_cmd(SET_PASSPHRASE_CMD, set_passphrase_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
	spi_add_cmd(SET_KEY_CMD, set_key_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
	spi_add_cmd(SET_IP_CONFIG_CMD, set_ip_config_cmd_cb, ack_reply_cb, ctx, CMD_SET_FLAG);
	spi_add_cmd(SET_DNS_CONFIG_CMD, set_dns_config_cmd_cb, ack_reply_cb, ctx, CMD_SET_FLAG);
	spi_add_cmd(GET_CONN_STATUS_CMD, get_result_cmd_cb, get_reply_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(GET_IPADDR_CMD, ack_cmd_cb, get_reply_ipaddr_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(GET_MACADDR_CMD, ack_cmd_cb, get_reply_mac_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(GET_CURR_SSID_CMD, ack_cmd_cb, get_reply_curr_net_cb, (void*)GET_CURR_SSID_CMD, CMD_GET_FLAG);
	spi_add_cmd(GET_CURR_BSSID_CMD, ack_cmd_cb, get_reply_curr_net_cb, (void*)GET_CURR_BSSID_CMD, CMD_GET_FLAG);
	spi_add_cmd(GET_CURR_RSSI_CMD, ack_cmd_cb, get_reply_curr_net_cb, (void*)GET_CURR_RSSI_CMD, CMD_GET_FLAG);
	spi_add_cmd(GET_CURR_ENCT_CMD, ack_cmd_cb, get_reply_curr_net_cb, (void*)GET_CURR_ENCT_CMD, CMD_GET_FLAG);
	spi_add_cmd(START_SCAN_NETWORKS, start_scan_net_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
	spi_add_cmd(SCAN_NETWORKS, ack_cmd_cb, get_reply_scan_networks_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(DISCONNECT_CMD, disconnect_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
	spi_add_cmd(GET_IDX_ENCT_CMD, ack_cmd_cb, get_reply_idx_net_cb, (void*)GET_IDX_ENCT_CMD, CMD_GET_FLAG);
	spi_add_cmd(GET_IDX_SSID_CMD, ack_cmd_cb, get_reply_idx_net_cb, (void*)GET_IDX_SSID_CMD, CMD_GET_FLAG);
	spi_add_cmd(GET_IDX_RSSI_CMD, ack_cmd_cb, get_reply_idx_net_cb, (void*)GET_IDX_RSSI_CMD, CMD_GET_FLAG);
	spi_add_cmd(REQ_HOST_BY_NAME_CMD, req_reply_host_by_name_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
	spi_add_cmd(GET_HOST_BY_NAME_CMD, ack_cmd_cb, get_reply_host_by_name_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(START_SERVER_TCP_CMD, start_server_tcp_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
	spi_add_cmd(START_CLIENT_TCP_CMD, start_client_tcp_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
	spi_add_cmd(STOP_CLIENT_TCP_CMD, stop_client_tcp_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
	spi_add_cmd(GET_STATE_TCP_CMD, ack_cmd_cb, get_state_tcp_cmd_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(GET_DATA_TCP_CMD, ack_cmd_cb, get_data_tcp_cmd_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(AVAIL_DATA_TCP_CMD, ack_cmd_cb, avail_data_tcp_cmd_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(SEND_DATA_TCP_CMD, send_data_tcp_cmd_cb, ack_reply_cb, NULL, CMD_IMM_SET_FLAG);
	spi_add_cmd(DATA_SENT_TCP_CMD, ack_cmd_cb, data_sent_tcp_cmd_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(GET_DATABUF_TCP_CMD, ack_cmd_cb, get_databuf_tcp_cmd_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(GET_CLIENT_STATE_TCP_CMD, ack_cmd_cb, get_client_state_tcp_cmd_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(GET_FW_VERSION_CMD, ack_cmd_cb, get_firmware_version_cmd_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(GET_TEST_CMD, ack_cmd_cb, get_test_cmd_cb, NULL, CMD_GET_FLAG);
	spi_add_cmd(INSERT_DATABUF_CMD, insert_data_cmd_cb, ack_reply_cb, NULL, CMD_IMM_SET_FLAG);
	spi_add_cmd(SEND_DATA_UDP_CMD, send_data_udp_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
	spi_add_cmd(GET_REMOTE_DATA_CMD, ack_cmd_cb, get_reply_remote_data_cb, NULL, CMD_GET_FLAG);
}


int checkMsgParam8(unsigned char* buf)
{
	int paramLenTot=0;
	tSpiMsg* spiMsg = (tSpiMsg*)buf;
	tParam	*param = spiMsg->params;
	int i=0;
	for (; i<spiMsg->nParam; ++i)
	{
		uint8_t _len = param->paramLen;
		paramLenTot+= _len+1;
		//printk("%d) len:0x%x\n", i, _len);
		param = (tParam*)((char*)(param)+_len+1);
	}
	return paramLenTot;
}

int checkMsgParam16(unsigned char* buf)
{
	int paramLenTot=0;
	tSpiMsgData* spiMsg = (tSpiMsgData*)buf;
	tDataParam* param = (tDataParam*)spiMsg->params;
	int i=0;
	for (; i<spiMsg->nParam; ++i)
	{
		uint16_t _len = param->dataLen;
		paramLenTot+= _len+sizeof(param->dataLen);
		//printk("%d) len:0x%x\n", i, _len);
		param = (tDataParam*)((char*)(param)+_len+sizeof(param->dataLen));
	}
	return paramLenTot;
}

bool checkMsgFormat(uint8_t* _recv, int len, int* offset)
{

	unsigned char* recv = getStartCmdSeq(_recv, len, offset);
	if ((recv == NULL)||(recv!=_recv))
	{
		DEB_PIN_TRIGGER();

		IF_WARN_VER(DUMP((char*)_recv, len));

		STATSPI_DISALIGN_ERROR();

		if (recv == NULL)
			return false;
	}
	tSpiMsg* spiMsg = (tSpiMsg*) recv;
	if ((spiMsg->cmd == START_CMD)&&((spiMsg->tcmd & REPLY_FLAG) == 0))
	{
		int paramLenTot = 0;
		if (spiMsg8(spiMsg->tcmd))
			paramLenTot = checkMsgParam8(recv);
		else
		{
			DUMP_SPI(_recv, len);
			paramLenTot = checkMsgParam16(recv);
		}

		//INFO_SPI("cmd:0x%x TotLen:%d\n", spiMsg->tcmd, paramLenTot);
		char* p = (char*)recv + paramLenTot + sizeof(tSpiHdr);
		if (*p == END_CMD)
		{
			return true;
		}else{
			WARN("%d] Not found end cmd: 0x%x\n", cmdCorr, *p);
		}
	}
	return false;
}

//#define AVR32_USART_CSR_ITERATION_MASK (UNDERRUN)            0x00000400
//#define AVR32_USART_CSR_OVRE_MASK                            0x00000020
//#define AVR32_USART_CSR_RXRDY_MASK                           0x00000001


void spi_poll(struct netif* netif) {

    ard_netif = netif;

    if (startReply)
	{
		startReply = false;
		int offset = 0;
		DISABLE_SPI_INT();
		if (checkMsgFormat(_receiveBuffer, receivedChars, &offset))
		{
			state = SPI_CMD_INPROGRESS;
			count = receivedChars-offset;
			if (count >= CMD_MAX_LEN)
				count = CMD_MAX_LEN;
			memcpy(buf, &_receiveBuffer[offset], count);

			//mark as buffer used
			_receiveBuffer[0] = 0;

			int err = call_reply_cb(buf, &reply[0]);
			if (err != REPLY_NO_ERR)
			{
				DUMP_SPI(buf, count);
				DUMP_SPI(reply, replyCount);
			}
			receivedChars = 0;
			count = 0;
			state = SPI_CMD_IDLE;
		}
		else
		{
			sendError();
			WARN("%d] Check format msg failed!\n", cmdCorr);
			IF_WARN_VER(dump((char*)_receiveBuffer, receivedChars));
			state = SPI_CMD_IDLE;
			count=0;
			//mark as buffer used
			_receiveBuffer[0] = 0;
		}
		CLEAR_SPI_INT();
		//Enable Spi int to receive a new command
		ENABLE_SPI_INT();
		//Available for receiving a new spi data
	    AVAIL_FOR_SPI();
	}

#ifdef _SPI_STATS_
    if (statSpi.lastError != 0)
    {
    	WARN("%d] Errot=0x%x spiStatus:0x%x\n", cmdCorr, statSpi.lastError, statSpi.status);
    	statSpi.lastError = 0;
    }
#endif
}

inline int spi_slaveReceiveInt(volatile avr32_spi_t *spi)
{
	receivedChars=0;
	int index = 0;
	int err = SPI_OK;
	state = SPI_CMD_INPUT;
	bool endOfFrame = false;

	do {
		unsigned int timeout = SPI_TIMEOUT;
		err = SPI_OK;

		while ((spi->sr & (AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK)) !=
				(AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK)) {
			if ((timeout--)==0) {
				err=SPI_ERROR_TIMEOUT;
				break;
			}
		}
		//DEB_PIN_TG();
		#if 0
#ifdef _SPI_STATS_		
		if (spi->sr & AVR32_SPI_SR_OVRES_MASK)
		{
			STATSPI_OVERRIDE_ERROR();
		}
#endif	
#endif					
		if (err == SPI_OK) {
			_receiveBuffer[index] = (spi->rdr >> AVR32_SPI_RDR_RD_OFFSET) & 0x00ff;
			DEB_PIN_UP(2);
			if ((index==0) && (_receiveBuffer[index] != START_CMD))
				DEB_PIN_TRIGGER();
			++index;
			++receivedChars;
		}else{
#ifdef _SPI_STATS_
			STATSPI_TIMEOUT_ERROR();
#endif
			break;
		}

		/* break on buffer overflow */
		if (receivedChars >= _BUFFERSIZE) {
			err = SPI_ERROR_OVERRUN_AND_MODE_FAULT;
			break;
		}

		if (_receiveBuffer[index - 1] == END_CMD)
		{
			int8_t numParams = 0;
			int idx = PARAM_LEN_POS+1;
			bool islen16bit = ((_receiveBuffer[CMD_POS] & DATA_FLAG) == DATA_FLAG);
			if (index >= idx)
			{
				numParams = _receiveBuffer[PARAM_LEN_POS];					
				while (((index-1) > idx)&&(numParams>0))
				{
					if (islen16bit)
						idx += (_receiveBuffer[idx]<<8) + _receiveBuffer[idx+1]+2;
					else
						idx += _receiveBuffer[idx]+1;
					--numParams;
				}
				if (((index-1) == idx) && (numParams == 0))
					endOfFrame = true;
			}
			if (!endOfFrame){
				WARN("Wrong termination index:%d nParam:%d idx:%d 16bit:%d\n", index, numParams, idx, islen16bit);
				#ifdef _DEBUG_
					dump((char*)_receiveBuffer, receivedChars); 
					while(0);
				#endif
			}		
		}
	} while (!endOfFrame);
	return err;
}

#if defined (__GNUC__)
__attribute__((__interrupt__))
#elif defined (__ICCAVR32__)
__interrupt
#endif
static void spi_int_handler(void)
{
	volatile avr32_spi_t *spi = ARD_SPI;
	DEB_PIN_DN(2);
	DISABLE_SPI_INT();

	if ((spi->sr & AVR32_SPI_SR_RDRF_MASK) != 0)
	{
		int err = spi_slaveReceiveInt(ARD_SPI);
        if (err == SPI_OK)
        {
        	BUSY_FOR_SPI();
        	startReply=true;
        	++cmdCorr;
        	//maintain disable interrupt to send the reply command
        	return;
        }
   	}
	ENABLE_SPI_INT();
}

inline spi_status_t spi_read8(volatile avr32_spi_t *spi, unsigned char *data)
{
  unsigned int timeout = SPI_TIMEOUT;

  while ((spi->sr & (AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK)) !=
         (AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK)) {
    if (!timeout--) {
      return SPI_ERROR_TIMEOUT;
    }
  }

  *data = (spi->rdr >> AVR32_SPI_RDR_RD_OFFSET) & 0x00ff;

  return SPI_OK;
}


/*!
 * \brief Interrupt handler of the External interrupt line "1".
 */
#if __GNUC__
__attribute__((__interrupt__))
#elif __ICCAVR32__
__interrupt
#endif
static void eic_int_handler1(void)
{
  eic_clear_interrupt_line(&AVR32_EIC, EXT_INT_LINE1);
  startRecvCmdSignal = TRUE;
}

//! Structure holding the configuration parameters of the EIC module.
eic_options_t eic_options[EXT_INT_NB_LINES];

void initExtInt()
{
	  // Enable edge-triggered interrupt.
	  eic_options[0].eic_mode   = EIC_MODE_EDGE_TRIGGERED;
	  // Interrupt will trigger on falling edge.
	  eic_options[0].eic_edge  = EIC_EDGE_FALLING_EDGE;
	  // Initialize in synchronous mode : interrupt is synchronized to the clock
	  eic_options[0].eic_async  = EIC_SYNCH_MODE;
	  // Set the interrupt line number.
	  eic_options[0].eic_line   = EXT_INT_LINE1;

	  // Disable all interrupts.
	  Disable_global_interrupt();

	  INTC_register_interrupt(&eic_int_handler1, EXT_INT_IRQ_LINE1, AVR32_INTC_INT0);

	  // Map the interrupt lines to the GPIO pins with the right peripheral functions.
	  gpio_enable_module_pin(EXT_INT_PIN_LINE1,EXT_INT_FUNCTION_LINE1);

	  // Init the EIC controller with the options
	  eic_init(&AVR32_EIC, eic_options, EXT_INT_NB_LINES);

	  // Enable the chosen lines and their corresponding interrupt feature.
	  eic_enable_line(&AVR32_EIC, eic_options[0].eic_line);
	  eic_enable_interrupt_line(&AVR32_EIC, eic_options[0].eic_line);

	  // Enable all interrupts.
	  Enable_global_interrupt();
}

int initSpi(void* ctx)
{
	volatile avr32_spi_t *spi = &AVR32_SPI0;
	gpio_map_t spi_piomap = {          \
	    {AVR32_SPI0_SCK_0_0_PIN, AVR32_SPI0_SCK_0_0_FUNCTION},  \
	    {AVR32_SPI0_MISO_0_0_PIN, AVR32_SPI0_MISO_0_0_FUNCTION},  \
	    {AVR32_SPI0_MOSI_0_0_PIN, AVR32_SPI0_MOSI_0_0_FUNCTION},  \
	    {AVR32_SPI0_NPCS_0_0_PIN, AVR32_SPI0_NPCS_0_0_FUNCTION},  \
	  };

	INFO_INIT("SPI init...\n");

	/* Init PIO */
	gpio_enable_module(spi_piomap, ARRAY_SIZE(spi_piomap));

	spi_options_t spiOptions;

	spiOptions.reg = 0;
	spiOptions.baudrate = SPI_SLAVE_SPEED;
	spiOptions.bits = SPI_BITS;
	spiOptions.spck_delay = 0;
	spiOptions.trans_delay = 4;
	spiOptions.stay_act = 0;
	spiOptions.spi_mode = 0;
	spiOptions.modfdis = 0;

	/* Initialize as slave; bits, spi_mode */
	if (spi_initSlave(spi, spiOptions.bits, spiOptions.spi_mode) != SPI_OK)
	{
		INFO_SPI("SPI initialization failed!");
		return 1;
	}

	spi_status_t status = spi_setupChipReg(spi, &spiOptions, FPBA_HZ);
	if (status == SPI_ERROR_ARGUMENT)
		WARN("Error configuring SPI\n");

	// Disable all interrupts.
	Disable_global_interrupt();

    // Register the SPI interrupt handler to the interrupt controller.
    INTC_register_interrupt((__int_handler)(&spi_int_handler), AVR32_SPI0_IRQ, AVR32_INTC_INT0);

    // Enable all interrupts.
	Enable_global_interrupt();

    ENABLE_SPI_INT();

	spi_enable(spi);
#ifdef _SPI_STATS_
	initStatSpi();
#endif
	init_spi_cmds(ctx);

	memset(_receiveBuffer, 0, sizeof(_receiveBuffer));
	memset(buf, 0, sizeof(buf));
	memset(reply, 0, sizeof(reply));

	initMapSockTcp();
	set_result(WL_IDLE_STATUS);

	init_pBuf();

	return 0;
}