mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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