Skip to content

[USB HID] Harden init #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions cores/arduino/stm32/usb/hid/usbd_hid_composite_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,58 @@ extern "C" {
/* USB Device Core HID composite handle declaration */
USBD_HandleTypeDef hUSBD_Device_HID;

static bool HID_keyboard_initialized = false;
static bool HID_mouse_initialized = false;

/**
* @brief Initialize USB devices
* @param none
* @param HID_Interface device type: HID_KEYBOARD or HID_MOUSE
* @retval none
*/
void HID_Composite_Init(void)
void HID_Composite_Init(HID_Interface device)
{
/* Init Device Library */
USBD_Init(&hUSBD_Device_HID, &HID_Desc, 0);

/* Add Supported Class */
USBD_RegisterClass(&hUSBD_Device_HID, USBD_COMPOSITE_HID_CLASS);

/* Start Device Process */
USBD_Start(&hUSBD_Device_HID);
if (IS_HID_INTERFACE(device) &&
!HID_keyboard_initialized && !HID_mouse_initialized) {
/* Init Device Library */
if (USBD_Init(&hUSBD_Device_HID, &HID_Desc, 0) == USBD_OK) {
/* Add Supported Class */
if (USBD_RegisterClass(&hUSBD_Device_HID, USBD_COMPOSITE_HID_CLASS) == USBD_OK) {
/* Start Device Process */
USBD_Start(&hUSBD_Device_HID);
HID_keyboard_initialized = true;
HID_mouse_initialized = true;
}
}
}
if (device == HID_KEYBOARD) {
HID_keyboard_initialized = HID_mouse_initialized;
}
if (device == HID_MOUSE) {
HID_mouse_initialized = HID_keyboard_initialized;
}
}

/**
* @brief DeInitialize USB devices
* @param none
* @param HID_Interface device type: HID_KEYBOARD or HID_MOUSE
* @retval none
*/
void HID_Composite_DeInit(void)
void HID_Composite_DeInit(HID_Interface device)
{
/* Stop Device Process */
USBD_Stop(&hUSBD_Device_HID);

/* DeInit Device Library */
USBD_DeInit(&hUSBD_Device_HID);
if (IS_HID_INTERFACE(device) &&
((HID_keyboard_initialized && !HID_mouse_initialized) ||
(HID_mouse_initialized && !HID_keyboard_initialized))) {
/* Stop Device Process */
USBD_Stop(&hUSBD_Device_HID);
/* DeInit Device Library */
USBD_DeInit(&hUSBD_Device_HID);
}
if (device == HID_KEYBOARD) {
HID_keyboard_initialized = false;
}
if (device == HID_MOUSE) {
HID_mouse_initialized = false;
}
}

/**
Expand Down
14 changes: 12 additions & 2 deletions cores/arduino/stm32/usb/hid/usbd_hid_composite_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,19 @@
extern "C" {
#endif

/* Exported types ------------------------------------------------------------*/
typedef enum {
HID_KEYBOARD,
HID_MOUSE
} HID_Interface;

/* Exported macro ------------------------------------------------------------*/
#define IS_HID_INTERFACE(DEVICE) (((DEVICE) == HID_KEYBOARD) ||\
((DEVICE) == HID_MOUSE))

/* Exported functions ------------------------------------------------------- */
void HID_Composite_Init(void);
void HID_Composite_DeInit(void);
void HID_Composite_Init(HID_Interface device);
void HID_Composite_DeInit(HID_Interface device);

void HID_Composite_mouse_sendReport(uint8_t *report, uint16_t len);
void HID_Composite_keyboard_sendReport(uint8_t *report, uint16_t len);
Expand Down
4 changes: 2 additions & 2 deletions libraries/Keyboard/src/Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ Keyboard_::Keyboard_(void)

void Keyboard_::begin(void)
{
HID_Composite_Init();
HID_Composite_Init(HID_KEYBOARD);
}

void Keyboard_::end(void)
{
HID_Composite_DeInit();
HID_Composite_DeInit(HID_KEYBOARD);
}

void Keyboard_::sendReport(KeyReport *keys)
Expand Down
4 changes: 2 additions & 2 deletions libraries/Mouse/src/Mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ Mouse_::Mouse_(void) : _buttons(0)

void Mouse_::begin(void)
{
HID_Composite_Init();
HID_Composite_Init(HID_MOUSE);
}

void Mouse_::end(void)
{
HID_Composite_DeInit();
HID_Composite_DeInit(HID_MOUSE);
}

void Mouse_::click(uint8_t b)
Expand Down