@@ -1169,6 +1169,23 @@ void check_root_or_die(const char *cmd)
}
}
+int parse_bool(const char *val, bool *res)
+{
+ if (strcasecmp(val, "0") == 0 ||
+ strcasecmp(val, "disable") == 0 ||
+ strcasecmp(val, "disabled") == 0) {
+ *res = false;
+ return 0;
+ } else if (strcasecmp(val, "1") == 0 ||
+ strcasecmp(val, "enable") == 0 ||
+ strcasecmp(val, "enabled") == 0) {
+ *res = true;
+ return 0;
+ }
+
+ return -EINVAL;
+}
+
bool parse_throughput(char *buff, const char *description, uint32_t *throughput)
{
enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
@@ -72,6 +72,7 @@ int check_mesh_iface_ownership(char *mesh_iface, char *hard_iface);
void get_random_bytes(void *buf, size_t buflen);
void check_root_or_die(const char *cmd);
+int parse_bool(const char *val, bool *res);
bool parse_throughput(char *buff, const char *description,
uint32_t *throughput);
@@ -47,6 +47,34 @@ const char *sysfs_param_enable[] = {
NULL,
};
+int parse_simple_boolean(struct state *state, int argc, char *argv[])
+{
+ struct settings_data *settings = state->cmd->arg;
+ struct simple_boolean_data *data = settings->data;
+ int ret;
+
+ if (argc != 2) {
+ fprintf(stderr, "Error - incorrect number of arguments (expected 1)\n");
+ return -EINVAL;
+ }
+
+ ret = parse_bool(argv[1], &data->val);
+ if (ret < 0) {
+ fprintf(stderr, "Error - the supplied argument is invalid: %s\n", argv[1]);
+ fprintf(stderr, "The following values are allowed:\n");
+ fprintf(stderr, " * 0\n");
+ fprintf(stderr, " * disable\n");
+ fprintf(stderr, " * disabled\n");
+ fprintf(stderr, " * 1\n");
+ fprintf(stderr, " * enable\n");
+ fprintf(stderr, " * enabled\n");
+
+ return ret;
+ }
+
+ return 0;
+}
+
static int sys_simple_nlerror(struct sockaddr_nl *nla __maybe_unused,
struct nlmsgerr *nlerr, void *arg)
{
@@ -106,6 +134,33 @@ int sys_simple_nlquery(struct state *state, enum batadv_nl_commands nl_cmd,
return result;
}
+int sys_simple_print_boolean(struct nl_msg *msg, void *arg,
+ enum batadv_nl_attrs attr)
+{
+ struct nlattr *attrs[BATADV_ATTR_MAX + 1];
+ struct nlmsghdr *nlh = nlmsg_hdr(msg);
+ struct genlmsghdr *ghdr;
+ int *result = arg;
+
+ if (!genlmsg_valid_hdr(nlh, 0))
+ return NL_OK;
+
+ ghdr = nlmsg_data(nlh);
+
+ if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0),
+ genlmsg_len(ghdr), batadv_netlink_policy)) {
+ return NL_OK;
+ }
+
+ if (!attrs[attr])
+ return NL_OK;
+
+ printf("%s\n", nla_get_u8(attrs[attr]) ? "enabled" : "disabled");
+
+ *result = 0;
+ return NL_STOP;
+}
+
static void settings_usage(struct state *state)
{
fprintf(stderr, "Usage: batctl [options] %s|%s [parameters] %s\n",
@@ -27,6 +27,7 @@
#include <linux/genetlink.h>
#include <netlink/genl/genl.h>
+#include <stdbool.h>
#include "batman_adv.h"
#include "netlink.h"
@@ -51,10 +52,17 @@ struct settings_data {
extern const char *sysfs_param_enable[];
+struct simple_boolean_data {
+ bool val;
+};
+
int handle_sys_setting(struct state *state, int argc, char **argv);
+int parse_simple_boolean(struct state *state, int argc, char *argv[]);
int sys_simple_nlquery(struct state *state, enum batadv_nl_commands nl_cmd,
nl_recvmsg_msg_cb_t attribute_cb,
nl_recvmsg_msg_cb_t callback);
+int sys_simple_print_boolean(struct nl_msg *msg, void *arg,
+ enum batadv_nl_attrs attr);
#endif
Most of the available settings commands in batctl only operate on a single boolean value. Both the reading, parsing and writing of these values can mostly be handled by the same set of helper functions. Only the actual setting/getting of the correct attribute has to be handled by the actual settings command implementation. Signed-off-by: Sven Eckelmann <sven@narfation.org> --- functions.c | 17 +++++++++++++++++ functions.h | 1 + sys.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ sys.h | 8 ++++++++ 4 files changed, 81 insertions(+)