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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/errno.h>
41 /* The autolearn period is given in seconds. */
43 mfi_autolearn_period(uint32_t period, char *buf, size_t sz)
48 d = period / (24 * 3600);
49 h = (period % (24 * 3600)) / 3600;
53 tmp += snprintf(buf, sz, "%u day%s", d, d == 1 ? "" : "s");
56 tmp += snprintf(tmp, sz, ", ");
61 snprintf(tmp, sz, "%u hour%s", h, h == 1 ? "" : "s");
64 snprintf(tmp, sz, "less than 1 hour");
67 /* The time to the next relearn is given in seconds since 1/1/2000. */
69 mfi_next_learn_time(uint32_t next_learn_time, char *buf, size_t sz)
75 memset(&tm, 0, sizeof(tm));
77 basetime = timegm(&tm);
78 basetime += (time_t)next_learn_time;
79 len = snprintf(buf, sz, "%s", ctime(&basetime));
81 /* Get rid of the newline added by ctime(3). */
86 mfi_autolearn_mode(uint8_t mode, char *buf, size_t sz)
91 snprintf(buf, sz, "enabled");
94 snprintf(buf, sz, "disabled");
97 snprintf(buf, sz, "warn via event");
100 snprintf(buf, sz, "mode 0x%02x", mode);
106 mfi_bbu_get_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp)
109 return (mfi_dcmd_command(fd, MFI_DCMD_BBU_GET_PROP, props,
110 sizeof(*props), NULL, 0, statusp));
114 mfi_bbu_set_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp)
117 return (mfi_dcmd_command(fd, MFI_DCMD_BBU_SET_PROP, props,
118 sizeof(*props), NULL, 0, statusp));
122 start_bbu_learn(int ac, char **av __unused)
127 status = MFI_STAT_OK;
131 warnx("start learn: unexpected arguments");
135 fd = mfi_open(mfi_unit, O_RDWR);
142 if (mfi_dcmd_command(fd, MFI_DCMD_BBU_START_LEARN, NULL, 0, NULL, 0,
145 warn("Failed to start BBU learn");
146 } else if (status != MFI_STAT_OK) {
147 warnx("Failed to start BBU learn: %s", mfi_status(status));
153 MFI_COMMAND(start, learn, start_bbu_learn);
156 update_bbu_props(int ac, char **av)
158 struct mfi_bbu_properties props;
164 status = MFI_STAT_OK;
168 warnx("bbu: property and value required");
172 fd = mfi_open(mfi_unit, O_RDWR);
179 if (mfi_bbu_get_props(fd, &props, &status) < 0) {
181 warn("Failed to get BBU properties");
183 } else if (status != MFI_STAT_OK) {
184 warnx("Failed to get BBU properties: %s", mfi_status(status));
189 if (strcmp(av[1], "learn-delay") == 0) {
190 delay = strtoul(av[2], &endptr, 10);
191 if (strlen(av[2]) == 0 || *endptr != '\0' || delay > 255) {
192 warnx("Invalid learn delay '%s'", av[2]);
197 props.learn_delay_interval = delay;
198 } else if (strcmp(av[1], "autolearn-mode") == 0) {
201 if (strcmp(av[2], "enable") == 0)
202 props.auto_learn_mode = 0;
203 else if (strcmp(av[2], "disable") == 0)
204 props.auto_learn_mode = 1;
205 else if (mode[0] >= '0' && mode[0] <= '2' && mode[1] == '\0')
206 props.auto_learn_mode = mode[0] - '0';
208 warnx("Invalid mode '%s'", mode);
212 } else if (strcmp(av[1], "bbu-mode") == 0) {
213 if (props.bbu_mode == 0) {
214 warnx("This BBU does not implement different modes");
219 /* The mode must be an integer between 1 and 5. */
221 if (mode[0] < '1' || mode[0] > '5' || mode[1] != '\0') {
222 warnx("Invalid mode '%s'", mode);
227 props.bbu_mode = mode[0] - '0';
229 warnx("bbu: Invalid command '%s'", av[1]);
234 if (mfi_bbu_set_props(fd, &props, &status) < 0) {
236 warn("Failed to set BBU properties");
238 } else if (status != MFI_STAT_OK) {
239 warnx("Failed to set BBU properties: %s", mfi_status(status));
249 MFI_COMMAND(top, bbu, update_bbu_props);