Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

Who changed what in which revision?

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