Skip to content

Commit 77d30db

Browse files
committed
Merge pull request #254 from geky/lwip-keepalive
Add the NSAPI_KEEPALIVE socket option
2 parents 28a39bd + 3a62aa8 commit 77d30db

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

net/LWIPInterface/LWIPInterface.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,39 @@ int LWIPInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *buf,
545545
return copied;
546546
}
547547

548+
int LWIPInterface::setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen) {
549+
struct lwip_socket *s = (struct lwip_socket *)handle;
550+
551+
switch (optname) {
552+
case NSAPI_KEEPALIVE:
553+
if (optlen != sizeof(int) || s->proto != NSAPI_TCP) {
554+
return NSAPI_ERROR_UNSUPPORTED;
555+
}
556+
557+
s->tcp->so_options |= SOF_KEEPALIVE;
558+
return 0;
559+
560+
case NSAPI_KEEPIDLE:
561+
if (optlen != sizeof(int) || s->proto != NSAPI_TCP) {
562+
return NSAPI_ERROR_UNSUPPORTED;
563+
}
564+
565+
s->tcp->keep_idle = *(int*)optval;
566+
return 0;
567+
568+
case NSAPI_KEEPINTVL:
569+
if (optlen != sizeof(int) || s->proto != NSAPI_TCP) {
570+
return NSAPI_ERROR_UNSUPPORTED;
571+
}
572+
573+
s->tcp->keep_intvl = *(int*)optval;
574+
return 0;
575+
576+
default:
577+
return NSAPI_ERROR_UNSUPPORTED;
578+
}
579+
}
580+
548581
void LWIPInterface::socket_attach(void *handle, void (*callback)(void *), void *data)
549582
{
550583
struct lwip_socket *s = (struct lwip_socket *)handle;

net/LWIPInterface/LWIPInterface.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,21 @@ class LWIPInterface : public NetworkStack, public EthernetInterface
139139
*/
140140
virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
141141

142+
/* Set stack-specific socket options
143+
*
144+
* The setsockopt allow an application to pass stack-specific hints
145+
* to the underlying stack. For unsupported options,
146+
* NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.
147+
*
148+
* @param handle Socket handle
149+
* @param level Stack-specific protocol level
150+
* @param optname Stack-specific option identifier
151+
* @param optval Option value
152+
* @param optlen Length of the option value
153+
* @return 0 on success, negative error code on failure
154+
*/
155+
virtual int setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen);
156+
142157
/** Register a callback on state change of the socket
143158
* @param handle Socket handle
144159
* @param callback Function to call on state change

net/NetworkSocketAPI/NetworkStack.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ enum nsapi_level_t {
7373
enum nsapi_option_t {
7474
NSAPI_REUSEADDR, /*!< Allow bind to reuse local addresses */
7575
NSAPI_KEEPALIVE, /*!< Enables sending of keepalive messages */
76+
NSAPI_KEEPIDLE, /*!< Sets timeout value to initiate keepalive */
77+
NSAPI_KEEPINTVL, /*!< Sets timeout value for keepalive */
7678
NSAPI_LINGER, /*!< Keeps close from returning until queues empty */
7779
NSAPI_SNDBUF, /*!< Sets send buffer size */
7880
NSAPI_RCVBUF, /*!< Sets recv buffer size */

0 commit comments

Comments
 (0)