2 * Copyright (c) 2013 Sandvine Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
32 #include <sys/param.h>
33 #include <sys/errno.h>
44 /* The autolearn period is given in seconds. */
46 mfi_autolearn_period(uint32_t period, char *buf, size_t sz)
51 d = period / (24 * 3600);
52 h = (period % (24 * 3600)) / 3600;
56 tmp += snprintf(buf, sz, "%u day%s", d, d == 1 ? "" : "s");
59 tmp += snprintf(tmp, sz, ", ");
64 snprintf(tmp, sz, "%u hour%s", h, h == 1 ? "" : "s");
67 snprintf(tmp, sz, "less than 1 hour");
70 /* The time to the next relearn is given in seconds since 1/1/2000. */
72 mfi_next_learn_time(uint32_t next_learn_time, char *buf, size_t sz)
78 memset(&tm, 0, sizeof(tm));
80 basetime = timegm(&tm);
81 basetime += (time_t)next_learn_time;
82 len = snprintf(buf, sz, "%s", ctime(&basetime));
84 /* Get rid of the newline added by ctime(3). */
89 mfi_autolearn_mode(uint8_t mode, char *buf, size_t sz)
94 snprintf(buf, sz, "enabled");
97 snprintf(buf, sz, "disabled");
100 snprintf(buf, sz, "warn via event");
103 snprintf(buf, sz, "mode 0x%02x", mode);
109 mfi_bbu_get_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp)
112 return (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_PROP, props,
113 sizeof(*props), NULL, 0, statusp));
117 mfi_bbu_set_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp)
120 return (mfi_dcmd_command(fd, MFI_DCMD_BBU_SET_PROP, props,
121 sizeof(*props), NULL, 0, statusp));
125 start_bbu_learn(int ac, char **av __unused)
130 status = MFI_STAT_OK;
134 warnx("start learn: unexpected arguments");
138 fd = mfi_open(mfi_unit, O_RDWR);
145 if (mfi_dcmd_command(fd, MFI_DCMD_BBU_START_LEARN, NULL, 0, NULL, 0,
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));
156 MFI_COMMAND(start, learn, start_bbu_learn);
159 update_bbu_props(int ac, char **av)
161 struct mfi_bbu_properties props;
167 status = MFI_STAT_OK;
171 warnx("bbu: property and value required");
175 fd = mfi_open(mfi_unit, O_RDWR);
182 if (mfi_bbu_get_props(fd, &props, &status) < 0) {
184 warn("Failed to get BBU properties");
186 } else if (status != MFI_STAT_OK) {
187 warnx("Failed to get BBU properties: %s", mfi_status(status));
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]);
200 props.learn_delay_interval = delay;
201 } else if (strcmp(av[1], "autolearn-mode") == 0) {
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';
211 warnx("Invalid mode '%s'", mode);
215 } else if (strcmp(av[1], "bbu-mode") == 0) {
216 if (props.bbu_mode == 0) {
217 warnx("This BBU does not implement different modes");
222 /* The mode must be an integer between 1 and 5. */
224 if (mode[0] < '1' || mode[0] > '5' || mode[1] != '\0') {
225 warnx("Invalid mode '%s'", mode);
230 props.bbu_mode = mode[0] - '0';
232 warnx("bbu: Invalid command '%s'", av[1]);
237 if (mfi_bbu_set_props(fd, &props, &status) < 0) {
239 warn("Failed to set BBU properties");
241 } else if (status != MFI_STAT_OK) {
242 warnx("Failed to set BBU properties: %s", mfi_status(status));
252 MFI_COMMAND(top, bbu, update_bbu_props);