6
6
7
7
#include <zephyr/kernel.h>
8
8
#include <zephyr/shell/shell.h>
9
+ #include <zephyr/modem/at/user_pipe.h>
9
10
#include <zephyr/modem/chat.h>
10
11
#include <zephyr/modem/pipelink.h>
11
12
#include <zephyr/sys/atomic.h>
12
13
13
14
#include <zephyr/logging/log.h>
14
15
LOG_MODULE_REGISTER (modem_at_shell , CONFIG_MODEM_LOG_LEVEL );
15
16
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
-
27
17
static struct modem_chat at_shell_chat ;
28
18
static uint8_t at_shell_chat_receive_buf [CONFIG_MODEM_AT_SHELL_CHAT_RECEIVE_BUF_SIZE ];
29
19
static uint8_t * at_shell_chat_argv_buf [2 ];
@@ -32,10 +22,6 @@ static struct modem_chat_script_chat at_shell_script_chat[1];
32
22
static struct modem_chat_match at_shell_script_chat_matches [2 ];
33
23
static uint8_t at_shell_match_buf [CONFIG_MODEM_AT_SHELL_RESPONSE_MAX_SIZE ];
34
24
static 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 ;
39
25
40
26
static void at_shell_print_any_match (struct modem_chat * chat , char * * argv , uint16_t argc ,
41
27
void * user_data )
@@ -74,7 +60,7 @@ static void at_shell_script_callback(struct modem_chat *chat,
74
60
enum modem_chat_script_result result ,
75
61
void * user_data )
76
62
{
77
- atomic_clear_bit ( & at_shell_state , AT_SHELL_STATE_SCRIPT_RUNNING_BIT );
63
+ modem_at_user_pipe_release ( );
78
64
}
79
65
80
66
MODEM_CHAT_SCRIPT_DEFINE (
@@ -85,83 +71,6 @@ MODEM_CHAT_SCRIPT_DEFINE(
85
71
CONFIG_MODEM_AT_SHELL_RESPONSE_TIMEOUT_S
86
72
);
87
73
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
-
165
74
static void at_shell_init_chat (void )
166
75
{
167
76
const struct modem_chat_config at_shell_chat_config = {
@@ -204,17 +113,11 @@ static void at_shell_init_script_chat(void)
204
113
CONFIG_MODEM_AT_SHELL_RESPONSE_TIMEOUT_S );
205
114
}
206
115
207
- static void at_shell_init_pipelink (void )
208
- {
209
- modem_pipelink_attach (at_shell_pipelink , at_shell_pipelink_callback , NULL );
210
- }
211
-
212
116
static int at_shell_init (void )
213
117
{
214
- at_shell_init_work ();
215
118
at_shell_init_chat ();
216
119
at_shell_init_script_chat ();
217
- at_shell_init_pipelink ( );
120
+ modem_at_user_pipe_init ( & at_shell_chat );
218
121
return 0 ;
219
122
}
220
123
@@ -228,14 +131,19 @@ static int at_shell_cmd_handler(const struct shell *sh, size_t argc, char **argv
228
131
return - EINVAL ;
229
132
}
230
133
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 ;
239
147
}
240
148
241
149
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
260
168
ret = modem_chat_run_script_async (& at_shell_chat , & at_shell_script );
261
169
if (ret < 0 ) {
262
170
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 ( );
264
172
}
265
173
266
174
return ret ;
0 commit comments