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

📄 xnputil.java

📁 传感器网络中的嵌入式操作系统源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
* readSrecCode* -read a .srec code file; file format: S<type><length><address><data><checksum>* -create prog_id which is crc of data from srec file* -search for 1st "1" in file, then read all data records until end of "1" as 1st char* -return true if srec file read* srec file format:*  <type> : 0 -9*    0: start record;*    1: data record, 16 bit address;*    9 end record giving exe address*  <length> : two char, length of record*  <address> : 4 char for 16bit address*  <data>: 2 char per byte of memory*  <checksum>: 1's complement of the 8 bit checksum*-----------------------------------------------------------------------------*/public boolean readSrecCode(String name) {int j = 0;int jCRC = 0;   FileInputStream fis = null;   m_NmbCodeCapsules = 0;                                       //# code capsules to xmit   try {      DataInputStream dis = new DataInputStream (fis = new FileInputStream(name));      String line;      while (true) {	line = dis.readLine();                                  //read a line from file	char [] bline = line.toUpperCase().toCharArray();       //get array of bytes	int line_size = bline.length;	if (bline[1] > '0') {                                  //bline[0] = 'S'          m_NmbCodeCapsules++;                                  //inc # of code capsules	  //int n = htoi(bline, 2)-3;                             //# of data bytes in rec	  if (line_size > MAX_SREC_CHAR_SIZE){	    System.out.println("Too many data bytes in srec file line: " + m_NmbCodeCapsules);	    return false;	  }          //int start = (htoi(bline, 4) << 8) + htoi(bline, 6); //start address of rc          m_srec[m_NmbCodeCapsules-1][0] = (byte)ctoi(bline[1]);              //srec 'type'	  CRCsrec[jCRC++] = m_srec[m_NmbCodeCapsules-1][0];	  m_srec[m_NmbCodeCapsules-1][1] = (byte) htoi(bline,2);              //srec 'length'	  CRCsrec[jCRC++] = m_srec[m_NmbCodeCapsules-1][1];	  int s;          for (j = 2, s = 4;  j < m_srec[m_NmbCodeCapsules-1][1]+2; j++,s+=2) {	    m_srec[m_NmbCodeCapsules-1][j] = (byte) htoi(bline, s); //srec len,addr,data,chk	    CRCsrec[jCRC++] = m_srec[m_NmbCodeCapsules-1][j];	  } //end for	} // end if       } // end while   }//end try   catch(FileNotFoundException e){        System.out.println("readCode: " + e);         return false;   }   catch (Exception e) {//       System.out.println("# of Code Capsules: :" +  m_NmbCodeCapsules + e);         System.out.println("# of Code Capsules: :" +  m_NmbCodeCapsules);	 length = j;   }   try { if( fis != null ) fis.close(); } catch(Exception e) { }   //prog_id = calculateCRC(flash);   prog_id = calculateCRC(CRCsrec);          //compute CRC of array   System.out.println("Program ID:" + Integer.toHexString(prog_id).toUpperCase());   return true;}/*-----------------------------------------------------------------------------* CmdStartDwnload* -xmit msg to Mote to start rcving code bytes* -xmit prog id* -xmit number of code capsules to be loaded* - if node = broadcast address xmit message 'repeat' time, no response* -rqst response msg from mote if node <> broadcast*-----------------------------------------------------------------------------*/public boolean CmdStartDwnload(short node,boolean bGetReply, int iTries, int iSleep){   XnpMsg xnpmsg = new XnpMsg();   xnpmsg.set_cmd((byte) CMD_START_DWNLOAD);  // cmd to initiate download   xnpmsg.set_subcmd((byte) 0);   xnpmsg.set_pid(prog_id);   xnpmsg.set_cid(m_NmbCodeCapsules);   return (WriteMsg(xnpmsg, bGetReply, iTries, iSleep));}/*---------------------------------------------------------------------------* CmdSendCapsule:* -xmit a code capsule in the flash array to the mote* -node : Mote Id (can be broadcast)* -sindx : index in srec[] array for srec code record* -These msgs xmitted after mote notified to start loading new program* -msg consist of the data:*   -localnode - if bcast msg to all, then mote with localnode address*                should respond that it received the capsule.*                if bcast msg to all, and localnode = 0 then no response*                expected.*   -program id (2 bytes):*   -capsule# (2 bytes)  : increments from 1..m_NmbCodeCapsulesXmitted*   -srec type(1 byte)   : type of srec record*   -srec length(1 byte) : # of bytes to follow in srec record*   -data[]: srec data bytes, including code address, code data bytes, chksum*---------------------------------------------------------------------------*/    public boolean CmdsendCapsule(short node, short localnode, int sindx,                                  boolean bGetReply, int iTries, int iSleep){        XnpMsg xnpmsg = new XnpMsg();        int isrec, ipkt;        int capsule= sindx+1;        m_bMoteMsgRcvd = false;        xnpmsg.set_cmd((byte) CMD_DWNLOADING);        xnpmsg.set_subcmd((byte) localnode);        xnpmsg.set_pid(prog_id);        xnpmsg.set_cid(capsule);        xnpmsg.setElement_data(0, m_srec[sindx][0]);		// srec type        byte iLen = m_srec[sindx][1];                           // srec length        xnpmsg.setElement_data(1, iLen);        for (isrec = 0, ipkt = 2 ; isrec < iLen; isrec ++, ipkt++){           xnpmsg.setElement_data(ipkt, m_srec[sindx][2+isrec]);        }        return (WriteMsg(xnpmsg, bGetReply, iTries, iSleep));    }/*-----------------------------------------------------------------------------* CmdGetLoadStatus* -xmit msg to request status of download*-----------------------------------------------------------------------------*/    public boolean CmdGetLoadStatus(short node,boolean bGetReply, int iTries, int iSleep){        XnpMsg xnpmsg = new XnpMsg();        m_bMoteMsgRcvd = false;		// rst mote message received        xnpmsg.set_cmd((byte) CMD_DWNLOAD_STATUS);        xnpmsg.set_subcmd((byte) node);        xnpmsg.set_pid(prog_id);        return (WriteMsg(xnpmsg, bGetReply, iTries, iSleep));    }/*-----------------------------------------------------------------------------* CmdTerminateLoad* -xmit msg to terminate downloading*-----------------------------------------------------------------------------*/    public boolean CmdTerminateLoad(short node,boolean bGetReply, int iTries, int iSleep){        XnpMsg xnpmsg = new XnpMsg();        m_bMoteMsgRcvd = false;           //rst mote message received        xnpmsg.set_cmd((byte) CMD_TERMINATE_LOAD);         xnpmsg.set_subcmd((byte) node);        xnpmsg.set_pid(prog_id);        xnpmsg.set_cid(m_NmbCodeCapsules);        return (WriteMsg(xnpmsg, bGetReply, iTries, iSleep));    }/*-----------------------------------------------------------------------------* CmdStartISP* -xmit msg to start reflashing/rebooting of motes*-----------------------------------------------------------------------------*/    public boolean CmdStartISP(short node, boolean bGetReply, int iTries, int iSleep){        XnpMsg xnpmsg = new XnpMsg();        xnpmsg.set_cmd((byte) CMD_START_ICP);        xnpmsg.set_subcmd((byte) node);        xnpmsg.set_pid(prog_id);        xnpmsg.set_cid(m_NmbCodeCapsules);        return (WriteMsg(xnpmsg, bGetReply, iTries, iSleep));    }/*-----------------------------------------------------------------------------* CmdQryProgId* -xmit cmd to request prog_id or mismatch* -if Match = true then requesting mote to return the prog_id its now running.*     Match = false then mote returns prog_id only if its prog_id doesn't match.*             [This is used for bcast downloads, at end*             of download, request all motes with incorrect prog_ids to respond]*-----------------------------------------------------------------------------*/    public boolean CmdQryProgId(short node, boolean Match,boolean bGetReply, int iTries, int iSleep){        XnpMsg xnpmsg = new XnpMsg();        xnpmsg.set_cmd((byte) CMD_PROG_ID);        xnpmsg.set_subcmd((byte) 0);        xnpmsg.set_pid(prog_id);        xnpmsg.set_cid(0);        byte mMatch = 0;        if (Match) mMatch = 1;        xnpmsg.setElement_data(0, mMatch);        return (WriteMsg(xnpmsg, bGetReply, iTries, iSleep));    }/*-----------------------------------------------------------------------------* CmdQryCapsules* -xmit cmd to motes to ask for missing capsules* -if node_qry_id = 0 then all motes respond to query* -if node_qry_id = n then only mote_id=n responds*-----------------------------------------------------------------------------*/    public boolean CmdQryCapsules(short node,short node_qry_id,boolean bGetReply, int iTries, int iSleep){        XnpMsg xnpmsg = new XnpMsg();        xnpmsg.set_cmd((byte) CMD_QRY_CAPSULES);        xnpmsg.set_subcmd((byte) node_qry_id);        xnpmsg.set_pid(prog_id);        return (WriteMsg(xnpmsg, bGetReply, iTries, iSleep));    }//-----------------------------------------------------------------------------//write mote packet to uart// iTries : Nmb of times to xmit the packet (min value = 1)// bGetReply: if true then expect reply from mote, if get response then exit// iSleep: time to sleep between retries, 10msec increments// if bGetReplay = true then return true immediatley,if get response from mote// if no response from mote then return false//------------------------------------------------------------------------------  public boolean WriteMsg(XnpMsg xnpmsg,boolean bGetReply, int iTries, int iSleep){    int iRetryNmb = 0;    int i,j;    m_bMoteMsgRcvd = false;    if (iTries <= 0) iTries = 1;    try {       while (iRetryNmb < iTries){          int count = iSleep;          mote.send(MoteIF.TOS_BCAST_ADDR, xnpmsg);          while (--count >= 0) {            Thread.currentThread().sleep(10);       //wait for mote to reply            if (bGetReply && m_bMoteMsgRcvd) return true;          }          iRetryNmb++;       }    }    catch (Exception f) {    }    return false;  }}

⌨️ 快捷键说明

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