mbed OS5

Fork of UIPEthernet by Zoltan Hudak

Committer:
hudakz
Date:
Sun Mar 08 20:26:56 2015 +0000
Revision:
4:d774541a34da
Parent:
2:049ce85163c5
Child:
8:4acb22344932
Version 1.09 (fixed leaking client-data caused by race-condition on remote close)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:5350a66d5279 1 /*
hudakz 0:5350a66d5279 2 UIPClient.h - Arduino implementation of a uIP wrapper class.
hudakz 0:5350a66d5279 3 Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
hudakz 0:5350a66d5279 4 All rights reserved.
hudakz 0:5350a66d5279 5
hudakz 0:5350a66d5279 6 This program is free software: you can redistribute it and/or modify
hudakz 0:5350a66d5279 7 it under the terms of the GNU General Public License as published by
hudakz 0:5350a66d5279 8 the Free Software Foundation, either version 3 of the License, or
hudakz 0:5350a66d5279 9 (at your option) any later version.
hudakz 0:5350a66d5279 10
hudakz 0:5350a66d5279 11 This program is distributed in the hope that it will be useful,
hudakz 0:5350a66d5279 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
hudakz 0:5350a66d5279 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hudakz 0:5350a66d5279 14 GNU General Public License for more details.
hudakz 0:5350a66d5279 15
hudakz 0:5350a66d5279 16 You should have received a copy of the GNU General Public License
hudakz 0:5350a66d5279 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
hudakz 0:5350a66d5279 18 */
hudakz 4:d774541a34da 19
hudakz 0:5350a66d5279 20 #ifndef UIPCLIENT_H
hudakz 4:d774541a34da 21 #define UIPCLIENT_H
hudakz 0:5350a66d5279 22
hudakz 4:d774541a34da 23 #include "ethernet_comp.h"
hudakz 4:d774541a34da 24 //#include "Print.h"
hudakz 4:d774541a34da 25 #include "Client.h"
hudakz 4:d774541a34da 26 #include "utility/mempool.h"
hudakz 4:d774541a34da 27
hudakz 4:d774541a34da 28 extern "C" {
hudakz 4:d774541a34da 29 #include "utility/uip.h"
hudakz 4:d774541a34da 30 }
hudakz 0:5350a66d5279 31
hudakz 4:d774541a34da 32 #define UIP_SOCKET_DATALEN UIP_TCP_MSS
hudakz 0:5350a66d5279 33 //#define UIP_SOCKET_NUMPACKETS UIP_RECEIVE_WINDOW/UIP_TCP_MSS+1
hudakz 4:d774541a34da 34 #ifndef UIP_SOCKET_NUMPACKETS
hudakz 4:d774541a34da 35 #define UIP_SOCKET_NUMPACKETS 5
hudakz 4:d774541a34da 36 #endif
hudakz 0:5350a66d5279 37
hudakz 4:d774541a34da 38 #define UIP_CLIENT_CONNECTED 0x10
hudakz 4:d774541a34da 39 #define UIP_CLIENT_CLOSE 0x20
hudakz 4:d774541a34da 40 #define UIP_CLIENT_REMOTECLOSED 0x40
hudakz 4:d774541a34da 41 #define UIP_CLIENT_RESTART 0x80
hudakz 4:d774541a34da 42 #define UIP_CLIENT_STATEFLAGS (UIP_CLIENT_CONNECTED | UIP_CLIENT_CLOSE | UIP_CLIENT_REMOTECLOSED | UIP_CLIENT_RESTART)
hudakz 4:d774541a34da 43 #define UIP_CLIENT_SOCKETS ~UIP_CLIENT_STATEFLAGS
hudakz 0:5350a66d5279 44
hudakz 0:5350a66d5279 45 typedef uint8_t uip_socket_ptr;
hudakz 0:5350a66d5279 46
hudakz 4:d774541a34da 47 typedef struct {
hudakz 4:d774541a34da 48 uint8_t state;
hudakz 4:d774541a34da 49 memhandle packets_in[UIP_SOCKET_NUMPACKETS];
hudakz 4:d774541a34da 50 uint16_t lport; /**< The local TCP port, in network byte order. */
hudakz 0:5350a66d5279 51 } uip_userdata_closed_t;
hudakz 0:5350a66d5279 52
hudakz 4:d774541a34da 53 typedef struct {
hudakz 4:d774541a34da 54 uint8_t state;
hudakz 4:d774541a34da 55 memhandle packets_in[UIP_SOCKET_NUMPACKETS];
hudakz 4:d774541a34da 56 memhandle packets_out[UIP_SOCKET_NUMPACKETS];
hudakz 4:d774541a34da 57 memaddress out_pos;
hudakz 4:d774541a34da 58 #if UIP_CLIENT_TIMER >= 0
hudakz 4:d774541a34da 59 unsigned long timer;
hudakz 4:d774541a34da 60 #endif
hudakz 0:5350a66d5279 61 } uip_userdata_t;
hudakz 0:5350a66d5279 62
hudakz 4:d774541a34da 63 class UIPClient : public Client {
hudakz 4:d774541a34da 64
hudakz 0:5350a66d5279 65 public:
hudakz 4:d774541a34da 66 UIPClient();
hudakz 4:d774541a34da 67 int connect(IPAddress ip, uint16_t port);
hudakz 4:d774541a34da 68 int connect(const char *host, uint16_t port);
hudakz 4:d774541a34da 69 int read(uint8_t *buf, size_t size);
hudakz 4:d774541a34da 70 void stop();
hudakz 4:d774541a34da 71 uint8_t connected();
hudakz 4:d774541a34da 72 operator bool();
hudakz 4:d774541a34da 73 virtual bool operator==(const EthernetClient&);
hudakz 4:d774541a34da 74 virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); };
hudakz 0:5350a66d5279 75
hudakz 4:d774541a34da 76 size_t write(uint8_t);
hudakz 4:d774541a34da 77 size_t write(const uint8_t *buf, size_t size);
hudakz 4:d774541a34da 78 int available();
hudakz 4:d774541a34da 79 int read();
hudakz 4:d774541a34da 80 int peek();
hudakz 4:d774541a34da 81 void flush();
hudakz 4:d774541a34da 82
hudakz 4:d774541a34da 83 // using Print::write;
hudakz 4:d774541a34da 84
hudakz 0:5350a66d5279 85 private:
hudakz 4:d774541a34da 86 UIPClient(struct uip_conn *_conn);
hudakz 4:d774541a34da 87 UIPClient(uip_userdata_t* conn_data);
hudakz 4:d774541a34da 88
hudakz 4:d774541a34da 89 uip_userdata_t* data;
hudakz 0:5350a66d5279 90
hudakz 4:d774541a34da 91 static uip_userdata_t all_data[UIP_CONNS];
hudakz 4:d774541a34da 92 static uip_userdata_t* _allocateData();
hudakz 0:5350a66d5279 93
hudakz 4:d774541a34da 94 static size_t _write(uip_userdata_t *,const uint8_t *buf, size_t size);
hudakz 4:d774541a34da 95 static int _available(uip_userdata_t *);
hudakz 0:5350a66d5279 96
hudakz 4:d774541a34da 97 static uint8_t _currentBlock(memhandle* blocks);
hudakz 4:d774541a34da 98 static void _eatBlock(memhandle* blocks);
hudakz 4:d774541a34da 99 static void _flushBlocks(memhandle* blocks);
hudakz 4:d774541a34da 100
hudakz 4:d774541a34da 101 #ifdef UIPETHERNET_DEBUG_CLIENT
hudakz 4:d774541a34da 102 static void _dumpAllData();
hudakz 4:d774541a34da 103 #endif
hudakz 0:5350a66d5279 104
hudakz 4:d774541a34da 105 friend class UIPEthernetClass;
hudakz 4:d774541a34da 106 friend class UIPServer;
hudakz 0:5350a66d5279 107
hudakz 4:d774541a34da 108 friend void uipclient_appcall(void);
hudakz 4:d774541a34da 109
hudakz 0:5350a66d5279 110 };
hudakz 4:d774541a34da 111
hudakz 0:5350a66d5279 112 #endif