|
8 | 8 | #include <zephyr/logging/log.h> |
9 | 9 | #include <zephyr/net/mcp/mcp_server.h> |
10 | 10 |
|
| 11 | +LOG_MODULE_REGISTER(mcp_server, CONFIG_MCP_LOG_LEVEL); |
| 12 | + |
| 13 | +/* Configuration defaults */ |
| 14 | + |
| 15 | +#define MCP_REQUEST_WORKERS 2 |
| 16 | +#define MCP_MESSAGE_WORKERS 2 |
| 17 | +#define MCP_REQUEST_QUEUE_SIZE 10 |
| 18 | +#define MCP_MESSAGE_QUEUE_SIZE 10 |
| 19 | +#define MCP_WORKER_PRIORITY 7 |
| 20 | + |
| 21 | +/* Message structures */ |
| 22 | +struct mcp_request_msg { |
| 23 | + uint32_t request_id; |
| 24 | + /* More fields will be added later */ |
| 25 | +}; |
| 26 | + |
| 27 | +struct mcp_message_msg { |
| 28 | + uint32_t token; |
| 29 | + /* More fields will be added later */ |
| 30 | +}; |
| 31 | + |
| 32 | +/* Message queues */ |
| 33 | +K_MSGQ_DEFINE(mcp_request_queue, sizeof(struct mcp_request_msg), MCP_REQUEST_QUEUE_SIZE, 4); |
| 34 | +K_MSGQ_DEFINE(mcp_message_queue, sizeof(struct mcp_message_msg), MCP_MESSAGE_QUEUE_SIZE, 4); |
| 35 | + |
| 36 | +/* Worker thread stacks */ |
| 37 | +K_THREAD_STACK_ARRAY_DEFINE(mcp_request_worker_stacks, MCP_REQUEST_WORKERS, 2048); |
| 38 | +K_THREAD_STACK_ARRAY_DEFINE(mcp_message_worker_stacks, MCP_MESSAGE_WORKERS, 2048); |
| 39 | + |
| 40 | +/* Worker thread structures */ |
| 41 | +static struct k_thread mcp_request_workers[MCP_REQUEST_WORKERS]; |
| 42 | +static struct k_thread mcp_message_workers[MCP_MESSAGE_WORKERS]; |
| 43 | + |
| 44 | +/* Request worker function */ |
| 45 | +static void mcp_request_worker(void *arg1, void *arg2, void *arg3) |
| 46 | +{ |
| 47 | + struct mcp_request_msg msg; |
| 48 | + int worker_id = POINTER_TO_INT(arg1); |
| 49 | + |
| 50 | + LOG_INF("Request worker %d started", worker_id); |
| 51 | + |
| 52 | + while (1) { |
| 53 | + if (k_msgq_get(&mcp_request_queue, &msg, K_FOREVER) == 0) { |
| 54 | + LOG_DBG("Processing request (worker %d)", worker_id); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/* Message worker function */ |
| 60 | +static void mcp_message_worker(void *arg1, void *arg2, void *arg3) |
| 61 | +{ |
| 62 | + struct mcp_message_msg msg; |
| 63 | + int worker_id = POINTER_TO_INT(arg1); |
| 64 | + |
| 65 | + LOG_INF("Message worker %d started", worker_id); |
| 66 | + |
| 67 | + while (1) { |
| 68 | + if (k_msgq_get(&mcp_message_queue, &msg, K_FOREVER) == 0) { |
| 69 | + LOG_DBG("Processing message (worker %d)", worker_id); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
11 | 74 | int mcp_server_init(void) |
12 | 75 | { |
13 | | - printk("Hello World from MCP Server Core\n\r"); |
| 76 | + LOG_INF("Initializing MCP Server Core"); |
| 77 | + |
| 78 | + /* Initialize request worker threads */ |
| 79 | + for (int i = 0; i < MCP_REQUEST_WORKERS; i++) { |
| 80 | + k_thread_create(&mcp_request_workers[i], mcp_request_worker_stacks[i], |
| 81 | + K_THREAD_STACK_SIZEOF(mcp_request_worker_stacks[i]), |
| 82 | + mcp_request_worker, INT_TO_POINTER(i), NULL, NULL, |
| 83 | + K_PRIO_COOP(MCP_WORKER_PRIORITY), 0, K_NO_WAIT); |
| 84 | + |
| 85 | + k_thread_name_set(&mcp_request_workers[i], "mcp_req_worker"); |
| 86 | + } |
| 87 | + |
| 88 | + /* Initialize message worker threads */ |
| 89 | + for (int i = 0; i < MCP_MESSAGE_WORKERS; i++) { |
| 90 | + k_thread_create(&mcp_message_workers[i], mcp_message_worker_stacks[i], |
| 91 | + K_THREAD_STACK_SIZEOF(mcp_message_worker_stacks[i]), |
| 92 | + mcp_message_worker, INT_TO_POINTER(i), NULL, NULL, |
| 93 | + K_PRIO_COOP(MCP_WORKER_PRIORITY), 0, K_NO_WAIT); |
| 94 | + |
| 95 | + k_thread_name_set(&mcp_message_workers[i], "mcp_msg_worker"); |
| 96 | + } |
| 97 | + |
| 98 | + LOG_INF("MCP Server Core initialized: %d request workers, %d message workers", |
| 99 | + MCP_REQUEST_WORKERS, MCP_MESSAGE_WORKERS); |
| 100 | + |
14 | 101 | return 0; |
15 | 102 | } |
0 commit comments