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