📄 memorybankshaee.java
字号:
* bytes, or SHA-1 result. If this method returns true then the * methods with an 'extraInfo' parameter can be used: * {@link #readPage(int,boolean,byte[],int,byte[]) readPage}, * {@link #readPageCRC(int,boolean,byte[],int,byte[]) readPageCRC}, and * {@link #readPagePacket(int,boolean,byte[],int,byte[]) readPagePacket}. * * @return <CODE> true </CODE> if reading the this memory bank's * pages provides extra information * * @see #readPage(int,boolean,byte[],int,byte[]) readPage(extra) * @see #readPageCRC(int,boolean,byte[],int,byte[]) readPageCRC(extra) * @see #readPagePacket(int,boolean,byte[],int,byte[]) readPagePacket(extra) * @since 1-Wire API 0.01 */ public boolean hasExtraInfo () { return extraInfo; } /** * Query to get the length in bytes of extra information that * is read when read a page in the current memory bank. See * 'hasExtraInfo()'. * * @return number of bytes in Extra Information read when reading * pages in the current memory bank. */ public int getExtraInfoLength () { return extraInfoLength; } /** * Query to get a string description of what is contained in * the Extra Informationed return when reading pages in the current * memory bank. See 'hasExtraInfo()'. * * @return string describing extra information. */ public String getExtraInfoDescription () { return "The MAC for the SHA Engine"; } /** * Query to get Maximum data page length in bytes for a packet * read or written in the current memory bank. See the 'ReadPagePacket()' * and 'WritePagePacket()' methods. This method is only usefull * if the current memory bank is general purpose memory. * * @return max packet page length in bytes in current memory bank */ public int getMaxPacketDataLength () { return maxPacketDataLength; } //-------- //-------- MemoryBank I/O methods //-------- /** * Read memory in the current bank with no CRC checking (device or * data). The resulting data from this API may or may not be what is on * the 1-Wire device. It is recommends that the data contain some kind * of checking (CRC) like in the readPagePacket() method or have * the 1-Wire device provide the CRC as in readPageCRC(). readPageCRC() * however is not supported on all memory types, see 'hasPageAutoCRC()'. * If neither is an option then this method could be called more * then once to at least verify that the same thing is read consistantly. * * @param startAddr starting address, relative to physical address for * this memory bank. * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * read() continious where the last one led off * and it is inside a 'beginExclusive/endExclusive' * block. * @param readBuf byte array to place read data into * @param offset offset into readBuf to place data * @param len length in bytes to read * * @throws OneWireIOException * @throws OneWireException */ public void read (int startAddr, boolean readContinue, byte[] readBuf, int offset, int len) throws OneWireIOException, OneWireException { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) { Debug.debug("-----------------------------------------------------------"); Debug.debug("MemoryBankSHAEE.read(int, boolean, byte[], int, int) called"); Debug.debug(" startAddr=0x" + Convert.toHexString((byte)startAddr)); Debug.debug(" readContinue=" + readContinue); Debug.debug(" offset=" + offset); Debug.debug(" len=" + len); Debug.debug(" this.startPhysicalAddress=0x" + Convert.toHexString((byte)startPhysicalAddress)); Debug.debug(" this.pageLength=" + this.pageLength); Debug.debug(" this.numberPages=" + this.numberPages); Debug.stackTrace(); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// // attempt to put device at max desired speed if (!readContinue && ib.adapterSet()) checkSpeed(); // check if read exceeds memory if ((startAddr + len) > (pageLength * numberPages)) throw new OneWireException("Read exceeds memory bank end."); // see if need to access the device if (!readContinue) { // select the device if (!adapter.select(ib.getAddress())) throw new OneWireIOException("Device select failed."); // build start reading memory block int addr = startAddr + startPhysicalAddress; byte[] raw_buf = new byte[3]; raw_buf [0] = READ_MEMORY; raw_buf [1] = ( byte ) (addr & 0xFF); raw_buf [2] = ( byte ) (((addr & 0xFFFF) >>> 8) & 0xFF); // do the first block for command, address adapter.dataBlock(raw_buf, 0, 3); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) Debug.debug(" raw_buf", raw_buf, 0, 3); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// } // pre-fill readBuf with 0xFF int pgs = len / pageLength; int extra = len % pageLength; for (int i = 0; i < pgs; i++) System.arraycopy(ffBlock, 0, readBuf, offset + i * pageLength, pageLength); System.arraycopy(ffBlock, 0, readBuf, offset + pgs * pageLength, extra); // send second block to read data, return result adapter.dataBlock(readBuf, offset, len); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) { Debug.debug(" readBuf", readBuf, offset, len); Debug.debug("-----------------------------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// } /** * Write memory in the current bank. It is recommended that * when writing data that some structure in the data is created * to provide error free reading back with read(). Or the * method 'writePagePacket()' could be used which automatically * wraps the data in a length and CRC. * * When using on Write-Once devices care must be taken to write into * into empty space. If write() is used to write over an unlocked * page on a Write-Once device it will fail. If write verification * is turned off with the method 'setWriteVerification(false)' then * the result will be an 'AND' of the existing data and the new data. * * @param startAddr starting address, relative to the starting physical address * of this memory bank * @param writeBuf byte array containing data to write * @param offset offset into writeBuf to get data * @param len length in bytes to write * * @throws OneWireIOException * @throws OneWireException */ public void write (int startAddr, byte[] writeBuf, int offset, int len) throws OneWireIOException, OneWireException { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { Debug.debug("-----------------------------------------------------------"); Debug.debug("MemoryBankSHAEE.write(int,byte[],int,int) called"); Debug.debug(" startAddr=0x" + Convert.toHexString((byte)startAddr)); Debug.debug(" writeBuf", writeBuf, offset, len); Debug.debug(" startPhysicalAddress=0x" + Convert.toHexString((byte)startPhysicalAddress)); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ int room_left; if(!checked) checked = ib.checkStatus(); // return if nothing to do if (len == 0) return; // attempt to put device at speed checkSpeed(); // check to see if secret is set if(!ib.isContainerSecretSet()) throw new OneWireException("Secret is not set."); // check if write exceeds memory if ((startAddr + len) > size) throw new OneWireException("Write exceeds memory bank end"); // check if trying to write read only bank if (isReadOnly()) throw new OneWireException("Trying to write read-only memory bank"); // loop while still have pages to write int startx = 0, nextx = 0; // (start and next index into writeBuf) byte[] raw_buf = new byte [8]; byte[] memory = new byte [size]; int abs_addr = startPhysicalAddress + startAddr; int pl = 8; read(startAddr&0xE0,false,memory,0,size); if(abs_addr >= 128) { ib.getContainerSecret(memory, 0); } do { // calculate room left in current page room_left = pl - ((abs_addr + startx) % pl); // check if block left will cross end of page if ((len - startx) > room_left) nextx = startx + room_left; else nextx = len; // bug fix, if updating pages two and three in the same write op // this used to fail, was (startAddr>=pageLength) if((startx+startAddr) >= pageLength) System.arraycopy(memory,(((startx+startAddr)/8)*8)-32,raw_buf,0,8); else System.arraycopy(memory,(((startx+startAddr)/8)*8),raw_buf,0,8); if((nextx-startx) == 8) System.arraycopy(writeBuf,offset+startx,raw_buf,0,8); else if(((startAddr+nextx)%8) == 0) System.arraycopy(writeBuf,offset+startx,raw_buf,((startAddr+startx)%8),8-((startAddr+startx)%8)); else System.arraycopy(writeBuf,offset+startx,raw_buf,((startAddr+startx)%8), ((startAddr+nextx)%8)-((startAddr+startx)%8)); // write the page of data to scratchpad scratchpad.writeScratchpad(abs_addr+startx+room_left-8, raw_buf, 0, 8); // Copy data from scratchpad into memory scratchpad.copyScratchpad(abs_addr+startx+room_left-8, raw_buf, 0, memory, 0); // bug fix, if updating pages two and three in the same write op // this used to fail, was (startAddr>=pageLength) if((startx+startAddr) >= pageLength) System.arraycopy(raw_buf,0,memory,(((startx+startAddr)/8)*8)-32,8); else System.arraycopy(raw_buf,0,memory,(((startx+startAddr)/8)*8),8); // point to next index startx = nextx; } while (nextx < len); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) Debug.debug("-----------------------------------------------------------"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ } //-------- //-------- PagedMemoryBank I/O methods //-------- /** * Read page in the current bank with no * CRC checking (device or data). The resulting data from this API * may or may not be what is on the 1-Wire device. It is recommends * that the data contain some kind of checking (CRC) like in the * readPagePacket() method or have the 1-Wire device provide the * CRC as in readPageCRC(). readPageCRC() however is not * supported on all memory types, see 'hasPageAutoCRC()'. * If neither is an option then this method could be called more * then once to at least verify that the same thing is read consistantly. * * @param page page number to read packet from * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPage() continious where the last one * led off and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to place read data into * @param offset offset into readBuf to place data * * @throws OneWireIOException * @throws OneWireException */ public void readPage (int page, boolean readContinue, byte[] readBuf, int offset) throws OneWireIOException, OneWireException { read(page * pageLength, readContinue, readBuf, offset, pageLength); } /** * Read page with extra information in the current bank with no * CRC checking (device or data). The resulting data from this API * may or may not be what is on the 1-Wire device. It is recommends * that the data contain some kind of checking (CRC) like in the * readPagePacket() method or have the 1-Wire device provide the * CRC as in readPageCRC(). readPageCRC() however is not * supported on all memory types, see 'hasPageAutoCRC()'. * If neither is an option then this method could be called more * then once to at least verify that the same thing is read consistantly. * See the method 'hasExtraInfo()' for a description of the optional * extra information some devices have. * * @param page page number to read packet from * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPage() continious where the last one * led off and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to place read data into * @param offset offset into readBuf to place data * @param extraInfo byte array to put extra info read into * * @throws OneWireIOException * @throws OneWireException */ public void readPage (int page, boolean readContinue, byte[] readBuf, int offset, byte[] extraInfo) throws OneWireIOException, OneWireException { byte[] pg = new byte [32]; if(!checked) checked = ib.checkStatus(); if(!hasPageAutoCRC()) throw new OneWireException("This memory bank doesn't have crc capabilities."); // attempt to put device at max desired speed if (!readContinue) checkSpeed(); if(!readAuthenticatedPage(page,pg,0,extraInfo,0)) throw new OneWireException("Read didn't work."); System.arraycopy(pg,0,readBuf,offset,32); } /** * Read a Universal Data Packet. * * The Universal Data Packet always starts on page boundaries but * can end anywhere in the page. The structure specifies the length of * data bytes not including the length byte and the CRC16 bytes. * There is one length byte. The CRC16 is first initialized to * the page number. This provides a check to verify the page that * was intended is being read. The CRC16 is then calculated over * the length and data bytes. The CRC16 is then inverted and stored * low byte first followed by the high byte. This is structure is * used by this method to verify the data but is not returned, only * the data payload is returned. * * @param page page number to read packet from * @param readContinue if 'true' then device read is continued without
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -