feat: merge subscriptions crate into cosmic-settings repo
This commit is contained in:
parent
a2f53f2239
commit
600720b7d1
47 changed files with 8399 additions and 63 deletions
15
subscriptions/bluetooth/Cargo.toml
Normal file
15
subscriptions/bluetooth/Cargo.toml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
[package]
|
||||
name = "cosmic-settings-bluetooth-subscription"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license = "MPL-2.0"
|
||||
rust-version.workspace = true
|
||||
publish = true
|
||||
|
||||
[dependencies]
|
||||
bluez-zbus = { git = "https://github.com/pop-os/dbus-settings-bindings" }
|
||||
futures = "0.3.31"
|
||||
iced_futures = { git = "https://github.com/pop-os/libcosmic" }
|
||||
tokio = "1.47.1"
|
||||
tracing = "0.1.41"
|
||||
zbus = "5.11.0"
|
||||
359
subscriptions/bluetooth/LICENSE.md
Normal file
359
subscriptions/bluetooth/LICENSE.md
Normal file
|
|
@ -0,0 +1,359 @@
|
|||
Mozilla Public License Version 2.0
|
||||
==================================
|
||||
|
||||
## 1. Definitions
|
||||
|
||||
### 1.1. "Contributor"
|
||||
means each individual or legal entity that creates, contributes to
|
||||
the creation of, or owns Covered Software.
|
||||
|
||||
### 1.2. "Contributor Version"
|
||||
means the combination of the Contributions of others (if any) used
|
||||
by a Contributor and that particular Contributor's Contribution.
|
||||
|
||||
### 1.3. "Contribution"
|
||||
means Covered Software of a particular Contributor.
|
||||
|
||||
### 1.4. "Covered Software"
|
||||
means Source Code Form to which the initial Contributor has attached
|
||||
the notice in Exhibit A, the Executable Form of such Source Code
|
||||
Form, and Modifications of such Source Code Form, in each case
|
||||
including portions thereof.
|
||||
|
||||
### 1.5. "Incompatible With Secondary Licenses"
|
||||
means
|
||||
|
||||
+ (a) that the initial Contributor has attached the notice described
|
||||
in Exhibit B to the Covered Software; or
|
||||
|
||||
+ (b) that the Covered Software was made available under the terms of
|
||||
version 1.1 or earlier of the License, but not also under the
|
||||
terms of a Secondary License.
|
||||
|
||||
### 1.6. "Executable Form"
|
||||
means any form of the work other than Source Code Form.
|
||||
|
||||
### 1.7. "Larger Work"
|
||||
means a work that combines Covered Software with other material, in
|
||||
a separate file or files, that is not Covered Software.
|
||||
|
||||
### 1.8. "License"
|
||||
means this document.
|
||||
|
||||
### 1.9. "Licensable"
|
||||
means having the right to grant, to the maximum extent possible,
|
||||
whether at the time of the initial grant or subsequently, any and
|
||||
all of the rights conveyed by this License.
|
||||
|
||||
### 1.10. "Modifications"
|
||||
means any of the following:
|
||||
|
||||
+ (a) any file in Source Code Form that results from an addition to,
|
||||
deletion from, or modification of the contents of Covered
|
||||
Software; or
|
||||
|
||||
+ (b) any new file in Source Code Form that contains any Covered
|
||||
Software.
|
||||
|
||||
### 1.11. "Patent Claims" of a Contributor
|
||||
means any patent claim(s), including without limitation, method,
|
||||
process, and apparatus claims, in any patent Licensable by such
|
||||
Contributor that would be infringed, but for the grant of the
|
||||
License, by the making, using, selling, offering for sale, having
|
||||
made, import, or transfer of either its Contributions or its
|
||||
Contributor Version.
|
||||
|
||||
### 1.12. "Secondary License"
|
||||
means either the GNU General Public License, Version 2.0, the GNU
|
||||
Lesser General Public License, Version 2.1, the GNU Affero General
|
||||
Public License, Version 3.0, or any later versions of those
|
||||
licenses.
|
||||
|
||||
### 1.13. "Source Code Form"
|
||||
means the form of the work preferred for making modifications.
|
||||
|
||||
### 1.14. "You" (or "Your")
|
||||
means an individual or a legal entity exercising rights under this
|
||||
License. For legal entities, "You" includes any entity that
|
||||
controls, is controlled by, or is under common control with You. For
|
||||
purposes of this definition, "control" means (a) the power, direct
|
||||
or indirect, to cause the direction or management of such entity,
|
||||
whether by contract or otherwise, or (b) ownership of more than
|
||||
fifty percent (50%) of the outstanding shares or beneficial
|
||||
ownership of such entity.
|
||||
|
||||
## 2. License Grants and Conditions
|
||||
|
||||
### 2.1. Grants
|
||||
|
||||
Each Contributor hereby grants You a world-wide, royalty-free,
|
||||
non-exclusive license:
|
||||
|
||||
+ (a) under intellectual property rights (other than patent or trademark)
|
||||
Licensable by such Contributor to use, reproduce, make available,
|
||||
modify, display, perform, distribute, and otherwise exploit its
|
||||
Contributions, either on an unmodified basis, with Modifications, or
|
||||
as part of a Larger Work; and
|
||||
|
||||
+ (b) under Patent Claims of such Contributor to make, use, sell, offer
|
||||
for sale, have made, import, and otherwise transfer either its
|
||||
Contributions or its Contributor Version.
|
||||
|
||||
### 2.2. Effective Date
|
||||
|
||||
The licenses granted in Section 2.1 with respect to any Contribution
|
||||
become effective for each Contribution on the date the Contributor first
|
||||
distributes such Contribution.
|
||||
|
||||
### 2.3. Limitations on Grant Scope
|
||||
|
||||
The licenses granted in this Section 2 are the only rights granted under
|
||||
this License. No additional rights or licenses will be implied from the
|
||||
distribution or licensing of Covered Software under this License.
|
||||
Notwithstanding Section 2.1(b) above, no patent license is granted by a
|
||||
Contributor:
|
||||
|
||||
+ (a) for any code that a Contributor has removed from Covered Software;
|
||||
or
|
||||
|
||||
+ (b) for infringements caused by: (i) Your and any other third party's
|
||||
modifications of Covered Software, or (ii) the combination of its
|
||||
Contributions with other software (except as part of its Contributor
|
||||
Version); or
|
||||
|
||||
+ (c) under Patent Claims infringed by Covered Software in the absence of
|
||||
its Contributions.
|
||||
|
||||
This License does not grant any rights in the trademarks, service marks,
|
||||
or logos of any Contributor (except as may be necessary to comply with
|
||||
the notice requirements in Section 3.4).
|
||||
|
||||
### 2.4. Subsequent Licenses
|
||||
|
||||
No Contributor makes additional grants as a result of Your choice to
|
||||
distribute the Covered Software under a subsequent version of this
|
||||
License (see Section 10.2) or under the terms of a Secondary License (if
|
||||
permitted under the terms of Section 3.3).
|
||||
|
||||
### 2.5. Representation
|
||||
|
||||
Each Contributor represents that the Contributor believes its
|
||||
Contributions are its original creation(s) or it has sufficient rights
|
||||
to grant the rights to its Contributions conveyed by this License.
|
||||
|
||||
### 2.6. Fair Use
|
||||
|
||||
This License is not intended to limit any rights You have under
|
||||
applicable copyright doctrines of fair use, fair dealing, or other
|
||||
equivalents.
|
||||
|
||||
### 2.7. Conditions
|
||||
|
||||
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
|
||||
in Section 2.1.
|
||||
|
||||
## 3. Responsibilities
|
||||
|
||||
### 3.1. Distribution of Source Form
|
||||
|
||||
All distribution of Covered Software in Source Code Form, including any
|
||||
Modifications that You create or to which You contribute, must be under
|
||||
the terms of this License. You must inform recipients that the Source
|
||||
Code Form of the Covered Software is governed by the terms of this
|
||||
License, and how they can obtain a copy of this License. You may not
|
||||
attempt to alter or restrict the recipients' rights in the Source Code
|
||||
Form.
|
||||
|
||||
### 3.2. Distribution of Executable Form
|
||||
|
||||
If You distribute Covered Software in Executable Form then:
|
||||
|
||||
+ (a) such Covered Software must also be made available in Source Code
|
||||
Form, as described in Section 3.1, and You must inform recipients of
|
||||
the Executable Form how they can obtain a copy of such Source Code
|
||||
Form by reasonable means in a timely manner, at a charge no more
|
||||
than the cost of distribution to the recipient; and
|
||||
|
||||
+ (b) You may distribute such Executable Form under the terms of this
|
||||
License, or sublicense it under different terms, provided that the
|
||||
license for the Executable Form does not attempt to limit or alter
|
||||
the recipients' rights in the Source Code Form under this License.
|
||||
|
||||
### 3.3. Distribution of a Larger Work
|
||||
|
||||
You may create and distribute a Larger Work under terms of Your choice,
|
||||
provided that You also comply with the requirements of this License for
|
||||
the Covered Software. If the Larger Work is a combination of Covered
|
||||
Software with a work governed by one or more Secondary Licenses, and the
|
||||
Covered Software is not Incompatible With Secondary Licenses, this
|
||||
License permits You to additionally distribute such Covered Software
|
||||
under the terms of such Secondary License(s), so that the recipient of
|
||||
the Larger Work may, at their option, further distribute the Covered
|
||||
Software under the terms of either this License or such Secondary
|
||||
License(s).
|
||||
|
||||
### 3.4. Notices
|
||||
|
||||
You may not remove or alter the substance of any license notices
|
||||
(including copyright notices, patent notices, disclaimers of warranty,
|
||||
or limitations of liability) contained within the Source Code Form of
|
||||
the Covered Software, except that You may alter any license notices to
|
||||
the extent required to remedy known factual inaccuracies.
|
||||
|
||||
### 3.5. Application of Additional Terms
|
||||
|
||||
You may choose to offer, and to charge a fee for, warranty, support,
|
||||
indemnity or liability obligations to one or more recipients of Covered
|
||||
Software. However, You may do so only on Your own behalf, and not on
|
||||
behalf of any Contributor. You must make it absolutely clear that any
|
||||
such warranty, support, indemnity, or liability obligation is offered by
|
||||
You alone, and You hereby agree to indemnify every Contributor for any
|
||||
liability incurred by such Contributor as a result of warranty, support,
|
||||
indemnity or liability terms You offer. You may include additional
|
||||
disclaimers of warranty and limitations of liability specific to any
|
||||
jurisdiction.
|
||||
|
||||
## 4. Inability to Comply Due to Statute or Regulation
|
||||
|
||||
If it is impossible for You to comply with any of the terms of this
|
||||
License with respect to some or all of the Covered Software due to
|
||||
statute, judicial order, or regulation then You must: (a) comply with
|
||||
the terms of this License to the maximum extent possible; and (b)
|
||||
describe the limitations and the code they affect. Such description must
|
||||
be placed in a text file included with all distributions of the Covered
|
||||
Software under this License. Except to the extent prohibited by statute
|
||||
or regulation, such description must be sufficiently detailed for a
|
||||
recipient of ordinary skill to be able to understand it.
|
||||
|
||||
## 5. Termination
|
||||
|
||||
5.1. The rights granted under this License will terminate automatically
|
||||
if You fail to comply with any of its terms. However, if You become
|
||||
compliant, then the rights granted under this License from a particular
|
||||
Contributor are reinstated (a) provisionally, unless and until such
|
||||
Contributor explicitly and finally terminates Your grants, and (b) on an
|
||||
ongoing basis, if such Contributor fails to notify You of the
|
||||
non-compliance by some reasonable means prior to 60 days after You have
|
||||
come back into compliance. Moreover, Your grants from a particular
|
||||
Contributor are reinstated on an ongoing basis if such Contributor
|
||||
notifies You of the non-compliance by some reasonable means, this is the
|
||||
first time You have received notice of non-compliance with this License
|
||||
from such Contributor, and You become compliant prior to 30 days after
|
||||
Your receipt of the notice.
|
||||
|
||||
5.2. If You initiate litigation against any entity by asserting a patent
|
||||
infringement claim (excluding declaratory judgment actions,
|
||||
counter-claims, and cross-claims) alleging that a Contributor Version
|
||||
directly or indirectly infringes any patent, then the rights granted to
|
||||
You by any and all Contributors for the Covered Software under Section
|
||||
2.1 of this License shall terminate.
|
||||
|
||||
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
|
||||
end user license agreements (excluding distributors and resellers) which
|
||||
have been validly granted by You or Your distributors under this License
|
||||
prior to termination shall survive termination.
|
||||
|
||||
|
||||
## 6. Disclaimer of Warranty
|
||||
|
||||
**Covered Software is provided under this License on an "as is"
|
||||
basis, without warranty of any kind, either expressed, implied, or
|
||||
statutory, including, without limitation, warranties that the
|
||||
Covered Software is free of defects, merchantable, fit for a
|
||||
particular purpose or non-infringing. The entire risk as to the
|
||||
quality and performance of the Covered Software is with You.
|
||||
Should any Covered Software prove defective in any respect, You
|
||||
(not any Contributor) assume the cost of any necessary servicing,
|
||||
repair, or correction. This disclaimer of warranty constitutes an
|
||||
essential part of this License. No use of any Covered Software is
|
||||
authorized under this License except under this disclaimer.**
|
||||
|
||||
|
||||
#7. Limitation of Liability
|
||||
|
||||
**Under no circumstances and under no legal theory, whether tort
|
||||
(including negligence), contract, or otherwise, shall any
|
||||
Contributor, or anyone who distributes Covered Software as
|
||||
permitted above, be liable to You for any direct, indirect,
|
||||
special, incidental, or consequential damages of any character
|
||||
including, without limitation, damages for lost profits, loss of
|
||||
goodwill, work stoppage, computer failure or malfunction, or any
|
||||
and all other commercial damages or losses, even if such party
|
||||
shall have been informed of the possibility of such damages. This
|
||||
limitation of liability shall not apply to liability for death or
|
||||
personal injury resulting from such party's negligence to the
|
||||
extent applicable law prohibits such limitation. Some
|
||||
jurisdictions do not allow the exclusion or limitation of
|
||||
incidental or consequential damages, so this exclusion and
|
||||
limitation may not apply to You.**
|
||||
|
||||
|
||||
## 8. Litigation
|
||||
|
||||
Any litigation relating to this License may be brought only in the
|
||||
courts of a jurisdiction where the defendant maintains its principal
|
||||
place of business and such litigation shall be governed by laws of that
|
||||
jurisdiction, without reference to its conflict-of-law provisions.
|
||||
Nothing in this Section shall prevent a party's ability to bring
|
||||
cross-claims or counter-claims.
|
||||
|
||||
## 9. Miscellaneous
|
||||
|
||||
This License represents the complete agreement concerning the subject
|
||||
matter hereof. If any provision of this License is held to be
|
||||
unenforceable, such provision shall be reformed only to the extent
|
||||
necessary to make it enforceable. Any law or regulation which provides
|
||||
that the language of a contract shall be construed against the drafter
|
||||
shall not be used to construe this License against a Contributor.
|
||||
|
||||
## 10. Versions of the License
|
||||
|
||||
### 10.1. New Versions
|
||||
|
||||
Mozilla Foundation is the license steward. Except as provided in Section
|
||||
10.3, no one other than the license steward has the right to modify or
|
||||
publish new versions of this License. Each version will be given a
|
||||
distinguishing version number.
|
||||
|
||||
### 10.2. Effect of New Versions
|
||||
|
||||
You may distribute the Covered Software under the terms of the version
|
||||
of the License under which You originally received the Covered Software,
|
||||
or under the terms of any subsequent version published by the license
|
||||
steward.
|
||||
|
||||
### 10.3. Modified Versions
|
||||
|
||||
If you create software not governed by this License, and you want to
|
||||
create a new license for such software, you may create and use a
|
||||
modified version of this License if you rename the license and remove
|
||||
any references to the name of the license steward (except to note that
|
||||
such modified license differs from this License).
|
||||
|
||||
### 10.4. Distributing Source Code Form that is Incompatible With Secondary
|
||||
Licenses
|
||||
|
||||
If You choose to distribute Source Code Form that is Incompatible With
|
||||
Secondary Licenses under the terms of this version of the License, the
|
||||
notice described in Exhibit B of this License must be attached.
|
||||
|
||||
## Exhibit A - Source Code Form License Notice
|
||||
|
||||
|
||||
This Source Code Form is subject to the terms of the Mozilla Public
|
||||
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
If it is not possible or desirable to put the notice in a particular
|
||||
file, then You may include the notice in a location (such as a LICENSE
|
||||
file in a relevant directory) where a recipient would be likely to look
|
||||
for such a notice.
|
||||
|
||||
You may add additional accurate notices of copyright ownership.
|
||||
|
||||
## Exhibit B - "Incompatible With Secondary Licenses" Notice
|
||||
|
||||
|
||||
This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
defined by the Mozilla Public License, v. 2.0.
|
||||
|
||||
320
subscriptions/bluetooth/src/adapter.rs
Normal file
320
subscriptions/bluetooth/src/adapter.rs
Normal file
|
|
@ -0,0 +1,320 @@
|
|||
// Copyright 2024 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use crate::{Active, Event};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
hash::{Hash, Hasher},
|
||||
time::Duration,
|
||||
};
|
||||
use zbus::zvariant::OwnedObjectPath;
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct Adapter {
|
||||
pub alias: String,
|
||||
pub address: String,
|
||||
pub scanning: Active,
|
||||
pub enabled: Active,
|
||||
}
|
||||
|
||||
impl Hash for Adapter {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.address.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Adapter {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.address == other.address
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Adapter {}
|
||||
|
||||
impl Adapter {
|
||||
pub async fn from_device(
|
||||
proxy: &bluez_zbus::adapter1::Adapter1Proxy<'_>,
|
||||
) -> zbus::Result<Self> {
|
||||
let (address, alias, scanning, enabled) = futures::try_join!(
|
||||
proxy.address(),
|
||||
proxy.alias(),
|
||||
async {
|
||||
Ok(
|
||||
if proxy.discoverable().await? && proxy.discovering().await? {
|
||||
Active::Enabled
|
||||
} else {
|
||||
Active::Disabled
|
||||
},
|
||||
)
|
||||
},
|
||||
async {
|
||||
Ok(if proxy.powered().await? {
|
||||
Active::Enabled
|
||||
} else {
|
||||
Active::Disabled
|
||||
})
|
||||
}
|
||||
)?;
|
||||
|
||||
Ok(Self {
|
||||
alias,
|
||||
address,
|
||||
scanning,
|
||||
enabled,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn update(&mut self, updates: Vec<AdapterUpdate>) {
|
||||
for update in updates {
|
||||
match update {
|
||||
AdapterUpdate::Alias(alias) => self.alias = alias,
|
||||
AdapterUpdate::Address(address) => self.address = address,
|
||||
AdapterUpdate::Enabled(enabled) => {
|
||||
self.enabled = match (self.enabled, enabled) {
|
||||
(Active::Enabling, Active::Enabled) => Active::Enabled,
|
||||
(Active::Disabling, Active::Disabled) => Active::Disabled,
|
||||
(Active::Enabled | Active::Disabled, status) => status,
|
||||
(status, _) => status,
|
||||
}
|
||||
}
|
||||
AdapterUpdate::Scanning(scanning) => {
|
||||
self.scanning = match (self.scanning, scanning) {
|
||||
(Active::Enabling, Active::Enabled) => Active::Enabled,
|
||||
(Active::Disabling, Active::Disabled) => Active::Disabled,
|
||||
(Active::Enabled | Active::Disabled, status) => status,
|
||||
(status, _) => status,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum AdapterUpdate {
|
||||
Alias(String),
|
||||
Address(String),
|
||||
Scanning(Active),
|
||||
Enabled(Active),
|
||||
}
|
||||
|
||||
impl AdapterUpdate {
|
||||
#[must_use]
|
||||
pub fn from_update(update: HashMap<&'_ str, zbus::zvariant::Value<'_>>) -> Vec<Self> {
|
||||
update
|
||||
.into_iter()
|
||||
.filter_map(|(key, value)| {
|
||||
match (key, value) {
|
||||
("Alias", zbus::zvariant::Value::Str(value)) => Some(Self::Alias(value.into())),
|
||||
("Discovering" | "Discoverable", zbus::zvariant::Value::Bool(value)) => {
|
||||
Some(Self::Scanning(if value {
|
||||
Active::Enabled
|
||||
} else {
|
||||
Active::Disabled
|
||||
}))
|
||||
}
|
||||
("Powered", zbus::zvariant::Value::Bool(value)) => {
|
||||
Some(Self::Enabled(if value {
|
||||
Active::Enabled
|
||||
} else {
|
||||
Active::Disabled
|
||||
}))
|
||||
}
|
||||
("Address", zbus::zvariant::Value::Str(value)) => {
|
||||
Some(Self::Address(value.into()))
|
||||
}
|
||||
// Battery
|
||||
(message, value) => {
|
||||
tracing::error!(message, ?value, "adapter update");
|
||||
None
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start_discovery(connection: zbus::Connection, adapter_path: OwnedObjectPath) -> Event {
|
||||
let result: zbus::Result<()> = Ok(());
|
||||
|
||||
let adapter = match bluez_zbus::get_adapter(&connection, adapter_path).await {
|
||||
Err(why) => {
|
||||
tracing::error!("Unable to get the adapter: {why}");
|
||||
return Event::DBusError(why);
|
||||
}
|
||||
Ok(adapter) => adapter,
|
||||
};
|
||||
|
||||
for attempt in 1..5 {
|
||||
let result = async {
|
||||
tracing::debug!("Starting discovery");
|
||||
// We don't seem to be able to use join here as it seem to lead to some kind of race condition and not start scanning occasionally
|
||||
adapter.set_pairable(true).await?;
|
||||
adapter.set_discoverable(true).await?;
|
||||
if adapter.discovering().await? {
|
||||
return Ok(());
|
||||
}
|
||||
adapter.start_discovery().await
|
||||
}
|
||||
.await;
|
||||
|
||||
if let Err(why) = result {
|
||||
tracing::warn!("Unable to start bluetooth scanning: {why}");
|
||||
tokio::time::sleep(Duration::from_millis(1000 * attempt)).await;
|
||||
} else {
|
||||
tracing::debug!("Discovery started");
|
||||
return Event::Ok;
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(why) = result {
|
||||
Event::DBusError(why)
|
||||
} else {
|
||||
Event::Ok
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn stop_discovery(connection: zbus::Connection, adapter_path: OwnedObjectPath) -> Event {
|
||||
let result: zbus::Result<()> = Ok(());
|
||||
|
||||
let adapter = match bluez_zbus::get_adapter(&connection, adapter_path).await {
|
||||
Err(why) => return Event::DBusError(why),
|
||||
Ok(adapter) => adapter,
|
||||
};
|
||||
|
||||
for attempt in 1..5 {
|
||||
let result = async {
|
||||
tracing::debug!("Stopping discovery");
|
||||
|
||||
// We don't seem to be able to use join here as it seem to lead to some kind of race condition and not stop scanning occasionally
|
||||
adapter.set_pairable(false).await?;
|
||||
adapter.set_discoverable(false).await?;
|
||||
if adapter.discovering().await? {
|
||||
adapter.stop_discovery().await
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
.await;
|
||||
|
||||
if let Err(why) = result {
|
||||
tracing::warn!("Unable to stop bluetooth scanning: {why}");
|
||||
if why.to_string().contains("No discovery started") {
|
||||
return Event::DBusError(why);
|
||||
}
|
||||
|
||||
tracing::warn!("Unable to stop bluetooth scanning: {why}");
|
||||
tokio::time::sleep(Duration::from_millis(1000 * attempt)).await;
|
||||
} else {
|
||||
tracing::debug!("Discovery stopped");
|
||||
return Event::Ok;
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(why) = result {
|
||||
return Event::DBusError(why);
|
||||
}
|
||||
Event::Ok
|
||||
}
|
||||
|
||||
pub async fn change_adapter_status(
|
||||
connection: zbus::Connection,
|
||||
adapter_path: OwnedObjectPath,
|
||||
active: bool,
|
||||
) -> Event {
|
||||
let mut result: zbus::Result<()> = Ok(());
|
||||
for attempt in 1..5 {
|
||||
result = async {
|
||||
let adapter = bluez_zbus::get_adapter(&connection, adapter_path.clone()).await?;
|
||||
if active {
|
||||
adapter.set_powered(true).await?;
|
||||
adapter.set_discoverable(true).await
|
||||
} else {
|
||||
if let Err(why) = adapter.set_discoverable(false).await {
|
||||
tracing::warn!("Unable to change discoverability: {why}");
|
||||
}
|
||||
adapter.set_powered(false).await
|
||||
}
|
||||
}
|
||||
.await;
|
||||
if let Err(why) = &result {
|
||||
tracing::warn!("Unable to change the adapter state: {why}");
|
||||
tokio::time::sleep(Duration::from_millis(1000 * attempt)).await;
|
||||
} else {
|
||||
return Event::Ok;
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(why) = result {
|
||||
tracing::error!("Failed to change the adapter state!");
|
||||
return Event::DBusError(why);
|
||||
}
|
||||
|
||||
Event::Ok
|
||||
}
|
||||
|
||||
pub async fn get_adapters(connection: zbus::Connection) -> Event {
|
||||
let result: zbus::Result<HashMap<OwnedObjectPath, Adapter>> = async {
|
||||
futures::future::join_all(
|
||||
bluez_zbus::get_adapters(&connection)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|(path, proxy)| async move {
|
||||
Ok((path.to_owned(), Adapter::from_device(&proxy).await?))
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.into_iter()
|
||||
.collect::<zbus::Result<HashMap<_, _>>>()
|
||||
}
|
||||
.await;
|
||||
match result {
|
||||
Ok(adapters) => Event::SetAdapters(adapters),
|
||||
Err(why) => {
|
||||
tracing::error!("dbus connection failed. {why}");
|
||||
Event::DBusError(why)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_adapter_device_with_intermediary_state() {
|
||||
let mut adapter = Adapter {
|
||||
alias: "foo".to_owned(),
|
||||
address: "AA:BB:CC:DD:EE:FF".to_owned(),
|
||||
scanning: Active::Disabled,
|
||||
enabled: Active::Disabled,
|
||||
};
|
||||
adapter.update(vec![
|
||||
AdapterUpdate::Enabled(Active::Enabled),
|
||||
AdapterUpdate::Alias("xxx".to_owned()),
|
||||
]);
|
||||
assert_eq!(adapter.enabled, Active::Enabled);
|
||||
assert_eq!(&adapter.alias, "xxx");
|
||||
|
||||
adapter.enabled = Active::Disabling;
|
||||
adapter.update(vec![
|
||||
AdapterUpdate::Enabled(Active::Enabled),
|
||||
AdapterUpdate::Alias("xxx".to_owned()),
|
||||
]);
|
||||
assert_eq!(adapter.enabled, Active::Disabling);
|
||||
|
||||
adapter.scanning = Active::Enabling;
|
||||
adapter.update(vec![
|
||||
AdapterUpdate::Scanning(Active::Disabled),
|
||||
AdapterUpdate::Alias("xxx".to_owned()),
|
||||
]);
|
||||
assert_eq!(adapter.scanning, Active::Enabling);
|
||||
|
||||
adapter.update(vec![
|
||||
AdapterUpdate::Scanning(Active::Enabled),
|
||||
AdapterUpdate::Alias("xxx".to_owned()),
|
||||
]);
|
||||
assert_eq!(adapter.scanning, Active::Enabled);
|
||||
assert_eq!(&adapter.alias, "xxx");
|
||||
}
|
||||
}
|
||||
67
subscriptions/bluetooth/src/agent.rs
Normal file
67
subscriptions/bluetooth/src/agent.rs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
// Copyright 2024 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use crate::Event;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use zbus::zvariant::ObjectPath;
|
||||
|
||||
const AGENT_PATH: &str = "/org/bluez/agent/cosmic_settings";
|
||||
|
||||
pub async fn unregister(connection: zbus::Connection) -> zbus::Result<()> {
|
||||
let agent_path = ObjectPath::from_static_str_unchecked(AGENT_PATH);
|
||||
let bluez = bluez_zbus::agent_manager1::AgentManager1Proxy::new(&connection).await?;
|
||||
bluez.unregister_agent(&agent_path).await
|
||||
}
|
||||
|
||||
pub async fn watch(
|
||||
connection: zbus::Connection,
|
||||
mut tx: futures::channel::mpsc::Sender<Event>,
|
||||
) -> zbus::Result<()> {
|
||||
let span = tracing::span!(tracing::Level::INFO, "bluetooth::agent::watch");
|
||||
let _span = span.enter();
|
||||
|
||||
let (agent, mut receiver) = bluez_zbus::agent1::create();
|
||||
|
||||
let agent_path = ObjectPath::from_static_str_unchecked(AGENT_PATH);
|
||||
|
||||
tracing::debug!("connecting agent");
|
||||
|
||||
connection.object_server().at(&agent_path, agent).await?;
|
||||
|
||||
tracing::debug!("connecting to bluez agent manager");
|
||||
|
||||
let bluez = bluez_zbus::agent_manager1::AgentManager1Proxy::new(&connection).await?;
|
||||
|
||||
tracing::debug!("registering agent");
|
||||
|
||||
bluez
|
||||
.register_agent(
|
||||
&agent_path,
|
||||
<&'static str>::from(bluez_zbus::agent1::Capability::DisplayYesNo),
|
||||
)
|
||||
.await?;
|
||||
|
||||
if let Err(why) = bluez.request_default_agent(&agent_path).await {
|
||||
_ = bluez.unregister_agent(&agent_path).await;
|
||||
Err(why)?;
|
||||
}
|
||||
|
||||
tracing::debug!("registered");
|
||||
|
||||
while let Some(msg) = receiver.next().await {
|
||||
tracing::debug!(?msg, "agent message received");
|
||||
|
||||
if tx.send(Event::Agent(Arc::new(msg))).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_ = bluez.unregister_agent(&agent_path).await;
|
||||
|
||||
tracing::debug!("exiting");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
374
subscriptions/bluetooth/src/device.rs
Normal file
374
subscriptions/bluetooth/src/device.rs
Normal file
|
|
@ -0,0 +1,374 @@
|
|||
// Copyright 2024 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use crate::{Active, Event};
|
||||
use futures::join;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
hash::{Hash, Hasher},
|
||||
time::Duration,
|
||||
};
|
||||
use zbus::zvariant::OwnedObjectPath;
|
||||
|
||||
const DEFAILT_DEVICE_ICON: &str = "bluetooth-symbolic";
|
||||
|
||||
// Copied from https://github.com/bluez/bluez/blob/39467578207889fd015775cbe81a3db9dd26abea/src/dbus-common.c#L53
|
||||
fn device_type_to_icon(device_type: &str) -> &'static str {
|
||||
match device_type {
|
||||
"computer" => "laptop-symbolic",
|
||||
"phone" => "smartphone-symbolic",
|
||||
"network-wireless" => "network-wireless-symbolic",
|
||||
"audio-headset" => "audio-headset-symbolic",
|
||||
"audio-headphones" => "audio-headphones-symbolic",
|
||||
"camera-video" => "camera-video-symbolic",
|
||||
"audio-card" => "audio-card-symbolic",
|
||||
"input-gaming" => "input-gaming-symbolic",
|
||||
"input-keyboard" => "input-keyboard-symbolic",
|
||||
"input-tablet" => "input-tablet-symbolic",
|
||||
"input-mouse" => "input-mouse-symbolic",
|
||||
"printer" => "printer-network-symbolic",
|
||||
"camera-photo" => "camera-photo-symbolic",
|
||||
_ => DEFAILT_DEVICE_ICON,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct Device {
|
||||
alias: Option<String>,
|
||||
pub address: String,
|
||||
pub adapter: OwnedObjectPath,
|
||||
pub enabled: Active,
|
||||
pub paired: bool,
|
||||
pub icon: &'static str,
|
||||
pub battery: Option<String>,
|
||||
}
|
||||
|
||||
impl Device {
|
||||
pub async fn from_device(proxy: &bluez_zbus::BluetoothDevice<'_>) -> zbus::Result<Self> {
|
||||
let (address, adapter, alias) = join!(
|
||||
proxy.device.address(),
|
||||
proxy.device.adapter(),
|
||||
proxy.device.name()
|
||||
);
|
||||
let address = address?;
|
||||
if address.is_empty() {
|
||||
return Err(zbus::Error::Failure("Device has no MAC address".to_owned()));
|
||||
}
|
||||
let adapter = adapter?;
|
||||
if adapter.is_empty() {
|
||||
return Err(zbus::Error::Failure("Device has no adapter".to_owned()));
|
||||
}
|
||||
let alias = alias.ok();
|
||||
let device_type: String = proxy.icon().await;
|
||||
let paired = proxy.device.paired().await.unwrap_or(false);
|
||||
let enabled = if proxy.device.connected().await.unwrap_or(false) && paired {
|
||||
Active::Enabled
|
||||
} else {
|
||||
Active::Disabled
|
||||
};
|
||||
let battery = match &proxy.battery {
|
||||
Some(battery) => match battery.percentage().await {
|
||||
Ok(percentage) => Some(percentage.to_string()),
|
||||
Err(why) => {
|
||||
eprintln!("couldn't fetch battery percentage: {why}");
|
||||
None
|
||||
}
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
|
||||
let icon = device_type_to_icon(device_type.as_str());
|
||||
|
||||
Ok(Self {
|
||||
alias,
|
||||
address,
|
||||
adapter,
|
||||
enabled,
|
||||
paired,
|
||||
icon,
|
||||
battery,
|
||||
})
|
||||
}
|
||||
#[must_use]
|
||||
pub fn is_connected(&self) -> bool {
|
||||
self.enabled == Active::Enabled
|
||||
}
|
||||
/// Update the state of the device without overriding intermediary states.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the device used for update doesn't have the same MAC address
|
||||
pub fn update(&mut self, updates: Vec<DeviceUpdate>) {
|
||||
for udpate in updates {
|
||||
match udpate {
|
||||
DeviceUpdate::Alias(alias) => self.alias = alias,
|
||||
DeviceUpdate::Enabled(enabled) => {
|
||||
self.enabled = match (self.enabled, enabled) {
|
||||
(Active::Enabling, Active::Enabled) => Active::Enabled,
|
||||
(Active::Disabling, Active::Disabled) => Active::Disabled,
|
||||
(Active::Enabled | Active::Disabled, status) => status,
|
||||
(status, _) => status,
|
||||
}
|
||||
}
|
||||
DeviceUpdate::Paired(paired) => {
|
||||
self.enabled = Active::Disabling;
|
||||
self.paired = paired;
|
||||
}
|
||||
DeviceUpdate::Icon(icon) => self.icon = icon,
|
||||
DeviceUpdate::Battery(battery) => self.battery = battery,
|
||||
}
|
||||
}
|
||||
if self.enabled == Active::Disabled {
|
||||
self.battery = None;
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn has_alias(&self) -> bool {
|
||||
self.alias.is_some()
|
||||
}
|
||||
#[must_use]
|
||||
pub fn is_known_device_type(&self) -> bool {
|
||||
self.icon != DEFAILT_DEVICE_ICON
|
||||
}
|
||||
#[must_use]
|
||||
pub fn alias_or_addr(&self) -> &str {
|
||||
self.alias.as_ref().unwrap_or(&self.address)
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for Device {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.address.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Device {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.address == other.address
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Device {}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum DeviceUpdate {
|
||||
Alias(Option<String>),
|
||||
Enabled(Active),
|
||||
Paired(bool),
|
||||
Icon(&'static str),
|
||||
Battery(Option<String>),
|
||||
}
|
||||
|
||||
impl DeviceUpdate {
|
||||
pub fn from_update(update: HashMap<&'_ str, zbus::zvariant::Value<'_>>) -> Vec<Self> {
|
||||
update
|
||||
.into_iter()
|
||||
.filter_map(|(key, value)| {
|
||||
match (key, value) {
|
||||
("Alias", zbus::zvariant::Value::Str(value)) => {
|
||||
Some(DeviceUpdate::Alias(Some(value.into())))
|
||||
}
|
||||
("Connected", zbus::zvariant::Value::Bool(value)) => {
|
||||
Some(DeviceUpdate::Enabled(if value {
|
||||
Active::Enabled
|
||||
} else {
|
||||
Active::Disabled
|
||||
}))
|
||||
}
|
||||
("Paired", zbus::zvariant::Value::Bool(value)) => {
|
||||
Some(DeviceUpdate::Paired(value))
|
||||
}
|
||||
("Icon", zbus::zvariant::Value::Str(value)) => {
|
||||
Some(DeviceUpdate::Icon(device_type_to_icon(&value)))
|
||||
}
|
||||
("Percentage", zbus::zvariant::Value::U8(percentage)) => {
|
||||
Some(DeviceUpdate::Battery(Some(percentage.to_string())))
|
||||
}
|
||||
// Battery
|
||||
(message, value) => {
|
||||
tracing::debug!(message, ?value, "device update");
|
||||
None
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn disconnect_device(
|
||||
connection: zbus::Connection,
|
||||
device_path: OwnedObjectPath,
|
||||
) -> Event {
|
||||
let proxy = match bluez_zbus::get_device(&connection, device_path.clone()).await {
|
||||
Err(why) => {
|
||||
tracing::error!("Unable to get the device: {why}");
|
||||
return Event::DeviceFailed(device_path);
|
||||
}
|
||||
Ok(proxy) => proxy,
|
||||
};
|
||||
|
||||
for attempt in 1..5 {
|
||||
let result = async {
|
||||
if !proxy.device.connected().await? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
proxy.device.disconnect().await
|
||||
}
|
||||
.await;
|
||||
|
||||
if let Err(why) = result {
|
||||
tracing::warn!("Unable to disconnect to device: {why}");
|
||||
tokio::time::sleep(Duration::from_millis(1000 * attempt)).await;
|
||||
} else {
|
||||
return Event::Ok;
|
||||
}
|
||||
}
|
||||
|
||||
Event::DeviceFailed(device_path)
|
||||
}
|
||||
|
||||
pub async fn connect_device(connection: zbus::Connection, device_path: OwnedObjectPath) -> Event {
|
||||
let proxy = match bluez_zbus::get_device(&connection, device_path.clone()).await {
|
||||
Err(why) => {
|
||||
tracing::error!("Unable to get the device: {why}");
|
||||
return Event::DeviceFailed(device_path);
|
||||
}
|
||||
Ok(proxy) => proxy,
|
||||
};
|
||||
|
||||
for attempt in 1..5 {
|
||||
let result = async {
|
||||
if proxy.device.connected().await? {
|
||||
Ok(())
|
||||
} else {
|
||||
proxy.device.connect().await
|
||||
}
|
||||
}
|
||||
.await;
|
||||
|
||||
if let Err(why) = result {
|
||||
tracing::warn!("Unable to connect to device: {why}");
|
||||
tokio::time::sleep(Duration::from_millis(1000 * attempt)).await;
|
||||
} else {
|
||||
return Event::Ok;
|
||||
}
|
||||
}
|
||||
|
||||
Event::DeviceFailed(device_path)
|
||||
}
|
||||
|
||||
pub async fn forget_device(connection: zbus::Connection, device_path: OwnedObjectPath) -> Event {
|
||||
let mut result: zbus::Result<()> = Ok(());
|
||||
|
||||
let proxy = match bluez_zbus::get_device(&connection, device_path.clone()).await {
|
||||
Err(why) => {
|
||||
tracing::error!("Unable to get the device: {why}");
|
||||
return Event::DeviceFailed(device_path);
|
||||
}
|
||||
Ok(proxy) => proxy,
|
||||
};
|
||||
|
||||
let adapter_path = match proxy.device.adapter().await {
|
||||
Err(why) => {
|
||||
tracing::error!("Unable to get the adapter: {why}");
|
||||
return Event::DeviceFailed(device_path);
|
||||
}
|
||||
Ok(adapter_path) => adapter_path,
|
||||
};
|
||||
|
||||
let adapter = match bluez_zbus::get_adapter(&connection, adapter_path).await {
|
||||
Err(why) => {
|
||||
tracing::error!("Unable to get the adapter: {why}");
|
||||
return Event::DeviceFailed(device_path);
|
||||
}
|
||||
Ok(adapter) => adapter,
|
||||
};
|
||||
|
||||
for attempt in 1..5 {
|
||||
result = async {
|
||||
if proxy.device.connected().await? {
|
||||
proxy.device.disconnect().await?;
|
||||
}
|
||||
|
||||
adapter.remove_device(&proxy.path()).await
|
||||
}
|
||||
.await;
|
||||
|
||||
if let Err(why) = &result {
|
||||
tracing::warn!("Unable to connect to device: {why}");
|
||||
tokio::time::sleep(Duration::from_millis(1000 * attempt)).await;
|
||||
} else {
|
||||
return Event::Ok;
|
||||
}
|
||||
}
|
||||
|
||||
if result.is_err() {
|
||||
Event::DeviceFailed(device_path)
|
||||
} else {
|
||||
Event::Ok
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_devices(connection: zbus::Connection, adapter_path: OwnedObjectPath) -> Event {
|
||||
// TODO error handling
|
||||
let result: zbus::Result<HashMap<OwnedObjectPath, Device>> = async {
|
||||
futures::future::join_all(
|
||||
bluez_zbus::get_devices(&connection, Some(&adapter_path))
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(
|
||||
|(path, device)| async move { Ok((path, Device::from_device(&device).await?)) },
|
||||
),
|
||||
)
|
||||
.await
|
||||
.into_iter()
|
||||
.collect::<Result<HashMap<_, _>, _>>()
|
||||
}
|
||||
.await;
|
||||
match result {
|
||||
Ok(devices) => Event::SetDevices(devices),
|
||||
Err(why) => {
|
||||
tracing::error!("zbus connection failed. {why}");
|
||||
Event::DBusError(why)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_update_device_with_intermediary_state() {
|
||||
let mut device = Device {
|
||||
alias: None,
|
||||
adapter: OwnedObjectPath::try_from("/dev/bluez/hci0").unwrap(),
|
||||
address: "AA:BB:CC:DD:EE:FF".to_owned(),
|
||||
enabled: Active::Disabled,
|
||||
paired: false,
|
||||
icon: "bluetooth-symbolic",
|
||||
battery: None,
|
||||
};
|
||||
device.update(vec![
|
||||
DeviceUpdate::Enabled(Active::Enabled),
|
||||
DeviceUpdate::Alias(Some("Foo".to_owned())),
|
||||
]);
|
||||
assert_eq!(device.enabled, Active::Enabled);
|
||||
assert_eq!(device.alias, Some("Foo".to_owned()));
|
||||
|
||||
device.enabled = Active::Disabling;
|
||||
device.update(vec![
|
||||
DeviceUpdate::Enabled(Active::Enabled),
|
||||
DeviceUpdate::Alias(Some("Foo".to_owned())),
|
||||
]);
|
||||
assert_eq!(device.enabled, Active::Disabling);
|
||||
|
||||
device.enabled = Active::Enabling;
|
||||
device.update(vec![
|
||||
DeviceUpdate::Enabled(Active::Enabled),
|
||||
DeviceUpdate::Alias(Some("Foo".to_owned())),
|
||||
]);
|
||||
assert_eq!(device.enabled, Active::Enabled);
|
||||
}
|
||||
}
|
||||
41
subscriptions/bluetooth/src/lib.rs
Normal file
41
subscriptions/bluetooth/src/lib.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// Copyright 2024 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use zbus::zvariant::OwnedObjectPath;
|
||||
|
||||
mod adapter;
|
||||
pub mod agent;
|
||||
mod device;
|
||||
pub mod subscription;
|
||||
|
||||
pub use adapter::*;
|
||||
pub use device::*;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Event {
|
||||
AddedAdapter(OwnedObjectPath, Adapter),
|
||||
AddedDevice(OwnedObjectPath, Device),
|
||||
Agent(Arc<bluez_zbus::agent1::Message>),
|
||||
DBusError(zbus::Error),
|
||||
DBusServiceUnknown,
|
||||
DeviceFailed(OwnedObjectPath),
|
||||
Ok,
|
||||
NameHasNoOwner,
|
||||
RemovedAdapter(OwnedObjectPath),
|
||||
RemovedDevice(OwnedObjectPath),
|
||||
SetAdapters(HashMap<OwnedObjectPath, Adapter>),
|
||||
SetDevices(HashMap<OwnedObjectPath, Device>),
|
||||
UpdatedAdapter(OwnedObjectPath, Vec<AdapterUpdate>),
|
||||
UpdatedDevice(OwnedObjectPath, Vec<DeviceUpdate>),
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
|
||||
pub enum Active {
|
||||
#[default]
|
||||
Disabled,
|
||||
Disabling,
|
||||
Enabling,
|
||||
Enabled,
|
||||
}
|
||||
227
subscriptions/bluetooth/src/subscription.rs
Normal file
227
subscriptions/bluetooth/src/subscription.rs
Normal file
|
|
@ -0,0 +1,227 @@
|
|||
// Copyright 2024 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use crate::{AdapterUpdate, Device, DeviceUpdate, Event};
|
||||
use std::pin::Pin;
|
||||
|
||||
use bluez_zbus::BluetoothDevice;
|
||||
use futures::{channel::mpsc, stream::FusedStream};
|
||||
use iced_futures::futures::{SinkExt, StreamExt};
|
||||
use zbus::{fdo, zvariant::OwnedObjectPath};
|
||||
|
||||
enum DevicePropertyWatcherTask {
|
||||
Add(OwnedObjectPath),
|
||||
Removed(OwnedObjectPath),
|
||||
}
|
||||
|
||||
struct DevicePropertyWatcher {
|
||||
stream: futures::stream::SelectAll<SignalWatcher>,
|
||||
rx: mpsc::Receiver<DevicePropertyWatcherTask>,
|
||||
}
|
||||
|
||||
struct SignalWatcher {
|
||||
stream: zbus::fdo::PropertiesChangedStream,
|
||||
path: OwnedObjectPath,
|
||||
}
|
||||
|
||||
impl futures::Stream for SignalWatcher {
|
||||
type Item = zbus::fdo::PropertiesChanged;
|
||||
|
||||
fn poll_next(
|
||||
mut self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<Option<Self::Item>> {
|
||||
futures::Stream::poll_next(Pin::new(&mut self.stream), cx)
|
||||
}
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.stream.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl DevicePropertyWatcher {
|
||||
fn new() -> (Self, mpsc::Sender<DevicePropertyWatcherTask>) {
|
||||
let stream = futures::stream::select_all(vec![]);
|
||||
let (tx, rx) = mpsc::channel(10);
|
||||
|
||||
(Self { stream, rx }, tx)
|
||||
}
|
||||
async fn insert(
|
||||
&mut self,
|
||||
connection: &zbus::Connection,
|
||||
path: OwnedObjectPath,
|
||||
) -> zbus::Result<()> {
|
||||
if let Some(signal) = self.stream.iter_mut().find(|s| s.path.eq(&path)) {
|
||||
if signal.stream.is_terminated() {
|
||||
let property_proxy =
|
||||
zbus::fdo::PropertiesProxy::new(connection, "org.bluez", path.clone()).await?;
|
||||
signal.stream = property_proxy.receive_properties_changed().await?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
let property_proxy =
|
||||
zbus::fdo::PropertiesProxy::new(connection, "org.bluez", path.clone()).await?;
|
||||
let stream = property_proxy.receive_properties_changed().await?;
|
||||
self.stream.push(SignalWatcher { stream, path });
|
||||
Ok(())
|
||||
}
|
||||
fn remove(mut self, path: &OwnedObjectPath) -> Self {
|
||||
self.stream =
|
||||
futures::stream::select_all(self.stream.into_iter().filter(|p| !p.path.eq(path)));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Watching new/removed devices, connected state changed
|
||||
pub async fn watch(connection: zbus::Connection, mut tx: futures::channel::mpsc::Sender<Event>) {
|
||||
let span = tracing::span!(tracing::Level::INFO, "bluetooth::subscription::watch");
|
||||
let _span = span.enter();
|
||||
|
||||
loop {
|
||||
let result = async {
|
||||
let managed_object_proxy =
|
||||
zbus::fdo::ObjectManagerProxy::new(&connection, "org.bluez", "/")
|
||||
.await?;
|
||||
|
||||
let mut receive_interfaces_added = managed_object_proxy
|
||||
.receive_interfaces_added()
|
||||
.await?;
|
||||
let mut receive_interfaces_removed = managed_object_proxy
|
||||
.receive_interfaces_removed()
|
||||
.await?;
|
||||
|
||||
let (mut property_watcher, mut property_watcher_task) = DevicePropertyWatcher::new();
|
||||
|
||||
for (path, interfaces) in managed_object_proxy.get_managed_objects().await? {
|
||||
if interfaces.contains_key("org.bluez.Device1")
|
||||
|| interfaces.contains_key("org.bluez.Adapter1")
|
||||
|| interfaces.contains_key("org.bluez.Battery1")
|
||||
{
|
||||
property_watcher.insert(&connection, path).await?;
|
||||
}
|
||||
}
|
||||
|
||||
while !property_watcher.rx.is_terminated() {
|
||||
futures::select! {
|
||||
task = property_watcher.rx.next() => match task {
|
||||
Some(DevicePropertyWatcherTask::Add(path)) => {
|
||||
property_watcher.insert(&connection, path).await?;
|
||||
}
|
||||
Some(DevicePropertyWatcherTask::Removed(path)) => {
|
||||
property_watcher = property_watcher.remove(&path);
|
||||
}
|
||||
None => {
|
||||
tracing::error!("Bluetooth property watcher has shutdown unexpectedly");
|
||||
}
|
||||
},
|
||||
signal = property_watcher.stream.next() => match signal {
|
||||
Some(signal) => {
|
||||
let args = signal.args()?;
|
||||
let header = signal.message().header();
|
||||
match header.path() {
|
||||
Some(path) if path.contains("/dev_") =>
|
||||
tx
|
||||
.send(Event::UpdatedDevice(path.to_owned().into(), DeviceUpdate::from_update(args.changed_properties)))
|
||||
.await
|
||||
.map_err(|e| zbus::Error::Failure(e.to_string()))?,
|
||||
Some(path) => tx
|
||||
.send(Event::UpdatedAdapter(path.to_owned().into(), AdapterUpdate::from_update(args.changed_properties)))
|
||||
.await
|
||||
.map_err(|e| zbus::Error::Failure(e.to_string()))?,
|
||||
None => continue
|
||||
}
|
||||
}
|
||||
None => {
|
||||
tracing::error!("Bluetooth object watcher has shutdown unexpectedly");
|
||||
}
|
||||
},
|
||||
signal = receive_interfaces_added.next() => match signal {
|
||||
Some(signal) => {
|
||||
let args = signal.args()?;
|
||||
match BluetoothDevice::new(&connection, args.object_path.clone()).await {
|
||||
Ok(device) => {
|
||||
match Device::from_device(&device).await {
|
||||
Ok(device) => {
|
||||
property_watcher_task
|
||||
.send(DevicePropertyWatcherTask::Add(args.object_path.to_owned().into())).await.map_err(|e| zbus::Error::Failure(e.to_string()))?;
|
||||
|
||||
tx
|
||||
.send(Event::AddedDevice(args.object_path.to_owned().into(), device))
|
||||
.await
|
||||
.map_err(|e| zbus::Error::Failure(e.to_string()))?;
|
||||
|
||||
}
|
||||
Err(why) => {
|
||||
tracing::warn!("Cannot deserialise device: {why}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(zbus::Error::InterfaceNotFound) => continue,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
None => {
|
||||
tracing::error!("Bluetooth object watcher has shutdown unexpectedly");
|
||||
}
|
||||
},
|
||||
signal = receive_interfaces_removed.next() => match signal {
|
||||
Some(signal) => {
|
||||
let args = signal.args()?;
|
||||
if args.interfaces.iter().any(|i| i == "org.bluez.Device1") {
|
||||
property_watcher_task.send(DevicePropertyWatcherTask::Removed(
|
||||
args.object_path.to_owned().into(),
|
||||
)).await.map_err(|e| zbus::Error::Failure(e.to_string()))?;
|
||||
tx
|
||||
.send(Event::RemovedDevice(args.object_path.to_owned().into()))
|
||||
.await
|
||||
.map_err(|e| zbus::Error::Failure(e.to_string()))?;
|
||||
|
||||
} else if args.interfaces.iter().any(|i| i == "org.bluez.Battery1") {
|
||||
tx
|
||||
.send(Event::UpdatedDevice(args.object_path.to_owned().into(), vec![DeviceUpdate::Battery(None)]))
|
||||
.await
|
||||
.map_err(|e| zbus::Error::Failure(e.to_string()))?;
|
||||
} else if args.interfaces.iter().any(|i| i == "org.bluez.Adapter1") {
|
||||
tx
|
||||
.send(Event::RemovedAdapter(args.object_path.to_owned().into()))
|
||||
.await
|
||||
.map_err(|e| zbus::Error::Failure(e.to_string()))?;
|
||||
}
|
||||
},
|
||||
None => {
|
||||
tracing::error!("Bluetooth object watcher has shutdown unexpectedly");
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
tracing::warn!("bluetooth event loop gracefully terminated");
|
||||
Ok(())
|
||||
}.await;
|
||||
|
||||
if let Err(why) = result {
|
||||
_ = tx.send(Event::DBusError(why.clone())).await;
|
||||
|
||||
tracing::error!("failed to watch bluetooth event: {why:?}.");
|
||||
|
||||
// Exit if the dbus service is not found.
|
||||
if let zbus::Error::FDO(fdo_error) = why {
|
||||
match *fdo_error {
|
||||
fdo::Error::ServiceUnknown(_) => {
|
||||
tracing::error!(
|
||||
"The org.bluez dbus service is unknown. Is the bluez service installed and activatable?"
|
||||
);
|
||||
_ = tx.send(Event::DBusServiceUnknown).await;
|
||||
return;
|
||||
}
|
||||
|
||||
fdo::Error::NameHasNoOwner(_) => {
|
||||
tracing::error!("The org.bluez dbus service is not enabled or active");
|
||||
_ = tx.send(Event::NameHasNoOwner).await;
|
||||
return;
|
||||
}
|
||||
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue