Skip to content

Commit

Permalink
refactor: Allow backend fns self by val or ref
Browse files Browse the repository at this point in the history
  • Loading branch information
mochalins committed Sep 20, 2024
1 parent 9339853 commit 3ca6381
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 55 deletions.
77 changes: 37 additions & 40 deletions src/backend/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub fn open(path: []const u8) !PortImpl {
});
}

pub fn close(port: *const PortImpl) void {
pub fn close(port: PortImpl) void {
port.close();
}

pub fn configure(port: *const PortImpl, config: serialport.Config) !void {
pub fn configure(port: PortImpl, config: serialport.Config) !void {
var settings = try std.posix.tcgetattr(port.handle);

settings.iflag = .{};
Expand Down Expand Up @@ -55,10 +55,7 @@ pub fn configure(port: *const PortImpl, config: serialport.Config) !void {
try std.posix.tcsetattr(port.handle, .NOW, settings);
}

pub fn flush(
port: *const PortImpl,
options: serialport.Port.FlushOptions,
) !void {
pub fn flush(port: PortImpl, options: serialport.Port.FlushOptions) !void {
if (!options.input and !options.output) return;

const TCIFLUSH = 0;
Expand All @@ -85,7 +82,7 @@ pub fn flush(
};
}

pub fn poll(port: *const PortImpl) !bool {
pub fn poll(port: PortImpl) !bool {
var pollfds: [1]linux.pollfd = .{
.{
.fd = port.handle,
Expand All @@ -103,11 +100,11 @@ pub fn poll(port: *const PortImpl) !bool {
return true;
}

pub fn reader(port: *const PortImpl) Reader {
pub fn reader(port: PortImpl) Reader {
return port.reader();
}

pub fn writer(port: *const PortImpl) Writer {
pub fn writer(port: PortImpl) Writer {
return port.writer();
}

Expand Down Expand Up @@ -171,7 +168,7 @@ fn openVirtualPorts(master_port: *PortImpl, slave_port: *PortImpl) !void {
});

master_port.* = try open("/dev/ptmx");
errdefer close(master_port);
errdefer close(master_port.*);

if (c.grantpt(master_port.handle) < 0 or
c.unlockpt(master_port.handle) < 0)
Expand All @@ -190,82 +187,82 @@ test {
var master: PortImpl = undefined;
var slave: PortImpl = undefined;
try openVirtualPorts(&master, &slave);
defer close(&master);
defer close(&slave);
defer close(master);
defer close(slave);

const config: serialport.Config = .{
.baud_rate = .B230400,
.handshake = .software,
};

try configure(&master, config);
try configure(&slave, config);
try configure(master, config);
try configure(slave, config);

const writer_m = writer(&master);
const reader_s = reader(&slave);
const writer_m = writer(master);
const reader_s = reader(slave);

try std.testing.expectEqual(false, try poll(&slave));
try std.testing.expectEqual(false, try poll(slave));
try std.testing.expectEqual(12, try writer_m.write("test message"));
try std.testing.expectEqual(true, try poll(&slave));
try std.testing.expectEqual(true, try poll(slave));

var buffer: [16]u8 = undefined;
try std.testing.expectEqual(12, try reader_s.read(&buffer));
try std.testing.expectEqualSlices(u8, "test message", buffer[0..12]);
try std.testing.expectEqual(false, try poll(&slave));
try std.testing.expectEqual(false, try poll(slave));

try std.testing.expectEqual(12, try writer_m.write("test message"));
try std.testing.expectEqual(true, try poll(&slave));
try std.testing.expectEqual(true, try poll(slave));

var small_buffer: [8]u8 = undefined;
try std.testing.expectEqual(8, try reader_s.read(&small_buffer));
try std.testing.expectEqualSlices(u8, "test mes", &small_buffer);
try std.testing.expectEqual(true, try poll(&slave));
try std.testing.expectEqual(true, try poll(slave));
try std.testing.expectEqual(4, try reader_s.read(&small_buffer));
try std.testing.expectEqualSlices(u8, "sage", small_buffer[0..4]);
try std.testing.expectEqual(false, try poll(&slave));
try std.testing.expectEqual(false, try poll(slave));

try std.testing.expectEqual(12, try writer_m.write("test message"));
try std.testing.expectEqual(true, try poll(&slave));
try flush(&slave, .{ .input = true });
try std.testing.expectEqual(false, try poll(&slave));
try std.testing.expectEqual(true, try poll(slave));
try flush(slave, .{ .input = true });
try std.testing.expectEqual(false, try poll(slave));
}

test {
var master: PortImpl = undefined;
var slave: PortImpl = undefined;
try openVirtualPorts(&master, &slave);
defer close(&master);
defer close(&slave);
defer close(master);
defer close(slave);

const config: serialport.Config = .{ .baud_rate = .B115200 };
try configure(&master, config);
try configure(&slave, config);
try configure(master, config);
try configure(slave, config);

const writer_m = writer(&master);
const reader_s = reader(&slave);
const writer_m = writer(master);
const reader_s = reader(slave);

try std.testing.expectEqual(false, try poll(&slave));
try std.testing.expectEqual(false, try poll(slave));
try std.testing.expectEqual(12, try writer_m.write("test message"));
try std.testing.expectEqual(true, try poll(&slave));
try std.testing.expectEqual(true, try poll(slave));

var buffer: [16]u8 = undefined;
try std.testing.expectEqual(12, try reader_s.read(&buffer));
try std.testing.expectEqualSlices(u8, "test message", buffer[0..12]);
try std.testing.expectEqual(false, try poll(&slave));
try std.testing.expectEqual(false, try poll(slave));

try std.testing.expectEqual(12, try writer_m.write("test message"));
try std.testing.expectEqual(true, try poll(&slave));
try std.testing.expectEqual(true, try poll(slave));

var small_buffer: [8]u8 = undefined;
try std.testing.expectEqual(8, try reader_s.read(&small_buffer));
try std.testing.expectEqualSlices(u8, "test mes", &small_buffer);
try std.testing.expectEqual(true, try poll(&slave));
try std.testing.expectEqual(true, try poll(slave));
try std.testing.expectEqual(4, try reader_s.read(&small_buffer));
try std.testing.expectEqualSlices(u8, "sage", small_buffer[0..4]);
try std.testing.expectEqual(false, try poll(&slave));
try std.testing.expectEqual(false, try poll(slave));

try std.testing.expectEqual(12, try writer_m.write("test message"));
try std.testing.expectEqual(true, try poll(&slave));
try flush(&slave, .{ .input = true });
try std.testing.expectEqual(false, try poll(&slave));
try std.testing.expectEqual(true, try poll(slave));
try flush(slave, .{ .input = true });
try std.testing.expectEqual(false, try poll(slave));
}
15 changes: 6 additions & 9 deletions src/backend/macos.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub fn open(path: []const u8) !PortImpl {
});
}

pub fn close(port: *const PortImpl) void {
pub fn close(port: PortImpl) void {
port.close();
}

pub fn configure(port: *const PortImpl, config: serialport.Config) !void {
pub fn configure(port: PortImpl, config: serialport.Config) !void {
var settings = try std.posix.tcgetattr(port.handle);

settings.iflag = .{};
Expand Down Expand Up @@ -67,10 +67,7 @@ pub fn configure(port: *const PortImpl, config: serialport.Config) !void {
try std.posix.tcsetattr(port.handle, .NOW, settings);
}

pub fn flush(
port: *const PortImpl,
options: serialport.Port.FlushOptions,
) !void {
pub fn flush(port: PortImpl, options: serialport.Port.FlushOptions) !void {
if (!options.input and !options.output) return;
const result = c.tcflush(
port.handle,
Expand All @@ -89,7 +86,7 @@ pub fn flush(
};
}

pub fn poll(port: *const PortImpl) !bool {
pub fn poll(port: PortImpl) !bool {
var pollfds: [1]std.posix.pollfd = .{
.{
.fd = port.handle,
Expand All @@ -106,11 +103,11 @@ pub fn poll(port: *const PortImpl) !bool {
return true;
}

pub fn reader(port: *const PortImpl) Reader {
pub fn reader(port: PortImpl) Reader {
return port.reader();
}

pub fn writer(port: *const PortImpl) Writer {
pub fn writer(port: PortImpl) Writer {
return port.writer();
}

Expand Down
48 changes: 42 additions & 6 deletions src/serialport.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,63 @@ pub const Port = struct {
};

pub fn close(self: *@This()) void {
backend.close(&self._impl);
const fnti = @typeInfo(@TypeOf(backend.close)).@"fn";
const param_type = @typeInfo(fnti.params[0].type.?);
switch (comptime param_type) {
.pointer => backend.close(&self._impl),
.@"struct" => backend.close(self._impl),
else => @compileError("invalid function signature"),
}
}

pub fn configure(self: *@This(), config: Config) !void {
return backend.configure(&self._impl, config);
const fnti = @typeInfo(@TypeOf(backend.configure)).@"fn";
const param_type = @typeInfo(fnti.params[0].type.?);
return switch (comptime param_type) {
.pointer => backend.configure(&self._impl, config),
.@"struct" => backend.configure(self._impl, config),
else => @compileError("invalid function signature"),
};
}

pub fn flush(self: *@This(), options: FlushOptions) !void {
return backend.flush(&self._impl, options);
const fnti = @typeInfo(@TypeOf(backend.flush)).@"fn";
const param_type = @typeInfo(fnti.params[0].type.?);
return switch (comptime param_type) {
.pointer => backend.flush(&self._impl, options),
.@"struct" => backend.flush(self._impl, options),
else => @compileError("invalid function signature"),
};
}

pub fn poll(self: *@This()) !bool {
return backend.poll(&self._impl);
const fnti = @typeInfo(@TypeOf(backend.poll)).@"fn";
const param_type = @typeInfo(fnti.params[0].type.?);
return switch (comptime param_type) {
.pointer => backend.poll(&self._impl),
.@"struct" => backend.poll(self._impl),
else => @compileError("invalid function signature"),
};
}

pub fn reader(self: *@This()) Reader {
return backend.reader(&self._impl);
const fnti = @typeInfo(@TypeOf(backend.reader)).@"fn";
const param_type = @typeInfo(fnti.params[0].type.?);
return switch (comptime param_type) {
.pointer => backend.reader(&self._impl),
.@"struct" => backend.reader(self._impl),
else => @compileError("invalid function signature"),
};
}

pub fn writer(self: *@This()) Writer {
return backend.writer(&self._impl);
const fnti = @typeInfo(@TypeOf(backend.writer)).@"fn";
const param_type = @typeInfo(fnti.params[0].type.?);
return switch (comptime param_type) {
.pointer => backend.writer(&self._impl),
.@"struct" => backend.writer(self._impl),
else => @compileError("invalid function signature"),
};
}
};

Expand Down

0 comments on commit 3ca6381

Please sign in to comment.