Add new commands in btmgmt to support adv monitor
v4 cover Add new commands in btmgmt to support adv monitor
Hi linux-bluetooth,
This series of patches add support for new advertisement monitor mgmt opcodes by introducing new btmgmt commands into a new submenu 'monitor'. The new commands are ‘features’, ‘add’, and ‘remove’. They provide the ability for users to query supported advertisement monitoring features and add/remove monitor filters through btmgmt console.
Changes in v4:
- Fix endianness
- Use regular options instead of optget options per feedback, and fix endianness
Changes in v3:
- Fix build errors
Changes in v2:
- Move features command into submenu and fix build warnings
- Move remove command into submenu and fix build warnings
- Move add command into submenu and fix build warnings
Michael Sun (3):
btmgmt: Add "monitor" btmgmt submenu and command "features"
btmgmt: Add command "remove" into "monitor" btmgmt submenu
btmgmt: Add command "add" into "monitor" btmgmt submenu
tools/btmgmt.c | 268 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 268 insertions(+)
Signature · 1 line
2.27.0.111.gc72c7da667-goog
PATCH v4 1/3 btmgmt: Add "monitor" btmgmt submenu and command "features"
This patch introduces a new submenu "monitor" along with command ‘features’ to help user query for supported advertisement features. The command will work with the new MGMT opcode MGMT_OP_READ_ADV_MONITOR_FEATURES.
Tested on Atlas Chromebook with a sample stdout below:
[mgmt]# features
Supported features:
None
Enabled features:
None
Max number of handles: 32
Max number of patterns: 16
Handles list with 2 items:
0x0001
0x0002---
Changes in v4: None
Changes in v3: None
Changes in v2:
- Move features command into submenu and fix build warnings
tools/btmgmt.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+)
tools/btmgmt.c +88 -0
@@ -4567,6 +4567,83 @@ static void cmd_wbs(int argc, char **argv)cmd_setting (MGMT_OP_SET_WIDEBAND_SPEECH , argc, argv); } +static const char *const advmon_features_str[] = { +"Pattern monitor with logic OR." , +}; + +static const char *advmon_features2str (uint32_t features) +{ +static char str[512 ]; +unsigned int off, i; + + off =0 ; +snprintf (str,sizeof (str),"\n\tNone" ); + +for (i =0 ; i <NELEM (advmon_features_str); i++) { +if ((features & (1 << i)) !=0 && off <sizeof (str)) + off +=snprintf (str + off,sizeof (str) - off,"\n\t%s" , + advmon_features_str[i]); + } + +return str; +} + +static void advmon_features_rsp (uint8_t status,uint16_t len,const void *param, +void *user_data) +{ +const struct mgmt_rp_read_adv_monitor_features *rp = param; +uint32_t supported_features, enabled_features; +uint16_t num_handles; +int i; + +if (status !=MGMT_STATUS_SUCCESS ) { +error ("Reading adv monitor features failed with status 0x%02x " +"(%s)" , status,mgmt_errstr (status)); +return bt_shell_noninteractive_quit (EXIT_FAILURE ); + } + +if (len <sizeof (*rp)) { +error ("Too small adv monitor features reply (%u bytes)" , len); +return bt_shell_noninteractive_quit (EXIT_FAILURE ); + } + + supported_features =le32_to_cpu (rp->supported_features); + enabled_features =le32_to_cpu (rp->enabled_features); + num_handles =le16_to_cpu (rp->num_handles); + +if (len <sizeof (*rp) + num_handles *sizeof (uint16_t )) { +error ("Handles count (%u) doesn't match reply length (%u)" , + num_handles, len); +return bt_shell_noninteractive_quit (EXIT_FAILURE ); + } + +"Supported features:%s" ,advmon_features2str (supported_features)); +"Enabled features:%s" ,advmon_features2str (enabled_features)); +"Max number of handles: %u" ,le16_to_cpu (rp->max_num_handles)); +"Max number of patterns: %u" , rp->max_num_patterns); +"Handles list with %u item%s" , num_handles, + num_handles ==0 ?"" : num_handles ==1 ?":" :"s:" ); +for (i =0 ; i < num_handles; i++) +"\t0x%04x " ,le16_to_cpu (rp->handles[i])); + +return bt_shell_noninteractive_quit (EXIT_SUCCESS ); +} + +static void cmd_advmon_features (int argc,char **argv) +{ +uint16_t index; + + index = mgmt_index; +if (index ==MGMT_INDEX_NONE ) + index =0 ; + +if (!mgmt_send (mgmt,MGMT_OP_READ_ADV_MONITOR_FEATURES , index,0 ,NULL , + advmon_features_rsp,NULL ,NULL )) { +error ("Unable to send advertising monitor features command" ); +return bt_shell_noninteractive_quit (EXIT_FAILURE ); + } +} +static void register_mgmt_callbacks (struct mgmt *mgmt,uint16_t index) { mgmt_register(mgmt, MGMT_EV_CONTROLLER_ERROR, index, controller_error,
@@ -4635,6 +4712,16 @@ static void cmd_select(int argc, char **argv)update_prompt (mgmt_index); } +static const struct bt_shell_menu monitor_menu = { + .name ="monitor" , + .desc ="Advertisement Monitor Submenu" , + .entries = { + {"features" ,NULL , + cmd_advmon_features,"Show advertisement monitor " +"features" }, + { } }, +}; +static const struct bt_shell_menu main_menu = { .name ="main" , .entries = {
@@ -4815,6 +4902,7 @@ int main(int argc, char *argv[])bt_shell_init (argc, argv, &opt);bt_shell_set_menu (&main_menu); +bt_shell_add_submenu (&monitor_menu); mgmt =mgmt_new_default ();if (!mgmt) {
Signature · 1 line
2.27.0.111.gc72c7da667-goog
PATCH v4 2/3 btmgmt: Add command "remove" into "monitor" btmgmt submenu
In reply to Michael SunThis patch introduces a new btmgmt command ‘remove’ wihtin "monitor" submenu to allow users to remove previously added filters along with a event handler to handle kernel issued MGMT_EV_ADV_MONITOR_REMOVED event. The command will work with the new
MGMT opcode MGMT_OP_REMOVE_ADV_MONITOR.
Tested on Atlas Chromebook with a sample stdout below:
[mgmt]# remove 1
Advertisement monitor with handle: 0x0001 removed
[mgmt]# remove 1234
Could not remove advertisement monitor with status 0x11 (Invalid Index)
---
Changes in v4:
- Fix endianness
Changes in v3: None
Changes in v2:
- Move remove command into submenu and fix build warnings
tools/btmgmt.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+)
tools/btmgmt.c +56 -0
@@ -1013,6 +1013,20 @@ static void advertising_removed(uint16_t index, uint16_t len,"hci%u advertising_removed: instance %u" , index, ev->instance); } +static void advmon_removed (uint16_t index,uint16_t len,const void *param, +void *user_data) +{ +const struct mgmt_ev_adv_monitor_removed *ev = param; + +if (len <sizeof (*ev)) { +error ("Too small (%u bytes) %s event" , len, __func__); +return ; + } + +"hci%u %s: handle %u" , index, __func__, +le16_to_cpu (ev->monitor_handle)); +} +static void version_rsp (uint8_t status,uint16_t len,const void *param,void *user_data) {
@@ -4644,6 +4658,44 @@ static void cmd_advmon_features(int argc, char **argv) } } +static void advmon_remove_rsp (uint8_t status,uint16_t len,const void *param, +void *user_data) +{ +const struct mgmt_rp_remove_adv_monitor *rp = param; + +if (status !=MGMT_STATUS_SUCCESS ) { +error ("Could not remove advertisement monitor with status " +"0x%02x (%s)" , status,mgmt_errstr (status)); +return bt_shell_noninteractive_quit (EXIT_FAILURE ); + } + +"Advertisement monitor with handle: 0x%04x removed" , +le16_to_cpu (rp->monitor_handle)); +return bt_shell_noninteractive_quit (EXIT_SUCCESS ); +} + +static void cmd_advmon_remove (int argc,char **argv) +{ +struct mgmt_cp_remove_adv_monitor cp; +uint16_t index, monitor_handle; + + index = mgmt_index; +if (index ==MGMT_INDEX_NONE ) + index =0 ; + +if (sscanf (argv[1 ],"%hx" , &monitor_handle) !=1 ) { +error ("Wrong formatted handle argument" ); +return bt_shell_noninteractive_quit (EXIT_FAILURE ); + } + + cp.monitor_handle =cpu_to_le16 (monitor_handle); +if (mgmt_send (mgmt,MGMT_OP_REMOVE_ADV_MONITOR , index,sizeof (cp), &cp, + advmon_remove_rsp,NULL ,NULL ) ==0 ) { +error ("Unable to send appearance cmd" ); +return bt_shell_noninteractive_quit (EXIT_FAILURE ); + } +} +static void register_mgmt_callbacks (struct mgmt *mgmt,uint16_t index) { mgmt_register(mgmt, MGMT_EV_CONTROLLER_ERROR, index, controller_error,
@@ -4696,6 +4748,8 @@ static void register_mgmt_callbacks(struct mgmt *mgmt, uint16_t index) advertising_added, NULL, NULL);mgmt_register (mgmt,MGMT_EV_ADVERTISING_REMOVED , index, advertising_removed,NULL ,NULL ); +mgmt_register (mgmt,MGMT_EV_ADV_MONITOR_REMOVED , index, advmon_removed, +NULL ,NULL ); }static void cmd_select (int argc,char **argv)
@@ -4719,6 +4773,8 @@ static const struct bt_shell_menu monitor_menu = { {"features" ,NULL , cmd_advmon_features,"Show advertisement monitor " "features" }, + { "remove", "<handle>", + cmd_advmon_remove, "Remove advertisement monitor " }, { } }, };
Signature · 1 line
2.27.0.111.gc72c7da667-goog
RE: [bluez,v4,2/3] btmgmt: Add command "remove" into "monitor" btmgmt submenu
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. While we are preparing for reviewing the patches, we found the following issue/warning.
Test Result:
checkpatch Failed
Outputs:
WARNING:SSCANF_TO_KSTRTO: Prefer kstrto<type> to single variable sscanf #78: FILE: tools/btmgmt.c:4686:
+ if (sscanf(argv[1], "%hx", &monitor_handle) != 1) {
+ error("Wrong formatted handle argument");
+ return bt_shell_noninteractive_quit(EXIT_FAILURE);
+ }
- total: 0 errors, 1 warnings, 80 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.
Your patch has style problems, please review.
NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPLIT_STRING
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
---
Regards,
Linux Bluetooth
Luiz Augusto von Dentz
maintainerRe: [bluez,v4,2/3] btmgmt: Add command "remove" into "monitor" btmgmt submenu
Hi Tedd,
Quoted 16 lines · On Fri, Jun 19, 2020 at 9:57 PM <[email protected]> wrote:
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. While we are preparing for reviewing the patches, we found the following issue/warning.
Test Result:
checkpatch FailedOutputs:
WARNING:SSCANF_TO_KSTRTO: Prefer kstrto<type> to single variable sscanf #78: FILE:tools/btmgmt.c:4686:
tools/btmgmt.c static void cmd_advmon_features(int argc, char **argv) · line 4689
Quoted 4 lines
tools/btmgmt.c +4
-0+if (sscanf (argv[1 ],"%hx" , &monitor_handle) !=1 ) { +error ("Wrong formatted handle argument" ); +return bt_shell_noninteractive_quit (EXIT_FAILURE ); + }
We might want to turn off this warning since it doesn't apply to userspace where kstrto is not available.
Quoted 17 lines
- total: 0 errors, 1 warnings, 80 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.Your patch has style problems, please review.
NOTE: Ignored message types:
COMMIT_MESSAGECOMPLEX_MACROCONST_STRUCTFILE_PATH_CHANGESMISSING_SIGN_OFFPREFER_PACKEDSPLIT_STRINGNOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH inMAINTAINERS.---
Regards,
Linux Bluetooth
Signature · 1 line
Luiz Augusto von Dentz
Re: [bluez,v4,2/3] btmgmt: Add command "remove" into "monitor" btmgmt submenu
Hi Luiz,
Quoted 23 lines · Luiz Augusto von Dentz
Hi Tedd,
Quoted 15 lines · On Fri, Jun 19, 2020 at 9:57 PM <[email protected]> wrote:
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. While we are preparing for reviewing the patches, we found the following issue/warning.
Test Result:
checkpatch FailedOutputs:
WARNING:SSCANF_TO_KSTRTO: Prefer kstrto<type> to single variable sscanf #78: FILE:tools/btmgmt.c:4686:tools/btmgmt.c static void cmd_advmon_features(int argc, char **argv) · line 4689
Quoted 4 lines
tools/btmgmt.c +4
-0+if (sscanf (argv[1 ],"%hx" , &monitor_handle) !=1 ) { +error ("Wrong formatted handle argument" ); +return bt_shell_noninteractive_quit (EXIT_FAILURE ); + }
Quoted 2 lines
We might want to turn off this warning since it doesn't apply to userspace where kstrto is not available.
I made a change.
Also, I submit the patch to add .checkpatch.conf in the tree which will also help others to run checkpatch before submissing.
Once it is checked in, I will update the CI.
Quoted 12 lines
Quoted 12 lines
- total: 0 errors, 1 warnings, 80 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.Your patch has style problems, please review.
NOTE: Ignored message types:
COMMIT_MESSAGECOMPLEX_MACROCONST_STRUCTFILE_PATH_CHANGESMISSING_SIGN_OFFPREFER_PACKEDSPLIT_STRING
Quoted 10 lines
Quoted 8 lines
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH inMAINTAINERS.---
Regards,
Linux Bluetooth
Regards,
Tedd Ho-Jeong An
PATCH v4 3/3 btmgmt: Add command "add" into "monitor" btmgmt submenu
In reply to Michael SunThis patch introduces a new command ‘add’ within "monitor" submenu to allow users to add advertisement monitor filters and get back monitor handlers. An event handler is also added to handle kernel issued MGMT_EV_ADV_MONITOR_ADDED events. The command will work with the new MGMT opcode MGMT_OP_ADD_ADV_MONITOR. This patch only adds support for adding pattern based filters.
Tested on Atlas Chromebook with a sample stdout below:
[mgmt]# add -p 0:1:c504 1:a:9a55beef Advertisement monitor with handle:0x0005 added [mgmt]# add -p 0:1:xxxx
Failed to parse monitor patterns.
Usage: add <-p|-h> [options...]
Monitor Types:
-p <ad_type:offset:pattern>... Pattern Monitor
e.g.:
add -p 0:1:c504 1:a:9a55beef
---
Changes in v4:
- Use regular options instead of optget options per feedback, and fix endianness
Changes in v3:
- Fix build errors
Changes in v2:
- Move add command into submenu and fix build warnings
tools/btmgmt.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+)
tools/btmgmt.c +124 -0
@@ -1013,6 +1013,20 @@ static void advertising_removed(uint16_t index, uint16_t len,"hci%u advertising_removed: instance %u" , index, ev->instance); } +static void advmon_added (uint16_t index,uint16_t len,const void *param, +void *user_data) +{ +const struct mgmt_ev_adv_monitor_added *ev = param; + +if (len <sizeof (*ev)) { +error ("Too small (%u bytes) %s event" , len, __func__); +return ; + } + +"hci%u %s: handle %u" , index, __func__, +le16_to_cpu (ev->monitor_handle)); +} +static void advmon_removed (uint16_t index,uint16_t len,const void *param,void *user_data) {
@@ -4658,6 +4672,112 @@ static void cmd_advmon_features(int argc, char **argv) } } +static void advmon_add_rsp (uint8_t status,uint16_t len,const void *param, +void *user_data) +{ +const struct mgmt_rp_add_adv_patterns_monitor *rp = param; + +if (status !=MGMT_STATUS_SUCCESS ) { +error ("Could not add advertisement monitor with status " +"0x%02x (%s)" , status,mgmt_errstr (status)); +return bt_shell_noninteractive_quit (EXIT_FAILURE ); + } + +"Advertisement monitor with handle:0x%04x added" , +le16_to_cpu (rp->monitor_handle)); +return bt_shell_noninteractive_quit (EXIT_SUCCESS ); +} + +static bool str2pattern (struct mgmt_adv_pattern *pattern,const char *str) +{ +int type_len, offset_len, offset_end_pos, str_len; +int i, j; +char pattern_str[62 ] = {0 }; +char tmp; + +if (sscanf (str,"%2hhx%n:%2hhx%n:%s" , &pattern->ad_type, &type_len, + &pattern->offset, &offset_end_pos, pattern_str) !=3 ) +return false; + + offset_len = offset_end_pos - type_len -1 ; + str_len =strlen (pattern_str); + pattern->length = str_len /2 + str_len %2 ; + +if (type_len >2 || offset_len >2 || + pattern->offset + pattern->length >31 ) +return false; + +for (i =0 , j =0 ; i < str_len; i++, j++) { +if (sscanf (&pattern_str[i++],"%2hhx" , &pattern->value[j]) + !=1 ) +return false; +if (i < str_len &&sscanf (&pattern_str[i],"%1hhx" , &tmp) !=1 ) +return false; + } + +return true; +} + +static void advmon_add_usage (void ) +{ +bt_shell_usage (); +"Monitor Types:\n\t-p <ad_type:offset:pattern>..." +"\tPattern Monitor\ne.g.:\n\tadd -p 0:1:c504 1:a:9a55beef" ); +} + +static bool advmon_add_pattern (int argc,char **argv) +{ +uint16_t index; +int i, cp_len; +struct mgmt_cp_add_adv_monitor *cp =NULL ; +bool success = false; + + index = mgmt_index; +if (index ==MGMT_INDEX_NONE ) + index =0 ; + + cp_len =sizeof (struct mgmt_cp_add_adv_monitor ) + + argc *sizeof (struct mgmt_adv_pattern ); + + cp =malloc0 (cp_len); + cp->pattern_count = argc; + +for (i =0 ; i < argc; i++) { +if (!str2pattern (&cp->patterns[i], argv[i])) { +error ("Failed to parse monitor patterns." ); + goto done; + } + } + +if (!mgmt_send (mgmt,MGMT_OP_ADD_ADV_PATTERNS_MONITOR , index, cp_len, + cp, advmon_add_rsp,NULL ,NULL )) { +error ("Unable to send \"Add Advertising Monitor\" command" ); + goto done; + } + + success = true; + +done: +free (cp); +return success; +} + +static void cmd_advmon_add (int argc,char **argv) +{ +bool success = false; + +if (strcasecmp (argv[1 ],"-p" ) ==0 && argc >2 ) { + argc -=2 ; + argv +=2 ; + success =advmon_add_pattern (argc, argv); + } + +if (!success) { +advmon_add_usage (); +bt_shell_noninteractive_quit (EXIT_FAILURE ); + } +} +static void advmon_remove_rsp (uint8_t status,uint16_t len,const void *param,void *user_data) {
@@ -4748,6 +4868,8 @@ static void register_mgmt_callbacks(struct mgmt *mgmt, uint16_t index) advertising_added, NULL, NULL);mgmt_register (mgmt,MGMT_EV_ADVERTISING_REMOVED , index, advertising_removed,NULL ,NULL ); +mgmt_register (mgmt,MGMT_EV_ADV_MONITOR_ADDED , index, advmon_added, +NULL ,NULL );mgmt_register (mgmt,MGMT_EV_ADV_MONITOR_REMOVED , index, advmon_removed,NULL ,NULL ); }
@@ -4775,6 +4897,8 @@ static const struct bt_shell_menu monitor_menu = { "features" }, {"remove" ,"<handle>" , cmd_advmon_remove, "Remove advertisement monitor " }, + { "add", "<-p|-h> [options...]", + cmd_advmon_add, "Add advertisement monitor" }, { } }, };
Signature · 1 line
2.27.0.111.gc72c7da667-goog
Hi Michael,
Quoted 28 lines · Michael Sun
Hi linux-bluetooth,
This series of patches add support for new advertisement monitor mgmt opcodes by introducing new btmgmt commands into a new submenu 'monitor'. The new commands are ‘features’, ‘add’, and ‘remove’. They provide the ability for users to query supported advertisement monitoring features and add/remove monitor filters through btmgmt console.
Changes in v4:
- Fix endianness
- Use regular options instead of optget options per feedback, and fix endiannessChanges in v3:
- Fix build errorsChanges in v2:
- Move features command into submenu and fix build warnings
- Move remove command into submenu and fix build warnings
- Move add command into submenu and fix build warningsMichael Sun (3):
btmgmt: Add "monitor" btmgmt submenu and command "features"
btmgmt: Add command "remove" into "monitor" btmgmt submenu
btmgmt: Add command "add" into "monitor" btmgmt submenu
Quoted 6 lines
tools/btmgmt.c | 268 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 268 insertions(+)--
2.27.0.111.gc72c7da667-goog
Applied, thanks.
Signature · 1 line
Luiz Augusto von Dentz