Dependencies:   MSCUsbHost NetServices RPCInterface TextLCD mbed

Committer:
yueee_yt
Date:
Wed Aug 22 05:10:09 2012 +0000
Revision:
1:f2088fbce73f
Parent:
0:fd83f3540b1a
???????LM35??????WebServer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yueee_yt 0:fd83f3540b1a 1
yueee_yt 0:fd83f3540b1a 2 /*
yueee_yt 0:fd83f3540b1a 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
yueee_yt 0:fd83f3540b1a 4
yueee_yt 0:fd83f3540b1a 5 Permission is hereby granted, free of charge, to any person obtaining a copy
yueee_yt 0:fd83f3540b1a 6 of this software and associated documentation files (the "Software"), to deal
yueee_yt 0:fd83f3540b1a 7 in the Software without restriction, including without limitation the rights
yueee_yt 0:fd83f3540b1a 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yueee_yt 0:fd83f3540b1a 9 copies of the Software, and to permit persons to whom the Software is
yueee_yt 0:fd83f3540b1a 10 furnished to do so, subject to the following conditions:
yueee_yt 0:fd83f3540b1a 11
yueee_yt 0:fd83f3540b1a 12 The above copyright notice and this permission notice shall be included in
yueee_yt 0:fd83f3540b1a 13 all copies or substantial portions of the Software.
yueee_yt 0:fd83f3540b1a 14
yueee_yt 0:fd83f3540b1a 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yueee_yt 0:fd83f3540b1a 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yueee_yt 0:fd83f3540b1a 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yueee_yt 0:fd83f3540b1a 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yueee_yt 0:fd83f3540b1a 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yueee_yt 0:fd83f3540b1a 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yueee_yt 0:fd83f3540b1a 21 THE SOFTWARE.
yueee_yt 0:fd83f3540b1a 22 */
yueee_yt 0:fd83f3540b1a 23
yueee_yt 0:fd83f3540b1a 24 /** \file
yueee_yt 0:fd83f3540b1a 25 UDP Socket header file
yueee_yt 0:fd83f3540b1a 26 */
yueee_yt 0:fd83f3540b1a 27
yueee_yt 0:fd83f3540b1a 28 #ifndef UDPSOCKET_H
yueee_yt 0:fd83f3540b1a 29 #define UDPSOCKET_H
yueee_yt 0:fd83f3540b1a 30
yueee_yt 0:fd83f3540b1a 31 #include "core/net.h"
yueee_yt 0:fd83f3540b1a 32 #include "core/host.h"
yueee_yt 0:fd83f3540b1a 33 //Essentially it is a safe interface to NetUdpSocket
yueee_yt 0:fd83f3540b1a 34
yueee_yt 0:fd83f3540b1a 35 ///UDP Socket error codes
yueee_yt 0:fd83f3540b1a 36 enum UDPSocketErr
yueee_yt 0:fd83f3540b1a 37 {
yueee_yt 0:fd83f3540b1a 38 __UDPSOCKET_MIN = -0xFFFF,
yueee_yt 0:fd83f3540b1a 39 UDPSOCKET_SETUP, ///<UDPSocket not properly configured
yueee_yt 0:fd83f3540b1a 40 UDPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
yueee_yt 0:fd83f3540b1a 41 UDPSOCKET_MEM, ///<Not enough mem
yueee_yt 0:fd83f3540b1a 42 UDPSOCKET_INUSE, ///<Interface / Port is in use
yueee_yt 0:fd83f3540b1a 43 //...
yueee_yt 0:fd83f3540b1a 44 UDPSOCKET_OK = 0 ///<Success
yueee_yt 0:fd83f3540b1a 45 };
yueee_yt 0:fd83f3540b1a 46
yueee_yt 0:fd83f3540b1a 47 ///UDP Socket Event(s)
yueee_yt 0:fd83f3540b1a 48 enum UDPSocketEvent //Only one event here for now, but keeps that model in case we need to implement some others
yueee_yt 0:fd83f3540b1a 49 {
yueee_yt 0:fd83f3540b1a 50 UDPSOCKET_READABLE, ///<Data in buf
yueee_yt 0:fd83f3540b1a 51 };
yueee_yt 0:fd83f3540b1a 52
yueee_yt 0:fd83f3540b1a 53 class NetUdpSocket;
yueee_yt 0:fd83f3540b1a 54 enum NetUdpSocketEvent;
yueee_yt 0:fd83f3540b1a 55
yueee_yt 0:fd83f3540b1a 56 ///This is a simple UDP Socket class
yueee_yt 0:fd83f3540b1a 57 /**
yueee_yt 0:fd83f3540b1a 58 This class exposes an API to deal with UDP Sockets
yueee_yt 0:fd83f3540b1a 59 */
yueee_yt 0:fd83f3540b1a 60 class UDPSocket
yueee_yt 0:fd83f3540b1a 61 {
yueee_yt 0:fd83f3540b1a 62 public:
yueee_yt 0:fd83f3540b1a 63 ///Creates a new socket
yueee_yt 0:fd83f3540b1a 64 UDPSocket();
yueee_yt 0:fd83f3540b1a 65
yueee_yt 0:fd83f3540b1a 66 ///Closes and destroys socket
yueee_yt 0:fd83f3540b1a 67 ~UDPSocket(); //close()
yueee_yt 0:fd83f3540b1a 68
yueee_yt 0:fd83f3540b1a 69 ///Binds the socket to local host or a multicast address
yueee_yt 0:fd83f3540b1a 70 UDPSocketErr bind(const Host& me);
yueee_yt 0:fd83f3540b1a 71
yueee_yt 0:fd83f3540b1a 72 ///Sends data
yueee_yt 0:fd83f3540b1a 73 /*
yueee_yt 0:fd83f3540b1a 74 @param pHost : host to send data to
yueee_yt 0:fd83f3540b1a 75 @return a negative error code or the number of bytes transmitted
yueee_yt 0:fd83f3540b1a 76 */
yueee_yt 0:fd83f3540b1a 77 int /*if < 0 : UDPSocketErr*/ sendto(const char* buf, int len, Host* pHost);
yueee_yt 0:fd83f3540b1a 78
yueee_yt 0:fd83f3540b1a 79 ///Receives data
yueee_yt 0:fd83f3540b1a 80 /*
yueee_yt 0:fd83f3540b1a 81 @param pHost : host from which this piece of data comes from
yueee_yt 0:fd83f3540b1a 82 @return a negative error code or the number of bytes received
yueee_yt 0:fd83f3540b1a 83 */
yueee_yt 0:fd83f3540b1a 84 int /*if < 0 : UDPSocketErr*/ recvfrom(char* buf, int len, Host* pHost);
yueee_yt 0:fd83f3540b1a 85
yueee_yt 0:fd83f3540b1a 86 /* TODO NTH : printf / scanf helpers that call send/recv */
yueee_yt 0:fd83f3540b1a 87
yueee_yt 0:fd83f3540b1a 88 ///Closes socket
yueee_yt 0:fd83f3540b1a 89 UDPSocketErr close();
yueee_yt 0:fd83f3540b1a 90
yueee_yt 0:fd83f3540b1a 91 //Callbacks
yueee_yt 0:fd83f3540b1a 92 ///Setups callback
yueee_yt 0:fd83f3540b1a 93 /**
yueee_yt 0:fd83f3540b1a 94 @param pMethod : callback function
yueee_yt 0:fd83f3540b1a 95 */
yueee_yt 0:fd83f3540b1a 96 void setOnEvent( void (*pMethod)(UDPSocketEvent) );
yueee_yt 0:fd83f3540b1a 97
yueee_yt 0:fd83f3540b1a 98 class CDummy;
yueee_yt 0:fd83f3540b1a 99 ///Setups callback
yueee_yt 0:fd83f3540b1a 100 /**
yueee_yt 0:fd83f3540b1a 101 @param pItem : instance of class on which to execute the callback method
yueee_yt 0:fd83f3540b1a 102 @param pMethod : callback method
yueee_yt 0:fd83f3540b1a 103 */
yueee_yt 0:fd83f3540b1a 104 template<class T>
yueee_yt 0:fd83f3540b1a 105 void setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) )
yueee_yt 0:fd83f3540b1a 106 {
yueee_yt 0:fd83f3540b1a 107 m_pCbItem = (CDummy*) pItem;
yueee_yt 0:fd83f3540b1a 108 m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod;
yueee_yt 0:fd83f3540b1a 109 }
yueee_yt 0:fd83f3540b1a 110
yueee_yt 0:fd83f3540b1a 111 ///Disables callback
yueee_yt 0:fd83f3540b1a 112 void resetOnEvent();
yueee_yt 0:fd83f3540b1a 113
yueee_yt 0:fd83f3540b1a 114 protected:
yueee_yt 0:fd83f3540b1a 115 void onNetUdpSocketEvent(NetUdpSocketEvent e);
yueee_yt 0:fd83f3540b1a 116 UDPSocketErr checkInst();
yueee_yt 0:fd83f3540b1a 117
yueee_yt 0:fd83f3540b1a 118 private:
yueee_yt 0:fd83f3540b1a 119 NetUdpSocket* m_pNetUdpSocket;
yueee_yt 0:fd83f3540b1a 120
yueee_yt 0:fd83f3540b1a 121 CDummy* m_pCbItem;
yueee_yt 0:fd83f3540b1a 122 void (CDummy::*m_pCbMeth)(UDPSocketEvent);
yueee_yt 0:fd83f3540b1a 123
yueee_yt 0:fd83f3540b1a 124 void (*m_pCb)(UDPSocketEvent);
yueee_yt 0:fd83f3540b1a 125
yueee_yt 0:fd83f3540b1a 126 };
yueee_yt 0:fd83f3540b1a 127
yueee_yt 0:fd83f3540b1a 128 #endif