📄 onewirecontainer18.java
字号:
IOHelper.writeHex((byte)(addr>>8)); IOHelper.writeLineHex((byte)addr); IOHelper.writeLine("inputbuffer"); IOHelper.writeBytesHex(inputbuffer); IOHelper.writeLine("start: " + start); IOHelper.writeLine("length: " + length); IOHelper.writeLine("------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ buffer [0] = WRITE_SCRATCHPAD; buffer [1] = ( byte ) addr; buffer [2] = ( byte ) (addr >> 8); int maxbytes = 32 - (addr & 31); if (length > maxbytes) length = maxbytes; //if we are going to go over the 32byte boundary //let's cut it System.arraycopy(inputbuffer, start, buffer, 3, length); buffer [3 + length] = ( byte ) 0xff; buffer [4 + length] = ( byte ) 0xff; //leave space for the CRC //this nasty statement sends the length depending on if we are //going to get a CRC back. you only get a CRC if you get to the end of //the scratchpad writing. so we look at the address and add the length //that we are writing. if this is not a multiple of 32 then we do not //finish reading at the scratchpad boundary (and we checked earlier to //make sure don't go over THEREFORE we have gone under). if we are at //the boundary we need two extra bytes to read the crc adapter.dataBlock(buffer, 0, ((((addr + length) & 31) == 0) ? length + 5 : length + 3)); //if we dont check the CRC we are done if (((addr + length) & 31) != 0) return true; //else we need to do a CRC calculation if (CRC16.compute(buffer, 0, length + 5, 0) != 0xB001) { if(DEBUG) System.out.println("CRC Failed in Write Scratchpad"); return false; } return true; } /** * <p>Verifies the hidden signature in the scratchpad of the DS1963S. * After a <code>VALIDATE_DATA_PAGE</code> command, the scratchpad * contains a signature that cannot be read, but must be verified. * This method gives the DS1963S a signature. The iButton then * checks to see if it equals the hidden signature in its * scratchpad.</p> * * <p>The signature must be 20 bytes long, and it must start at offset 0 * of the input array.</p> * * @param mac the signature starting at offset 0 * * @return <code>true</code> if the signature matches * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter * * @see #VALIDATE_DATA_PAGE */ public synchronized boolean matchScratchPad (byte[] mac) throws OneWireIOException, OneWireException { if (doSpeedEnable && (!resume)) doSpeed(); if (!resume) adapter.select(address); else { adapter.reset(); adapter.putByte(RESUME); } byte[] buffer = byte_buffer; buffer [0] = MATCH_SCRATCHPAD; System.arraycopy(mac, 0, buffer, 1, 20); buffer [21] = ( byte ) 0x0ff; //CRC1 buffer [22] = ( byte ) 0x0ff; //CRC2 buffer [23] = ( byte ) 0x0ff; //status adapter.dataBlock(buffer, 0, 24); if (CRC16.compute(buffer, 0, 23, 0) != 0xB001) { return false; } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("------------------------------------"); IOHelper.writeLine("Matching the scratchpad"); IOHelper.writeLine("Address"); IOHelper.writeBytesHex(address); IOHelper.writeLine("mac"); IOHelper.writeBytesHex(mac); IOHelper.writeLine("matchScratchpad buffer"); IOHelper.writeBytesHex(buffer,0,23); IOHelper.writeLine("------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if (buffer [23] != ( byte ) 0x0ff) return true; return false; } /** * <p>Reads the contents of the DS1963S scratchpad. If the device is in hidden * mode, all 1's (0x0ff bytes) will be returned.</p> * * <p>This method will return up to 32 bytes. It may return less * if the target address stored internally on the DS1963S is * not a multiple of 32.</p> * * @param data array to hold the contents of the scratchpad * @param start offset to copy the scratchpad data into the <code>byte</code> array * * @return the number of bytes read, or -1 if there was a CRC failure * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter * * @see #writeScratchPad(int,int,byte[],int,int) */ public synchronized int readScratchPad (byte[] data, int start) throws OneWireIOException, OneWireException { if (doSpeedEnable && (!resume)) doSpeed(); if (!resume) adapter.select(address); else { adapter.reset(); adapter.putByte(RESUME); } byte[] buffer = byte_buffer; buffer [0] = READ_SCRATCHPAD; System.arraycopy(FF, 0, buffer, 1, 37); adapter.dataBlock(buffer, 0, 38); TA1 = buffer [1]; TA2 = buffer [2]; ES = buffer [3]; int length = 32 - (TA1 & 31); //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("------------------------------------"); IOHelper.writeLine("Read Scratchpad"); IOHelper.writeLine("address"); IOHelper.writeBytesHex(address); IOHelper.write("target address: 0x"); IOHelper.writeHex((byte)TA2); IOHelper.writeLineHex((byte)TA1); IOHelper.write("ES: 0x"); IOHelper.writeLineHex((byte)ES); IOHelper.writeLine("data"); IOHelper.writeBytesHex(buffer,4,length); IOHelper.writeLine("------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if (CRC16.compute(buffer, 0, 6 + length, 0) != 0xB001) { return -1; } if (data != null) System.arraycopy(buffer, 4, data, start, length); return length; } /** * Copies the contents of the scratchpad to the target destination * that was specified in a call to <code>writeScratchPad()</code> or * <code>eraseScratchPad()</code>. This method will not success unless * a call to <code>readScratchPad()</code> is made first to verify * the target address registers in the DS1963S. * * @return <code>true</code> if successful, <code>false</code> if the operation * failed while waiting for the DS1963S's output to alternate * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter * * @see #eraseScratchPad(int) * @see #readScratchPad(byte[],int) * @see #writeScratchPad(int,int,byte[],int,int) */ public synchronized boolean copyScratchPad () throws OneWireIOException, OneWireException { if (doSpeedEnable && (!resume)) doSpeed(); if (!resume) adapter.select(address); else { adapter.reset(); adapter.putByte(RESUME); } byte[] buffer = byte_buffer; buffer [0] = COPY_SCRATCHPAD; buffer [1] = TA1; buffer [2] = TA2; buffer [3] = ES; System.arraycopy(FF, 0, buffer, 4, 5); //adapter.dataBlock(buffer,0,4); adapter.dataBlock(buffer, 0, 9); if (buffer [8] == ( byte ) 0x0ff) return waitForSuccessfulFinish(); else return true; } /** * <p>Installs a secret on a DS1963S. The secret is written in partial phrases * of 47 bytes (32 bytes to a memory page, 15 bytes to the scratchpad) and * is cumulative until the entire secret is processed. Secrets are associated * with a page number. See the datasheet for more information on this * association.</p> * * <p>In most cases, <code>page</code> should be equal to <code>secret_number</code> * or <code>secret_number+8</code>, based on the association of secrets and page numbers. * Secrets are stored across pages 16 and 17 of the DS1963S memory. A secret is 8 bytes, so * there are 8 secrets. These 8 secrets are associated with the first 16 pages of memory.</p> * * <p>On TINI, this method will be slightly faster if the secret's length is divisible by 47. * However, since secret key generation is a part of initialization, it is probably * not necessary.</p> * * @param page the page number used to write the partial secrets to * @param secret the entire secret, in partial phrases, to be installed * @param secret_number the secret 'page' to use (0 - 7) * * @return <code>true</code> if successful * * @throws OneWireIOException on a 1-Wire communication error such as * reading an incorrect CRC from a 1-Wire device. This could be * caused by a physical interruption in the 1-Wire Network due to * shorts or a newly arriving 1-Wire device issuing a 'presence pulse'. * @throws OneWireException on a communication or setup error with the 1-Wire * adapter * * @see #bindSecretToiButton(int,byte[],byte[],int) */ public synchronized boolean installMasterSecret (int page, byte[] secret, int secret_number) throws OneWireIOException, OneWireException { //47 is a magic number here because every time a partial secret //is to be computed, 32 bytes goes in the page and 15 goes in //the scratchpad, so it's going to be easier in the computations //if i know the input buffer length is divisible by 47 if (secret.length == 0) return false; byte[] input_secret = null; byte[] buffer = byte_buffer; int secret_mod_length = secret.length % 47; if (secret_mod_length == 0) //if the length of the secret is divisible by 47 input_secret = secret; else { /* i figure in the case where secret is not divisible by 47 it will be quicker to just create a new array once and copy the data in, rather than on every partial secret calculation do bounds checking */ input_secret = new byte [secret.length + (47 - secret_mod_length)]; System.arraycopy(secret, 0, input_secret, 0, secret.length); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ if(DEBUG) { IOHelper.writeLine("------------------------------------"); IOHelper.writeLine("Installing Secret on iButton"); IOHelper.writeLine("address"); IOHelper.writeBytesHex(address); IOHelper.writeLine("page: " + page); IOHelper.writeLine("secret"); IOHelper.writeBytesHex(secret); IOHelper.writeLine("input_secret"); IOHelper.writeBytesHex(input_secret); IOHelper.writeLine("secret_number: " + (secret_number&7)); IOHelper.writeLine("------------------------------------"); } //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\ //make sure the secret number is between 0 and 7 secret_number = secret_number & 7;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -