#![allow(dead_code)]
use nord_usb::transport::{Direction, Step};
use nord_usb::wire::{cmd, ui, Location, Message, ObjectClass, Service};
pub const SUBSYSTEM: u32 = 10;
pub fn notify(msg: Message) -> Step {
Step {
direction: Direction::Out,
bytes: msg.encode(),
}
}
pub fn reply(msg: Message) -> Step {
Step {
direction: Direction::In,
bytes: msg.encode(),
}
}
pub fn request(command: u32, args: &[u8]) -> Step {
notify(Message::new(
Service::Program,
SUBSYSTEM,
command,
args.to_vec(),
))
}
pub fn response(command: u32, payload: &[u8]) -> Step {
response_with_status(command, 0, payload)
}
pub fn response_with_status(command: u32, status: u32, payload: &[u8]) -> Step {
reply(Message::new(
Service::Program,
SUBSYSTEM,
command + 1,
[&status.to_be_bytes()[..], payload].concat(),
))
}
pub fn refusal(command: u32, status: u32) -> Step {
response_with_status(command, status, &[])
}
pub fn changed() -> Step {
reply(Message::new(
Service::Program,
SUBSYSTEM,
cmd::CHANGED,
Vec::new(),
))
}
pub fn ui_request(command: u32) -> Step {
notify(Message::new(
Service::Ui,
ui::SUBSYSTEM,
command,
Vec::new(),
))
}
pub fn ui_response(command: u32, status: u32) -> Step {
reply(Message::new(
Service::Ui,
ui::SUBSYSTEM,
command + 1,
status.to_be_bytes().to_vec(),
))
}
pub fn session_open(class: ObjectClass) -> Vec<Step> {
vec![
ui_request(ui::HELLO),
ui_response(ui::HELLO, 0),
request(cmd::SESSION_OPEN, &class.to_raw().to_be_bytes()),
response(cmd::SESSION_OPEN, &class.to_raw().to_be_bytes()),
]
}
pub fn session_close() -> Vec<Step> {
vec![
request(cmd::SESSION_CLOSE, &[]),
response(cmd::SESSION_CLOSE, &[]),
ui_request(ui::GOODBYE),
ui_response(ui::GOODBYE, 0),
]
}
pub fn slot_args(at: Location) -> Vec<u8> {
let mut v = Vec::new();
at.write_to(&mut v);
v
}
pub fn words(values: &[u32]) -> Vec<u8> {
values.iter().flat_map(|w| w.to_be_bytes()).collect()
}