|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdlib.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <assert.h> |
| 10 | +#include <unistd.h> |
| 11 | +#include <zephyr/kernel.h> |
| 12 | +#include <zephyr/types.h> |
| 13 | +#include <zephyr/mctp/mctp_uart.h> |
| 14 | +#include <libmctp.h> |
| 15 | + |
| 16 | +#include <zephyr/logging/log.h> |
| 17 | +LOG_MODULE_REGISTER(mctp_endpoint); |
| 18 | + |
| 19 | +#include <zephyr/drivers/uart.h> |
| 20 | + |
| 21 | +static struct mctp *mctp_ctx; |
| 22 | + |
| 23 | +#define LOCAL_HELLO_EID 10 |
| 24 | + |
| 25 | +#define REMOTE_HELLO_EID 20 |
| 26 | + |
| 27 | +K_SEM_DEFINE(mctp_rx, 0, 1); |
| 28 | + |
| 29 | +static void rx_message(uint8_t eid, bool tag_owner, uint8_t msg_tag, void *data, void *msg, |
| 30 | + size_t len) |
| 31 | +{ |
| 32 | + switch (eid) { |
| 33 | + case REMOTE_HELLO_EID: |
| 34 | + LOG_INF("got mctp message %s for eid %d, replying to 5 with \"world\"", (char *)msg, |
| 35 | + eid); |
| 36 | + mctp_message_tx(mctp_ctx, LOCAL_HELLO_EID, false, 0, "world", sizeof("world")); |
| 37 | + break; |
| 38 | + default: |
| 39 | + LOG_INF("Unknown endpoint %d", eid); |
| 40 | + break; |
| 41 | + } |
| 42 | + |
| 43 | + k_sem_give(&mctp_rx); |
| 44 | +} |
| 45 | + |
| 46 | +MCTP_UART_DT_DEFINE(mctp_endpoint, DEVICE_DT_GET(DT_NODELABEL(arduino_serial))); |
| 47 | + |
| 48 | +#define RX_BUF_SZ 128 |
| 49 | + |
| 50 | +int main(void) |
| 51 | +{ |
| 52 | + LOG_INF("MCTP Endpoint EID:%d on %s\n", LOCAL_HELLO_EID, CONFIG_BOARD_TARGET); |
| 53 | + |
| 54 | + mctp_set_alloc_ops(malloc, free, realloc); |
| 55 | + mctp_ctx = mctp_init(); |
| 56 | + __ASSERT_NO_MSG(mctp_ctx != NULL); |
| 57 | + mctp_register_bus(mctp_ctx, &mctp_endpoint.binding, LOCAL_HELLO_EID); |
| 58 | + mctp_set_rx_all(mctp_ctx, rx_message, NULL); |
| 59 | + |
| 60 | + /* MCTP poll loop */ |
| 61 | + while (true) { |
| 62 | + mctp_uart_start_rx(&mctp_endpoint); |
| 63 | + k_sem_take(&mctp_rx, K_FOREVER); |
| 64 | + } |
| 65 | + |
| 66 | + LOG_INF("exiting"); |
| 67 | + return 0; |
| 68 | +} |
0 commit comments