// Sends the given WP_Packet (header) and public static void SendPacket(UsbStream stream, WP_Packet packet, byte[] data) { int index; // Create a buffer whose size is large enough to contain both the WP_Packet (header) // and data so that they may be sent as a single packet. byte[] buffer = new byte[WP_Packet.packetSize + data.Length]; // Marshal the signature string into the buffer for (index = 0; index < packet.m_signature.Length && index < WP_Packet.m_crcHeader_offset; index++) { buffer[index] = (byte)packet.m_signature[index]; } while (index++ < WP_Packet.m_crcHeader_offset) { buffer[index] = 0; // Fill to end with zeros } // Marshal the WP_Packet (header) into the buffer. Note that the CRC values start as zero // so that the CRC of the WP_Packet (header) may be computed the same for host and target. WriteUINT32(buffer, WP_Packet.m_crcHeader_offset, 0); WriteUINT32(buffer, WP_Packet.m_crcData_offset, 0); WriteUINT32(buffer, WP_Packet.m_cmd_offset, (UInt32)packet.m_cmd); WriteUINT16(buffer, WP_Packet.m_seq_offset, packet.m_seq); WriteUINT16(buffer, WP_Packet.m_seqReply_offset, packet.m_seqReply); WriteUINT32(buffer, WP_Packet.m_flags_offset, packet.m_flags); WriteUINT32(buffer, WP_Packet.m_size_offset, (UInt32)data.Length); // Copy the data to the buffer for (int i = 0; i < data.Length; i++) { buffer[WP_Packet.packetSize + i] = data[i]; } // Calculate the CRC of the data if (data.Length != 0) { packet.m_crcData = SUPPORT_ComputeCRC(data, 0, data.Length, 0); WriteUINT32(buffer, WP_Packet.m_crcData_offset, packet.m_crcData); } // Calculate the CRC of the packet header packet.m_crcHeader = SUPPORT_ComputeCRC(buffer, 0, WP_Packet.packetSize, 0); WriteUINT32(buffer, WP_Packet.m_crcHeader_offset, packet.m_crcHeader); // Write out the complete packet to the USB stream stream.Write(buffer, 0, buffer.Length); }
// Check the given UsbStream for a valid WireProtocol packet (header). Returns true // along with the WP_Packet (header) if available. Returns false if nothing valid received as yet. public static bool ReadPacket(UsbStream stream, out WP_Packet packet) { packet = new WP_Packet(); byte[] buffer = new byte[WP_Packet.packetSize]; // Size of marshalled WP_Packet int nRead; // Read in enough data for a complete WP_Packet (header) nRead = stream.Read(buffer, 0, WP_Packet.packetSize); // If there were not enough bytes to fill the complete WP_Packet, it's no good if (nRead != WP_Packet.packetSize) { return(false); } // Unmarshal the signature string from the buffer to the WP_Packet packet.m_signature = ""; for (int i = 0; (i < WP_Packet.m_crcHeader_offset) && (buffer[i] != 0); i++) { packet.m_signature += (char)buffer[i]; } // Unmarshal the rest of the buffer into the WP_Packet structure. packet.m_crcHeader = ReadUINT32(buffer, WP_Packet.m_crcHeader_offset); packet.m_crcData = ReadUINT32(buffer, WP_Packet.m_crcData_offset); packet.m_seq = ReadUINT16(buffer, WP_Packet.m_seq_offset); packet.m_seqReply = ReadUINT16(buffer, WP_Packet.m_seqReply_offset); packet.m_flags = ReadUINT32(buffer, WP_Packet.m_flags_offset); packet.m_size = ReadUINT32(buffer, WP_Packet.m_size_offset); packet.m_cmd = (Commands)ReadUINT32(buffer, WP_Packet.m_cmd_offset); // The header CRC must first be zeroed to calculate the correct header CRC WriteUINT32(buffer, WP_Packet.m_crcHeader_offset, 0); // If the calculated CRC does not match that of the packet, it is no good if (packet.m_crcHeader != SUPPORT_ComputeCRC(buffer, 0, WP_Packet.packetSize, 0)) { return(false); } return(true); }
// Sends the given WP_Packet (header) and public static void SendPacket(UsbStream stream, WP_Packet packet, byte[] data) { int index; // Create a buffer whose size is large enough to contain both the WP_Packet (header) // and data so that they may be sent as a single packet. byte[] buffer = new byte[WP_Packet.packetSize + data.Length]; // Marshal the signature string into the buffer for (index = 0; index < packet.m_signature.Length && index < WP_Packet.m_crcHeader_offset; index++) { buffer[index] = (byte)packet.m_signature[index]; } while (index++ < WP_Packet.m_crcHeader_offset) buffer[index] = 0; // Fill to end with zeros // Marshal the WP_Packet (header) into the buffer. Note that the CRC values start as zero // so that the CRC of the WP_Packet (header) may be computed the same for host and target. WriteUINT32(buffer, WP_Packet.m_crcHeader_offset, 0); WriteUINT32(buffer, WP_Packet.m_crcData_offset, 0); WriteUINT32(buffer, WP_Packet.m_cmd_offset, (UInt32)packet.m_cmd); WriteUINT16(buffer, WP_Packet.m_seq_offset, packet.m_seq); WriteUINT16(buffer, WP_Packet.m_seqReply_offset, packet.m_seqReply); WriteUINT32(buffer, WP_Packet.m_flags_offset, packet.m_flags); WriteUINT32(buffer, WP_Packet.m_size_offset, (UInt32)data.Length); // Copy the data to the buffer for (int i = 0; i < data.Length; i++) buffer[WP_Packet.packetSize + i] = data[i]; // Calculate the CRC of the data if (data.Length != 0) { packet.m_crcData = SUPPORT_ComputeCRC(data, 0, data.Length, 0); WriteUINT32(buffer, WP_Packet.m_crcData_offset, packet.m_crcData); } // Calculate the CRC of the packet header packet.m_crcHeader = SUPPORT_ComputeCRC(buffer, 0, WP_Packet.packetSize, 0); WriteUINT32(buffer, WP_Packet.m_crcHeader_offset, packet.m_crcHeader); // Write out the complete packet to the USB stream stream.Write(buffer, 0, buffer.Length); }
// Check the given UsbStream for a valid WireProtocol packet (header). Returns true // along with the WP_Packet (header) if available. Returns false if nothing valid received as yet. public static bool ReadPacket(UsbStream stream, out WP_Packet packet) { packet = new WP_Packet(); byte[] buffer = new byte[WP_Packet.packetSize]; // Size of marshalled WP_Packet int nRead; // Read in enough data for a complete WP_Packet (header) nRead = stream.Read(buffer, 0, WP_Packet.packetSize); // If there were not enough bytes to fill the complete WP_Packet, it's no good if (nRead != WP_Packet.packetSize) { return false; } // Unmarshal the signature string from the buffer to the WP_Packet packet.m_signature = ""; for (int i = 0; (i < WP_Packet.m_crcHeader_offset) && (buffer[i] != 0); i++) { packet.m_signature += (char)buffer[i]; } // Unmarshal the rest of the buffer into the WP_Packet structure. packet.m_crcHeader = ReadUINT32(buffer, WP_Packet.m_crcHeader_offset); packet.m_crcData = ReadUINT32(buffer, WP_Packet.m_crcData_offset); packet.m_seq = ReadUINT16(buffer, WP_Packet.m_seq_offset); packet.m_seqReply = ReadUINT16(buffer, WP_Packet.m_seqReply_offset); packet.m_flags = ReadUINT32(buffer, WP_Packet.m_flags_offset); packet.m_size = ReadUINT32(buffer, WP_Packet.m_size_offset); packet.m_cmd = (Commands)ReadUINT32(buffer, WP_Packet.m_cmd_offset); // The header CRC must first be zeroed to calculate the correct header CRC WriteUINT32(buffer, WP_Packet.m_crcHeader_offset, 0); // If the calculated CRC does not match that of the packet, it is no good if (packet.m_crcHeader != SUPPORT_ComputeCRC(buffer, 0, WP_Packet.packetSize, 0)) { return false; } return true; }