Skip to content

Commit db2c970

Browse files
author
Arto Kinnunen
committed
Add getsockopt option to reading network property
Add getsockopt options NSAPI_LATENCY and NSAPI_STAGGER to read network specific timing constraints from socket. -NS_LATENCY returns estimated latency to given address. -NSAPI_STAGGER returns estimated initial delay that application should wait before transmitting data to network. Application can use the new options to avoid network congestion by adjusting transmission delays and retry timeouts.
1 parent dc21432 commit db2c970

File tree

4 files changed

+118
-2
lines changed

4 files changed

+118
-2
lines changed

features/nanostack/nanostack-interface/Nanostack.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,18 +838,58 @@ nsapi_error_t Nanostack::setsockopt(void *handle, int level, int optname, const
838838
nsapi_error_t Nanostack::getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen)
839839
{
840840
NanostackSocket *socket = static_cast<NanostackSocket *>(handle);
841+
841842
if (handle == NULL) {
842843
MBED_ASSERT(false);
843844
return NSAPI_ERROR_NO_SOCKET;
844845
}
845846

846847
NanostackLockGuard lock;
848+
// pointers to Mbed OS structures
849+
nsapi_latency_req_t *ns_latency_r = static_cast<nsapi_latency_req_t *>(optval);
850+
nsapi_stagger_req_t *ns_stagger_r = static_cast<nsapi_stagger_req_t *>(optval);
851+
// Nanostack internal structures
852+
ns_ipv6_latency_t nanostack_latency;
853+
ns_ipv6_stagger_t nanostack_stagger;
854+
int nanostack_optname = optname;
855+
856+
void *ns_option_value = optval;
857+
858+
if (level == NSAPI_SOCKET) {
859+
if (optname == NSAPI_LATENCY) {
860+
if (*optlen < sizeof(nsapi_latency_req_t)) {
861+
return NSAPI_ERROR_PARAMETER;
862+
}
863+
// Adjust to Nanostack namespace
864+
level = SOCKET_IPPROTO_IPV6;
865+
nanostack_optname = SOCKET_LATENCY;
866+
memcpy(nanostack_latency.dest_addr, ns_latency_r->addr, 16);
867+
ns_option_value = &nanostack_latency;
868+
} else if (optname == NSAPI_STAGGER) {
869+
if (*optlen < sizeof(nsapi_stagger_req_t)) {
870+
return NSAPI_ERROR_PARAMETER;
871+
}
872+
// Adjust to Nanostack namespace
873+
level = SOCKET_IPPROTO_IPV6;
874+
nanostack_optname = SOCKET_STAGGER;
875+
memcpy(nanostack_stagger.dest_addr, ns_stagger_r->addr, 16);
876+
nanostack_stagger.data_amount = ns_stagger_r->data_amount;
877+
ns_option_value = &nanostack_stagger;
878+
}
879+
}
847880

848881
uint16_t optlen16 = *optlen;
849882

850-
int retcode = ::socket_getsockopt(socket->socket_id, level, optname, optval, &optlen16);
883+
int retcode = ::socket_getsockopt(socket->socket_id, level, nanostack_optname, ns_option_value, &optlen16);
851884
if (retcode == 0) {
852885
*optlen = optlen16;
886+
if (optname == NSAPI_LATENCY) {
887+
ns_latency_r->latency = nanostack_latency.latency;
888+
} else if (optname == NSAPI_STAGGER) {
889+
ns_stagger_r->stagger_min = nanostack_stagger.stagger_min;
890+
ns_stagger_r->stagger_max = nanostack_stagger.stagger_max;
891+
ns_stagger_r->stagger_rand = nanostack_stagger.stagger_rand;
892+
}
853893
return NSAPI_ERROR_OK;
854894
} else if (retcode == -2) {
855895
return NSAPI_ERROR_UNSUPPORTED;

features/netsocket/InternetSocket.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,44 @@ int InternetSocket::leave_multicast_group(const SocketAddress &address)
114114
return modify_multicast_group(address, NSAPI_DROP_MEMBERSHIP);
115115
}
116116

117+
int InternetSocket::get_latency_estimate_to_address(const SocketAddress &address, uint32_t *latency)
118+
{
119+
nsapi_error_t ret;
120+
nsapi_latency_req_t ns_api_latency_req;
121+
unsigned opt_len = sizeof(nsapi_latency_req_t);
122+
123+
// Set up address
124+
memcpy(ns_api_latency_req.addr, address.get_ip_bytes(), 16);
125+
126+
ret = this->getsockopt(NSAPI_SOCKET, NSAPI_LATENCY, (void *)&ns_api_latency_req, &opt_len);
127+
if (ret == NSAPI_ERROR_OK) {
128+
// success, latency found
129+
*latency = ns_api_latency_req.latency;
130+
}
131+
132+
return ret;
133+
}
134+
135+
int InternetSocket::get_stagger_estimate_to_address(const SocketAddress &address, uint32_t data_amount, uint16_t *stagger_min, uint16_t *stagger_max, uint16_t *stagger_rand)
136+
{
137+
nsapi_error_t ret;
138+
nsapi_stagger_req_t nsapi_stagger;
139+
unsigned opt_len = sizeof(nsapi_stagger_req_t);
140+
141+
// Set up address
142+
memcpy(nsapi_stagger.addr, address.get_ip_bytes(), 16);
143+
nsapi_stagger.data_amount = data_amount;
144+
145+
ret = this->getsockopt(NSAPI_SOCKET, NSAPI_STAGGER, (void *)&nsapi_stagger, &opt_len);
146+
if (ret == NSAPI_ERROR_OK) {
147+
// success, stagger found
148+
*stagger_min = nsapi_stagger.stagger_min;
149+
*stagger_max = nsapi_stagger.stagger_max;
150+
*stagger_rand = nsapi_stagger.stagger_rand;
151+
}
152+
153+
return ret;
154+
}
117155

118156
nsapi_error_t InternetSocket::bind(uint16_t port)
119157
{

features/netsocket/InternetSocket.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,25 @@ class InternetSocket : public Socket {
8686
*/
8787
int leave_multicast_group(const SocketAddress &address);
8888

89+
/** Get estimated latency to reach destination address.
90+
*
91+
* @param address Destination address to estimate latency.
92+
* @param latency Returned latency value in milliseconds.
93+
* @return NSAPI_ERROR_OK on success, negative error code on failure (@see InternetSocket::getsockopt).
94+
*/
95+
int get_latency_estimate_to_address(const SocketAddress &address, uint32_t *latency);
96+
97+
/** Get estimated stagger value to reach destination address.
98+
*
99+
* @param address Address to estimate stagger values.
100+
* @param data_amount Amount of bytes to transfer in kilobytes.
101+
* @param stagger_min Minimum stagger value in seconds.
102+
* @param stagger_max Maximum stagger value in seconds.
103+
* @param stagger_rand Randomized stagger value in seconds.
104+
* @return NSAPI_ERROR_OK on success, negative error code on failure (@see InternetSocket::getsockopt).
105+
*/
106+
int get_stagger_estimate_to_address(const SocketAddress &address, uint32_t data_amount, uint16_t *stagger_min, uint16_t *stagger_max, uint16_t *stagger_rand);
107+
89108
/** Bind the socket to a port on which to receive data.
90109
*
91110
* @param port Local port to bind.

features/netsocket/nsapi_types.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ typedef enum nsapi_socket_option {
267267
NSAPI_RCVBUF, /*!< Sets recv buffer size */
268268
NSAPI_ADD_MEMBERSHIP, /*!< Add membership to multicast address */
269269
NSAPI_DROP_MEMBERSHIP, /*!< Drop membership to multicast address */
270-
NSAPI_BIND_TO_DEVICE, /*!< Bind socket network interface name*/
270+
NSAPI_BIND_TO_DEVICE, /*!< Bind socket network interface name*/
271+
NSAPI_LATENCY, /*!< Read estimated latency to destination */
272+
NSAPI_STAGGER, /*!< Read estimated stagger value to destination */
271273
} nsapi_socket_option_t;
272274

273275
typedef enum nsapi_tlssocket_level {
@@ -338,6 +340,23 @@ typedef struct nsapi_ip_mreq {
338340
nsapi_addr_t imr_interface; /* local IP address of interface */
339341
} nsapi_ip_mreq_t;
340342

343+
/** nsapi_latency_req structure
344+
*/
345+
typedef struct nsapi_latency_req {
346+
uint8_t addr[16]; /* [IN] Destination address to estimate latency */
347+
uint32_t latency; /* [OUT] Latency value */
348+
} nsapi_latency_req_t;
349+
350+
/** nsapi_stagger_req structure
351+
*/
352+
typedef struct nsapi_stagger_req {
353+
uint8_t addr[16]; /* [IN] Destination address to estimate stagger */
354+
uint16_t data_amount; /* [IN] Amount of data to be sent in kilobytes */
355+
uint16_t stagger_min; /* [OUT] Minimum stagger value in seconds */
356+
uint16_t stagger_max; /* [OUT] Maximum stagger value in seconds */
357+
uint16_t stagger_rand; /* [OUT] Randomized stagger value in seconds */
358+
} nsapi_stagger_req_t;
359+
341360
/** nsapi_stack_api structure
342361
*
343362
* Common api structure for network stack operations. A network stack

0 commit comments

Comments
 (0)