Host library for controlling a WiConnect enabled Wi-Fi module.
Dependents: wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more
api/types/Socket/UDPSocket.h@29:b6af04b77a56, 2014-10-27 (annotated)
- Committer:
- dan_ackme
- Date:
- Mon Oct 27 13:42:26 2014 -0700
- Revision:
- 29:b6af04b77a56
refactored library layout
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dan_ackme | 29:b6af04b77a56 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
dan_ackme | 29:b6af04b77a56 | 2 | * |
dan_ackme | 29:b6af04b77a56 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
dan_ackme | 29:b6af04b77a56 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
dan_ackme | 29:b6af04b77a56 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
dan_ackme | 29:b6af04b77a56 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
dan_ackme | 29:b6af04b77a56 | 7 | * furnished to do so, subject to the following conditions: |
dan_ackme | 29:b6af04b77a56 | 8 | * |
dan_ackme | 29:b6af04b77a56 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
dan_ackme | 29:b6af04b77a56 | 10 | * substantial portions of the Software. |
dan_ackme | 29:b6af04b77a56 | 11 | * |
dan_ackme | 29:b6af04b77a56 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
dan_ackme | 29:b6af04b77a56 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
dan_ackme | 29:b6af04b77a56 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
dan_ackme | 29:b6af04b77a56 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
dan_ackme | 29:b6af04b77a56 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
dan_ackme | 29:b6af04b77a56 | 17 | */ |
dan_ackme | 29:b6af04b77a56 | 18 | |
dan_ackme | 29:b6af04b77a56 | 19 | #ifndef UDPSOCKET_H |
dan_ackme | 29:b6af04b77a56 | 20 | #define UDPSOCKET_H |
dan_ackme | 29:b6af04b77a56 | 21 | |
dan_ackme | 29:b6af04b77a56 | 22 | #include "api/types/Socket/Socket.h" |
dan_ackme | 29:b6af04b77a56 | 23 | #include "api/types/Socket/Endpoint.h" |
dan_ackme | 29:b6af04b77a56 | 24 | |
dan_ackme | 29:b6af04b77a56 | 25 | /** |
dan_ackme | 29:b6af04b77a56 | 26 | UDP Socket |
dan_ackme | 29:b6af04b77a56 | 27 | */ |
dan_ackme | 29:b6af04b77a56 | 28 | class UDPSocket : public Socket { |
dan_ackme | 29:b6af04b77a56 | 29 | |
dan_ackme | 29:b6af04b77a56 | 30 | public: |
dan_ackme | 29:b6af04b77a56 | 31 | /** Instantiate an UDP Socket. |
dan_ackme | 29:b6af04b77a56 | 32 | */ |
dan_ackme | 29:b6af04b77a56 | 33 | UDPSocket(int rxBufferLen = SOCKET_API_DEFAULT_RX_BUFFER_SIZE, void *rxBuffer = NULL, int txBufferLen = SOCKET_API_DEFAULT_TX_BUFFER_SIZE, void *txBuffer = NULL); |
dan_ackme | 29:b6af04b77a56 | 34 | |
dan_ackme | 29:b6af04b77a56 | 35 | /** Init the UDP Client Socket without binding it to any specific port |
dan_ackme | 29:b6af04b77a56 | 36 | \return 0 on success, -1 on failure. |
dan_ackme | 29:b6af04b77a56 | 37 | */ |
dan_ackme | 29:b6af04b77a56 | 38 | int init(void); |
dan_ackme | 29:b6af04b77a56 | 39 | |
dan_ackme | 29:b6af04b77a56 | 40 | /** Bind a UDP Server Socket to a specific port |
dan_ackme | 29:b6af04b77a56 | 41 | \param port The port to listen for incoming connections on |
dan_ackme | 29:b6af04b77a56 | 42 | \return 0 on success, -1 on failure. |
dan_ackme | 29:b6af04b77a56 | 43 | */ |
dan_ackme | 29:b6af04b77a56 | 44 | int bind(int port); |
dan_ackme | 29:b6af04b77a56 | 45 | |
dan_ackme | 29:b6af04b77a56 | 46 | /** Join the multicast group at the given address |
dan_ackme | 29:b6af04b77a56 | 47 | * |
dan_ackme | 29:b6af04b77a56 | 48 | * \note This is not currently supported. |
dan_ackme | 29:b6af04b77a56 | 49 | * |
dan_ackme | 29:b6af04b77a56 | 50 | \param address The address of the multicast group |
dan_ackme | 29:b6af04b77a56 | 51 | \return 0 on success, -1 on failure. |
dan_ackme | 29:b6af04b77a56 | 52 | */ |
dan_ackme | 29:b6af04b77a56 | 53 | int join_multicast_group(const char* address); |
dan_ackme | 29:b6af04b77a56 | 54 | |
dan_ackme | 29:b6af04b77a56 | 55 | /** Set the socket in broadcasting mode |
dan_ackme | 29:b6af04b77a56 | 56 | * |
dan_ackme | 29:b6af04b77a56 | 57 | * \note Broadcasting is always enabled, so this |
dan_ackme | 29:b6af04b77a56 | 58 | * doesn't do anything useful. |
dan_ackme | 29:b6af04b77a56 | 59 | * |
dan_ackme | 29:b6af04b77a56 | 60 | \return 0 on success, -1 on failure. |
dan_ackme | 29:b6af04b77a56 | 61 | */ |
dan_ackme | 29:b6af04b77a56 | 62 | int set_broadcasting(bool broadcast=true); |
dan_ackme | 29:b6af04b77a56 | 63 | |
dan_ackme | 29:b6af04b77a56 | 64 | /** Send a packet to a remote endpoint |
dan_ackme | 29:b6af04b77a56 | 65 | * |
dan_ackme | 29:b6af04b77a56 | 66 | * \note Currently only one remote client address is supported |
dan_ackme | 29:b6af04b77a56 | 67 | * |
dan_ackme | 29:b6af04b77a56 | 68 | \param remote The remote endpoint |
dan_ackme | 29:b6af04b77a56 | 69 | \param packet The packet to be sent |
dan_ackme | 29:b6af04b77a56 | 70 | \param length The length of the packet to be sent |
dan_ackme | 29:b6af04b77a56 | 71 | \return the number of written bytes on success (>=0) or -1 on failure |
dan_ackme | 29:b6af04b77a56 | 72 | */ |
dan_ackme | 29:b6af04b77a56 | 73 | int sendTo(Endpoint &remote, char *packet, int length); |
dan_ackme | 29:b6af04b77a56 | 74 | |
dan_ackme | 29:b6af04b77a56 | 75 | /** Receive a packet from a remote endpoint |
dan_ackme | 29:b6af04b77a56 | 76 | * |
dan_ackme | 29:b6af04b77a56 | 77 | * \note Currently only one remote client address is supported |
dan_ackme | 29:b6af04b77a56 | 78 | * |
dan_ackme | 29:b6af04b77a56 | 79 | * |
dan_ackme | 29:b6af04b77a56 | 80 | \param remote The remote endpoint |
dan_ackme | 29:b6af04b77a56 | 81 | \param buffer The buffer for storing the incoming packet data. If a packet |
dan_ackme | 29:b6af04b77a56 | 82 | is too long to fit in the supplied buffer, excess bytes are discarded |
dan_ackme | 29:b6af04b77a56 | 83 | \param length The length of the buffer |
dan_ackme | 29:b6af04b77a56 | 84 | \return the number of received bytes on success (>=0) or -1 on failure |
dan_ackme | 29:b6af04b77a56 | 85 | */ |
dan_ackme | 29:b6af04b77a56 | 86 | int receiveFrom(Endpoint &remote, char *buffer, int length); |
dan_ackme | 29:b6af04b77a56 | 87 | |
dan_ackme | 29:b6af04b77a56 | 88 | |
dan_ackme | 29:b6af04b77a56 | 89 | private: |
dan_ackme | 29:b6af04b77a56 | 90 | int localPort; |
dan_ackme | 29:b6af04b77a56 | 91 | }; |
dan_ackme | 29:b6af04b77a56 | 92 | |
dan_ackme | 29:b6af04b77a56 | 93 | #endif |