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 Dec 5, 2024
1 parent b7ec4ee commit 1601c9c
Show file tree
Hide file tree
Showing 54 changed files with 253 additions and 234 deletions.
43 changes: 18 additions & 25 deletions device/src/usb/command_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
#include "hid/report_protocol.hpp"
#include "zephyr/sys/printk.h"

extern "C" bool CommandProtocolTx(const uint8_t *data, size_t size)
extern "C" void UsbProtocolHandler(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);

command_app &command_app::usb_handle()
{
return command_app::handle().send(std::span<const uint8_t>(data, size));
static command_app app{};
return app;
}

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

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

void command_app::start(hid::protocol prot)
{
Expand All @@ -32,7 +35,15 @@ 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));
auto buf_idx = in_buffer_.active_side();
auto &in = in_buffer_[buf_idx];
UsbProtocolHandler(out.payload.data(), in.payload.data());
auto err = send_report(&in);
if (err == hid::result::OK) {
in_buffer_.compare_swap(buf_idx);
} else {
printk("Command app failed to send report with: %d\n", (int)err);
}
}

void command_app::get_report(hid::report::selector select, const std::span<uint8_t> &buffer)
Expand All @@ -47,24 +58,6 @@ void command_app::get_report(hid::report::selector select, const std::span<uint8
send_report(buffer.subspan(0, sizeof(report)));
}

bool command_app::send(std::span<const uint8_t> buffer)
{
auto buf_idx = in_buffer_.active_side();
auto &report = in_buffer_[buf_idx];
if (buffer.size() > report.payload.max_size()) {
printk("Usb payload exceeded maximum size!\n");
return false;
}
std::copy(buffer.begin(), buffer.end(), report.payload.begin());
c2usb::result err = send_report(&in_buffer_[buf_idx]);
if (err == hid::result::OK) {
in_buffer_.compare_swap(buf_idx);
return true;
}
printk("Command app failed to send report with: %i\n", (int)err);
return false;
}

void command_app::in_report_sent(const std::span<const uint8_t> &data)
{
if (data.front() != report_ids::IN_COMMAND) {
Expand Down
6 changes: 4 additions & 2 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,9 +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();

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

void start(hid::protocol prot) override;
void set_report(hid::report::type type, const std::span<const uint8_t>& data) override;
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
19 changes: 10 additions & 9 deletions right/src/usb_commands/usb_command_apply_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,34 @@
#include "main.h"
#endif

void updateUsbBuffer(uint8_t usbStatusCode, uint16_t parserOffset, parser_stage_t parserStage)
void updateUsbBuffer(uint8_t *GenericHidInBuffer, uint8_t usbStatusCode, uint16_t parserOffset, parser_stage_t parserStage)
{
SetUsbTxBufferUint8(0, usbStatusCode);
SetUsbTxBufferUint16(1, parserOffset);
SetUsbTxBufferUint8(3, parserStage);
}

static uint8_t validateConfig() {
static uint8_t validateConfig(uint8_t *GenericHidInBuffer) {
// Validate the staging configuration.
ParserRunDry = true;
StagingUserConfigBuffer.offset = 0;
uint8_t parseConfigStatus = ParseConfig(&StagingUserConfigBuffer);
updateUsbBuffer(UsbStatusCode_Success, StagingUserConfigBuffer.offset, ParsingStage_Validate);

if (GenericHidInBuffer) {
updateUsbBuffer(GenericHidInBuffer, UsbStatusCode_Success, StagingUserConfigBuffer.offset, ParsingStage_Validate);
}
return parseConfigStatus;
}

void UsbCommand_ApplyConfigAsync(void) {
if (validateConfig() == UsbStatusCode_Success) {
void UsbCommand_ApplyConfigAsync(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer) {
if (validateConfig(GenericHidInBuffer) == UsbStatusCode_Success) {
EventVector_Set(EventVector_ApplyConfig);
#ifdef __ZEPHYR__
Main_Wake();
#endif
}
}

void UsbCommand_ApplyFactory(void)
void UsbCommand_ApplyFactory(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer)
{
EventVector_Unset(EventVector_ApplyConfig);

Expand All @@ -63,12 +64,12 @@ void UsbCommand_ApplyFactory(void)
LedManager_FullUpdate();
}

uint8_t UsbCommand_ApplyConfig(void)
uint8_t UsbCommand_ApplyConfig(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer)
{
static bool isBoot = true;
EventVector_Unset(EventVector_ApplyConfig);

uint8_t parseConfigStatus = validateConfig();
uint8_t parseConfigStatus = validateConfig(GenericHidInBuffer);

if (parseConfigStatus != UsbStatusCode_Success) {
return parseConfigStatus;
Expand Down
6 changes: 3 additions & 3 deletions right/src/usb_commands/usb_command_apply_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

// Functions:

uint8_t UsbCommand_ApplyConfig(void);
void UsbCommand_ApplyFactory(void);
void UsbCommand_ApplyConfigAsync(void);
uint8_t UsbCommand_ApplyConfig(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);
void UsbCommand_ApplyFactory(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);
void UsbCommand_ApplyConfigAsync(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);

#endif
2 changes: 1 addition & 1 deletion right/src/usb_commands/usb_command_draw_oled.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "debug.h"
#include <stdint.h>

void UsbCommand_DrawOled()
void UsbCommand_DrawOled(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer)
{
#if defined(__ZEPHYR__) && DEVICE_HAS_OLED
uint8_t x = GetUsbRxBufferUint8(1);
Expand Down
2 changes: 1 addition & 1 deletion right/src/usb_commands/usb_command_draw_oled.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

// Functions:

void UsbCommand_DrawOled();
void UsbCommand_DrawOled(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);

#endif
6 changes: 3 additions & 3 deletions right/src/usb_commands/usb_command_exec_macro_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ char UsbMacroCommand[USB_GENERIC_HID_OUT_BUFFER_LENGTH+1];
uint8_t UsbMacroCommandLength = 0;
key_state_t dummyState;

static void requestExecution()
static void requestExecution(const uint8_t *GenericHidOutBuffer)
{
Utils_SafeStrCopy(UsbMacroCommand, ((char*)GenericHidOutBuffer) + 1, sizeof(GenericHidOutBuffer)-1);
UsbMacroCommandLength = strlen(UsbMacroCommand);
Expand All @@ -41,9 +41,9 @@ void UsbMacroCommand_ExecuteSynchronously()
EventVector_Unset(EventVector_UsbMacroCommandWaitingForExecution);
}

void UsbCommand_ExecMacroCommand()
void UsbCommand_ExecMacroCommand(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer)
{
if (canExecute()) {
requestExecution();
requestExecution(GenericHidOutBuffer);
}
}
2 changes: 1 addition & 1 deletion right/src/usb_commands/usb_command_exec_macro_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
// Functions:

void UsbMacroCommand_ExecuteSynchronously();
void UsbCommand_ExecMacroCommand();
void UsbCommand_ExecMacroCommand(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);

#endif
2 changes: 1 addition & 1 deletion right/src/usb_commands/usb_command_get_adc_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "usb_commands/usb_command_get_adc_value.h"
#include "peripherals/adc.h"

void UsbCommand_GetAdcValue(void)
void UsbCommand_GetAdcValue(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer)
{
SetUsbTxBufferUint32(1, ADC_Measure());
}
6 changes: 5 additions & 1 deletion right/src/usb_commands/usb_command_get_adc_value.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#ifndef __USB_COMMAND_GET_ADC_VALUE_H__
#define __USB_COMMAND_GET_ADC_VALUE_H__

// Includes:

#include <stdint.h>

// Functions:

void UsbCommand_GetAdcValue(void);
void UsbCommand_GetAdcValue(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);

#endif
2 changes: 1 addition & 1 deletion right/src/usb_commands/usb_command_get_debug_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

uint8_t DebugBuffer[USB_GENERIC_HID_IN_BUFFER_LENGTH];

void UsbCommand_GetDebugBuffer(void)
void UsbCommand_GetDebugBuffer(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer)
{
#ifndef __ZEPHYR__
SetDebugBufferUint32(1, I2C_Watchdog);
Expand Down
2 changes: 1 addition & 1 deletion right/src/usb_commands/usb_command_get_debug_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// Functions:

void UsbCommand_GetDebugBuffer(void);
void UsbCommand_GetDebugBuffer(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);

void SetDebugBufferUint8(uint32_t offset, uint8_t value);
void SetDebugBufferUint16(uint32_t offset, uint16_t value);
Expand Down
4 changes: 2 additions & 2 deletions right/src/usb_commands/usb_command_get_device_property.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

uint16_t configSizes[] = {HARDWARE_CONFIG_SIZE, USER_CONFIG_SIZE};

void UsbCommand_GetDeviceProperty(void)
void UsbCommand_GetDeviceProperty(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer)
{
uint8_t propertyId = GetUsbRxBufferUint8(1);
uint8_t *dest = GenericHidInBuffer + 1;
Expand Down Expand Up @@ -105,7 +105,7 @@ void UsbCommand_GetDeviceProperty(void)
} break;
case DevicePropertyId_NewPairings:
#ifdef __ZEPHYR__
UsbCommand_GetNewPairings();
UsbCommand_GetNewPairings(GenericHidOutBuffer, GenericHidInBuffer);
#endif
break;
default:
Expand Down
6 changes: 5 additions & 1 deletion right/src/usb_commands/usb_command_get_device_property.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef __USB_COMMAND_GET_DEVICE_PROPERTY_H__
#define __USB_COMMAND_GET_DEVICE_PROPERTY_H__

// Includes:

#include <stdint.h>

// Typedefs:

typedef enum {
Expand Down Expand Up @@ -31,6 +35,6 @@

// Functions:

void UsbCommand_GetDeviceProperty(void);
void UsbCommand_GetDeviceProperty(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);

#endif
2 changes: 1 addition & 1 deletion right/src/usb_commands/usb_command_get_device_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#define Bt_NewPairedDevice 0
#endif

void UsbCommand_GetKeyboardState(void)
void UsbCommand_GetKeyboardState(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer)
{

#ifdef __ZEPHYR__
Expand Down
6 changes: 5 additions & 1 deletion right/src/usb_commands/usb_command_get_device_state.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef __USB_COMMAND_GET_KEYBOARD_STATE_H__
#define __USB_COMMAND_GET_KEYBOARD_STATE_H__

// Includes:

#include <stdint.h>

// Typedefs:

typedef enum {
Expand All @@ -18,6 +22,6 @@ typedef enum {

// Functions:

void UsbCommand_GetKeyboardState(void);
void UsbCommand_GetKeyboardState(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);

#endif
2 changes: 1 addition & 1 deletion right/src/usb_commands/usb_command_get_module_property.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "utils.h"
#include "versioning.h"

void UsbCommand_GetModuleProperty()
void UsbCommand_GetModuleProperty(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer)
{
slot_t slotId = GetUsbRxBufferUint8(1);

Expand Down
6 changes: 5 additions & 1 deletion right/src/usb_commands/usb_command_get_module_property.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#ifndef __USB_COMMAND_GET_MODULE_PROPERTY_H__
#define __USB_COMMAND_GET_MODULE_PROPERTY_H__

// Includes:

#include <stdint.h>

// Functions:

void UsbCommand_GetModuleProperty();
void UsbCommand_GetModuleProperty(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer);

// Typedefs:

Expand Down
Loading

0 comments on commit 1601c9c

Please sign in to comment.