xref: /illumos-gate/usr/src/lib/libipmi/common/ipmi_sensor.c (revision 208e1562df6d45a86b71f40db0028ada4de4b56a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2018, Joyent, Inc.
27  */
28 
29 #include <libipmi.h>
30 #include <string.h>
31 
32 #include "ipmi_impl.h"
33 
34 ipmi_sensor_reading_t *
ipmi_get_sensor_reading(ipmi_handle_t * ihp,uint8_t id)35 ipmi_get_sensor_reading(ipmi_handle_t *ihp, uint8_t id)
36 {
37 	ipmi_cmd_t cmd, *resp;
38 	ipmi_sensor_reading_t *srp;
39 
40 	cmd.ic_netfn = IPMI_NETFN_SE;
41 	cmd.ic_cmd = IPMI_CMD_GET_SENSOR_READING;
42 	cmd.ic_lun = 0;
43 	cmd.ic_data = &id;
44 	cmd.ic_dlen = sizeof (id);
45 
46 	if ((resp = ipmi_send(ihp, &cmd)) == NULL)
47 		return (NULL);
48 
49 	/*
50 	 * The upper half of the state field is optional, so if it's not
51 	 * present, then set it to zero.  We also need to convert to the
52 	 * native endianness.
53 	 */
54 	if (resp->ic_dlen < sizeof (ipmi_sensor_reading_t) - sizeof (uint8_t)) {
55 		(void) ipmi_set_error(ihp, EIPMI_BAD_RESPONSE_LENGTH, NULL);
56 		return (NULL);
57 	}
58 	srp = resp->ic_data;
59 
60 	if (resp->ic_dlen < sizeof (ipmi_sensor_reading_t))
61 		(void) memset((char *)srp + resp->ic_dlen, '\0',
62 		    sizeof (ipmi_sensor_reading_t) - resp->ic_dlen);
63 
64 	srp->isr_state = LE_IN16(&srp->isr_state);
65 	return (srp);
66 }
67 
68 int
ipmi_set_sensor_reading(ipmi_handle_t * ihp,ipmi_set_sensor_reading_t * req)69 ipmi_set_sensor_reading(ipmi_handle_t *ihp, ipmi_set_sensor_reading_t *req)
70 {
71 	ipmi_set_sensor_reading_t realreq;
72 	ipmi_cmd_t cmd, *resp;
73 	uint16_t tmp;
74 
75 	/*
76 	 * Convert states to little endian.
77 	 */
78 	(void) memcpy(&realreq, req, sizeof (realreq));
79 
80 	tmp = LE_IN16(&realreq.iss_assert_state);
81 	(void) memcpy(&realreq.iss_assert_state, &tmp, sizeof (tmp));
82 	tmp = LE_IN16(&realreq.iss_deassert_state);
83 	(void) memcpy(&realreq.iss_deassert_state, &tmp, sizeof (tmp));
84 
85 	cmd.ic_netfn = IPMI_NETFN_SE;
86 	cmd.ic_cmd = IPMI_CMD_SET_SENSOR_READING;
87 	cmd.ic_lun = 0;
88 	cmd.ic_data = &realreq;
89 	cmd.ic_dlen = sizeof (realreq);
90 
91 	if ((resp = ipmi_send(ihp, &cmd)) == NULL)
92 		return (-1);
93 
94 	if (resp->ic_dlen != 0)
95 		return (ipmi_set_error(ihp, EIPMI_BAD_RESPONSE_LENGTH, NULL));
96 
97 	return (0);
98 }
99 
100 int
ipmi_get_sensor_thresholds(ipmi_handle_t * ihp,ipmi_sensor_thresholds_t * thresh,uint8_t id)101 ipmi_get_sensor_thresholds(ipmi_handle_t *ihp, ipmi_sensor_thresholds_t *thresh,
102     uint8_t id)
103 {
104 	ipmi_cmd_t cmd, *resp;
105 
106 	cmd.ic_netfn = IPMI_NETFN_SE;
107 	cmd.ic_cmd = IPMI_CMD_GET_SENSOR_THRESHOLDS;
108 	cmd.ic_lun = 0;
109 	cmd.ic_data = &id;
110 	cmd.ic_dlen = sizeof (id);
111 
112 	if ((resp = ipmi_send(ihp, &cmd)) == NULL)
113 		return (-1);
114 
115 	if (resp->ic_dlen < sizeof (ipmi_sensor_thresholds_t)) {
116 		return (ipmi_set_error(ihp, EIPMI_BAD_RESPONSE_LENGTH, NULL));
117 	}
118 
119 	(void) memcpy(thresh, resp->ic_data, sizeof (ipmi_sensor_thresholds_t));
120 
121 	return (0);
122 }
123