Skip to content

Commit 580ee2a

Browse files
sddf: cleanup begin string sort of things
Signed-off-by: Ivan-Velickovic <i.velickovic@unsw.edu.au>
1 parent 4e67fe7 commit 580ee2a

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

src/c/c.zig

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,18 @@ export fn sdfgen_sddf_timer_serialise_config(system: *align(8) anyopaque, output
457457
return true;
458458
}
459459

460-
export fn sdfgen_sddf_serial(c_sdf: *align(8) anyopaque, c_device: ?*align(8) anyopaque, driver: *align(8) anyopaque, virt_tx: *align(8) anyopaque, virt_rx: ?*align(8) anyopaque, enable_color: bool) ?*anyopaque {
460+
export fn sdfgen_sddf_serial(c_sdf: *align(8) anyopaque, c_device: ?*align(8) anyopaque, driver: *align(8) anyopaque, virt_tx: *align(8) anyopaque, virt_rx: ?*align(8) anyopaque, enable_color: bool, begin_str: [*c]u8) ?*anyopaque {
461461
const sdf: *SystemDescription = @ptrCast(c_sdf);
462462
const device: *dtb.Node = @ptrCast(c_device);
463+
var options: sddf.Serial.Options = .{
464+
.virt_rx = @ptrCast(virt_rx),
465+
.enable_color = enable_color,
466+
};
467+
if (begin_str != null) {
468+
options.begin_str = std.mem.span(begin_str);
469+
}
463470
const serial = allocator.create(sddf.Serial) catch @panic("OOM");
464-
serial.* = sddf.Serial.init(allocator, sdf, device, @ptrCast(driver), @ptrCast(virt_tx), .{ .virt_rx = @ptrCast(virt_rx), .enable_color = enable_color }) catch |e| {
471+
serial.* = sddf.Serial.init(allocator, sdf, device, @ptrCast(driver), @ptrCast(virt_tx), options) catch |e| {
465472
log.err("failed to initialiase serial system for device '{s}': {any}", .{ device.name, e });
466473
allocator.destroy(serial);
467474
return null;
@@ -483,7 +490,7 @@ export fn sdfgen_sddf_serial_add_client(system: *align(8) anyopaque, client: *al
483490
sddf.Serial.Error.DuplicateClient => return 1,
484491
sddf.Serial.Error.InvalidClient => return 2,
485492
// Should never happen when adding a client
486-
sddf.Serial.Error.InvalidVirt, sddf.Serial.Error.NotConnected => @panic("internal error"),
493+
sddf.Serial.Error.InvalidBeginString, sddf.Serial.Error.InvalidVirt, sddf.Serial.Error.NotConnected => @panic("internal error"),
487494
}
488495
};
489496

src/data.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ pub const Resources = struct {
171171
clients: [MAX_NUM_CLIENTS]VirtTxClient,
172172
num_clients: u8,
173173
begin_str: [MAX_BEGIN_STR_LEN]u8,
174-
begin_str_len: u8,
175174
enable_colour: u8,
176175
enable_rx: u8,
177176
};

src/python/module.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class SddfStatus(IntEnum):
169169
libsdfgen.sdfgen_sddf_blk_serialise_config.argtypes = [c_void_p, c_char_p]
170170

171171
libsdfgen.sdfgen_sddf_serial.restype = c_void_p
172-
libsdfgen.sdfgen_sddf_serial.argtypes = [c_void_p, c_void_p, c_void_p, c_void_p, c_void_p, c_bool]
172+
libsdfgen.sdfgen_sddf_serial.argtypes = [c_void_p, c_void_p, c_void_p, c_void_p, c_void_p, c_bool, c_char_p]
173173
libsdfgen.sdfgen_sddf_serial_destroy.restype = None
174174
libsdfgen.sdfgen_sddf_serial_destroy.argtypes = [c_void_p]
175175

@@ -719,7 +719,8 @@ def __init__(
719719
virt_tx: SystemDescription.ProtectionDomain,
720720
*,
721721
virt_rx: Optional[SystemDescription.ProtectionDomain] = None,
722-
enable_color: bool = True
722+
enable_color: bool = True,
723+
begin_str: Optional[str] = None,
723724
) -> None:
724725
if device is None:
725726
device_obj = None
@@ -731,8 +732,12 @@ def __init__(
731732
else:
732733
virt_rx_obj = virt_rx._obj
733734

735+
if begin_str:
736+
c_begin_str = c_char_p(begin_str.encode("utf-8"))
737+
else:
738+
c_begin_str = None
734739
self._obj = libsdfgen.sdfgen_sddf_serial(
735-
sdf._obj, device_obj, driver._obj, virt_tx._obj, virt_rx_obj, c_bool(enable_color)
740+
sdf._obj, device_obj, driver._obj, virt_tx._obj, virt_rx_obj, c_bool(enable_color), c_begin_str
736741
)
737742
if self._obj is None:
738743
raise Exception("failed to create serial system")

src/sddf.zig

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ pub const Serial = struct {
913913
connected: bool = false,
914914
enable_color: bool,
915915
serialised: bool = false,
916+
begin_str: [:0]const u8,
916917

917918
driver_config: ConfigResources.Serial.Driver,
918919
virt_rx_config: ConfigResources.Serial.VirtRx,
@@ -921,20 +922,26 @@ pub const Serial = struct {
921922

922923
pub const Error = SystemError || error{
923924
InvalidVirt,
925+
InvalidBeginString,
924926
};
925927

928+
const MAX_BEGIN_STR_LEN = 128;
929+
const DEFAULT_BEGIN_STR = "Begin input\r\n";
930+
926931
pub const Options = struct {
927932
data_size: usize = 0x10000,
928933
queue_size: usize = 0x1000,
929934
virt_rx: ?*Pd = null,
930935
enable_color: bool = true,
936+
begin_str: [:0]const u8 = DEFAULT_BEGIN_STR,
931937
};
932938

933939
pub fn init(allocator: Allocator, sdf: *SystemDescription, device: *dtb.Node, driver: *Pd, virt_tx: *Pd, options: Options) Error!Serial {
934940
if (std.mem.eql(u8, driver.name, virt_tx.name)) {
935941
log.err("invalid serial tx virtualiser, same name as driver '{s}", .{virt_tx.name});
936942
return Error.InvalidVirt;
937943
}
944+
938945
if (options.virt_rx) |virt_rx| {
939946
if (std.mem.eql(u8, driver.name, virt_rx.name)) {
940947
log.err("invalid serial rx virtualiser, same name as driver '{s}", .{virt_rx.name});
@@ -945,6 +952,12 @@ pub const Serial = struct {
945952
return Error.InvalidVirt;
946953
}
947954
}
955+
956+
if (options.begin_str.len > MAX_BEGIN_STR_LEN) {
957+
log.err("invalid begin string '{s}', length of {} is greater than max length {}", .{ options.begin_str, options.begin_str.len, MAX_BEGIN_STR_LEN });
958+
return error.InvalidBeginString;
959+
}
960+
948961
return .{
949962
.allocator = allocator,
950963
.sdf = sdf,
@@ -957,6 +970,7 @@ pub const Serial = struct {
957970
.virt_rx = options.virt_rx,
958971
.virt_tx = virt_tx,
959972
.enable_color = options.enable_color,
973+
.begin_str = allocator.dupeZ(u8, options.begin_str) catch @panic("OOM"),
960974

961975
.driver_config = std.mem.zeroInit(ConfigResources.Serial.Driver, .{}),
962976
.virt_rx_config = std.mem.zeroInit(ConfigResources.Serial.VirtRx, .{}),
@@ -968,6 +982,7 @@ pub const Serial = struct {
968982
pub fn deinit(system: *Serial) void {
969983
system.clients.deinit();
970984
system.client_configs.deinit();
985+
system.allocator.free(system.begin_str);
971986
}
972987

973988
pub fn addClient(system: *Serial, client: *Pd) Error!void {
@@ -1052,11 +1067,8 @@ pub const Serial = struct {
10521067

10531068
system.virt_tx_config.enable_colour = @intFromBool(system.enable_color);
10541069

1055-
const begin_str = "Begin input\n";
1056-
@memcpy(system.virt_tx_config.begin_str[0..begin_str.len], begin_str);
1057-
assert(system.virt_tx_config.begin_str[begin_str.len] == 0);
1058-
1059-
system.virt_tx_config.begin_str_len = begin_str.len;
1070+
@memcpy(system.virt_tx_config.begin_str[0..system.begin_str.len], system.begin_str);
1071+
assert(system.virt_tx_config.begin_str[system.begin_str.len] == 0);
10601072

10611073
system.connected = true;
10621074
}

0 commit comments

Comments
 (0)