From 4adfb24542ca2df3ce1b81fe71ade2bc6d8272ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benito=20Palacios=20S=C3=A1nchez?= Date: Sun, 15 Mar 2015 12:45:46 +0100 Subject: [PATCH] Store last command sent for context in reply converter --- NitroDebugger.UnitTests/PacketTypesTest.cs | 3 ++- NitroDebugger/RSP/PacketBinConverter.cs | 4 ++-- NitroDebugger/RSP/Presentation.cs | 4 +++- NitroDebugger/RSP/ReplyPacketFactory.cs | 4 ++-- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/NitroDebugger.UnitTests/PacketTypesTest.cs b/NitroDebugger.UnitTests/PacketTypesTest.cs index b32eb0d..c080ffb 100644 --- a/NitroDebugger.UnitTests/PacketTypesTest.cs +++ b/NitroDebugger.UnitTests/PacketTypesTest.cs @@ -182,7 +182,8 @@ namespace UnitTests byte[] expected = new byte[] { 0xCA, 0xFE, 0xBE, 0xBE, 0x00, 0x10, 0x20 }; string dataString = BitConverter.ToString(expected).Replace("-", ""); - ReplyPacket reply = ReplyPacketFactory.CreateReplyPacket(dataString); + ReadMemoryCommand readMemory = new ReadMemoryCommand(0x00, 0x00); + ReplyPacket reply = ReplyPacketFactory.CreateReplyPacket(dataString, readMemory); Assert.IsInstanceOf(reply); Assert.AreEqual(expected, ((DataReply)reply).GetData()); diff --git a/NitroDebugger/RSP/PacketBinConverter.cs b/NitroDebugger/RSP/PacketBinConverter.cs index 06d9f21..1dcdd4c 100644 --- a/NitroDebugger/RSP/PacketBinConverter.cs +++ b/NitroDebugger/RSP/PacketBinConverter.cs @@ -46,7 +46,7 @@ namespace NitroDebugger.RSP return TextEncoding.GetBytes(binPacket.ToString()); } - public static ReplyPacket FromBinary(byte[] data) + public static ReplyPacket FromBinary(byte[] data, CommandPacket commandSent = null) { if (!ValidateBinary(data)) throw new FormatException("[BIN] Invalid packet"); @@ -61,7 +61,7 @@ namespace NitroDebugger.RSP if (receivedChecksum != calculatedChecksum.ToString("x2")) throw new FormatException("[BIN] Invalid checksum"); - return ReplyPacketFactory.CreateReplyPacket(packetData); + return ReplyPacketFactory.CreateReplyPacket(packetData, commandSent); } private static bool ValidateBinary(byte[] data) diff --git a/NitroDebugger/RSP/Presentation.cs b/NitroDebugger/RSP/Presentation.cs index be2d789..88b3d7f 100644 --- a/NitroDebugger/RSP/Presentation.cs +++ b/NitroDebugger/RSP/Presentation.cs @@ -36,6 +36,7 @@ namespace NitroDebugger.RSP private Session session; private CancellationTokenSource cts; + private CommandPacket lastCommandSent; public Presentation(string hostname, int port) { @@ -50,6 +51,7 @@ namespace NitroDebugger.RSP public void SendCommand(CommandPacket command) { + this.lastCommandSent = command; this.SendData(PacketBinConverter.ToBinary(command)); } @@ -101,7 +103,7 @@ namespace NitroDebugger.RSP try { // Get data byte[] packet = this.session.ReadPacket(PacketSeparator); - response = PacketBinConverter.FromBinary(packet); + response = PacketBinConverter.FromBinary(packet, lastCommandSent); // Send ACK this.session.Write(RawPacket.Ack); diff --git a/NitroDebugger/RSP/ReplyPacketFactory.cs b/NitroDebugger/RSP/ReplyPacketFactory.cs index b1a8587..21a175b 100644 --- a/NitroDebugger/RSP/ReplyPacketFactory.cs +++ b/NitroDebugger/RSP/ReplyPacketFactory.cs @@ -26,7 +26,7 @@ namespace NitroDebugger.RSP { public static class ReplyPacketFactory { - public static ReplyPacket CreateReplyPacket(string data) + public static ReplyPacket CreateReplyPacket(string data, CommandPacket commandSent = null) { if (data == "OK") return new OkReply(); @@ -37,7 +37,7 @@ namespace NitroDebugger.RSP if (data.Length == 3 && data[0] == 'E') return new ErrorReply(Convert.ToInt32(data.Substring(1), 16)); - if (data.Length % 2 == 0) { + if (commandSent is ReadMemoryCommand) { try { byte[] dataBytes = Enumerable.Range(0, data.Length / 2) .Select(i => data.Substring(i * 2, 2))