1 /*
2  * Copyright (c) 2025 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_i2c_gpio_target.h>
15 
16 #include <zephyr/logging/log.h>
17 LOG_MODULE_REGISTER(mctp_i2c_gpio_bus_endpoint);
18 
19 MCTP_I2C_GPIO_TARGET_DT_DEFINE(mctp_i2c_ctrl, DT_NODELABEL(mctp_i2c));
20 
rx_message(uint8_t eid,bool tag_owner,uint8_t msg_tag,void * data,void * msg,size_t len)21 static void rx_message(uint8_t eid, bool tag_owner, uint8_t msg_tag, void *data, void *msg,
22 		       size_t len)
23 {
24 	LOG_INF("received message \"%s\" from endpoint %d, msg_tag %d, len %zu", (char *)msg, eid,
25 		msg_tag, len);
26 }
27 
28 #define BUS_OWNER_ID 20
29 
main(void)30 int main(void)
31 {
32 	int rc;
33 	struct mctp *mctp_ctx;
34 
35 	LOG_INF("MCTP Host EID:%d on %s\n", mctp_i2c_ctrl.endpoint_id, CONFIG_BOARD_TARGET);
36 
37 	mctp_ctx = mctp_init();
38 	__ASSERT_NO_MSG(mctp_ctx != NULL);
39 	mctp_register_bus(mctp_ctx, &mctp_i2c_ctrl.binding, mctp_i2c_ctrl.endpoint_id);
40 	mctp_set_rx_all(mctp_ctx, rx_message, NULL);
41 
42 	/*
43 	 * 1. MCTP poll loop, send "ping" to each endpoint and get "pong" back
44 	 * 2. Then send a broadcast "hello" to each endpoint and get a "from endoint %d" back
45 	 */
46 	while (true) {
47 		rc = mctp_message_tx(mctp_ctx, BUS_OWNER_ID, false,
48 				0, "ping", sizeof("ping"));
49 		if (rc != 0) {
50 			LOG_WRN("Failed to send message \"ping\", errno %d\n", rc);
51 		}
52 		k_msleep(500);
53 	}
54 
55 	return 0;
56 }
57