1 |
786 |
skrzyp |
//=================================================================
|
2 |
|
|
//
|
3 |
|
|
// sntp1.c
|
4 |
|
|
//
|
5 |
|
|
// Simple Network Time Protocol test 1
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 2003 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): Andrew Lunn
|
43 |
|
|
// Contributors:
|
44 |
|
|
// Date: 2003-02-11
|
45 |
|
|
// Description: Tests the sntp client
|
46 |
|
|
//####DESCRIPTIONEND####
|
47 |
|
|
|
48 |
|
|
#include <pkgconf/isoinfra.h>
|
49 |
|
|
#include <cyg/infra/testcase.h>
|
50 |
|
|
|
51 |
|
|
#if defined(CYGINT_ISO_STDIO_FORMATTED_IO) && defined(CYGINT_ISO_STRING_STRFUNCS)
|
52 |
|
|
#include <network.h>
|
53 |
|
|
#include <time.h>
|
54 |
|
|
#include <cyg/sntp/sntp.h>
|
55 |
|
|
#include <stdio.h>
|
56 |
|
|
|
57 |
|
|
#define SECONDSPERMINUTE (cyg_uint32)60
|
58 |
|
|
#define SECONDSPERHOUR (cyg_uint32)(SECONDSPERMINUTE * 60)
|
59 |
|
|
#define SECONDSPERDAY (cyg_uint32)(SECONDSPERHOUR * 24)
|
60 |
|
|
#define SECONDSPERYEAR (cyg_uint32)(SECONDSPERDAY * 365)
|
61 |
|
|
|
62 |
|
|
void
|
63 |
|
|
net_test(cyg_addrword_t param)
|
64 |
|
|
{
|
65 |
|
|
int seconds;
|
66 |
|
|
time_t now, build_time;
|
67 |
|
|
struct tm tm={ 0,0,0,0,0,0,0,0,0 };
|
68 |
|
|
int i, loop, waittime;
|
69 |
|
|
char month[4];
|
70 |
|
|
char months[12][4] = { "Jan", "Feb", "Mar",
|
71 |
|
|
"Apr", "May", "Jun",
|
72 |
|
|
"Jul", "Aug", "Sep",
|
73 |
|
|
"Oct", "Nov", "Dec" };
|
74 |
|
|
char time_info[32];
|
75 |
|
|
|
76 |
|
|
CYG_TEST_INIT();
|
77 |
|
|
|
78 |
|
|
CYG_TEST_INFO("sntp1 test build " __DATE__);
|
79 |
|
|
|
80 |
|
|
init_all_network_interfaces();
|
81 |
|
|
|
82 |
|
|
cyg_sntp_start();
|
83 |
|
|
|
84 |
|
|
/* The SNTP client will try to obtain NTP time updates by
|
85 |
|
|
* listening for multicasts. It can also be configured
|
86 |
|
|
* to send unicast requests to specific NTP servers. By
|
87 |
|
|
* default, unicast NTP servers are obtained from DHCP.
|
88 |
|
|
*
|
89 |
|
|
* If unicast mode is enabled, the run the test loop twice.
|
90 |
|
|
* The first time, unicast requests will be sent. The
|
91 |
|
|
* second time, the unicast list will be unconfigured and
|
92 |
|
|
* the client will listen only for multicasts.
|
93 |
|
|
*
|
94 |
|
|
* Note that this test is somewhat bogus since multicast
|
95 |
|
|
* NTP packets will actually allow both test loops to
|
96 |
|
|
* pass. But it is the best we can do for the automated
|
97 |
|
|
* test, so consider this more of a usage example.
|
98 |
|
|
*/
|
99 |
|
|
#ifdef CYGPKG_NET_SNTP_UNICAST
|
100 |
|
|
loop = 2;
|
101 |
|
|
#else
|
102 |
|
|
loop = 1;
|
103 |
|
|
#endif
|
104 |
|
|
while (loop-- > 0)
|
105 |
|
|
{
|
106 |
|
|
if (loop == 1) {
|
107 |
|
|
CYG_TEST_INFO("Testing SNTP unicast mode.");
|
108 |
|
|
waittime=14;
|
109 |
|
|
} else {
|
110 |
|
|
CYG_TEST_INFO("Testing SNTP multicast mode.");
|
111 |
|
|
waittime=20;
|
112 |
|
|
}
|
113 |
|
|
for (seconds = waittime; seconds > 0; seconds--) {
|
114 |
|
|
now = time(NULL);
|
115 |
|
|
ctime_r(&now, time_info);
|
116 |
|
|
time_info[strlen(time_info)-1] = '\0'; // Strip \n
|
117 |
|
|
CYG_TEST_INFO(time_info);
|
118 |
|
|
cyg_thread_delay(100);
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
now = time(NULL);
|
122 |
|
|
|
123 |
|
|
if ( now < (5 * 60)) {
|
124 |
|
|
CYG_TEST_FAIL_FINISH("Nothing recieved from the SNTP server");
|
125 |
|
|
} else {
|
126 |
|
|
|
127 |
|
|
i=sscanf(__DATE__, "%s %d %d",month,&tm.tm_mday,&tm.tm_year);
|
128 |
|
|
CYG_ASSERT(3==i,"sscanf did not return enough results");
|
129 |
|
|
for (i=0; i < 12; i++) {
|
130 |
|
|
if (!strcmp(month,months[i]))
|
131 |
|
|
break;
|
132 |
|
|
}
|
133 |
|
|
tm.tm_mon = i;
|
134 |
|
|
tm.tm_year -= 1900;
|
135 |
|
|
|
136 |
|
|
build_time = mktime(&tm);
|
137 |
|
|
CYG_ASSERT(-1 != build_time,"mktime returned -1");
|
138 |
|
|
|
139 |
|
|
if (build_time > time(NULL)) {
|
140 |
|
|
CYG_TEST_FAIL_FINISH("Build time is ahead of SNTP time");
|
141 |
|
|
} else {
|
142 |
|
|
if ((build_time + 60 * 60 * 24 * 90) < time(NULL)) {
|
143 |
|
|
CYG_TEST_FAIL_FINISH("Build time is more than 90 days old");
|
144 |
|
|
}
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
#ifdef CYGPKG_NET_SNTP_UNICAST
|
149 |
|
|
/* For the second pass of the test, we set the time
|
150 |
|
|
* back to epoch and unconfigure the list of SNTP
|
151 |
|
|
* unicast servers. This will test non-unicast mode.
|
152 |
|
|
*/
|
153 |
|
|
cyg_sntp_set_servers(NULL, 0);
|
154 |
|
|
cyg_libc_time_settime(0);
|
155 |
|
|
#endif
|
156 |
|
|
}
|
157 |
|
|
CYG_TEST_PASS_FINISH("sntp1 test is complete");
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL*2)
|
161 |
|
|
static char thread_stack[STACK_SIZE];
|
162 |
|
|
static cyg_thread thread_data;
|
163 |
|
|
static cyg_handle_t thread_handle;
|
164 |
|
|
|
165 |
|
|
void
|
166 |
|
|
cyg_user_start(void)
|
167 |
|
|
{
|
168 |
|
|
// Create a main thread, so we can run the scheduler and have time 'pass'
|
169 |
|
|
cyg_thread_create(10, // Priority - just a number
|
170 |
|
|
net_test, // entry
|
171 |
|
|
0, // entry parameter
|
172 |
|
|
"Network test", // Name
|
173 |
|
|
thread_stack, // Stack
|
174 |
|
|
STACK_SIZE, // Size
|
175 |
|
|
&thread_handle, // Handle
|
176 |
|
|
&thread_data // Thread data structure
|
177 |
|
|
);
|
178 |
|
|
cyg_thread_resume(thread_handle); // Start it
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
#else //defined(CYGINT_ISO_STDIO_FORMATTED_IO) && defined(CYGINT_ISO_STRING_STRFUNCS)
|
182 |
|
|
|
183 |
|
|
void cyg_user_start(void)
|
184 |
|
|
{
|
185 |
|
|
CYG_TEST_NA("Not all the required packages are available");
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
#endif
|