testing of combination of LCS and LAN

Dependencies:   mbed

Committer:
damir
Date:
Wed Jan 14 13:29:55 2015 +0000
Revision:
0:a7a6a692162f
Test of LAN

Who changed what in which revision?

UserRevisionLine numberNew contents of line
damir 0:a7a6a692162f 1
damir 0:a7a6a692162f 2 /*
damir 0:a7a6a692162f 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
damir 0:a7a6a692162f 4
damir 0:a7a6a692162f 5 Permission is hereby granted, free of charge, to any person obtaining a copy
damir 0:a7a6a692162f 6 of this software and associated documentation files (the "Software"), to deal
damir 0:a7a6a692162f 7 in the Software without restriction, including without limitation the rights
damir 0:a7a6a692162f 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
damir 0:a7a6a692162f 9 copies of the Software, and to permit persons to whom the Software is
damir 0:a7a6a692162f 10 furnished to do so, subject to the following conditions:
damir 0:a7a6a692162f 11
damir 0:a7a6a692162f 12 The above copyright notice and this permission notice shall be included in
damir 0:a7a6a692162f 13 all copies or substantial portions of the Software.
damir 0:a7a6a692162f 14
damir 0:a7a6a692162f 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
damir 0:a7a6a692162f 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
damir 0:a7a6a692162f 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
damir 0:a7a6a692162f 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
damir 0:a7a6a692162f 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
damir 0:a7a6a692162f 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
damir 0:a7a6a692162f 21 THE SOFTWARE.
damir 0:a7a6a692162f 22 */
damir 0:a7a6a692162f 23
damir 0:a7a6a692162f 24 #ifndef NETTCPSOCKET_H
damir 0:a7a6a692162f 25 #define NETTCPSOCKET_H
damir 0:a7a6a692162f 26
damir 0:a7a6a692162f 27 #include "net.h"
damir 0:a7a6a692162f 28 #include "host.h"
damir 0:a7a6a692162f 29
damir 0:a7a6a692162f 30 #include <queue>
damir 0:a7a6a692162f 31 using std::queue;
damir 0:a7a6a692162f 32
damir 0:a7a6a692162f 33 //Implements a Berkeley-like socket if
damir 0:a7a6a692162f 34 //Can be interfaced either to lwip or a Telit module
damir 0:a7a6a692162f 35
damir 0:a7a6a692162f 36 enum NetTcpSocketErr
damir 0:a7a6a692162f 37 {
damir 0:a7a6a692162f 38 __NETTCPSOCKET_MIN = -0xFFFF,
damir 0:a7a6a692162f 39 NETTCPSOCKET_SETUP, //NetTcpSocket not properly configured
damir 0:a7a6a692162f 40 NETTCPSOCKET_TIMEOUT,
damir 0:a7a6a692162f 41 NETTCPSOCKET_IF, //If has problems
damir 0:a7a6a692162f 42 NETTCPSOCKET_MEM, //Not enough mem
damir 0:a7a6a692162f 43 NETTCPSOCKET_INUSE, //If/Port is in use
damir 0:a7a6a692162f 44 NETTCPSOCKET_EMPTY, //Connections queue is empty
damir 0:a7a6a692162f 45 NETTCPSOCKET_RST, // Connection was reset by remote host
damir 0:a7a6a692162f 46 //...
damir 0:a7a6a692162f 47 NETTCPSOCKET_OK = 0
damir 0:a7a6a692162f 48 };
damir 0:a7a6a692162f 49
damir 0:a7a6a692162f 50 enum NetTcpSocketEvent
damir 0:a7a6a692162f 51 {
damir 0:a7a6a692162f 52 NETTCPSOCKET_CONNECTED, //Connected to host, must call accept() if we were listening
damir 0:a7a6a692162f 53 NETTCPSOCKET_ACCEPT, //Connected to client
damir 0:a7a6a692162f 54 NETTCPSOCKET_READABLE, //Data in buf
damir 0:a7a6a692162f 55 NETTCPSOCKET_WRITEABLE, //Can write data to buf
damir 0:a7a6a692162f 56 NETTCPSOCKET_CONTIMEOUT,
damir 0:a7a6a692162f 57 NETTCPSOCKET_CONRST,
damir 0:a7a6a692162f 58 NETTCPSOCKET_CONABRT,
damir 0:a7a6a692162f 59 NETTCPSOCKET_ERROR,
damir 0:a7a6a692162f 60 NETTCPSOCKET_DISCONNECTED
damir 0:a7a6a692162f 61 };
damir 0:a7a6a692162f 62
damir 0:a7a6a692162f 63
damir 0:a7a6a692162f 64 class NetTcpSocket
damir 0:a7a6a692162f 65 {
damir 0:a7a6a692162f 66 public:
damir 0:a7a6a692162f 67 NetTcpSocket();
damir 0:a7a6a692162f 68 virtual ~NetTcpSocket(); //close()
damir 0:a7a6a692162f 69
damir 0:a7a6a692162f 70 virtual NetTcpSocketErr bind(const Host& me) = 0;
damir 0:a7a6a692162f 71 virtual NetTcpSocketErr listen() = 0;
damir 0:a7a6a692162f 72 virtual NetTcpSocketErr connect(const Host& host) = 0;
damir 0:a7a6a692162f 73 virtual NetTcpSocketErr accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket) = 0;
damir 0:a7a6a692162f 74
damir 0:a7a6a692162f 75 virtual int /*if < 0 : NetTcpSocketErr*/ send(const char* buf, int len) = 0;
damir 0:a7a6a692162f 76 virtual int /*if < 0 : NetTcpSocketErr*/ recv(char* buf, int len) = 0;
damir 0:a7a6a692162f 77
damir 0:a7a6a692162f 78 virtual NetTcpSocketErr close() = 0;
damir 0:a7a6a692162f 79
damir 0:a7a6a692162f 80 virtual NetTcpSocketErr poll() = 0;
damir 0:a7a6a692162f 81
damir 0:a7a6a692162f 82 class CDummy;
damir 0:a7a6a692162f 83 //Callbacks
damir 0:a7a6a692162f 84 template<class T>
damir 0:a7a6a692162f 85 //Linker bug : Must be defined here :(
damir 0:a7a6a692162f 86 void setOnEvent( T* pItem, void (T::*pMethod)(NetTcpSocketEvent) )
damir 0:a7a6a692162f 87 {
damir 0:a7a6a692162f 88 m_pCbItem = (CDummy*) pItem;
damir 0:a7a6a692162f 89 m_pCbMeth = (void (CDummy::*)(NetTcpSocketEvent)) pMethod;
damir 0:a7a6a692162f 90 }
damir 0:a7a6a692162f 91
damir 0:a7a6a692162f 92 void resetOnEvent(); //Disable callback
damir 0:a7a6a692162f 93
damir 0:a7a6a692162f 94 protected:
damir 0:a7a6a692162f 95 void queueEvent(NetTcpSocketEvent e);
damir 0:a7a6a692162f 96 void discardEvents();
damir 0:a7a6a692162f 97 void flushEvents(); //to be called during polling
damir 0:a7a6a692162f 98
damir 0:a7a6a692162f 99 Host m_host;
damir 0:a7a6a692162f 100 Host m_client;
damir 0:a7a6a692162f 101
damir 0:a7a6a692162f 102 friend class Net;
damir 0:a7a6a692162f 103 int m_refs;
damir 0:a7a6a692162f 104
damir 0:a7a6a692162f 105 bool m_closed;
damir 0:a7a6a692162f 106 bool m_removed;
damir 0:a7a6a692162f 107
damir 0:a7a6a692162f 108 private:
damir 0:a7a6a692162f 109 //We do not want to execute user code in interrupt routines, so we queue events until the server is polled
damir 0:a7a6a692162f 110 //If we port this to a multithreaded OS, we could avoid this (however some functions here are not thread-safe, so beware ;) )
damir 0:a7a6a692162f 111 void onEvent(NetTcpSocketEvent e); //To be called on poll
damir 0:a7a6a692162f 112 CDummy* m_pCbItem;
damir 0:a7a6a692162f 113 void (CDummy::*m_pCbMeth)(NetTcpSocketEvent);
damir 0:a7a6a692162f 114 queue<NetTcpSocketEvent> m_events;
damir 0:a7a6a692162f 115
damir 0:a7a6a692162f 116 };
damir 0:a7a6a692162f 117
damir 0:a7a6a692162f 118 #endif