Ability to send HCI commands from Rust

* Autogenerate packet code in Rust from PDL (packet file copied from rootcanal)
* Implement parsing of packets that have a type header
* Expose Python APIs for sending HCI commands
* Expose Python APIs for instantiating a local controller
This commit is contained in:
Gabriel White-Vega
2023-09-14 14:28:22 -04:00
parent c12dee4e76
commit 7e331c2944
16 changed files with 7041 additions and 72 deletions

View File

@@ -0,0 +1,34 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Shared resources found under bumble's common.py
use pyo3::{PyObject, Python, ToPyObject};
/// Represents the sink for some transport mechanism
pub struct TransportSink(pub(crate) PyObject);
impl ToPyObject for TransportSink {
fn to_object(&self, _py: Python<'_>) -> PyObject {
self.0.clone()
}
}
/// Represents the source for some transport mechanism
pub struct TransportSource(pub(crate) PyObject);
impl ToPyObject for TransportSource {
fn to_object(&self, _py: Python<'_>) -> PyObject {
self.0.clone()
}
}

View File

@@ -0,0 +1,62 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Controller components
use crate::wrapper::{
common::{TransportSink, TransportSource},
hci::Address,
link::Link,
PyDictExt,
};
use pyo3::{
intern,
types::{PyDict, PyModule},
PyObject, PyResult, Python,
};
use pyo3_asyncio::tokio::into_future;
/// A controller that can send and receive HCI frames via some link
#[derive(Clone)]
pub struct Controller(pub(crate) PyObject);
impl Controller {
/// Creates a new [Controller] object. When optional arguments are not specified, the Python
/// module specifies the defaults. Must be called from a thread with a Python event loop, which
/// should be true on `tokio::main` and `async_std::main`.
///
/// For more info, see https://awestlake87.github.io/pyo3-asyncio/master/doc/pyo3_asyncio/#event-loop-references-and-contextvars.
pub async fn new(
name: &str,
host_source: Option<TransportSource>,
host_sink: Option<TransportSink>,
link: Option<Link>,
public_address: Option<Address>,
) -> PyResult<Self> {
Python::with_gil(|py| {
let kwargs = PyDict::new(py);
kwargs.set_item("name", name)?;
kwargs.set_opt_item("host_source", host_source)?;
kwargs.set_opt_item("host_sink", host_sink)?;
kwargs.set_opt_item("link", link)?;
kwargs.set_opt_item("public_address", public_address)?;
PyModule::import(py, intern!(py, "bumble.controller"))?
.getattr(intern!(py, "Controller"))?
.call_method("create", (), Some(kwargs))
.and_then(into_future)
})?
.await
.map(Self)
}
}

View File

@@ -19,7 +19,10 @@ use crate::{
wrapper::{
core::AdvertisingData,
gatt_client::{ProfileServiceProxy, ServiceProxy},
hci::{Address, HciErrorCode},
hci::{
packets::{Command, ErrorCode, Event},
Address, HciCommandWrapper, WithPacketType,
},
host::Host,
l2cap::LeConnectionOrientedChannel,
transport::{Sink, Source},
@@ -27,18 +30,73 @@ use crate::{
},
};
use pyo3::{
exceptions::PyException,
intern,
types::{PyDict, PyModule},
IntoPy, PyObject, PyResult, Python, ToPyObject,
IntoPy, PyErr, PyObject, PyResult, Python, ToPyObject,
};
use pyo3_asyncio::tokio::into_future;
use std::path;
/// Represents the various properties of some device
pub struct DeviceConfiguration(PyObject);
impl DeviceConfiguration {
/// Creates a new configuration, letting the internal Python object set all the defaults
pub fn new() -> PyResult<DeviceConfiguration> {
Python::with_gil(|py| {
PyModule::import(py, intern!(py, "bumble.device"))?
.getattr(intern!(py, "DeviceConfiguration"))?
.call0()
.map(|any| Self(any.into()))
})
}
/// Creates a new configuration from the specified file
pub fn load_from_file(&mut self, device_config: &path::Path) -> PyResult<()> {
Python::with_gil(|py| {
self.0
.call_method1(py, intern!(py, "load_from_file"), (device_config,))
})
.map(|_| ())
}
}
impl ToPyObject for DeviceConfiguration {
fn to_object(&self, _py: Python<'_>) -> PyObject {
self.0.clone()
}
}
/// A device that can send/receive HCI frames.
#[derive(Clone)]
pub struct Device(PyObject);
impl Device {
/// Creates a Device. When optional arguments are not specified, the Python object specifies the
/// defaults.
pub fn new(
name: Option<&str>,
address: Option<Address>,
config: Option<DeviceConfiguration>,
host: Option<Host>,
generic_access_service: Option<bool>,
) -> PyResult<Self> {
Python::with_gil(|py| {
let kwargs = PyDict::new(py);
kwargs.set_opt_item("name", name)?;
kwargs.set_opt_item("address", address)?;
kwargs.set_opt_item("config", config)?;
kwargs.set_opt_item("host", host)?;
kwargs.set_opt_item("generic_access_service", generic_access_service)?;
PyModule::import(py, intern!(py, "bumble.device"))?
.getattr(intern!(py, "Device"))?
.call((), Some(kwargs))
.map(|any| Self(any.into()))
})
}
/// Create a Device per the provided file configured to communicate with a controller through an HCI source/sink
pub fn from_config_file_with_hci(
device_config: &path::Path,
@@ -66,6 +124,29 @@ impl Device {
})
}
/// Sends an HCI command on this Device, returning the command's event result.
pub async fn send_command(&self, command: &Command, check_result: bool) -> PyResult<Event> {
Python::with_gil(|py| {
self.0
.call_method1(
py,
intern!(py, "send_command"),
(HciCommandWrapper(command.clone()), check_result),
)
.and_then(|coroutine| into_future(coroutine.as_ref(py)))
})?
.await
.and_then(|event| {
Python::with_gil(|py| {
let py_bytes = event.call_method0(py, intern!(py, "__bytes__"))?;
let bytes: &[u8] = py_bytes.extract(py)?;
let event = Event::parse_with_packet_type(bytes)
.map_err(|e| PyErr::new::<PyException, _>(e.to_string()))?;
Ok(event)
})
})
}
/// Turn the device on
pub async fn power_on(&self) -> PyResult<()> {
Python::with_gil(|py| {
@@ -244,7 +325,7 @@ impl Connection {
/// Disconnect from device with provided reason. When optional arguments are not specified, the
/// Python module specifies the defaults.
pub async fn disconnect(&mut self, reason: Option<HciErrorCode>) -> PyResult<()> {
pub async fn disconnect(&mut self, reason: Option<ErrorCode>) -> PyResult<()> {
Python::with_gil(|py| {
let kwargs = PyDict::new(py);
kwargs.set_opt_item("reason", reason)?;
@@ -259,7 +340,7 @@ impl Connection {
/// Register a callback to be called on disconnection.
pub fn on_disconnection(
&mut self,
callback: impl Fn(Python, HciErrorCode) -> PyResult<()> + Send + 'static,
callback: impl Fn(Python, ErrorCode) -> PyResult<()> + Send + 'static,
) -> PyResult<()> {
let boxed = ClosureCallback::new(move |py, args, _kwargs| {
callback(py, args.get_item(0)?.extract()?)

View File

@@ -14,84 +14,62 @@
//! HCI
pub use crate::internal::hci::packets;
use crate::wrapper::hci::packets::{
Acl, AddressType, Command, Error, ErrorCode, Event, Packet, Sco,
};
use itertools::Itertools as _;
use pyo3::{
exceptions::PyException, intern, types::PyModule, FromPyObject, PyAny, PyErr, PyObject,
PyResult, Python, ToPyObject,
exceptions::PyException,
intern, pyclass, pymethods,
types::{PyBytes, PyModule},
FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
};
/// HCI error code.
pub struct HciErrorCode(u8);
impl<'source> FromPyObject<'source> for HciErrorCode {
fn extract(ob: &'source PyAny) -> PyResult<Self> {
Ok(HciErrorCode(ob.extract()?))
}
}
impl ToPyObject for HciErrorCode {
fn to_object(&self, py: Python<'_>) -> PyObject {
self.0.to_object(py)
}
}
use std::fmt::{Display, Formatter};
/// Provides helpers for interacting with HCI
pub struct HciConstant;
impl HciConstant {
/// Human-readable error name
pub fn error_name(status: HciErrorCode) -> PyResult<String> {
pub fn error_name(status: ErrorCode) -> PyResult<String> {
Python::with_gil(|py| {
PyModule::import(py, intern!(py, "bumble.hci"))?
.getattr(intern!(py, "HCI_Constant"))?
.call_method1(intern!(py, "error_name"), (status.0,))?
.call_method1(intern!(py, "error_name"), (status.to_object(py),))?
.extract()
})
}
}
/// A Bluetooth address
#[derive(Clone)]
pub struct Address(pub(crate) PyObject);
impl Address {
/// Creates a new [Address] object
pub fn new(address: &str, address_type: &AddressType) -> PyResult<Self> {
Python::with_gil(|py| {
PyModule::import(py, intern!(py, "bumble.device"))?
.getattr(intern!(py, "Address"))?
.call1((address, address_type.to_object(py)))
.map(|any| Self(any.into()))
})
}
/// The type of address
pub fn address_type(&self) -> PyResult<AddressType> {
Python::with_gil(|py| {
let addr_type = self
.0
self.0
.getattr(py, intern!(py, "address_type"))?
.extract::<u32>(py)?;
let module = PyModule::import(py, intern!(py, "bumble.hci"))?;
let klass = module.getattr(intern!(py, "Address"))?;
if addr_type
== klass
.getattr(intern!(py, "PUBLIC_DEVICE_ADDRESS"))?
.extract::<u32>()?
{
Ok(AddressType::PublicDevice)
} else if addr_type
== klass
.getattr(intern!(py, "RANDOM_DEVICE_ADDRESS"))?
.extract::<u32>()?
{
Ok(AddressType::RandomDevice)
} else if addr_type
== klass
.getattr(intern!(py, "PUBLIC_IDENTITY_ADDRESS"))?
.extract::<u32>()?
{
Ok(AddressType::PublicIdentity)
} else if addr_type
== klass
.getattr(intern!(py, "RANDOM_IDENTITY_ADDRESS"))?
.extract::<u32>()?
{
Ok(AddressType::RandomIdentity)
} else {
Err(PyErr::new::<PyException, _>("Invalid address type"))
}
.extract::<u8>(py)?
.try_into()
.map_err(|addr_type| {
PyErr::new::<PyException, _>(format!(
"Failed to convert {addr_type} to AddressType"
))
})
})
}
@@ -134,12 +112,221 @@ impl Address {
}
}
/// BT address types
#[allow(missing_docs)]
#[derive(PartialEq, Eq, Debug)]
pub enum AddressType {
PublicDevice,
RandomDevice,
PublicIdentity,
RandomIdentity,
impl ToPyObject for Address {
fn to_object(&self, _py: Python<'_>) -> PyObject {
self.0.clone()
}
}
/// Implements minimum necessary interface to be treated as bumble's [HCI_Command].
/// While pyo3's macros do not support generics, this could probably be refactored to allow multiple
/// implementations of the HCI_Command methods in the future, if needed.
#[pyclass]
pub(crate) struct HciCommandWrapper(pub(crate) Command);
#[pymethods]
impl HciCommandWrapper {
fn __bytes__(&self, py: Python) -> PyResult<PyObject> {
let bytes = PyBytes::new(py, &self.0.clone().to_vec_with_packet_type());
Ok(bytes.into_py(py))
}
#[getter]
fn op_code(&self) -> u16 {
self.0.get_op_code().into()
}
}
/// HCI Packet type, prepended to the packet.
/// Rootcanal's PDL declaration excludes this from ser/deser and instead is implemented in code.
/// To maintain the ability to easily use future versions of their packet PDL, packet type is
/// implemented here.
#[derive(Debug)]
pub(crate) enum PacketType {
Command = 0x01,
Acl = 0x02,
Sco = 0x03,
Event = 0x04,
}
impl From<PacketType> for u8 {
fn from(packet_type: PacketType) -> Self {
match packet_type {
PacketType::Command => 0x01,
PacketType::Acl => 0x02,
PacketType::Sco => 0x03,
PacketType::Event => 0x04,
}
}
}
impl TryFrom<u8> for PacketType {
type Error = PacketTypeParseError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0x01 => Ok(PacketType::Command),
0x02 => Ok(PacketType::Acl),
0x03 => Ok(PacketType::Sco),
0x04 => Ok(PacketType::Event),
_ => Err(PacketTypeParseError::NonexistentPacketType(value)),
}
}
}
/// Allows for smoother interoperability between a [Packet] and a bytes representation of it that
/// includes its type as a header
pub(crate) trait WithPacketType<T: Packet> {
/// Converts the [Packet] into bytes, prefixed with its type
fn to_vec_with_packet_type(self) -> Vec<u8>;
/// Parses a [Packet] out of bytes that are prefixed with the packet's type
fn parse_with_packet_type(bytes: &[u8]) -> Result<T, PacketTypeParseError>;
}
/// Errors that may arise when parsing a packet that is prefixed with its type
pub(crate) enum PacketTypeParseError {
EmptySlice,
NoPacketBytes,
PacketTypeMismatch {
expected: PacketType,
actual: PacketType,
},
NonexistentPacketType(u8),
PacketParse(packets::Error),
}
impl Display for PacketTypeParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
PacketTypeParseError::EmptySlice => write!(f, "The slice being parsed was empty"),
PacketTypeParseError::NoPacketBytes => write!(
f,
"There were no bytes left after parsing the packet type header"
),
PacketTypeParseError::PacketTypeMismatch { expected, actual } => {
write!(f, "Expected type: {expected:?}, but got: {actual:?}")
}
PacketTypeParseError::NonexistentPacketType(packet_byte) => {
write!(f, "Packet type ({packet_byte:X}) does not exist")
}
PacketTypeParseError::PacketParse(e) => f.write_str(&e.to_string()),
}
}
}
impl From<packets::Error> for PacketTypeParseError {
fn from(value: Error) -> Self {
Self::PacketParse(value)
}
}
impl WithPacketType<Self> for Command {
fn to_vec_with_packet_type(self) -> Vec<u8> {
let mut bytes = Vec::<u8>::new();
bytes.push(PacketType::Command.into());
bytes.append(&mut self.to_vec());
bytes
}
fn parse_with_packet_type(bytes: &[u8]) -> Result<Self, PacketTypeParseError> {
let first_byte = bytes.first().ok_or(PacketTypeParseError::EmptySlice)?;
match PacketType::try_from(*first_byte)? {
PacketType::Command => {
let packet_bytes = bytes.get(1..).ok_or(PacketTypeParseError::NoPacketBytes)?;
Ok(Command::parse(packet_bytes)?)
}
packet_type => Err(PacketTypeParseError::PacketTypeMismatch {
expected: PacketType::Command,
actual: packet_type,
}),
}
}
}
impl WithPacketType<Self> for Acl {
fn to_vec_with_packet_type(self) -> Vec<u8> {
let mut bytes = Vec::<u8>::new();
bytes.push(PacketType::Acl.into());
bytes.append(&mut self.to_vec());
bytes
}
fn parse_with_packet_type(bytes: &[u8]) -> Result<Self, PacketTypeParseError> {
let first_byte = bytes.first().ok_or(PacketTypeParseError::EmptySlice)?;
match PacketType::try_from(*first_byte)? {
PacketType::Acl => {
let packet_bytes = bytes.get(1..).ok_or(PacketTypeParseError::NoPacketBytes)?;
Ok(Acl::parse(packet_bytes)?)
}
packet_type => Err(PacketTypeParseError::PacketTypeMismatch {
expected: PacketType::Acl,
actual: packet_type,
}),
}
}
}
impl WithPacketType<Self> for Sco {
fn to_vec_with_packet_type(self) -> Vec<u8> {
let mut bytes = Vec::<u8>::new();
bytes.push(PacketType::Sco.into());
bytes.append(&mut self.to_vec());
bytes
}
fn parse_with_packet_type(bytes: &[u8]) -> Result<Self, PacketTypeParseError> {
let first_byte = bytes.first().ok_or(PacketTypeParseError::EmptySlice)?;
match PacketType::try_from(*first_byte)? {
PacketType::Sco => {
let packet_bytes = bytes.get(1..).ok_or(PacketTypeParseError::NoPacketBytes)?;
Ok(Sco::parse(packet_bytes)?)
}
packet_type => Err(PacketTypeParseError::PacketTypeMismatch {
expected: PacketType::Sco,
actual: packet_type,
}),
}
}
}
impl WithPacketType<Self> for Event {
fn to_vec_with_packet_type(self) -> Vec<u8> {
let mut bytes = Vec::<u8>::new();
bytes.push(PacketType::Event.into());
bytes.append(&mut self.to_vec());
bytes
}
fn parse_with_packet_type(bytes: &[u8]) -> Result<Self, PacketTypeParseError> {
let first_byte = bytes.first().ok_or(PacketTypeParseError::EmptySlice)?;
match PacketType::try_from(*first_byte)? {
PacketType::Event => {
let packet_bytes = bytes.get(1..).ok_or(PacketTypeParseError::NoPacketBytes)?;
Ok(Event::parse(packet_bytes)?)
}
packet_type => Err(PacketTypeParseError::PacketTypeMismatch {
expected: PacketType::Event,
actual: packet_type,
}),
}
}
}
impl ToPyObject for AddressType {
fn to_object(&self, py: Python<'_>) -> PyObject {
u8::from(self).to_object(py)
}
}
impl<'source> FromPyObject<'source> for ErrorCode {
fn extract(ob: &'source PyAny) -> PyResult<Self> {
ob.extract()
}
}
impl ToPyObject for ErrorCode {
fn to_object(&self, py: Python<'_>) -> PyObject {
u8::from(self).to_object(py)
}
}

View File

@@ -15,7 +15,7 @@
//! Host-side types
use crate::wrapper::transport::{Sink, Source};
use pyo3::{intern, prelude::PyModule, types::PyDict, PyObject, PyResult, Python};
use pyo3::{intern, prelude::PyModule, types::PyDict, PyObject, PyResult, Python, ToPyObject};
/// Host HCI commands
pub struct Host {
@@ -61,6 +61,12 @@ impl Host {
}
}
impl ToPyObject for Host {
fn to_object(&self, _py: Python<'_>) -> PyObject {
self.obj.clone()
}
}
/// Driver factory to use when initializing a host
#[derive(Debug, Clone)]
pub enum DriverFactory {

38
rust/src/wrapper/link.rs Normal file
View File

@@ -0,0 +1,38 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Link components
use pyo3::{intern, types::PyModule, PyObject, PyResult, Python, ToPyObject};
/// Link bus for controllers to communicate with each other
#[derive(Clone)]
pub struct Link(pub(crate) PyObject);
impl Link {
/// Creates a [Link] object that transports messages locally
pub fn new_local_link() -> PyResult<Self> {
Python::with_gil(|py| {
PyModule::import(py, intern!(py, "bumble.link"))?
.getattr(intern!(py, "LocalLink"))?
.call0()
.map(|any| Self(any.into()))
})
}
}
impl ToPyObject for Link {
fn to_object(&self, _py: Python<'_>) -> PyObject {
self.0.clone()
}
}

View File

@@ -29,6 +29,8 @@ use pyo3::{
pub use pyo3_asyncio;
pub mod assigned_numbers;
pub mod common;
pub mod controller;
pub mod core;
pub mod device;
pub mod drivers;
@@ -36,6 +38,7 @@ pub mod gatt_client;
pub mod hci;
pub mod host;
pub mod l2cap;
pub mod link;
pub mod logging;
pub mod profile;
pub mod transport;

View File

@@ -14,6 +14,7 @@
//! HCI packet transport
use crate::wrapper::controller::Controller;
use pyo3::{intern, types::PyModule, PyObject, PyResult, Python};
/// A source/sink pair for HCI packet I/O.
@@ -67,6 +68,18 @@ impl Drop for Transport {
#[derive(Clone)]
pub struct Source(pub(crate) PyObject);
impl From<Controller> for Source {
fn from(value: Controller) -> Self {
Self(value.0)
}
}
/// The sink side of a [Transport].
#[derive(Clone)]
pub struct Sink(pub(crate) PyObject);
impl From<Controller> for Sink {
fn from(value: Controller) -> Self {
Self(value.0)
}
}