/********************************* (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 "common_config.h"
#include "stm32f4xx.h"  
#include "trap_api.h"
#include "parameters.h"
#include "settings_api.h"

#include "snmp_trap_pdu2.h"

#include "lwip/apps/snmp.h"
#include "lwip/snmp.h"
#include "lwip/sys.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[ALL_TRAPS];

#define TRAP_ADD(trap_num, varbind, flag_enable, get_param) \
  do { \
    traps[trap_num].trapId = trap_num; \
    traps[trap_num].varbindId = varbind; \
    traps[trap_num].trapEnable = flag_enable; \
    traps[trap_num].handle = get_param; \
  } while (0);

/**
  * @brief  Инициализация базы трапов
  * @retval 
  */
void SNMP_InitTrapsBase(void)
{
#define XTRAP(trap_num, varbind, flag_enable, get_param) TRAP_ADD(trap_num, varbind, flag_enable, get_param);
    TRAPS_TABLE
#undef XTRAP
}

/**
  * @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;
*/  
}

struct snmp_varbind vb1, vb2, vb;
char msg[50];
uint32_t snmp_time = 0;

struct snmp_obj_id  mib2_base_oid_systime ={ 9, { 1, 3, 6, 1, 2, 1, 1, 3, 0 }};
struct snmp_obj_id  trap_oid = {11, {1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0}};
struct snmp_obj_id trapObjId = {11, {1, 3, 6, 1, 4, 1, 41752, 911, SNMP_DEV_ROOT_OID, 2, 1}};
struct snmp_obj_id varObjId  = {11, {1, 3, 6, 1, 4, 1, 41752, 911, SNMP_DEV_ROOT_OID, 1, 1}};

/**
  * @brief  Отправка трапа с переменной, зарегистрированного в базе.
  * @retval 
  */
bool SNMP_SendVarbindTrap(TRAP_t *trap)
{
  uint8_t len = 0;
  
  memset(&vb1, 0, sizeof(struct snmp_varbind));
  memset(&vb2, 0, sizeof(struct snmp_varbind));
  memset(&vb, 0, sizeof(struct snmp_varbind));

  trapObjId.id[trapObjId.len - 1] = trap->trapId;
  varObjId.id[varObjId.len - 1] = trap->varbindId;
  
  trap->handle(msg, &len);
  
  vb.oid = mib2_base_oid_systime;
  vb.type = SNMP_ASN1_TYPE_TIMETICKS;
  MIB2_COPY_SYSUPTIME_TO(&snmp_time);
  vb.value = &snmp_time;
  vb.value_len = sizeof(uint32_t);
  vb.next = &vb1;
  vb.prev = NULL;

  vb1.oid = trap_oid;
  vb1.type = SNMP_ASN1_TYPE_OBJECT_ID;
  vb1.value = trapObjId.id;
  vb1.value_len = 4*trapObjId.len;
  vb1.next = &vb2;
  vb1.prev = &vb;

  vb2.oid = varObjId;
  vb2.type = SNMP_ASN1_TYPE_OCTET_STRING;
  vb2.value = msg;
  vb2.value_len = len;
  vb2.next = NULL;
  vb2.prev = &vb1;
  
  snmp_send_trap_pduv2(&vb);
  //snmp_send_trap(&trapObjId, SNMP_GENTRAP_ENTERPRISE_SPECIFIC, 0, &vb);
  
  return true;
}  


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