Changeset 4461 for pjproject/trunk/pjsip-apps/src/pjsua/pjsua_cli_cmd.c
- Timestamp:
- Apr 5, 2013 3:02:19 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjsip-apps/src/pjsua/pjsua_cli_cmd.c
r4459 r4461 19 19 */ 20 20 21 #include "pjsua_c md.h"21 #include "pjsua_common.h" 22 22 #include <pjlib-util/cli.h> 23 23 #include <pjlib-util/cli_imp.h> 24 24 #include <pjlib-util/cli_console.h> 25 25 #include <pjlib-util/cli_telnet.h> 26 #include <pjlib-util/scanner.h> 26 27 27 28 #define THIS_FILE "pjsua_cli_cmd.c" 29 30 #define CHECK_PJSUA_RUNNING() if (pjsua_get_state()!=PJSUA_STATE_RUNNING) \ 31 return PJ_EINVALIDOP 28 32 29 33 /* CLI command id */ … … 39 43 #define CMD_NETWORK 900 40 44 #define CMD_QUIT 110 45 #define CMD_RESTART 120 46 #define CMD_RELOAD 130 41 47 42 48 /* call level 2 command */ … … 144 150 #define DYN_CHOICE_ADDED_BUDDY_ID (DYN_CHOICE_START)+12 145 151 146 static pj_cli_cfg cli_cfg;147 static pj_cli_telnet_cfg telnet_cfg;148 static pj_cli_t *cli = NULL;149 static pj_cli_sess *sess = NULL;150 static pj_cli_console_cfg console_cfg;151 152 152 /* Get input URL */ 153 153 static void get_input_url(char *buf, 154 154 int len, 155 155 pj_cli_cmd_val *cval, 156 struct input_result *result)156 struct input_result *result) 157 157 { 158 158 static const pj_str_t err_invalid_input = {"Invalid input\n", 15}; … … 203 203 204 204 pj_cli_sess_write_msg(cval->sess, err_invalid_input.ptr, 205 err_invalid_input.slen);205 err_invalid_input.slen); 206 206 result->nb_result = NO_NB; 207 207 return; … … 616 616 default: 617 617 param->cnt = 0; 618 } 619 } 620 621 void cli_console_app_main(const pj_str_t *uri_to_call, pj_bool_t *app_restart) 622 { 623 pjsua_call_setting_default(&call_opt); 624 call_opt.aud_cnt = app_config.aud_cnt; 625 call_opt.vid_cnt = app_config.vid.vid_cnt; 626 627 /* If user specifies URI to call, then call the URI */ 628 if (uri_to_call->slen) { 629 pjsua_call_make_call(current_acc, uri_to_call, &call_opt, NULL, 630 NULL, NULL); 631 } 632 633 /* 634 * Main loop. 635 */ 636 for (;;) { 637 char cmdline[PJ_CLI_MAX_CMDBUF]; 638 pj_status_t status; 639 640 status = pj_cli_console_process(sess, &cmdline[0], sizeof(cmdline)); 641 642 if (status == PJ_CLI_EEXIT) { 643 /* exit is called */ 644 break; 645 } else if (status != PJ_SUCCESS) { 646 /* Something wrong with the cmdline */ 647 PJ_PERROR(1,(THIS_FILE, status, "Exec error")); 648 } 649 } 650 *app_restart = pj_cli_is_restarting(cli); 618 break; 619 } 651 620 } 652 621 … … 795 764 { 796 765 pj_status_t status = PJ_SUCCESS; 766 767 CHECK_PJSUA_RUNNING(); 797 768 798 769 switch(pj_cli_get_cmd_id(cval->cmd)) { … … 1065 1036 pj_status_t status = PJ_SUCCESS; 1066 1037 1038 CHECK_PJSUA_RUNNING(); 1039 1067 1040 switch(pj_cli_get_cmd_id(cval->cmd)) { 1068 1041 case CMD_PRESENCE_ADD_BUDDY: … … 1107 1080 1108 1081 for (i=0; i<count; ++i) { 1109 char out_str[ PJSUA_MAX_CALLS*6];1110 char txlist[ PJSUA_MAX_CALLS*4+10];1082 char out_str[128]; 1083 char txlist[16]; 1111 1084 unsigned j; 1112 1085 pjsua_conf_port_info info; … … 1224 1197 pj_status_t status = PJ_SUCCESS; 1225 1198 1199 CHECK_PJSUA_RUNNING(); 1200 1226 1201 switch(pj_cli_get_cmd_id(cval->cmd)) { 1227 1202 case CMD_MEDIA_LIST: … … 1306 1281 { 1307 1282 pj_status_t status = PJ_SUCCESS; 1283 1284 CHECK_PJSUA_RUNNING(); 1308 1285 1309 1286 switch(pj_cli_get_cmd_id(cval->cmd)) { … … 1524 1501 { 1525 1502 if (current_call != PJSUA_INVALID_ID) { 1526 call_opt.flag |= PJSUA_CALL_UNHOLD;1527 1503 pjsua_call_update2(current_call, &call_opt, NULL); 1528 1504 } else { … … 1923 1899 pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd); 1924 1900 1901 CHECK_PJSUA_RUNNING(); 1902 1925 1903 switch(cmd_id) { 1926 1904 case CMD_CALL_NEW: … … 2012 1990 pjsua_acc_config acc_cfg; 2013 1991 pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd); 1992 1993 CHECK_PJSUA_RUNNING(); 2014 1994 2015 1995 pjsua_acc_get_config(current_acc, &acc_cfg); … … 2322 2302 pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd); 2323 2303 2304 CHECK_PJSUA_RUNNING(); 2305 2324 2306 switch(cmd_id) { 2325 2307 case CMD_VIDEO_ENABLE: … … 2415 2397 { 2416 2398 pj_status_t status = PJ_SUCCESS; 2417 PJ_UNUSED_ARG(cval); 2399 PJ_UNUSED_ARG(cval); 2400 2401 CHECK_PJSUA_RUNNING(); 2402 2418 2403 status = pjsua_detect_nat_type(); 2419 2404 if (status != PJ_SUCCESS) … … 2425 2410 static pj_status_t cmd_quit_handler(pj_cli_cmd_val *cval) 2426 2411 { 2412 extern pj_cli_on_quit cli_on_quit_cb; 2427 2413 PJ_LOG(3,(THIS_FILE, "Quitting app..")); 2428 pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_FALSE); 2414 pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_FALSE); 2415 /** Call pj_cli_on_quit callback **/ 2416 if (cli_on_quit_cb) 2417 (*cli_on_quit_cb)(PJ_FALSE); 2418 2429 2419 return PJ_SUCCESS; 2430 2420 } … … 2432 2422 static pj_status_t cmd_restart_handler(pj_cli_cmd_val *cval) 2433 2423 { 2424 extern pj_cli_on_quit cli_on_quit_cb; 2434 2425 PJ_LOG(3,(THIS_FILE, "Restarting app..")); 2435 pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_TRUE); 2426 pj_cli_quit(cval->sess->fe->cli, cval->sess, PJ_TRUE); 2427 /** Call pj_cli_on_quit callback **/ 2428 if (cli_on_quit_cb) 2429 (*cli_on_quit_cb)(PJ_TRUE); 2430 2431 return PJ_SUCCESS; 2432 } 2433 2434 /* 2435 * Syntax error handler for parser. 2436 */ 2437 static void on_syntax_error(pj_scanner *scanner) 2438 { 2439 PJ_UNUSED_ARG(scanner); 2440 PJ_THROW(PJ_EINVAL); 2441 } 2442 2443 static pj_status_t get_options(pj_str_t *options, unsigned *cmd_count) 2444 { 2445 pj_scanner scanner; 2446 pj_str_t str; 2447 2448 PJ_USE_EXCEPTION; 2449 2450 if (!options) 2451 return PJ_SUCCESS; 2452 2453 pj_scan_init(&scanner, options->ptr, options->slen, PJ_SCAN_AUTOSKIP_WS, 2454 &on_syntax_error); 2455 PJ_TRY { 2456 while (!pj_scan_is_eof(&scanner)) { 2457 ++(*cmd_count); 2458 pj_scan_get_until_chr(&scanner, " \t\r\n", &str); 2459 /** Store to command arguments **/ 2460 add_reload_config(*cmd_count, &str); 2461 } 2462 } 2463 PJ_CATCH_ANY { 2464 pj_scan_fini(&scanner); 2465 return PJ_GET_EXCEPTION(); 2466 } 2467 PJ_END; 2468 return PJ_SUCCESS; 2469 } 2470 2471 static pj_status_t cmd_reload_handler(pj_cli_cmd_val *cval) 2472 { 2473 int i; 2474 unsigned cmd_count = 0; 2475 extern pj_bool_t pjsua_restarted; 2476 extern pj_cli_on_restart_pjsua cli_on_restart_pjsua_cb; 2477 2478 PJ_LOG(3,(THIS_FILE, "Reloading Pjsua..")); 2479 /** Get the pjsua option **/ 2480 2481 for (i=1;i<cval->argc;i++) { 2482 get_options(&cval->argv[i], &cmd_count); 2483 } 2484 pjsua_restarted = PJ_TRUE; 2485 2486 if (cli_on_restart_pjsua_cb) 2487 (*cli_on_restart_pjsua_cb)(); 2488 2436 2489 return PJ_SUCCESS; 2437 2490 } … … 2832 2885 "<CMD name='restart' id='120' desc='Restart application'/>"; 2833 2886 2887 char* reload_command = 2888 "<CMD name='reload' id='130' desc='Reload pjsua'>" 2889 " <ARG name='options1' type='string' desc='Options' optional='1'/>" 2890 " <ARG name='options2' type='string' desc='Options' optional='1'/>" 2891 " <ARG name='options3' type='string' desc='Options' optional='1'/>" 2892 " <ARG name='options4' type='string' desc='Options' optional='1'/>" 2893 "</CMD>"; 2894 2834 2895 pj_status_t status; 2835 2896 pj_str_t sleep_xml = pj_str(sleep_command); … … 2837 2898 pj_str_t shutdown_xml = pj_str(shutdown_command); 2838 2899 pj_str_t restart_xml = pj_str(restart_command); 2900 pj_str_t reload_xml = pj_str(reload_command); 2839 2901 2840 2902 status = pj_cli_add_cmd_from_xml(cli, NULL, … … 2860 2922 &restart_xml, cmd_restart_handler, 2861 2923 NULL, NULL); 2924 if (status != PJ_SUCCESS) 2925 return status; 2926 2927 status = pj_cli_add_cmd_from_xml(cli, NULL, 2928 &reload_xml, cmd_reload_handler, 2929 NULL, NULL); 2862 2930 2863 2931 return status; 2864 2932 } 2865 2933 2866 static pj_status_t setup_command()2934 pj_status_t setup_command(pj_cli_t *cli) 2867 2935 { 2868 2936 pj_status_t status; … … 2898 2966 return status; 2899 2967 } 2900 2901 static void log_writer(int level, const char *buffer, int len)2902 {2903 if (cli)2904 pj_cli_write_log(cli, level, buffer, len);2905 }2906 2907 pj_status_t setup_cli()2908 {2909 pj_status_t status;2910 pj_cli_cfg_default(&cli_cfg);2911 cli_cfg.pf = app_config.pool->factory;2912 cli_cfg.name = pj_str("pjsua_cli");2913 cli_cfg.title = pj_str("Pjsua CLI Application");2914 2915 status = pj_cli_create(&cli_cfg, &cli);2916 if (status != PJ_SUCCESS)2917 return status;2918 2919 status = setup_command();2920 if (status != PJ_SUCCESS)2921 return status;2922 2923 pj_log_set_log_func(&log_writer);2924 2925 /*2926 * Init telnet2927 */2928 pj_cli_telnet_cfg_default(&telnet_cfg);2929 telnet_cfg.log_level = 5;2930 if (app_config.cli_telnet_port)2931 telnet_cfg.port = (pj_uint16_t)app_config.cli_telnet_port;2932 2933 status = pj_cli_telnet_create(cli, &telnet_cfg, NULL);2934 if (status != PJ_SUCCESS)2935 return status;2936 2937 app_config.cli_telnet_port = telnet_cfg.port;;2938 2939 /*2940 * Init console2941 */2942 pj_cli_console_cfg_default(&console_cfg);2943 console_cfg.quit_command = pj_str("shutdown");2944 status = pj_cli_console_create(cli, &console_cfg, &sess, NULL);2945 return status;2946 }2947 2948 void destroy_cli()2949 {2950 pj_log_set_log_func(&pj_log_write);2951 pj_cli_destroy(cli);2952 cli = NULL;2953 }2954
Note: See TracChangeset
for help on using the changeset viewer.