📄 onewirecontainer02.java
字号:
/*--------------------------------------------------------------------------- * Copyright (C) 1999,2000 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.*;import com.dalsemi.onewire.adapter.*;import java.io.*;/** * <P>1-Wire® container that encapsulates the functionality of the 1-Wire * family type <B>02</B> (hex), Dallas Semiconductor part number: <B>DS1991, * MultiKey</B>.</P> * * <P> This iButton is primarily used as a minimal security read/write portable memory device. </P> * * <H2> Features </H2> * <UL> * <LI> Three 384 bit (48 bytes) password protected memory blocks * <LI> 64 bit (8 byte) password per memory block * <LI> 64 bit (8 byte) identification per memory block * <LI> Data integrity assured with strict read/write * protocols * <LI> Operating temperature range from -40°C to * +70°C * <LI> Over 10 years of data retention * </UL> * * <H2> Memory </H2> * * <P> All memory is accessed through read/write routines, no <code>MemoryBank</code> classes are used.</P> * * <H2> Alternate Names </H2> * <UL> * <LI> DS1425 (Family 82) * </UL> * * <H2> DataSheets </H2> * * <A HREF="http://pdfserv.maxim-ic.com/arpdf/DS1991.pdf"> http://pdfserv.maxim-ic.com/arpdf/DS1991.pdf</A><br> * <A HREF="http://pdfserv.maxim-ic.com/arpdf/DS1425.pdf"> http://pdfserv.maxim-ic.com/arpdf/DS1425.pdf</A> * * * @version 0.00, 28 Aug 2000 * @author DS,GH */public class OneWireContainer02 extends OneWireContainer{ //-------- //-------- Static Final Variables //-------- /** * DS1991 Write Scratchpad Command */ private static final byte WRITE_SCRATCHPAD_COMMAND = ( byte ) 0x96; /** * DS1991 Read Scratchpad Command */ private static final byte READ_SCRATCHPAD_COMMAND = 0x69; /** * DS1991 Copy Scratchpad Command */ private static final byte COPY_SCRATCHPAD_COMMAND = 0x3C; /** * DS1991 Write Password Command */ private static final byte WRITE_PASSWORD_COMMAND = 0x5A; /** * DS1991 Write SubKey Command */ private static final byte WRITE_SUBKEY_COMMAND = ( byte ) 0x99; /** * DS1991 Read SubKey Command */ private static final byte READ_SUBKEY_COMMAND = 0x66; /** * DS1991 Block code commands */ private static byte blockCodes [][] = null; //-------- //-------- Variables //-------- /** * General purpose buffer */ private byte[] buffer = new byte [82]; //-------- //-------- Constructor //-------- static { blockCodes = new byte [9][8]; initBlockCodes(blockCodes); } /** * Create an empty container. Must call <code>setupContainer</code> before * using this new container.<p> * * This is one of the methods to construct a container. The others are * through creating a OneWireContainer with parameters. * * @see #OneWireContainer02(DSPortAdapter,byte[]) * @see #OneWireContainer02(DSPortAdapter,long) * @see #OneWireContainer02(DSPortAdapter,String) * @see #setupContainer(DSPortAdapter,byte[]) * @see #setupContainer(DSPortAdapter,long) * @see #setupContainer(DSPortAdapter,String) */ public OneWireContainer02 () { super(); } /** * Create a container with a provided adapter object * and the address of the iButton or 1-Wire device.<p> * * This is one of the methods to construct a container. The other is * through creating a OneWireContainer with NO parameters. * * @param sourceAdapter adapter object required to communicate with * this iButton. * @param newAddress address of this 1-Wire device * @see #OneWireContainer02() * @see com.dalsemi.onewire.utils.Address */ public OneWireContainer02 (DSPortAdapter sourceAdapter, byte[] newAddress) { super(sourceAdapter, newAddress); } /** * Create a container with a provided adapter object * and the address of the iButton or 1-Wire device.<p> * * This is one of the methods to construct a container. The other is * through creating a OneWireContainer with NO parameters. * * @param sourceAdapter adapter object required to communicate with * this iButton. * @param newAddress address of this 1-Wire device * @see #OneWireContainer02() * @see com.dalsemi.onewire.utils.Address */ public OneWireContainer02 (DSPortAdapter sourceAdapter, long newAddress) { super(sourceAdapter, newAddress); } /** * Create a container with a provided adapter object * and the address of the iButton or 1-Wire device.<p> * * This is one of the methods to construct a container. The other is * through creating a OneWireContainer with NO parameters. * * @param sourceAdapter adapter object required to communicate with * this iButton. * @param newAddress address of this 1-Wire device * @see #OneWireContainer02() * @see com.dalsemi.onewire.utils.Address */ public OneWireContainer02 (DSPortAdapter sourceAdapter, String newAddress) { super(sourceAdapter, newAddress); } //-------- //-------- Information methods //-------- /** */ public String getName () { return "DS1991"; } /** */ public String getAlternateNames () { return "DS1425"; } /** */ public String getDescription () { return "2048 bits of nonvolatile read/write memory " + "organized as three secure keys of 384 bits each " + "and a 512 bit scratch pad. Each key has its own " + "64 bit password and 64 bit ID field. Secure " + "memory cannot be deciphered without matching 64 " + "bit password."; } //-------- //-------- I/O methods //-------- /** * Writes the data to the scratchpad from the given address. * * @param addr address to begin writing. Must be between * 0x00 and 0x3F. * @param data data to write. * * * @throws IllegalArgumentException If address is out of range, or data is to long for scratchpad * @throws OneWireIOException If device is not found on the 1-Wire network * @throws OneWireException on a communication or setup error with the 1-Wire * adapter */ public void writeScratchpad (int addr, byte[] data) throws OneWireIOException, OneWireException, IllegalArgumentException { //confirm that data will fit if (addr > 0x3F) throw new IllegalArgumentException( "Address out of range: 0x00 to 0x3F"); int dataRoom = 0x3F - addr + 1; if (dataRoom < data.length) throw new IllegalArgumentException( "Data is too long for scratchpad."); buffer [0] = WRITE_SCRATCHPAD_COMMAND; buffer [1] = ( byte ) (addr | 0xC0); buffer [2] = ( byte ) (~buffer [1]); System.arraycopy(data, 0, buffer, 3, data.length); //send command block if (adapter.select(address)) { adapter.dataBlock(buffer, 0, 3 + data.length); } else { //device must not have been present throw new OneWireIOException("MultiKey iButton " + this.getAddressAsString() + " not found on 1-Wire Network"); } } /** * Reads the entire scratchpad. * * @return <code>byte[]</code> containing the data from the scratchpad; * the array will have a length of 64. * * @throws OneWireIOException If device is not found on the 1-Wire network * @throws OneWireException on a communication or setup error with the 1-Wire * adapter */ public byte[] readScratchpad () throws OneWireIOException, OneWireException { buffer [0] = READ_SCRATCHPAD_COMMAND; buffer [1] = ( byte ) 0xC0; //Starting address of scratchpad buffer [2] = 0x3F; for (int i = 3; i < 67; i++) buffer [i] = ( byte ) 0xFF; //send command block if (adapter.select(address)) { adapter.dataBlock(buffer, 0, 67); byte[] retData = new byte [64]; System.arraycopy(buffer, 3, retData, 0, 64); return retData; } else { //device must not have been present throw new OneWireIOException("MultiKey iButton " + this.getAddressAsString() + " not found on 1-Wire Network"); } } /** * Writes the data from the scratchpad to the specified block or * blocks. Note that the write will erase the data from the * scratchpad. * * @param key subkey being written * @param passwd password for the subkey being written * @param blockNum number of the block to be copied (see page 7 of the * DS1991 data sheet) block 0-7, or 8 to copy all 64 bytes. * * * @throws IllegalArgumentException If key is out of range (0 to 2), or password is not 8 characters, or if * blockNum is out of range (0 to 8) * @throws OneWireIOException If device is not found on the 1-Wire network * @throws OneWireException on a communication or setup error with the 1-Wire * adapter */ public void copyScratchpad (int key, byte[] passwd, int blockNum) throws OneWireIOException, OneWireException, IllegalArgumentException { //confirm that input is OK
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -