Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Simon Cooksey 0:fb7af294d5d9 1
Simon Cooksey 0:fb7af294d5d9 2 /** \addtogroup netsocket */
Simon Cooksey 0:fb7af294d5d9 3 /** @{*/
Simon Cooksey 0:fb7af294d5d9 4 /* UDPSocket
Simon Cooksey 0:fb7af294d5d9 5 * Copyright (c) 2015 ARM Limited
Simon Cooksey 0:fb7af294d5d9 6 *
Simon Cooksey 0:fb7af294d5d9 7 * Licensed under the Apache License, Version 2.0 (the "License");
Simon Cooksey 0:fb7af294d5d9 8 * you may not use this file except in compliance with the License.
Simon Cooksey 0:fb7af294d5d9 9 * You may obtain a copy of the License at
Simon Cooksey 0:fb7af294d5d9 10 *
Simon Cooksey 0:fb7af294d5d9 11 * http://www.apache.org/licenses/LICENSE-2.0
Simon Cooksey 0:fb7af294d5d9 12 *
Simon Cooksey 0:fb7af294d5d9 13 * Unless required by applicable law or agreed to in writing, software
Simon Cooksey 0:fb7af294d5d9 14 * distributed under the License is distributed on an "AS IS" BASIS,
Simon Cooksey 0:fb7af294d5d9 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Simon Cooksey 0:fb7af294d5d9 16 * See the License for the specific language governing permissions and
Simon Cooksey 0:fb7af294d5d9 17 * limitations under the License.
Simon Cooksey 0:fb7af294d5d9 18 */
Simon Cooksey 0:fb7af294d5d9 19
Simon Cooksey 0:fb7af294d5d9 20 #ifndef UDPSOCKET_H
Simon Cooksey 0:fb7af294d5d9 21 #define UDPSOCKET_H
Simon Cooksey 0:fb7af294d5d9 22
Simon Cooksey 0:fb7af294d5d9 23 #include "netsocket/Socket.h"
Simon Cooksey 0:fb7af294d5d9 24 #include "netsocket/NetworkStack.h"
Simon Cooksey 0:fb7af294d5d9 25 #include "netsocket/NetworkInterface.h"
Simon Cooksey 0:fb7af294d5d9 26 #include "rtos/Semaphore.h"
Simon Cooksey 0:fb7af294d5d9 27
Simon Cooksey 0:fb7af294d5d9 28
Simon Cooksey 0:fb7af294d5d9 29 /** UDP socket
Simon Cooksey 0:fb7af294d5d9 30 */
Simon Cooksey 0:fb7af294d5d9 31 class UDPSocket : public Socket {
Simon Cooksey 0:fb7af294d5d9 32 public:
Simon Cooksey 0:fb7af294d5d9 33 /** Create an uninitialized socket
Simon Cooksey 0:fb7af294d5d9 34 *
Simon Cooksey 0:fb7af294d5d9 35 * Must call open to initialize the socket on a network stack.
Simon Cooksey 0:fb7af294d5d9 36 */
Simon Cooksey 0:fb7af294d5d9 37 UDPSocket();
Simon Cooksey 0:fb7af294d5d9 38
Simon Cooksey 0:fb7af294d5d9 39 /** Create a socket on a network interface
Simon Cooksey 0:fb7af294d5d9 40 *
Simon Cooksey 0:fb7af294d5d9 41 * Creates and opens a socket on the network stack of the given
Simon Cooksey 0:fb7af294d5d9 42 * network interface.
Simon Cooksey 0:fb7af294d5d9 43 *
Simon Cooksey 0:fb7af294d5d9 44 * @param stack Network stack as target for socket
Simon Cooksey 0:fb7af294d5d9 45 */
Simon Cooksey 0:fb7af294d5d9 46 template <typename S>
Simon Cooksey 0:fb7af294d5d9 47 UDPSocket(S *stack)
Simon Cooksey 0:fb7af294d5d9 48 : _pending(0), _read_sem(0), _write_sem(0),
Simon Cooksey 0:fb7af294d5d9 49 _read_in_progress(false), _write_in_progress(false)
Simon Cooksey 0:fb7af294d5d9 50 {
Simon Cooksey 0:fb7af294d5d9 51 open(stack);
Simon Cooksey 0:fb7af294d5d9 52 }
Simon Cooksey 0:fb7af294d5d9 53
Simon Cooksey 0:fb7af294d5d9 54 /** Destroy a socket
Simon Cooksey 0:fb7af294d5d9 55 *
Simon Cooksey 0:fb7af294d5d9 56 * Closes socket if the socket is still open
Simon Cooksey 0:fb7af294d5d9 57 */
Simon Cooksey 0:fb7af294d5d9 58 virtual ~UDPSocket();
Simon Cooksey 0:fb7af294d5d9 59
Simon Cooksey 0:fb7af294d5d9 60 /** Send a packet over a UDP socket
Simon Cooksey 0:fb7af294d5d9 61 *
Simon Cooksey 0:fb7af294d5d9 62 * Sends data to the specified address specified by either a domain name
Simon Cooksey 0:fb7af294d5d9 63 * or an IP address and port. Returns the number of bytes sent from the
Simon Cooksey 0:fb7af294d5d9 64 * buffer.
Simon Cooksey 0:fb7af294d5d9 65 *
Simon Cooksey 0:fb7af294d5d9 66 * By default, sendto blocks until data is sent. If socket is set to
Simon Cooksey 0:fb7af294d5d9 67 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
Simon Cooksey 0:fb7af294d5d9 68 * immediately.
Simon Cooksey 0:fb7af294d5d9 69 *
Simon Cooksey 0:fb7af294d5d9 70 * @param host Hostname of the remote host
Simon Cooksey 0:fb7af294d5d9 71 * @param port Port of the remote host
Simon Cooksey 0:fb7af294d5d9 72 * @param data Buffer of data to send to the host
Simon Cooksey 0:fb7af294d5d9 73 * @param size Size of the buffer in bytes
Simon Cooksey 0:fb7af294d5d9 74 * @return Number of sent bytes on success, negative error
Simon Cooksey 0:fb7af294d5d9 75 * code on failure
Simon Cooksey 0:fb7af294d5d9 76 */
Simon Cooksey 0:fb7af294d5d9 77 int sendto(const char *host, uint16_t port, const void *data, unsigned size);
Simon Cooksey 0:fb7af294d5d9 78
Simon Cooksey 0:fb7af294d5d9 79 /** Send a packet over a UDP socket
Simon Cooksey 0:fb7af294d5d9 80 *
Simon Cooksey 0:fb7af294d5d9 81 * Sends data to the specified address. Returns the number of bytes
Simon Cooksey 0:fb7af294d5d9 82 * sent from the buffer.
Simon Cooksey 0:fb7af294d5d9 83 *
Simon Cooksey 0:fb7af294d5d9 84 * By default, sendto blocks until data is sent. If socket is set to
Simon Cooksey 0:fb7af294d5d9 85 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
Simon Cooksey 0:fb7af294d5d9 86 * immediately.
Simon Cooksey 0:fb7af294d5d9 87 *
Simon Cooksey 0:fb7af294d5d9 88 * @param address The SocketAddress of the remote host
Simon Cooksey 0:fb7af294d5d9 89 * @param data Buffer of data to send to the host
Simon Cooksey 0:fb7af294d5d9 90 * @param size Size of the buffer in bytes
Simon Cooksey 0:fb7af294d5d9 91 * @return Number of sent bytes on success, negative error
Simon Cooksey 0:fb7af294d5d9 92 * code on failure
Simon Cooksey 0:fb7af294d5d9 93 */
Simon Cooksey 0:fb7af294d5d9 94 int sendto(const SocketAddress &address, const void *data, unsigned size);
Simon Cooksey 0:fb7af294d5d9 95
Simon Cooksey 0:fb7af294d5d9 96 /** Receive a packet over a UDP socket
Simon Cooksey 0:fb7af294d5d9 97 *
Simon Cooksey 0:fb7af294d5d9 98 * Receives data and stores the source address in address if address
Simon Cooksey 0:fb7af294d5d9 99 * is not NULL. Returns the number of bytes received into the buffer.
Simon Cooksey 0:fb7af294d5d9 100 *
Simon Cooksey 0:fb7af294d5d9 101 * By default, recvfrom blocks until data is sent. If socket is set to
Simon Cooksey 0:fb7af294d5d9 102 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
Simon Cooksey 0:fb7af294d5d9 103 * immediately.
Simon Cooksey 0:fb7af294d5d9 104 *
Simon Cooksey 0:fb7af294d5d9 105 * @param address Destination for the source address or NULL
Simon Cooksey 0:fb7af294d5d9 106 * @param data Destination buffer for data received from the host
Simon Cooksey 0:fb7af294d5d9 107 * @param size Size of the buffer in bytes
Simon Cooksey 0:fb7af294d5d9 108 * @return Number of received bytes on success, negative error
Simon Cooksey 0:fb7af294d5d9 109 * code on failure
Simon Cooksey 0:fb7af294d5d9 110 */
Simon Cooksey 0:fb7af294d5d9 111 int recvfrom(SocketAddress *address, void *data, unsigned size);
Simon Cooksey 0:fb7af294d5d9 112
Simon Cooksey 0:fb7af294d5d9 113 protected:
Simon Cooksey 0:fb7af294d5d9 114 virtual nsapi_protocol_t get_proto();
Simon Cooksey 0:fb7af294d5d9 115 virtual void event();
Simon Cooksey 0:fb7af294d5d9 116
Simon Cooksey 0:fb7af294d5d9 117 volatile unsigned _pending;
Simon Cooksey 0:fb7af294d5d9 118 rtos::Semaphore _read_sem;
Simon Cooksey 0:fb7af294d5d9 119 rtos::Semaphore _write_sem;
Simon Cooksey 0:fb7af294d5d9 120 bool _read_in_progress;
Simon Cooksey 0:fb7af294d5d9 121 bool _write_in_progress;
Simon Cooksey 0:fb7af294d5d9 122 };
Simon Cooksey 0:fb7af294d5d9 123
Simon Cooksey 0:fb7af294d5d9 124
Simon Cooksey 0:fb7af294d5d9 125 #endif
Simon Cooksey 0:fb7af294d5d9 126
Simon Cooksey 0:fb7af294d5d9 127 /** @}*/