📄 bcastinject.java
字号:
/* @(#)BcastInject.java * * "Copyright (c) 2001 and The Regents of the University * of California. All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice and the following * two paragraphs appear in all copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * $\Id$ *//** * * * @author <a href="mailto:szewczyk@sourceforge.net">Robert Szewczyk</a> */package net.tinyos.tools;import net.tinyos.util.*;import java.io.*;import java.util.Properties;public class BcastInject { static Properties p = new Properties(); public static final int LED_ON = 1; public static final int LED_OFF = 2; public static final int RADIO_LOUDER = 3; public static final int RADIO_QUIETER = 4; public static final int START_SENSING = 5; public static final int READ_LOG = 6; public static final int YELLOW_LED = 1; public static final int GREEN_LED = 1; public static final int RED_LED = 1; public static final short TOS_BCAST_ADDR = (short) 0xffff; public static final byte MSG_TYPE = 8; public static void usage() { System.err.println("Usage: java net.tinyos.utils.Bcast inject"+ " [forwarder_address port] group_id command"); System.err.println("\twhere command is one of the following:"); System.err.println("\t\tled_on"); System.err.println("\t\tled_off"); System.err.println("\t\tradio_louder"); System.err.println("\t\tradio_quieter"); } public static byte restoreSequenceNo() { try { FileInputStream fis = new FileInputStream("bcast.properties"); p.load(fis); byte i = (byte)Integer.parseInt(p.getProperty("sequenceNo", "1")); fis.close(); return i; } catch (IOException e) { p.setProperty("sequenceNo", "1"); return 1; } } public static void saveSequenceNo(int i) { try { FileOutputStream fos = new FileOutputStream("bcast.properties"); p.setProperty("sequenceNo", Integer.toString(i)); p.store(fos, "#Properties for BcastInject\n"); } catch (IOException e) { System.err.println("Exception while saving sequence number" + e); e.printStackTrace(); } } public static void main(String[] argv) throws IOException{ String host = "localhost"; int port = 9000; String cmd = ""; byte group_id = 0; int cmd_offset = 0; if (argv.length == 2) { group_id = (byte) Integer.parseInt(argv[0]); cmd = argv[1]; cmd_offset = 1; } else if (argv.length == 3) { host = argv[0]; port = Integer.parseInt(argv[1]); group_id = (byte)Integer.parseInt(argv[2]); cmd = argv[3]; cmd_offset = 3; } else if (argv.length < 2){ usage(); System.exit(-1); } else { host = argv[0]; port = Integer.parseInt(argv[1]); group_id = (byte)Integer.parseInt(argv[2]); cmd = argv[3]; cmd_offset = 3; } SerialForwarderStub rw = new SerialForwarderStub(host, port); byte [] packet = new byte[SerialForwarderStub.PACKET_SIZE]; byte command = 0; byte command_args = 0; byte sequenceNo = 0; if (cmd.equals("led_on")) { command = LED_ON; } else if (cmd.equals("led_off")) { command = LED_OFF; } else if (cmd.equals("radio_louder")) { command = RADIO_LOUDER; } else if (cmd.equals("radio_quieter")) { command = RADIO_QUIETER; } else if (cmd.equals("start_sensing")) { command = START_SENSING; int nsamples = Integer.parseInt(argv[cmd_offset + 1]); packet[9] = (byte)(nsamples & 0xff); // number of data points packet[10] = (byte) ((nsamples >> 8) & 0xff); packet[11] = (byte) (Integer.parseInt(argv[cmd_offset + 2]) & 0xff); packet[12] = (byte) (Integer.parseInt(argv[cmd_offset + 3]) & 0xff); } else if (cmd.equals("read_log")) { command = READ_LOG; int address = Integer.parseInt(argv[cmd_offset + 1]); int line = Integer.parseInt(argv[cmd_offset + 2]); packet[9] = (byte) (address & 0xff); packet[10] = (byte) ((address>>8) & 0xff); packet[11] = (byte) (line & 0xff); packet[12] = (byte) ((line>>8) & 0xff); } else { usage(); System.exit(-1); } sequenceNo = restoreSequenceNo(); //Generic message header, destination, group id, and message type packet[0] = (byte) ((TOS_BCAST_ADDR >> 8) & 0xff); packet[1] = (byte) (TOS_BCAST_ADDR & 0xff); packet[2] = MSG_TYPE; packet[3] = group_id; // BCast specific information: sequence number. packet[4] = sequenceNo++; // Commands to be executed: command and args packet[5] = command; packet[6] = (byte) 0xff; packet[7] = (byte) 0xfb; packet[8] = (byte) 0; rw.Open(); rw.Write(packet); for (int i = 0; i < SerialForwarderStub.PACKET_SIZE; i++) { System.out.print(Integer.toHexString(packet[i] & 0xff)+ " "); } System.out.println(); saveSequenceNo(sequenceNo); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -