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 /** \file
sherckuith 0:479ce5546098 25 UDP Socket header file
sherckuith 0:479ce5546098 26 */
sherckuith 0:479ce5546098 27
sherckuith 0:479ce5546098 28 #ifndef UDPSOCKET_H
sherckuith 0:479ce5546098 29 #define UDPSOCKET_H
sherckuith 0:479ce5546098 30
sherckuith 0:479ce5546098 31 #include "core/net.h"
sherckuith 0:479ce5546098 32 #include "core/host.h"
sherckuith 0:479ce5546098 33 //Essentially it is a safe interface to NetUdpSocket
sherckuith 0:479ce5546098 34
sherckuith 0:479ce5546098 35 ///UDP Socket error codes
sherckuith 0:479ce5546098 36 enum UDPSocketErr
sherckuith 0:479ce5546098 37 {
sherckuith 0:479ce5546098 38 __UDPSOCKET_MIN = -0xFFFF,
sherckuith 0:479ce5546098 39 UDPSOCKET_SETUP, ///<UDPSocket not properly configured
sherckuith 0:479ce5546098 40 UDPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
sherckuith 0:479ce5546098 41 UDPSOCKET_MEM, ///<Not enough mem
sherckuith 0:479ce5546098 42 UDPSOCKET_INUSE, ///<Interface / Port is in use
sherckuith 0:479ce5546098 43 //...
sherckuith 0:479ce5546098 44 UDPSOCKET_OK = 0 ///<Success
sherckuith 0:479ce5546098 45 };
sherckuith 0:479ce5546098 46
sherckuith 0:479ce5546098 47 ///UDP Socket Event(s)
sherckuith 0:479ce5546098 48 enum UDPSocketEvent //Only one event here for now, but keeps that model in case we need to implement some others
sherckuith 0:479ce5546098 49 {
sherckuith 0:479ce5546098 50 UDPSOCKET_READABLE, ///<Data in buf
sherckuith 0:479ce5546098 51 };
sherckuith 0:479ce5546098 52
sherckuith 0:479ce5546098 53 class NetUdpSocket;
sherckuith 0:479ce5546098 54 enum NetUdpSocketEvent;
sherckuith 0:479ce5546098 55
sherckuith 0:479ce5546098 56 ///This is a simple UDP Socket class
sherckuith 0:479ce5546098 57 /**
sherckuith 0:479ce5546098 58 This class exposes an API to deal with UDP Sockets
sherckuith 0:479ce5546098 59 */
sherckuith 0:479ce5546098 60 class UDPSocket
sherckuith 0:479ce5546098 61 {
sherckuith 0:479ce5546098 62 public:
sherckuith 0:479ce5546098 63 ///Creates a new socket
sherckuith 0:479ce5546098 64 UDPSocket();
sherckuith 0:479ce5546098 65
sherckuith 0:479ce5546098 66 ///Closes and destroys socket
sherckuith 0:479ce5546098 67 ~UDPSocket(); //close()
sherckuith 0:479ce5546098 68
sherckuith 0:479ce5546098 69 ///Binds the socket to local host or a multicast address
sherckuith 0:479ce5546098 70 UDPSocketErr bind(const Host& me);
sherckuith 0:479ce5546098 71
sherckuith 0:479ce5546098 72 ///Sends data
sherckuith 0:479ce5546098 73 /*
sherckuith 0:479ce5546098 74 @param pHost : host to send data to
sherckuith 0:479ce5546098 75 @return a negative error code or the number of bytes transmitted
sherckuith 0:479ce5546098 76 */
sherckuith 0:479ce5546098 77 int /*if < 0 : UDPSocketErr*/ sendto(const char* buf, int len, Host* pHost);
sherckuith 0:479ce5546098 78
sherckuith 0:479ce5546098 79 ///Receives data
sherckuith 0:479ce5546098 80 /*
sherckuith 0:479ce5546098 81 @param pHost : host from which this piece of data comes from
sherckuith 0:479ce5546098 82 @return a negative error code or the number of bytes received
sherckuith 0:479ce5546098 83 */
sherckuith 0:479ce5546098 84 int /*if < 0 : UDPSocketErr*/ recvfrom(char* buf, int len, Host* pHost);
sherckuith 0:479ce5546098 85
sherckuith 0:479ce5546098 86 /* TODO NTH : printf / scanf helpers that call send/recv */
sherckuith 0:479ce5546098 87
sherckuith 0:479ce5546098 88 ///Closes socket
sherckuith 0:479ce5546098 89 UDPSocketErr close();
sherckuith 0:479ce5546098 90
sherckuith 0:479ce5546098 91 //Callbacks
sherckuith 0:479ce5546098 92 ///Setups callback
sherckuith 0:479ce5546098 93 /**
sherckuith 0:479ce5546098 94 @param pMethod : callback function
sherckuith 0:479ce5546098 95 */
sherckuith 0:479ce5546098 96 void setOnEvent( void (*pMethod)(UDPSocketEvent) );
sherckuith 0:479ce5546098 97
sherckuith 0:479ce5546098 98 class CDummy;
sherckuith 0:479ce5546098 99 ///Setups callback
sherckuith 0:479ce5546098 100 /**
sherckuith 0:479ce5546098 101 @param pItem : instance of class on which to execute the callback method
sherckuith 0:479ce5546098 102 @param pMethod : callback method
sherckuith 0:479ce5546098 103 */
sherckuith 0:479ce5546098 104 template<class T>
sherckuith 0:479ce5546098 105 void setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) )
sherckuith 0:479ce5546098 106 {
sherckuith 0:479ce5546098 107 m_pCbItem = (CDummy*) pItem;
sherckuith 0:479ce5546098 108 m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod;
sherckuith 0:479ce5546098 109 }
sherckuith 0:479ce5546098 110
sherckuith 0:479ce5546098 111 ///Disables callback
sherckuith 0:479ce5546098 112 void resetOnEvent();
sherckuith 0:479ce5546098 113
sherckuith 0:479ce5546098 114 protected:
sherckuith 0:479ce5546098 115 void onNetUdpSocketEvent(NetUdpSocketEvent e);
sherckuith 0:479ce5546098 116 UDPSocketErr checkInst();
sherckuith 0:479ce5546098 117
sherckuith 0:479ce5546098 118 private:
sherckuith 0:479ce5546098 119 NetUdpSocket* m_pNetUdpSocket;
sherckuith 0:479ce5546098 120
sherckuith 0:479ce5546098 121 CDummy* m_pCbItem;
sherckuith 0:479ce5546098 122 void (CDummy::*m_pCbMeth)(UDPSocketEvent);
sherckuith 0:479ce5546098 123
sherckuith 0:479ce5546098 124 void (*m_pCb)(UDPSocketEvent);
sherckuith 0:479ce5546098 125
sherckuith 0:479ce5546098 126 };
sherckuith 0:479ce5546098 127
sherckuith 0:479ce5546098 128 #endif