Skip to content

Commit

Permalink
fix: send the keyboard report only after the connection is established
Browse files Browse the repository at this point in the history
Signed-off-by: Haobo Gu <[email protected]>
  • Loading branch information
HaoboGu committed Dec 17, 2024
1 parent 55c1ce3 commit 536617a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
57 changes: 33 additions & 24 deletions rmk/src/ble/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
hid::HidWriterWrapper,
keyboard::{write_other_report_to_host, KeyboardReportMessage, REPORT_CHANNEL_SIZE},
usb::descriptor::CompositeReportType,
CONNECTION_STATE,
};

/// BLE communication task, send reports to host via BLE.
Expand All @@ -41,31 +42,39 @@ pub(crate) async fn ble_communication_task<
// Wait 1 seconds, ensure that gatt server has been started
Timer::after_secs(1).await;
loop {
match keyboard_report_receiver.receive().await {
KeyboardReportMessage::KeyboardReport(report) => {
debug!(
"Send keyboard report via BLE: {=[u8]:#X}, modifier: {:b}",
report.keycodes, report.modifier
);
match ble_keyboard_writer.write_serialize(&report).await {
Ok(()) => {}
Err(e) => error!("Send keyboard report error: {}", e),
};
}
KeyboardReportMessage::CompositeReport(report, report_type) => {
match report_type {
CompositeReportType::Media => {
write_other_report_to_host(report, report_type, ble_media_writer).await
}
CompositeReportType::Mouse => {
write_other_report_to_host(report, report_type, ble_mouse_writer).await
}
CompositeReportType::System => {
write_other_report_to_host(report, report_type, ble_system_control_writer)
let report = keyboard_report_receiver.receive().await;
// Only send the report after the connection is established.
if CONNECTION_STATE.load(core::sync::atomic::Ordering::Acquire) {
match report {
KeyboardReportMessage::KeyboardReport(report) => {
debug!(
"Send keyboard report via BLE: {=[u8]:#X}, modifier: {:b}",
report.keycodes, report.modifier
);
match ble_keyboard_writer.write_serialize(&report).await {
Ok(()) => {}
Err(e) => error!("Send keyboard report error: {}", e),
};
}
KeyboardReportMessage::CompositeReport(report, report_type) => {
match report_type {
CompositeReportType::Media => {
write_other_report_to_host(report, report_type, ble_media_writer).await
}
CompositeReportType::Mouse => {
write_other_report_to_host(report, report_type, ble_mouse_writer).await
}
CompositeReportType::System => {
write_other_report_to_host(
report,
report_type,
ble_system_control_writer,
)
.await
}
CompositeReportType::None => (),
};
}
CompositeReportType::None => (),
};
}
}
}
}
Expand Down
23 changes: 14 additions & 9 deletions rmk/src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::config::BehaviorConfig;
use crate::CONNECTION_STATE;
use crate::{
action::{Action, KeyAction},
hid::{ConnectionType, HidWriterWrapper},
Expand Down Expand Up @@ -82,15 +83,19 @@ pub(crate) async fn communication_task<'a, W: HidWriterWrapper, W2: HidWriterWra
// This delay is necessary otherwise this task will stuck at the first send when the USB is suspended
Timer::after_secs(2).await;
loop {
match receiver.receive().await {
KeyboardReportMessage::KeyboardReport(report) => {
match keybooard_hid_writer.write_serialize(&report).await {
Ok(()) => {}
Err(e) => error!("Send keyboard report error: {}", e),
};
}
KeyboardReportMessage::CompositeReport(report, report_type) => {
write_other_report_to_host(report, report_type, other_hid_writer).await;
let report = receiver.receive().await;
// Only send the report after the connection is established.
if CONNECTION_STATE.load(core::sync::atomic::Ordering::Acquire) {
match report {
KeyboardReportMessage::KeyboardReport(report) => {
match keybooard_hid_writer.write_serialize(&report).await {
Ok(()) => {}
Err(e) => error!("Send keyboard report error: {}", e),
};
}
KeyboardReportMessage::CompositeReport(report, report_type) => {
write_other_report_to_host(report, report_type, other_hid_writer).await;
}
}
}
}
Expand Down

0 comments on commit 536617a

Please sign in to comment.