/**
* This function will set MCU RTC time to a new value. Time value must be given in the format of
* the structure wiced_rtc_time_t
*
* @return    WICED_SUCCESS : on success.
* @return    WICED_ERROR   : if an error occurred with any step
*/
OSStatus MicoRtcSetTime(mico_rtc_time_t* time)
{
#ifdef MICO_ENABLE_MCU_RTC
  RTC_TimeTypeDef rtc_write_time;
  RTC_DateTypeDef rtc_write_date;
  bool    valid = false;
  
  MicoMcuPowerSaveConfig(false);
  
  MICO_VERIFY_TIME(time, valid);
  if( valid == false )
  {
    return kParamErr;
  }
  rtc_write_time.RTC_Seconds = time->sec;
  rtc_write_time.RTC_Minutes = time->min;
  rtc_write_time.RTC_Hours   = time->hr;
  rtc_write_date.RTC_WeekDay = time->weekday;
  rtc_write_date.RTC_Date    = time->date;
  rtc_write_date.RTC_Month   = time->month;
  rtc_write_date.RTC_Year    = time->year;
  
  
  RTC_SetTime( RTC_Format_BIN, &rtc_write_time );
  RTC_SetDate( RTC_Format_BIN, &rtc_write_date );
  
  MicoMcuPowerSaveConfig(true);
  return kNoErr;
#else /* #ifdef MICO_ENABLE_MCU_RTC */
  UNUSED_PARAMETER(time);
  return kUnsupportedErr;
#endif /* #ifdef MICO_ENABLE_MCU_RTC */
}
/**
* This function will set MCU RTC time to a new value. Time value must be given in the format of
* the structure wiced_rtc_time_t
*
* @return    WICED_SUCCESS : on success.
* @return    WICED_ERROR   : if an error occurred with any step
*/
OSStatus platform_rtc_set_time( const mico_rtc_time_t* time )
{
#ifdef MICO_ENABLE_MCU_RTC
  bool    valid = false;

  MICO_VERIFY_TIME(time, valid);
  if( valid == false )
  {
    return kParamErr;
  }

  Chip_RTC_SetCount(LPC_RTC, convert_rtc_calendar_values_to_units_passed( time ));

	return kNoErr;
#else /* #ifdef MICO_ENABLE_MCU_RTC */

	UNUSED_PARAMETER(time);
	return kUnsupportedErr;
#endif /* #ifdef MICO_ENABLE_MCU_RTC */
}
/**
* This function will return the value of time read from the on board CPU real time clock. Time value must be given in the format of
* the structure wiced_rtc_time_t
*
* @return    WICED_SUCCESS : on success.
* @return    WICED_ERROR   : if an error occurred with any step
*/
OSStatus platform_rtc_get_time( platform_rtc_time_t* time)
{
#ifdef MICO_ENABLE_MCU_RTC
  bool    valid = false;

  if( time == 0 )
  {
    return kParamErr;
  }

  /* get current rtc time */
  convert_units_passed_to_rtc_calendar_values( Chip_RTC_GetCount(LPC_RTC), time );

  MICO_VERIFY_TIME(time, valid);
  if( valid == false )
  {
    return kParamErr;
  }

#if 0
  rtc_log(" return convert_units_passed_to_rtc_calendar_values address = 0x%x ", temp_time);
  rtc_log(" sec     = %d ", temp_time->sec);
  rtc_log(" min     = %d ", temp_time->min);
  rtc_log(" hr      = %d ", temp_time->hr);
  rtc_log(" weekday = %d ", temp_time->weekday);
  rtc_log(" date    = %d ", temp_time->date);
  rtc_log(" month   = %d ", temp_time->month);
  rtc_log(" year    = %d ", temp_time->year);
#endif

  return kNoErr;
#else /* #ifdef WICED_ENABLE_MCU_RTC */
  UNUSED_PARAMETER(time);
  return kUnsupportedErr;
#endif /* #ifdef MICO_ENABLE_MCU_RTC */
}