Skip to content

Commit

Permalink
Connect parallel routing c2usb implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
kareltucek committed Jan 9, 2025
1 parent fe16966 commit 925fea3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 40 deletions.
7 changes: 5 additions & 2 deletions device/src/bt_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ static void connectHid(struct bt_conn *conn, connection_id_t connectionId, conne
ATTR_UNUSED static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr, struct bt_gatt_discover_params *params)
{
if (!attr) {
printk("Service discovery completed, connection wasn't matched. Requesting authentication!\n");
bt_conn_set_security(conn, BT_SECURITY_L4);
printk("Service discovery completed, connection wasn't matched.\n");
// TODO: consider setting a timer to disconnect the connection if neither auth nor security is Established
return BT_GATT_ITER_STOP;
}

Expand Down Expand Up @@ -808,6 +808,9 @@ uint8_t BtConn_UnusedPeripheralConnectionCount() {
count++;
}
}
if (auth_conn) {
count--;
}
return count;
}

Expand Down
7 changes: 5 additions & 2 deletions device/src/bt_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "bt_scan.h"
#include "settings.h"

#define BT_SHORT_RETRY_DELAY 1000

bool BtManager_Restarting = false;

Expand Down Expand Up @@ -85,6 +86,8 @@ void BtManager_StartScanningAndAdvertisingAsync() {
* - first try: start advertising and scanning
* - second try: stop and then start advertising and scanning
* - third try: disconnect all connections, stop and then start advertising and scanning
*
* This should be called from the event scheduler.
*/
void BtManager_StartScanningAndAdvertising() {
bool success = true;
Expand Down Expand Up @@ -139,7 +142,7 @@ void BtManager_StartScanningAndAdvertising() {
try = 0;
} else {
try++;
uint32_t delay = try < 3 ? 100 : 10000;
uint32_t delay = try < 3 ? BT_SHORT_RETRY_DELAY : 10000;
EventScheduler_Reschedule(CurrentTime + delay, EventSchedulerEvent_BtStartScanningAndAdvertising, "BtManager_StartScanningAndAdvertising failed");
}
}
Expand Down Expand Up @@ -183,5 +186,5 @@ void BtManager_RestartBt() {
}

void BtManager_RestartBtAsync() {
EventScheduler_Schedule(k_uptime_get()+10, EventSchedulerEvent_RestartBt, "Restart bluetooth");
EventScheduler_Schedule(k_uptime_get()+BT_SHORT_RETRY_DELAY, EventSchedulerEvent_RestartBt, "Restart bluetooth");
}
98 changes: 62 additions & 36 deletions device/src/usb/usb_compatibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ static controls_buffer controls;

keyboard_led_state_t KeyboardLedsState;

static bool sendOverC2usb() {
typedef enum {
ReportSink_Invalid,
ReportSink_Usb,
ReportSink_BleHid,
ReportSink_Dongle,
} report_sink_t;

static report_sink_t determineSink() {
if (DEVICE_IS_UHK_DONGLE) {
return true;
return ReportSink_Usb;
}

connection_type_t connectionType = Connections_Type(ActiveHostConnectionId);
Expand All @@ -39,44 +46,57 @@ static bool sendOverC2usb() {
Connections_HandleSwitchover(ConnectionId_Invalid, false);
if (!Connections_IsReady(ActiveHostConnectionId)) {
printk("Giving report to c2usb anyways!\n");
return true;
return ReportSink_Usb;
}
}

switch (connectionType) {
case ConnectionType_BtHid:
return ReportSink_BleHid;
case ConnectionType_UsbHidRight:
return true;
return ReportSink_Usb;
case ConnectionType_NusDongle:
return false;
if (DEVICE_IS_UHK80_RIGHT) {
return ReportSink_Dongle;
}
default:
printk("Unhandled connection type. Is this connection really meant to be a report target?\n");
return false;
printk("Unhandled sink type. Is this connection really meant to be a report target?\n");
return ReportSink_Usb;
}
}

extern "C" void UsbCompatibility_SendKeyboardReport(const usb_basic_keyboard_report_t* report)
{
keyboard_app *keyboard_app = &keyboard_app::usb_handle();
// TODO: keyboard_app = &keyboard_app::ble_handle();

if (sendOverC2usb()) {
printk("Handing report over to c2usb!\n");
keyboard_app->set_report_state(*reinterpret_cast<const scancode_buffer*>(report));
} else if (DEVICE_IS_UHK80_RIGHT){
Messenger_Send2(DeviceId_Uhk_Dongle, MessageId_SyncableProperty, SyncablePropertyId_KeyboardReport, (const uint8_t*)report, sizeof(*report));
switch (determineSink()) {
case ReportSink_Usb:
keyboard_app::usb_handle().set_report_state(*reinterpret_cast<const scancode_buffer*>(report));
break;
case ReportSink_BleHid:
keyboard_app::ble_handle().set_report_state(*reinterpret_cast<const scancode_buffer*>(report));
printk("Giving report to c2usb ble hid!\n");
break;
case ReportSink_Dongle:
Messenger_Send2(DeviceId_Uhk_Dongle, MessageId_SyncableProperty, SyncablePropertyId_KeyboardReport, (const uint8_t*)report, sizeof(*report));
break;
default:
printk("Unhandled and unexpected switch state!\n");
}
}

extern "C" void UsbCompatibility_SendMouseReport(const usb_mouse_report_t *report)
{
mouse_app *mouse_app = &mouse_app::usb_handle();
// TODO: mouse_app = &mouse_app::ble_handle();

if (sendOverC2usb()) {
mouse_app->set_report_state(*reinterpret_cast<const mouse_buffer*>(report));
} else if (DEVICE_IS_UHK80_RIGHT) {
Messenger_Send2(DeviceId_Uhk_Dongle, MessageId_SyncableProperty, SyncablePropertyId_MouseReport, (const uint8_t*)report, sizeof(*report));
switch (determineSink()) {
case ReportSink_Usb:
mouse_app::usb_handle().set_report_state(*reinterpret_cast<const mouse_buffer*>(report));
break;
case ReportSink_BleHid:
mouse_app::ble_handle().set_report_state(*reinterpret_cast<const mouse_buffer*>(report));
break;
case ReportSink_Dongle:
Messenger_Send2(DeviceId_Uhk_Dongle, MessageId_SyncableProperty, SyncablePropertyId_MouseReport, (const uint8_t*)report, sizeof(*report));
break;
default:
printk("Unhandled and unexpected switch state!\n");
}
}

Expand All @@ -95,28 +115,34 @@ extern "C" void UsbCompatibility_SendConsumerReport(const usb_media_keyboard_rep
}
UsbSystemKeyboard_ForeachScancode(systemReport, &UsbCompatibility_ConsumerKeyboardAddScancode);

controls_app *controls_app = &controls_app::usb_handle();
// TODO: controls_app = &controls_app::ble_handle();

if (sendOverC2usb()) {
controls_app->set_report_state(controls);
} else if (DEVICE_IS_UHK80_RIGHT) {
Messenger_Send2(DeviceId_Uhk_Dongle, MessageId_SyncableProperty, SyncablePropertyId_ControlsReport, (const uint8_t*)(&controls), sizeof(controls));
switch (determineSink()) {
case ReportSink_Usb:
controls_app::usb_handle().set_report_state(controls);
break;
case ReportSink_BleHid:
controls_app::ble_handle().set_report_state(controls);
break;
case ReportSink_Dongle:
Messenger_Send2(DeviceId_Uhk_Dongle, MessageId_SyncableProperty, SyncablePropertyId_ControlsReport, (const uint8_t*)&controls, sizeof(controls));
break;
default:
printk("Unhandled and unexpected switch state!\n");
}
}

extern "C" void UsbCompatibility_SendConsumerReport2(const uint8_t *report)
{
controls_app *controls_app = &controls_app::usb_handle();
// TODO: controls_app = &controls_app::ble_handle();

if (controls_app->active()) {
controls_app->set_report_state(*(const controls_buffer *)report);
switch (determineSink()) {
case ReportSink_Usb:
controls_app::usb_handle().set_report_state(*(const controls_buffer *)report);
break;
default:
printk("This wasn't expected. Is this a dongle?\n");
break;
}
}

extern "C" void UsbCompatibility_SetKeyboardLedsState(
connection_id_t connectionId, bool capsLock, bool numLock, bool scrollLock)
extern "C" void UsbCompatibility_SetKeyboardLedsState(connection_id_t connectionId, bool capsLock, bool numLock, bool scrollLock)
{
// TODO: deal with connectionId
if (KeyboardLedsState.capsLock != capsLock) {
Expand Down

0 comments on commit 925fea3

Please sign in to comment.