| /* |
| * Copyright (c) 2023-2024 Nordic Semiconductor ASA |
| * |
| * SPDX-License-Identifier: Apache-2.0 |
| */ |
| |
| #include <sample_usbd.h> |
| |
| #include <zephyr/sys/byteorder.h> |
| #include <zephyr/usb/usbd.h> |
| #include <zephyr/usb/class/usbd_hid.h> |
| #include <zephyr/usb/msos_desc.h> |
| |
| #include <zephyr/logging/log.h> |
| LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); |
| |
| /* |
| * There are three BOS descriptors used in the sample, a USB 2.0 EXTENSION from |
| * the USB samples common code, a Microsoft OS 2.0 platform capability |
| * descriptor, and a WebUSB platform capability descriptor. |
| */ |
| #include "webusb.h" |
| #include "msosv2.h" |
| |
| static void msg_cb(struct usbd_context *const usbd_ctx, |
| const struct usbd_msg *const msg) |
| { |
| LOG_INF("USBD message: %s", usbd_msg_type_string(msg->type)); |
| |
| if (usbd_can_detect_vbus(usbd_ctx)) { |
| if (msg->type == USBD_MSG_VBUS_READY) { |
| if (usbd_enable(usbd_ctx)) { |
| LOG_ERR("Failed to enable device support"); |
| } |
| } |
| |
| if (msg->type == USBD_MSG_VBUS_REMOVED) { |
| if (usbd_disable(usbd_ctx)) { |
| LOG_ERR("Failed to disable device support"); |
| } |
| } |
| } |
| } |
| |
| int main(void) |
| { |
| struct usbd_context *sample_usbd; |
| int ret; |
| |
| sample_usbd = sample_usbd_setup_device(msg_cb); |
| if (sample_usbd == NULL) { |
| LOG_ERR("Failed to setup USB device"); |
| return -ENODEV; |
| } |
| |
| ret = usbd_add_descriptor(sample_usbd, &bos_vreq_msosv2); |
| if (ret) { |
| LOG_ERR("Failed to add MSOSv2 capability descriptor"); |
| return ret; |
| } |
| |
| ret = usbd_add_descriptor(sample_usbd, &bos_vreq_webusb); |
| if (ret) { |
| LOG_ERR("Failed to add WebUSB capability descriptor"); |
| return ret; |
| } |
| |
| ret = usbd_init(sample_usbd); |
| if (ret) { |
| LOG_ERR("Failed to initialize device support"); |
| return ret; |
| } |
| |
| if (!usbd_can_detect_vbus(sample_usbd)) { |
| ret = usbd_enable(sample_usbd); |
| if (ret) { |
| LOG_ERR("Failed to enable device support"); |
| return ret; |
| } |
| } |
| |
| return 0; |
| } |