📄 onewirecontainer37.java
字号:
{ register.write(READ_ACCESS_PASSWORD&0x3F, password, offset, 8); if(verifyPassword(password, offset, READ_ONLY_PWD)) setContainerReadOnlyPassword(password, offset); } /** * Attempts to change the value of the read/write password in the device's * register. For this to be successful, either passwords must be disabled, * or the read/write password for this container must be set and must match * the current value of the read/write password in the device's register. * * @param password the new value of 8-byte device read/write password, to be * copied into the devices register. * @param offset the offset to start copying the 8 bytes from the array */ public void setDeviceReadWritePassword(byte[] password, int offset) throws OneWireException, OneWireIOException { register.write(READ_WRITE_ACCESS_PASSWORD&0x3F, password, offset, 8); if(verifyPassword(password, offset, READ_WRITE_PWD)) setContainerReadWritePassword(password, offset); } /** * Attempts to change the value of the write only password in the device's * register. For this to be successful, either passwords must be disabled, * or the read/write password for this container must be set and must match * the current value of the read/write password in the device's register. * * @param password the new value of 8-byte device read/write password, to be * copied into the devices register. * @param offset the offset to start copying the 8 bytes from the array */ public void setDeviceWriteOnlyPassword(byte[] password, int offset) throws OneWireException, OneWireIOException { throw new OneWireException("The DS1977 does not have a write only password."); } /** * <P>Enables/disables passwords by writing to the devices password control * register. For this to be successful, either passwords must be disabled, * or the read/write password for this container must be set and must match * the current value of the read/write password in the device's register.</P> * * <P><B> * WARNING: Enabling passwords requires that both the read password and the * read/write password be re-written to the part. Before calling this method, * you should set the container read password and read/write password values. * This will ensure that the correct value is written into the part. * </B></P> * * @param enable if <code>true</code>, device passwords will be enabled. * All subsequent read and write operations will require that the * passwords for the container are set. */ public void setDevicePasswordEnable(boolean enableReadOnly, boolean enableReadWrite, boolean enableWriteOnly) throws OneWireException, OneWireIOException { if(enableWriteOnly) throw new OneWireException( "The DS1922 does not have a write only password."); if(!isContainerReadOnlyPasswordSet() && enableReadOnly) throw new OneWireException("Container Read Password is not set"); if(!isContainerReadWritePasswordSet()) throw new OneWireException("Container Read/Write Password is not set"); if(enableReadOnly != enableReadWrite) throw new OneWireException("Both read only and read/write passwords " + "will both be disable or enabled"); // must write both passwords for this to work byte[] enableCommand = new byte[1]; enableCommand[0] = (enableReadWrite?ENABLE_BYTE:DISABLE_BYTE); register.write(PASSWORD_CONTROL_REGISTER&0x3F, enableCommand, 0, 1); if(enableReadOnly) { readOnlyPasswordEnabled = true; readWritePasswordEnabled = true; } else { readOnlyPasswordEnabled = false; readWritePasswordEnabled = false; } } /** * Sets the value of the read password for the container. This is the value * used by this container to read the memory of the device. If this password * does not match the value of the read password in the device's password * register, all subsequent read operations will fail. * * @param password New 8-byte value of container's read password. * @param offset Index to start copying the password from the array. */ public void setContainerReadOnlyPassword(byte[] password, int offset) throws OneWireException { System.arraycopy(password, offset, readPassword, 0, PASSWORD_LENGTH); readPasswordSet = true; } /** * Returns the read password used by this container to read the memory * of the device. * * @param password Holds the 8-byte value of container's read password. * @param offset Index to start copying the password into the array. */ public void getContainerReadOnlyPassword(byte[] password, int offset) throws OneWireException { System.arraycopy(readPassword, 0, password, offset, PASSWORD_LENGTH); } /** * Returns true if the container's read password has been set. The return * value is not affected by whether or not the read password of the container * actually matches the value in the device's password register. * * @return <code>true</code> if the container's read password has been set */ public boolean isContainerReadOnlyPasswordSet() throws OneWireException { return readPasswordSet; } /** * Sets the value of the read/write password for the container. This is the * value used by this container to read and write to the memory of the * device. If this password does not match the value of the read/write * password in the device's password register, all subsequent read and write * operations will fail. * * @param password New 8-byte value of container's read/write password. * @param offset Index to start copying the password from the array. */ public void setContainerReadWritePassword(byte[] password, int offset) throws OneWireException { System.arraycopy(password, offset, readWritePassword, 0, 8); readWritePasswordSet = true; } /** * Returns the read/write password used by this container to read from and * write to the memory of the device. * * @param password Holds the 8-byte value of container's read/write password. * @param offset Index to start copying the password into the array. */ public void getContainerReadWritePassword(byte[] password, int offset) throws OneWireException { System.arraycopy(readWritePassword, 0, password, offset, PASSWORD_LENGTH); } /** * Returns true if the container's read/write password has been set. The * return value is not affected by whether or not the read/write password of * the container actually matches the value in the device's password * register. * * @return <code>true</code> if the container's read/write password has been * set. */ public boolean isContainerReadWritePasswordSet() throws OneWireException { return readWritePasswordSet; } /** * Sets the value of the read/write password for the container. This is the * value used by this container to read and write to the memory of the * device. If this password does not match the value of the read/write * password in the device's password register, all subsequent read and write * operations will fail. * * @param password New 8-byte value of container's read/write password. * @param offset Index to start copying the password from the array. */ public void setContainerWriteOnlyPassword(byte[] password, int offset) throws OneWireException { throw new OneWireException("The DS1977 does not have a write only password."); } /** * Returns the read/write password used by this container to read from and * write to the memory of the device. * * @param password Holds the 8-byte value of container's read/write password. * @param offset Index to start copying the password into the array. */ public void getContainerWriteOnlyPassword(byte[] password, int offset) throws OneWireException { throw new OneWireException("The DS1977 does not have a write only password."); } /** * Returns true if the container's read/write password has been set. The * return value is not affected by whether or not the read/write password of * the container actually matches the value in the device's password * register. * * @return <code>true</code> if the container's read/write password has been * set. */ public boolean isContainerWriteOnlyPasswordSet() throws OneWireException { throw new OneWireException("The DS1977 does not have a write only password."); } public boolean verifyPassword(byte[] password, int offset, int type) throws OneWireException, OneWireIOException { byte[] raw_buf = new byte [15]; int addr = ((type == READ_ONLY_PWD) ? READ_ACCESS_PASSWORD : READ_WRITE_ACCESS_PASSWORD); // command, address, offset, password (except last byte) raw_buf [0] = VERIFY_PSW_COMMAND; raw_buf [1] = ( byte ) (addr & 0xFF); raw_buf [2] = ( byte ) (((addr & 0xFFFF) >>> 8) & 0xFF); System.arraycopy(password, offset, raw_buf, 3, 8); // send block (check copy indication complete) register.ib.adapter.dataBlock(raw_buf, 0, 10); if(!register.ib.adapter.startPowerDelivery( register.ib.adapter.CONDITION_AFTER_BYTE)) { return false; } // send last byte of password and enable strong pullup register.ib.adapter.putByte(raw_buf[11]); // delay for read to complete msWait(5); // turn off strong pullup register.ib.adapter.setPowerNormal(); // read the confirmation byte if (register.ib.adapter.getByte() != 0xAA) { throw new OneWireIOException("Verifing Password failed."); } return true; }// *****************************************************************************// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// Private initilizers// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// ***************************************************************************** /** * Construct the memory banks used for I/O. */ private void initMem () { // scratchpad scratch = new MemoryBankScratchCRCPW(this); scratch.pageLength = 64; scratch.size = 64; scratch.numberPages = 1; scratch.maxPacketDataLength = 61; scratch.enablePower = true; // User Data Memory userDataMemory = new MemoryBankNVCRCPW(this, scratch); userDataMemory.numberPages = 511; userDataMemory.size = 32704; userDataMemory.pageLength = 64; userDataMemory.maxPacketDataLength = 61; userDataMemory.bankDescription = "Data Memory"; userDataMemory.startPhysicalAddress = 0x0000; userDataMemory.generalPurposeMemory = true; userDataMemory.readOnly = false; userDataMemory.readWrite = true; userDataMemory.enablePower = true; // Register register = new MemoryBankNVCRCPW(this, scratch); register.numberPages = 1; register.size = 64; register.pageLength = 64; register.maxPacketDataLength = 61; register.bankDescription = "Register control"; register.startPhysicalAddress = 0x7FC0; register.generalPurposeMemory = false; register.enablePower = true; } /** * helper method to pause for specified milliseconds */ private static final void msWait(final long ms) { try { Thread.sleep(ms); } catch(InterruptedException ie) {;} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -