/* getTime.c */
static  char  SccsId[] UNUSED = "%W%   %G%  Spectral Dynamics" ;
#include "stdio.h"
#include "stdlib.h"
#include "vxWorks.h"
#include "string.h"

#include "time.h"
#include "timers.h"
#include "getTime.h"
#include "boardId.h"
#include "tc_tcpip.h"
#include "acqLibPrototypes.h"
#include "taskLib.h"

#define DEBUG 0
#include "debug.h"


int 
getTime( void )
{
    int    status;
    time_t timeValue;
    struct timespec clockValue;
    char   buff[80], *bP;
    int    length = 80;

    timeValue = getHostTime( ) ;

    if( ! timeValue )
    {
	printf("getTime() getHostTime is hosed\n") ;
	switch (get_board_id())
	{
	  case MOTOROLA_230X:
	    timeValue = getMotTime();
	    break;
	  default:
	    printf("Unknown Board type for Time\n");
	    break;
	}
    }
    
    if( ! timeValue )
    {
	printf("No valid time found\n");
	return ERROR;
    }


    clockValue.tv_sec =  timeValue ;
    clockValue.tv_nsec = 0 ;
    status = clock_settime( CLOCK_REALTIME, &clockValue ) ;
    if (status)
    {
	printf("Error in getTime=%d\n",status);
	return ERROR;
    }

    D(clock_gettime( CLOCK_REALTIME, &clockValue ));
    D(printf("clock_gettime sec=%d nsec=%d\n", (int)clockValue.tv_sec, (int)clockValue.tv_nsec));

    ctime_r( (time_t *)&timeValue, buff, (size_t *)&length);
    bP = (char *)strchr( buff, 0x0a );
    *bP = 0;
    printf("%s\n", buff);

    return OK;
}


/*
 * Port 13 returns local time as a string in the following format:
 *   Sun Sep 16 01:03:52 1973\n\0
 *
 * This routine converts this to broken-down time and then to time in seconds
 *  to set the clock on the MDSP3
 */

#define DOWINDEX    0
#define MONTHINDEX  4
#define DAYINDEX    8
#define HOURINDEX  11
#define MININDEX   14
#define SECINDEX   17
#define YEARINDEX  20

char *DayOfWeek[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
		    "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
char *Months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		   "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
time_t 
getHostTime( void )
{
    int			numBytes, ilop, index, acp_num ;
    unsigned int	timeValue ;
    char                *host, *cP;
    SockArgs            *args;
    char                buff[80];
    struct tm           brokenTime;

    host = get_local_ip();
    acp_num = get_acp_number();

    args = tc_client_wait( host, 13 /* port */, 10 /* secs timeout */, acp_num ) ;

    timeValue = 0 ;

    if( args )
    {
	
	numBytes = tc_read_sock( args->sock, buff, 24 ) ;
	tc_kill_socket( args ) ;
	if( numBytes < 4 )
	{
	    printf("shellGetTime() Read %d bytes\n", timeValue) ;
	    return 0;
	}
	buff[numBytes] = 0;
	D(printf("getHostTime (from Workstation)= %s\n", buff));

	bzero( (char *)&brokenTime, sizeof(brokenTime));

	index = -1;
	cP = buff + MONTHINDEX;
	for ( ilop = 0; ilop < 24; ilop++ )
	    if ( ! strncmp(cP, Months[ilop], 3) )
	    {
		index = ilop;
		if (index > 11)
		    index -= 12;
		break;
	    }
	if (index == -1)
	    return 0;
	brokenTime.tm_mon = index;
	
	cP = buff + DAYINDEX;
	brokenTime.tm_mday = atoi(cP);

	cP = buff + HOURINDEX;
	brokenTime.tm_hour = atoi(cP);

	cP = buff + MININDEX;
	brokenTime.tm_min = atoi(cP);

	cP = buff + SECINDEX;
	brokenTime.tm_sec = atoi(cP);

	cP = buff + YEARINDEX;
	brokenTime.tm_year = atoi(cP) - 1900;

	timeValue = mktime( &brokenTime );
    }

    return( timeValue ) ;
}


#if 0

/* Port 37 gets binary GMT */

time_t 
getHostTime( void )
{
    int			numBytes ;
    unsigned int	timeValue ;
    char                *host;
    SockArgs            *args;
    D(char              buff[80]);
    D(int               length = 80);
    
    host = get_local_ip();

    args = tc_client_wait( host, 37 /* port */, 10 /* secs timeout */ ) ;

    timeValue = 0 ;

    if( args )
    {
	numBytes = tc_read_sock( args->sock, (char *)&timeValue, sizeof(timeValue) ) ;
	tc_kill_socket( args ) ;
	if( numBytes != 4 )
	    printf("shellGetTime() Read %d bytes\n", timeValue) ;

	timeValue -= JAN_1_1970 ;
	
	D(ctime_r( (time_t *)&timeValue, buff, (size_t *)&length));
	D(host = (char *)strchr( buff, 0x0a ));
	D(*host = 0);
	D(printf("getHostTime= %s\n", buff));
    }
    else
    {
	timeValue = 0 ;
    }

    return( timeValue ) ;
}

#endif


