Skip to content

Commit 570d255

Browse files
author
Seppo Takalo
committed
Refactor Sockets by moving functions up into common base.
* Move IP Socket stuff to InternetSocket class which is inherited by TCP/UDP * Implement sendto() and recvfrom() on TCP socket * Implement connect() call on UDP * Implement send() and recv() calls on UDP socket
1 parent ed9a1f1 commit 570d255

File tree

12 files changed

+539
-159
lines changed

12 files changed

+539
-159
lines changed

features/netsocket/Socket.cpp renamed to features/netsocket/InternetSocket.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Socket
1+
/* InternetSocket
22
* Copyright (c) 2015 ARM Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,17 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "Socket.h"
18-
#include "mbed.h"
17+
#include "InternetSocket.h"
18+
#include "platform/Callback.h"
1919

20-
Socket::Socket()
20+
using namespace mbed;
21+
22+
InternetSocket::InternetSocket()
2123
: _stack(0)
2224
, _socket(0)
2325
, _timeout(osWaitForever)
2426
{
2527
}
2628

27-
nsapi_error_t Socket::open(NetworkStack *stack)
29+
nsapi_error_t InternetSocket::open(NetworkStack *stack)
2830
{
2931
_lock.lock();
3032

@@ -42,14 +44,14 @@ nsapi_error_t Socket::open(NetworkStack *stack)
4244
}
4345

4446
_socket = socket;
45-
_event = callback(this, &Socket::event);
47+
_event = callback(this, &InternetSocket::event);
4648
_stack->socket_attach(_socket, Callback<void()>::thunk, &_event);
4749

4850
_lock.unlock();
4951
return NSAPI_ERROR_OK;
5052
}
5153

52-
nsapi_error_t Socket::close()
54+
nsapi_error_t InternetSocket::close()
5355
{
5456
_lock.lock();
5557

@@ -70,7 +72,7 @@ nsapi_error_t Socket::close()
7072
return ret;
7173
}
7274

73-
int Socket::modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt)
75+
int InternetSocket::modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt)
7476
{
7577
nsapi_ip_mreq_t mreq;
7678

@@ -81,32 +83,32 @@ int Socket::modify_multicast_group(const SocketAddress &address, nsapi_socket_op
8183
return this->setsockopt(NSAPI_SOCKET, socketopt, &mreq, sizeof(mreq));
8284
}
8385

84-
int Socket::join_multicast_group(const SocketAddress &address)
86+
int InternetSocket::join_multicast_group(const SocketAddress &address)
8587
{
8688
return modify_multicast_group(address, NSAPI_ADD_MEMBERSHIP);
8789
}
8890

89-
int Socket::leave_multicast_group(const SocketAddress &address)
91+
int InternetSocket::leave_multicast_group(const SocketAddress &address)
9092
{
9193
return modify_multicast_group(address, NSAPI_DROP_MEMBERSHIP);
9294
}
9395

9496

95-
nsapi_error_t Socket::bind(uint16_t port)
97+
nsapi_error_t InternetSocket::bind(uint16_t port)
9698
{
9799
// Underlying bind is thread safe
98100
SocketAddress addr(0, port);
99101
return bind(addr);
100102
}
101103

102-
nsapi_error_t Socket::bind(const char *address, uint16_t port)
104+
nsapi_error_t InternetSocket::bind(const char *address, uint16_t port)
103105
{
104106
// Underlying bind is thread safe
105107
SocketAddress addr(address, port);
106108
return bind(addr);
107109
}
108110

109-
nsapi_error_t Socket::bind(const SocketAddress &address)
111+
nsapi_error_t InternetSocket::bind(const SocketAddress &address)
110112
{
111113
_lock.lock();
112114
nsapi_error_t ret;
@@ -121,13 +123,13 @@ nsapi_error_t Socket::bind(const SocketAddress &address)
121123
return ret;
122124
}
123125

124-
void Socket::set_blocking(bool blocking)
126+
void InternetSocket::set_blocking(bool blocking)
125127
{
126-
// Socket::set_timeout is thread safe
128+
// InternetSocket::set_timeout is thread safe
127129
set_timeout(blocking ? -1 : 0);
128130
}
129131

130-
void Socket::set_timeout(int timeout)
132+
void InternetSocket::set_timeout(int timeout)
131133
{
132134
_lock.lock();
133135

@@ -140,7 +142,7 @@ void Socket::set_timeout(int timeout)
140142
_lock.unlock();
141143
}
142144

143-
nsapi_error_t Socket::setsockopt(int level, int optname, const void *optval, unsigned optlen)
145+
nsapi_error_t InternetSocket::setsockopt(int level, int optname, const void *optval, unsigned optlen)
144146
{
145147
_lock.lock();
146148
nsapi_error_t ret;
@@ -155,7 +157,7 @@ nsapi_error_t Socket::setsockopt(int level, int optname, const void *optval, uns
155157
return ret;
156158
}
157159

158-
nsapi_error_t Socket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
160+
nsapi_error_t InternetSocket::getsockopt(int level, int optname, void *optval, unsigned *optlen)
159161
{
160162
_lock.lock();
161163
nsapi_error_t ret;
@@ -171,14 +173,14 @@ nsapi_error_t Socket::getsockopt(int level, int optname, void *optval, unsigned
171173

172174
}
173175

174-
void Socket::sigio(Callback<void()> callback)
176+
void InternetSocket::sigio(Callback<void()> callback)
175177
{
176178
_lock.lock();
177179
_callback = callback;
178180
_lock.unlock();
179181
}
180182

181-
void Socket::attach(Callback<void()> callback)
183+
void InternetSocket::attach(Callback<void()> callback)
182184
{
183185
sigio(callback);
184186
}

features/netsocket/InternetSocket.h

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
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 "Callback.h"
27+
#include "mbed_toolchain.h"
28+
29+
/** Socket implementation that uses IP network stack.
30+
* Not to be directly used by applications. Cannot be directly instantiated.
31+
*/
32+
class InternetSocket : public Socket {
33+
public:
34+
/** Destroy a socket
35+
*
36+
* Closes socket if the socket is still open
37+
*/
38+
virtual ~InternetSocket() {}
39+
40+
/** Opens a socket
41+
*
42+
* Creates a network socket on the network stack of the given
43+
* network interface. Not needed if stack is passed to the
44+
* socket's constructor.
45+
*
46+
* @param stack Network stack as target for socket
47+
* @return 0 on success, negative error code on failure
48+
*/
49+
nsapi_error_t open(NetworkStack *stack);
50+
51+
template <typename S>
52+
nsapi_error_t open(S *stack) {
53+
return open(nsapi_create_stack(stack));
54+
}
55+
56+
/** Close the socket
57+
*
58+
* Closes any open connection and deallocates any memory associated
59+
* with the socket. Called from destructor if socket is not closed.
60+
*
61+
* @return 0 on success, negative error code on failure
62+
*/
63+
virtual nsapi_error_t close();
64+
65+
/** Subscribes to an IP multicast group
66+
*
67+
* @param address Multicast group IP address
68+
* @return Negative error code on failure
69+
*/
70+
int join_multicast_group(const SocketAddress &address);
71+
72+
/** Leave an IP multicast group
73+
*
74+
* @param address Multicast group IP address
75+
* @return Negative error code on failure
76+
*/
77+
int leave_multicast_group(const SocketAddress &address);
78+
79+
80+
/** Bind a specific address to a socket
81+
*
82+
* Binding a socket specifies the address and port on which to receive
83+
* data.
84+
*
85+
* @param port Local port to bind
86+
* @return 0 on success, negative error code on failure.
87+
*/
88+
nsapi_error_t bind(uint16_t port);
89+
90+
/** Bind a specific address to a socket
91+
*
92+
* Binding a socket specifies the address and port on which to receive
93+
* data. If the IP address is zeroed, only the port is bound.
94+
*
95+
* @param address Null-terminated local address to bind
96+
* @param port Local port to bind
97+
* @return 0 on success, negative error code on failure.
98+
*/
99+
nsapi_error_t bind(const char *address, uint16_t port);
100+
101+
/** Bind a specific address to a socket
102+
*
103+
* Binding a socket specifies the address and port on which to receive
104+
* data. If the IP address is zeroed, only the port is bound.
105+
*
106+
* @param address Local address to bind
107+
* @return 0 on success, negative error code on failure.
108+
*/
109+
virtual nsapi_error_t bind(const SocketAddress &address);
110+
111+
/** Set blocking or non-blocking mode of the socket
112+
*
113+
* Initially all sockets are in blocking mode. In non-blocking mode
114+
* blocking operations such as send/recv/accept return
115+
* NSAPI_ERROR_WOULD_BLOCK if they can not continue.
116+
*
117+
* set_blocking(false) is equivalent to set_timeout(-1)
118+
* set_blocking(true) is equivalent to set_timeout(0)
119+
*
120+
* @param blocking true for blocking mode, false for non-blocking mode.
121+
*/
122+
virtual void set_blocking(bool blocking);
123+
124+
/** Set timeout on blocking socket operations
125+
*
126+
* Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK
127+
* is returned if a blocking operation takes longer than the specified
128+
* timeout. A timeout of 0 removes the timeout from the socket. A negative
129+
* value give the socket an unbounded timeout.
130+
*
131+
* set_timeout(0) is equivalent to set_blocking(false)
132+
* set_timeout(-1) is equivalent to set_blocking(true)
133+
*
134+
* @param timeout Timeout in milliseconds
135+
*/
136+
virtual void set_timeout(int timeout);
137+
138+
/* Set socket options
139+
*
140+
* setsockopt allows an application to pass stack-specific options
141+
* to the underlying stack using stack-specific level and option names,
142+
* or to request generic options using levels from nsapi_socket_level_t.
143+
*
144+
* For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned
145+
* and the socket is unmodified.
146+
*
147+
* @param level Stack-specific protocol level or nsapi_socket_level_t
148+
* @param optname Level-specific option name
149+
* @param optval Option value
150+
* @param optlen Length of the option value
151+
* @return 0 on success, negative error code on failure
152+
*/
153+
virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen);
154+
155+
/* Get socket options
156+
*
157+
* getsockopt allows an application to retrieve stack-specific options
158+
* from the underlying stack using stack-specific level and option names,
159+
* or to request generic options using levels from nsapi_socket_level_t.
160+
*
161+
* For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned
162+
* and the socket is unmodified.
163+
*
164+
* @param level Stack-specific protocol level or nsapi_socket_level_t
165+
* @param optname Level-specific option name
166+
* @param optval Destination for option value
167+
* @param optlen Length of the option value
168+
* @return 0 on success, negative error code on failure
169+
*/
170+
virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen);
171+
172+
/** Register a callback on state change of the socket
173+
*
174+
* The specified callback will be called on state changes such as when
175+
* the socket can recv/send/accept successfully and on when an error
176+
* occurs. The callback may also be called spuriously without reason.
177+
*
178+
* The callback may be called in an interrupt context and should not
179+
* perform expensive operations such as recv/send calls.
180+
*
181+
* Note! This is not intended as a replacement for a poll or attach-like
182+
* asynchronous api, but rather as a building block for constructing
183+
* such functionality. The exact timing of when the registered function
184+
* is called is not guaranteed and susceptible to change.
185+
*
186+
* @param func Function to call on state change
187+
*/
188+
virtual void sigio(mbed::Callback<void()> func);
189+
190+
/** Register a callback on state change of the socket
191+
*
192+
* @see Socket::sigio
193+
* @deprecated
194+
* The behaviour of Socket::attach differs from other attach functions in
195+
* mbed OS and has been known to cause confusion. Replaced by Socket::sigio.
196+
*/
197+
MBED_DEPRECATED_SINCE("mbed-os-5.4",
198+
"The behaviour of Socket::attach differs from other attach functions in "
199+
"mbed OS and has been known to cause confusion. Replaced by Socket::sigio.")
200+
void attach(mbed::Callback<void()> func);
201+
202+
/** Register a callback on state change of the socket
203+
*
204+
* @see Socket::sigio
205+
* @deprecated
206+
* The attach function does not support cv-qualifiers. Replaced by
207+
* attach(callback(obj, method)).
208+
*/
209+
template <typename T, typename M>
210+
MBED_DEPRECATED_SINCE("mbed-os-5.1",
211+
"The attach function does not support cv-qualifiers. Replaced by "
212+
"attach(callback(obj, method)).")
213+
void attach(T *obj, M method) {
214+
attach(mbed::callback(obj, method));
215+
}
216+
217+
protected:
218+
InternetSocket();
219+
virtual nsapi_protocol_t get_proto() = 0;
220+
virtual void event() = 0;
221+
int modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt);
222+
223+
NetworkStack *_stack;
224+
nsapi_socket_t _socket;
225+
uint32_t _timeout;
226+
mbed::Callback<void()> _event;
227+
mbed::Callback<void()> _callback;
228+
rtos::Mutex _lock;
229+
SocketAddress _remote_peer;
230+
};
231+
232+
#endif // INTERNETSOCKET_H

0 commit comments

Comments
 (0)