Measure system

Dependencies:   EthernetNetIf mbed RF12B

Committer:
benecsj
Date:
Thu Mar 10 19:56:45 2011 +0000
Revision:
1:b26ab2467b1a

        

Who changed what in which revision?

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