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