/****************************************************************************** * 函数名称:ConvertBetween_ConnID_IPv6() * 功能:实现ConnID协议到IPv6协议的转换 * 参数:data表示ConnID报文; * 返回值:IPv6_packet 返回IPv6报文 * ***************************************************************************/ private PDU_Network ConvertBetween_ConnID_IPv6(Object data) { PDU_ConnID pdu_connid = (PDU_ConnID)data; IPv6_packet = new PDU_Network(); //构造IPv6数据报 UDP_packet = new PDU_Transport(); //构造UDP数据报 UInt64 connid = pdu_connid.connid; //获取ConnID报文连接标识ConnID List <Byte[]> app_data = pdu_connid.application_data; //获取ConnID报文应用层数据 Communicarion_Patameter commPata = (Communicarion_Patameter)CommPara_ConnID_Mapping[connid]; IPv6_packet.version = 0x6; //版本号4bit IPv6_packet.traffic_class = 0x00; //通信类型8bit IPv6_packet.flow_label = 0x0000; //流标签20bit IPv6_packet.flow_label2 = 0x0; IPv6_packet.next_header = commPata.next_header; //下一首部8bit IPv6_packet.hop_limit = commPata.hop_limit; //跳数限制8bit IPv6_packet.source_ipv6_address = commPata.source_ipv6_addr; //源IPv6地址 IPv6_packet.dest_ipv6_address = commPata.dest_ipv6_addr; //目的IPv6地址 UDP_packet.source_port = commPata.source_port; //源端口号 UDP_packet.dest_port = commPata.dest_port; //目的端口号 UDP_packet.udp_checksum = 0x0000; UInt16 app_data_len = 0x0000; //应用层数据字节数 foreach (Byte[] B in app_data) { foreach (Byte b in B) { app_data_len++; } } //================== UDP长度 =====================// UDP_packet.udp_length = (UInt16)(0x0008 + app_data_len); //UDP长度为UDP报头+UDP数据长度 //================ IPv6有效载荷长度 ===============// IPv6_packet.payload_length = UDP_packet.udp_length; //================== UDP数据 ======================// UDP_packet.application_data = app_data; //================ IPv6上层PDU ====================// IPv6_packet.pdu_transport = UDP_packet; return(IPv6_packet); }
/****************************************************************************** * 函数名称:MessageEncapsulation() * 功能:报文封装 * 参数:x 表示模拟传输协议 * x="6LoWPAN" 表示构建6LoWPAN协议数据单元 ,x="ConnID"表示构建基于ConnID * 方式协议数据单元 * 返回值:无 * ***************************************************************************/ public void MessageEncapsulation(string x) { try { Object temp = null; if (this.Component_reveice_queue.Count > 0) { //读取微处理器组件接收队列数据 temp = this.Component_reveice_queue.Dequeue(); //将该数据作为应用层数据,同时转换为字节表示形式 List <Byte[]> app_data = new List <Byte[]>(); //应用层数据列表 //if (temp.GetType().Name == "Int32[]") switch (temp.GetType().Name) { case "BloodPressureDataType": BloodPressureDataType bpData = (BloodPressureDataType)temp; Byte[] bytes0 = BitConverter.GetBytes(bpData.HighBP); Byte[] bytes1 = BitConverter.GetBytes(bpData.LowBP); app_data.Add(bytes0); app_data.Add(bytes1); break; case "TemperatureDataType": TemperatureDataType tempData = (TemperatureDataType)temp; //Byte[] bytes2 = BitConverter.GetBytes(tempData.TemperatureInteger); //Byte[] bytes3 = BitConverter.GetBytes(tempData.TemperatureDecimal); app_data.Add(new Byte[] { tempData.TemperatureInteger }); app_data.Add(new Byte[] { tempData.TemperatureDecimal }); break; case "HeartRateDataType": HeartRateDataType hrData = (HeartRateDataType)temp; Byte[] bytes4 = BitConverter.GetBytes(hrData.HeartRate); app_data.Add(bytes4); break; } switch (x) { case "6LoWPAN": //6LoWPAN协议数据单元 //构建6LoWPAN PDU PDU_LoWPAN_IPHC pdu_lowpan_iphc = new PDU_LoWPAN_IPHC(); pdu_lowpan_iphc.dispatch = 0x7C; //0111 1100 pdu_lowpan_iphc.iphc_header = 0xD5; //1101 0101 pdu_lowpan_iphc.context_identifier = 0x12; //0001 0010 pdu_lowpan_iphc.IP_uncompressed_field = new List <Byte[]>(); //非压缩字段1,跳数限制255 Byte[] hop_limit = new Byte[] { 0xFF }; pdu_lowpan_iphc.IP_uncompressed_field.Add(hop_limit); //非压缩字段2,源地址1201_0585_FEAB_5001H Byte[] source_addr = new Byte[] { 0x12, 0x01, 0x05, 0x85, 0xFE, 0xAB, 0x50, 0x01 }; pdu_lowpan_iphc.IP_uncompressed_field.Add(source_addr); //非压缩字段3,目的地址0000_0000_0000_1234H Byte[] dest_addr = new Byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34 }; pdu_lowpan_iphc.IP_uncompressed_field.Add(dest_addr); //根据IPHC报头NH=1,可判断下一报头采用LoWPAN_NHC压缩 pdu_lowpan_iphc.nhc_header = 0xF3; //1111 0011 pdu_lowpan_iphc.NH_field = new List <Byte[]>(); //根据nhc_header最后两位,确定端口号采用最短压缩形式,即源端口号和目的端口号所占存储均为4bit,默认前12bit为0xF0B Byte[] port = new Byte[] { 0xF0 }; //即源端口号为0xF0BF,目的端口号为0xF0B0; pdu_lowpan_iphc.NH_field.Add(port); //UDP校验和0 Byte[] checksum = new Byte[] { 0x00, 0x0F }; pdu_lowpan_iphc.NH_field.Add(checksum); //添加应用层数据 //foreach (Byte[] data in app_data) //{ // pdu_lowpan_iphc.application_data.Add(data); //} pdu_lowpan_iphc.application_data = app_data; //6LoWPAN PDU进入组件发送队列 this.Component_send_queue.Enqueue(pdu_lowpan_iphc); //foreach (PDU_LoWPAN_IPHC p in Component_send_queue) //{ // Console.WriteLine(p + "////"); //} break; case "ConnID": //ConnID协议数据单元 //构建ConnID PDU PDU_ConnID pdu_connid = new PDU_ConnID(); pdu_connid.message_identifier = 0xFF; //FF表示Data消息类型 //Byte[] connid = new Byte[] { 0xF4, 0x31, 0xCA, 0x18, 0x5F, 0x74, 0xC6, 0xE6 }; //f431ca185f74c6e6 //pdu_connid.connid = new List<byte[]>(); //pdu_connid.connid.Add(connid); pdu_connid.connid = 0xF431CA185F74C6E6; //添加应用层数据 //foreach (Byte[] data in app_data) //{ // pdu_connid.application_data.Add(data); //} pdu_connid.application_data = app_data; //ConnID PDU进入组件发送队列 this.Component_send_queue.Enqueue(pdu_connid); break; case "ConnID_2": //ConnID协议数据单元 //构建ConnID PDU PDU_ConnID pdu_connid_2 = new PDU_ConnID(); pdu_connid_2.message_identifier = 0xFF; //FF表示Data消息类型 //Byte[] connid_2 = new Byte[] { 0x73, 0xB6, 0xD1, 0x72, 0x50, 0x35, 0x87, 0x8d }; //73b6d1725035878d //pdu_connid_2.connid = new List<byte[]>(); //pdu_connid_2.connid.Add(connid_2); pdu_connid_2.connid = 0x73B6D1725035878D; //添加应用层数据 //foreach (Byte[] data in app_data) //{ // pdu_connid.application_data.Add(data); //} pdu_connid_2.application_data = app_data; //ConnID PDU进入组件发送队列 this.Component_send_queue.Enqueue(pdu_connid_2); break; } } } catch (Exception e) { Console.WriteLine("MP错误情况:" + e.Message + e.StackTrace); } }
/****************************************************************************** * 函数名称:ConnID_802151_Packet_Buffering() * 功能:执行ConnID报文缓冲,帧格式为802.15.1 * 参数:data表示所收到的ConnID报文 * 返回值:无 * ***************************************************************************/ private void ConnID_802151_Packet_Buffering(Object data) { PDU_ConnID pdu_connid = (PDU_ConnID)data; //foreach (Byte[] B in pdu_lowpan_iphc.uncompressed_field) //{ // foreach (Byte b in B) // { // uncompressed_field_byte_num++; // } //} //Console.WriteLine("非压缩字段字节数=" + uncompressed_field_byte_num); int app_data_byte_num = 0; //应用层数据字节数 //记录当前数据报应用层数据量 foreach (Byte[] B in pdu_connid.application_data) { foreach (Byte b in B) { app_data_byte_num++; } } //Console.WriteLine("应用层数据字节数=" + app_data_byte_num); //当应用层总数据量+当前数据报应用层数据量 <= 22 字节时 if (total_app_data_byte_num + app_data_byte_num <= 22) { total_app_data_byte_num += app_data_byte_num; //Console.WriteLine("应用层数据总量=" + total_app_data_byte_num); //当前报文应用层数据进入N数据缓冲队列 this.Buffer_queue.Enqueue(pdu_connid.application_data); } else //当应用层数据总量达到22字节时 { List <Byte[]> app_data = new List <byte[]>(); while (this.Buffer_queue.Count > 0) { //缓冲队列内数据全部出队 foreach (Byte[] B in (List <Byte[]>) this.Buffer_queue.Dequeue()) { app_data.Add(B); } } //将应用层数据重新封装成ConnID数据包 PDU_ConnID pdu_connid2 = new PDU_ConnID(pdu_connid.message_identifier, pdu_connid.connid, app_data); //重新封装的数据包进入缓冲区组件发送队列 this.Component_send_queue.Enqueue(pdu_connid2); //当前报文应用层数据进入缓冲区队列 this.Buffer_queue.Enqueue(pdu_connid.application_data); app_data_byte_num = 0; //统计当前数据报应用层数据量 foreach (Byte[] B in pdu_connid.application_data) { foreach (Byte b in B) { app_data_byte_num++; } } total_app_data_byte_num = app_data_byte_num; //重新设定应用层数据总量 //pdu_lowpan_iphc.application_data = app_data; } }