aboutsummaryrefslogtreecommitdiff
path: root/firmwares/wifishield/wifiHD/src/ard_tcp.c
blob: addbeca8da4fcf448fd11f861afe6d8ca65ae3b2 (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
/*
 * ard_tcp.c
 *
 *  Created on: May 27, 2010
 *      Author: mlf by Metodo2 srl
 */

//#define _APP_DEBUG_
#include "lwip/opt.h"

#include "lwip/mem.h"
#include "lwip/raw.h"
#include "lwip/icmp.h"
#include "lwip/netif.h"
#include "lwip/sys.h"
#include "lwip/sockets.h"
#include "lwip/inet.h"
#include "lwip/inet_chksum.h"
#include "lwip/tcp.h"
#include "lwip/udp.h"

#include "ard_tcp.h"
#include "ard_spi.h"
#include "timer.h"
#include "util.h"

#include "getopt.h"
#include "ard_utils.h"
#include "debug.h"

unsigned int startTime = 0;
extern bool ifStatus;

static int isDataSentCount = 0;

static err_t tcp_data_sent(void *arg, struct tcp_pcb *pcb, u16_t len);

static void atcp_init_pend_flags(struct ttcp* _ttcp)
{
	int i = 0;
	for (; i<MAX_CLIENT_ACCEPTED; ++i)
	{
		if (_ttcp) _ttcp->pending_close[i] = false;
	}		
}

/**
 * Clean up and free the ttcp structure
 */
static void ard_tcp_destroy(struct ttcp* ttcp) {
	err_t err = ERR_OK;
	DUMP_TCP_STATE(ttcp);

	uint8_t sock = getSock(ttcp);
	if (sock == -1)
		WARN("ttcp already deallocated!\n");

	freeAllTcpData(sock);
	int i = 0;
	for (; i<MAX_CLIENT_ACCEPTED; ++i)
	{
		if (ttcp->tpcb[i]) {
			tcp_arg(ttcp->tpcb[i], NULL);
			tcp_sent(ttcp->tpcb[i], NULL);
			tcp_recv(ttcp->tpcb[i], NULL);
			tcp_err(ttcp->tpcb[i], NULL);
			//TEMPORAQARY
			//err = tcp_close(ttcp->tpcb);
			INFO_TCP("Closing tpcb: state:0x%x err:%d\n", ttcp->tpcb[i]->state, err);
		}		
	}

	if (ttcp->lpcb) {
		tcp_arg(ttcp->lpcb, NULL);
		tcp_accept(ttcp->lpcb, NULL);
		err = tcp_close(ttcp->lpcb);
		INFO_TCP("Closing lpcb: state:0x%x err:%d\n", ttcp->lpcb->state, err);
	}

	if (ttcp->upcb) {
		udp_disconnect(ttcp->upcb);
		udp_remove(ttcp->upcb);
	}

	if (ttcp->payload)
		free(ttcp->payload);

	free(ttcp);
}

/**
 * Invoked when transfer is done or aborted (non-zero result).
 */
static void ard_tcp_done(struct ttcp* ttcp, int result) {
//	if (result == 0)
//		ard_tcp_print_stats(ttcp);

	if (ttcp->done_cb)
		ttcp->done_cb(ttcp->opaque, result);

	ard_tcp_destroy(ttcp);
	clearMapSockTcp(getSock(ttcp), GET_TCP_MODE(ttcp));
}

/**
 * Only used in TCP mode. 
 * Will transmit a maximum of pbuf->tot_len bytes. 
 * Called upon connect and when there's space available in the TCP send window
 * 
 */
static err_t tcp_send_data(struct ttcp *ttcp) {
	err_t err = ERR_OK;
	uint32_t len, orig_len;

	len = ttcp->left;
	ttcp->buff_sent = 0;

	if (len == 0) return ERR_MEM;

	INFO_TCP_VER("left=%d len:%d\n", ttcp->left, len);

	/* don't send more than we have in the payload */
	if (len > ttcp->buflen)
		len = ttcp->buflen;

	struct tcp_pcb * pcb = GET_FIRST_CLIENT_TCP_NV(ttcp);
	/* We cannot send more data than space available in the send
	 buffer. */
	if (len > tcp_sndbuf(pcb))
		len = tcp_sndbuf(pcb);

	orig_len = len;

	IF_TCP(startTime = timer_get_ms());
	err = tcp_write(pcb, ttcp->payload, len, TCP_WRITE_FLAG_COPY);
	if (err != ERR_OK)
	{
		INFO_TCP("tcp_write failed %p state:%d len:%d err:%d\n", 
				pcb, pcb->state, len, err);
		ttcp->buff_sent = 0;
	}else{
		ttcp->buff_sent = 1;
		isDataSentCount = 0;
		ttcp->left -= len;
	}

	return err;
}

/**
 * Only used in TCP mode.
 */
static err_t tcp_connect_cb(void *arg, struct tcp_pcb *tpcb, err_t err) {
	struct ttcp* _ttcp = arg;

	if (_ttcp == NULL) return ERR_ARG;

	INFO_TCP("TTCP [%p-%p]: connect %d %d\n", _ttcp, tpcb, err, tpcb->state);

	_connected =  ( tpcb->state == ESTABLISHED) ? 1 : 0;
	_ttcp->tcp_poll_retries = 0;

	_ttcp->start_time = timer_get_ms();

	return ERR_OK;
}

static void cleanSockState_cb(void *ctx) {
	struct ttcp* _ttcp = ctx;

	if (_ttcp == NULL) return;

	int sock = getSock(_ttcp);
	if (sock != -1)
		clearMapSockTcp(sock, GET_TCP_MODE(_ttcp));
	INFO_TCP("TTCP [%p]: cleanSockState_cb %d\n", _ttcp, sock);
	_connected = false;
}

/** 
 * Only used in TCP mode.
 */

static err_t close_conn_pcb(struct tcp_pcb* tpcb) {

	err_t err = tcp_close(tpcb);
	if (err== ERR_OK)
	{
		tcp_arg(tpcb, NULL);
		tcp_sent(tpcb, NULL);
		tcp_recv(tpcb, NULL);
	}

	INFO_TCP("Closing tpcb[%p]: state:0x%x err:%d\n", tpcb, tpcb->state, err);
	return err;
}

static void atcp_conn_err_cb(void *arg, err_t err) {
	struct ttcp* _ttcp = arg;

	WARN("TTCP [%p]: connection error: %d\n",
			_ttcp, err);

	if (ifStatus == false)
		printk("Abort connection\n");

	atcp_init_pend_flags(_ttcp);
}

static void atcp_conn_cli_err_cb(void *arg, err_t err) {
	struct ttcp* _ttcp = arg;

	if (_ttcp == NULL) return;

	WARN("TTCP [%p]: connection error: %d arg:%p\n",
			_ttcp, err, arg);

	if (ifStatus == false)
		printk("Abort connection\n");

	if ((_ttcp)&&(err == ERR_ABRT))
	{
		WARN("TTCP [%p]: free memory\n", _ttcp);
		_ttcp->tcp_poll_retries = 0;
		cleanSockState_cb(_ttcp);
		if (_ttcp->payload)
			free(_ttcp->payload);
		free(_ttcp);
	}

	atcp_init_pend_flags(_ttcp);
}

static err_t close_conn(struct ttcp *_ttcp, struct tcp_pcb* tpcb) {

	if (_ttcp == NULL) return ERR_MEM;
	
	int8_t id = getNewClientConnId(_ttcp, tpcb);
	if (id == NO_VALID_ID) return ERR_MEM;
	err_t err = close_conn_pcb(_ttcp->tpcb[id]);

	if (err == ERR_MEM)
		_ttcp->pending_close[id] = true;
	else{
		atcp_init_pend_flags(_ttcp);
		removeNewClientConn(_ttcp, _ttcp->tpcb[id]);
		WARN("----------------------\n");
	}
	return err;
}

void closeConnections()
{
	int ii=0;
	for (; ii<MAX_MODE_NUM; ii++)
	{
		int i = 0;
		for (; i<MAX_SOCK_NUM; i++)
		{
			void* p = getTTCP(i, ii);
			if (p)
			{
				ttcp_t* _ttcp = (ttcp_t* )p;
				if (_ttcp->udp == TCP_MODE)
				{
					ard_tcp_destroy(_ttcp);
					clearMapSockTcp(getSock(_ttcp), GET_TCP_MODE(_ttcp));
				}
			}
		}
	}
}

/**
 * Only used in TCP mode.
 */
static err_t atcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
		err_t err) {
	struct ttcp* ttcp = arg;

	if (err == ERR_OK && p != NULL) {
		DATA_LED_ON();
		/* for print_stats() */
		ttcp->recved += p->tot_len;

		if ((ttcp->verbose)||(verboseDebug & INFO_TCP_FLAG)) {
			INFO_TCP("len:%d\n",p->tot_len);
			DUMP_TCP(p->payload, p->tot_len);
			ttcp->print_cnt++;
		}

		uint8_t* pBufferStore = insert_pBuf(p, ttcp->sock, (void*) pcb);
		INFO_TCP("sock:%d pcb:%p pbuf:%p err:%d bufStore:%p len:%d\n",
				ttcp->sock, pcb, p, err, pBufferStore, p->tot_len);
		pbuf_free(p);
		DATA_LED_OFF();
	}

	/* p will be NULL when remote end is done */
	if (err == ERR_OK && p == NULL) {
		INFO_TCP("atcp_recv_cb p=NULL on sock:%d pcb:%p\n",	ttcp->sock, pcb);
		close_conn(ttcp, pcb);
	}

	if (err!=ERR_OK)
		WARN("err=%d p=%p\n", err, p);
	return ERR_OK;
}

void ack_recved(void* pcb, int len) {
	// Comment the call because it is activated on atcp_recv_cb
	INFO_TCP("Received %p len:%d\n", pcb, len);
	tcp_recved(pcb, len);
}

static err_t atcp_poll(void *arg, struct tcp_pcb *pcb) {
	struct ttcp* _ttcp = arg;

	if (_ttcp == NULL) return ERR_ARG;

	if (_ttcp->left>0)
		++_ttcp->tcp_poll_retries;

	if (_ttcp->tcp_poll_retries > 4) {
		WARN("ARD TCP [%p] arg=%p retries=%d\n",
				pcb, arg, _ttcp->tcp_poll_retries);
		_ttcp->tcp_poll_retries = 0;
		tcp_abort(pcb);
		atcp_init_pend_flags(_ttcp);
	    return ERR_ABRT;
	}
	
	if (pcb)
		INFO_TCP_POLL("keepAliveCnt:%d keep_idle:%d persist_cnt:%d\n", 
						pcb->keep_cnt_sent, pcb->keep_idle, pcb->persist_cnt);

	int8_t id = getNewClientConnId(_ttcp, pcb);
	if (_ttcp->left > 0)
		INFO_TCP("ARD TCP [%p-%p] arg=%p retries=%d pend.close:%d len:%d\n",
			(_ttcp)?GET_FIRST_CLIENT_TCP(_ttcp):0, pcb, arg,
			_ttcp->tcp_poll_retries, _ttcp->pending_close[id], (_ttcp)?_ttcp->left:0);
	tcp_send_data(_ttcp);

	if ((id != NO_VALID_ID) && (_ttcp->pending_close[id]))
	{		
		err_t err = ERR_OK;
		if (id >=0){
			err = tcp_close(pcb);
			if (err == ERR_MEM)
			{
				_ttcp->pending_close[id] = true;
			}
			else
			{
				atcp_init_pend_flags(_ttcp);
			}
		}	
		INFO_TCP("ARD TCP [%p-%p] try to close pending:%d err:%d id:%d\n", pcb, 
			(_ttcp)?GET_FIRST_CLIENT_TCP(_ttcp):0, _ttcp->pending_close[id], err, id);
	}
	return ERR_OK;
}

static err_t atcp_poll_conn(void *arg, struct tcp_pcb *pcb) {
	struct ttcp* _ttcp = arg;

	if (_ttcp == NULL) return ERR_ARG;

	int8_t id = getNewClientConnId(_ttcp, pcb);
	if (id != NO_VALID_ID)
	{
		if (_ttcp->pending_close[id])
			++(_ttcp->tcp_poll_retries);
	}

	if (_ttcp->tcp_poll_retries > 8) {
		WARN("ARD TCP [%p-%p] arg=%p retries=%d\n",
				pcb, GET_FIRST_CLIENT_TCP(_ttcp), arg, _ttcp->tcp_poll_retries);
		_ttcp->tcp_poll_retries = 0;
		tcp_abort(pcb);
		return ERR_ABRT;
	}

	INFO_TCP_POLL("ARD TCP [%p-%p] arg=%p retries=%d pend.close:%d conn:%d\n",
			(_ttcp)?GET_FIRST_CLIENT_TCP(_ttcp):0, pcb, arg,
			_ttcp->tcp_poll_retries, _ttcp->pending_close[id], _connected);

	if ((_ttcp)&&(_connected)) tcp_send_data(_ttcp);

	if ((id != NO_VALID_ID) && (_ttcp->pending_close[id]))
	{
		err_t err = tcp_close(pcb);
		if (err == ERR_MEM)
		{
			_ttcp->pending_close[id] = true;
		}
		else
		{
			cleanSockState_cb(_ttcp);
			if (_ttcp->payload)
				free(_ttcp->payload);
			free(_ttcp);
			_ttcp->pending_close[id] = false;

		}

		INFO_TCP("ARD TCP [%p-%p] try to close pending:%d\n", pcb, (_ttcp)?GET_FIRST_CLIENT_TCP(_ttcp):0, _ttcp->pending_close[id]);
	}
	return ERR_OK;
}

int8_t currConnId = 0;
#define GET_IDX_CONN(I) ((I+currConnId)<MAX_CLIENT_ACCEPTED ? (I+currConnId) : (I+currConnId-MAX_CLIENT_ACCEPTED))

int8_t getCurrClientConnId() { return currConnId;}

int8_t getNewClientConnId(struct ttcp* _ttcp, struct tcp_pcb *newpcb)
{
	if (_ttcp != NULL){
		int i = 0;
		for (; i<MAX_CLIENT_ACCEPTED; ++i)
		{
			int idx = GET_IDX_CONN(i);

			if (_ttcp->tpcb[idx] == newpcb)
			{
				INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, idx, newpcb);
				return idx;
			}				
		}
	}
	WARN("No Valid Id for ttcp:%p pcb:%p\n",  _ttcp, newpcb);
	return NO_VALID_ID;
}

struct tcp_pcb * getFirstClient(struct ttcp* _ttcp, bool verbose)
{
	if (_ttcp != NULL){
		int i = 0;
		for (; i<MAX_CLIENT_ACCEPTED; ++i)
		{
			int idx = GET_IDX_CONN(i);
			if (_ttcp->tpcb[idx] != NULL)
			{
				if (verbose) INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, idx, _ttcp->tpcb[idx]);
				currConnId = idx;
				return _ttcp->tpcb[idx];
			}				
		}
	}
	if (verbose) WARN("No Valid client for ttcp:%p\n",  _ttcp);
	return NULL;
}


int8_t setNewClientConn(struct ttcp* _ttcp, struct tcp_pcb *newpcb, uint8_t id)
{
	if ((_ttcp != NULL)&&(id>=0)&&(id<MAX_CLIENT_ACCEPTED)){
		INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, id, newpcb);
		_ttcp->tpcb[id] = newpcb;
		return id;
	}
	return NO_VALID_ID;
}

int8_t insertNewClientConn(struct ttcp* _ttcp, struct tcp_pcb *newpcb)
{
	if (_ttcp != NULL){
		int i = 0;
		for (; i<MAX_CLIENT_ACCEPTED; ++i)
		{
			int idx = GET_IDX_CONN(i);
			if ((_ttcp->tpcb[idx] == NULL)||(_ttcp->tpcb[idx] == newpcb))
			{
				INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, idx, newpcb);
				_ttcp->tpcb[idx] = newpcb;
				return idx;
			}
		}
	}
	return NO_VALID_ID;
}

int8_t removeNewClientConn(struct ttcp* _ttcp, struct tcp_pcb *newpcb)
{
	if (_ttcp != NULL){
		int i = 0;
		for (; i<MAX_CLIENT_ACCEPTED; ++i)
		{
			int idx = GET_IDX_CONN(i);
			if (_ttcp->tpcb[idx] == newpcb)
			{
				INFO_TCP("ttcp:%p id=%d, tpcb=%p\n", _ttcp, idx, newpcb);
				_ttcp->tpcb[idx] = NULL;
				return idx;
			}
		}
	}
	return NO_VALID_ID;
}

bool cleanNewClientConn(struct ttcp* _ttcp)
{
	if (_ttcp != NULL){
		int i = 0;
		for (; i<MAX_CLIENT_ACCEPTED; ++i)
			_ttcp->tpcb[i] = NULL;
		return true;
	}
	return false;
}


/**
 * Only used in TCP mode.
 */
static err_t atcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) {
	struct ttcp* _ttcp = arg;

	if (_ttcp == NULL) return ERR_ARG;

	INFO_TCP("ARD TCP [%p]: accept new [%p]\n", _ttcp, newpcb);
	INFO_TCP("local:%d remote:%d state:%d\n", newpcb->local_port, newpcb->remote_port, newpcb->state);

	int8_t id = insertNewClientConn(_ttcp, newpcb);
	tcp_arg(_ttcp->tpcb[id], _ttcp);
	tcp_recv(_ttcp->tpcb[id], atcp_recv_cb);
	tcp_err(_ttcp->tpcb[id], atcp_conn_err_cb);
	tcp_poll(_ttcp->tpcb[id], atcp_poll, 4);
	// Copy the pointer to ttcp also to TRANSMIT mode for the clients connected to the server
	int _sock = getSock(_ttcp);
	if ((_sock != -1)&&(IS_VALID_SOCK(_sock)))
		setMapSockMode(_sock, _ttcp, TTCP_MODE_TRANSMIT);
	_ttcp->start_time = timer_get_ms();
	return ERR_OK;
}

/**
 * Start TCP transfer.
 */
static int atcp_start(struct ttcp* ttcp) {
	err_t err = ERR_OK;

	struct tcp_pcb * p = tcp_new();
	
	if (p == NULL) {
		WARN("TTCP [%p]: could not allocate pcb\n", ttcp);
		return -1;
	}

	ttcp->payload = malloc(ttcp->buflen);
	if (ttcp->payload == NULL) {
		WARN("TTCP [%p]: could not allocate payload\n", ttcp);
		return -1;
	}

	tcp_arg(p, ttcp);
	atcp_init_pend_flags(ttcp);

	if (ttcp->mode == TTCP_MODE_TRANSMIT) {
		setNewClientConn(ttcp, p, 0);
		struct tcp_pcb * pcb = GET_FIRST_CLIENT_TCP(ttcp);
		tcp_err(pcb, atcp_conn_cli_err_cb);
		tcp_recv(pcb, atcp_recv_cb);
		tcp_sent(pcb, tcp_data_sent);
		tcp_poll(pcb, atcp_poll_conn, 4);
		_connected = false;
		INFO_TCP("[tpcb]-%p payload:%p\n", pcb, ttcp->payload);
		DUMP_TCP_STATE(ttcp);
		if (tcp_connect(pcb, &ttcp->addr, ttcp->port, tcp_connect_cb)
				!= ERR_OK) {
			WARN("TTCP [%p]: tcp connect failed\n", ttcp);
			return -1;
		}

	} else {
		INFO_TCP("BEFORE BIND ttcp:%p lpcb:%p pcb:%p\n", ttcp, ttcp->lpcb, GET_FIRST_CLIENT_TCP(ttcp));

		err = tcp_bind(p, IP_ADDR_ANY, ttcp->port);
		if (err != ERR_OK){
			WARN("TTCP [%p]: bind failed err=%d Port already used\n", ttcp, err);
			return -1;
		}

		ttcp->lpcb = tcp_listen(p);
		if (ttcp->lpcb == NULL) {
			WARN("TTCP [%p]: listen failed\n", ttcp);
			return -1;
		}

		DUMP_TCP_STATE(ttcp);
		tcp_accept(ttcp->lpcb, atcp_accept_cb);
	}

	return 0;
}

static void
udp_send_data(struct ttcp* ttcp);

/**
 * Only used in UDP mode. Scheduled after data has been sent in udp_send_data()
 * if we have more data to send.
 */
static void udp_timeout_cb(void *ctx) {
	struct ttcp* ttcp = ctx;
	udp_send_data(ttcp);
}

static int udp_send_bytes(struct ttcp* ttcp, uint32_t len) {
	struct pbuf* p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
	if (p == NULL) {
		WARN("TTCP [%p]: could not allocate pbuf\n", ttcp);
		return -1;
	}

	if (udp_send(ttcp->upcb, p) != ERR_OK) {
		WARN("TTCP [%p]: udp_send() failed\n", ttcp);
		pbuf_free(p);
		return -1;
	}

	pbuf_free(p);
	return 0;
}

/**
 * Only used in UDP mode. First call will send the start marker. When all 
 * ttcp data has been sent, a number of end markers will be sent. After
 * end marker transmission, this function will complete the ttcp process.
 */
static void udp_send_data(struct ttcp* ttcp) {
	/* send start marker first time */
	if (!ttcp->udp_started) {
		if (udp_send_bytes(ttcp, 4) == 0) {
			ttcp->udp_started = 1;
			ttcp->start_time = timer_get_ms();
		}
	}

	/* normal case */
	else if (ttcp->left) {
		/* send data */
		if (udp_send_bytes(ttcp, ttcp->buflen) == 0)
			ttcp->left -= ttcp->buflen;
	}

	/* end marker? */
	else if (ttcp->left == 0 && ttcp->udp_end_marker_left) {
		if (udp_send_bytes(ttcp, 4) == 0)
			ttcp->udp_end_marker_left--;
	}

	/* all end markers sent */
	else if (ttcp->left == 0) {
		ard_tcp_done(ttcp, 0);
		return;
	}

	ttcp->tid
			= timer_sched_timeout_cb(0, TIMEOUT_ONESHOT, udp_timeout_cb, ttcp);
}

/**
 * Only used in UDP mode. Will finalize the ttcp process when an end marker
 * is seen.
 */
static void audp_recv_cb(void *arg, struct udp_pcb *upcb, struct pbuf *p,
		struct ip_addr *addr, u16_t port) {
	struct ttcp* ttcp = arg;

	/* for print_stats() */
	ttcp->recved += p->tot_len;
	DUMP(p->payload,p->tot_len);
	if (ttcp->verbose) {
		printk(".");
		if (ttcp->print_cnt % 80 == 0)
			printk("\n");
		ttcp->print_cnt++;
	}
	INFO_TCP("UDP Insert %p sock:%d addr:%s port:%d\n", p, ttcp->sock,
			ip2str(*addr), port);
	insert_pBuf(p, ttcp->sock, (void*) upcb);
	setRemoteClient(ttcp->sock, addr->addr, port);

	pbuf_free(p);
}

/**
 * Start UDP transfer.
 */
static int udp_start(struct ttcp* ttcp) {
	err_t err = ERR_OK;
	ttcp->udp_end_marker_left = 5;
	ttcp->upcb = udp_new();
	if (ttcp->upcb == NULL) {
		WARN("TTCP [%p]: could not allocate pcb\n", ttcp);
		return -1;
	}

	INFO_TCP("%s, upcb:%p %s:%d\n", __FUNCTION__, ttcp->upcb, ip2str(ttcp->addr), ttcp->port);
	if (ttcp->mode == TTCP_MODE_TRANSMIT) {
		if (udp_connect(ttcp->upcb, &(ttcp->addr), ttcp->port) != ERR_OK) {
			WARN("TTCP [%p]: udp connect failed\n", ttcp);
			return -1;
		}
		udp_recv(ttcp->upcb, audp_recv_cb, ttcp);
	} else {
		/* bind to any IP address on port specified */
		err = udp_bind(ttcp->upcb, IP_ADDR_ANY, ttcp->port);
		if  (err!= ERR_OK) {
			WARN("TTCP [%p]: bind failed err=%d Port already used\n", ttcp, err);
		    return -1;
		}
		// clear remote client data
		setRemoteClient(ttcp->sock, 0, 0);
		udp_recv(ttcp->upcb, audp_recv_cb, ttcp);
	}
	INFO_TCP("%s, loc:0x%x-%d rem:0x%x-%d\n", __FUNCTION__, 
		 ttcp->upcb->local_ip.addr, ttcp->upcb->local_port, 
		 ttcp->upcb->remote_ip.addr, ttcp->upcb->remote_port);
	return 0;
}

/**
 * Start a new ttcp transfer. It should be possible to call this function
 * multiple times in order to get multiple ttcp streams. done_cb() will be
 * invoked upon completion.
 * 
 */
int ard_tcp_start(struct ip_addr addr, uint16_t port, void *opaque,
		ard_tcp_done_cb_t *done_cb, int mode, uint16_t nbuf, uint16_t buflen,
		int udp, int verbose, uint8_t sock, void** _ttcp) {
	struct ttcp* ttcp;
	int status;

	if (mode != TTCP_MODE_TRANSMIT && mode != TTCP_MODE_RECEIVE) {
		WARN("TTCP [-]: invalid mode\n");
		return -1;
	}

	if (nbuf == 0) {
		WARN("TTCP [-]: invalid nbuf\n");
		return -1;
	}

	if (buflen == 0) {
		WARN("TTCP [-]: invalid buflen\n");
		return -1;
	}

	ttcp = calloc(1, sizeof(struct ttcp));
	if (ttcp == NULL) {
		WARN("TTCP [-]: could not allocate memory for ttcp\n");
		return -1;
	}

	ttcp->addr = addr;
	ttcp->port = port;
	ttcp->nbuf = nbuf;
	ttcp->mode = mode;
	ttcp->left = 0;
	ttcp->done_cb = done_cb;
	ttcp->opaque = opaque;
	ttcp->udp = udp;
	ttcp->verbose = verbose;
	ttcp->buflen = buflen;
	cleanNewClientConn(ttcp);

	if (ttcp->udp)
		status = udp_start(ttcp);
	else
		status = atcp_start(ttcp);

	if (status) {
		WARN("Start server FAILED!\n");
		goto fail;
	}
	INFO_TCP("TTCP [%p-%p]: nbuf=%d, buflen=%d, port=%d (%s/%s)\n", ttcp,
			((ttcp->udp==1)?(void*)ttcp->upcb:GET_FIRST_CLIENT_TCP(ttcp)), ttcp->nbuf, ttcp->buflen, 
			ttcp->port, ProtMode2Str(ttcp->udp), Mode2Str(ttcp->mode));

	*_ttcp = (void*) ttcp;
	ttcp->sock = sock;
	ttcp->buff_sent = 1;

	return 0;

	fail: ard_tcp_destroy(ttcp);
	return -1;
}

void ard_tcp_stop(void* ttcp) {
	struct ttcp* _ttcp = (struct ttcp*) ttcp;
	if (_ttcp == NULL)
	{
		WARN("ttcp = NULL!\n");
		return;
	}
	if (_ttcp->mode == TTCP_MODE_TRANSMIT) {
		ard_tcp_destroy(_ttcp);
    	clearMapSockTcp(getSock(_ttcp), GET_TCP_MODE(_ttcp));
    	_ttcp->tcp_poll_retries = 0;
	}else{
		DUMP_TCP_STATE(_ttcp);

		int i = getCurrClientConnId();
		if ((_ttcp)&&(_ttcp->tpcb[i])&&(_ttcp->tpcb[i]->state!=LAST_ACK)&&(_ttcp->tpcb[i]->state!=CLOSED))
		{
			// Flush all the data
			err_t err=tcp_output(_ttcp->tpcb[i]);
			INFO_TCP("flush data: tpcb:%p err:%d\n", _ttcp->tpcb[i], err);
			// if any socket  cannot be close stop the close connection
			close_conn(_ttcp, _ttcp->tpcb[i]);
		}
	}
}

uint8_t getStateTcp(void* p, bool client) {
	struct ttcp* _ttcp = (struct ttcp*) p;

	if (ifStatus == false)
		return CLOSED;
	struct tcp_pcb * pcb = GET_FIRST_CLIENT_TCP_NV(_ttcp);
	if ((_ttcp != NULL) && ((pcb != NULL) || (client==0))) {
		IF_SPI_POLL(DUMP_TCP_STATE(_ttcp));
		if (client)
			return pcb->state;
		else
			return _ttcp->lpcb->state;
	} else {
		WARN_POLL("TCP not initialized ttcp:%p tpcb:%p lpcb:%p\n",
				_ttcp, ((_ttcp)?pcb:0), ((_ttcp)?_ttcp->lpcb:0));
	}
	return CLOSED;
}

uint8_t getModeTcp(void* p) {
	struct ttcp* _ttcp = (struct ttcp*) p;

	if (_ttcp != NULL)
		return _ttcp->mode;
	return 0;
}

uint8_t isDataSent(void* p) {
	struct ttcp *_ttcp = (struct ttcp *)p;

	if ((_ttcp)&&(!_ttcp->buff_sent))
	{
		INFO_TCP_VER("%d) Wait to send data\n", ++isDataSentCount);
		return 0;
	}

	return 1;
}

static err_t tcp_data_sent(void *arg, struct tcp_pcb *pcb, u16_t len) {
	struct ttcp *_ttcp;

	LWIP_UNUSED_ARG(len);

	_ttcp = arg;

	if (_ttcp == NULL) return ERR_ARG;

	_ttcp->tcp_poll_retries = 0;
	if (_ttcp) _ttcp->buff_sent = 1;


	INFO_TCP("Packet sent pcb:%p len:%d dur:%d left:%d count:%d\n", pcb, len, timer_get_ms() - startTime,
			(_ttcp)?(_ttcp->left):0, isDataSentCount);

	isDataSentCount = 0;

	if ((_ttcp)&&(_ttcp->left > 0)) {
		tcp_send_data(_ttcp);
	}

	return ERR_OK;
}

int sendTcpData(void* p, uint8_t* buf, uint16_t len) 
{
	struct ttcp* _ttcp = (struct ttcp*) p;

	if (_ttcp==NULL)
	{
		WARN("ttcp == NULL!\n");
		return WL_FAILURE;
	}
	
	struct tcp_pcb * pcb = GET_FIRST_CLIENT_TCP_NV(_ttcp);
	INFO_TCP_VER("ttcp:%p pcb:%p buf:%p len:%d\n", _ttcp, pcb, buf, len);
	DUMP_TCP(buf,len);
	IF_TCP_VER(DUMP_TCP_STATE(_ttcp));

	if ((_ttcp != NULL) && (pcb != NULL) &&
			(buf != NULL) && (len != 0) && (_ttcp->payload != NULL)) {
		if (pcb->state == ESTABLISHED || pcb->state == CLOSE_WAIT ||
			pcb->state == SYN_SENT || pcb->state == SYN_RCVD) {

		memcpy(_ttcp->payload, buf, len);
		_ttcp->payload[len]='\0';
		INFO_TCP_VER("'%s'\n", _ttcp->payload);
		_ttcp->left = len;
		tcp_sent(pcb, tcp_data_sent);
		tcp_send_data(_ttcp);

		return WL_SUCCESS;
		}
	}
	//printk("Write failure _ttcp=%p _ttcp->tpcb=%p buf=%p len=%d\n", _ttcp, _ttcp->tpcb, buf, len);
	return WL_FAILURE;
}

int sendUdpData(void* ttcp, uint8_t* buf, uint16_t len) {
	struct ttcp* _ttcp = (struct ttcp*) ttcp;
	if ((_ttcp != NULL) && (buf != NULL) && (len != 0))
	{
		INFO_TCP("buf:%p len:%d\n", buf, len);
		DUMP_TCP(buf,len);
	}else{
		return WL_FAILURE;
	}

	struct pbuf* p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
	if (p == NULL) {
		WARN("TTCP [%p]: could not allocate pbuf\n", ttcp);
		return WL_FAILURE;
	}
	memcpy(p->payload, buf, len);
	if (udp_send(_ttcp->upcb, p) != ERR_OK) {
		WARN("TTCP [%p]: udp_send() failed\n", _ttcp);
		pbuf_free(p);
		return WL_FAILURE;
	}

	pbuf_free(p);
	return WL_SUCCESS;
}



char
		usage[] =
				"Usage: ttcp -t/-r [-options] host\n\
        -l      length of bufs written to network (default 1024)\n\
        -n      number of bufs written to network (default 1024)\n\
        -p      port number to send to (default 2000)\n\
        -u      udp\n\
        -v      verbose\n";

/**
 *
 */
cmd_state_t cmd_ttcp(int argc, char* argv[], void* ctx) {

	int c;
	int mode = TTCP_MODE_TRANSMIT;
	int verbose = 0;
	uint16_t buflen = 1024;
	uint16_t nbuf = 1024;
	uint16_t port = 2000;
	int udp = 0;
	struct ip_addr addr = { 0 };

	optind = 1;
	while ((c = getopt(argc, argv, "utrl:n:p:v")) != -1) {
		switch (c) {
		case 't':
			mode = TTCP_MODE_TRANSMIT;
			break;
		case 'r':
			mode = TTCP_MODE_RECEIVE;
			break;
		case 'l':
			buflen = atoi(optarg);
			break;
		case 'v':
			verbose = 1;
			break;
		case 'n':
			nbuf = atoi(optarg);
			break;
		case 'u':
			udp = 1;
			break;
		case 'p':
			port = atoi(optarg);
			break;
		}
	}

	if (mode == TTCP_MODE_TRANSMIT) {
		if (optind >= argc) {
			printk("%s", usage);
			return CMD_DONE;
		}

		addr = str2ip(argv[optind]);
		if (!addr.addr) {
			printk("%s", usage);
			return CMD_DONE;
		}
	}
	void* _ttcp = NULL;
	if (ard_tcp_start(addr, port, NULL, NULL, mode, nbuf, buflen, udp, verbose,
			0, &_ttcp))
		return CMD_DONE;

	return CMD_DONE;
}


#if 0
#include "lwip/sockets.h"

void testlwip()
{
	int Sock;
	fd_set fdsetR;
	FD_ZERO(&fdsetR);
	FD_SET(Sock, &fdsetR);
	fd_set fdsetE = fdsetR;

	int rc;
	const int cMillies = 10000;
	struct timeval timeout;
	timeout.tv_sec = cMillies / 1000;
	timeout.tv_usec = (cMillies % 1000) * 1000;
	//rc = lwip_select(Sock + 1, &fdsetR, NULL, &fdsetE, &timeout);
}
#endif