mbed-os/features/netsocket/InternetSocket.h

238 lines
8.8 KiB
C
Raw Normal View History

/** \addtogroup netsocket */
/** @{*/
/* Socket
* Copyright (c) 2015 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef INTERNETSOCKET_H
#define INTERNETSOCKET_H
#include "netsocket/Socket.h"
#include "netsocket/NetworkStack.h"
#include "rtos/Mutex.h"
#include "rtos/EventFlags.h"
#include "Callback.h"
#include "mbed_toolchain.h"
/** Socket implementation that uses IP network stack.
* Not to be directly used by applications. Cannot be directly instantiated.
*/
class InternetSocket : public Socket {
public:
2018-10-24 15:50:35 +00:00
/** Destroy the socket.
*
2018-10-24 15:50:35 +00:00
* @note Closes socket if it's still open.
*/
virtual ~InternetSocket() {}
2018-10-24 15:50:35 +00:00
/** Open a network socket on the network stack of the given
* network interface.
*
2018-10-24 15:50:35 +00:00
* @note Not needed if stack is passed to the socket's constructor.
*
2018-10-24 15:50:35 +00:00
* @param stack Network stack as target for socket.
* @return 0 on success, negative error code on failure (@see nsapi_types.h).
*/
nsapi_error_t open(NetworkStack *stack);
2018-10-24 15:50:35 +00:00
#if !defined(DOXYGEN_ONLY)
template <typename S>
2018-08-03 12:32:29 +00:00
nsapi_error_t open(S *stack)
{
return open(nsapi_create_stack(stack));
}
2018-10-24 15:50:35 +00:00
#endif //!defined(DOXYGEN_ONLY)
2018-10-24 15:50:35 +00:00
/** Close any open connection and deallocate any memory associated
* with the socket. Called from destructor if socket is not closed.
*
2018-10-24 15:50:35 +00:00
* @return 0 on success, negative error code on failure (@see nsapi_types.h).
*/
virtual nsapi_error_t close();
2018-10-24 15:50:35 +00:00
/** Subscribe to an IP multicast group.
*
2018-10-24 15:50:35 +00:00
* @param address Multicast group IP address.
* @return 0 on success, negative error code on failure (@see nsapi_types.h).
*/
int join_multicast_group(const SocketAddress &address);
2018-10-24 15:50:35 +00:00
/** Leave an IP multicast group.
*
2018-10-24 15:50:35 +00:00
* @param address Multicast group IP address.
* @return 0 on success, negative error code on failure (@see nsapi_types.h).
*/
int leave_multicast_group(const SocketAddress &address);
2018-10-24 15:50:35 +00:00
/** Bind the socket to a port on which to receive data.
*
2018-10-24 15:50:35 +00:00
* @param port Local port to bind.
* @return 0 on success, negative error code on failure (@see nsapi_types.h).
*/
nsapi_error_t bind(uint16_t port);
2018-10-24 15:50:35 +00:00
/** Bind the socket to a specific address and port on which to receive
* data. If the IP address is zeroed only the port is bound.
*
2018-10-24 15:50:35 +00:00
* @param address Null-terminated local address to bind.
* @param port Local port to bind.
* @return 0 on success, negative error code on failure (@see nsapi_types.h).
*/
nsapi_error_t bind(const char *address, uint16_t port);
2018-10-24 15:50:35 +00:00
/** Bind the socket to a specific address and port on which to receive
* data. If the IP address is zeroed, only the port is bound.
*
2018-10-24 15:50:35 +00:00
* @param address Local SocketAddress to bind which includes the address and port.
* @return 0 on success, negative error code on failure (@see nsapi_types.h).
*/
virtual nsapi_error_t bind(const SocketAddress &address);
2018-10-24 15:50:35 +00:00
/** Set blocking or non-blocking mode of the socket.
*
* Initially all sockets are in blocking mode. In non-blocking mode
* blocking operations such as send/recv/accept return
* NSAPI_ERROR_WOULD_BLOCK if they can not continue.
*
2018-10-24 15:50:35 +00:00
* @note set_blocking(false) is equivalent to set_timeout(-1) and
* set_blocking(true) is equivalent to set_timeout(0).
*
2018-10-24 15:50:35 +00:00
* @param blocking Use true for blocking mode, false for non-blocking mode.
*/
virtual void set_blocking(bool blocking);
2018-10-24 15:50:35 +00:00
/** Set timeout on blocking socket operations.
*
* Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK
* is returned if a blocking operation takes longer than the specified
* timeout. A timeout of 0 removes the timeout from the socket. A negative
* value give the socket an unbounded timeout.
*
2018-10-24 15:50:35 +00:00
* @note set_timeout(0) is equivalent to set_blocking(false) and
* set_timeout(-1) is equivalent to set_blocking(true).
*
2018-10-24 15:50:35 +00:00
* @param timeout Timeout in milliseconds.
*/
virtual void set_timeout(int timeout);
2018-10-24 15:50:35 +00:00
/** Pass stack-specific options to the underlying stack using stack-specific
* level and option names, or request generic options using levels from nsapi_socket_level_t.
*
* For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned
* and the socket is unmodified.
*
2018-10-24 15:50:35 +00:00
* @param level Stack-specific protocol level or nsapi_socket_level_t.
* @param optname Level-specific option name.
* @param optval Option value.
* @param optlen Length of the option value.
* @return 0 on success, negative error code on failure (@see nsapi_types.h).
*/
virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen);
2018-10-24 15:50:35 +00:00
/** Retrieve stack-specific options from the underlying stack using
* stack-specific level and option names, or request generic options
* using levels from nsapi_socket_level_t.
*
* For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned
* and the socket is unmodified.
*
2018-10-24 15:50:35 +00:00
* @param level Stack-specific protocol level or nsapi_socket_level_t.
* @param optname Level-specific option name.
* @param optval Destination for option value.
* @param optlen Length of the option value.
* @return 0 on success, negative error code on failure (@see nsapi_types.h).
*/
virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen);
2018-10-24 15:50:35 +00:00
/** Register a callback on state change of the socket.
*
* The specified callback will be called on state changes such as when
2018-10-24 15:50:35 +00:00
* the socket can recv/send/accept successfully and when an error
* occurs. The callback may also be called spuriously without a reason.
*
* The callback may be called in an interrupt context and should not
* perform expensive operations such as recv/send calls.
*
2018-10-24 15:50:35 +00:00
* @note This is not intended as a replacement for a poll or attach-like
* asynchronous API, but rather as a building block for constructing
* such functionality. The exact timing of when the registered function
2018-10-24 15:50:35 +00:00
* is called is not guaranteed and is susceptible to change.
*
2018-10-24 15:50:35 +00:00
* @param func Function to call on state change.
*/
virtual void sigio(mbed::Callback<void()> func);
2018-10-24 15:50:35 +00:00
/** Register a callback on state change of the socket.
*
* @see Socket::sigio
* @deprecated
* The behaviour of Socket::attach differs from other attach functions in
* mbed OS and has been known to cause confusion. Replaced by Socket::sigio.
*/
MBED_DEPRECATED_SINCE("mbed-os-5.4",
2018-08-03 12:32:29 +00:00
"The behaviour of Socket::attach differs from other attach functions in "
"mbed OS and has been known to cause confusion. Replaced by Socket::sigio.")
void attach(mbed::Callback<void()> func);
2018-10-24 15:50:35 +00:00
/** Register a callback on state change of the socket.
*
* @see Socket::sigio
* @deprecated
* The attach function does not support cv-qualifiers. Replaced by
* attach(callback(obj, method)).
*/
template <typename T, typename M>
MBED_DEPRECATED_SINCE("mbed-os-5.1",
2018-08-03 12:32:29 +00:00
"The attach function does not support cv-qualifiers. Replaced by "
"attach(callback(obj, method)).")
void attach(T *obj, M method)
{
attach(mbed::callback(obj, method));
}
2018-10-24 15:50:35 +00:00
#if !defined(DOXYGEN_ONLY)
protected:
InternetSocket();
virtual nsapi_protocol_t get_proto() = 0;
virtual void event();
int modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt);
NetworkStack *_stack;
nsapi_socket_t _socket;
uint32_t _timeout;
mbed::Callback<void()> _event;
mbed::Callback<void()> _callback;
rtos::EventFlags _event_flag;
rtos::Mutex _lock;
SocketAddress _remote_peer;
uint8_t _readers;
uint8_t _writers;
volatile unsigned _pending;
bool _factory_allocated;
// Event flags
static const int READ_FLAG = 0x1u;
static const int WRITE_FLAG = 0x2u;
static const int FINISHED_FLAG = 0x3u;
2018-10-24 15:50:35 +00:00
#endif //!defined(DOXYGEN_ONLY)
};
#endif // INTERNETSOCKET_H
2018-06-19 13:35:16 +00:00
/** @}*/