EthernetNetIf

Dependents:   XBee_WiFi_EA_LPC4088

Committer:
hmike
Date:
Wed Nov 19 12:00:42 2014 +0000
Revision:
0:62580107d20c
EthernetNetIf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hmike 0:62580107d20c 1
hmike 0:62580107d20c 2 /*
hmike 0:62580107d20c 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
hmike 0:62580107d20c 4
hmike 0:62580107d20c 5 Permission is hereby granted, free of charge, to any person obtaining a copy
hmike 0:62580107d20c 6 of this software and associated documentation files (the "Software"), to deal
hmike 0:62580107d20c 7 in the Software without restriction, including without limitation the rights
hmike 0:62580107d20c 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hmike 0:62580107d20c 9 copies of the Software, and to permit persons to whom the Software is
hmike 0:62580107d20c 10 furnished to do so, subject to the following conditions:
hmike 0:62580107d20c 11
hmike 0:62580107d20c 12 The above copyright notice and this permission notice shall be included in
hmike 0:62580107d20c 13 all copies or substantial portions of the Software.
hmike 0:62580107d20c 14
hmike 0:62580107d20c 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hmike 0:62580107d20c 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hmike 0:62580107d20c 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hmike 0:62580107d20c 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hmike 0:62580107d20c 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hmike 0:62580107d20c 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hmike 0:62580107d20c 21 THE SOFTWARE.
hmike 0:62580107d20c 22 */
hmike 0:62580107d20c 23
hmike 0:62580107d20c 24 /** \file
hmike 0:62580107d20c 25 TCP Socket header file
hmike 0:62580107d20c 26 */
hmike 0:62580107d20c 27
hmike 0:62580107d20c 28 #ifndef TCPSOCKET_H
hmike 0:62580107d20c 29 #define TCPSOCKET_H
hmike 0:62580107d20c 30
hmike 0:62580107d20c 31 #include "core/net.h"
hmike 0:62580107d20c 32 #include "core/host.h"
hmike 0:62580107d20c 33 //Essentially it is a safe interface to NetTcpSocket
hmike 0:62580107d20c 34
hmike 0:62580107d20c 35 ///TCP Socket error codes
hmike 0:62580107d20c 36 enum TCPSocketErr
hmike 0:62580107d20c 37 {
hmike 0:62580107d20c 38 __TCPSOCKET_MIN = -0xFFFF,
hmike 0:62580107d20c 39 TCPSOCKET_SETUP, ///<TCPSocket not properly configured
hmike 0:62580107d20c 40 TCPSOCKET_TIMEOUT, ///<Connection timed out
hmike 0:62580107d20c 41 TCPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
hmike 0:62580107d20c 42 TCPSOCKET_MEM, ///<Not enough mem
hmike 0:62580107d20c 43 TCPSOCKET_INUSE, ///<Interface / Port is in use
hmike 0:62580107d20c 44 TCPSOCKET_EMPTY, ///<Connections queue is empty
hmike 0:62580107d20c 45 TCPSOCKET_RST, ///<Connection was reset by remote host
hmike 0:62580107d20c 46 //...
hmike 0:62580107d20c 47 TCPSOCKET_OK = 0 ///<Success
hmike 0:62580107d20c 48 };
hmike 0:62580107d20c 49
hmike 0:62580107d20c 50 ///TCP Socket Events
hmike 0:62580107d20c 51 enum TCPSocketEvent
hmike 0:62580107d20c 52 {
hmike 0:62580107d20c 53 TCPSOCKET_CONNECTED, ///<Connected to host
hmike 0:62580107d20c 54 TCPSOCKET_ACCEPT, ///<Client is connected, must call accept() to get a new Socket
hmike 0:62580107d20c 55 TCPSOCKET_READABLE, ///<Data in buf
hmike 0:62580107d20c 56 TCPSOCKET_WRITEABLE, ///<Can write data to buf
hmike 0:62580107d20c 57 TCPSOCKET_CONTIMEOUT, ///<Connection timed out
hmike 0:62580107d20c 58 TCPSOCKET_CONRST, ///<Connection was reset by remote host
hmike 0:62580107d20c 59 TCPSOCKET_CONABRT, ///<Connection was aborted
hmike 0:62580107d20c 60 TCPSOCKET_ERROR, ///<Unknown error
hmike 0:62580107d20c 61 TCPSOCKET_DISCONNECTED ///<Disconnected
hmike 0:62580107d20c 62 };
hmike 0:62580107d20c 63
hmike 0:62580107d20c 64 class NetTcpSocket;
hmike 0:62580107d20c 65 enum NetTcpSocketEvent;
hmike 0:62580107d20c 66
hmike 0:62580107d20c 67 ///This is a simple TCP Socket class
hmike 0:62580107d20c 68 /**
hmike 0:62580107d20c 69 This class exposes an API to deal with TCP Sockets
hmike 0:62580107d20c 70 */
hmike 0:62580107d20c 71 class TCPSocket
hmike 0:62580107d20c 72 {
hmike 0:62580107d20c 73 public:
hmike 0:62580107d20c 74 ///Creates a new socket
hmike 0:62580107d20c 75 TCPSocket();
hmike 0:62580107d20c 76 protected:
hmike 0:62580107d20c 77 TCPSocket(NetTcpSocket* pNetTcpSocket);
hmike 0:62580107d20c 78 public:
hmike 0:62580107d20c 79 ///Closes if needed and destroys the socket
hmike 0:62580107d20c 80 ~TCPSocket(); //close()
hmike 0:62580107d20c 81
hmike 0:62580107d20c 82 ///Binds the socket to (local) host
hmike 0:62580107d20c 83 TCPSocketErr bind(const Host& me);
hmike 0:62580107d20c 84
hmike 0:62580107d20c 85 ///Starts listening
hmike 0:62580107d20c 86 TCPSocketErr listen();
hmike 0:62580107d20c 87
hmike 0:62580107d20c 88 ///Connects socket to host
hmike 0:62580107d20c 89 TCPSocketErr connect(const Host& host);
hmike 0:62580107d20c 90
hmike 0:62580107d20c 91 ///Accepts connection from client and gets connected socket
hmike 0:62580107d20c 92 TCPSocketErr accept(Host* pClient, TCPSocket** ppNewTcpSocket);
hmike 0:62580107d20c 93
hmike 0:62580107d20c 94 ///Sends data
hmike 0:62580107d20c 95 /*
hmike 0:62580107d20c 96 @return a negative error code or the number of bytes transmitted
hmike 0:62580107d20c 97 */
hmike 0:62580107d20c 98 int /*if < 0 : TCPSocketErr*/ send(const char* buf, int len);
hmike 0:62580107d20c 99
hmike 0:62580107d20c 100 ///Receives data
hmike 0:62580107d20c 101 /*
hmike 0:62580107d20c 102 @return a negative error code or the number of bytes received
hmike 0:62580107d20c 103 */
hmike 0:62580107d20c 104 int /*if < 0 : TCPSocketErr*/ recv(char* buf, int len);
hmike 0:62580107d20c 105
hmike 0:62580107d20c 106 /* TODO NTH : printf / scanf helpers that call send/recv */
hmike 0:62580107d20c 107
hmike 0:62580107d20c 108 ///Closes socket
hmike 0:62580107d20c 109 TCPSocketErr close();
hmike 0:62580107d20c 110
hmike 0:62580107d20c 111 //Callbacks
hmike 0:62580107d20c 112 ///Setups callback
hmike 0:62580107d20c 113 /**
hmike 0:62580107d20c 114 @param pMethod : callback function
hmike 0:62580107d20c 115 */
hmike 0:62580107d20c 116 void setOnEvent( void (*pMethod)(TCPSocketEvent) );
hmike 0:62580107d20c 117
hmike 0:62580107d20c 118 class CDummy;
hmike 0:62580107d20c 119 ///Setups callback
hmike 0:62580107d20c 120 /**
hmike 0:62580107d20c 121 @param pItem : instance of class on which to execute the callback method
hmike 0:62580107d20c 122 @param pMethod : callback method
hmike 0:62580107d20c 123 */
hmike 0:62580107d20c 124 template<class T>
hmike 0:62580107d20c 125 void setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
hmike 0:62580107d20c 126 {
hmike 0:62580107d20c 127 m_pCbItem = (CDummy*) pItem;
hmike 0:62580107d20c 128 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
hmike 0:62580107d20c 129 }
hmike 0:62580107d20c 130
hmike 0:62580107d20c 131 ///Disables callback
hmike 0:62580107d20c 132 void resetOnEvent();
hmike 0:62580107d20c 133
hmike 0:62580107d20c 134 protected:
hmike 0:62580107d20c 135 void onNetTcpSocketEvent(NetTcpSocketEvent e);
hmike 0:62580107d20c 136 TCPSocketErr checkInst();
hmike 0:62580107d20c 137
hmike 0:62580107d20c 138 private:
hmike 0:62580107d20c 139 NetTcpSocket* m_pNetTcpSocket;
hmike 0:62580107d20c 140
hmike 0:62580107d20c 141 CDummy* m_pCbItem;
hmike 0:62580107d20c 142 void (CDummy::*m_pCbMeth)(TCPSocketEvent);
hmike 0:62580107d20c 143
hmike 0:62580107d20c 144 void (*m_pCb)(TCPSocketEvent);
hmike 0:62580107d20c 145
hmike 0:62580107d20c 146 };
hmike 0:62580107d20c 147
hmike 0:62580107d20c 148 #endif