⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codeinjector.java

📁 无线传感器网络操作系统源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* @(#)CodeInjector.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$ *//**  *  Queries and injects code into a local mote RF network. * * @author <a href="mailto:szewczyk@sourceforge.net">Robert Szewczyk</a> * @author <a href="mailto:scipio@sourceforge.net">Phil Levis</a> */package net.tinyos.codeGUI;import java.io.*;import java.util.*;import java.awt.*;import java.applet.Applet;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.sql.Time;import net.tinyos.util.*;public class CodeInjector implements PacketListenerIF, Runnable{    public static final byte MSG_NEW_PROG   = 47;    public static final byte MSG_START = 48;    public static final byte MSG_WRITE = 49;    public static final byte MSG_READ  = 50;    public static final byte MSG_RUN = 8;    public static final int GENERIC_BASE_ADDR = 0xfff7;    public static final int TOS_BROADCAST_ADDR = 0xffff;    public static final byte MSG_LENGTH = 36;    public static int nrepeats = 4;    public int nretries = 1000;    public static final int MAX_CODE_SIZE  = 16384;    public static int longDelay = 200;    public static int shortDelay = 80;    public static final int MAX_CAPSULES = MAX_CODE_SIZE / 16;		    private static final int debug = 1;    private static final boolean showReceived = true;    private int awaitingResponse;    private int reliableRequest;    private int requestID;    private MotePanel motePanel;    private LogPanel logPanel;        byte flash[];    int length;    int acked;    short prog_id = 1;    byte group_id = 0x42;    SerialStub serialStub;    boolean packets_received[];    public CodeInjector() {	packets_received = new boolean[MAX_CAPSULES];	for (int i = 0; i < MAX_CAPSULES; i++) {	    packets_received[i] = true;	}	flash = new byte[MAX_CODE_SIZE];	for (int i=0; i < MAX_CODE_SIZE; i++) {	    flash[i] = (byte) 0xff;	}	reliableRequest = 0;	requestID = -1;	acked = 0;    }        public CodeInjector(String commPort) {	this();	setStub(new SerialPortStub(commPort));    }        public CodeInjector(String host, int port) {	this();	setStub(new SerialForwarderStub(host, port));    }    public CodeInjector(MotePanel motes, LogPanel log, String commPort) {	this(commPort);	motePanel = motes;	logPanel = log;    }    public CodeInjector(MotePanel motes, LogPanel log, String host, int port) {	this(host, port);	motePanel = motes;	logPanel = log;    }    public void run() {	try {	    //	    while (acked < length) {	    while (true) {		serialStub.Read();	    }	}	catch (Exception e) {	    System.err.println("Reading ERROR");	    System.err.println(e);	    e.printStackTrace();	}	System.err.print("error");    }    public void setStub(SerialStub stub) {	serialStub = stub;    }    public SerialStub getStub() {	return serialStub;    }        public void setGroupID(byte group) { 	group_id = group;    }    public byte getGroupID() {	return group_id;    }        public int htoi(char []line, int index) {	String val = "" + line[index] + line[index + 1];	return Integer.parseInt(val, 16);    }    /** Read and store code from <TT>name</TT> file for later use. */    public void readCode(String name) {	int j = 0;	try {	    DataInputStream dis = new DataInputStream (new FileInputStream(name));	    String line;	    while (true) {		line = dis.readLine();		char [] bline = line.toUpperCase().toCharArray();		if (bline[1] == '1') {		    int n = htoi(bline, 2)-3;		    int start = (htoi(bline, 4) << 8) + htoi(bline, 6);		    int s;		    for (j = start, s = 8; n > 0; n--, j++, s+=2) {			flash[j] = (byte) htoi(bline, s);			//System.out.println("Index: "+j+", Data: "+Integer.toHaexString(flash[j]&0xff));		    }		}	    }	}	catch (Exception e) {	    //	    System.out.println("EOF?: "+e);	    length = j;	}	prog_id = calculateCRC(flash);	System.out.println("Program ID:" + Integer.toHexString(prog_id).toUpperCase());    }    /** Called when received update packets. */        public void updatePacketsReceived(byte [] readings) {	int capsule = readings[6] & 0xff;	capsule += (readings[7] & 0xff)<<8;	int base = capsule;	base -= MAX_CODE_SIZE;	base *= 8;	base &= 0x7fff;	readings[1] = (byte) ((GENERIC_BASE_ADDR >> 8) & 0xff);	readings[0] = (byte) (GENERIC_BASE_ADDR & 0xff);	short crc = calculateCRC(readings);	//if (debug > 2)	//System.out.println("Updating range from "+ base+" to " +	//	       (base+128));	//	System.out.println("Base: "+base);	if (requestID == capsule) {	    requestID = -1;	    reliableRequest--;	} else if (capsule == 0x7FC0) {	    int progid = (readings[10] & 0xff) + ((readings[11] & 0xff) << 8);	    int proglen = (readings[12] & 0xff) + ((readings[13] & 0xff) <<						   8);	    int moteid = (readings[8] & 0xff) + ((readings[9] & 0xff) << 8);	    if (motePanel != null) {		motePanel.addMote(new MoteInfo(moteid, progid, proglen));	    } else {		System.out.println("Node ID: " + moteid);		System.out.println("Next Program ID: " + progid);		System.out.println("Next Program Length: " + proglen);	    }	} else if ((capsule >= MAX_CODE_SIZE) && (capsule <						  (MAX_CODE_SIZE+(MAX_CAPSULES/8)))) {	    short pid = (short)(((readings[5] & 0xff) << 8) + (readings[4] & 0xff));	    if ((readings[readings.length-1] == (byte) ((crc>>8) & 0xff)) &&		(readings[readings.length-2] == (byte) (crc & 0xff)) &&	        (prog_id == pid)) {		for (int i = 0; i < 16; i++) {		    int map = readings[i+ 8] & 0xff; 		    for (int j = 0; j < 8; j++) {			packets_received[base + (i*8) + j]  &= ((map & 0x01) == 1);			map >>= 1;		    }		}	    		//System.out.println("Awaiting responses: "+awaitingResponse);		awaitingResponse--;	    } else {		if (debug> 0) {		    System.out.println("CRC check failed, expected CRC "+				       Integer.toHexString(crc&0xffff)+" "+				       Integer.toHexString(pid&0xffff)+" "+				       Integer.toHexString(prog_id&0xffff)+" ");		}	    }	}     }    /** Processes a received packet. */    public synchronized void packetReceived(byte [] readings) {	if (debug > 0) {	    System.err.print(".");	} 	if (showReceived) {	    for(int j = 0; j < readings.length; j++)		System.out.print(Integer.toHexString(readings[j] & 0xff) + " ");	    System.out.println("\n");	}	if (readings[2] == MSG_WRITE) {	    updatePacketsReceived(readings);	}	acked = ((readings[4] &0xff) << 8) + (readings[5] &0xff) +16;	notify();    }        private short calculateCRC(byte packet[]) {	short crc;	int i;	int index = 0;	int count = packet.length - 2;	crc = 0;	while (--count >= 0) {	    crc = (short) (crc ^ ((short) (packet[index++]) << 8));	    i = 8;	    do {		if ((crc & 0x8000) != 0)		    crc = (short)(crc << 1 ^ ((short)0x1021));		else		    crc = (short)(crc << 1);	    } while(--i>0);	}	return (crc);    }        synchronized void preparePacket(byte [] packet)  throws IOException{	short crc;	crc = calculateCRC(packet);	packet[packet.length-1] = (byte) ((crc>>8) & 0xff);	packet[packet.length-2] = (byte) (crc & 0xff);	if (debug > 2) {	    for(int j = 0; j < packet.length; j++)		System.out.print(Integer.toHexString(packet[j] & 0xff) + " ");	    System.out.println("\n");	}    }    public void sendCapsule(short node, int capsule) throws IOException{	byte [] packet = new byte[MSG_LENGTH];	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(flash, capsule, packet, 8, 16);	preparePacket(packet);	serialStub.Write(packet);    }        public void download(short node) throws IOException  {	for (int i = 0; i < ((length+15) & 0xfff0); i += 16) {	    if (debug > 0) {		System.out.print("+");		System.out.flush();		if (i % 1280 == 0) {		    System.out.println();		}	    }	    if (logPanel != null) {		if (i % 10 == 0) {logPanel.repaint();}	    }	    sendCapsule(node, i);	    try {		Thread.currentThread().yield();		if (((i>>4) & 127) == 1) {		    System.out.print("!");		    Thread.currentThread().sleep(longDelay);		} else {		    Thread.currentThread().sleep(shortDelay);		}		logPanel.repaint();	    }	    catch (Exception e) {}	    	}	int capsule = MAX_CODE_SIZE +(MAX_CAPSULES/8);	byte [] packet = new byte[MSG_LENGTH];	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);    }    public void verify(short node) throws IOException {	for (int i = 0; i < ((length+15) & 0xfff0); i+= 16) {	    if (debug > 0) {		System.out.print("+");	    }	    if (logPanel != null) {		if (i % 10 == 0) {logPanel.repaint();}	    }	    readCapsule(node, i);	    try {		//	Thread.currentThread().sleep(350);	    } catch (Exception e) {}	}    }    public synchronized void reliableDownload(short node, int start, int end)     throws IOException {	for (int i = start; i < end; i+= 16) {	    reliableRequest = 1;	    requestID = (i - 64) & 0x7ff0;	    while (reliableRequest > 0) {		try {		    //		    Thread.currentThread().sleep(100);		    if (debug > 0)			System.out.print("+");		    //		    System.out.println("Reading: " + ( 1024*16-64+i));		    readCapsule(node, ((i - 64) & 0x7ff0));		    wait(350);		} catch (InterruptedException e) {		    System.err.println("Interrupted wait:"+e);		    e.printStackTrace();		}	    }	    if ((i & 63) == 48) {		try {		    Thread.currentThread().sleep(500);		} catch (Exception e) {}	    }	}    }        public synchronized void check(short node) throws IOException {	for (int i = 0; i < (MAX_CAPSULES/8); i+=16) {	    awaitingResponse = 1;	    while (awaitingResponse > 0) {		if (debug > 0)		    System.out.print("+");		readCapsule(node, MAX_CODE_SIZE+i);		try {		    wait(250);		} catch (InterruptedException e) {		    System.err.println("Interrupted wait:"+e);		    e.printStackTrace();		}	    }	}			if (debug >0) 	    System.out.print("\nMissing packets:");	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();}		}		sendCapsule(node, i * 16);		try {		    Thread.currentThread().sleep(100);		} catch (Exception e){}	    }	}			int capsule = MAX_CODE_SIZE +(MAX_CAPSULES/8);	byte [] packet = new byte[MSG_LENGTH];	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);    }

⌨️ 快捷键说明

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