#ifndef BOARD_BT6711_H
#define BOARD_BT6711_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 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)  \
LEDS(X)             \
JUMPERS(X) \
BUTTONS(X)      \
WDG_PIN(X)

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

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

/**
  * brief  Текст сообщения при запуске CLI
  */
#define TELNET_CLI_WELCOME_MESSAGE      "BT-6711 command server - connection accepted.\r\nlogin:"

#define RTC_ENABLE                  //RTC clock
#define LED_ENABLE                  //LED индикация
#define BUTTON_ENABLE               //Кнопки
#define NET_ENABLE                  //Ethernet сеть
#define HTTP_SERVER_ENABLE          //HTTP сервер
#define SNMP_ENABLE                 //SNMP протокол
#define NOTIFICATION_CONTROL_ENABLE //Управление уведомлениями
#define WHITELIST_ENABLE            //Белый список
#define TELNET_ENABLE               //Telnet сервер
#define UPS_ENABLE                  //UPS
#ifndef BT6702_SERVICE // no web server in the service fw
#define FTP_ENABLE                  //FTP boot loader
#endif // BT6702_SERVICE
#define SYSLOG_ENABLE
//#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( UPS_Setting_t, UPS_Setting, SETTINGS_SetUPSSettingsDef, ALL_DEFAULT ) \
XSETTING( ALARM_MNGR_t, sAlarmManager, SETTINGS_SetAlarmManagerDef, ALL_DEFAULT ) \
XSETTING( TELNET_t, sTelnet, SETTINGS_SetTelnetDef, PART_DEFAULT_1 ) \
XSETTING( WHITE_LIST_t, sWhiteList[MAX_WHITE_LIST], SETTINGS_SetWhiteListDef, ALL_DEFAULT ) \
XSETTING( WHITE_LIST_t, sWhiteListTemp[MAX_WHITE_LIST], SETTINGS_SetWhiteListDef, ALL_DEFAULT ) \
XSETTING( uint8_t, sFlagNotification[ALL_TRAPS], SETTINGS_SetFlagNotificationDef, ALL_DEFAULT ) \
XSETTING( FTP_Update_t,	sFTPUpdate, SETTINGS_SetFTPUpdateDef, ALL_DEFAULT ) \
XSETTING( Syslog_Settings_t,	sSyslog, SETTINGS_SetSyslogDef, ALL_DEFAULT ) \

#define SNMP_DEV_ROOT_OID       11

#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("ipaddr",             GetIpStr,                            SetIPStr) \
  XJSON_SETTINGS_TAG("gw",                 GetGatewayStr,                       SetGatewayStr) \
  XJSON_SETTINGS_TAG("mask",               GetMaskStr,                          SetMaskStr) \
  XJSON_SETTINGS_TAG("dhcp",               GetDhcpStateStr,                     SetDhcpStateStr) \
  XJSON_SETTINGS_TAG("tn_port",            GetTelnetPortStr,                    SetTelnetPortStr) \
  XJSON_SETTINGS_TAG("tn_enabled",         GetTelnetEnableStateStr,             SetTelnetEnableStateStr) \
  XJSON_SETTINGS_TAG("wtl_ip1",            GetWhiteList1STR,                    SetWhiteList1STR) \
  XJSON_SETTINGS_TAG("wtl_ip2",            GetWhiteList2STR,                    SetWhiteList2STR) \
  XJSON_SETTINGS_TAG("wtl_ip3",            GetWhiteList3STR,                    SetWhiteList3STR) \
  XJSON_SETTINGS_TAG("wtl_ip4",            GetWhiteList4STR,                    SetWhiteList4STR) \
  XJSON_SETTINGS_TAG("wtl_ip5",            GetWhiteList5STR,                    SetWhiteList5STR) \
  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("bat_cap",            GetCapacityNominalAKBStr,            SetCapacityNominalAKBStr) \
  XJSON_SETTINGS_TAG("ups_max_power",      GetUPSPowerStr,                      SetUPSPowerStr) \
  XJSON_SETTINGS_TAG("bat_charge",         GetVoltageAKBNominalStr,             SetVoltageAKBNominalStr) \
  XJSON_SETTINGS_TAG("mainvolt_high",      GetVACAlarmHighRangeStr,             SetVACAlarmHighRangeStr) \
  XJSON_SETTINGS_TAG("mainvolt_low",       GetVACAlarmLowRangeStr,              SetVACAlarmLowRangeStr) \
  XJSON_SETTINGS_TAG("mainvolt_hist",      GetVACAlarmHisteStr,                 SetVACAlarmHisteStr) \
  XJSON_SETTINGS_TAG("mainvolt_in_high",   GetVACInputAlarmHighRangeStr,        SetVACInputAlarmHighRangeStr) \
  XJSON_SETTINGS_TAG("mainvolt_in_low",    GetVACInputAlarmLowRangeStr,         SetVACInputAlarmLowRangeStr) \
  XJSON_SETTINGS_TAG("mainvolt_in_hist",   GetVACinputAlarmHisteStr,            SetVACinputAlarmHisteStr) \
  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) \
  XJSON_SETTINGS_TAG("ftp_server",         GetFTPServer,                        SetEmptyFuncStr) \
  XJSON_SETTINGS_TAG("ftp_port",           GetFTPPort,                          SetEmptyFuncStr) \
  XJSON_SETTINGS_TAG("ftp_path",           GetFTPPath,                          SetEmptyFuncStr) \
  XJSON_SETTINGS_TAG("ftp_login",          GetFTPLogin,                         SetEmptyFuncStr) \
  XJSON_SETTINGS_TAG("ftp_password",       GetFTPPassword,                      SetEmptyFuncStr) \
  XJSON_SETTINGS_TAG("syslog_enable",      GetSyslogEnable,                     SetSyslogEnable) \
  XJSON_SETTINGS_TAG("syslog_server",      GetSyslogServer,                     SetSyslogServer) \
  XJSON_SETTINGS_TAG("syslog_port",        GetSyslogPort,                       SetSyslogPort) \

#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("in_cur",             GetInputCurrentStr) \
  XJSON_PARAMS_TAG("out_cur",            GetOutputCurrentStr) \
  XJSON_PARAMS_TAG("bat_volt",           GetVoltageAKBtStr) \
  XJSON_PARAMS_TAG("bat_cap",            GetCapacityNominalAKBStr) \
  XJSON_PARAMS_TAG("pmode",              GetModeStr) \
  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(UPS_MODE,                          1,      true,      GetModeStr) \
  XTRAP(BATTERY_HIGH_TEMPERATURE_NORM,     9,      true,      GetInternalTempStr) \
  XTRAP(BATTERY_HIGH_TEMPERATURE_ALARM,    9,      true,      GetInternalTempStr) \
  XTRAP(BATTERY_LOW_TEMPERATURE_NORM,      9,      true,      GetInternalTempStr) \
  XTRAP(BATTERY_LOW_TEMPERATURE_ALARM,     9,      true,      GetInternalTempStr) \
  XTRAP(LINE_ALARM,                        11,     true,      GetInputVoltageStr) \
  XTRAP(LINE_NORM,                         11,     true,      GetInputVoltageStr) \
  XTRAP(VAC_LOW_OUTPUT_ALARM,              12,     true,      GetOutputVoltageStr) \
  XTRAP(VAC_LOW_OUTPUT_NORM,               12,     true,      GetOutputVoltageStr) \
  XTRAP(VAC_HIGH_OUTPUT_ALARM,             12,     true,      GetOutputVoltageStr) \
  XTRAP(VAC_HIGH_OUTPUT_NORM,              12,     true,      GetOutputVoltageStr) \
  XTRAP(LOW_BAT_ALARM,                     14,     true,      GetBatCapacityStr) \
  XTRAP(LOW_BAT_NORM,                      14,     true,      GetBatCapacityStr) \
  XTRAP(BATTERY_CONNECT_ALARM,             14,     true,      GetBatCapacityStr) \
  XTRAP(BATTERY_CONNECT_NORM,              14,     true,      GetBatCapacityStr) \
  XTRAP(POWER_ALARM,                       13,     true,      GetPowerStr) \
  XTRAP(POWER_NORM,                        13,     true,      GetPowerStr) \
  XTRAP(CONNECT_MONITOR_ALARM,             16,     true,      GetConnectMonitorStr) \
  XTRAP(CONNECT_MONITOR_NORM,              16,     true,      GetConnectMonitorStr) \

typedef enum
{
  FW_VERSION_UPDATE = 1,
  FW_VERSION_UPDATED,
  DEVICE_RESTORED,
  DEVICE_REBOOTED,
  UPS_MODE,
  BATTERY_HIGH_TEMPERATURE_NORM,
  BATTERY_HIGH_TEMPERATURE_ALARM,
  BATTERY_LOW_TEMPERATURE_NORM,
  BATTERY_LOW_TEMPERATURE_ALARM,
  LINE_ALARM,
  LINE_NORM,
  VAC_LOW_OUTPUT_ALARM,
  VAC_LOW_OUTPUT_NORM,
  VAC_HIGH_OUTPUT_ALARM,
  VAC_HIGH_OUTPUT_NORM,
#if 0
  TEST_BAT_RUN,
  TEST_BAT_STOP,
#endif
  LOW_BAT_ALARM,
  LOW_BAT_NORM,
  BATTERY_CONNECT_ALARM,
  BATTERY_CONNECT_NORM,
  POWER_ALARM,
  POWER_NORM,
  CONNECT_MONITOR_ALARM,
  CONNECT_MONITOR_NORM,
  ALL_TRAPS
} TRAP_LIST_t;

typedef enum {
	LOG_SYSTEM_BOOT = 0,			// device booted
	LOG_SYSTEM_DEFCONFIG,	// default config applied
	LOG_UPDATE_SOFT,
	LOG_TURN_ON,		//
	LOG_PSW_CHANGE,		//
	LOG_SETTING_SAVE,		//
	LOG_LOGIN_TELNET,		//
	LOG_LOGIN,		//
  LOG_UPS_MODE,
	LOG_TEST_UPS,		//
	LOG_SHUTDOWN_UPS,
	LOG_ALARM_VAC_LOW_OUTPUT,
	LOG_ALARM_VAC_HIGH_OUTPUT,
	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_TEST_ALARM_AKB,
	LOG_VALUE,
	LOG_NONE,
} log_type_t;

#define UPS_MODE_MONITOR
#define TEST_AKB_FINISH_MONITOR
#define VAC_IN_MONITOR
#define LINE_FAIL_MONITOR
#define VAC_OUT_MONITOR
#define LOW_BAT_MONITOR
#define LOAD_MONITOR
#define TEMP_AKB_MONITOR
#define BAT_CONNECT_MONITOR
#define UPS_CONNECT_MONITOR
#define TEST_ALARM_AKB_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(UPS_PowerMonitor,                    UPS.Present) \
  XMONITOR(UPS_TemperatureHighRangeMonitor,     UPS.Present) \
  XMONITOR(UPS_TemperatureLowRangeMonitor,      UPS.Present) \
  XMONITOR(UPS_BatteryConnectMonitor,           UPS.Present) \
  XMONITOR(UPS_ConnectMonitor,                  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 SERVICE_CONTROLLER_IP  "192.168.0.12"

#endif /* BOARD_PRS_H */
