Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
allow agent on both transport concurrently (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
benedekkupper committed Nov 28, 2024
1 parent 42f2cbc commit 7cdb838
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
18 changes: 13 additions & 5 deletions device/src/usb/command_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@
#include "hid/report_protocol.hpp"
#include "zephyr/sys/printk.h"

extern "C" bool CommandProtocolTx(const uint8_t *data, size_t size)
extern "C" bool CommandProtocolTx(void* owner, const uint8_t *data, size_t size)
{
return command_app::handle().send(std::span<const uint8_t>(data, size));
return static_cast<command_app*>(owner)->send(std::span<const uint8_t>(data, size));
}

extern "C" void CommandProtocolRxHandler(const uint8_t *data, size_t size);
extern "C" void CommandProtocolRxHandler(void* owner, const uint8_t *data, size_t size);

command_app &command_app::handle()
command_app &command_app::usb_handle()
{
static command_app app{};
return app;
}

#if DEVICE_IS_UHK80_RIGHT
command_app &command_app::handle()
{
static command_app ble_app{};
return ble_app;
}
#endif

void command_app::start(hid::protocol prot)
{
// start receiving reports
Expand All @@ -32,7 +40,7 @@ void command_app::set_report(hid::report::type type, const std::span<const uint8
receive_report(&out_buffer_[out_buffer_.active_side()]);

auto &out = *reinterpret_cast<const report_out *>(data.data());
CommandProtocolRxHandler(out.payload.data(), data.size() - (report_out::has_id() ? 1 : 0));
CommandProtocolRxHandler(this, out.payload.data(), data.size() - (report_out::has_id() ? 1 : 0));
}

void command_app::get_report(hid::report::selector select, const std::span<uint8_t> &buffer)
Expand Down
4 changes: 4 additions & 0 deletions device/src/usb/command_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "hid/rdf/descriptor.hpp"
#include "hid/report_protocol.hpp"
#include "report_ids.h"
#include "../device.h"

namespace hid::page {
enum class ugl : uint8_t;
Expand Down Expand Up @@ -61,7 +62,10 @@ class command_app : public hid::application {
using report_in = report_base<hid::report::type::INPUT, report_ids::IN_COMMAND>;
using report_out = report_base<hid::report::type::OUTPUT, report_ids::OUT_COMMAND>;

static command_app& usb_handle();
#if DEVICE_IS_UHK80_RIGHT
static command_app& handle();
#endif

bool send(std::span<const uint8_t> buffer);

Expand Down
22 changes: 10 additions & 12 deletions device/src/usb/usb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ struct usb_manager {
static usb::df::hid::function usb_kb{
keyboard_app::handle(), usb::hid::boot_protocol_mode::KEYBOARD};
static usb::df::hid::function usb_mouse{mouse_app::handle()};
static usb::df::hid::function usb_command{command_app::handle()};
static usb::df::hid::function usb_command{command_app::usb_handle()};
static usb::df::hid::function usb_controls{controls_app::handle()};
static usb::df::hid::function usb_gamepad{gamepad_app::handle()};
static usb::df::microsoft::xfunction usb_xpad{gamepad_app::handle()};
Expand All @@ -128,17 +128,19 @@ struct usb_manager {
usb::df::hid::config(usb_mouse, speed, usb::endpoint::address(0x82), 1),
usb::df::hid::config(usb_command, speed, usb::endpoint::address(0x83), 10),
usb::df::hid::config(usb_controls, speed, usb::endpoint::address(0x84), 1)
#if !DEVICE_HAS_BATTERY
);
#else
#if DEVICE_HAS_BATTERY
,
usb::df::hid::config(usb_battery, speed, usb::endpoint::address(0x86), 1)
// not very useful at the moment
#endif
);

static const auto battery_config = usb::df::config::make_config(config_header,
usb::df::hid::config(usb_battery, speed, usb::endpoint::address(0x86), 1));
static const auto inactive_config = usb::df::config::make_config(config_header,
usb::df::hid::config(usb_command, speed, usb::endpoint::address(0x83), 10)
#if DEVICE_HAS_BATTERY
,
usb::df::hid::config(usb_battery, speed, usb::endpoint::address(0x86), 1)
#endif
);

static const auto base_config =
usb::df::config::make_config(config_header, shared_config_elems);
Expand All @@ -155,12 +157,8 @@ struct usb_manager {
printk("USB config changing to %s\n", magic_enum::enum_name(conf).data());
switch (conf) {
case Hid_Empty:
#if DEVICE_HAS_BATTERY
ms_enum_.set_config({});
device_.set_config(battery_config);
#else
assert(false);
#endif
device_.set_config(inactive_config);
break;
case Hid_NoGamepad:
ms_enum_.set_config({});
Expand Down
4 changes: 2 additions & 2 deletions right/src/usb_protocol_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
#endif

#ifdef __ZEPHYR__
void CommandProtocolRxHandler(const uint8_t* data, size_t size)
void CommandProtocolRxHandler(void* owner, const uint8_t* data, size_t size)
{
GenericHidOutBuffer = data;
// printk("CommandProtocolRxHandler: data[0]:%u size:%d\n", data[0], size);
UsbProtocolHandler();
CommandProtocolTx(GenericHidInBuffer, USB_GENERIC_HID_OUT_BUFFER_LENGTH);
CommandProtocolTx(owner, GenericHidInBuffer, USB_GENERIC_HID_OUT_BUFFER_LENGTH);
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion right/src/usb_protocol_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
// Functions:

#ifdef __ZEPHYR__
extern bool CommandProtocolTx(const uint8_t* data, size_t size);
extern bool CommandProtocolTx(void* owner, const uint8_t* data, size_t size);

void SetUsbTxBufferBleAddress(uint32_t offset, const bt_addr_le_t* addr);
extern bt_addr_le_t GetUsbRxBufferBleAddress(uint32_t offset);
Expand Down

0 comments on commit 7cdb838

Please sign in to comment.