xref: /netbsd-src/sys/dev/sysmon/sysmon_envsys_tables.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /* $NetBSD: sysmon_envsys_tables.c,v 1.13 2018/05/27 01:39:00 thorpej 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.13 2018/05/27 01:39:00 thorpej 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 	{ ENVSYS_LUX,		-1,			"Illuminance" },
56 	{ -1,			-1,			"unknown" }
57 };
58 
59 /*
60  * Available sensor state descriptions.
61  */
62 static const struct sme_descr_entry sme_state_description[] = {
63 	{ ENVSYS_SVALID,	-1, 	"valid" },
64 	{ ENVSYS_SINVALID,	-1, 	"invalid" },
65 	{ ENVSYS_SCRITICAL,	-1, 	"critical" },
66 	{ ENVSYS_SCRITUNDER,	-1, 	"critical-under" },
67 	{ ENVSYS_SCRITOVER,	-1, 	"critical-over" },
68 	{ ENVSYS_SWARNUNDER,	-1, 	"warning-under" },
69 	{ ENVSYS_SWARNOVER,	-1, 	"warning-over" },
70 	{ -1,			-1, 	"unknown" }
71 };
72 
73 /*
74  * Available drive state descriptions.
75  */
76 static const struct sme_descr_entry sme_drivestate_description[] = {
77 	{ ENVSYS_DRIVE_EMPTY,		-1, 	"unknown" },
78 	{ ENVSYS_DRIVE_READY,		-1, 	"ready" },
79 	{ ENVSYS_DRIVE_POWERUP,		-1, 	"powering up" },
80 	{ ENVSYS_DRIVE_ONLINE,		-1, 	"online" },
81 	{ ENVSYS_DRIVE_OFFLINE, 	-1, 	"offline" },
82 	{ ENVSYS_DRIVE_IDLE,		-1, 	"idle" },
83 	{ ENVSYS_DRIVE_ACTIVE,		-1, 	"active" },
84 	{ ENVSYS_DRIVE_BUILD,		-1,	"building" },
85 	{ ENVSYS_DRIVE_REBUILD,		-1, 	"rebuilding" },
86 	{ ENVSYS_DRIVE_POWERDOWN,	-1, 	"powering down" },
87 	{ ENVSYS_DRIVE_FAIL,		-1, 	"failed" },
88 	{ ENVSYS_DRIVE_PFAIL,		-1, 	"degraded" },
89 	{ ENVSYS_DRIVE_MIGRATING,	-1,	"migrating" },
90 	{ ENVSYS_DRIVE_CHECK,		-1,	"checking" },
91 	{ -1,				-1, 	"unknown" }
92 };
93 
94 /*
95  * Available battery capacity descriptions.
96  */
97 static const struct sme_descr_entry sme_batterycap_description[] = {
98 	{ ENVSYS_BATTERY_CAPACITY_NORMAL,	-1,	"NORMAL" },
99 	{ ENVSYS_BATTERY_CAPACITY_WARNING,	-1, 	"WARNING" },
100 	{ ENVSYS_BATTERY_CAPACITY_CRITICAL,	-1, 	"CRITICAL" },
101 	{ ENVSYS_BATTERY_CAPACITY_LOW,		-1,	"LOW" },
102 	{ -1,					-1, 	"UNKNOWN" }
103 };
104 
105 /*
106  * Available indicator descriptions.
107  */
108 static const struct sme_descr_entry sme_indicator_description[] = {
109 	{ ENVSYS_INDICATOR_FALSE,		-1,	"FALSE" },
110 	{ ENVSYS_INDICATOR_TRUE,		-1, 	"TRUE" },
111 	{ -1,					-1, 	"UNKNOWN" }
112 };
113 
114 static const struct sme_descr_entry *
115 sme_find_table(enum sme_descr_type table_id)
116 {
117 	switch (table_id) {
118 	case SME_DESC_UNITS:
119 		return sme_units_description;
120 		break;
121 	case SME_DESC_STATES:
122 		return sme_state_description;
123 		break;
124 	case SME_DESC_DRIVE_STATES:
125 		return sme_drivestate_description;
126 		break;
127 	case SME_DESC_BATTERY_CAPACITY:
128 		return sme_batterycap_description;
129 		break;
130 	case SME_DESC_INDICATOR:
131 		return sme_indicator_description;
132 		break;
133 	default:
134 		return NULL;
135 	}
136 }
137 
138 /*
139  * Returns the entry from specified table with type == key
140  */
141 const struct sme_descr_entry *
142 sme_find_table_entry(enum sme_descr_type table_id, int key)
143 {
144 	const struct sme_descr_entry *table = sme_find_table(table_id);
145 
146 	if (table != NULL)
147 		for (; table->type != -1; table++)
148 			if (table->type == key)
149 				return table;
150 
151 	return NULL;
152 }
153 
154 const struct sme_descr_entry *
155 sme_find_table_desc(enum sme_descr_type table_id, const char *str)
156 {
157 	const struct sme_descr_entry *table = sme_find_table(table_id);
158 
159 	if (table != NULL)
160 		for (; table->type != -1; table++)
161 			if (strcmp(table->desc, str) == 0)
162 				return table;
163 	return NULL;
164 }
165