|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Intel Corporation |
| 3 | + * Copyright 2025 NXP |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <sample_usbd.h> |
| 9 | + |
| 10 | +#include <stdlib.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <assert.h> |
| 13 | +#include <unistd.h> |
| 14 | +#include <zephyr/kernel.h> |
| 15 | +#include <zephyr/types.h> |
| 16 | +#include <zephyr/pmci/mctp/mctp_usb.h> |
| 17 | +#include <zephyr/usb/usbd.h> |
| 18 | +#include <libmctp.h> |
| 19 | + |
| 20 | +#include <zephyr/logging/log.h> |
| 21 | +LOG_MODULE_REGISTER(mctp_endpoint); |
| 22 | + |
| 23 | +#define SELF_ID 10 |
| 24 | +#define BUS_OWNER_ID 20 |
| 25 | + |
| 26 | +K_SEM_DEFINE(mctp_rx, 0, 1); |
| 27 | +MCTP_USB_DEFINE(mctp0, USBD_MCTP_SUBCLASS_MANAGED_DEVICE_ENDPOINT, USBD_MCTP_PROTOCOL_1_X); |
| 28 | + |
| 29 | +static struct mctp *mctp_ctx; |
| 30 | +static bool usb_configured; |
| 31 | + |
| 32 | +static void sample_msg_cb(struct usbd_context *const ctx, const struct usbd_msg *msg) |
| 33 | +{ |
| 34 | + LOG_INF("USBD message: %s", usbd_msg_type_string(msg->type)); |
| 35 | + |
| 36 | + if (msg->type == USBD_MSG_CONFIGURATION) { |
| 37 | + usb_configured = true; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +static int enable_usb_device_next(void) |
| 42 | +{ |
| 43 | + static struct usbd_context *mctp_poc_usbd; |
| 44 | + int err; |
| 45 | + |
| 46 | + mctp_poc_usbd = sample_usbd_init_device(sample_msg_cb); |
| 47 | + if (mctp_poc_usbd == NULL) { |
| 48 | + LOG_ERR("Failed to initialize USB device"); |
| 49 | + return -ENODEV; |
| 50 | + } |
| 51 | + |
| 52 | + if (!usbd_can_detect_vbus(mctp_poc_usbd)) { |
| 53 | + err = usbd_enable(mctp_poc_usbd); |
| 54 | + if (err) { |
| 55 | + LOG_ERR("Failed to enable device support"); |
| 56 | + return err; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + LOG_INF("USB device support enabled"); |
| 61 | + |
| 62 | + return 0; |
| 63 | +} |
| 64 | + |
| 65 | +static void rx_message(uint8_t eid, bool tag_owner, uint8_t msg_tag, void *data, void *msg, |
| 66 | + size_t len) |
| 67 | +{ |
| 68 | + switch (eid) { |
| 69 | + case BUS_OWNER_ID: { |
| 70 | + LOG_INF("Received MCTP message \"%s\" from EID %d", (char *)msg, eid); |
| 71 | + k_sem_give(&mctp_rx); |
| 72 | + break; |
| 73 | + } |
| 74 | + default: { |
| 75 | + LOG_INF("Unknown endpoint %d", eid); |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +int main(void) |
| 82 | +{ |
| 83 | + LOG_INF("MCTP Endpoint EID: %d on %s", SELF_ID, CONFIG_BOARD_TARGET); |
| 84 | + |
| 85 | + int ret = enable_usb_device_next(); |
| 86 | + |
| 87 | + if (ret != 0) { |
| 88 | + LOG_ERR("Failed to enable USB device support"); |
| 89 | + return 0; |
| 90 | + } |
| 91 | + |
| 92 | + while (!usb_configured) { |
| 93 | + k_msleep(5); |
| 94 | + } |
| 95 | + |
| 96 | + mctp_set_alloc_ops(malloc, free, realloc); |
| 97 | + mctp_ctx = mctp_init(); |
| 98 | + __ASSERT_NO_MSG(mctp_ctx != NULL); |
| 99 | + |
| 100 | + mctp_register_bus(mctp_ctx, &mctp0.binding, SELF_ID); |
| 101 | + mctp_set_rx_all(mctp_ctx, rx_message, NULL); |
| 102 | + |
| 103 | + while (1) { |
| 104 | + k_sem_take(&mctp_rx, K_FOREVER); |
| 105 | + mctp_message_tx(mctp_ctx, BUS_OWNER_ID, false, 0, "Hello, bus owner", |
| 106 | + sizeof("Hello, bus owner")); |
| 107 | + } |
| 108 | + |
| 109 | + return 0; |
| 110 | +} |
0 commit comments