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