Example #1
0
        public static int EncodeComplexAck(EncodeBuffer buffer, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, byte sequence_number, byte proposed_window_number)
        {
            int len = 3;

            buffer.buffer[buffer.offset++] = (byte)type;
            buffer.buffer[buffer.offset++] = invoke_id;
            if ((type & BacnetPduTypes.SEGMENTED_MESSAGE) > 0)
            {
                buffer.buffer[buffer.offset++] = sequence_number;
                buffer.buffer[buffer.offset++] = proposed_window_number;
                len += 2;
            }
            buffer.buffer[buffer.offset++] = (byte)service;
            return(len);
        }
Example #2
0
 public static void EncodeSimpleAck(EncodeBuffer buffer, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id)
 {
     buffer.buffer[buffer.offset++] = (byte)type;
     buffer.buffer[buffer.offset++] = invoke_id;
     buffer.buffer[buffer.offset++] = (byte)service;
 }
Example #3
0
        public static void EncodeConfirmedServiceRequest(EncodeBuffer buffer, BacnetPduTypes type, BacnetConfirmedServices service, BacnetMaxSegments max_segments, BacnetMaxApdu max_adpu, byte invoke_id, byte sequence_number, byte proposed_window_size)
        {
            buffer.buffer[buffer.offset++] = (byte)type;
            buffer.buffer[buffer.offset++] = (byte)((byte)max_segments | (byte)max_adpu);
            buffer.buffer[buffer.offset++] = invoke_id;

            if ((type & BacnetPduTypes.SEGMENTED_MESSAGE) > 0)
            {
                buffer.buffer[buffer.offset++] = sequence_number;
                buffer.buffer[buffer.offset++] = proposed_window_size;
            }
            buffer.buffer[buffer.offset++] = (byte)service;
        }
Example #4
0
 private void m_comm_OnSegment(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, BacnetMaxSegments max_segments, BacnetMaxApdu max_adpu, byte sequence_number, bool first, bool more_follows, byte[] buffer, int offset, int length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         Segmented = true;
         ((System.Threading.ManualResetEvent)AsyncWaitHandle).Set();
     }
 }
Example #5
0
        // Fc
        public IAsyncResult BeginRawEncodedDecodedPropertyConfirmedRequest(BacnetAddress adr, BacnetObjectId object_id, BacnetPropertyIds property_id, BacnetConfirmedServices service_id, byte[] InOutBuffer, bool wait_for_transmit, byte invoke_id = 0)
        {
            Trace.WriteLine("Sending RawEncodedRequest ... ", null);
            if (invoke_id == 0) invoke_id = unchecked(m_invoke_id++);

            EncodeBuffer b = GetEncodeBuffer(m_client.HeaderLength);
            NPDU.Encode(b, BacnetNpduControls.PriorityNormalMessage | BacnetNpduControls.ExpectingReply, adr.RoutedSource, null, DEFAULT_HOP_COUNT, BacnetNetworkMessageTypes.NETWORK_MESSAGE_WHO_IS_ROUTER_TO_NETWORK, 0);
            APDU.EncodeConfirmedServiceRequest(b, BacnetPduTypes.PDU_TYPE_CONFIRMED_SERVICE_REQUEST | (m_max_segments != BacnetMaxSegments.MAX_SEG0 ? BacnetPduTypes.SEGMENTED_RESPONSE_ACCEPTED : 0), service_id , m_max_segments, m_client.MaxAdpuLength, invoke_id, 0, 0);

            ASN1.encode_context_object_id(b, 0, object_id.type, object_id.instance);
            ASN1.encode_context_enumerated(b, 1, (byte)property_id);

            // No content encoding to do
            if (InOutBuffer!=null)
                b.Add(InOutBuffer, InOutBuffer.Length);

            //send
            BacnetAsyncResult ret = new BacnetAsyncResult(this, adr, invoke_id, b.buffer, b.offset - m_client.HeaderLength, wait_for_transmit, m_transmit_timeout);
            ret.Resend();

            return ret;
        }
Example #6
0
 private void m_comm_OnError(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, BacnetErrorClasses error_class, BacnetErrorCodes error_code, byte[] buffer, int offset, int length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         Error = new Exception("Error from device: " + error_class + " - " + error_code);
     }
 }
Example #7
0
 private void m_comm_OnComplexAck(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, byte[] buffer, int offset, int length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         Segmented = false;
         m_result = new byte[length];
         if (length > 0) Array.Copy(buffer, offset, m_result, 0, length);
         ((System.Threading.ManualResetEvent)AsyncWaitHandle).Set();     //notify waiter even if segmented
     }
 }
Example #8
0
        private void ProcessSegment(BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, BacnetMaxSegments max_segments, BacnetMaxAdpu max_adpu, bool server, byte sequence_number, byte proposed_window_number, byte[] buffer, int offset, int length)
        {
            bool first = false;
            if (sequence_number == 0 && m_last_sequence_number == 0)
            {
                first = true;
            }
            else
            {
                //send negative ack
                if (sequence_number != (m_last_sequence_number + 1))
                {
                    SegmentAckResponse(adr, true, server, invoke_id, m_last_sequence_number, proposed_window_number);
                    Trace.WriteLine("Segment sequence out of order", null);
                    return;
                }
            }
            m_last_sequence_number = sequence_number;

            bool more_follows = (type & BacnetPduTypes.MORE_FOLLOWS) == BacnetPduTypes.MORE_FOLLOWS;
            if (!more_follows) m_last_sequence_number = 0;  //reset last sequence_number

            //send ACK
            if ((sequence_number % proposed_window_number) == 0 || !more_follows)
            {
                if (m_force_window_size) proposed_window_number = m_proposed_window_size;
                SegmentAckResponse(adr, false, server, invoke_id, sequence_number, proposed_window_number);
            }

            //Send on
            if (OnSegment != null)
                OnSegment(this, adr, type, service, invoke_id, max_segments, max_adpu, sequence_number, first, more_follows, buffer, offset, length);

            //default segment assembly. We run this separately from the above handler, to make sure that it comes after!
            if (m_default_segmentation_handling)
                PerformDefaultSegmentHandling(this, adr, type, service, invoke_id, max_segments, max_adpu, sequence_number, first, more_follows, buffer, offset, length);
        }
Example #9
0
 // Fc
 // Read or Write without APDU Data encoding nor Decoding (just Request type, Object id and Property id)
 // Data is given by the caller starting with the Tag 3 (or maybe another one), and ending with it
 // return buffer start also with the Tag 3
 public bool RawEncodedDecodedPropertyConfirmedRequest(BacnetAddress adr, BacnetObjectId object_id, BacnetPropertyIds property_id, BacnetConfirmedServices service_id, ref byte[] InOutBuffer, byte invoke_id = 0)
 {
     using (BacnetAsyncResult result = (BacnetAsyncResult)BeginRawEncodedDecodedPropertyConfirmedRequest(adr, object_id, property_id, service_id, InOutBuffer, true, invoke_id))
     {
         for (int r = 0; r < m_retries; r++)
         {
             if (result.WaitForDone(m_timeout))
             {
                 Exception ex;
                 EndRawEncodedDecodedPropertyConfirmedRequest(result, service_id, out InOutBuffer, out ex);
                 if (ex != null) throw ex;
                 else return true;
             }
             result.Resend();
         }
     }
     InOutBuffer = null;
     return false;
 }
Example #10
0
 public void SimpleAckResponse(BacnetAddress adr, BacnetConfirmedServices service, byte invoke_id)
 {
     Trace.WriteLine("Sending SimpleAckResponse ... ", null);
     EncodeBuffer b = GetEncodeBuffer(m_client.HeaderLength);
     NPDU.Encode(b, BacnetNpduControls.PriorityNormalMessage, adr.RoutedSource, null, DEFAULT_HOP_COUNT, BacnetNetworkMessageTypes.NETWORK_MESSAGE_WHO_IS_ROUTER_TO_NETWORK, 0);
     APDU.EncodeSimpleAck(b, BacnetPduTypes.PDU_TYPE_SIMPLE_ACK, service, invoke_id);
     m_client.Send(b.buffer, m_client.HeaderLength, b.offset - m_client.HeaderLength, adr, false, 0);
 }
Example #11
0
        public static int DecodeError(byte[] buffer, int offset, out BacnetPduTypes type, out BacnetConfirmedServices service, out byte invokeId)
        {
            var orgOffset = offset;

            type     = (BacnetPduTypes)buffer[offset++];
            invokeId = buffer[offset++];
            service  = (BacnetConfirmedServices)buffer[offset++];

            return(offset - orgOffset);
        }
Example #12
0
        private void OnSimpleAck(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invokeId, byte[] data, int dataOffset, int dataLength)
        {
            if (invokeId != _waitInvokeId)
            {
                return;
            }

            _waitHandle.Set();
        }
Example #13
0
        private void OnSegment(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invokeId, BacnetMaxSegments maxSegments, BacnetMaxAdpu maxAdpu, byte sequenceNumber, bool first, bool moreFollows, byte[] buffer, int offset, int length)
        {
            if (invokeId != _waitInvokeId)
            {
                return;
            }

            Segmented = true;
            _waitHandle.Set();
        }
Example #14
0
        private void OnComplexAck(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invokeId, byte[] buffer, int offset, int length)
        {
            if (invokeId != _waitInvokeId)
            {
                return;
            }

            Segmented = false;
            Result    = new byte[length];

            if (length > 0)
            {
                Array.Copy(buffer, offset, Result, 0, length);
            }

            //notify waiter even if segmented
            _waitHandle.Set();
        }
Example #15
0
 private void m_comm_OnError(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, BacnetErrorClasses error_class, BacnetErrorCodes error_code, byte[] buffer, int offset, int length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         Error = new Exception("Error from device: " + error_class + " - " + error_code);
     }
 }
Example #16
0
        /// <summary>
        /// This is a simple handling that stores all segments in memory and assembles them when done
        /// </summary>
        private void PerformDefaultSegmentHandling(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, BacnetMaxSegments max_segments, BacnetMaxAdpu max_adpu, byte sequence_number, bool first, bool more_follows, byte[] buffer, int offset, int length)
        {
            if (first)
            {
                //clear any leftover segments
                m_segments.Clear();

                //copy buffer + encode new adpu header
                type &= ~BacnetPduTypes.SEGMENTED_MESSAGE;
                int adpu_header_len = 3;
                if ((type & BacnetPduTypes.PDU_TYPE_MASK) == BacnetPduTypes.PDU_TYPE_CONFIRMED_SERVICE_REQUEST) adpu_header_len = 4;
                byte[] copy = new byte[length + adpu_header_len];
                Array.Copy(buffer, offset, copy, adpu_header_len, length);
                if ((type & BacnetPduTypes.PDU_TYPE_MASK) == BacnetPduTypes.PDU_TYPE_CONFIRMED_SERVICE_REQUEST)
                    APDU.EncodeConfirmedServiceRequest(new EncodeBuffer(copy, 0), type, service, max_segments, max_adpu, invoke_id, 0, 0);
                else
                    APDU.EncodeComplexAck(new EncodeBuffer(copy, 0), type, service, invoke_id, 0, 0);
                m_segments.AddLast(copy);       //doesn't include BVLC or NPDU
            }
            else
            {
                //copy only content part
                byte[] copy = new byte[length];
                Array.Copy(buffer, offset, copy, 0, copy.Length);
                m_segments.AddLast(copy);
            }

            //process when finished
            if (!more_follows)
            {
                //assemble whole part
                byte[] apdu_buffer = AssembleSegments();
                m_segments.Clear();

                //process
                ProcessApdu(adr, type, apdu_buffer, 0, apdu_buffer.Length);
            }
        }
Example #17
0
        protected void ProcessConfirmedServiceRequest(BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, BacnetMaxSegments max_segments, BacnetMaxAdpu max_adpu, byte invoke_id, byte[] buffer, int offset, int length)
        {
            try
            {
                Trace.WriteLine("ConfirmedServiceRequest", null);

                raw_buffer = buffer;
                raw_length = length;
                raw_offset = offset;

                if (OnConfirmedServiceRequest != null)
                    OnConfirmedServiceRequest(this, adr, type, service, max_segments, max_adpu, invoke_id, buffer, offset, length);

                //don't send segmented messages, if client don't want it
                if ((type & BacnetPduTypes.SEGMENTED_RESPONSE_ACCEPTED) == 0)
                    max_segments = BacnetMaxSegments.MAX_SEG0;

                if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_READ_PROPERTY && OnReadPropertyRequest != null)
                {
                    BacnetObjectId object_id;
                    BacnetPropertyReference property;
                    if (Services.DecodeReadProperty(buffer, offset, length, out object_id, out property) >= 0)
                        OnReadPropertyRequest(this, adr, invoke_id, object_id, property, max_segments);
                    else
                        Trace.TraceWarning("Couldn't decode DecodeReadProperty");
                }
                else if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROPERTY && OnWritePropertyRequest != null)
                {
                    BacnetObjectId object_id;
                    BacnetPropertyValue value;
                    if (Services.DecodeWriteProperty(buffer, offset, length, out object_id, out value) >= 0)
                        OnWritePropertyRequest(this, adr, invoke_id, object_id, value, max_segments);
                    else
                        Trace.TraceWarning("Couldn't decode DecodeWriteProperty");
                }
                else if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_READ_PROP_MULTIPLE && OnReadPropertyMultipleRequest != null)
                {
                    IList<BacnetReadAccessSpecification> properties;
                    if (Services.DecodeReadPropertyMultiple(buffer, offset, length, out properties) >= 0)
                        OnReadPropertyMultipleRequest(this, adr, invoke_id, properties, max_segments);
                    else
                        Trace.TraceWarning("Couldn't decode DecodeReadPropertyMultiple");
                }
                else if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_WRITE_PROP_MULTIPLE && OnWritePropertyMultipleRequest != null)
                {
                    BacnetObjectId object_id;
                    ICollection<BacnetPropertyValue> values;
                    if (Services.DecodeWritePropertyMultiple(buffer, offset, length, out object_id, out values) >= 0)
                        OnWritePropertyMultipleRequest(this, adr, invoke_id, object_id, values, max_segments);
                    else
                        Trace.TraceWarning("Couldn't decode DecodeWritePropertyMultiple");
                }
                else if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_COV_NOTIFICATION && OnCOVNotification != null)
                {
                    uint subscriberProcessIdentifier;
                    BacnetObjectId initiatingDeviceIdentifier;
                    BacnetObjectId monitoredObjectIdentifier;
                    uint timeRemaining;
                    ICollection<BacnetPropertyValue> values;
                    if (Services.DecodeCOVNotifyUnconfirmed(buffer, offset, length, out subscriberProcessIdentifier, out initiatingDeviceIdentifier, out monitoredObjectIdentifier, out timeRemaining, out values) >= 0)
                        OnCOVNotification(this, adr, invoke_id, subscriberProcessIdentifier, initiatingDeviceIdentifier, monitoredObjectIdentifier, timeRemaining, true, values, max_segments);
                    else
                        Trace.TraceWarning("Couldn't decode COVNotify");
                }
                else if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_ATOMIC_WRITE_FILE && OnAtomicWriteFileRequest != null)
                {
                    bool is_stream;
                    BacnetObjectId object_id;
                    int position;
                    uint block_count;
                    byte[][] blocks;
                    int[] counts;
                    if (Services.DecodeAtomicWriteFile(buffer, offset, length, out is_stream, out object_id, out position, out block_count, out blocks, out counts) >= 0)
                        OnAtomicWriteFileRequest(this, adr, invoke_id, is_stream, object_id, position, block_count, blocks, counts, max_segments);
                    else
                        Trace.TraceWarning("Couldn't decode AtomicWriteFile");
                }
                else if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_ATOMIC_READ_FILE && OnAtomicReadFileRequest != null)
                {
                    bool is_stream;
                    BacnetObjectId object_id;
                    int position;
                    uint count;
                    if (Services.DecodeAtomicReadFile(buffer, offset, length, out is_stream, out object_id, out position, out count) >= 0)
                        OnAtomicReadFileRequest(this, adr, invoke_id, is_stream, object_id, position, count, max_segments);
                    else
                        Trace.TraceWarning("Couldn't decode AtomicReadFile");
                }
                else if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_SUBSCRIBE_COV && OnSubscribeCOV != null)
                {
                    uint subscriberProcessIdentifier;
                    BacnetObjectId monitoredObjectIdentifier;
                    bool cancellationRequest;
                    bool issueConfirmedNotifications;
                    uint lifetime;
                    if (Services.DecodeSubscribeCOV(buffer, offset, length, out subscriberProcessIdentifier, out monitoredObjectIdentifier, out cancellationRequest, out issueConfirmedNotifications, out lifetime) >= 0)
                        OnSubscribeCOV(this, adr, invoke_id, subscriberProcessIdentifier, monitoredObjectIdentifier, cancellationRequest, issueConfirmedNotifications, lifetime, max_segments);
                    else
                        Trace.TraceWarning("Couldn't decode SubscribeCOV");
                }
                else if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_SUBSCRIBE_COV_PROPERTY && OnSubscribeCOVProperty != null)
                {
                    uint subscriberProcessIdentifier;
                    BacnetObjectId monitoredObjectIdentifier;
                    BacnetPropertyReference monitoredProperty;
                    bool cancellationRequest;
                    bool issueConfirmedNotifications;
                    uint lifetime;
                    float covIncrement;
                    if (Services.DecodeSubscribeProperty(buffer, offset, length, out subscriberProcessIdentifier, out monitoredObjectIdentifier, out monitoredProperty, out cancellationRequest, out issueConfirmedNotifications, out lifetime, out covIncrement) >= 0)
                        OnSubscribeCOVProperty(this, adr, invoke_id, subscriberProcessIdentifier, monitoredObjectIdentifier, monitoredProperty, cancellationRequest, issueConfirmedNotifications, lifetime, covIncrement, max_segments);
                    else
                        Trace.TraceWarning("Couldn't decode SubscribeCOVProperty");
                }
                else if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL && OnDeviceCommunicationControl != null)
                {
                    uint timeDuration;
                    uint enable_disable;
                    string password;
                    if (Services.DecodeDeviceCommunicationControl(buffer, offset, length, out timeDuration, out enable_disable, out password) >= 0)
                        OnDeviceCommunicationControl(this, adr, invoke_id, timeDuration, enable_disable, password, max_segments);
                    else
                        Trace.TraceWarning("Couldn't decode DeviceCommunicationControl");
                }
                else if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_REINITIALIZE_DEVICE && OnReinitializedDevice != null)
                {
                    BacnetReinitializedStates state;
                    string password;
                    if (Services.DecodeReinitializeDevice(buffer, offset, length, out state, out password) >= 0)
                        OnReinitializedDevice(this, adr, invoke_id, state, password, max_segments);
                    else
                        Trace.TraceWarning("Couldn't decode ReinitializeDevice");
                }
                else if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_EVENT_NOTIFICATION && OnEventNotify != null) // F. Chaxel
                {
                    BacnetEventNotificationData EventData;
                    if (Services.DecodeEventNotifyData(buffer, offset, length, out EventData) >= 0)
                    {
                        OnEventNotify(this, adr, EventData);
                    }
                    else
                        Trace.TraceWarning("Couldn't decode Event/Alarm Notification");
                }
                else if (service == BacnetConfirmedServices.SERVICE_CONFIRMED_READ_RANGE && OnReadRange != null)
                {
                    BacnetObjectId objectId;
                    BacnetPropertyReference property;
                    BacnetReadRangeRequestTypes requestType;
                    uint position;
                    DateTime time;
                    int count;
                    if (Services.DecodeReadRange(buffer, offset, length, out objectId, out property, out requestType, out position, out time, out count) >= 0)
                    {
                        OnReadRange(this, adr, invoke_id, objectId, property, requestType, position, time, count, max_segments);
                    }
                    else
                        Trace.TraceWarning("Couldn't decode ReadRange");
                }
                else
                {
                    Trace.TraceWarning("Confirmed service not handled: " + service.ToString());
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error in ProcessConfirmedServiceRequest: " + ex.Message);
            }
        }
Example #18
0
        private void SendComplexAck(BacnetAddress adr, byte invoke_id, Segmentation segmentation, BacnetConfirmedServices service, Action<EncodeBuffer> apdu_content_encode)
        {
            Trace.WriteLine("Sending " + System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(service.ToString().ToLower()) + " ... ", null);

            //encode
            EncodeBuffer buffer;
            if (EncodeSegment(adr, invoke_id, segmentation, service, out buffer, apdu_content_encode))
            {
                //client doesn't support segments
                if (segmentation == null)
                {
                    Trace.TraceInformation("Segmenation denied");
                    ErrorResponse(adr, service, invoke_id, BacnetErrorClasses.ERROR_CLASS_SERVICES, BacnetErrorCodes.ERROR_CODE_ABORT_APDU_TOO_LONG);
                    buffer.result = EncodeResult.Good;     //don't continue the segmentation
                    return;
                }

                //first segment? validate max segments
                if (segmentation.sequence_number == 0)  //only validate first segment
                {
                    if (segmentation.max_segments != 0xFF && segmentation.buffer.offset > (segmentation.max_segments * (GetMaxApdu() - 5)))      //5 is adpu header
                    {
                        Trace.TraceInformation("Too much segmenation");
                        ErrorResponse(adr, service, invoke_id, BacnetErrorClasses.ERROR_CLASS_SERVICES, BacnetErrorCodes.ERROR_CODE_ABORT_APDU_TOO_LONG);
                        buffer.result = EncodeResult.Good;     //don't continue the segmentation
                        return;
                    }
                    else
                        Trace.WriteLine("Segmentation required", null);
                }

                //increment before ack can do so (race condition)
                unchecked { segmentation.sequence_number++; };
            }

            //send
            m_client.Send(buffer.buffer, m_client.HeaderLength, buffer.GetLength() - m_client.HeaderLength, adr, false, 0);
        }
Example #19
0
 private void m_comm_OnSegment(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, BacnetMaxSegments max_segments, BacnetMaxAdpu max_adpu, byte sequence_number, bool first, bool more_follows, byte[] buffer, int offset, int length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         Segmented = true;
         ((System.Threading.ManualResetEvent)AsyncWaitHandle).Set();
     }
 }
Example #20
0
        // Fc
        public void EndRawEncodedDecodedPropertyConfirmedRequest(IAsyncResult result, BacnetConfirmedServices service_id, out byte[] InOutBuffer, out Exception ex)
        {
            BacnetAsyncResult res = (BacnetAsyncResult)result;
            ex = res.Error;
            if (ex == null && !res.WaitForDone(m_timeout))
                ex = new Exception("Wait Timeout");

            InOutBuffer = null;

            if (ex == null)
            {
                if (service_id == BacnetConfirmedServices.SERVICE_CONFIRMED_READ_PROPERTY)
                {
                    //decode
                    BacnetObjectId response_object_id;
                    BacnetPropertyReference response_property;
                    int offset = 0; int len = 0;
                    byte[] buffer = res.Result; byte tag_number; uint len_value_type;

                    ex = new Exception("Decode");

                    if (!ASN1.decode_is_context_tag(buffer, offset, 0))
                        return;
                    len = 1;
                    len += ASN1.decode_object_id(buffer, offset + len, out response_object_id.type, out response_object_id.instance);
                    /* Tag 1: Property ID */
                    len += ASN1.decode_tag_number_and_value(buffer, offset + len, out tag_number, out len_value_type);
                    if (tag_number != 1)
                        return;
                    len += ASN1.decode_enumerated(buffer, offset + len, len_value_type, out response_property.propertyIdentifier);

                    InOutBuffer = new byte[buffer.Length - len];
                    Array.Copy(buffer, len, InOutBuffer, 0, InOutBuffer.Length);

                    ex = null;
                }
            }

            res.Dispose();
        }
Example #21
0
 private void m_comm_OnSimpleAck(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, byte[] data, int data_offset, int data_length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         ((System.Threading.ManualResetEvent)AsyncWaitHandle).Set();
     }
 }
Example #22
0
 private void m_comm_OnComplexAck(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, byte[] buffer, int offset, int length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         Segmented = false;
         m_result  = new byte[length];
         if (length > 0)
         {
             Array.Copy(buffer, offset, m_result, 0, length);
         }
         ((System.Threading.ManualResetEvent)AsyncWaitHandle).Set();     //notify waiter even if segmented
     }
 }
Example #23
0
        protected void ProcessError(BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, byte[] buffer, int offset, int length)
        {
            try
            {
                Trace.WriteLine("Error", null);

                BacnetErrorClasses error_class;
                BacnetErrorCodes error_code;
                if (Services.DecodeError(buffer, offset, length, out error_class, out error_code) < 0)
                    Trace.TraceWarning("Couldn't decode Error");

                if (OnError != null) OnError(this, adr, type, service, invoke_id, error_class, error_code, buffer, offset, length);
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error in ProcessError: " + ex.Message);
            }
        }
Example #24
0
 private void m_comm_OnSimpleAck(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, byte[] data, int data_offset, int data_length)
 {
     if (invoke_id == m_wait_invoke_id)
     {
         ((System.Threading.ManualResetEvent)AsyncWaitHandle).Set();
     }
 }
Example #25
0
 protected void ProcessSimpleAck(BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invoke_id, byte[] buffer, int offset, int length)
 {
     try
     {
         Trace.WriteLine("SimpleAck", null);
         if (OnSimpleAck != null) OnSimpleAck(this, adr, type, service, invoke_id, buffer, offset, length);
     }
     catch (Exception ex)
     {
         Trace.TraceError("Error in ProcessSimpleAck: " + ex.Message);
     }
 }
Example #26
0
        public static int DecodeComplexAck(byte[] buffer, int offset, out BacnetPduTypes type, out BacnetConfirmedServices service, out byte invoke_id, out byte sequence_number, out byte proposed_window_number)
        {
            int org_offset = offset;

            type      = (BacnetPduTypes)buffer[offset++];
            invoke_id = buffer[offset++];

            sequence_number        = 0;
            proposed_window_number = 0;
            if ((type & BacnetPduTypes.SEGMENTED_MESSAGE) > 0)
            {
                sequence_number        = buffer[offset++];
                proposed_window_number = buffer[offset++];
            }
            service = (BacnetConfirmedServices)buffer[offset++];

            return(offset - org_offset);
        }
Example #27
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;
            }
        }
Example #28
0
        public static int DecodeConfirmedServiceRequest(byte[] buffer, int offset, out BacnetPduTypes type, out BacnetConfirmedServices service, out BacnetMaxSegments max_segments, out BacnetMaxApdu max_adpu, out byte invoke_id, out byte sequence_number, out byte proposed_window_number)
        {
            int org_offset = offset;

            type         = (BacnetPduTypes)buffer[offset++];
            max_segments = (BacnetMaxSegments)(buffer[offset] & 0xF0);
            max_adpu     = (BacnetMaxApdu)(buffer[offset++] & 0x0F);
            invoke_id    = buffer[offset++];

            sequence_number        = 0;
            proposed_window_number = 0;
            if ((type & BacnetPduTypes.SEGMENTED_MESSAGE) > 0)
            {
                sequence_number        = buffer[offset++];
                proposed_window_number = buffer[offset++];
            }
            service = (BacnetConfirmedServices)buffer[offset++];

            return(offset - org_offset);
        }
Example #29
0
        private EncodeBuffer EncodeSegmentHeader(BacnetAddress adr, byte invoke_id, Segmentation segmentation, BacnetConfirmedServices service, bool more_follows)
        {
            EncodeBuffer buffer;
            bool is_segmented = false;
            if (segmentation == null)
                buffer = GetEncodeBuffer(m_client.HeaderLength);
            else
            {
                buffer = segmentation.buffer;
                is_segmented = segmentation.sequence_number > 0 | more_follows;
            }
            buffer.Reset(m_client.HeaderLength);

            //encode
            NPDU.Encode(buffer, BacnetNpduControls.PriorityNormalMessage, adr.RoutedSource, null, DEFAULT_HOP_COUNT, BacnetNetworkMessageTypes.NETWORK_MESSAGE_WHO_IS_ROUTER_TO_NETWORK, 0);

            //set segments limits
            buffer.max_offset = buffer.offset + GetMaxApdu();
            int apdu_header = APDU.EncodeComplexAck(buffer, BacnetPduTypes.PDU_TYPE_COMPLEX_ACK | (is_segmented ? BacnetPduTypes.SEGMENTED_MESSAGE | BacnetPduTypes.SERVER : 0) | (more_follows ? BacnetPduTypes.MORE_FOLLOWS : 0), service, invoke_id, segmentation != null ? segmentation.sequence_number : (byte)0, segmentation != null ? segmentation.window_size : (byte)0);
            buffer.min_limit = (GetMaxApdu() - apdu_header) * (segmentation != null ? segmentation.sequence_number : 0);

            return buffer;
        }
Example #30
0
        public static int DecodeSimpleAck(byte[] buffer, int offset, out BacnetPduTypes type, out BacnetConfirmedServices service, out byte invoke_id)
        {
            int org_offset = offset;

            type      = (BacnetPduTypes)buffer[offset++];
            invoke_id = buffer[offset++];
            service   = (BacnetConfirmedServices)buffer[offset++];

            return(offset - org_offset);
        }
Example #31
0
        private void OnError(BacnetClient sender, BacnetAddress adr, BacnetPduTypes type, BacnetConfirmedServices service, byte invokeId, BacnetErrorClasses errorClass, BacnetErrorCodes errorCode, byte[] buffer, int offset, int length)
        {
            if (invokeId != _waitInvokeId)
            {
                return;
            }

            Error = new Exception("Error from device: " + errorClass + " - " + errorCode);
        }