diff options
| author | xtraeme <xtraeme@NetBSD.org> | 2007-11-03 23:05:21 +0000 |
|---|---|---|
| committer | xtraeme <xtraeme@NetBSD.org> | 2007-11-03 23:05:21 +0000 |
| commit | 4e10a8485581088a3d9789854873d07cc95aec10 (patch) | |
| tree | b117617091e4e509da2ee7b588a6343cdf2ecd06 /sys/dev/sysmon | |
| parent | ad0f60a40b6ba7618f08267afba39ddc118c205e (diff) | |
- Rename ENVSYS_BATTERY_STATE units to ENVSYS_BATTERY_CAPACITY and
introduce ENVSYS_BATTERY_CHARGE, which is the same than an Indicator and
it's used to know if the battery is currently charging or discharging.
- Require two sensors at least for SME_CLASS_BATTERY to make the low-power
condition work: a ENVSYS_BATTERY_CAPACITY plus ENVSYS_BATTERY_CHARGE.
- Simplify sme_event_check_lowpower() and make it check the required
sensors in the SME_CLASS_ACADAPTER and SME_CLASS_BATTERY classes.
If the acadapter is not ready, trust the state returned by the battery
device.
Based on suggestion from joerg@.
Diffstat (limited to 'sys/dev/sysmon')
| -rw-r--r-- | sys/dev/sysmon/sysmon_envsys.c | 40 | ||||
| -rw-r--r-- | sys/dev/sysmon/sysmon_envsys_events.c | 93 | ||||
| -rw-r--r-- | sys/dev/sysmon/sysmon_envsys_tables.c | 24 | ||||
| -rw-r--r-- | sys/dev/sysmon/sysmon_envsysvar.h | 5 |
4 files changed, 90 insertions, 72 deletions
diff --git a/sys/dev/sysmon/sysmon_envsys.c b/sys/dev/sysmon/sysmon_envsys.c index f8ae7623ac5..316f5430116 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.70 2007/11/02 19:21:29 plunky Exp $ */ +/* $NetBSD: sysmon_envsys.c,v 1.71 2007/11/03 23:05:21 xtraeme Exp $ */ /*- * Copyright (c) 2007 Juan Romero Pardines. @@ -64,7 +64,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.70 2007/11/02 19:21:29 plunky Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.71 2007/11/03 23:05:21 xtraeme Exp $"); #include <sys/param.h> #include <sys/types.h> @@ -382,6 +382,9 @@ sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) tred->min.data_s = edata->value_min; tred->avg.data_us = edata->value_avg; tred->avg.data_s = edata->value_avg; + if (edata->units == ENVSYS_BATTERY_CHARGE) + tred->units = ENVSYS_INDICATOR; + else tred->units = edata->units; tred->validflags |= ENVSYS_FVALID; @@ -429,6 +432,9 @@ sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) binfo->validflags |= ENVSYS_FVALID; if (binfo->sensor < sme->sme_nsensors) { + if (edata->units == ENVSYS_BATTERY_CHARGE) + binfo->units = ENVSYS_INDICATOR; + else binfo->units = edata->units; /* @@ -1000,14 +1006,15 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, * <true/> * ... * - * always false on Battery state, Drive and Indicator types. + * always false on Battery {capacity,charge}, Drive and Indicator types. * They cannot be monitored. * */ if ((edata->flags & ENVSYS_FMONNOTSUPP) || (edata->units == ENVSYS_INDICATOR) || (edata->units == ENVSYS_DRIVE) || - (edata->units == ENVSYS_BATTERY_STATE)) { + (edata->units == ENVSYS_BATTERY_CAPACITY) || + (edata->units == ENVSYS_BATTERY_CHARGE)) { if (sme_sensor_upbool(dict, "monitoring-supported", false)) goto out; } else { @@ -1050,20 +1057,20 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, /* - * Add the battery-state object for battery state sensors: + * Add the object for battery capacity sensors: * * ... - * <key>battery-state</key> + * <key>battery-capacity</key> * <string>NORMAL</string> * ... */ - if (edata->units == ENVSYS_BATTERY_STATE) { - sdt = sme_get_description_table(SME_DESC_BATTERY_STATES); + if (edata->units == ENVSYS_BATTERY_CAPACITY) { + sdt = sme_get_description_table(SME_DESC_BATTERY_CAPACITY); for (j = 0; sdt[j].type != -1; j++) if (sdt[j].type == edata->value_cur) break; - if (sme_sensor_upstring(dict, "battery-state", sdt[j].desc)) + if (sme_sensor_upstring(dict, "battery-capacity", sdt[j].desc)) goto out; } @@ -1271,11 +1278,12 @@ sme_update_dictionary(struct sysmon_envsys *sme) break; /* - * Integer and Indicator types do not the following - * values, so skip them. + * 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_INDICATOR || + edata->units == ENVSYS_BATTERY_CHARGE) continue; /* update sensor flags */ @@ -1344,16 +1352,16 @@ sme_update_dictionary(struct sysmon_envsys *sme) break; } - /* update 'battery-state' only in ENVSYS_BATTERY_STATE. */ - if (edata->units == ENVSYS_BATTERY_STATE) { + /* update 'battery-capacity' only in ENVSYS_BATTERY_CAPACITY. */ + if (edata->units == ENVSYS_BATTERY_CAPACITY) { sdt = - sme_get_description_table(SME_DESC_BATTERY_STATES); + sme_get_description_table(SME_DESC_BATTERY_CAPACITY); for (j = 0; sdt[j].type != -1; j++) if (sdt[j].type == edata->value_cur) break; error = sme_sensor_upstring(dict, - "battery-state", + "battery-capacity", sdt[j].desc); if (error) break; diff --git a/sys/dev/sysmon/sysmon_envsys_events.c b/sys/dev/sysmon/sysmon_envsys_events.c index e9aede046aa..f10de5f9955 100644 --- a/sys/dev/sysmon/sysmon_envsys_events.c +++ b/sys/dev/sysmon/sysmon_envsys_events.c @@ -1,4 +1,4 @@ -/* $NetBSD: sysmon_envsys_events.c,v 1.43 2007/10/23 21:36:03 xtraeme Exp $ */ +/* $NetBSD: sysmon_envsys_events.c,v 1.44 2007/11/03 23:05:21 xtraeme Exp $ */ /*- * Copyright (c) 2007 Juan Romero Pardines. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.43 2007/10/23 21:36:03 xtraeme Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.44 2007/11/03 23:05:21 xtraeme Exp $"); #include <sys/param.h> #include <sys/types.h> @@ -75,7 +75,8 @@ static int sme_events_timeout = 10; static int sme_events_timeout_sysctl(SYSCTLFN_PROTO); #define SME_EVTIMO (sme_events_timeout * hz) -static int sme_event_check_low_power(void); +static bool sme_event_check_low_power(void); +static bool sme_battery_critical(envsys_data_t *); /* * sysctl(9) stuff to handle the refresh value in the callout @@ -598,10 +599,10 @@ do { \ sdt = sme_get_description_table(SME_DESC_DRIVE_STATES); state = ENVSYS_DRIVE_ONLINE; break; - case ENVSYS_BATTERY_STATE: + case ENVSYS_BATTERY_CAPACITY: sdt = - sme_get_description_table(SME_DESC_BATTERY_STATES); - state = ENVSYS_BATTERY_STATE_NORMAL; + sme_get_description_table(SME_DESC_BATTERY_CAPACITY); + state = ENVSYS_BATTERY_CAPACITY_NORMAL; break; default: panic("%s: invalid units for ENVSYS_FMONSTCHANGED", @@ -649,19 +650,18 @@ out: mutex_exit(&sme_mtx); } -static int +static bool sme_event_check_low_power(void) { struct sysmon_envsys *sme; envsys_data_t *edata; - int i, batteries_cnt, batteries_discharged; - bool acadapter_on, acadapter_sensor_found; - - acadapter_on = acadapter_sensor_found = false; - batteries_cnt = batteries_discharged = 0; + int i; + bool battery, batterycap, batterycharge; KASSERT(mutex_owned(&sme_mtx)); + battery = batterycap = batterycharge = false; + LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) if (sme->sme_class == SME_CLASS_ACADAPTER) break; @@ -670,50 +670,59 @@ sme_event_check_low_power(void) * No AC Adapter devices were found, do nothing. */ if (!sme) - return 0; + return false; + /* + * If there's an AC Adapter connected, there's no need + * to continue... + */ for (i = 0; i < sme->sme_nsensors; i++) { edata = &sme->sme_sensor_data[i]; if (edata->units == ENVSYS_INDICATOR) { - acadapter_sensor_found = true; - if (edata->value_cur) { - acadapter_on = true; - break; - } + if (edata->value_cur) + return false; } } + /* - * There's an AC Adapter device connected or there wasn't - * any sensor capable of returning its state. + * Check for battery devices and its state. */ - if (acadapter_on || !acadapter_sensor_found) - return 0; - -#define IS_BATTERY_DISCHARGED() \ -do { \ - if (((edata->units == ENVSYS_BATTERY_STATE) && \ - ((edata->value_cur == ENVSYS_BATTERY_STATE_CRITICAL) || \ - (edata->value_cur == ENVSYS_BATTERY_STATE_LOW)))) { \ - batteries_discharged++; \ - break; \ - } \ -} while (/* CONSTCOND */ 0) - LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { - if (sme->sme_class == SME_CLASS_BATTERY) { - batteries_cnt++; - for (i = 0; i < sme->sme_nsensors; i++) { - edata = &sme->sme_sensor_data[i]; - IS_BATTERY_DISCHARGED(); + if (sme->sme_class != SME_CLASS_BATTERY) + continue; + + /* + * We've found a battery device... + */ + battery = true; + for (i = 0; i < sme->sme_nsensors; i++) { + edata = &sme->sme_sensor_data[i]; + if (edata->units == ENVSYS_BATTERY_CAPACITY) { + batterycap = true; + if (!sme_battery_critical(edata)) + return false; + } else if (edata->units == ENVSYS_BATTERY_CHARGE) { + batterycharge = true; + if (edata->value_cur) + return false; } } } + if (!battery || !batterycap || !batterycharge) + return false; /* - * All batteries are discharged? + * All batteries in low/critical capacity and discharging. */ - if (batteries_cnt == batteries_discharged) - return 1; + return true; +} - return 0; +static bool +sme_battery_critical(envsys_data_t *edata) +{ + if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL || + edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW) + return true; + + return false; } diff --git a/sys/dev/sysmon/sysmon_envsys_tables.c b/sys/dev/sysmon/sysmon_envsys_tables.c index a217bfe3190..63dc07ab131 100644 --- a/sys/dev/sysmon/sysmon_envsys_tables.c +++ b/sys/dev/sysmon/sysmon_envsys_tables.c @@ -1,4 +1,4 @@ -/* $NetBSD: sysmon_envsys_tables.c,v 1.2 2007/10/07 04:11:15 xtraeme Exp $ */ +/* $NetBSD: sysmon_envsys_tables.c,v 1.3 2007/11/03 23:05:21 xtraeme Exp $ */ /*- * Copyright (c) 2007 Juan Romero Pardines. @@ -26,7 +26,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_tables.c,v 1.2 2007/10/07 04:11:15 xtraeme Exp $"); +__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_tables.c,v 1.3 2007/11/03 23:05:21 xtraeme Exp $"); #include <sys/types.h> @@ -49,7 +49,8 @@ static const struct sme_description_table sme_units_description[] = { { ENVSYS_INDICATOR, PENVSYS_TYPE_INDICATOR, "Indicator" }, { ENVSYS_INTEGER, PENVSYS_TYPE_INDICATOR, "Integer" }, { ENVSYS_DRIVE, PENVSYS_TYPE_DRIVE, "Drive" }, - { ENVSYS_BATTERY_STATE, PENVSYS_TYPE_BATTERY, "Battery state" }, + { ENVSYS_BATTERY_CAPACITY, PENVSYS_TYPE_BATTERY,"Battery capacity" }, + { ENVSYS_BATTERY_CHARGE, -1, "Battery charge" }, { -1, -1, "unknown" } }; @@ -85,13 +86,13 @@ static const struct sme_description_table sme_drivestate_description[] = { }; /* - * Available battery state descriptions. + * Available battery capacity descriptions. */ -static const struct sme_description_table sme_batterystate_description[] = { - { ENVSYS_BATTERY_STATE_NORMAL, -1, "NORMAL" }, - { ENVSYS_BATTERY_STATE_WARNING, -1, "WARNING" }, - { ENVSYS_BATTERY_STATE_CRITICAL, -1, "CRITICAL" }, - { ENVSYS_BATTERY_STATE_LOW, -1, "LOW" }, +static const struct sme_description_table sme_batterycap_description[] = { + { ENVSYS_BATTERY_CAPACITY_NORMAL, -1, "NORMAL" }, + { ENVSYS_BATTERY_CAPACITY_WARNING, -1, "WARNING" }, + { ENVSYS_BATTERY_CAPACITY_CRITICAL, -1, "CRITICAL" }, + { ENVSYS_BATTERY_CAPACITY_LOW, -1, "LOW" }, { -1, -1, "UNKNOWN" } }; @@ -104,8 +105,7 @@ sme_get_description_table(int type) const struct sme_description_table *ud = sme_units_description; const struct sme_description_table *sd = sme_state_description; const struct sme_description_table *dd = sme_drivestate_description; - const struct sme_description_table *bd = sme_batterystate_description; - + const struct sme_description_table *bd = sme_batterycap_description; switch (type) { case SME_DESC_UNITS: return ud; @@ -113,7 +113,7 @@ sme_get_description_table(int type) return sd; case SME_DESC_DRIVE_STATES: return dd; - case SME_DESC_BATTERY_STATES: + case SME_DESC_BATTERY_CAPACITY: return bd; default: return NULL; diff --git a/sys/dev/sysmon/sysmon_envsysvar.h b/sys/dev/sysmon/sysmon_envsysvar.h index 6c45b77f9e3..da5c42822e5 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.22 2007/10/23 21:36:03 xtraeme Exp $ */ +/* $NetBSD: sysmon_envsysvar.h,v 1.23 2007/11/03 23:05:22 xtraeme Exp $ */ /*- * Copyright (c) 2007 Juan Romero Pardines. @@ -44,7 +44,7 @@ enum sme_description_types { SME_DESC_UNITS = 1, SME_DESC_STATES, SME_DESC_DRIVE_STATES, - SME_DESC_BATTERY_STATES + SME_DESC_BATTERY_CAPACITY }; #ifdef ENVSYS_DEBUG @@ -114,6 +114,7 @@ int sme_update_dictionary(struct sysmon_envsys *); 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 *); +struct sysmon_envsys *sysmon_envsys_find(const char *); /* functions to handle sysmon envsys events */ int sme_event_register(prop_dictionary_t, envsys_data_t *, const char *, |
