Skip to content

Commit cffa107

Browse files
author
Arto Kinnunen
committed
Change new API to use Socket API: getsockopt
Use getsockopt to read latency value as it provides better solution than a separate API.
1 parent 52a00e2 commit cffa107

File tree

8 files changed

+46
-147
lines changed

8 files changed

+46
-147
lines changed

features/nanostack/mbed-mesh-api/mbed-mesh-api/MeshInterfaceNanostack.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,6 @@ class InterfaceNanostack : public virtual NetworkInterface {
140140
*/
141141
nsapi_error_t set_file_system_root_path(const char *root_path);
142142

143-
/** @copydoc NetworkInterface::get_interface_timing_property */
144-
virtual nsapi_error_t get_interface_timing_property(unsigned int data_amount,
145-
unsigned int *initial_delay_min,
146-
unsigned int *initial_delay_max,
147-
unsigned int *initial_delay_rand,
148-
unsigned int *expected_rtt);
149-
150-
/** @copydoc NetworkInterface::get_interface_initial_delay */
151-
virtual nsapi_error_t get_interface_initial_delay(unsigned int data_amount, unsigned int *initial_delay);
152-
153-
/** @copydoc NetworkInterface::get_interface_rtt */
154-
virtual nsapi_error_t get_interface_rtt(unsigned int *expected_rtt);
155-
156143
/** Get the interface ID
157144
* @return Interface identifier
158145
*/

features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -223,61 +223,6 @@ nsapi_error_t InterfaceNanostack::set_file_system_root_path(const char *root_pat
223223
return MESH_ERROR_UNKNOWN;
224224
}
225225

226-
nsapi_error_t InterfaceNanostack::get_interface_timing_property(unsigned int data_amount,
227-
unsigned int *initial_delay_min,
228-
unsigned int *initial_delay_max,
229-
unsigned int *initial_delay_rand,
230-
unsigned int *expected_rtt)
231-
{
232-
net_timing_constraint_t net_timing_constraint;
233-
234-
if (!initial_delay_min || !initial_delay_max || !initial_delay_rand || !expected_rtt) {
235-
return NSAPI_ERROR_PARAMETER;
236-
}
237-
238-
if (arm_nwk_interface_timing_constraint_get(get_interface_id(), data_amount, &net_timing_constraint) == 0) {
239-
*initial_delay_min = net_timing_constraint.jitter_min;
240-
*initial_delay_max = net_timing_constraint.jitter_max;
241-
*initial_delay_rand = net_timing_constraint.jitter_rand;
242-
*expected_rtt = net_timing_constraint.network_latency * 2; // RTT = 2 * latency + some processing time
243-
return NSAPI_ERROR_OK;
244-
}
245-
246-
return NSAPI_ERROR_NO_CONNECTION;
247-
}
248-
249-
nsapi_error_t InterfaceNanostack::get_interface_initial_delay(unsigned int data_amount, unsigned int *initial_delay)
250-
{
251-
net_timing_constraint_t net_timing_constraint;
252-
253-
if (!initial_delay) {
254-
return NSAPI_ERROR_PARAMETER;
255-
}
256-
257-
if (arm_nwk_interface_timing_constraint_get(get_interface_id(), 0, &net_timing_constraint) == 0) {
258-
*initial_delay = net_timing_constraint.jitter_rand;
259-
return NSAPI_ERROR_OK;
260-
}
261-
262-
return NSAPI_ERROR_NO_CONNECTION;
263-
}
264-
265-
nsapi_error_t InterfaceNanostack::get_interface_rtt(unsigned int *expected_rtt)
266-
{
267-
net_timing_constraint_t net_timing_constraint;
268-
269-
if (!expected_rtt) {
270-
return NSAPI_ERROR_PARAMETER;
271-
}
272-
273-
if (arm_nwk_interface_timing_constraint_get(get_interface_id(), 0, &net_timing_constraint) == 0) {
274-
*expected_rtt = net_timing_constraint.network_latency * 2; // RTT = 2 * latency + some processing time
275-
return NSAPI_ERROR_OK;
276-
}
277-
278-
return NSAPI_ERROR_NO_CONNECTION;
279-
}
280-
281226
#if !DEVICE_802_15_4_PHY
282227
MBED_WEAK MeshInterface *MeshInterface::get_target_default_instance()
283228
{

features/nanostack/nanostack-interface/Nanostack.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,13 +838,25 @@ 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;
847848

849+
if (level == NSAPI_SOCKET) {
850+
if (optname == NSAPI_LATENCY) {
851+
if (*optlen < 16) {
852+
return NSAPI_ERROR_PARAMETER;
853+
}
854+
// Adjust to Nanostack namespace
855+
level = SOCKET_IPPROTO_IPV6;
856+
optname = SOCKET_LATENCY;
857+
}
858+
}
859+
848860
uint16_t optlen16 = *optlen;
849861

850862
int retcode = ::socket_getsockopt(socket->socket_id, level, optname, optval, &optlen16);

features/netsocket/InternetSocket.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,30 @@ 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+
void *option[4];
121+
unsigned opt_len = 16; // room for IPv6 address
122+
123+
// Set up address
124+
memcpy(option, address.get_ip_bytes(), 16);
125+
tr_debug("IS dest_addr: %s", tr_array((const uint8_t*)option, 16));
126+
127+
ret = this->getsockopt(NSAPI_SOCKET, NSAPI_LATENCY, (void*)option, &opt_len);
128+
if (ret == NSAPI_ERROR_OK) {
129+
if (option[0] == 0) {
130+
// address was not valid as 0 was returned
131+
ret = NSAPI_ERROR_NO_ADDRESS;
132+
} else {
133+
// success, latency found
134+
*latency = (uint32_t)option[0];
135+
}
136+
}
137+
138+
return ret;
139+
}
140+
117141

118142
nsapi_error_t InternetSocket::bind(uint16_t port)
119143
{

features/netsocket/InternetSocket.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ 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 where latency will be estimated.
92+
* @param latency Address to 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+
8997
/** Bind the socket to a port on which to receive data.
9098
*
9199
* @param port Local port to bind.

features/netsocket/NetworkInterface.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,3 @@ nsapi_error_t NetworkInterface::set_blocking(bool blocking)
174174
return NSAPI_ERROR_UNSUPPORTED;
175175
}
176176

177-
nsapi_error_t NetworkInterface::get_interface_timing_property(unsigned int data_amount,
178-
unsigned int *initial_delay_min,
179-
unsigned int *initial_delay_max,
180-
unsigned int *initial_delay_rand,
181-
unsigned int *expected_rtt)
182-
{
183-
return NSAPI_ERROR_UNSUPPORTED;
184-
}
185-
186-
nsapi_error_t NetworkInterface::get_interface_initial_delay(unsigned int data_amount,
187-
unsigned int *initial_delay)
188-
{
189-
return NSAPI_ERROR_UNSUPPORTED;
190-
}
191-
192-
nsapi_error_t NetworkInterface::get_interface_rtt(unsigned int *expected_rtt)
193-
{
194-
return NSAPI_ERROR_UNSUPPORTED;
195-
}

features/netsocket/NetworkInterface.h

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -378,65 +378,6 @@ class NetworkInterface: public DNS {
378378
*/
379379
virtual nsapi_error_t set_blocking(bool blocking);
380380

381-
/** Get network interface timing properties.
382-
*
383-
* Network interfaces have different characteristics. Some wireless networks are
384-
* established slowly and may have a high latency while some wired networks are
385-
* ready almost instantly after power on.
386-
*
387-
* Application can use network timing properties to adapt to the varying network
388-
* interfaces. Properties include:
389-
* * initial delay, a estimated wait time that application should wait after startup before sending data to the network.
390-
* * round-trip time, a estimated time between sending a signal and receiving the response.
391-
*
392-
* @param data_amount Amount of data to be sent in kilobytes.
393-
* @param initial_delay_min Address to minimum delay value (ms), must be non-null.
394-
* @param initial_delay_max Address to maximum delay value (ms), must be non-null.
395-
* @param initial_delay_rand Address to randomized delay value (ms), must be non-null.
396-
* @param expected_rtt Address to expected round-trip time value (ms), must be non-null.
397-
* @return NSAPI_ERROR_OK on success.
398-
* @return NSAPI_ERROR_UNSUPPORTED if API is not implemented to interface.
399-
* @return NSAPI_ERROR_NO_CONNECTION if network is not connected.
400-
* @return NSAPI_ERROR_PARAMETER. if input parameters are not valid.
401-
*/
402-
virtual nsapi_error_t get_interface_timing_property(unsigned int data_amount,
403-
unsigned int *initial_delay_min,
404-
unsigned int *initial_delay_max,
405-
unsigned int *initial_delay_rand,
406-
unsigned int *expected_rtt);
407-
408-
/** Get initial delay for the network interface
409-
*
410-
* Initial delay can be used to optimize network performance. Some wireless networks
411-
* use a lot of network bandwidth to find and extend the network during startup.
412-
* Having an application sending data aggressively during the startup can result to
413-
* network congestion that will drop network performance even more.
414-
* Initial delay is a randomized value based on the selected interface properties.
415-
*
416-
* @param data_amount Amount of data in kilobytes that application is going to send.
417-
* @param initial_delay Address to location where initial_delay is written in milliseconds.
418-
* @return NSAPI_ERROR_OK on success.
419-
* @return NSAPI_ERROR_UNSUPPORTED if API is not implemented to interface.
420-
* @return NSAPI_ERROR_NO_CONNECTION if network is not connected.
421-
* @return NSAPI_ERROR_PARAMETER. if input parameters are not valid.
422-
*/
423-
virtual nsapi_error_t get_interface_initial_delay(unsigned int data_amount,
424-
unsigned int *initial_delay);
425-
426-
/** Get network interface expected round-trip time (RTT)
427-
*
428-
* Application can use expected RTT to adjust possible retry intervals.
429-
* Wired networks may have RTT value less than a seconds while some wireless
430-
* networks could have a RTT up to several minutes.
431-
*
432-
* @param expected_rtt Address to location where expected RTT is written (ms).
433-
* @return NSAPI_ERROR_OK on success.
434-
* @return NSAPI_ERROR_UNSUPPORTED if API is not implemented to interface.
435-
* @return NSAPI_ERROR_NO_CONNECTION if network is not connected.
436-
* @return NSAPI_ERROR_PARAMETER. if input parameters are not valid.
437-
*/
438-
virtual nsapi_error_t get_interface_rtt(unsigned int *expected_rtt);
439-
440381
/** Return pointer to an EthInterface.
441382
* @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
442383
*/

features/netsocket/nsapi_types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ 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 latency and initial delay */
271272
} nsapi_socket_option_t;
272273

273274
typedef enum nsapi_tlssocket_level {

0 commit comments

Comments
 (0)