/// <summary> Query to see if the specified page is redirected. /// Not supported by all devices. See the method 'canRedirectPage()'. /// /// </summary> /// <param name="page"> number of page check for redirection /// /// </param> /// <returns> return the new page number or 0 if not redirected /// /// </returns> /// <throws> OneWireIOException </throws> /// <throws> OneWireException </throws> /// <summary> /// </summary> /// <deprecated> As of 1-Wire API 0.01, replaced by {@link #getRedirectedPage(int)} /// </deprecated> public virtual int isPageRedirected(int page) { // read page that redirect byte is on int pg_len = mbRedirect.PageLength; int read_pg = (page + redirectOffset) / pg_len; // read page with byte byte[] read_buf = new byte[pg_len]; mbRedirect.readPageCRC(read_pg, false, read_buf, 0); // return page return ~ read_buf[(page + redirectOffset) % pg_len] & 0x000000FF; }
/// <summary> Query to see if the specified page has redirection locked. /// Not supported by all devices. See the method 'canRedirectPage()'. /// /// </summary> /// <param name="page"> number of page check for locked redirection /// /// </param> /// <returns> return 'true' if redirection is locked for this page /// /// </returns> /// <throws> OneWireIOException </throws> /// <throws> OneWireException </throws> public virtual bool isRedirectPageLocked(int page) { // read page that lock redirect bit is on int pg_len = mbLockRedirect.PageLength; int read_pg = (page + lockRedirectOffset) / (pg_len * 8); // read page with bit byte[] read_buf = new byte[pg_len]; mbLockRedirect.readPageCRC(read_pg, false, read_buf, 0); // return boolean on lock redirect bit int index = (page + lockRedirectOffset) - (read_pg * 8 * pg_len); int nbyt = (SupportClass.URShift(index, 3)); int nbit = index - (nbyt << 3); return !(((SupportClass.URShift(read_buf[nbyt], nbit)) & 0x01) == 0x01); }
/// <summary> /// Read a page from a memory bank and print in hex /// </summary> /// <param name="bank"> PagedMemoryBank to read a page from </param> /// <param name="pg"> page to read </param> public static void dumpBankPage(OneWireContainer33 owd, PagedMemoryBank bank, int pg) { byte[] read_buf = new byte [bank.PageLength]; byte[] extra_buf = new byte [bank.ExtraInfoLength]; byte[] challenge = new byte [8]; byte[] secret = new byte [8]; byte[] sernum = new byte [8]; bool macvalid = false; try { // read a page (use the most verbose and secure method) if (bank.hasPageAutoCRC()) { Debug.WriteLine("Using device generated CRC"); if (bank.hasExtraInfo()) { bank.readPageCRC(pg, false, read_buf, 0, extra_buf); owd.getChallenge(challenge, 0); owd.getContainerSecret(secret, 0); sernum = owd.Address; macvalid = OneWireContainer33.isMACValid(bank.StartPhysicalAddress + pg * bank.PageLength, sernum, read_buf, extra_buf, challenge, secret); } else { bank.readPageCRC(pg, false, read_buf, 0); } } else { if (bank.hasExtraInfo()) { bank.readPage(pg, false, read_buf, 0, extra_buf); } else { bank.readPage(pg, false, read_buf, 0); } } Debug.Write("Page " + pg + ": "); hexPrint(read_buf, 0, read_buf.Length); Debug.WriteLine(""); if (bank.hasExtraInfo()) { Debug.Write("Extra: "); hexPrint(extra_buf, 0, bank.ExtraInfoLength); Debug.WriteLine(""); if (macvalid) { Debug.WriteLine("Data validated with correct MAC."); } else { Debug.WriteLine("Data not validated because incorrect MAC."); } } } catch (Exception e) { Debug.WriteLine(e); } }
/// <summary> /// Dump pages from memory. /// in the provided owd instance. /// /// @parameter owd device to check for memory banks. /// @parameter showContents flag to indicate if the packet memory bank contents will /// be displayed /// </summary> public static void dumpDevicePages(OneWireContainer owd, bool showContents) { byte[] read_buf, extra_buf; int reps, i, pg, numberPages; bool found_bank = false, hasExtraInfo, hasPageAutoCRC, readContinue; Stopwatch stopWatch = new Stopwatch(); // get the port names we can use and try to open, test and close each for (System.Collections.IEnumerator bank_enum = owd.MemoryBanks; bank_enum.MoveNext();) { // get the next memory bank MemoryBank mb = (MemoryBank)bank_enum.Current; // check if has paged services if (!(mb is PagedMemoryBank)) { continue; } // cast to page bank PagedMemoryBank bank = (PagedMemoryBank)mb; // found a memory bank found_bank = true; // display bank information displayBankInformation(bank); read_buf = new byte [bank.PageLength]; extra_buf = new byte [bank.ExtraInfoLength]; // get bank flags hasPageAutoCRC = bank.hasPageAutoCRC(); hasExtraInfo = bank.hasExtraInfo(); numberPages = bank.NumberPages; // get overdrive going so not a factor in time tests try { bank.read(0, false, read_buf, 0, 1); } catch (Exception) { } // dynamically change number of reps reps = 1000 / (read_buf.Length * bank.NumberPages); if (owd.MaxSpeed == DSPortAdapter.SPEED_OVERDRIVE) { reps *= 2; } if ((reps == 0) || showContents) { reps = 1; } if (!showContents) { Debug.Write("[" + reps + "]"); } // start timer to time the dump of the bank contents stopWatch.Start(); for (i = 0; i < reps; i++) { // loop to read all of the pages in bank readContinue = false; for (pg = 0; pg < numberPages; pg++) { try { // read a page (use the most verbose and secure method) if (hasPageAutoCRC) { if (hasExtraInfo) { bank.readPageCRC(pg, readContinue, read_buf, 0, extra_buf); } else { bank.readPageCRC(pg, readContinue, read_buf, 0); } } else { if (hasExtraInfo) { bank.readPage(pg, readContinue, read_buf, 0, extra_buf); } else { bank.readPage(pg, readContinue, read_buf, 0); } } readContinue = true; if (showContents) { Debug.Write("Page " + pg + ": "); hexPrint(read_buf, 0, read_buf.Length); Debug.WriteLine(""); if (bank.hasExtraInfo()) { Debug.Write("Extra: "); hexPrint(extra_buf, 0, bank.ExtraInfoLength); Debug.WriteLine(""); } } } catch (Exception e) { Debug.WriteLine("Exception in reading page: " + e + "TRACE: "); Debug.WriteLine(e.ToString()); Debug.Write(e.StackTrace); readContinue = false; } } } stopWatch.Stop(); Debug.WriteLine(" (time to read PAGES = " + (stopWatch.ElapsedMilliseconds / reps).ToString() + "ms)"); } if (!found_bank) { Debug.WriteLine("XXXX Does not contain any general-purpose non-volatile page memory bank's"); } }