Add a new sensor type to sysmon_envsys(9): ENVSYS_GSTRING.

ENVSYS_GSTRING (aka Generic String) uses the genstr member in
the envsys_data_t struct to add a generic string that envstat(8)
will show as value or state.

It's like the ENVSYS_DRIVER, but doesn't use value_cur. Below
is the dictionary created on these sensors:

<dict>
	<key>description</key>
	<string>acpibat0 charge state</string>
	<key>generic-state-string</key>
	<string>NORMAL</string>
	<key>monitoring-supported</key>
	<false/>
	<key>state</key>
	<string>valid</string>
	<key>type</key>
	<string>Generic string</string>
</dict>

Note that it's limited to 32 chars, but we can grow it if needed.

envstat(8) will print ENVSYS_GSTRING sensors as:

$ envstat -dacpibat0 -s"acpibat0 charge state"
  acpibat0 charge state:     NORMAL
$
This commit is contained in:
xtraeme 2007-09-02 19:36:59 +00:00
parent 12d3047f85
commit 1b3709ecde
3 changed files with 85 additions and 28 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: sysmon_envsys.c,v 1.53 2007/09/01 13:43:10 xtraeme Exp $ */
/* $NetBSD: sysmon_envsys.c,v 1.54 2007/09/02 19:36:59 xtraeme Exp $ */
/*-
* Copyright (c) 2007 The NetBSD Foundation, Inc.
@ -75,7 +75,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.53 2007/09/01 13:43:10 xtraeme Exp $");
__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.54 2007/09/02 19:36:59 xtraeme Exp $");
#include <sys/param.h>
#include <sys/types.h>
@ -113,6 +113,7 @@ static const struct sme_sensor_type sme_sensor_type[] = {
{ ENVSYS_INDICATOR, PENVSYS_TYPE_INDICATOR, "Indicator" },
{ ENVSYS_INTEGER, PENVSYS_TYPE_INDICATOR, "Integer" },
{ ENVSYS_DRIVE, PENVSYS_TYPE_DRIVE, "Drive" },
{ ENVSYS_GSTRING, -1, "Generic string" },
{ -1, -1, "unknown" }
};
@ -810,6 +811,51 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
if (sme_sensor_upstring(dict, "state", ess[j].desc))
goto invalidate_sensor;
/*
* Add the monitoring boolean object:
*
* ...
* <key>monitoring-supported</key>
* <true/>
* ...
*
* always false on Drive, Generic strings and Indicator types.
* They cannot be monitored.
*
*/
if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
(edata->units == ENVSYS_INDICATOR) ||
(edata->units == ENVSYS_DRIVE) ||
(edata->units == ENVSYS_GSTRING)) {
if (sme_sensor_upbool(dict, "monitoring-supported", false))
return;
} else {
if (sme_sensor_upbool(dict, "monitoring-supported", true))
return;
}
/*
* Add the generic-state-string object for gstring sensors:
*
* ...
* <key>generic-state-string</key>
* <string>NORMAL</string>
* ...
*/
if (edata->units == ENVSYS_GSTRING) {
if (strlen(edata->genstr) == 0) {
DPRINTF(("%s: invalid generic state string for "
"sensor=%s\n", __func__, edata->desc));
goto invalidate_sensor;
}
if (sme_sensor_upstring(dict,
"generic-state-string",
edata->genstr))
goto invalidate_sensor;
goto add_sensor;
}
/*
* add the percentage boolean object:
*
@ -822,28 +868,6 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
if (sme_sensor_upbool(dict, "want-percentage", true))
return;
/*
* Add the monitoring boolean object:
*
* ...
* <key>monitoring-supported</key>
* <true/>
* ...
*
* always false on Drive and Indicator types, they
* cannot be monitored.
*
*/
if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
(edata->units == ENVSYS_INDICATOR) ||
(edata->units == ENVSYS_DRIVE)) {
if (sme_sensor_upbool(dict, "monitoring-supported", false))
return;
} else {
if (sme_sensor_upbool(dict, "monitoring-supported", true))
return;
}
/*
* Add the drive-state object for drive sensors:
*
@ -861,7 +885,9 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
return;
}
/* if sensor is enabled, add the following properties... */
/*
* if sensor is enabled, add the following properties...
*/
if (edata->state == ENVSYS_SVALID) {
/*
* ...
@ -921,6 +947,7 @@ sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
*
*/
add_sensor:
if (!prop_array_set(array, sme->sme_uniqsensors - 1, dict)) {
DPRINTF(("%s: prop_array_add\n", __func__));
goto invalidate_sensor;
@ -1036,6 +1063,22 @@ sme_update_dictionary(struct sysmon_envsys *sme)
if (error)
break;
/*
* Update the generic-state-string object for gstring
* sensors, they only need the following objects:
*
* description, generic-state-string, state and type.
*
*/
if (edata->units == ENVSYS_GSTRING) {
error = sme_sensor_upstring(dict,
"generic-state-string",
edata->genstr);
if (error)
break;
continue;
}
/* update sensor current value */
error = sme_sensor_upint32(dict,
"cur-value",

View File

@ -1,4 +1,4 @@
/* $NetBSD: envsys.h,v 1.14 2007/07/22 18:17:02 xtraeme Exp $ */
/* $NetBSD: envsys.h,v 1.15 2007/09/02 19:36:59 xtraeme Exp $ */
/*-
* Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
@ -67,6 +67,7 @@ struct envsys_data {
int32_t value_avg; /* avg value */
bool monitor; /* monitoring enabled/disabled */
char desc[ENVSYS_DESCLEN]; /* sensor description */
char genstr[ENVSYS_DESCLEN]; /* generic sensor string */
};
typedef struct envsys_data envsys_data_t;
@ -85,6 +86,7 @@ enum envsys_units {
ENVSYS_INDICATOR, /* Indicator */
ENVSYS_INTEGER, /* Integer */
ENVSYS_DRIVE, /* Drive */
ENVSYS_GSTRING, /* Generic string */
ENVSYS_NSENSORS
};

View File

@ -1,4 +1,4 @@
/* $NetBSD: envstat.c,v 1.43 2007/08/29 16:55:17 xtraeme Exp $ */
/* $NetBSD: envstat.c,v 1.44 2007/09/02 19:36:59 xtraeme Exp $ */
/*-
* Copyright (c) 2007 The NetBSD Foundation, Inc.
@ -87,6 +87,7 @@ struct envsys_sensor {
char desc[ENVSYS_DESCLEN];
char type[ENVSYS_DESCLEN];
char drvstate[ENVSYS_DESCLEN];
char genstr[ENVSYS_DESCLEN];
};
static int interval, flags, width;
@ -660,6 +661,13 @@ find_sensors(prop_array_t array)
prop_string_cstring_nocopy(obj1),
sizeof(gesen[gnelems].drvstate));
/* get current generic state string */
obj1 = prop_dictionary_get(obj, "generic-state-string");
if (obj1 != NULL)
(void)strlcpy(gesen[gnelems].genstr,
prop_string_cstring_nocopy(obj1),
sizeof(gesen[gnelems].genstr));
/* get current value */
obj1 = prop_dictionary_get(obj, "cur-value");
gesen[gnelems].cur_value = prop_number_integer_value(obj1);
@ -822,8 +830,12 @@ print_sensors(struct envsys_sensor *es, size_t nelems)
(void)printf(": %10s\n", invalid);
continue;
}
if (strcmp(es[i].type, "Generic string") == 0) {
if (strcmp(es[i].type, "Indicator") == 0) {
(void)printf(": %10s", es[i].genstr);
} else if (strcmp(es[i].type, "Indicator") == 0) {
(void)printf(": %10s", es[i].cur_value ? "ON" : "OFF");