From: Arman Uguray <armansito@chromium.org>
To: linux-bluetooth@vger.kernel.org
Cc: Arman Uguray <armansito@chromium.org>
Subject: [PATCH BlueZ v2 4/7] tools/btgatt-client: Add command parsing.
Date: Sat, 30 Aug 2014 09:02:46 -0700 [thread overview]
Message-ID: <1409414569-20068-5-git-send-email-armansito@chromium.org> (raw)
In-Reply-To: <1409414569-20068-1-git-send-email-armansito@chromium.org>
Added basic support for command parsing, including the "help" command. Also
added a call to bt_gatt_client_set_debug.
---
tools/btgatt-client.c | 53 +++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 45 insertions(+), 8 deletions(-)
diff --git a/tools/btgatt-client.c b/tools/btgatt-client.c
index 008cc9f..26f2d14 100644
--- a/tools/btgatt-client.c
+++ b/tools/btgatt-client.c
@@ -68,7 +68,7 @@ static void att_disconnect_cb(void *user_data)
mainloop_quit();
}
-static void att_debug(const char *str, void *user_data)
+static void debug_cb(const char *str, void *user_data)
{
const char *prefix = user_data;
@@ -108,9 +108,6 @@ static struct client *client_create(int fd, uint16_t mtu)
return NULL;
}
- if (verbose)
- bt_att_set_debug(att, att_debug, "att: ", NULL);
-
cli->fd = fd;
cli->gatt = bt_gatt_client_new(att, mtu);
if (!cli->gatt) {
@@ -120,6 +117,12 @@ static struct client *client_create(int fd, uint16_t mtu)
return NULL;
}
+ if (verbose) {
+ bt_att_set_debug(att, debug_cb, "att: ", NULL);
+ bt_gatt_client_set_debug(cli->gatt, debug_cb, "gatt-client: ",
+ NULL);
+ }
+
/* bt_gatt_client already holds a reference */
bt_att_unref(att);
@@ -131,21 +134,55 @@ static void client_destroy(struct client *cli)
bt_gatt_client_unref(cli->gatt);
}
+
+typedef void (*command_func_t)(struct client *cli);
+
+static void cmd_help(struct client *cli);
+
+static struct {
+ char *cmd;
+ command_func_t func;
+ char *doc;
+} command[] = {
+ { "help", cmd_help, "\tDisplay help message" },
+ { }
+};
+
+static void cmd_help(struct client *cli)
+{
+ int i;
+
+ printf("Commands:\n");
+ for (i = 0; command[i].cmd; i++)
+ printf("\t%-15s\t%s\n", command[i].cmd, command[i].doc);
+}
+
static void prompt_read_cb(int fd, uint32_t events, void *user_data)
{
+ ssize_t read;
size_t len = 0;
char *line = NULL;
+ struct client *cli = user_data;
+ int i;
if (events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)) {
mainloop_quit();
return;
}
- if (getline(&line, &len, stdin) == -1)
+ if ((read = getline(&line, &len, stdin)) == -1)
return;
- /* TODO: Process commands here */
- printf(" Typed line: %s\n", line);
+ for (i = 0; command[i].cmd; i++) {
+ if (strncmp(command[i].cmd, line, read - 1) == 0)
+ break;
+ }
+
+ if (command[i].cmd)
+ command[i].func(cli);
+ else
+ fprintf(stderr, "Unknown command: %s\n", line);
+
print_prompt();
free(line);
@@ -384,7 +421,7 @@ int main(int argc, char *argv[])
if (mainloop_add_fd(fileno(stdin),
EPOLLIN | EPOLLRDHUP | EPOLLHUP | EPOLLERR,
- prompt_read_cb, NULL, NULL) < 0) {
+ prompt_read_cb, cli, NULL) < 0) {
fprintf(stderr, "Failed to initialize console\n");
return EXIT_FAILURE;
}
--
2.1.0.rc2.206.gedb03e5
next prev parent reply other threads:[~2014-08-30 16:02 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-08-30 16:02 [PATCH BlueZ v2 0/7] Introduce tools/btgatt-client Arman Uguray
2014-08-30 16:02 ` [PATCH BlueZ v2 1/7] tools/btgatt-client: " Arman Uguray
2014-08-30 16:02 ` [PATCH BlueZ v2 2/7] tools/btgatt-client: Added L2CAP LE ATT connection Arman Uguray
2014-08-30 16:02 ` [PATCH BlueZ v2 3/7] tools/btgatt-client: Integrate bt_att and bt_gatt_client Arman Uguray
2014-08-30 16:02 ` Arman Uguray [this message]
2014-08-30 16:02 ` [PATCH BlueZ v2 5/7] tools/btgatt-client: Add "ready" handler Arman Uguray
2014-08-30 16:02 ` [PATCH BlueZ v2 6/7] tools/btgatt-client: Add "services" command Arman Uguray
2014-08-30 16:02 ` [PATCH BlueZ v2 7/7] tools/btgatt: Add command argument support Arman Uguray
2014-08-30 16:48 ` [PATCH BlueZ v2 0/7] Introduce tools/btgatt-client Marcel Holtmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1409414569-20068-5-git-send-email-armansito@chromium.org \
--to=armansito@chromium.org \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.