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>
This commit is contained in:
parent
5c6efa011a
commit
be5bfb6169
2 changed files with 30 additions and 4 deletions
22
samples/net/sockets/sntp_client/src/config.h
Normal file
22
samples/net/sockets/sntp_client/src/config.h
Normal file
|
@ -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,13 +10,17 @@ 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;
|
||||
|
||||
|
@ -24,8 +28,7 @@ void main(void)
|
|||
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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue