Now that my upd(4) is working (thanks to all involved), I’m looking to improve
behavior a bit. Let’s add some state transitions to the sensors.
Feedback is welcome.
--david
## before
[vm@vm usb]$ sysctl hw.sensors.upd0
hw.sensors.upd0.indicator0=Off (Charging), OK
hw.sensors.upd0.indicator1=Off (Discharging), OK
hw.sensors.upd0.indicator2=On (ACPresent), OK
hw.sensors.upd0.indicator3=Off (ShutdownImminent), OK
hw.sensors.upd0.indicator4=On (BatteryPresent), OK
hw.sensors.upd0.percent0=100.00% (RemainingCapacity), OK
hw.sensors.upd0.percent1=100.00% (FullChargeCapacity), OK
## AC power loss
[vm@vm usb]$ sysctl hw.sensors.upd0
hw.sensors.upd0.indicator0=Off (Charging), OK
hw.sensors.upd0.indicator1=On (Discharging), WARNING
hw.sensors.upd0.indicator2=Off (ACPresent), WARNING
hw.sensors.upd0.indicator3=Off (ShutdownImminent), OK
hw.sensors.upd0.indicator4=On (BatteryPresent), OK
hw.sensors.upd0.percent0=95.00% (RemainingCapacity), OK
hw.sensors.upd0.percent1=100.00% (FullChargeCapacity), OK
## AC power restored
[vm@vm usb]$ sysctl hw.sensors.upd0
hw.sensors.upd0.indicator0=On (Charging), WARNING
hw.sensors.upd0.indicator1=Off (Discharging), OK
hw.sensors.upd0.indicator2=On (ACPresent), OK
hw.sensors.upd0.indicator3=Off (ShutdownImminent), OK
hw.sensors.upd0.indicator4=On (BatteryPresent), OK
hw.sensors.upd0.percent0=97.00% (RemainingCapacity), OK
hw.sensors.upd0.percent1=100.00% (FullChargeCapacity), OK
Index: upd.c
===================================================================
RCS file: /cvs/src/sys/dev/usb/upd.c,v
retrieving revision 1.12
diff -u -p -r1.12 upd.c
--- upd.c 11 Dec 2014 18:50:32 -0000 1.12
+++ upd.c 12 Dec 2014 00:32:09 -0000
@@ -324,6 +324,7 @@ upd_update_sensors(struct upd_softc *sc,
struct upd_sensor *sensor;
ulong hdata, batpres;
ulong adjust;
+ enum sensor_status status;
int i;
sensor = upd_lookup_sensor(sc, HUP_BATTERY, HUB_BATTERY_PRESENT);
@@ -346,6 +347,10 @@ upd_update_sensors(struct upd_softc *sc,
}
}
+ adjust = 1;
+ status = SENSOR_S_OK;
+ hdata = hid_get_data(buf, len, &sensor->hitem.loc);
+
switch (HID_GET_USAGE(sensor->hitem.usage)) {
case HUB_REL_STATEOF_CHARGE:
case HUB_ABS_STATEOF_CHARGE:
@@ -353,15 +358,28 @@ upd_update_sensors(struct upd_softc *sc,
case HUB_FULLCHARGE_CAPACITY:
adjust = 1000; /* scale adjust */
break;
+ case HUB_CHARGING:
+ case HUB_DISCHARGING:
+ if (hdata)
+ status = SENSOR_S_WARN;
+ break;
+ case HUB_BATTERY_PRESENT:
+ if (!hdata)
+ status = SENSOR_S_CRIT;
+ break;
+ case HUP_SHUTDOWN_IMMINENT:
+ if (hdata)
+ status = SENSOR_S_CRIT;
+ break;
+ case HUB_AC_PRESENT:
+ if (!hdata)
+ status = SENSOR_S_WARN;
default:
- adjust = 1; /* no scale adjust */
break;
}
- hdata = hid_get_data(buf, len, &sensor->hitem.loc);
-
sensor->ksensor.value = hdata * adjust;
- sensor->ksensor.status = SENSOR_S_OK;
+ sensor->ksensor.status = status;
sensor->ksensor.flags &= ~SENSOR_FINVALID;
DPRINTF(("%s: hidget data: %lu\n",
sc->sc_sensordev.xname, hdata));