EthernetNetIf

Dependents:   XBee_WiFi_EA_LPC4088

Committer:
hmike
Date:
Wed Nov 19 12:00:42 2014 +0000
Revision:
0:62580107d20c
EthernetNetIf

Who changed what in which revision?

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