Skip to content

Commit 84778c7

Browse files
committed
Semaphores: Revive the old UsbReportUpdateSemaphore, and hook it to retries and logs.
1 parent bb53267 commit 84778c7

12 files changed

Lines changed: 188 additions & 91 deletions

device/src/messenger.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ static void processSyncablePropertyDongle(device_id_t src, const uint8_t* data,
241241

242242
#if DEVICE_IS_UHK_DONGLE
243243
uint8_t retryCounter = 0;
244-
while (ShouldResendReport(ret == 0, &retryCounter)) {
244+
while (ShouldResendReport(ret, &retryCounter)) {
245245
uint16_t delay = GetResendThrottleDelay(retryCounter);
246246
k_sleep(K_MSEC(delay));
247247
k_sem_take(&dongleUsbSem, K_MSEC(128));

right/src/debug.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define DEBUG_MODE false
1717
#define DEBUG_STRESS_UART false
1818
#define DEBUG_STRESS_GATT false
19+
#define DEBUG_STRESS_REPORTS false
1920
#define DEBUG_TEST_RTT false
2021
#define DEBUG_LOG_UART true
2122
#define DEBUG_LOG_MESSAGES false
@@ -150,7 +151,7 @@
150151
#define WATCH_SEMAPHORE_TAKE(SEM, FILENAME, N) if(CurrentWatch == N) { WatchSemaforeTake(SEM, FILENAME, N); } else { k_sem_take(SEM, K_FOREVER); }
151152
#define SEM_TAKE(SEM) WATCH_SEMAPHORE_TAKE(SEM, __FILE__, 0)
152153
#else
153-
#define SEM_TAKE(SEM) if (k_sem_take(SEM, K_MSEC(256)) != 0) { uint8_t tgt = Cfg.DevMode ? LogTarget_Uart | LogTarget_ErrorBuffer : LogTarget_Uart; LogTo(DEVICE_ID, tgt, "Failed to take semaphore " #SEM " in file %s. This shouldn't have happened. Please report it!\n", __FILE__); Trace_Print(tgt, "Failed semaphore"); }
154+
#define SEM_TAKE(SEM) if (k_sem_take(SEM, K_MSEC(256)) != 0) { uint8_t tgt = Cfg.DevMode ? LogTarget_Uart | LogTarget_ErrorBuffer : LogTarget_Uart; LogTo(DEVICE_ID, tgt, "Failed to take semaphore " #SEM " in file %s.!\n", __FILE__); Trace_Print(tgt, "Failed semaphore"); }
154155
#define WATCH_SEMAPHORE_TAKE(SEM, LABEL, N) k_sem_take(SEM, K_FOREVER);
155156
#endif
156157

right/src/event_scheduler.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "slave_drivers/uhk_module_driver.h"
1111
#include "peripherals/merge_sensor.h"
1212
#include "power_mode.h"
13+
#include "usb_report_updater.h"
1314
#include "oneshot.h"
1415
#include "trace.h"
1516

@@ -251,6 +252,11 @@ static void processEvt(event_scheduler_event_t evt)
251252
case EventSchedulerEvent_SendUsbReports:
252253
EventVector_Set(EventVector_SendUsbReports);
253254
break;
255+
case EventSchedulerEvent_UpdateUsbSemaphore:
256+
// Drives the semaphore timeout so a stuck (unconfirmed) report gets resent
257+
// even if nothing else wakes the event loop.
258+
UsbReadyForTransfers();
259+
break;
254260
case EventSchedulerEvent_CheckLeftBleVsUart:
255261
#if DEVICE_IS_UHK80_LEFT
256262
BtManager_CheckLeftBleVsUart();

right/src/event_scheduler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
EventSchedulerEvent_OneShotTimeout,
5151
EventSchedulerEvent_KickHid,
5252
EventSchedulerEvent_SendUsbReports,
53+
EventSchedulerEvent_UpdateUsbSemaphore,
5354
EventSchedulerEvent_CheckLeftBleVsUart,
5455
EventSchedulerEvent_ConnectionsUpdateState,
5556
EventSchedulerEvent_Count

right/src/hid/command_app.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,19 @@ void command_app::get_report(hid::report::selector select, const std::span<uint8
6565

6666
void command_app::in_report_sent(const std::span<const uint8_t> &data)
6767
{
68+
#if DEVICE_IS_UHK80_RIGHT
69+
// On BLE all apps share one merged HOGP interface, so in_report_sent is broadcast
70+
// to every app; act only on our own (command) report ID. The USB handle is a
71+
// standalone interface (whose report may carry no report-ID byte), so the filter
72+
// must not be applied there.
73+
if ((this == &ble_handle()) && (data.front() != report_ids::IN_COMMAND)) {
74+
return;
75+
}
76+
#else
6877
if (data.front() != report_ids::IN_COMMAND) {
6978
return;
7079
}
80+
#endif
7181
auto buf_idx = in_buffer_.indexof(data.data());
7282
in_buffer_.compare_swap(buf_idx);
7383
}

right/src/hid/controls_app.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ int controls_app::send_report(const hid_controls_report_t &report)
3333

3434
void controls_app::in_report_sent(const std::span<const uint8_t> &data)
3535
{
36+
#if DEVICE_IS_UHK80_RIGHT
37+
// On BLE all apps share one merged HOGP interface, so in_report_sent is broadcast
38+
// to every app for every sent report. Act only on our own (controls) report ID.
39+
// The USB handle is a standalone interface, so the filter must not be applied there.
40+
if ((this == &ble_handle()) && (data.front() != report_ids::IN_CONTROLS)) {
41+
return;
42+
}
43+
#endif
3644
Hid_ControlsReportSentCallback((this == &usb_handle()) ? HID_TRANSPORT_USB : HID_TRANSPORT_BLE);
3745
}
3846

right/src/hid/keyboard_app.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ void keyboard_app::set_report(hid::report::type type, const std::span<const uint
180180
void keyboard_app::in_report_sent(const std::span<const uint8_t> &data)
181181
{
182182
#if DEVICE_IS_UHK80_RIGHT
183-
if ((prot_ == hid::protocol::REPORT) && (data.front() != KEYS_NKRO_REPORT_ID) &&
184-
(data.front() != KEYS_6KRO_REPORT_ID)) {
183+
if ((this == &ble_handle()) && (prot_ == hid::protocol::REPORT) &&
184+
(data.front() != KEYS_NKRO_REPORT_ID) && (data.front() != KEYS_6KRO_REPORT_ID)) {
185185
return;
186186
}
187187
#endif

right/src/hid/mouse_app.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ int mouse_app::send_report(const hid_mouse_report_t &report)
3434

3535
void mouse_app::in_report_sent(const std::span<const uint8_t> &data)
3636
{
37+
#if DEVICE_IS_UHK80_RIGHT
38+
// On BLE all apps share one merged HOGP interface, so in_report_sent is broadcast
39+
// to every app for every sent report. Act only on our own (mouse) report ID. The
40+
// USB handle is a standalone interface that only ever receives mouse completions,
41+
// so the filter must not be applied there.
42+
if ((this == &ble_handle()) && (data.front() != report_ids::IN_MOUSE)) {
43+
return;
44+
}
45+
#endif
3746
Hid_MouseReportSentCallback((this == &usb_handle()) ? HID_TRANSPORT_USB : HID_TRANSPORT_BLE);
3847
}
3948

right/src/hid/transport.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern "C" {
1919
#include "led_display.h"
2020
#include "jitter_test.h"
2121
#include "usb_state.h"
22+
#include "utils.h"
2223
}
2324
#include "command_app.hpp"
2425
#include "controls_app.hpp"
@@ -173,6 +174,9 @@ extern "C" errno_t Hid_SendKeyboardReport(const hid_keyboard_report_t *report)
173174
noteReportDispatched(sink);
174175
Trace_Printf("z11,%d", sink);
175176
errno_t err;
177+
if (DEBUG_STRESS_REPORTS && Utils_Random() % 16 == 0) {
178+
return 1;
179+
}
176180
switch (sink) {
177181
case ReportSink_Usb:
178182
err = keyboard_app::usb_handle().send_report(*report);
@@ -187,6 +191,8 @@ extern "C" errno_t Hid_SendKeyboardReport(const hid_keyboard_report_t *report)
187191
err = Messenger_Send2(DeviceId_Uhk_Dongle, MessageId_SyncableProperty, SyncablePropertyId_KeyboardReport, (const uint8_t *)report, sizeof(*report));
188192
if (err != 0) {
189193
printk("Failed to send keyboard report to dongle: %d\n", err);
194+
} else {
195+
UsbReportUpdater_ConfirmKeyboardReportSent();
190196
}
191197
break;
192198
#endif
@@ -203,7 +209,11 @@ extern "C" errno_t Hid_SendKeyboardReport(const hid_keyboard_report_t *report)
203209

204210
extern "C" void Hid_KeyboardReportSentCallback(hid_transport_t transport)
205211
{
206-
UsbReportUpdateSemaphore &= ~UsbReportUpdate_Keyboard;
212+
if (DEBUG_STRESS_REPORTS && Utils_Random() % 16 == 0) {
213+
// 666
214+
return;
215+
}
216+
UsbReportUpdater_ConfirmKeyboardReportSent();
207217
noteReportSent(transport == HID_TRANSPORT_USB ? ReportSink_Usb : ReportSink_BleHid);
208218
#if DEVICE_IS_UHK_DONGLE
209219
Dongle_SignalUsbReportSent();
@@ -230,6 +240,8 @@ extern "C" errno_t Hid_SendMouseReport(const hid_mouse_report_t *report)
230240
err = Messenger_Send2(DeviceId_Uhk_Dongle, MessageId_SyncableProperty, SyncablePropertyId_MouseReport, (const uint8_t *)report, sizeof(*report));
231241
if (err != 0) {
232242
printk("Failed to send mouse report to dongle: %d\n", err);
243+
} else {
244+
UsbReportUpdater_ConfirmMouseReportSent();
233245
}
234246
break;
235247
#endif
@@ -249,7 +261,7 @@ extern "C" errno_t Hid_SendMouseReport(const hid_mouse_report_t *report)
249261

250262
extern "C" void Hid_MouseReportSentCallback(hid_transport_t transport)
251263
{
252-
UsbReportUpdateSemaphore &= ~UsbReportUpdate_Mouse;
264+
UsbReportUpdater_ConfirmMouseReportSent();
253265
noteReportSent( transport == HID_TRANSPORT_USB ? ReportSink_Usb : ReportSink_BleHid);
254266
#if DEVICE_IS_UHK_DONGLE
255267
Dongle_SignalUsbReportSent();
@@ -276,6 +288,8 @@ extern "C" errno_t Hid_SendControlsReport(const hid_controls_report_t *report)
276288
err = Messenger_Send2(DeviceId_Uhk_Dongle, MessageId_SyncableProperty, SyncablePropertyId_ControlsReport, (const uint8_t *)report, sizeof(*report));
277289
if (err != 0) {
278290
printk("Failed to send controls report to dongle: %d\n", err);
291+
} else {
292+
UsbReportUpdater_ConfirmControlsReportSent();
279293
}
280294
break;
281295
#endif
@@ -292,7 +306,7 @@ extern "C" errno_t Hid_SendControlsReport(const hid_controls_report_t *report)
292306

293307
extern "C" void Hid_ControlsReportSentCallback(hid_transport_t transport)
294308
{
295-
UsbReportUpdateSemaphore &= ~UsbReportUpdate_Controls;
309+
UsbReportUpdater_ConfirmControlsReportSent();
296310
noteReportSent( transport == HID_TRANSPORT_USB ? ReportSink_Usb : ReportSink_BleHid);
297311
#if DEVICE_IS_UHK_DONGLE
298312
Dongle_SignalUsbReportSent();

right/src/main.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,6 @@ void CopyRightKeystateMatrix(void)
123123
}
124124
}
125125

126-
bool UsbReadyForTransfers(void) {
127-
if (UsbReportUpdateSemaphore && CurrentPowerMode > PowerMode_LastAwake) {
128-
if (Timer_GetElapsedTime(&UpdateUsbReports_LastUpdateTime) < USB_SEMAPHORE_TIMEOUT) {
129-
return false;
130-
} else {
131-
UsbReportUpdateSemaphore = 0;
132-
}
133-
}
134-
return true;
135-
}
136-
137126
static void initUsb() {
138127
while (!IsHardwareConfigInitialized) {
139128
__WFI();
@@ -258,7 +247,7 @@ int main(void)
258247
checkSleepMode();
259248
}
260249

261-
if (UsbReadyForTransfers() && EventScheduler_Vector & EventVector_UserLogicUpdateMask) {
250+
if (EventScheduler_Vector & EventVector_UserLogicUpdateMask) {
262251
Trace('(');
263252
RunUserLogic();
264253
Trace(')');

0 commit comments

Comments
 (0)