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 #ifndef NETTCPSOCKET_H
takashiyamanoue 0:ffac63d6a7f0 25 #define NETTCPSOCKET_H
takashiyamanoue 0:ffac63d6a7f0 26
takashiyamanoue 0:ffac63d6a7f0 27 #include "net.h"
takashiyamanoue 0:ffac63d6a7f0 28 #include "host.h"
takashiyamanoue 0:ffac63d6a7f0 29
takashiyamanoue 0:ffac63d6a7f0 30 #include <queue>
takashiyamanoue 0:ffac63d6a7f0 31 using std::queue;
takashiyamanoue 0:ffac63d6a7f0 32
takashiyamanoue 0:ffac63d6a7f0 33 //Implements a Berkeley-like socket if
takashiyamanoue 0:ffac63d6a7f0 34 //Can be interfaced either to lwip or a Telit module
takashiyamanoue 0:ffac63d6a7f0 35
takashiyamanoue 0:ffac63d6a7f0 36 enum NetTcpSocketErr
takashiyamanoue 0:ffac63d6a7f0 37 {
takashiyamanoue 0:ffac63d6a7f0 38 __NETTCPSOCKET_MIN = -0xFFFF,
takashiyamanoue 0:ffac63d6a7f0 39 NETTCPSOCKET_SETUP, //NetTcpSocket not properly configured
takashiyamanoue 0:ffac63d6a7f0 40 NETTCPSOCKET_TIMEOUT,
takashiyamanoue 0:ffac63d6a7f0 41 NETTCPSOCKET_IF, //If has problems
takashiyamanoue 0:ffac63d6a7f0 42 NETTCPSOCKET_MEM, //Not enough mem
takashiyamanoue 0:ffac63d6a7f0 43 NETTCPSOCKET_INUSE, //If/Port is in use
takashiyamanoue 0:ffac63d6a7f0 44 NETTCPSOCKET_EMPTY, //Connections queue is empty
takashiyamanoue 0:ffac63d6a7f0 45 NETTCPSOCKET_RST, // Connection was reset by remote host
takashiyamanoue 0:ffac63d6a7f0 46 //...
takashiyamanoue 0:ffac63d6a7f0 47 NETTCPSOCKET_OK = 0
takashiyamanoue 0:ffac63d6a7f0 48 };
takashiyamanoue 0:ffac63d6a7f0 49
takashiyamanoue 0:ffac63d6a7f0 50 enum NetTcpSocketEvent
takashiyamanoue 0:ffac63d6a7f0 51 {
takashiyamanoue 0:ffac63d6a7f0 52 NETTCPSOCKET_CONNECTED, //Connected to host, must call accept() if we were listening
takashiyamanoue 0:ffac63d6a7f0 53 NETTCPSOCKET_ACCEPT, //Connected to client
takashiyamanoue 0:ffac63d6a7f0 54 NETTCPSOCKET_READABLE, //Data in buf
takashiyamanoue 0:ffac63d6a7f0 55 NETTCPSOCKET_WRITEABLE, //Can write data to buf
takashiyamanoue 0:ffac63d6a7f0 56 NETTCPSOCKET_CONTIMEOUT,
takashiyamanoue 0:ffac63d6a7f0 57 NETTCPSOCKET_CONRST,
takashiyamanoue 0:ffac63d6a7f0 58 NETTCPSOCKET_CONABRT,
takashiyamanoue 0:ffac63d6a7f0 59 NETTCPSOCKET_ERROR,
takashiyamanoue 0:ffac63d6a7f0 60 NETTCPSOCKET_DISCONNECTED
takashiyamanoue 0:ffac63d6a7f0 61 };
takashiyamanoue 0:ffac63d6a7f0 62
takashiyamanoue 0:ffac63d6a7f0 63
takashiyamanoue 0:ffac63d6a7f0 64 class NetTcpSocket
takashiyamanoue 0:ffac63d6a7f0 65 {
takashiyamanoue 0:ffac63d6a7f0 66 public:
takashiyamanoue 0:ffac63d6a7f0 67 NetTcpSocket();
takashiyamanoue 0:ffac63d6a7f0 68 virtual ~NetTcpSocket(); //close()
takashiyamanoue 0:ffac63d6a7f0 69
takashiyamanoue 0:ffac63d6a7f0 70 virtual NetTcpSocketErr bind(const Host& me) = 0;
takashiyamanoue 0:ffac63d6a7f0 71 virtual NetTcpSocketErr listen() = 0;
takashiyamanoue 0:ffac63d6a7f0 72 virtual NetTcpSocketErr connect(const Host& host) = 0;
takashiyamanoue 0:ffac63d6a7f0 73 virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket) = 0;
takashiyamanoue 0:ffac63d6a7f0 74
takashiyamanoue 0:ffac63d6a7f0 75 virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len) = 0;
takashiyamanoue 0:ffac63d6a7f0 76 virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len) = 0;
takashiyamanoue 0:ffac63d6a7f0 77
takashiyamanoue 0:ffac63d6a7f0 78 virtual NetTcpSocketErr close() = 0;
takashiyamanoue 0:ffac63d6a7f0 79
takashiyamanoue 0:ffac63d6a7f0 80 virtual NetTcpSocketErr poll() = 0;
takashiyamanoue 0:ffac63d6a7f0 81
takashiyamanoue 0:ffac63d6a7f0 82 class CDummy;
takashiyamanoue 0:ffac63d6a7f0 83 //Callbacks
takashiyamanoue 0:ffac63d6a7f0 84 template<class T>
takashiyamanoue 0:ffac63d6a7f0 85 //Linker bug : Must be defined here :(
takashiyamanoue 0:ffac63d6a7f0 86 void setOnEvent( T* pItem, void (T::*pMethod)(NetTcpSocketEvent) )
takashiyamanoue 0:ffac63d6a7f0 87 {
takashiyamanoue 0:ffac63d6a7f0 88 m_pCbItem = (CDummy*) pItem;
takashiyamanoue 0:ffac63d6a7f0 89 m_pCbMeth = (void (CDummy::*)(NetTcpSocketEvent)) pMethod;
takashiyamanoue 0:ffac63d6a7f0 90 }
takashiyamanoue 0:ffac63d6a7f0 91
takashiyamanoue 0:ffac63d6a7f0 92 void resetOnEvent(); //Disable callback
takashiyamanoue 0:ffac63d6a7f0 93
takashiyamanoue 0:ffac63d6a7f0 94 protected:
takashiyamanoue 0:ffac63d6a7f0 95 void queueEvent(NetTcpSocketEvent e);
takashiyamanoue 0:ffac63d6a7f0 96 void discardEvents();
takashiyamanoue 0:ffac63d6a7f0 97 void flushEvents(); //to be called during polling
takashiyamanoue 0:ffac63d6a7f0 98
takashiyamanoue 0:ffac63d6a7f0 99 Host m_host;
takashiyamanoue 0:ffac63d6a7f0 100 Host m_client;
takashiyamanoue 0:ffac63d6a7f0 101
takashiyamanoue 0:ffac63d6a7f0 102 friend class Net;
takashiyamanoue 0:ffac63d6a7f0 103 int m_refs;
takashiyamanoue 0:ffac63d6a7f0 104
takashiyamanoue 0:ffac63d6a7f0 105 bool m_closed;
takashiyamanoue 0:ffac63d6a7f0 106 bool m_removed;
takashiyamanoue 0:ffac63d6a7f0 107
takashiyamanoue 0:ffac63d6a7f0 108 private:
takashiyamanoue 0:ffac63d6a7f0 109 //We do not want to execute user code in interrupt routines, so we queue events until the server is polled
takashiyamanoue 0:ffac63d6a7f0 110 //If we port this to a multithreaded OS, we could avoid this (however some functions here are not thread-safe, so beware ;) )
takashiyamanoue 0:ffac63d6a7f0 111 void onEvent(NetTcpSocketEvent e); //To be called on poll
takashiyamanoue 0:ffac63d6a7f0 112 CDummy* m_pCbItem;
takashiyamanoue 0:ffac63d6a7f0 113 void (CDummy::*m_pCbMeth)(NetTcpSocketEvent);
takashiyamanoue 0:ffac63d6a7f0 114 queue<NetTcpSocketEvent> m_events;
takashiyamanoue 0:ffac63d6a7f0 115
takashiyamanoue 0:ffac63d6a7f0 116 };
takashiyamanoue 0:ffac63d6a7f0 117
takashiyamanoue 0:ffac63d6a7f0 118 #endif