@@ -913,6 +913,7 @@ pub const Serial = struct {
913
913
connected : bool = false ,
914
914
enable_color : bool ,
915
915
serialised : bool = false ,
916
+ begin_str : [:0 ]const u8 ,
916
917
917
918
driver_config : ConfigResources.Serial.Driver ,
918
919
virt_rx_config : ConfigResources.Serial.VirtRx ,
@@ -921,20 +922,26 @@ pub const Serial = struct {
921
922
922
923
pub const Error = SystemError || error {
923
924
InvalidVirt ,
925
+ InvalidBeginString ,
924
926
};
925
927
928
+ const MAX_BEGIN_STR_LEN = 128 ;
929
+ const DEFAULT_BEGIN_STR = "Begin input\r \n " ;
930
+
926
931
pub const Options = struct {
927
932
data_size : usize = 0x10000 ,
928
933
queue_size : usize = 0x1000 ,
929
934
virt_rx : ? * Pd = null ,
930
935
enable_color : bool = true ,
936
+ begin_str : [:0 ]const u8 = DEFAULT_BEGIN_STR ,
931
937
};
932
938
933
939
pub fn init (allocator : Allocator , sdf : * SystemDescription , device : * dtb.Node , driver : * Pd , virt_tx : * Pd , options : Options ) Error ! Serial {
934
940
if (std .mem .eql (u8 , driver .name , virt_tx .name )) {
935
941
log .err ("invalid serial tx virtualiser, same name as driver '{s}" , .{virt_tx .name });
936
942
return Error .InvalidVirt ;
937
943
}
944
+
938
945
if (options .virt_rx ) | virt_rx | {
939
946
if (std .mem .eql (u8 , driver .name , virt_rx .name )) {
940
947
log .err ("invalid serial rx virtualiser, same name as driver '{s}" , .{virt_rx .name });
@@ -945,6 +952,12 @@ pub const Serial = struct {
945
952
return Error .InvalidVirt ;
946
953
}
947
954
}
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
+
948
961
return .{
949
962
.allocator = allocator ,
950
963
.sdf = sdf ,
@@ -957,6 +970,7 @@ pub const Serial = struct {
957
970
.virt_rx = options .virt_rx ,
958
971
.virt_tx = virt_tx ,
959
972
.enable_color = options .enable_color ,
973
+ .begin_str = allocator .dupeZ (u8 , options .begin_str ) catch @panic ("OOM" ),
960
974
961
975
.driver_config = std .mem .zeroInit (ConfigResources .Serial .Driver , .{}),
962
976
.virt_rx_config = std .mem .zeroInit (ConfigResources .Serial .VirtRx , .{}),
@@ -968,6 +982,7 @@ pub const Serial = struct {
968
982
pub fn deinit (system : * Serial ) void {
969
983
system .clients .deinit ();
970
984
system .client_configs .deinit ();
985
+ system .allocator .free (system .begin_str );
971
986
}
972
987
973
988
pub fn addClient (system : * Serial , client : * Pd ) Error ! void {
@@ -1052,11 +1067,8 @@ pub const Serial = struct {
1052
1067
1053
1068
system .virt_tx_config .enable_colour = @intFromBool (system .enable_color );
1054
1069
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 );
1060
1072
1061
1073
system .connected = true ;
1062
1074
}
0 commit comments