Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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