blob: 9d727a58206e22fdad4124a1fa9776dec9c3f109 [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $Id: pjsua_app_cli.c 4537 2013-06-19 06:47:43Z riza $ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "pjsua_app_common.h"
22
23#define THIS_FILE "pjsua_app_cli.c"
24
25#define CHECK_PJSUA_RUNNING() if (pjsua_get_state()!=PJSUA_STATE_RUNNING) \
26 return PJ_EINVALIDOP
27
28/* CLI command id */
29/* level 1 command */
30#define CMD_CALL 100
31#define CMD_PRESENCE 200
32#define CMD_ACCOUNT 300
33#define CMD_MEDIA 400
34#define CMD_CONFIG 500
35#define CMD_VIDEO 600
36#define CMD_SLEEP 700
37#define CMD_ECHO 800
38#define CMD_NETWORK 900
39#define CMD_QUIT 110
40#define CMD_RESTART 120
41
42/* call level 2 command */
43#define CMD_CALL_NEW ((CMD_CALL*10)+1)
44#define CMD_CALL_MULTI ((CMD_CALL*10)+2)
45#define CMD_CALL_ANSWER ((CMD_CALL*10)+3)
46#define CMD_CALL_HANGUP ((CMD_CALL*10)+4)
47#define CMD_CALL_HANGUP_ALL ((CMD_CALL*10)+5)
48#define CMD_CALL_HOLD ((CMD_CALL*10)+6)
49#define CMD_CALL_REINVITE ((CMD_CALL*10)+7)
50#define CMD_CALL_UPDATE ((CMD_CALL*10)+8)
51#define CMD_CALL_NEXT ((CMD_CALL*10)+9)
52#define CMD_CALL_PREVIOUS ((CMD_CALL*10)+10)
53#define CMD_CALL_TRANSFER ((CMD_CALL*10)+11)
54#define CMD_CALL_TRANSFER_REPLACE ((CMD_CALL*10)+12)
55#define CMD_CALL_REDIRECT ((CMD_CALL*10)+13)
56#define CMD_CALL_D2833 ((CMD_CALL*10)+14)
57#define CMD_CALL_INFO ((CMD_CALL*10)+15)
58#define CMD_CALL_DUMP_Q ((CMD_CALL*10)+16)
59#define CMD_CALL_SEND_ARB ((CMD_CALL*10)+17)
60#define CMD_CALL_LIST ((CMD_CALL*10)+18)
61
62/* im & presence level 2 command */
63#define CMD_PRESENCE_ADD_BUDDY ((CMD_PRESENCE*10)+1)
64#define CMD_PRESENCE_DEL_BUDDY ((CMD_PRESENCE*10)+2)
65#define CMD_PRESENCE_SEND_IM ((CMD_PRESENCE*10)+3)
66#define CMD_PRESENCE_SUB ((CMD_PRESENCE*10)+4)
67#define CMD_PRESENCE_UNSUB ((CMD_PRESENCE*10)+5)
68#define CMD_PRESENCE_TOG_STATE ((CMD_PRESENCE*10)+6)
69#define CMD_PRESENCE_TEXT ((CMD_PRESENCE*10)+7)
70#define CMD_PRESENCE_LIST ((CMD_PRESENCE*10)+8)
71
72/* account level 2 command */
73#define CMD_ACCOUNT_ADD ((CMD_ACCOUNT*10)+1)
74#define CMD_ACCOUNT_DEL ((CMD_ACCOUNT*10)+2)
75#define CMD_ACCOUNT_MOD ((CMD_ACCOUNT*10)+3)
76#define CMD_ACCOUNT_REG ((CMD_ACCOUNT*10)+4)
77#define CMD_ACCOUNT_UNREG ((CMD_ACCOUNT*10)+5)
78#define CMD_ACCOUNT_NEXT ((CMD_ACCOUNT*10)+6)
79#define CMD_ACCOUNT_PREV ((CMD_ACCOUNT*10)+7)
80#define CMD_ACCOUNT_SHOW ((CMD_ACCOUNT*10)+8)
81
82/* conference & media level 2 command */
83#define CMD_MEDIA_LIST ((CMD_MEDIA*10)+1)
84#define CMD_MEDIA_CONF_CONNECT ((CMD_MEDIA*10)+2)
85#define CMD_MEDIA_CONF_DISCONNECT ((CMD_MEDIA*10)+3)
86#define CMD_MEDIA_ADJUST_VOL ((CMD_MEDIA*10)+4)
87#define CMD_MEDIA_CODEC_PRIO ((CMD_MEDIA*10)+5)
88
89/* status & config level 2 command */
90#define CMD_CONFIG_DUMP_STAT ((CMD_CONFIG*10)+1)
91#define CMD_CONFIG_DUMP_DETAIL ((CMD_CONFIG*10)+2)
92#define CMD_CONFIG_DUMP_CONF ((CMD_CONFIG*10)+3)
93#define CMD_CONFIG_WRITE_SETTING ((CMD_CONFIG*10)+4)
94
95/* video level 2 command */
96#define CMD_VIDEO_ENABLE ((CMD_VIDEO*10)+1)
97#define CMD_VIDEO_DISABLE ((CMD_VIDEO*10)+2)
98#define CMD_VIDEO_ACC ((CMD_VIDEO*10)+3)
99#define CMD_VIDEO_CALL ((CMD_VIDEO*10)+4)
100#define CMD_VIDEO_DEVICE ((CMD_VIDEO*10)+5)
101#define CMD_VIDEO_CODEC ((CMD_VIDEO*10)+6)
102#define CMD_VIDEO_WIN ((CMD_VIDEO*10)+7)
103
104/* video level 3 command */
105#define CMD_VIDEO_ACC_SHOW ((CMD_VIDEO_ACC*10)+1)
106#define CMD_VIDEO_ACC_AUTORX ((CMD_VIDEO_ACC*10)+2)
107#define CMD_VIDEO_ACC_AUTOTX ((CMD_VIDEO_ACC*10)+3)
108#define CMD_VIDEO_ACC_CAP_ID ((CMD_VIDEO_ACC*10)+4)
109#define CMD_VIDEO_ACC_REN_ID ((CMD_VIDEO_ACC*10)+5)
110#define CMD_VIDEO_CALL_RX ((CMD_VIDEO_CALL*10)+1)
111#define CMD_VIDEO_CALL_TX ((CMD_VIDEO_CALL*10)+2)
112#define CMD_VIDEO_CALL_ADD ((CMD_VIDEO_CALL*10)+3)
113#define CMD_VIDEO_CALL_ENABLE ((CMD_VIDEO_CALL*10)+4)
114#define CMD_VIDEO_CALL_DISABLE ((CMD_VIDEO_CALL*10)+5)
115#define CMD_VIDEO_CALL_CAP ((CMD_VIDEO_CALL*10)+6)
116#define CMD_VIDEO_DEVICE_LIST ((CMD_VIDEO_DEVICE*10)+1)
117#define CMD_VIDEO_DEVICE_REFRESH ((CMD_VIDEO_DEVICE*10)+2)
118#define CMD_VIDEO_DEVICE_PREVIEW ((CMD_VIDEO_DEVICE*10)+3)
119#define CMD_VIDEO_CODEC_LIST ((CMD_VIDEO_CODEC*10)+1)
120#define CMD_VIDEO_CODEC_PRIO ((CMD_VIDEO_CODEC*10)+2)
121#define CMD_VIDEO_CODEC_FPS ((CMD_VIDEO_CODEC*10)+3)
122#define CMD_VIDEO_CODEC_BITRATE ((CMD_VIDEO_CODEC*10)+4)
123#define CMD_VIDEO_CODEC_SIZE ((CMD_VIDEO_CODEC*10)+5)
124#define CMD_VIDEO_WIN_LIST ((CMD_VIDEO_WIN*10)+1)
125#define CMD_VIDEO_WIN_ARRANGE ((CMD_VIDEO_WIN*10)+2)
126#define CMD_VIDEO_WIN_SHOW ((CMD_VIDEO_WIN*10)+3)
127#define CMD_VIDEO_WIN_HIDE ((CMD_VIDEO_WIN*10)+4)
128#define CMD_VIDEO_WIN_MOVE ((CMD_VIDEO_WIN*10)+5)
129#define CMD_VIDEO_WIN_RESIZE ((CMD_VIDEO_WIN*10)+6)
130
131/* dynamic choice argument list */
132#define DYN_CHOICE_START 9900
133#define DYN_CHOICE_BUDDY_ID (DYN_CHOICE_START)+1
134#define DYN_CHOICE_ACCOUNT_ID (DYN_CHOICE_START)+2
135#define DYN_CHOICE_MEDIA_PORT (DYN_CHOICE_START)+3
136#define DYN_CHOICE_AUDIO_CODEC_ID (DYN_CHOICE_START)+4
137#define DYN_CHOICE_CAP_DEV_ID (DYN_CHOICE_START)+5
138#define DYN_CHOICE_REN_DEV_ID (DYN_CHOICE_START)+6
139#define DYN_CHOICE_VID_DEV_ID (DYN_CHOICE_START)+7
140#define DYN_CHOICE_STREAM_ID (DYN_CHOICE_START)+8
141#define DYN_CHOICE_VIDEO_CODEC_ID (DYN_CHOICE_START)+9
142#define DYN_CHOICE_WIN_ID (DYN_CHOICE_START)+10
143#define DYN_CHOICE_CALL_ID (DYN_CHOICE_START)+11
144#define DYN_CHOICE_ADDED_BUDDY_ID (DYN_CHOICE_START)+12
145
146static pj_bool_t pj_inited = PJ_FALSE;
147static pj_caching_pool cli_cp;
148static pj_bool_t cli_cp_inited = PJ_FALSE;
149static pj_cli_t *cli = NULL;
150static pj_cli_sess *cli_cons_sess = NULL;
151static pj_cli_front_end *telnet_front_end = NULL;
152
153/** Forward declaration **/
154pj_status_t cli_setup_command(pj_cli_t *cli);
155void cli_destroy();
156
157PJ_DEF(void) cli_get_info(char *info, pj_size_t size)
158{
159 pj_cli_telnet_info telnet_info;
160 pj_cli_telnet_get_info(telnet_front_end, &telnet_info);
161
162 pj_ansi_snprintf(info, size, "Telnet to %.*s:%d",
163 (int)telnet_info.ip_address.slen,
164 telnet_info.ip_address.ptr,
165 telnet_info.port);
166}
167
168static void cli_log_writer(int level, const char *buffer, int len)
169{
170 if (cli)
171 pj_cli_write_log(cli, level, buffer, len);
172}
173
174pj_status_t cli_init()
175{
176 pj_status_t status;
177
178 pj_cli_cfg *cfg = &app_config.cli_cfg.cfg;
179
180 /* Init PJLIB */
181 status = pj_init();
182 if (status != PJ_SUCCESS)
183 goto on_error;
184
185 pj_inited = PJ_TRUE;
186
187 /* Init PJLIB-UTIL */
188 status = pjlib_util_init();
189 if (status != PJ_SUCCESS)
190 goto on_error;
191
192 /* Init CLI */
193 pj_caching_pool_init(&cli_cp, NULL, 0);
194 cli_cp_inited = PJ_TRUE;
195 cfg->pf = &cli_cp.factory;
196 cfg->name = pj_str("pjsua_cli");
197 cfg->title = pj_str("Pjsua CLI Application");
198 status = pj_cli_create(cfg, &cli);
199 if (status != PJ_SUCCESS)
200 goto on_error;
201
202 status = cli_setup_command(cli);
203 if (status != PJ_SUCCESS)
204 goto on_error;
205
206 /* Init telnet frontend */
207 if (app_config.cli_cfg.cli_fe & CLI_FE_TELNET) {
208 pj_cli_telnet_cfg *fe_cfg = &app_config.cli_cfg.telnet_cfg;
209 pj_pool_t *pool;
210
211 pool = pj_pool_create(cfg->pf, "cli_cp", 128, 128, NULL);
212 pj_assert(pool);
213
214 status = pj_cli_telnet_create(cli, fe_cfg, &telnet_front_end);
215 if (status != PJ_SUCCESS)
216 goto on_error;
217 }
218
219 /* Init console frontend */
220 if (app_config.cli_cfg.cli_fe & CLI_FE_CONSOLE) {
221 pj_cli_console_cfg *fe_cfg = &app_config.cli_cfg.console_cfg;
222
223 fe_cfg->quit_command = pj_str("shutdown");
224 status = pj_cli_console_create(cli, fe_cfg,
225 &cli_cons_sess, NULL);
226 if (status != PJ_SUCCESS)
227 goto on_error;
228 }
229
230 return PJ_SUCCESS;
231
232on_error:
233 cli_destroy();
234 return status;
235}
236
237pj_status_t cli_main(pj_bool_t wait_telnet_cli)
238{
239 char cmdline[PJ_CLI_MAX_CMDBUF];
240
241 /* ReInit logging */
242 app_config.log_cfg.cb = &cli_log_writer;
243 pjsua_reconfigure_logging(&app_config.log_cfg);
244
245 if (app_config.cli_cfg.cli_fe & CLI_FE_CONSOLE) {
246 /* Main loop for CLI FE console */
247 while (!pj_cli_is_quitting(cli)) {
248 pj_cli_console_process(cli_cons_sess, cmdline, sizeof(cmdline));
249 }
250 } else if (wait_telnet_cli) {
251 /* Just wait for CLI quit */
252 while (!pj_cli_is_quitting(cli)) {
253 pj_thread_sleep(200);
254 }
255 }
256
257 return PJ_SUCCESS;
258}
259
260void cli_destroy()
261{
262 /* Destroy CLI, it will automatically destroy any FEs */
263 if (cli) {
264 pj_cli_destroy(cli);
265 cli = NULL;
266 }
267
268 /* Destroy CLI caching pool factory */
269 if (cli_cp_inited) {
270 pj_caching_pool_destroy(&cli_cp);
271 cli_cp_inited = PJ_FALSE;
272 }
273
274 /* Shutdown PJLIB */
275 if (pj_inited) {
276 pj_shutdown();
277 pj_inited = PJ_FALSE;
278 }
279}
280
281/* Get input URL */
282static void get_input_url(char *buf,
283 pj_size_t len,
284 pj_cli_cmd_val *cval,
285 struct input_result *result)
286{
287 static const pj_str_t err_invalid_input = {"Invalid input\n", 15};
288 result->nb_result = PJSUA_APP_NO_NB;
289 result->uri_result = NULL;
290
291 len = strlen(buf);
292
293 /* Left trim */
294 while (pj_isspace(*buf)) {
295 ++buf;
296 --len;
297 }
298
299 /* Remove trailing newlines */
300 while (len && (buf[len-1] == '\r' || buf[len-1] == '\n'))
301 buf[--len] = '\0';
302
303 if (len == 0 || buf[0]=='q')
304 return;
305
306 if (pj_isdigit(*buf) || *buf=='-') {
307
308 unsigned i;
309
310 if (*buf=='-')
311 i = 1;
312 else
313 i = 0;
314
315 for (; i<len; ++i) {
316 if (!pj_isdigit(buf[i])) {
317 pj_cli_sess_write_msg(cval->sess, err_invalid_input.ptr,
318 (int)err_invalid_input.slen);
319 return;
320 }
321 }
322
323 result->nb_result = my_atoi(buf);
324
325 if (result->nb_result >= 0 &&
326 result->nb_result <= (int)pjsua_get_buddy_count())
327 {
328 return;
329 }
330 if (result->nb_result == -1)
331 return;
332
333 pj_cli_sess_write_msg(cval->sess, err_invalid_input.ptr,
334 (int)err_invalid_input.slen);
335 result->nb_result = PJSUA_APP_NO_NB;
336 return;
337
338 } else {
339 pj_status_t status;
340
341 if ((status=pjsua_verify_url(buf)) != PJ_SUCCESS) {
342 pjsua_perror(THIS_FILE, "Invalid URL", status);
343 return;
344 }
345
346 result->uri_result = buf;
347 }
348}
349
350/* CLI dynamic choice handler */
351/* Get buddy id */
352static void get_buddy_id(pj_cli_dyn_choice_param *param)
353{
354 if (param->cnt < param->max_cnt) {
355 pjsua_buddy_id ids[64];
356 int i = 0;
357 unsigned count = PJ_ARRAY_SIZE(ids);
358 char data_out[64];
359
360 pjsua_enum_buddies(ids, &count);
361
362 if (count > 0) {
363 for (i=0; i<(int)count; ++i) {
364 pjsua_buddy_info info;
365
366 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
367 continue;
368
369 /* Fill buddy id */
370 pj_ansi_snprintf(data_out, sizeof(data_out), "%d", ids[i]+1);
371 pj_strdup2(param->pool, &param->choice[i].value, data_out);
372 pj_bzero(data_out, PJ_ARRAY_SIZE(data_out));
373
374 /* Format & fill description */
375 pj_ansi_snprintf(data_out,
376 sizeof(data_out),
377 "<%.*s> %.*s",
378 (int)info.status_text.slen,
379 info.status_text.ptr,
380 (int)info.uri.slen,
381 info.uri.ptr);
382
383 pj_strdup2(param->pool, &param->choice[i].desc, data_out);
384 if (++param->cnt >= (param->max_cnt-1))
385 break;
386 }
387 }
388 if (param->arg_id == DYN_CHOICE_BUDDY_ID) {
389 /* Add URL input option */
390 pj_ansi_snprintf(data_out, sizeof(data_out), "URL");
391 pj_strdup2(param->pool, &param->choice[i].value, data_out);
392 pj_ansi_snprintf(data_out, sizeof(data_out), "An URL");
393 pj_strdup2(param->pool, &param->choice[i].desc, data_out);
394 ++param->cnt;
395 }
396 }
397}
398
399static void get_account_id(pj_cli_dyn_choice_param *param)
400{
401 if (param->cnt < param->max_cnt) {
402 char buf[8];
403 char buf_out[80];
404 pjsua_acc_info info;
405
406 pjsua_acc_id acc_ids[16];
407 unsigned count = PJ_ARRAY_SIZE(acc_ids);
408 int i;
409
410 pjsua_enum_accs(acc_ids, &count);
411
412 for (i=0; i<(int)count; ++i) {
413 pj_bzero(&buf_out[0], PJ_ARRAY_SIZE(buf_out));
414
415 pjsua_acc_get_info(acc_ids[i], &info);
416
417 pj_ansi_snprintf(buf_out,
418 sizeof(buf_out),
419 "%c%.*s",
420 (acc_ids[i]==current_acc?'*':' '),
421 (int)info.acc_uri.slen,
422 info.acc_uri.ptr);
423
424 pj_bzero(buf, sizeof(buf));
425 pj_ansi_snprintf(buf, sizeof(buf), "%d", acc_ids[i]);
426 pj_strdup2(param->pool, &param->choice[i].value, buf);
427 pj_strdup2(param->pool, &param->choice[i].desc, buf_out);
428 if (++param->cnt >= param->max_cnt)
429 break;
430 }
431 }
432}
433
434static void get_media_port(pj_cli_dyn_choice_param *param)
435{
436 unsigned i, count;
437 pjsua_conf_port_id id[PJSUA_MAX_CONF_PORTS];
438
439 count = PJ_ARRAY_SIZE(id);
440 pjsua_enum_conf_ports(id, &count);
441
442 for (i=0; i<count; ++i) {
443 char slot_id[8];
444 char desc[256];
445 char txlist[256];
446 unsigned j;
447 pjsua_conf_port_info info;
448
449 pjsua_conf_get_port_info(id[i], &info);
450
451 pj_ansi_snprintf(slot_id, sizeof(slot_id),
452 "%d", info.slot_id);
453 pj_strdup2(param->pool, &param->choice[i].value, slot_id);
454
455 txlist[0] = '\0';
456 for (j=0; j<info.listener_cnt; ++j) {
457 char s[10];
458 pj_ansi_snprintf(s, sizeof(s), "#%d ", info.listeners[j]);
459 pj_ansi_strcat(txlist, s);
460 }
461
462 pj_ansi_snprintf(desc,
463 sizeof(desc),
464 "[%2dKHz/%dms/%d] %20.*s transmitting to: %s",
465 info.clock_rate/1000,
466 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
467 info.channel_count,
468 (int)info.name.slen,
469 info.name.ptr,
470 txlist);
471
472 pj_strdup2(param->pool, &param->choice[i].desc, desc);
473 if (++param->cnt >= param->max_cnt)
474 break;
475 }
476}
477
478static void get_audio_codec_id(pj_cli_dyn_choice_param *param)
479{
480 if (param->cnt < param->max_cnt) {
481 pjsua_codec_info c[32];
482 unsigned i, count = PJ_ARRAY_SIZE(c);
483 char codec_id[64];
484 char desc[128];
485
486 pjsua_enum_codecs(c, &count);
487 for (i=0; i<count; ++i) {
488 pj_ansi_snprintf(codec_id, sizeof(codec_id),
489 "%.*s", (int)c[i].codec_id.slen,
490 c[i].codec_id.ptr);
491
492 pj_strdup2(param->pool, &param->choice[param->cnt].value, codec_id);
493
494 pj_ansi_snprintf(desc, sizeof(desc),
495 "Audio, prio: %d%s%.*s",
496 c[i].priority,
497 c[i].desc.slen? " - ":"",
498 (int)c[i].desc.slen,
499 c[i].desc.ptr);
500
501 pj_strdup2(param->pool, &param->choice[param->cnt].desc, desc);
502 if (++param->cnt >= param->max_cnt)
503 break;
504 }
505 }
506}
507
508#if PJSUA_HAS_VIDEO
509static void get_video_stream_id(pj_cli_dyn_choice_param *param)
510{
511 if (param->cnt < param->max_cnt) {
512 pjsua_call_info call_info;
513
514 if (current_call != PJSUA_INVALID_ID) {
515 unsigned i;
516 pjsua_call_get_info(current_call, &call_info);
517 for (i=0; i<call_info.media_cnt; ++i) {
518 if (call_info.media[i].type == PJMEDIA_TYPE_VIDEO) {
519 char med_idx[8];
520 pj_ansi_snprintf(med_idx, sizeof(med_idx), "%d",
521 call_info.media[i].index);
522 pj_strdup2(param->pool, &param->choice[i].value, med_idx);
523
524 switch (call_info.media[i].status) {
525 case PJSUA_CALL_MEDIA_NONE:
526 pj_strdup2(param->pool, &param->choice[i].desc,
527 "Status:None");
528 break;
529 case PJSUA_CALL_MEDIA_ACTIVE:
530 pj_strdup2(param->pool, &param->choice[i].desc,
531 "Status:Active");
532 break;
533 case PJSUA_CALL_MEDIA_LOCAL_HOLD:
534 pj_strdup2(param->pool, &param->choice[i].desc,
535 "Status:Local Hold");
536 break;
537 case PJSUA_CALL_MEDIA_REMOTE_HOLD:
538 pj_strdup2(param->pool, &param->choice[i].desc,
539 "Status:Remote Hold");
540 break;
541 case PJSUA_CALL_MEDIA_ERROR:
542 pj_strdup2(param->pool, &param->choice[i].desc,
543 "Status:Media Error");
544 break;
545 }
546 if (++param->cnt >= param->max_cnt)
547 break;
548 }
549 }
550 }
551 }
552}
553
554static void get_video_dev_hint(pj_cli_dyn_choice_param *param,
555 pjmedia_vid_dev_info *vdi,
556 unsigned vid_dev_id)
557{
558 char desc[128];
559 char dev_id[8];
560 pj_ansi_snprintf(dev_id, sizeof(dev_id),
561 "%d", vid_dev_id);
562 pj_ansi_snprintf(desc, sizeof(desc), "%s [%s]",
563 vdi->name, vdi->driver);
564
565 pj_strdup2(param->pool, &param->choice[param->cnt].value,
566 dev_id);
567 pj_strdup2(param->pool, &param->choice[param->cnt].desc,
568 desc);
569}
570
571static void get_video_dev_id(pj_cli_dyn_choice_param *param,
572 pj_bool_t all,
573 pj_bool_t capture)
574{
575 if (param->cnt < param->max_cnt) {
576 unsigned i, count;
577 pjmedia_vid_dev_info vdi;
578 pj_status_t status;
579
580 count = pjsua_vid_dev_count();
581 if (count == 0) {
582 return;
583 }
584
585 for (i=0; i<count; ++i) {
586 status = pjsua_vid_dev_get_info(i, &vdi);
587 if (status == PJ_SUCCESS) {
588 if ((all) ||
589 ((capture) && (vdi.dir == PJMEDIA_DIR_CAPTURE)) ||
590 ((!capture) && (vdi.dir == PJMEDIA_DIR_RENDER)))
591 {
592 get_video_dev_hint(param, &vdi, i);
593 if (++param->cnt >= param->max_cnt)
594 break;
595 }
596 }
597 }
598 }
599}
600
601static void get_video_codec_id(pj_cli_dyn_choice_param *param)
602{
603 if (param->cnt < param->max_cnt) {
604 pjsua_codec_info ci[32];
605 unsigned i, count = PJ_ARRAY_SIZE(ci);
606 char codec_id[64];
607 char desc[128];
608
609 pjsua_vid_enum_codecs(ci, &count);
610 for (i=0; i<count; ++i) {
611 pjmedia_vid_codec_param cp;
612 pjmedia_video_format_detail *vfd;
613 pj_status_t status = PJ_SUCCESS;
614
615 status = pjsua_vid_codec_get_param(&ci[i].codec_id, &cp);
616 if (status != PJ_SUCCESS)
617 continue;
618
619 vfd = pjmedia_format_get_video_format_detail(&cp.enc_fmt, PJ_TRUE);
620
621 pj_ansi_snprintf(codec_id, sizeof(codec_id),
622 "%.*s", (int)ci[i].codec_id.slen,
623 ci[i].codec_id.ptr);
624
625 pj_strdup2(param->pool, &param->choice[param->cnt].value, codec_id);
626
627 pj_ansi_snprintf(desc, sizeof(desc),
628 "Video, p[%d], f[%.2f], b[%d/%d], s[%dx%d]",
629 ci[i].priority,
630 (vfd->fps.num*1.0/vfd->fps.denum),
631 vfd->avg_bps/1000, vfd->max_bps/1000,
632 vfd->size.w, vfd->size.h);
633
634 pj_strdup2(param->pool, &param->choice[param->cnt].desc, desc);
635 if (++param->cnt >= param->max_cnt)
636 break;
637 }
638 }
639}
640
641static void get_video_window_id(pj_cli_dyn_choice_param *param)
642{
643 if (param->cnt < param->max_cnt) {
644 pjsua_vid_win_id wids[PJSUA_MAX_VID_WINS];
645 unsigned i, cnt = PJ_ARRAY_SIZE(wids);
646 char win_id[64];
647 char desc[128];
648
649 pjsua_vid_enum_wins(wids, &cnt);
650
651 for (i = 0; i < cnt; ++i) {
652 pjsua_vid_win_info wi;
653
654 pjsua_vid_win_get_info(wids[i], &wi);
655 pj_ansi_snprintf(win_id, sizeof(win_id), "%d", wids[i]);
656 pj_strdup2(param->pool, &param->choice[i].value, win_id);
657
658 pj_ansi_snprintf(desc, sizeof(desc),
659 "Show:%c Pos(%d,%d) Size(%dx%d)",
660 (wi.show?'Y':'N'), wi.pos.x, wi.pos.y,
661 wi.size.w, wi.size.h);
662
663 pj_strdup2(param->pool, &param->choice[i].desc, desc);
664 if (++param->cnt >= param->max_cnt)
665 break;
666 }
667 }
668}
669
670static void get_call_id(pj_cli_dyn_choice_param *param)
671{
672 if (param->cnt < param->max_cnt) {
673 char call_id[64];
674 char desc[128];
675 unsigned i, count;
676 pjsua_call_id ids[PJSUA_MAX_CALLS];
677 int call = current_call;
678
679 count = PJ_ARRAY_SIZE(ids);
680 pjsua_enum_calls(ids, &count);
681
682 if (count > 1) {
683 for (i=0; i<count; ++i) {
684 pjsua_call_info call_info;
685
686 if (ids[i] == call)
687 return;
688
689 pjsua_call_get_info(ids[i], &call_info);
690 pj_ansi_snprintf(call_id, sizeof(call_id), "%d", ids[i]);
691 pj_strdup2(param->pool, &param->choice[i].value, call_id);
692 pj_ansi_snprintf(desc, sizeof(desc), "%.*s [%.*s]",
693 (int)call_info.remote_info.slen,
694 call_info.remote_info.ptr,
695 (int)call_info.state_text.slen,
696 call_info.state_text.ptr);
697 pj_strdup2(param->pool, &param->choice[i].desc, desc);
698 if (++param->cnt >= param->max_cnt)
699 break;
700
701 }
702 }
703 }
704}
705
706#endif
707
708static void get_choice_value(pj_cli_dyn_choice_param *param)
709{
710 switch (param->arg_id) {
711 case DYN_CHOICE_BUDDY_ID:
712 case DYN_CHOICE_ADDED_BUDDY_ID:
713 get_buddy_id(param);
714 break;
715 case DYN_CHOICE_ACCOUNT_ID:
716 get_account_id(param);
717 break;
718 case DYN_CHOICE_MEDIA_PORT:
719 get_media_port(param);
720 break;
721 case DYN_CHOICE_AUDIO_CODEC_ID:
722 get_audio_codec_id(param);
723 break;
724#if PJSUA_HAS_VIDEO
725 case DYN_CHOICE_CAP_DEV_ID:
726 case DYN_CHOICE_REN_DEV_ID:
727 case DYN_CHOICE_VID_DEV_ID:
728 get_video_dev_id(param,
729 (param->arg_id==DYN_CHOICE_VID_DEV_ID),
730 (param->arg_id==DYN_CHOICE_CAP_DEV_ID));
731 break;
732 case DYN_CHOICE_STREAM_ID:
733 get_video_stream_id(param);
734 break;
735 case DYN_CHOICE_VIDEO_CODEC_ID:
736 get_video_codec_id(param);
737 break;
738 case DYN_CHOICE_WIN_ID:
739 get_video_window_id(param);
740 break;
741 case DYN_CHOICE_CALL_ID:
742 get_call_id(param);
743 break;
744#endif
745 default:
746 param->cnt = 0;
747 break;
748 }
749}
750
751/*
752 * CLI command handler
753 */
754
755/* Add account */
756static pj_status_t cmd_add_account(pj_cli_cmd_val *cval)
757{
758 pjsua_acc_config acc_cfg;
759 pj_status_t status;
760
761 pjsua_acc_config_default(&acc_cfg);
762 acc_cfg.id = cval->argv[1];
763 acc_cfg.reg_uri = cval->argv[2];
764 acc_cfg.cred_count = 1;
765 acc_cfg.cred_info[0].scheme = pj_str("Digest");
766 acc_cfg.cred_info[0].realm = cval->argv[3];
767 acc_cfg.cred_info[0].username = cval->argv[4];
768 acc_cfg.cred_info[0].data_type = 0;
769 acc_cfg.cred_info[0].data = cval->argv[5];
770
771 acc_cfg.rtp_cfg = app_config.rtp_cfg;
772 app_config_init_video(&acc_cfg);
773
774 status = pjsua_acc_add(&acc_cfg, PJ_TRUE, NULL);
775 if (status != PJ_SUCCESS) {
776 pjsua_perror(THIS_FILE, "Error adding new account", status);
777 }
778
779 return status;
780}
781
782/* Delete account */
783static pj_status_t cmd_del_account(pj_cli_cmd_val *cval)
784{
785 char out_str[64];
786 unsigned str_len;
787
788 int i = my_atoi(cval->argv[1].ptr);
789
790 if (!pjsua_acc_is_valid(i)) {
791 pj_ansi_snprintf(out_str, sizeof(out_str),
792 "Invalid account id %d\n", i);
793 str_len = (unsigned)pj_ansi_strlen(out_str);
794 pj_cli_sess_write_msg(cval->sess, out_str, str_len);
795 } else {
796 pjsua_acc_del(i);
797 pj_ansi_snprintf(out_str, sizeof(out_str),
798 "Account %d deleted\n", i);
799 str_len = (unsigned)pj_ansi_strlen(out_str);
800 pj_cli_sess_write_msg(cval->sess, out_str, str_len);
801 }
802 return PJ_SUCCESS;
803}
804
805/* Modify account */
806static pj_status_t cmd_mod_account(pj_cli_cmd_val *cval)
807{
808 PJ_UNUSED_ARG(cval);
809 return PJ_SUCCESS;
810}
811
812/* Register account */
813static pj_status_t cmd_reg_account()
814{
815 pjsua_acc_set_registration(current_acc, PJ_TRUE);
816 return PJ_SUCCESS;
817}
818
819/* Unregister account */
820static pj_status_t cmd_unreg_account()
821{
822 pjsua_acc_set_registration(current_acc, PJ_FALSE);
823 return PJ_SUCCESS;
824}
825
826/* Select account to be used for sending outgoing request */
827static pj_status_t cmd_next_account(pj_cli_cmd_val *cval)
828{
829 int i = my_atoi(cval->argv[1].ptr);
830 if (pjsua_acc_is_valid(i)) {
831 pjsua_acc_set_default(i);
832 PJ_LOG(3,(THIS_FILE, "Current account changed to %d", i));
833 } else {
834 PJ_LOG(3,(THIS_FILE, "Invalid account id %d", i));
835 }
836 return PJ_SUCCESS;
837}
838
839/* Show account list */
840static pj_status_t cmd_show_account(pj_cli_cmd_val *cval)
841{
842 pjsua_acc_id acc_ids[16];
843 unsigned count = PJ_ARRAY_SIZE(acc_ids);
844 int i;
845 static const pj_str_t header = {"Account list:\n", 15};
846
847 pjsua_enum_accs(acc_ids, &count);
848
849 pj_cli_sess_write_msg(cval->sess, header.ptr, header.slen);
850 for (i=0; i<(int)count; ++i) {
851 char acc_info[80];
852 char out_str[160];
853 pjsua_acc_info info;
854
855 pjsua_acc_get_info(acc_ids[i], &info);
856
857 if (!info.has_registration) {
858 pj_ansi_snprintf(acc_info, sizeof(acc_info), "%.*s",
859 (int)info.status_text.slen,
860 info.status_text.ptr);
861
862 } else {
863 pj_ansi_snprintf(acc_info, sizeof(acc_info),
864 "%d/%.*s (expires=%d)",
865 info.status,
866 (int)info.status_text.slen,
867 info.status_text.ptr,
868 info.expires);
869
870 }
871
872 pj_ansi_snprintf(out_str, sizeof(out_str),
873 " %c[%2d] %.*s: %s\n",
874 (acc_ids[i]==current_acc?'*':' '), acc_ids[i],
875 (int)info.acc_uri.slen, info.acc_uri.ptr,
876 acc_info);
877 pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str));
878
879 pj_bzero(out_str, sizeof(out_str));
880 pj_ansi_snprintf(out_str, sizeof(out_str),
881 " Online status: %.*s\n",
882 (int)info.online_status_text.slen,
883 info.online_status_text.ptr);
884
885 pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str));
886 }
887
888 return PJ_SUCCESS;
889}
890
891/* Account command handler */
892pj_status_t cmd_account_handler(pj_cli_cmd_val *cval)
893{
894 pj_status_t status = PJ_SUCCESS;
895
896 CHECK_PJSUA_RUNNING();
897
898 switch(pj_cli_get_cmd_id(cval->cmd)) {
899 case CMD_ACCOUNT_ADD:
900 status = cmd_add_account(cval);
901 break;
902 case CMD_ACCOUNT_DEL:
903 status = cmd_del_account(cval);
904 break;
905 case CMD_ACCOUNT_MOD:
906 status = cmd_mod_account(cval);
907 break;
908 case CMD_ACCOUNT_REG:
909 status = cmd_reg_account();
910 break;
911 case CMD_ACCOUNT_UNREG:
912 status = cmd_unreg_account();
913 break;
914 case CMD_ACCOUNT_NEXT:
915 case CMD_ACCOUNT_PREV:
916 status = cmd_next_account(cval);
917 break;
918 case CMD_ACCOUNT_SHOW:
919 status = cmd_show_account(cval);
920 break;
921 }
922 return status;
923}
924
925/* Add buddy */
926static pj_status_t cmd_add_buddy(pj_cli_cmd_val *cval)
927{
928 char out_str[80];
929 pjsua_buddy_config buddy_cfg;
930 pjsua_buddy_id buddy_id;
931 pj_status_t status = PJ_SUCCESS;
932 cval->argv[1].ptr[cval->argv[1].slen] = 0;
933
934 if (pjsua_verify_url(cval->argv[1].ptr) != PJ_SUCCESS) {
935 pj_ansi_snprintf(out_str, sizeof(out_str),
936 "Invalid URI '%s'\n", cval->argv[1].ptr);
937 } else {
938 pj_bzero(&buddy_cfg, sizeof(pjsua_buddy_config));
939
940 buddy_cfg.uri = pj_str(cval->argv[1].ptr);
941 buddy_cfg.subscribe = PJ_TRUE;
942
943 status = pjsua_buddy_add(&buddy_cfg, &buddy_id);
944 if (status == PJ_SUCCESS) {
945 pj_ansi_snprintf(out_str, sizeof(out_str),
946 "New buddy '%s' added at index %d\n",
947 cval->argv[1].ptr, buddy_id+1);
948 }
949 }
950 pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str));
951 return status;
952}
953
954/* Delete buddy */
955static pj_status_t cmd_del_buddy(pj_cli_cmd_val *cval)
956{
957 int i = my_atoi(cval->argv[1].ptr) - 1;
958 char out_str[80];
959
960 if (!pjsua_buddy_is_valid(i)) {
961 pj_ansi_snprintf(out_str, sizeof(out_str),
962 "Invalid buddy id %d\n", i);
963 } else {
964 pjsua_buddy_del(i);
965 pj_ansi_snprintf(out_str, sizeof(out_str),
966 "Buddy %d deleted\n", i);
967 }
968 pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str));
969 return PJ_SUCCESS;
970}
971
972/* Send IM */
973static pj_status_t cmd_send_im(pj_cli_cmd_val *cval)
974{
975 int i = -1;
976 struct input_result result;
977 char dest[64];
978 pj_str_t tmp = pj_str(dest);
979
980 /* make compiler happy. */
981 char *uri = NULL;
982
983 pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(dest));
984
985 /* input destination. */
986 get_input_url(tmp.ptr, tmp.slen, cval, &result);
987 if (result.nb_result != PJSUA_APP_NO_NB) {
988
989 if (result.nb_result == -1) {
990 static const pj_str_t err_msg = {"you can't send broadcast im "
991 "like that!\n", 40 };
992 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
993 return PJ_SUCCESS;
994 } else if (result.nb_result == 0) {
995 i = current_call;
996 } else {
997 pjsua_buddy_info binfo;
998 pjsua_buddy_get_info(result.nb_result-1, &binfo);
999 pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(dest));
1000 uri = tmp.ptr;
1001 }
1002
1003 } else if (result.uri_result) {
1004 uri = result.uri_result;
1005 }
1006
1007 /* send typing indication. */
1008 if (i != -1)
1009 pjsua_call_send_typing_ind(i, PJ_TRUE, NULL);
1010 else {
1011 pj_str_t tmp_uri = pj_str(uri);
1012 pjsua_im_typing(current_acc, &tmp_uri, PJ_TRUE, NULL);
1013 }
1014
1015 /* send the im */
1016 if (i != -1)
1017 pjsua_call_send_im(i, NULL, &cval->argv[2], NULL, NULL);
1018 else {
1019 pj_str_t tmp_uri = pj_str(uri);
1020 pjsua_im_send(current_acc, &tmp_uri, NULL, &cval->argv[2], NULL, NULL);
1021 }
1022 return PJ_SUCCESS;
1023}
1024
1025/* Subscribe/unsubscribe presence */
1026static pj_status_t cmd_subs_pres(pj_cli_cmd_val *cval, pj_bool_t subscribe)
1027{
1028 struct input_result result;
1029 char dest[64] = {0};
1030 pj_str_t tmp = pj_str(dest);
1031
1032 pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(dest));
1033 get_input_url(tmp.ptr, tmp.slen, cval, &result);
1034 if (result.nb_result != PJSUA_APP_NO_NB) {
1035 if (result.nb_result == -1) {
1036 int i, count;
1037 count = pjsua_get_buddy_count();
1038 for (i=0; i<count; ++i)
1039 pjsua_buddy_subscribe_pres(i, subscribe);
1040 } else if (result.nb_result == 0) {
1041 static const pj_str_t err_msg = {"Sorry, can only subscribe to "
1042 "buddy's presence, not from "
1043 "existing call\n", 71};
1044 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1045 } else {
1046 pjsua_buddy_subscribe_pres(result.nb_result-1, subscribe);
1047 }
1048
1049 } else if (result.uri_result) {
1050 static const pj_str_t err_msg = {"Sorry, can only subscribe to "
1051 "buddy's presence, not arbitrary "
1052 "URL (for now)\n", 76};
1053 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1054 }
1055 return PJ_SUCCESS;
1056}
1057
1058/* Toggle online state */
1059static pj_status_t cmd_toggle_state(pj_cli_cmd_val *cval)
1060{
1061 char out_str[128];
1062 pjsua_acc_info acc_info;
1063
1064 pjsua_acc_get_info(current_acc, &acc_info);
1065 acc_info.online_status = !acc_info.online_status;
1066 pjsua_acc_set_online_status(current_acc, acc_info.online_status);
1067 pj_ansi_snprintf(out_str, sizeof(out_str),
1068 "Setting %s online status to %s\n",
1069 acc_info.acc_uri.ptr,
1070 (acc_info.online_status?"online":"offline"));
1071 pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str));
1072 return PJ_SUCCESS;
1073}
1074
1075/* Set presence text */
1076static pj_status_t cmd_set_presence_text(pj_cli_cmd_val *cval)
1077{
1078 pjrpid_element elem;
1079 int choice;
1080 pj_bool_t online_status;
1081
1082 enum {
1083 AVAILABLE, BUSY, OTP, IDLE, AWAY, BRB, OFFLINE, OPT_MAX
1084 };
1085
1086 choice = pj_strtol(&cval->argv[1]) - 1;
1087
1088 pj_bzero(&elem, sizeof(elem));
1089 elem.type = PJRPID_ELEMENT_TYPE_PERSON;
1090
1091 online_status = PJ_TRUE;
1092
1093 switch (choice) {
1094 case AVAILABLE:
1095 break;
1096 case BUSY:
1097 elem.activity = PJRPID_ACTIVITY_BUSY;
1098 elem.note = pj_str("Busy");
1099 break;
1100 case OTP:
1101 elem.activity = PJRPID_ACTIVITY_BUSY;
1102 elem.note = pj_str("On the phone");
1103 break;
1104 case IDLE:
1105 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
1106 elem.note = pj_str("Idle");
1107 break;
1108 case AWAY:
1109 elem.activity = PJRPID_ACTIVITY_AWAY;
1110 elem.note = pj_str("Away");
1111 break;
1112 case BRB:
1113 elem.activity = PJRPID_ACTIVITY_UNKNOWN;
1114 elem.note = pj_str("Be right back");
1115 break;
1116 case OFFLINE:
1117 online_status = PJ_FALSE;
1118 break;
1119 }
1120 pjsua_acc_set_online_status2(current_acc, online_status, &elem);
1121 return PJ_SUCCESS;
1122}
1123
1124/* Show buddy list */
1125static pj_status_t cmd_show_buddy(pj_cli_cmd_val *cval)
1126{
1127 pjsua_buddy_id ids[64];
1128 int i;
1129 unsigned count = PJ_ARRAY_SIZE(ids);
1130 static const pj_str_t header = {"Buddy list:\n", 13};
1131 char out_str[64];
1132
1133 pj_cli_sess_write_msg(cval->sess, header.ptr, header.slen);
1134
1135 pjsua_enum_buddies(ids, &count);
1136
1137 if (count == 0) {
1138 pj_ansi_snprintf(out_str, sizeof(out_str), " -none-\n");
1139 pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str));
1140 } else {
1141 for (i=0; i<(int)count; ++i) {
1142 pjsua_buddy_info info;
1143 pj_bzero(out_str, sizeof(out_str));
1144
1145 if (pjsua_buddy_get_info(ids[i], &info) != PJ_SUCCESS)
1146 continue;
1147
1148 pj_ansi_snprintf(out_str, sizeof(out_str),
1149 " [%2d] <%.*s> %.*s\n",
1150 ids[i]+1,
1151 (int)info.status_text.slen,
1152 info.status_text.ptr,
1153 (int)info.uri.slen,
1154 info.uri.ptr);
1155
1156 pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str));
1157 }
1158 }
1159 return PJ_SUCCESS;
1160}
1161
1162/* Presence/buddy command handler */
1163pj_status_t cmd_presence_handler(pj_cli_cmd_val *cval)
1164{
1165 pj_status_t status = PJ_SUCCESS;
1166
1167 CHECK_PJSUA_RUNNING();
1168
1169 switch(pj_cli_get_cmd_id(cval->cmd)) {
1170 case CMD_PRESENCE_ADD_BUDDY:
1171 status = cmd_add_buddy(cval);
1172 break;
1173 case CMD_PRESENCE_DEL_BUDDY:
1174 status = cmd_del_buddy(cval);
1175 break;
1176 case CMD_PRESENCE_SEND_IM:
1177 status = cmd_send_im(cval);
1178 break;
1179 case CMD_PRESENCE_SUB:
1180 case CMD_PRESENCE_UNSUB:
1181 status = cmd_subs_pres(cval,
1182 pj_cli_get_cmd_id(cval->cmd)==CMD_PRESENCE_SUB);
1183 break;
1184 case CMD_PRESENCE_TOG_STATE:
1185 status = cmd_toggle_state(cval);
1186 break;
1187 case CMD_PRESENCE_TEXT:
1188 status = cmd_set_presence_text(cval);
1189 break;
1190 case CMD_PRESENCE_LIST:
1191 status = cmd_show_buddy(cval);
1192 break;
1193 }
1194
1195 return status;
1196}
1197
1198/* Show conference list */
1199static pj_status_t cmd_media_list(pj_cli_cmd_val *cval)
1200{
1201 unsigned i, count;
1202 pjsua_conf_port_id id[PJSUA_MAX_CONF_PORTS];
1203 static const pj_str_t header = {"Conference ports:\n", 19};
1204
1205 pj_cli_sess_write_msg(cval->sess, header.ptr, header.slen);
1206
1207 count = PJ_ARRAY_SIZE(id);
1208 pjsua_enum_conf_ports(id, &count);
1209
1210 for (i=0; i<count; ++i) {
1211 char out_str[128];
1212 char txlist[16];
1213 unsigned j;
1214 pjsua_conf_port_info info;
1215
1216 pjsua_conf_get_port_info(id[i], &info);
1217
1218 pj_bzero(txlist, sizeof(txlist));
1219 for (j=0; j<info.listener_cnt; ++j) {
1220 char s[10];
1221 pj_ansi_snprintf(s, sizeof(s), "#%d ", info.listeners[j]);
1222 pj_ansi_strcat(txlist, s);
1223 }
1224 pj_ansi_snprintf(out_str,
1225 sizeof(out_str),
1226 "Port #%02d[%2dKHz/%dms/%d] %20.*s transmitting to: %s\n",
1227 info.slot_id,
1228 info.clock_rate/1000,
1229 info.samples_per_frame*1000/info.channel_count/info.clock_rate,
1230 info.channel_count,
1231 (int)info.name.slen,
1232 info.name.ptr,
1233 txlist);
1234 pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str));
1235 }
1236 return PJ_SUCCESS;
1237}
1238
1239/* Conference connect/disconnect */
1240static pj_status_t cmd_media_connect(pj_cli_cmd_val *cval, pj_bool_t connect)
1241{
1242 pj_status_t status;
1243
1244 if (connect)
1245 status = pjsua_conf_connect(pj_strtol(&cval->argv[1]),
1246 pj_strtol(&cval->argv[2]));
1247 else
1248 status = pjsua_conf_disconnect(pj_strtol(&cval->argv[1]),
1249 pj_strtol(&cval->argv[2]));
1250
1251 if (status == PJ_SUCCESS) {
1252 static const pj_str_t success_msg = {"Success\n", 9};
1253 pj_cli_sess_write_msg(cval->sess, success_msg.ptr, success_msg.slen);
1254 } else {
1255 static const pj_str_t err_msg = {"ERROR!!\n", 9};
1256 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1257 }
1258 return status;
1259}
1260
1261/* Adjust audio volume */
1262static pj_status_t cmd_adjust_vol(pj_cli_cmd_val *cval)
1263{
1264 char buf[80];
1265 float orig_level;
1266 char *err;
1267 char level_val[16] = {0};
1268 pj_str_t tmp = pj_str(level_val);
1269
1270 /* Adjust mic level */
1271 orig_level = app_config.mic_level;
1272 pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(level_val));
1273 app_config.mic_level = (float)strtod(level_val, &err);
1274 pjsua_conf_adjust_rx_level(0, app_config.mic_level);
1275
1276 pj_ansi_snprintf(buf, sizeof(buf),
1277 "Adjust mic level: [%4.1fx] -> [%4.1fx]\n",
1278 orig_level, app_config.mic_level);
1279
1280 pj_cli_sess_write_msg(cval->sess, buf, pj_ansi_strlen(buf));
1281
1282 /* Adjust speaker level */
1283 orig_level = app_config.speaker_level;
1284 pj_strncpy_with_null(&tmp, &cval->argv[2], sizeof(level_val));
1285 app_config.speaker_level = (float)strtod(level_val, &err);
1286 pjsua_conf_adjust_tx_level(0, app_config.speaker_level);
1287
1288 pj_ansi_snprintf(buf, sizeof(buf),
1289 "Adjust speaker level: [%4.1fx] -> [%4.1fx]\n",
1290 orig_level, app_config.speaker_level);
1291
1292 pj_cli_sess_write_msg(cval->sess, buf, pj_ansi_strlen(buf));
1293
1294 return PJ_SUCCESS;
1295}
1296
1297/* Set codec priority */
1298static pj_status_t cmd_set_codec_prio(pj_cli_cmd_val *cval)
1299{
1300 int new_prio;
1301 pj_status_t status;
1302
1303 new_prio = pj_strtol(&cval->argv[2]);
1304 if (new_prio < 0)
1305 new_prio = 0;
1306 else if (new_prio > PJMEDIA_CODEC_PRIO_HIGHEST)
1307 new_prio = PJMEDIA_CODEC_PRIO_HIGHEST;
1308
1309 status = pjsua_codec_set_priority(&cval->argv[1],
1310 (pj_uint8_t)new_prio);
1311#if PJSUA_HAS_VIDEO
1312 if (status != PJ_SUCCESS) {
1313 status = pjsua_vid_codec_set_priority(&cval->argv[1],
1314 (pj_uint8_t)new_prio);
1315 }
1316#endif
1317 if (status != PJ_SUCCESS)
1318 pjsua_perror(THIS_FILE, "Error setting codec priority", status);
1319
1320 return status;
1321}
1322
1323/* Conference/media command handler */
1324pj_status_t cmd_media_handler(pj_cli_cmd_val *cval)
1325{
1326 pj_status_t status = PJ_SUCCESS;
1327
1328 CHECK_PJSUA_RUNNING();
1329
1330 switch(pj_cli_get_cmd_id(cval->cmd)) {
1331 case CMD_MEDIA_LIST:
1332 status = cmd_media_list(cval);
1333 break;
1334 case CMD_MEDIA_CONF_CONNECT:
1335 case CMD_MEDIA_CONF_DISCONNECT:
1336 status = cmd_media_connect(cval,
1337 pj_cli_get_cmd_id(cval->cmd)==CMD_MEDIA_CONF_CONNECT);
1338 break;
1339 case CMD_MEDIA_ADJUST_VOL:
1340 status = cmd_adjust_vol(cval);
1341 break;
1342 case CMD_MEDIA_CODEC_PRIO:
1343 status = cmd_set_codec_prio(cval);
1344 break;
1345 }
1346
1347 return status;
1348}
1349
1350/* Dump status */
1351static pj_status_t cmd_stat_dump(pj_bool_t detail)
1352{
1353 pjsua_dump(detail);
1354 return PJ_SUCCESS;
1355}
1356
1357static pj_status_t cmd_show_config()
1358{
1359 char settings[2000];
1360 int len;
1361
1362 len = write_settings(&app_config, settings, sizeof(settings));
1363 if (len < 1)
1364 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
1365 else
1366 PJ_LOG(3,(THIS_FILE,
1367 "Dumping configuration (%d bytes):\n%s\n",
1368 len, settings));
1369
1370 return PJ_SUCCESS;
1371}
1372
1373static pj_status_t cmd_write_config(pj_cli_cmd_val *cval)
1374{
1375 char settings[2000];
1376 char buf[128] = {0};
1377 int len;
1378 pj_str_t tmp = pj_str(buf);
1379
1380 pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(buf));
1381
1382 len = write_settings(&app_config, settings, sizeof(settings));
1383 if (len < 1)
1384 PJ_LOG(1,(THIS_FILE, "Error: not enough buffer"));
1385 else {
1386 pj_oshandle_t fd;
1387 pj_status_t status;
1388
1389 status = pj_file_open(app_config.pool, buf, PJ_O_WRONLY, &fd);
1390 if (status != PJ_SUCCESS) {
1391 pjsua_perror(THIS_FILE, "Unable to open file", status);
1392 } else {
1393 char out_str[256];
1394 pj_ssize_t size = len;
1395 pj_file_write(fd, settings, &size);
1396 pj_file_close(fd);
1397
1398 pj_ansi_snprintf(out_str, sizeof(out_str),
1399 "Settings successfully written to '%s'\n", buf);
1400
1401 pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str));
1402 }
1403 }
1404
1405 return PJ_SUCCESS;
1406}
1407
1408/* Status and config command handler */
1409pj_status_t cmd_config_handler(pj_cli_cmd_val *cval)
1410{
1411 pj_status_t status = PJ_SUCCESS;
1412
1413 CHECK_PJSUA_RUNNING();
1414
1415 switch(pj_cli_get_cmd_id(cval->cmd)) {
1416 case CMD_CONFIG_DUMP_STAT:
1417 status = cmd_stat_dump(PJ_FALSE);
1418 break;
1419 case CMD_CONFIG_DUMP_DETAIL:
1420 status = cmd_stat_dump(PJ_TRUE);
1421 break;
1422 case CMD_CONFIG_DUMP_CONF:
1423 status = cmd_show_config();
1424 break;
1425 case CMD_CONFIG_WRITE_SETTING:
1426 status = cmd_write_config(cval);
1427 break;
1428 }
1429
1430 return status;
1431}
1432
1433/* Make single call */
1434static pj_status_t cmd_make_single_call(pj_cli_cmd_val *cval)
1435{
1436 struct input_result result;
1437 char dest[64] = {0};
1438 char out_str[128];
1439 pj_str_t tmp = pj_str(dest);
1440
1441 pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(dest));
1442
1443 pj_ansi_snprintf(out_str,
1444 sizeof(out_str),
1445 "(You currently have %d calls)\n",
1446 pjsua_call_get_count());
1447
1448 pj_cli_sess_write_msg(cval->sess, out_str, pj_ansi_strlen(out_str));
1449
1450 /* input destination. */
1451 get_input_url(tmp.ptr, tmp.slen, cval, &result);
1452 if (result.nb_result != PJSUA_APP_NO_NB) {
1453 pjsua_buddy_info binfo;
1454 if (result.nb_result == -1 || result.nb_result == 0) {
1455 static const pj_str_t err_msg =
1456 {"You can't do that with make call!\n", 35};
1457 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1458 return PJ_SUCCESS;
1459 }
1460 pjsua_buddy_get_info(result.nb_result-1, &binfo);
1461 pj_strncpy(&tmp, &binfo.uri, sizeof(dest));
1462 } else if (result.uri_result) {
1463 tmp = pj_str(result.uri_result);
1464 } else {
1465 tmp.slen = 0;
1466 }
1467
1468 pjsua_msg_data_init(&msg_data);
1469 TEST_MULTIPART(&msg_data);
1470 pjsua_call_make_call(current_acc, &tmp, &call_opt, NULL,
1471 &msg_data, &current_call);
1472 return PJ_SUCCESS;
1473}
1474
1475/* Make multi call */
1476static pj_status_t cmd_make_multi_call(pj_cli_cmd_val *cval)
1477{
1478 struct input_result result;
1479 char dest[64] = {0};
1480 char out_str[128];
1481 int i, count;
1482 pj_str_t tmp = pj_str(dest);
1483
1484 pj_ansi_snprintf(out_str,
1485 sizeof(out_str),
1486 "(You currently have %d calls)\n",
1487 pjsua_call_get_count());
1488
1489 count = pj_strtol(&cval->argv[1]);
1490 if (count < 1)
1491 return PJ_SUCCESS;
1492
1493 pj_strncpy_with_null(&tmp, &cval->argv[2], sizeof(dest));
1494
1495 /* input destination. */
1496 get_input_url(tmp.ptr, tmp.slen, cval, &result);
1497 if (result.nb_result != PJSUA_APP_NO_NB) {
1498 pjsua_buddy_info binfo;
1499 if (result.nb_result == -1 || result.nb_result == 0) {
1500 static const pj_str_t err_msg =
1501 {"You can't do that with make call!\n", 35};
1502 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1503 return PJ_SUCCESS;
1504 }
1505 pjsua_buddy_get_info(result.nb_result-1, &binfo);
1506 pj_strncpy(&tmp, &binfo.uri, sizeof(dest));
1507 } else {
1508 tmp = pj_str(result.uri_result);
1509 }
1510
1511 for (i=0; i<count; ++i) {
1512 pj_status_t status;
1513
1514 status = pjsua_call_make_call(current_acc, &tmp, &call_opt, NULL,
1515 NULL, NULL);
1516 if (status != PJ_SUCCESS)
1517 break;
1518 }
1519 return PJ_SUCCESS;
1520}
1521
1522/* Answer call */
1523static pj_status_t cmd_answer_call(pj_cli_cmd_val *cval)
1524{
1525 pjsua_call_info call_info;
1526
1527 if (current_call != PJSUA_INVALID_ID) {
1528 pjsua_call_get_info(current_call, &call_info);
1529 } else {
1530 /* Make compiler happy */
1531 call_info.role = PJSIP_ROLE_UAC;
1532 call_info.state = PJSIP_INV_STATE_DISCONNECTED;
1533 }
1534
1535 if (current_call == PJSUA_INVALID_ID ||
1536 call_info.role != PJSIP_ROLE_UAS ||
1537 call_info.state >= PJSIP_INV_STATE_CONNECTING)
1538 {
1539 static const pj_str_t err_msg = {"No pending incoming call\n", 26};
1540 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1541
1542 } else {
1543 int st_code;
1544 char contact[120];
1545 pj_str_t hname = { "Contact", 7 };
1546 pj_str_t hvalue;
1547 pjsip_generic_string_hdr hcontact;
1548
1549 st_code = pj_strtol(&cval->argv[1]);
1550 if ((st_code < 100) || (st_code > 699))
1551 return PJ_SUCCESS;
1552
1553 pjsua_msg_data_init(&msg_data);
1554
1555 if (st_code/100 == 3) {
1556 if (cval->argc < 3) {
1557 static const pj_str_t err_msg = {"Enter URL to be put "
1558 "in Contact\n", 32};
1559 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1560 return PJ_SUCCESS;
1561 }
1562
1563 hvalue = pj_str(contact);
1564 pjsip_generic_string_hdr_init2(&hcontact, &hname, &hvalue);
1565
1566 pj_list_push_back(&msg_data.hdr_list, &hcontact);
1567 }
1568
1569 /*
1570 * Must check again!
1571 * Call may have been disconnected while we're waiting for
1572 * keyboard input.
1573 */
1574 if (current_call == PJSUA_INVALID_ID) {
1575 static const pj_str_t err_msg = {"Call has been disconnected\n",
1576 28};
1577 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1578 }
1579
1580 pjsua_call_answer2(current_call, &call_opt, st_code, NULL, &msg_data);
1581 }
1582 return PJ_SUCCESS;
1583}
1584
1585/* Hangup call */
1586static pj_status_t cmd_hangup_call(pj_cli_cmd_val *cval, pj_bool_t all)
1587{
1588 if (current_call == PJSUA_INVALID_ID) {
1589 static const pj_str_t err_msg = {"No current call\n", 17};
1590 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1591 } else {
1592 if (all)
1593 pjsua_call_hangup_all();
1594 else
1595 pjsua_call_hangup(current_call, 0, NULL, NULL);
1596 }
1597 return PJ_SUCCESS;
1598}
1599
1600/* Hold call */
1601static pj_status_t cmd_hold_call()
1602{
1603 if (current_call != PJSUA_INVALID_ID) {
1604 pjsua_call_set_hold(current_call, NULL);
1605
1606 } else {
1607 PJ_LOG(3,(THIS_FILE, "No current call"));
1608 }
1609 return PJ_SUCCESS;
1610}
1611
1612/* Call reinvite */
1613static pj_status_t cmd_call_reinvite()
1614{
1615 if (current_call != PJSUA_INVALID_ID) {
1616 /*
1617 * re-INVITE
1618 */
1619 call_opt.flag |= PJSUA_CALL_UNHOLD;
1620 pjsua_call_reinvite2(current_call, &call_opt, NULL);
1621
1622 } else {
1623 PJ_LOG(3,(THIS_FILE, "No current call"));
1624 }
1625 return PJ_SUCCESS;
1626}
1627
1628/* Send update */
1629static pj_status_t cmd_call_update()
1630{
1631 if (current_call != PJSUA_INVALID_ID) {
1632 pjsua_call_update2(current_call, &call_opt, NULL);
1633 } else {
1634 PJ_LOG(3,(THIS_FILE, "No current call"));
1635 }
1636 return PJ_SUCCESS;
1637}
1638
1639/* Select next call */
1640static pj_status_t cmd_next_call(pj_bool_t next)
1641{
1642 /*
1643 * Cycle next/prev dialog.
1644 */
1645 if (next) {
1646 find_next_call(current_call);
1647 } else {
1648 find_prev_call(current_call);
1649 }
1650
1651 if (current_call != PJSUA_INVALID_ID) {
1652 pjsua_call_info call_info;
1653
1654 pjsua_call_get_info(current_call, &call_info);
1655 PJ_LOG(3,(THIS_FILE,"Current dialog: %.*s",
1656 (int)call_info.remote_info.slen,
1657 call_info.remote_info.ptr));
1658
1659 } else {
1660 PJ_LOG(3,(THIS_FILE,"No current dialog"));
1661 }
1662 return PJ_SUCCESS;
1663}
1664
1665/* Transfer call */
1666static pj_status_t cmd_transfer_call(pj_cli_cmd_val *cval)
1667{
1668 if (current_call == PJSUA_INVALID_ID) {
1669
1670 PJ_LOG(3,(THIS_FILE, "No current call"));
1671
1672 } else {
1673 char out_str[64];
1674 int call = current_call;
1675 char dest[64] = {0};
1676 pj_str_t tmp = pj_str(dest);
1677 struct input_result result;
1678 pjsip_generic_string_hdr refer_sub;
1679 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
1680 pj_str_t STR_FALSE = { "false", 5 };
1681 pjsua_call_info ci;
1682
1683 pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(dest));
1684
1685 pjsua_call_get_info(current_call, &ci);
1686 pj_ansi_snprintf(out_str,
1687 sizeof(out_str),
1688 "Transfering current call [%d] %.*s\n",
1689 current_call,
1690 (int)ci.remote_info.slen,
1691 ci.remote_info.ptr);
1692
1693 get_input_url(tmp.ptr, tmp.slen, cval, &result);
1694
1695 /* Check if call is still there. */
1696
1697 if (call != current_call) {
1698 puts("Call has been disconnected");
1699 return PJ_SUCCESS;
1700 }
1701
1702 pjsua_msg_data_init(&msg_data);
1703 if (app_config.no_refersub) {
1704 /* Add Refer-Sub: false in outgoing REFER request */
1705 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
1706 &STR_FALSE);
1707 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
1708 }
1709 if (result.nb_result != PJSUA_APP_NO_NB) {
1710 if (result.nb_result == -1 || result.nb_result == 0) {
1711 static const pj_str_t err_msg = {"You can't do that with "
1712 "transfer call!\n", 39};
1713
1714 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1715 } else {
1716 pjsua_buddy_info binfo;
1717 pjsua_buddy_get_info(result.nb_result-1, &binfo);
1718 pjsua_call_xfer( current_call, &binfo.uri, &msg_data);
1719 }
1720 } else if (result.uri_result) {
1721 pj_str_t tmp;
1722 tmp = pj_str(result.uri_result);
1723 pjsua_call_xfer( current_call, &tmp, &msg_data);
1724 }
1725 }
1726 return PJ_SUCCESS;
1727}
1728
1729/* Transfer call */
1730static pj_status_t cmd_transfer_replace_call(pj_cli_cmd_val *cval)
1731{
1732 if (current_call == -1) {
1733 PJ_LOG(3,(THIS_FILE, "No current call"));
1734 } else {
1735 int call = current_call;
1736 int dst_call;
1737 pjsip_generic_string_hdr refer_sub;
1738 pj_str_t STR_REFER_SUB = { "Refer-Sub", 9 };
1739 pj_str_t STR_FALSE = { "false", 5 };
1740 pjsua_call_id ids[PJSUA_MAX_CALLS];
1741 pjsua_msg_data msg_data;
1742 char buf[8] = {0};
1743 pj_str_t tmp = pj_str(buf);
1744 unsigned count;
1745 static const pj_str_t err_invalid_num =
1746 {"Invalid destination call number\n", 32 };
1747 count = PJ_ARRAY_SIZE(ids);
1748 pjsua_enum_calls(ids, &count);
1749
1750 if (count <= 1) {
1751 static const pj_str_t err_no_other_call =
1752 {"There are no other calls\n", 25};
1753
1754 pj_cli_sess_write_msg(cval->sess, err_no_other_call.ptr,
1755 err_no_other_call.slen);
1756 return PJ_SUCCESS;
1757 }
1758
1759 pj_strncpy_with_null(&tmp, &cval->argv[1], sizeof(buf));
1760 dst_call = my_atoi(tmp.ptr);
1761
1762 /* Check if call is still there. */
1763 if (call != current_call) {
1764 static pj_str_t err_call_dc =
1765 {"Call has been disconnected\n", 27};
1766
1767 pj_cli_sess_write_msg(cval->sess, err_call_dc.ptr,
1768 err_call_dc.slen);
1769 return PJ_SUCCESS;
1770 }
1771
1772 /* Check that destination call is valid. */
1773 if (dst_call == call) {
1774 static pj_str_t err_same_num =
1775 {"Destination call number must not be the "
1776 "same as the call being transfered\n", 74};
1777
1778 pj_cli_sess_write_msg(cval->sess, err_same_num.ptr,
1779 err_same_num.slen);
1780 return PJ_SUCCESS;
1781 }
1782
1783 if (dst_call >= PJSUA_MAX_CALLS) {
1784 pj_cli_sess_write_msg(cval->sess, err_invalid_num.ptr,
1785 err_invalid_num.slen);
1786 return PJ_SUCCESS;
1787 }
1788
1789 if (!pjsua_call_is_active(dst_call)) {
1790 pj_cli_sess_write_msg(cval->sess, err_invalid_num.ptr,
1791 err_invalid_num.slen);
1792 return PJ_SUCCESS;
1793 }
1794
1795 pjsua_msg_data_init(&msg_data);
1796 if (app_config.no_refersub) {
1797 /* Add Refer-Sub: false in outgoing REFER request */
1798 pjsip_generic_string_hdr_init2(&refer_sub, &STR_REFER_SUB,
1799 &STR_FALSE);
1800 pj_list_push_back(&msg_data.hdr_list, &refer_sub);
1801 }
1802
1803 pjsua_call_xfer_replaces(call, dst_call,
1804 PJSUA_XFER_NO_REQUIRE_REPLACES,
1805 &msg_data);
1806 }
1807 return PJ_SUCCESS;
1808}
1809
1810static pj_status_t cmd_redirect_call(pj_cli_cmd_val *cval)
1811{
1812 if (current_call == PJSUA_INVALID_ID) {
1813 PJ_LOG(3,(THIS_FILE, "No current call"));
1814 return PJ_SUCCESS;
1815 }
1816 if (!pjsua_call_is_active(current_call)) {
1817 PJ_LOG(1,(THIS_FILE, "Call %d has gone", current_call));
1818 } else {
1819 enum {
1820 ACCEPT_REPLACE, ACCEPT, REJECT, STOP
1821 };
1822 int choice = pj_strtol(&cval->argv[1]);
1823
1824 switch (choice) {
1825 case ACCEPT_REPLACE:
1826 pjsua_call_process_redirect(current_call,
1827 PJSIP_REDIRECT_ACCEPT_REPLACE);
1828 break;
1829 case ACCEPT:
1830 pjsua_call_process_redirect(current_call, PJSIP_REDIRECT_ACCEPT);
1831 break;
1832 case REJECT:
1833 pjsua_call_process_redirect(current_call, PJSIP_REDIRECT_REJECT);
1834 break;
1835 default:
1836 pjsua_call_process_redirect(current_call, PJSIP_REDIRECT_STOP);
1837 break;
1838 }
1839 }
1840 return PJ_SUCCESS;
1841}
1842
1843/* Send DTMF (RFC2833) */
1844static pj_status_t cmd_dtmf_2833(pj_cli_cmd_val *cval)
1845{
1846 if (current_call == PJSUA_INVALID_ID) {
1847
1848 PJ_LOG(3,(THIS_FILE, "No current call"));
1849
1850 } else if (!pjsua_call_has_media(current_call)) {
1851
1852 PJ_LOG(3,(THIS_FILE, "Media is not established yet!"));
1853
1854 } else {
1855 int call = current_call;
1856 pj_status_t status;
1857
1858 if (call != current_call) {
1859 static const pj_str_t err_msg = {"Call has been disconnected\n",
1860 28};
1861 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1862 return PJ_SUCCESS;;
1863 }
1864
1865 status = pjsua_call_dial_dtmf(current_call, &cval->argv[1]);
1866 if (status != PJ_SUCCESS) {
1867 pjsua_perror(THIS_FILE, "Unable to send DTMF", status);
1868 } else {
1869 static const pj_str_t msg = {"DTMF digits enqueued "
1870 "for transmission\n", 39};
1871 pj_cli_sess_write_msg(cval->sess, msg.ptr, msg.slen);
1872 }
1873 }
1874 return PJ_SUCCESS;
1875}
1876
1877/* Send DTMF with SIP Info */
1878static pj_status_t cmd_call_info(pj_cli_cmd_val *cval)
1879{
1880 if (current_call == PJSUA_INVALID_ID) {
1881
1882 PJ_LOG(3,(THIS_FILE, "No current call"));
1883
1884 } else {
1885 const pj_str_t SIP_INFO = pj_str("INFO");
1886 int call = current_call;
1887 int i;
1888 pj_status_t status;
1889
1890 if (call != current_call) {
1891 static const pj_str_t err_msg = {"Call has been disconnected\n",
1892 28};
1893 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1894 return PJ_SUCCESS;;
1895 }
1896
1897 for (i=0; i<cval->argv[1].slen; ++i) {
1898 char body[64];
1899
1900 pjsua_msg_data_init(&msg_data);
1901 msg_data.content_type = pj_str("application/dtmf-relay");
1902
1903 pj_ansi_snprintf(body,
1904 sizeof(body),
1905 "Signal=%c\n"
1906 "Duration=160",
1907 cval->argv[1].ptr[i]);
1908
1909 msg_data.msg_body = pj_str(body);
1910
1911 status = pjsua_call_send_request(current_call, &SIP_INFO,
1912 &msg_data);
1913 if (status != PJ_SUCCESS) {
1914 break;
1915 }
1916 }
1917 }
1918 return PJ_SUCCESS;
1919}
1920
1921/* Dump call quality */
1922static pj_status_t cmd_call_quality()
1923{
1924 if (current_call != PJSUA_INVALID_ID) {
1925 log_call_dump(current_call);
1926 } else {
1927 PJ_LOG(3,(THIS_FILE, "No current call"));
1928 }
1929 return PJ_SUCCESS;
1930}
1931
1932/* Send arbitrary request */
1933static pj_status_t cmd_send_arbitrary(pj_cli_cmd_val *cval)
1934{
1935 if (pjsua_acc_get_count() == 0) {
1936 static const pj_str_t err_msg = {"Sorry, need at least one "
1937 "account configured\n", 45};
1938 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1939 } else {
1940 char *uri;
1941 char dest[64] = {0};
1942 pj_str_t tmp = pj_str(dest);
1943 struct input_result result;
1944 static const pj_str_t header = {"Send arbitrary request to "
1945 "remote host\n", 39};
1946
1947 pj_cli_sess_write_msg(cval->sess, header.ptr, header.slen);
1948
1949 pj_strncpy_with_null(&tmp, &cval->argv[2], sizeof(dest));
1950 /* Input destination URI */
1951 uri = NULL;
1952 get_input_url(tmp.ptr, tmp.slen, cval, &result);
1953 if (result.nb_result != PJSUA_APP_NO_NB) {
1954 if (result.nb_result == -1) {
1955 static const pj_str_t err_msg = {"Sorry you can't do that!\n",
1956 26};
1957 pj_cli_sess_write_msg(cval->sess, err_msg.ptr, err_msg.slen);
1958 return PJ_SUCCESS;
1959 } else if (result.nb_result == 0) {
1960 uri = NULL;
1961 if (current_call == PJSUA_INVALID_ID) {
1962 static const pj_str_t err_msg = {"No current call\n",
1963 17};
1964 pj_cli_sess_write_msg(cval->sess, err_msg.ptr,
1965 err_msg.slen);
1966
1967 return PJ_SUCCESS;
1968 }
1969 } else {
1970 pjsua_buddy_info binfo;
1971 pjsua_buddy_get_info(result.nb_result-1, &binfo);
1972 pj_strncpy_with_null(&tmp, &binfo.uri, sizeof(dest));
1973 uri = tmp.ptr;
1974 }
1975 } else if (result.uri_result) {
1976 uri = result.uri_result;
1977 } else {
1978 return PJ_SUCCESS;;
1979 }
1980
1981 if (uri) {
1982 char method[64] = {0};
1983 pj_str_t tmp_method = pj_str(method);
1984 pj_strncpy_with_null(&tmp_method, &cval->argv[1], sizeof(method));
1985 tmp = pj_str(uri);
1986 send_request(method, &tmp);
1987 } else {
1988 /* If you send call control request using this method
1989 * (such requests includes BYE, CANCEL, etc.), it will
1990 * not go well with the call state, so don't do it
1991 * unless it's for testing.
1992 */
1993 pjsua_call_send_request(current_call, &cval->argv[1], NULL);
1994 }
1995 }
1996 return PJ_SUCCESS;
1997}
1998
1999static pj_status_t cmd_show_current_call(pj_cli_cmd_val *cval)
2000{
2001 char out_str[128];
2002 int i = pjsua_call_get_count();
2003 pj_ansi_snprintf(out_str, sizeof(out_str),
2004 "You have %d active call%s\n", i, (i>1?"s":""));
2005
2006 pj_cli_sess_write_msg(cval->sess, out_str,
2007 pj_ansi_strlen(out_str));
2008
2009 if (current_call != PJSUA_INVALID_ID) {
2010 pjsua_call_info ci;
2011 if (pjsua_call_get_info(current_call, &ci)==PJ_SUCCESS) {
2012 pj_ansi_snprintf(out_str, sizeof(out_str),
2013 "Current call id=%d to %.*s [%.*s]\n", current_call,
2014 (int)ci.remote_info.slen, ci.remote_info.ptr,
2015 (int)ci.state_text.slen, ci.state_text.ptr);
2016
2017 pj_cli_sess_write_msg(cval->sess, out_str,
2018 pj_ansi_strlen(out_str));
2019 }
2020 }
2021 return PJ_SUCCESS;
2022}
2023
2024/* Call handler */
2025pj_status_t cmd_call_handler(pj_cli_cmd_val *cval)
2026{
2027 pj_status_t status = PJ_SUCCESS;
2028 pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd);
2029
2030 CHECK_PJSUA_RUNNING();
2031
2032 switch(cmd_id) {
2033 case CMD_CALL_NEW:
2034 status = cmd_make_single_call(cval);
2035 break;
2036 case CMD_CALL_MULTI:
2037 status = cmd_make_multi_call(cval);
2038 break;
2039 case CMD_CALL_ANSWER:
2040 status = cmd_answer_call(cval);
2041 break;
2042 case CMD_CALL_HANGUP:
2043 case CMD_CALL_HANGUP_ALL:
2044 status = cmd_hangup_call(cval, (cmd_id==CMD_CALL_HANGUP_ALL));
2045 break;
2046 case CMD_CALL_HOLD:
2047 status = cmd_hold_call();
2048 break;
2049 case CMD_CALL_REINVITE:
2050 status = cmd_call_reinvite();
2051 break;
2052 case CMD_CALL_UPDATE:
2053 status = cmd_call_update();
2054 break;
2055 case CMD_CALL_NEXT:
2056 case CMD_CALL_PREVIOUS:
2057 status = cmd_next_call(cmd_id==CMD_CALL_NEXT);
2058 break;
2059 case CMD_CALL_TRANSFER:
2060 status = cmd_transfer_call(cval);
2061 break;
2062 case CMD_CALL_TRANSFER_REPLACE:
2063 status = cmd_transfer_replace_call(cval);
2064 break;
2065 case CMD_CALL_REDIRECT:
2066 status = cmd_redirect_call(cval);
2067 break;
2068 case CMD_CALL_D2833:
2069 status = cmd_dtmf_2833(cval);
2070 break;
2071 case CMD_CALL_INFO:
2072 status = cmd_call_info(cval);
2073 break;
2074 case CMD_CALL_DUMP_Q:
2075 status = cmd_call_quality();
2076 break;
2077 case CMD_CALL_SEND_ARB:
2078 status = cmd_send_arbitrary(cval);
2079 break;
2080 case CMD_CALL_LIST:
2081 status = cmd_show_current_call(cval);
2082 break;
2083 }
2084
2085 return status;
2086}
2087
2088#if PJSUA_HAS_VIDEO
2089static pj_status_t cmd_set_video_enable(pj_bool_t enabled)
2090{
2091 app_config.vid.vid_cnt = (enabled ? 1 : 0);
2092 PJ_LOG(3,(THIS_FILE, "Video will be %s in next offer/answer",
2093 (enabled?"enabled":"disabled")));
2094
2095 return PJ_SUCCESS;
2096}
2097
2098static pj_status_t modify_video_account(pjsua_acc_config *acc_cfg)
2099{
2100 pj_status_t status = pjsua_acc_modify(current_acc, acc_cfg);
2101 if (status != PJ_SUCCESS)
2102 PJ_PERROR(1,(THIS_FILE, status, "Error modifying account %d",
2103 current_acc));
2104
2105 return status;
2106}
2107
2108static pj_status_t cmd_show_account_video()
2109{
2110 pjsua_acc_config acc_cfg;
2111
2112 pjsua_acc_get_config(current_acc, &acc_cfg);
2113 app_config_show_video(current_acc, &acc_cfg);
2114 return PJ_SUCCESS;
2115}
2116
2117static pj_status_t cmd_video_acc_handler(pj_cli_cmd_val *cval)
2118{
2119 pjsua_acc_config acc_cfg;
2120 pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd);
2121
2122 CHECK_PJSUA_RUNNING();
2123
2124 pjsua_acc_get_config(current_acc, &acc_cfg);
2125
2126 switch(cmd_id) {
2127 case CMD_VIDEO_ACC_AUTORX:
2128 case CMD_VIDEO_ACC_AUTOTX:
2129 {
2130 int on = (pj_ansi_strnicmp(cval->argv[1].ptr, "On", 2)==0);
2131
2132 if (cmd_id == CMD_VIDEO_ACC_AUTORX)
2133 acc_cfg.vid_in_auto_show = on;
2134 else
2135 acc_cfg.vid_out_auto_transmit = on;
2136 }
2137 break;
2138 case CMD_VIDEO_ACC_CAP_ID:
2139 case CMD_VIDEO_ACC_REN_ID:
2140 {
2141 int dev = pj_strtol(&cval->argv[1]);
2142
2143 if (cmd_id == CMD_VIDEO_ACC_CAP_ID)
2144 acc_cfg.vid_cap_dev = dev;
2145 else
2146 acc_cfg.vid_rend_dev = dev;
2147 }
2148 break;
2149 }
2150 modify_video_account(&acc_cfg);
2151 return PJ_SUCCESS;
2152}
2153
2154static pj_status_t cmd_add_vid_strm()
2155{
2156 return pjsua_call_set_vid_strm(current_call,
2157 PJSUA_CALL_VID_STRM_ADD, NULL);
2158}
2159
2160static pj_status_t cmd_enable_vid_rx(pj_cli_cmd_val *cval)
2161{
2162 pjsua_call_vid_strm_op_param param;
2163 pjsua_stream_info si;
2164 pj_status_t status = PJ_SUCCESS;
2165 pj_bool_t on = (pj_ansi_strnicmp(cval->argv[1].ptr, "On", 2) == 0);
2166
2167 pjsua_call_vid_strm_op_param_default(&param);
2168
2169 param.med_idx = pj_strtol(&cval->argv[2]);
2170 if (pjsua_call_get_stream_info(current_call, param.med_idx, &si) ||
2171 si.type != PJMEDIA_TYPE_VIDEO)
2172 {
2173 PJ_PERROR(1,(THIS_FILE, PJ_EINVAL, "Invalid stream"));
2174 return status;
2175 }
2176
2177 if (on) param.dir = (si.info.vid.dir | PJMEDIA_DIR_DECODING);
2178 else param.dir = (si.info.vid.dir & PJMEDIA_DIR_ENCODING);
2179
2180 status = pjsua_call_set_vid_strm(current_call,
2181 PJSUA_CALL_VID_STRM_CHANGE_DIR,
2182 &param);
2183 return status;
2184}
2185
2186static pj_status_t cmd_enable_vid_tx(pj_cli_cmd_val *cval)
2187{
2188 pjsua_call_vid_strm_op_param param;
2189 pj_status_t status = PJ_SUCCESS;
2190 pj_bool_t on = (pj_ansi_strnicmp(cval->argv[1].ptr, "On", 2) == 0);
2191
2192 pjsua_call_vid_strm_op op = on? PJSUA_CALL_VID_STRM_START_TRANSMIT :
2193 PJSUA_CALL_VID_STRM_STOP_TRANSMIT;
2194
2195 pjsua_call_vid_strm_op_param_default(&param);
2196
2197 param.med_idx = pj_strtol(&cval->argv[2]);
2198
2199 status = pjsua_call_set_vid_strm(current_call, op, &param);
2200 return status;
2201}
2202
2203static pj_status_t cmd_enable_vid_stream(pj_cli_cmd_val *cval,
2204 pj_bool_t enable)
2205{
2206 pjsua_call_vid_strm_op_param param;
2207 pjsua_call_vid_strm_op op = enable? PJSUA_CALL_VID_STRM_CHANGE_DIR :
2208 PJSUA_CALL_VID_STRM_REMOVE;
2209
2210 pjsua_call_vid_strm_op_param_default(&param);
2211
2212 param.med_idx = cval->argc > 1 ? pj_strtol(&cval->argv[1]) : -1;
2213 param.dir = PJMEDIA_DIR_ENCODING_DECODING;
2214 return pjsua_call_set_vid_strm(current_call, op, &param);
2215}
2216
2217static pj_status_t cmd_set_cap_dev_id(pj_cli_cmd_val *cval)
2218{
2219 pjsua_call_vid_strm_op_param param;
2220
2221 pjsua_call_vid_strm_op_param_default(&param);
2222 param.med_idx = cval->argc > 1? pj_strtol(&cval->argv[1]) : -1;
2223 param.cap_dev = cval->argc > 2? pj_strtol(&cval->argv[2]) :
2224 PJMEDIA_VID_DEFAULT_CAPTURE_DEV;
2225
2226 return pjsua_call_set_vid_strm(current_call,
2227 PJSUA_CALL_VID_STRM_CHANGE_CAP_DEV,
2228 &param);
2229}
2230
2231static pj_status_t cmd_list_vid_dev()
2232{
2233 vid_list_devs();
2234 return PJ_SUCCESS;
2235}
2236
2237static pj_status_t cmd_vid_device_refresh()
2238{
2239 pjmedia_vid_dev_refresh();
2240 return PJ_SUCCESS;
2241}
2242
2243static pj_status_t cmd_vid_device_preview(pj_cli_cmd_val *cval)
2244{
2245 int dev_id = pj_strtol(&cval->argv[2]);
2246 pj_bool_t on = (pj_ansi_strnicmp(cval->argv[1].ptr, "On", 2) == 0);
2247
2248 if (on) {
2249 pjsua_vid_preview_param param;
2250
2251 pjsua_vid_preview_param_default(&param);
2252 param.wnd_flags = PJMEDIA_VID_DEV_WND_BORDER |
2253 PJMEDIA_VID_DEV_WND_RESIZABLE;
2254 pjsua_vid_preview_start(dev_id, &param);
2255 arrange_window(pjsua_vid_preview_get_win(dev_id));
2256 } else {
2257 pjsua_vid_win_id wid;
2258 wid = pjsua_vid_preview_get_win(dev_id);
2259 if (wid != PJSUA_INVALID_ID) {
2260 /* Preview window hiding once it is stopped is
2261 * responsibility of app */
2262 pjsua_vid_win_set_show(wid, PJ_FALSE);
2263 pjsua_vid_preview_stop(dev_id);
2264 }
2265 }
2266 return PJ_SUCCESS;
2267}
2268
2269static pj_status_t cmd_vid_codec_list()
2270{
2271 pjsua_codec_info ci[PJMEDIA_CODEC_MGR_MAX_CODECS];
2272 unsigned count = PJ_ARRAY_SIZE(ci);
2273 pj_status_t status = pjsua_vid_enum_codecs(ci, &count);
2274 if (status != PJ_SUCCESS) {
2275 PJ_PERROR(1,(THIS_FILE, status, "Error enumerating codecs"));
2276 } else {
2277 unsigned i;
2278 PJ_LOG(3,(THIS_FILE, "Found %d video codecs:", count));
2279 PJ_LOG(3,(THIS_FILE, "codec id prio fps bw(kbps) size"));
2280 PJ_LOG(3,(THIS_FILE, "------------------------------------------"));
2281 for (i=0; i<count; ++i) {
2282 pjmedia_vid_codec_param cp;
2283 pjmedia_video_format_detail *vfd;
2284
2285 status = pjsua_vid_codec_get_param(&ci[i].codec_id, &cp);
2286 if (status != PJ_SUCCESS)
2287 continue;
2288
2289 vfd = pjmedia_format_get_video_format_detail(&cp.enc_fmt,
2290 PJ_TRUE);
2291 PJ_LOG(3,(THIS_FILE, "%.*s%.*s %3d %7.2f %4d/%4d %dx%d",
2292 (int)ci[i].codec_id.slen, ci[i].codec_id.ptr,
2293 13-(int)ci[i].codec_id.slen, " ",
2294 ci[i].priority,
2295 (vfd->fps.num*1.0/vfd->fps.denum),
2296 vfd->avg_bps/1000, vfd->max_bps/1000,
2297 vfd->size.w, vfd->size.h));
2298 }
2299 }
2300 return PJ_SUCCESS;
2301}
2302
2303static pj_status_t cmd_set_vid_codec_prio(pj_cli_cmd_val *cval)
2304{
2305 int prio = pj_strtol(&cval->argv[2]);
2306 pj_status_t status;
2307
2308 status = pjsua_vid_codec_set_priority(&cval->argv[1], (pj_uint8_t)prio);
2309 if (status != PJ_SUCCESS)
2310 PJ_PERROR(1,(THIS_FILE, status, "Set codec priority error"));
2311
2312 return PJ_SUCCESS;
2313}
2314
2315static pj_status_t cmd_set_vid_codec_fps(pj_cli_cmd_val *cval)
2316{
2317 pjmedia_vid_codec_param cp;
2318 int M, N;
2319 pj_status_t status;
2320
2321 M = pj_strtol(&cval->argv[2]);
2322 N = pj_strtol(&cval->argv[3]);
2323 status = pjsua_vid_codec_get_param(&cval->argv[1], &cp);
2324 if (status == PJ_SUCCESS) {
2325 cp.enc_fmt.det.vid.fps.num = M;
2326 cp.enc_fmt.det.vid.fps.denum = N;
2327 status = pjsua_vid_codec_set_param(&cval->argv[1], &cp);
2328 }
2329 if (status != PJ_SUCCESS)
2330 PJ_PERROR(1,(THIS_FILE, status, "Set codec framerate error"));
2331
2332 return PJ_SUCCESS;
2333}
2334
2335static pj_status_t cmd_set_vid_codec_bitrate(pj_cli_cmd_val *cval)
2336{
2337 pjmedia_vid_codec_param cp;
2338 int M, N;
2339 pj_status_t status;
2340
2341 M = pj_strtol(&cval->argv[2]);
2342 N = pj_strtol(&cval->argv[3]);
2343 status = pjsua_vid_codec_get_param(&cval->argv[1], &cp);
2344 if (status == PJ_SUCCESS) {
2345 cp.enc_fmt.det.vid.avg_bps = M * 1000;
2346 cp.enc_fmt.det.vid.max_bps = N * 1000;
2347 status = pjsua_vid_codec_set_param(&cval->argv[1], &cp);
2348 }
2349 if (status != PJ_SUCCESS)
2350 PJ_PERROR(1,(THIS_FILE, status, "Set codec bitrate error"));
2351
2352 return status;
2353}
2354
2355static pj_status_t cmd_set_vid_codec_size(pj_cli_cmd_val *cval)
2356{
2357 pjmedia_vid_codec_param cp;
2358 int M, N;
2359 pj_status_t status;
2360
2361 M = pj_strtol(&cval->argv[2]);
2362 N = pj_strtol(&cval->argv[3]);
2363 status = pjsua_vid_codec_get_param(&cval->argv[1], &cp);
2364 if (status == PJ_SUCCESS) {
2365 cp.enc_fmt.det.vid.size.w = M;
2366 cp.enc_fmt.det.vid.size.h = N;
2367 status = pjsua_vid_codec_set_param(&cval->argv[1], &cp);
2368 }
2369 if (status != PJ_SUCCESS)
2370 PJ_PERROR(1,(THIS_FILE, status, "Set codec size error"));
2371
2372 return status;
2373}
2374
2375static pj_status_t cmd_vid_win_list()
2376{
2377 pjsua_vid_win_id wids[PJSUA_MAX_VID_WINS];
2378 unsigned i, cnt = PJ_ARRAY_SIZE(wids);
2379
2380 pjsua_vid_enum_wins(wids, &cnt);
2381
2382 PJ_LOG(3,(THIS_FILE, "Found %d video windows:", cnt));
2383 PJ_LOG(3,(THIS_FILE, "WID show pos size"));
2384 PJ_LOG(3,(THIS_FILE, "------------------------------"));
2385 for (i = 0; i < cnt; ++i) {
2386 pjsua_vid_win_info wi;
2387 pjsua_vid_win_get_info(wids[i], &wi);
2388 PJ_LOG(3,(THIS_FILE, "%3d %c (%d,%d) %dx%d",
2389 wids[i], (wi.show?'Y':'N'), wi.pos.x, wi.pos.y,
2390 wi.size.w, wi.size.h));
2391 }
2392 return PJ_SUCCESS;
2393}
2394
2395static pj_status_t cmd_arrange_vid_win()
2396{
2397 arrange_window(PJSUA_INVALID_ID);
2398 return PJ_SUCCESS;
2399}
2400
2401static pj_status_t cmd_show_vid_win(pj_cli_cmd_val *cval, pj_bool_t show)
2402{
2403 pjsua_vid_win_id wid = pj_strtol(&cval->argv[1]);
2404 return pjsua_vid_win_set_show(wid, show);
2405}
2406
2407static pj_status_t cmd_move_vid_win(pj_cli_cmd_val *cval)
2408{
2409 pjsua_vid_win_id wid = pj_strtol(&cval->argv[1]);
2410 pjmedia_coord pos;
2411
2412 pos.x = pj_strtol(&cval->argv[2]);
2413 pos.y = pj_strtol(&cval->argv[3]);
2414 return pjsua_vid_win_set_pos(wid, &pos);
2415}
2416
2417static pj_status_t cmd_resize_vid_win(pj_cli_cmd_val *cval)
2418{
2419 pjsua_vid_win_id wid = pj_strtol(&cval->argv[1]);
2420 pjmedia_rect_size size;
2421
2422 size.w = pj_strtol(&cval->argv[2]);
2423 size.h = pj_strtol(&cval->argv[3]);
2424 return pjsua_vid_win_set_size(wid, &size);
2425}
2426
2427/* Video handler */
2428static pj_status_t cmd_video_handler(pj_cli_cmd_val *cval)
2429{
2430 pj_status_t status = PJ_SUCCESS;
2431 pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd);
2432
2433 CHECK_PJSUA_RUNNING();
2434
2435 switch(cmd_id) {
2436 case CMD_VIDEO_ENABLE:
2437 status = cmd_set_video_enable(PJ_TRUE);
2438 break;
2439 case CMD_VIDEO_DISABLE:
2440 status = cmd_set_video_enable(PJ_FALSE);
2441 break;
2442 case CMD_VIDEO_ACC_SHOW:
2443 status = cmd_show_account_video();
2444 break;
2445 case CMD_VIDEO_ACC_AUTORX:
2446 case CMD_VIDEO_ACC_AUTOTX:
2447 case CMD_VIDEO_ACC_CAP_ID:
2448 case CMD_VIDEO_ACC_REN_ID:
2449 status = cmd_video_acc_handler(cval);
2450 break;
2451 case CMD_VIDEO_CALL_ADD:
2452 status = cmd_add_vid_strm();
2453 break;
2454 case CMD_VIDEO_CALL_RX:
2455 status = cmd_enable_vid_rx(cval);
2456 break;
2457 case CMD_VIDEO_CALL_TX:
2458 status = cmd_enable_vid_tx(cval);
2459 break;
2460 case CMD_VIDEO_CALL_ENABLE:
2461 case CMD_VIDEO_CALL_DISABLE:
2462 status = cmd_enable_vid_stream(cval, (cmd_id==CMD_VIDEO_CALL_ENABLE));
2463 break;
2464 case CMD_VIDEO_CALL_CAP:
2465 status = cmd_set_cap_dev_id(cval);
2466 break;
2467 case CMD_VIDEO_DEVICE_LIST:
2468 status = cmd_list_vid_dev();
2469 break;
2470 case CMD_VIDEO_DEVICE_REFRESH:
2471 status = cmd_vid_device_refresh();
2472 break;
2473 case CMD_VIDEO_DEVICE_PREVIEW:
2474 status = cmd_vid_device_preview(cval);
2475 break;
2476 case CMD_VIDEO_CODEC_LIST:
2477 status = cmd_vid_codec_list();
2478 break;
2479 case CMD_VIDEO_CODEC_PRIO:
2480 status = cmd_set_vid_codec_prio(cval);
2481 break;
2482 case CMD_VIDEO_CODEC_FPS:
2483 status = cmd_set_vid_codec_fps(cval);
2484 break;
2485 case CMD_VIDEO_CODEC_BITRATE:
2486 status = cmd_set_vid_codec_bitrate(cval);
2487 break;
2488 case CMD_VIDEO_CODEC_SIZE:
2489 status = cmd_set_vid_codec_size(cval);
2490 break;
2491 case CMD_VIDEO_WIN_LIST:
2492 status = cmd_vid_win_list();
2493 break;
2494 case CMD_VIDEO_WIN_ARRANGE:
2495 status = cmd_arrange_vid_win();
2496 break;
2497 case CMD_VIDEO_WIN_SHOW:
2498 case CMD_VIDEO_WIN_HIDE:
2499 status = cmd_show_vid_win(cval, (cmd_id==CMD_VIDEO_WIN_SHOW));
2500 break;
2501 case CMD_VIDEO_WIN_MOVE:
2502 status = cmd_move_vid_win(cval);
2503 break;
2504 case CMD_VIDEO_WIN_RESIZE:
2505 status = cmd_resize_vid_win(cval);
2506 break;
2507 }
2508
2509 return status;
2510}
2511#endif
2512
2513/* Other command handler */
2514static pj_status_t cmd_sleep_handler(pj_cli_cmd_val *cval)
2515{
2516 int delay;
2517
2518 delay = pj_strtoul(&cval->argv[1]);
2519 if (delay < 0) delay = 0;
2520 pj_thread_sleep(delay);
2521
2522 return PJ_SUCCESS;
2523}
2524
2525static pj_status_t cmd_network_handler(pj_cli_cmd_val *cval)
2526{
2527 pj_status_t status = PJ_SUCCESS;
2528 PJ_UNUSED_ARG(cval);
2529
2530 CHECK_PJSUA_RUNNING();
2531
2532 status = pjsua_detect_nat_type();
2533 if (status != PJ_SUCCESS)
2534 pjsua_perror(THIS_FILE, "Error", status);
2535
2536 return status;
2537}
2538
2539static pj_status_t cmd_quit_handler(pj_cli_cmd_val *cval)
2540{
2541 PJ_LOG(3,(THIS_FILE, "Quitting app.."));
2542 pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_FALSE);
2543
2544 /* Invoke CLI stop callback (defined in pjsua_app.c) */
2545 cli_on_stopped(PJ_FALSE, 0, NULL);
2546
2547 return PJ_SUCCESS;
2548}
2549
2550/*
2551 * Syntax error handler for parser.
2552 */
2553static void on_syntax_error(pj_scanner *scanner)
2554{
2555 PJ_UNUSED_ARG(scanner);
2556 PJ_THROW(PJ_EINVAL);
2557}
2558
2559/*
2560 * This method will parse buffer string info array of argument string
2561 * @argc On input, maximum array size of argument. On output, number of argument
2562 * parsed
2563 * @argv Array of argument string
2564 */
2565static pj_status_t get_options(pj_str_t *options, unsigned *argc,
2566 pj_str_t argv[])
2567{
2568 pj_scanner scanner;
2569 unsigned max_argc = *argc;
2570
2571 PJ_USE_EXCEPTION;
2572
2573 if (!options)
2574 return PJ_SUCCESS;
2575
2576 pj_scan_init(&scanner, options->ptr, options->slen, PJ_SCAN_AUTOSKIP_WS,
2577 &on_syntax_error);
2578 PJ_TRY {
2579 *argc = 0;
2580 while (!pj_scan_is_eof(&scanner) && (max_argc > *argc)) {
2581 pj_str_t str;
2582
2583 pj_scan_get_until_chr(&scanner, " \t\r\n", &str);
2584 argv[*argc] = str;
2585 ++(*argc);
2586 }
2587 }
2588 PJ_CATCH_ANY {
2589 pj_scan_fini(&scanner);
2590 return PJ_GET_EXCEPTION();
2591 }
2592 PJ_END;
2593 return PJ_SUCCESS;
2594}
2595
2596static pj_status_t cmd_restart_handler(pj_cli_cmd_val *cval)
2597{
2598 enum { MAX_ARGC = 64 };
2599 int i;
2600 unsigned argc = 1;
2601 static char argv_buffer[PJ_CLI_MAX_CMDBUF];
2602 static char *argv[MAX_ARGC] = {NULL};
2603 char *pbuf = argv_buffer;
2604
2605 PJ_LOG(3,(THIS_FILE, "Restarting app.."));
2606 pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_TRUE);
2607
2608 /** Get the pjsua option **/
2609 for (i=1; i < cval->argc; i++) {
2610 pj_str_t argvst[MAX_ARGC];
2611 unsigned j, ac;
2612
2613 ac = MAX_ARGC - argc;
2614 get_options(&cval->argv[i], &ac, argvst);
2615 for (j = 0; j < ac; j++) {
2616 pj_ansi_strncpy(pbuf, argvst[j].ptr, argvst[j].slen);
2617 pbuf[argvst[j].slen] = '\0';
2618 argv[argc + j] = pbuf;
2619 pbuf += argvst[j].slen + 1;
2620 }
2621 argc += ac;
2622 }
2623
2624 /* Invoke CLI stop callback (defined in pjsua_app.c) */
2625 cli_on_stopped(PJ_TRUE, argc, (char**)argv);
2626
2627 return PJ_SUCCESS;
2628}
2629
2630static pj_status_t add_call_command(pj_cli_t *cli)
2631{
2632 char* call_command =
2633 "<CMD name='call' id='100' desc='Call related commands'>"
2634 " <CMD name='new' id='1001' desc='Make a new call/INVITE'>"
2635 " <ARG name='buddy_id' type='choice' id='9901' validate='0' "
2636 " desc='Buddy Id'>"
2637 " <CHOICE value='-1' desc='All buddies'/>"
2638 " <CHOICE value='0' desc='Current dialog'/>"
2639 " </ARG>"
2640 " </CMD>"
2641 " <CMD name='multi' id='1002' desc='Make multiple calls'>"
2642 " <ARG name='number_of_calls' type='int' desc='Number of calls'/>"
2643 " <ARG name='buddy_id' type='choice' id='9901' validate='0' "
2644 " desc='Buddy Id'>"
2645 " <CHOICE value='-1' desc='All buddies'/>"
2646 " <CHOICE value='0' desc='Current dialog'/>"
2647 " </ARG>"
2648 " </CMD>"
2649 " <CMD name='answer' id='1003' desc='Answer call'>"
2650 " <ARG name='code' type='int' desc='Answer code'/>"
2651 " <ARG name='new_url' type='string' optional='1' "
2652 " desc='New URL(for 3xx resp)'/>"
2653 " </CMD>"
2654 " <CMD name='hangup' id='1004' sc='g' desc='Hangup call'/>"
2655 " <CMD name='hangup_all' id='1005' sc='hA' desc='Hangup all call'/>"
2656 " <CMD name='hold' id='1006' sc='H' desc='Hold call'/>"
2657 " <CMD name='reinvite' id='1007' sc='v' "
2658 " desc='Re-invite (release hold)'/>"
2659 " <CMD name='update' id='1008' sc='U' desc='Send Update request'/>"
2660 " <CMD name='next' id='1009' sc=']' desc='Select next call'/>"
2661 " <CMD name='previous' id='1010' sc='[' desc='Select previous call'/>"
2662 " <CMD name='transfer' id='1011' sc='x' desc='Transfer call'>"
2663 " <ARG name='buddy_id' type='choice' id='9901' validate='0' "
2664 " desc='Buddy Id'>"
2665 " <CHOICE value='-1' desc='All buddies'/>"
2666 " <CHOICE value='0' desc='Current dialog'/>"
2667 " </ARG>"
2668 " </CMD>"
2669 " <CMD name='transfer_replaces' id='1012' sc='X' "
2670 " desc='Transfer replace call'>"
2671 " <ARG name='call_id' type='choice' id='9911' desc='Call Id'/>"
2672 " </CMD>"
2673 " <CMD name='redirect' id='1013' sc='R' desc='Redirect call'>"
2674 " <ARG name='redirect_option' type='choice' desc='Redirect option'>"
2675 " <CHOICE value='0' desc='Redirect accept replace'/>"
2676 " <CHOICE value='1' desc='Redirect accept'/>"
2677 " <CHOICE value='2' desc='Redirect reject'/>"
2678 " <CHOICE value='3' desc='Redirect stop'/>"
2679 " </ARG>"
2680 " </CMD>"
2681 " <CMD name='d_2833' id='1014' sc='#' desc='Send DTMF (RFC 2833)'>"
2682 " <ARG name='dtmf_to_send' type='string' "
2683 " desc='DTMF String to send'/>"
2684 " </CMD>"
2685 " <CMD name='d_info' id='1015' sc='*' desc='Send DTMF with SIP INFO'>"
2686 " <ARG name='dtmf_to_send' type='string' "
2687 " desc='DTMF String to send'/>"
2688 " </CMD>"
2689 " <CMD name='dump_q' id='1016' sc='dq' desc='Dump (call) quality'/>"
2690 " <CMD name='send_arb' id='1017' sc='S' desc='Send arbitrary request'>"
2691 " <ARG name='request_method' type='string' desc='Request method'/>"
2692 " <ARG name='buddy_id' type='choice' id='9901' validate='0' "
2693 " desc='Buddy Id'>"
2694 " <CHOICE value='-1' desc='All buddies'/>"
2695 " <CHOICE value='0' desc='Current dialog'/>"
2696 " </ARG>"
2697 " </CMD>"
2698 " <CMD name='list' id='1018' desc='Show current call'/>"
2699 "</CMD>";
2700
2701 pj_str_t xml = pj_str(call_command);
2702 return pj_cli_add_cmd_from_xml(cli, NULL,
2703 &xml, cmd_call_handler,
2704 NULL, get_choice_value);
2705}
2706
2707static pj_status_t add_presence_command(pj_cli_t *cli)
2708{
2709 char* presence_command =
2710 "<CMD name='im' id='200' desc='IM and Presence Commands'>"
2711 " <CMD name='add_b' id='2001' sc='+b' desc='Add buddy'>"
2712 " <ARG name='buddy_uri' type='string' desc='Buddy URI'/>"
2713 " </CMD>"
2714 " <CMD name='del_b' id='2002' sc='-b' desc='Delete buddy'>"
2715 " <ARG name='added_buddy_id' type='choice' id='9912' "
2716 " desc='Buddy ID'/>"
2717 " </CMD>"
2718 " <CMD name='send_im' id='2003' sc='i' desc='Send IM'>"
2719 " <ARG name='buddy_id' type='choice' id='9901' validate='0' "
2720 " desc='Buddy Id'>"
2721 " <CHOICE value='-1' desc='All buddies'/>"
2722 " <CHOICE value='0' desc='Current dialog'/>"
2723 " </ARG>"
2724 " <ARG name='message_content' type='string' desc='Message Content'/>"
2725 " </CMD>"
2726 " <CMD name='sub_pre' id='2004' desc='Subscribe presence'>"
2727 " <ARG name='buddy_id' type='choice' id='9901' validate='0' "
2728 " desc='Buddy Id'>"
2729 " <CHOICE value='-1' desc='All buddies'/>"
2730 " <CHOICE value='0' desc='Current dialog'/>"
2731 " </ARG>"
2732 " </CMD>"
2733 " <CMD name='unsub_pre' id='2005' desc='Unsubscribe Presence'>"
2734 " <ARG name='buddy_id' type='choice' id='9901' validate='0' "
2735 " desc='Buddy Id'>"
2736 " <CHOICE value='-1' desc='All buddies'/>"
2737 " <CHOICE value='0' desc='Current dialog'/>"
2738 " </ARG>"
2739 " </CMD>"
2740 " <CMD name='tog_state' id='2006' desc='Toggle online state'/>"
2741 " <CMD name='pre_text' id='2007' sc='T' "
2742 " desc='Specify custom presence text'>"
2743 " <ARG name='online_state' type='choice' desc='Online state'>"
2744 " <CHOICE value='1' desc='Available'/>"
2745 " <CHOICE value='2' desc='Busy'/>"
2746 " <CHOICE value='3' desc='On The Phone'/>"
2747 " <CHOICE value='4' desc='Idle'/>"
2748 " <CHOICE value='5' desc='Away'/>"
2749 " <CHOICE value='6' desc='Be Right Back'/>"
2750 " <CHOICE value='7' desc='Offline'/>"
2751 " </ARG>"
2752 " </CMD>"
2753 " <CMD name='bud_list' id='2008' sc='bl' desc='Show buddy list'/>"
2754 "</CMD>";
2755
2756 pj_str_t xml = pj_str(presence_command);
2757
2758 return pj_cli_add_cmd_from_xml(cli, NULL,
2759 &xml, cmd_presence_handler,
2760 NULL, get_choice_value);
2761}
2762
2763static pj_status_t add_account_command(pj_cli_t *cli)
2764{
2765 char* account_command =
2766 "<CMD name='acc' id='300' desc='Account commands'>"
2767 " <CMD name='add' id='3001' sc='+a' desc='Add new account'>"
2768 " <ARG name='sip_url' type='string' desc='Your SIP URL'/>"
2769 " <ARG name='registrar_url' type='string' "
2770 " desc='URL of the registrar'/>"
2771 " <ARG name='auth_realm' type='string' desc='Auth realm'/>"
2772 " <ARG name='auth_username' type='string' desc='Auth username'/>"
2773 " <ARG name='auth_password' type='string' desc='Auth password'/>"
2774 " </CMD>"
2775 " <CMD name='del' id='3002' sc='-a' desc='Delete account'>"
2776 " <ARG name='account_id' type='choice' id='9902' desc='Account Id'/>"
2777 " </CMD>"
2778 " <CMD name='mod' id='3003' sc='!a' desc='Modify account'>"
2779 " <ARG name='account_id' type='choice' id='9902' desc='Account Id'/>"
2780 " <ARG name='sip_url' type='string' desc='Your SIP URL'/>"
2781 " <ARG name='registrar_url' type='string' "
2782 " desc='URL of the registrar'/>"
2783 " <ARG name='auth_realm' type='string' desc='Auth realm'/>"
2784 " <ARG name='auth_username' type='string' desc='Auth username'/>"
2785 " <ARG name='auth_password' type='string' desc='Auth password'/>"
2786 " </CMD>"
2787 " <CMD name='reg' id='3004' sc='rr' "
2788 " desc='Send (Refresh) Register request to register'/>"
2789 " <CMD name='unreg' id='3005' sc='ru' "
2790 " desc='Send Register request to unregister'/>"
2791 " <CMD name='next' id='3006' sc='<' "
2792 " desc='Select the next account for sending outgoing requests'>"
2793 " <ARG name='account_id' type='choice' id='9902' desc='Account Id'/>"
2794 " </CMD>"
2795 " <CMD name='prev' id='3007' sc='>' "
2796 " desc='Select the previous account for sending outgoing requests'>"
2797 " <ARG name='account_id' type='choice' id='9902' desc='Account Id'/>"
2798 " </CMD>"
2799 " <CMD name='show' id='3008' sc='l' desc='Show account list'/>"
2800 "</CMD>";
2801
2802 pj_str_t xml = pj_str(account_command);
2803 return pj_cli_add_cmd_from_xml(cli, NULL,
2804 &xml, cmd_account_handler,
2805 NULL, get_choice_value);
2806}
2807
2808static pj_status_t add_media_command(pj_cli_t *cli)
2809{
2810 char* media_command =
2811 "<CMD name='audio' id='400' desc='Conference and Media commands'>"
2812 " <CMD name='list' id='4001' sc='cl' desc='Show conference list'/>"
2813 " <CMD name='conf_con' id='4002' sc='cc' desc='Conference connect'>"
2814 " <ARG name='source_port' type='choice' id='9903' "
2815 " desc='Source Port'/>"
2816 " <ARG name='destination_port' type='choice' id='9903' "
2817 " desc='Destination Port'/>"
2818 " </CMD>"
2819 " <CMD name='conf_dis' id='4003' sc='cd' desc='Conference disconnect'>"
2820 " <ARG name='source_port' type='choice' id='9903' "
2821 " desc='Source Port'/>"
2822 " <ARG name='destination_port' type='choice' id='9903' "
2823 " desc='Destination Port'/>"
2824 " </CMD>"
2825 " <CMD name='adjust_vol' id='4004' sc='V' desc='Adjust volume'>"
2826 " <ARG name='mic_level' type='int' desc='Mic Level'/>"
2827 " <ARG name='speaker_port' type='int' desc='Speaker Level'/>"
2828 " </CMD>"
2829 " <CMD name='codec_prio' id='4005' sc='Cp' "
2830 " desc='Arrange codec priorities'>"
2831 " <ARG name='codec_id' type='choice' id='9904' desc='Codec Id'/>"
2832 " <ARG name='priority' type='int' desc='Codec Priority'/>"
2833 " </CMD>"
2834 "</CMD>";
2835
2836 pj_str_t xml = pj_str(media_command);
2837 return pj_cli_add_cmd_from_xml(cli, NULL,
2838 &xml, cmd_media_handler,
2839 NULL, get_choice_value);
2840}
2841
2842static pj_status_t add_config_command(pj_cli_t *cli)
2843{
2844 char* config_command =
2845 "<CMD name='stat' id='500' desc='Status and config commands'>"
2846 " <CMD name='dump_stat' id='5001' sc='ds' desc='Dump status'/>"
2847 " <CMD name='dump_detail' id='5002' sc='dd' "
2848 " desc='Dump detail status'/>"
2849 " <CMD name='dump_conf' id='5003' sc='dc' "
2850 " desc='Dump configuration to screen'/>"
2851 " <CMD name='write_setting' id='5004' sc='f' "
2852 " desc='Write current configuration file'>"
2853 " <ARG name='output_file' type='string' desc='Output filename'/>"
2854 " </CMD>"
2855 "</CMD>";
2856
2857 pj_str_t xml = pj_str(config_command);
2858 return pj_cli_add_cmd_from_xml(cli, NULL,
2859 &xml, cmd_config_handler,
2860 NULL, get_choice_value);
2861}
2862
2863#if PJSUA_HAS_VIDEO
2864static pj_status_t add_video_command(pj_cli_t *cli)
2865{
2866 char* video_command =
2867 "<CMD name='video' id='600' desc='Video commands'>"
2868 " <CMD name='enable' id='6001' desc='Enable video'/>"
2869 " <CMD name='disable' id='6002' desc='Disable video'/>"
2870 " <CMD name='acc' id='6003' desc='Video setting for current account'>"
2871 " <CMD name='show' id='60031' "
2872 " desc='Show video setting for current account'/>"
2873 " <CMD name='autorx' id='60032' sc='ar' "
2874 " desc='Automatically show incoming video'>"
2875 " <ARG name='enable_option' type='choice' "
2876 " desc='Enable/Disable option'>"
2877 " <CHOICE value='On' desc='Enable'/>"
2878 " <CHOICE value='Off' desc='Disable'/>"
2879 " </ARG>"
2880 " </CMD>"
2881 " <CMD name='autotx' id='60033' sc='at' "
2882 " desc='Automatically offer video'>"
2883 " <ARG name='enable_option' type='choice' "
2884 " desc='Enable/Disable option'>"
2885 " <CHOICE value='On' desc='Enable'/>"
2886 " <CHOICE value='Off' desc='Disable'/>"
2887 " </ARG>"
2888 " </CMD>"
2889 " <CMD name='cap_id' id='60034' sc='ci' "
2890 " desc='Set default capture device for current account'>"
2891 " <ARG name='cap_dev_id' type='choice' id='9905' "
2892 " desc='Capture device Id'/>"
2893 " </CMD>"
2894 " <CMD name='ren_id' id='60035' sc='ri' "
2895 " desc='Set default renderer device for current account'>"
2896 " <ARG name='ren_dev_id' type='choice' id='9906' "
2897 " desc='Renderer device Id'/>"
2898 " </CMD>"
2899 " </CMD>"
2900 " <CMD name='call' id='6004' sc='vcl' "
2901 " desc='Video call commands/settings'>"
2902 " <CMD name='rx' id='60041' "
2903 " desc='Enable/disable video RX for stream in curr call'>"
2904 " <ARG name='enable_option' type='choice' "
2905 " desc='Enable/Disable option'>"
2906 " <CHOICE value='On' desc='Enable'/>"
2907 " <CHOICE value='Off' desc='Disable'/>"
2908 " </ARG>"
2909 " <ARG name='stream_id' type='choice' id='9908' desc='Stream Id'/>"
2910 " </CMD>"
2911 " <CMD name='tx' id='60042' "
2912 " desc='Enable/disable video TX for stream in curr call'>"
2913 " <ARG name='enable_option' type='choice' "
2914 " desc='Enable/Disable option'>"
2915 " <CHOICE value='On' desc='Enable'/>"
2916 " <CHOICE value='Off' desc='Disable'/>"
2917 " </ARG>"
2918 " <ARG name='stream_id' type='choice' id='9908' desc='Stream Id'/>"
2919 " </CMD>"
2920 " <CMD name='add' id='60043' "
2921 " desc='Add video stream for current call'/>"
2922 " <CMD name='enable' id='60044' "
2923 " desc='Enable stream #N in current call'>"
2924 " <ARG name='stream_id' type='choice' id='9908' optional='1' "
2925 " desc='Stream Id'/>"
2926 " </CMD>"
2927 " <CMD name='disable' id='60045' "
2928 " desc='Disable stream #N in current call'>"
2929 " <ARG name='stream_id' type='choice' id='9908' optional='1' "
2930 " desc='Stream Id'/>"
2931 " </CMD>"
2932 " <CMD name='cap' id='60046' "
2933 " desc='Set capture dev ID for stream #N in current call'>"
2934 " <ARG name='stream_id' type='choice' id='9908' desc='Stream Id'/>"
2935 " <ARG name='cap_device_id' type='choice' id='9905' "
2936 " desc='Device Id'/>"
2937 " </CMD>"
2938 " </CMD>"
2939 " <CMD name='device' id='6005' sc='vv' desc='Video device commands'>"
2940 " <CMD name='list' id='60051' desc='Show all video devices'/>"
2941 " <CMD name='refresh' id='60052' desc='Refresh video device list'/>"
2942 " <CMD name='prev' id='60053' "
2943 " desc='Enable/disable preview for specified device ID'>"
2944 " <ARG name='enable_option' type='choice' "
2945 " desc='Enable/Disable option'>"
2946 " <CHOICE value='On' desc='Enable'/>"
2947 " <CHOICE value='Off' desc='Disable'/>"
2948 " </ARG>"
2949 " <ARG name='device_id' type='choice' id='9907' "
2950 " desc='Video Device Id'/>"
2951 " </CMD>"
2952 " </CMD>"
2953 " <CMD name='codec' id='6006' desc='Video codec commands'>"
2954 " <CMD name='list' id='60061' desc='Show video codec list'/>"
2955 " <CMD name='prio' id='60062' desc='Set video codec priority'>"
2956 " <ARG name='codec_id' type='choice' id='9909' desc='Codec Id'/>"
2957 " <ARG name='priority' type='int' desc='Priority'/>"
2958 " </CMD>"
2959 " <CMD name='fps' id='60063' desc='Set video codec framerate'>"
2960 " <ARG name='codec_id' type='choice' id='9909' desc='Codec Id'/>"
2961 " <ARG name='num' type='int' desc='Numerator'/>"
2962 " <ARG name='denum' type='int' desc='Denumerator'/>"
2963 " </CMD>"
2964 " <CMD name='bitrate' id='60064' desc='Set video codec bitrate'>"
2965 " <ARG name='codec_id' type='choice' id='9909' desc='Codec Id'/>"
2966 " <ARG name='avg' type='int' desc='Average bps'/>"
2967 " <ARG name='max' type='int' desc='Maximum bps'/>"
2968 " </CMD>"
2969 " <CMD name='size' id='60065' desc='Set codec ID size/resolution'>"
2970 " <ARG name='codec_id' type='choice' id='9909' desc='Codec Id'/>"
2971 " <ARG name='width' type='int' desc='Width'/>"
2972 " <ARG name='height' type='int' desc='Height'/>"
2973 " </CMD>"
2974 " </CMD>"
2975 " <CMD name='win' id='6007' desc='Video windows settings/commands'>"
2976 " <CMD name='list' id='60071' desc='List all active video windows'/>"
2977 " <CMD name='arrange' id='60072' desc='Auto arrange windows'/>"
2978 " <CMD name='show' id='60073' desc='Show specific windows'>"
2979 " <ARG name='window_id' type='choice' id='9910' "
2980 " desc='Windows Id'/>"
2981 " </CMD>"
2982 " <CMD name='hide' id='60074' desc='Hide specific windows'>"
2983 " <ARG name='window_id' type='choice' id='9910' "
2984 " desc='Windows Id'/>"
2985 " </CMD>"
2986 " <CMD name='move' id='60075' desc='Move window position'>"
2987 " <ARG name='window_id' type='choice' id='9910' "
2988 " desc='Windows Id'/>"
2989 " <ARG name='x' type='int' desc='Horizontal position'/>"
2990 " <ARG name='y' type='int' desc='Vertical position'/>"
2991 " </CMD>"
2992 " <CMD name='resize' id='60076' "
2993 " desc='Resize window to specific width/height'>"
2994 " <ARG name='window_id' type='choice' id='9910' "
2995 " desc='Windows Id'/>"
2996 " <ARG name='width' type='int' desc='Width'/>"
2997 " <ARG name='height' type='int' desc='Height'/>"
2998 " </CMD>"
2999 " </CMD>"
3000 "</CMD>";
3001
3002 pj_str_t xml = pj_str(video_command);
3003 return pj_cli_add_cmd_from_xml(cli, NULL,
3004 &xml, cmd_video_handler,
3005 NULL, get_choice_value);
3006}
3007#endif
3008
3009static pj_status_t add_other_command(pj_cli_t *cli)
3010{
3011 char* sleep_command =
3012 "<CMD name='sleep' id='700' desc='Suspend keyboard input'>"
3013 " <ARG name='msec' type='int' desc='Millisecond'/>"
3014 "</CMD>";
3015
3016 char* network_command =
3017 "<CMD name='network' id='900' desc='Detect network type'/>";
3018
3019 char* shutdown_command =
3020 "<CMD name='shutdown' id='110' desc='Shutdown application'/>";
3021
3022 char* restart_command =
3023 "<CMD name='restart' id='120' desc='Restart application'>"
3024 " <ARG name='options1' type='string' desc='Options' optional='1'/>"
3025 " <ARG name='options2' type='string' desc='Options' optional='1'/>"
3026 " <ARG name='options3' type='string' desc='Options' optional='1'/>"
3027 " <ARG name='options4' type='string' desc='Options' optional='1'/>"
3028 "</CMD>";
3029
3030 pj_status_t status;
3031 pj_str_t sleep_xml = pj_str(sleep_command);
3032 pj_str_t network_xml = pj_str(network_command);
3033 pj_str_t shutdown_xml = pj_str(shutdown_command);
3034 pj_str_t restart_xml = pj_str(restart_command);
3035
3036 status = pj_cli_add_cmd_from_xml(cli, NULL,
3037 &sleep_xml, cmd_sleep_handler,
3038 NULL, NULL);
3039 if (status != PJ_SUCCESS)
3040 return status;
3041
3042 status = pj_cli_add_cmd_from_xml(cli, NULL,
3043 &network_xml, cmd_network_handler,
3044 NULL, NULL);
3045 if (status != PJ_SUCCESS)
3046 return status;
3047
3048 status = pj_cli_add_cmd_from_xml(cli, NULL,
3049 &shutdown_xml, cmd_quit_handler,
3050 NULL, NULL);
3051
3052 if (status != PJ_SUCCESS)
3053 return status;
3054
3055 status = pj_cli_add_cmd_from_xml(cli, NULL,
3056 &restart_xml, cmd_restart_handler,
3057 NULL, NULL);
3058
3059 return status;
3060}
3061
3062pj_status_t cli_setup_command(pj_cli_t *cli)
3063{
3064 pj_status_t status;
3065
3066 status = add_call_command(cli);
3067 if (status != PJ_SUCCESS)
3068 return status;
3069
3070 status = add_presence_command(cli);
3071 if (status != PJ_SUCCESS)
3072 return status;
3073
3074 status = add_account_command(cli);
3075 if (status != PJ_SUCCESS)
3076 return status;
3077
3078 status = add_media_command(cli);
3079 if (status != PJ_SUCCESS)
3080 return status;
3081
3082 status = add_config_command(cli);
3083 if (status != PJ_SUCCESS)
3084 return status;
3085
3086#if PJSUA_HAS_VIDEO
3087 status = add_video_command(cli);
3088 if (status != PJ_SUCCESS)
3089 return status;
3090#endif
3091
3092 status = add_other_command(cli);
3093
3094 return status;
3095}