/**
 *@file    sr_rtc.h
 *@brief   rtc抽象接口头文件
 *@details 提供rtc相关抽象接口、枚举、结构体
 *@copyright Copyright (c) 2024 lierda. All rights reserved.
 *@author  Lierda-RDC
 *@date    2024-04-18
 *@example ZhaoyangSDK\example\pal\hal\rtc_example.c
 */

#ifndef _SR_HAL_RTC_H
#define _SR_HAL_RTC_H

#include "sr_device.h"

/**
 * @brief 日期时间结构体
 */
typedef struct SR_RTC_TIME
{
  uint8_t tmSec;   /*!< seconds [0,59]*/
  uint8_t tmMin;   /*!< minutes [0,59]*/
  uint8_t tmHour;  /*!< hour [0,23]*/
  uint8_t tmMday;  /*!< day of month [1,31]*/
  uint8_t tmWday;  /*!< wday [1-7],该值在设置RTC时忽略  */
  uint8_t tmMon;   /*!< month of year [1,12]*/
  uint16_t tmYear; /*!< year [2000-2100]*/
} SR_RTC_TIME_T;
#define IS_RTC_TIME(time) ((time->tmSec < 60) && (time->tmMin < 60) && (time->tmHour < 24) && (time->tmMday <=31)&& (time->tmMday >=1) && (time->tmMon <=12)&& (time->tmMon >=1) && (time->tmYear >=2000) && (time->tmYear <=2100))

typedef  enum SR_RTC_EVT SR_RTC_EVT_E;

/**
 * @brief RTC事件回调
 *
 * @param[in]  evt   RTC事件枚举，取值为SR_RTC_EVT_E
 * @param[in]  args  用户参数
 *
 * @return 无
 */
typedef void (*RtcIntCb)(SR_RTC_EVT_E evt,void *args);


#include "sr_bsp_rtc.h"

#ifdef SR_USE_RTC_EX

/**
 * @brief BSP适配接口定义
 */
typedef struct SR_RTC_OPS
{
  SR_DEVICE_T *(*rtc_open)(SR_RTC_ID_E rtcId);           /*!< 打开RTC的函数指针 */
  int (*rtc_close)(SR_DEVICE_T *dev);                    /*!< 关闭RTC的函数指针 */
  int (*rtc_get)(SR_DEVICE_T *dev, SR_RTC_TIME_T *time); /*!< 获取RTC时间的函数指针 */
  int (*rtc_set)(SR_DEVICE_T *dev, SR_RTC_TIME_T *time); /*!< 设置RTC时间的函数指针 */
} SR_RTC_OPS_T;

#endif

/**
 * @brief RTC事件枚举
 */
enum SR_RTC_EVT
{
  SR_RTC_EVT_WAUP = 0,  /*!< 唤醒中断 */
  SR_RTC_EVT_ALARM1,    /*!< 闹钟1 中断 */
  SR_RTC_EVT_ALARM2,    /*!< 闹钟1 中断 */
  
};


/**
 * @brief RTC设备结构体
 */
typedef struct SR_RTC_DEV
{
  SR_DEVICE_T parent; /*!< 继承自设备结构体 */
  RtcIntCb cb;        /*!< 事件回调函数 */
  void *cbArgs;       /*!< 事件回调函数的用户参数 */

#ifdef SR_USE_RTC_EX
  const SR_RTC_OPS_T *rtcOps; /*!< RTC操作函数指针集 */
#endif

} SR_RTC_DEV_T;

/*****************抽象接口*****************************************/
/**
 * @brief 打开RTC设备
 *
 * @param[in] id  RTC设备ID，取值为SR_RTC_ID_E
 *
 * @return 成功：返回RTC设备指针，失败：返回SR_NULL
 */
#define SR_HalRtcOpen(id) BspRtcOpen(id)

/**
 * @brief 关闭RTC设备
 *
 * @param[in] dev  SR_DEVICE_T *类型的设备指针
 *
 * @return 0成功，失败返回负值错误码
 */
#define SR_HalRtcClose(dev) BspRtcClose(dev)

/**
 * @brief 获取rtc时间(默认：UTC+0)
 *
 * @param[in] dev    SR_DEVICE_T *类型的设备指针
 * @param[in] time   SR_RTC_TIME_T*类型的时间结构体
 *
 * @return 0成功，失败返回负值错误码
 */
#define SR_HalRtcGet(dev, time) BspRtcGet(dev, time)

/**
 * @brief 设置rtc时间(UTC)
 *
 * @param[in] dev   SR_DEVICE_T *类型的设备指针
 * @param[in] time  SR_RTC_TIME_T*类型的时间结构体
 *
 * @return 0成功，失败返回负值错误码
 */
#define SR_HalRtcSet(dev, time) BspRtcSet(dev, time)


/**
 * @brief 使能RTC唤醒中断
 *
 * @param[in] dev     SR_DEVICE_T *类型的设备指针
 * @param[in] enable  是否使能。1：使能，0：禁止
 * @param[in] period  唤醒周期，单位：秒
 *
 * @return 0成功，失败返回负值错误码
 */
#define SR_HalRtcWkupEnable(dev,enable,period) BspRtcWkupEnable(dev,enable,period)


/**
 * @brief 注册RTC事件回调函数
 *
 * @param[in] dev   SR_DEVICE_T *类型的设备指针
 * @param[in] cb    回调函数，RtcIntCb类型
 * @param[in] args  回调函数参数，用户自定义参数
 *
 * @return 0成功，失败返回负值错误码
 */
#define SR_HalRtcIntCbReg(dev,cb,args)  BspRtIntCbReg(dev,cb,args)

#endif
