Skip to content
Permalink
Browse files

samples: sockets: sntp_client: make IPv6 & NET_CONFIG_SETTINGS optional

In this commit, a config.h header file is added to support the case
when the NET_CONFIG_SETTINGS is not used. main.c is also modified to
enable platforms that do not support IPv6. These changes are necessary
in order to allow cc3220sf_launchxl support.

Signed-off-by: Vincent Wan <vincent.wan@linaro.org>
  • Loading branch information...
vanti authored and galak committed Mar 14, 2019
1 parent 5c6efa0 commit be5bfb6169042570c8001fa887098cea02623b22
Showing with 30 additions and 4 deletions.
  1. +22 −0 samples/net/sockets/sntp_client/src/config.h
  2. +8 −4 samples/net/sockets/sntp_client/src/main.c
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2019 Linaro Limited
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __CONFIG_H__
#define __CONFIG_H__

#ifdef CONFIG_NET_CONFIG_SETTINGS
#ifdef CONFIG_NET_IPV6
#define SERVER_ADDR6 CONFIG_NET_CONFIG_PEER_IPV6_ADDR
#endif
#define SERVER_ADDR CONFIG_NET_CONFIG_PEER_IPV4_ADDR
#else
#ifdef CONFIG_NET_IPV6
#define SERVER_ADDR6 "2001:db8::2"
#endif
#define SERVER_ADDR "192.0.2.2"
#endif

#endif
@@ -10,22 +10,25 @@ LOG_MODULE_REGISTER(net_sntp_client_sample, LOG_LEVEL_DBG);

#include <net/sntp.h>

#include "config.h"

#define SNTP_PORT 123

void main(void)
{
struct sntp_ctx ctx;
struct sockaddr_in addr;
#if defined(CONFIG_NET_IPV6)
struct sockaddr_in6 addr6;
#endif
u64_t epoch_time;
int rv;

/* ipv4 */
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(SNTP_PORT);
inet_pton(AF_INET, CONFIG_NET_CONFIG_PEER_IPV4_ADDR,
&addr.sin_addr);
inet_pton(AF_INET, SERVER_ADDR, &addr.sin_addr);

rv = sntp_init(&ctx, (struct sockaddr *) &addr,
sizeof(struct sockaddr_in));
@@ -42,14 +45,14 @@ void main(void)
LOG_DBG("time: %lld", epoch_time);
LOG_DBG("status: %d", rv);

#if defined(CONFIG_NET_IPV6)
sntp_close(&ctx);

/* ipv6 */
memset(&addr6, 0, sizeof(addr6));
addr6.sin6_family = AF_INET6;
addr6.sin6_port = htons(SNTP_PORT);
inet_pton(AF_INET6, CONFIG_NET_CONFIG_PEER_IPV6_ADDR,
&addr6.sin6_addr);
inet_pton(AF_INET6, SERVER_ADDR6, &addr6.sin6_addr);

rv = sntp_init(&ctx, (struct sockaddr *) &addr6,
sizeof(struct sockaddr_in6));
@@ -66,6 +69,7 @@ void main(void)

LOG_DBG("time: %lld", epoch_time);
LOG_DBG("status: %d", rv);
#endif

end:
sntp_close(&ctx);

0 comments on commit be5bfb6

Please sign in to comment.
You can’t perform that action at this time.