Skip to content

Commit 69aee41

Browse files
committed
Add h0030_openurl and CommandInterface for modules
Implemented - h0000_supported_ids - h0001_info - h0030_openurl - Updated open crate - Removed dead code - Update flexi_logger
1 parent f680f3c commit 69aee41

File tree

10 files changed

+278
-95
lines changed

10 files changed

+278
-95
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ hid-io-protocol = { path = "hid-io-protocol", version = "0.1.0" }
104104
libc = { version = "0.2", optional = true }
105105
log = "0.4"
106106
nanoid = { version = "0.4", optional = true }
107-
open = "2.1"
107+
open = "3.0"
108108
rcgen = { version = "0.9", optional = true }
109109
regex = { version = "1.5", optional = true }
110110
rustls = { version = "0.20", optional = true, features = ["dangerous_configuration"] }

examples/tool.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,9 @@ async fn try_main() -> Result<(), ::capnp::Error> {
489489
.has_success()
490490
{
491491
println!("Manufacturing Test set: {}:{}", cmd, arg);
492+
} else {
493+
println!("NAK: Manufacturing Test set: {}:{} - FAILED", cmd, arg);
492494
}
493-
// TODO Implement errors
494495
}
495496
}
496497
Some(("sleep", _)) => {

hid-io-protocol/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ num_enum = { version = "0.5", default-features = false }
4444

4545

4646
[dev-dependencies]
47-
flexi_logger = "^0.16"
47+
flexi_logger = "0.22"

hid-io-protocol/src/commands/mod.rs

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ pub mod h0001 {
180180
MacOs = 0x04,
181181
Ios = 0x05,
182182
ChromeOs = 0x06,
183+
FreeBsd = 0x07,
184+
OpenBsd = 0x08,
185+
NetBsd = 0x09,
183186
}
184187

185188
#[derive(Clone, Debug)]
@@ -492,10 +495,21 @@ pub mod h0025 {
492495
}
493496

494497
/// Open URL
495-
/// TODO
496498
pub mod h0030 {
497-
pub struct Cmd {}
499+
use heapless::String;
500+
501+
#[derive(Clone, Debug)]
502+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
503+
pub struct Cmd<const S: usize> {
504+
pub url: String<S>,
505+
}
506+
507+
#[derive(Clone, Debug)]
508+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
498509
pub struct Ack {}
510+
511+
#[derive(Clone, Debug)]
512+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
499513
pub struct Nak {}
500514
}
501515

@@ -779,6 +793,7 @@ pub trait Commands<const H: usize, const HSUB1: usize, const HSUB4: usize, const
779793
HidIoCommandId::UnicodeText => self.h0017_unicodetext_handler(buf),
780794
HidIoCommandId::UnicodeState => self.h0018_unicodestate_handler(buf),
781795
HidIoCommandId::SleepMode => self.h001a_sleepmode_handler(buf),
796+
HidIoCommandId::OpenUrl => self.h0030_openurl_handler(buf),
782797
HidIoCommandId::TerminalCmd => self.h0031_terminalcmd_handler(buf),
783798
HidIoCommandId::TerminalOut => self.h0034_terminalout_handler(buf),
784799
HidIoCommandId::ManufacturingTest => self.h0050_manufacturing_handler(buf),
@@ -1594,6 +1609,73 @@ pub trait Commands<const H: usize, const HSUB1: usize, const HSUB4: usize, const
15941609
}
15951610
}
15961611

1612+
fn h0030_openurl(&mut self, data: h0030::Cmd<H>) -> Result<(), CommandError> {
1613+
// Create appropriately sized buffer
1614+
let mut buf = HidIoPacketBuffer {
1615+
// Test packet id
1616+
id: HidIoCommandId::OpenUrl,
1617+
// Detect max size
1618+
max_len: self.default_packet_chunk(),
1619+
// Use defaults for other fields
1620+
..Default::default()
1621+
};
1622+
1623+
// Build payload
1624+
if !buf.append_payload(data.url.as_bytes()) {
1625+
return Err(CommandError::DataVecTooSmall);
1626+
}
1627+
buf.done = true;
1628+
1629+
self.tx_packetbuffer_send(&mut buf)
1630+
}
1631+
fn h0030_openurl_cmd(&mut self, _data: h0030::Cmd<H>) -> Result<h0030::Ack, h0030::Nak> {
1632+
Err(h0030::Nak {})
1633+
}
1634+
fn h0030_openurl_nacmd(&mut self, _data: h0030::Cmd<H>) -> Result<(), CommandError> {
1635+
Err(CommandError::IdNotImplemented(
1636+
HidIoCommandId::OpenUrl,
1637+
HidIoPacketType::NaData,
1638+
))
1639+
}
1640+
fn h0030_openurl_ack(&mut self, _data: h0030::Ack) -> Result<(), CommandError> {
1641+
Err(CommandError::IdNotImplemented(
1642+
HidIoCommandId::OpenUrl,
1643+
HidIoPacketType::Ack,
1644+
))
1645+
}
1646+
fn h0030_openurl_nak(&mut self, _data: h0030::Nak) -> Result<(), CommandError> {
1647+
Err(CommandError::IdNotImplemented(
1648+
HidIoCommandId::OpenUrl,
1649+
HidIoPacketType::Nak,
1650+
))
1651+
}
1652+
fn h0030_openurl_handler(&mut self, buf: HidIoPacketBuffer<H>) -> Result<(), CommandError> {
1653+
// Handle packet type
1654+
match buf.ptype {
1655+
HidIoPacketType::Data => {
1656+
// Copy data into struct
1657+
let mut cmd = h0030::Cmd::<H> { url: String::new() };
1658+
cmd.url
1659+
.push_str(match core::str::from_utf8(&buf.data) {
1660+
Ok(url) => url,
1661+
Err(e) => {
1662+
return Err(CommandError::InvalidUtf8(Utf8Error::new(e)));
1663+
}
1664+
})
1665+
.unwrap();
1666+
1667+
match self.h0030_openurl_cmd(cmd) {
1668+
Ok(_ack) => self.empty_ack(buf.id),
1669+
Err(_nak) => self.empty_nak(buf.id),
1670+
}
1671+
}
1672+
HidIoPacketType::NaData => Err(CommandError::InvalidPacketBufferType(buf.ptype)),
1673+
HidIoPacketType::Ack => self.h0030_openurl_ack(h0030::Ack {}),
1674+
HidIoPacketType::Nak => self.h0030_openurl_nak(h0030::Nak {}),
1675+
_ => Ok(()),
1676+
}
1677+
}
1678+
15971679
fn h0031_terminalcmd(&mut self, data: h0031::Cmd<H>, na: bool) -> Result<(), CommandError> {
15981680
// Create appropriately sized buffer
15991681
let mut buf = HidIoPacketBuffer {

hid-io-protocol/src/commands/test.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ enum LogError {
4242

4343
/// Lite logging setup
4444
fn setup_logging_lite() -> Result<(), LogError> {
45-
match Logger::with_env_or_str("")
45+
match Logger::try_with_env_or_str("")
46+
.unwrap()
4647
.format(flexi_logger::colored_default_format)
4748
.format_for_files(flexi_logger::colored_detailed_format)
4849
.duplicate_to_stderr(flexi_logger::Duplicate::All)
@@ -359,6 +360,17 @@ impl<
359360
Ok(())
360361
}
361362

363+
fn h0030_openurl_cmd(&mut self, data: h0030::Cmd<H>) -> Result<h0030::Ack, h0030::Nak> {
364+
if data.url == "https://input.club" {
365+
Ok(h0030::Ack {})
366+
} else {
367+
Err(h0030::Nak {})
368+
}
369+
}
370+
fn h0030_openurl_ack(&mut self, _data: h0030::Ack) -> Result<(), CommandError> {
371+
Ok(())
372+
}
373+
362374
fn h0031_terminalcmd_cmd(&mut self, data: h0031::Cmd<H>) -> Result<h0031::Ack, h0031::Nak> {
363375
if data.command == "terminal command string\n\r" {
364376
Ok(h0031::Ack {})
@@ -838,6 +850,35 @@ fn h001a_sleepmode() {
838850
assert!(process.is_ok(), "process_rx2 => {:?}", process);
839851
}
840852

853+
#[test]
854+
fn h0030_openurl() {
855+
setup_logging_lite().ok();
856+
857+
// Build list of supported ids
858+
let ids = [HidIoCommandId::OpenUrl];
859+
860+
// Setup command interface
861+
let mut intf = CommandInterface::<8, 8, 64, 150, 149, 146, 165, 1>::new(&ids).unwrap();
862+
863+
// Normal data packet
864+
// Send command
865+
let cmd = h0030::Cmd {
866+
url: String::from("https://input.club"),
867+
};
868+
let send = intf.h0030_openurl(cmd.clone());
869+
assert!(send.is_ok(), "h0030_openurl {:?} => {:?}", cmd, send);
870+
871+
// Flush tx->rx
872+
// Process rx buffer
873+
let process = intf.process_rx();
874+
assert!(process.is_ok(), "process_rx1 {:?} => {:?}", cmd, process);
875+
876+
// Flush tx->rx
877+
// Process rx buffer
878+
let process = intf.process_rx();
879+
assert!(process.is_ok(), "process_rx2 {:?} => {:?}", cmd, process);
880+
}
881+
841882
#[test]
842883
fn h0031_terminalcmd() {
843884
setup_logging_lite().ok();

hid-io-protocol/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub enum HidIoPacketType {
8181
}
8282

8383
#[repr(u32)]
84-
#[derive(PartialEq, Eq, Clone, Copy, Debug, IntoPrimitive, TryFromPrimitive)]
84+
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, IntoPrimitive, TryFromPrimitive)]
8585
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
8686
/// Requests for to perform a specific action
8787
pub enum HidIoCommandId {

hid-io-protocol/src/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ enum LogError {
3636

3737
/// Lite logging setup
3838
fn setup_logging_lite() -> Result<(), LogError> {
39-
match Logger::with_env_or_str("")
39+
match Logger::try_with_env_or_str("")
40+
.unwrap()
4041
.format(flexi_logger::colored_default_format)
4142
.format_for_files(flexi_logger::colored_detailed_format)
4243
.duplicate_to_stderr(flexi_logger::Duplicate::All)

src/bin/hid-io-core.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ mod service {
151151
}
152152

153153
fn my_service_main(arguments: Vec<OsString>) {
154-
Logger::with_env()
154+
Logger::try_with_env()
155+
.unwrap()
155156
.log_to_file()
156157
.directory("log_files")
157158
.format(opt_format)

src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2017-2021 by Jacob Alexander
1+
/* Copyright (C) 2017-2022 by Jacob Alexander
22
*
33
* This file is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU General Public License as published by
@@ -76,6 +76,7 @@ pub mod keyboard_capnp {
7676

7777
// ----- Functions -----
7878

79+
use hid_io_protocol::HidIoCommandId;
7980
use lazy_static::lazy_static;
8081
use std::sync::atomic::AtomicBool;
8182
use std::sync::Arc;
@@ -85,6 +86,22 @@ lazy_static! {
8586
pub static ref RUNNING: Arc<AtomicBool> = Arc::new(AtomicBool::new(true));
8687
}
8788

89+
/// Supported Ids by hid-io-core
90+
/// This is used to determine all supported ids (always recursive).
91+
pub fn supported_ids() -> Vec<HidIoCommandId> {
92+
let mut ids = Vec::new();
93+
94+
ids.extend(api::supported_ids().iter().cloned());
95+
ids.extend(device::supported_ids(true).iter().cloned());
96+
ids.extend(module::supported_ids(true).iter().cloned());
97+
98+
// Sort, then deduplicate
99+
ids.sort_unstable();
100+
ids.dedup();
101+
102+
ids
103+
}
104+
88105
/// Main entry-point for the hid-io-core library
89106
pub async fn initialize(mailbox: mailbox::Mailbox) -> Result<(), std::io::Error> {
90107
// Setup signal handler

0 commit comments

Comments
 (0)