1 /*
2  * Copyright (c) 2024 Trackunit Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/modem/pipe.h>
8 #include <zephyr/devicetree.h>
9 #include <zephyr/sys/util.h>
10 
11 #ifndef ZEPHYR_MODEM_PIPELINK_
12 #define ZEPHYR_MODEM_PIPELINK_
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /**
19  * @brief Modem pipelink
20  * @defgroup modem_pipelink Modem pipelink
21  * @ingroup modem
22  * @{
23  */
24 
25 /** Pipelink event */
26 enum modem_pipelink_event {
27 	/** Modem pipe has been connected and can be opened */
28 	MODEM_PIPELINK_EVENT_CONNECTED = 0,
29 	/** Modem pipe has been disconnected and can't be opened */
30 	MODEM_PIPELINK_EVENT_DISCONNECTED,
31 };
32 
33 /** @cond INTERNAL_HIDDEN */
34 
35 /** Forward declaration */
36 struct modem_pipelink;
37 
38 /** @endcond */
39 
40 /**
41  * @brief Pipelink callback definition
42  * @param link Modem pipelink instance
43  * @param event Modem pipelink event
44  * @param user_data User data passed to modem_pipelink_attach()
45  */
46 typedef void (*modem_pipelink_callback)(struct modem_pipelink *link,
47 					enum modem_pipelink_event event,
48 					void *user_data);
49 
50 /** @cond INTERNAL_HIDDEN */
51 
52 /** Pipelink structure */
53 struct modem_pipelink {
54 	struct modem_pipe *pipe;
55 	modem_pipelink_callback callback;
56 	void *user_data;
57 	bool connected;
58 	struct k_spinlock spinlock;
59 };
60 
61 /** @endcond */
62 
63 /**
64  * @brief Attach callback to pipelink
65  * @param link Pipelink instance
66  * @param callback Pipelink callback
67  * @param user_data User data passed to pipelink callback
68  */
69 void modem_pipelink_attach(struct modem_pipelink *link,
70 			   modem_pipelink_callback callback,
71 			   void *user_data);
72 
73 /**
74  * @brief Check whether pipelink pipe is connected
75  * @param link Pipelink instance
76  * @retval true if pipe is connected
77  * @retval false if pipe is not connected
78  */
79 bool modem_pipelink_is_connected(struct modem_pipelink *link);
80 
81 /**
82  * @brief Get pipe from pipelink
83  * @param link Pipelink instance
84  * @retval Pointer to pipe if pipelink has been initialized
85  * @retval NULL if pipelink has not been initialized
86  */
87 struct modem_pipe *modem_pipelink_get_pipe(struct modem_pipelink *link);
88 
89 /**
90  * @brief Clear callback
91  * @param link Pipelink instance
92  */
93 void modem_pipelink_release(struct modem_pipelink *link);
94 
95 /** @cond INTERNAL_HIDDEN */
96 
97 /** Initialize modem pipelink */
98 void modem_pipelink_init(struct modem_pipelink *link, struct modem_pipe *pipe);
99 
100 /** Notify user of pipelink that pipe has been connected */
101 void modem_pipelink_notify_connected(struct modem_pipelink *link);
102 
103 /** Notify user of pipelink that pipe has been disconnected */
104 void modem_pipelink_notify_disconnected(struct modem_pipelink *link);
105 
106 /** @endcond */
107 
108 /** @cond INTERNAL_HIDDEN */
109 
110 /**
111  * @brief Synthesize pipelink symbol from devicetree node identifier and name
112  * @param node_id Devicetree node identifier
113  * @param name Pipelink name
114  */
115 #define MODEM_PIPELINK_DT_SYM(node_id, name) \
116 	_CONCAT_4(__modem_pipelink_, DT_DEP_ORD(node_id), _, name)
117 
118 /** @endcond */
119 
120 /**
121  * @brief Declare pipelink from devicetree node identifier and name
122  * @param node_id Devicetree node identifier
123  * @param name Pipelink name
124  */
125 #define MODEM_PIPELINK_DT_DECLARE(node_id, name) \
126 	extern struct modem_pipelink MODEM_PIPELINK_DT_SYM(node_id, name)
127 
128 /**
129  * @brief Define pipelink from devicetree node identifier and name
130  * @param node_id Devicetree node identifier
131  * @param name Pipelink name
132  */
133 #define MODEM_PIPELINK_DT_DEFINE(node_id, name) \
134 	struct modem_pipelink MODEM_PIPELINK_DT_SYM(node_id, name)
135 
136 /**
137  * @brief Get pointer to pipelink from devicetree node identifier and name
138  * @param node_id Devicetree node identifier
139  * @param name Pipelink name
140  */
141 #define MODEM_PIPELINK_DT_GET(node_id, name) \
142 	(&MODEM_PIPELINK_DT_SYM(node_id, name))
143 
144 /**
145  * @brief Device driver instance variants of MODEM_PIPELINK_DT macros
146  * @name MODEM_PIPELINK_DT_INST macros
147  * @anchor MODEM_PIPELINK_DT_INST
148  * @{
149  */
150 
151 #define MODEM_PIPELINK_DT_INST_DECLARE(inst, name) \
152 	MODEM_PIPELINK_DT_DECLARE(DT_DRV_INST(inst), name)
153 
154 #define MODEM_PIPELINK_DT_INST_DEFINE(inst, name) \
155 	MODEM_PIPELINK_DT_DEFINE(DT_DRV_INST(inst), name)
156 
157 #define MODEM_PIPELINK_DT_INST_GET(inst, name) \
158 	MODEM_PIPELINK_DT_GET(DT_DRV_INST(inst), name)
159 
160 /** @} */
161 
162 /** @} */
163 
164 #ifdef __cplusplus
165 }
166 #endif
167 
168 #endif /* ZEPHYR_MODEM_PIPELINK_ */
169