UIPEthernet library for Arduino IDE, Eclipse with arduino plugin and MBED/SMeshStudio (AVR,STM32F,ESP8266,Intel ARC32,Nordic nRF51,Teensy boards,Realtek Ameba(RTL8195A,RTL8710)), ENC28j60 network chip. Compatible with Wiznet W5100 Ethernet library API. Compiled and tested on Nucleo-F302R8. Master repository is: https://github.com/UIPEthernet/UIPEthernet/

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UIPServer.cpp Source File

UIPServer.cpp

00001 /*
00002  UIPServer.cpp - Arduino implementation of a uIP wrapper class.
00003  Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
00004  All rights reserved.
00005 
00006  This program is free software: you can redistribute it and/or modify
00007  it under the terms of the GNU General Public License as published by
00008  the Free Software Foundation, either version 3 of the License, or
00009  (at your option) any later version.
00010 
00011  This program is distributed in the hope that it will be useful,
00012  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  GNU General Public License for more details.
00015 
00016  You should have received a copy of the GNU General Public License
00017  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018   */
00019 #include "UIPEthernet.h"
00020 #include "UIPServer.h"
00021 #include "utility/logging.h"
00022 extern "C" {
00023   #include "utility/uipopt.h"
00024 }
00025 
00026 UIPServer::UIPServer(uint16_t port) : _port(htons(port))
00027 {
00028 }
00029 
00030 UIPClient UIPServer::available()
00031 {
00032   UIPEthernetClass::tick();
00033   for ( uip_userdata_t* data = &UIPClient::all_data[0]; data < &UIPClient::all_data[UIP_CONNS]; data++ )
00034     {
00035       if (data->packets_in[0] != NOBLOCK
00036           && (((data->state & UIP_CLIENT_CONNECTED) && uip_conns[data->state & UIP_CLIENT_SOCKETS].lport ==_port)
00037               || ((data->state & UIP_CLIENT_REMOTECLOSED) && ((uip_userdata_closed_t *)data)->lport == _port)))
00038         return UIPClient(data);
00039     }
00040   return UIPClient();
00041 }
00042 
00043 void UIPServer::begin()
00044 {
00045   uip_listen(_port);
00046   UIPEthernetClass::tick();
00047 }
00048 
00049 size_t UIPServer::write(uint8_t c)
00050 {
00051   return write(&c,1);
00052 }
00053 
00054 size_t UIPServer::write(const uint8_t *buf, size_t size)
00055 {
00056   size_t ret = 0;
00057   for ( uip_userdata_t* data = &UIPClient::all_data[0]; data < &UIPClient::all_data[UIP_CONNS]; data++ )
00058     {
00059       if ((data->state & UIP_CLIENT_CONNECTED) && uip_conns[data->state & UIP_CLIENT_SOCKETS].lport ==_port)
00060         ret += UIPClient::_write(data,buf,size);
00061     }
00062   return ret;
00063 }
00064