ID the MBED processor on the network if you have more than one. View MBED name on DHCP of your Router - Example \"MBED PE\" Change your ID in Core / host.h

Committer:
JeffM
Date:
Sat Nov 06 14:11:02 2010 +0000
Revision:
0:fd3b85615428
MBED ID 1A

Who changed what in which revision?

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