BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Committer:
gustavatmel
Date:
Tue May 01 15:55:34 2018 +0000
Revision:
2:798925c9e4a8
Parent:
1:9c5af431a1f1
bluetooth

Who changed what in which revision?

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