Esempio n. 1
0
        /// <summary>  Reads authenticated page.
        ///
        /// </summary>
        /// <param name="page">         the page number in this bank to read from.
        /// </param>
        /// <param name="data">         the data read from the address
        /// </param>
        /// <param name="extra_info">   the MAC calculated for this function
        ///
        /// </param>
        /// <throws>  OneWireIOException </throws>
        /// <throws>  OneWireException </throws>
        public virtual bool readAuthenticatedPage(int page, byte[] data, int dataStart, byte[] extra_info, int extraStart)
        {
            byte[] send_block = new byte[40];
            byte[] challenge  = new byte[8];

            int addr = (page * pageLength) + startPhysicalAddress;

            ib.getChallenge(challenge, 4);
            scratchpad.writeScratchpad(addr, challenge, 0, 8);

            // access the device
            if (!adapter.select(ib.Address))
            {
                throw new OneWireIOException("Device select failed.");
            }

            // Read Authenticated Command
            send_block[0] = READ_AUTH_PAGE;
            // address 1
            send_block[1] = (byte)(addr & 0xFF);
            // address 2
            send_block[2] = (byte)((SupportClass.URShift((addr & 0xFFFF), 8)) & 0xFF);

            // data + FF byte
            Array.Copy(ffBlock, 0, send_block, 3, 35);

            // now send the block
            adapter.dataBlock(send_block, 0, 38);

            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
            if (DEBUG)
            {
                IOHelper.writeLine("-------------------------------------------------------------");
                IOHelper.writeLine("ReadAuthPage - send_block:");
                IOHelper.writeBytesHex(send_block, 0, 38);
            }
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\

            // verify CRC16 is correct
            if (CRC16.compute(send_block, 0, 38, 0) != 0x0000B001)
            {
                throw new OneWireException("First CRC didn't pass.");
            }

            Array.Copy(send_block, 3, data, dataStart, 32);

            Array.Copy(ffBlock, 0, send_block, 0, 22);

            //adapter.startPowerDelivery(DSPortAdapter.CONDITION_NOW);
            try
            {
                //UPGRADE_TODO: Method 'java.lang.Thread.sleep' was converted to 'System.Threading.Thread.Sleep' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javalangThreadsleep_long'"
                System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 2));
            }
            catch (System.Threading.ThreadInterruptedException ie)
            {
                ;
            }

            adapter.dataBlock(send_block, 0, 22);

            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
            if (DEBUG)
            {
                IOHelper.writeLine("ReadAuthPage - MAC:");
                IOHelper.writeBytesHex(send_block, 0, 20);
            }
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\

            // verify CRC16 is correct
            if (CRC16.compute(send_block, 0, 22, 0) != 0x0000B001)
            {
                throw new OneWireException("Second CRC didn't pass.");
            }

            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
            if (DEBUG)
            {
                IOHelper.writeLine("next read:");
                IOHelper.writeBytesHex(send_block, 0, 22);
                IOHelper.writeLine("-------------------------------------------------------------");
            }
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\

            Array.Copy(send_block, 0, extra_info, extraStart, 20);
            return(true);
        }
Esempio n. 2
0
    /// <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);
        }
    }
Esempio n. 3
0
        /// <summary>
        ///  Reads authenticated page.
        /// </summary>
        /// <param name="page">          the page number in this bank to read from. </param>
        /// <param name="data">          the data read from the address </param>
        /// <param name="extra_info">    the MAC calculated for this function
        /// </param>
        /// <exception cref="OneWireIOException"> </exception>
        /// <exception cref="OneWireException"> </exception>
        public virtual bool readAuthenticatedPage(int page, byte[] data, int dataStart, byte[] extra_info, int extraStart)
        {
            byte[] send_block = new byte[40];
            byte[] challenge  = new byte[8];

            int addr = (page * pageLength) + startPhysicalAddress;

            ib.getChallenge(challenge, 4);
            scratchpad.writeScratchpad(addr, challenge, 0, 8);

            // access the device
            if (!adapter.select(ib.Address))
            {
                throw new OneWireIOException("Device select failed.");
            }

            // Read Authenticated Command
            send_block[0] = READ_AUTH_PAGE;
            // address 1
            send_block[1] = unchecked ((byte)(addr & 0xFF));
            // address 2
            send_block[2] = unchecked ((byte)(((int)((uint)(addr & 0xFFFF) >> 8)) & 0xFF));

            // data + FF byte
            Array.Copy(ffBlock, 0, send_block, 3, 35);

            // now send the block
            adapter.dataBlock(send_block, 0, 38);

            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
            if (DEBUG)
            {
                IOHelper.writeLine("-------------------------------------------------------------");
                IOHelper.writeLine("ReadAuthPage - send_block:");
                IOHelper.writeBytesHex(send_block, 0, 38);
            }
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\

            // verify CRC16 is correct
            if (CRC16.compute(send_block, 0, 38, 0) != 0x0000B001)
            {
                throw new OneWireException("First CRC didn't pass.");
            }

            Array.Copy(send_block, 3, data, dataStart, 32);

            Array.Copy(ffBlock, 0, send_block, 0, 22);

            //adapter.startPowerDelivery(DSPortAdapter.CONDITION_NOW);
            try
            {
                Thread.Sleep(2);
            }
            catch (InterruptedException)
            {
                ;
            }

            adapter.dataBlock(send_block, 0, 22);

            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
            if (DEBUG)
            {
                IOHelper.writeLine("ReadAuthPage - MAC:");
                IOHelper.writeBytesHex(send_block, 0, 20);
            }
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\

            // verify CRC16 is correct
            if (CRC16.compute(send_block, 0, 22, 0) != 0x0000B001)
            {
                throw new OneWireException("Second CRC didn't pass.");
            }

            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
            if (DEBUG)
            {
                IOHelper.writeLine("next read:");
                IOHelper.writeBytesHex(send_block, 0, 22);
                IOHelper.writeLine("-------------------------------------------------------------");
            }
            //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\

            Array.Copy(send_block, 0, extra_info, extraStart, 20);
            return(true);
        }