]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/mfiutil/mfi_bbu.c
Add support for getting and setting BBU properties related to battery
[FreeBSD/FreeBSD.git] / usr.sbin / mfiutil / mfi_bbu.c
1 /*-
2  * Copyright (c) 2013 Sandvine Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The names of the authors may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #include <sys/param.h>
33 #include <sys/errno.h>
34 #include <sys/stat.h>
35 #include <err.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <time.h>
41 #include <unistd.h>
42 #include "mfiutil.h"
43
44 /* The autolearn period is given in seconds. */
45 void
46 mfi_autolearn_period(uint32_t period, char *buf, size_t sz)
47 {
48         unsigned int d, h;
49         char *tmp;
50
51         d = period / (24 * 3600);
52         h = (period % (24 * 3600)) / 3600;
53
54         tmp = buf;
55         if (d != 0) {
56                 tmp += snprintf(buf, sz, "%u day%s", d, d == 1 ? "" : "s");
57                 sz -= tmp - buf;
58                 if (h != 0) {
59                         tmp += snprintf(tmp, sz, ", ");
60                         sz -= 2;
61                 }
62         }
63         if (h != 0)
64                 snprintf(tmp, sz, "%u hour%s", h, h == 1 ? "" : "s");
65
66         if (d == 0 && h == 0)
67                 snprintf(tmp, sz, "less than 1 hour");
68 }
69
70 /* The time to the next relearn is given in seconds since 1/1/2000. */
71 void
72 mfi_next_learn_time(uint32_t next_learn_time, char *buf, size_t sz)
73 {
74         time_t basetime;
75         struct tm tm;
76         size_t len;
77
78         memset(&tm, 0, sizeof(tm));
79         tm.tm_year = 100;
80         basetime = timegm(&tm);
81         basetime += (time_t)next_learn_time;
82         len = snprintf(buf, sz, "%s", ctime(&basetime));
83         if (len > 0)
84                 /* Get rid of the newline added by ctime(3). */
85                 buf[len - 1] = '\0';
86 }
87
88 void
89 mfi_autolearn_mode(uint8_t mode, char *buf, size_t sz)
90 {
91
92         switch (mode) {
93         case 0:
94                 snprintf(buf, sz, "enabled");
95                 break;
96         case 1:
97                 snprintf(buf, sz, "disabled");
98                 break;
99         case 2:
100                 snprintf(buf, sz, "warn via event");
101                 break;
102         default:
103                 snprintf(buf, sz, "mode 0x%02x", mode);
104                 break;
105         }
106 }
107
108 int
109 mfi_bbu_get_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp)
110 {
111
112         return (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_PROP, props,
113             sizeof(*props), NULL, 0, statusp));
114 }
115
116 int
117 mfi_bbu_set_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp)
118 {
119
120         return (mfi_dcmd_command(fd, MFI_DCMD_BBU_SET_PROP, props,
121             sizeof(*props), NULL, 0, statusp));
122 }
123
124 static int
125 start_bbu_learn(int ac, char **av __unused)
126 {
127         uint8_t status;
128         int error, fd;
129
130         status = MFI_STAT_OK;
131         error = 0;
132
133         if (ac != 1) {
134                 warnx("start learn: unexpected arguments");
135                 return (EINVAL);
136         }
137
138         fd = mfi_open(mfi_unit, O_RDWR);
139         if (fd < 0) {
140                 error = errno;
141                 warn("mfi_open");
142                 return (error);
143         }
144
145         if (mfi_dcmd_command(fd, MFI_DCMD_BBU_START_LEARN, NULL, 0, NULL, 0,
146             &status) < 0) {
147                 error = errno;
148                 warn("Failed to start BBU learn");
149         } else if (status != MFI_STAT_OK) {
150                 warnx("Failed to start BBU learn: %s", mfi_status(status));
151                 error = EIO;
152         }
153
154         return (error);
155 }
156 MFI_COMMAND(start, learn, start_bbu_learn);
157
158 static int
159 update_bbu_props(int ac, char **av)
160 {
161         struct mfi_bbu_properties props;
162         unsigned long delay;
163         uint8_t status;
164         int error, fd;
165         char *mode, *endptr;
166
167         status = MFI_STAT_OK;
168         error = 0;
169
170         if (ac != 3) {
171                 warnx("bbu: property and value required");
172                 return (EINVAL);
173         }
174
175         fd = mfi_open(mfi_unit, O_RDWR);
176         if (fd < 0) {
177                 error = errno;
178                 warn("mfi_open");
179                 return (error);
180         }
181
182         if (mfi_bbu_get_props(fd, &props, &status) < 0) {
183                 error = errno;
184                 warn("Failed to get BBU properties");
185                 goto done;
186         } else if (status != MFI_STAT_OK) {
187                 warnx("Failed to get BBU properties: %s", mfi_status(status));
188                 error = EIO;
189                 goto done;
190         }
191
192         if (strcmp(av[1], "learn-delay") == 0) {
193                 delay = strtoul(av[2], &endptr, 10);
194                 if (strlen(av[2]) == 0 || *endptr != '\0' || delay > 255) {
195                         warnx("Invalid learn delay '%s'", av[2]);
196                         error = EINVAL;
197                         goto done;
198                 }
199
200                 props.learn_delay_interval = delay;
201         } else if (strcmp(av[1], "autolearn-mode") == 0) {
202                 mode = av[2];
203
204                 if (strcmp(av[2], "enable") == 0)
205                         props.auto_learn_mode = 0;
206                 else if (strcmp(av[2], "disable") == 0)
207                         props.auto_learn_mode = 1;
208                 else if (mode[0] >= '0' && mode[0] <= '2' && mode[1] == '\0')
209                         props.auto_learn_mode = mode[0] - '0';
210                 else {
211                         warnx("Invalid mode '%s'", mode);
212                         error = EINVAL;
213                         goto done;
214                 }
215         } else if (strcmp(av[1], "bbu-mode") == 0) {
216                 if (props.bbu_mode == 0) {
217                         warnx("This BBU does not implement different modes");
218                         error = EINVAL;
219                         goto done;
220                 }
221
222                 /* The mode must be an integer between 1 and 5. */
223                 mode = av[2];
224                 if (mode[0] < '1' || mode[0] > '5' || mode[1] != '\0') {
225                         warnx("Invalid mode '%s'", mode);
226                         error = EINVAL;
227                         goto done;
228                 }
229
230                 props.bbu_mode = mode[0] - '0';
231         } else {
232                 warnx("bbu: Invalid command '%s'", av[1]);
233                 error = EINVAL;
234                 goto done;
235         }
236
237         if (mfi_bbu_set_props(fd, &props, &status) < 0) {
238                 error = errno;
239                 warn("Failed to set BBU properties");
240                 goto done;
241         } else if (status != MFI_STAT_OK) {
242                 warnx("Failed to set BBU properties: %s", mfi_status(status));
243                 error = EIO;
244                 goto done;
245         }
246
247 done:
248         close(fd);
249
250         return (error);
251 }
252 MFI_COMMAND(top, bbu, update_bbu_props);