3
* Copyright (C) 2010 Teluu Inc. (http://www.teluu.com)
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
* Sample CLI application
23
#include <pjlib-util/cli.h>
24
#include <pjlib-util/cli_imp.h>
25
#include <pjlib-util/cli_console.h>
26
#include <pjlib-util/cli_telnet.h>
27
#include <pjlib-util/errno.h>
30
#define THIS_FILE "clidemo.c"
32
/* Set this to 1 if you want to let the system assign a port
33
* for the CLI telnet daemon.
36
#define USE_RANDOM_PORT 1
40
pj_cli_cmd_handler handler;
44
* Declaration of system specific main loop, which will be defined in
47
pj_status_t app_main(pj_cli_t *cli);
49
#define print_msg(arg) \
51
unsigned d = pj_log_get_decor(); \
52
pj_log_set_decor(0); \
54
pj_log_set_decor(d); \
57
static pj_cli_t *cli = NULL;
59
/* Handler for sayhello command */
60
static pj_status_t sayhello(pj_cli_cmd_val *cval)
62
print_msg(("", "Hello %.*s!\r\n",
63
(int)cval->argv[1].slen, cval->argv[1].ptr));
67
/* Handler for saybye command */
68
static pj_status_t saybye(pj_cli_cmd_val *cval)
70
print_msg(("", "Bye %.*s!\r\n",
71
(int)cval->argv[1].slen, cval->argv[1].ptr));
75
/* Handler for say command */
76
static pj_status_t say(pj_cli_cmd_val *cval)
78
print_msg(("", "%.*s %.*s\r\n",
79
(int)cval->argv[1].slen, cval->argv[1].ptr,
80
(int)cval->argv[2].slen, cval->argv[2].ptr));
84
static pj_status_t quit_app(pj_cli_cmd_val *cval)
87
pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_FALSE);
92
static void get_codec_list(pj_cli_dyn_choice_param *param)
94
if (param->arg_id == 3) {
96
pj_strdup2(param->pool, ¶m->choice[0].value, "iLbc");
97
pj_strdup2(param->pool, ¶m->choice[0].desc, "iLbc Codec");
98
pj_strdup2(param->pool, ¶m->choice[1].value, "g729");
99
pj_strdup2(param->pool, ¶m->choice[1].desc, "g729 Codec");
103
static struct cmd_xml_t cmd_xmls[] = {
104
{"<CMD name='sayhello' id='1' sc=' ,h , ,, sh ,' desc='Will say hello'>"
105
" <ARG name='whom' type='text' desc='Whom to say hello to'/>"
108
{"<CMD name='saybye' id='2' sc='b,sb' desc='Will say bye'>"
109
" <ARG name='whom' type='text' desc='Whom to say bye to'/>"
112
{"<CMD name='saymsg' id='3' sc='s' desc='Will say something'>"
113
" <ARG name='msg' type='text' desc='Message to say'/>"
114
" <ARG name='whom' type='text' desc='Whom to say to'/>"
117
{"<CMD name='vid' id='1' desc='Video Command'>"
118
" <CMD name='help' id='2' desc='Show Help' />"
119
" <CMD name='enable' id='3' desc='Enable Video' />"
120
" <CMD name='disable' id='4' desc='Disable Video' />"
121
" <CMD name='call' id='5' desc='Video call' >"
122
" <CMD name='add' id='6' desc='Add Call' />"
123
" <CMD name='cap' id='7' desc='Capture Call' >"
124
" <ARG name='streamno' type='int' desc='Stream No' id='1'/>"
125
" <ARG name='devid' type='int' desc='Device Id' id='2'/>"
130
{"<CMD name='disable_codec' id='8' desc='Disable codec'>"
131
" <ARG name='codec_list' type='choice' id='3' desc='Codec list'>"
132
" <CHOICE value='g711' desc='G711 Codec'/>"
133
" <CHOICE value='g722' desc='G722 Codec'/>"
137
{"<CMD name='quit_app' id='999' sc='qa' desc='Quit the application'>"
142
static void log_writer(int level, const char *buffer, int len)
145
pj_cli_write_log(cli, level, buffer, len);
152
pj_cli_telnet_cfg tcfg;
158
pj_caching_pool_init(&cp, NULL, 0);
164
pj_cli_cfg_default(&cli_cfg);
165
cli_cfg.pf = &cp.factory;
166
cli_cfg.name = pj_str("mycliapp");
167
cli_cfg.title = pj_str("My CLI Application");
169
status = pj_cli_create(&cli_cfg, &cli);
170
if (status != PJ_SUCCESS)
174
* Register some commands.
176
for (i = 0; i < sizeof(cmd_xmls)/sizeof(cmd_xmls[0]); i++) {
177
xml = pj_str(cmd_xmls[i].xml);
178
status = pj_cli_add_cmd_from_xml(cli, NULL, &xml,
179
cmd_xmls[i].handler, NULL, get_codec_list);
180
if (status != PJ_SUCCESS)
185
* Start telnet daemon
187
pj_cli_telnet_cfg_default(&tcfg);
188
// tcfg.passwd = pj_str("pjsip");
194
tcfg.prompt_str = pj_str("CoolWater% ");
195
status = pj_cli_telnet_create(cli, &tcfg, NULL);
196
if (status != PJ_SUCCESS)
200
* Run the system specific main loop.
202
status = app_main(cli);
211
pj_caching_pool_destroy(&cp);
214
return (status != PJ_SUCCESS ? 1 : 0);
218
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxx main_console.c xxxxxxxxxxxxxxxxxxxxxxxxxxxx */
220
* Simple implementation of app_main() for console targets
222
pj_status_t app_main(pj_cli_t *cli)
226
pj_cli_console_cfg console_cfg;
228
pj_cli_console_cfg_default(&console_cfg);
229
console_cfg.prompt_str = pj_str("HotWater> ");
232
* Create the console front end
234
status = pj_cli_console_create(cli, &console_cfg, &sess, NULL);
235
if (status != PJ_SUCCESS)
238
pj_log_set_log_func(&log_writer);
244
char cmdline[PJ_CLI_MAX_CMDBUF];
247
status = pj_cli_console_process(sess, &cmdline[0], sizeof(cmdline));
248
if (status != PJ_SUCCESS)
251
//pj_ansi_strcpy(cmdline, "sayhello {Teluu Inc.}");
252
if (status == PJ_CLI_EEXIT) {
255
} else if (status != PJ_SUCCESS) {
256
/* Something wrong with the cmdline */
257
PJ_PERROR(1,(THIS_FILE, status, "Exec error"));