Esempio n. 1
0
        private int Notify_Encode_Data(ref Byte[] apdu, BACNET_COV_DATA cov_data, int pos)
        {
            int len      = 0;                   /* length of each encoding */
            int apdu_len = 0;
            BACNET_PROPERTY_VALUE value = null; /* value in list */

            /* tag 0 - subscriberProcessIdentifier */
            len       = BasicalProcessor.Encode_Context_Unsigned(ref apdu, 0, cov_data.subscriberProcessIdentifier, pos + apdu_len);
            apdu_len += len;
            /* tag 1 - initiatingDeviceIdentifier */
            len       = BasicalProcessor.Encode_Context_ObjectId(ref apdu, 1, (int)BACNET_OBJECT_TYPE.OBJECT_DEVICE, cov_data.initiatingDeviceIdentifier, pos + apdu_len);
            apdu_len += len;
            /* tag 2 - monitoredObjectIdentifier */
            len       = BasicalProcessor.Encode_Context_ObjectId(ref apdu, 2, (int)cov_data.monitoredObjectIdentifier.type, cov_data.monitoredObjectIdentifier.instance, pos + apdu_len);
            apdu_len += len;
            /* tag 3 - timeRemaining */
            len       = BasicalProcessor.Encode_Context_Unsigned(ref apdu, 3, cov_data.timeRemaining, pos + apdu_len);
            apdu_len += len;
            /* tag 4 - listOfValues */
            len       = BasicalProcessor.Encode_Opening_Tag(ref apdu, 4, pos + apdu_len);
            apdu_len += len;
            /* the first value includes a pointer to the next value, etc */



            for (int count = 0; count < cov_data.listOfValues.Count; count++)
            {
                value = cov_data.listOfValues[count];
                /* tag 0 - propertyIdentifier */
                len       = BasicalProcessor.Encode_Context_Enumerate(ref apdu, 0, (uint)value.propertyIdentifier, pos + apdu_len);
                apdu_len += len;
                /* tag 1 - propertyArrayIndex OPTIONAL */
                if (value.propertyArrayIndex != BacnetConst.BACNET_ARRAY_ALL)
                {
                    len =
                        BasicalProcessor.Encode_Context_Unsigned(ref apdu, 1,
                                                                 (uint)value.propertyArrayIndex, apdu_len);
                    apdu_len += len;
                }
                /* tag 2 - value */
                len       = BasicalProcessor.Encode_Opening_Tag(ref apdu, 2, pos + apdu_len);
                apdu_len += len;
                len       = BasicalProcessor.Encode_Application_Data(ref apdu, ref value.value, apdu_len + pos);
                apdu_len += len;
                len       = BasicalProcessor.Encode_Closing_Tag(ref apdu, 2, apdu_len + pos);
                apdu_len += len;
                /* tag 3 - priority OPTIONAL */
                if (value.priority != 0)
                {
                    len =
                        BasicalProcessor.Encode_Context_Unsigned(ref apdu, 3, value.priority, apdu_len);
                    apdu_len += len;
                }
            }

            len       = BasicalProcessor.Encode_Closing_Tag(ref apdu, 4, pos + apdu_len);
            apdu_len += len;

            return(apdu_len);
        }
        int Cov_Subscribe_Encode(ref Byte[] apdu, int invoke_id, ref BACNET_SUBSCRIBE_COV_DATA cov_data, int pos)
        {
            int len      = 0;   /* length of each encoding */
            int apdu_len = 0;

            apdu[pos + 0] = (byte)BACNET_PDU_TYPE.PDU_TYPE_CONFIRMED_SERVICE_REQUEST;
            apdu[pos + 1] = BasicalProcessor.Encode_MaxSegsandApdu(0, 1476);
            apdu[pos + 2] = (Byte)invoke_id;
            apdu[pos + 3] = (byte)BACNET_CONFIRMED_SERVICE.SERVICE_CONFIRMED_SUBSCRIBE_COV;
            apdu_len      = 4;
            len           = BasicalProcessor.Encode_Context_Unsigned(ref apdu, 0, cov_data.subscriberProcessIdentifier, pos + apdu_len);
            apdu_len     += len;

            len       = BasicalProcessor.Encode_Context_ObjectId(ref apdu, 1, (int)cov_data.monitoredObjectIdentifier.type, cov_data.monitoredObjectIdentifier.instance, pos + apdu_len);
            apdu_len += len;

            /*
             * If both the 'Issue Confirmed Notifications' and
             * 'Lifetime' parameters are absent, then this shall
             * indicate a cancellation request.
             */
            if (!cov_data.cancellationRequest)
            {
                /* tag 2 - issueConfirmedNotifications */
                len =
                    BasicalProcessor.Encode_Context_Boolean(ref apdu, 2,
                                                            cov_data.issueConfirmedNotifications, pos + apdu_len);
                apdu_len += len;
                /* tag 3 - lifetime */
                len       = BasicalProcessor.Encode_Context_Unsigned(ref apdu, 3, cov_data.lifetime, pos + apdu_len);
                apdu_len += len;
            }

            return(apdu_len);
        }
Esempio n. 3
0
        private int Decode_Service_Request(ref Byte[] apdu, uint request_len, ref ACKNOWLEDGE_ALARM_DATA Ack_data)
        {
            int len = 0;

            int    section_length = 0;
            UInt32 value          = 0;

            /* tag 0 - processIdentifier */
            section_length = BasicalProcessor.Decode_Context_Unsigned(ref apdu, 0, ref Ack_data.ProcessIdentifier, len);
            len           += section_length;
            /* tag 1 - eventObjectIdentifier */
            section_length = BasicalProcessor.Decode_Object_Id(ref apdu, ref Ack_data.EventIdentifier.type, ref Ack_data.EventIdentifier.instance, len);
            len           += section_length;
            /* tag2  EventStare */
            section_length             = BasicalProcessor.Decode_Enumerated(ref apdu, 2, ref value, len);
            Ack_data.StateAcknowledged = (BACNET_EVENT_STATE)value;
            len += section_length;

            /* tag 3 - timeStamp */
            section_length = BasicalProcessor.Decode_Context_Timestamp(ref apdu, 3, ref Ack_data.TimeStamp, len);
            len           += section_length;
            /* tag 4 Acknowledgment Source */
            section_length = BasicalProcessor.Decode_Context_Character_String(ref apdu, 4, ref Ack_data.Source, len);
            len           += section_length;

            /* tag 5 - AcktimeStamp */
            section_length = BasicalProcessor.Decode_Context_Timestamp(ref apdu, 5, ref Ack_data.TimeOfAcknowledgment, len);
            len           += section_length;

            return(len);
        }
Esempio n. 4
0
        private int Read_Property_Encode(ref byte[] apdu, Byte invoke_id, BACnet_Read_Property_Data rpdata, int pos)
        {
            int len      = 0;    /* length of each encoding */
            int apdu_len = 0;

            apdu[pos + 0] = (byte)BACNET_PDU_TYPE.PDU_TYPE_CONFIRMED_SERVICE_REQUEST;
            apdu[pos + 1] = BasicalProcessor.Encode_MaxSegsandApdu(0, 1476);
            apdu[pos + 2] = invoke_id;
            apdu[pos + 3] = (byte)BACNET_CONFIRMED_SERVICE.SERVICE_CONFIRMED_READ_PROPERTY;
            apdu_len      = 4;

            len       = BasicalProcessor.Encode_Context_ObjectId(ref apdu, 0, (int)rpdata.object_type, rpdata.object_instance, pos + apdu_len);
            apdu_len += len;
            if ((int)rpdata.object_property <= 4194303)
            {
                len       = BasicalProcessor.Encode_Context_Enumerate(ref apdu, 1, (uint)rpdata.object_property, pos + apdu_len);
                apdu_len += len;
            }

            if (rpdata.array_index != BacnetConst.BACNET_ARRAY_ALL)
            {
                len =
                    BasicalProcessor.Encode_Context_Unsigned(ref apdu, 2,
                                                             (UInt32)rpdata.array_index, pos + apdu_len);
                apdu_len += len;
            }


            return(apdu_len);
        }
Esempio n. 5
0
        private int Acknowledge_Alarm_Encode(ref Byte[] apdu, Byte invoke_id, ref ACKNOWLEDGE_ALARM_DATA Ack_data, int pos)
        {
            int len      = 0;   /* length of each encoding */
            int apdu_len = 0;

            apdu[pos + 0] = (byte)BACNET_PDU_TYPE.PDU_TYPE_CONFIRMED_SERVICE_REQUEST;
            apdu[pos + 1] = BasicalProcessor.Encode_MaxSegsandApdu(0, 1476);
            apdu[pos + 2] = invoke_id;
            apdu[pos + 3] = (byte)BACNET_CONFIRMED_SERVICE.SERVICE_CONFIRMED_ACKNOWLEDGE_ALARM;
            apdu_len      = 4;

            /* tag 0 - processIdentifier */
            len       = BasicalProcessor.Encode_Context_Unsigned(ref apdu, 0, Ack_data.ProcessIdentifier, pos + apdu_len);
            apdu_len += len;
            /* tag 1 - eventObjectIdentifier */
            len       = BasicalProcessor.Encode_Context_ObjectId(ref apdu, 1, (int)Ack_data.EventIdentifier.type, Ack_data.EventIdentifier.instance, pos + apdu_len);
            apdu_len += len;
            /* tag 2 - toState */
            len       = BasicalProcessor.Encode_Context_Enumerate(ref apdu, 2, (uint)Ack_data.StateAcknowledged, apdu_len + pos);
            apdu_len += len;
            /* tag 3 - timeStamp */
            len       = BasicalProcessor.Encode_Context_Timestamp(ref apdu, 3, ref Ack_data.TimeStamp, pos + apdu_len);
            apdu_len += len;
            /* tag 4 - Acknowledgment Source */

            len       = BasicalProcessor.Encode_Context_Character_String(ref apdu, 4, ref Ack_data.Source, pos + apdu_len);
            apdu_len += len;
            /* tag 5- timeStamp */
            len       = BasicalProcessor.Encode_Context_Timestamp(ref apdu, 5, ref Ack_data.TimeOfAcknowledgment, pos + apdu_len);
            apdu_len += len;
            return(apdu_len);
        }
Esempio n. 6
0
        public int Encode(ref byte[] buf, ref BACNET_ADDRESS dest, ref BACNET_NPDU_DATA npdu, int pdu_len)
        {
            UInt16 BVLC_length = 0;

            byte[] data = new byte[1024];
            Array.Copy(buf, 0, data, 4, pdu_len);
            buf    = data;
            buf[0] = 0x81;          //BVLL_TYPE_BACNET_IP
            if (dest.net == 0xFFFF) //全局广播

            {
                buf[1] = (byte)BACNET_BVLC_FUNCTION.BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK;
            }
            else if (dest.mac_len == 0)
            {
                buf[1] = (byte)BACNET_BVLC_FUNCTION.BVLC_ORIGINAL_BROADCAST_NPDU;
            }
            else if (dest.mac_len == 6)
            {
                buf[1] = (byte)BACNET_BVLC_FUNCTION.BVLC_ORIGINAL_UNICAST_NPDU;
            }



            BVLC_length = (UInt16)(pdu_len + 4);
            BasicalProcessor.Encode_Unsigned16(ref buf, BVLC_length, 2);
            return(BVLC_length);
        }
Esempio n. 7
0
        public int  Read_Property_ACK_Pack_Int(ref Byte[] apdu, Byte invoke_id, ref BACnet_Read_Property_Data rpdata, int pos)
        {
            int len      = 0;   /* length of each encoding */
            int apdu_len = 0;

            apdu[pos + 0] = (Byte)BACNET_PDU_TYPE.PDU_TYPE_COMPLEX_ACK;                     /* complex ACK service */
            apdu[pos + 1] = invoke_id;                                                      /* original invoke id from request */
            apdu[pos + 2] = (Byte)BACNET_CONFIRMED_SERVICE.SERVICE_CONFIRMED_READ_PROPERTY; /* service choice */
            apdu_len      = 3;
            len           = BasicalProcessor.Encode_Context_ObjectId(ref apdu, 0, (int)rpdata.object_type, rpdata.object_instance, pos + apdu_len);
            apdu_len     += len;
            len           = BasicalProcessor.Encode_Context_Enumerate(ref apdu, 1, (UInt32)rpdata.object_property, pos + apdu_len);
            apdu_len     += len;
            //  if (rpdata->array_index != BACNET_ARRAY_ALL) {

            len       = BasicalProcessor.Encode_Opening_Tag(ref apdu, 3, pos + apdu_len);
            apdu_len += len;

            len       = Device_Read_Property(ref apdu, ref rpdata, pos + apdu_len);
            apdu_len += len;

            len       = BasicalProcessor.Encode_Closing_Tag(ref apdu, 3, pos + apdu_len);
            apdu_len += len;
            return(apdu_len);
        }
Esempio n. 8
0
        public int Decode_Ack_Service_Request(ref Byte[] apdu, UInt16 apdu_len, ref BACnet_Read_Property_Data rpdata, int pos = 0)
        {
            Byte   tag_number     = 0;
            UInt32 len_value_type = 0;
            int    tag_len        = 0; /* length of tag decode */
            int    len            = 0; /* total length of decodes */
            UInt16 object_type    = 0; /* object type */
            UInt32 property       = 0; /* for decoding */
            UInt32 array_value    = 0; /* for decoding */

            len  = 1;
            len += BasicalProcessor.Decode_Object_Id(ref apdu, ref object_type, ref rpdata.object_instance, len);
            rpdata.object_type = (BACNET_OBJECT_TYPE)object_type;
            len += BasicalProcessor.Decode_Tag_number_and_Value(ref apdu, ref tag_number, ref len_value_type, len);
            if (tag_number != 1)
            {
                return(-1);
            }
            len += BasicalProcessor.Decode_Enumerated(ref apdu, len_value_type, ref property, len);
            rpdata.object_property = (BACNET_PROPERTY_ID)property;
            /* Tag 2: Optional Array Index 没有 */
            rpdata.array_index = BacnetConst.BACNET_ARRAY_ALL;
            if (BasicalProcessor.Decode_Is_Opening_Tag_Number(ref apdu, 3, pos + len))
            {
                len++;
            }
            rpdata.application_data_pos = len;
            rpdata.application_data_len = (int)apdu_len - len - 1;
            return(len);
        }
        public UInt16 Decode_confirmed_Service_Request(ref Byte[] apdu, UInt16 apdu_len, ref BACNET_CONFIRMED_SERVICE_DATA service_data, ref Byte service_choice,
                                                       ref UInt16 service_requst_len, int pos)
        {
            UInt16 len = 0; /* counts where we are in PDU */

            service_data.segmented_message           = (apdu[0 + pos] & 0x08) != 0 ? true : false;
            service_data.more_follows                = (apdu[0] & 0x04) != 0 ? true : false;
            service_data.segmented_response_accepted =
                (apdu[pos] & 0x02) != 0 ? true : false;
            service_data.max_segs  = BasicalProcessor.Decode_Max_Segs(ref apdu, pos + 1);
            service_data.max_resp  = BasicalProcessor.Decode_Max_Apdu(ref apdu, pos + 1);
            service_data.invoke_id = apdu[pos + 2];



            len = 3;
            if (service_data.segmented_message)
            {
                service_data.sequence_number        = apdu[pos + len++];
                service_data.proposed_window_number = apdu[pos + len++];
            }
            service_choice = apdu[pos + len++];

            ;
            service_requst_len = (UInt16)(apdu_len - len);

            return(len);
        }
Esempio n. 10
0
        private int Decode_Service_Request(ref Byte[] request, uint request_len, ref BACnet_Read_Property_Data rpdata)
        {
            int    len            = 0;
            Byte   tag_number     = 0;
            UInt32 len_value_type = 0;
            UInt16 type           = 0; /* for decoding */
            UInt32 property       = 0; /* for decoding */
            UInt32 array_value    = 0; /* for decoding */

            //此处有对于错误请求的判断 decode_is_context_tag(&apdu[len++], 0)
            len++;
            len += BasicalProcessor.Decode_Object_Id(ref request, ref type, ref rpdata.object_instance, len);
            rpdata.object_type = (BACNET_OBJECT_TYPE)type;



            len += BasicalProcessor.Decode_Tag_number_and_Value(ref request, ref tag_number, ref len_value_type, len);
            //if (tag_number != 1)...


            len += BasicalProcessor.Decode_Enumerated(ref request, len_value_type, ref property, len);
            rpdata.object_property = (BACNET_PROPERTY_ID)property;
            // /* Tag 2: Optional Array Index */if (len < apdu_len)

            rpdata.application_data_len = (int)request_len - len - 1;
            return(len);
        }
Esempio n. 11
0
        public int error_encode_apdu(ref Byte[] apdu, Byte invoke_id, BACNET_CONFIRMED_SERVICE service, BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code, int pos)
        {
            int apdu_len = 0;

            apdu[pos + 0] = (Byte)BACNET_PDU_TYPE.PDU_TYPE_ERROR;
            apdu[pos + 1] = invoke_id;
            apdu[pos + 2] = (Byte)service;
            apdu_len      = 3;
            apdu_len     += BasicalProcessor.Encode_Application_Enumerated(ref apdu, (uint)error_class, pos + apdu_len);
            apdu_len     += BasicalProcessor.Encode_Application_Enumerated(ref apdu, (uint)error_code, pos + apdu_len);
            return(apdu_len);
        }
Esempio n. 12
0
        public int Device_Read_Property(ref Byte[] apdu, ref BACnet_Read_Property_Data rpdata, int pos)
        {
            int len = 0;
            BACNET_APPLICATION_DATA_VALUE value = new BACNET_APPLICATION_DATA_VALUE();

            value.context_specific = false;
            switch (rpdata.object_property)
            {
            case BACNET_PROPERTY_ID.PROP_LIFT_Car_Direction:
            {
                value.tag = 2;
                value.value.Unsigned_Int = Form_main.true_lift.Car_Direction;
                break;
            }

            case BACNET_PROPERTY_ID.PROP_LIFT_Car_Position:
            {
                value.tag = 2;
                value.value.Unsigned_Int = 4;
                break;
            }

            case BACNET_PROPERTY_ID.PROP_LIFT_Device_Identifer:
            {
                value.tag = (Byte)BACNET_APPLICATION_TAG.BACNET_APPLICATION_TAG_CHARACTER_STRING;
                for (int i = 0; i < Form_main.lift_count; i++)
                {
                    if (Form_main.lift_list[i].Object_Identifier.instance == rpdata.object_instance)
                    {
                        String result = Form_main.lift_list[i].Identification_Number;

                        value.value.Character_String          = new BACNET_CHARACTER_STRING(result.Length);
                        value.value.Character_String.size     = (uint)result.Length;
                        value.value.Character_String.encoding = 1;
                        value.value.Character_String.value    = result.ToCharArray();
                    }
                }


                break;
            }
            }


            len = BasicalProcessor.Encode_Application_Data(ref apdu, ref value, pos);

            //此处读出属性 加打入编码app类型和值
            //X
            // uint 61
            return(len);
        }
Esempio n. 13
0
        public void Read_Property_Ack_Handler(ref Byte[] request, UInt16 request_len, ref BACNET_ADDRESS src, ref BACNET_CONFIRMED_SERVICE_ACK_DATA service_data)
        {
            int len = 0;
            BACnet_Read_Property_Data data = new BACnet_Read_Property_Data();

            len = Decode_Ack_Service_Request(ref request, request_len, ref data);

            BACNET_APPLICATION_DATA_VALUE value = new BACNET_APPLICATION_DATA_VALUE();

            BasicalProcessor.Decode_Application_Data(ref request, (uint)data.application_data_len, ref value, data.application_data_pos);


            MessageBox.Show(value.value.Unsigned_Int.ToString(), data.object_property.ToString());
            //  if (len > 0)
            //   rp_ack_print_data(ref data);
            //此处添加对读取到的data的处理
        }
        private int Decode_Service_Request(ref Byte[] request, uint request_len, ref BACNET_SUBSCRIBE_COV_DATA data)
        {
            int    len           = 0;
            Byte   tag_number    = 0;
            UInt32 len_value     = 0;
            UInt32 decoded_value = 0; /* for decoding */
            UInt16 decoded_type  = 0; /* for decoding */

            // if (decode_is_context_tag(&apdu[len], 0))
            len += BasicalProcessor.Decode_Tag_number_and_Value(ref request, ref tag_number, ref len_value, len);
            len += BasicalProcessor.Decode_Unsigned(ref request, len_value, ref decoded_value, len);
            data.subscriberProcessIdentifier = decoded_value;
            /* tag 1 - monitoredObjectIdentifier */
            len += BasicalProcessor.Decode_Tag_number_and_Value(ref request, ref tag_number, ref len_value, len);
            len += BasicalProcessor.Decode_Object_Id(ref request, ref decoded_type, ref data.monitoredObjectIdentifier.instance, len);
            data.monitoredObjectIdentifier.type = decoded_type;
            /* optional parameters - if missing, means cancellation */
            if ((uint)len < request_len)
            {
                if (BasicalProcessor.Decode_Is_Context_Tag(ref request, 2, len))
                {
                    data.cancellationRequest = false;
                    len += BasicalProcessor.Decode_Tag_number_and_Value(ref request, ref tag_number, ref len_value, len);
                    data.issueConfirmedNotifications = BasicalProcessor.Decode_Context_Boolean(ref request, len);
                    len += (int)len_value;
                    //  if (decode_is_context_tag(&apdu[len], 3))
                    len += BasicalProcessor.Decode_Tag_number_and_Value(ref request, ref tag_number, ref len_value, len);

                    len          += BasicalProcessor.Decode_Unsigned(ref request, len_value, ref decoded_value, len);
                    data.lifetime = decoded_value;
                }
                else
                {
                    data.cancellationRequest = true;
                }
            }
            else
            {
                data.cancellationRequest = true;
            }

            return(len);
        }
 public static void Get_Broadcast_Address(ref BACNET_ADDRESS dest_address, Boolean local, UInt16 port)
 {
     dest_address.net = 0;
     dest_address.len = 6;
     for (int u = 0; u < 7; u++)
     {
         dest_address.adr[u] = 0;
     }
     dest_address.mac_len = 6;
     for (int i = 0; i < 4; i++)
     {
         dest_address.mac[i] = 255;
     }
     BasicalProcessor.Encode_Unsigned16(ref dest_address.mac, port, 4);
     if (!local)
     {
         dest_address.net = 0xFFFF;    //远程广播
     }
 }
        public static int Encode_Application_Data(ref Byte[] apdu, ref BACNET_APPLICATION_DATA_VALUE value, int pos)
        {
            int apdu_len = 0;

            switch (value.tag)
            {
            case (Byte)BACNET_APPLICATION_TAG.BACNET_APPLICATION_TAG_UNSIGNED_INT:
            {
                apdu_len  = BasicalProcessor.Encode_Unsigned(ref apdu, value.value.Unsigned_Int, pos + 1);
                apdu_len += BasicalProcessor.Encode_Tag(ref apdu, (Byte)BACNET_APPLICATION_TAG.BACNET_APPLICATION_TAG_UNSIGNED_INT, false, (uint)apdu_len, pos);

                break;
            }

            case (Byte)BACNET_APPLICATION_TAG.BACNET_APPLICATION_TAG_BOOLEAN:
            {
                apdu_len += BasicalProcessor.Encode_Application_Boolbean(ref apdu, value.value.Boolean, pos);
                break;
            }

            case (Byte)BACNET_APPLICATION_TAG.BACNET_APPLICATION_TAG_DATE:
            {
                apdu_len = BasicalProcessor.Encode_Application_Date(ref apdu, ref value.value.Date, pos);

                break;
            }

            case (Byte)BACNET_APPLICATION_TAG.BACNET_APPLICATION_TAG_TIME:
            {
                apdu_len = BasicalProcessor.Encode_Application_Time(ref apdu, ref value.value.Time, pos);
                break;
            }

            case (Byte)BACNET_APPLICATION_TAG.BACNET_APPLICATION_TAG_CHARACTER_STRING:
            {
                apdu_len = BasicalProcessor.Encode_Application_Character_String(ref apdu, ref value.value.Character_String, pos);
                break;
            }
            }


            return(apdu_len);
        }
        public static int Decode_Application_Data(ref Byte[] apdu, uint max_apdu_len, ref BACNET_APPLICATION_DATA_VALUE value, int pos)
        {
            int    len            = 0;
            int    tag_len        = 0;
            int    decode_len     = 0;
            Byte   tag_number     = 0;
            UInt32 len_value_type = 0;

            // if (apdu && value && !IS_CONTEXT_SPECIFIC(*apdu))
            value.context_specific = false;
            tag_len    = BasicalProcessor.Decode_Tag_number_and_Value(ref apdu, ref tag_number, ref len_value_type, pos);
            len       += tag_len;
            value.tag  = tag_number;
            decode_len = BasicalProcessor.Decode_Data(ref apdu, tag_number, len_value_type, ref value, pos + len);
            //  if (value->tag != MAX_BACNET_APPLICATION_TAG)

            len       += decode_len;
            value.next = null;
            return(len);
        }
        public static void Get_My_Address(ref BACNET_ADDRESS my_address)
        {
            my_address.net = 0;
            my_address.len = 6;
            for (int i = 0; i < 7; i++)
            {
            }
            my_address.mac_len = 6;
            my_address.mac[0]  = 192;
            my_address.mac[1]  = 168;
            my_address.mac[2]  = 110;
            my_address.mac[3]  = 1;
            BasicalProcessor.Encode_Unsigned16(ref my_address.mac, 50, 4);

            my_address.adr[0] = 192;
            my_address.adr[1] = 168;
            my_address.adr[2] = 110;
            my_address.adr[3] = 1;
            BasicalProcessor.Encode_Unsigned16(ref my_address.adr, 50, 4);
            //测试用
        }
Esempio n. 19
0
        ///////////////////////////////oooooooooooooooooooooooooooooooooooooooooooooo///////////////////////////////////////////////////////////////////////////////////
        public int Decode(ref BACNET_ADDRESS src, ref byte[] buf)
        {
            UInt16 npdu_len = 0;

            BasicalProcessor.Decode_Unsigned16(ref buf, ref npdu_len, 2);
            npdu_len = (UInt16)(npdu_len - 4);

            int function_type = buf[1];

            switch (function_type)
            {
            case (byte)BACNET_BVLC_FUNCTION.BVLC_ORIGINAL_BROADCAST_NPDU:
            {
                byte[] data = new byte[1024];
                Array.Copy(buf, 4, data, 0, npdu_len);
                buf = data;
                break;
            }

            case (byte)BACNET_BVLC_FUNCTION.BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK:
            {
                byte[] data = new byte[1024];
                Array.Copy(buf, 4, data, 0, npdu_len);
                buf = data;
                break;
            }

            case (byte)BACNET_BVLC_FUNCTION.BVLC_ORIGINAL_UNICAST_NPDU:
            {
                byte[] data = new byte[1024];
                Array.Copy(buf, 4, data, 0, npdu_len);
                buf = data;
                break;
            }
            }
            return(npdu_len);
        }
Esempio n. 20
0
        private int Unconfirm_Cov_Decode(ref Byte[] request, uint service_len, ref BACNET_COV_DATA data)
        {
            int    len           = 0; /* return value */
            Byte   tag_number    = 0;
            UInt32 len_value     = 0;
            UInt32 decoded_value = 0; /* for decoding */
            UInt16 decoded_type  = 0; /* for decoding */
            UInt32 property      = 0; /* for decoding */

            /* value in list */
            /* tag 0 - subscriberProcessIdentifier */
            //  if (decode_is_context_tag(&apdu[len], 0))
            len += BasicalProcessor.Decode_Tag_number_and_Value(ref request, ref tag_number, ref len_value, len);
            len += BasicalProcessor.Decode_Unsigned(ref request, len_value, ref decoded_value, len);
            data.subscriberProcessIdentifier = decoded_value;
            /* tag 1 - initiatingDeviceIdentifier */
            len += BasicalProcessor.Decode_Tag_number_and_Value(ref request, ref tag_number, ref len_value, len);
            len += BasicalProcessor.Decode_Object_Id(ref request, ref decoded_type, ref data.initiatingDeviceIdentifier, len);
            /* tag 2 - monitoredObjectIdentifier */
            len += BasicalProcessor.Decode_Tag_number_and_Value(ref request, ref tag_number, ref len_value, len);
            len += BasicalProcessor.Decode_Object_Id(ref request, ref decoded_type, ref data.monitoredObjectIdentifier.instance, len);
            /* tag 3 - timeRemaining */
            len += BasicalProcessor.Decode_Tag_number_and_Value(ref request, ref tag_number, ref len_value, len);
            len += BasicalProcessor.Decode_Unsigned(ref request, len_value, ref decoded_value, len);
            data.timeRemaining = decoded_value;

            /* tag 4: opening context tag - listOfValues */
            //   if (!decode_is_opening_tag_number(&apdu[len], 4)) {
            len++;

            while (true) //应该设置遇到时间戳就BREAK? 下面CLOSING TAG有break
            {
                BACNET_PROPERTY_VALUE value = new BACNET_PROPERTY_VALUE();
                /* tag 0 - propertyIdentifier */

                if (BasicalProcessor.Decode_Is_Context_Tag(ref request, 0, len))
                {
                    len += BasicalProcessor.Decode_Tag_number_and_Value(ref request, ref tag_number, ref len_value, len);
                    len += BasicalProcessor.Decode_Enumerated(ref request, len_value, ref property, len);
                    value.propertyIdentifier = (BACNET_PROPERTY_ID)property;
                }
                else
                {
                    return(-1);
                }
                /* tag 1 - propertyArrayIndex OPTIONAL */
                if (BasicalProcessor.Decode_Is_Context_Tag(ref request, 1, len))
                {
                    len += BasicalProcessor.Decode_Tag_number_and_Value(ref request, ref tag_number, ref len_value, len);
                    len += BasicalProcessor.Decode_Unsigned(ref request, len_value, ref decoded_value, len);
                    value.propertyArrayIndex = (UInt32)decoded_value;
                }
                else
                {
                    value.propertyArrayIndex = BacnetConst.BACNET_ARRAY_ALL;//#define BACNET_ARRAY_ALL (~(unsigned int)0)
                }
                /* tag 2: opening context tag - value */
                if (!BasicalProcessor.Decode_Is_Opening_Tag_Number(ref request, 2, len))
                {
                    return(-1);
                }
                len++;
                len += BasicalProcessor.Decode_Application_Data(ref request, (uint)(service_len - len), ref value.value, len);

                if (!BasicalProcessor.Decode_Is_Closing_Tag_Number(ref request, 2, len))
                {
                    return(-1);
                }
                len++;

                /* tag 3 - priority OPTIONAL */
                if (BasicalProcessor.Decode_Is_Context_Tag(ref request, 3, len))
                {
                    len           += BasicalProcessor.Decode_Tag_number_and_Value(ref request, ref tag_number, ref len_value, len);
                    len           += BasicalProcessor.Decode_Unsigned(ref request, len_value, ref decoded_value, len);
                    value.priority = (Byte)decoded_value;
                }
                else
                {
                    value.priority = 0;
                }
                data.listOfValues.Add(value);
                if (BasicalProcessor.Decode_Is_Closing_Tag_Number(ref request, 4, len))
                {
                    break;
                }
            }

            return(len);
        }
        private int Encode_Service_Request(ref Byte[] apdu, ref BACNET_EVENT_NOTIFICATION_DATA data, int pos)
        {
            int len      = 0;   /* length of each encoding */
            int apdu_len = 0;

            /* tag 0 - processIdentifier */
            len       = BasicalProcessor.Encode_Context_Unsigned(ref apdu, 0, data.processIdentifier, pos + apdu_len);
            apdu_len += len;
            /* tag 1 - initiatingObjectIdentifier */
            len       = BasicalProcessor.Encode_Context_ObjectId(ref apdu, 1, (int)data.initiatingObjectIdentifier.type, data.initiatingObjectIdentifier.instance, pos + apdu_len);
            apdu_len += len;
            /* tag 2 - eventObjectIdentifier */
            len       = BasicalProcessor.Encode_Context_ObjectId(ref apdu, 2, (int)data.eventObjectIdentifier.type, data.eventObjectIdentifier.instance, pos + apdu_len);
            apdu_len += len;
            /* tag 3 - timeStamp */
            len       = BasicalProcessor.Encode_Context_Timestamp(ref apdu, 3, ref data.timeStamp, pos + apdu_len);
            apdu_len += len;
            /* tag 4 - noticicationClass */
            len       = BasicalProcessor.Encode_Context_Unsigned(ref apdu, 4, data.notificationClass, apdu_len + pos);
            apdu_len += len;
            /* tag 5 - priority */
            len       = BasicalProcessor.Encode_Context_Unsigned(ref apdu, 5, data.priority, apdu_len + pos);
            apdu_len += len;
            /* tag 6 - eventType */
            len       = BasicalProcessor.Encode_Context_Enumerate(ref apdu, 6, (uint)data.eventType, apdu_len + pos);
            apdu_len += len;
            /* tag 7 - messageText */
            // if (data->messageText) 可选参数
            /* tag 8 - notifyType */
            len       = BasicalProcessor.Encode_Context_Enumerate(ref apdu, 8, (uint)data.notifyType, apdu_len + pos);
            apdu_len += len;
            /* tag 9 - ackRequired */
            switch (data.notifyType)
            {
            case BACNET_NOTIFY_TYPE.NOTIFY_ALARM:
            case BACNET_NOTIFY_TYPE.NOTIFY_EVENT:
                /* tag 9 - ackRequired */
                len       = BasicalProcessor.Encode_Context_Boolean(ref apdu, 9, data.ackRequired, apdu_len + pos);
                apdu_len += len;
                /* tag 10 - fromState */
                len = BasicalProcessor.Encode_Context_Enumerate(ref apdu, 10, (uint)data.fromState, apdu_len + pos);

                apdu_len += len;
                break;

            default:
                break;
            }
            /* tag 11 - toState */
            len       = BasicalProcessor.Encode_Context_Enumerate(ref apdu, 11, (uint)data.toState, apdu_len + pos);
            apdu_len += len;

            /* tag 12 - event values */
            if (data.notifyType == BACNET_NOTIFY_TYPE.NOTIFY_ALARM || data.notifyType == BACNET_NOTIFY_TYPE.NOTIFY_EVENT)
            {
                switch (data.eventType)
                {
                //EVENT_CHANGE_OF_STATE:

                /*   case BACNET_EVENT_TYPE.EVENT_CHANGE_OF_STATE:
                 * {
                 * len = BasicalProcessor.Encode_Opening_Tag(ref apdu, 12,apdu_len+pos);
                 * apdu_len += len;
                 * len = BasicalProcessor.Encode_Opening_Tag(ref apdu, 1, apdu_len + pos);
                 * apdu_len += len;
                 * len = BasicalProcessor.Encode_Opening_Tag(ref apdu, 0, apdu_len + pos);
                 * apdu_len += len;
                 * len=BasicalProcessor.Encode_Property_State(ref apdu,ref data.notificationParams.newState,apdu_len+pos);
                 * apdu_len += len;
                 * len = BasicalProcessor.Encode_Closing_Tag(ref apdu, 0, apdu_len + pos);
                 * apdu_len += len;
                 * len = BasicalProcessor.Encode_Context_Bitstring(ref apdu, 1, ref data.notificationParams.statusFlags, apdu_len + pos);
                 * apdu_len += len;
                 * len = BasicalProcessor.Encode_Opening_Tag(ref apdu,1, apdu_len + pos);
                 * apdu_len += len;
                 * len = BasicalProcessor.Encode_Closing_Tag(ref apdu, 12,apdu_len+pos);
                 * apdu_len += len;
                 * break;
                 *  }*/
                case BACNET_EVENT_TYPE.EVENT_CHANGE_OF_LIFE_SAFETY:
                {
                    len       = BasicalProcessor.Encode_Opening_Tag(ref apdu, 8, apdu_len + pos);
                    apdu_len += len;

                    len = BasicalProcessor.Encode_Context_Enumerate(ref apdu, 0, (UInt16)data.notificationParams.change_of_lifesafety.newState, apdu_len + pos);

                    apdu_len += len;
                    len       = BasicalProcessor.Encode_Context_Enumerate(ref apdu, 1, (UInt16)data.notificationParams.change_of_lifesafety.newMode, apdu_len + pos);

                    apdu_len += len;
                    len       = BasicalProcessor.Encode_Context_Bitstring(ref apdu, 2, ref data.notificationParams.change_of_lifesafety.statusFlags, apdu_len + pos);
                    apdu_len += len;

                    len = BasicalProcessor.Encode_Context_Enumerate(ref apdu, 3, (UInt16)data.notificationParams.change_of_lifesafety.operationExpected, apdu_len + pos);

                    apdu_len += len;


                    len       = BasicalProcessor.Encode_Closing_Tag(ref apdu, 8, apdu_len + pos);
                    apdu_len += len;
                    break;
                }
                }
            }
            return(apdu_len);
        }
        public int Uevent_Notify_Decode(ref Byte[] apdu, uint apdu_len, ref BACNET_EVENT_NOTIFICATION_DATA data)
        {
            int    len            = 0; /* return value */
            int    section_length = 0;
            UInt32 value          = 0;
            Byte   tag_number     = 0;
            uint   len_value      = 0;

            /* tag 0 - processIdentifier */
            section_length = BasicalProcessor.Decode_Context_Unsigned(ref apdu, 0, ref data.processIdentifier, len);
            len           += section_length;

            /* tag 1 - initiatingObjectIdentifier */
            len           += BasicalProcessor.Decode_Tag_number_and_Value(ref apdu, ref tag_number, ref len_value, len);
            section_length = BasicalProcessor.Decode_Object_Id(ref apdu, ref data.initiatingObjectIdentifier.type, ref data.initiatingObjectIdentifier.instance, len);
            len           += section_length;
            /* tag 2 - eventObjectIdentifier */
            len           += BasicalProcessor.Decode_Tag_number_and_Value(ref apdu, ref tag_number, ref len_value, len);
            section_length = BasicalProcessor.Decode_Object_Id(ref apdu, ref data.eventObjectIdentifier.type, ref data.eventObjectIdentifier.instance, len);
            len           += section_length;
            /* tag 3 - timeStamp */
            section_length = BasicalProcessor.Decode_Context_Timestamp(ref apdu, 3, ref data.timeStamp, len);
            len           += section_length;
            /* tag 4 - noticicationClass */
            section_length = BasicalProcessor.Decode_Context_Unsigned(ref apdu, 4, ref data.notificationClass, len);
            len           += section_length;
            /* tag 5 - priority */
            section_length = BasicalProcessor.Decode_Context_Unsigned(ref apdu, 5, ref value, len);
            data.priority  = (Byte)value;
            len           += section_length;
            /* tag 6 - eventType */
            section_length = BasicalProcessor.Decode_Context_Enumerated(ref apdu, 6, ref value, len);
            data.eventType = (BACNET_EVENT_TYPE)value;
            len           += section_length;
            /* tag 7 - messageText */  //option
            //此处没写

            /* tag 8 - notifyType */
            section_length  = BasicalProcessor.Decode_Context_Enumerated(ref apdu, 8, ref value, len);
            data.notifyType = (BACNET_NOTIFY_TYPE)value;
            len            += section_length;

            switch (data.notifyType)
            {
            case BACNET_NOTIFY_TYPE.NOTIFY_ALARM:
            case BACNET_NOTIFY_TYPE.NOTIFY_EVENT:
                /* tag 9 - ackRequired */
                data.ackRequired = BasicalProcessor.Decode_Context_Boolean(ref apdu, len);
                len++;
                len++;
                /* tag 10 - fromState */
                section_length = BasicalProcessor.Decode_Context_Enumerated(ref apdu, 10, ref value, len);
                data.fromState = (BACNET_EVENT_STATE)value;

                len += section_length;
                break;

            default:
                break;
            }
            /* tag 11 - toState */
            section_length = BasicalProcessor.Decode_Context_Enumerated(ref apdu, 11, ref value, len);
            data.toState   = (BACNET_EVENT_STATE)value;
            len           += section_length;

            /* tag 12 - eventValues */
            if (BasicalProcessor.Decode_Is_Opening_Tag_Number(ref apdu, 12, len))
            {
                len++;
            }
            if (BasicalProcessor.Decode_Is_Opening_Tag_Number(ref apdu, (Byte )data.eventType, len))
            {
                len++;
            }

            if (data.notifyType == BACNET_NOTIFY_TYPE.NOTIFY_ALARM || data.notifyType == BACNET_NOTIFY_TYPE.NOTIFY_EVENT)
            {
                switch (data.eventType)
                {
                case BACNET_EVENT_TYPE.EVENT_CHANGE_OF_STATE:
                    //    section_length = BasicalProcessor.Decode_Context_Poroperty_State(ref apdu, 0, ref data.notificationParams.newState, len);
                    len += section_length;

                    //     section_length = BasicalProcessor.Decode_Context_Bitstring(ref apdu, 1, ref data.notificationParams.statusFlags, len);
                    len += section_length;

                    break;

                default:
                    break;
                }
                if (BasicalProcessor.Decode_Is_Closing_Tag_Number(ref apdu, (Byte)data.eventType, len))
                {
                    len++;
                }
                if (BasicalProcessor.Decode_Is_Closing_Tag_Number(ref apdu, 12, len))
                {
                    len++;
                }
            }


            return(len);
        }
        public int Encode(ref byte[] npdu, ref BACNET_ADDRESS dest, ref BACNET_ADDRESS src, ref BACNET_NPDU_DATA npdu_data, int pos = 0)
        {
            int  len = 0;    /* return value - number of octets loaded in this function */
            byte i   = 0;    /* counter  */

            if (npdu != null)
            {
                npdu[pos + 0] = npdu_data.protocol_version;
                //控制号
                npdu[pos + 1] = 0;
                //控制信息
                if (npdu_data.network_layer_message)
                {
                    npdu[pos + 1] |= BacnetConst.BIT7;
                }
                //是否是网络型报文
                if (dest.net != null)
                {
                    npdu[pos + 1] |= BacnetConst.BIT5;
                }
                //0 表示DNET DLEN DADR HPO COUNT不存在
                // 1 表示都存在,
                if (src.len != 0)
                {
                    npdu[pos + 1] |= BacnetConst.BIT3;
                }
                //0代表SNET SLEN 和SADR都不存在
                // 1表示都存在
                if (npdu_data.data_expecting_reply)
                {
                    npdu[pos + 1] |= BacnetConst.BIT2;
                }
                //是否需要回复
                npdu[pos + 1] |= (byte)((byte)npdu_data.priority & 0x03);
                //优先级
                len = 2;
                if (dest.len != 0)
                {
                    len            += BasicalProcessor.Encode_Unsigned16(ref npdu, dest.net, pos + len);
                    npdu[pos + len] = dest.len;
                    len++;
                    //当DNET=FFFF 表示全局广播
                    //缺省表示本地广播
                    if (dest.len != 0)
                    {
                        for (i = 0; i < dest.len; i++)
                        {
                            npdu[pos + len++] = dest.adr[i];
                        }
                    }
                }
                if (src.len != 0)
                {
                    len            += BasicalProcessor.Encode_Unsigned16(ref npdu, src.net, pos + len);
                    npdu[pos + len] = src.len;


                    len++;

                    if (src.len != 0)
                    {
                        for (i = 0; i < src.len; i++)
                        {
                            npdu[pos + len++] = src.adr[i];
                        }
                    }
                }

                if (dest.net != 0)
                {
                    npdu[pos + len] = (byte)npdu_data.hop_count;
                    len++;
                    //转发计数
                }
                if (npdu_data.network_layer_message)
                {
                    npdu[pos + len] = (byte)npdu_data.network_message_type;
                    len++;
                    if ((int)npdu_data.network_message_type >= 0x80)
                    {
                        len += BasicalProcessor.Encode_Unsigned16(ref npdu, npdu_data.vendor_id, pos + len);
                    }
                    //报文类型
                    //0x80以后为用户扩展类型
                }
            }
            return(len);
        }
        /**********************************************************************************************************************************************************************/
        public int Decode(ref byte[] npdu, ref BACNET_ADDRESS dest, ref BACNET_ADDRESS src, ref BACNET_NPDU_DATA npdu_data)
        {
            int    len         = 0;
            byte   i           = 0;
            UInt16 src_net     = 0;
            UInt16 dest_net    = 0;
            byte   address_len = 0;
            byte   mac_octet   = 0;

            npdu_data.protocol_version      = npdu[0];
            npdu_data.network_layer_message = ((npdu[1] & BacnetConst.BIT7) != 0) ? true : false; //是否是网络层的数据报文或者网络层协议报文
            npdu_data.data_expecting_reply  = ((npdu[1] & BacnetConst.BIT2) != 0) ? true : false;
            npdu_data.priority = (BACNET_MESSAGE_PRIORITY)(npdu[1] & 0x03);
            len = 2;


            if ((npdu[1] & BacnetConst.BIT5) != 0)
            {
                len        += BasicalProcessor.Decode_Unsigned16(ref npdu, ref dest_net, len);
                address_len = npdu[len++];
                dest.net    = dest_net;
                dest.len    = address_len;



                if (address_len > 0)
                {
                    for (i = 0; i < address_len; i++)
                    {
                        mac_octet = npdu[len++];//11

                        dest.adr[i] = mac_octet;
                    }
                }
            }
            else
            {
                dest.net = 0;
                dest.len = 0;
                for (i = 0; i < 7; i++)
                {
                    dest.adr[i] = 0;
                }
            }
            //源mac

            if ((npdu[1] & BacnetConst.BIT3) != 0)
            {
                len += BasicalProcessor.Decode_Unsigned16(ref npdu, ref src_net, len); //13

                address_len = npdu[len++];                                             //14


                src.net = src_net;
                src.len = address_len;



                if (address_len > 0)
                {
                    for (i = 0; i < address_len; i++)
                    {
                        mac_octet = npdu[len++];    //21

                        src.adr[i] = mac_octet;
                    }
                }
            }
            else
            {
                src.net = 0;
                src.len = 0;
                for (i = 0; i < 7; i++)
                {
                    src.adr[i] = 0;
                }
            }

            //hop count

            if (dest_net != 0)
            {
                npdu_data.hop_count = npdu[len++];
            }
            else
            {
                npdu_data.hop_count = 0;
            }

            //对于网络层协议无处理功能
            if (npdu_data.network_layer_message)
            {
                npdu_data.network_message_type =
                    (BACNET_NETWORK_MESSAGE_TYPE)npdu[len++];
            }
            else
            {
                npdu_data.network_message_type = BACNET_NETWORK_MESSAGE_TYPE.NETWORK_MESSAGE_INVALID;
            }

            return(len);
        }