Dependents:   RTnoV3 RTnoV3_LED RTnoV3_Template RTnoV3_ADC ... more

Committer:
sherckuith
Date:
Sat Dec 31 11:25:27 2011 +0000
Revision:
0:479ce5546098
Ethernet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sherckuith 0:479ce5546098 1
sherckuith 0:479ce5546098 2 /*
sherckuith 0:479ce5546098 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
sherckuith 0:479ce5546098 4
sherckuith 0:479ce5546098 5 Permission is hereby granted, free of charge, to any person obtaining a copy
sherckuith 0:479ce5546098 6 of this software and associated documentation files (the "Software"), to deal
sherckuith 0:479ce5546098 7 in the Software without restriction, including without limitation the rights
sherckuith 0:479ce5546098 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
sherckuith 0:479ce5546098 9 copies of the Software, and to permit persons to whom the Software is
sherckuith 0:479ce5546098 10 furnished to do so, subject to the following conditions:
sherckuith 0:479ce5546098 11
sherckuith 0:479ce5546098 12 The above copyright notice and this permission notice shall be included in
sherckuith 0:479ce5546098 13 all copies or substantial portions of the Software.
sherckuith 0:479ce5546098 14
sherckuith 0:479ce5546098 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
sherckuith 0:479ce5546098 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
sherckuith 0:479ce5546098 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
sherckuith 0:479ce5546098 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
sherckuith 0:479ce5546098 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sherckuith 0:479ce5546098 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
sherckuith 0:479ce5546098 21 THE SOFTWARE.
sherckuith 0:479ce5546098 22 */
sherckuith 0:479ce5546098 23
sherckuith 0:479ce5546098 24 #ifndef NETUDPSOCKET_H
sherckuith 0:479ce5546098 25 #define NETUDPSOCKET_H
sherckuith 0:479ce5546098 26
sherckuith 0:479ce5546098 27 #include "net.h"
sherckuith 0:479ce5546098 28 #include "host.h"
sherckuith 0:479ce5546098 29
sherckuith 0:479ce5546098 30 #include <queue>
sherckuith 0:479ce5546098 31 using std::queue;
sherckuith 0:479ce5546098 32
sherckuith 0:479ce5546098 33 //Implements a Berkeley-like socket if
sherckuith 0:479ce5546098 34 //Can be interfaced either to lwip or a Telit module
sherckuith 0:479ce5546098 35
sherckuith 0:479ce5546098 36 enum NetUdpSocketErr
sherckuith 0:479ce5546098 37 {
sherckuith 0:479ce5546098 38 __NETUDPSOCKET_MIN = -0xFFFF,
sherckuith 0:479ce5546098 39 NETUDPSOCKET_SETUP, //NetUdpSocket not properly configured
sherckuith 0:479ce5546098 40 NETUDPSOCKET_IF, //If has problems
sherckuith 0:479ce5546098 41 NETUDPSOCKET_MEM, //Not enough mem
sherckuith 0:479ce5546098 42 NETUDPSOCKET_INUSE, //If/Port is in use
sherckuith 0:479ce5546098 43 //...
sherckuith 0:479ce5546098 44 NETUDPSOCKET_OK = 0
sherckuith 0:479ce5546098 45 };
sherckuith 0:479ce5546098 46
sherckuith 0:479ce5546098 47 enum NetUdpSocketEvent //Only one lonely event here... but who knows, maybe some day there'll be another one!
sherckuith 0:479ce5546098 48 {
sherckuith 0:479ce5546098 49 NETUDPSOCKET_READABLE, //Data in buf
sherckuith 0:479ce5546098 50 };
sherckuith 0:479ce5546098 51
sherckuith 0:479ce5546098 52
sherckuith 0:479ce5546098 53 class NetUdpSocket
sherckuith 0:479ce5546098 54 {
sherckuith 0:479ce5546098 55 public:
sherckuith 0:479ce5546098 56 NetUdpSocket();
sherckuith 0:479ce5546098 57 virtual ~NetUdpSocket(); //close()
sherckuith 0:479ce5546098 58
sherckuith 0:479ce5546098 59 virtual NetUdpSocketErr bind(const Host& me) = 0;
sherckuith 0:479ce5546098 60
sherckuith 0:479ce5546098 61 virtual int /*if < 0 : NetUdpSocketErr*/ sendto(const char* buf, int len, Host* pHost) = 0;
sherckuith 0:479ce5546098 62 virtual int /*if < 0 : NetUdpSocketErr*/ recvfrom(char* buf, int len, Host* pHost) = 0;
sherckuith 0:479ce5546098 63
sherckuith 0:479ce5546098 64 /* TODO NTH : printf / scanf helpers that call send/recv */
sherckuith 0:479ce5546098 65
sherckuith 0:479ce5546098 66 virtual NetUdpSocketErr close() = 0;
sherckuith 0:479ce5546098 67
sherckuith 0:479ce5546098 68 virtual NetUdpSocketErr poll() = 0;
sherckuith 0:479ce5546098 69
sherckuith 0:479ce5546098 70 class CDummy;
sherckuith 0:479ce5546098 71 //Callbacks
sherckuith 0:479ce5546098 72 template<class T>
sherckuith 0:479ce5546098 73 //Linker bug : Must be defined here :(
sherckuith 0:479ce5546098 74 void setOnEvent( T* pItem, void (T::*pMethod)(NetUdpSocketEvent) )
sherckuith 0:479ce5546098 75 {
sherckuith 0:479ce5546098 76 m_pCbItem = (CDummy*) pItem;
sherckuith 0:479ce5546098 77 m_pCbMeth = (void (CDummy::*)(NetUdpSocketEvent)) pMethod;
sherckuith 0:479ce5546098 78 }
sherckuith 0:479ce5546098 79
sherckuith 0:479ce5546098 80 void resetOnEvent(); //Disable callback
sherckuith 0:479ce5546098 81
sherckuith 0:479ce5546098 82 protected:
sherckuith 0:479ce5546098 83 void queueEvent(NetUdpSocketEvent e);
sherckuith 0:479ce5546098 84 void discardEvents();
sherckuith 0:479ce5546098 85 void flushEvents(); //to be called during polling
sherckuith 0:479ce5546098 86
sherckuith 0:479ce5546098 87 Host m_host;
sherckuith 0:479ce5546098 88 Host m_client;
sherckuith 0:479ce5546098 89
sherckuith 0:479ce5546098 90 friend class Net;
sherckuith 0:479ce5546098 91 int m_refs;
sherckuith 0:479ce5546098 92
sherckuith 0:479ce5546098 93 bool m_closed;
sherckuith 0:479ce5546098 94 bool m_removed;
sherckuith 0:479ce5546098 95
sherckuith 0:479ce5546098 96 private:
sherckuith 0:479ce5546098 97 //We do not want to execute user code in interrupt routines, so we queue events until the server is polled
sherckuith 0:479ce5546098 98 //If we port this to a multithreaded OS, we could avoid this (however some functions here are not thread-safe, so beware ;) )
sherckuith 0:479ce5546098 99 void onEvent(NetUdpSocketEvent e); //To be called on poll
sherckuith 0:479ce5546098 100 CDummy* m_pCbItem;
sherckuith 0:479ce5546098 101 void (CDummy::*m_pCbMeth)(NetUdpSocketEvent);
sherckuith 0:479ce5546098 102 queue<NetUdpSocketEvent> m_events;
sherckuith 0:479ce5546098 103
sherckuith 0:479ce5546098 104 };
sherckuith 0:479ce5546098 105
sherckuith 0:479ce5546098 106 #endif