Xbee test 2

Dependencies:   XBee mbed

Committer:
takashiyamanoue
Date:
Sat Jul 21 04:08:27 2012 +0000
Revision:
0:ffac63d6a7f0
xbee test 2

Who changed what in which revision?

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