1 /*
2  * Copyright (c) 2024 Basalte bv
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_SHELL_RPMSG_H_
8 #define ZEPHYR_INCLUDE_SHELL_RPMSG_H_
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/shell/shell.h>
12 #include <openamp/rpmsg.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 extern const struct shell_transport_api shell_rpmsg_transport_api;
19 
20 /** RPMsg received message placeholder */
21 struct shell_rpmsg_rx {
22 	/** Pointer to the data held by RPMsg endpoint */
23 	void *data;
24 	/** The length of the data */
25 	size_t len;
26 };
27 
28 /** RPMsg-based shell transport. */
29 struct shell_rpmsg {
30 	/** Handler function registered by shell. */
31 	shell_transport_handler_t shell_handler;
32 
33 	/** Context registered by shell. */
34 	void *shell_context;
35 
36 	/** Indicator if we are ready to read/write */
37 	bool ready;
38 
39 	/** Setting for blocking mode */
40 	bool blocking;
41 
42 	/** RPMsg endpoint */
43 	struct rpmsg_endpoint ept;
44 
45 	/** Queue for received data. */
46 	struct k_msgq rx_q;
47 
48 	/** Buffer for received messages */
49 	struct shell_rpmsg_rx rx_buf[CONFIG_SHELL_RPMSG_MAX_RX];
50 
51 	/** The current rx message */
52 	struct shell_rpmsg_rx rx_cur;
53 
54 	/** The number of bytes consumed from rx_cur */
55 	size_t rx_consumed;
56 };
57 
58 #define SHELL_RPMSG_DEFINE(_name)					\
59 	static struct shell_rpmsg _name##_shell_rpmsg;			\
60 	struct shell_transport _name = {				\
61 		.api = &shell_rpmsg_transport_api,			\
62 		.ctx = (struct shell_rpmsg *)&_name##_shell_rpmsg,	\
63 	}
64 
65 /**
66  * @brief Initialize the Shell backend using the provided @p rpmsg_dev device.
67  *
68  * @param rpmsg_dev A pointer to an RPMsg device
69  * @return 0 on success or a negative value on error
70  */
71 int shell_backend_rpmsg_init_transport(struct rpmsg_device *rpmsg_dev);
72 
73 /**
74  * @brief This function provides pointer to shell RPMsg backend instance.
75  *
76  * Function returns pointer to the shell RPMsg instance. This instance can be
77  * next used with shell_execute_cmd function in order to test commands behavior.
78  *
79  * @returns Pointer to the shell instance.
80  */
81 const struct shell *shell_backend_rpmsg_get_ptr(void);
82 
83 #ifdef __cplusplus
84 }
85 #endif
86 
87 #endif /* ZEPHYR_INCLUDE_SHELL_RPMSG_H_ */
88