fix: handle ack+packet strings

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-12-15 20:23:54 -04:00
parent 460fcec370
commit 1bd9964f58
2 changed files with 41 additions and 42 deletions

View File

@ -19,8 +19,6 @@ pub fn from(allocator: Allocator, str: []const u8) !Self {
const chksum_str = tokens.next() orelse return error.MissingCheckSum; const chksum_str = tokens.next() orelse return error.MissingCheckSum;
const chksum = std.fmt.parseInt(u8, chksum_str, 16) catch return error.InvalidChecksum; const chksum = std.fmt.parseInt(u8, chksum_str, 16) catch return error.InvalidChecksum;
// log.info("Contents: {s}", .{contents});
if (!Self.verify(contents, chksum)) return error.ChecksumMismatch; if (!Self.verify(contents, chksum)) return error.ChecksumMismatch;
return .{ .contents = try allocator.dupe(u8, contents) }; return .{ .contents = try allocator.dupe(u8, contents) };
@ -99,10 +97,8 @@ pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !String {
'M' => @panic("TODO: Memory Write"), 'M' => @panic("TODO: Memory Write"),
'c' => @panic("TODO: Continue"), 'c' => @panic("TODO: Continue"),
's' => { 's' => {
var tokens = std.mem.tokenize(u8, self.contents[1..], " "); // var tokens = std.mem.tokenize(u8, self.contents[1..], " ");
const addr = if (tokens.next()) |s| try std.fmt.parseInt(u32, s, 16) else null; // const addr = if (tokens.next()) |s| try std.fmt.parseInt(u32, s, 16) else null;
log.debug("s addr: {?X:0>8}", .{addr});
emu.step(); emu.step();
@ -111,18 +107,8 @@ pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !String {
}, },
// Optional // Optional
// 'S' => {
// var tokens = std.mem.tokenize(u8, self.contents[1..], " ");
// const signal = if (tokens.next()) |s| try std.fmt.parseInt(u8, s, 16) else null;
// log.debug("S signal: {?}", .{signal});
// emu.step();
// const ret = try std.fmt.allocPrint(allocator, "S{x:0>2}", .{@enumToInt(Signal.Trap)});
// return .{ .alloc = ret };
// },
'D' => { 'D' => {
log.info("Disconneccting...", .{}); log.info("Disconnecting...", .{});
return .{ .static = "OK" }; return .{ .static = "OK" };
}, },
'H' => return .{ .static = "" }, 'H' => return .{ .static = "" },
@ -153,13 +139,11 @@ pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !String {
_ = tokens.next(); // Xfer _ = tokens.next(); // Xfer
_ = tokens.next(); // features _ = tokens.next(); // features
_ = tokens.next(); // read _ = tokens.next(); // read
const annex = tokens.next() orelse return .{ .static = "E00" }; const annex = tokens.next() orelse return .{ .static = "E9999" };
const offset_str = tokens.next() orelse return .{ .static = "E00" }; const offset_str = tokens.next() orelse return .{ .static = "E99999" };
const length_str = tokens.next() orelse return .{ .static = "E00" }; const length_str = tokens.next() orelse return .{ .static = "E9999" };
if (std.mem.eql(u8, annex, "target.xml")) { if (std.mem.eql(u8, annex, "target.xml")) {
log.info("Providing ARMv4T target description", .{});
const offset = try std.fmt.parseInt(usize, offset_str, 16); const offset = try std.fmt.parseInt(usize, offset_str, 16);
const length = try std.fmt.parseInt(usize, length_str, 16); const length = try std.fmt.parseInt(usize, length_str, 16);
@ -175,7 +159,7 @@ pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !String {
return .{ .alloc = ret }; return .{ .alloc = ret };
} else { } else {
log.err("Unexpected Annex: {s}", .{annex}); log.err("Unexpected Annex: {s}", .{annex});
return .{ .static = "E00" }; return .{ .static = "E9999" };
} }
return .{ .static = "" }; return .{ .static = "" };
@ -189,8 +173,6 @@ pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !String {
const offset_str = tokens.next() orelse return .{ .static = "E9999" }; const offset_str = tokens.next() orelse return .{ .static = "E9999" };
const length_str = tokens.next() orelse return .{ .static = "E9999" }; const length_str = tokens.next() orelse return .{ .static = "E9999" };
log.info("Providing GBA memory map", .{});
const offset = try std.fmt.parseInt(usize, offset_str, 16); const offset = try std.fmt.parseInt(usize, offset_str, 16);
const length = try std.fmt.parseInt(usize, length_str, 16); const length = try std.fmt.parseInt(usize, length_str, 16);

View File

@ -107,34 +107,44 @@ pub fn run(self: *Self, allocator: Allocator) !void {
} }
fn parse(self: *Self, allocator: Allocator, input: []const u8) !Action { fn parse(self: *Self, allocator: Allocator, input: []const u8) !Action {
// log.debug("-> {s}", .{input});
return switch (input[0]) { return switch (input[0]) {
'+' => .nothing, '+' => blk: {
'-' => .retry, if (input.len == 1) break :blk .nothing;
'$' => blk: {
// Packet
var packet = Packet.from(allocator, input) catch return .nack;
defer packet.deinit(allocator);
var string = packet.parse(allocator, self.emu) catch return .nack; break :blk switch (input[1]) {
defer string.deinit(allocator); '$' => self.handlePacket(allocator, input[1..]),
else => std.debug.panic("Unknown: {s}", .{input}),
const reply = string.inner(); };
// deallocated by the caller
const response = try std.fmt.allocPrint(allocator, "${s}#{x:0>2}", .{ reply, Packet.checksum(reply) });
break :blk .{ .send = response };
}, },
'-' => .retry,
'$' => try self.handlePacket(allocator, input),
'\x03' => .nothing, '\x03' => .nothing,
else => std.debug.panic("Unknown: {s}", .{input}), else => std.debug.panic("Unknown: {s}", .{input}),
}; };
} }
fn handlePacket(self: *Self, allocator: Allocator, input: []const u8) !Action {
var packet = Packet.from(allocator, input) catch return .nack;
defer packet.deinit(allocator);
var string = packet.parse(allocator, self.emu) catch return .nack;
defer string.deinit(allocator);
const reply = string.inner();
// deallocated by the caller
const response = try std.fmt.allocPrint(allocator, "+${s}#{x:0>2}", .{ reply, Packet.checksum(reply) });
return .{ .send = response };
}
fn send(self: *Self, allocator: Allocator, action: Action) !void { fn send(self: *Self, allocator: Allocator, action: Action) !void {
switch (action) { switch (action) {
.send => |pkt| { .send => |pkt| {
_ = try self.client.send("+"); // ACK
_ = try self.client.send(pkt); _ = try self.client.send(pkt);
// log.debug("<- {s}", .{pkt});
self.reset(allocator); self.reset(allocator);
self.pkt_cache = pkt; self.pkt_cache = pkt;
@ -142,14 +152,21 @@ fn send(self: *Self, allocator: Allocator, action: Action) !void {
.retry => { .retry => {
log.warn("received nack, resending: \"{?s}\"", .{self.pkt_cache}); log.warn("received nack, resending: \"{?s}\"", .{self.pkt_cache});
if (self.pkt_cache) |pkt| _ = try self.client.send(pkt); // FIXME: is an ack to a nack necessary? if (self.pkt_cache) |pkt| {
_ = try self.client.send(pkt);
// log.debug("<- {s}", .{pkt});
}
}, },
.ack => { .ack => {
_ = try self.client.send("+"); _ = try self.client.send("+");
// log.debug("<- +", .{});
self.reset(allocator); self.reset(allocator);
}, },
.nack => { .nack => {
_ = try self.client.send("-"); _ = try self.client.send("-");
// log.debug("<- -", .{});
self.reset(allocator); self.reset(allocator);
}, },
.nothing => self.reset(allocator), .nothing => self.reset(allocator),