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:
@@ -109,7 +109,7 @@ pub fn main(init: runtime.process.Init) void {
|
||||
return;
|
||||
};
|
||||
if (attached.len < @sizeOf(ps2.AttachReply) or
|
||||
std.mem.bytesToValue(ps2.AttachReply, attach_reply[0..@sizeOf(ps2.AttachReply)]).status != 0)
|
||||
std.mem.bytesToValue(ps2.AttachReply, attach_reply[0..@sizeOf(ps2.AttachReply)]).status != @intFromEnum(ps2.AttachStatus.ok))
|
||||
{
|
||||
_ = runtime.system.write("system/drivers/ps2-bus/keyboard: attach refused\n");
|
||||
return;
|
||||
|
||||
@@ -82,7 +82,7 @@ pub fn main(init: runtime.process.Init) void {
|
||||
return;
|
||||
};
|
||||
if (attached.len < @sizeOf(ps2.AttachReply) or
|
||||
std.mem.bytesToValue(ps2.AttachReply, attach_reply[0..@sizeOf(ps2.AttachReply)]).status != 0)
|
||||
std.mem.bytesToValue(ps2.AttachReply, attach_reply[0..@sizeOf(ps2.AttachReply)]).status != @intFromEnum(ps2.AttachStatus.ok))
|
||||
{
|
||||
_ = runtime.system.write("system/drivers/ps2-bus/mouse: attach refused\n");
|
||||
return;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -285,7 +285,18 @@ pub const AttachRequest = extern struct {
|
||||
device_type: u32,
|
||||
};
|
||||
|
||||
/// Reply to an `AttachRequest`. `status` is 0 on success or a negative errno.
|
||||
/// How the bus answered an `AttachRequest` (`AttachReply.status`).
|
||||
pub const AttachStatus = enum(i32) {
|
||||
ok = 0,
|
||||
/// The request was malformed (too short to be an `AttachRequest`).
|
||||
invalid_request = -1,
|
||||
/// The call carried no endpoint capability to forward to.
|
||||
missing_endpoint = -2,
|
||||
/// No port identified a device of the requested type.
|
||||
no_such_device = -3,
|
||||
};
|
||||
|
||||
/// Reply to an `AttachRequest`. `status` is an `AttachStatus` value.
|
||||
pub const AttachReply = extern struct {
|
||||
status: i32,
|
||||
_padding: u32 = 0,
|
||||
|
||||
Reference in New Issue
Block a user