codeinjector.java

来自「无线传感器网络操作系统源代码」· Java 代码 · 共 864 行 · 第 1/2 页

JAVA
864
字号
    public synchronized void multicheck(int nnodes) throws IOException {	boolean redo = false;	int n;	for (int i = 0; i < (MAX_CAPSULES/8); i+=16) {	    awaitingResponse = nnodes;	    n = 0;	    while ((awaitingResponse > 0) && (n < nretries)) {		if (debug > 0)		    System.out.print("+");		n++;		readCapsule((short)-1, MAX_CODE_SIZE+i, 1);		try {		    wait(400);		} catch (InterruptedException e) {		    System.err.println("Interrupted wait:"+e);		    e.printStackTrace();		}	    }	}			if (debug >0) 	    System.out.print("\nMissing packets:");		for (int q= 0; q < nrepeats; q++) {	for (int i =0; i < ((length+15)>>4); i++) {	    if (!packets_received[i]) {		if (debug >0)		    System.out.print(i+" ");		if (logPanel != null) {		    if (i % 10 == 0) {logPanel.repaint();}		}		redo = true;		sendCapsule((short)-1, i * 16);		try {		    Thread.currentThread().sleep(80);		} catch (Exception e){}	    }	}}			int capsule = MAX_CODE_SIZE +(MAX_CAPSULES/8);	byte [] packet = new byte[MSG_LENGTH];	short node = -1;	packet[0] = (byte) (node & 0xff);	packet[1] = (byte) ((node >> 8) & 0xff);	packet[2] = MSG_WRITE;	packet[3] = group_id;	packet[5] = (byte) ((prog_id >> 8) & 0xff); 	packet[4] = (byte) (prog_id & 0xff);        	packet[7] = (byte) ((capsule >> 8) & 0xff);	packet[6] = (byte) (capsule & 0xff);          	preparePacket(packet);	serialStub.Write(packet);	if (redo) {	    for (int i = 0; i < MAX_CAPSULES; i++) {		packets_received[i] = true;	    }	    multicheck(nnodes);	}    }	    public void id(short node) throws IOException {	readCapsule(node, 32768 - 64);	if (logPanel == null){	    try {		Thread.currentThread().sleep(3000);	    } catch (Exception e) {}}    }    public void setId(short node, short newID) {	byte [] packet = new byte[MSG_LENGTH];	int capsule = 32768 - 64;	packet[0] = (byte) (node & 0xff);	packet[1] = (byte) ((node >> 8) & 0xff);	packet[2] = MSG_WRITE;	packet[3] = group_id;	packet[5] = (byte) ((prog_id >> 8) & 0xff); 	packet[4] = (byte) (prog_id & 0xff);        	packet[7] = (byte) ((capsule >> 8) & 0xff);	packet[6] = (byte) (capsule & 0xff);          	packet[9] = (byte) ((newID >> 8) & 0xff);	packet[8] = (byte)(newID & 0xff);	packet[11] = (byte) ((prog_id >> 8) & 0xff); 	packet[10] = (byte) (prog_id & 0xff);        	packet[13] = (byte) ((length >> 8) & 0xff); 	packet[12] = (byte) (length & 0xff);        	try {	    preparePacket(packet);	    serialStub.Write(packet); 	} catch (Exception e ) {	    System.err.println("Something bad happened in setID"+e);	    e.printStackTrace();	}    }    public void runOption(byte groupID,			  short moteID,			  short src,			  byte type,			  short prob,			  byte pot) {	byte [] packet = new byte[MSG_LENGTH];	int capsule = 32768 - 64;		packet[0] = (byte) (moteID & 0xff);        //mote ID	packet[1] = (byte) ((moteID & 0xff00) >> 8); //mote ID	packet[2] = MSG_RUN;                                   //AM type	packet[3] = group_id;                                  //group ID	packet[4] = (byte) (src & 0xff);                       //src	packet[5] = (byte) ((src >> 8) & 0xff);                //src	packet[6] = 1;                                         //hop-count	packet[7] = 55;                                        //exp_id	packet[8] = (byte) (prob & 0xff);                      //prob	packet[9] = (byte) ((prob >> 8) & 0xff);               //prob	packet[10] = pot;                                      //pot        	packet[11] = type;                                     //comm	try {	    preparePacket(packet);	    for(int i=0;i<14;i++){		String datum = Integer.toString((int)(packet[i] & 0xff), 16);		if (datum.length() == 1) {datum = "0" + datum;}		datum += " ";		System.out.print(datum);	    }	    serialStub.Write(packet); 	} catch (Exception e ) {	    System.err.println("Something bad happened in setID"+e);	    e.printStackTrace();	}    }	    public void startProgram(short node)  throws IOException{	byte [] packet = new byte[MSG_LENGTH];	packet[0] = (byte) (node & 0xff);	packet[1] = (byte) ((node >> 8) & 0xff);	packet[2] = MSG_START;	packet[3] = group_id;	packet[5] = (byte) ((prog_id >> 8) & 0xff); 	packet[4] = (byte) (prog_id & 0xff);        	//	packet[6] = (byte) ((i >> 8) & 0xff);	//	packet[5] = (byte) (i & 0xff);          	preparePacket(packet);	serialStub.Write(packet);    }    public void fillBitmap(short node) throws IOException{	byte bitmap[] = new byte[MAX_CAPSULES/8]; 	for (int i = 0; i < (MAX_CAPSULES/8); i++) {	    bitmap[i] = (byte)0xff;	}	for (int i =0; i < ((length+15)>>4); i++) {	    bitmap[(i >> 3) & ((MAX_CAPSULES/8) -1)] &= (byte)(~(1 << (i & 0x7)));	}	for (int i = 0; i < (MAX_CAPSULES/8); i++) {	    System.out.print(Integer.toHexString(bitmap[i] & 0xff) + " "); 	}	System.out.println();	for (int i = 0; i < (MAX_CAPSULES / 128); i++) {	    byte [] packet = new byte[MSG_LENGTH];	    int capsule = MAX_CODE_SIZE + (i * 16);	    packet[0] = (byte) (node & 0xff);	    packet[1] = (byte) ((node >> 8) & 0xff);	    packet[2] = MSG_WRITE;	    packet[3] = group_id;	    packet[5] = (byte) ((prog_id >> 8) & 0xff); 	    packet[4] = (byte) (prog_id & 0xff);        	    packet[7] = (byte) ((capsule >> 8) & 0xff);	    packet[6] = (byte) (capsule & 0xff);          	    System.arraycopy(bitmap, i*16, packet, 8, 16);	    int any = 0;	    for (int j = 0; j < 16; j++) {		any |= packet[7+j];	    }	    if (any != 0) {		if (debug > 0)		    System.out.print("+");		preparePacket(packet);		serialStub.Write(packet);		try {		    Thread.currentThread().sleep(300);		} catch (Exception e) {}	    } else {		if (debug > 0)		    System.out.print("?");	    }	}    }    public void turnOn(short node) throws IOException {	System.out.println("Turning on " + node);	runOption(group_id, node, (short)0, (byte)1, (short)100, (byte)10);	    }    public void turnOff(short node) throws IOException {	System.out.println("Turning off " + node);	runOption(group_id, node, (short)0, (byte)2, (short)100, (byte)10);    }    public void turnAllOn() throws IOException {	System.out.println("Turning all motes in group " + getGroupID() + " on.");	runOption(group_id, (short)0xffff, (short)0, (byte)1, (short)100, (byte)10);    }    public void turnAllOff() throws IOException {	System.out.println("Turning all motes in group " + getGroupID() + " off.");	runOption(group_id, (short)0xffff, (short)0, (byte)2, (short)100, (byte)10);    }        public void newProgram(short node)  throws IOException{	byte [] packet = new byte[MSG_LENGTH];	packet[0] = (byte) (node & 0xff);	packet[1] = (byte) ((node >> 8) & 0xff);	packet[2] = MSG_NEW_PROG;	packet[3] = group_id;	packet[5] = (byte) ((prog_id >> 8) & 0xff); 	packet[4] = (byte) (prog_id & 0xff);        	packet[7] = (byte) ((length >> 8) & 0xff);	packet[6] = (byte) (length & 0xff);          	preparePacket(packet);	serialStub.Write(packet);	fillBitmap(node);    }    private void printPacket(byte[] packet) {	System.out.print("Packet:");	for (int i = 0; i < packet.length; i++) {	    if (i % 16 == 0) {System.out.println();}	    String val = Integer.toHexString((int)(packet[i] & 0xff));	    for (int j = 0; j < (3 - val.length()); j++) {		System.out.print(" ");	    }	    	    System.out.print(val);	}	System.out.println();    }    public void readCapsule(short node, int capsule) throws IOException {	readCapsule(node, capsule, 0);    }    public void readCapsule(short node, int capsule, int check) throws IOException {		byte [] packet = new byte[MSG_LENGTH];	packet[0] = (byte) (node & 0xff);	packet[1] = (byte) ((node >> 8) & 0xff);	packet[2] = MSG_READ;	packet[3] = group_id;	packet[5] = (byte) ((prog_id >> 8) & 0xff); 	packet[4] = (byte) (prog_id & 0xff);        	packet[7] = (byte) ((capsule >> 8) & 0xff);	packet[6] = (byte) (capsule & 0xff);          	packet[9] = (byte) ((GENERIC_BASE_ADDR >> 8) & 0xff);	packet[8] = (byte) (GENERIC_BASE_ADDR & 0xff);	packet[10] = (byte) check;	preparePacket(packet);	serialStub.Write(packet);    }    public static void usage() {	System.out.println("Usage: java codeGUI.CodeInjector command args\n");	System.out.println("where command is one of the following:\n"+			   "\trun <group-id> <src> <command-type> <probability of forwarding> <pot> \n" +			   "\tnew srec dest-- initialize uploading a new program(erase counters, etc.). \n"+			   "\tstart srec dest -- start reprogramming on a given node\n"+			   "\twrite srec dest -- write capsules to the appropriate nodes\n"+			   "\tread srec dest -- read capsules from the stored nodes\n"+			   "\tcheck srec dest -- resend unreceived packets\n"+			   "\tid [dest] -- find an id of a node. "+			   "dest defaults to broadcast\n" +			   "\tsetid [oldid] newid -- set ID of a mote (requires power cycling). oldid defaults to broadcast\n"+			   "\tmultiprog srec nnodes -- reliably download code to nnodes.\n"+			   "srec file in the above usage refers to the program"+" to download. It is best specified with absolute paths.\n");	    System.exit(-1);	}	    public static void main(String[] args) {	CodeInjector ic;	SerialStub r;	try{	    try {		ic = new CodeInjector("localhost", 9000); 		r = ic.getStub();		r.Open();		r.registerPacketListener(ic);	    } catch(IOException e) {		ic = new CodeInjector("COM1");		r = ic.getStub();		r.Open();		r.registerPacketListener(ic);	    }				    Thread rt = new Thread(ic);	    rt.setDaemon(true);	    rt.start();	    if (args.length == 0) {		usage();	    }	    if (args[0].equals("help")) {		usage();	    } else if (args[0].equals("new")) {		if (args.length != 3) 		    usage();		ic.readCode(args[1]);		int node = Integer.parseInt(args[2]);		ic.newProgram((short)node);	    } else if (args[0].equals("start")) {		if (args.length != 3) 		    usage();		ic.readCode(args[1]);		int node = Integer.parseInt(args[2]);		ic.startProgram((short)node);	    } else if (args[0].equals("write")) {		if (args.length < 3) 		    usage();		else if (args.length > 3){		    shortDelay = Integer.parseInt(args[3]);		    longDelay = Integer.parseInt(args[4]);		}		ic.readCode(args[1]);		int node = Integer.parseInt(args[2]);				ic.download((short)node);	    } else if (args[0].equals("read")) {		if (args.length != 3) 		    usage();		ic.readCode(args[1]);		int node = Integer.parseInt(args[2]);		ic.verify((short)node);	    } else if (args[0].equals("check")) {		if (args.length != 3) 		    usage();		ic.readCode(args[1]);		int node = Integer.parseInt(args[2]);		ic.check((short) node);	    } else if (args[0].equals("id")) {		int node = -1;		if (args.length > 2 ) 		    usage();		if (args.length > 1) 		    node = Integer.parseInt(args[1]);		ic.id((short) node);	    } else if (args[0].equals("run")) {		short src, prob;		byte type, pot, groupID;		if (args.length != 6) 		    usage();		groupID = Byte.parseByte(args[1]);		src = Short.parseShort(args[2]);		type = Byte.parseByte(args[3]);		prob = Short.parseShort(args[4]);		pot = Byte.parseByte(args[5]);		ic.runOption(groupID,(short)0xffff, src,type,prob,pot);	    } else if (args[0].equals("setid")) {		int node = -1;		int nid = -1;		if (args.length == 3) {		    node = Integer.parseInt(args[1]);		    nid = Integer.parseInt(args[2]);		} else if (args.length == 2) {		    nid = Integer.parseInt(args[1]);		} else {		    usage();		}		for (int i = 0; i < 3; i++) {		    ic.newProgram((short) node);		    try {			Thread.currentThread().sleep(500);		    } catch (Exception e) {}		}		for (int i = 0; i < 3; i++) {		    ic.setId((short) node, (short) nid);		    try {			Thread.currentThread().sleep(500);		    } catch (Exception e) {}		}	    } else if (args[0].equals("multiprog")) {		if (args.length < 3) 		    usage();		if (args.length > 3)		    ic.nrepeats = Integer.parseInt(args[3]);		if (args.length > 4) 		    ic.nretries = Integer.parseInt(args[4]);		ic.readCode(args[1]);		int nnodes = Integer.parseInt(args[2]);		for (int i = 0; i < nrepeats; i++) {		    ic.newProgram((short)-1);		    try {			Thread.currentThread().sleep(200);		    } catch (Exception e) {}		}		for (int i = 0; i < nrepeats; i++) {		    ic.download((short) -1);		}		ic.multicheck(nnodes);	    }else if (args[0].equals("multicheck")) {		if (args.length < 3) 		    usage();		if (args.length > 3)		    ic.nrepeats = Integer.parseInt(args[3]);		if (args.length > 4) 		    ic.nretries = Integer.parseInt(args[4]);		System.out.println("Will resend "+ic.nrepeats+" times, wait for" +				   " response "+ic.nretries+" packets");		ic.readCode(args[1]);		int nnodes = Integer.parseInt(args[2]);		ic.multicheck(nnodes);	    } else if (args[0].equals("download") ){		ic.reliableDownload((short)Integer.parseInt(args[1]), 				 Integer.parseInt(args[2]),				 Integer.parseInt(args[3]));	    } else {		System.out.println("Unknown command");		usage();	    }				    System.out.println("");				}	catch(Exception e){	    e.printStackTrace();	}    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?