Loading tools/pdl/src/generator.rs +194 −141 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ use crate::ast; use anyhow::{anyhow, bail, Context, Result}; use quote::{format_ident, quote}; use std::collections::HashMap; use std::fmt::Write; use std::path::Path; use syn::parse_quote; Loading @@ -22,9 +23,12 @@ fn generate_preamble(path: &Path) -> Result<String> { .file_name() .and_then(|path| path.to_str()) .ok_or_else(|| anyhow!("could not find filename in {:?}", path))?; code.push_str(&format!("// @generated rust packets from {filename}\n\n")); let _ = write!(code, "// @generated rust packets from {filename}\n\n"); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { use bytes::{BufMut, Bytes, BytesMut}; use num_derive::{FromPrimitive, ToPrimitive}; use num_traits::{FromPrimitive, ToPrimitive}; Loading @@ -32,13 +36,21 @@ fn generate_preamble(path: &Path) -> Result<String> { use std::fmt; use std::sync::Arc; use thiserror::Error; }); } ); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { type Result<T> = std::result::Result<T, Error>; }); } ); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { #[derive(Debug, Error)] pub enum Error { #[error("Packet parsing failed")] Loading @@ -52,20 +64,29 @@ fn generate_preamble(path: &Path) -> Result<String> { #[error("when parsing field {obj}.{field}, {value} is not a valid {type_} value")] InvalidEnumValueError { obj: String, field: String, value: u64, type_: String }, } }); } ); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { #[derive(Debug, Error)] #[error("{0}")] pub struct TryFromError(&'static str); }); } ); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { pub trait Packet { fn to_bytes(self) -> Bytes; fn to_vec(self) -> Vec<u8>; } }); } ); Ok(code) } Loading Loading @@ -226,7 +247,10 @@ fn generate_packet_decl( let child_name = format_ident!("{id}Child"); if has_children { let child_data_idents = child_idents.iter().map(|ident| format_ident!("{ident}Data")); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { #[derive(Debug)] enum #data_child_ident { #(#child_idents(Arc<#child_data_idents>),)* Loading @@ -248,7 +272,8 @@ fn generate_packet_decl( #(#child_idents(#child_decl_packet_name),)* None, } }); } ); } let data_name = format_ident!("{id}Data"); Loading @@ -261,13 +286,17 @@ fn generate_packet_decl( .iter() .map(|field| generate_field(field, parse_quote!())) .collect::<Result<Vec<_>>>()?; code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { #[derive(Debug)] struct #data_name { #(#plain_fields,)* #child_field } }); } ); let parent = parent_id.as_ref().map(|parent_id| match packets.get(parent_id.as_str()) { Some(ast::Decl::Packet { id, .. }) => { Loading @@ -281,25 +310,33 @@ fn generate_packet_decl( }); let packet_name = format_ident!("{id}Packet"); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { #[derive(Debug, Clone)] pub struct #packet_name { #parent #ident: Arc<#data_name>, } }); } ); let builder_name = format_ident!("{id}Builder"); let pub_fields = fields .iter() .map(|field| generate_field(field, parse_quote!(pub))) .collect::<Result<Vec<_>>>()?; code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { #[derive(Debug)] pub struct #builder_name { #(#pub_fields,)* } }); } ); // TODO(mgeisler): use the `Buf` trait instead of tracking // the offset manually. Loading Loading @@ -336,7 +373,10 @@ fn generate_packet_decl( }) }); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { impl #data_name { fn conforms(bytes: &[u8]) -> bool { // TODO(mgeisler): return Boolean expression directly. Loading Loading @@ -366,9 +406,13 @@ fn generate_packet_decl( ret } } }); } ); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { impl Packet for #packet_name { fn to_bytes(self) -> Bytes { let mut buffer = BytesMut::new(); Loading @@ -390,7 +434,8 @@ fn generate_packet_decl( packet.to_vec() } } }); } ); let specialize = has_children.then(|| { quote! { Loading @@ -408,7 +453,10 @@ fn generate_packet_decl( .iter() .map(|field| generate_field_getter(&ident, field)) .collect::<Result<Vec<_>>>()?; code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { impl #packet_name { pub fn parse(bytes: &[u8]) -> Result<Self> { Ok(Self::new(Arc::new(#data_name::parse(bytes)?)).unwrap()) Loading @@ -423,14 +471,18 @@ fn generate_packet_decl( #(#field_getters)* } }); } ); let child = has_children.then(|| { quote! { child: #data_child_ident::None, } }); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { impl #builder_name { pub fn build(self) -> #packet_name { let #ident = Arc::new(#data_name { Loading @@ -440,7 +492,8 @@ fn generate_packet_decl( #packet_name::new(#ident).unwrap() } } }); } ); Ok(code) } Loading tools/rootcanal/lmp/src/procedure/secure_simple_pairing.rs +3 −3 Original line number Diff line number Diff line Loading @@ -375,12 +375,12 @@ pub async fn initiate(ctx: &impl Context) -> Result<(), ()> { AuthenticationMethod::NumericComparaison => { send_commitment(ctx, true).await; let _user_confirmation = user_confirmation_request(ctx).await?; user_confirmation_request(ctx).await?; Ok(()) } AuthenticationMethod::PasskeyEntry => { if initiator.io_capability == hci::IoCapability::KeyboardOnly { let _user_passkey = user_passkey_request(ctx).await?; user_passkey_request(ctx).await?; } else { ctx.send_hci_event( hci::UserPasskeyNotificationBuilder { Loading @@ -397,7 +397,7 @@ pub async fn initiate(ctx: &impl Context) -> Result<(), ()> { } AuthenticationMethod::OutOfBand => { if initiator.oob_data_present != hci::OobDataPresent::NotPresent { let _remote_oob_data = remote_oob_data_request(ctx).await?; remote_oob_data_request(ctx).await?; } send_commitment(ctx, false).await; Loading Loading
tools/pdl/src/generator.rs +194 −141 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ use crate::ast; use anyhow::{anyhow, bail, Context, Result}; use quote::{format_ident, quote}; use std::collections::HashMap; use std::fmt::Write; use std::path::Path; use syn::parse_quote; Loading @@ -22,9 +23,12 @@ fn generate_preamble(path: &Path) -> Result<String> { .file_name() .and_then(|path| path.to_str()) .ok_or_else(|| anyhow!("could not find filename in {:?}", path))?; code.push_str(&format!("// @generated rust packets from {filename}\n\n")); let _ = write!(code, "// @generated rust packets from {filename}\n\n"); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { use bytes::{BufMut, Bytes, BytesMut}; use num_derive::{FromPrimitive, ToPrimitive}; use num_traits::{FromPrimitive, ToPrimitive}; Loading @@ -32,13 +36,21 @@ fn generate_preamble(path: &Path) -> Result<String> { use std::fmt; use std::sync::Arc; use thiserror::Error; }); } ); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { type Result<T> = std::result::Result<T, Error>; }); } ); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { #[derive(Debug, Error)] pub enum Error { #[error("Packet parsing failed")] Loading @@ -52,20 +64,29 @@ fn generate_preamble(path: &Path) -> Result<String> { #[error("when parsing field {obj}.{field}, {value} is not a valid {type_} value")] InvalidEnumValueError { obj: String, field: String, value: u64, type_: String }, } }); } ); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { #[derive(Debug, Error)] #[error("{0}")] pub struct TryFromError(&'static str); }); } ); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { pub trait Packet { fn to_bytes(self) -> Bytes; fn to_vec(self) -> Vec<u8>; } }); } ); Ok(code) } Loading Loading @@ -226,7 +247,10 @@ fn generate_packet_decl( let child_name = format_ident!("{id}Child"); if has_children { let child_data_idents = child_idents.iter().map(|ident| format_ident!("{ident}Data")); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { #[derive(Debug)] enum #data_child_ident { #(#child_idents(Arc<#child_data_idents>),)* Loading @@ -248,7 +272,8 @@ fn generate_packet_decl( #(#child_idents(#child_decl_packet_name),)* None, } }); } ); } let data_name = format_ident!("{id}Data"); Loading @@ -261,13 +286,17 @@ fn generate_packet_decl( .iter() .map(|field| generate_field(field, parse_quote!())) .collect::<Result<Vec<_>>>()?; code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { #[derive(Debug)] struct #data_name { #(#plain_fields,)* #child_field } }); } ); let parent = parent_id.as_ref().map(|parent_id| match packets.get(parent_id.as_str()) { Some(ast::Decl::Packet { id, .. }) => { Loading @@ -281,25 +310,33 @@ fn generate_packet_decl( }); let packet_name = format_ident!("{id}Packet"); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { #[derive(Debug, Clone)] pub struct #packet_name { #parent #ident: Arc<#data_name>, } }); } ); let builder_name = format_ident!("{id}Builder"); let pub_fields = fields .iter() .map(|field| generate_field(field, parse_quote!(pub))) .collect::<Result<Vec<_>>>()?; code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { #[derive(Debug)] pub struct #builder_name { #(#pub_fields,)* } }); } ); // TODO(mgeisler): use the `Buf` trait instead of tracking // the offset manually. Loading Loading @@ -336,7 +373,10 @@ fn generate_packet_decl( }) }); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { impl #data_name { fn conforms(bytes: &[u8]) -> bool { // TODO(mgeisler): return Boolean expression directly. Loading Loading @@ -366,9 +406,13 @@ fn generate_packet_decl( ret } } }); } ); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { impl Packet for #packet_name { fn to_bytes(self) -> Bytes { let mut buffer = BytesMut::new(); Loading @@ -390,7 +434,8 @@ fn generate_packet_decl( packet.to_vec() } } }); } ); let specialize = has_children.then(|| { quote! { Loading @@ -408,7 +453,10 @@ fn generate_packet_decl( .iter() .map(|field| generate_field_getter(&ident, field)) .collect::<Result<Vec<_>>>()?; code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { impl #packet_name { pub fn parse(bytes: &[u8]) -> Result<Self> { Ok(Self::new(Arc::new(#data_name::parse(bytes)?)).unwrap()) Loading @@ -423,14 +471,18 @@ fn generate_packet_decl( #(#field_getters)* } }); } ); let child = has_children.then(|| { quote! { child: #data_child_ident::None, } }); code.push_str("e_block! { let _ = write!( code, "{}", "e_block! { impl #builder_name { pub fn build(self) -> #packet_name { let #ident = Arc::new(#data_name { Loading @@ -440,7 +492,8 @@ fn generate_packet_decl( #packet_name::new(#ident).unwrap() } } }); } ); Ok(code) } Loading
tools/rootcanal/lmp/src/procedure/secure_simple_pairing.rs +3 −3 Original line number Diff line number Diff line Loading @@ -375,12 +375,12 @@ pub async fn initiate(ctx: &impl Context) -> Result<(), ()> { AuthenticationMethod::NumericComparaison => { send_commitment(ctx, true).await; let _user_confirmation = user_confirmation_request(ctx).await?; user_confirmation_request(ctx).await?; Ok(()) } AuthenticationMethod::PasskeyEntry => { if initiator.io_capability == hci::IoCapability::KeyboardOnly { let _user_passkey = user_passkey_request(ctx).await?; user_passkey_request(ctx).await?; } else { ctx.send_hci_event( hci::UserPasskeyNotificationBuilder { Loading @@ -397,7 +397,7 @@ pub async fn initiate(ctx: &impl Context) -> Result<(), ()> { } AuthenticationMethod::OutOfBand => { if initiator.oob_data_present != hci::OobDataPresent::NotPresent { let _remote_oob_data = remote_oob_data_request(ctx).await?; remote_oob_data_request(ctx).await?; } send_commitment(ctx, false).await; Loading