1 | /*
|
---|
2 | * Redistribution and use in source and binary forms, with or without modification,
|
---|
3 | * are permitted provided that the following conditions are met:
|
---|
4 | *
|
---|
5 | * 1. Redistributions of source code must retain the above copyright notice,
|
---|
6 | * this list of conditions and the following disclaimer.
|
---|
7 | * 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
8 | * this list of conditions and the following disclaimer in the documentation
|
---|
9 | * and/or other materials provided with the distribution.
|
---|
10 | * 3. The name of the author may not be used to endorse or promote products
|
---|
11 | * derived from this software without specific prior written permission.
|
---|
12 | *
|
---|
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
---|
14 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
---|
15 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
---|
16 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
17 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
---|
18 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
19 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
20 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
---|
21 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
---|
22 | * OF SUCH DAMAGE.
|
---|
23 | *
|
---|
24 | * This file is part of the lwIP TCP/IP stack.
|
---|
25 | *
|
---|
26 | * Author: Dirk Ziegelmeier <dziegel@gmx.de>
|
---|
27 | *
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include <time.h>
|
---|
31 |
|
---|
32 | #include "lwip/opt.h"
|
---|
33 | #include "lwip/apps/sntp.h"
|
---|
34 | #include "sntp_example.h"
|
---|
35 | #include "lwip/netif.h"
|
---|
36 |
|
---|
37 | void
|
---|
38 | sntp_set_system_time(u32_t sec)
|
---|
39 | {
|
---|
40 | char buf[32];
|
---|
41 | struct tm current_time_val;
|
---|
42 | time_t current_time = (time_t)sec;
|
---|
43 |
|
---|
44 | #ifdef _MSC_VER
|
---|
45 | localtime_s(¤t_time_val, ¤t_time);
|
---|
46 | #else
|
---|
47 | localtime_r(¤t_time, ¤t_time_val);
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | strftime(buf, sizeof(buf), "%d.%m.%Y %H:%M:%S", ¤t_time_val);
|
---|
51 | printf("SNTP time: %s\n", buf);
|
---|
52 | }
|
---|
53 |
|
---|
54 | void
|
---|
55 | sntp_example_init(void)
|
---|
56 | {
|
---|
57 | sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
---|
58 | #if LWIP_DHCP
|
---|
59 | sntp_servermode_dhcp(1); /* get SNTP server via DHCP */
|
---|
60 | #else /* LWIP_DHCP */
|
---|
61 | #if LWIP_IPV4
|
---|
62 | sntp_setserver(0, netif_ip_gw4(netif_default));
|
---|
63 | #endif /* LWIP_IPV4 */
|
---|
64 | #endif /* LWIP_DHCP */
|
---|
65 | sntp_init();
|
---|
66 | }
|
---|