From 7941078420c7b6114e17689645a0ccc25a5eee41 Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Wed, 26 Oct 2011 12:01:47 +0200 Subject: [PATCH] Add basic generic command handling to btmgmt --- mgmt/main.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/mgmt/main.c b/mgmt/main.c index 0a5dabb3e..c8ebcc74d 100644 --- a/mgmt/main.c +++ b/mgmt/main.c @@ -23,6 +23,7 @@ #include #endif +#include #include #include #include @@ -30,6 +31,7 @@ #include #include #include +#include #include #include @@ -244,11 +246,68 @@ static int mgmt_process_data(int mgmt_sk) return 0; } +static void cmd_monitor(int mgmt_sk, int argc, char **argv) +{ + printf("Monitoring mgmt events...\n"); +} + +static struct { + char *cmd; + void (*func)(int mgmt_sk, int argc, char **argv); + char *doc; +} command[] = { + { "monitor", cmd_monitor, "Monitor events" }, + { NULL, NULL, 0 } +}; + +static void usage(void) +{ + int i; + + printf("btmgmt ver %s\n", VERSION); + printf("Usage:\n" + "\tbtmgmt [options] [command parameters]\n"); + + printf("Options:\n" + "\t--help\tDisplay help\n"); + + printf("Commands:\n"); + for (i = 0; command[i].cmd; i++) + printf("\t%-4s\t%s\n", command[i].cmd, command[i].doc); + + printf("\n" + "For more information on the usage of each command use:\n" + "\tbtmgmt --help\n" ); +} + +static struct option main_options[] = { + { "help", 0, 0, 'h' }, + { 0, 0, 0, 0 } +}; + int main(int argc, char *argv[]) { - int mgmt_sk; + int opt, i, mgmt_sk; struct pollfd pollfd; + while ((opt=getopt_long(argc, argv, "+h", main_options, NULL)) != -1) { + switch (opt) { + case 'h': + default: + usage(); + return 0; + } + } + + argc -= optind; + argv += optind; + optind = 0; + + if (argc < 1) { + usage(); + return 0; + } + mgmt_sk = mgmt_open(); if (mgmt_sk < 0) { fprintf(stderr, "Unable to open mgmt socket\n"); @@ -257,6 +316,14 @@ int main(int argc, char *argv[]) printf("mgmt socket successfully opened\n"); + for (i = 0; command[i].cmd; i++) { + if (strcmp(command[i].cmd, argv[0]) != 0) + continue; + + command[i].func(mgmt_sk, argc, argv); + break; + } + pollfd.fd = mgmt_sk; pollfd.events = POLLIN; pollfd.revents = 0; -- 2.47.3