/* * Copyright (c) 2001-2003 Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of the lwIP TCP/IP stack. * * Author: Adam Dunkels * */ #include "lwip/debug.h" #include "lwip/stats.h" #include "lwip/tcp.h" #include "TxTest.h" struct tcp_pcb *tx_pcb; struct TxTest_state *ps = NULL; int totsent = 0; char data_buf[TXTEST_PKT_SIZE] __attribute__((aligned (4))); /*---------------------------------------------------------------------------*/ static void TxTest_err(void *arg, err_t err) { //struct TxTest_state *ps = arg; xil_printf("TxTest_err\r\n"); if(arg != NULL) { //mem_free(ps->buf_p); mem_free(ps); } } /*---------------------------------------------------------------------------*/ static void close_conn(struct tcp_pcb *pcb, struct TxTest_state *ps) { tcp_arg(pcb, NULL); tcp_sent(pcb, NULL); tcp_recv(pcb, NULL); if(ps != NULL) { //mem_free(ps->buf_p); mem_free(ps); } if (tcp_close(pcb) != ERR_OK) print("[TxTest could not close] \r\n"); else print("[TxTest_closed] \r\n"); } /*-----------------------------------------------------------------------------------*/ void send_data(struct tcp_pcb *pcb, struct TxTest_state *ps) { err_t err = ERR_OK; u16_t len; /* We cannot send more data than space available in the send buffer. */ if (tcp_sndbuf(pcb) < ps->left) { len = tcp_sndbuf(pcb); } else { len = ps->left; } //xil_printf("Len: %d \r\n", len); if (len > 0) { err = tcp_write(pcb, ps->buf_p, len, 1); //err = tcp_write(pcb, ps->buf_p, len, 0); } /* if (err != ERR_MEM) { if (len > 0 ) { ps->buf_p += len; ps->left -= len; } else { ps->buf_p = data_buf; ps->left = TXTEST_PKT_SIZE; } } */ } /*---------------------------------------------------------------------------*/ static err_t TxTest_sent(void *arg, struct tcp_pcb *pcb, int rcvlen) { struct TxTest_state *ps = (struct TxTest_state *)arg; int buflen, len; int stat; /* if (ps->left > 0) { send_data(pcb, ps); } else { //close_conn(pcb, ps); ps->buf_p = data_buf; ps->left = TXTEST_PKT_SIZE; send_data(pcb, ps); } */ if (rcvlen > 0 ) { ps->buf_p += len; ps->left -= len; } if (ps->left <= 0) { ps->buf_p = data_buf; ps->left = TXTEST_PKT_SIZE; } //print("[TxTest_sent] \r\n"); return ERR_OK; } /*---------------------------------------------------------------------------*/ static err_t TxTest_connected(void *arg, struct tcp_pcb *pcb, err_t error) { char *buf_p; int i, len; int stat, buflen; xil_printf("[TxTest_connected]\r\n"); tcp_setprio(pcb, TCP_PRIO_MIN); ps = mem_malloc(sizeof(struct TxTest_state)); if (ps == NULL){ xil_printf("could not malloc memory for TxTest_state\r\n"); return ERR_MEM; } len = TXTEST_PKT_SIZE; for (i=0;ileft = len; ps->buf_p = data_buf; /* Tell TCP that this is the structure we wish to be passed for our callbacks. */ tcp_arg(pcb, ps); tcp_err(pcb, TxTest_err); tcp_sent(pcb, TxTest_sent); /* We cannot send more data than space available in the send buffer. */ send_data(pcb, ps); return ERR_OK; } /*---------------------------------------------------------------------------*/ void TxTest_init(void) { struct ip_addr ipaddr; char ip[4] = {1,2,3,5}; int port; int stat; int i; xil_printf("[TxTest_init]\r\n"); IP4_ADDR(&ipaddr, ip[0],ip[1],ip[2],ip[3]); port = 2000; tx_pcb = tcp_new(); if ( !tx_pcb){ xil_printf("tcp_new fails\r\n"); return; } stat = tcp_connect(tx_pcb, &ipaddr, port, TxTest_connected); if (stat == ERR_MEM) print("[TxTest_connect] memory error \r\n"); } /*---------------------------------------------------------------------------*/