#ifndef BOARD_BT6703_H
#define BOARD_BT6703_H

#define SERVICE_RS232_UART_TABLE(X)     \
X( SERVICE_TXDINT,   GPIOD,   5, AF_USART2) \
X( SERVICE_RXDINT,   GPIOD,   6, AF_USART2)

#define RS232_UART_TABLE(X)     \
X( TXDINT,   GPIOA,   9, AF_USART1) \
X( RXDINT,   GPIOA,   10, AF_USART1)

#define SPI2_TABLE(X) \
X( SPI2_MISO,   GPIOB,  14, AF_SPI2) \
X( SPI2_MOSI,   GPIOB,  15, AF_SPI2) \
X( SPI2_SCK,    GPIOB,  10, AF_SPI2) \
X( SPI2_NSS,    GPIOB,  9,  GPIO_OUT | GPIO_SET)

#define MCU_PERIPHERALS(X)  \
SERVICE_RS232_UART_TABLE(X)\
RS232_UART_TABLE(X) \
SPI2_TABLE(X)

#define DI_TABLE(X)                  \
X( DIN1,       GPIOA,   0, GPIO_IN)

#ifdef BT6702_SERVICE
#define RELAYS(X) \
X( DOUT1,      GPIOE,  3, GPIO_OUT)\
X( DOUT2,      GPIOE,  4, GPIO_OUT) \
X( DOUT_PREV_VER,      GPIOE,  5, GPIO_OUT)
#else
#define RELAYS(X) \
X( DOUT1,      GPIOE,  3, GPIO_OUT)\
X( DOUT2,      GPIOE,  4, GPIO_OUT) 
#endif 


#define LEDS(X) \
X( LED_INIT_R,  GPIOA,  4,  GPIO_OUT | GPIO_INV) \
X( LED_INIT_G,  GPIOA,  3,  GPIO_OUT | GPIO_INV) \
X( LED_MAJOR_R, GPIOA,  5,  GPIO_OUT | GPIO_INV) \
X( LED_MINOR_G, GPIOE,  11, GPIO_OUT | GPIO_INV) \
X( LED_MINOR_R, GPIOE,  12, GPIO_OUT | GPIO_INV)

#define JUMPERS(X) \
X( MODE_JUMPER, GPIOE,  6,  GPIO_IN_PU | GPIO_INV)

#define BUTTONS(X) \
X( KEY_DEF,     GPIOE,  7,  GPIO_IN )//| GPIO_INV

#define WDG_PIN(X) \
X( _WDG,     GPIOE,   15, GPIO_OUT)

#define GPIO_TABLE(X)   \
MCU_PERIPHERALS(X)  \
DI_TABLE(X)         \
RELAYS(X) \
LEDS(X)             \
JUMPERS(X) \
BUTTONS(X)      \
WDG_PIN(X)

/**
  * @brief  Маркер ревизии платы для валидации *.bin файла
  *
  */
#define HW_REV                          "BT-6703"

/**
  * @brief  Версия прошивки
  */
#define VERSION                         "1.1"

#define RTC_ENABLE                  //RTC clock
#define LED_ENABLE                  //LED индикация
#define BUTTON_ENABLE               //Кнопки
#define DOUTS_ENABLE                  //Выходы
#define DINS_ENABLE                 //Входы
#define NET_ENABLE                  //Ethernet сеть
#define SSL_SERVER_ENABLE           //HTTPS сервер
#define RADIUS_SERVER_ENABLE        //RADIUS сервер
#define SNMP_ENABLE                 //SNMP протокол
#define UPS_ENABLE                  //UPS
//#define DEBUG_FREERTOS            //Мониторинг FreeRtos

#define SETTINGS_TABLE \
XSETTING( SNMP_t,	sSnmp, SETTINGS_SetSnmpDef, ALL_DEFAULT ) \
XSETTING( AUTH_t,	sAuth[MAX_WEB_USERS], SETTINGS_SetServiceDef, PART_DEFAULT_2 ) \
XSETTING( SNTP_t,	sSNTP, SETTINGS_SetSntpDef, ALL_DEFAULT ) \
XSETTING( RELAY_Settings_t,	sRelays[OUTPUTS_TOTAL_COUNT], SETTINGS_SetRelaysDef, ALL_DEFAULT ) \
XSETTING( DIN_Settings_t,	sDINs[INPUTS_TOTAL_COUNT], SETTINGS_SetDINSDef, ALL_DEFAULT ) \
XSETTING( UPS_Setting_t, UPS_Setting, SETTINGS_SetUPSSettingsDef, ALL_DEFAULT ) \
XSETTING( ALARM_MNGR_t, sAlarmManager, SETTINGS_SetAlarmManagerDef, ALL_DEFAULT ) \
XSETTING( RADIUS_t, sRADIUS, SETTINGS_SetRADIUSDef, PART_DEFAULT_1 ) \
XSETTING( char, our_srv_crt[1705], SETTINGS_SetSSLcrtDef, PART_DEFAULT_1 ) \

#define SNMP_DEV_ROOT_OID       6

#define MAX_IRQ_HANDLERS 4

#define WEB_SETTINGS_TAGS_TABLE \
  XJSON_SETTINGS_TAG("read_community",     GetReadCommunity,                        SetReadCommunity) \
  XJSON_SETTINGS_TAG("write_community",    GetWriteCommunity,                       SetWriteCommunity) \
  XJSON_SETTINGS_TAG("managerIP",          GetManagerIp,                            SetManagerIp) \
  XJSON_SETTINGS_TAG("managerIP2",         GetManagerIp2,                           SetManagerIp2) \
  XJSON_SETTINGS_TAG("managerIP3",         GetManagerIp3,                           SetManagerIp3) \
  XJSON_SETTINGS_TAG("managerIP4",         GetManagerIp4,                           SetManagerIp4) \
  XJSON_SETTINGS_TAG("managerIP5",         GetManagerIp5,                           SetManagerIp5) \
  XJSON_SETTINGS_TAG("di1",                GetDIN0TypeActStr,                       SetDIN0TypeActStr) \
  XJSON_SETTINGS_TAG("ro1",                GetRO0TypeActStr,                        SetRO1TypeActStr) \
  XJSON_SETTINGS_TAG("ro2",                GetRO1TypeActStr,                        SetRO2TypeActStr) \
  XJSON_SETTINGS_TAG("ipaddr",             GetIpStr,                                SetIPStr) \
  XJSON_SETTINGS_TAG("gw",                 GetGatewayStr,                           SetGatewayStr) \
  XJSON_SETTINGS_TAG("mask",               GetMaskStr,                              SetMaskStr) \
  XJSON_SETTINGS_TAG("dhcp",               GetDhcpStateStr,                         SetDhcpStateStr) \
  XJSON_SETTINGS_TAG("swauth",             GetAuthEnableStateStr,                   SetAuthEnableStateStr) \
  XJSON_SETTINGS_TAG("rs_server",          GetRDSIpStr,                             SetRDSIpStr) \
  XJSON_SETTINGS_TAG("rs_port",            GetRDSPortStr,                           SetRDSPortStr) \
  XJSON_SETTINGS_TAG("rs_pwd",             GetRDSPasswordkStr,                      SetRDSPasswordkStr) \
  XJSON_SETTINGS_TAG("rs_key",             GetRDSKeyAccesstStr,                     SetRDSKeyAccesstStr) \
  XJSON_SETTINGS_TAG("rs_enabled",         GetRDSEnableStateStr,                    SetRDSEnableStateStr) \
  XJSON_SETTINGS_TAG("date",               GetDateStr,                              SetDateStr) \
  XJSON_SETTINGS_TAG("time",               GetTimeStr,                              SetTimeWebStr) \
  XJSON_SETTINGS_TAG("ntp",                GetSntpStateStr,                         SetSntpStateStr) \
  XJSON_SETTINGS_TAG("ntpservip",          GetSntpServerIpStr,                      SetSntpServerIpStr) \
  XJSON_SETTINGS_TAG("utc",                GetSntpTimeZoneStr,                      SetSntpTimeZoneStr) \
  XJSON_SETTINGS_TAG("utm",                GetUnixTimeStr,                          SetEmptyFuncStr) \
  XJSON_SETTINGS_TAG("lastsynctime",       GetSntpLastDataStr,                      SetEmptyFuncStr) \
  XJSON_SETTINGS_TAG("ups_cell_min",       GetUPSVoltCellMinStr,                    SetUPSVoltCellMinStr) \
  XJSON_SETTINGS_TAG("ups_cell_max",       GetUPSVoltCellMaxStr,                    SetUPSVoltCellMaxStr) \
  XJSON_SETTINGS_TAG("temp_high",          GetTemperatureAlarmHighRangeStr,         SetTemperatureAlarmHighRangeStr) \
  XJSON_SETTINGS_TAG("temp_low",           GetTemperatureAlarmLowRangeStr,          SetTemperatureAlarmLowRangeStr) \
  XJSON_SETTINGS_TAG("temp_hist",          GetTemperatureAlarmHisteStr,             SetTemperatureAlarmHisteStr) \
  XJSON_SETTINGS_TAG("loadvolt_high",      GetLoadAlarmHighRangeStr,                SetLoadAlarmHighRangeStr) \
  XJSON_SETTINGS_TAG("loadvolt_hist",      GetLoadAlarmHisteStr,                    SetLoadAlarmHistStr) \
  XJSON_SETTINGS_TAG("netsettings_changed",GetWebReinitFlag,                        SetEmptyFuncStr) \

  #define WEB_PARAMS_TAGS_TABLE \
  XJSON_PARAMS_TAG("AC",                 GetInputVoltageStr) \
  XJSON_PARAMS_TAG("DC",                 GetOutputVoltageStr) \
  XJSON_PARAMS_TAG("in_freq",            GetInputFreqStr) \
  XJSON_PARAMS_TAG("out_freq",           GetOutputFreqStr) \
  XJSON_PARAMS_TAG("pwr",                GetPowerStr) \
  XJSON_PARAMS_TAG("bat_rem_cap",        GetBatCapacityStr) \
  XJSON_PARAMS_TAG("inner_temp",         GetInternalTempStr) \
  XJSON_PARAMS_TAG("bat_time_left",      GetRuntimeStr) \
  XJSON_PARAMS_TAG("di1",                GetDIN0StatusStr) \
  XJSON_PARAMS_TAG("ro1",                GetDOUT0StatusStr) \
  XJSON_PARAMS_TAG("ro2",                GetDOUT1StatusStr) \
  XJSON_PARAMS_TAG("m_alarm",            GetAlarmMonitorStr) \
  XJSON_PARAMS_TAG("u_alarm",            GetAlarmStr) \
  XJSON_PARAMS_TAG("netsettings_changed",GetWebReinitFlag) \

#define TRAPS_TABLE \
  XTRAP(FW_VERSION_UPDATE,                 1,      true,      GetVersionStr) \
  XTRAP(FW_VERSION_UPDATED,                1,      true,      GetVersionStr) \
  XTRAP(DEVICE_RESTORED,                   1,      true,      GetVersionStr) \
  XTRAP(DEVICE_REBOOTED,                   1,      true,      GetVersionStr) \
  XTRAP(DI0_NORM,                          9,      true,      GetDIN0StatusStr) \
  XTRAP(DI0_ALARM,                         9,      true,      GetDIN0StatusStr) \
  XTRAP(DO0_TOGGLED,                       5,      true,      GetDOUT0StatusStr) \
  XTRAP(DO1_TOGGLED,                       6,      true,      GetDOUT1StatusStr) \
  XTRAP(BATTERY_HIGH_TEMPERATURE_NORM,     10,     true,      GetInternalTempStr) \
  XTRAP(BATTERY_HIGH_TEMPERATURE_ALARM,    10,     true,      GetInternalTempStr) \
  XTRAP(LINE_ALARM,                        12,     true,      GetInputVoltageStr) \
  XTRAP(LINE_NORM,                         12,     true,      GetInputVoltageStr) \
  XTRAP(LOW_BAT_ALARM,                     15,     true,      GetBatCapacityStr) \
  XTRAP(LOW_BAT_NORM,                      15,     true,      GetBatCapacityStr) \
  XTRAP(POWER_ALARM,                       14,     true,      GetPowerStr) \
  XTRAP(POWER_NORM,                        14,     true,      GetPowerStr) \
  XTRAP(CONNECT_MONITOR_ALARM,             17,     true,      GetConnectMonitorStr) \
  XTRAP(CONNECT_MONITOR_NORM,              17,     true,      GetConnectMonitorStr) \
  XTRAP(BATTERY_CONNECT_ALARM,             15,     true,      GetBatCapacityStr) \
  XTRAP(BATTERY_CONNECT_NORM,              15,     true,      GetBatCapacityStr) \
  XTRAP(BATTERY_LOW_TEMPERATURE_NORM,      10,     true,      GetInternalTempStr) \
  XTRAP(BATTERY_LOW_TEMPERATURE_ALARM,     10,     true,      GetInternalTempStr) \

typedef enum
{
  FW_VERSION_UPDATE = 1,
  FW_VERSION_UPDATED,
  DEVICE_RESTORED,
  DEVICE_REBOOTED,
  DI0_NORM,
  DI0_ALARM,
  DO0_TOGGLED,
  DO1_TOGGLED,
  BATTERY_HIGH_TEMPERATURE_NORM,
  BATTERY_HIGH_TEMPERATURE_ALARM,
  LINE_ALARM,
  LINE_NORM,
  LOW_BAT_ALARM,
  LOW_BAT_NORM,
  POWER_ALARM,
  POWER_NORM,
  CONNECT_MONITOR_ALARM,
  CONNECT_MONITOR_NORM,
  BATTERY_CONNECT_ALARM,
  BATTERY_CONNECT_NORM,
  BATTERY_LOW_TEMPERATURE_NORM,
  BATTERY_LOW_TEMPERATURE_ALARM,
  ALL_TRAPS
} TRAP_LIST_t;

typedef enum {
	LOG_SYSTEM_BOOT = 0x00,			// device booted
	LOG_SYSTEM_DEFCONFIG,	// default config applied
	LOG_UPDATE_SOFT,
	LOG_TURN_ON,		//
	LOG_PSW_CHANGE,		//
	LOG_SETTING_SAVE,		//
	LOG_LOGIN,		//
	LOG_TEST_UPS,		//
	LOG_SHUTDOWN_UPS,
	LOG_ALARM_DIO,
	LOG_DO0_STATE,
	LOG_DO1_STATE,
	LOG_ALARM_HIGH_TEMP,
	LOG_ALARM_LOW_TEMP,
	LOG_ALARM_LINE,
	LOG_ALARM_LOW_BAT,
	LOG_ALARM_POWER,
	LOG_ALARM_UPS,
	LOG_ALARM_AKB,
	LOG_VALUE,
	LOG_NONE,
} log_type_t;

#define TEST_AKB_FINISH_MONITOR
#define LINE_FAIL_MONITOR
#define LOW_BAT_MONITOR
#define LOAD_MONITOR
#define TEMP_AKB_MONITOR
#define BAT_CONNECT_MONITOR
#define UPS_CONNECT_MONITOR
#define DIN_MONITOR
#define TYPE_CRITICAL_ALARM_MONITOR

#define MONITOR_TABLE \
  XMONITOR(UPS_TestFinishMonitor,                 UPS.Present) \
  XMONITOR(UPS_LineFailMonitor,                   UPS.Present) \
  XMONITOR(UPS_LowBatMonitor,                     UPS.Present) \
  XMONITOR(UPS_PowerMonitor,                      UPS.Present) \
  XMONITOR(UPS_TemperatureHighRangeMonitor,       UPS.Present) \
  XMONITOR(UPS_TemperatureLowRangeMonitor,        UPS.Present) \
  XMONITOR(UPS_BatteryConnectMonitor,             UPS.Present) \
  XMONITOR(UPS_ConnectMonitor,                    1) \
  XMONITOR(UPS_DI0Monitor,                        1) \
  XMONITOR(UPS_CriticalAlarmMonitor,              1) \
  XMONITOR(UPS_NonCriticalAlarmMonitor,           1) \

#define LED_INIT_OK               LED_INIT_G
#define LED_INIT_ERR              LED_INIT_R
#define LED_ALARM                 LED_MAJOR_R
#define LED_GREEN_MINOR           LED_MINOR_G
#define LED_RED_MINOR             LED_MINOR_R

#define RELAY_NO

#define SERVICE_CONTROLLER_IP  "192.168.0.3"

#endif /* BOARD_PRS_H */
