Ignore:
Timestamp:
Apr 5, 2013 3:02:19 AM (11 years ago)
Author:
riza
Message:

Re #1643: Code restructure+add callback to support symbian gui app

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/pjsua/pjsua_cli_cmd.c

    r4459 r4461  
    1919 */ 
    2020 
    21 #include "pjsua_cmd.h" 
     21#include "pjsua_common.h" 
    2222#include <pjlib-util/cli.h> 
    2323#include <pjlib-util/cli_imp.h> 
    2424#include <pjlib-util/cli_console.h> 
    2525#include <pjlib-util/cli_telnet.h> 
     26#include <pjlib-util/scanner.h> 
    2627 
    2728#define THIS_FILE       "pjsua_cli_cmd.c" 
     29 
     30#define CHECK_PJSUA_RUNNING() if (pjsua_get_state()!=PJSUA_STATE_RUNNING) \ 
     31                                  return PJ_EINVALIDOP 
    2832 
    2933/* CLI command id */ 
     
    3943#define CMD_NETWORK                 900 
    4044#define CMD_QUIT                    110 
     45#define CMD_RESTART                 120 
     46#define CMD_RELOAD                  130 
    4147 
    4248/* call level 2 command */ 
     
    144150#define DYN_CHOICE_ADDED_BUDDY_ID   (DYN_CHOICE_START)+12 
    145151 
    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  
    152152/* Get input URL */ 
    153153static void get_input_url(char *buf,  
    154154                          int len, 
    155155                          pj_cli_cmd_val *cval, 
    156 struct input_result *result) 
     156                          struct input_result *result) 
    157157{ 
    158158    static const pj_str_t err_invalid_input = {"Invalid input\n", 15}; 
     
    203203 
    204204        pj_cli_sess_write_msg(cval->sess, err_invalid_input.ptr,  
    205             err_invalid_input.slen); 
     205                              err_invalid_input.slen); 
    206206        result->nb_result = NO_NB; 
    207207        return; 
     
    616616    default: 
    617617        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    } 
    651620} 
    652621 
     
    795764{     
    796765    pj_status_t status = PJ_SUCCESS; 
     766 
     767    CHECK_PJSUA_RUNNING(); 
    797768 
    798769    switch(pj_cli_get_cmd_id(cval->cmd)) { 
     
    10651036    pj_status_t status = PJ_SUCCESS; 
    10661037 
     1038    CHECK_PJSUA_RUNNING(); 
     1039 
    10671040    switch(pj_cli_get_cmd_id(cval->cmd)) { 
    10681041    case CMD_PRESENCE_ADD_BUDDY: 
     
    11071080 
    11081081    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]; 
    11111084        unsigned j; 
    11121085        pjsua_conf_port_info info; 
     
    12241197    pj_status_t status = PJ_SUCCESS; 
    12251198 
     1199    CHECK_PJSUA_RUNNING(); 
     1200 
    12261201    switch(pj_cli_get_cmd_id(cval->cmd)) { 
    12271202    case CMD_MEDIA_LIST: 
     
    13061281{ 
    13071282    pj_status_t status = PJ_SUCCESS; 
     1283 
     1284    CHECK_PJSUA_RUNNING(); 
    13081285 
    13091286    switch(pj_cli_get_cmd_id(cval->cmd)) { 
     
    15241501{ 
    15251502    if (current_call != PJSUA_INVALID_ID) { 
    1526         call_opt.flag |= PJSUA_CALL_UNHOLD; 
    15271503        pjsua_call_update2(current_call, &call_opt, NULL); 
    15281504    } else { 
     
    19231899    pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd); 
    19241900 
     1901    CHECK_PJSUA_RUNNING(); 
     1902 
    19251903    switch(cmd_id) { 
    19261904    case CMD_CALL_NEW: 
     
    20121990    pjsua_acc_config acc_cfg; 
    20131991    pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd); 
     1992 
     1993    CHECK_PJSUA_RUNNING(); 
    20141994 
    20151995    pjsua_acc_get_config(current_acc, &acc_cfg); 
     
    23222302    pj_cli_cmd_id cmd_id = pj_cli_get_cmd_id(cval->cmd);     
    23232303 
     2304    CHECK_PJSUA_RUNNING(); 
     2305 
    23242306    switch(cmd_id) { 
    23252307    case CMD_VIDEO_ENABLE: 
     
    24152397{ 
    24162398    pj_status_t status = PJ_SUCCESS; 
    2417     PJ_UNUSED_ARG(cval);     
     2399    PJ_UNUSED_ARG(cval); 
     2400 
     2401    CHECK_PJSUA_RUNNING(); 
     2402     
    24182403    status = pjsua_detect_nat_type(); 
    24192404    if (status != PJ_SUCCESS) 
     
    24252410static pj_status_t cmd_quit_handler(pj_cli_cmd_val *cval) 
    24262411{ 
     2412    extern pj_cli_on_quit cli_on_quit_cb; 
    24272413    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 
    24292419    return PJ_SUCCESS; 
    24302420} 
     
    24322422static pj_status_t cmd_restart_handler(pj_cli_cmd_val *cval) 
    24332423{ 
     2424    extern pj_cli_on_quit cli_on_quit_cb; 
    24342425    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 */ 
     2437static void on_syntax_error(pj_scanner *scanner) 
     2438{ 
     2439    PJ_UNUSED_ARG(scanner); 
     2440    PJ_THROW(PJ_EINVAL); 
     2441} 
     2442 
     2443static 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 
     2471static 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 
    24362489    return PJ_SUCCESS; 
    24372490} 
     
    28322885        "<CMD name='restart' id='120' desc='Restart application'/>"; 
    28332886 
     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 
    28342895    pj_status_t status; 
    28352896    pj_str_t sleep_xml = pj_str(sleep_command);     
     
    28372898    pj_str_t shutdown_xml = pj_str(shutdown_command); 
    28382899    pj_str_t restart_xml = pj_str(restart_command); 
     2900    pj_str_t reload_xml = pj_str(reload_command); 
    28392901 
    28402902    status = pj_cli_add_cmd_from_xml(cli, NULL,  
     
    28602922                                     &restart_xml, cmd_restart_handler,  
    28612923                                     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); 
    28622930 
    28632931    return status; 
    28642932} 
    28652933 
    2866 static pj_status_t setup_command() 
     2934pj_status_t setup_command(pj_cli_t *cli) 
    28672935{ 
    28682936    pj_status_t status; 
     
    28982966    return status; 
    28992967} 
    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 telnet 
    2927     */  
    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 console 
    2941     */  
    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.