net: samples: Add socket based SNTP client application
SNTP client sample requests time sync on IPv4 and IPv6 addresses using socket based SNTP client library. Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
This commit is contained in:
parent
f51cebeea2
commit
a9acc6d4d7
5 changed files with 152 additions and 0 deletions
6
samples/net/sockets/sntp_client/CMakeLists.txt
Normal file
6
samples/net/sockets/sntp_client/CMakeLists.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.13.1)
|
||||
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
|
||||
project(sntp_client)
|
||||
|
||||
FILE(GLOB app_sources src/*.c)
|
||||
target_sources(app PRIVATE ${app_sources})
|
29
samples/net/sockets/sntp_client/README.rst
Normal file
29
samples/net/sockets/sntp_client/README.rst
Normal file
|
@ -0,0 +1,29 @@
|
|||
.. _sntp-client-sample:
|
||||
|
||||
SNTP client sample
|
||||
##################
|
||||
|
||||
Overview
|
||||
********
|
||||
|
||||
This sample is a simple SNTP client showing how to retrieve the current
|
||||
time in seconds since 1st January 1970.
|
||||
|
||||
This demo assumes that the platform of choice has networking support,
|
||||
some adjustments to the configuration may be needed. It also assumes
|
||||
SNTP server is running on the host.
|
||||
|
||||
Building and Running
|
||||
********************
|
||||
|
||||
When the application runs, it issues an SNTP request to the host and waits
|
||||
for a response. When the response is received, the current epoch time, in
|
||||
seconds, as well as the status code of the response (0 on success), is
|
||||
printed.
|
||||
|
||||
See the `net-tools`_ project for more details.
|
||||
|
||||
This sample can be built and executed on QEMU as described
|
||||
in :ref:`networking_with_qemu`.
|
||||
|
||||
.. _`net-tools`: https://github.com/zephyrproject-rtos/net-tools
|
36
samples/net/sockets/sntp_client/prj.conf
Normal file
36
samples/net/sockets/sntp_client/prj.conf
Normal file
|
@ -0,0 +1,36 @@
|
|||
# General config
|
||||
CONFIG_NEWLIB_LIBC=y
|
||||
|
||||
# Networking config
|
||||
CONFIG_NETWORKING=y
|
||||
CONFIG_NET_IPV4=y
|
||||
CONFIG_NET_IPV6=y
|
||||
CONFIG_NET_UDP=y
|
||||
CONFIG_NET_SHELL=y
|
||||
|
||||
# Sockets
|
||||
CONFIG_NET_SOCKETS=y
|
||||
CONFIG_NET_SOCKETS_POSIX_NAMES=y
|
||||
CONFIG_NET_SOCKETS_POLL_MAX=4
|
||||
|
||||
# Network driver config
|
||||
CONFIG_TEST_RANDOM_GENERATOR=y
|
||||
|
||||
# Network address config
|
||||
CONFIG_NET_CONFIG_SETTINGS=y
|
||||
CONFIG_NET_CONFIG_NEED_IPV4=y
|
||||
CONFIG_NET_CONFIG_NEED_IPV6=y
|
||||
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
|
||||
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
|
||||
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1"
|
||||
CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2"
|
||||
|
||||
# SNTP
|
||||
CONFIG_SNTP=y
|
||||
|
||||
CONFIG_MAIN_STACK_SIZE=2048
|
||||
# Network debug config
|
||||
CONFIG_NET_LOG=y
|
||||
CONFIG_LOG=y
|
||||
CONFIG_NET_SOCKETS_LOG_LEVEL_DBG=n
|
||||
CONFIG_SNTP_LOG_LEVEL_DBG=n
|
8
samples/net/sockets/sntp_client/sample.yaml
Normal file
8
samples/net/sockets/sntp_client/sample.yaml
Normal file
|
@ -0,0 +1,8 @@
|
|||
sample:
|
||||
description: SNTP client sample
|
||||
name: sntp_client
|
||||
tests:
|
||||
test:
|
||||
harness: net
|
||||
platform_whitelist: qemu_x86
|
||||
tags: net
|
73
samples/net/sockets/sntp_client/src/main.c
Normal file
73
samples/net/sockets/sntp_client/src/main.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright (c) 2017 Linaro Limited
|
||||
* Copyright (c) 2019 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(net_sntp_client_sample, LOG_LEVEL_DBG);
|
||||
|
||||
#include <net/sntp.h>
|
||||
|
||||
#define SNTP_PORT 123
|
||||
|
||||
void resp_callback(struct sntp_ctx *ctx, int status,
|
||||
u64_t epoch_time)
|
||||
{
|
||||
LOG_DBG("time: %lld", epoch_time);
|
||||
LOG_DBG("status: %d", status);
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
struct sntp_ctx ctx;
|
||||
struct sockaddr_in addr;
|
||||
struct sockaddr_in6 addr6;
|
||||
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);
|
||||
|
||||
rv = sntp_init(&ctx, (struct sockaddr *) &addr,
|
||||
sizeof(struct sockaddr_in));
|
||||
if (rv < 0) {
|
||||
LOG_ERR("Failed to init sntp ctx: %d", rv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
rv = sntp_request(&ctx, K_FOREVER, resp_callback);
|
||||
if (rv < 0) {
|
||||
LOG_ERR("Failed to send sntp request: %d", rv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
rv = sntp_init(&ctx, (struct sockaddr *) &addr6,
|
||||
sizeof(struct sockaddr_in6));
|
||||
if (rv < 0) {
|
||||
LOG_ERR("Failed to init sntp ctx: %d", rv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
rv = sntp_request(&ctx, K_NO_WAIT, resp_callback);
|
||||
if (rv < 0) {
|
||||
LOG_ERR("Failed to send sntp request: %d", rv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
end:
|
||||
sntp_close(&ctx);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue