Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2010 Teluu Inc. (http://www.teluu.com) |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Sample CLI application |
| 22 | */ |
| 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> |
| 28 | #include <pjlib.h> |
| 29 | |
| 30 | #define THIS_FILE "clidemo.c" |
| 31 | |
| 32 | /* Set this to 1 if you want to let the system assign a port |
| 33 | * for the CLI telnet daemon. |
| 34 | * Default: 1 |
| 35 | */ |
| 36 | #define USE_RANDOM_PORT 1 |
| 37 | |
| 38 | struct cmd_xml_t { |
| 39 | char * xml; |
| 40 | pj_cli_cmd_handler handler; |
| 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * Declaration of system specific main loop, which will be defined in |
| 45 | * a separate file. |
| 46 | */ |
| 47 | pj_status_t app_main(pj_cli_t *cli); |
| 48 | |
| 49 | #define print_msg(arg) \ |
| 50 | do { \ |
| 51 | unsigned d = pj_log_get_decor(); \ |
| 52 | pj_log_set_decor(0); \ |
| 53 | PJ_LOG(1, arg); \ |
| 54 | pj_log_set_decor(d); \ |
| 55 | } while (0) |
| 56 | |
| 57 | static pj_cli_t *cli = NULL; |
| 58 | |
| 59 | /* Handler for sayhello command */ |
| 60 | static pj_status_t sayhello(pj_cli_cmd_val *cval) |
| 61 | { |
| 62 | print_msg(("", "Hello %.*s!\r\n", |
| 63 | (int)cval->argv[1].slen, cval->argv[1].ptr)); |
| 64 | return PJ_SUCCESS; |
| 65 | } |
| 66 | |
| 67 | /* Handler for saybye command */ |
| 68 | static pj_status_t saybye(pj_cli_cmd_val *cval) |
| 69 | { |
| 70 | print_msg(("", "Bye %.*s!\r\n", |
| 71 | (int)cval->argv[1].slen, cval->argv[1].ptr)); |
| 72 | return PJ_SUCCESS; |
| 73 | } |
| 74 | |
| 75 | /* Handler for say command */ |
| 76 | static pj_status_t say(pj_cli_cmd_val *cval) |
| 77 | { |
| 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)); |
| 81 | return PJ_SUCCESS; |
| 82 | } |
| 83 | |
| 84 | static pj_status_t quit_app(pj_cli_cmd_val *cval) |
| 85 | { |
| 86 | PJ_UNUSED_ARG(cval); |
| 87 | pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_FALSE); |
| 88 | |
| 89 | return PJ_CLI_EEXIT; |
| 90 | } |
| 91 | |
| 92 | static void get_codec_list(pj_cli_dyn_choice_param *param) |
| 93 | { |
| 94 | if (param->arg_id == 3) { |
| 95 | param->cnt = 2; |
| 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"); |
| 100 | } |
| 101 | } |
| 102 | |
| 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'/>" |
| 106 | "</CMD>", |
| 107 | &sayhello}, |
| 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'/>" |
| 110 | "</CMD>", |
| 111 | &saybye}, |
| 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'/>" |
| 115 | "</CMD>", |
| 116 | &say}, |
| 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'/>" |
| 126 | " </CMD>" |
| 127 | " </CMD>" |
| 128 | "</CMD>", |
| 129 | NULL}, |
| 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'/>" |
| 134 | " </ARG>" |
| 135 | "</CMD>", |
| 136 | NULL}, |
| 137 | {"<CMD name='quit_app' id='999' sc='qa' desc='Quit the application'>" |
| 138 | "</CMD>", |
| 139 | &quit_app}, |
| 140 | }; |
| 141 | |
| 142 | static void log_writer(int level, const char *buffer, int len) |
| 143 | { |
| 144 | if (cli) |
| 145 | pj_cli_write_log(cli, level, buffer, len); |
| 146 | } |
| 147 | |
| 148 | int main() |
| 149 | { |
| 150 | pj_caching_pool cp; |
| 151 | pj_cli_cfg cli_cfg; |
| 152 | pj_cli_telnet_cfg tcfg; |
| 153 | pj_str_t xml; |
| 154 | pj_status_t status; |
| 155 | int i; |
| 156 | |
| 157 | pj_init(); |
| 158 | pj_caching_pool_init(&cp, NULL, 0); |
| 159 | pjlib_util_init(); |
| 160 | |
| 161 | /* |
| 162 | * Create CLI app. |
| 163 | */ |
| 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"); |
| 168 | |
| 169 | status = pj_cli_create(&cli_cfg, &cli); |
| 170 | if (status != PJ_SUCCESS) |
| 171 | goto on_return; |
| 172 | |
| 173 | /* |
| 174 | * Register some commands. |
| 175 | */ |
| 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) |
| 181 | goto on_return; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Start telnet daemon |
| 186 | */ |
| 187 | pj_cli_telnet_cfg_default(&tcfg); |
| 188 | // tcfg.passwd = pj_str("pjsip"); |
| 189 | #if USE_RANDOM_PORT |
| 190 | tcfg.port = 0; |
| 191 | #else |
| 192 | tcfg.port = 2233; |
| 193 | #endif |
| 194 | tcfg.prompt_str = pj_str("CoolWater% "); |
| 195 | status = pj_cli_telnet_create(cli, &tcfg, NULL); |
| 196 | if (status != PJ_SUCCESS) |
| 197 | goto on_return; |
| 198 | |
| 199 | /* |
| 200 | * Run the system specific main loop. |
| 201 | */ |
| 202 | status = app_main(cli); |
| 203 | |
| 204 | on_return: |
| 205 | |
| 206 | /* |
| 207 | * Destroy |
| 208 | */ |
| 209 | pj_cli_destroy(cli); |
| 210 | cli = NULL; |
| 211 | pj_caching_pool_destroy(&cp); |
| 212 | pj_shutdown(); |
| 213 | |
| 214 | return (status != PJ_SUCCESS ? 1 : 0); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /*xxxxxxxxxxxxxxxxxxxxxxxxxxxxx main_console.c xxxxxxxxxxxxxxxxxxxxxxxxxxxx */ |
| 219 | /* |
| 220 | * Simple implementation of app_main() for console targets |
| 221 | */ |
| 222 | pj_status_t app_main(pj_cli_t *cli) |
| 223 | { |
| 224 | pj_status_t status; |
| 225 | pj_cli_sess *sess; |
| 226 | pj_cli_console_cfg console_cfg; |
| 227 | |
| 228 | pj_cli_console_cfg_default(&console_cfg); |
| 229 | console_cfg.prompt_str = pj_str("HotWater> "); |
| 230 | |
| 231 | /* |
| 232 | * Create the console front end |
| 233 | */ |
| 234 | status = pj_cli_console_create(cli, &console_cfg, &sess, NULL); |
| 235 | if (status != PJ_SUCCESS) |
| 236 | return status; |
| 237 | |
| 238 | pj_log_set_log_func(&log_writer); |
| 239 | |
| 240 | /* |
| 241 | * Main loop. |
| 242 | */ |
| 243 | for (;;) { |
| 244 | char cmdline[PJ_CLI_MAX_CMDBUF]; |
| 245 | pj_status_t status; |
| 246 | |
| 247 | status = pj_cli_console_process(sess, &cmdline[0], sizeof(cmdline)); |
| 248 | if (status != PJ_SUCCESS) |
| 249 | break; |
| 250 | |
| 251 | //pj_ansi_strcpy(cmdline, "sayhello {Teluu Inc.}"); |
| 252 | if (status == PJ_CLI_EEXIT) { |
| 253 | /* exit is called */ |
| 254 | break; |
| 255 | } else if (status != PJ_SUCCESS) { |
| 256 | /* Something wrong with the cmdline */ |
| 257 | PJ_PERROR(1,(THIS_FILE, status, "Exec error")); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return PJ_SUCCESS; |
| 262 | } |