Skip to content

Commit 5c71ad8

Browse files
authored
Merge pull request #12428 from kjbracey-arm/chrono_dns
nsapi_dns: Convert to Chrono
2 parents 8e789b0 + af358d1 commit 5c71ad8

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

UNITTESTS/stubs/Kernel_stub.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
namespace rtos {
2121

2222
uint64_t Kernel::get_ms_count()
23+
{
24+
return impl::get_tick_count();
25+
26+
}
27+
uint64_t Kernel::impl::get_tick_count()
2328
{
2429
return 20;
2530
}

features/netsocket/nsapi_dns.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include "PlatformMutex.h"
3232
#include "SingletonPtr.h"
3333

34+
using namespace std::chrono;
35+
using rtos::Kernel::Clock;
36+
3437
#define CLASS_IN 1
3538

3639
#define RR_A 1
@@ -51,9 +54,9 @@
5154
struct DNS_CACHE {
5255
nsapi_addr_t *address;
5356
char *host;
54-
uint64_t expires; /*!< time to live in milliseconds */
55-
uint64_t accessed; /*!< last accessed */
56-
uint8_t count; /*!< number of IP addresses */
57+
Clock::time_point expires; /*!< time to live in milliseconds */
58+
Clock::time_point accessed; /*!< last accessed */
59+
uint8_t count; /*!< number of IP addresses */
5760
};
5861

5962
struct SOCKET_CB_DATA {
@@ -80,7 +83,7 @@ struct DNS_QUERY {
8083
UDPSocket *socket;
8184
SOCKET_CB_DATA *socket_cb_data;
8285
nsapi_addr_t *addrs;
83-
uint32_t ttl;
86+
duration<uint32_t> ttl;
8487
uint32_t total_timeout;
8588
uint32_t socket_timeout;
8689
uint16_t dns_message_id;
@@ -92,7 +95,7 @@ struct DNS_QUERY {
9295
dns_state state;
9396
};
9497

95-
static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_t ttl, uint8_t count);
98+
static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, duration<uint32_t> ttl, uint8_t count);
9699
static nsapi_size_or_error_t nsapi_dns_cache_find(const char *host, nsapi_version_t version, nsapi_addr_t *address);
97100
static void nsapi_dns_cache_reset();
98101

@@ -227,7 +230,7 @@ static int dns_append_question(uint8_t *ptr, uint16_t id, const char *host, nsap
227230
return *p - s_ptr;
228231
}
229232

230-
static int dns_scan_response(const uint8_t *ptr, uint16_t exp_id, uint32_t *ttl, nsapi_addr_t *addr, unsigned addr_count)
233+
static int dns_scan_response(const uint8_t *ptr, uint16_t exp_id, duration<uint32_t> *ttl, nsapi_addr_t *addr, unsigned addr_count)
231234
{
232235
const uint8_t **p = &ptr;
233236

@@ -293,7 +296,7 @@ static int dns_scan_response(const uint8_t *ptr, uint16_t exp_id, uint32_t *ttl,
293296
if (ttl_val > INT32_MAX) {
294297
ttl_val = INT32_MAX;
295298
}
296-
*ttl = ttl_val;
299+
*ttl = duration<uint32_t>(ttl_val);
297300
}
298301

299302
if (rtype == RR_A && rclass == CLASS_IN && rdlength == NSAPI_IPv4_BYTES) {
@@ -323,11 +326,11 @@ static int dns_scan_response(const uint8_t *ptr, uint16_t exp_id, uint32_t *ttl,
323326
return count;
324327
}
325328

326-
static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_t ttl, uint8_t count)
329+
static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, duration<uint32_t> ttl, uint8_t count)
327330
{
328331
#if (MBED_CONF_NSAPI_DNS_CACHE_SIZE > 0)
329332
// RFC 1034: if TTL is zero, entry is not added to cache
330-
if (ttl == 0) {
333+
if (ttl == ttl.zero()) {
331334
return;
332335
}
333336

@@ -339,7 +342,7 @@ static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_
339342
dns_cache_mutex->lock();
340343

341344
int index = -1;
342-
uint64_t accessed = UINT64_MAX;
345+
Clock::time_point accessed = Clock::time_point::max();
343346

344347
// Finds free or last accessed entry
345348
for (int i = 0; i < MBED_CONF_NSAPI_DNS_CACHE_SIZE; i++) {
@@ -373,9 +376,9 @@ static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_
373376
dns_cache[index]->count = count;
374377
dns_cache[index]->host = new (std::nothrow) char[strlen(host) + 1];
375378
strcpy(dns_cache[index]->host, host);
376-
uint64_t ms_count = rtos::Kernel::get_ms_count();
377-
dns_cache[index]->expires = ms_count + (uint64_t) ttl * 1000;
378-
dns_cache[index]->accessed = ms_count;
379+
auto now = Clock::now();
380+
dns_cache[index]->expires = now + ttl;
381+
dns_cache[index]->accessed = now;
379382
}
380383

381384
dns_cache_mutex->unlock();
@@ -391,9 +394,9 @@ static nsapi_size_or_error_t nsapi_dns_cache_find(const char *host, nsapi_versio
391394

392395
for (int i = 0; i < MBED_CONF_NSAPI_DNS_CACHE_SIZE; i++) {
393396
if (dns_cache[i]) {
394-
uint64_t ms_count = rtos::Kernel::get_ms_count();
397+
auto now = Clock::now();
395398
// Checks all entries for expired entries
396-
if (ms_count > dns_cache[i]->expires) {
399+
if (now > dns_cache[i]->expires) {
397400
delete dns_cache[i]->host;
398401
delete dns_cache[i];
399402
dns_cache[i] = NULL;
@@ -406,7 +409,7 @@ static nsapi_size_or_error_t nsapi_dns_cache_find(const char *host, nsapi_versio
406409
ret_val++;
407410
}
408411
}
409-
dns_cache[i]->accessed = ms_count;
412+
dns_cache[i]->accessed = now;
410413

411414
}
412415
}
@@ -566,7 +569,7 @@ static nsapi_size_or_error_t nsapi_dns_query_multiple(NetworkStack *stack, const
566569
}
567570

568571
const uint8_t *response = packet;
569-
uint32_t ttl;
572+
duration<uint32_t> ttl;
570573
int resp = dns_scan_response(response, 1, &ttl, addr, addr_count);
571574
if (resp > 0) {
572575
nsapi_dns_cache_add(host, addr, ttl, resp);

0 commit comments

Comments
 (0)