Example #1
0
  /**
   * 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.
   *
   * <p>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
   * @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 {
    // find the last (non-inclusive) address for this write
    int endingOffset = (startAddr + len);
    if (((endingOffset & 0x1F) > 0) && (!enablePower)) {
      // find the number of bytes left until the end of the page
      int numBytes = pageLength - (endingOffset & 0x1F);
      if ( // endingOffset == 0x250 ???? why??
      (ibPass.hasReadWritePassword()
              && (0xFFE0 & endingOffset) == (0xFFE0 & ibPass.getReadWritePasswordAddress())
              && endingOffset
                  < (ibPass.getReadWritePasswordAddress() + ibPass.getReadWritePasswordLength()))
          || (ibPass.hasReadOnlyPassword()
              && (0xFFE0 & endingOffset) == (0xFFE0 & ibPass.getReadOnlyPasswordAddress())
              && endingOffset
                  < (ibPass.getReadOnlyPasswordAddress() + ibPass.getReadOnlyPasswordLength()))
          || (ibPass.hasWriteOnlyPassword()
              && (0xFFE0 & endingOffset) == (0xFFE0 & ibPass.getWriteOnlyPasswordAddress())
              && endingOffset
                  < (ibPass.getWriteOnlyPasswordAddress() + ibPass.getWriteOnlyPasswordLength()))) {

        // password block would be written to with potentially bad data
        throw new OneWireException(
            "Executing write would overwrite password control registers with "
                + "potentially invalid data.  Please ensure write does not occur over"
                + "password control register page, or the password control data is "
                + "specified exactly in the write buffer.");
      }

      byte[] tempBuf = new byte[len + numBytes];
      System.arraycopy(writeBuf, offset, tempBuf, 0, len);
      read(endingOffset, false, tempBuf, len, numBytes);

      super.write(startAddr, tempBuf, 0, tempBuf.length);
    } else {
      // write does extend to end of page
      super.write(startAddr, writeBuf, offset, len);
    }
  }