Skip to content

Commit 7333c5a

Browse files
Hasnain Virkadbridge
authored andcommitted
Avoid option level collisions
To allow a network stack to support both NSAPI and its own options, try to make sure the NSAPI levels don't collide with level numbers likely to be used by network stacks. Distinguish between socket and stack options, and tighten up documentation. Add IP MRU stack options as an example (implementation not immediately planned for any stack, but could be useful).
1 parent b2e0dd2 commit 7333c5a

File tree

3 files changed

+70
-34
lines changed

3 files changed

+70
-34
lines changed

features/netsocket/NetworkStack.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,31 @@ class NetworkStack
6868
*/
6969
virtual nsapi_error_t add_dns_server(const SocketAddress &address);
7070

71-
/* Set stack-specific stack options
71+
/* Set stack options
7272
*
73-
* The setstackopt allow an application to pass stack-specific hints
74-
* to the underlying stack. For unsupported options,
75-
* NSAPI_ERROR_UNSUPPORTED is returned and the stack is unmodified.
73+
* setstackopt allows an application to pass stack-specific options
74+
* to the underlying stack using stack-specific level and option names,
75+
* or to request generic options using levels from nsapi_stack_level_t.
7676
*
77-
* @param level Stack-specific protocol level
78-
* @param optname Stack-specific option identifier
77+
* For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned
78+
* and the stack is unmodified.
79+
*
80+
* @param level Stack-specific protocol level or nsapi_stack_level_t
81+
* @param optname Level-specific option name
7982
* @param optval Option value
8083
* @param optlen Length of the option value
8184
* @return 0 on success, negative error code on failure
8285
*/
8386
virtual nsapi_error_t setstackopt(int level, int optname, const void *optval, unsigned optlen);
8487

85-
/* Get stack-specific stack options
88+
/* Get stack options
8689
*
87-
* The getstackopt allow an application to retrieve stack-specific hints
88-
* from the underlying stack. For unsupported options,
89-
* NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
90+
* getstackopt allows an application to retrieve stack-specific options
91+
* to the underlying stack using stack-specific level and option names,
92+
* or to request generic options using levels from nsapi_stack_level_t.
9093
*
91-
* @param level Stack-specific protocol level
92-
* @param optname Stack-specific option identifier
94+
* @param level Stack-specific protocol level or nsapi_stack_level_t
95+
* @param optname Level-specific option name
9396
* @param optval Destination for option value
9497
* @param optlen Length of the option value
9598
* @return 0 on success, negative error code on failure

features/netsocket/Socket.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,34 @@ class Socket {
120120
*/
121121
void set_timeout(int timeout);
122122

123-
/* Set stack-specific socket options
123+
/* Set socket options
124124
*
125-
* The setsockopt allow an application to pass stack-specific hints
126-
* to the underlying stack. For unsupported options,
127-
* NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
125+
* setsockopt allows an application to pass stack-specific options
126+
* to the underlying stack using stack-specific level and option names,
127+
* or to request generic options using levels from nsapi_socket_level_t.
128128
*
129-
* @param level Stack-specific protocol level
130-
* @param optname Stack-specific option identifier
129+
* For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned
130+
* and the socket is unmodified.
131+
*
132+
* @param level Stack-specific protocol level or nsapi_socket_level_t
133+
* @param optname Level-specific option name
131134
* @param optval Option value
132135
* @param optlen Length of the option value
133136
* @return 0 on success, negative error code on failure
134137
*/
135138
nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen);
136139

137-
/* Get stack-specific socket options
140+
/* Get socket options
141+
*
142+
* getsockopt allows an application to retrieve stack-specific options
143+
* from the underlying stack using stack-specific level and option names,
144+
* or to request generic options using levels from nsapi_socket_level_t.
138145
*
139-
* The getstackopt allow an application to retrieve stack-specific hints
140-
* from the underlying stack. For unsupported options,
141-
* NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.
146+
* For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned
147+
* and the socket is unmodified.
142148
*
143-
* @param level Stack-specific protocol level
144-
* @param optname Stack-specific option identifier
149+
* @param level Stack-specific protocol level or nsapi_socket_level_t
150+
* @param optname Level-specific option name
145151
* @param optval Destination for option value
146152
* @param optlen Length of the option value
147153
* @return 0 on success, negative error code on failure

features/netsocket/nsapi_types.h

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,30 +164,57 @@ typedef enum nsapi_protocol {
164164
} nsapi_protocol_t;
165165

166166
/* Enum of standardized stack option levels
167+
* for use with NetworkStack::setstackopt and getstackopt.
167168
*
168-
* @enum nsapi_level_t
169+
* @enum nsapi_stack_level_t
169170
*/
170-
typedef enum nsapi_level {
171-
NSAPI_STACK, /*!< Stack option level */
172-
NSAPI_SOCKET, /*!< Socket option level */
173-
} nsapi_level_t;
171+
typedef enum nsapi_stack_level {
172+
NSAPI_STACK = 5000, /*!< Stack option level - see nsapi_stack_option_t for options */
173+
} nsapi_stack_level_t;
174174

175-
/* Enum of standardized stack options
175+
/* Enum of standardized stack option names for level NSAPI_STACK
176+
* of NetworkStack::setstackopt and getstackopt.
176177
*
177178
* These options may not be supported on all stacks, in which
178-
* case NSAPI_ERROR_UNSUPPORTED may be returned from setsockopt.
179+
* case NSAPI_ERROR_UNSUPPORTED may be returned.
179180
*
180-
* @enum nsapi_option_t
181+
* @enum nsapi_stack_option_t
181182
*/
182-
typedef enum nsapi_option {
183+
typedef enum nsapi_stack_option {
184+
NSAPI_IPV4_MRU, /*!< Sets/gets size of largest IPv4 fragmented datagram to reassemble */
185+
NSAPI_IPV6_MRU, /*!< Sets/gets size of largest IPv6 fragmented datagram to reassemble */
186+
} nsapi_stack_option_t;
187+
188+
/* Enum of standardized socket option levels
189+
* for use with Socket::setsockopt and getsockopt.
190+
*
191+
* @enum nsapi_socket_level_t
192+
*/
193+
typedef enum nsapi_socket_level {
194+
NSAPI_SOCKET = 7000, /*!< Socket option level - see nsapi_socket_option_t for options */
195+
} nsapi_socket_level_t;
196+
197+
/* Enum of standardized socket option names for level NSAPI_SOCKET
198+
* of Socket::setsockopt and getsockopt.
199+
*
200+
* These options may not be supported on all stacks, in which
201+
* case NSAPI_ERROR_UNSUPPORTED may be returned.
202+
*
203+
* @enum nsapi_socket_option_t
204+
*/
205+
typedef enum nsapi_socket_option {
183206
NSAPI_REUSEADDR, /*!< Allow bind to reuse local addresses */
184207
NSAPI_KEEPALIVE, /*!< Enables sending of keepalive messages */
185208
NSAPI_KEEPIDLE, /*!< Sets timeout value to initiate keepalive */
186209
NSAPI_KEEPINTVL, /*!< Sets timeout value for keepalive */
187210
NSAPI_LINGER, /*!< Keeps close from returning until queues empty */
188211
NSAPI_SNDBUF, /*!< Sets send buffer size */
189212
NSAPI_RCVBUF, /*!< Sets recv buffer size */
190-
} nsapi_option_t;
213+
} nsapi_socket_option_t;
214+
215+
/* Backwards compatibility - previously didn't distinguish stack and socket options */
216+
typedef nsapi_socket_level_t nsapi_level_t;
217+
typedef nsapi_socket_option_t nsapi_option_t;
191218

192219
/** nsapi_wifi_ap structure
193220
*

0 commit comments

Comments
 (0)