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

📄 xnputil.java

📁 传感器网络中的嵌入式操作系统源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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 CROSSBOW TECHNOLOGY 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 CROSSBOW * TECHNOLOGY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * CROSSBOW TECHNOLOGY 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 CROSSBOW TECHNOLOGY HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." **//*-----------------------------------------------------------------------------* xnpUtil:* Utility functions for xnp:* - 1. Read srec file* - 2. Xmit/rcv mote messages* SerialPortStub modified to set higher baud rates, variable packet sizes*---------------------------------------------------------------------------- */package net.tinyos.xnp;import java.io.*;import java.util.*;import net.tinyos.util.*;import net.tinyos.message.*;import javax.swing.*;public class xnpUtil implements MessageListener, Runnable{/*-----------------------------------------------------------------------------* General Description:*--------------------------------------------------------------------------- */    public static final byte AM_REPROG = 47;      //AM handler for this reprogamming code    public static final byte MSG_LENGTH = 36;     //TOS message length    static final byte MSG_DATALENGTH = 29;        //number of data bytes in mote msg    public static final byte MSG_DATA = 5;         //1st location in mote pck for  data    public static final byte MSG_READ  = 50;    public static final byte MSG_RUN = 8;    public static final int GENERIC_BASE_ADDR = 0x7e;    public static final int TOS_BROADCAST_ADDR = 0xffff;    public static int nrepeats = 4;    public int nretries = 1000;    public static final int MAX_CODE_SIZE  = 128*1024;          //128K bytes max code to load    public static final int MAX_CAPSULES = MAX_CODE_SIZE / 16;  //16 bytes per cap => 8K capsuless    public static final int MAX_SREC_BYTE_SIZE = 24;            //max bytes of srec data in srec line    public static final int MAX_SREC_CHAR_SIZE = 2* MAX_SREC_BYTE_SIZE; //   public static int longDelay = 200; //   public static int shortDelay =  60;          //problem with generic base overrun?    private static final boolean debug = true;         //true to display debug info    byte flash[];                    //array of code bytes, read from srec file - OLD REMOVE!!!!    byte CRCsrec[];                   //array to hold CRC calculation    int length;                      //# of code bytes to xfr to node    public static short prog_id = 1;              // program id: CRC of all data in srec file    SerialStub serialStub;    MoteMsgIF mote;    byte m_srec[][];                     //array to hold srec file lines    public byte m_group_id;                       //group_id    public static int m_NmbCodeCapsules;        //# of code capsules to xmit    public static int m_NmbCodeCapsulesXmitted; //# code capsules xmitd to mote    public static int m_NmbCodeCapsulesRcvd;    //# code capsules rcvd by mote    public static boolean m_bCodeDwnloadDone;    //true when code download complete    public static boolean m_bMoteMsgRcvd;            // true if message rcv from mote    public static boolean m_bCmdAccepted;           //true if cmd accepted by mote    public static boolean m_bBatteryVoltsOK;        //true if battery volts OK for reprogramming    public static int m_BatteryVolts;           // battery volage in millivolts    public static int m_prog_id_rcvd;               //prog_id read back from mote    public static int m_mote_id_rcvd;            //mote id returned after query    private static boolean m_bXGenericBase = false;    public static int m_MoteIdQry;	// to screen the mote id for query// Commands sent to the Mote    static final int     CMD_START_DWNLOAD = 1,          //start download of new code     CMD_DWNLOADING = 2,             //code capsule to load into serial flash     CMD_DWNLOAD_COMPLETE = 3,       //no more code capsules to download     CMD_DWNLOAD_STATUS = 4,         //respond with download status     CMD_START_ICP = 5,              //start ICP - in cirucit programming     CMD_QRY_CAPSULES = 6,           //qry for missing capsules     CMD_PROG_ID = 7,                //qry for prog_id     CMD_TERMINATE_LOAD = 8,         //cmd to terminate download// return codes from MOTE     CMD_NO_ERR = 0    ;//------------------------------------- Constructor----------------------------------------    public xnpUtil() {	   flash = new byte[MAX_CODE_SIZE];               //stores code bytes for download	   m_srec = new byte[MAX_CAPSULES][MAX_SREC_BYTE_SIZE];           CRCsrec = new byte[MAX_CAPSULES*MAX_SREC_BYTE_SIZE];	   for (int i=0; i < MAX_CODE_SIZE; i++) {	     flash[i] = (byte) 0xff;                      //init to 0xff	   }    }  public xnpUtil( MoteMsgIF motestub ) {      this();      mote = motestub;  }//------------------------------------------------------------------------------// run: serial port read thread//------------------------------------------------------------------------------    public void run() {	try {          while (true) {	    serialStub.Read();                        //read the serial port          }	}	catch (Exception e) {	    System.err.println("Reading ERROR");         //port err	    System.err.println(e);	    e.printStackTrace();	}	System.err.print("error");    }//--------------------------------------------------------------------------------------------// packetReceived://   -packet received from mote//   -decode packet//   -if using XGenericBase then there 2 extra header bytes at the start of the packet//   -MSG_DATA location in packet is always echo of cmd that requested message//--------------------------------------------------------------------------------------------// set/get group id    public void setGroupID(short group) {	m_group_id = (byte)group;    }    public byte getGroupID() {	return (byte)m_group_id;    }  // stop the code download    public void stopDownload(){      m_bCodeDwnloadDone = true;    }    public void messageReceived(int dest_addr, Message msg) {        if (msg instanceof XnpMsg) {            xnpReceived((XnpMsg) msg);        } else {            throw new RuntimeException("messageReceived: Got bad message type: "               + msg);        }    }    void xnpReceived(XnpMsg xnpmsg) {       m_bMoteMsgRcvd = true;       if (xnpmsg.get_cmd() == CMD_DWNLOAD_STATUS) {           if (xnpmsg.getElement_data(1) == CMD_NO_ERR) m_bCmdAccepted = true;           else m_bCmdAccepted = false;           m_NmbCodeCapsulesRcvd = xnpmsg.get_cid();       }       else if (xnpmsg.get_cmd() == CMD_PROG_ID) {           int itmp = 1;           itmp = xnpmsg.get_pid();           m_prog_id_rcvd = itmp;       }       else if (xnpmsg.get_cmd() == CMD_QRY_CAPSULES) {           if (m_MoteIdQry == 0 || m_MoteIdQry == xnpmsg.get_subcmd()) {               m_MoteIdQry = xnpmsg.get_subcmd();               m_mote_id_rcvd = xnpmsg.get_subcmd();               m_NmbCodeCapsulesRcvd = xnpmsg.get_cid();               if (m_NmbCodeCapsulesRcvd > m_NmbCodeCapsules)                   m_NmbCodeCapsulesRcvd = 1;           }       }       else if (xnpmsg.get_cmd() == CMD_DWNLOAD_COMPLETE) {           m_mote_id_rcvd = xnpmsg.get_subcmd();           // set the received capsule number larger than the total           // number of capsules           m_NmbCodeCapsulesRcvd = m_NmbCodeCapsules + 2;       }       else if (xnpmsg.get_cmd() == CMD_TERMINATE_LOAD) {       }       //notify();    }//--------------------------htoi-----------------------------------------------// convert 2 chars to a 16 bit integer// line: an array of chars// index: index into 1st of 2 chars to convert//-----------------------------------------------------------------------------    public int htoi(char []line, int index) {	String val = "" + line[index] + line[index + 1];  //val = 2 byte char	return Integer.parseInt(val, 16);                 //return 16 bit integer    }//--------------------------ctoi-----------------------------------------------// convert 1 char to a 16 bit integer// line: an array of chars// index: index into 1st of 2 chars to convert//-----------------------------------------------------------------------------    public int ctoi(char sChar) {	String val = "" + sChar;	return Integer.parseInt(val, 16);                 //return 16 bit integer    }//----------------------calculateCRC-------------------------------------------//compute CRC of array//------------------------------------------------------------------------------    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);    }/*-----------------------------------------------------------------------------

⌨️ 快捷键说明

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