forked from nrfconnect/sdk-zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsip_svc_shell.c
385 lines (329 loc) · 9.85 KB
/
sip_svc_shell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* Copyright (c) 2022-2023, Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*
* ARM SiP service shell command 'sip_svc'.
*/
#include <zephyr/shell/shell.h>
#include <zephyr/sip_svc/sip_svc.h>
#include <zephyr/sip_svc/sip_svc_controller.h>
#include <stdlib.h>
#include <errno.h>
#define MAX_TIMEOUT_MSECS (1 * 1000UL)
struct private_data {
struct k_sem semaphore;
const struct shell *sh;
};
static int parse_common_args(const struct shell *sh, char **argv, void **ctrl)
{
*ctrl = sip_svc_get_controller(argv[1]);
if (!*ctrl) {
shell_error(sh, "service %s not found", argv[1]);
return -ENODEV;
}
struct sip_svc_controller *ct = (struct sip_svc_controller *)(*ctrl);
if (!ct->init) {
shell_error(sh, "ARM SiP services method %s not initialized", argv[1]);
return -ENODEV;
}
return 0;
}
static int cmd_reg(const struct shell *sh, size_t argc, char **argv)
{
struct sip_svc_controller *ctrl;
uint32_t c_token;
int err;
err = parse_common_args(sh, argv, (void **)&ctrl);
if (err < 0) {
return err;
}
c_token = sip_svc_register(ctrl, NULL);
if (c_token == SIP_SVC_ID_INVALID) {
shell_error(sh, "%s: register fail", ctrl->method);
err = -1;
} else {
shell_print(sh, "%s: register success: client token %08x\n", ctrl->method, c_token);
err = 0;
}
return err;
}
static int cmd_unreg(const struct shell *sh, size_t argc, char **argv)
{
struct sip_svc_controller *ctrl;
uint32_t c_token;
int err;
char *endptr;
err = parse_common_args(sh, argv, (void **)&ctrl);
if (err < 0) {
return err;
}
errno = 0;
c_token = strtoul(argv[2], &endptr, 16);
if (errno == ERANGE) {
shell_error(sh, "Out of range value");
return -ERANGE;
} else if (errno || endptr == argv[2] || *endptr) {
shell_error(sh, "Invalid argument");
return -errno;
}
err = sip_svc_unregister(ctrl, (uint32_t)c_token);
if (err) {
shell_error(sh, "%s: unregister fail (%d): client token %08x", ctrl->method, err,
(uint32_t)c_token);
} else {
shell_print(sh, "%s: unregister success: client token %08x", ctrl->method,
(uint32_t)c_token);
}
return err;
}
static int cmd_open(const struct shell *sh, size_t argc, char **argv)
{
struct sip_svc_controller *ctrl;
uint32_t c_token;
unsigned long mseconds = 0;
int err;
char *endptr;
k_timeout_t timeout = K_MSEC(MAX_TIMEOUT_MSECS);
err = parse_common_args(sh, argv, (void **)&ctrl);
if (err < 0) {
return err;
}
errno = 0;
c_token = strtoul(argv[2], &endptr, 16);
if (errno == ERANGE) {
shell_error(sh, "Out of range value");
return -ERANGE;
} else if (errno || endptr == argv[2] || *endptr) {
shell_error(sh, "Invalid argument");
return -errno;
}
if (argc > 3) {
errno = 0;
mseconds = strtoul(argv[3], &endptr, 10);
if (errno == ERANGE) {
shell_error(sh, "Out of range value");
return -ERANGE;
} else if (errno || endptr == argv[3] || *endptr) {
shell_error(sh, "Invalid Argument");
return -EINVAL;
} else if (mseconds <= MAX_TIMEOUT_MSECS) {
timeout = K_MSEC(mseconds);
} else {
timeout = K_MSEC(MAX_TIMEOUT_MSECS);
shell_error(sh, "Setting timeout value to %lu milliseconds",
MAX_TIMEOUT_MSECS);
}
}
err = sip_svc_open(ctrl, (uint32_t)c_token, timeout);
if (err) {
shell_error(sh, "%s: open fail (%d): client token %08x", ctrl->method, err,
(uint32_t)c_token);
} else {
shell_print(sh, "%s: open success: client token %08x", ctrl->method,
(uint32_t)c_token);
}
return err;
}
static int cmd_close(const struct shell *sh, size_t argc, char **argv)
{
struct sip_svc_controller *ctrl;
uint32_t c_token;
int err;
char *endptr;
err = parse_common_args(sh, argv, (void **)&ctrl);
if (err < 0) {
return err;
}
errno = 0;
c_token = strtoul(argv[2], &endptr, 16);
if (errno == ERANGE) {
shell_error(sh, "Out of range value");
return -ERANGE;
} else if (errno || endptr == argv[2] || *endptr) {
shell_error(sh, "Invalid argument");
return -errno;
}
err = sip_svc_close(ctrl, (uint32_t)c_token, NULL);
if (err) {
shell_error(sh, "%s: close fail (%d): client token %08x", ctrl->method, err,
(uint32_t)c_token);
} else {
shell_print(sh, "%s: close success: client token %08x", ctrl->method,
(uint32_t)c_token);
}
return err;
}
static void cmd_send_callback(uint32_t c_token, struct sip_svc_response *response)
{
if (response == NULL) {
return;
}
struct private_data *priv = (struct private_data *)response->priv_data;
const struct shell *sh = priv->sh;
shell_print(sh, "\n\rsip_svc send callback response\n");
shell_print(sh, "\theader=%08x\n", response->header);
shell_print(sh, "\ta0=%016lx\n", response->a0);
shell_print(sh, "\ta1=%016lx\n", response->a1);
shell_print(sh, "\ta2=%016lx\n", response->a2);
shell_print(sh, "\ta3=%016lx\n", response->a3);
k_sem_give(&(priv->semaphore));
}
static int cmd_send(const struct shell *sh, size_t argc, char **argv)
{
struct sip_svc_controller *ctrl;
uint32_t c_token;
int trans_id;
struct sip_svc_request request;
struct private_data priv;
char *endptr;
int err;
err = parse_common_args(sh, argv, (void **)&ctrl);
if (err < 0) {
return err;
}
errno = 0;
c_token = strtoul(argv[2], &endptr, 16);
if (errno == ERANGE) {
shell_error(sh, "Out of range value");
return -ERANGE;
} else if (errno || endptr == argv[2] || *endptr) {
shell_error(sh, "Invalid argument");
return -errno;
}
request.header = SIP_SVC_PROTO_HEADER(SIP_SVC_PROTO_CMD_SYNC, 0);
request.a0 = strtoul(argv[3], &endptr, 16);
if (errno == ERANGE) {
shell_error(sh, "Out of range value for a0");
return -ERANGE;
} else if (errno || endptr == argv[3] || *endptr) {
shell_error(sh, "Invalid argument for a0");
return -errno;
}
if (argc > 4) {
request.a1 = strtoul(argv[4], &endptr, 16);
if (errno == ERANGE) {
shell_error(sh, "Out of range value for a1");
return -ERANGE;
} else if (errno || endptr == argv[4] || *endptr) {
shell_error(sh, "Invalid argument for a1");
return -errno;
}
}
if (argc > 5) {
request.a2 = strtoul(argv[5], &endptr, 16);
if (errno == ERANGE) {
shell_error(sh, "Out of range value for a2");
return -ERANGE;
} else if (errno || endptr == argv[5] || *endptr) {
shell_error(sh, "Invalid argument for a2");
return -errno;
}
}
if (argc > 6) {
request.a3 = strtoul(argv[6], &endptr, 16);
if (errno == ERANGE) {
shell_error(sh, "Out of range value for a3");
return -ERANGE;
} else if (errno || endptr == argv[6] || *endptr) {
shell_error(sh, "Invalid argument for a3");
return -errno;
}
}
if (argc > 7) {
request.a4 = strtoul(argv[7], &endptr, 16);
if (errno == ERANGE) {
shell_error(sh, "Out of range value for a4");
return -ERANGE;
} else if (errno || endptr == argv[7] || *endptr) {
shell_error(sh, "Invalid argument for a4");
return -errno;
}
}
if (argc > 8) {
request.a5 = strtoul(argv[8], &endptr, 16);
if (errno == ERANGE) {
shell_error(sh, "Out of range value for a5");
return -ERANGE;
} else if (errno || endptr == argv[8] || *endptr) {
shell_error(sh, "Invalid argument for a5");
return -errno;
}
}
if (argc > 9) {
request.a6 = strtoul(argv[9], &endptr, 16);
if (errno == ERANGE) {
shell_error(sh, "Out of range value for a6");
return -ERANGE;
} else if (errno || endptr == argv[9] || *endptr) {
shell_error(sh, "Invalid argument for a6");
return -errno;
}
}
if (argc > 10) {
request.a7 = strtoul(argv[10], &endptr, 16);
if (errno == ERANGE) {
shell_error(sh, "Out of range value for a7");
return -ERANGE;
} else if (errno || endptr == argv[10] || *endptr) {
shell_error(sh, "Invalid argument for a7");
return -errno;
}
}
k_sem_init(&(priv.semaphore), 0, 1);
priv.sh = sh;
request.resp_data_addr = 0;
request.resp_data_size = 0;
request.priv_data = (void *)&priv;
trans_id = sip_svc_send(ctrl, (uint32_t)c_token, &request, cmd_send_callback);
if (trans_id < 0) {
shell_error(sh, "%s: send fail: client token %08x", ctrl->method,
(uint32_t)c_token);
err = trans_id;
} else {
/*wait for callback*/
k_sem_take(&(priv.semaphore), K_FOREVER);
shell_print(sh, "%s: send success: client token %08x, trans_id %d", ctrl->method,
(uint32_t)c_token, trans_id);
err = 0;
}
return err;
}
static int cmd_info(const struct shell *sh, size_t argc, char **argv)
{
struct sip_svc_controller *ctrl;
int err;
uint32_t i;
static const char *const state_str_list[] = {"INVALID", "IDLE", "OPEN", "ABORT"};
const char *state_str = "UNKNOWN";
err = parse_common_args(sh, argv, (void **)&ctrl);
if (err < 0) {
return err;
}
shell_print(sh, "---------------------------------------\n");
shell_print(sh, "sip_svc service information\n");
shell_print(sh, "---------------------------------------\n");
shell_print(sh, "active job cnt %d\n", ctrl->active_job_cnt);
shell_print(sh, "active async job cnt %d\n", ctrl->active_async_job_cnt);
shell_print(sh, "---------------------------------------\n");
shell_print(sh, "Client Token\tState\tTrans Cnt\n");
shell_print(sh, "---------------------------------------\n");
for (i = 0; i < ctrl->num_clients; i++) {
if (ctrl->clients[i].id != SIP_SVC_ID_INVALID) {
if (ctrl->clients[i].state <= SIP_SVC_CLIENT_ST_ABORT) {
state_str = state_str_list[ctrl->clients[i].state];
}
shell_print(sh, "%08x \t%-10s\t%-9d\n", ctrl->clients[i].token,
state_str, ctrl->clients[i].active_trans_cnt);
}
}
return err;
}
SHELL_STATIC_SUBCMD_SET_CREATE(
sub_sip_svc, SHELL_CMD_ARG(reg, NULL, "<method>", cmd_reg, 2, 0),
SHELL_CMD_ARG(unreg, NULL, "<method> <token>", cmd_unreg, 3, 0),
SHELL_CMD_ARG(open, NULL, "<method> <token> <[timeout_msec]>", cmd_open, 3, 1),
SHELL_CMD_ARG(close, NULL, "<method> <token>", cmd_close, 3, 0),
SHELL_CMD_ARG(send, NULL, "<method> <token> <a0> [<a1> <a2> ... <a7>]", cmd_send, 4, 7),
SHELL_CMD_ARG(info, NULL, "<method>", cmd_info, 2, 0), SHELL_SUBCMD_SET_END);
SHELL_CMD_REGISTER(sip_svc, &sub_sip_svc, "ARM SiP services commands", NULL);