fix: handle ack+packet strings
This commit is contained in:
parent
460fcec370
commit
1bd9964f58
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -107,11 +107,25 @@ 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: {
|
||||||
|
if (input.len == 1) break :blk .nothing;
|
||||||
|
|
||||||
|
break :blk switch (input[1]) {
|
||||||
|
'$' => self.handlePacket(allocator, input[1..]),
|
||||||
|
else => std.debug.panic("Unknown: {s}", .{input}),
|
||||||
|
};
|
||||||
|
},
|
||||||
'-' => .retry,
|
'-' => .retry,
|
||||||
'$' => blk: {
|
'$' => try self.handlePacket(allocator, input),
|
||||||
// Packet
|
'\x03' => .nothing,
|
||||||
|
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;
|
var packet = Packet.from(allocator, input) catch return .nack;
|
||||||
defer packet.deinit(allocator);
|
defer packet.deinit(allocator);
|
||||||
|
|
||||||
|
@ -121,20 +135,16 @@ fn parse(self: *Self, allocator: Allocator, input: []const u8) !Action {
|
||||||
const reply = string.inner();
|
const reply = string.inner();
|
||||||
|
|
||||||
// deallocated by the caller
|
// deallocated by the caller
|
||||||
const response = try std.fmt.allocPrint(allocator, "${s}#{x:0>2}", .{ reply, Packet.checksum(reply) });
|
const response = try std.fmt.allocPrint(allocator, "+${s}#{x:0>2}", .{ reply, Packet.checksum(reply) });
|
||||||
|
|
||||||
break :blk .{ .send = response };
|
return .{ .send = response };
|
||||||
},
|
|
||||||
'\x03' => .nothing,
|
|
||||||
else => std.debug.panic("Unknown: {s}", .{input}),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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),
|
||||||
|
|
Loading…
Reference in New Issue