Mistake on this page?
Report an issue in GitHub or email us
InternetSocket.h
1 
2 /** \addtogroup netsocket */
3 /** @{*/
4 /* Socket
5  * Copyright (c) 2015 ARM Limited
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef INTERNETSOCKET_H
21 #define INTERNETSOCKET_H
22 
23 #include "netsocket/Socket.h"
24 #include "netsocket/NetworkStack.h"
25 #include "rtos/Mutex.h"
26 #include "rtos/EventFlags.h"
27 #include "Callback.h"
28 #include "mbed_atomic.h"
29 #include "mbed_toolchain.h"
30 #include "SocketStats.h"
31 
32 /** Socket implementation that uses IP network stack.
33  * Not to be directly used by applications. Cannot be directly instantiated.
34  */
35 class InternetSocket : public Socket {
36 public:
37  /** Destroy the socket.
38  *
39  * @note Closes socket if it's still open.
40  */
41  virtual ~InternetSocket();
42 
43  /** Open a network socket on the network stack of the given
44  * network interface.
45  *
46  * @note Not needed if stack is passed to the socket's constructor.
47  *
48  * @param stack Network stack as target for socket.
49  * @return 0 on success, negative error code on failure (@see nsapi_types.h).
50  */
52 
53 #if !defined(DOXYGEN_ONLY)
54  template <typename S>
55  nsapi_error_t open(S *stack)
56  {
57  return open(nsapi_create_stack(stack));
58  }
59 #endif //!defined(DOXYGEN_ONLY)
60 
61  /** Close any open connection, and deallocate any memory associated
62  * with the socket. Called from destructor if socket is not closed.
63  *
64  * @return 0 on success, negative error code on failure (@see nsapi_types.h).
65  */
66  virtual nsapi_error_t close();
67 
68  /** Subscribe to an IP multicast group.
69  *
70  * @param address Multicast group IP address.
71  * @return 0 on success, negative error code on failure (@see nsapi_types.h).
72  */
73  int join_multicast_group(const SocketAddress &address);
74 
75  /** Leave an IP multicast group.
76  *
77  * @param address Multicast group IP address.
78  * @return 0 on success, negative error code on failure (@see nsapi_types.h).
79  */
80  int leave_multicast_group(const SocketAddress &address);
81 
82  /** Bind the socket to a port on which to receive data.
83  *
84  * @param port Local port to bind.
85  * @return 0 on success, negative error code on failure (@see nsapi_types.h).
86  */
87  nsapi_error_t bind(uint16_t port);
88 
89  /** Bind the socket to a specific address and port on which to receive
90  * data. If the IP address is zeroed, only the port is bound.
91  *
92  * @param address Null-terminated local address to bind.
93  * @param port Local port to bind.
94  * @return 0 on success, negative error code on failure (@see nsapi_types.h).
95  */
96  nsapi_error_t bind(const char *address, uint16_t port);
97 
98  /** @copydoc Socket::bind
99  */
100  virtual nsapi_error_t bind(const SocketAddress &address);
101 
102  /** @copydoc Socket::set_blocking
103  */
104  virtual void set_blocking(bool blocking);
105 
106  /** @copydoc Socket::set_timeout
107  */
108  virtual void set_timeout(int timeout);
109 
110  /** @copydoc Socket::setsockopt
111  */
112  virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen);
113 
114  /** @copydoc Socket::getsockopt
115  */
116  virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen);
117 
118  /** @copydoc Socket::sigio
119  */
120  virtual void sigio(mbed::Callback<void()> func);
121 
122  /** @copydoc Socket::getpeername
123  */
124  virtual nsapi_error_t getpeername(SocketAddress *address);
125 
126  /** Register a callback on state change of the socket.
127  *
128  * @see Socket::sigio
129  * @deprecated
130  * The behavior of Socket::attach differs from other attach functions in
131  * Mbed OS and has been known to cause confusion. Replaced by Socket::sigio.
132  */
133  MBED_DEPRECATED_SINCE("mbed-os-5.4",
134  "The behavior of Socket::attach differs from other attach functions in "
135  "Mbed OS and has been known to cause confusion. Replaced by Socket::sigio.")
136  void attach(mbed::Callback<void()> func);
137 
138  /** Register a callback on state change of the socket.
139  *
140  * @see Socket::sigio
141  * @deprecated
142  * The attach function does not support cv-qualifiers. Replaced by
143  * attach(callback(obj, method)).
144  */
145  template <typename T, typename M>
146  MBED_DEPRECATED_SINCE("mbed-os-5.1",
147  "The attach function does not support cv-qualifiers. Replaced by "
148  "attach(callback(obj, method)).")
149  void attach(T *obj, M method)
150  {
151  attach(mbed::callback(obj, method));
152  }
153 
154 #if !defined(DOXYGEN_ONLY)
155 
156 protected:
157  InternetSocket();
158  virtual nsapi_protocol_t get_proto() = 0;
159  virtual void event();
160  int modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt);
161  char _interface_name[NSAPI_INTERFACE_NAME_MAX_SIZE];
162  NetworkStack *_stack;
163  nsapi_socket_t _socket;
164  uint32_t _timeout;
165  mbed::Callback<void()> _event;
166  mbed::Callback<void()> _callback;
167  rtos::EventFlags _event_flag;
168  rtos::Mutex _lock;
169  SocketAddress _remote_peer;
170  uint8_t _readers;
171  uint8_t _writers;
172  core_util_atomic_flag _pending;
173  bool _factory_allocated;
174 
175  // Event flags
176  static const int READ_FLAG = 0x1u;
177  static const int WRITE_FLAG = 0x2u;
178  static const int FINISHED_FLAG = 0x3u;
179 
180  friend class DTLSSocket; // Allow DTLSSocket::connect() to do name resolution on the _stack
181  SocketStats _socket_stats;
182 
183 #endif //!defined(DOXYGEN_ONLY)
184 };
185 
186 #endif // INTERNETSOCKET_H
187 
188 /** @}*/
nsapi_error_t open(NetworkStack *stack)
Open a network socket on the network stack of the given network interface.
Socket implementation that uses IP network stack.
DTLSSocket implement DTLS stream over UDP Socket.
Definition: DTLSSocket.h:39
virtual void sigio(mbed::Callback< void()> func)
Register a callback on state change of the socket.
NetworkStack * nsapi_create_stack(nsapi_stack_t *stack)
Convert a raw nsapi_stack_t object into a C++ NetworkStack object.
int join_multicast_group(const SocketAddress &address)
Subscribe to an IP multicast group.
The EventFlags class is used to control event flags or wait for event flags other threads control...
Definition: EventFlags.h:49
virtual nsapi_error_t getpeername(SocketAddress *address)
Get the remote-end peer associated with this socket.
NetworkStack class.
Definition: NetworkStack.h:40
void * nsapi_socket_t
Opaque handle for network sockets.
Definition: nsapi_types.h:204
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:95
#define NSAPI_INTERFACE_NAME_MAX_SIZE
Maximum size of network interface name.
Definition: nsapi_types.h:139
A lock-free, primitive atomic flag.
Definition: mbed_atomic.h:115
Callback< R()> callback(R(*func)()=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:3848
Abstract Socket interface.
virtual ~InternetSocket()
Destroy the socket.
void attach(mbed::Callback< void()> func)
Register a callback on state change of the socket.
virtual nsapi_error_t close()
defined(DOXYGEN_ONLY)
virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen)
Get socket options.
SocketAddress class.
Definition: SocketAddress.h:35
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:66
Socket interface.
Definition: Socket.h:39
virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen)
Set socket options.
SocketStats class.
Definition: SocketStats.h:59
NetworkStack class.
int leave_multicast_group(const SocketAddress &address)
Leave an IP multicast group.
virtual void set_blocking(bool blocking)
Set blocking or non-blocking mode of the socket.
virtual void set_timeout(int timeout)
Set timeout on blocking socket operations.
Callback class based on template specialization.
Definition: Callback.h:39
nsapi_error_t bind(uint16_t port)
Bind the socket to a port on which to receive data.
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.