Nicolas Borla / Mbed OS ROME2_Robot_Firmware
Committer:
boro
Date:
Mon Mar 16 13:12:31 2020 +0000
Revision:
0:4beb2ea291ec
a

Who changed what in which revision?

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