/**
* Copyright(c) 2011 Sunmedia Technologies - All Rights Reserved.
*
* @file
*
* @brief The header file of al time module
*
*/

#ifndef _AL_TIME_H_
#define _AL_TIME_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "al_basictypes.h"

/**
* @brief define the default value of year
*
*/
#define	DEFAULT_YEAR			(2000)

/**
* @brief define the default value of month
*
*/
#define	DEFAULT_MONTH			(1)

/**
* @brief define the default value of day
*
*/
#define	DEFAULT_DAY			(1)

/**
* @brief define the default value of hour
*
*/
#define	DEFAULT_HOUR			(0)

/**
* @brief define the default value of minute
*
*/
#define	DEFAULT_MIN			(0)

/**
* @brief define the default value of second
*
*/
#define	DEFAULT_SECOND		(0)

/**
* @brief GMT offset mode
*
*/
typedef enum
{
	AL_TIME_GMT_OFFSET_ZERO = 24,
	AL_TIME_GMT_OFFSET_AUTO = 49,
	AL_TIME_GMT_OFFSET_MAX = 50,
} AL_Time_GMT_OffSet_t;

/**
* @brief Day light saving mode
*
*/
typedef enum {
	AL_TIME_DST_OFF = 0,
	AL_TIME_DST_ON,
	AL_TIME_DST_MAX,
} AL_Time_DstMode_t;

/**
* @brief System time is obtained from where?
*
*/
typedef  enum _al_time_SyncMode_
{
	AL_TIME_SyncMode_Manual = 0,
	AL_TIME_SyncMode_Auto,  /* get systime time from TOT */ 
	AL_TIME_SyncMode_MAX,
}AL_Time_SyncMode_e;

/**
* @brief al time format including year, month, day, hour, min and second
*
*/
typedef struct _AL_Time_t
{
	al_uint16 	Year;						/*!< \brief year */
	al_uint8 	Month;						/*!< \brief month */
	al_uint8 	Day;						/*!< \brief day */
	al_uint8 	Hour;						/*!< \brief hour */
	al_uint8 	Min;						/*!< \brief minute */
	al_uint8 	Sec;						/*!< \brief second */
} AL_Time_t;

/**
* @brief Data structure for al time module to get time zone, DST, etc
*
*/
typedef struct _AL_Time_Context_t
{
	al_uint8 Timezone;
	al_uint8 DST;
	al_uint8 Param1;
	al_uint8 Param2;
} AL_Time_Context_t;

/**
* @brief Callback function prototype to get context
*
*/
typedef AL_Return_t (* AL_Time_GetConext)(AL_Time_Context_t * pstContext);

/**
* === Miscellaneous Time Operation===
*/
/**
* @brief Called to update system time periodically.
*
* This API is used to update the system time every second, and check
* the available timer and post message to its SM if timeout happened
*
* @param none
*
* @retval  None
*/
void AL_Time_Timeout(void);

/**
* @brief To reset system time when user change country.
*
* @note This API is used to reset system time when change country.
*
* @param none
*
* @retval  AL_SUCCESS means success
*/
AL_Return_t AL_Time_Reset(void);

/**
* @brief This API is used to get days by the requested month and year
*
* @param u16Year year
*
* @param u8Month Month
*
* @retval  None
*/
al_uint8 AL_Time_GetDayByMonth(al_uint16 u16Year, al_uint8 u8Month);

/**
* @brief This API is used to get GMT time offset, which is also affected by DST
*
* @param None
*
* @retval  AL_SUCCESS means succss.  AL_FAILURE means failed.
*/
al_int32 AL_Time_GetGMTOffset(void);

/**
* @brief This API is used to set next time offset value which is used for tot update
*
* @param None
*
* @retval  AL_SUCCESS means succss.  AL_FAILURE means failed.
*/
al_int32 AL_Time_GetNextGMTOffset(void);

/**
* @brief This API is used to get time offset
*
* @param pTimeOffset Time offset
*
* @retval  None
*/
void AL_Time_GetTimeOffset(al_int32 *pTimeOffset);

/**
* @brief Get total secs from 1970/01/01
*
* @param CurTimeInSec Get total secs from 1970/01/01
*
* @retval  AL_SUCCESS means success
*/
AL_Return_t AL_Time_GetCurTimeInSecs(al_uint32 *CurTimeInSec);

/**
* @brief This API is used to get system date in the form of Y,M,D, this date is affected by GMT offset
*
* @param pusYear MJD
*
* @param pucMonth year
*
* @param pucDay month
*
* @retval  None
*/
void AL_Time_GetDate(al_uint16 *pusYear, al_uint8 *pucMonth, al_uint8 *pucDay);

/**
* @brief This API is used to get system time in the form of Hour,Min,Second, this value is affected by GMT offset
*
* @param pucHour Hour
*
* @param pucMin Minite
*
* @param pucSec Second
*
* @retval  None
*/
void AL_Time_GetTime(al_uint8 *pucHour, al_uint8 *pucMin, al_uint8 *pucSec);

/**
* @brief This API is used to get current system time has been updated by ts or not after power on
*
* @retval  g_bDateUpdated
*/
al_bool AL_Time_GetDateBeUpdated(void);


/**
* === MJD Time Operation===
*/
/**
* @brief :This API is used to get system date in the form of MJD, this date is affected by GMT offset
*
* @param None
*
* @retval  MJD
*/
al_uint32 AL_Time_GetMjd(void);

/**
* @brief :This API is used to get system date in the form of MJD, this date is not affected by GMT offset
*
* @param None
*
* @retval  None
*/
al_uint32 AL_Time_GetRawMjd(void);


/**
* @brief This API is used to get system time in the form of Hour,Min,Second
*
* @param pucHour Hour
*
* @param pucMin Minite
*
* @param pucSec Second
*
* @retval  None
*/
void AL_Time_GetRawTime(al_uint8 * pu8Hour, al_uint8 * pu8Min, al_uint8 * pu8Sec);

/**
* @brief This API is used to translate date from MJD format to YMD format
*
* @param uiMjd MJD
*
* @param pusYear year
*
* @param pucMonth month
*
* @param pucDay day
*
* @retval  al_true means succss.  al_false means failed.
*/
al_bool AL_Time_MjdToYmd(al_uint32 uiMjd, al_uint16 *pusYear, al_uint8 *pucMonth, al_uint8 *pucDay);


/**
* @brief This API is used to translate date from YMD format to MJD format
*
* @param usYear Year
*
* @param ucMonth Month
*
* @param ucDay Day
*
* @retval  MJD
*/
al_uint32 AL_Time_YmdToMjd(al_uint16 usYear, al_uint8 ucMonth, al_uint8 ucDay);


/**
* @brief :This API is used to get system time in seconds, this value should be less
* then 86400 (= 60*60*24), this value is not affected by GMT offset
*
* @param None
*
* @retval  Raw second
*/
al_uint32 AL_Time_GetRawSecond(void);


/**
* === Date And Seconds Transfer Operation ===
*/
/**
* @brief Translate date to secs(from 1970/01/01)
*
* @param p_RTCDate Date
*
* @param p_u32secs Total Seconds
*
* @retval  AL_SUCCESS means success
*/
AL_Return_t AL_Time_DateToSecs(const AL_Time_t *p_RTCDate, al_uint32 *p_u32secs);


/**
* @brief Translate date to secs(from 1970/01/01)
*
* @param p_RTCDate Date
*
* @param p_u32secs Total Seconds from 1970/01/01
*
* @retval  AL_SUCCESS means success
*/
AL_Return_t AL_Time_Secs2Date(const al_uint32 *p_u32secs, AL_Time_t *p_RTCDate);


/**
* @brief This API is used to set time dst
*
* @param dTimeDST time dst applied to
*
* @retval  None
*/
void AL_Time_SetTimeDST(al_uint32 dTimeDST);


/**
* @brief This API is used to set time offset
*
* @param iTimeOffset time offset applied to
*
* @retval  None
*/
void AL_Time_SetTimeOffset(al_int32 iTimeOffset);


/**
* @brief This API is used to set current system time when boot up
*
* @param None
*
* @retval  None
*/
void AL_Time_BootOnUpdateTime(void);


/**
* @brief This API is used to update RTC
*
* @param None
*
* @retval  None
*/
void AL_Time_UpdateRTC(void);


/**
* @brief This API is set system time which user set by manual 
*
* @param None
*
* @retval  None
*/
AL_Return_t AL_Time_SetSystimefromManual(AL_Time_t* stTime, al_bool bSync);


/**
* @brief This API is get system time which user set by manual 
*
* @param None
*
* @retval  None
*/
AL_Return_t AL_Time_GetSystimefromManual(AL_Time_t* stTime);

/**
* @brief This API is used to update RTC
*
* @param None
*
* @retval  g_SyncMode
*/
AL_Time_SyncMode_e AL_Time_GetSyncMode(void);

/**
* @brief This API is get Sync mode 
*
* @param AL_TIME_SyncMode_Manual - system time from menu
*             AL_TIME_SyncMode_Auto - system time from TOT/TDT
* @retval  None
*/
AL_Return_t AL_Time_SetSyncMode(AL_Time_SyncMode_e mode);



int AL_Time_GetTimeZoneOffset();

 AL_Return_t AL_Time_GetRawTimeInSecs(al_uint32 *CurTimeInSec);

AL_Return_t AL_Time_Get_NTPTime();

#ifdef __cplusplus
}
#endif

#endif /* _AL_TIME_H_ */

