Skip to content

Commit 598875e

Browse files
author
Veijo Pesonen
committed
getsockopt()
1 parent 1f756f6 commit 598875e

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

ESP8266Interface.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,14 @@ nsapi_error_t ESP8266Interface::setsockopt(nsapi_socket_t handle, int level,
485485
{
486486
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
487487

488-
if (level == NSAPI_SOCKET) {
488+
if (!optlen || !socket) {
489+
return NSAPI_ERROR_PARAMETER;
490+
}
491+
492+
if (level == NSAPI_SOCKET && socket->proto == NSAPI_TCP) {
489493
switch (optname) {
490494
case NSAPI_KEEPALIVE: {
491-
if(socket->connected == true) {// ESP8266 limitation, keepalive needs to be given before connecting
495+
if(socket->connected) {// ESP8266 limitation, keepalive needs to be given before connecting
492496
return NSAPI_ERROR_UNSUPPORTED;
493497
}
494498

@@ -507,6 +511,29 @@ nsapi_error_t ESP8266Interface::setsockopt(nsapi_socket_t handle, int level,
507511
return NSAPI_ERROR_UNSUPPORTED;
508512
}
509513

514+
nsapi_error_t ESP8266Interface::getsockopt(nsapi_socket_t handle, int level, int optname, void *optval, unsigned *optlen)
515+
{
516+
struct esp8266_socket *socket = (struct esp8266_socket *)handle;
517+
518+
if (!optval || !optlen || !socket) {
519+
return NSAPI_ERROR_PARAMETER;
520+
}
521+
522+
if (level == NSAPI_SOCKET && socket->proto == NSAPI_TCP) {
523+
switch (optname) {
524+
case NSAPI_KEEPALIVE: {
525+
if(*optlen > sizeof(int)) {
526+
*optlen = sizeof(int);
527+
}
528+
memcpy(optval, &(socket->keepalive), *optlen);
529+
return NSAPI_ERROR_OK;
530+
}
531+
}
532+
}
533+
534+
return NSAPI_ERROR_UNSUPPORTED;
535+
}
536+
510537

511538
void ESP8266Interface::event() {
512539
for (int i = 0; i < ESP8266_SOCKET_COUNT; i++) {

ESP8266Interface.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
147147
*/
148148
using NetworkInterface::add_dns_server;
149149

150-
/* Set ESP8266-specific socket options
150+
/* Set socket options
151151
*
152152
* The setsockopt allow an application to pass stack-specific hints
153153
* to the underlying stack. For unsupported options,
@@ -163,6 +163,24 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
163163
virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level,
164164
int optname, const void *optval, unsigned optlen);
165165

166+
/* Get socket options
167+
*
168+
* getsockopt allows an application to retrieve stack-specific options
169+
* from the underlying stack using stack-specific level and option names,
170+
* or to request generic options using levels from nsapi_socket_level_t.
171+
*
172+
* For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned
173+
* and the socket is unmodified.
174+
*
175+
* @param level Stack-specific protocol level or nsapi_socket_level_t
176+
* @param optname Level-specific option name
177+
* @param optval Destination for option value
178+
* @param optlen Length of the option value
179+
* @return 0 on success, negative error code on failure
180+
*/
181+
virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, int optname,
182+
void *optval, unsigned *optlen);
183+
166184
protected:
167185
/** Open a socket
168186
* @param handle Handle in which to store new socket

0 commit comments

Comments
 (0)