Beispiel #1
0
        /// <summary>
        ///     Close Riff File.
        ///     Length is written too.
        /// </summary>
        public virtual int Close()
        {
            int retcode = DDC_SUCCESS;

            switch (Fmode)
            {
            case RFM_WRITE:

                try
                {
                    m_File.Seek(0, SeekOrigin.Begin);

                    try
                    {
                        sbyte[] br = new sbyte[8];
                        br[0] = (sbyte)(SupportClass.URShift(m_RiffHeader.CkId, 24) & 0x000000FF);
                        br[1] = (sbyte)(SupportClass.URShift(m_RiffHeader.CkId, 16) & 0x000000FF);
                        br[2] = (sbyte)(SupportClass.URShift(m_RiffHeader.CkId, 8) & 0x000000FF);
                        br[3] = (sbyte)(m_RiffHeader.CkId & 0x000000FF);

                        br[7] = (sbyte)(SupportClass.URShift(m_RiffHeader.CkSize, 24) & 0x000000FF);
                        br[6] = (sbyte)(SupportClass.URShift(m_RiffHeader.CkSize, 16) & 0x000000FF);
                        br[5] = (sbyte)(SupportClass.URShift(m_RiffHeader.CkSize, 8) & 0x000000FF);
                        br[4] = (sbyte)(m_RiffHeader.CkSize & 0x000000FF);
                        m_File.Write(SupportClass.ToByteArray(br), 0, 8);
                        m_File.Close();
                    }
                    catch
                    {
                        retcode = DDC_FILE_ERROR;
                    }
                }
                catch
                {
                    retcode = DDC_FILE_ERROR;
                }

                break;

            case RFM_READ:

                try
                {
                    m_File.Close();
                }
                catch
                {
                    retcode = DDC_FILE_ERROR;
                }

                break;
            }

            m_File = null;
            Fmode  = RFM_UNKNOWN;

            return(retcode);
        }
        public int next()
        {
            int t = x ^ (x << 11);

            x = y;
            y = z;
            z = w;
            return(w = (w ^ (SupportClass.URShift(w, 19)) ^ (t ^ (SupportClass.URShift(t, 8)))));
        }
        /// <summary> Writes the EOC marker.
        ///
        /// </summary>
        /// <exception cref="IOException">If an error occurs while closing the underlying
        /// stream.
        ///
        /// </exception>
        public override void  close()
        {
            // Write the EOC marker and close the codestream.
            // CONVERSION PROBLEM?
            out_Renamed.WriteByte((byte)SupportClass.URShift(CSJ2K.j2k.codestream.Markers.EOC, 8));
            out_Renamed.WriteByte((byte)(CSJ2K.j2k.codestream.Markers.EOC & 0x00FF));

            ndata += 2;             // Add two to length of codestream for EOC marker
        }
    /// <summary>  Computes a linear congruential random number a random number
    /// of times.
    /// </summary>
    public static int compute2(int x)
    {
        int loops = (SupportClass.URShift(x, 4)) & 7;

        while (loops-- > 0)
        {
            x = (x * 2147483647) % 16807;
        }
        return(x);
    }
Beispiel #5
0
 /// <summary> Returns 32-character hex representation of this objects hash
 ///
 /// </summary>
 /// <returns> String of this object's hash
 /// </returns>
 public static System.String toHex(sbyte[] hash)
 {
     char[] buf = new char[hash.Length * 2];
     for (int i = 0, x = 0; i < hash.Length; i++)
     {
         buf[x++] = HEX_CHARS[(SupportClass.URShift(hash[i], 4)) & 0xf];
         buf[x++] = HEX_CHARS[hash[i] & 0xf];
     }
     return(new System.String(buf));
 }
Beispiel #6
0
        /// <summary> Read a complete memory page with CRC verification provided by the
        /// device.  Not supported by all devices.  See the method
        /// 'hasPageAutoCRC()'.
        ///
        /// </summary>
        /// <param name="page">         page number to read
        /// </param>
        /// <param name="readContinue"> if 'true' then device read is continued without
        /// re-selecting.  This can only be used if the new
        /// readPagePacket() continious where the last one
        /// stopped and it is inside a
        /// 'beginExclusive/endExclusive' block.
        /// </param>
        /// <param name="readBuf">      byte array to put data read. Must have at least
        /// 'getMaxPacketDataLength()' elements.
        /// </param>
        /// <param name="offset">       offset into readBuf to place data
        ///
        /// </param>
        /// <throws>  OneWireIOException </throws>
        /// <throws>  OneWireException </throws>
        public virtual void  readPageCRC(int page, bool readContinue, byte[] readBuf, int offset)
        {
            byte[] raw_buf = new byte[5 + PAGE_LENGTH];
            int    len;

            // attempt to put device at max desired speed
            if (!readContinue)
            {
                checkSpeed();
            }

            // see if need to access the device
            if (!readContinue)
            {
                // select the device
                if (!ib.adapter.select(ib.address))
                {
                    forceVerify();

                    throw new OneWireIOException("Device select failed");
                }

                // build start reading memory block with: command, address
                len = raw_buf.Length;

                Array.Copy(ffBlock, 0, raw_buf, 0, len);

                raw_buf[0] = READ_MEMORY_COMMAND;

                int addr = page * PAGE_LENGTH + startPhysicalAddress;

                raw_buf[1] = (byte)(addr & 0xFF);
                raw_buf[2] = (byte)((SupportClass.URShift((addr & 0xFFFF), 8)) & 0xFF);
            }
            else
            {
                len = PAGE_LENGTH + 2;

                Array.Copy(ffBlock, 0, raw_buf, 0, len);
            }

            // do the block
            ib.adapter.dataBlock(raw_buf, 0, len);

            // check the CRC
            if (CRC16.compute(raw_buf, 0, len, 0) != 0x0000B001)
            {
                forceVerify();

                throw new OneWireIOException("Invalid CRC16 read from device");
            }

            // extract the data to return
            Array.Copy(raw_buf, len - 2 - PAGE_LENGTH, readBuf, offset, PAGE_LENGTH);
        }
Beispiel #7
0
        //--------
        //-------- Information methods
        //--------

        //--------
        //-------- Custom Methods for this 1-Wire Device Type
        //--------

        /// <summary> Read the counter value associated with a page on this
        /// 1-Wire Device.
        ///
        /// </summary>
        /// <param name="counterPage">   page number of the counter to read
        ///
        /// </param>
        /// <returns>  4 byte value counter stored in a long integer
        ///
        /// </returns>
        /// <throws>  OneWireIOException on a 1-Wire communication error such as  </throws>
        /// <summary>         no 1-Wire device present.  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'.
        /// </summary>
        /// <throws>  OneWireException on a communication or setup error with the 1-Wire  </throws>
        /// <summary>         adapter
        /// </summary>
        public virtual long readCounter(int counterPage)
        {
            // check if counter page provided is valid
            if ((counterPage < 12) || (counterPage > 15))
            {
                throw new OneWireException("OneWireContainer1D-invalid counter page");
            }

            // select the device
            if (adapter.select(this.address))
            {
                int crc16;

                // read memory command
                buffer[0] = READ_MEMORY_COMMAND;
                crc16     = CRC16.compute(READ_MEMORY_COMMAND);

                // address of last data byte before counter
                int address = (counterPage << 5) + 31;

                // append the address
                buffer[1] = (byte)address;
                crc16     = CRC16.compute(buffer[1], crc16);
                buffer[2] = (byte)(SupportClass.URShift(address, 8));
                crc16     = CRC16.compute(buffer[2], crc16);

                // now add the read bytes for data byte,counter,zero bits, crc16
                for (int i = 3; i < 14; i++)
                {
                    buffer[i] = (byte)SupportClass.Identity(0xFF);
                }

                // send the block
                adapter.dataBlock(buffer, 0, 14);

                // calculate the CRC16 on the result and check if correct
                if (CRC16.compute(buffer, 3, 11, crc16) == 0xB001)
                {
                    // extract the counter out of this verified packet
                    ulong return_count = 0;

                    for (int i = 4; i >= 1; i--)
                    {
                        return_count <<= 8;
                        return_count  |= (byte)(buffer[i + 3] & (byte)0xFF);
                    }

                    // return the result count
                    return((long)return_count);
                }
            }

            // device must not have been present
            throw new OneWireIOException("OneWireContainer1D-device not present");
        }
Beispiel #8
0
        // Return the position of the most significant bit set (to one) in the "value". The most
        // significant bit is position 32. If there is no bit set, return 0. Examples:
        // - findMSBSet(0) => 0
        // - findMSBSet(1) => 1
        // - findMSBSet(255) => 8
        public static int FindMsbSet(int value_Renamed)
        {
            int numDigits = 0;

            while (value_Renamed != 0)
            {
                value_Renamed = SupportClass.URShift(value_Renamed, 1);
                ++numDigits;
            }
            return(numDigits);
        }
Beispiel #9
0
        /// <summary> This method constructs a Lbyte byte array with specified length from an int.
        ///
        /// </summary>
        /// <param name="intVal">the int value to convert to a byte array.
        /// </param>
        /// <param name="byteArray">Lbyte first byte array, holds bytes from int
        /// </param>
        /// <param name="offset">byte offset into the array
        /// </param>
        /// <param name="len">number of bytes to get
        /// </param>
        public static void  toByteArray(int intVal, byte[] byteArray, int offset, int len)
        {
            int max = offset + len;

            // Concatanate the byte array into one variable.
            for (int i = offset; i < max; i++)
            {
                byteArray[i] = (byte)intVal;
                intVal       = SupportClass.URShift(intVal, 8);
            }
        }
Beispiel #10
0
        // Return the position of the most significant bit set (to one) in the "value". The most
        // significant bit is position 32. If there is no bit set, return 0. Examples:
        // - findMSBSet(0) => 0
        // - findMSBSet(1) => 1
        // - findMSBSet(255) => 8
        internal static int findMSBSet(int value_Renamed)
        {
            int numDigits = 0;

            while (value_Renamed != 0)
            {
                value_Renamed = SupportClass.URShift(value_Renamed, 1);
                ++numDigits;
            }
            return(numDigits);
        }
Beispiel #11
0
 /// <summary> Writes the long value of <tt>v</tt> (i.e., the 64 bits) to the
 /// output. Prior to writing, the output should be realigned at the byte
 /// level.
 ///
 /// </summary>
 /// <param name="v">The value to write to the output
 ///
 /// </param>
 /// <exception cref="java.io.IOException">If an I/O error ocurred.
 ///
 /// </exception>
 public override void writeLong(long v)
 {
     write((int)(SupportClass.URShift(v, 56)));
     write((int)(SupportClass.URShift(v, 48)));
     write((int)(SupportClass.URShift(v, 40)));
     write((int)(SupportClass.URShift(v, 32)));
     write((int)(SupportClass.URShift(v, 24)));
     write((int)(SupportClass.URShift(v, 16)));
     write((int)(SupportClass.URShift(v, 8)));
     write((int)v);
 }
Beispiel #12
0
        // Reverse the first len bits of a code, using straightforward code (a faster
        // method would use a table)
        // IN assertion: 1 <= len <= 15
        internal static int bi_reverse(int code, int len)
        {
            int res = 0;

            do
            {
                res  |= code & 1;
                code  = SupportClass.URShift(code, 1);
                res <<= 1;
            }while (--len > 0);
            return(SupportClass.URShift(res, 1));
        }
Beispiel #13
0
        //--------
        //-------- MemoryBank I/O methods
        //--------

        /// <summary> Read  memory in the current bank with no CRC checking (device or
        /// data). The resulting data from this API may or may not be what is on
        /// the 1-Wire device.  It is recommends that the data contain some kind
        /// of checking (CRC) like in the readPagePacket() method or have
        /// the 1-Wire device provide the CRC as in readPageCRC().  readPageCRC()
        /// however is not supported on all memory types, see 'hasPageAutoCRC()'.
        /// If neither is an option then this method could be called more
        /// then once to at least verify that the same thing is read consistantly.
        ///
        /// </summary>
        /// <param name="startAddr">    starting physical address
        /// </param>
        /// <param name="readContinue"> if 'true' then device read is continued without
        /// re-selecting.  This can only be used if the new
        /// read() continious where the last one led off
        /// and it is inside a 'beginExclusive/endExclusive'
        /// block.
        /// </param>
        /// <param name="readBuf">      byte array to place read data into
        /// </param>
        /// <param name="offset">       offset into readBuf to place data
        /// </param>
        /// <param name="len">          length in bytes to read
        ///
        /// </param>
        /// <throws>  OneWireIOException </throws>
        /// <throws>  OneWireException </throws>
        public virtual void  read(int startAddr, bool readContinue, byte[] readBuf, int offset, int len)
        {
            int i;

            // attempt to put device at max desired speed
            if (!readContinue)
            {
                sp.checkSpeed();
            }

            // check if read exceeds memory
            if ((startAddr + len) > (pageLength * numberPages))
            {
                throw new OneWireException("Read exceeds memory bank end");
            }

            // see if need to access the device
            if (!readContinue)
            {
                // select the device
                if (!ib.adapter.select(ib.address))
                {
                    sp.forceVerify();

                    throw new OneWireIOException("Device select failed");
                }

                // build start reading memory block
                int    addr    = startAddr + startPhysicalAddress;
                byte[] raw_buf = new byte[3];

                raw_buf[0] = READ_MEMORY_COMMAND;
                raw_buf[1] = (byte)(addr & 0xFF);
                raw_buf[2] = (byte)((SupportClass.URShift((addr & 0xFFFF), 8)) & 0xFF);

                // do the first block for command, address
                ib.adapter.dataBlock(raw_buf, 0, 3);
            }

            // pre-fill readBuf with 0xFF
            int pgs   = len / pageLength;
            int extra = len % pageLength;

            for (i = 0; i < pgs; i++)
            {
                Array.Copy(ffBlock, 0, readBuf, offset + i * pageLength, pageLength);
            }
            Array.Copy(ffBlock, 0, readBuf, offset + pgs * pageLength, extra);

            // send second block to read data, return result
            ib.adapter.dataBlock(readBuf, offset, len);
        }
Beispiel #14
0
        /// <summary> Close Riff File.
        /// Length is written too.
        /// </summary>
        public virtual int Close()
        {
            int retcode = DDC_SUCCESS;

            switch (fmode)
            {
            case RFM_WRITE:
                try
                {
                    file.Seek(0, System.IO.SeekOrigin.Begin);
                    try
                    {
                        sbyte[] br = new sbyte[8];
                        br[0] = (sbyte)((SupportClass.URShift(riff_header.ckID, 24)) & 0x000000FF);
                        br[1] = (sbyte)((SupportClass.URShift(riff_header.ckID, 16)) & 0x000000FF);
                        br[2] = (sbyte)((SupportClass.URShift(riff_header.ckID, 8)) & 0x000000FF);
                        br[3] = (sbyte)(riff_header.ckID & 0x000000FF);

                        br[7] = (sbyte)((SupportClass.URShift(riff_header.ckSize, 24)) & 0x000000FF);
                        br[6] = (sbyte)((SupportClass.URShift(riff_header.ckSize, 16)) & 0x000000FF);
                        br[5] = (sbyte)((SupportClass.URShift(riff_header.ckSize, 8)) & 0x000000FF);
                        br[4] = (sbyte)(riff_header.ckSize & 0x000000FF);
                        file.Write(SupportClass.ToByteArray(br), 0, 8);
                        file.Close();
                    }
                    catch (System.IO.IOException ioe)
                    {
                        retcode = DDC_FILE_ERROR;
                    }
                }
                catch (System.IO.IOException ioe)
                {
                    retcode = DDC_FILE_ERROR;
                }
                break;


            case RFM_READ:
                try
                {
                    file.Close();
                }
                catch (System.IO.IOException ioe)
                {
                    retcode = DDC_FILE_ERROR;
                }
                break;
            }
            file  = null;
            fmode = RFM_UNKNOWN;
            return(retcode);
        }
 internal static int numBitsDiffering(int a, int b)
 {
     a ^= b; // a now has a 1 bit exactly where its bit differs with b's
     // Count bits set quickly with a series of lookups:
     return(BITS_SET_IN_HALF_BYTE[a & 0x0F]
            + BITS_SET_IN_HALF_BYTE[(SupportClass.URShift(a, 4) & 0x0F)]
            + BITS_SET_IN_HALF_BYTE[(SupportClass.URShift(a, 8) & 0x0F)]
            + BITS_SET_IN_HALF_BYTE[(SupportClass.URShift(a, 12) & 0x0F)]
            + BITS_SET_IN_HALF_BYTE[(SupportClass.URShift(a, 16) & 0x0F)]
            + BITS_SET_IN_HALF_BYTE[(SupportClass.URShift(a, 20) & 0x0F)]
            + BITS_SET_IN_HALF_BYTE[(SupportClass.URShift(a, 24) & 0x0F)]
            + BITS_SET_IN_HALF_BYTE[(SupportClass.URShift(a, 28) & 0x0F)]);
 }
Beispiel #16
0
        public bool isSyncMark(int headerstring, int syncmode, int word)
        {
            bool sync = false;

            if (syncmode == INITIAL_SYNC)
            {
                //sync =  ((headerstring & 0xFFF00000) == 0xFFF00000);
                sync = (headerstring & 0xFFE00000) == 0xFFE00000; // SZD: MPEG 2.5
            }
            else
            {
                //sync = ((headerstring & 0xFFF80C00) == word)
                sync = (headerstring & 0xFFE00000) == 0xFFE00000 && // ROB -- THIS IS PROBABLY WRONG. A WEAKER CHECK.
                       (headerstring & 0x000000C0) == 0x000000C0 == single_ch_mode;
            }

            // filter out invalid sample rate
            if (sync)
            {
                sync = (SupportClass.URShift(headerstring, 10) & 3) != 3;

                if (!sync)
                {
                    Log.Trace("Bitstream: INVALID SAMPLE RATE DETECTED");
                }
            }

            // filter out invalid layer
            if (sync)
            {
                sync = (SupportClass.URShift(headerstring, 17) & 3) != 0;

                if (!sync)
                {
                    Log.Trace("Bitstream: INVALID LAYER DETECTED");
                }
            }

            // filter out invalid version
            if (sync)
            {
                sync = (SupportClass.URShift(headerstring, 19) & 3) != 1;

                if (!sync)
                {
                    Log.Trace("Bitstream: INVALID VERSION DETECTED");
                }
            }

            return(sync);
        }
Beispiel #17
0
        //--------
        //-------- MemoryBank I/O methods
        //--------

        /// <summary> Read  memory in the current bank with no CRC checking (device or
        /// data). The resulting data from this API may or may not be what is on
        /// the 1-Wire device.  It is recommends that the data contain some kind
        /// of checking (CRC) like in the readPagePacket() method or have
        /// the 1-Wire device provide the CRC as in readPageCRC().  readPageCRC()
        /// however is not supported on all memory types, see 'hasPageAutoCRC()'.
        /// If neither is an option then this method could be called more
        /// then once to at least verify that the same thing is read consistantly.
        ///
        /// </summary>
        /// <param name="startAddr">    starting physical address
        /// </param>
        /// <param name="readContinue"> if 'true' then device read is continued without
        /// re-selecting.  This can only be used if the new
        /// read() continious where the last one led off
        /// and it is inside a 'beginExclusive/endExclusive'
        /// block.
        /// </param>
        /// <param name="readBuf">      byte array to place read data into
        /// </param>
        /// <param name="offset">       offset into readBuf to place data
        /// </param>
        /// <param name="len">          length in bytes to read
        ///
        /// </param>
        /// <throws>  OneWireIOException </throws>
        /// <throws>  OneWireException </throws>
        public virtual void  read(int startAddr, bool readContinue, byte[] readBuf, int offset, int len)
        {
            byte[] buff = new byte[150];

            Array.Copy(ffBlock, 0, buff, 0, 150);

            // check if read exceeds memory
            if ((startAddr + len) > (pageLength * numberPages))
            {
                throw new OneWireException("Read exceeds memory bank end");
            }

            if (len < 0)
            {
                throw new OneWireException("Invalid length");
            }


            // attempt to put device at max desired speed
            if (!readContinue)
            {
                sp.checkSpeed();

                // select the device
                if (ib.adapter.select(ib.address))
                {
                    buff[0] = READ_MEMORY_COMMAND;

                    // address 1
                    buff[1] = (byte)((startAddr + startPhysicalAddress) & 0xFF);
                    // address 2
                    buff[2] = (byte)((SupportClass.URShift(((startAddr + startPhysicalAddress) & 0xFFFF), 8)) & 0xFF);

                    ib.adapter.dataBlock(buff, 0, len + 3);

                    // extract the data
                    Array.Copy(buff, 3, readBuf, offset, len);
                }
                else
                {
                    throw new OneWireIOException("Device select failed");
                }
            }
            else
            {
                ib.adapter.dataBlock(buff, 0, len);

                // extract the data
                Array.Copy(buff, 0, readBuf, offset, len);
            }
        }
Beispiel #18
0
        protected internal virtual sbyte[] toByteArray(short[] samples, int offs, int len)
        {
            sbyte[] b   = getByteArray(len * 2);
            int     idx = 0;
            short   s;

            while (len-- > 0)
            {
                s        = samples[offs++];
                b[idx++] = (sbyte)s;
                b[idx++] = (sbyte)(SupportClass.URShift(s, 8));
            }
            return(b);
        }
Beispiel #19
0
        // Return the mask bit for "getMaskPattern" at "x" and "y". See 8.8 of JISX0510:2004 for mask
        // pattern conditions.
        internal static bool getDataMaskBit(int maskPattern, int x, int y)
        {
            if (!QRCodeInternal.isValidMaskPattern(maskPattern))
            {
                throw new System.ArgumentException("Invalid mask pattern");
            }
            int intermediate, temp;

            switch (maskPattern)
            {
            case 0:
                intermediate = (y + x) & 0x1;
                break;

            case 1:
                intermediate = y & 0x1;
                break;

            case 2:
                intermediate = x % 3;
                break;

            case 3:
                intermediate = (y + x) % 3;
                break;

            case 4:
                intermediate = ((SupportClass.URShift(y, 1)) + (x / 3)) & 0x1;
                break;

            case 5:
                temp         = y * x;
                intermediate = (temp & 0x1) + (temp % 3);
                break;

            case 6:
                temp         = y * x;
                intermediate = (((temp & 0x1) + (temp % 3)) & 0x1);
                break;

            case 7:
                temp         = y * x;
                intermediate = (((temp % 3) + ((y + x) & 0x1)) & 0x1);
                break;

            default:
                throw new System.ArgumentException("Invalid mask pattern: " + maskPattern);
            }
            return(intermediate == 0);
        }
 /// <summary> Writes the contents of the bit buffer and byte aligns the output by
 /// filling bits with an alternating sequence of 0's and 1's.
 ///
 /// </summary>
 internal virtual void flush()
 {
     if (delFF)
     {
         // There was a bit stuffing
         if (bpos != 6)
         {
             // Bit buffer is not empty
             // Output delayed 0xFF
             out_Renamed.write(0xFF);
             nb++;
             delFF = false;
             // Pad to byte boundary with an alternating sequence of 0's
             // and 1's.
             bbuf |= (SupportClass.URShift(PAD_SEQ, (6 - bpos)));
             // Output the bit buffer
             out_Renamed.write(bbuf);
             nb++;
             bpos = 7;
             bbuf = 0;
         }
         else if (isPredTerm)
         {
             out_Renamed.write(0xFF);
             nb++;
             out_Renamed.write(0x2A);
             nb++;
             bpos  = 7;
             bbuf  = 0;
             delFF = false;
         }
     }
     else
     {
         // There was no bit stuffing
         if (bpos != 7)
         {
             // Bit buffer is not empty
             // Pad to byte boundary with an alternating sequence of 0's and
             // 1's.
             bbuf |= (SupportClass.URShift(PAD_SEQ, (6 - bpos)));
             // Output the bit buffer (bbuf can not be 0xFF)
             out_Renamed.write(bbuf);
             nb++;
             bpos = 7;
             bbuf = 0;
         }
     }
 }
        //--------
        //-------- ScratchPad methods
        //--------

        /// <summary> Write to the scratchpad page of memory a NVRAM device.
        ///
        /// </summary>
        /// <param name="startAddr">    starting address
        /// </param>
        /// <param name="writeBuf">     byte array containing data to write
        /// </param>
        /// <param name="offset">       offset into readBuf to place data
        /// </param>
        /// <param name="len">          length in bytes to write
        ///
        /// </param>
        /// <throws>  OneWireIOException </throws>
        /// <throws>  OneWireException </throws>
        public override void  writeScratchpad(int startAddr, byte[] writeBuf, int offset, int len)
        {
            bool calcCRC = false;

            if (len > pageLength)
            {
                throw new OneWireException("Write exceeds memory bank end");
            }

            // select the device
            if (!ib.adapter.select(ib.address))
            {
                forceVerify();

                throw new OneWireIOException("Device select failed");
            }

            // build block to send
            byte[] raw_buf = new byte[pageLength + 5];             //[37];

            raw_buf[0] = WRITE_SCRATCHPAD_COMMAND;
            raw_buf[1] = (byte)(startAddr & 0xFF);
            raw_buf[2] = (byte)((SupportClass.URShift((startAddr & 0xFFFF), 8)) & 0xFF);

            Array.Copy(writeBuf, offset, raw_buf, 3, len);

            // check if full page (can utilize CRC)
            if (((startAddr + len) % pageLength) == 0)
            {
                Array.Copy(ffBlock, 0, raw_buf, len + 3, 2);

                calcCRC = true;
            }

            // send block, return result
            ib.adapter.dataBlock(raw_buf, 0, len + 3 + ((calcCRC)?2:0));
            //System.out.println("WriteScratchpad: " + com.dalsemi.onewire.utils.Convert.toHexString(raw_buf));

            // check crc
            if (calcCRC)
            {
                if (CRC16.compute(raw_buf, 0, len + 5, 0) != 0x0000B001)
                {
                    forceVerify();

                    throw new OneWireIOException("Invalid CRC16 read from device");
                }
            }
        }
Beispiel #22
0
        /// <summary> Write the bit state in a byte array.
        ///
        /// </summary>
        /// <param name="state">new state of the bit 1, 0
        /// </param>
        /// <param name="index">bit index into byte array
        /// </param>
        /// <param name="offset">byte offset into byte array to start
        /// </param>
        /// <param name="buf">byte array to manipulate
        /// </param>
        public static void  arrayWriteBit(int state, int index, int offset, byte[] buf)
        {
            int nbyt = (SupportClass.URShift(index, 3));
            int nbit = index - (nbyt << 3);

            if (state == 1)
            {
                buf[nbyt + offset] |= (byte)((0x01 << nbit));
            }
            else
            {
                //buf[nbyt + offset] &= ~ (0x01 << nbit);
                buf[nbyt + offset] &= (byte)~(0x01 << nbit); // !!!
            }
        }
Beispiel #23
0
        /// <since> 1.5
        /// </since>
        public static int hashCode(long[] a)
        {
            if (a == null)
            {
                return(0);
            }
            int hash = 1;

            for (int i = 0; i < a.Length; i++)
            {
                long e = a[i];
                hash = 31 * hash + (int)(e ^ (SupportClass.URShift(e, 32)));
            }
            return(hash);
        }
Beispiel #24
0
        /// <summary> Writes the IEEE float value <tt>v</tt> (i.e., 32 bits) to the
        /// output. Prior to writing, the output should be realigned at the byte
        /// level.
        ///
        /// </summary>
        /// <param name="v">The value to write to the output
        ///
        /// </param>
        /// <exception cref="java.io.IOException">If an I/O error ocurred.
        ///
        /// </exception>
        public override void writeFloat(float v)
        {
            // CONVERSION PROBLEM? OPTIMIZE!!!
            //byte[] floatbytes = BitConverter.GetBytes(v);
            //for (int i = floatbytes.Length-1; i >= 0 ; i--) write(floatbytes[i]);

            //UPGRADE_ISSUE: Method 'java.lang.Float.floatToIntBits' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javalangFloatfloatToIntBits_float'"
            //int intV = Float.floatToIntBits(v);
            int intV = BitConverter.ToInt32(BitConverter.GetBytes(v), 0);

            write(SupportClass.URShift(intV, 24));
            write(SupportClass.URShift(intV, 16));
            write(SupportClass.URShift(intV, 8));
            write(intV);
        }
Beispiel #25
0
        private static sbyte[] encode(int[] input, int len)
        {
            int i, j;

            sbyte[] out_Renamed;
            out_Renamed = new sbyte[len];
            for (i = j = 0; j < len; i++, j += 4)
            {
                out_Renamed[j]     = (sbyte)(input[i] & 0xff);
                out_Renamed[j + 1] = (sbyte)((SupportClass.URShift(input[i], 8)) & 0xff);
                out_Renamed[j + 2] = (sbyte)((SupportClass.URShift(input[i], 16)) & 0xff);
                out_Renamed[j + 3] = (sbyte)((SupportClass.URShift(input[i], 24)) & 0xff);
            }
            return(out_Renamed);
        }
Beispiel #26
0
        /// <summary>
        ///     Read bits from buffer into the lower bits of an unsigned int.
        ///     The LSB contains the latest read bit of the stream.
        ///     (between 1 and 16, inclusive).
        /// </summary>
        public int GetBitsFromBuffer(int countBits)
        {
            int returnvalue = 0;
            int sum         = m_BitIndex + countBits;

            // E.B
            // There is a problem here, wordpointer could be -1 ?!
            if (m_WordPointer < 0)
            {
                m_WordPointer = 0;
            }
            // E.B : End.

            if (sum <= 32)
            {
                // all bits contained in *wordpointer
                returnvalue = SupportClass.URShift(m_FrameBuffer[m_WordPointer], 32 - sum) & bitmask[countBits];

                // returnvalue = (wordpointer[0] >> (32 - sum)) & bitmask[number_of_bits];
                if ((m_BitIndex += countBits) == 32)
                {
                    m_BitIndex = 0;
                    m_WordPointer++; // added by me!
                }

                return(returnvalue);
            }

            // Magouille a Voir
            //((short[])&returnvalue)[0] = ((short[])wordpointer + 1)[0];
            //wordpointer++; // Added by me!
            //((short[])&returnvalue + 1)[0] = ((short[])wordpointer)[0];
            int Right = m_FrameBuffer[m_WordPointer] & 0x0000FFFF;

            m_WordPointer++;
            int Left = m_FrameBuffer[m_WordPointer] & (int)SupportClass.Identity(0xFFFF0000);

            returnvalue = ((Right << 16) & (int)SupportClass.Identity(0xFFFF0000)) |
                          (SupportClass.URShift(Left, 16) & 0x0000FFFF);

            returnvalue = SupportClass.URShift(returnvalue, 48 - sum);
            // returnvalue >>= 16 - (number_of_bits - (32 - bitindex))
            returnvalue &= bitmask[countBits];
            m_BitIndex   = sum - 32;

            return(returnvalue);
        }
Beispiel #27
0
        /// <summary>
        ///     Feed a bitstring to the crc calculation (length between 0 and 32, not inclusive).
        /// </summary>
        public void add_bits(int bitstring, int length)
        {
            int bitmask = 1 << (length - 1);

            do
            {
                if (((m_Crc & 0x8000) == 0) ^ ((bitstring & bitmask) == 0))
                {
                    m_Crc <<= 1;
                    m_Crc  ^= Polynomial;
                }
                else
                {
                    m_Crc <<= 1;
                }
            } while ((bitmask = SupportClass.URShift(bitmask, 1)) != 0);
        }
Beispiel #28
0
        /// <summary>
        /// Feed a bitstring to the crc calculation (length between 0 and 32, not inclusive).
        /// </summary>
        internal void AddBits(int bitstring, int length)
        {
            int bitmask = 1 << (length - 1);

            do
            {
                if (((_CRC & 0x8000) == 0) ^ ((bitstring & bitmask) == 0))
                {
                    _CRC <<= 1;
                    _CRC  ^= Polynomial;
                }
                else
                {
                    _CRC <<= 1;
                }
            }while ((bitmask = SupportClass.URShift(bitmask, 1)) != 0);
        }
Beispiel #29
0
        private System.String rwxString(int v, int r)
        {
            v = SupportClass.URShift(v, r);

            System.String rwx = ((((v & 0x04) != 0)?"r":"-") + (((v & 0x02) != 0)?"w":"-"));

            if (((r == 6) && ((permissions & S_ISUID) == S_ISUID)) || ((r == 3) && ((permissions & S_ISGID) == S_ISGID)))
            {
                rwx += (((v & 0x01) != 0)?"s":"S");
            }
            else
            {
                rwx += (((v & 0x01) != 0)?"x":"-");
            }

            return(rwx);
        }
        /// <summary> Write to the Scratch Pad, which is a max of 8 bytes...  Note that if
        /// less than 8 bytes are written, the ending offset will still report
        /// that a full eight bytes are on the buffer.  This means that all 8 bytes
        /// of the data in the scratchpad will be copied, not just the bytes user
        /// wrote into it.
        ///
        /// </summary>
        /// <param name="addr">         the address to write the data to
        /// </param>
        /// <param name="out_buf">      byte array to write into scratch pad
        /// </param>
        /// <param name="offset">       offset into out_buf to write the data
        /// </param>
        /// <param name="len">          length of the write data
        ///
        /// </param>
        /// <throws>  OneWireIOException </throws>
        /// <throws>  OneWireException </throws>
        public virtual bool writeScratchpad(int addr, byte[] out_buf, int offset, int len)
        {
            byte[] send_block = new byte[14];

            // protect send buffer
            // since the scratchpad is only eight bytes, there is no reason to write
            // more than eight bytes..  and we can optimize our send buffer's size.
            if (len > 8)
            {
                len = 8;
            }

            // access the device
            if (ib.adapter.select(ib.Address))
            {
                int cnt = 0;
                // set data block up
                // start by sending the write scratchpad command
                send_block[cnt++] = WRITE_SCRATCHPAD_COMMAND;
                // followed by the target address
                send_block[cnt++] = (byte)(addr & 0x00FF);
                send_block[cnt++] = (byte)((SupportClass.URShift((addr & 0x00FFFF), 8)) & 0x00FF);

                // followed by the data to write to the scratchpad
                Array.Copy(out_buf, offset, send_block, 3, len);
                cnt += len;

                // followed by two bytes for reading CRC16 value
                send_block[cnt++] = (byte)SupportClass.Identity(0x00FF);
                send_block[cnt++] = (byte)SupportClass.Identity(0x00FF);

                // send the data
                ib.adapter.dataBlock(send_block, 0, cnt);

                // verify the CRC is correct
                //         if (CRC16.compute(send_block, 0, cnt) != 0x0000B001)
                //            throw new OneWireIOException("Invalid CRC16 in Writing Scratch Pad");
            }
            else
            {
                throw new OneWireIOException("Device select failed.");
            }

            return(true);
        }