save loops

Dependencies:   mbed

Committer:
mbedalvaro
Date:
Tue Dec 02 08:29:59 2014 +0000
Revision:
1:3be7b7d050f4
Parent:
0:df6fdd9b99f0
updated

Who changed what in which revision?

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