usb: prevent registering duplicate config callbacks

Originally discussed at https://github.com/libopencm3/libopencm3/pull/722
This commit is contained in:
Karl Palsson 2018-07-25 22:11:25 +00:00
parent c55ec70f49
commit f4bbe7c5bb
2 changed files with 5 additions and 1 deletions

View File

@ -139,7 +139,8 @@ extern int usbd_register_control_callback(usbd_device *usbd_dev, uint8_t type,
/* <usb_standard.c> */
/** Registers a "Set Config" callback
* @return 0 if successful
* @return 0 if successful or already existed.
* @return -1 if no more space was available for callbacks.
*/
extern int usbd_register_set_config_callback(usbd_device *usbd_dev,
usbd_set_config_callback callback);

View File

@ -46,6 +46,9 @@ int usbd_register_set_config_callback(usbd_device *usbd_dev,
for (i = 0; i < MAX_USER_SET_CONFIG_CALLBACK; i++) {
if (usbd_dev->user_callback_set_config[i]) {
if (usbd_dev->user_callback_set_config[i] == callback) {
return 0;
}
continue;
}