Skip to content

Commit 3231b99

Browse files
maass-hamburgnashif
authored andcommitted
samples: net: cloud: aws_iot_mqtt: use auto init
use auto init for networking, dhcp and sntp. This simplifys the code of this sample by using the auto init feature of the networking stack. Signed-off-by: Fin Maaß <[email protected]>
1 parent d6013e7 commit 3231b99

File tree

7 files changed

+12
-117
lines changed

7 files changed

+12
-117
lines changed

samples/net/cloud/aws_iot_mqtt/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ else()
2121
endif()
2222

2323
target_sources(app PRIVATE "src/main.c" ${creds})
24-
target_sources_ifdef(CONFIG_NET_DHCPV4 app PRIVATE "src/dhcp.c")
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
11
CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR=y
2-
3-
CONFIG_NET_DHCPV4=y
4-
CONFIG_NET_DHCPV4_INITIAL_DELAY_MAX=2
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
CONFIG_QEMU_ICOUNT=n
22

33
# QEMU networking configuration
4-
CONFIG_NET_CONFIG_SETTINGS=y
54
CONFIG_NET_CONFIG_NEED_IPV6=y
65
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1"
76
CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2"
8-
CONFIG_NET_CONFIG_NEED_IPV4=y
7+
CONFIG_NET_DHCPV4=n
98
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
109
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
1110
CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2"

samples/net/cloud/aws_iot_mqtt/prj.conf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ CONFIG_TEST_RANDOM_GENERATOR=y
1111
CONFIG_INIT_STACKS=y
1212
CONFIG_HW_STACK_PROTECTION=y
1313
CONFIG_REQUIRES_FULL_LIBC=y
14-
CONFIG_SNTP=y
1514
CONFIG_JSON_LIBRARY=y
1615
CONFIG_POSIX_API=y
1716

@@ -33,6 +32,17 @@ CONFIG_NET_IPV4=y
3332
CONFIG_NET_SOCKETS=y
3433
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
3534

35+
# Network configuration
36+
CONFIG_NET_CONFIG_SETTINGS=y
37+
CONFIG_NET_CONFIG_NEED_IPV4=y
38+
CONFIG_NET_DHCPV4=y
39+
40+
# SNTP
41+
CONFIG_SNTP=y
42+
CONFIG_POSIX_TIMERS=y
43+
CONFIG_NET_CONFIG_CLOCK_SNTP_INIT=y
44+
CONFIG_NET_CONFIG_SNTP_INIT_SERVER="0.pool.ntp.org"
45+
3646
# Logging
3747
CONFIG_LOG=y
3848

samples/net/cloud/aws_iot_mqtt/src/dhcp.c

Lines changed: 0 additions & 66 deletions
This file was deleted.

samples/net/cloud/aws_iot_mqtt/src/dhcp.h

Lines changed: 0 additions & 15 deletions
This file was deleted.

samples/net/cloud/aws_iot_mqtt/src/main.c

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
#include "creds/creds.h"
8-
#include "dhcp.h"
98

109
#include <errno.h>
1110
#include <stdio.h>
@@ -14,11 +13,9 @@
1413
#include <zephyr/net/socket.h>
1514
#include <zephyr/net/dns_resolve.h>
1615
#include <zephyr/net/mqtt.h>
17-
#include <zephyr/net/sntp.h>
1816
#include <zephyr/net/tls_credentials.h>
1917
#include <zephyr/data/json.h>
2018
#include <zephyr/random/random.h>
21-
#include <zephyr/posix/time.h>
2219
#include <zephyr/logging/log.h>
2320

2421

@@ -421,26 +418,6 @@ void aws_client_loop(void)
421418
fds.fd = -1;
422419
}
423420

424-
int sntp_sync_time(void)
425-
{
426-
int rc;
427-
struct sntp_time now;
428-
struct timespec tspec;
429-
430-
rc = sntp_simple(SNTP_SERVER, SYS_FOREVER_MS, &now);
431-
if (rc == 0) {
432-
tspec.tv_sec = now.seconds;
433-
tspec.tv_nsec = ((uint64_t)now.fraction * (1000lu * 1000lu * 1000lu)) >> 32;
434-
435-
clock_settime(CLOCK_REALTIME, &tspec);
436-
437-
LOG_DBG("Acquired time from NTP server: %u", (uint32_t)tspec.tv_sec);
438-
} else {
439-
LOG_ERR("Failed to acquire SNTP, code %d\n", rc);
440-
}
441-
return rc;
442-
}
443-
444421
static int resolve_broker_addr(struct sockaddr_in *broker)
445422
{
446423
int ret;
@@ -473,12 +450,6 @@ static int resolve_broker_addr(struct sockaddr_in *broker)
473450

474451
int main(void)
475452
{
476-
#if defined(CONFIG_NET_DHCPV4)
477-
app_dhcpv4_startup();
478-
#endif
479-
480-
sntp_sync_time();
481-
482453
setup_credentials();
483454

484455
for (;;) {

0 commit comments

Comments
 (0)