xref: /netbsd-src/sys/dev/sysmon/sysmon_envsys_tables.c (revision aad9773e38ed2370a628a6416e098f9008fc10a7)
1 /* $NetBSD: sysmon_envsys_tables.c,v 1.12 2014/05/18 11:46:23 kardel Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 Juan Romero Pardines.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_tables.c,v 1.12 2014/05/18 11:46:23 kardel Exp $");
30 
31 #include <sys/types.h>
32 
33 #include <dev/sysmon/sysmonvar.h>
34 #include <dev/sysmon/sysmon_envsysvar.h>
35 
36 /*
37  * Available units type descriptions.
38  */
39 static const struct sme_descr_entry sme_units_description[] = {
40 	{ ENVSYS_STEMP,		PENVSYS_TYPE_TEMP,	"Temperature" },
41 	{ ENVSYS_SFANRPM,	PENVSYS_TYPE_FAN,	"Fan" },
42 	{ ENVSYS_SVOLTS_AC,	PENVSYS_TYPE_VOLTAGE,	"Voltage AC" },
43 	{ ENVSYS_SVOLTS_DC,	PENVSYS_TYPE_VOLTAGE,	"Voltage DC" },
44 	{ ENVSYS_SOHMS,		PENVSYS_TYPE_RESISTANCE,"Ohms" },
45 	{ ENVSYS_SWATTS,	PENVSYS_TYPE_POWER,	"Watts" },
46 	{ ENVSYS_SAMPS,		PENVSYS_TYPE_POWER,	"Ampere" },
47 	{ ENVSYS_SWATTHOUR,	PENVSYS_TYPE_BATTERY,	"Watt hour" },
48 	{ ENVSYS_SAMPHOUR,	PENVSYS_TYPE_BATTERY,	"Ampere hour" },
49 	{ ENVSYS_INDICATOR,	PENVSYS_TYPE_INDICATOR,	"Indicator" },
50 	{ ENVSYS_INTEGER,	PENVSYS_TYPE_INDICATOR,	"Integer" },
51 	{ ENVSYS_DRIVE,		PENVSYS_TYPE_DRIVE,	"Drive" },
52 	{ ENVSYS_BATTERY_CAPACITY, PENVSYS_TYPE_BATTERY,"Battery capacity" },
53 	{ ENVSYS_BATTERY_CHARGE, -1,			"Battery charge" },
54 	{ ENVSYS_SRELHUMIDITY,	-1,			"relative Humidity" },
55 	{ -1,			-1,			"unknown" }
56 };
57 
58 /*
59  * Available sensor state descriptions.
60  */
61 static const struct sme_descr_entry sme_state_description[] = {
62 	{ ENVSYS_SVALID,	-1, 	"valid" },
63 	{ ENVSYS_SINVALID,	-1, 	"invalid" },
64 	{ ENVSYS_SCRITICAL,	-1, 	"critical" },
65 	{ ENVSYS_SCRITUNDER,	-1, 	"critical-under" },
66 	{ ENVSYS_SCRITOVER,	-1, 	"critical-over" },
67 	{ ENVSYS_SWARNUNDER,	-1, 	"warning-under" },
68 	{ ENVSYS_SWARNOVER,	-1, 	"warning-over" },
69 	{ -1,			-1, 	"unknown" }
70 };
71 
72 /*
73  * Available drive state descriptions.
74  */
75 static const struct sme_descr_entry sme_drivestate_description[] = {
76 	{ ENVSYS_DRIVE_EMPTY,		-1, 	"unknown" },
77 	{ ENVSYS_DRIVE_READY,		-1, 	"ready" },
78 	{ ENVSYS_DRIVE_POWERUP,		-1, 	"powering up" },
79 	{ ENVSYS_DRIVE_ONLINE,		-1, 	"online" },
80 	{ ENVSYS_DRIVE_OFFLINE, 	-1, 	"offline" },
81 	{ ENVSYS_DRIVE_IDLE,		-1, 	"idle" },
82 	{ ENVSYS_DRIVE_ACTIVE,		-1, 	"active" },
83 	{ ENVSYS_DRIVE_BUILD,		-1,	"building" },
84 	{ ENVSYS_DRIVE_REBUILD,		-1, 	"rebuilding" },
85 	{ ENVSYS_DRIVE_POWERDOWN,	-1, 	"powering down" },
86 	{ ENVSYS_DRIVE_FAIL,		-1, 	"failed" },
87 	{ ENVSYS_DRIVE_PFAIL,		-1, 	"degraded" },
88 	{ ENVSYS_DRIVE_MIGRATING,	-1,	"migrating" },
89 	{ ENVSYS_DRIVE_CHECK,		-1,	"checking" },
90 	{ -1,				-1, 	"unknown" }
91 };
92 
93 /*
94  * Available battery capacity descriptions.
95  */
96 static const struct sme_descr_entry sme_batterycap_description[] = {
97 	{ ENVSYS_BATTERY_CAPACITY_NORMAL,	-1,	"NORMAL" },
98 	{ ENVSYS_BATTERY_CAPACITY_WARNING,	-1, 	"WARNING" },
99 	{ ENVSYS_BATTERY_CAPACITY_CRITICAL,	-1, 	"CRITICAL" },
100 	{ ENVSYS_BATTERY_CAPACITY_LOW,		-1,	"LOW" },
101 	{ -1,					-1, 	"UNKNOWN" }
102 };
103 
104 /*
105  * Available indicator descriptions.
106  */
107 static const struct sme_descr_entry sme_indicator_description[] = {
108 	{ ENVSYS_INDICATOR_FALSE,		-1,	"FALSE" },
109 	{ ENVSYS_INDICATOR_TRUE,		-1, 	"TRUE" },
110 	{ -1,					-1, 	"UNKNOWN" }
111 };
112 
113 static const struct sme_descr_entry *
114 sme_find_table(enum sme_descr_type table_id)
115 {
116 	switch (table_id) {
117 	case SME_DESC_UNITS:
118 		return sme_units_description;
119 		break;
120 	case SME_DESC_STATES:
121 		return sme_state_description;
122 		break;
123 	case SME_DESC_DRIVE_STATES:
124 		return sme_drivestate_description;
125 		break;
126 	case SME_DESC_BATTERY_CAPACITY:
127 		return sme_batterycap_description;
128 		break;
129 	case SME_DESC_INDICATOR:
130 		return sme_indicator_description;
131 		break;
132 	default:
133 		return NULL;
134 	}
135 }
136 
137 /*
138  * Returns the entry from specified table with type == key
139  */
140 const struct sme_descr_entry *
141 sme_find_table_entry(enum sme_descr_type table_id, int key)
142 {
143 	const struct sme_descr_entry *table = sme_find_table(table_id);
144 
145 	if (table != NULL)
146 		for (; table->type != -1; table++)
147 			if (table->type == key)
148 				return table;
149 
150 	return NULL;
151 }
152 
153 const struct sme_descr_entry *
154 sme_find_table_desc(enum sme_descr_type table_id, const char *str)
155 {
156 	const struct sme_descr_entry *table = sme_find_table(table_id);
157 
158 	if (table != NULL)
159 		for (; table->type != -1; table++)
160 			if (strcmp(table->desc, str) == 0)
161 				return table;
162 	return NULL;
163 }
164