diff --git a/build.zig b/build.zig index c1fa711..9f57029 100644 --- a/build.zig +++ b/build.zig @@ -24,7 +24,7 @@ pub fn build(b: *std.Build) !void { const optimize = b.standardOptimizeOption(.{}); // Add the gpio module so it can be used by the package manager - var gpio_module = b.createModule(.{ .source_file = .{ .path = "src/index.zig" } }); + const gpio_module = b.createModule(.{ .root_source_file = .{ .path = "src/index.zig" } }); try b.modules.put(b.dupe("gpio"), gpio_module); // Create a step to build all the examples @@ -41,7 +41,7 @@ pub fn build(b: *std.Build) !void { .target = target, .optimize = optimize, }); - exe.addModule("gpio", gpio_module); + exe.root_module.addImport("gpio", gpio_module); const build_step = b.addInstallArtifact(exe, .{}); step.dependOn(&build_step.step); @@ -62,7 +62,7 @@ pub fn build(b: *std.Build) !void { .target = target, .optimize = optimize, }); - exe.addModule("gpio", gpio_module); + exe.root_module.addImport("gpio", gpio_module); const build_step = b.addInstallArtifact(exe, .{}); step.dependOn(&build_step.step); diff --git a/src/cmd/detect.zig b/src/cmd/detect.zig index c8de3a3..e8a1e6c 100644 --- a/src/cmd/detect.zig +++ b/src/cmd/detect.zig @@ -2,7 +2,7 @@ const std = @import("std"); const gpio = @import("gpio"); pub fn main() !void { - var iter_dir = try std.fs.openIterableDirAbsolute("/dev", .{}); + var iter_dir = try std.fs.openDirAbsolute("/dev", .{ .iterate = true }); defer iter_dir.close(); const stdout = std.io.getStdOut().writer(); @@ -11,7 +11,7 @@ pub fn main() !void { while (try iter.next()) |entry| { if (!hasPrefix(entry.name, "gpiochip")) continue; - const fl = try iter_dir.dir.openFile(entry.name, .{}); + const fl = try iter_dir.openFile(entry.name, .{}); var chip = try gpio.getChipByFd(fl.handle); defer chip.close(); // This will close the fd diff --git a/src/cmd/get.zig b/src/cmd/get.zig index f2860ae..894d7b2 100644 --- a/src/cmd/get.zig +++ b/src/cmd/get.zig @@ -16,7 +16,7 @@ pub fn main() !void { return error.InsufficientArguments; } - var path: []const u8 = if (hasPrefix(args[1], "gpiochip")) + const path: []const u8 = if (hasPrefix(args[1], "gpiochip")) try std.mem.concat(alloc, u8, &.{ "/dev/", args[1] }) else try std.mem.concat(alloc, u8, &.{ "/dev/gpiochip", args[1] }); @@ -32,7 +32,7 @@ pub fn main() !void { // Iterate over each argument starting from the second one for (args[2..args.len]) |argument| { // Parse each argument as an integer and add it to offsets - var offset = try std.fmt.parseUnsigned(u32, argument, 10); + const offset = try std.fmt.parseUnsigned(u32, argument, 10); try offsets.append(offset); } diff --git a/src/cmd/info.zig b/src/cmd/info.zig index 98cd0a4..f8200a3 100644 --- a/src/cmd/info.zig +++ b/src/cmd/info.zig @@ -18,7 +18,7 @@ pub fn main() !void { // If the argument has the "gpiochip" prefix, // just use it unchanged. Otherwise, add the prefix. - var filename: []const u8 = if (hasGpiochip) + const filename: []const u8 = if (hasGpiochip) argument else try std.mem.concat(alloc, u8, &.{ "gpiochip", argument }); diff --git a/src/cmd/set.zig b/src/cmd/set.zig index 5bc44f7..ff7b6fb 100644 --- a/src/cmd/set.zig +++ b/src/cmd/set.zig @@ -16,7 +16,7 @@ pub fn main() !void { return error.InsufficientArguments; } - var path: []const u8 = if (hasPrefix(args[1], "gpiochip")) + const path: []const u8 = if (hasPrefix(args[1], "gpiochip")) try std.mem.concat(alloc, u8, &.{ "/dev/", args[1] }) else try std.mem.concat(alloc, u8, &.{ "/dev/gpiochip", args[1] }); @@ -34,8 +34,8 @@ pub fn main() !void { // Get the index of the equals sign in the argument const eqIndex = std.mem.indexOf(u8, argument, "=") orelse return error.InvalidArgument; // Parse each argument's offset and value, and add it to the values map - var offset = try std.fmt.parseUnsigned(u32, argument[0..eqIndex], 10); - var value = try std.fmt.parseUnsigned(u1, argument[eqIndex + 1 .. argument.len], 10); + const offset = try std.fmt.parseUnsigned(u32, argument[0..eqIndex], 10); + const value = try std.fmt.parseUnsigned(u1, argument[eqIndex + 1 .. argument.len], 10); try values.put(offset, value != 0); } diff --git a/src/gpio.zig b/src/gpio.zig index 3083d91..62dcdae 100644 --- a/src/gpio.zig +++ b/src/gpio.zig @@ -3,19 +3,19 @@ const gpio = @import("index.zig"); /// Opens the file at path and uses the file descriptor to get the gpiochip. pub fn getChip(path: []const u8) !Chip { - var fl = try std.fs.openFileAbsolute(path, .{}); + const fl = try std.fs.openFileAbsolute(path, .{}); return try getChipByFd(fl.handle); } /// Same as `getChip` but the `path` parameter is null-terminated. pub fn getChipZ(path: [*:0]const u8) !Chip { - var fl = try std.fs.openFileAbsoluteZ(path, .{}); + const fl = try std.fs.openFileAbsoluteZ(path, .{}); return try getChipByFd(fl.handle); } /// Returns a `chip` with the given file descriptor. -pub fn getChipByFd(fd: std.os.fd_t) !Chip { - var info = try gpio.uapi.getChipInfo(fd); +pub fn getChipByFd(fd: std.posix.fd_t) !Chip { + const info = try gpio.uapi.getChipInfo(fd); return Chip{ .name = info.name, .label = info.label, @@ -35,7 +35,7 @@ pub const Chip = struct { /// If it isn't set, "zig-gpio" will be used instead. consumer: ?[gpio.uapi.MAX_NAME_SIZE]u8 = null, /// The file descriptor of the `gpiochip` device. - handle: std.os.fd_t, + handle: std.posix.fd_t, // The amount of lines available under this device. lines: u32, closed: bool = false, @@ -71,7 +71,7 @@ pub const Chip = struct { /// Requests and returns a single line at the given `offset`, from the given `chip`. pub fn requestLine(self: Chip, offset: u32, flags: gpio.uapi.LineFlags) !Line { - var l = try self.requestLines(&.{offset}, flags); + const l = try self.requestLines(&.{offset}, flags); return Line{ .lines = l }; } @@ -108,14 +108,14 @@ pub const Chip = struct { pub fn close(self: *Chip) void { if (self.closed) return; self.closed = true; - std.os.close(self.handle); + std.posix.close(self.handle); } }; /// Represents a collection of lines requested from a `chip`. pub const Lines = struct { /// The file descriptor of the lines. - handle: std.os.fd_t, + handle: std.posix.fd_t, /// The amount of lines being controlled. num_lines: u32, /// The offsets of the lines being controlled. @@ -254,7 +254,7 @@ pub const Lines = struct { pub fn close(self: *Lines) void { if (self.closed) return; self.closed = true; - std.os.close(self.handle); + std.posix.close(self.handle); } }; diff --git a/src/uapi.zig b/src/uapi.zig index 3a74dc4..6ec9ecc 100644 --- a/src/uapi.zig +++ b/src/uapi.zig @@ -176,20 +176,20 @@ pub const LineInfoChanged = extern struct { /// Returns an error based on the given return code fn handleErrno(ret: usize) !void { if (ret == 0) return; - return switch (std.os.errno(ret)) { + return switch (std.posix.errno(ret)) { .BUSY => error.DeviceIsBusy, .INVAL => error.InvalidArgument, .BADF => error.BadFileDescriptor, .NOTTY => error.InappropriateIOCTLForDevice, .IO => error.IOError, .FAULT => unreachable, - else => |err| return std.os.unexpectedErrno(err), + else => |err| return std.posix.unexpectedErrno(err), }; } /// Executes `GPIO_GET_CHIPINFO_IOCTL` on the given fd and returns the resulting /// `ChipInfo` value -pub fn getChipInfo(fd: std.os.fd_t) !ChipInfo { +pub fn getChipInfo(fd: std.posix.fd_t) !ChipInfo { const req = std.os.linux.IOCTL.IOR(0xB4, 0x01, ChipInfo); var info = std.mem.zeroes(ChipInfo); try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&info))); @@ -198,7 +198,7 @@ pub fn getChipInfo(fd: std.os.fd_t) !ChipInfo { /// Executes `GPIO_V2_GET_LINEINFO_IOCTL` on the given fd and returns the resulting /// `LineInfo` value -pub fn getLineInfo(fd: std.os.fd_t, offset: u32) !LineInfo { +pub fn getLineInfo(fd: std.posix.fd_t, offset: u32) !LineInfo { const req = std.os.linux.IOCTL.IOWR(0xB4, 0x05, LineInfo); var info = std.mem.zeroes(LineInfo); info.offset = offset; @@ -208,7 +208,7 @@ pub fn getLineInfo(fd: std.os.fd_t, offset: u32) !LineInfo { /// Executes `GPIO_V2_GET_LINEINFO_WATCH_IOCTL` on the given fd and returns the resulting /// `LineInfo` value -pub fn watchLineInfo(fd: std.os.fd_t, offset: u32) !LineInfo { +pub fn watchLineInfo(fd: std.posix.fd_t, offset: u32) !LineInfo { const req = std.os.linux.IOCTL.IOWR(0xB4, 0x06, LineInfo); var info = std.mem.zeroes(LineInfo); info.offset = offset; @@ -217,14 +217,14 @@ pub fn watchLineInfo(fd: std.os.fd_t, offset: u32) !LineInfo { } /// Executes `GPIO_GET_LINEINFO_UNWATCH_IOCTL` on the given fd -pub fn unwatchLineInfo(fd: std.os.fd_t, offset: u32) !void { +pub fn unwatchLineInfo(fd: std.posix.fd_t, offset: u32) !void { const req = std.os.linux.IOCTL.IOWR(0xB4, 0x0C, u32); try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&offset))); } /// Executes `GPIO_V2_GET_LINE_IOCTL` on the given fd and returns the resulting /// line descriptor -pub fn getLine(fd: std.os.fd_t, lr: LineRequest) !std.os.fd_t { +pub fn getLine(fd: std.posix.fd_t, lr: LineRequest) !std.posix.fd_t { const lrp = &lr; const req = std.os.linux.IOCTL.IOWR(0xB4, 0x07, LineRequest); try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(lrp))); @@ -233,7 +233,7 @@ pub fn getLine(fd: std.os.fd_t, lr: LineRequest) !std.os.fd_t { /// Executes `GPIO_V2_LINE_GET_VALUES_IOCTL` on the given fd with the given mask, /// and returns a bitset representing all the line values. -pub fn getLineValues(fd: std.os.fd_t, mask: LineValueBitset) !LineValueBitset { +pub fn getLineValues(fd: std.posix.fd_t, mask: LineValueBitset) !LineValueBitset { var vals = LineValues{ .mask = mask }; const req = std.os.linux.IOCTL.IOWR(0xB4, 0x0E, LineValues); try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&vals))); @@ -241,13 +241,13 @@ pub fn getLineValues(fd: std.os.fd_t, mask: LineValueBitset) !LineValueBitset { } /// Executes `GPIO_V2_LINE_SET_VALUES_IOCTL` on the given fd -pub fn setLineValues(fd: std.os.fd_t, lv: LineValues) !void { +pub fn setLineValues(fd: std.posix.fd_t, lv: LineValues) !void { const req = std.os.linux.IOCTL.IOWR(0xB4, 0x0F, LineValues); try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&lv))); } /// Executes `GPIO_V2_LINE_SET_CONFIG_IOCTL` on the given fd -pub fn setLineConfig(fd: std.os.fd_t, lc: LineConfig) !void { +pub fn setLineConfig(fd: std.posix.fd_t, lc: LineConfig) !void { const req = std.os.linux.IOCTL.IOWR(0xB4, 0x0D, LineConfig); try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&lc))); }