| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <pjlib-util/cli.h> |
| 23 |
#include <pjlib-util/cli_imp.h> |
| 24 |
#include <pjlib-util/cli_console.h> |
| 25 |
#include <pjlib-util/cli_telnet.h> |
| 26 |
#include <pjlib-util/errno.h> |
| 27 |
#include <pjlib.h> |
| 28 |
|
| 29 |
#define THIS_FILE "clidemo.c" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#define USE_RANDOM_PORT 1 |
| 36 |
|
| 37 |
struct cmd_xml_t { |
| 38 |
char * xml; |
| 39 |
pj_cli_cmd_handler handler; |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
pj_status_t app_main(pj_cli_t *cli); |
| 47 |
|
| 48 |
#define print_msg(arg) \ |
| 49 |
do { \ |
| 50 |
unsigned d = pj_log_get_decor(); \ |
| 51 |
pj_log_set_decor(0); \ |
| 52 |
PJ_LOG(1, arg); \ |
| 53 |
pj_log_set_decor(d); \ |
| 54 |
} while (0) |
| 55 |
|
| 56 |
static pj_cli_t *cli = NULL; |
| 57 |
|
| 58 |
|
| 59 |
static pj_status_t sayhello(pj_cli_cmd_val *cval) |
| 60 |
{ |
| 61 |
print_msg(("", "Hello %.*s!\r\n", |
| 62 |
(int)cval->argv[1].slen, cval->argv[1].ptr)); |
| 63 |
return PJ_SUCCESS; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
static pj_status_t saybye(pj_cli_cmd_val *cval) |
| 68 |
{ |
| 69 |
print_msg(("", "Bye %.*s!\r\n", |
| 70 |
(int)cval->argv[1].slen, cval->argv[1].ptr)); |
| 71 |
return PJ_SUCCESS; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
static pj_status_t say(pj_cli_cmd_val *cval) |
| 76 |
{ |
| 77 |
print_msg(("", "%.*s %.*s\r\n", |
| 78 |
(int)cval->argv[1].slen, cval->argv[1].ptr, |
| 79 |
(int)cval->argv[2].slen, cval->argv[2].ptr)); |
| 80 |
return PJ_SUCCESS; |
| 81 |
} |
| 82 |
|
| 83 |
static pj_status_t quit_app(pj_cli_cmd_val *cval) |
| 84 |
{ |
| 85 |
PJ_UNUSED_ARG(cval); |
| 86 |
pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_FALSE); |
| 87 |
|
| 88 |
return PJ_CLI_EEXIT; |
| 89 |
} |
| 90 |
|
| 91 |
static void get_codec_list(pj_cli_dyn_choice_param *param) |
| 92 |
{ |
| 93 |
if (param->arg_id == 3) { |
| 94 |
param->cnt = 2; |
| 95 |
pj_strdup2(param->pool, ¶m->choice[0].value, "iLbc"); |
| 96 |
pj_strdup2(param->pool, ¶m->choice[0].desc, "iLbc Codec"); |
| 97 |
pj_strdup2(param->pool, ¶m->choice[1].value, "g729"); |
| 98 |
pj_strdup2(param->pool, ¶m->choice[1].desc, "g729 Codec"); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
static struct cmd_xml_t cmd_xmls[] = { |
| 103 |
{"<CMD name='sayhello' id='1' sc=' ,h , ,, sh ,' desc='Will say hello'>" |
| 104 |
" <ARG name='whom' type='text' desc='Whom to say hello to'/>" |
| 105 |
"</CMD>", |
| 106 |
&sayhello}, |
| 107 |
{"<CMD name='saybye' id='2' sc='b,sb' desc='Will say bye'>" |
| 108 |
" <ARG name='whom' type='text' desc='Whom to say bye to'/>" |
| 109 |
"</CMD>", |
| 110 |
&saybye}, |
| 111 |
{"<CMD name='saymsg' id='3' sc='s' desc='Will say something'>" |
| 112 |
" <ARG name='msg' type='text' desc='Message to say'/>" |
| 113 |
" <ARG name='whom' type='text' desc='Whom to say to'/>" |
| 114 |
"</CMD>", |
| 115 |
&say}, |
| 116 |
{"<CMD name='vid' id='7' desc='Video Command'>" |
| 117 |
" <CMD name='help' id='72' desc='Show Help' />" |
| 118 |
" <CMD name='enable' id='73' desc='Enable Video' />" |
| 119 |
" <CMD name='disable' id='74' desc='Disable Video' />" |
| 120 |
" <CMD name='call' id='75' desc='Video call' >" |
| 121 |
" <CMD name='add' id='756' desc='Add Call' />" |
| 122 |
" <CMD name='cap' id='757' desc='Capture Call' >" |
| 123 |
" <ARG name='streamno' type='int' desc='Stream No' id='1'/>" |
| 124 |
" <ARG name='devid' type='int' desc='Device Id' id='2'/>" |
| 125 |
" </CMD>" |
| 126 |
" </CMD>" |
| 127 |
"</CMD>", |
| 128 |
NULL}, |
| 129 |
{"<CMD name='disable_codec' id='8' desc='Disable codec'>" |
| 130 |
" <ARG name='codec_list' type='choice' id='38' desc='Codec list'>" |
| 131 |
" <CHOICE value='g711' desc='G711 Codec'/>" |
| 132 |
" <CHOICE value='g722' desc='G722 Codec'/>" |
| 133 |
" </ARG>" |
| 134 |
"</CMD>", |
| 135 |
NULL}, |
| 136 |
{"<CMD name='quit_app' id='999' sc='qa' desc='Quit the application'>" |
| 137 |
"</CMD>", |
| 138 |
&quit_app}, |
| 139 |
}; |
| 140 |
|
| 141 |
static void log_writer(int level, const char *buffer, int len) |
| 142 |
{ |
| 143 |
if (cli) |
| 144 |
pj_cli_write_log(cli, level, buffer, len); |
| 145 |
} |
| 146 |
|
| 147 |
int main() |
| 148 |
{ |
| 149 |
pj_caching_pool cp; |
| 150 |
pj_cli_cfg cli_cfg; |
| 151 |
pj_cli_telnet_cfg tcfg; |
| 152 |
pj_str_t xml; |
| 153 |
pj_status_t status; |
| 154 |
unsigned i; |
| 155 |
|
| 156 |
status = pj_init(); |
| 157 |
if (status != PJ_SUCCESS) |
| 158 |
return 1; |
| 159 |
pj_caching_pool_init(&cp, NULL, 0); |
| 160 |
status = pjlib_util_init(); |
| 161 |
if (status != PJ_SUCCESS) |
| 162 |
return 1; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
pj_cli_cfg_default(&cli_cfg); |
| 168 |
cli_cfg.pf = &cp.factory; |
| 169 |
cli_cfg.name = pj_str("mycliapp"); |
| 170 |
cli_cfg.title = pj_str("My CLI Application"); |
| 171 |
|
| 172 |
status = pj_cli_create(&cli_cfg, &cli); |
| 173 |
if (status != PJ_SUCCESS) |
| 174 |
goto on_return; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
for (i = 0; i < PJ_ARRAY_SIZE(cmd_xmls); i++) { |
| 180 |
xml = pj_str(cmd_xmls[i].xml); |
| 181 |
status = pj_cli_add_cmd_from_xml(cli, NULL, &xml, |
| 182 |
cmd_xmls[i].handler, NULL, |
| 183 |
get_codec_list); |
| 184 |
if (status != PJ_SUCCESS) { |
| 185 |
PJ_PERROR(1,(THIS_FILE, status, |
| 186 |
"Failed adding command from XML #%d", i)); |
| 187 |
goto on_return; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
pj_cli_telnet_cfg_default(&tcfg); |
| 195 |
|
| 196 |
#if USE_RANDOM_PORT |
| 197 |
tcfg.port = 0; |
| 198 |
#else |
| 199 |
tcfg.port = 2233; |
| 200 |
#endif |
| 201 |
tcfg.prompt_str = pj_str("CoolWater% "); |
| 202 |
status = pj_cli_telnet_create(cli, &tcfg, NULL); |
| 203 |
if (status != PJ_SUCCESS) |
| 204 |
goto on_return; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
status = app_main(cli); |
| 210 |
|
| 211 |
on_return: |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
pj_cli_destroy(cli); |
| 217 |
cli = NULL; |
| 218 |
pj_caching_pool_destroy(&cp); |
| 219 |
pj_shutdown(); |
| 220 |
|
| 221 |
return (status != PJ_SUCCESS ? 1 : 0); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
pj_status_t app_main(pj_cli_t *c) |
| 230 |
{ |
| 231 |
pj_status_t status; |
| 232 |
pj_cli_sess *sess; |
| 233 |
pj_cli_console_cfg console_cfg; |
| 234 |
|
| 235 |
pj_cli_console_cfg_default(&console_cfg); |
| 236 |
console_cfg.prompt_str = pj_str("HotWater> "); |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
status = pj_cli_console_create(c, &console_cfg, &sess, NULL); |
| 242 |
if (status != PJ_SUCCESS) |
| 243 |
return status; |
| 244 |
|
| 245 |
pj_log_set_log_func(&log_writer); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
for (;;) { |
| 251 |
char cmdline[PJ_CLI_MAX_CMDBUF]; |
| 252 |
|
| 253 |
status = pj_cli_console_process(sess, &cmdline[0], sizeof(cmdline)); |
| 254 |
if (status != PJ_SUCCESS) |
| 255 |
break; |
| 256 |
|
| 257 |
|
| 258 |
if (status == PJ_CLI_EEXIT) { |
| 259 |
|
| 260 |
break; |
| 261 |
} else if (status != PJ_SUCCESS) { |
| 262 |
|
| 263 |
PJ_PERROR(1,(THIS_FILE, status, "Exec error")); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
return PJ_SUCCESS; |
| 268 |
} |