CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
UsbManager.h
Go to the documentation of this file.
1#pragma once
2
3#include "cdc_core/IService.h"
4#include <cstdint>
5
6namespace cdc::core {
7
8enum class UsbHidInterface : uint8_t {
9 Fido = 0,
11 Ccid = 2,
12};
13
14enum class UsbInterfaceClass : uint8_t {
15 Hid = 0,
16 Ccid = 1,
17};
18
20 uint16_t (*onGetReport)(uint8_t report_id, uint8_t report_type,
21 uint8_t* buffer, uint16_t reqlen) = nullptr;
22 void (*onSetReport)(uint8_t report_id, uint8_t report_type,
23 uint8_t const* buffer, uint16_t bufsize) = nullptr;
24 void (*onReportComplete)(uint8_t const* report, uint16_t len) = nullptr;
25};
26
29 const char* name = nullptr; // Interface name for USB descriptor
30 const uint8_t* reportDesc = nullptr;
31 uint16_t reportDescLen = 0;
32 uint8_t protocol = 0; // HID protocol (0=none, 1=keyboard)
33 bool hasOut = false;
34 uint16_t epInSize = 64;
35 uint16_t epOutSize = 64;
37
38 // Optional device-descriptor VID/PID hint. When non-zero, an active
39 // interface carrying these values overrides the default device VID/PID so
40 // the host recognizes the composite as a specific product (e.g. mod_otphid
41 // presents OnlyKey's KeePassXC-whitelisted IDs). 0 means "no preference".
42 uint16_t preferredVid = 0;
43 uint16_t preferredPid = 0;
44};
45
52class UsbManager : public IService {
53public:
54 static UsbManager& instance();
55
56 const char* getName() const override { return "UsbManager"; }
57
58 bool init() override;
59 bool start() override;
60 void stop() override;
61 ServiceState getState() const override { return state_; }
62
63 bool registerInterface(UsbHidInterface type, const char* moduleName,
64 const UsbInterfaceSpec& def);
65 void unregisterInterface(UsbHidInterface type, const char* moduleName);
66
67 uint8_t activeInterfaceMask() const { return activeMask_; }
68 bool applyConfiguration();
69 bool needsReplug() const { return needsReplug_; }
70
75 bool hidSlotsFull() const { return activeHidCount() >= MAX_ACTIVE_HID; }
76
85 bool newlyRequiresReplug(bool wasNeededBefore) const {
86 return !wasNeededBefore && needsReplug_;
87 }
88
89private:
90 struct InterfaceEntry {
91 bool active = false;
92 const char* owner = nullptr;
93 UsbInterfaceSpec def = {};
94 };
95
96 uint8_t activeHidCount() const;
97 bool canActivate(UsbHidInterface type) const;
98 static constexpr uint8_t MAX_ACTIVE_HID = 2;
99
101 uint8_t activeMask_ = 0;
102 bool needsReplug_ = false;
103 InterfaceEntry entries_[3] = {};
104};
105
106} // namespace cdc::core
bool init() override
Initializes USB manager service state.
bool registerInterface(UsbHidInterface type, const char *moduleName, const UsbInterfaceSpec &def)
Registers a HID interface request from a module.
const char * getName() const override
Definition UsbManager.h:56
bool hidSlotsFull() const
Reports whether the HID interface budget is exhausted.
Definition UsbManager.h:75
ServiceState getState() const override
Definition UsbManager.h:61
bool needsReplug() const
Definition UsbManager.h:69
bool newlyRequiresReplug(bool wasNeededBefore) const
Tests whether a toggle newly introduced a replug requirement.
Definition UsbManager.h:85
bool start() override
Starts USB manager service state.
static UsbManager & instance()
Returns singleton USB manager instance.
void unregisterInterface(UsbHidInterface type, const char *moduleName)
Unregisters a previously registered HID interface.
void stop() override
Stops USB manager service state.
uint8_t activeInterfaceMask() const
Definition UsbManager.h:67
bool applyConfiguration()
Applies current interface set to USB HID stack.
void(* onReportComplete)(uint8_t const *report, uint16_t len)
Definition UsbManager.h:24
void(* onSetReport)(uint8_t report_id, uint8_t report_type, uint8_t const *buffer, uint16_t bufsize)
Definition UsbManager.h:22
uint16_t(* onGetReport)(uint8_t report_id, uint8_t report_type, uint8_t *buffer, uint16_t reqlen)
Definition UsbManager.h:20
const uint8_t * reportDesc
Definition UsbManager.h:30
UsbInterfaceClass cls
Definition UsbManager.h:28
UsbHidCallbacks callbacks
Definition UsbManager.h:36