Beispiel #1
0
        public int GetDiff(EncodeBuffer buffer)
        {
            var diff = Math.Abs(buffer.offset - offset);

            diff = Math.Max(Math.Abs(buffer.serialize_counter - serialize_counter), diff);
            return(diff);
        }
Beispiel #2
0
        public override EncodeBuffer Encode(Object value)
        {
            var v = value as String;

            if (v == null)
            {
                if (IsNullable)
                {
                    return(Precomputed.Zero);
                }
                else
                {
                    throw new UnexpectedNullValueException();
                }
            }

            var length = (UInt64)v.Length;

            if (IsNullable)
            {
                length++;
            }

            // Encode value
            var output = new EncodeBuffer(2);

            output.Append(new ArraySegment <Byte>(Encoding.UTF8.GetBytes(v)));

            // Prepend length
            output.SetFirst(UnsignedVlq.Encode(length)); // Offset by one, since 0 is NULL

            return(output);
        }
Beispiel #3
0
 public void Encode(EncodeBuffer buffer)
 {
     ASN1.encode_tag(buffer, (byte)BacnetApplicationTags.BACNET_APPLICATION_TAG_DATE, false, 4);
     startDate.Encode(buffer);
     ASN1.encode_tag(buffer, (byte)BacnetApplicationTags.BACNET_APPLICATION_TAG_DATE, false, 4);
     endDate.Encode(buffer);
 }
Beispiel #4
0
        public List <object> Entries; // BacnetDate or BacnetDateRange or BacnetweekNDay

        public void Encode(EncodeBuffer buffer)
        {
            if (Entries == null)
            {
                return;
            }

            foreach (ASN1.IEncode entry in Entries)
            {
                if (entry is BacnetDate)
                {
                    ASN1.encode_tag(buffer, 0, true, 4);
                    entry.Encode(buffer);
                }

                if (entry is BacnetDateRange)
                {
                    ASN1.encode_opening_tag(buffer, 1);
                    entry.Encode(buffer);
                    ASN1.encode_closing_tag(buffer, 1);
                }

                if (entry is BacnetweekNDay)
                {
                    ASN1.encode_tag(buffer, 2, true, 3);
                    entry.Encode(buffer);
                }
            }
        }
Beispiel #5
0
        private int Res2Forward(EncodeBuffer buffer, int pcmend, int[][] couples, bool[] nonzero, int channels,
                                int[][] partword)
        {
            var n = pcmend / 2;

            var used = false;

            // don't duplicate the code; use a working vector hack for now and
            // reshape ourselves into a single channel res1
            var work = new int[channels * n];

            for (var i = 0; i < channels; i++)
            {
                var pcm = couples[i];
                used = used || nonzero[i];

                for (int j = 0, k = i; j < n; j++, k += channels)
                {
                    work[k] = pcm[j];
                }
            }

            return(used
                ? Res1Forward(buffer, work, 1, partword)
                : 0);
        }
Beispiel #6
0
        public Delegate GenerateEncoder(Type type, Func <Type, Delegate> recurse)
        {
            // Get serializer for sub items
            var valueEncoder = recurse(type.GenericTypeArguments[0]);

            return(new Func <IList, EncodeBuffer>(value =>
            {
                // Handle nulls
                if (null == value)
                {
                    return Null;
                }

                // Serialize elements
                var output = new EncodeBuffer();
                foreach (var element in value)
                {
                    output.Append((EncodeBuffer)valueEncoder.DynamicInvokeTransparent(element));
                }

                // Encode length
                output.SetFirst(UnsignedVlq.Encode((UInt64)value.Count + 1));

                return output;
            }));
        }
        private void WriteEffectivePeriod()
        {
            // Manual ASN.1/BER encoding
            EncodeBuffer b = comm.GetEncodeBuffer(0);

            ASN1.encode_opening_tag(b, 3);

            DateTime dt;

            if (TxtStartDate.Text != "Always")
            {
                dt = Convert.ToDateTime(TxtStartDate.Text);
            }
            else
            {
                dt = new DateTime(0);
            }
            ASN1.encode_application_date(b, dt);

            if (TxtEndDate.Text != "Always")
            {
                dt = Convert.ToDateTime(TxtEndDate.Text);
            }
            else
            {
                dt = new DateTime(0);
            }
            ASN1.encode_application_date(b, dt);

            ASN1.encode_closing_tag(b, 3);

            Array.Resize <byte>(ref b.buffer, b.offset);
            byte[] InOutBuffer = b.buffer;
            comm.RawEncodedDecodedPropertyConfirmedRequest(adr, schedule_id, BacnetPropertyIds.PROP_EFFECTIVE_PERIOD, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, ref InOutBuffer);
        }
Beispiel #8
0
 public void Encode(EncodeBuffer buffer)
 {
     ASN1.encode_opening_tag(buffer, 1);
     ASN1.encode_application_unsigned(buffer, net);
     ASN1.encode_application_octet_string(buffer, adr, 0, adr.Length);
     ASN1.encode_closing_tag(buffer, 1);
 }
Beispiel #9
0
        public Delegate GenerateEncoder(Type type, Func <Type, Delegate> recurse)
        {
            // Get serializer for sub items
            var valueEncoder = recurse(type.GetElementType());

            return(new Func <Array, EncodeBuffer>(value =>
            {
                // Handle nulls
                if (null == value)
                {
                    return Null;
                }

                // Serialize elements
                var output = new EncodeBuffer();
                foreach (var subValue in value)
                {
                    output.Append((EncodeBuffer)valueEncoder.DynamicInvokeTransparent(subValue));
                }

                // Encode length
                output.SetFirst(UnsignedVlq.Encode((UInt64)value.Length + 1)); // Number of elements, not number of bytes

                return output;
            }));
        }
Beispiel #10
0
        public void Encode(EncodeBuffer buffer)
        {
            ASN1.encode_tag(buffer, (byte)BacnetApplicationTags.BACNET_APPLICATION_TAG_TIME, false, 4);
            ASN1.encode_bacnet_time(buffer, Time.Time);

            ASN1.bacapp_encode_application_data(buffer, Value);
        }
Beispiel #11
0
 public static void EncodeSegmentAck(EncodeBuffer buffer, BacnetPduTypes type, byte originalInvokeId, byte sequenceNumber, byte actualWindowSize)
 {
     buffer.buffer[buffer.offset++] = (byte)type;
     buffer.buffer[buffer.offset++] = originalInvokeId;
     buffer.buffer[buffer.offset++] = sequenceNumber;
     buffer.buffer[buffer.offset++] = actualWindowSize;
 }
        public void Encode(EncodeBuffer buffer)
        {
            for (int i = 0; i < 1; i++)
            {
                ASN1.encode_opening_tag(buffer, 0);

                List <ExceptionScheduleArray> loArray = loExceptionScheduleArray[i];

                for (int j = 0; j < 1; j++)
                {
                    var loExceptionScheduleTimeValue = loArray[j];
                    ASN1.bacapp_encode_application_data(buffer, new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_DATE, loArray[j].period));

                    foreach (var ds in loExceptionScheduleTimeValue.loExceptionScheduleTimeValue)
                    {
                        var loTime  = ds[0].dt;
                        var loValue = ds[0].Value;

                        ASN1.bacapp_encode_application_data(buffer, new BacnetValue(BacnetApplicationTags.BACNET_APPLICATION_TAG_TIME, loTime));
                        ASN1.bacapp_encode_application_data(buffer, new BacnetValue(loValue));
                    }
                }

                ASN1.encode_closing_tag(buffer, 0);
            }
        }
Beispiel #13
0
 private static void PackModes(EncodeBuffer buffer, CodecSetup codecSetup, int i)
 {
     buffer.Write((uint)codecSetup.ModeParams[i].BlockFlag, 1);
     buffer.Write((uint)codecSetup.ModeParams[i].WindowType, 16);
     buffer.Write((uint)codecSetup.ModeParams[i].TransformType, 16);
     buffer.Write((uint)codecSetup.ModeParams[i].Mapping, 8);
 }
Beispiel #14
0
    private static void PackComment(EncodeBuffer buffer, Comments vorbisComment)
    {
        // Preamble
        buffer.Write(0x03, 8);
        buffer.WriteString(VorbisString);

        // Vendor
        buffer.Write((uint)VendorString.Length, 32);
        buffer.WriteString(VendorString);

        // Comments
        buffer.Write((uint)vorbisComment.UserComments.Count, 32);

        foreach (var comment in vorbisComment.UserComments)
        {
            if (!string.IsNullOrEmpty(comment))
            {
                buffer.Write((uint)comment.Length, 32);
                buffer.WriteString(comment);
            }
            else
            {
                buffer.Write(0, 32);
            }
        }

        buffer.Write(1, 1);
    }
Beispiel #15
0
        public Delegate GenerateEncoder(Type type, Func <Type, Delegate> recurse)
        {
            // Get serializer for sub items
            var keyEncoder   = recurse(type.GenericTypeArguments[0]);
            var valueEncoder = recurse(type.GenericTypeArguments[1]);

            return(new Func <IDictionary, EncodeBuffer>(value =>
            {
                // Handle nulls
                if (null == value)
                {
                    return Null;
                }

                // Serialize elements
                var output = new EncodeBuffer();
                var e = value.GetEnumerator();
                UInt64 count = 0;
                while (e.MoveNext())
                {
                    output.Append((EncodeBuffer)keyEncoder.DynamicInvokeTransparent(e.Key));
                    output.Append((EncodeBuffer)valueEncoder.DynamicInvokeTransparent(e.Value));
                    count++;
                }

                // Encode length
                output.SetFirst(UnsignedVlq.Encode(count + 1));

                return output;
            }));
        }
Beispiel #16
0
 public void Encode(EncodeBuffer buffer)
 {
     buffer.Add(year);
     buffer.Add(month);
     buffer.Add(day);
     buffer.Add(wday);
 }
Beispiel #17
0
        public static void Encode(EncodeBuffer buffer, BacnetNpduControls function, BacnetAddress destination,
                                  BacnetAddress source = null, byte hopCount = 0xFF)
        {
            // Modif FC
            var hasDestination = destination != null && destination.net > 0; // && destination.net != 0xFFFF;
            var hasSource      = source != null && source.net > 0 && source.net != 0xFFFF;

            buffer.buffer[buffer.offset++] = BACNET_PROTOCOL_VERSION;
            buffer.buffer[buffer.offset++] = (byte)(function | (hasDestination ? BacnetNpduControls.DestinationSpecified : 0) | (hasSource ? BacnetNpduControls.SourceSpecified : 0));

            if (hasDestination)
            {
                buffer.buffer[buffer.offset++] = (byte)((destination.net & 0xFF00) >> 8);
                buffer.buffer[buffer.offset++] = (byte)((destination.net & 0x00FF) >> 0);

                if (destination.net == 0xFFFF)                  //patch by F. Chaxel
                {
                    buffer.buffer[buffer.offset++] = 0;
                }
                else
                {
                    buffer.buffer[buffer.offset++] = (byte)destination.adr.Length;
                    if (destination.adr.Length > 0)
                    {
                        foreach (var t in destination.adr)
                        {
                            buffer.buffer[buffer.offset++] = t;
                        }
                    }
                }
            }

            if (hasSource)
            {
                buffer.buffer[buffer.offset++] = (byte)((source.net & 0xFF00) >> 8);
                buffer.buffer[buffer.offset++] = (byte)((source.net & 0x00FF) >> 0);
                // Modif FC
                if (destination.net == 0xFFFF)
                {
                    buffer.buffer[buffer.offset++] = 0;
                }
                else
                {
                    buffer.buffer[buffer.offset++] = (byte)destination.adr.Length;
                    if (destination.adr.Length > 0)
                    {
                        foreach (var t in destination.adr)
                        {
                            buffer.buffer[buffer.offset++] = t;
                        }
                    }
                }
            }

            if (hasDestination)
            {
                buffer.buffer[buffer.offset++] = hopCount;
            }
        }
Beispiel #18
0
        private int Res1Forward(EncodeBuffer buffer, int[] work, int channels, int[][] partword)
        {
            var n = _residue.End - _residue.Begin;
            var partitionValues = n / _residue.Grouping;

            // we code the partition words for each channel, then the residual
            // words for a partition per channel until we've written all the
            // residual words for that partition word.  Then write the next
            // partition channel words
            for (var s = 0; s < _stages; s++)
            {
                for (var i = 0; i < partitionValues;)
                {
                    // first we encode a partition codeword for each channel
                    if (s == 0)
                    {
                        for (var j = 0; j < channels; j++)
                        {
                            var val = partword[j][i];
                            for (var k = 1; k < _phraseBook.Dimensions; k++)
                            {
                                val *= _residue.Partitions;
                                if (i + k < partitionValues)
                                {
                                    val += partword[j][i + k];
                                }
                            }

                            if (val < _phraseBook.Entries)
                            {
                                buffer.WriteBook(_phraseBook, val);
                            }
                        }
                    }

                    // now we encode interleaved residual values for the partitions
                    for (var k = 0; (k < _phraseBook.Dimensions) && (i < partitionValues); k++, i++)
                    {
                        var offset = i * _residue.Grouping + _residue.Begin;

                        for (var j = 0; j < channels; j++)
                        {
                            if ((_residue.SecondStages[partword[j][i]] & (1 << s)) != 0)
                            {
                                var statebook = _partitionBooks[partword[j][i]][s];
                                if (statebook != null)
                                {
                                    EncodePart(buffer, work, j + offset, _residue.Grouping, statebook);
                                }
                            }
                        }
                    }
                }
            }

            return(0);
        }
Beispiel #19
0
    public static OggPacket BuildCommentsPacket(Comments comments)
    {
        var buffer = new EncodeBuffer();

        PackComment(buffer, comments);

        var bytes = buffer.GetBytes();

        return(new OggPacket(bytes, false, 0, 1));
    }
Beispiel #20
0
    public static OggPacket BuildBooksPacket(VorbisInfo info)
    {
        var buffer = new EncodeBuffer(4096);

        PackBooks(buffer, info);

        var bytes = buffer.GetBytes();

        return(new OggPacket(bytes, false, 0, 2));
    }
Beispiel #21
0
    public static OggPacket BuildInfoPacket(VorbisInfo info)
    {
        var buffer = new EncodeBuffer();

        PackInfo(buffer, info);

        var bytes = buffer.GetBytes();

        return(new OggPacket(bytes, false, 0, 0));
    }
Beispiel #22
0
 public int Forward(
     EncodeBuffer buffer,
     int pcmend,
     int[][] couples,
     bool[] nonzero,
     int channels,
     int[][] partword)
 {
     return(Res2Forward(buffer, pcmend, couples, nonzero, channels, partword));
 }
Beispiel #23
0
        private void EncodePart(EncodeBuffer buffer, int[] vec, int offset, int n, CodeBook book)
        {
            var step = n / book.Dimensions;

            for (var i = 0; i < step; i++)
            {
                var entry = LocalBookBestError(book, vec, offset + i * book.Dimensions);
                buffer.WriteBook(book, entry);
            }
        }
        // no test here if buffer is to small
        private void WriteEffectiveWeeklySchedule()
        {
            // Write Default Schedule First
            try
            {
                BacnetValue[] bv = new BacnetValue[1];
                bv[0] = Property.DeserializeValue(TxtScheduleDefault.Text, ScheduleType);
                comm.WritePropertyRequest(adr, schedule_id, BacnetPropertyIds.PROP_SCHEDULE_DEFAULT, bv);
            }
            catch { }

            // Manual ASN.1/BER encoding
            EncodeBuffer b = comm.GetEncodeBuffer(0);

            ASN1.encode_opening_tag(b, 3);

            // Monday
            //  Time
            //  Value
            //  Time
            //  Value
            // Thusday
            //  ....
            for (int i = 0; i < 7; i++)
            {
                ASN1.encode_opening_tag(b, 0);
                TreeNode T = Schedule.Nodes[i];

                foreach (TreeNode entry in T.Nodes)
                {
                    String[] s = entry.Text.Split('=');

                    BacnetValue bdt = Property.DeserializeValue(s[0], BacnetApplicationTags.BACNET_APPLICATION_TAG_TIME);
                    BacnetValue bval;
                    if (s[1].ToLower().Contains("null"))
                    {
                        bval = new BacnetValue(null);
                    }
                    else
                    {
                        bval = Property.DeserializeValue(s[1], ScheduleType);
                    }

                    ASN1.bacapp_encode_application_data(b, bdt);
                    ASN1.bacapp_encode_application_data(b, bval);
                }

                ASN1.encode_closing_tag(b, 0);
            }
            ASN1.encode_closing_tag(b, 3);

            Array.Resize <byte>(ref b.buffer, b.offset);
            byte[] InOutBuffer = b.buffer;
            comm.RawEncodedDecodedPropertyConfirmedRequest(adr, schedule_id, BacnetPropertyIds.PROP_WEEKLY_SCHEDULE, BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY, ref InOutBuffer);
        }
        public void ASN1encode(EncodeBuffer buffer)
        {
            if (references == null)
            {
                return;
            }

            foreach (BacnetDeviceObjectPropertyReference reference in references)
            {
                reference.ASN1encode(buffer);
            }
        }
Beispiel #26
0
    private static void PackMapping(EncodeBuffer buffer, VorbisInfo info, Mapping mapping)
    {
        /* another 'we meant to do it this way' hack...  up to beta 4, we
         * packed 4 binary zeros here to signify one submapping in use.  We
         * now redefine that to mean four bitflags that indicate use of
         * deeper features; bit0:submappings, bit1:coupling,
         * bit2,3:reserved. This is backward compatible with all actual uses
         * of the beta code. */
        if (mapping.SubMaps > 1)
        {
            buffer.Write(1, 1);
            buffer.Write((uint)mapping.SubMaps - 1, 4);
        }
        else
        {
            buffer.Write(0, 1);
        }

        if (mapping.CouplingSteps > 0)
        {
            buffer.Write(1, 1);
            buffer.Write((uint)mapping.CouplingSteps - 1, 8);

            var couplingBits = Encoding.Log(info.Channels - 1);
            for (var i = 0; i < mapping.CouplingSteps; i++)
            {
                buffer.Write((uint)mapping.CouplingMag[i], couplingBits);
                buffer.Write((uint)mapping.CouplingAng[i], couplingBits);
            }
        }
        else
        {
            buffer.Write(0, 1);
        }

        buffer.Write(0, 2); // 2,3:reserved

        // we don't write the channel submappings if we only have one...
        if (mapping.SubMaps > 1)
        {
            for (var i = 0; i < info.Channels; i++)
            {
                buffer.Write((uint)mapping.ChannelMuxList[i], 4);
            }
        }

        for (var i = 0; i < mapping.SubMaps; i++)
        {
            buffer.Write(0, 8); // time submap unused
            buffer.Write((uint)mapping.FloorSubMap[i], 8);
            buffer.Write((uint)mapping.ResidueSubMap[i], 8);
        }
    }
Beispiel #27
0
        public void Encode(EncodeBuffer buffer)
        {
            ASN1.encode_opening_tag(buffer, 0);

            if (DaySchedule != null)
            {
                foreach (var dayItem in DaySchedule)
                {
                    dayItem.Encode(buffer);
                }
            }
            ASN1.encode_closing_tag(buffer, 0);
        }
Beispiel #28
0
        public static void EncodeConfirmedServiceRequest(EncodeBuffer buffer, BacnetPduTypes type, BacnetConfirmedServices service, BacnetMaxSegments maxSegments,
                                                         BacnetMaxAdpu maxAdpu, byte invokeId, byte sequenceNumber = 0, byte proposedWindowSize = 0)
        {
            buffer.buffer[buffer.offset++] = (byte)type;
            buffer.buffer[buffer.offset++] = (byte)((byte)maxSegments | (byte)maxAdpu);
            buffer.buffer[buffer.offset++] = invokeId;

            if ((type & BacnetPduTypes.SEGMENTED_MESSAGE) > 0)
            {
                buffer.buffer[buffer.offset++] = sequenceNumber;
                buffer.buffer[buffer.offset++] = proposedWindowSize;
            }
            buffer.buffer[buffer.offset++] = (byte)service;
        }
Beispiel #29
0
 public int Forward(
     EncodeBuffer buffer,
     int pcmend,
     int[][] couples,
     bool[] nonzero,
     int channels,
     int[][] partword)
 {
     return(_residue.ResidueType switch
     {
         ResidueType.One => Res1Forward(buffer, pcmend, couples, nonzero, channels, partword),
         ResidueType.Two => Res2Forward(buffer, pcmend, couples, nonzero, channels, partword),
         _ => throw new NotImplementedException(),
     });
Beispiel #30
0
    private static void PackFloor(EncodeBuffer buffer, Floor floor)
    {
        var count    = 0;
        var maxposit = floor.PostList[1];
        var maxclass = -1;

        // save out partitions
        buffer.Write((uint)floor.PartitionClass.Length, 5); // only 0 to 31 legal
        foreach (var partitionClass in floor.PartitionClass)
        {
            buffer.Write((uint)partitionClass, 4); // only 0 to 15 legal
            if (maxclass < partitionClass)
            {
                maxclass = partitionClass;
            }
        }

        // save out partition classes
        for (var j = 0; j < maxclass + 1; j++)
        {
            buffer.Write((uint)(floor.ClassDimensions[j] - 1), 3); // 1 to 8
            buffer.Write((uint)floor.ClassSubs[j], 2);             // 0 to 3
            if (floor.ClassSubs[j] != 0)
            {
                buffer.Write((uint)floor.ClassBook[j], 8);
            }

            for (var k = 0; k < 1 << floor.ClassSubs[j]; k++)
            {
                buffer.Write((uint)(floor.ClassSubBook[j][k] + 1), 8);
            }
        }

        // save out the post list, only 1,2,3,4 legal now
        buffer.Write((uint)(floor.Mult - 1), 2);

        // maxposit cannot legally be less than 1; this is encode-side, we can assume our setup is OK
        buffer.Write((uint)Encoding.Log(maxposit - 1), 4);
        var rangebits = Encoding.Log(maxposit - 1);

        for (int j = 0, k = 0; j < floor.PartitionClass.Length; j++)
        {
            count += floor.ClassDimensions[floor.PartitionClass[j]];
            for (; k < count; k++)
            {
                buffer.Write((uint)floor.PostList[k + 2], rangebits);
            }
        }
    }
Beispiel #31
0
        private bool EncodeSegment(BacnetAddress adr, byte invoke_id, Segmentation segmentation, BacnetConfirmedServices service, out EncodeBuffer buffer, Action<EncodeBuffer> apdu_content_encode)
        {
            //encode (regular)
            buffer = EncodeSegmentHeader(adr, invoke_id, segmentation, service, false);
            apdu_content_encode(buffer);

            bool more_follows = (buffer.result & EncodeResult.NotEnoughBuffer) > 0;
            if (segmentation != null && more_follows)
            {
                //reencode in segmented
                EncodeSegmentHeader(adr, invoke_id, segmentation, service, true);
                apdu_content_encode(buffer);
                return true;
            }
            else if (more_follows)
                return true;
            else
            {
                return segmentation != null ? segmentation.sequence_number > 0 : false;
            }
        }