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); }
public void Decode(ref BACNET_ADDRESS src, ref byte[] apdu, UInt16 apdu_len, int pos = 0) { BACNET_CONFIRMED_SERVICE_DATA service_data = new BACNET_CONFIRMED_SERVICE_DATA(); BACNET_CONFIRMED_SERVICE_ACK_DATA service_ack_data = new BACNET_CONFIRMED_SERVICE_ACK_DATA(); Byte invoke_id = 0; Byte service_choice = 0; Byte[] service_request; UInt16 service_request_len = 0; int len = 0; /* counts where we are in PDU */ Byte tag_number = 0; UInt32 len_value = 0; UInt32 error_code = 0; UInt32 error_class = 0; Byte reason = 0; Boolean server = false; //服务选择 switch (apdu[pos] & 0xF0) { case (Byte)BACNET_PDU_TYPE.PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST: { service_choice = apdu[pos + 1]; service_request_len = (ushort)(apdu_len - 2); service_request = new Byte[service_request_len]; Array.Copy(apdu, pos + 2, service_request, 0, service_request_len); switch (service_choice) { case (Byte)BACNET_UNCONFIRMED_SERVICE.SERVICE_UNCONFIRMED_COV_NOTIFICATION: { // UnconFirmCovService un_cov = new UnconFirmCovService(); // un_cov.Unconfirm_Cov_Handler(ref service_request,(ushort) service_request_len,ref src); break; } case (Byte)BACNET_UNCONFIRMED_SERVICE.SERVICE_UNCONFIRMED_EVENT_NOTIFICATION: { UnconfirmedEventNotificationService un_event_not = new UnconfirmedEventNotificationService(); un_event_not.Uevent_Notify_Handler(ref service_request, (ushort)service_request_len, ref src); break; } } break; } case (Byte)BACNET_PDU_TYPE.PDU_TYPE_CONFIRMED_SERVICE_REQUEST: { len = (int)Decode_confirmed_Service_Request(ref apdu, apdu_len, ref service_data, ref service_choice, ref service_request_len, pos); service_request = new Byte[service_request_len]; Array.Copy(apdu, pos + len, service_request, 0, service_request_len); switch (service_choice) { case (Byte)BACNET_CONFIRMED_SERVICE.SERVICE_CONFIRMED_READ_PROPERTY: { BACnet_Read_Property_Data data = new BACnet_Read_Property_Data(); ReadPropertyService rp_service = new ReadPropertyService(); rp_service.Read_Property_Handler(ref service_request, service_request_len, ref src, ref service_data); break; } case (Byte)BACNET_CONFIRMED_SERVICE.SERVICE_CONFIRMED_SUBSCRIBE_COV: { CovSubscribeService cov_service = new CovSubscribeService(); cov_service.Cov_Subscribe_Handler(ref service_request, service_request_len, ref src, ref service_data); break; } case (Byte)BACNET_CONFIRMED_SERVICE.SERVICE_CONFIRMED_ACKNOWLEDGE_ALARM: { AcknowledgeAlarmService ack_al = new AcknowledgeAlarmService(); ack_al.Acknowledge_Alarm_Handler(ref service_request, service_request_len, ref src, ref service_data); break; } } break; } case (Byte)BACNET_PDU_TYPE.PDU_TYPE_SIMPLE_ACK: { invoke_id = apdu[pos + 1]; service_choice = apdu[pos + 2]; TsmProcessor.free_invoke_id(invoke_id); break; } case (Byte)BACNET_PDU_TYPE.PDU_TYPE_COMPLEX_ACK: { if ((apdu[pos + 0] & BacnetConst.BIT3) != 0) { service_ack_data.segmented_message = true; } if ((apdu[pos + 0] & BacnetConst.BIT2) != 0) { service_ack_data.more_follows = true; } invoke_id = service_ack_data.invoke_id = apdu[pos + 1]; len = 2; if (service_ack_data.segmented_message) { service_ack_data.sequence_number = apdu[pos + len++]; service_ack_data.proposed_window_number = apdu[pos + len++]; } service_choice = apdu[pos + len++]; service_request_len = (UInt16)(apdu_len - len); service_request = new Byte[service_request_len]; Array.Copy(apdu, pos + len, service_request, 0, service_request_len); switch (service_choice) { case (Byte)BACNET_CONFIRMED_SERVICE.SERVICE_CONFIRMED_READ_PROPERTY: ReadPropertyService rp_ack = new ReadPropertyService(); rp_ack.Read_Property_Ack_Handler(ref service_request, service_request_len, ref src, ref service_ack_data); break; } TsmProcessor.free_invoke_id(invoke_id); break; } case (Byte)BACNET_PDU_TYPE.PDU_TYPE_ERROR: { break; } case (Byte)BACNET_PDU_TYPE.PDU_TYPE_REJECT: { break; } case (Byte)BACNET_PDU_TYPE.PDU_TYPE_ABORT: { break; } } }
public void Acknowledge_Alarm_Handler(ref Byte[] request, UInt16 request_len, ref BACNET_ADDRESS src, ref BACNET_CONFIRMED_SERVICE_DATA request_data) { ACKNOWLEDGE_ALARM_DATA Ack_data = new ACKNOWLEDGE_ALARM_DATA(); Byte[] buffer = new Byte[1024]; BACNET_ADDRESS my_address = new BACNET_ADDRESS(); NpduProcessor n_pro = new NpduProcessor(); BACNET_NPDU_DATA npdu_data = new BACNET_NPDU_DATA(); ApduProcessor a_pro = new ApduProcessor(); BvlcProcessor b_pro = new BvlcProcessor(); int pdu_len = 0; int byte_len = 0; int apdu_len = -1; int npdu_len = -1; int len = 0; len = Decode_Service_Request(ref request, request_len, ref Ack_data); n_pro.Encode_NpduData(ref npdu_data, false, BACNET_MESSAGE_PRIORITY.MESSAGE_PRIORITY_NORMAL); npdu_len = n_pro.Encode(ref buffer, ref src, ref my_address, ref npdu_data); apdu_len = a_pro.Encode_Simple_Ack(ref buffer, request_data.invoke_id, (Byte)BACNET_CONFIRMED_SERVICE.SERVICE_CONFIRMED_SUBSCRIBE_COV, npdu_len); pdu_len = npdu_len + apdu_len; byte_len = b_pro.Encode(ref buffer, ref src, ref npdu_data, pdu_len); }
/********************************************************************************************************************************************************************************/ public void Cov_Subscribe_Handler(ref Byte[] request, UInt16 request_len, ref BACNET_ADDRESS src, ref BACNET_CONFIRMED_SERVICE_DATA request_data) { int pdu_len = 0; int byte_len = 0; Byte[] buffer = new Byte[1024]; BACNET_SUBSCRIBE_COV_DATA cov_data = new BACNET_SUBSCRIBE_COV_DATA(); BACNET_ADDRESS my_address = new BACNET_ADDRESS(); NpduProcessor n_pro = new NpduProcessor(); BACNET_NPDU_DATA npdu_data = new BACNET_NPDU_DATA(); ApduProcessor a_pro = new ApduProcessor(); BvlcProcessor b_pro = new BvlcProcessor(); int apdu_len = -1; int npdu_len = -1; int len = 0; //cov_data.error_code = ABORT_REASON_SEGMENTATION_NOT_SUPPORTED; //datalink_get_my_address(&my_address); n_pro.Encode_NpduData(ref npdu_data, false, BACNET_MESSAGE_PRIORITY.MESSAGE_PRIORITY_NORMAL); npdu_len = n_pro.Encode(ref buffer, ref src, ref my_address, ref npdu_data); // if (service_data.segmented_message) /* we don't support segmentation - send an abort */ len = Decode_Service_Request(ref request, request_len, ref cov_data); // for (int i = 0; i < Form_main.lift_count; i++) { if (cov_data.monitoredObjectIdentifier.instance == Form_main.lift_list[i].Object_Identifier.instance) { Form_main.lift_list[i].cov_subscribe(src, cov_data); } } // apdu_len = a_pro.Encode_Simple_Ack(ref buffer, request_data.invoke_id, (Byte)BACNET_CONFIRMED_SERVICE.SERVICE_CONFIRMED_SUBSCRIBE_COV, npdu_len); pdu_len = npdu_len + apdu_len; byte_len = b_pro.Encode(ref buffer, ref src, ref npdu_data, pdu_len); }
public void Read_Property_Handler(ref Byte[] request, UInt16 request_len, ref BACNET_ADDRESS src, ref BACNET_CONFIRMED_SERVICE_DATA request_data) { Byte[] buffer = new Byte[1024]; BACnet_Read_Property_Data rpdata = new BACnet_Read_Property_Data(); int len = 0; int pdu_len = 0; int apdu_len = -1; int npdu_len = -1; BACNET_NPDU_DATA npdu_data = new BACNET_NPDU_DATA(); bool error = true; /* assume that there is an error */ int bytes_sent = 0; BACNET_ADDRESS my_address = new BACNET_ADDRESS(); BacnetAddresssProcessor.Get_My_Address(ref my_address); NpduProcessor n_pro = new NpduProcessor(); BvlcProcessor b_pro = new BvlcProcessor(); n_pro.Encode_NpduData(ref npdu_data, false, BACNET_MESSAGE_PRIORITY.MESSAGE_PRIORITY_NORMAL); npdu_len = n_pro.Encode(ref buffer, ref src, ref my_address, ref npdu_data); // if (service_data.segmented_message) /* we don't support segmentation - send an abort */ len = Decode_Service_Request(ref request, request_len, ref rpdata); len = Read_Property_ACK_Pack_Int(ref buffer, request_data.invoke_id, ref rpdata, npdu_len); pdu_len = npdu_len + len; bytes_sent = b_pro.Encode(ref buffer, ref src, ref npdu_data, pdu_len); IPEndPoint Ipep = new IPEndPoint( IPAddress.Parse("10.10.161.153"), 60); UdpSender udpsendr = new UdpSender(ref buffer, Ipep); udpsendr.Send(bytes_sent); }