From 35a3b0e4a6fe034fcf8f91a8fb3a195be4211275 Mon Sep 17 00:00:00 2001 From: pgoyette Date: Wed, 8 Jun 2011 18:22:24 +0000 Subject: [PATCH] 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. --- sys/dev/sysmon/sysmon_envsys.c | 294 ++++++++++-------------------- sys/dev/sysmon/sysmon_envsysvar.h | 3 +- 2 files changed, 96 insertions(+), 201 deletions(-) diff --git a/sys/dev/sysmon/sysmon_envsys.c b/sys/dev/sysmon/sysmon_envsys.c index d001e6913b37..46c08d112b52 100644 --- a/sys/dev/sysmon/sysmon_envsys.c +++ b/sys/dev/sysmon/sysmon_envsys.c @@ -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. @@ -64,7 +64,7 @@ */ #include -__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 #include @@ -1263,16 +1263,11 @@ static sme_event_drv_t * sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, 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_drive; + const struct sme_descr_entry *sdt; + int error; sme_event_drv_t *sme_evdrv_t = NULL; 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. * @@ -1287,34 +1282,13 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, /* * ... - * type - * foo * description * blah blah * ... */ - if (sme_sensor_upstring(dict, "type", sdt_units->desc)) - goto bad; - if (sme_sensor_upstring(dict, "description", edata->desc)) goto bad; - /* - * Add sensor's state description. - * - * ... - * state - * valid - * ... - */ - 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: * @@ -1339,22 +1313,9 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, goto out; } - /* - * Add the percentage boolean object, true if ENVSYS_FPERCENT - * is set or false otherwise. - * - * ... - * want-percentage - * - * ... - */ - if (edata->flags & ENVSYS_FPERCENT) - if (sme_sensor_upbool(dict, "want-percentage", true)) - goto out; - /* * Add the allow-rfact boolean object, true if - * ENVSYS_FCHANGERFACT if set or false otherwise. + * ENVSYS_FCHANGERFACT is set, false otherwise. * * ... * allow-rfact @@ -1372,83 +1333,12 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, } } - /* - * Add the object for battery capacity sensors: - * - * ... - * battery-capacity - * NORMAL - * ... - */ - 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; - } - - /* - * Add the drive-state object for drive sensors: - * - * ... - * drive-state - * drive is online - * ... - */ - 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: - * - * ... - * rpms - * 2500 - * rfact - * 10000 - * cur-value - * 1250 - * min-value - * 800 - * max-value - * 3000 - * ... - */ - 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; - } - } + error = sme_update_sensor_dictionary(dict, edata, + (edata->state == ENVSYS_SVALID)); + if (error < 0) + goto bad; + else if (error) + 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_edata = edata; 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: @@ -1555,7 +1446,6 @@ sme_get_max_value(struct sysmon_envsys *sme, int sme_update_dictionary(struct sysmon_envsys *sme) { - const struct sme_descr_entry *sdt; envsys_data_t *edata; prop_object_t array, dict, obj, obj2; int error = 0; @@ -1628,85 +1518,92 @@ sme_update_dictionary(struct sysmon_envsys *sme) /* * update sensor's state. */ - sdt = sme_find_table_entry(SME_DESC_STATES, edata->state); + error = sme_update_sensor_dictionary(dict, edata, true); - DPRINTFOBJ(("%s: sensor #%d type=%d (%s) flags=%d\n", - __func__, edata->sensor, sdt->type, sdt->desc, - edata->flags)); - - error = sme_sensor_upstring(dict, "state", sdt->desc); if (error) break; + } - /* - * update sensor's type. - */ - sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units); + return error; +} - DPRINTFOBJ(("%s: sensor #%d units=%d (%s)\n", - __func__, edata->sensor, sdt->type, sdt->desc)); +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; - error = sme_sensor_upstring(dict, "type", sdt->desc); + sdt = sme_find_table_entry(SME_DESC_STATES, edata->state); + + DPRINTFOBJ(("%s: sensor #%d type=%d (%s) flags=%d\n", __func__, + edata->sensor, sdt->type, sdt->desc, edata->flags)); + + error = sme_sensor_upstring(dict, "state", sdt->desc); + if (error) + return (-error); + + /* + * update sensor's type. + */ + sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units); + + DPRINTFOBJ(("%s: sensor #%d units=%d (%s)\n", __func__, edata->sensor, + sdt->type, sdt->desc)); + + error = sme_sensor_upstring(dict, "type", sdt->desc); + if (error) + return (-error); + + /* + * Battery charge and Indicator types do not + * need the remaining objects, so skip them. + */ + if (edata->units == ENVSYS_INDICATOR || + edata->units == ENVSYS_BATTERY_CHARGE) + return error; + + /* + * update sensor flags. + */ + if (edata->flags & ENVSYS_FPERCENT) { + error = sme_sensor_upbool(dict, "want-percentage", true); if (error) - break; + return error; + } + if (value_update) { /* * update sensor's current value. */ - error = sme_sensor_upint32(dict, - "cur-value", - edata->value_cur); + error = sme_sensor_upint32(dict, "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) - continue; - - /* - * update sensor flags. - */ - if (edata->flags & ENVSYS_FPERCENT) { - error = sme_sensor_upbool(dict, - "want-percentage", - true); - if (error) - break; - } + return error; /* * update sensor's {max,min}-value. */ if (edata->flags & ENVSYS_FVALID_MAX) { - error = sme_sensor_upint32(dict, - "max-value", + error = sme_sensor_upint32(dict, "max-value", edata->value_max); if (error) - break; + return error; } - + if (edata->flags & ENVSYS_FVALID_MIN) { - error = sme_sensor_upint32(dict, - "min-value", + error = sme_sensor_upint32(dict, "min-value", edata->value_min); if (error) - break; + return error; } /* * update 'rpms' only for ENVSYS_SFANRPM sensors. */ if (edata->units == ENVSYS_SFANRPM) { - error = sme_sensor_upuint32(dict, - "rpms", - edata->rpms); + error = sme_sensor_upuint32(dict, "rpms", edata->rpms); if (error) - break; + return error; } /* @@ -1714,37 +1611,34 @@ sme_update_dictionary(struct sysmon_envsys *sme) */ if (edata->units == ENVSYS_SVOLTS_AC || edata->units == ENVSYS_SVOLTS_DC) { - error = sme_sensor_upint32(dict, - "rfact", - edata->rfact); + error = sme_sensor_upint32(dict, "rfact", edata->rfact); if (error) - break; - } - - /* - * update 'drive-state' only for ENVSYS_DRIVE sensors. - */ - if (edata->units == ENVSYS_DRIVE) { - sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES, - edata->value_cur); - error = sme_sensor_upstring(dict, "drive-state", - sdt->desc); - if (error) - break; + return error; } + } - /* - * update 'battery-capacity' only for ENVSYS_BATTERY_CAPACITY - * sensors. - */ - if (edata->units == ENVSYS_BATTERY_CAPACITY) { - sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY, - edata->value_cur); - error = sme_sensor_upstring(dict, "battery-capacity", - sdt->desc); - if (error) - break; - } + /* + * update 'drive-state' only for ENVSYS_DRIVE sensors. + */ + if (edata->units == ENVSYS_DRIVE) { + sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES, + edata->value_cur); + error = sme_sensor_upstring(dict, "drive-state", sdt->desc); + if (error) + return error; + } + + /* + * update 'battery-capacity' only for ENVSYS_BATTERY_CAPACITY + * sensors. + */ + if (edata->units == ENVSYS_BATTERY_CAPACITY) { + sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY, + edata->value_cur); + error = sme_sensor_upstring(dict, "battery-capacity", + sdt->desc); + if (error) + return error; } return error; diff --git a/sys/dev/sysmon/sysmon_envsysvar.h b/sys/dev/sysmon/sysmon_envsysvar.h index 4b993cd164a5..66eec06645fc 100644 --- a/sys/dev/sysmon/sysmon_envsysvar.h +++ b/sys/dev/sysmon/sysmon_envsysvar.h @@ -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. @@ -111,6 +111,7 @@ LIST_HEAD(, sysmon_envsys) sysmon_envsys_list; * functions to handle sysmon envsys devices. */ 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 *, prop_dictionary_t, prop_array_t); prop_dictionary_t sme_sensor_dictionary_get(prop_array_t, const char *);