/*
 *******************************************************************************
 *  Copyright (c) 2010-2022 VATICS(KNERON) Inc. All rights reserved.
 *
 *  +-----------------------------------------------------------------+
 *  | THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY ONLY BE USED |
 *  | AND COPIED IN ACCORDANCE WITH THE TERMS AND CONDITIONS OF SUCH  |
 *  | A LICENSE AND WITH THE INCLUSION OF THE THIS COPY RIGHT NOTICE. |
 *  | THIS SOFTWARE OR ANY OTHER COPIES OF THIS SOFTWARE MAY NOT BE   |
 *  | PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY OTHER PERSON. THE   |
 *  | OWNERSHIP AND TITLE OF THIS SOFTWARE IS NOT TRANSFERRED.        |
 *  |                                                                 |
 *  | THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT   |
 *  | ANY PRIOR NOTICE AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY |
 *  | VATICS(KNERON) INC.                                             |
 *  +-----------------------------------------------------------------+
 *
 *******************************************************************************
 */
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
#include <sys/time.h>
#include <vmf/audio_tu.h>

int main(int argc __attribute__((__unused__)), char* argv[])
{
	VMF_ATU_HANDLE_T* ptAtuHandle = NULL;
	VMF_ATU_OPTION_T tOption;
	struct timeval tNewTime;
	struct timeval tOriTime;

	if(argv[1] == NULL || argv[2] == NULL)
    {
        printf("usage: %s sec usec\n",argv[0]);
        exit(1);
    }

    ptAtuHandle = VMF_ATU_Init();
    if(!ptAtuHandle) {
    	printf("Initialized ATU handle failed!\n");
        exit(1);
    }
    tNewTime.tv_sec = atoi(argv[1]);
    tNewTime.tv_usec = atoi(argv[2]);

    gettimeofday(&tOriTime, NULL);
    settimeofday(&tNewTime, NULL);

    tOption.eCmdFlag = VMF_ATU_CMD_SYNC_TIME;
    tOption.adwUserData[0] = (unsigned int)&tOriTime;
    tOption.adwUserData[1] = (unsigned int)&tNewTime;
    if (VMF_ATU_SetOption(ptAtuHandle, &tOption))
    {
        printf("Audio time sync fail.\n");        
    }
    printf("Current Time: %ld sec, %ld usec.\n", tOriTime.tv_sec, tOriTime.tv_usec);
    printf("New Time: %ld sec, %ld usec.\n", tNewTime.tv_sec, tNewTime.tv_usec);
	

	return VMF_ATU_Release(ptAtuHandle);
}
