/*
 * private_mib_bt6709.c
 *
 *  Created on: 03.10.2018
 *      Author: balbekova
 */


#if defined HARDWARE_BT6709

#include "stm32f4xx.h"
#include "private_mib.h"
#include "parameters.h"
#include "settings_api.h"
#include "log.h"
#include "megatec.h"
#include "control_symbol.h"
#include "sntp_api.h"
#include "web_params_api.h"
#include "hal.h"

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

#include <ctype.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"

#define MUL100          100
#define MUL10           10

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.10.1.49  */
static const struct snmp_scalar_node signal49 = SNMP_SCALAR_CREATE_NODE_READONLY(49, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.48  */
static const struct snmp_scalar_node signal48 = SNMP_SCALAR_CREATE_NODE_READONLY(48, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.47  */
static const struct snmp_scalar_node signal47 = SNMP_SCALAR_CREATE_NODE_READONLY(47, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.46  */
static const struct snmp_scalar_node signal46 = SNMP_SCALAR_CREATE_NODE_READONLY(46, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.45  */
static const struct snmp_scalar_node signal45 = SNMP_SCALAR_CREATE_NODE_READONLY(45, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.44  */
static const struct snmp_scalar_node signal44 = SNMP_SCALAR_CREATE_NODE_READONLY(44, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.43  */
static const struct snmp_scalar_node signal43 = SNMP_SCALAR_CREATE_NODE(43, 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.10.1.42  */
static const struct snmp_scalar_node signal42 = SNMP_SCALAR_CREATE_NODE(42, 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.10.1.41  */
static const struct snmp_scalar_node signal41 = SNMP_SCALAR_CREATE_NODE(41, 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.10.1.40  */
static const struct snmp_scalar_node signal40 = SNMP_SCALAR_CREATE_NODE(40, 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.10.1.39  */
static const struct snmp_scalar_node signal39 = SNMP_SCALAR_CREATE_NODE(39, 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.10.1.38  */
static const struct snmp_scalar_node signal38 = SNMP_SCALAR_CREATE_NODE(38, 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.10.1.37  */
static const struct snmp_scalar_node signal37 = SNMP_SCALAR_CREATE_NODE(37, 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.10.1.36  */
static const struct snmp_scalar_node signal36 = SNMP_SCALAR_CREATE_NODE(36, 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.10.1.35  */
static const struct snmp_scalar_node signal35 = SNMP_SCALAR_CREATE_NODE(35, 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.10.1.34  */
static const struct snmp_scalar_node signal34 = SNMP_SCALAR_CREATE_NODE(34, 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.10.1.33  */
static const struct snmp_scalar_node signal33 = SNMP_SCALAR_CREATE_NODE(33, 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.10.1.32  */
static const struct snmp_scalar_node signal32 = SNMP_SCALAR_CREATE_NODE(32, 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.10.1.31  */
static const struct snmp_scalar_node signal31 = SNMP_SCALAR_CREATE_NODE(31, 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.10.1.30  */
static const struct snmp_scalar_node signal30 = SNMP_SCALAR_CREATE_NODE(30, 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.10.1.29  */
static const struct snmp_scalar_node signal29 = SNMP_SCALAR_CREATE_NODE(29, 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.10.1.28  */
static const struct snmp_scalar_node signal28 = SNMP_SCALAR_CREATE_NODE(28, 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.10.1.27  */
static const struct snmp_scalar_node signal27 = SNMP_SCALAR_CREATE_NODE_READONLY(27, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.26  */
static const struct snmp_scalar_node signal26 = SNMP_SCALAR_CREATE_NODE_READONLY(26, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.25  */
static const struct snmp_scalar_node signal25 = SNMP_SCALAR_CREATE_NODE_READONLY(25, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.24  */
static const struct snmp_scalar_node signal24 = SNMP_SCALAR_CREATE_NODE_READONLY(24, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.23  */
static const struct snmp_scalar_node signal23 = SNMP_SCALAR_CREATE_NODE_READONLY(23, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.22  */
static const struct snmp_scalar_node signal22 = SNMP_SCALAR_CREATE_NODE(22, 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.10.1.21  */
static const struct snmp_scalar_node signal21 = SNMP_SCALAR_CREATE_NODE(21, 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.10.1.20  */
static const struct snmp_scalar_node signal20 = SNMP_SCALAR_CREATE_NODE(20, 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.10.1.19  */
static const struct snmp_scalar_node signal19 = SNMP_SCALAR_CREATE_NODE(19, 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.10.1.18  */
static const struct snmp_scalar_node signal18 = SNMP_SCALAR_CREATE_NODE(18, 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.10.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.10.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.10.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.10.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.10.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.10.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.10.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.10.1.10  */
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.10.1.9  */
static const struct snmp_scalar_node signal9 = SNMP_SCALAR_CREATE_NODE_READONLY(9, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.8  */
static const struct snmp_scalar_node signal8 = SNMP_SCALAR_CREATE_NODE(8, 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.10.1.7  */
static const struct snmp_scalar_node signal7 = SNMP_SCALAR_CREATE_NODE(7, 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.10.1.6  */
static const struct snmp_scalar_node signal6 = SNMP_SCALAR_CREATE_NODE_READONLY(6, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.1.5  */
static const struct snmp_scalar_node signal5 = SNMP_SCALAR_CREATE_NODE_READONLY(5, SNMP_ASN1_TYPE_OCTET_STRING,
        signal_get_value);

/* signal .1.3.6.1.4.1.41752.911.10.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.10.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.10.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.10.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.10.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,
    &signal20.node.node,
    &signal21.node.node,
    &signal22.node.node,
    &signal23.node.node,
    &signal24.node.node,
    &signal25.node.node,
    &signal26.node.node,
    &signal27.node.node,
    &signal28.node.node,
    &signal29.node.node,
    &signal30.node.node,
    &signal31.node.node,
    &signal32.node.node,
    &signal33.node.node,
    &signal34.node.node,
    &signal35.node.node,
    &signal36.node.node,
    &signal37.node.node,
    &signal38.node.node,
    &signal39.node.node,
    &signal40.node.node,
    &signal41.node.node,
    &signal42.node.node,
    &signal43.node.node,
    &signal44.node.node,
    &signal45.node.node,
    &signal46.node.node,
    &signal47.node.node,
    &signal48.node.node,
    &signal49.node.node,
};
static const struct snmp_tree_node signals_node = SNMP_CREATE_TREE_NODE(1, signals_nodes);

// bt_6703 .1.3.6.1.4.1.41752.911.10
static const struct snmp_node *const bt6709_nodes[] = {
    &signals_node.node
};
static const struct snmp_tree_node bt6709_node = SNMP_CREATE_TREE_NODE(10, bt6709_nodes);

// swt .1.3.6.1.4.1.41752.911
static const struct snmp_node *const swt_nodes[] = {
    &bt6709_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));
}



uint32_t check_netsettings(char *addr, uint8_t len)
{
    uint32_t val = 0;
    uint8_t i, j;
    uint8_t cnt_dot = 0;
    uint8_t digit = 0;

    uint8_t c = *addr;

    for (j = 0; j <= len; j ++) {
        if (isdigit(c)) {
            val = (val * 10) + (int)(c - '0');
        } else if (c == '.' || c == 0) {
            if (val > 0xff) {
                return 0;
            }
            cnt_dot ++;
            val = 0;
        } else {
            return 0;
        }
        c = *++addr;
    }

    if (cnt_dot != 4) {
        return 0;
    }
    return 1;
}

static s16_t signal_get_value(struct snmp_node_instance *instance, void *value)
{
    u8_t *paramStr = (u8_t *)value;
    s32_t *paramInt = (u8_t*)value;
    u8_t paramLength = 0;
    float val = 0;
    int32_t int_val = 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: /* UPSSerial*/
            GetUPSSerialStr((char *)paramStr, &paramLength);
            break;
        case 6: /* UPSVersion*/
            GetUPSVersionStr((char *)paramStr, &paramLength);
            break;
        case 9: /* IntTemp */
            GetInternalTempStr((char *)paramStr, &paramLength);
            break;
        case 10: /* InFreq */
            GetInputFreqStr((char *)paramStr, &paramLength);
            break;
        case 11: /* InVoltVAC */
            GetInputVoltageStr((char *)paramStr, &paramLength);
            break;
        case 12: /* OutVoltVAC */
            GetOutputVoltageStr((char *)paramStr, &paramLength);
            break;
        case 13: /* Power */
            GetPowerStr((char *)paramStr, &paramLength);
            break;
        case 14: /* BatCap */
            GetBatCapacityStr((char *)paramStr, &paramLength);
            break;
        case 15: /* BatTime */
            GetRuntimeStr((char *)paramStr, &paramLength);
            break;
        case 16: /* ConnectMonitor */
            GetConnectMonitorStr((char *)paramStr, &paramLength);
            break;
        case 17: /* Alarms */
            GetAlarmStr((char *)paramStr, &paramLength);
            break;
        case 18: /* IP server1 (traps) */
            GetManagerIp((char *)paramStr, &paramLength);
            break;
        case 19: /* IP server2 (traps) */
            GetManagerIp2((char *)paramStr, &paramLength);
            break;
        case 20: /* IP server3 (traps) */
            GetManagerIp3((char *)paramStr, &paramLength);
            break;
        case 21: /* IP server4 (traps) */
            GetManagerIp4((char *)paramStr, &paramLength);
            break;
        case 22: /* IP server5 (traps) */
            GetManagerIp5((char *)paramStr, &paramLength);
            break;
        case 23: /* WhiteList range 1 */
            GetWhiteListSTR((char *)paramStr, &paramLength, 0);
            break;
        case 24: /* WhiteList range 2 */
            GetWhiteListSTR((char *)paramStr, &paramLength, 1);
            break;
        case 25: /* WhiteList range 3 */
            GetWhiteListSTR((char *)paramStr, &paramLength, 2);
            break;
        case 26: /* WhiteList range 4 */
            GetWhiteListSTR((char *)paramStr, &paramLength, 3);
            break;
        case 27: /* WhiteList range 5 */
            GetWhiteListSTR((char *)paramStr, &paramLength, 4);
            break;
        case 28: /* UPSVoltCellMin */
            GetUPSVoltCellMinStr((char *)paramStr, &paramLength);
            break;
        case 29: /* UPSVoltCellMax */
            GetUPSVoltCellMaxStr((char *)paramStr, &paramLength);
            break;
        case 30: /* VACAlarmHighRange */
            GetVACAlarmHighRangeStr((char *)paramStr, &paramLength);
            break;
        case 31: /* VACAlarmLowRange */
            GetVACAlarmLowRangeStr((char *)paramStr, &paramLength);
            break;
        case 32: /* VACAlarmHistRange */
            GetVACAlarmHisteStr((char *)paramStr, &paramLength);
            break;
        case 33: /* TemperatureAlarmHighRange */
            GetTemperatureAlarmHighRangeStr((char *)paramStr, &paramLength);
            break;
        case 34: /* TemperatureAlarmLowRange */
            GetTemperatureAlarmLowRangeStr((char *)paramStr, &paramLength);
            break;
        case 35: /* TemperatureAlarmHistRange */
            GetTemperatureAlarmHisteStr((char *)paramStr, &paramLength);
            break;
        case 36: /* LoadAlarmHighRange */
            GetLoadAlarmHighRangeStr((char *)paramStr, &paramLength);
            break;
        case 37: /* LoadAlarmHistRange */
            GetLoadAlarmHisteStr((char *)paramStr, &paramLength);
            break;
        case 38: /* SntpTimeZone */
            GetSntpTimeZoneStr((char *)paramStr, &paramLength);
            break;
        case 39: /* SntpState */
            GetSntpStateStr((char *)paramStr, &paramLength);
            break;
        case 40: /* SntpServerIp */
            GetSntpServerIpStr((char *)paramStr, &paramLength);
            break;
        case 41: /* SntpLastData */
            GetSntpLastDataStr((char *)paramStr, &paramLength);
            break;
        case 42: /* Date */
            GetDateStr((char *)paramStr, &paramLength);
            break;
        case 43: /* Time */
            GetTimeStr((char *)paramStr, &paramLength);
            break;
        case 44: /* AKBTimeWork */
            GetAKBWorktimeStr((char *)paramStr, &paramLength);
            break;
        case 45: /* InputCurrent */
            GetInputCurrentStr((char *)paramStr, &paramLength);
            break;
        case 46: /* OutputCurrent */
            GetOutputCurrentStr((char *)paramStr, &paramLength);
            break;
        case 47: /* VoltageAKB */
            GetVoltageAKBtStr((char *)paramStr, &paramLength);
            break;
        case 48: /*  CapacityNominalAKB*/
            GetCapacityNominalAKBStr((char *)paramStr, &paramLength);
            break;
        case 49: /*  DataNextChangeAKB*/
            GetDataNextChangeAKBStr((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;
    float setVal;
    bool enable_old_sntp;

    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) {
                HTTP_StartResetTask(false);
            }
            break;
        case 7: /* 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);
                set_act_source(SNMP_ACT);
            } else if (val == 100) {
                res = ups_metac_service_pdu(ups_test_10sec);
                set_act_source(SNMP_ACT);
            } else if (val == 999) {
                res = ups_metac_service_pdu(ups_test_low_bat);
                set_act_source(SNMP_ACT);
            }
            break;
        case 8: /* 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;
        case 18:
            val_string = (char *)value;
            SetManagerIp(val_string);
            break;
        case 19:
            val_string = (char *)value;
            SetManagerIp2(val_string);
            break;
        case 20:
            val_string = (char *)value;
            SetManagerIp3(val_string);
            break;
        case 21:
            val_string = (char *)value;
            SetManagerIp4(val_string);
            break;
        case 22:
            val_string = (char *)value;
            SetManagerIp5(val_string);
            break;
        case 28:
            val_string = (char *)value;
            SetUPSVoltCellMinStr(val_string);
            break;
        case 29:
            val_string = (char *)value;
            SetUPSVoltCellMaxStr(val_string);
            break;
        case 30:
            val_string = (char *)value;
            SetVACAlarmHighRangeStr(val_string);
            break;
        case 31:
            val_string = (char *)value;
            SetVACAlarmLowRangeStr(val_string);
            break;
        case 32:
            val_string = (char *)value;
            SetVACAlarmHisteStr(val_string);
            break;
        case 33:
            val_string = (char *)value;
            SetTemperatureAlarmHighRangeStr(val_string);
            break;
        case 34:
            val_string = (char *)value;
            SetTemperatureAlarmLowRangeStr(val_string);
            break;
        case 35:
            val_string = (char *)value;
            SetTemperatureAlarmHisteStr(val_string);
            break;
        case 36:
            val_string = (char *)value;
            SetLoadAlarmHighRangeStr(val_string);
            break;
        case 37:
            val_string = (char *)value;
            SetLoadAlarmHistStr(val_string);
            break;
        case 38:
            val_string = (char *)value;
            SetSntpTimeZoneStr(val_string);
            break;
        case 39:
            enable_old_sntp = sSettings.sSNTP.sntpEnable;
            val_string = (char *)value;
            SetSntpStateStr(val_string);
            if (sSettings.sSNTP.sntpEnable != enable_old_sntp) {
                SETTINGS_Save();
                log_event_data(LOG_SETTING_SAVE, "Администратор (SNMP)");
            }
            break;
        case 40:
            val_string = (char *)value;
            if (strncmp(val_string, sSettings.sSNTP.ip, strlen(val_string)) != 0) {
                SetSntpServerIpStr(val_string);
                SETTINGS_Save();
                log_event_data(LOG_SETTING_SAVE, "Администратор (SNMP)");
                SNTP_Init();
                //vTaskDelay(7000);
                SNTP_Poll();
            }
            break;
        case 41:
            break;
        case 42:
            val_string = (char *)value;
            SetDateStr(val_string);
            break;
        case 43:
            val_string = (char *)value;
            SetTimeStr(val_string);
            break;
        default :
            return SNMP_ERR_GENERROR;
            break;
    };

    if ((oid >= 18 && oid <= 22)
        || (oid >= 28 && oid <= 38)) {
        SETTINGS_Save();
        log_event_data(LOG_SETTING_SAVE, "Администратор (SNMP)");
    }

    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;
    float tmp_value;
    uint8_t j;
    u32_t oid = instance->node->oid; // id сигнала
    bool fail = false;

    switch (oid) {
        case 2:
        case 3:
            if ( len == sizeof(s32_t) ) {
                val = *((s32_t *)value);
                if (val == 1) {
                    ret = SNMP_ERR_NOERROR;
                }
            }
            break;
        case 7: /* BatTest */
            val = *((s32_t *)value);
            if ((val >= 0 && val <= 100) || val == 999) {
                ret = SNMP_ERR_NOERROR;
            }
            break;
        case 8: /* Shutdown UPS */
            if ( len <= 3 ) {
                val_string = (char *)value;
                val_string[len] = 0;
                if (atof(val_string) <= 10) {
                    ret = SNMP_ERR_NOERROR;
                }
            }
            break;
        case 18:
        case 19:
        case 20:
        case 21:
        case 22:
        case 40:
            if ( len <= 15 ) {
                val_string = (char *)value;
                val_string[len] = 0;
                if (check_netsettings(val_string, len)) {
                    ret = SNMP_ERR_NOERROR;
                }
            }
            break;
        case 28:
            val_string = (char *)value;
            val_string[len] = 0;
            for (j = 0; j < len; j ++) {
                if (!isfloatdigit(val_string[j])) {
                    return ret;
                }
            }
            tmp_value = atof(val_string);
            if (tmp_value < sSettings.UPS_Setting.Ucellmax && tmp_value > MIN_VOLT_CELL_RANGE) {
                ret = SNMP_ERR_NOERROR;
            }
            break;
        case 29:
            val_string = (char *)value;
            val_string[len] = 0;
            for (j = 0; j < len; j ++) {
                if (!isfloatdigit(val_string[j])) {
                    return ret;
                }
            }
            tmp_value = atof(val_string);
            if (tmp_value > sSettings.UPS_Setting.Ucellmin && tmp_value <= MAX_VOLT_CELL_RANGE) {
                ret = SNMP_ERR_NOERROR;
            }
            break;
        case 30:
            val_string = (char *)value;
            val_string[len] = 0;
            for (j = 0; j < len; j ++) {
                if (!isdigit_int(val_string[j])) {
                    return ret;
                }
            }
            tmp_value = atof(val_string);
            if (tmp_value >= MAX_VAC_MIN_RANGE && tmp_value <= MAX_VAC_MAX_RANGE) {
                ret = SNMP_ERR_NOERROR;
            }
            break;
        case 31:
            val_string = (char *)value;
            val_string[len] = 0;
            for (j = 0; j < len; j ++) {
                if (!isdigit_int(val_string[j])) {
                    return ret;
                }
            }
            tmp_value = atof(val_string);
            if (tmp_value >= MIN_VAC_MIN_RANGE && tmp_value <= MIN_VAC_MAX_RANGE) {
                ret = SNMP_ERR_NOERROR;
            }
            break;
        case 32:
            val_string = (char *)value;
            val_string[len] = 0;
            for (j = 0; j < len; j ++) {
                if (!isdigit_int(val_string[j])) {
                    return ret;
                }
            }
            tmp_value = atof(val_string);
            if (tmp_value >= HIST_VAC_MIN_RANGE && tmp_value <= HIST_VAC_MAX_RANGE) {
                ret = SNMP_ERR_NOERROR;
            }
            break;
        case 33:
            val_string = (char *)value;
            val_string[len] = 0;
            for (j = 0; j < len; j ++) {
                if (!isdigit_int(val_string[j])) {
                    return ret;
                }
            }
            tmp_value = atof(val_string);
            if (tmp_value >= MAX_TEMP_MIN_RANGE && tmp_value <= MAX_TEMP_MAX_RANGE) {
                ret = SNMP_ERR_NOERROR;
            }
            break;
        case 34:
            val_string = (char *)value;
            val_string[len] = 0;
            for (j = 0; j < len; j ++) {
                if (!isdigit_int(val_string[j])) {
                    if (j == 0 && val_string[j] == '-') {
                        continue;
                    } else {
                        return ret;
                    }
                }
            }
            tmp_value = atof(val_string);
            if (tmp_value >= MIN_TEMP_MIN_RANGE && tmp_value <= MIN_TEMP_MAX_RANGE) {
                ret = SNMP_ERR_NOERROR;
            }
            break;
        case 35:
            val_string = (char *)value;
            val_string[len] = 0;
            for (j = 0; j < len; j ++) {
                if (!isfloatdigit(val_string[j])) {
                    return ret;
                }
            }
            tmp_value = atof(val_string);
            if (tmp_value >= HIST_TEMP_MIN_RANGE && tmp_value <= HIST_TEMP_MAX_RANGE) {
                ret = SNMP_ERR_NOERROR;
            }
            break;
        case 36:
            val_string = (char *)value;
            val_string[len] = 0;
            for (j = 0; j < len; j ++) {
                if (!isdigit_int(val_string[j])) {
                    return ret;
                }
            }
            tmp_value = atof(val_string);
            if (tmp_value >= MAX_LOAD_MIN_RANGE && tmp_value <= MAX_LOAD_MAX_RANGE) {
                ret = SNMP_ERR_NOERROR;
            }
            break;
        case 37:
            val_string = (char *)value;
            val_string[len] = 0;
            for (j = 0; j < len; j ++) {
                if (!isfloatdigit(val_string[j])) {
                    return ret;
                }
            }
            tmp_value = atof(val_string);
            if (tmp_value >= HIST_LOAD_MIN_RANGE && tmp_value <= HIST_LOAD_MAX_RANGE) {
                ret = SNMP_ERR_NOERROR;
            }
            break;
        case 38:
            val_string = (char *)value;
            val_string[len] = 0;
            if (len > 1 && len < 5) {
                bool fail = false;
                if (val_string[0] != '-' && val_string[0] != '+') {
                    fail = true;
                }
                if (!isdigit_int(val_string[1]) && (!isdigit_int(val_string[2]) && val_string[2] != '.')
                    && (!isdigit_int(val_string[3]) && val_string[3] != '.')) {
                    fail = true;
                }
                if (len == 5) {
                    if (!isdigit_int(val_string[4])) {
                        fail = true;
                    }
                }
                if (!fail) {
                    tmp_value = atof(val_string);
                    if (tmp_value >= -12.0 && tmp_value <= 12.0) {
                        ret = SNMP_ERR_NOERROR;
                    }
                }
            }
            break;
        case 39:
            val_string = (char *)value;
            val_string[len] = 0;
            if (len == 1) {
                if (val_string[0] == '0' || val_string[0] == '1') {
                    ret = SNMP_ERR_NOERROR;
                }
            }
            break;
        case 42:
            val_string = (char *)value;
            val_string[len] = 0;
            if (len == 10) {
                for (j = 0; j < len; j++) {
                    if (j != 4 && j != 7) {
                        if (val_string[j] > 0x39 || val_string[j] < 0x30) {
                            fail = true;
                            break;
                        }
                    } else if (j == 4 || j == 7) {
                        if (val_string[j] != '-') {
                            fail = true;
                            break;
                        }
                    }
                }
                if (!fail) {
                    uint16_t temp = 0;
                    temp = 1000 * (val_string[0] - 0x30) + 100 * (val_string[1] - 0x30) + 10 * (val_string[2] - 0x30) + val_string[3] -
                        0x30;
                    if (temp > 2099 || temp < 2000) {
                        fail = true;
                    }
                    temp = 0;
                    temp = 10 * (val_string[5] - 0x30) + (val_string[6] - 0x30);
                    if (temp > 12) {
                        fail = true;
                    }
                    temp = 0;
                    temp = 10 * (val_string[8] - 0x30) + (val_string[9] - 0x30);
                    if (temp > 31) {
                        fail = true;
                    }
                }
                if (!fail) {
                    ret = SNMP_ERR_NOERROR;
                }
            }
            break;
        case 43:
            val_string = (char *)value;
            val_string[len] = 0;
            if (len == 5) {
                for (j = 0; j < len; j++) {
                    if (j != 2) {
                        if (val_string[j] > 0x39 || val_string[j] < 0x30) {
                            fail = true;
                            break;
                        }

                    } else if (j == 2) {
                        if (val_string[j]  != ':') {
                            fail = true;
                            break;
                        }
                    }
                }
                if (!fail) {
                    uint16_t temp = 0;
                    temp = 10 * (val_string[0] - 0x30) + (val_string[1] - 0x30);
                    if (temp > 23) {
                        fail = true;
                    }
                    temp = 0;
                    temp = 10 * (val_string[3] - 0x30) + (val_string[4] - 0x30);
                    if (temp > 59) {
                        fail = true;
                    }
                }
                if (!fail) {
                    ret = SNMP_ERR_NOERROR;
                }
            }
            break;
    };


    return ret;
}

#endif

