Name the attach reply statuses instead of magic numbers
Add an AttachStatus enum to ps2-library.zig for AttachReply.status, distinguishing the three failure causes handleAttach previously collapsed into a bare -1: invalid_request (message too short), missing_endpoint (no capability passed), and no_such_device (no port identified the requested device type). The keyboard and mouse drivers check against AttachStatus.ok rather than a literal 0.
This commit is contained in:
@@ -67,25 +67,25 @@ var port_device_types = [_]?ps2.DeviceType{ null, null };
|
||||
/// Writes an `AttachReply` into `out` and returns its length.
|
||||
fn handleAttach(message: []const u8, got: ipc.Received, out: []u8) usize {
|
||||
const reply = struct {
|
||||
fn write(buffer: []u8, status: i32) usize {
|
||||
const header = ps2.AttachReply{ .status = status };
|
||||
fn write(buffer: []u8, status: ps2.AttachStatus) usize {
|
||||
const header = ps2.AttachReply{ .status = @intFromEnum(status) };
|
||||
@memcpy(buffer[0..@sizeOf(ps2.AttachReply)], std.mem.asBytes(&header));
|
||||
return @sizeOf(ps2.AttachReply);
|
||||
}
|
||||
};
|
||||
|
||||
if (message.len < @sizeOf(ps2.AttachRequest)) return reply.write(out, -1);
|
||||
if (message.len < @sizeOf(ps2.AttachRequest)) return reply.write(out, .invalid_request);
|
||||
const request = std.mem.bytesToValue(ps2.AttachRequest, message[0..@sizeOf(ps2.AttachRequest)]);
|
||||
const endpoint = got.cap orelse return reply.write(out, -1); // no endpoint passed
|
||||
const endpoint = got.cap orelse return reply.write(out, .missing_endpoint);
|
||||
|
||||
for (&port_device_types, 0..) |maybe_type, port_index| {
|
||||
const device_type = maybe_type orelse continue;
|
||||
if (@intFromEnum(device_type) != request.device_type) continue;
|
||||
port_endpoints[port_index] = endpoint;
|
||||
writeLine("system/drivers/ps2-bus: {s} driver attached\n", .{@tagName(device_type)});
|
||||
return reply.write(out, 0);
|
||||
return reply.write(out, .ok);
|
||||
}
|
||||
return reply.write(out, -1); // no port identified as that device type
|
||||
return reply.write(out, .no_such_device);
|
||||
}
|
||||
|
||||
pub fn main() void {
|
||||
|
||||
Reference in New Issue
Block a user