66
77#include <zephyr/kernel.h>
88#include <zephyr/shell/shell.h>
9+ #include <zephyr/modem/at/user_pipe.h>
910#include <zephyr/modem/chat.h>
1011#include <zephyr/modem/pipelink.h>
1112#include <zephyr/sys/atomic.h>
1213
1314#include <zephyr/logging/log.h>
1415LOG_MODULE_REGISTER (modem_at_shell , CONFIG_MODEM_LOG_LEVEL );
1516
16- #define AT_SHELL_MODEM_NODE DT_ALIAS(modem)
17- #define AT_SHELL_PIPELINK_NAME _CONCAT(user_pipe_, CONFIG_MODEM_AT_SHELL_USER_PIPE)
18-
19- #define AT_SHELL_STATE_ATTACHED_BIT 0
20- #define AT_SHELL_STATE_SCRIPT_RUNNING_BIT 1
21-
22- MODEM_PIPELINK_DT_DECLARE (AT_SHELL_MODEM_NODE , AT_SHELL_PIPELINK_NAME );
23-
24- static struct modem_pipelink * at_shell_pipelink =
25- MODEM_PIPELINK_DT_GET (AT_SHELL_MODEM_NODE , AT_SHELL_PIPELINK_NAME );
26-
2717static struct modem_chat at_shell_chat ;
2818static uint8_t at_shell_chat_receive_buf [CONFIG_MODEM_AT_SHELL_CHAT_RECEIVE_BUF_SIZE ];
2919static uint8_t * at_shell_chat_argv_buf [2 ];
@@ -32,10 +22,6 @@ static struct modem_chat_script_chat at_shell_script_chat[1];
3222static struct modem_chat_match at_shell_script_chat_matches [2 ];
3323static uint8_t at_shell_match_buf [CONFIG_MODEM_AT_SHELL_RESPONSE_MAX_SIZE ];
3424static const struct shell * at_shell_active_shell ;
35- static struct k_work at_shell_open_pipe_work ;
36- static struct k_work at_shell_attach_chat_work ;
37- static struct k_work at_shell_release_chat_work ;
38- static atomic_t at_shell_state ;
3925
4026static void at_shell_print_any_match (struct modem_chat * chat , char * * argv , uint16_t argc ,
4127 void * user_data )
@@ -74,7 +60,7 @@ static void at_shell_script_callback(struct modem_chat *chat,
7460 enum modem_chat_script_result result ,
7561 void * user_data )
7662{
77- atomic_clear_bit ( & at_shell_state , AT_SHELL_STATE_SCRIPT_RUNNING_BIT );
63+ modem_at_user_pipe_release ( );
7864}
7965
8066MODEM_CHAT_SCRIPT_DEFINE (
@@ -85,83 +71,6 @@ MODEM_CHAT_SCRIPT_DEFINE(
8571 CONFIG_MODEM_AT_SHELL_RESPONSE_TIMEOUT_S
8672);
8773
88- static void at_shell_pipe_callback (struct modem_pipe * pipe ,
89- enum modem_pipe_event event ,
90- void * user_data )
91- {
92- ARG_UNUSED (user_data );
93-
94- switch (event ) {
95- case MODEM_PIPE_EVENT_OPENED :
96- LOG_INF ("pipe opened" );
97- k_work_submit (& at_shell_attach_chat_work );
98- break ;
99-
100- default :
101- break ;
102- }
103- }
104-
105- void at_shell_pipelink_callback (struct modem_pipelink * link ,
106- enum modem_pipelink_event event ,
107- void * user_data )
108- {
109- ARG_UNUSED (user_data );
110-
111- switch (event ) {
112- case MODEM_PIPELINK_EVENT_CONNECTED :
113- LOG_INF ("pipe connected" );
114- k_work_submit (& at_shell_open_pipe_work );
115- break ;
116-
117- case MODEM_PIPELINK_EVENT_DISCONNECTED :
118- LOG_INF ("pipe disconnected" );
119- k_work_submit (& at_shell_release_chat_work );
120- break ;
121-
122- default :
123- break ;
124- }
125- }
126-
127- static void at_shell_open_pipe_handler (struct k_work * work )
128- {
129- ARG_UNUSED (work );
130-
131- LOG_INF ("opening pipe" );
132-
133- modem_pipe_attach (modem_pipelink_get_pipe (at_shell_pipelink ),
134- at_shell_pipe_callback ,
135- NULL );
136-
137- modem_pipe_open_async (modem_pipelink_get_pipe (at_shell_pipelink ));
138- }
139-
140- static void at_shell_attach_chat_handler (struct k_work * work )
141- {
142- ARG_UNUSED (work );
143-
144- modem_chat_attach (& at_shell_chat , modem_pipelink_get_pipe (at_shell_pipelink ));
145- atomic_set_bit (& at_shell_state , AT_SHELL_STATE_ATTACHED_BIT );
146- LOG_INF ("chat attached" );
147- }
148-
149- static void at_shell_release_chat_handler (struct k_work * work )
150- {
151- ARG_UNUSED (work );
152-
153- modem_chat_release (& at_shell_chat );
154- atomic_clear_bit (& at_shell_state , AT_SHELL_STATE_ATTACHED_BIT );
155- LOG_INF ("chat released" );
156- }
157-
158- static void at_shell_init_work (void )
159- {
160- k_work_init (& at_shell_open_pipe_work , at_shell_open_pipe_handler );
161- k_work_init (& at_shell_attach_chat_work , at_shell_attach_chat_handler );
162- k_work_init (& at_shell_release_chat_work , at_shell_release_chat_handler );
163- }
164-
16574static void at_shell_init_chat (void )
16675{
16776 const struct modem_chat_config at_shell_chat_config = {
@@ -204,17 +113,11 @@ static void at_shell_init_script_chat(void)
204113 CONFIG_MODEM_AT_SHELL_RESPONSE_TIMEOUT_S );
205114}
206115
207- static void at_shell_init_pipelink (void )
208- {
209- modem_pipelink_attach (at_shell_pipelink , at_shell_pipelink_callback , NULL );
210- }
211-
212116static int at_shell_init (void )
213117{
214- at_shell_init_work ();
215118 at_shell_init_chat ();
216119 at_shell_init_script_chat ();
217- at_shell_init_pipelink ( );
120+ modem_at_user_pipe_init ( & at_shell_chat );
218121 return 0 ;
219122}
220123
@@ -228,14 +131,19 @@ static int at_shell_cmd_handler(const struct shell *sh, size_t argc, char **argv
228131 return - EINVAL ;
229132 }
230133
231- if (!atomic_test_bit (& at_shell_state , AT_SHELL_STATE_ATTACHED_BIT )) {
232- shell_error (sh , "modem is not ready" );
233- return - EPERM ;
234- }
235-
236- if (atomic_test_and_set_bit (& at_shell_state , AT_SHELL_STATE_SCRIPT_RUNNING_BIT )) {
237- shell_error (sh , "script is already running" );
238- return - EBUSY ;
134+ ret = modem_at_user_pipe_claim ();
135+ if (ret < 0 ) {
136+ switch (ret ) {
137+ case - EPERM :
138+ shell_error (sh , "modem is not ready" );
139+ break ;
140+ case - EBUSY :
141+ shell_error (sh , "script is already running" );
142+ break ;
143+ default :
144+ shell_error (sh , "unknown" );
145+ }
146+ return ret ;
239147 }
240148
241149 strncpy (at_shell_request_buf , argv [1 ], sizeof (at_shell_request_buf ) - 1 );
@@ -260,7 +168,7 @@ static int at_shell_cmd_handler(const struct shell *sh, size_t argc, char **argv
260168 ret = modem_chat_run_script_async (& at_shell_chat , & at_shell_script );
261169 if (ret < 0 ) {
262170 shell_error (sh , "failed to start script" );
263- atomic_clear_bit ( & at_shell_state , AT_SHELL_STATE_SCRIPT_RUNNING_BIT );
171+ modem_at_user_pipe_release ( );
264172 }
265173
266174 return ret ;
0 commit comments