Mbed library for ENC28J60 Ethernet modules. Full support for TCP/IP and UDP Server, Client and HTTP server (webserver). DHCP and DNS is included.

Dependents:   mBuino_ENC28_MQTT Nucleo_Web_ENC28J60 Nucleo_Web_ENC28J60_ADC Serial_over_Ethernet ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TcpClient.h Source File

TcpClient.h

00001 /*
00002  UIPClient.h - 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 #ifndef TCPCLIENT_h
00020 #define TCPCLIENT_h
00021 
00022 #include "mbed.h"
00023 #include "IpAddress.h"
00024 #include "utility/MemPool.h"
00025 
00026 extern "C"
00027 {
00028 #include "utility/uip.h"
00029 }
00030 #define UIP_SOCKET_DATALEN  UIP_TCP_MSS
00031 //#define UIP_SOCKET_NUMPACKETS UIP_RECEIVE_WINDOW/UIP_TCP_MSS+1
00032 
00033 #ifndef UIP_SOCKET_NUMPACKETS
00034 #define UIP_SOCKET_NUMPACKETS   5
00035 #endif
00036 #define UIP_CLIENT_CONNECTED    0x10
00037 #define UIP_CLIENT_CLOSE        0x20
00038 #define UIP_CLIENT_REMOTECLOSED 0x40
00039 #define UIP_CLIENT_RESTART      0x80
00040 #define UIP_CLIENT_STATEFLAGS   (UIP_CLIENT_CONNECTED | UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED | UIP_CLIENT_RESTART)
00041 #define UIP_CLIENT_SOCKETS      ~UIP_CLIENT_STATEFLAGS
00042 
00043 typedef uint8_t uip_socket_ptr;
00044 
00045 typedef struct
00046 {
00047     uint8_t         state;
00048     memhandle       packets_in[UIP_SOCKET_NUMPACKETS];
00049     uint16_t        lport;      /**< The local TCP port, in network byte order. */
00050 } uip_userdata_closed_t;
00051 
00052 typedef struct
00053 {
00054     Timer           pollTimer;
00055     uint8_t         state;
00056     uip_ipaddr_t    ripaddr;    /**< The IP address of the remote host. */
00057     memhandle       packets_in[UIP_SOCKET_NUMPACKETS];
00058     memhandle       packets_out[UIP_SOCKET_NUMPACKETS];
00059     memaddress      out_pos;
00060 } uip_userdata_t;
00061 
00062 class UipEthernet;
00063 
00064 class TcpClient
00065 {
00066 public:
00067     TcpClient();
00068     TcpClient(uip_userdata_t* conn_data);
00069     virtual ~TcpClient() {}
00070 
00071     int                     open(UipEthernet* ethernet);
00072     int                     connect(IpAddress ip, uint16_t port);
00073     int                     connect(const char* host, uint16_t port);
00074     int                     recv(uint8_t* buf, size_t size);
00075     void                    stop();
00076     uint8_t                 connected();
00077     void                    setInstance(TcpClient* client);
00078     operator                bool();
00079     virtual bool operator   ==(const TcpClient& );
00080     virtual bool operator   !=(const TcpClient& rhs)    { return !this->operator ==(rhs); }
00081     int                     send(uint8_t);
00082     int                     send(const uint8_t* buf, size_t size);
00083     size_t                  available();
00084     int                     recv();
00085     int                     peek();
00086     void                    flush();
00087     const char*             getpeername();
00088     IpAddress               getRemoteIp();
00089     void                    close();
00090 
00091     static uip_userdata_t   all_data[UIP_CONNS];
00092     static int             _write(uip_userdata_t* , const uint8_t* buf, size_t size);
00093 
00094 protected:
00095     uint8_t*                rawIPAddress(IpAddress& addr)   { return addr.rawAddress(); }
00096 
00097 private:
00098     uip_userdata_t*         data;
00099     TcpClient*              _instance;
00100     static uip_userdata_t*  _allocateData();
00101     static size_t           _available(uip_userdata_t* );
00102     static uint8_t          _currentBlock(memhandle* blocks);
00103     static void             _eatBlock(memhandle* blocks);
00104     static void             _flushBlocks(memhandle* blocks);
00105 
00106 #ifdef UIPETHERNET_DEBUG_CLIENT
00107     static void             _dumpAllData();
00108 #endif
00109     friend class            UipEthernet;
00110     friend class            TcpServer;
00111     friend void             uipclient_appcall();
00112 };
00113 #endif