Ejemplo n.º 1
0
void *
aim_malloc(size_t size)
{
    void *ptr = AIM_MALLOC(size);
    if (ptr == NULL) {
        AIM_DIE("memory allocation failed");
    }
    return ptr;
}
/*
 * Register a sensor
 * Caller must make sure that 1 sensor registered only once
 * If it calls this twice, it will get 2 oid entries
 * for the same sensor
 *
 * We want to keep this snmp code as simple as possible
 */
static int
onlp_snmp_sensor_reg__(int sensor_type,
                       onlp_snmp_sensor_t *sensor)
{
    oid otemp[] = { ONLP_SNMP_SENSOR_TEMP_OID };
    oid ofan[]  = { ONLP_SNMP_SENSOR_FAN_OID };
    oid opsu[]  = { ONLP_SNMP_SENSOR_PSU_OID };
    oid oled[]  = { ONLP_SNMP_SENSOR_LED_OID };
    oid omisc[] = { ONLP_SNMP_SENSOR_MISC_OID };
    oid *o;
    u_long o_len;
    int ret = MIB_REGISTRATION_FAILED;

    onlp_snmp_sensor_ctrl_t *ss_type = get_sensor_ctrl__(sensor_type);

    /* We start with Base 1 */
    AIM_TRUE_OR_DIE(onlp_snmp_sensor_type_valid(sensor_type));
    AIM_TRUE_OR_DIE(sensor);
    AIM_TRUE_OR_DIE(ss_type);

    switch(sensor_type)
        {
        case ONLP_SNMP_SENSOR_TYPE_TEMP:
            o = otemp;
            o_len = OID_LENGTH(otemp);

            /* Not init yet, init oid table */
            if (!ss_type->handlers) {
                ss_type->handler_cnt = sizeof(temp_handler_fn__) / sizeof(temp_handler_fn__[0]);
                ss_type->handlers    = temp_handler_fn__;
                snprintf(ss_type->name, sizeof(ss_type->name), "%s", "temp_table");
            }
            break;

        case ONLP_SNMP_SENSOR_TYPE_FAN:
            o = ofan;
            o_len = OID_LENGTH(ofan);

            /* Not init yet, init oid table */
            if (!ss_type->handlers) {
                ss_type->handler_cnt = sizeof(fan_handler_fn__) / sizeof(fan_handler_fn__[0]);
                ss_type->handlers    = fan_handler_fn__;
                snprintf(ss_type->name, sizeof(ss_type->name), "%s", "fan_table");
            }
            break;

        case ONLP_SNMP_SENSOR_TYPE_PSU:
            o = opsu;
            o_len = OID_LENGTH(opsu);

            /* Not init yet, init oid table */
            if (!ss_type->handlers) {
                ss_type->handler_cnt = sizeof(psu_handler_fn__) / sizeof(psu_handler_fn__[0]);
                ss_type->handlers    = psu_handler_fn__;
                snprintf(ss_type->name, sizeof(ss_type->name), "%s", "psu_table");

            }
            break;

        case ONLP_SNMP_SENSOR_TYPE_LED:
            o = oled;
            o_len = OID_LENGTH(oled);

            /* Not init yet, init oid table */
            if (!ss_type->handlers) {
                ss_type->handler_cnt = sizeof(led_handler_fn__) / sizeof(led_handler_fn__[0]);
                ss_type->handlers    = led_handler_fn__;
                snprintf(ss_type->name, sizeof(ss_type->name), "%s", "led_table");
            }
            break;

        case ONLP_SNMP_SENSOR_TYPE_MISC:
            o = omisc;
            o_len = OID_LENGTH(omisc);

            /* Not init yet, init oid table */
            if (!ss_type->handlers) {
                ss_type->handler_cnt = sizeof(misc_handler_fn__) / sizeof(misc_handler_fn__[0]);
                ss_type->handlers    = misc_handler_fn__;
                snprintf(ss_type->name, sizeof(ss_type->name), "%s", "misc_table");
            }
            break;

        default:
            AIM_DIE("Invalid sensor value.");
            break;
    }

    /*
     * sensor_cnt original is 0
     * When sensor_cnt == ONLP_SNMP_CONFIG_DEV_MAX_INDEX
     * We stop adding
     */
    if (ss_type->sensor_cnt < ONLP_SNMP_CONFIG_DEV_MAX_INDEX) {
        /* Device index equal to ss_type->sensor_cnt */
        ss_type->sensor_cnt++;

        /* This entry must be null */
        AIM_TRUE_OR_DIE(!ss_type->sensor_list[ss_type->sensor_cnt]);

        snmp_log(LOG_INFO, "init type=%d, index=%d, id=%d",
                 sensor_type, ss_type->sensor_cnt, sensor->sensor_id);

        onlp_snmp_sensor_t *ss = AIM_MALLOC(sizeof(onlp_snmp_sensor_t));
        AIM_TRUE_OR_DIE(ss);
        AIM_MEMCPY(ss, sensor, sizeof(*sensor));
        ss->sensor_type = sensor_type;
        ss->info_valid = 0;
        ss->last_update_time = 0;

        /* Assign sensor to the list */
        ss_type->sensor_list[ss_type->sensor_cnt] = ss;

    } else {
        snmp_log(LOG_ALERT,
                 "Failed to register sensor type=%d id=%d, resource limited",
                 sensor_type, sensor->sensor_id);
        return ret;
    }

    AIM_TRUE_OR_DIE(o_len == ONLP_SNMP_SENSOR_OID_LENGTH,
                    "invalid oid length=%d", o_len);

    ret = reg_snmp_sensor_helper__(sensor_type, o, o_len,
                                   ss_type->sensor_cnt);
    if (ret) {
        snmp_log(LOG_ALERT,
                 "Failed to register sensor type=%d id=%d, MIB_ERROR=%d",
                 sensor_type, sensor->sensor_id, ret);
    }

    return ret;
}