/*
 * private_mib_bt6706.c
 *
 *  Created on: 17.11.2017
 *      Author: balbekova
 */


#ifdef HARDWARE_BT6706

#include "stm32f4xx.h"
#include "private_mib.h"
#include "parameters.h"
#include "settings_api.h"
#include "log.h"
#include "megatec.h"

#include "FreeRTOS.h"
#include "task.h"

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

#include "lwip/apps/snmp_scalar.h"
#include "lwip/apps/snmp.h"
//#include "snmp_core.h"

extern struct snmp_mib mib2;

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


static s16_t signal_get_value(struct snmp_node_instance* instance, void* value);
static snmp_err_t signal_set_value(struct snmp_node_instance* instance, u16_t len, void* value);
static snmp_err_t signal_set_test(struct snmp_node_instance* instance, u16_t len, void* value);


/* signal .1.3.6.1.4.1.41752.911.4.1.19  */
static const struct snmp_scalar_node signal19 = SNMP_SCALAR_CREATE_NODE_READONLY(19, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.18  */
static const struct snmp_scalar_node signal18 = SNMP_SCALAR_CREATE_NODE_READONLY(18, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.17  */
static const struct snmp_scalar_node signal17 = SNMP_SCALAR_CREATE_NODE_READONLY(17, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.16  */
static const struct snmp_scalar_node signal16 = SNMP_SCALAR_CREATE_NODE_READONLY(16, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.15  */
static const struct snmp_scalar_node signal15 = SNMP_SCALAR_CREATE_NODE_READONLY(15, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.14  */
static const struct snmp_scalar_node signal14 = SNMP_SCALAR_CREATE_NODE_READONLY(14, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.13  */
static const struct snmp_scalar_node signal13 = SNMP_SCALAR_CREATE_NODE_READONLY(13, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.12  */
static const struct snmp_scalar_node signal12 = SNMP_SCALAR_CREATE_NODE_READONLY(12, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.11  */
static const struct snmp_scalar_node signal11 = SNMP_SCALAR_CREATE_NODE_READONLY(11, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.9  */
static const struct snmp_scalar_node signal10 = SNMP_SCALAR_CREATE_NODE_READONLY(10, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.9  */
static const struct snmp_scalar_node signal9 = SNMP_SCALAR_CREATE_NODE(9, SNMP_NODE_INSTANCE_WRITE_ONLY, SNMP_ASN1_TYPE_OCTET_STRING, NULL, signal_set_test, signal_set_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.8  */
static const struct snmp_scalar_node signal8 = SNMP_SCALAR_CREATE_NODE(8, SNMP_NODE_INSTANCE_WRITE_ONLY, SNMP_ASN1_TYPE_INTEGER, NULL, signal_set_test, signal_set_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.7  */
static const struct snmp_scalar_node signal7 = SNMP_SCALAR_CREATE_NODE(7, SNMP_NODE_INSTANCE_READ_WRITE, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value, signal_set_test, signal_set_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.6  */
static const struct snmp_scalar_node signal6 = SNMP_SCALAR_CREATE_NODE(6, SNMP_NODE_INSTANCE_READ_WRITE, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value, signal_set_test, signal_set_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.5  */
static const struct snmp_scalar_node signal5 = SNMP_SCALAR_CREATE_NODE(5, SNMP_NODE_INSTANCE_READ_WRITE, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value, signal_set_test, signal_set_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.4  */
static const struct snmp_scalar_node signal4 = SNMP_SCALAR_CREATE_NODE_READONLY(4, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.3  */
static const struct snmp_scalar_node signal3 = SNMP_SCALAR_CREATE_NODE(3, SNMP_NODE_INSTANCE_WRITE_ONLY, SNMP_ASN1_TYPE_INTEGER, NULL, signal_set_test, signal_set_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.2  */
static const struct snmp_scalar_node signal2 = SNMP_SCALAR_CREATE_NODE(2, SNMP_NODE_INSTANCE_WRITE_ONLY, SNMP_ASN1_TYPE_INTEGER, NULL, signal_set_test, signal_set_value);

/* signal .1.3.6.1.4.1.41752.911.4.1.1  */
static const struct snmp_scalar_node signal1 = SNMP_SCALAR_CREATE_NODE_READONLY(1, SNMP_ASN1_TYPE_OCTET_STRING, signal_get_value);


// signals .1.3.6.1.4.1.41752.911.4.1
static const struct snmp_node* const signals_nodes[] = {
	&signal1.node.node,
	&signal2.node.node,
	&signal3.node.node,
	&signal4.node.node,
	&signal5.node.node,
	&signal6.node.node,
	&signal7.node.node,
	&signal8.node.node,
	&signal9.node.node,
	&signal10.node.node,
	&signal11.node.node,
	&signal12.node.node,
	&signal13.node.node,
	&signal14.node.node,
	&signal15.node.node,
	&signal16.node.node,
	&signal17.node.node,
	&signal18.node.node,
	&signal19.node.node
};
static const struct snmp_tree_node signals_node = SNMP_CREATE_TREE_NODE(1, signals_nodes);

// bt_6702 .1.3.6.1.4.1.41752.911.4
static const struct snmp_node* const bt6706_nodes[] = {
  &signals_node.node
};
static const struct snmp_tree_node bt6706_node = SNMP_CREATE_TREE_NODE(4, bt6706_nodes);

// swt .1.3.6.1.4.1.41752.911
static const struct snmp_node* const swt_nodes[] = {
  &bt6706_node.node
};
static const struct snmp_tree_node swt_node = SNMP_CREATE_TREE_NODE(911, swt_nodes);

// rotek .1.3.6.1.4.1.41752
static const struct snmp_node* const rotek_nodes[] = {
  &swt_node.node
};
static const struct snmp_tree_node rotek_node = SNMP_CREATE_TREE_NODE(41752, rotek_nodes);

// enterprises .1.3.6.1.4.1
static const struct snmp_node* const enterprises_nodes[] = {
  &rotek_node.node
};
static const struct snmp_tree_node enterprises_node = SNMP_CREATE_TREE_NODE(1, enterprises_nodes);

// private .1.3.6.1.4
static const struct snmp_node* const private_nodes[] = {
  &enterprises_node.node
};
static const struct snmp_tree_node private_root = SNMP_CREATE_TREE_NODE(0, private_nodes);

static const u32_t prvmib_base_oid[] = { 1,3,6,1,4 };
static const struct snmp_mib private_mib = SNMP_MIB_CREATE(prvmib_base_oid, &private_root.node);




void lwip_privmib_init(void)
{
    static const struct snmp_mib *my_snmp_mibs[] = {&mib2, &private_mib};

    snmp_set_mibs(my_snmp_mibs, LWIP_ARRAYSIZE(my_snmp_mibs));
}



static s16_t signal_get_value(struct snmp_node_instance* instance, void* value)
{
	u8_t *paramStr = (u8_t*)value;
	u8_t paramLength = 0;
    u32_t oid = instance->node->oid; // id сигнала

    switch (oid){
	case 1: /* FWVersion */
		GetVersionStr((char*)paramStr, &paramLength);
		break;
	case 4: /* UPSModel*/
		GetUPSModelStr((char*)paramStr, &paramLength);
		break;
	case 5: /* DO1*/
		GetDOUTStatusStr((char*)paramStr, &paramLength, 0);
		break;
	case 6: /* DO2*/
		GetDOUTStatusStr((char*)paramStr, &paramLength, 1);
		break;
	case 7: /* DO3*/
		GetDOUTStatusStr((char*)paramStr, &paramLength, 2);
		break;
	case 10: /* DIO*/
		GetDINStatusStr((char*)paramStr, &paramLength, 0);
		break;
	case 11: /* IntTemp */
		GetInternalTempStr((char*)paramStr, &paramLength);
		break;
	case 12: /* InFreq */
		GetInputFreqStr((char*)paramStr, &paramLength);
		break;
	case 13: /* InVoltVAC */
		GetInputVoltageStr((char*)paramStr, &paramLength);
		break;
	case 14: /* OutVoltVAC */
		GetOutputVoltageStr((char*)paramStr, &paramLength);
		break;
	case 15: /* Power */
		GetPowerStr((char*)paramStr, &paramLength);
		break;
	case 16: /* BatCap */
		GetBatCapacityStr((char*)paramStr, &paramLength);
		break;
	case 17: /* BatTime */
		GetRuntimeStr((char*)paramStr, &paramLength);
		break;
	case 18: /* ConnectMonitor */
		GetConnectMonitorStr((char*)paramStr, &paramLength);
		break;
	case 19: /* Alarms */
		GetAlarmStr((char*)paramStr, &paramLength);
		break;
	default:
	break;
    }

    return paramLength;

}

static snmp_err_t signal_set_value(struct snmp_node_instance* instance, u16_t len, void* value)
{

	u32_t oid = instance->node->oid; // id сигнала
	char *val_string;
	char str[20];
	int8_t res = 0;
	s32_t val;

     memset(str, 0, 20);

    switch (oid)
    {
      case 2: /* RestoreSignal */
        val = *(s32_t*)value;
        if (val == 1) {
      //	SNMP_SendUserTrap(DEVICE_RESTORED);
  		log_event_data(LOG_SYSTEM_DEFCONFIG, "Администратор");
  		vTaskDelay(500);
  		SETTINGS_SetPartDefault();
  		SETTINGS_Save();
        }
      break;
      case 3: /* RebootSignal */
        val = *(s32_t*)value;
        if (val == 1){
#ifndef BT6702_SERVICE
  	//	SNMP_SendUserTrap(DEVICE_REBOOTED);
  		log_event_data(LOG_SYSTEM_BOOT, "Администратор");

  		vTaskDelay(1010);
  		LOG_Disable();
#endif
  		NVIC_SystemReset();
        }
      break;
  	case 5: /* DO1 */
  	  if(sSettings.sInOuts.ro_type_source[0] == SNMP_SET){
  		  val_string = (char*)value;
  		  SetROStr(val_string, 0);
  		  if(val_string[0] == 0x31)
  			  strcat(str, "Замкнуто");
  		  else
  			  strcat(str, "Разомкнуто");
  		//  SNMP_SendUserTrap(DO0_TOGGLED);
  		  log_event_data(LOG_DO0_STATE, str);
  	  }
  	  break;
  	case 6: /* DO2 */
  	  if(sSettings.sInOuts.ro_type_source[1] == SNMP_SET){
  		  val_string = (char*)value;
  		  SetROStr(val_string, 1);
  		  if(val_string[0] == 0x31)
  			  strcat(str, "Замкнуто");
  		  else
  			  strcat(str, "Разомкнуто");
  		//  SNMP_SendUserTrap(DO1_TOGGLED);
  		  log_event_data(LOG_DO1_STATE, str);
  	  }
  	  break;
	case 7: /* DO2*/
		if(sSettings.sInOuts.ro_type_source[2] == SNMP_SET){
			  val_string = (char*)value;
			  SetROStr(val_string, 2);
			  if(val_string[0] == 0x31)
				  strcat(str, "Замкнуто");
			  else
				  strcat(str, "Разомкнуто");
			//  SNMP_SendUserTrap(DO1_TOGGLED);
			  log_event_data(LOG_DO2_STATE, str);
		  }
		break;
  	case 8: /* BatTest */
  	  val =*(s32_t*)value;
  	  if(val == 0){
  		  res = ups_metac_service_pdu(ups_cancel_test);
  		  if(res == 1){
  			  log_event_data(LOG_TEST_UPS, "Администратор (Останов)");
  		  }
  	  }
  	  else if(val > 0 && val < 100){
  		  TimeParam = val;
  		  res = ups_metac_service_pdu(ups_test_time);
  		  if(res == 1){
  			  log_event_data(LOG_TEST_UPS, "Администратор (Запущен)");
  		  }
  	  }
  	  else if(val == 100){
  		  res = ups_metac_service_pdu(ups_test_10sec);
  		  if(res == 1){
  			  log_event_data(LOG_TEST_UPS, "Администратор (Запущен)");
  		  }
  	  }
  	  else if(val == 999){
  		  res = ups_metac_service_pdu(ups_test_low_bat);
  		  if(res == 1){
  			  log_event_data(LOG_TEST_UPS, "Администратор (Запущен)");
  		  }
  	  }
  	  break;
  	case 9: /* Shutdown UPS */
  	  val_string = (char*)value;
  	  float shtdn_val = atof(val_string);
  	  if(shtdn_val == 0){
  		  res = ups_metac_service_pdu(ups_cancel_shut_down);
  		 	if(res == 1){
  		 		log_event_data(LOG_SHUTDOWN_UPS, "Администратор (Останов)");
  		 	}
  	  }
  	  else{
  		TimeParamFloat = shtdn_val;
  		res = ups_metac_service_pdu(ups_shutdown);
  		if(res == 1)
  			log_event_data(LOG_SHUTDOWN_UPS, "Администратор");
  	  }
  	  break;
      default :
    	  return SNMP_ERR_GENERROR;
      break;
    };

  return SNMP_ERR_NOERROR;
}

static snmp_err_t signal_set_test(struct snmp_node_instance* instance, u16_t len, void* value)
{
	snmp_err_t ret = SNMP_ERR_WRONGVALUE;
    char *val_string;
    int32_t val;
    u32_t oid = instance->node->oid; // id сигнала

    switch (oid)
    {
      case 2:
        if ( len == sizeof(s32_t) )
        {
        	ret = SNMP_ERR_NOERROR;
        }
        break;

      case 3:
        if ( len == sizeof(s32_t) )
        {
        	ret = SNMP_ERR_NOERROR;
        }
        break;
  	case 5: /* DO1 */
  		if ( len <= 1 )
  	    {
  		   val_string = (char*)value;
  		   val_string[len] = 0;
  		   if(atoi(val_string) <= 1){
  			   if(sSettings.sInOuts.ro_type_source[0] == SNMP_SET)
  				 ret = SNMP_ERR_NOERROR;
  		   }
  	    }
  	  break;
  	case 6: /* DO2 */
  		if ( len <= 1 )
  		{
  		   val_string = (char*)value;
  		   val_string[len] = 0;
  		   if(atoi(val_string) <= 1){
  			   if(sSettings.sInOuts.ro_type_source[1] == SNMP_SET)
  				 ret = SNMP_ERR_NOERROR;
  		   }
  		}
  	  break;
  	case 7: /* DO2*/
		if ( len <= 1 )
		{
		   val_string = (char*)value;
		   val_string[len] = 0;
		   if(atoi(val_string) <= 1){
			   if(sSettings.sInOuts.ro_type_source[2] == SNMP_SET)
				 ret = SNMP_ERR_NOERROR;
		   }
		}
		break;
  	case 8: /* BatTest */
  	   val = *((s32_t*)value);
  	   if((val >= 0 && val <= 100) || val == 999){
  		 ret = SNMP_ERR_NOERROR;
  	   }
  	  break;
  	case 9: /* Shutdown UPS */
  		if ( len <= 3 )
  		{
  		   val_string = (char*)value;
  		   val_string[len] = 0;
  		   if(atof(val_string) <= 10){
  			 ret = SNMP_ERR_NOERROR;
  		   }
  		}
  	  break;
    };


  return ret;
}

#endif

