shared/shell: Add support for -i/--init-script
Luiz Augusto von Dentz
maintainerPATCH v2 1/2 shared/shell: Add support for -i/--init-script
From: Luiz Augusto von Dentz <[email protected]>
This adds support for -i/--init-script which can be used to provide a file with commands to be initialized, the commands are then run in sequence after completing:
client/bluetoothctl -i client/power-on-off.bt Agent registered
Changing power on succeeded
[CHG] Controller A8:7E:EA:56:87:D5 Pairable: yes [CHG] Controller 98:8D:46:EE:6D:16 Pairable: yes [CHG] Controller 98:8D:46:EE:6D:16 PowerState: on-disabling AdvertisementMonitor path registered
--- src/shared/shell.c | 159 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 142 insertions(+), 17 deletions(-)
src/shared/shell.c +142 -17
@@ -24,6 +24,7 @@#include <sys/signalfd.h> #include <wordexp.h> #include <getopt.h> +#include <fcntl.h> #include <readline/readline.h> #include <readline/history.h>
@@ -69,7 +70,12 @@ static struct {bool zsh;bool monitor;int timeout; -struct io *input; +int init_fd; +FILE *f; +struct queue *inputs; + +char *line; +struct queue *queue;bool saved_prompt;bt_shell_prompt_input_func saved_func;
@@ -535,7 +541,7 @@ void bt_shell_printf(const char *fmt, ...)char *saved_line;int saved_point; -if (!data.input) +if (queue_isempty (data.inputs))return ;if (data.mode) {
@@ -615,6 +621,32 @@ void bt_shell_usage() data.exec->arg ? data.exec->arg : ""); } +static void bt_shell_dequeue_exec (void ) +{ +int err; + +if (!data.line) +return ; + +free (data.line); + data.line =NULL ; + + data.line =queue_pop_head (data.queue); +if (!data.line) +return ; + +bt_shell_printf ("%s\n" , data.line); + +if (!bt_shell_release_prompt (data.line)) { +bt_shell_dequeue_exec (); +return ; + } + + err =bt_shell_exec (data.line); +if (err) +bt_shell_dequeue_exec (); +} +static void prompt_input (const char *str,bt_shell_prompt_input_func func,void *user_data) {
@@ -988,6 +1020,11 @@ static char **shell_completion(const char *text, int start, int end)static bool io_hup (struct io *io,void *user_data) { +if (queue_remove (data.inputs, io)) { +if (!queue_isempty (data.inputs)) +return false; + } +mainloop_quit ();return false;
@@ -999,7 +1036,7 @@ static void signal_callback(int signum, void *user_data)switch (signum) {case SIGINT : -if (data.input && !data.mode) { +if (!queue_isempty (data.inputs) && !data.mode) {rl_replace_line ("" ,0 );rl_crlf ();rl_on_new_line ();
@@ -1091,6 +1128,7 @@ static void rl_init(void)static const struct option main_options[] = { {"version" , no_argument,0 ,'v' }, {"help" , no_argument,0 ,'h' }, + {"init-script" ,required_argument,0 ,'i' }, {"timeout" , required_argument,0 ,'t' }, {"monitor" , no_argument,0 ,'m' }, {"zsh-complete" , no_argument,0 ,'z' },
@@ -1112,6 +1150,7 @@ static void usage(int argc, char **argv, const struct bt_shell_opt *opt)printf ("\t--monitor \tEnable monitor output\n" "\t--timeout \tTimeout in seconds for non-interactive mode\n" "\t--version \tDisplay version\n" +"\t--init-script \tInit script file\n" "\t--help \t\tDisplay help\n" ); }
@@ -1130,9 +1169,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)if (opt) {memcpy (options + offset, opt->options,sizeof (struct option ) * opt->optno); -snprintf (optstr,sizeof (optstr),"+mhvt:%s" , opt->optstr); +snprintf (optstr,sizeof (optstr),"+mhvi:t:%s" , opt->optstr); }else -snprintf (optstr,sizeof (optstr),"+mhvt:" ); +snprintf (optstr,sizeof (optstr),"+mhvi:t:" ); data.name =strrchr (argv[0 ],'/' );if (!data.name)
@@ -1140,6 +1179,8 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt)else data.name =strdup (++data.name); + data.init_fd =-1 ; +while ((c =getopt_long (argc, argv, optstr, options, &index)) !=-1 ) {switch (c) {case 'v' :
@@ -1152,6 +1193,13 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt) data.argv = &cmplt; data.mode =1 ; goto done; +case 'i' : +if (optarg) + data.init_fd =open (optarg,O_RDONLY ); +if (data.init_fd <0 ) +printf ("Unable to open %s: %s (%d)\n" , optarg, +strerror (errno), errno); +break ;case 't' :if (optarg) data.timeout =strtol (optarg, &endptr,0 );
@@ -1205,6 +1253,8 @@ done:rl_init (); data.init = true; + data.inputs =queue_new (); + data.queue =queue_new (); data.prompts =queue_new (); }
@@ -1239,6 +1289,25 @@ int bt_shell_run(void)return status; } +static int bt_shell_queue_exec (char *line) +{ +int err; + +/* Queue if already executing */ +if (data.line) { +queue_push_tail (data.queue,strdup (line)); +return 0 ; + } + +bt_shell_printf ("%s\n" , line); + + err =bt_shell_exec (line); +if (!err) + data.line =strdup (line); + +return err; +} +int bt_shell_exec (const char *input) {wordexp_t w;
@@ -1247,8 +1316,20 @@ int bt_shell_exec(const char *input)if (!input)return 0 ; -if (wordexp (input, &w,WRDE_NOCMD )) -return -ENOEXEC ; + err =wordexp (input, &w,WRDE_NOCMD ); +switch (err) { +case WRDE_BADCHAR : +return -EBADMSG ; +case WRDE_BADVAL : +case WRDE_SYNTAX : +return -EINVAL ; +case WRDE_NOSPACE : +return -ENOMEM ; +case WRDE_CMDSUB : +if (wordexp (input, &w,0 )) +return -ENOEXEC ; +break ; + };if (w.we_wordc ==0 ) {wordfree (&w);
@@ -1277,6 +1358,8 @@ void bt_shell_cleanup(void)rl_cleanup (); +queue_destroy (data.inputs,NULL ); +queue_destroy (data.queue, free);queue_destroy (data.prompts, prompt_free); data.prompts =NULL ;
@@ -1294,8 +1377,10 @@ void bt_shell_quit(int status)void bt_shell_noninteractive_quit (int status) { -if (!data.mode || data.timeout) +if (!data.mode || data.timeout) { +bt_shell_dequeue_exec ();return ; + }bt_shell_quit (status); }
@@ -1340,7 +1425,43 @@ void bt_shell_set_prompt(const char *string)static bool input_read (struct io *io,void *user_data) { -rl_callback_read_char (); +int fd; +char *line =NULL ; +size_t len =0 ; +ssize_t nread; + + fd =io_get_fd (io); + +if (fd ==STDIN_FILENO ) { +rl_callback_read_char (); +return true; + } + +if (!data.f) { + data.f =fdopen (fd,"r" ); +if (!data.f) { +printf ("fdopen: %s (%d)\n" ,strerror (errno), errno); +return false; + } + } + + nread =getline (&line, &len, data.f); +if (nread >0 ) { +int err; + +if (line[nread -1 ] =='\n' ) + line[nread -1 ] ='\0' ; + + err =bt_shell_queue_exec (line); +if (err <0 ) +printf ("%s: %s (%d)\n" , line,strerror (-err), -err); + }else { +fclose (data.f); + data.f =NULL ; + } + +free (line); +return true; }
@@ -1355,18 +1476,16 @@ bool bt_shell_attach(int fd) {struct io *io; -/* TODO: Allow more than one input? */ -if (data.input) -return false; - io =io_new (fd); +if (!io) +return false;if (!data.mode) {io_set_read_handler (io, input_read,NULL ,NULL );io_set_disconnect_handler (io, io_hup,NULL ,NULL ); } - data.input = io; +queue_push_tail (data.inputs, io);if (data.mode) {if (shell_exec (data.argc, data.argv) <0 ) {
@@ -1377,6 +1496,12 @@ bool bt_shell_attach(int fd)if (data.timeout)timeout_add (data.timeout *1000 , shell_quit,NULL ,NULL ); + }else if (data.init_fd >=0 ) { +int fd = data.init_fd; + + data.init_fd =-1 ; +if (!bt_shell_attach (fd)) +return false; }return true;
@@ -1384,11 +1509,11 @@ bool bt_shell_attach(int fd)bool bt_shell_detach (void ) { -if (!data.input) +if (queue_isempty (data.inputs))return false; -io_destroy (data.input); - data.input =NULL ; +queue_remove_all (data.inputs,NULL ,NULL , + (queue_destroy_func_t ) io_destroy);return true; }
Signature · 1 line
2.39.2
Luiz Augusto von Dentz
maintainerPATCH v2 2/2 client: Add samples init scripts
From: Luiz Augusto von Dentz <[email protected]>
This adds sample init scripts that can be passed to bluetoothctl for testing.
--- client/scripts/advertise-broadcast.bt | 2 ++ client/scripts/advertise-on.bt | 2 ++ client/scripts/advertise-peripheral.bt | 2 ++ client/scripts/gatt-batt.bt | 5 +++++ client/scripts/power-on-off.bt | 3 +++ client/scripts/power-on.bt | 3 +++ client/scripts/preset-custom.bt | 26 ++++++++++++++++++++++++++ client/scripts/scan-bredr.bt | 2 ++ client/scripts/scan-le.bt | 2 ++ client/scripts/scan-on-off.bt | 4 ++++ client/scripts/scan-on.bt | 2 ++ 11 files changed, 53 insertions(+) create mode 100644 client/scripts/advertise-broadcast.bt create mode 100644 client/scripts/advertise-on.bt create mode 100644 client/scripts/advertise-peripheral.bt create mode 100644 client/scripts/gatt-batt.bt create mode 100644 client/scripts/power-on-off.bt create mode 100644 client/scripts/power-on.bt create mode 100644 client/scripts/preset-custom.bt create mode 100644 client/scripts/scan-bredr.bt create mode 100644 client/scripts/scan-le.bt create mode 100644 client/scripts/scan-on-off.bt create mode 100644 client/scripts/scan-on.bt
client/scripts/advertise-broadcast.bt new file +2 -0
@@ -0,0 +1,2 @@ +power on +advertise broadcast
client/scripts/advertise-on.bt new file +2 -0
@@ -0,0 +1,2 @@ +power on +advertise on
client/scripts/advertise-peripheral.bt new file +2 -0
@@ -0,0 +1,2 @@ +power on +advertise peripheral
client/scripts/gatt-batt.bt new file +5 -0
@@ -0,0 +1,5 @@ +gatt.register-service 0x180f +yes +gatt.register-characteristic 0x2a19 read,notify +64 +gatt.register-application
client/scripts/power-on-off.bt new file +3 -0
@@ -0,0 +1,3 @@ +power on +power off +quit
client/scripts/power-on.bt new file +3 -0
@@ -0,0 +1,3 @@ +power on +show +quit
client/scripts/preset-custom.bt new file +26 -0
@@ -0,0 +1,26 @@ +endpoint.presets 00002bc9-0000-1000-8000-00805f9b34fb custom +48 +10 +3 +100 +Low +10000 +Unframed +2M +200 +3 +10 +20000 +endpoint.presets 00002bcb-0000-1000-8000-00805f9b34fb custom +48 +10 +3 +100 +Low +10000 +Unframed +2M +200 +3 +10 +20000
client/scripts/scan-bredr.bt new file +2 -0
@@ -0,0 +1,2 @@ +power on +scan bredr
client/scripts/scan-le.bt new file +2 -0
@@ -0,0 +1,2 @@ +power on +scan le
client/scripts/scan-on-off.bt new file +4 -0
@@ -0,0 +1,4 @@ +scan on +show +scan off +quit
client/scripts/scan-on.bt new file +2 -0
@@ -0,0 +1,2 @@ +power on +scan on
Signature · 1 line
2.39.2
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=732537
---Test result---
Test Summary:
CheckPatch FAIL 1.39 seconds GitLint PASS 0.70 seconds BuildEll PASS 27.31 seconds BluezMake PASS 969.44 seconds MakeCheck PASS 11.74 seconds MakeDistcheck PASS 150.88 seconds CheckValgrind PASS 249.36 seconds CheckSmatch WARNING 331.54 seconds bluezmakeextell PASS 99.48 seconds IncrementalBuild PASS 1621.23 seconds ScanBuild WARNING 1047.57 seconds
Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[v2,1/2] shared/shell: Add support for -i/--init-script ERROR:SPACING: space required after that ',' (ctx:VxV) #188: FILE: src/shared/shell.c:1131:
+ { "init-script",required_argument, 0, 'i' },
^
/github/workspace/src/src/13183442.patch total: 1 errors, 0 warnings, 290 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
/github/workspace/src/src/13183442.patch has style problems, please review.
NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
##############################
Test: CheckSmatch - WARNING
Desc: Run smatch tool with source
Output:src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c:615:21: warning: non-ANSI function declaration of function 'bt_shell_usage'src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c:615:21: warning: non-ANSI function declaration of function 'bt_shell_usage'src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c:615:21: warning: non-ANSI function declaration of function 'bt_shell_usage'
##############################
Test: ScanBuild - WARNING
Desc: Run Scan Build
Output:src/shared/shell.c:1228:13: warning: Access to field 'options' results in a dereference of a null pointer (loaded from variable 'opt')
if (c != opt->options[index - offset].val) {
^~~~~~~~~~~~1 warning generated.
---
Regards,
Linux Bluetooth
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:
Hello:
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 14 lines · On Tue, 21 Mar 2023 18:13:48 -0700 you wrote:
From: Luiz Augusto von Dentz <[email protected]>
This adds support for -i/--init-script which can be used to provide a file with commands to be initialized, the commands are then run in sequence after completing:
client/bluetoothctl -i client/power-on-off.bt Agent registered
Changing power on succeeded
[CHG] Controller A8:7E:EA:56:87:D5 Pairable: yes [CHG] Controller 98:8D:46:EE:6D:16 Pairable: yes [CHG] Controller 98:8D:46:EE:6D:16 PowerState: on-disabling AdvertisementMonitor path registered
Quoted 1 line
[...]
Here is the summary with links:
- [v2,1/2] shared/shell: Add support for -i/--init-script (no matching commit)
- [v2,2/2] client: Add samples init scripts
https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=fb1c694100b2
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html