Factor out some duplicated code to simplify maintenance. Reduces

the source file by ~100 lines, and amd64 object file shrinks by
~650 bytes.

No functional change intended.
This commit is contained in:
pgoyette 2011-06-08 18:22:24 +00:00
parent 463c1a0c0f
commit 35a3b0e4a6
2 changed files with 96 additions and 201 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: sysmon_envsys.c,v 1.113 2011/06/04 13:24:33 pgoyette Exp $ */ /* $NetBSD: sysmon_envsys.c,v 1.114 2011/06/08 18:22:24 pgoyette Exp $ */
/*- /*-
* Copyright (c) 2007, 2008 Juan Romero Pardines. * Copyright (c) 2007, 2008 Juan Romero Pardines.
@ -64,7 +64,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.113 2011/06/04 13:24:33 pgoyette Exp $"); __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.114 2011/06/08 18:22:24 pgoyette Exp $");
#include <sys/param.h> #include <sys/param.h>
#include <sys/types.h> #include <sys/types.h>
@ -1263,16 +1263,11 @@ static sme_event_drv_t *
sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
prop_dictionary_t dict, envsys_data_t *edata) prop_dictionary_t dict, envsys_data_t *edata)
{ {
const struct sme_descr_entry *sdt_state, *sdt_units, *sdt_battcap; const struct sme_descr_entry *sdt;
const struct sme_descr_entry *sdt_drive; int error;
sme_event_drv_t *sme_evdrv_t = NULL; sme_event_drv_t *sme_evdrv_t = NULL;
char indexstr[ENVSYS_DESCLEN]; char indexstr[ENVSYS_DESCLEN];
/*
* Find the correct units for this sensor.
*/
sdt_units = sme_find_table_entry(SME_DESC_UNITS, edata->units);
/* /*
* Add the index sensor string. * Add the index sensor string.
* *
@ -1287,34 +1282,13 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
/* /*
* ... * ...
* <key>type</key>
* <string>foo</string>
* <key>description</key> * <key>description</key>
* <string>blah blah</string> * <string>blah blah</string>
* ... * ...
*/ */
if (sme_sensor_upstring(dict, "type", sdt_units->desc))
goto bad;
if (sme_sensor_upstring(dict, "description", edata->desc)) if (sme_sensor_upstring(dict, "description", edata->desc))
goto bad; goto bad;
/*
* Add sensor's state description.
*
* ...
* <key>state</key>
* <string>valid</string>
* ...
*/
sdt_state = sme_find_table_entry(SME_DESC_STATES, edata->state);
DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
__func__, edata->desc, edata->units, edata->state));
if (sme_sensor_upstring(dict, "state", sdt_state->desc))
goto bad;
/* /*
* Add the monitoring boolean object: * Add the monitoring boolean object:
* *
@ -1339,22 +1313,9 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
goto out; goto out;
} }
/*
* Add the percentage boolean object, true if ENVSYS_FPERCENT
* is set or false otherwise.
*
* ...
* <key>want-percentage</key>
* <true/>
* ...
*/
if (edata->flags & ENVSYS_FPERCENT)
if (sme_sensor_upbool(dict, "want-percentage", true))
goto out;
/* /*
* Add the allow-rfact boolean object, true if * Add the allow-rfact boolean object, true if
* ENVSYS_FCHANGERFACT if set or false otherwise. * ENVSYS_FCHANGERFACT is set, false otherwise.
* *
* ... * ...
* <key>allow-rfact</key> * <key>allow-rfact</key>
@ -1372,83 +1333,12 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
} }
} }
/* error = sme_update_sensor_dictionary(dict, edata,
* Add the object for battery capacity sensors: (edata->state == ENVSYS_SVALID));
* if (error < 0)
* ... goto bad;
* <key>battery-capacity</key> else if (error)
* <string>NORMAL</string>
* ...
*/
if (edata->units == ENVSYS_BATTERY_CAPACITY) {
sdt_battcap = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY,
edata->value_cur);
if (sme_sensor_upstring(dict, "battery-capacity",
sdt_battcap->desc))
goto out; goto out;
}
/*
* Add the drive-state object for drive sensors:
*
* ...
* <key>drive-state</key>
* <string>drive is online</string>
* ...
*/
if (edata->units == ENVSYS_DRIVE) {
sdt_drive = sme_find_table_entry(SME_DESC_DRIVE_STATES,
edata->value_cur);
if (sme_sensor_upstring(dict, "drive-state", sdt_drive->desc))
goto out;
}
/*
* Add the following objects if sensor is enabled...
*/
if (edata->state == ENVSYS_SVALID) {
/*
* Add the following objects:
*
* ...
* <key>rpms</key>
* <integer>2500</integer>
* <key>rfact</key>
* <integer>10000</integer>
* <key>cur-value</key>
* <integer>1250</integer>
* <key>min-value</key>
* <integer>800</integer>
* <key>max-value</integer>
* <integer>3000</integer>
* ...
*/
if (edata->units == ENVSYS_SFANRPM)
if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
goto out;
if (edata->units == ENVSYS_SVOLTS_AC ||
edata->units == ENVSYS_SVOLTS_DC)
if (sme_sensor_upint32(dict, "rfact", edata->rfact))
goto out;
if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
goto out;
if (edata->flags & ENVSYS_FVALID_MIN) {
if (sme_sensor_upint32(dict,
"min-value",
edata->value_min))
goto out;
}
if (edata->flags & ENVSYS_FVALID_MAX) {
if (sme_sensor_upint32(dict,
"max-value",
edata->value_max))
goto out;
}
}
/* /*
* ... * ...
@ -1470,7 +1360,8 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
sme_evdrv_t->sed_sdict = dict; sme_evdrv_t->sed_sdict = dict;
sme_evdrv_t->sed_edata = edata; sme_evdrv_t->sed_edata = edata;
sme_evdrv_t->sed_sme = sme; sme_evdrv_t->sed_sme = sme;
sme_evdrv_t->sed_powertype = sdt_units->crittype; sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units);
sme_evdrv_t->sed_powertype = sdt->crittype;
} }
out: out:
@ -1555,7 +1446,6 @@ sme_get_max_value(struct sysmon_envsys *sme,
int int
sme_update_dictionary(struct sysmon_envsys *sme) sme_update_dictionary(struct sysmon_envsys *sme)
{ {
const struct sme_descr_entry *sdt;
envsys_data_t *edata; envsys_data_t *edata;
prop_object_t array, dict, obj, obj2; prop_object_t array, dict, obj, obj2;
int error = 0; int error = 0;
@ -1628,85 +1518,92 @@ sme_update_dictionary(struct sysmon_envsys *sme)
/* /*
* update sensor's state. * update sensor's state.
*/ */
error = sme_update_sensor_dictionary(dict, edata, true);
if (error)
break;
}
return error;
}
int
sme_update_sensor_dictionary(prop_object_t dict, envsys_data_t *edata,
bool value_update)
{
const struct sme_descr_entry *sdt;
int error = 0;
sdt = sme_find_table_entry(SME_DESC_STATES, edata->state); sdt = sme_find_table_entry(SME_DESC_STATES, edata->state);
DPRINTFOBJ(("%s: sensor #%d type=%d (%s) flags=%d\n", DPRINTFOBJ(("%s: sensor #%d type=%d (%s) flags=%d\n", __func__,
__func__, edata->sensor, sdt->type, sdt->desc, edata->sensor, sdt->type, sdt->desc, edata->flags));
edata->flags));
error = sme_sensor_upstring(dict, "state", sdt->desc); error = sme_sensor_upstring(dict, "state", sdt->desc);
if (error) if (error)
break; return (-error);
/* /*
* update sensor's type. * update sensor's type.
*/ */
sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units); sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units);
DPRINTFOBJ(("%s: sensor #%d units=%d (%s)\n", DPRINTFOBJ(("%s: sensor #%d units=%d (%s)\n", __func__, edata->sensor,
__func__, edata->sensor, sdt->type, sdt->desc)); sdt->type, sdt->desc));
error = sme_sensor_upstring(dict, "type", sdt->desc); error = sme_sensor_upstring(dict, "type", sdt->desc);
if (error) if (error)
break; return (-error);
/* /*
* update sensor's current value. * Battery charge and Indicator types do not
* need the remaining objects, so skip them.
*/ */
error = sme_sensor_upint32(dict, if (edata->units == ENVSYS_INDICATOR ||
"cur-value",
edata->value_cur);
if (error)
break;
/*
* Battery charge, Integer and Indicator types do not
* need the following objects, so skip them.
*/
if (edata->units == ENVSYS_INTEGER ||
edata->units == ENVSYS_INDICATOR ||
edata->units == ENVSYS_BATTERY_CHARGE) edata->units == ENVSYS_BATTERY_CHARGE)
continue; return error;
/* /*
* update sensor flags. * update sensor flags.
*/ */
if (edata->flags & ENVSYS_FPERCENT) { if (edata->flags & ENVSYS_FPERCENT) {
error = sme_sensor_upbool(dict, error = sme_sensor_upbool(dict, "want-percentage", true);
"want-percentage",
true);
if (error) if (error)
break; return error;
} }
if (value_update) {
/*
* update sensor's current value.
*/
error = sme_sensor_upint32(dict, "cur-value", edata->value_cur);
if (error)
return error;
/* /*
* update sensor's {max,min}-value. * update sensor's {max,min}-value.
*/ */
if (edata->flags & ENVSYS_FVALID_MAX) { if (edata->flags & ENVSYS_FVALID_MAX) {
error = sme_sensor_upint32(dict, error = sme_sensor_upint32(dict, "max-value",
"max-value",
edata->value_max); edata->value_max);
if (error) if (error)
break; return error;
} }
if (edata->flags & ENVSYS_FVALID_MIN) { if (edata->flags & ENVSYS_FVALID_MIN) {
error = sme_sensor_upint32(dict, error = sme_sensor_upint32(dict, "min-value",
"min-value",
edata->value_min); edata->value_min);
if (error) if (error)
break; return error;
} }
/* /*
* update 'rpms' only for ENVSYS_SFANRPM sensors. * update 'rpms' only for ENVSYS_SFANRPM sensors.
*/ */
if (edata->units == ENVSYS_SFANRPM) { if (edata->units == ENVSYS_SFANRPM) {
error = sme_sensor_upuint32(dict, error = sme_sensor_upuint32(dict, "rpms", edata->rpms);
"rpms",
edata->rpms);
if (error) if (error)
break; return error;
} }
/* /*
@ -1714,11 +1611,10 @@ sme_update_dictionary(struct sysmon_envsys *sme)
*/ */
if (edata->units == ENVSYS_SVOLTS_AC || if (edata->units == ENVSYS_SVOLTS_AC ||
edata->units == ENVSYS_SVOLTS_DC) { edata->units == ENVSYS_SVOLTS_DC) {
error = sme_sensor_upint32(dict, error = sme_sensor_upint32(dict, "rfact", edata->rfact);
"rfact",
edata->rfact);
if (error) if (error)
break; return error;
}
} }
/* /*
@ -1727,10 +1623,9 @@ sme_update_dictionary(struct sysmon_envsys *sme)
if (edata->units == ENVSYS_DRIVE) { if (edata->units == ENVSYS_DRIVE) {
sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES, sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES,
edata->value_cur); edata->value_cur);
error = sme_sensor_upstring(dict, "drive-state", error = sme_sensor_upstring(dict, "drive-state", sdt->desc);
sdt->desc);
if (error) if (error)
break; return error;
} }
/* /*
@ -1743,8 +1638,7 @@ sme_update_dictionary(struct sysmon_envsys *sme)
error = sme_sensor_upstring(dict, "battery-capacity", error = sme_sensor_upstring(dict, "battery-capacity",
sdt->desc); sdt->desc);
if (error) if (error)
break; return error;
}
} }
return error; return error;

View File

@ -1,4 +1,4 @@
/* $NetBSD: sysmon_envsysvar.h,v 1.38 2010/12/30 03:59:59 pgoyette Exp $ */ /* $NetBSD: sysmon_envsysvar.h,v 1.39 2011/06/08 18:22:24 pgoyette Exp $ */
/*- /*-
* Copyright (c) 2007, 2008 Juan Romero Pardines. * Copyright (c) 2007, 2008 Juan Romero Pardines.
@ -111,6 +111,7 @@ LIST_HEAD(, sysmon_envsys) sysmon_envsys_list;
* functions to handle sysmon envsys devices. * functions to handle sysmon envsys devices.
*/ */
int sme_update_dictionary(struct sysmon_envsys *); int sme_update_dictionary(struct sysmon_envsys *);
int sme_update_sensor_dictionary(prop_object_t, envsys_data_t *, bool);
int sme_userset_dictionary(struct sysmon_envsys *, int sme_userset_dictionary(struct sysmon_envsys *,
prop_dictionary_t, prop_array_t); prop_dictionary_t, prop_array_t);
prop_dictionary_t sme_sensor_dictionary_get(prop_array_t, const char *); prop_dictionary_t sme_sensor_dictionary_get(prop_array_t, const char *);