/********************************* (C) ROTEK ***********************************
 * @module  trap_api
 * @file    trap_api.c
 * @version 1.0.0
 * @date    XX.XX.XXXX
 * $brief   trap_api
 *******************************************************************************
 * @history     Version  Author         Comment
 * XX.XX.XXXX   1.0.0    Telenkov D.A.  First release.
 *******************************************************************************
 */

#include "stm32f4xx.h"  
#include "trap_api.h"
#include "parameters.h"
#include "settings_api.h"

#include "lwip/snmp.h"
#include "lwip/snmp_msg.h"
#include "lwip/snmp_asn1.h"
#include "lwip/snmp_structs.h"

#ifdef PRINTF_STDLIB
#include <stdio.h>
#endif
#ifdef PRINTF_CUSTOM
#include "tinystdio.h"
#endif
#include <string.h>
#include <stdbool.h>

/**
  * @brief  Общая структура настроек
  */
extern SETTINGS_t sSettings;

/**
  * @brief  Пул всех возможных трапов устройства
  */
TRAP_t traps[21];

/**
  * @brief  Инициализация базы трапов
  * @retval 
  */
void SNMP_InitTrapsBase(void)
{
  /* 1. FWVersionUpdate */ // +
  traps[FW_VERSION_UPDATE].trapId = FW_VERSION_UPDATE;
  traps[FW_VERSION_UPDATE].varbindId = 1;
  traps[FW_VERSION_UPDATE].trapEnable = true;
  traps[FW_VERSION_UPDATE].handle = GetVersionStr; /* FWVersion */
  
  /* 2. FWVersionUpdated */ // +
  traps[FW_VERSION_UPDATED].trapId = FW_VERSION_UPDATED;
  traps[FW_VERSION_UPDATED].varbindId = 1;
  traps[FW_VERSION_UPDATED].trapEnable = true;
  traps[FW_VERSION_UPDATED].handle = GetVersionStr; /* FWVersion */
  
  /* 3. DeviceRestored */ // +
  traps[DEVICE_RESTORED].trapId = DEVICE_RESTORED;
  traps[DEVICE_RESTORED].varbindId = 1;
  traps[DEVICE_RESTORED].trapEnable = true;
  traps[DEVICE_RESTORED].handle = GetVersionStr;//GetRestore; /* RestoreSignal */
  
  /* 4. DeviceRebooted */ // +
  traps[DEVICE_REBOOTED].trapId = DEVICE_REBOOTED;
  traps[DEVICE_REBOOTED].varbindId = 1;
  traps[DEVICE_REBOOTED].trapEnable = true;
  traps[DEVICE_REBOOTED].handle = GetVersionStr;//GetReboot; /* RebootSignal */
  
  /* 5. DI0Norm */ // +
   traps[DI0_NORM].trapId = DI0_NORM;
   traps[DI0_NORM].varbindId = 9;
   traps[DI0_NORM].trapEnable = true;
   traps[DI0_NORM].handle = GetDIN0StatusStr; /* State DIN */

   /* 6. DI0Alarm */ // +
   traps[DI0_ALARM].trapId = DI0_ALARM;
   traps[DI0_ALARM].varbindId = 9;
   traps[DI0_ALARM].trapEnable = true;
   traps[DI0_ALARM].handle = GetDIN0StatusStr; /* State DIN */

   /* 7. DO0Toggle */ // +
    traps[DO0_TOGGLED].trapId = DO0_TOGGLED;
    traps[DO0_TOGGLED].varbindId = 5;
    traps[DO0_TOGGLED].trapEnable = true;
    traps[DO0_TOGGLED].handle = GetDOUT0StatusStr; /* State DO0 */

    /* 8. DO1Toggle */ // +
    traps[DO1_TOGGLED].trapId = DO1_TOGGLED;
    traps[DO1_TOGGLED].varbindId = 6;
    traps[DO1_TOGGLED].trapEnable = true;
    traps[DO1_TOGGLED].handle = GetDOUT1StatusStr; /* State DO1 */

  /* 9. BatteryTemperatureNorm */ // +
  traps[BATTERY_TEMPERATURE_NORM].trapId = BATTERY_TEMPERATURE_NORM;
  traps[BATTERY_TEMPERATURE_NORM].varbindId = 10;
  traps[BATTERY_TEMPERATURE_NORM].trapEnable = true;
  traps[BATTERY_TEMPERATURE_NORM].handle = GetInternalTempStr; /* BatteryTemperature */  
  
  /* 10. BatteryTemperatureAlarm */ // +
  traps[BATTERY_TEMPERATURE_ALARM].trapId = BATTERY_TEMPERATURE_ALARM;
  traps[BATTERY_TEMPERATURE_ALARM].varbindId = 10;
  traps[BATTERY_TEMPERATURE_ALARM].trapEnable = true;
  traps[BATTERY_TEMPERATURE_ALARM].handle = GetInternalTempStr; /* BatteryTemperature */
  
  /* 11. LineAlarm */ // +
  traps[LINE_ALARM].trapId = LINE_ALARM;
  traps[LINE_ALARM].varbindId = 12;
  traps[LINE_ALARM].trapEnable = true;
  traps[LINE_ALARM].handle = GetInputVoltageStr; /* LineAlarm */
  
  /* 12. LineNorm */ // +
  traps[LINE_NORM].trapId = LINE_NORM;
  traps[LINE_NORM].varbindId = 12;
  traps[LINE_NORM].trapEnable = true;
  traps[LINE_NORM].handle = GetInputVoltageStr; /* LineNorm */
  
  /* 13. LowBatAlarm */ // +
  traps[LOW_BAT_ALARM].trapId = LOW_BAT_ALARM;
  traps[LOW_BAT_ALARM].varbindId = 15;
  traps[LOW_BAT_ALARM].trapEnable = true;
  traps[LOW_BAT_ALARM].handle = GetBatCapacityStr; /* LowBatAlarm */
  
  /* 14. LowBatNorm */ // +
  traps[LOW_BAT_NORM].trapId = LOW_BAT_NORM;
  traps[LOW_BAT_NORM].varbindId = 15;
  traps[LOW_BAT_NORM].trapEnable = true;
  traps[LOW_BAT_NORM].handle = GetBatCapacityStr; /* LowBatNorm */
  
  /* 15. PowerAlarm */ // +
  traps[POWER_ALARM].trapId = POWER_ALARM;
  traps[POWER_ALARM].varbindId = 14;
  traps[POWER_ALARM].trapEnable = true;
  traps[POWER_ALARM].handle = GetPowerStr; /* PowerAlarm */
  
  /* 16. PowerNorm */ // +
  traps[POWER_NORM].trapId = POWER_NORM;
  traps[POWER_NORM].varbindId = 14;
  traps[POWER_NORM].trapEnable = true;
  traps[POWER_NORM].handle = GetPowerStr; /* PowerNorm */
  
  /* 17. ConnectMonitorAlarm */ //
  traps[CONNECT_MONITOR_ALARM].trapId = CONNECT_MONITOR_ALARM;
  traps[CONNECT_MONITOR_ALARM].varbindId = 17;
  traps[CONNECT_MONITOR_ALARM].trapEnable = true;
  traps[CONNECT_MONITOR_ALARM].handle = GetConnectMonitorStr;//GetConnectMonitorStr; /* ConnectMonitor */
  
  /* 18. ConnectMonitorNorm */ //
  traps[CONNECT_MONITOR_NORM].trapId = CONNECT_MONITOR_NORM;
  traps[CONNECT_MONITOR_NORM].varbindId = 17;
  traps[CONNECT_MONITOR_NORM].trapEnable = true;
  traps[CONNECT_MONITOR_NORM].handle = GetConnectMonitorStr;//GetConnectMonitorStr; /* ConnectMonitor */

  /* 19. BatteryConnectAlarm */ // +
  traps[BATTERY_CONNECT_ALARM].trapId = BATTERY_CONNECT_ALARM;
  traps[BATTERY_CONNECT_ALARM].varbindId = 15;
  traps[BATTERY_CONNECT_ALARM].trapEnable = true;
  traps[BATTERY_CONNECT_ALARM].handle = GetBatCapacityStr; /* LowBatAlarm */

  /* 20. BatteryConnectNorm */ // +
  traps[BATTERY_CONNECT_NORM].trapId = BATTERY_CONNECT_NORM;
  traps[BATTERY_CONNECT_NORM].varbindId = 15;
  traps[BATTERY_CONNECT_NORM].trapEnable = true;
  traps[BATTERY_CONNECT_NORM].handle = GetBatCapacityStr; /* LowBatNorm */
}

/**
  * @brief  Отправка обычного трапа с сообщением, зарегистрированного в базе.
  * @retval 
  */
bool SNMP_SendMessageTrap(TRAP_t trap, char* str, uint8_t len)
{
  const uint8_t maxMsgLen = 30;
  char msg[30];
  struct snmp_varbind *vb;
  struct snmp_obj_id trapObjId = {11, {1, 3, 6, 1, 4, 1, 41752, 911, 3, 2, 1}};

  if (len <= maxMsgLen)
  {
    trapObjId.id[trapObjId.len - 1] = trap.trapId;
	
	strcpy(msg, str);
	
    vb = snmp_varbind_alloc(&trapObjId, SNMP_ASN1_OPAQUE, strlen(msg));
	
	vb->value = msg;
	            
    trap_msg.outvb.head = vb;
    trap_msg.outvb.tail = vb;
    trap_msg.outvb.count = 1;
	
    snmp_send_trap(SNMP_GENTRAP_ENTERPRISESPC, &trapObjId, 0);
     
    trap_msg.outvb.head = NULL;
    trap_msg.outvb.tail = NULL;
    trap_msg.outvb.count = 0;
     
	snmp_varbind_list_free(&trap_msg.outvb);
    snmp_varbind_free(vb);  
	
	return true;
  }
  else
	return false;
}

/**
  * @brief  Отправка трапа с переменной, зарегистрированного в базе.
  * @retval 
  */
bool SNMP_SendVarbindTrap(TRAP_t *trap)
{
  char msg[255];
  uint8_t len = 0;
  struct snmp_varbind *vb;
  struct snmp_obj_id trapObjId = {11, {1, 3, 6, 1, 4, 1, 41752, 911, 3, 2, 1}};
  struct snmp_obj_id varObjId  = {11, {1, 3, 6, 1, 4, 1, 41752, 911, 3, 1, 1}};
  
  trapObjId.id[trapObjId.len - 1] = trap->trapId;
  varObjId.id[varObjId.len - 1] = trap->varbindId;
    
  trap->handle(msg, &len);
  vb = snmp_varbind_alloc(&varObjId, SNMP_ASN1_OC_STR, len);
    
  if(vb != NULL ) {
        memcpy(vb->value, msg, len);
  
	snmp_varbind_tail_add(&trap_msg.outvb, vb);
			
        snmp_send_trap(SNMP_GENTRAP_ENTERPRISESPC, &trapObjId, 0);
	  
        snmp_varbind_list_free(&trap_msg.outvb);
  
        return true;
  }
  else {
        return false;
  }
}  


/********************************* (C) ROTEK **********************************/
