📄 gatewaymote.java
字号:
/* ex: set tabstop=4 shiftwidth=4 expandtab:*/ /* $Id: GatewayMote.java,v 1.9 2006/10/03 06:51:26 maoy Exp $ *//** * @author Rodrigo Fonseca, based on BCastInject by Robert Szewczyk adopted by Yun Mao for S4 */package net.tinyos.cr;import net.tinyos.util.*;import java.io.*;import java.util.*;import net.tinyos.message.*;import net.tinyos.cr.messages.*;import net.tinyos.packet.*;import net.tinyos.util.*;public class GatewayMote extends Mote implements MessageListener, S4Constants { static Properties p = new Properties(); public static final int TIMEOUT = 1000; public boolean response_received = false; public int gateway_addr; public boolean verbose = false; public String propertiesFileName; public S4CommandResponseMessage response; public MoteIF mote; public S4CommandResponseMessage cmd_result; public boolean bPrintMsg; public GatewayMote(int id, String name) { gateway_addr = id; propertiesFileName = new String(name + ".properties"); getSerialForwarderSetting(); int port = baseport+gateway_addr; if (verbose){ System.err.println("Starting connection to mote at "+host+":"+port); } PhoenixSource source = BuildSource.makePhoenix("sf@"+host+":"+port, PrintStreamMessenger.err); mote = new MoteIF(source); //MoteIF mote = new MoteIF(PrintStreamMessenger.err); // Need to wait for a response message to come back response = new S4CommandResponseMessage(); mote.registerListener(response, this); } public short restoreSequenceNo() { try { //FileInputStream fis = new FileInputStream("cmd.properties"); FileInputStream fis = new FileInputStream(propertiesFileName); p.load(fis); short i = (short)Integer.parseInt(p.getProperty("sequenceNo", "1")); fis.close(); return i; } catch (IOException e) { p.setProperty("sequenceNo", "1"); return 1; } } public void saveSequenceNo(int i) { try { //FileOutputStream fos = new FileOutputStream("cmd.properties"); FileOutputStream fos = new FileOutputStream(propertiesFileName); p.setProperty("sequenceNo", Integer.toString(i)); p.store(fos, "#Properties for GatewayMote\n"); } catch (IOException e) { System.err.println("Exception while saving sequence number" + e); e.printStackTrace(); } } public S4CommandResponseMessage runCommand(String [] argv, boolean printMsg) { S4CommandMessage command = new S4CommandMessage(); short sequenceNo = 0; response_received = false; bPrintMsg = printMsg; short mote_id = (short)Integer.parseInt(argv[0]); String cmd = argv[1]; if (verbose) System.out.println("via gateway "+gateway_addr); sequenceNo = restoreSequenceNo(); command.set_header_last_hop(TOS_UART_ADDR); command.set_header_seqno(sequenceNo); command.set_type_data_hopcount((short)1); //command.set_type_data_origin(TOS_UART_ADDR); //command.set_type_data_origin(mote_id); //int gateway_addr = 3; command.set_type_data_gateway_addr(gateway_addr); command.set_type_data_cmd_addr(mote_id); command.set_type_data_data_seqno(sequenceNo); command.set_type_data_data_flags((short)0); if (cmd.equals("hello")) { command.set_type_data_type(S4_CMD_HELLO); mote_id = TOS_BCAST_ADDR; } else if (cmd.equals("led_on")) { command.set_type_data_type(S4_CMD_LED_ON); } else if (cmd.equals("led_off")) { command.set_type_data_type(S4_CMD_LED_OFF); } else if (cmd.equals("set_root")) { if (argv.length != 3) { return null; } command.set_type_data_type(S4_CMD_SET_ROOT_BEACON); short byte_arg = (byte)Integer.parseInt(argv[2]); command.set_type_data_data_args_byte_arg(byte_arg); } else if (cmd.equals("get_root")) { command.set_type_data_type(S4_CMD_IS_ROOT_BEACON); }/* else if (cmd.equals("set_coords")) { if (argv.length < 4) { usage(); System.exit(-1); } command.set_type_data_type(S4_CMD_SET_COORDS); //short valid = 0; for (int i = 3; i < argv.length && i < 20; i++) { //valid |= 1 << (i-3); command.setElement_type_data_data_args_dest_coords_comps( i-3,(short)Integer.parseInt(argv[i]) ); } //command.set_type_data_data_args_dest_coords_valid(valid); }*/ else if (cmd.equals("get_coords")) { command.set_type_data_type(S4_CMD_GET_COORDS); } else if (cmd.equals("set_radio")) { if (argv.length != 3) { return null; } command.set_type_data_type(S4_CMD_SET_RADIO_PWR); short byte_arg = (short)Integer.parseInt(argv[2]); command.set_type_data_data_args_byte_arg(byte_arg); } else if (cmd.equals("get_radio")) { command.set_type_data_type(S4_CMD_GET_RADIO_PWR); } else if (cmd.equals("set_fr_timer")) { if (argv.length != 3) { return null; } command.set_type_data_type(S4_CMD_SET_FR_TIMER); int short_arg = Integer.parseInt(argv[2]); command.set_type_data_data_args_short_arg(short_arg); } else if (cmd.equals("shutdown")) { if (argv.length != 3) { return null; } command.set_type_data_type(S4_CMD_SHUTDOWN_RADIO); int short_arg = Integer.parseInt(argv[2]); command.set_type_data_data_args_short_arg(short_arg); } else if (cmd.equals("get_fr_timer")) { command.set_type_data_type(S4_CMD_GET_FR_TIMER); } else if (cmd.equals("get_info")) { command.set_type_data_type(S4_CMD_GET_INFO); } else if (cmd.equals("dump_link")) {//dump link qualities /*new*/ if (argv.length != 2) { return null; } String [] tmp_argv = new String[2]; tmp_argv[0] = argv[0]; tmp_argv[1] = "get_info"; S4CommandResponseMessage bcrm = runCommand(tmp_argv, printMsg); int link_size = bcrm.get_type_data_data_args_info_links(); String [] new_argv = new String[3]; new_argv[0] = argv[0]; new_argv[1] = "get_link_info"; for (int i=0;i<link_size;i++){ new_argv[2] = Integer.toString(i); bcrm = runCommand(new_argv, false); if (bcrm==null) continue; if (bcrm.get_type_data_data_args_routing_table_entry_valid()>0) { System.out.println("#"+i+"dump_link:::src:"+ argv[0]+ ",dest:"+ bcrm.get_type_data_data_args_link_info_addr() +",quality:"+ bcrm.get_type_data_data_args_link_info_quality() +",reverse_q:"+ bcrm.get_type_data_data_args_link_info_reverse_quality() ); }else { System.out.println("#"+i+"invalid"); } } return null; } else if (cmd.equals("dump_rt")) { //dump routing table entries /*new*/ if (argv.length != 2) { return null; } argv[1] = "get_info"; S4CommandResponseMessage bcrm = runCommand(argv, printMsg); int rt_size = bcrm.get_type_data_data_args_info_routing_table_size(); String [] new_argv = new String[3]; new_argv[0] = argv[0]; new_argv[1] = "get_rt"; for (int i=0;i<rt_size;i++){ new_argv[2] = Integer.toString(i); bcrm = runCommand(new_argv, false); if (bcrm==null) continue; if (bcrm.get_type_data_data_args_routing_table_entry_valid()>0) { System.out.println("#"+i+" dest:"+ bcrm.get_type_data_data_args_routing_table_entry_dest() +"\tparent:"+ bcrm.get_type_data_data_args_routing_table_entry_parent() +"\thop:"+ bcrm.get_type_data_data_args_routing_table_entry_hops() +"\tscope:"+ bcrm.get_type_data_data_args_routing_table_entry_scope() ); }else { System.out.println("#"+i+"invalid"); } } return null; } else if (cmd.equals("dump_root")) { /*new*/ if (argv.length != 2) { return null; } int root_size = 3; //!!!! number of beacons String [] new_argv = new String[3]; new_argv[0] = argv[0]; new_argv[1] = "get_root_info"; for (int i=0;i<root_size;i++){ new_argv[2] = Integer.toString(i); S4CommandResponseMessage bcrm = runCommand(new_argv, false); if (bcrm==null) continue; if (bcrm.get_type_data_data_args_root_info_valid()>0) { System.out.println("#"+i +"\tparent:"+ bcrm.get_type_data_data_args_root_info_parent() +"\thop:"+ bcrm.get_type_data_data_args_root_info_hops() ); }else { System.out.println("#"+i+"invalid"); } } return null; } else if (cmd.equals("get_neighbor")) { if (argv.length != 3) { return null; } command.set_type_data_type(S4_CMD_GET_NEIGHBOR); short byte_arg = (short)Integer.parseInt(argv[2]); command.set_type_data_data_args_byte_arg(byte_arg); } else if (cmd.equals("get_link_info")) { if (argv.length != 3) { return null; } command.set_type_data_type(S4_CMD_GET_LINK_INFO); short byte_arg = (short)Integer.parseInt(argv[2]); command.set_type_data_data_args_byte_arg(byte_arg); } else if (cmd.equals("get_rt")) { if (argv.length != 3) { return null; } command.set_type_data_type(S4_CMD_GET_ROUTING_TABLE); short byte_arg = (short)Integer.parseInt(argv[2]); command.set_type_data_data_args_byte_arg(byte_arg); //added by Feng Wang on Sept. 28 } else if (cmd.equals("get_stats")) { if (argv.length != 2) { return null; } command.set_type_data_type(S4_CMD_GET_STATS); } else if (cmd.equals("get_root_info")) { if (argv.length != 3) { return null; } command.set_type_data_type(S4_CMD_GET_ROOT_INFO);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -