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