diff mbox series

[BlueZ,v6,1/6] adapter: Keep track of whether the adapter is rfkill'ed

Message ID 20220901104331.174588-1-hadess@hadess.net (mailing list archive)
State Superseded
Headers show
Series [BlueZ,v6,1/6] adapter: Keep track of whether the adapter is rfkill'ed | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
tedd_an/checkpatch success Checkpatch PASS
tedd_an/gitlint success Gitlint PASS
tedd_an/setupell success Setup ELL PASS
tedd_an/buildprep success Build Prep PASS
tedd_an/build success Build Configuration PASS
tedd_an/makecheck success Make Check PASS
tedd_an/makecheckvalgrind success Make Check PASS
tedd_an/makedistcheck success Make Distcheck PASS
tedd_an/build_extell success Build External ELL PASS
tedd_an/build_extell_make success Build Make with External ELL PASS
tedd_an/incremental_build success Pass
tedd_an/scan_build success Pass

Commit Message

Bastien Nocera Sept. 1, 2022, 10:43 a.m. UTC
Instead of only replying to D-Bus requests with an error saying the
adapter is blocked, keep track of the rfkill being enabled or disabled
so we know the rfkill state of the adapter at all times.
---
 src/adapter.c | 25 +++++++++++++++++--
 src/adapter.h |  1 +
 src/btd.h     |  1 +
 src/rfkill.c  | 68 +++++++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 91 insertions(+), 4 deletions(-)

Comments

Bastien Nocera Sept. 1, 2022, 11:07 a.m. UTC | #1
On Thu, 2022-09-01 at 12:43 +0200, Bastien Nocera wrote:
> diff --git a/src/rfkill.c b/src/rfkill.c
> index 2099c5ac5..511e351b8 100644
> --- a/src/rfkill.c
> +++ b/src/rfkill.c
> @@ -55,12 +55,73 @@ struct rfkill_event {
>  };
>  #define RFKILL_EVENT_SIZE_V1    8
>  
> +int rfkill_get_blocked(uint16_t index)
> +{
> +       struct rfkill_event event = { 0 };
> +       int fd;
> +       ssize_t len;
> +       int blocked = -1;

I simplified this code in v7 (as well as fixing indentation problems).
bluez.test.bot@gmail.com Sept. 1, 2022, 11:32 a.m. UTC | #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=673181

---Test result---

Test Summary:
CheckPatch                    FAIL      6.85 seconds
GitLint                       PASS      4.57 seconds
Prep - Setup ELL              PASS      26.48 seconds
Build - Prep                  PASS      0.70 seconds
Build - Configure             PASS      8.06 seconds
Build - Make                  PASS      714.85 seconds
Make Check                    PASS      11.37 seconds
Make Check w/Valgrind         PASS      284.31 seconds
Make Distcheck                PASS      232.78 seconds
Build w/ext ELL - Configure   PASS      8.10 seconds
Build w/ext ELL - Make        PASS      81.59 seconds
Incremental Build w/ patches  PASS      578.06 seconds
Scan Build                    PASS      585.92 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script with rule in .checkpatch.conf
Output:
[BlueZ,v6,2/6] adapter: Implement PowerState property
WARNING:LONG_LINE: line length of 81 exceeds 80 columns
#137: FILE: src/adapter.c:659:
+static void reset_power_state_target(struct btd_adapter *adapter, uint32_t value)

WARNING:LONG_LINE: line length of 90 exceeds 80 columns
#177: FILE: src/adapter.c:714:
+			adapter_set_power_state(adapter, ADAPTER_POWER_STATE_OFF_BLOCKED);

WARNING:LONG_LINE: line length of 82 exceeds 80 columns
#191: FILE: src/adapter.c:758:
+						ADAPTER_POWER_STATE_OFF_ENABLING :

WARNING:LONG_LINE: line length of 82 exceeds 80 columns
#192: FILE: src/adapter.c:759:
+						ADAPTER_POWER_STATE_ON_DISABLING);

WARNING:LONG_LINE: line length of 90 exceeds 80 columns
#233: FILE: src/adapter.c:3025:
+			adapter_set_power_state(adapter, ADAPTER_POWER_STATE_OFF_BLOCKED);

WARNING:LONG_LINE: line length of 86 exceeds 80 columns
#326: FILE: src/adapter.c:6830:
+		DBG("Power state: %s", adapter_power_state_str(adapter->power_state));

/github/workspace/src/12962285.patch total: 0 errors, 6 warnings, 278 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/12962285.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.




---
Regards,
Linux Bluetooth
diff mbox series

Patch

diff --git a/src/adapter.c b/src/adapter.c
index 51b099dae..7c11a688d 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -250,6 +250,7 @@  struct btd_adapter {
 	uint32_t dev_class;		/* controller class of device */
 	char *name;			/* controller device name */
 	char *short_name;		/* controller short name */
+	bool blocked;			/* whether rfkill is enabled */
 	uint32_t supported_settings;	/* controller supported settings */
 	uint32_t pending_settings;	/* pending controller settings */
 	uint32_t current_settings;	/* current controller settings */
@@ -654,6 +655,8 @@  static void set_mode_complete(uint8_t status, uint16_t length,
 	if (status != MGMT_STATUS_SUCCESS) {
 		btd_error(adapter->dev_id, "Failed to set mode: %s (0x%02x)",
 						mgmt_errstr(status), status);
+		if (status == MGMT_STATUS_RFKILLED)
+			adapter->blocked = true;
 		adapter->pending_settings &= ~data->setting;
 		return;
 	}
@@ -2947,10 +2950,12 @@  static void property_set_mode_complete(uint8_t status, uint16_t length,
 		btd_error(adapter->dev_id, "Failed to set mode: %s (0x%02x)",
 						mgmt_errstr(status), status);
 
-		if (status == MGMT_STATUS_RFKILLED)
+		if (status == MGMT_STATUS_RFKILLED) {
 			dbus_err = ERROR_INTERFACE ".Blocked";
-		else
+			adapter->blocked = true;
+		} else {
 			dbus_err = ERROR_INTERFACE ".Failed";
+		}
 
 		g_dbus_pending_property_error(data->id, dbus_err,
 							mgmt_errstr(status));
@@ -6681,6 +6686,7 @@  static void load_config(struct btd_adapter *adapter)
 static struct btd_adapter *btd_adapter_new(uint16_t index)
 {
 	struct btd_adapter *adapter;
+	int blocked;
 
 	adapter = g_try_new0(struct btd_adapter, 1);
 	if (!adapter)
@@ -6689,6 +6695,9 @@  static struct btd_adapter *btd_adapter_new(uint16_t index)
 	adapter->dev_id = index;
 	adapter->mgmt = mgmt_ref(mgmt_primary);
 	adapter->pincode_requested = false;
+	blocked = rfkill_get_blocked(index);
+	if (blocked > 0)
+		adapter->blocked = true;
 
 	/*
 	 * Setup default configuration values. These are either adapter
@@ -6714,6 +6723,8 @@  static struct btd_adapter *btd_adapter_new(uint16_t index)
 	DBG("Modalias: %s", adapter->modalias);
 	DBG("Discoverable timeout: %u seconds", adapter->discoverable_timeout);
 	DBG("Pairable timeout: %u seconds", adapter->pairable_timeout);
+	if (blocked > 0)
+		DBG("Blocked: yes");
 
 	adapter->auths = g_queue_new();
 	adapter->exps = queue_new();
@@ -7581,6 +7592,9 @@  int btd_cancel_authorization(guint id)
 
 int btd_adapter_restore_powered(struct btd_adapter *adapter)
 {
+	if (adapter->blocked)
+		adapter->blocked = false;
+
 	if (btd_adapter_get_powered(adapter))
 		return 0;
 
@@ -7589,6 +7603,13 @@  int btd_adapter_restore_powered(struct btd_adapter *adapter)
 	return 0;
 }
 
+int btd_adapter_set_blocked(struct btd_adapter *adapter)
+{
+	if (!adapter->blocked)
+		adapter->blocked = true;
+	return 0;
+}
+
 void btd_adapter_register_pin_cb(struct btd_adapter *adapter,
 							btd_adapter_pin_cb_t cb)
 {
diff --git a/src/adapter.h b/src/adapter.h
index f38f473b7..78eb069ae 100644
--- a/src/adapter.h
+++ b/src/adapter.h
@@ -144,6 +144,7 @@  guint btd_request_authorization_cable_configured(const bdaddr_t *src, const bdad
 int btd_cancel_authorization(guint id);
 
 int btd_adapter_restore_powered(struct btd_adapter *adapter);
+int btd_adapter_set_blocked(struct btd_adapter *adapter);
 
 typedef ssize_t (*btd_adapter_pin_cb_t) (struct btd_adapter *adapter,
 			struct btd_device *dev, char *out, bool *display,
diff --git a/src/btd.h b/src/btd.h
index c02b2691e..63be6d8d4 100644
--- a/src/btd.h
+++ b/src/btd.h
@@ -144,6 +144,7 @@  void plugin_cleanup(void);
 
 void rfkill_init(void);
 void rfkill_exit(void);
+int rfkill_get_blocked(uint16_t index);
 
 GKeyFile *btd_get_main_conf(void);
 bool btd_kernel_experimental_enabled(const char *uuid);
diff --git a/src/rfkill.c b/src/rfkill.c
index 2099c5ac5..511e351b8 100644
--- a/src/rfkill.c
+++ b/src/rfkill.c
@@ -55,12 +55,73 @@  struct rfkill_event {
 };
 #define RFKILL_EVENT_SIZE_V1    8
 
+int rfkill_get_blocked(uint16_t index)
+{
+	struct rfkill_event event = { 0 };
+	int fd;
+	ssize_t len;
+	int blocked = -1;
+
+	fd = open("/dev/rfkill", O_RDWR);
+	if (fd < 0) {
+		DBG("Failed to open RFKILL control device");
+		return -1;
+	}
+
+	while (1) {
+		char sysname[PATH_MAX];
+		int namefd, id;
+
+		len = read(fd, &event, sizeof(event));
+		if (len < 0) {
+			if (errno == EAGAIN)
+				return -1;
+			return -1;
+		}
+
+		if (len < RFKILL_EVENT_SIZE_V1)
+			return -1;
+
+		snprintf(sysname, sizeof(sysname) - 1,
+				"/sys/class/rfkill/rfkill%u/name", event.idx);
+
+		namefd = open(sysname, O_RDONLY);
+		if (namefd < 0)
+			continue;
+
+		memset(sysname, 0, sizeof(sysname));
+
+		if (read(namefd, sysname, sizeof(sysname) - 1) < 4) {
+			close(namefd);
+			continue;
+		}
+
+		close(namefd);
+
+		if (g_str_has_prefix(sysname, "hci") == FALSE)
+			continue;
+
+		id = atoi(sysname + 3);
+		if (id < 0)
+			continue;
+
+		if (index == id) {
+			blocked = event.soft || event.hard;
+			break;
+		}
+	}
+	close(fd);
+
+	return blocked;
+}
+
 static gboolean rfkill_event(GIOChannel *chan,
 				GIOCondition cond, gpointer data)
 {
 	struct rfkill_event event = { 0 };
 	struct btd_adapter *adapter;
 	char sysname[PATH_MAX];
+	bool blocked = false;
 	ssize_t len;
 	int fd, id;
 
@@ -84,7 +145,7 @@  static gboolean rfkill_event(GIOChannel *chan,
 						event.soft, event.hard);
 
 	if (event.soft || event.hard)
-		return TRUE;
+		blocked = true;
 
 	if (event.op != RFKILL_OP_CHANGE)
 		return TRUE;
@@ -122,7 +183,10 @@  static gboolean rfkill_event(GIOChannel *chan,
 
 	DBG("RFKILL unblock for hci%d", id);
 
-	btd_adapter_restore_powered(adapter);
+	if (blocked)
+		btd_adapter_set_blocked(adapter);
+	else
+		btd_adapter_restore_powered(adapter);
 
 	return TRUE;
 }