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/types.h>
12 #include <zephyr/kernel.h>
13 #include <libmctp.h>
14 #include <zephyr/pmci/mctp/mctp_uart.h>
15 
16 #include <zephyr/logging/log.h>
17 LOG_MODULE_REGISTER(mctp_host);
18 
19 #define LOCAL_HELLO_EID 20
20 
21 #define REMOTE_HELLO_EID 10
22 
23 K_SEM_DEFINE(mctp_rx, 0, 1);
24 
rx_message(uint8_t eid,bool tag_owner,uint8_t msg_tag,void * data,void * msg,size_t len)25 static void rx_message(uint8_t eid, bool tag_owner, uint8_t msg_tag, void *data, void *msg,
26 		       size_t len)
27 {
28 	LOG_INF("received message %s for endpoint %d, msg_tag %d, len %zu", (char *)msg, eid,
29 		msg_tag, len);
30 	k_sem_give(&mctp_rx);
31 }
32 
33 MCTP_UART_DT_DEFINE(mctp_host, DEVICE_DT_GET(DT_NODELABEL(arduino_serial)));
34 
main(void)35 int main(void)
36 {
37 	int rc;
38 	struct mctp *mctp_ctx;
39 
40 	LOG_INF("MCTP Host EID:%d on %s\n", LOCAL_HELLO_EID, CONFIG_BOARD_TARGET);
41 
42 	mctp_set_alloc_ops(malloc, free, realloc);
43 	mctp_ctx = mctp_init();
44 	__ASSERT_NO_MSG(mctp_ctx != NULL);
45 	mctp_register_bus(mctp_ctx, &mctp_host.binding, LOCAL_HELLO_EID);
46 	mctp_set_rx_all(mctp_ctx, rx_message, NULL);
47 	mctp_uart_start_rx(&mctp_host);
48 
49 	/* MCTP poll loop, send "hello" and get "world" back */
50 	while (true) {
51 		rc = mctp_message_tx(mctp_ctx, REMOTE_HELLO_EID, false, 0, "hello",
52 				     sizeof("hello"));
53 		if (rc != 0) {
54 			LOG_WRN("Failed to send message, errno %d\n", rc);
55 			k_msleep(1000);
56 		} else {
57 			k_sem_take(&mctp_rx, K_MSEC(10));
58 		}
59 		rc = 0;
60 	}
61 
62 	return 0;
63 }
64