#ifndef BOARD_BT6710_H
#define BOARD_BT6710_H

#define RS232_UART_TABLE(X)     \
X( TXDINT,   GPIOD,   5, AF_USART2) \
X( RXDINT,   GPIOD,   6, AF_USART2)

#define SERVICE_RS232_UART_TABLE(X)     \
X( SERVICE_TXDINT,   GPIOA,   9, AF_USART1) \
X( SERVICE_RXDINT,   GPIOA,   10, AF_USART1)

#define RS485_UART_TABLE(X)     \
X( RS485_TXDINT,   GPIOD,   8, AF_USART3) \
X( RS485_RXDINT,   GPIOD,   9, AF_USART3)

#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 ONEWIRE_DATA_TABLE(X) \
X( WDATA1,      GPIOA,  0,  AF_USART4)\
X( WDATA2,      GPIOC,  12, AF_USART5)

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

#define DI_TABLE(X)                  \
X( DIN1,       GPIOE,   0, GPIO_IN)\
X( DIN2,       GPIOE,   1, GPIO_IN)

#define RELAYS(X) \
X( DOUT1,      GPIOC,  3, GPIO_OUT)\
X( DOUT2,      GPIOC,  2, GPIO_OUT)

#define LEDS(X) \
X( LED_INIT_G,  GPIOA,  4,  GPIO_OUT | GPIO_INV) \
X( LED_INIT_R,  GPIOA,  3,  GPIO_OUT | GPIO_INV) \
X( LED_STAT_G,  GPIOE,  3,  GPIO_OUT | GPIO_INV) \
X( LED_STAT_R,  GPIOE,  2,  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,     GPIOD,   0, 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-6710"

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

#define RTC_ENABLE                  //RTC clock
#define LED_ENABLE                  //LED индикация
#define BUTTON_ENABLE               //Кнопки
#define DINS_ENABLE                 //Дискретные входы
#define NET_ENABLE                  //Ethernet сеть
#define HTTP_SERVER_ENABLE           //Веб сервер
#define SNMP_ENABLE                 //SNMP протокол
#define UPS_ENABLE                  //UPS
#define PORTGW_ENABLE               //Прозрачный порт
#define DALLAS_SENSOR_ENABLE        //DS18B20
//#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( 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( PORTGW_t, sPortGw, SETTINGS_SetPortGwDef, ALL_DEFAULT ) \
XSETTING( TEMP_COTROL_t, sTempControl[MAX_T_SENSORS], SETTINGS_SetTempControlDef, ALL_DEFAULT ) \

#define SNMP_DEV_ROOT_OID       9

#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("di_name1",           GetDIN0NameStr,                            SetDIN0NameStr) \
  XJSON_SETTINGS_TAG("di2",                GetDIN1TypeActStr,                         SetDIN1TypeActStr) \
  XJSON_SETTINGS_TAG("di_name2",           GetDIN1NameStr,                            SetDIN1NameStr) \
  XJSON_SETTINGS_TAG("type_ts1",           GetTS1locationStr,                         SetTS1locationStr) \
  XJSON_SETTINGS_TAG("type_ts2",           GetTS2locationStr,                         SetTS2locationStr) \
  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("pgw_en",             GetPortGwEnabledStr,                       SetPortGwEnabledStr) \
  XJSON_SETTINGS_TAG("pgw_port",           GetPortGwPortnumStr,                       SetPortGwPortnumStr) \
  XJSON_SETTINGS_TAG("pgw_baud",           GetPortGwBaudStr,                          SetPortGwBaudStr) \
  XJSON_SETTINGS_TAG("pgw_par",            GetPortGwParityStr,                        SetPortGwParityStr) \
  XJSON_SETTINGS_TAG("pgw_ndata",          GetPortGwDatabitsStr,                      SetPortGwDatabitsStr) \
  XJSON_SETTINGS_TAG("pgw_nstop",          GetPortGwStopbitsStr,                      SetPortGwStopbitsStr) \
  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("mainvolt_high",      GetVACAlarmHighRangeStr,                   SetVACAlarmHighRangeStr) \
  XJSON_SETTINGS_TAG("mainvolt_low",       GetVACAlarmLowRangeStr,                    SetVACAlarmLowRangeStr) \
  XJSON_SETTINGS_TAG("mainvolt_hist",      GetVACAlarmHisteStr,                       SetVACAlarmHisteStr) \
  XJSON_SETTINGS_TAG("temp_high",          GetTemperatureAlarmHighRangeStr,           SetTemperatureAlarmHighRangeStr) \
  XJSON_SETTINGS_TAG("temp_low",           GetTemperatureAlarmLowRangeStr,            SetTemperatureAlarmLowRangeStr) \
  XJSON_SETTINGS_TAG("temp_hist",          GetTemperatureAlarmHisteStr,               SetTemperatureAlarmHisteStr) \
  XJSON_SETTINGS_TAG("case_temp_high",     GetTemperatureCabinetAlarmHighRangeStr,    SetTemperatureCabinetAlarmHighRangeStr) \
  XJSON_SETTINGS_TAG("case_temp_low",      GetTemperatureCabinetAlarmLowRangeStr,     SetTemperatureCabinetAlarmLowRangeStr) \
  XJSON_SETTINGS_TAG("case_temp_hist",     GetTemperatureCabinetAlarmHisteStr,        SetTemperatureCabinetAlarmHisteStr) \
  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("case_temp",          GetTempCaseStr) \
  XJSON_PARAMS_TAG("di1",                GetDIN0StatusStr) \
  XJSON_PARAMS_TAG("di_name1",           GetDIN0NameStr) \
  XJSON_PARAMS_TAG("di2",                GetDIN1StatusStr) \
  XJSON_PARAMS_TAG("di_name2",           GetDIN1NameStr) \
  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,                          5,      true,      GetDIN0StatusStr) \
  XTRAP(DI0_ALARM,                         5,      true,      GetDIN0StatusStr) \
  XTRAP(DI1_NORM,                          6,      true,      GetDIN1StatusStr) \
  XTRAP(DI1_ALARM,                         6,      true,      GetDIN1StatusStr) \
  XTRAP(BATTERY_HIGH_TEMPERATURE_NORM,     10,     true,      GetInternalTempStr) \
  XTRAP(BATTERY_HIGH_TEMPERATURE_ALARM,    10,     true,      GetInternalTempStr) \
  XTRAP(BATTERY_LOW_TEMPERATURE_NORM,      10,     true,      GetInternalTempStr) \
  XTRAP(BATTERY_LOW_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(CABINET_HIGH_TEMPERATURE_ALARM,    9,      true,      GetTempCaseStr) \
  XTRAP(CABINET_HIGH_TEMPERATURE_NORM,     9,      true,      GetTempCaseStr) \
  XTRAP(CABINET_LOW_TEMPERATURE_ALARM,     9,      true,      GetTempCaseStr) \
  XTRAP(CABINET_LOW_TEMPERATURE_NORM,      9,      true,      GetTempCaseStr) \
  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) \

typedef enum
{
  FW_VERSION_UPDATE = 1,
  FW_VERSION_UPDATED,
  DEVICE_RESTORED,
  DEVICE_REBOOTED,
  DI0_NORM,
  DI0_ALARM,
  DI1_NORM,
  DI1_ALARM,
  BATTERY_HIGH_TEMPERATURE_NORM,
  BATTERY_HIGH_TEMPERATURE_ALARM,
  BATTERY_LOW_TEMPERATURE_NORM,
  BATTERY_LOW_TEMPERATURE_ALARM,
  LINE_ALARM,
  LINE_NORM,
  LOW_BAT_ALARM,
  LOW_BAT_NORM,
  CABINET_HIGH_TEMPERATURE_ALARM,
  CABINET_HIGH_TEMPERATURE_NORM,
  CABINET_LOW_TEMPERATURE_ALARM,
  CABINET_LOW_TEMPERATURE_NORM,
  CONNECT_MONITOR_ALARM,
  CONNECT_MONITOR_NORM,
  BATTERY_CONNECT_ALARM,
  BATTERY_CONNECT_NORM,
  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_ALARM_DI1,
	LOG_ALARM_VAC_LOW_OUTPUT,
	LOG_ALARM_VAC_HIGH_OUTPUT,
	LOG_ALARM_HIGH_TEMP,
	LOG_ALARM_LOW_TEMP,
  LOG_ALARM_HIGH_CABINET_TEMP,
	LOG_ALARM_LOW_CABINET_TEMP,
	LOG_ALARM_LINE,
	LOG_ALARM_LOW_BAT,
	LOG_ALARM_UPS,
	LOG_ALARM_AKB,
  LOG_ALARM_SENSOR_AKB,
  LOG_ALARM_SENSOR_CABINET,
	LOG_VALUE,
	LOG_NONE,
} log_type_t;

#define TEST_AKB_FINISH_MONITOR
#define LINE_FAIL_MONITOR
#define VAC_OUT_MONITOR
#define LOW_BAT_MONITOR
#define SENSOR_TEMP_MONITOR
#define TEMP_AKB_MONITOR
#define TEMP_CABINET_MONITOR
#define BAT_CONNECT_MONITOR
#define UPS_CONNECT_MONITOR
#define DIN_MONITOR

#define MONITOR_TABLE \
  XMONITOR(UPS_TestFinishMonitor,               UPS.Present) \
  XMONITOR(UPS_LineFailMonitor,                 UPS.Present) \
  XMONITOR(UPS_VACoutputLowRangeMonitor,        UPS.Present) \
  XMONITOR(UPS_VACoutputHighRangeMonitor,       UPS.Present) \
  XMONITOR(UPS_LowBatMonitor,                   UPS.Present) \
  XMONITOR(sensorTemperatureMonitor,            1) \
  XMONITOR(UPS_TemperatureHighRangeMonitor,     (UPS.Present || sSettings.sTempControl[0].type_sensor == TS_AKB || sSettings.sTempControl[1].type_sensor == TS_AKB)) \
  XMONITOR(UPS_TemperatureLowRangeMonitor,      (UPS.Present || sSettings.sTempControl[0].type_sensor == TS_AKB || sSettings.sTempControl[1].type_sensor == TS_AKB)) \
  XMONITOR(Cabinet_TemperatureHighRangeMonitor, 1) \
  XMONITOR(Cabinet_TemperatureLowRangeMonitor,  1) \
  XMONITOR(UPS_BatteryConnectMonitor,           UPS.Present) \
  XMONITOR(UPS_ConnectMonitor,                  1) \
  XMONITOR(UPS_DI0Monitor,                      1) \

#define T_SENSOR_TABLE \
  XTSENSOR(T_SENSOR_1,     WDATA1,      DALLAS_SENSOR_1_USART) \
  XTSENSOR(T_SENSOR_2,     WDATA2,      DALLAS_SENSOR_2_USART) \

typedef enum
{
  T_SENSOR_1 = 0,
  T_SENSOR_2,
  MAX_T_SENSORS
} t_sensor_list_t;

#define LED_INIT_OK               LED_INIT_G
#define LED_INIT_ERR              LED_INIT_R
#define LED_ALARM                 LED_STAT_R

#define SERVICE_CONTROLLER_IP  "192.168.0.10"


#endif /* BOARD_PRS_H */
