mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:5b88d5760320 1 /*
kenjiArai 0:5b88d5760320 2 * Copyright (c) 2015 ARM Limited
kenjiArai 0:5b88d5760320 3 *
kenjiArai 0:5b88d5760320 4 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 0:5b88d5760320 5 * you may not use this file except in compliance with the License.
kenjiArai 0:5b88d5760320 6 * You may obtain a copy of the License at
kenjiArai 0:5b88d5760320 7 *
kenjiArai 0:5b88d5760320 8 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 0:5b88d5760320 9 *
kenjiArai 0:5b88d5760320 10 * Unless required by applicable law or agreed to in writing, software
kenjiArai 0:5b88d5760320 11 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 0:5b88d5760320 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 0:5b88d5760320 13 * See the License for the specific language governing permissions and
kenjiArai 0:5b88d5760320 14 * limitations under the License.
kenjiArai 0:5b88d5760320 15 */
kenjiArai 0:5b88d5760320 16
kenjiArai 0:5b88d5760320 17 /** @file TCPSocket.h TCPSocket class */
kenjiArai 0:5b88d5760320 18 /** \addtogroup netsocket
kenjiArai 0:5b88d5760320 19 * @{*/
kenjiArai 0:5b88d5760320 20
kenjiArai 0:5b88d5760320 21 #ifndef TCPSOCKET_H
kenjiArai 0:5b88d5760320 22 #define TCPSOCKET_H
kenjiArai 0:5b88d5760320 23
kenjiArai 0:5b88d5760320 24 #include "netsocket/InternetSocket.h"
kenjiArai 0:5b88d5760320 25 #include "netsocket/NetworkStack.h"
kenjiArai 0:5b88d5760320 26 #include "netsocket/NetworkInterface.h"
kenjiArai 0:5b88d5760320 27 #include "rtos/EventFlags.h"
kenjiArai 0:5b88d5760320 28
kenjiArai 0:5b88d5760320 29
kenjiArai 0:5b88d5760320 30 /** TCP socket connection
kenjiArai 0:5b88d5760320 31 */
kenjiArai 0:5b88d5760320 32 class TCPSocket : public InternetSocket {
kenjiArai 0:5b88d5760320 33 public:
kenjiArai 0:5b88d5760320 34 /** Create an uninitialized socket
kenjiArai 0:5b88d5760320 35 *
kenjiArai 0:5b88d5760320 36 * Must call open to initialize the socket on a network stack.
kenjiArai 0:5b88d5760320 37 */
kenjiArai 0:5b88d5760320 38 TCPSocket();
kenjiArai 0:5b88d5760320 39
kenjiArai 0:5b88d5760320 40 /** Create a socket on a network interface
kenjiArai 0:5b88d5760320 41 *
kenjiArai 0:5b88d5760320 42 * Creates and opens a socket on the network stack of the given
kenjiArai 0:5b88d5760320 43 * network interface.
kenjiArai 0:5b88d5760320 44 *
kenjiArai 0:5b88d5760320 45 * @param stack Network stack as target for socket
kenjiArai 0:5b88d5760320 46 *
kenjiArai 0:5b88d5760320 47 * @deprecated since mbed-os-5.11
kenjiArai 0:5b88d5760320 48 */
kenjiArai 0:5b88d5760320 49 template <typename S>
kenjiArai 0:5b88d5760320 50 MBED_DEPRECATED_SINCE("mbed-os-5.11",
kenjiArai 0:5b88d5760320 51 "The TCPSocket(S *stack) constructor is deprecated."
kenjiArai 0:5b88d5760320 52 "It discards the open() call return value."
kenjiArai 0:5b88d5760320 53 "Use another constructor and call open() explicitly, instead.")
kenjiArai 0:5b88d5760320 54 TCPSocket(S *stack)
kenjiArai 0:5b88d5760320 55 {
kenjiArai 0:5b88d5760320 56 open(stack);
kenjiArai 0:5b88d5760320 57 }
kenjiArai 0:5b88d5760320 58
kenjiArai 0:5b88d5760320 59 /** Destroy a socket
kenjiArai 0:5b88d5760320 60 *
kenjiArai 0:5b88d5760320 61 * Closes socket if the socket is still open
kenjiArai 0:5b88d5760320 62 */
kenjiArai 0:5b88d5760320 63 virtual ~TCPSocket();
kenjiArai 0:5b88d5760320 64
kenjiArai 0:5b88d5760320 65 /** Override multicast functions to return error for TCP
kenjiArai 0:5b88d5760320 66 *
kenjiArai 0:5b88d5760320 67 */
kenjiArai 0:5b88d5760320 68 virtual int join_multicast_group(const SocketAddress &address)
kenjiArai 0:5b88d5760320 69 {
kenjiArai 0:5b88d5760320 70 return NSAPI_ERROR_UNSUPPORTED;
kenjiArai 0:5b88d5760320 71 }
kenjiArai 0:5b88d5760320 72
kenjiArai 0:5b88d5760320 73 /** Connects TCP socket to a remote host
kenjiArai 0:5b88d5760320 74 *
kenjiArai 0:5b88d5760320 75 * Initiates a connection to a remote server specified by either
kenjiArai 0:5b88d5760320 76 * a domain name or an IP address and a port.
kenjiArai 0:5b88d5760320 77 *
kenjiArai 0:5b88d5760320 78 * @param host Hostname of the remote host
kenjiArai 0:5b88d5760320 79 * @param port Port of the remote host
kenjiArai 1:9db0e321a9f4 80 * @retval NSAPI_ERROR_OK on success
kenjiArai 1:9db0e321a9f4 81 * @retval NSAPI_ERROR_IN_PROGRESS if the operation is ongoing
kenjiArai 1:9db0e321a9f4 82 * @retval NSAPI_ERROR_NO_SOCKET if the socket has not been allocated
kenjiArai 1:9db0e321a9f4 83 * @retval NSAPI_ERROR_DNS_FAILURE if the DNS address of host could not be resolved
kenjiArai 1:9db0e321a9f4 84 * @retval NSAPI_ERROR_IS_CONNECTED if the connection is already established
kenjiArai 1:9db0e321a9f4 85 * @retval int Other negative error codes for stack-related failures.
kenjiArai 1:9db0e321a9f4 86 * See NetworkStack::socket_connect().
kenjiArai 0:5b88d5760320 87 */
kenjiArai 1:9db0e321a9f4 88 MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
kenjiArai 0:5b88d5760320 89 nsapi_error_t connect(const char *host, uint16_t port);
kenjiArai 0:5b88d5760320 90
kenjiArai 0:5b88d5760320 91 /** Connects TCP socket to a remote host
kenjiArai 0:5b88d5760320 92 *
kenjiArai 0:5b88d5760320 93 * Initiates a connection to a remote server specified by the
kenjiArai 0:5b88d5760320 94 * indicated address.
kenjiArai 0:5b88d5760320 95 *
kenjiArai 0:5b88d5760320 96 * @param address The SocketAddress of the remote host
kenjiArai 1:9db0e321a9f4 97 * @retval NSAPI_ERROR_OK on success
kenjiArai 1:9db0e321a9f4 98 * @retval NSAPI_ERROR_IN_PROGRESS if the operation is ongoing
kenjiArai 1:9db0e321a9f4 99 * @retval NSAPI_ERROR_NO_SOCKET if the socket has not been allocated
kenjiArai 1:9db0e321a9f4 100 * @retval NSAPI_ERROR_DNS_FAILURE if the DNS address of host could not be resolved
kenjiArai 1:9db0e321a9f4 101 * @retval NSAPI_ERROR_IS_CONNECTED if the connection is already established
kenjiArai 1:9db0e321a9f4 102 * @retval int Other negative error codes for stack-related failures.
kenjiArai 1:9db0e321a9f4 103 * See NetworkStack::socket_connect().
kenjiArai 0:5b88d5760320 104 */
kenjiArai 0:5b88d5760320 105 virtual nsapi_error_t connect(const SocketAddress &address);
kenjiArai 0:5b88d5760320 106
kenjiArai 0:5b88d5760320 107 /** Send data over a TCP socket
kenjiArai 0:5b88d5760320 108 *
kenjiArai 0:5b88d5760320 109 * The socket must be connected to a remote host. Returns the number of
kenjiArai 0:5b88d5760320 110 * bytes sent from the buffer.
kenjiArai 0:5b88d5760320 111 *
kenjiArai 0:5b88d5760320 112 * By default, send blocks until all data is sent. If socket is set to
kenjiArai 0:5b88d5760320 113 * non-blocking or times out, a partial amount can be written.
kenjiArai 0:5b88d5760320 114 * NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
kenjiArai 0:5b88d5760320 115 *
kenjiArai 0:5b88d5760320 116 * @param data Buffer of data to send to the host
kenjiArai 0:5b88d5760320 117 * @param size Size of the buffer in bytes
kenjiArai 1:9db0e321a9f4 118 * @retval int Number of sent bytes on success
kenjiArai 1:9db0e321a9f4 119 * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly
kenjiArai 1:9db0e321a9f4 120 * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
kenjiArai 1:9db0e321a9f4 121 * and send cannot be performed immediately
kenjiArai 1:9db0e321a9f4 122 * @retval int Other negative error codes for stack-related failures.
kenjiArai 1:9db0e321a9f4 123 * See @ref NetworkStack::socket_send.
kenjiArai 0:5b88d5760320 124 */
kenjiArai 0:5b88d5760320 125 virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size);
kenjiArai 0:5b88d5760320 126
kenjiArai 0:5b88d5760320 127 /** Receive data over a TCP socket
kenjiArai 0:5b88d5760320 128 *
kenjiArai 0:5b88d5760320 129 * The socket must be connected to a remote host. Returns the number of
kenjiArai 0:5b88d5760320 130 * bytes received into the buffer.
kenjiArai 0:5b88d5760320 131 *
kenjiArai 0:5b88d5760320 132 * By default, recv blocks until some data is received. If socket is set to
kenjiArai 0:5b88d5760320 133 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
kenjiArai 0:5b88d5760320 134 * indicate no data.
kenjiArai 0:5b88d5760320 135 *
kenjiArai 0:5b88d5760320 136 * @param data Destination buffer for data received from the host
kenjiArai 0:5b88d5760320 137 * @param size Size of the buffer in bytes
kenjiArai 1:9db0e321a9f4 138 * @retval int Number of received bytes on success
kenjiArai 1:9db0e321a9f4 139 * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly
kenjiArai 1:9db0e321a9f4 140 * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
kenjiArai 1:9db0e321a9f4 141 * and send cannot be performed immediately
kenjiArai 1:9db0e321a9f4 142 * @retval int Other negative error codes for stack-related failures.
kenjiArai 1:9db0e321a9f4 143 * See @ref NetworkStack::socket_recv.
kenjiArai 0:5b88d5760320 144 */
kenjiArai 0:5b88d5760320 145 virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size);
kenjiArai 0:5b88d5760320 146
kenjiArai 0:5b88d5760320 147 /** Send data on a socket.
kenjiArai 0:5b88d5760320 148 *
kenjiArai 0:5b88d5760320 149 * TCP socket is connection oriented protocol, so address is ignored.
kenjiArai 0:5b88d5760320 150 *
kenjiArai 0:5b88d5760320 151 * By default, sendto blocks until data is sent. If socket is set to
kenjiArai 0:5b88d5760320 152 * non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
kenjiArai 0:5b88d5760320 153 * immediately.
kenjiArai 0:5b88d5760320 154 *
kenjiArai 0:5b88d5760320 155 * @param address Remote address
kenjiArai 0:5b88d5760320 156 * @param data Buffer of data to send to the host
kenjiArai 0:5b88d5760320 157 * @param size Size of the buffer in bytes
kenjiArai 1:9db0e321a9f4 158 * @retval int Number of sent bytes on success
kenjiArai 1:9db0e321a9f4 159 * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly
kenjiArai 1:9db0e321a9f4 160 * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
kenjiArai 1:9db0e321a9f4 161 * and send cannot be performed immediately
kenjiArai 1:9db0e321a9f4 162 * @retval int Other negative error codes for stack-related failures.
kenjiArai 1:9db0e321a9f4 163 * See @ref NetworkStack::socket_send.
kenjiArai 0:5b88d5760320 164 */
kenjiArai 0:5b88d5760320 165 virtual nsapi_size_or_error_t sendto(const SocketAddress &address,
kenjiArai 0:5b88d5760320 166 const void *data, nsapi_size_t size);
kenjiArai 0:5b88d5760320 167
kenjiArai 0:5b88d5760320 168 /** Receive a data from a socket
kenjiArai 0:5b88d5760320 169 *
kenjiArai 0:5b88d5760320 170 * Receives a data and stores the source address in address if address
kenjiArai 0:5b88d5760320 171 * is not NULL. Returns the number of bytes written into the buffer.
kenjiArai 0:5b88d5760320 172 *
kenjiArai 0:5b88d5760320 173 * By default, recvfrom blocks until a data is received. If socket is set to
kenjiArai 0:5b88d5760320 174 * non-blocking or times out with no datagram, NSAPI_ERROR_WOULD_BLOCK
kenjiArai 0:5b88d5760320 175 * is returned.
kenjiArai 0:5b88d5760320 176 *
kenjiArai 0:5b88d5760320 177 * @param address Destination for the source address or NULL
kenjiArai 0:5b88d5760320 178 * @param data Destination buffer for datagram received from the host
kenjiArai 0:5b88d5760320 179 * @param size Size of the buffer in bytes
kenjiArai 1:9db0e321a9f4 180 * @retval int Number of received bytes on success
kenjiArai 1:9db0e321a9f4 181 * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly
kenjiArai 1:9db0e321a9f4 182 * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled
kenjiArai 1:9db0e321a9f4 183 * and send cannot be performed immediately
kenjiArai 1:9db0e321a9f4 184 * @retval int Other negative error codes for stack-related failures.
kenjiArai 1:9db0e321a9f4 185 * See @ref NetworkStack::socket_recv.
kenjiArai 0:5b88d5760320 186 */
kenjiArai 0:5b88d5760320 187 virtual nsapi_size_or_error_t recvfrom(SocketAddress *address,
kenjiArai 0:5b88d5760320 188 void *data, nsapi_size_t size);
kenjiArai 0:5b88d5760320 189
kenjiArai 0:5b88d5760320 190 /** Accepts a connection on a socket.
kenjiArai 0:5b88d5760320 191 *
kenjiArai 0:5b88d5760320 192 * The server socket must be bound and set to listen for connections.
kenjiArai 0:5b88d5760320 193 * On a new connection, returns connected network socket which user is expected to call close()
kenjiArai 0:5b88d5760320 194 * and that deallocates the resources. Referencing a returned pointer after a close()
kenjiArai 0:5b88d5760320 195 * call is not allowed and leads to undefined behavior.
kenjiArai 0:5b88d5760320 196 *
kenjiArai 0:5b88d5760320 197 * By default, accept blocks until incoming connection occurs. If socket is set to
kenjiArai 0:5b88d5760320 198 * non-blocking or times out, error is set to NSAPI_ERROR_WOULD_BLOCK.
kenjiArai 0:5b88d5760320 199 *
kenjiArai 1:9db0e321a9f4 200 * @param error pointer to storage of the error value or NULL:
kenjiArai 1:9db0e321a9f4 201 * NSAPI_ERROR_OK on success
kenjiArai 1:9db0e321a9f4 202 * NSAPI_ERROR_WOULD_BLOCK if socket is set to non-blocking and would block
kenjiArai 1:9db0e321a9f4 203 * NSAPI_ERROR_NO_SOCKET if the socket was not open
kenjiArai 0:5b88d5760320 204 * @return pointer to a socket
kenjiArai 0:5b88d5760320 205 */
kenjiArai 0:5b88d5760320 206 virtual TCPSocket *accept(nsapi_error_t *error = NULL);
kenjiArai 0:5b88d5760320 207
kenjiArai 0:5b88d5760320 208 /** Listen for incoming connections.
kenjiArai 0:5b88d5760320 209 *
kenjiArai 0:5b88d5760320 210 * Marks the socket as a passive socket that can be used to accept
kenjiArai 0:5b88d5760320 211 * incoming connections.
kenjiArai 0:5b88d5760320 212 *
kenjiArai 0:5b88d5760320 213 * @param backlog Number of pending connections that can be queued
kenjiArai 0:5b88d5760320 214 * simultaneously, defaults to 1
kenjiArai 1:9db0e321a9f4 215 * @retval NSAPI_ERROR_OK on success
kenjiArai 1:9db0e321a9f4 216 * @retval NSAPI_ERROR_NO_SOCKET in case socket was not created correctly
kenjiArai 1:9db0e321a9f4 217 * @retval int Other negative error codes for stack-related failures.
kenjiArai 1:9db0e321a9f4 218 * See @ref NetworkStack::socket_listen.
kenjiArai 0:5b88d5760320 219 */
kenjiArai 0:5b88d5760320 220 virtual nsapi_error_t listen(int backlog = 1);
kenjiArai 0:5b88d5760320 221
kenjiArai 0:5b88d5760320 222 protected:
kenjiArai 0:5b88d5760320 223 friend class TCPServer;
kenjiArai 0:5b88d5760320 224 virtual nsapi_protocol_t get_proto();
kenjiArai 0:5b88d5760320 225
kenjiArai 0:5b88d5760320 226 private:
kenjiArai 0:5b88d5760320 227 /** Create a socket out of a given socket
kenjiArai 0:5b88d5760320 228 *
kenjiArai 0:5b88d5760320 229 * To be used within accept() function. Close() will clean this up.
kenjiArai 0:5b88d5760320 230 */
kenjiArai 0:5b88d5760320 231 TCPSocket(TCPSocket *parent, nsapi_socket_t socket, SocketAddress address);
kenjiArai 0:5b88d5760320 232 };
kenjiArai 0:5b88d5760320 233
kenjiArai 0:5b88d5760320 234
kenjiArai 0:5b88d5760320 235 #endif
kenjiArai 0:5b88d5760320 236
kenjiArai 0:5b88d5760320 237 /** @}*/