int main( int argc, char **argv ) { int32_t ret; char optstring[OPTSTRING_LEN]; int opt; int opt_preferred = 0; int opt_explicit = 0; int opt_ntsc = 0; int opt_sdtvon = 0; int opt_off = 0; int opt_modes = 0; int opt_monitor = 0; int opt_status = 0; int opt_audiosup = 0; int opt_dumpedid = 0; int opt_showinfo = 0; int opt_3d = 0; int opt_json = 0; int opt_name = 0; char *dumpedid_filename = NULL; VCHI_INSTANCE_T vchi_instance; VCHI_CONNECTION_T *vchi_connection; HDMI_RES_GROUP_T power_on_explicit_group = HDMI_RES_GROUP_INVALID; uint32_t power_on_explicit_mode; uint32_t power_on_explicit_drive = HDMI_MODE_HDMI; HDMI_RES_GROUP_T get_modes_group = HDMI_RES_GROUP_INVALID; SDTV_MODE_T sdtvon_mode = SDTV_MODE_NTSC; SDTV_ASPECT_T sdtvon_aspect = SDTV_ASPECT_UNKNOWN; // Initialize VCOS vcos_init(); // Create the option string that we will be using to parse the arguments create_optstring( optstring ); // Parse the command line arguments while (( opt = getopt_long_only( argc, argv, optstring, long_opts, NULL )) != -1 ) { switch ( opt ) { case 0: { // getopt_long returns 0 for entries where flag is non-NULL break; } case OPT_PREFERRED: { opt_preferred = 1; break; } case OPT_EXPLICIT: { char group_str[32], drive_str[32]; /* coverity[secure_coding] String length specified, so can't overflow */ int s = sscanf( optarg, "%31s %u %31s", group_str, &power_on_explicit_mode, drive_str ); if ( s != 2 && s != 3 ) { LOG_ERR( "Invalid arguments '%s'", optarg ); goto err_out; } // Check the group first if ( vcos_strcasecmp( "CEA", group_str ) == 0 ) { power_on_explicit_group = HDMI_RES_GROUP_CEA; } else if ( vcos_strcasecmp( "DMT", group_str ) == 0 ) { power_on_explicit_group = HDMI_RES_GROUP_DMT; } else if ( vcos_strcasecmp( "CEA_3D", group_str ) == 0 || vcos_strcasecmp( "CEA_3D_SBS", group_str ) == 0) { power_on_explicit_group = HDMI_RES_GROUP_CEA; opt_3d = 1; } else if ( vcos_strcasecmp( "CEA_3D_TB", group_str ) == 0 ) { power_on_explicit_group = HDMI_RES_GROUP_CEA; opt_3d = 2; } else if ( vcos_strcasecmp( "CEA_3D_FP", group_str ) == 0 ) { power_on_explicit_group = HDMI_RES_GROUP_CEA; opt_3d = 3; } else if ( vcos_strcasecmp( "CEA_3D_FS", group_str ) == 0 ) { power_on_explicit_group = HDMI_RES_GROUP_CEA; opt_3d = 4; } else { LOG_ERR( "Invalid group '%s'", group_str ); goto err_out; } if (s==3) { if (vcos_strcasecmp( "HDMI", drive_str ) == 0 ) { power_on_explicit_drive = HDMI_MODE_HDMI; } else if (vcos_strcasecmp( "DVI", drive_str ) == 0 ) { power_on_explicit_drive = HDMI_MODE_DVI; } else { LOG_ERR( "Invalid drive '%s'", drive_str ); goto err_out; } } // Then check if mode is a sane number if ( power_on_explicit_mode > MAX_MODE_ID ) { LOG_ERR( "Invalid mode '%u'", power_on_explicit_mode ); goto err_out; } opt_explicit = 1; break; } case OPT_NTSC: { opt_ntsc = 1; break; } case OPT_SDTVON: { char mode_str[32], aspect_str[32]; if ( sscanf( optarg, "%s %s", mode_str, aspect_str ) != 2 ) { LOG_ERR( "Invalid arguments '%s'", optarg ); goto err_out; } // Check the group first if ( vcos_strcasecmp( "NTSC", mode_str ) == 0 ) { sdtvon_mode = SDTV_MODE_NTSC; } else if ( vcos_strcasecmp( "NTSC_J", mode_str ) == 0 ) { sdtvon_mode = SDTV_MODE_NTSC_J; } else if ( vcos_strcasecmp( "PAL", mode_str ) == 0 ) { sdtvon_mode = SDTV_MODE_PAL; } else if ( vcos_strcasecmp( "PAL_M", mode_str ) == 0 ) { sdtvon_mode = SDTV_MODE_PAL_M; } else { LOG_ERR( "Invalid mode '%s'", mode_str ); goto err_out; } if ( vcos_strcasecmp( "4:3", aspect_str ) == 0 ) { sdtvon_aspect = SDTV_ASPECT_4_3; } else if ( vcos_strcasecmp( "14:9", aspect_str ) == 0 ) { sdtvon_aspect = SDTV_ASPECT_14_9; } else if ( vcos_strcasecmp( "16:9", aspect_str ) == 0 ) { sdtvon_aspect = SDTV_ASPECT_16_9; } opt_sdtvon = 1; break; } case OPT_OFF: { opt_off = 1; break; } case OPT_MODES: { if ( vcos_strcasecmp( "CEA", optarg ) == 0 ) { get_modes_group = HDMI_RES_GROUP_CEA; } else if ( vcos_strcasecmp( "DMT", optarg ) == 0 ) { get_modes_group = HDMI_RES_GROUP_DMT; } else { LOG_ERR( "Invalid group '%s'", optarg ); goto err_out; } opt_modes = 1; break; } case OPT_MONITOR: { opt_monitor = 1; break; } case OPT_STATUS: { opt_status = 1; break; } case OPT_AUDIOSUP: { opt_audiosup = 1; break; } case OPT_DUMPEDID: { opt_dumpedid = 1; dumpedid_filename = optarg; break; } case OPT_SHOWINFO: { opt_showinfo = atoi(optarg)+1; break; } case OPT_JSON: { opt_json = 1; break; } case OPT_NAME: { opt_name = 1; break; } default: { LOG_ERR( "Unrecognized option '%d'\n", opt ); goto err_usage; } case '?': case OPT_HELP: { goto err_usage; } } // end switch } // end while argc -= optind; argv += optind; if (( optind == 1 ) || ( argc > 0 )) { if ( argc > 0 ) { LOG_ERR( "Unrecognized argument -- '%s'", *argv ); } goto err_usage; } if (( opt_preferred + opt_explicit + opt_sdtvon > 1 )) { LOG_ERR( "Conflicting power on options" ); goto err_usage; } if ((( opt_preferred == 1 ) || ( opt_explicit == 1 ) || ( opt_sdtvon == 1)) && ( opt_off == 1 )) { LOG_ERR( "Cannot power on and power off simultaneously" ); goto err_out; } // Initialize the VCHI connection ret = vchi_initialise( &vchi_instance ); if ( ret != 0 ) { LOG_ERR( "Failed to initialize VCHI (ret=%d)", ret ); goto err_out; } ret = vchi_connect( NULL, 0, vchi_instance ); if ( ret != 0) { LOG_ERR( "Failed to create VCHI connection (ret=%d)", ret ); goto err_out; } // LOG_INFO( "Starting tvservice" ); // Initialize the tvservice vc_vchi_tv_init( vchi_instance, &vchi_connection, 1 ); if ( opt_monitor == 1 ) { LOG_STD( "Starting to monitor for HDMI events" ); if ( start_monitor() != 0 ) { goto err_stop_service; } } if ( opt_modes == 1 ) { if ( get_modes( get_modes_group, opt_json ) != 0 ) { goto err_stop_service; } } if ( opt_preferred == 1 ) { if(set_property( HDMI_PROPERTY_3D_STRUCTURE, HDMI_3D_FORMAT_NONE, 0) != 0) { goto err_stop_service; } if ( power_on_preferred() != 0 ) { goto err_stop_service; } } else if ( opt_explicit == 1 ) { //Distinguish between turning on 3D side by side and 3D top/bottom if(opt_3d == 0 && set_property( HDMI_PROPERTY_3D_STRUCTURE, HDMI_3D_FORMAT_NONE, 0) != 0) { goto err_stop_service; } else if(opt_3d == 1 && set_property( HDMI_PROPERTY_3D_STRUCTURE, HDMI_3D_FORMAT_SBS_HALF, 0) != 0) { goto err_stop_service; } else if(opt_3d == 2 && set_property( HDMI_PROPERTY_3D_STRUCTURE, HDMI_3D_FORMAT_TB_HALF, 0) != 0) { goto err_stop_service; } else if(opt_3d == 3 && set_property( HDMI_PROPERTY_3D_STRUCTURE, HDMI_3D_FORMAT_FRAME_PACKING, 0) != 0) { goto err_stop_service; } else if(opt_3d == 4 && set_property( HDMI_PROPERTY_3D_STRUCTURE, HDMI_3D_FORMAT_FRAME_SEQUENTIAL, 0) != 0) { goto err_stop_service; } if (set_property( HDMI_PROPERTY_PIXEL_CLOCK_TYPE, opt_ntsc ? HDMI_PIXEL_CLOCK_TYPE_NTSC : HDMI_PIXEL_CLOCK_TYPE_PAL, 0) != 0) { goto err_stop_service; } if ( power_on_explicit( power_on_explicit_group, power_on_explicit_mode, power_on_explicit_drive ) != 0 ) { goto err_stop_service; } } else if ( opt_sdtvon == 1 ) { if ( power_on_sdtv( sdtvon_mode, sdtvon_aspect ) != 0 ) { goto err_stop_service; } } else if (opt_off == 1 ) { if ( power_off() != 0 ) { goto err_stop_service; } } if ( opt_status == 1 ) { if ( get_status() != 0 ) { goto err_stop_service; } } if ( opt_audiosup == 1 ) { if ( get_audiosup() != 0 ) { goto err_stop_service; } } if ( opt_dumpedid == 1 ) { if ( dump_edid(dumpedid_filename) != 0 ) { goto err_stop_service; } } if ( opt_showinfo ) { if ( show_info(opt_showinfo-1) != 0 ) { goto err_stop_service; } } if ( opt_name == 1 ) { TV_DEVICE_ID_T id; memset(&id, 0, sizeof(id)); if(vc_tv_get_device_id(&id) == 0) { if(id.vendor[0] == '\0' || id.monitor_name[0] == '\0') { LOG_ERR( "No device present" ); } else { LOG_STD( "device_name=%s-%s", id.vendor, id.monitor_name); } } else { LOG_ERR( "Failed to obtain device name" ); } } if ( opt_monitor == 1 ) { // Wait until we get the signal to exit vcos_event_wait( &quit_event ); vcos_event_delete( &quit_event ); } err_stop_service: // LOG_INFO( "Stopping tvservice" ); // Stop the tvservice vc_vchi_tv_stop(); // Disconnect the VCHI connection vchi_disconnect( vchi_instance ); exit( 0 ); err_usage: show_usage(); err_out: exit( 1 ); }
// used for various testing static int rctest_main(int argc, char *argv[]) { int on; if (argc < 2) { _dprintf("test what?\n"); } else if (strcmp(argv[1], "rc_service")==0) { notify_rc(argv[2]); } else if(strcmp(argv[1], "get_phy_status")==0) { int mask; mask = atoi(argv[2]); TRACE_PT("debug for phy_status %x\n", get_phy_status(mask)); } else if(strcmp(argv[1], "get_phy_speed")==0) { int mask; mask = atoi(argv[2]); TRACE_PT("debug for phy_speed %x\n", get_phy_speed(mask)); } else if(strcmp(argv[1], "set_phy_ctrl")==0) { int mask, ctrl; mask = atoi(argv[2]); ctrl = atoi(argv[3]); TRACE_PT("debug for phy_speed %x\n", set_phy_ctrl(mask, ctrl)); } else if(strcmp(argv[1], "handle_notifications")==0) { handle_notifications(); } else if(strcmp(argv[1], "check_action")==0) { _dprintf("check: %d\n", check_action()); } else if(strcmp(argv[1], "nvramhex")==0) { int i; char *nv; nv = nvram_safe_get(argv[2]); _dprintf("nvram %s(%d): ", nv, strlen(nv)); for(i=0;i<strlen(nv);i++) { _dprintf(" %x", (unsigned char)*(nv+i)); } _dprintf("\n"); } else { on = atoi(argv[2]); _dprintf("%s %d\n", argv[1], on); if (strcmp(argv[1], "vlan") == 0) { if(on) start_vlan(); else stop_vlan(); } else if (strcmp(argv[1], "lan") == 0) { if(on) start_lan(); else stop_lan(); } else if (strcmp(argv[1], "wl") == 0) { if(on) { start_wl(); lanaccess_wl(); } } else if (strcmp(argv[1], "wan") == 0) { if(on) start_wan(); else stop_wan(); } else if (strcmp(argv[1], "wan_port") == 0) { if(on) start_wan_port(); else stop_wan_port(); } else if (strcmp(argv[1], "firewall") == 0) { //if(on) start_firewall(); //else stop_firewall(); } else if (strcmp(argv[1], "watchdog") == 0) { if(on) start_watchdog(); else stop_watchdog(); } #ifdef RTCONFIG_FANCTRL else if (strcmp(argv[1], "phy_tempsense") == 0) { if(on) start_phy_tempsense(); else stop_phy_tempsense(); } #endif #ifdef RTCONFIG_BCMWL6 #ifdef RTCONFIG_PROXYSTA else if (strcmp(argv[1], "psta_monitor") == 0) { if(on) start_psta_monitor(); else stop_psta_monitor(); } #endif #endif #ifdef RTCONFIG_IPERF else if (strcmp(argv[1], "monitor") == 0) { if(on) start_monitor(); else stop_monitor(); } #endif else if (strcmp(argv[1], "qos") == 0) {//qos test if(on){ #ifdef RTCONFIG_RALINK if (module_loaded("hw_nat")) { modprobe_r("hw_nat"); sleep(1); #if 0 system("echo 0 > /proc/sys/net/ipv4/conf/default/force_igmp_version"); system("echo 0 > /proc/sys/net/ipv4/conf/all/force_igmp_version"); #endif } #endif #ifdef RTCONFIG_TMOBILE_QOS add_EbtablesRules(); #else add_iQosRules(get_wan_ifname(0)); #endif #ifdef RTCONFIG_BWDPI if(nvram_get_int("qos_type") == 1) start_dpi_engine_service(); else #endif start_iQos(); } else { #ifdef RTCONFIG_RALINK if (nvram_get_int("hwnat") && /* TODO: consider RTCONFIG_DUALWAN case */ // !nvram_match("wan0_proto", "l2tp") && // !nvram_match("wan0_proto", "pptp") && // !(nvram_get_int("fw_pt_l2tp") || nvram_get_int("fw_pt_ipsec") && // (nvram_match("wl0_radio", "0") || nvram_get_int("wl0_mrate_x")) && // (nvram_match("wl1_radio", "0") || nvram_get_int("wl1_mrate_x")) && !module_loaded("hw_nat")) { #if 0 system("echo 2 > /proc/sys/net/ipv4/conf/default/force_igmp_version"); system("echo 2 > /proc/sys/net/ipv4/conf/all/force_igmp_version"); #endif #if defined(RTN14U) || defined(RTAC52U) || defined(RTAC51U) || defined(RTN11P) || defined(RTN54U) if (!(!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", ""))) #endif { modprobe("hw_nat"); sleep(1); } } #endif #ifdef RTCONFIG_BWDPI if(nvram_get_int("qos_type") == 1){ stop_dpi_engine_service(); } else #endif stop_iQos(); del_iQosRules(); } } #ifdef RTCONFIG_WEBDAV else if (strcmp(argv[1], "webdav") == 0) { if(on) start_webdav(); } #endif else if (strcmp(argv[1], "gpiow") == 0) { if(argc>=4) set_gpio(atoi(argv[2]), atoi(argv[3])); } else if (strcmp(argv[1], "gpior") == 0) { _dprintf("%d\n", get_gpio(atoi(argv[2]))); } else if (strcmp(argv[1], "gpiod") == 0) { if(argc>=4) gpio_dir(atoi(argv[2]), atoi(argv[3])); } else if (strcmp(argv[1], "init_switch") == 0) { init_switch(on); } else if (strcmp(argv[1], "set_action") == 0) { set_action(on); } else if (strcmp(argv[1], "pwr_usb") == 0) { set_pwr_usb(atoi(argv[2])); _dprintf("done.\n"); } #ifdef RTCONFIG_BCMFA else if (strcmp(argv[1], "fa_rev") == 0) { _dprintf("(%d) done.\n", get_fa_rev()); } else if (strcmp(argv[1], "fa_dump") == 0) { _dprintf("(%d) done.\n", get_fa_dump()); } #endif else { printf("what?\n"); } } return 0; }
int main(int argc, char **argv) { INIT_GLB_VARS(); SET_PROCESS_ROLE(PROCESS_ROLE_MASTER); SET_CHILD_PROCESS(PROCESS_ROLE_MASTER, getpid()); /* 此处注册清理函数, 以便主进程由于某些原因退出时, kill掉 * 启动的所有子进程. 但man atexit可知, 通过fork的子进程会 * 继承atexit的注册链, 而exec后将抛弃此注册链. * <NOTE> 此处不考虑fork子进程也执行此函数带来的影响 */ (void)atexit(kill_child_all); if (log_init() == RET_ERR) { SDNS_LOG_ERR("LOG init failed"); exit(EXIT_FAILURE); } if (zone_init() == RET_ERR) { SDNS_LOG_ERR("ZONE init failed"); exit(EXIT_FAILURE); } if (get_options(argc, argv) == RET_ERR) { SDNS_LOG_ERR("parse cmdline failed"); usage_help(); exit(EXIT_FAILURE); } if (IS_PROCESS_ROLE(PROCESS_ROLE_SIGNALLER)) { process_option_signal(); exit(EXIT_SUCCESS); } if (IS_PROCESS_ROLE(PROCESS_ROLE_HELPER)) { usage_help(); exit(EXIT_SUCCESS); } if (start_monitor() == RET_ERR) { exit(EXIT_FAILURE); } if (parse_conf() == RET_ERR) { exit(EXIT_FAILURE); } if (IS_PROCESS_ROLE(PROCESS_ROLE_TESTER)) { SDNS_LOG_DEBUG("配置文件测试OK"); print_parse_res(); exit(EXIT_SUCCESS); } if (pkt_engine_init() == RET_ERR) { SDNS_LOG_ERR("engine init failed"); exit(EXIT_FAILURE); } if (set_required_signal() == RET_ERR || block_required_signal() == RET_ERR) { SDNS_LOG_ERR("signal init failed"); exit(EXIT_FAILURE); } if (sort_init() == RET_ERR) { SDNS_LOG_ERR("sort init failed"); exit(EXIT_FAILURE); } start_worker(); if (start_pkt_engine() == RET_ERR) { SDNS_LOG_ERR("start engine failed"); exit(EXIT_FAILURE); } for(;;) { (void)wait_required_signal(); (void)process_signals(); } exit(EXIT_SUCCESS); }
// used for various testing static int rctest_main(int argc, char *argv[]) { int on; if (argc < 2) { _dprintf("test what?\n"); } else if (strcmp(argv[1], "rc_service")==0) { notify_rc(argv[2]); } else if(strcmp(argv[1], "get_phy_status")==0) { int mask; mask = atoi(argv[2]); TRACE_PT("debug for phy_status %x\n", get_phy_status(mask)); } else if(strcmp(argv[1], "get_phy_speed")==0) { int mask; mask = atoi(argv[2]); TRACE_PT("debug for phy_speed %x\n", get_phy_speed(mask)); } else if(strcmp(argv[1], "set_phy_ctrl")==0) { int mask, ctrl; mask = atoi(argv[2]); ctrl = atoi(argv[3]); TRACE_PT("debug for phy_speed %x\n", set_phy_ctrl(mask, ctrl)); } else if(strcmp(argv[1], "handle_notifications")==0) { handle_notifications(); } else if(strcmp(argv[1], "check_action")==0) { _dprintf("check: %d\n", check_action()); } else if(strcmp(argv[1], "nvramhex")==0) { int i; char *nv; nv = nvram_safe_get(argv[2]); _dprintf("nvram %s(%d): ", nv, strlen(nv)); for(i=0;i<strlen(nv);i++) { _dprintf(" %x", (unsigned char)*(nv+i)); } _dprintf("\n"); } else { on = atoi(argv[2]); _dprintf("%s %d\n", argv[1], on); if (strcmp(argv[1], "vlan") == 0) { if(on) start_vlan(); else stop_vlan(); } else if (strcmp(argv[1], "lan") == 0) { if(on) start_lan(); else stop_lan(); } else if (strcmp(argv[1], "wl") == 0) { if(on) { start_wl(); lanaccess_wl(); } } else if (strcmp(argv[1], "wan") == 0) { if(on) start_wan(); else stop_wan(); } else if (strcmp(argv[1], "wan_port") == 0) { if(on) start_wan_port(); else stop_wan_port(); } else if (strcmp(argv[1], "firewall") == 0) { //if(on) start_firewall(); //else stop_firewall(); } else if (strcmp(argv[1], "watchdog") == 0) { if(on) start_watchdog(); else stop_watchdog(); } #if ! (defined(RTCONFIG_QCA) || defined(RTCONFIG_RALINK)) else if (strcmp(argv[1], "watchdog02") == 0) { if(on) start_watchdog02(); else stop_watchdog02(); } #endif /* ! (RTCONFIG_QCA || RTCONFIG_RALINK) */ else if (strcmp(argv[1], "sw_devled") == 0) { if(on) start_sw_devled(); else stop_sw_devled(); } #ifdef RTCONFIG_FANCTRL else if (strcmp(argv[1], "phy_tempsense") == 0) { if(on) start_phy_tempsense(); else stop_phy_tempsense(); } #endif #ifdef RTCONFIG_BCMWL6 #ifdef RTCONFIG_PROXYSTA else if (strcmp(argv[1], "psta_monitor") == 0) { if(on) start_psta_monitor(); else stop_psta_monitor(); } #endif #endif #ifdef RTCONFIG_IPERF else if (strcmp(argv[1], "monitor") == 0) { if(on) start_monitor(); else stop_monitor(); } #endif else if (strcmp(argv[1], "qos") == 0) {//qos test if(on){ #ifdef RTCONFIG_RALINK if (module_loaded("hw_nat")) { modprobe_r("hw_nat"); sleep(1); #if 0 f_write_string("/proc/sys/net/ipv4/conf/default/force_igmp_version", "0", 0, 0); f_write_string("/proc/sys/net/ipv4/conf/all/force_igmp_version", "0", 0, 0); #endif } #endif add_iQosRules(get_wan_ifname(wan_primary_ifunit())); #ifdef RTCONFIG_BWDPI if(nvram_get_int("qos_type") == 1) { start_dpi_engine_service(); // force to rebuild firewall to avoid some loopback issue if (nvram_match("fw_nat_loopback", "2")) start_firewall(wan_primary_ifunit(), 0); } else #endif start_iQos(); } else { #ifdef RTCONFIG_RALINK if (nvram_get_int("hwnat") && /* TODO: consider RTCONFIG_DUALWAN case */ // !nvram_match("wan0_proto", "l2tp") && // !nvram_match("wan0_proto", "pptp") && // !(nvram_get_int("fw_pt_l2tp") || nvram_get_int("fw_pt_ipsec") && // (nvram_match("wl0_radio", "0") || nvram_get_int("wl0_mrate_x")) && // (nvram_match("wl1_radio", "0") || nvram_get_int("wl1_mrate_x")) && !module_loaded("hw_nat")) { #if 0 f_write_string("/proc/sys/net/ipv4/conf/default/force_igmp_version", "2", 0, 0); f_write_string("/proc/sys/net/ipv4/conf/all/force_igmp_version", "2", 0, 0); #endif #if defined(RTN14U) || defined(RTAC52U) || defined(RTAC51U) || defined(RTN11P) || defined(RTN300) || defined(RTN54U) || defined(RTAC1200HP) || defined(RTN56UB1) || defined(RTAC54U) || defined(RTN56UB2) if (!(!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", ""))) #endif { modprobe("hw_nat"); sleep(1); } } #endif #ifdef RTCONFIG_BWDPI if(nvram_get_int("qos_type") == 1){ stop_dpi_engine_service(1); } else #endif stop_iQos(); del_iQosRules(); } } #ifdef RTCONFIG_WEBDAV else if (strcmp(argv[1], "webdav") == 0) { if(on) start_webdav(); } #endif #ifdef RTCONFIG_TUNNEL else if (strcmp(argv[1], "mastiff") == 0) { if(on) start_mastiff(); } #endif else if (strcmp(argv[1], "gpiow") == 0) { if(argc>=4) set_gpio(atoi(argv[2]), atoi(argv[3])); } else if (strcmp(argv[1], "gpior") == 0) { printf("%d\n", get_gpio(atoi(argv[2]))); } else if (strcmp(argv[1], "gpiod") == 0) { if(argc>=4) gpio_dir(atoi(argv[2]), atoi(argv[3])); } else if (strcmp(argv[1], "init_switch") == 0) { init_switch(); } else if (strcmp(argv[1], "set_action") == 0) { set_action(on); } else if (strcmp(argv[1], "pwr_usb") == 0) { set_pwr_usb(atoi(argv[2])); _dprintf("done.\n"); } else if (strcmp(argv[1], "enc_chk") == 0) { unsigned char enc_buf[ENC_WORDS_LEN]; unsigned char dec_buf[DATA_WORDS_LEN + 1]; _dprintf("get enc str:[%s]\n", enc_str(argv[2], (char *) enc_buf)); _dprintf("get dec str:[%s]\n", dec_str((char *) enc_buf, (char *) dec_buf)); _dprintf("done(%d)\n", strcmp(argv[2], (const char *) dec_buf)); } #ifdef RTCONFIG_BCMFA else if (strcmp(argv[1], "fa_rev") == 0) { _dprintf("(%d) done.\n", get_fa_rev()); } else if (strcmp(argv[1], "fa_dump") == 0) { _dprintf("(%d) done.\n", get_fa_dump()); } #endif else { printf("what?\n"); } } return 0; }
int main( int argc, char **argv ) { int32_t ret; char optstring[OPTSTRING_LEN]; int opt; int opt_alloc = 0; int opt_status = 0; uint32_t alloc_size = 0; int opt_pid = -1; VCSM_STATUS_T status_mode = VCSM_STATUS_NONE; void *usr_ptr_1; unsigned int usr_hdl_1; #if defined(DOUBLE_ALLOC) || defined(RESIZE_ALLOC) void *usr_ptr_2; unsigned int usr_hdl_2; #endif // Initialize VCOS vcos_init(); vcos_log_set_level(&smem_log_category, VCOS_LOG_INFO); smem_log_category.flags.want_prefix = 0; vcos_log_register( "smem", &smem_log_category ); // Create the option string that we will be using to parse the arguments create_optstring( optstring ); // Parse the command line arguments while (( opt = getopt_long_only( argc, argv, optstring, long_opts, NULL )) != -1 ) { switch ( opt ) { case 0: { // getopt_long returns 0 for entries where flag is non-NULL break; } case OPT_ALLOC: { char *end; alloc_size = (uint32_t)strtoul( optarg, &end, 10 ); if (end == optarg) { vcos_log_info( "Invalid arguments '%s'", optarg ); goto err_out; } opt_alloc = 1; break; } case OPT_PID: { char *end; opt_pid = (int)strtol( optarg, &end, 10 ); if (end == optarg) { vcos_log_info( "Invalid arguments '%s'", optarg ); goto err_out; } break; } case OPT_STATUS: { char status_str[32]; /* coverity[secure_coding] String length specified, so can't overflow */ if ( sscanf( optarg, "%31s", status_str ) != 1 ) { vcos_log_info( "Invalid arguments '%s'", optarg ); goto err_out; } if ( vcos_strcasecmp( status_str, "all" ) == 0 ) { status_mode = VCSM_STATUS_VC_MAP_ALL; } else if ( vcos_strcasecmp( status_str, "vc" ) == 0 ) { status_mode = VCSM_STATUS_VC_WALK_ALLOC; } else if ( vcos_strcasecmp( status_str, "map" ) == 0 ) { status_mode = VCSM_STATUS_HOST_WALK_MAP; } else if ( vcos_strcasecmp( status_str, "host" ) == 0 ) { status_mode = VCSM_STATUS_HOST_WALK_PID_ALLOC; } else { goto err_out; } opt_status = 1; break; } default: { vcos_log_info( "Unrecognized option '%d'", opt ); goto err_usage; } case '?': case OPT_HELP: { goto err_usage; } } // end switch } // end while argc -= optind; argv += optind; if (( optind == 1 ) || ( argc > 0 )) { if ( argc > 0 ) { vcos_log_info( "Unrecognized argument -- '%s'", *argv ); } goto err_usage; } // Start the shared memory support. if ( vcsm_init() == -1 ) { vcos_log_info( "Cannot initialize smem device" ); goto err_out; } if ( opt_alloc == 1 ) { vcos_log_info( "Allocating 2 times %u-bytes in shared memory", alloc_size ); usr_hdl_1 = vcsm_malloc( alloc_size, "smem-test-alloc" ); vcos_log_info( "Allocation 1 result: user %x, vc-hdl %x", usr_hdl_1, vcsm_vc_hdl_from_hdl( usr_hdl_1 ) ); #if defined(DOUBLE_ALLOC) || defined(RESIZE_ALLOC) usr_hdl_2 = vcsm_malloc( alloc_size, NULL ); vcos_log_info( "Allocation 2 result: user %x", usr_hdl_2 ); usr_ptr_2 = vcsm_lock( usr_hdl_2 ); vcos_log_info( "Allocation 2 : lock %p", usr_ptr_2 ); vcos_log_info( "Allocation 2 : unlock %d", vcsm_unlock_hdl( usr_hdl_2 ) ); #endif // Do a simple write/read test. if ( usr_hdl_1 != 0 ) { usr_ptr_1 = vcsm_lock( usr_hdl_1 ); vcos_log_info( "Allocation 1 : lock %p", usr_ptr_1 ); if ( usr_ptr_1 ) { memset ( usr_ptr_1, 0, alloc_size ); memcpy ( usr_ptr_1, blah_blah, 32 ); vcos_log_info( "Allocation 1 contains: \"%s\"", (char *)usr_ptr_1 ); vcos_log_info( "Allocation 1: vc-hdl %x", vcsm_vc_hdl_from_ptr ( usr_ptr_1 ) ); vcos_log_info( "Allocation 1: usr-hdl %x", vcsm_usr_handle ( usr_ptr_1 ) ); vcos_log_info( "Allocation 1 : unlock %d", vcsm_unlock_ptr( usr_ptr_1 ) ); } usr_ptr_1 = vcsm_lock( usr_hdl_1 ); vcos_log_info( "Allocation 1 (relock) : lock %p", usr_ptr_1 ); if ( usr_ptr_1 ) { vcos_log_info( "Allocation 1 (relock) : unlock %d", vcsm_unlock_hdl( usr_hdl_1 ) ); } } #if defined(RESIZE_ALLOC) ret = vcsm_resize( usr_hdl_1, 2 * alloc_size ); vcos_log_info( "Allocation 1 : resize %d", ret ); if ( ret == 0 ) { usr_ptr_1 = vcsm_lock( usr_hdl_1 ); vcos_log_info( "Allocation 1 (resize) : lock %p", usr_ptr_1 ); if ( usr_ptr_1 ) { memset ( usr_ptr_1, 0, 2 * alloc_size ); memcpy ( usr_ptr_1, blah_blah, 32 ); vcos_log_info( "Allocation 1 (resized) contains: \"%s\"", (char *)usr_ptr_1 ); vcos_log_info( "Allocation 1 (resized) : unlock %d", vcsm_unlock_ptr( usr_ptr_1 ) ); } } // This checks that the memory can be remapped properly // because the Block 1 expanded beyond Block 2 boundary. // usr_ptr_2 = vcsm_lock( usr_hdl_2 ); vcos_log_info( "Allocation 2 : lock %p", usr_ptr_2 ); vcos_log_info( "Allocation 2 : unlock %d", vcsm_unlock_hdl( usr_hdl_2 ) ); // This checks that we can free a memory block even if it // is locked, which could be the case if the application // dies. // usr_ptr_2 = vcsm_lock( usr_hdl_2 ); vcos_log_info( "Allocation 2 : lock %p", usr_ptr_2 ); vcsm_free ( usr_hdl_2 ); #endif #if defined(DOUBLE_ALLOC) #endif } if ( opt_status == 1 ) { get_status( status_mode, opt_pid ); } // If we allocated something, wait for the signal to exit to give chance for the // user to poke around the allocation test. // if ( opt_alloc == 1 ) { start_monitor(); vcos_event_wait( &quit_event ); vcos_event_delete( &quit_event ); } // Terminate the shared memory support. vcsm_exit (); goto err_out; err_usage: show_usage(); err_out: exit( 1 ); }