Example #1
0
END_TEST

START_TEST(test_pbuf_queueing_bigger_than_64k)
{
  int i;
  err_t err;
  struct pbuf *p1, *p2, *p3, *rest2=NULL, *rest3=NULL;
  LWIP_UNUSED_ARG(_i);

  for(i = 0; i < TESTBUFSIZE_1; i++) {
    testbuf_1[i] = (u8_t)rand();
  }
  for(i = 0; i < TESTBUFSIZE_2; i++) {
    testbuf_2[i] = (u8_t)rand();
  }
  for(i = 0; i < TESTBUFSIZE_3; i++) {
    testbuf_3[i] = (u8_t)rand();
  }

  p1 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_1, PBUF_POOL);
  fail_unless(p1 != NULL);
  p2 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_2, PBUF_POOL);
  fail_unless(p2 != NULL);
  p3 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_3, PBUF_POOL);
  fail_unless(p3 != NULL);
  err = pbuf_take(p1, testbuf_1, TESTBUFSIZE_1);
  fail_unless(err == ERR_OK);
  err = pbuf_take(p2, testbuf_2, TESTBUFSIZE_2);
  fail_unless(err == ERR_OK);
  err = pbuf_take(p3, testbuf_3, TESTBUFSIZE_3);
  fail_unless(err == ERR_OK);

  pbuf_cat(p1, p2);
  pbuf_cat(p1, p3);

  pbuf_split_64k(p1, &rest2);
  fail_unless(p1->tot_len == TESTBUFSIZE_1);
  fail_unless(rest2->tot_len == (u16_t)((TESTBUFSIZE_2+TESTBUFSIZE_3) & 0xFFFF));
  pbuf_split_64k(rest2, &rest3);
  fail_unless(rest2->tot_len == TESTBUFSIZE_2);
  fail_unless(rest3->tot_len == TESTBUFSIZE_3);

  pbuf_copy_partial(p1, testbuf_1a, TESTBUFSIZE_1, 0);
  pbuf_copy_partial(rest2, testbuf_2a, TESTBUFSIZE_2, 0);
  pbuf_copy_partial(rest3, testbuf_3a, TESTBUFSIZE_3, 0);
  for(i = 0; i < TESTBUFSIZE_1; i++)
    fail_unless(testbuf_1[i] == testbuf_1a[i]);
  for(i = 0; i < TESTBUFSIZE_2; i++)
    fail_unless(testbuf_2[i] == testbuf_2a[i]);
  for(i = 0; i < TESTBUFSIZE_3; i++)
    fail_unless(testbuf_3[i] == testbuf_3a[i]);

  pbuf_free(p1);
  pbuf_free(rest2);
  pbuf_free(rest3);
}
socket_error_t lwipv4_socket_send(struct socket *socket, const void * buf, const size_t len)
{
    err_t err = ERR_VAL;
    switch(socket->family) {
    case SOCKET_DGRAM: {
        struct pbuf *pb = pbuf_alloc(PBUF_TRANSPORT,len,PBUF_RAM);
        socket_event_t e;
        socket_api_handler_t handler = socket->handler;
        err = pbuf_take(pb, buf, len);
        if (err != ERR_OK)
            break;
        err = udp_send(socket->impl, pb);
        pbuf_free(pb);
        if (err != ERR_OK)
            break;
        //Notify the application that the transfer is queued at the MAC layer
        e.event = SOCKET_EVENT_TX_DONE;
        e.sock = socket;
        e.i.t.sentbytes = len;
        socket->event = &e;
        handler();
        socket->event = NULL;
        break;
    }
    case SOCKET_STREAM:
        err = tcp_write(socket->impl,buf,len,TCP_WRITE_FLAG_COPY);
        tcp_output(socket->impl); // TODO use opt instead with SOCKET_OPT_NAGGLE
        break;
    }
    return lwipv4_socket_error_remap(err);
}
socket_error_t lwipv4_socket_send_to(struct socket *socket, const void * buf, const size_t len, const struct socket_addr *addr, const uint16_t port)
{
    ip_addr_t a;
    err_t err = ERR_VAL;
    switch(socket->family) {
    case SOCKET_DGRAM: {
        struct pbuf *pb = pbuf_alloc(PBUF_TRANSPORT,len,PBUF_RAM);
        socket_event_t e;
        socket_api_handler_t handler = socket->handler;
        err = pbuf_take(pb, buf, len);
        if (err != ERR_OK)
        	break;
        a.addr = socket_addr_get_ipv4_addr(addr);
        err = udp_sendto(socket->impl, pb, &a, port);
        pbuf_free(pb);
        if (err != ERR_OK)
        	break;
        e.event = SOCKET_EVENT_TX_DONE;
        e.sock = socket;
        e.i.t.sentbytes = len;
        socket->event = &e;
        handler();
        socket->event = NULL;
        break;
    }
    case SOCKET_STREAM:
        err = ERR_USE;
        break;
    }
    return lwipv4_socket_error_remap(err);

}
Example #4
0
void SendMessage(int id, unsigned int msg_id, void* data, unsigned int len)
{
  if(data)memcpy(&tx_udp_msg[id].data[0],data,len);
  
  tx_udp_msg[id].msg_id  = msg_id;
  tx_udp_msg[id].msg_len = len;
  tx_udp_msg[id].msg_crc = crc32((uint8_t*)&tx_udp_msg[id]+4,(UDP_HEADER_SIZE-4) + tx_udp_msg[id].msg_len,UDP_MESSAGE_SIZE); 
  
  pbuf_free(pout[id]);
  pout[id] = pbuf_alloc(PBUF_TRANSPORT,tx_udp_msg_size,PBUF_RAM);
  
  pout[id]->tot_len = len + UDP_HEADER_SIZE;
     
  if(pbuf_take(pout[id],(uint8_t*)&tx_udp_msg[id],tx_udp_msg[id].msg_len + UDP_HEADER_SIZE) == ERR_OK)
  {
    struct ip_addr tx;
    IP4_ADDR(&tx,192,9,206,204);
    pudp_pcb[id]->remote_ip = tx;
    pudp_pcb[id]->remote_port = 456;
    
     if(udp_sendto(pudp_pcb[id],pout[id],&eth_ini_dat.addr[id],eth_ini_dat.UDP_TX_PORT[id]) != ERR_OK) 
     {
        rew_status[id] = 2;
     }
  }
  else rew_status[id] = 1;
}
static err_t _ieee802154_link_output(struct netif *netif, struct pbuf *p)
{
    LWIP_ASSERT("p->next == NULL", p->next == NULL);
    netdev2_t *netdev = (netdev2_t *)netif->state;
    struct iovec pkt = {
        .iov_base = p->payload,
        .iov_len = (p->len - IEEE802154_FCS_LEN),   /* FCS is written by driver */
    };

    return (netdev->driver->send(netdev, &pkt, 1) > 0) ? ERR_OK : ERR_BUF;
}
#endif

static struct pbuf *_get_recv_pkt(netdev2_t *dev)
{
    int len = dev->driver->recv(dev, _tmp_buf, sizeof(_tmp_buf), NULL);

    assert(((unsigned)len) <= UINT16_MAX);
    struct pbuf *p = pbuf_alloc(PBUF_RAW, (u16_t)len, PBUF_POOL);

    if (p == NULL) {
        DEBUG("lwip_netdev2: can not allocate in pbuf\n");
        return NULL;
    }
    pbuf_take(p, _tmp_buf, len);
    return p;
}
Example #6
0
/**
  * @brief  sends a TFTP message
  * @param  upcb: pointer on a udp pcb
  * @param  to_ip: pointer on remote IP address
  * @param  to_port: pointer on remote port  
  * @param buf: pointer on buffer where to create the message  
  * @param err: error code of type tftp_errorcode
  * @retval error code
  */
err_t tftp_send_message(struct udp_pcb *upcb, struct ip_addr *to_ip, unsigned short to_port, char *buf, unsigned short buflen)
{
  err_t err;
  struct pbuf *pkt_buf; /* Chain of pbuf's to be sent */


  /* PBUF_TRANSPORT - specifies the transport layer */
  pkt_buf = pbuf_alloc(PBUF_TRANSPORT, buflen, PBUF_POOL);

  if (!pkt_buf)      /*if the packet pbuf == NULL exit and end transmission */
    return ERR_MEM;

  /* Copy the original data buffer over to the packet buffer's payload */
  // memcpy(pkt_buf->payload, buf, buflen);
  // change use this to prevent error
  pbuf_take(pkt_buf, buf, buflen);

  /* Sending packet by UDP protocol */
  err = udp_sendto(upcb, pkt_buf, to_ip, to_port);

  /* free the buffer pbuf */
  pbuf_free(pkt_buf);

  return err;
}
Example #7
0
void provSendPacket(const void* buffer, int len)
{
	struct pbuf* p;
	p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
	pbuf_take(p, buffer, len);
	udp_send(eth_udppcb, p);
	pbuf_free(p);
}
Example #8
0
/**
  * @brief Function called when TCP connection established
  * @param tpcb: pointer on the connection contol block
  * @param err: when connection correctly established err should be ERR_OK 
  * @retval err_t: returned error 
  */
static err_t tcp_echoclient_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
{
  struct echoclient *es = NULL;
  
  if (err == ERR_OK)   
  {
    /* allocate structure es to maintain tcp connection informations */
    es = (struct echoclient *)mem_malloc(sizeof(struct echoclient));
  
    if (es != NULL)
    {
      es->state = ES_CONNECTED;
      es->pcb = tpcb;
      
      sprintf((char*)data, "sending tcp client message %d", (int)message_count);
        
      /* allocate pbuf */
      es->p_tx = pbuf_alloc(PBUF_TRANSPORT, strlen((char*)data) , PBUF_POOL);
         
      if (es->p_tx)
      {       
        /* copy data to pbuf */
        pbuf_take(es->p_tx, (char*)data, strlen((char*)data));
        
        /* pass newly allocated es structure as argument to tpcb */
        tcp_arg(tpcb, es);
  
        /* initialize LwIP tcp_recv callback function */ 
        tcp_recv(tpcb, tcp_echoclient_recv);
  
        /* initialize LwIP tcp_sent callback function */
        tcp_sent(tpcb, tcp_echoclient_sent);
  
        /* initialize LwIP tcp_poll callback function */
        tcp_poll(tpcb, tcp_echoclient_poll, 1);
    
        /* send data */
        tcp_echoclient_send(tpcb,es);
        
        return ERR_OK;
      }
    }
    else
    {
      /* close connection */
      tcp_echoclient_connection_close(tpcb, es);
      
      /* return memory allocation error */
      return ERR_MEM;  
    }
  }
  else
  {
    /* close connection */
    tcp_echoclient_connection_close(tpcb, es);
  }
  return err;
}
static VOID _DHCPOfferGenAndSend(PDHCP_CLIENT pClient, INT8U * pClientMacAddr, INT32U Xid)
{
	INT32U Len;
	INT8U * Body;
	PDHCP_MSG pDhcpMsg;
	struct pbuf * pDhcpBuf;

	pDhcpBuf = pbuf_alloc(PBUF_TRANSPORT, sizeof(DHCP_MSG), PBUF_RAM);
	if(pDhcpBuf == NULL)
	{
		return;
	}
	
	pDhcpMsg = &DhcpMsg;
	memset(pDhcpMsg, 0, sizeof(*pDhcpMsg));

	/* Initialize the DHCP message header. */
	pDhcpMsg->Op = DHCP_OP_REPLY;
	pDhcpMsg->HType = DHCP_HWTYPE_ETHERNET;
	pDhcpMsg->HLen = 6;
	pDhcpMsg->Xid = htonl(Xid);
	pDhcpMsg->Yiaddr = pClient->IpAddr.addr;
	pDhcpMsg->Siaddr = 0;
	NST_MOVE_MEM(pDhcpMsg->Chaddr, pClientMacAddr, 6);
	pDhcpMsg->Magic = htonl(DHCP_MAGIC);

	Len = 240;
	Body = &pDhcpMsg->Options[0];

	/* Set the message type. */
	DHCP_SET_OPTION_MSG_TYPE(Body, DHCP_MSG_OFFER, Len);

	/* Set the lease time. */
	DHCP_SET_OPTION_LEASE_TIME(Body, DHCP_DEFAULT_LEASE_TIME, Len);

	/* Set the server's ip address */
	DHCP_SET_OPTION_SERVER_ID(Body, DhcpServer.ServerIpAddr.addr, Len);

	/* Set the subnet mask. */
	DHCP_SET_OPTION_SUBNET_MASK(Body, DhcpServer.SubnetMask.addr, Len);

	/* Set the default gatway's ip address. */
	DHCP_SET_OPTION_GW(Body, DhcpServer.GateWay.addr, Len);

	/* Set the dns server's ip address. */
	DHCP_SET_OPTION_DNS(Body, DhcpServer.Dns1.addr, Len);

	DHCP_SET_OPTION_END(Body, Len);

	pbuf_take(pDhcpBuf, (const VOID *)pDhcpMsg, Len);
	pbuf_realloc(pDhcpBuf, Len);

	/* Send broadcast to the DHCP client. */
	udp_sendto(DhcpServer.Socket, pDhcpBuf, IP_ADDR_BROADCAST, DHCP_CLIENT_UDP_PORT);
	pbuf_free(pDhcpBuf);
}
Example #10
0
static ssize_t netSend(const octet_t *buf, int16_t  length, TimeInternal *time, const int32_t * addr, struct udp_pcb * pcb)
{
	err_t result;
	struct pbuf * p;

	/* Allocate the tx pbuf based on the current size. */
	p = pbuf_alloc(PBUF_TRANSPORT, length, PBUF_RAM);
	if (NULL == p)
	{
		ERROR("netSend: Failed to allocate Tx Buffer\n");
		goto fail01;
	}

	/* Copy the incoming data into the pbuf payload. */
	result = pbuf_take(p, buf, length);
	if (ERR_OK != result)
	{
		ERROR("netSend: Failed to copy data to Pbuf (%d)\n", result);
		goto fail02;
	}

	/* send the buffer. */
	result = udp_sendto(pcb, p, (void *)addr, pcb->local_port);
	if (ERR_OK != result)
	{
		ERROR("netSend: Failed to send data (%d)\n", result);
		goto fail02;
	}

	if (time != NULL)
	{
#if LWIP_PTP
		time->seconds = p->time_sec;
		time->nanoseconds = p->time_nsec;
#else
		/* TODO: use of loopback mode */
		/*
		time->seconds = 0;
		time->nanoseconds = 0;
		*/
		getTime(time);
#endif
		DBGV("netSend: %d sec %d nsec\n", time->seconds, time->nanoseconds);
	} else {
		DBGV("netSend\n");
	}


fail02:
	pbuf_free(p);

fail01:
	return length;

	/*  return (0 == result) ? length : 0; */
}
Example #11
0
/**
  * @brief Function called when TCP connection established
  */
static err_t tcp_echoclient_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
	uint16_t length;
	TM_TCPCLIENT_t* client;
	/* Client is passed as arguments */
	client = (TM_TCPCLIENT_t *)arg;
	if (err == ERR_OK) {
		/* We are connected */
		client->state = CLIENT_CONNECTED;

		/* Get HTTP request from user */
		length = TM_ETHERNETCLIENT_CreateHeadersCallback(client, (char *)data, sizeof(data));
		
		/* Check if length = 0 */
		if (length == 0) {
			/* Close connection */
			tcp_echoclient_connection_close(client, 1);
			
			/* Return */
			return ERR_CONN;
		}

		/* Allocate pbuf */
		client->p_tx = pbuf_alloc(PBUF_TRANSPORT, strlen((char *)data), PBUF_POOL);

		/* If we have memory for buffer */
		if (client->p_tx) {
			/* Call user function */
			TM_ETHERNETCLIENT_ConnectedCallback(client);
			
			/* copy data to pbuf */
			pbuf_take(client->p_tx, (char *)data, length);

			/* initialize LwIP tcp_recv callback function */ 
			tcp_recv(client->pcb, tcp_echoclient_recv);

			/* initialize LwIP tcp_sent callback function */
			tcp_sent(client->pcb, tcp_echoclient_sent);

			/* initialize LwIP tcp_poll callback function */
			tcp_poll(client->pcb, tcp_echoclient_poll, 1);

			/* Set new error handler */
			tcp_err(client->pcb, tcp_echoclient_error);
			
			/* send data */
			tcp_echoclient_send(client);
			/* Return OK */
			TM_Client[0] = client[0];
			return ERR_OK;
		}
	} else {
		/* close connection */
		tcp_echoclient_connection_close(client, 0);
	}
	return err;
}
Example #12
0
//用于发送数据,es由connect接口获取
void tcp_send_data(char *data,int len)
{
	if(g_es!=NULL)
	{
		g_es->p_tx = pbuf_alloc(PBUF_TRANSPORT, len , PBUF_POOL);
		pbuf_take(g_es->p_tx, data, len);
		tcp_echoclient_send(g_es->pcb,g_es);
	}
	else
		printf("need connect first\n");
}
STATIC void wiznet5k_lwip_poll(void *self_in, struct netif *netif) {
    wiznet5k_obj_t *self = self_in;
    uint16_t len;
    while ((len = wiznet5k_recv_ethernet(self)) > 0) {
        struct pbuf *p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
        if (p != NULL) {
            pbuf_take(p, self->eth_frame, len);
            if (self->netif.input(p, &self->netif) != ERR_OK) {
                pbuf_free(p);
            }
        }
    }
}
Example #14
0
void trans_Control(void* buf, size_t len) {
	struct pbuf *p;
	p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_POOL);
	if (p != NULL) {
		pbuf_take(p, buf, len);
    /* send udp data */
		udp_connect(upcb_c, &BagAddr.BagIPaddr, BagAddr.port_c);
    udp_send(upcb_c, p);  
		udp_disconnect(upcb_c);
    /* free pbuf */
    pbuf_free(p);
  }
	return;
}
Example #15
0
//------------------------------------------------------------------------------------------------------------------------------------------------------
bool TCPSocket_Send(const char *buffer, uint length)
{
    if (pcbClient)
    {
        struct pbuf *tcpBuffer = pbuf_alloc(PBUF_RAW, (u16_t)length, PBUF_POOL);
        tcpBuffer->flags = 1;
        pbuf_take(tcpBuffer, buffer, (u16_t)length);
        struct State *ss = (struct State*)mem_malloc(sizeof(struct State));
        ss->p = tcpBuffer;
        Send(pcbClient, ss);
        mem_free(ss);
    }
    return pcbClient != 0;
}
Example #16
0
void 
	udp_echoserver_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port) {
		char c[64];
		union {int i;char c[4];} ipaddr;
		ipaddr.i=(int)netif_default->ip_addr.addr;
		pbuf_free(p); 
 		sprintf(c,"LwIP %d.%d.%d.%d",ipaddr.c[0],ipaddr.c[1],ipaddr.c[2],ipaddr.c[3]);
		p = pbuf_alloc(PBUF_TRANSPORT, strlen(c) , PBUF_POOL);
		pbuf_take(p,c,strlen(c));
		udp_connect(upcb, addr, port);
		udp_send(upcb, p);
		udp_disconnect(upcb);
		pbuf_free(p); 
 
}
/******************************************************************************
 * Передача данных из буфера b.spi_receive по UDP
 ******************************************************************************/
void ICACHE_FLASH_ATTR user_tx (void)
{
	struct pbuf *z;

	z = pbuf_alloc (PBUF_TRANSPORT, sizeof (b.spi_receive), PBUF_RAM);
	if (z != NULL)
	{
		err_t err = pbuf_take (z, b.spi_receive, sizeof (b.spi_receive));
		if (err == ERR_OK)
		{
			udp_sendto (g.user_pcbv, z, &g.remote_ip, g.remote_port);
		}
		pbuf_free (z);
	}
}
Example #18
0
//------------------------------------------------------------------------------------------------------------------------------------------------------
void SendAnswer(void *_arg, struct tcp_pcb *_tpcb)
{
    static const char policy[] = "<?xml version=\"1.0\"?>"                                                  \
        "<!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">"   \
        "<cross-domain-policy>"                                                                             \
        "<allow-access-from domain=\"*\" to-ports=\"9999\" />"                                                 \
        "</cross-domain-policy>"                                                                            \
        "\0";
    struct pbuf *tcpBuffer = pbuf_alloc(PBUF_RAW, (u16_t)strlen(policy), PBUF_POOL);
    tcpBuffer->flags = 1;
    pbuf_take(tcpBuffer, policy, (u16_t)strlen(policy));
    struct State *s = (struct State *)_arg;
    s->p = tcpBuffer;
    Send(_tpcb, s);
}
Example #19
0
struct pbuf * allocate_and_cpy_buf_2_pbuf (uint8_t *buf, int len)
{
    struct pbuf        *p = NULL;

    p = pbuf_alloc (PBUF_RAW, len, PBUF_POOL);

    if (p != NULL)
    {
	if (pbuf_take (p, buf, len) != ERR_OK) {
		pbuf_free (p);
		return NULL;	
	}
    }
    return p;
}
Example #20
0
static void pcap_thread_entry(void* parameter)
{
    pcap_if_t *netif;
    pcap_t *tap;
    char errbuf[PCAP_ERRBUF_SIZE];
    struct pcap_pkthdr *header;
    const u_char *pkt_data;
    int res;

    netif = (pcap_if_t *) parameter;

    /* Open the adapter */
    if ((tap = pcap_open_live(netif->name,
        65536, // portion of the packet to capture. 
        1,     // promiscuous mode (nonzero means promiscuous)
        1,     // read timeout, 0 blocked, -1 no timeout
        errbuf )) == NULL)
    {
        rt_kprintf("Unable to open the adapter. %s is not supported by WinPcap\n", netif->name);
        return;
    }

    NETIF_PCAP(&pcap_netif_device) = tap;

    /* Read the packets */
    while (1)
    {
        struct eth_device* eth;
        struct pbuf *p;

        rt_enter_critical();
        res = pcap_next_ex(tap, &header, &pkt_data);
        rt_exit_critical();

        if (res == 0) continue;

        eth = (struct eth_device*) &pcap_netif_device;

        p = pbuf_alloc(PBUF_LINK, header->len, PBUF_RAM);
        pbuf_take(p, pkt_data, header->len);
        
        /* send to packet mailbox */
        rt_mb_send_wait(packet_mb, (rt_uint32_t)p, RT_WAITING_FOREVER);
        /* notify eth rx thread to receive packet */
        eth_device_ready(eth);
    }
}
Example #21
0
static rt_size_t mrvl_wlan_write(rt_device_t dev, rt_off_t pos,
	const void *buffer, rt_size_t size)
{
	struct eth_device* eth;
	struct pbuf* p;

	eth = (struct eth_device*)dev;
	RT_ASSERT(eth != RT_NULL);

	p = pbuf_alloc(PBUF_LINK, size, PBUF_RAM);
	if (p == RT_NULL) return 0;

	pbuf_take(p, buffer, size);

	eth->netif->linkoutput(eth->netif, p);

	return size;
}
Example #22
0
void trans_Data(short* buf_ch1, short* buf_ch2, unsigned short num) {
	int i = 0;
	struct pbuf *p;
	for(; i < num; i++) {
		memcpy(&packData.sampl[4 * i], &buf_ch1[2 * i], 2 * sizeof(short));
		memcpy(&packData.sampl[4 * i + 2], &buf_ch2[2 * i], 2 * sizeof(short));
	}
	p = pbuf_alloc(PBUF_TRANSPORT, sizeof(packData), PBUF_POOL);
	if (p != NULL) {
		pbuf_take(p, &packData, sizeof(packData));
    /* send udp data */
		udp_connect(upcb_d, &BagAddr.BagIPaddr, BagAddr.port_d);
    udp_send(upcb_d, p);  
		udp_disconnect(upcb_d);
    /* free pbuf */
    pbuf_free(p);
  }
}
void eth_mac_irq()
{
  /* Service MAC IRQ here */

  /* Allocate pbuf from pool (avoid using heap in interrupts) */
  struct pbuf* p = pbuf_alloc(PBUF_RAW, eth_data_count, PBUF_POOL);

  if(p != NULL) {
    /* Copy ethernet frame into pbuf */
    pbuf_take(p, eth_data, eth_data_count);

    /* Put in a queue which is processed in main loop */
    if(!queue_try_put(&queue, p)) {
      /* queue is full -> packet loss */
      pbuf_free(p);
    }
  }
}
BST_UINT32 BST_PAL_NetIpStackInput(
    const BST_UINT32   const ulLength,
    const BST_UINT8  * const pucData,
    const TTF_MEM_ST * const pstPktItem )
{
    BST_NET_PBUF_T                 *pbuf;
    BST_NETIF_T                    *pstNetif;
    BST_INT32                       lRtnVal;
    BST_UINT16                      usPortNum;

    if( 0 == ulLength )
    {
        return 0x00U;
    }
    if( BST_NULL_PTR == pucData )
    {
        return 0x00U;
    }
    if( BST_NULL_PTR == pstPktItem )
    {
        return 0x00U;
    }

    pbuf                            = pbuf_alloc( PBUF_LINK, (BST_UINT16)ulLength, PBUF_RAM );
    BST_ASSERT_NULL_RTN( pbuf, 0x00U );
    usPortNum                       = BST_IP_ApiRegistPacket(
                                    ( BST_IP_PKT_ID_T )pbuf, (BST_VOID*)pstPktItem );
    BST_DBG_UPDATE_RX_FLOW( usPortNum, ulLength );
    ( BST_VOID )pbuf_take( pbuf, pucData, (BST_UINT16)ulLength );
    pstNetif                        = BST_DRV_NetGetMainIfHandle();
    lRtnVal                         = pstNetif->input( pbuf, pstNetif );
    if ( lRtnVal < 0 )
    {
        BST_RLS_LOG1( "BST_PAL_NetIpStackInput, Write to Lwip Error=%s", lRtnVal );
        BST_IP_ApiUnRegistPacket( ( BST_IP_PKT_ID_T )pbuf );
        BST_PAL_NetApiPacketResume( (BST_VOID*)pstPktItem );
        pbuf_free( pbuf );
        return 0x00U;
    }
    else
    {
        return ulLength;
    }
}
Example #25
0
void 	_tcp_flush(void *v) {
		TCP *es=v;
		if(es->pcb->snd_queuelen > TCP_SND_QUEUELEN-1)				  	// !!!! pred vpisom preveri, ce ni queue ze poln  
			tcp_output(es->pcb);																		// kratkih blokov (Nagle algoritem bo javil MEM error....)
		else if(es->io) {																					// sicer nadaljevanje...
			char	c[256];
			int k,n=0;
			if(es->rx) {																						// a je kaj za sprejem ???
				struct pbuf	*q;
				for(q=es->rx; q != NULL; q=es->rx) {									// preskanirat je treba celo verigo pbuf 
					n+=k=_buffer_push(es->io[0],q->payload, q->len);		// push v io
					if(k < q->len) {
						pbuf_header(q,-k);																// skrajsaj header
						break;
					}
					es->rx = es->rx->next;
					if (es->rx != NULL)
						pbuf_ref(es->rx);
					pbuf_free(q);
				}
				tcp_recved(es->pcb,n);																// free raw input
			}																														
																										
			n=_buffer_count(es->io[1]);																// koliko je v buferju za izpis ...
			if(n > tcp_sndbuf(es->pcb))															// ne sme biti vec kot je placa na raw output ....
				n = tcp_sndbuf(es->pcb);	
			if(n > 256)																							// ne sme bit vec kot 1024.... glej c[1024]
				n=256;
			if(n) {																									// ce je sploh kej ...
				struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, n , PBUF_POOL);
				if(p != NULL) {																				// ce je alokacija pbuf uspela
					n=_buffer_pull(es->io[1],c,n);											// kopiraj pull vsebino v vmesni buffer
					pbuf_take(p,c,n);																		// formiraj pbuf
					if(es->tx)																					// verizi, ce je se kaj od prej..
						pbuf_chain(es->tx,p);															// 
					else
						es->tx = p;																				// sicer nastavi nov pointer 
					tcp_sent(es->pcb, TCP_sent);												// set callback & send..
					TCP_send(es->pcb, es);					
					tcp_output(es->pcb);							
				}
			}
		}
	}
Example #26
0
/* reception packet. */
struct pbuf *lpc_emac_rx(rt_device_t dev)
{
    struct pbuf *p;
    rt_uint32_t size;
    rt_uint32_t Index;

    /* init p pointer */
    p = RT_NULL;

    /* lock EMAC device */
    rt_sem_take(&sem_lock, RT_WAITING_FOREVER);

    Index = LPC_EMAC->RxConsumeIndex;
    if (Index != LPC_EMAC->RxProduceIndex)
    {
        size = (RX_STAT_INFO(Index) & 0x7ff) + 1;
        if (size > ETH_FRAG_SIZE) size = ETH_FRAG_SIZE;

        /* allocate buffer */
        p = pbuf_alloc(PBUF_LINK, size, PBUF_RAM);
        if (p != RT_NULL)
        {
            pbuf_take(p, (rt_uint8_t *)RX_BUF(Index), size);
        }

        /* move Index to the next */
        if (++Index > LPC_EMAC->RxDescriptorNumber)
            Index = 0;

        /* set consume index */
        LPC_EMAC->RxConsumeIndex = Index;
    }
    else
    {
        /* Enable RxDone interrupt */
        LPC_EMAC->IntEnable = INT_RX_DONE | INT_TX_DONE;
    }

    /* unlock EMAC device */
    rt_sem_release(&sem_lock);

    return p;
}
Example #27
0
socket_error_t lwipv4_socket_send(struct socket *socket, const void * buf, const size_t len)
{
    err_t err = ERR_VAL;
    switch(socket->family) {
    case SOCKET_DGRAM: {
        struct pbuf *pb = pbuf_alloc(PBUF_TRANSPORT,len,PBUF_RAM);
        socket_event_t e;
        socket_api_handler_t handler = socket->handler;
        err = pbuf_take(pb, buf, len);
        if (err != ERR_OK)
            break;
        err = udp_send(socket->impl, pb);
        pbuf_free(pb);
        if (err != ERR_OK)
            break;
        //Notify the application that the transfer is queued at the MAC layer
        e.event = SOCKET_EVENT_TX_DONE;
        e.sock = socket;
        e.i.t.sentbytes = len;
        socket->event = &e;
        handler();
        socket->event = NULL;
        break;
    }
    case SOCKET_STREAM:{
            struct tcp_pcb* pcb = socket->impl;
            err = tcp_write(pcb,buf,len,TCP_WRITE_FLAG_COPY);
            if (tcp_nagle_disabled(pcb)) {

// TODO: Remove when sal-driver-lwip-k64f-eth/#8 is fixed
#ifdef YOTTA_SAL_DRIVER_LWIP_K64F_ETH_VERSION_STRING
                emac_tcp_push_pcb = (struct pbuf * volatile)pcb;
                vIRQ_SetPendingIRQ(ENET_Receive_IRQn);
#else
                tcp_output(pcb);
#endif
            }
            break;
        }
    }
    return lwipv4_socket_error_remap(err);
}
Example #28
0
size_t EthernetUDP::write(const uint8_t *buffer, size_t size)
{
	uint16_t avail = _sendTop->tot_len - _write;

	/* If there is no more space available
	 * then return immediately */
	if(avail == 0)
		return 0;

	/* If size to send is larger than is available,
	 * then only send up to the space available */
	if(size > avail)
		size = avail;

	/* Copy buffer into the pbuf */
	pbuf_take(_sendTop, buffer, size);

	_write += size;

	return size;
}
Example #29
0
/**
  * @brief This function is called when an UDP datagrm has been received on the port UDP_PORT.
  * @param arg user supplied argument (udp_pcb.recv_arg)
  * @param pcb the udp_pcb which received data
  * @param p the packet buffer that was received
  * @param addr the remote IP address from which the packet was received
  * @param port the remote port from which the packet was received
  * @retval None
  */
void udp_echoclient_send(void)
{
  struct pbuf *p;
  
  sprintf((char*)data, "sending udp client message %d", (int)message_count);
  
  /* allocate pbuf from pool*/
  p = pbuf_alloc(PBUF_TRANSPORT,strlen((char*)data), PBUF_POOL);
  
  if (p != NULL)
  {
    /* copy data to pbuf */
    pbuf_take(p, (char*)data, strlen((char*)data));
    
    /* send udp data */
    udp_send(upcb, p); 
    
    /* free pbuf */
    pbuf_free(p);
  }
}
Example #30
0
void ReSendUdpData(void)
{
  for(int i = 0; i < MAX_UDP_SOCK; i++)
  {
    if(rew_status[i] == 1)
    {
      pout[i]->tot_len = tx_udp_msg[i].msg_len + UDP_HEADER_SIZE;
    
      if(pbuf_take(pout[i],(uint8_t*)&tx_udp_msg[i],tx_udp_msg[i].msg_len + UDP_HEADER_SIZE) == ERR_OK)
      {
      //  if(udp_sendto(pudp_pcb[i],pout[i],&re_addr[i],re_port[i]) != ERR_OK)  rew_status[i] = 2;
        if(udp_sendto(pudp_pcb[i],pout[i],&eth_ini_dat.addr[i],eth_ini_dat.UDP_TX_PORT[i]) != ERR_OK) rew_status[i] = 2;
      }
    }
    else if(rew_status[i] == 2)
    {
    //  if(udp_sendto(pudp_pcb[i],pout[i],&re_addr[i],re_port[i]) == ERR_OK) rew_status[i] = 0;
      if(udp_sendto(pudp_pcb[i],pout[i],&eth_ini_dat.addr[i],eth_ini_dat.UDP_TX_PORT[i]) != ERR_OK) rew_status[i] = 0;
    }
  }
}