This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

rdate for eCos


Hi

If anyone is interrested, here is source code for
getting the current time from a time server.
ie. use this in some initialiation code to get the current date and time

Maby someone wants to add this as a bootup feature??

--

Carl van Schaik
-----------------------------------------------------
4th Year B.Sc. Electrical Engineering, UCT
UCT Ground penetrating radar group

Rm 6.21
Menzies Building
University of Cape Town
Rondebosch, 7700
Western Cape
South Africa

Tel +27 21 650-3467
Fax +27 21 650-3465
/*	$NetBSD: rdate.c,v 1.8 1997/10/18 03:51:03 lukem Exp $	*/

/*
 * Copyright (c) 1994 Christos Zoulas
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Christos Zoulas.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * rdate.c: Set the date from the specified host
 * 
 * 	Uses the rfc868 time protocol at socket 37.
 *	Time is returned as the number of seconds since
 *	midnight January 1st 1900.
 */

/*
 * Adapted for eCos by
 * Carl van Schaik <carl@openh.org>
 * 08-09-2000
 */

#include <stdlib.h>
#include <stdio.h>
#include <network.h>
#include <arpa/inet.h>

/* seconds from midnight Jan 1900 - 1970 */
#if __STDC__
#define DIFFERENCE 2208988800UL
#else
#define DIFFERENCE 2208988800
#endif

/* Only accepts numeric representation eg "137.158.128.1" */
void init_date(char *time_server)
{
	int             s;
	time_t          tim;
	struct sockaddr_in sa;

	diag_printf("Requesting date/time from: %s\n", time_server);

	if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	    return;

	memset(&sa, 0, sizeof (sa));
	sa.sin_family = AF_INET;
	sa.sin_port = ntohs(37);;
	inet_aton(time_server, &(sa.sin_addr));

	if (connect(s, (struct sockaddr *) & sa, sizeof(sa)) == -1)
		return;

	if (read(s, &tim, sizeof(time_t)) != sizeof(time_t))
		return;

	close(s);
	tim = ntohl(tim) - DIFFERENCE;

	if (cyg_libc_time_settime(tim))
	    return;

	diag_printf("New time: %s\r\n", ctime(&tim));
	return;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]