📄 snmp_rsa_sensor.c
字号:
/* -*- linux-c -*- * * (C) Copyright IBM Corp. 2004 * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This * file and program are licensed under a BSD style license. See * the Copying file included with the OpenHPI distribution for * full licensing terms. * * Author(s): * Steve Sherman <stevees@us.ibm.com> * W. David Ashley <dashley@us.ibm.com> */#include <glib.h>#include <SaHpi.h>#include <openhpi.h>#include <oh_plugin.h>#include <snmp_util.h>#include <rsa_resources.h>#include <snmp_rsa.h>#include <snmp_rsa_utils.h>#include <snmp_rsa_sensor.h>SaErrorT snmp_rsa_get_sensor_data(void *hnd, SaHpiResourceIdT id, SaHpiSensorNumT num, SaHpiSensorReadingT *data){ gchar *oid; SaHpiSensorReadingT working; struct snmp_value get_value; struct oh_handler_state *handle = (struct oh_handler_state *)hnd; struct snmp_rsa_hnd *custom_handle = (struct snmp_rsa_hnd *)handle->data; SaHpiRdrT *rdr = oh_get_rdr_by_type(handle->rptcache, id, SAHPI_SENSOR_RDR, num); if(rdr == NULL) { return SA_ERR_HPI_NOT_PRESENT; } struct RSA_SensorInfo *s = (struct RSA_SensorInfo *)oh_get_rdr_data(handle->rptcache, id, rdr->RecordId); if(s == NULL) { return -1; } if (rdr->RdrTypeUnion.SensorRec.Ignore == SAHPI_TRUE) { return SA_ERR_HPI_INVALID_CMD; } memset(&working, 0, sizeof(SaHpiSensorReadingT)); /* Extract index from rdr id and get the snmp of the sensor */ if (s->mib.oid != NULL) { oid = snmp_derive_objid(rdr->Entity, s->mib.oid); if(oid == NULL) { dbg("NULL SNMP OID returned for %s\n",s->mib.oid); return -1; } /* Read the sensor value */ if(snmp_get(custom_handle->ss, oid, &get_value) != 0){ dbg("SNMP could not read sensor %s. Type = %d",oid,get_value.type); g_free(oid); return SA_ERR_HPI_NO_RESPONSE; } g_free(oid); /* Based on the sensor description, construct a reading to send up */ /* format the value into the reading for each type of reading format */ working.ValuesPresent = rdr->RdrTypeUnion.SensorRec.DataFormat.ReadingFormats; if(working.ValuesPresent & SAHPI_SRF_RAW) { if(get_value.type != ASN_INTEGER) { dbg("Sensor value type mismatches reading format."); return -1; } else { working.Raw = (SaHpiUint32T)get_value.integer; } } if(working.ValuesPresent & SAHPI_SRF_INTERPRETED) { if(get_value.type == ASN_INTEGER) { working.Interpreted.Type = SAHPI_SENSOR_INTERPRETED_TYPE_INT32; working.Interpreted.Value.SensorInt32 = get_value.integer; } else { SaHpiSensorInterpretedUnionT value; working.Interpreted.Type = rdr->RdrTypeUnion.SensorRec.DataFormat.Range.Max.Interpreted.Type; if(rdr->RdrTypeUnion.SensorRec.DataFormat.Range.Max.Interpreted.Type == SAHPI_SENSOR_INTERPRETED_TYPE_BUFFER) { strncpy(working.Interpreted.Value.SensorBuffer, get_value.string, SAHPI_SENSOR_BUFFER_LENGTH); } else { if(s->mib.convert_snmpstr >= 0) { if(get_interpreted_value(get_value.string,s->mib.convert_snmpstr,&value)) { dbg("Error: get_interpreted_value for %s, (%s)\n",s->mib.oid,get_value.string); return -1; } working.Interpreted.Value = value; } else { dbg("Sensor %s SNMP string value needs to be converted\n", s->mib.oid); return -1; } } } } } if (working.ValuesPresent & SAHPI_SRF_EVENT_STATE) { working.EventStatus.SensorStatus = s->sensor_evt_enablement.SensorStatus; /* Hardcoded hack for R/W LEDs */ if (working.ValuesPresent & SAHPI_SRF_RAW) { if (rdr->RdrTypeUnion.SensorRec.Category == SAHPI_EC_USAGE) { switch (working.Raw) { case 0: working.EventStatus.EventStatus = rdr->RdrTypeUnion.SensorRec.DataFormat.Range.Min.EventStatus.EventStatus; break; case 1: working.EventStatus.EventStatus = rdr->RdrTypeUnion.SensorRec.DataFormat.Range.Nominal.EventStatus.EventStatus; break; case 2: working.EventStatus.EventStatus = rdr->RdrTypeUnion.SensorRec.DataFormat.Range.Max.EventStatus.EventStatus; break; default: dbg("Unrecognized Raw values for LED=%s", rdr->IdString.Data); return -1; } } else { switch (working.Raw) { case 0: working.EventStatus.EventStatus = rdr->RdrTypeUnion.SensorRec.DataFormat.Range.Min.EventStatus.EventStatus; break; case 1: working.EventStatus.EventStatus = rdr->RdrTypeUnion.SensorRec.DataFormat.Range.Max.EventStatus.EventStatus; break; default: dbg("Unrecognized Raw values for LED=%s", rdr->IdString.Data); return -1; } } } else { /* Non-LED sensor - normal case */ working.EventStatus.EventStatus = s->cur_state; } } memcpy(data,&working,sizeof(SaHpiSensorReadingT)); return SA_OK;}#define get_raw_thresholds(thdmask, thdoid, thdname) \do { \ if(rdr->RdrTypeUnion.SensorRec.ThresholdDefn.ReadThold & thdmask) { \ if(s->mib.threshold_oids.RawThresholds.thdoid != NULL && s->mib.threshold_oids.RawThresholds.thdoid[0] != '\0') { \ oid = snmp_derive_objid(rdr->Entity,s->mib.threshold_oids.RawThresholds.thdoid); \ if(oid == NULL) { \ dbg("NULL SNMP OID returned for %s\n",s->mib.threshold_oids.RawThresholds.thdoid); \ return -1; \ } \ if((snmp_get(custom_handle->ss, oid, &get_value) != 0) | \ (get_value.type != ASN_INTEGER)) { \ dbg("SNMP could not read %s; Type=%d.\n",oid,get_value.type); \ g_free(oid); \ return SA_ERR_HPI_NO_RESPONSE; \ } \ g_free(oid); \ found_raw++; \ working.thdname.Raw = get_value.integer; \ working.thdname.ValuesPresent = working.thdname.ValuesPresent | SAHPI_SRF_RAW; \ } else { \ dbg("Raw threshold defined as readable but no OID defined\n"); \ } \ } \} while(0)#define get_interpreted_thresholds(thdmask, thdoid, thdname) \do { \ if(rdr->RdrTypeUnion.SensorRec.ThresholdDefn.ReadThold & thdmask) { \ if(s->mib.threshold_oids.InterpretedThresholds.thdoid != NULL && s->mib.threshold_oids.InterpretedThresholds.thdoid[0] != '\0') { \ oid = snmp_derive_objid(rdr->Entity,s->mib.threshold_oids.InterpretedThresholds.thdoid); \ if(oid == NULL) { \ dbg("NULL SNMP OID returned for %s\n",s->mib.threshold_oids.InterpretedThresholds.thdoid); \ return -1; \ } \ if((snmp_get(custom_handle->ss, oid, &get_value) != 0) | \ !((get_value.type == ASN_INTEGER) | (get_value.type == ASN_OCTET_STR))) { \ dbg("SNMP could not read %s; Type=%d.\n",oid,get_value.type); \ g_free(oid); \ return SA_ERR_HPI_NO_RESPONSE; \ } \ found_interpreted++; \ /* Means we always need to define this field in rsa_resources.h */ \ working.thdname.Interpreted.Type = rdr->RdrTypeUnion.SensorRec.DataFormat.Range.Max.Interpreted.Type; \ working.thdname.ValuesPresent = working.thdname.ValuesPresent | SAHPI_SRF_INTERPRETED; \ if(get_value.type == ASN_INTEGER) { \ working.thdname.Interpreted.Value.SensorInt32 = get_value.integer; \ } else if(get_value.type == ASN_OCTET_STR && s->mib.convert_snmpstr >= 0) { \ if(get_interpreted_value(get_value.string,s->mib.convert_snmpstr,&value)) { \ dbg("Error: bad return from get_interpreted_value for %s\n",oid); \ g_free(oid); \ return -1; \ } \ working.thdname.Interpreted.Value = value; \ } else { \ dbg("%s threshold is string but no conversion defined\n",oid); \ g_free(oid); \ return -1; \ } \ g_free(oid); \ } else { \ dbg("Interpreted threshold defined as readable but no OID defined\n"); \ } \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -