From a1dc4edb4302a6d76a764dd94c81931a3d4d5cfa Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Wed, 30 Nov 2016 11:29:55 +0100 Subject: [PATCH] stdin: add btstack_stdin_read() and use in examples --- example/hfp_ag_demo.c | 2 +- example/hfp_hf_demo.c | 3 ++- example/hsp_ag_demo.c | 4 ++-- example/hsp_hs_demo.c | 4 ++-- platform/posix/stdin_support.c | 11 +++++++++++ platform/posix/stdin_support.h | 3 +++ 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/example/hfp_ag_demo.c b/example/hfp_ag_demo.c index 707b6251f..902cc7ce0 100644 --- a/example/hfp_ag_demo.c +++ b/example/hfp_ag_demo.c @@ -335,7 +335,7 @@ static void show_usage(void){ } static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){ - read(ds->fd, &cmd, 1); + cmd = btstack_stdin_read(); switch (cmd){ case 'a': log_info("USER:\'%c\'", cmd); diff --git a/example/hfp_hf_demo.c b/example/hfp_hf_demo.c index 7592dd108..458110279 100644 --- a/example/hfp_hf_demo.c +++ b/example/hfp_hf_demo.c @@ -152,7 +152,8 @@ static void show_usage(void){ } static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){ - read(ds->fd, &cmd, 1); + + cmd = btstack_stdin_read(); if (cmd >= '0' && cmd <= '9'){ printf("DTMF Code: %c\n", cmd); diff --git a/example/hsp_ag_demo.c b/example/hsp_ag_demo.c index a395e8eda..8aeb22519 100644 --- a/example/hsp_ag_demo.c +++ b/example/hsp_ag_demo.c @@ -121,8 +121,8 @@ static void show_usage(void){ #ifdef HAVE_POSIX_STDIN static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){ - char buffer; - read(ds->fd, &buffer, 1); + + char buffer = btstack_stdin_read(); switch (buffer){ case 'c': diff --git a/example/hsp_hs_demo.c b/example/hsp_hs_demo.c index 6b01a4c24..fcdd238ae 100644 --- a/example/hsp_hs_demo.c +++ b/example/hsp_hs_demo.c @@ -121,8 +121,8 @@ static void show_usage(void){ #ifdef HAVE_POSIX_STDIN static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){ - char buffer; - read(ds->fd, &buffer, 1); + + char buffer = btstack_stdin_read(); switch (buffer){ case 'c': diff --git a/platform/posix/stdin_support.c b/platform/posix/stdin_support.c index 0766d6029..57c9d2743 100644 --- a/platform/posix/stdin_support.c +++ b/platform/posix/stdin_support.c @@ -37,6 +37,8 @@ #include #include +#include + #include "btstack_run_loop.h" #include @@ -89,6 +91,7 @@ void btstack_stdin_reset(void){ #endif } +#if 0 static int getstring(char *line, int size) { int i = 0; @@ -109,4 +112,12 @@ static int getstring(char *line, int size) line[i] = 0; return i; } +#endif + +// read single byte after data source callback was triggered +char btstack_stdin_read(void){ + char buffer; + read(stdin_source.fd, &buffer, 1); + return buffer; +} diff --git a/platform/posix/stdin_support.h b/platform/posix/stdin_support.h index ff57ea26f..487d0aa1b 100644 --- a/platform/posix/stdin_support.h +++ b/platform/posix/stdin_support.h @@ -47,6 +47,9 @@ extern "C" { // setup handler for command line interface void btstack_stdin_setup(void (*stdin_handler)(btstack_data_source_t *_ds, btstack_data_source_callback_type_t callback_type)); +// read single byte after data source callback was triggered +char btstack_stdin_read(void); + // gets called by main.c void btstack_stdin_reset(void);