/**
   * Reads the account data off the SHAiButton using a READ_AUTHENTICATE command. First, this
   * function asserts that the account page number is known as well as the length of the account
   * file. Then it copies the 3 byte challenge to the scratchpad before sending the command for
   * READ_AUTHENTICATE. The 32 byte account data page is copied into dataBuffer starting at
   * dataStart.
   *
   * <p>In addition to the account data, this function also returns a calculated MAC. The MAC
   * requires 20 bytes after the start index. The return value is the write cycle counter value for
   * the account data page
   *
   * <p>
   *
   * @param chlg the buffer containing a 3-byte random challenge.
   * @param chlgStart the index into the buffer where the 3 byte challenge begins.
   * @param dataBuffer the buffer to copy the account data into
   * @param dataStart the index into the buffer where copying should begin
   * @param mac the buffer to copy the resulting Message Authentication Code
   * @param macStart the index into the mac buffer to start copying
   * @return the value of the write cycle counter for the page
   * @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
   */
  public synchronized int readAccountData(
      byte[] chlg, int chlgStart, byte[] dataBuffer, int dataStart, byte[] mac, int macStart)
      throws OneWireException, OneWireIOException {
    // init local variables
    OneWireContainer18 ibcL = this.ibc;
    byte[] rawData = this.readAccountData_rawData;
    byte[] scratchpad = this.readAccountData_scratchpad;

    // make sure account info is properly setup
    if (this.accountPageNumber < 0) {
      // user not setup
      return -1;
    }

    // copy challenge into scratchpad buffer
    System.arraycopy(chlg, 0, scratchpad, 20, 3);

    if (ibcL.eraseScratchPad(this.accountPageNumber)) {
      // send 1 byte RESUME, instead of 9 byte SELECT
      ibcL.useResume(true);

      // write the challenge to the device scratchpad
      if (ibcL.writeScratchPad(this.accountPageNumber, 0, scratchpad, 0, 32)) {

        // reads 42 bytes = 32 bytes of page data
        //               +  4 bytes page counter
        //               +  4 bytes secret counter
        //               +  2 bytes CRC
        boolean readOK = ibcL.readAuthenticatedPage(this.accountPageNumber, rawData, 0);

        // read the scratchpad for mac
        int len = ibcL.readScratchPad(scratchpad, 0);

        // disable RESUME
        ibcL.useResume(false);

        if ((!readOK) || (len < 0)) {
          // read authenticate failed
          return -1;
        }

        // get the value of the write cycle counter
        int wcc = (rawData[35] & 0x0ff);
        wcc = (wcc << 8) | (rawData[34] & 0x0ff);
        wcc = (wcc << 8) | (rawData[33] & 0x0ff);
        wcc = (wcc << 8) | (rawData[32] & 0x0ff);

        // put the accountData in our local cache
        System.arraycopy(rawData, 0, this.accountData, 0, 32);

        // put the account data into return buffer
        System.arraycopy(rawData, 0, dataBuffer, dataStart, 32);

        // copy the mac into the return buffer
        System.arraycopy(scratchpad, 8, mac, macStart, 20);

        // \\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
        if (DEBUG) {
          IOHelper.writeLine("----------------------------------------------------------");
          IOHelper.writeLine("User's ReadAuthPage");
          IOHelper.write("address: ");
          IOHelper.writeBytesHex(this.address);
          IOHelper.writeLine("speed: " + this.ibc.getAdapter().getSpeed());
          IOHelper.writeLine("RawData: ");
          IOHelper.writeBytesHex(rawData);
          IOHelper.writeLine("mac: ");
          IOHelper.writeBytesHex(mac, macStart, 20);
          IOHelper.writeLine("wcc: " + wcc);
          IOHelper.writeLine("----------------------------------------------------------");
        }
        // \\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\

        // cache the write cycle counter
        this.writeCycleCounter = wcc;

        return wcc;
      }
      // write scratchpad failed
      return -1;
    }
    // erase scratchpad failed
    return -1;
  }