Skip to content

Commit fcd55fe

Browse files
author
Hasnain Virk
committed
[ONME-2844] 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 20eb127 commit fcd55fe

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
@@ -161,30 +161,57 @@ typedef enum nsapi_protocol {
161161
} nsapi_protocol_t;
162162

163163
/* Enum of standardized stack option levels
164+
* for use with NetworkStack::setstackopt and getstackopt.
164165
*
165-
* @enum nsapi_level_t
166+
* @enum nsapi_stack_level_t
166167
*/
167-
typedef enum nsapi_level {
168-
NSAPI_STACK, /*!< Stack option level */
169-
NSAPI_SOCKET, /*!< Socket option level */
170-
} nsapi_level_t;
168+
typedef enum nsapi_stack_level {
169+
NSAPI_STACK = 5000, /*!< Stack option level - see nsapi_stack_option_t for options */
170+
} nsapi_stack_level_t;
171171

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

189216
/** nsapi_wifi_ap structure
190217
*

0 commit comments

Comments
 (0)