public Byte Cov_Subscribe_pack(ref Byte[] Handler_Transmit_Buffer, UInt32 device_id, ref BACNET_SUBSCRIBE_COV_DATA cov_data) { Byte invoke_id = 0; BACNET_ADDRESS dest = new BACNET_ADDRESS(); BACNET_ADDRESS my_address = new BACNET_ADDRESS(); BacnetAddresssProcessor.Get_My_Address(ref my_address); BacnetAddresssProcessor.Get_Device_Address(ref dest, device_id); // uint max_apdu = 0; // bool status = false; int len = 0; int pdu_len = 0; int byte_len = 0; BACNET_NPDU_DATA npdu_data = new BACNET_NPDU_DATA(); NpduProcessor n_pro = new NpduProcessor(); BvlcProcessor b_pro = new BvlcProcessor(); //status = address_get_by_device(device_id, &max_apdu, &dest); //invoke_id = tsm_next_free_invokeID(); invoke_id = 1; n_pro.Encode_NpduData(ref npdu_data, true, BACNET_MESSAGE_PRIORITY.MESSAGE_PRIORITY_NORMAL); pdu_len = n_pro.Encode(ref Handler_Transmit_Buffer, ref dest, ref my_address, ref npdu_data); len = Cov_Subscribe_Encode(ref Handler_Transmit_Buffer, invoke_id, ref cov_data, pdu_len); pdu_len += len; // if ((unsigned) pdu_len < max_apdu) { // tsm_set_confirmed_unsegmented_transaction(invoke_id, &dest, // &npdu_data, &Handler_Transmit_Buffer[0], (uint16_t) pdu_len); byte_len = b_pro.Encode(ref Handler_Transmit_Buffer, ref dest, ref npdu_data, pdu_len); return(invoke_id); }
public int Unconfirmed_Event_Notification_Pack(ref Byte[] buffer, ref BACNET_EVENT_NOTIFICATION_DATA data) { int len = 0; int pdu_len = 0; int bytes_sent = 0; BACNET_NPDU_DATA npdu_data = new BACNET_NPDU_DATA(); BACNET_ADDRESS dest = new BACNET_ADDRESS(); BACNET_ADDRESS my_address = new BACNET_ADDRESS(); BacnetAddresssProcessor.Get_My_Address(ref my_address); BacnetAddresssProcessor.Get_My_Address(ref dest); NpduProcessor n_pro = new NpduProcessor(); BvlcProcessor b_pro = new BvlcProcessor(); n_pro.Encode_NpduData(ref npdu_data, true, BACNET_MESSAGE_PRIORITY.MESSAGE_PRIORITY_NORMAL); pdu_len = n_pro.Encode(ref buffer, ref dest, ref my_address, ref npdu_data); len = Uevent_Notify_Encode(ref buffer, ref data, pdu_len); pdu_len += len; bytes_sent = b_pro.Encode(ref buffer, ref dest, ref npdu_data, pdu_len); return(bytes_sent); }
public void handler(ref BACNET_ADDRESS src, ref byte[] pdu, UInt16 pdu_len) { int apdu_offset = 0;//网络层数据的长度 BACNET_ADDRESS dest = new BACNET_ADDRESS(); BACNET_NPDU_DATA npdu_data = new BACNET_NPDU_DATA(); BvlcProcessor b_pro = new BvlcProcessor(); NpduProcessor p_pro = new NpduProcessor(); ApduProcessor a_pro = new ApduProcessor(); b_pro.Decode(ref src, ref pdu); if (pdu[0] == 1) //1是版本号 { apdu_offset = p_pro.Decode(ref pdu, ref dest, ref src, ref npdu_data); //获得npdu的数据 //获得npdu的数据 } if (npdu_data.network_layer_message) { //尚未定义 } else { if ((dest.net == 0)) { a_pro.Decode(ref src, ref pdu, (UInt16)(pdu_len - apdu_offset), apdu_offset); } } }
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 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); }
public int Pack_Read_Property_Request(ref byte[] Handler_Transmit_Buffer, UInt32 device_id) { BACNET_ADDRESS dest = new BACNET_ADDRESS(); BACNET_ADDRESS my_address = new BACNET_ADDRESS(); BacnetAddresssProcessor.Get_My_Address(ref my_address); // BacnetAddresss.Get_Device_Address(ref dest, device_id); BacnetAddresssProcessor.Get_My_Address(ref dest); int byte_len = 0; Byte invoke_id; int len = 0; int pdu_len = 0; NpduProcessor n_pro = new NpduProcessor(); BACNET_NPDU_DATA npdu_data = new BACNET_NPDU_DATA(); BvlcProcessor b_pro = new BvlcProcessor(); invoke_id = TsmProcessor.next_free_id(); n_pro.Encode_NpduData(ref npdu_data, true, BACNET_MESSAGE_PRIORITY.MESSAGE_PRIORITY_NORMAL); pdu_len = n_pro.Encode(ref Handler_Transmit_Buffer, ref dest, ref my_address, ref npdu_data); len = Read_Property_Encode(ref Handler_Transmit_Buffer, invoke_id, rp_data, pdu_len); pdu_len += len; byte_len = b_pro.Encode(ref Handler_Transmit_Buffer, ref dest, ref npdu_data, pdu_len); return(invoke_id);; }
public void Encode_NpduData(ref BACNET_NPDU_DATA npdu_data, bool data_expecting_reply, BACNET_MESSAGE_PRIORITY priority) { npdu_data.data_expecting_reply = data_expecting_reply; npdu_data.protocol_version = 1; npdu_data.network_layer_message = false; npdu_data.network_message_type = BACNET_NETWORK_MESSAGE_TYPE.NETWORK_MESSAGE_INVALID; /* optional */ npdu_data.vendor_id = 0; /* optional, if net message type is > 0x80 */ npdu_data.priority = priority; npdu_data.hop_count = 255; //默认 }
/********************************************************************************************************************************************************************************/ 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 int Unconfirm_Cov_Service_Pack(ref byte[] Handler_Transmit_Buffer, ref BACNET_COV_DATA cov_data) { //无证实服务 广播 int len = 0; int pdu_len = 0; int bytes_len = 0; BACNET_ADDRESS dest = new BACNET_ADDRESS(); BACNET_NPDU_DATA npdu_data = new BACNET_NPDU_DATA(); BACNET_ADDRESS my_address = new BACNET_ADDRESS(); BacnetAddresssProcessor.Get_My_Address(ref my_address); BacnetAddresssProcessor.Get_Broadcast_Address(ref dest, true, 60); NpduProcessor n_pro = new NpduProcessor(); BvlcProcessor b_pro = new BvlcProcessor(); // datalink_get_my_address(&my_address); // datalink_get_broadcast_address(dest); n_pro.Encode_NpduData(ref npdu_data, true, BACNET_MESSAGE_PRIORITY.MESSAGE_PRIORITY_NORMAL); pdu_len = n_pro.Encode(ref Handler_Transmit_Buffer, ref dest, ref my_address, ref npdu_data); len = Unconfirm_Cov(ref Handler_Transmit_Buffer, cov_data, pdu_len); pdu_len += len; bytes_len = b_pro.Encode(ref Handler_Transmit_Buffer, ref dest, ref npdu_data, pdu_len); return(bytes_len); }
public int AcknowledgeAlarmService_Pack(ref byte[] Handler_Transmit_Buffer, BACNET_ADDRESS dest, ref ACKNOWLEDGE_ALARM_DATA Ack_data) { BACNET_ADDRESS my_address = new BACNET_ADDRESS(); BacnetAddresssProcessor.Get_My_Address(ref my_address); int byte_len = 0; Byte invoke_id; int len = 0; int pdu_len = 0; NpduProcessor n_pro = new NpduProcessor(); BACNET_NPDU_DATA npdu_data = new BACNET_NPDU_DATA(); BvlcProcessor b_pro = new BvlcProcessor(); //invoke_id=get_invoke_id() 获取invoke_id未写 假设为1 invoke_id = 1; n_pro.Encode_NpduData(ref npdu_data, true, BACNET_MESSAGE_PRIORITY.MESSAGE_PRIORITY_NORMAL); pdu_len = n_pro.Encode(ref Handler_Transmit_Buffer, ref dest, ref my_address, ref npdu_data); len = Acknowledge_Alarm_Encode(ref Handler_Transmit_Buffer, invoke_id, ref Ack_data, pdu_len); pdu_len += len; byte_len = b_pro.Encode(ref Handler_Transmit_Buffer, ref dest, ref npdu_data, pdu_len); return(invoke_id);; }
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); }
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); }