📄 memorybankscratchshaee.java
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 2003 Dallas Semiconductor Corporation, All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Dallas Semiconductor * shall not be used except as stated in the Dallas Semiconductor * Branding Policy. *--------------------------------------------------------------------------- */package com.dalsemi.onewire.container;// importsimport com.dalsemi.onewire.OneWireException;import com.dalsemi.onewire.adapter.OneWireIOException;import com.dalsemi.onewire.utils.CRC16;import com.dalsemi.onewire.utils.SHA;import com.dalsemi.onewire.container.OneWireContainer;import com.dalsemi.onewire.adapter.DSPortAdapter;import com.dalsemi.onewire.debug.Debug;import com.dalsemi.onewire.utils.Convert;/** * Memory bank class for the Scratchpad section of SHA EEPROM iButtons and * 1-Wire devices with SHA write-protected memory pages. * * @version 1.00, 11 Aug 2002 * @author SH */public class MemoryBankScratchSHAEE extends MemoryBankScratchEx{ /** turn on extra debugging output */ private static final boolean DEBUG = false; /** Load First Secret */ public static final byte LOAD_FIRST_SECRET = ( byte ) 0x5A; /** Compute next Secret command */ public static final byte COMPUTE_NEXT_SECRET = ( byte ) 0x33; /** Refresh Scratchpad command */ public static final byte REFRESH_SCRATCHPAD = ( byte ) 0xA3; /** cached byte[] for re-use in SHA debit applications, speeds up operation on TINI */ private final byte[] MT_buffer = new byte [64]; /** cached byte[] for re-use in SHA debit applications, speeds up operation on TINI */ private final byte[] MAC_buffer = new byte[20]; /** cached byte[] for re-use in SHA debit applications, speeds up operation on TINI */ private final byte[] page_data_buffer = new byte [32]; /** cached byte[] for re-use in SHA debit applications, speeds up operation on TINI */ private final byte[] scratchpad_buffer = new byte [8]; /** cached byte[] for re-use in SHA debit applications, speeds up operation on TINI */ private final byte[] copy_scratchpad_buffer = new byte[4]; /** cached byte[] for re-use in SHA debit applications, speeds up operation on TINI */ private final byte[] read_scratchpad_buffer = new byte[8+3+3]; /** * block of 0xFF's used for faster read pre-fill of 1-Wire blocks * Comes from OneWireContainer33 that this MemoryBank references. */ protected static final byte[] ffBlock = OneWireContainer33.ffBlock; /** * block of 0x00's used for faster read pre-fill of 1-Wire blocks * Comes from OneWireContainer33 that this MemoryBank references. */ protected static final byte[] zeroBlock = OneWireContainer33.zeroBlock; /** * The Password container to acces the 8 byte passwords */ protected OneWireContainer33 owc33 = null; //-------- //-------- Constructor //-------- /** * Memory bank contstuctor. Requires reference to the OneWireContainer * this memory bank resides on. */ public MemoryBankScratchSHAEE (OneWireContainer33 ibutton) { super((OneWireContainer)ibutton); owc33 = ibutton; // initialize attributes of this memory bank - DEFAULT: DS1963L scratchapd bankDescription = "Scratchpad with CRC and 'Copy Scratchpad w/ SHA MAC'"; pageAutoCRC = true; startPhysicalAddress = 0; size = 8; numberPages = 1; pageLength = 8; maxPacketDataLength = 8-3; extraInfo = true; extraInfoLength = 3; // COPY_SCRATCHPAD_WITH_MAC COPY_SCRATCHPAD_COMMAND = ( byte ) 0x55; } //-------- //-------- PagedMemoryBank I/O methods //-------- /** * Read a complete memory page with CRC verification provided by the * device. Not supported by all devices. See the method * 'hasPageAutoCRC()'. * * @param page page number to read * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPagePacket() continious where the last one * stopped and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to put data read. Must have at least * 'getMaxPacketDataLength()' elements. * @param offset offset into readBuf to place data * * @throws OneWireIOException * @throws OneWireException */ public void readPageCRC (int page, boolean readContinue, byte[] readBuf, int offset) throws OneWireIOException, OneWireException { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) { Debug.debug("-----------------------------------------------------------"); Debug.debug("MemoryBankScratchSHAEE.readPageCRC(int, boolean, byte[], int) called"); Debug.debug(" romID="+owc33.getAddressAsString()); Debug.debug(" page="+page); Debug.debug(" readContinue="+readContinue); Debug.debug(" offset="+offset); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// //byte[] extraInfo = new byte [extraInfoLength]; readPageCRC(page, readContinue, readBuf, offset, null); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) Debug.debug("-----------------------------------------------------------"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// } /** * Read a complete memory page with CRC verification provided by the * device with extra information. Not supported by all devices. * See the method 'hasPageAutoCRC()'. * See the method 'hasExtraInfo()' for a description of the optional * extra information. * * @param page page number to read * @param readContinue if 'true' then device read is continued without * re-selecting. This can only be used if the new * readPagePacket() continious where the last one * stopped and it is inside a * 'beginExclusive/endExclusive' block. * @param readBuf byte array to put data read. Must have at least * 'getMaxPacketDataLength()' elements. * @param offset offset into readBuf to place data * @param extraInfo byte array to put extra info read into * * @throws OneWireIOException * @throws OneWireException */ public void readPageCRC (int page, boolean readContinue, byte[] readBuf, int offset, byte[] extraInfo) throws OneWireIOException, OneWireException { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) { Debug.debug("-----------------------------------------------------------"); Debug.debug("MemoryBankScratchSHAEE.readPageCRC(int, boolean, byte[], int, byte[]) called"); Debug.debug(" romID=" + owc33.getAddressAsString()); Debug.debug(" page=" + page); Debug.debug(" readContinue=" + readContinue); Debug.debug(" offset=" + offset); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// // only needs to be implemented if supported by hardware if (!pageAutoCRC) throw new OneWireException( "Read page with CRC not supported in this memory bank"); // attempt to put device at max desired speed if (!readContinue) checkSpeed(); // check if read exceeds memory if (page > numberPages) throw new OneWireException("Read exceeds memory bank end"); // read the scratchpad readScratchpad(readBuf, offset, pageLength, extraInfo); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) Debug.debug("-----------------------------------------------------------"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// } //-------- //-------- ScratchPad methods //-------- /** * Read the scratchpad page of memory from a NVRAM device * This method reads and returns the entire scratchpad after the byte * offset regardless of the actual ending offset * * @param readBuf byte array to place read data into * length of array is always pageLength. * @param offset offset into readBuf to pug data * @param len length in bytes to read * @param extraInfo byte array to put extra info read into * (TA1, TA2, e/s byte) * length of array is always extraInfoLength. * Can be 'null' if extra info is not needed. * * @throws OneWireIOException * @throws OneWireException */ public void readScratchpad (byte[] readBuf, int offset, int len, byte[] extraInfo) throws OneWireIOException, OneWireException { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) { Debug.debug("-----------------------------------------------------------"); Debug.debug("MemoryBankScratchSHAEE.readScratchpad(byte[], int, int, byte[]) called"); Debug.debug(" romID=" + owc33.getAddressAsString()); Debug.debug(" offset=" + offset); Debug.debug(" len=" + len); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// synchronized(read_scratchpad_buffer) { int num_crc = 0; checkSpeed(); // select the device if (!ib.adapter.select(ib.address)) { forceVerify(); throw new OneWireIOException("Device select failed"); } // build block read_scratchpad_buffer[0] = READ_SCRATCHPAD_COMMAND; System.arraycopy(ffBlock, 0, read_scratchpad_buffer, 1, read_scratchpad_buffer.length - 1); // send block, command + (extra) + page data + CRC ib.adapter.dataBlock(read_scratchpad_buffer, 0, read_scratchpad_buffer.length); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) Debug.debug("read_scratchpad_buffer", read_scratchpad_buffer); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// // get the starting offset to see when the crc will show up int addr = read_scratchpad_buffer[1]; addr = (addr | ((read_scratchpad_buffer[2] << 8) & 0xFF00)) & 0xFFFF; num_crc = pageLength + 3 + extraInfoLength; //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) Debug.debug("num_crc=" + num_crc); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// // check crc of entire block if(len == pageLength) { if (CRC16.compute(read_scratchpad_buffer, 0, num_crc, 0) != 0x0000B001) { //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// if(DEBUG) Debug.debug("CRC16 Failed"); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// forceVerify(); throw new OneWireIOException("Invalid CRC16 read from device");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -