Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for In/Out Report SerDes #13

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ authors = ["twitchyliquid64"]
edition = "2018"
repository = "https://github.com/twitchyliquid64/usbd-hid"


[dependencies]
usbd-hid-macros = { path = "macros", version = "0.4.5" }
usb-device = "0.2.1"
serde = { version = "1.0", default-features = false }
ssmarshal = { version = "1.0", default-features = false }

[features]
default = ["serde/derive"]
2 changes: 1 addition & 1 deletion descriptors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Into<u8> for LocalItemKind {
/// 'Report Descriptor' of the spec, version 1.11.
#[repr(u8)]
#[allow(unused)]
#[derive(Copy, Debug, Clone, Eq, PartialEq)]
#[derive(Copy, Debug, Clone, Eq, PartialEq, Hash)]
pub enum MainItemKind {
Input = 0b1000,
Output = 0b1001,
Expand Down
96 changes: 96 additions & 0 deletions macros/src/gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use proc_macro::TokenStream as TokenStream1;
use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;

use proc_macro2::{Ident, Span};
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::{Attribute, AttrStyle, Fields, ItemEnum, ItemStruct, parse_str, Type, TypeTuple, Variant, Result, parse};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;

pub(crate) fn generate_type(
template: &ItemStruct,
ident: Ident,
mut fields: HashMap<Option<u8>, Fields>
) -> TokenStream {
let template = template.clone();
if let Some(fields) = fields.remove(&None) {
ItemStruct{
ident,
fields,
..template
}.to_token_stream()
} else {
let mut variants_iter = Vec::<Variant>::new();
for i in 0u8..=255u8 {
variants_iter.push(Variant {
attrs: Vec::new(),
ident: parse_str(format!("R{}", i).as_str()).unwrap(),
fields: fields.remove(&Some(i)).unwrap_or(Fields::Unit),
discriminant: None
});

if fields.is_empty() {
break
}
}

ItemEnum {
attrs: template.attrs,
vis: template.vis,
generics: template.generics,
brace_token: Default::default(),
ident,
enum_token: syn::token::Enum{ span: Span::call_site() },
variants: Punctuated::from_iter(variants_iter)
}.to_token_stream()
}
}

pub(crate) fn extract_and_format_derive(empty_struct: &mut ItemStruct) -> Result<Attribute> {
let pos = empty_struct.attrs.iter().position(|a| a.path.is_ident("derive") && a.style == AttrStyle::Outer);

let drives = {
let mut d: HashSet<Type> = if let Some(i) = pos {
let existing_drive = empty_struct.attrs.remove(i);
match parse::<syn::Type>(TokenStream1::from(existing_drive.tokens))? {
Type::Paren(p) => {
let mut h = HashSet::new();
h.insert(*p.elem);
h
}
Type::Tuple(t) => {
t.elems.into_iter().collect()
}
t => {
return Err(
parse::Error::new(t.span(), "`#[gen_hid_descriptor]` unrecognized derive")
)
}
}
} else {
HashSet::new()
};

d.insert(parse_str("Deserialize").unwrap());
d.insert(parse_str("Serialize").unwrap());

d
};

let mut attrs_ts = TokenStream::new();

TypeTuple {
paren_token: Default::default(),
elems: Punctuated::from_iter(drives)
}.to_tokens(&mut attrs_ts);

Ok(Attribute {
pound_token: syn::token::Pound { spans: [Span::call_site()] },
style: AttrStyle::Outer,
bracket_token: Default::default(),
path: parse_str("derive").unwrap(),
tokens: attrs_ts
})
}
2 changes: 2 additions & 0 deletions macros/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct MainItem {
pub logical_maximum: isize,
pub report_count: u16,
pub report_size: u16,
pub report_id: Option<u8>,
pub padding_bits: Option<u16>,
}

Expand Down Expand Up @@ -132,6 +133,7 @@ fn unary_item(id: Ident, kind: MainItemKind, bit_width: usize) -> ReportUnaryFie
logical_maximum: 0,
report_count: 1,
report_size: bit_width as u16,
report_id: None,
padding_bits: None,
},
}
Expand Down
Loading