21
21
#include " SocketAddress.h"
22
22
23
23
24
- /* * Enum of standardized error codes
24
+ /* * Enum of standardized error codes
25
+ *
26
+ * Valid error codes have negative values and may
27
+ * be returned by any network operation.
28
+ *
25
29
* @enum nsapi_error_t
26
30
*/
27
31
enum nsapi_error_t {
@@ -38,6 +42,10 @@ enum nsapi_error_t {
38
42
};
39
43
40
44
/* * Enum of socket protocols
45
+ *
46
+ * The socket protocol specifies a particular protocol to
47
+ * be used with a newly created socket.
48
+ *
41
49
* @enum protocol_t
42
50
*/
43
51
enum nsapi_protocol_t {
@@ -55,53 +63,66 @@ enum nsapi_protocol_t {
55
63
56
64
57
65
/* * NetworkInterface class
58
- * Common interface that is shared between all hardware that
59
- * can connect to a network over IP.
66
+ *
67
+ * Common interface that is shared between hardware that
68
+ * can connect to a network over IP. By implementing the
69
+ * NetworkInterface, a network stack can be used as a target
70
+ * for instantiating network sockets.
60
71
*/
61
72
class NetworkInterface
62
73
{
63
74
public:
64
75
virtual ~NetworkInterface () {};
65
76
66
77
/* * Get the internally stored IP address
78
+ *
67
79
* @return IP address of the interface or null if not yet connected
68
80
*/
69
81
virtual const char *get_ip_address () = 0;
70
82
71
83
/* * Get the internally stored MAC address
84
+ *
72
85
* @return MAC address of the interface
73
86
*/
74
87
virtual const char *get_mac_address () = 0;
75
88
76
- /* * Get the current status of the interface
77
- * @return true if connected
78
- */
79
- virtual bool is_connected () {
80
- return get_ip_address () != NULL ;
81
- }
82
-
83
- /* * Looks up the specified host's IP address
89
+ /* * Translates a host name to an IP address
90
+ *
91
+ * The host name may be either a domain name or an IP address.
92
+ * If no stack-specific DNS resolution is provided, the host name
93
+ * will be resolve using a UDP socket on the stack.
94
+ *
84
95
* @param address Destination for the host SocketAddress
85
- * @param name Hostname to lookup
86
- * @return 0 on success, negative on failure
96
+ * @param host Host name to lookup
97
+ * @return 0 on success, negative error code on failure
87
98
*/
88
- virtual int gethostbyname (SocketAddress *address, const char *name );
99
+ virtual int gethostbyname (SocketAddress *address, const char *host );
89
100
90
- /* Set stack options
91
- * @param level Option level
92
- * @param optname Option identifier
101
+ /* Set stack-specific stack options
102
+ *
103
+ * The setstackopt allow an application to pass stack-specific hints
104
+ * to the underlying stack. For unsupported options,
105
+ * NSAPI_ERROR_UNSUPPORTED is returned and the stack is unmodified.
106
+ *
107
+ * @param level Stack-specific protocol level
108
+ * @param optname Stack-specific option identifier
93
109
* @param optval Option value
94
110
* @param optlen Length of the option value
95
- * @return 0 on success, negative on failure
111
+ * @return 0 on success, negative error code on failure
96
112
*/
97
113
virtual int setstackopt (int level, int optname, const void *optval, unsigned optlen);
98
114
99
- /* Get stack options
100
- * @param level Option level
101
- * @param optname Option identifier
102
- * @param optval Buffer where to write option value
115
+ /* Get stack-specific stack options
116
+ *
117
+ * The getstackopt allow an application to retrieve stack-specific hints
118
+ * from the underlying stack. For unsupported options,
119
+ * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
120
+ *
121
+ * @param level Stack-specific protocol level
122
+ * @param optname Stack-specific option identifier
123
+ * @param optval Destination for option value
103
124
* @param optlen Length of the option value
104
- * @return 0 on success, negative on failure
125
+ * @return 0 on success, negative error code on failure
105
126
*/
106
127
virtual int getstackopt (int level, int optname, void *optval, unsigned *optlen);
107
128
@@ -111,127 +132,190 @@ class NetworkInterface
111
132
friend class TCPSocket ;
112
133
friend class TCPServer ;
113
134
114
- /* * Open a socket
115
- * @param handle Handle in which to store new socket
116
- * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
117
- * @return 0 on success, negative on failure
135
+ /* * Opens a socket
136
+ *
137
+ * Creates a socket for communication and stores it in the specified
138
+ * handle. The handle must be passed to following calls on the socket.
139
+ *
140
+ * A stack may have a finite number of sockets, in this case
141
+ * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
142
+ *
143
+ * @param handle Destination for the handle to a newly created socket
144
+ * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
145
+ * @return 0 on success, negative error code on failure
118
146
*/
119
147
virtual int socket_open (void **handle, nsapi_protocol_t proto) = 0;
120
148
121
149
/* * Close the socket
150
+ *
151
+ * Closes any open connection and deallocates any memory associated with
152
+ * the socket.
153
+ *
122
154
* @param handle Socket handle
123
- * @return 0 on success, negative on failure
124
- * @note On failure, any memory associated with the socket must still
125
- * be cleaned up
155
+ * @return 0 on success, negative error code on failure
126
156
*/
127
157
virtual int socket_close (void *handle) = 0;
128
158
129
- /* * Bind a server socket to a specific port
159
+ /* * Bind a specific address to a socket
160
+ *
161
+ * Binding a socket specifies the address and port on which to recieve
162
+ * data. If the IP address is zeroed, only the port is bound.
163
+ *
130
164
* @param handle Socket handle
131
- * @param address Local address to listen for incoming connections on
132
- * @return 0 on success, negative on failure.
165
+ * @param address Local address to bind
166
+ * @return 0 on success, negative error code on failure.
133
167
*/
134
168
virtual int socket_bind (void *handle, const SocketAddress &address) = 0;
135
169
136
- /* * Start listening for incoming connections
170
+ /* * Listen for connections on a TCP socket
171
+ *
172
+ * Marks the socket as a passive socket that can be used to accept
173
+ * incoming connections.
174
+ *
137
175
* @param handle Socket handle
138
- * @param backlog Number of pending connections that can be queued up at any
139
- * one time [Default: 1]
140
- * @return 0 on success, negative on failure
176
+ * @param backlog Number of pending connections that can be queued
177
+ * simultaneously, defaults to 1
178
+ * @return 0 on success, negative error code on failure
141
179
*/
142
180
virtual int socket_listen (void *handle, int backlog) = 0;
143
181
144
- /* * Connects this TCP socket to the server
182
+ /* * Connects TCP socket to a remote host
183
+ *
184
+ * Initiates a connection to a remote server specified by the
185
+ * indicated address.
186
+ *
145
187
* @param handle Socket handle
146
- * @param address SocketAddress to connect to
147
- * @return 0 on success, negative on failure
188
+ * @param address The SocketAddress of the remote host
189
+ * @return 0 on success, negative error code on failure
148
190
*/
149
191
virtual int socket_connect (void *handle, const SocketAddress &address) = 0;
150
-
151
- /* * Check if the socket is connected
152
- * @param handle Socket handle
153
- * @return true if connected, false otherwise
154
- */
155
- virtual bool socket_is_connected (void *handle) = 0;
156
192
157
- /* * Accept a new connection.
158
- * @param handle Handle in which to store new socket
193
+ /* * Accepts a connection on a TCP socket
194
+ *
195
+ * The server socket must be bound and set to listen for connections.
196
+ * On a new connection, creates a socket stores it in the specified
197
+ * handle. The handle must be passed to following calls on the socket.
198
+ *
199
+ * A stack may have a finite number of sockets, in this case
200
+ * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
201
+ *
202
+ * This call is non-blocking. If accept would block,
203
+ * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
204
+ *
205
+ * @param handle Destination for a handle to the newly created sockey
159
206
* @param server Socket handle to server to accept from
160
- * @return 0 on success, negative on failure
161
- * @note This call is not-blocking, if this call would block, must
162
- * immediately return NSAPI_ERROR_WOULD_WAIT
207
+ * @return 0 on success, negative error code on failure
163
208
*/
164
209
virtual int socket_accept (void **handle, void *server) = 0;
165
210
166
- /* * Send data to the remote host
211
+ /* * Send data over a TCP socket
212
+ *
213
+ * The socket must be connected to a remote host. Returns the number of
214
+ * bytes sent from the buffer.
215
+ *
216
+ * This call is non-blocking. If send would block,
217
+ * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
218
+ *
167
219
* @param handle Socket handle
168
- * @param data The buffer to send to the host
169
- * @param size The length of the buffer to send
170
- * @return Number of written bytes on success, negative on failure
171
- * @note This call is not-blocking, if this call would block, must
172
- * immediately return NSAPI_ERROR_WOULD_WAIT
220
+ * @param data Buffer of data to send to the host
221
+ * @param size Size of the buffer in bytes
222
+ * @return Number of sent bytes on success, negative error
223
+ * code on failure
173
224
*/
174
225
virtual int socket_send (void *handle, const void *data, unsigned size) = 0;
175
226
176
- /* * Receive data from the remote host
227
+ /* * Receive data over a TCP socket
228
+ *
229
+ * The socket must be connected to a remote host. Returns the number of
230
+ * bytes received into the buffer.
231
+ *
232
+ * This call is non-blocking. If recv would block,
233
+ * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
234
+ *
177
235
* @param handle Socket handle
178
- * @param data The buffer in which to store the data received from the host
179
- * @param size The maximum length of the buffer
180
- * @return Number of received bytes on success, negative on failure
181
- * @note This call is not-blocking, if this call would block, must
182
- * immediately return NSAPI_ERROR_WOULD_WAIT
236
+ * @param data Destination buffer for data received from the host
237
+ * @param size Size of the buffer in bytes
238
+ * @return Number of received bytes on success, negative error
239
+ * code on failure
183
240
*/
184
241
virtual int socket_recv (void *handle, void *data, unsigned size) = 0;
185
242
186
- /* * Send a packet to a remote endpoint
243
+ /* * Send a packet over a UDP socket
244
+ *
245
+ * Sends data to the specified address. Returns the number of bytes
246
+ * sent from the buffer.
247
+ *
248
+ * This call is non-blocking. If sendto would block,
249
+ * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
250
+ *
187
251
* @param handle Socket handle
188
- * @param address The remote SocketAddress
189
- * @param data The packet to be sent
190
- * @param size The length of the packet to be sent
191
- * @return the number of written bytes on success, negative on failure
192
- * @note This call is not-blocking, if this call would block, must
193
- * immediately return NSAPI_ERROR_WOULD_WAIT
252
+ * @param address The SocketAddress of the remote host
253
+ * @param data Buffer of data to send to the host
254
+ * @param size Size of the buffer in bytes
255
+ * @return Number of sent bytes on success, negative error
256
+ * code on failure
194
257
*/
195
258
virtual int socket_sendto (void *handle, const SocketAddress &address, const void *data, unsigned size) = 0;
196
259
197
- /* * Receive a packet from a remote endpoint
260
+ /* * Receive a packet over a UDP socket
261
+ *
262
+ * Receives data and stores the source address in address if address
263
+ * is not NULL. Returns the number of bytes received into the buffer.
264
+ *
265
+ * This call is non-blocking. If recvfrom would block,
266
+ * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
267
+ *
198
268
* @param handle Socket handle
199
- * @param address Destination for the remote SocketAddress or null
200
- * @param buffer The buffer for storing the incoming packet data
201
- * If a packet is too long to fit in the supplied buffer,
202
- * excess bytes are discarded
203
- * @param size The length of the buffer
204
- * @return the number of received bytes on success, negative on failure
205
- * @note This call is not-blocking, if this call would block, must
206
- * immediately return NSAPI_ERROR_WOULD_WAIT
269
+ * @param address Destination for the source address or NULL
270
+ * @param data Destination buffer for data received from the host
271
+ * @param size Size of the buffer in bytes
272
+ * @return Number of received bytes on success, negative error
273
+ * code on failure
207
274
*/
208
275
virtual int socket_recvfrom (void *handle, SocketAddress *address, void *buffer, unsigned size) = 0;
209
276
210
277
/* * Register a callback on state change of the socket
278
+ *
279
+ * The specified callback will be called on state changes such as when
280
+ * the socket can recv/send/accept successfully and on when an error
281
+ * occurs. The callback may also be called spuriously without reason.
282
+ *
283
+ * The callback may be called in an interrupt context and should not
284
+ * perform expensive operations such as recv/send calls.
285
+ *
211
286
* @param handle Socket handle
212
287
* @param callback Function to call on state change
213
288
* @param data Argument to pass to callback
214
- * @note Callback may be called in an interrupt context.
215
289
*/
216
290
virtual void socket_attach (void *handle, void (*callback)(void *), void *data) = 0;
217
291
218
- /* Set socket options
292
+ /* Set stack-specific socked options
293
+ *
294
+ * The setsockopt allow an application to pass stack-specific hints
295
+ * to the underlying stack. For unsupported options,
296
+ * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
297
+ *
219
298
* @param handle Socket handle
220
- * @param level Option level
221
- * @param optname Option identifier
299
+ * @param level Stack-specific protocol level
300
+ * @param optname Stack-specific option identifier
222
301
* @param optval Option value
223
302
* @param optlen Length of the option value
224
- * @return 0 on success, negative on failure
303
+ * @return 0 on success, negative error code on failure
225
304
*/
226
305
virtual int setsockopt (void *handle, int level, int optname, const void *optval, unsigned optlen);
227
306
228
- /* Get socket options
307
+ /* Get stack-specific socket options
308
+ *
309
+ * The getstackopt allow an application to retrieve stack-specific hints
310
+ * from the underlying stack. For unsupported options,
311
+ * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
312
+ *
229
313
* @param handle Socket handle
230
- * @param level Option level
231
- * @param optname Option identifier
232
- * @param optval Buffer where to write option value
314
+ * @param level Stack-specific protocol level
315
+ * @param optname Stack-specific option identifier
316
+ * @param optval Destination for option value
233
317
* @param optlen Length of the option value
234
- * @return 0 on success, negative on failure
318
+ * @return 0 on success, negative error code on failure
235
319
*/
236
320
virtual int getsockopt (void *handle, int level, int optname, void *optval, unsigned *optlen);
237
321
};
0 commit comments