/****************************************************************************** * 函数名称:ConvertBetween_LoWPAN_IPv6() * 功能:实现6LoWPAN协议到IPv6协议的转换 * 参数:data表示6LoWPAN报文; * 返回值:IPv6_packet 返回IPv6报文 * ***************************************************************************/ private PDU_Network ConvertBetween_LoWPAN_IPv6(Object data) { PDU_LoWPAN_IPHC pdu_lowpan = (PDU_LoWPAN_IPHC)data; Byte dispatch = pdu_lowpan.dispatch; //获取6LoWPAN报文分派值 Byte iphc_header = pdu_lowpan.iphc_header; //获取6LoWPAN报文IPHC报头 Byte context_id = pdu_lowpan.context_identifier; //获取6LoWPAN报文上下文标识符 List <Byte[]> unzip_field = pdu_lowpan.IP_uncompressed_field; //获取6LoWPAN报文IP域非压缩字段 Byte nhc_header = pdu_lowpan.nhc_header; //获取6LoWPAN报文NHC报头 List <Byte[]> nh_field = pdu_lowpan.NH_field; //获取6LoWPAN报文下一个首部域字段 List <Byte[]> app_data = pdu_lowpan.application_data; //获取6LoWPAN报文应用层数据 IPv6_packet = new PDU_Network(); //构造IPv6数据报 UDP_packet = new PDU_Transport(); //构造UDP数据报 //================ IPv6 版本号 =====================// IPv6_packet.version = 0x6; //版本号4bit //依据IPHC报头执行转换 Byte[] dispatch_arr = { dispatch }; BitArray arr = new BitArray(dispatch_arr); bool[] TF = new bool[2]; //通信类型&流标签 TF[0] = arr[arr.Count - 4]; TF[1] = arr[arr.Count - 5]; String TF_value = ""; if (!TF[0] && !TF[1]) { TF_value = "00"; //2bitECN+6bitDSCP+4bitPAD+20bit流标签 } else if (!TF[0] && TF[1]) { TF_value = "01"; //2bitECN+2bitPAD+20bit流标签 } else if (TF[0] && !TF[1]) { TF_value = "10"; //2bitECN+6bitDSCP } else { TF_value = "11"; //通信类型&流标签全部压缩 } switch (TF_value) { case "00": break; case "01": break; case "10": break; case "11": //================ IPv6 通信类型&流标签 =====================// IPv6_packet.traffic_class = 0x00; //通信类型8bit IPv6_packet.flow_label = 0x0000; //流标签20bit IPv6_packet.flow_label2 = 0x0; break; } bool NH = arr[arr.Count - 6]; //下一报头 switch (NH) { case false: //不压缩 (包含于非压缩字段) break; case true: //用LoWPAN_NHC压缩 //读取NHC Header字段各比特位值 Byte[] nhc_header_arr = { nhc_header }; BitArray arr2 = new BitArray(nhc_header_arr); if (arr2[arr2.Count - 1] && arr2[arr2.Count - 2] && arr2[arr2.Count - 3] && arr2[arr2.Count - 4] && !arr2[arr2.Count - 5]) //若NHC Header前5位为11110,则下一首部为UDP报头 { //================== IPv6 下一个首部=============================// IPv6_packet.next_header = 0x11; //下一首部11H,表示UDP报文 switch (arr2[arr2.Count - 6]) //第6为C位表示UDP校验和是否压缩 { //============================ UDP 校验和 =====================================// case false: //UDP校验和不压缩 UDP_packet.udp_checksum = BitConverter.ToUInt16(nh_field.ElementAt(1), 0); break; case true: //UDP校验和压缩 UDP_packet.udp_checksum = 0x0000; break; } //第7、8位为P位,表示源端口和目的端口是否压缩 if (!arr2[arr2.Count - 7] && !arr2[arr2.Count - 8]) { } //00,源端口、目的端口都不压缩 else if (!arr2[arr2.Count - 7] && arr2[arr2.Count - 8]) { } //01,源端口不压缩,目的端口前8位为0xF0,被省略 else if (arr2[arr2.Count - 7] && !arr2[arr2.Count - 8]) { } //10,目的端口不压缩,源端口前8位为0xF0,被省略 else //11,源端口、目的端口前12位均为0XF0B,被省略 { BitArray arr1 = new BitArray(nh_field.ElementAt(0)); bool[] zip_ports = new bool[8]; zip_ports[0] = arr1[arr1.Count - 1]; zip_ports[1] = arr1[arr1.Count - 2]; zip_ports[2] = arr1[arr1.Count - 3]; zip_ports[3] = arr1[arr1.Count - 4]; zip_ports[4] = arr1[arr1.Count - 5]; zip_ports[5] = arr1[arr1.Count - 6]; zip_ports[6] = arr1[arr1.Count - 7]; zip_ports[7] = arr1[arr1.Count - 8]; //================================ UDP 端口号 ==============================// //源端口 if (!zip_ports[0] && !zip_ports[1] && !zip_ports[2] && !zip_ports[3]) //0000 { UDP_packet.source_port = 0xF0B0 + 0x0; } else if (!zip_ports[0] && !zip_ports[1] && !zip_ports[2] && zip_ports[3]) //0001 { UDP_packet.source_port = 0xF0B0 + 0x1; } else if (!zip_ports[0] && !zip_ports[1] && zip_ports[2] && !zip_ports[3]) //0010 { UDP_packet.source_port = 0xF0B0 + 0x2; } else if (!zip_ports[0] && !zip_ports[1] && zip_ports[2] && zip_ports[3]) //0011 { UDP_packet.source_port = 0xF0B0 + 0x3; } else if (!zip_ports[0] && zip_ports[1] && !zip_ports[2] && !zip_ports[3]) //0100 { UDP_packet.source_port = 0xF0B0 + 0x4; } else if (!zip_ports[0] && zip_ports[1] && !zip_ports[2] && zip_ports[3]) //0101 { UDP_packet.source_port = 0xF0B0 + 0x5; } else if (!zip_ports[0] && zip_ports[1] && zip_ports[2] && !zip_ports[3]) //0110 { UDP_packet.source_port = 0xF0B0 + 0x6; } else if (!zip_ports[0] && zip_ports[1] && zip_ports[2] && zip_ports[3]) //0111 { UDP_packet.source_port = 0xF0B0 + 0x7; } else if (zip_ports[0] && !zip_ports[1] && !zip_ports[2] && !zip_ports[3]) //1000 { UDP_packet.source_port = 0xF0B0 + 0x8; } else if (zip_ports[0] && !zip_ports[1] && !zip_ports[2] && zip_ports[3]) //1001 { UDP_packet.source_port = 0xF0B0 + 0x9; } else if (zip_ports[0] && !zip_ports[1] && zip_ports[2] && !zip_ports[3]) //1010 { UDP_packet.source_port = 0xF0B0 + 0xA; } else if (zip_ports[0] && !zip_ports[1] && zip_ports[2] && zip_ports[3]) //1011 { UDP_packet.source_port = 0xF0B0 + 0xB; } else if (zip_ports[0] && zip_ports[1] && !zip_ports[2] && !zip_ports[3]) //1100 { UDP_packet.source_port = 0xF0B0 + 0xC; } else if (zip_ports[0] && zip_ports[1] && !zip_ports[2] && zip_ports[3]) //1101 { UDP_packet.source_port = 0xF0B0 + 0xD; } else if (zip_ports[0] && zip_ports[1] && zip_ports[2] && !zip_ports[3]) //1110 { UDP_packet.source_port = 0xF0B0 + 0xE; } else //1111 { UDP_packet.source_port = 0xF0B0 + 0xF; } //目的端口 if (!zip_ports[4] && !zip_ports[5] && !zip_ports[6] && !zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0x0; } else if (!zip_ports[4] && !zip_ports[5] && !zip_ports[6] && zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0x1; } else if (!zip_ports[4] && !zip_ports[5] && zip_ports[6] && !zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0x2; } else if (!zip_ports[4] && !zip_ports[5] && zip_ports[6] && zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0x3; } else if (!zip_ports[4] && zip_ports[5] && !zip_ports[6] && !zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0x4; } else if (!zip_ports[4] && zip_ports[5] && !zip_ports[6] && zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0x5; } else if (!zip_ports[4] && zip_ports[5] && zip_ports[6] && !zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0x6; } else if (!zip_ports[4] && zip_ports[5] && zip_ports[6] && zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0x7; } else if (zip_ports[4] && !zip_ports[5] && !zip_ports[6] && !zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0x8; } else if (zip_ports[4] && !zip_ports[5] && !zip_ports[6] && zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0x9; } else if (zip_ports[4] && !zip_ports[5] && zip_ports[6] && !zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0xA; } else if (zip_ports[4] && !zip_ports[5] && zip_ports[6] && zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0xB; } else if (zip_ports[4] && zip_ports[5] && !zip_ports[6] && !zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0xC; } else if (zip_ports[4] && zip_ports[5] && !zip_ports[6] && zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0xD; } else if (zip_ports[4] && zip_ports[5] && zip_ports[6] && !zip_ports[7]) { UDP_packet.dest_port = 0xF0B0 + 0xE; } else { UDP_packet.dest_port = 0xF0B0 + 0xF; } } // else //11,源端口、目的端口前12位均为0XF0B,被省略 } break; }//switch (NH) bool[] HLIM = new bool[2]; //跳数限制 HLIM[0] = arr[arr.Count - 7]; HLIM[1] = arr[arr.Count - 8]; String HLIM_value = ""; if (!HLIM[0] && !HLIM[1]) { HLIM_value = "00"; //跳数限制不压缩 } else if (!HLIM[0] && HLIM[1]) { HLIM_value = "01"; //跳数限制为1 } else if (HLIM[0] && !HLIM[1]) { HLIM_value = "10"; //跳数限制为64 } else { HLIM_value = "11"; //跳数限制为255 } switch (HLIM_value) { //========================= IPv6 跳数限制======================// case "00": IPv6_packet.hop_limit = unzip_field.ElementAt(0)[0]; //非压缩字段1为未压缩的跳数限制字段,1字节 break; case "01": IPv6_packet.hop_limit = 0x01; break; case "10": IPv6_packet.hop_limit = 0x40; break; case "11": IPv6_packet.hop_limit = 0xFF; break; } Byte[] iphc_header_arr = { iphc_header }; BitArray iphc_header_bitarr = new BitArray(iphc_header_arr); Byte[] context_id_arr = { context_id }; BitArray context_id_bitarr = new BitArray(context_id_arr); bool CID = iphc_header_bitarr[iphc_header_bitarr.Count - 1]; //上下文标识扩展 UInt16[] source_IPv6_addr_prefix = null; //定义源IPv6地址前缀 UInt16[] dest_IPv6_addr_prefix = null; //定义目的IPv6地址前缀 switch (CID) { case false: //使用默认上下文 0x0 source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0x0]; //获取源IPv6地址前缀 dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0x0]; //获取目的IPv6地址前缀 break; case true: //使用指定上下文 bool[] SCI = new bool[4]; //源地址上下文标识符 bool[] DCI = new bool[4]; //目的地址上下文标识符 SCI[0] = context_id_bitarr[context_id_bitarr.Count - 1]; SCI[1] = context_id_bitarr[context_id_bitarr.Count - 2]; SCI[2] = context_id_bitarr[context_id_bitarr.Count - 3]; SCI[3] = context_id_bitarr[context_id_bitarr.Count - 4]; DCI[0] = context_id_bitarr[context_id_bitarr.Count - 5]; DCI[1] = context_id_bitarr[context_id_bitarr.Count - 6]; DCI[2] = context_id_bitarr[context_id_bitarr.Count - 7]; DCI[3] = context_id_bitarr[context_id_bitarr.Count - 8]; if (!SCI[0] && !SCI[1] && !SCI[2] && !SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0x0]; } else if (!SCI[0] && !SCI[1] && !SCI[2] && SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0x1]; } else if (!SCI[0] && !SCI[1] && SCI[2] && !SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0x2]; } else if (!SCI[0] && !SCI[1] && SCI[2] && SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0x3]; } else if (!SCI[0] && SCI[1] && !SCI[2] && !SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0x4]; } else if (!SCI[0] && SCI[1] && !SCI[2] && SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0x5]; } else if (!SCI[0] && SCI[1] && SCI[2] && !SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0x6]; } else if (!SCI[0] && SCI[1] && SCI[2] && SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0x7]; } else if (SCI[0] && !SCI[1] && !SCI[2] && !SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0x8]; } else if (SCI[0] && !SCI[1] && !SCI[2] && SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0x9]; } else if (SCI[0] && !SCI[1] && SCI[2] && !SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0xA]; } else if (SCI[0] && !SCI[1] && SCI[2] && SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0xB]; } else if (SCI[0] && SCI[1] && !SCI[2] && !SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0xC]; } else if (SCI[0] && SCI[1] && !SCI[2] && SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0xD]; } else if (SCI[0] && SCI[1] && SCI[2] && !SCI[3]) { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0xE]; } else { source_IPv6_addr_prefix = (UInt16[])SCI_Mapping[0xF]; } if (!DCI[0] && !DCI[1] && !DCI[2] && !DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0x0]; } else if (!DCI[0] && !DCI[1] && !DCI[2] && DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0x1]; } else if (!DCI[0] && !DCI[1] && DCI[2] && !DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0x2]; } else if (!DCI[0] && !DCI[1] && DCI[2] && DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0x3]; } else if (!DCI[0] && DCI[1] && !DCI[2] && !DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0x4]; } else if (!DCI[0] && DCI[1] && !DCI[2] && DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0x5]; } else if (!DCI[0] && DCI[1] && DCI[2] && !DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0x6]; } else if (!DCI[0] && DCI[1] && DCI[2] && DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0x7]; } else if (DCI[0] && !DCI[1] && !DCI[2] && !DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0x8]; } else if (DCI[0] && !DCI[1] && !DCI[2] && DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0x9]; } else if (DCI[0] && !DCI[1] && DCI[2] && !DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0xA]; } else if (DCI[0] && !DCI[1] && DCI[2] && DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0xB]; } else if (DCI[0] && DCI[1] && !DCI[2] && !DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0xC]; } else if (DCI[0] && DCI[1] && !DCI[2] && DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0xD]; } else if (DCI[0] && DCI[1] && DCI[2] && !DCI[3]) { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0xE]; } else { dest_IPv6_addr_prefix = (UInt16[])DCI_Mapping[0xF]; } break; }// switch (CID) bool SAC = iphc_header_bitarr[iphc_header_bitarr.Count - 2]; //源地址压缩状态 bool[] SAM = new bool[2]; //源地址模式 SAM[0] = iphc_header_bitarr[iphc_header_bitarr.Count - 3]; SAM[1] = iphc_header_bitarr[iphc_header_bitarr.Count - 4]; bool M = iphc_header_bitarr[iphc_header_bitarr.Count - 5]; //组播压缩 bool DAC = iphc_header_bitarr[iphc_header_bitarr.Count - 6]; //目的地址压缩状态 bool[] DAM = new bool[2]; //目的地址模式 DAM[0] = iphc_header_bitarr[iphc_header_bitarr.Count - 7]; DAM[1] = iphc_header_bitarr[iphc_header_bitarr.Count - 8]; UInt16[] source_IPv6_addr_IID = null; //定义源IPv6地址IID UInt16[] dest_IPv6_addr_IID = null; //定义目的IPv6地址IID switch (SAC) { case false: //无状态压缩 break; case true: //有状态压缩 if (!SAM[0] && !SAM[1]) { } //保留 else if (!SAM[0] && SAM[1]) //64bit IID串联发送,前64bit由上下文计算得出 { Byte[] temp1 = { unzip_field.ElementAt(1)[0], unzip_field.ElementAt(1)[1] }; Byte[] temp2 = { unzip_field.ElementAt(1)[2], unzip_field.ElementAt(1)[3] }; Byte[] temp3 = { unzip_field.ElementAt(1)[4], unzip_field.ElementAt(1)[5] }; Byte[] temp4 = { unzip_field.ElementAt(1)[6], unzip_field.ElementAt(1)[7] }; source_IPv6_addr_IID = new UInt16[4]; source_IPv6_addr_IID[0] = BitConverter.ToUInt16(temp1, 0); //两个Byte类型转UInt16 source_IPv6_addr_IID[1] = BitConverter.ToUInt16(temp2, 0); source_IPv6_addr_IID[2] = BitConverter.ToUInt16(temp3, 0); source_IPv6_addr_IID[3] = BitConverter.ToUInt16(temp4, 0); } else if (SAM[0] && !SAM[1]) { } //16bit 短地址串联发送,前112bit由上下文计算得出 else { } //0bit,全部由上下文计算得出 break; } switch (DAC) { case false: //无状态压缩 break; case true: //有状态压缩 if (!DAM[0] && !DAM[1]) { } //保留 else if (!DAM[0] && DAM[1]) //64bit IID串联发送,前64bit由上下文计算得出 { Byte[] temp1 = { unzip_field.ElementAt(2)[0], unzip_field.ElementAt(2)[1] }; Byte[] temp2 = { unzip_field.ElementAt(2)[2], unzip_field.ElementAt(2)[3] }; Byte[] temp3 = { unzip_field.ElementAt(2)[4], unzip_field.ElementAt(2)[5] }; Byte[] temp4 = { unzip_field.ElementAt(2)[6], unzip_field.ElementAt(2)[7] }; dest_IPv6_addr_IID = new UInt16[4]; dest_IPv6_addr_IID[0] = BitConverter.ToUInt16(temp1, 0); //两个Byte类型转UInt16 dest_IPv6_addr_IID[1] = BitConverter.ToUInt16(temp2, 0); dest_IPv6_addr_IID[2] = BitConverter.ToUInt16(temp3, 0); dest_IPv6_addr_IID[3] = BitConverter.ToUInt16(temp4, 0); } else if (DAM[0] && !DAM[1]) { } //16bit 短地址串联发送,前112bit由上下文计算得出 else { } //0bit,全部由上下文计算得出 break; } //================================= IPv6 源地址、目的地址======================================// //计算源IPv6地址 IPv6_packet.source_ipv6_address = new UInt16[source_IPv6_addr_prefix.Length + source_IPv6_addr_IID.Length]; source_IPv6_addr_prefix.CopyTo(IPv6_packet.source_ipv6_address, 0); source_IPv6_addr_IID.CopyTo(IPv6_packet.source_ipv6_address, source_IPv6_addr_prefix.Length); //计算目的IPv6地址 IPv6_packet.dest_ipv6_address = new UInt16[dest_IPv6_addr_prefix.Length + dest_IPv6_addr_IID.Length]; dest_IPv6_addr_prefix.CopyTo(IPv6_packet.dest_ipv6_address, 0); dest_IPv6_addr_IID.CopyTo(IPv6_packet.dest_ipv6_address, dest_IPv6_addr_prefix.Length); 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); } }
/****************************************************************************** * 函数名称:LoWPAN_Packet_Buffering() * 功能:执行6LoWPAN报文缓冲 * 参数:data表示所收到的6LoWPAN报文 * 返回值:无 * ***************************************************************************/ private void LoWPAN_Packet_Buffering(Object data) { PDU_LoWPAN_IPHC pdu_lowpan_iphc = (PDU_LoWPAN_IPHC)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_lowpan_iphc.application_data) { foreach (Byte b in B) { app_data_byte_num++; } } //Console.WriteLine("应用层数据字节数=" + app_data_byte_num); //当应用层总数据量+当前数据报应用层数据量<81字节时 if (total_app_data_byte_num + app_data_byte_num < 81) { total_app_data_byte_num += app_data_byte_num; //Console.WriteLine("应用层数据总量=" + total_app_data_byte_num); //当前报文应用层数据进入6LoWPAN数据缓冲队列 this.Buffer_queue.Enqueue(pdu_lowpan_iphc.application_data); } else //当应用层数据总量达到81字节时 { 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); } } //将应用层数据重新封装成6LoWPAN数据包 PDU_LoWPAN_IPHC pdu_lowpan_iphc2 = new PDU_LoWPAN_IPHC(pdu_lowpan_iphc.dispatch, pdu_lowpan_iphc.iphc_header, pdu_lowpan_iphc.context_identifier, pdu_lowpan_iphc.IP_uncompressed_field, pdu_lowpan_iphc.nhc_header, pdu_lowpan_iphc.NH_field, app_data); //重新封装的数据包进入缓冲区组件发送队列 this.Component_send_queue.Enqueue(pdu_lowpan_iphc2); //当前报文应用层数据进入缓冲区队列 this.Buffer_queue.Enqueue(pdu_lowpan_iphc.application_data); app_data_byte_num = 0; //统计当前数据报应用层数据量 foreach (Byte[] B in pdu_lowpan_iphc.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; } }