Skip to content

Commit

Permalink
Add Obj_Value_Field type for ObjValue object types, Set default value…
Browse files Browse the repository at this point in the history
…s for tagged records, Replace Tag_To_Field_Number with Tag_To_OBJ_Value_Field and Tag_To_CDAP_Field, Add CDAPMessage.Put procedure for debug printing, Remove some old debug prints
  • Loading branch information
masonticehurst committed Feb 5, 2024
1 parent 7956575 commit 618ddb8
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 75 deletions.
3 changes: 0 additions & 3 deletions eRINA_Linux/src/buffers.adb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ package body Buffers is
return Str;
end ToHex;
begin
Ada.Text_IO.New_Line;
Ada.Text_IO.Put ("Byte Stream: ");

for Byte of Input loop
Ada.Text_IO.Put (ToHex (Byte) & " ");
end loop;
Expand Down
305 changes: 284 additions & 21 deletions eRINA_STM32F7/src/net/cdap.adb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Protobuf; use Protobuf;
with Interfaces; use Interfaces;

package body CDAP is

Expand Down Expand Up @@ -29,12 +31,55 @@ package body CDAP is
end Set_Field;

procedure Set_Field
(Self : in out CDAPMessage; Field : CDAP_Field; Val : Byte_Buffer)
(Self : in out Obj_Value; Field : Obj_Value_Field; Val : Uint64) is
begin
case Field is
when Intval =>
Self.Intval := Uint32 (Val);
when Sintval =>
Self.Sintval := Int32 (Val);
when Int_64val =>
Self.Int_64val := Val;
when Sint_64val =>
Self.Sint_64val := Int64 (Val);
when Floatval =>
Self.Floatval := Short_Float (Val);
when Doubleval =>
Self.Doubleval := Long_Long_Float (Val);
when others =>
-- MT: TODO: Should we make this throw an exception?
null;
end case;
end Set_Field;

procedure Set_Field
(Self : in out Obj_Value; Field : Obj_Value_Field; Val : Byte_Vector)
is
Final_String : constant String := Buffer_To_String (Byte_Vector_To_Buffer (Val));
Final_String_Unbounded : constant Unbounded_String :=
To_Unbounded_String (Final_String);
begin

if Field = Strval then
Self.Strval := Final_String_Unbounded;
elsif Field = Byteval then
Self.Byteval := Val;
end if;

end Set_Field;

procedure Set_Field
(Self : in out CDAPMessage; Field : CDAP_Field; Val : Byte_Vector)
is
Final_String : constant String := Buffer_To_String (Val);
Final_String : constant String := Buffer_To_String (Byte_Vector_To_Buffer (Val));
Final_String_Unbounded : constant Unbounded_String :=
To_Unbounded_String (Final_String);
begin

if Field = ObjValue then
Self.ObjValue := To_OBJ_Value (Val);
end if;

case Field is
when Obj_Class =>
Self.Obj_Class := Final_String_Unbounded;
Expand Down Expand Up @@ -81,30 +126,242 @@ package body CDAP is
end Set_Field;

procedure Set_Field
(Self : in out CDAPMessage; Field : CDAP_Field; Val : Obj_Value)
(Self : in out CDAPMessage; Field : CDAP_Field; Val : Auth_Value)
is
begin
null;
end Set_Field;

procedure Set_Field
(Self : in out CDAPMessage; Field : CDAP_Field; Val : Auth_Value)
is
(Self : in out Obj_Value; Field : Obj_Value_Field; Val : Unbounded_String) is
begin
null;
end Set_Field;

function Tag_To_Field_Number (Input : Byte) return CDAP_Field is
-- Drop most significant bit and shift right 3 times
Value : constant Byte := Shift_Right (Input and 2#0111_1111#, 3);
procedure Set_Field
(Self : in out Obj_Value; Field : Obj_Value_Field; Val : Boolean) is
begin
-- MT: TODO: Debug only! Remove Me!
Put_Line
("Decoded tagged field :: " & CDAP_Field'Enum_Val (Value)'Image);
null;
end Set_Field;

-- MT: TODO: I really don't like the code duplication here, optimize me later
function To_OBJ_Value (V : in Byte_Vector) return Obj_Value is
ObjValue : Obj_Value;
Wire_Type : Wire;
Field_Id : Obj_Value_Field;
Is_Tag_Field : Boolean := True;

C : Byte_Vectors.Cursor := V.First;
use type Byte_Vectors.Cursor;
begin
while C /= Byte_Vectors.No_Element loop
if Is_Tag_Field then
declare
Tag_Vector : Byte_Vector;
begin
while C /= Byte_Vectors.No_Element loop
Tag_Vector.Append (V(C));
exit when (not Has_MSB (V(C)));
C := Byte_Vectors.Next (C);
end loop;

Field_Id := Tag_To_OBJ_Value_Field (Tag_Vector);
Wire_Type := Tag_To_Wire_Type (V (C));
end;

Is_Tag_Field := False;
else
if Wire_Type = VARINT then
declare
VARINT_Vector : Byte_Vector;
begin
-- Keep reading bytes until we no longer have a MSB of 1
while C /= Byte_Vectors.No_Element loop
VARINT_Vector.Append (V (C));
exit when (not Has_MSB (V (C)));
C := Byte_Vectors.Next (C);
end loop;

-- Decode and update message
ObjValue.Set_Field (Field_Id, To_VARINT (VARINT_Vector));
end;
end if;

if Wire_Type = LEN then
declare
LEN_Vector : Byte_Vector;
Data_Vector : Byte_Vector;
LEN_Length : Natural := 0;
LEN_Iterator : Natural := 0;
begin
while C /= Byte_Vectors.No_Element loop
LEN_Vector.Append (V (C));
C := Byte_Vectors.Next (C);
exit when (not Has_MSB (V (C)));
end loop;

-- The VARINT storing the length is an int32
LEN_Length := Natural (To_VARINT (LEN_Vector));

while C /= Byte_Vectors.No_Element loop
Data_Vector.Append (V (C));
LEN_Iterator := LEN_Iterator + 1;
exit when (LEN_Iterator = LEN_Length);
C := Byte_Vectors.Next (C);
end loop;

ObjValue.Set_Field (Field_Id, Data_Vector);
end;
end if;
end if;

C := Byte_Vectors.Next (C);
end loop;

return ObjValue;
end To_OBJ_Value;

function Tag_To_OBJ_Value_Field (Input : Byte_Vector) return Obj_Value_Field is
Value : constant Uint64 := To_VARINT(Input) / 2 ** 3;
begin
-- MT: TODO: Need to handle weird case when resulting value does not match an enum in Obj_Value
return Obj_Value_Field'Enum_Val (Value);
end Tag_To_OBJ_Value_Field;

function Tag_To_CDAP_Field (Input : Byte_Vector) return CDAP_Field is
Value : constant Uint64 := To_VARINT(Input) / 2 ** 3;
begin
-- MT: TODO: Need to handle weird case when resulting value does not match an enum in CDAP_Field
return CDAP_Field'Enum_Val (Value);
end Tag_To_Field_Number;
end Tag_To_CDAP_Field;

procedure Put (Self : CDAPMessage) is begin
Put_Line ("Message Contents:");

-- Abs_Syntax
Put (Head ("Abs_Syntax:", 15, ' '));
Put_Line (Self.Abs_Syntax'Image);

-- OpCode
Put (Head ("OpCode:", 16, ' '));
Put_Line (Self.OpCode'Image);

-- Invoke_Id
Put (Head ("Invoke_Id:", 15, ' '));
Put_Line (Self.Invoke_Id'Image);

-- Flags
Put (Head ("Flags:", 16, ' '));
Put_Line (Self.Flags'Image);

-- Obj_Class
Put (Head ("Obj_Class:", 16, ' '));
Put_Line ("'" & To_String(Self.Obj_Class));

-- Obj_Name
Put (Head ("Obj_Name:", 16, ' '));
Put_Line ("'" & To_String(Self.Obj_Name));

-- Obj_Inst
Put (Head ("Obj_Inst:", 15, ' '));
Put_Line (Self.Obj_Inst'Image);

-- Obj_Value
Put_Line ("Obj_Value:");

-- Obj_Value.IntVal
Put (Head(" IntVal:", 15, ' '));
Put_Line (Self.ObjValue.Intval'Image);

-- Obj_Value.IntVal
Put (Head(" Sintval:", 15, ' '));
Put_Line (Self.ObjValue.Sintval'Image);

-- Obj_Value.Int_64val
Put (Head(" Int_64val:", 15, ' '));
Put_Line (Self.ObjValue.Int_64val'Image);

-- Obj_Value.Sint_64val
Put (Head(" Sint_64val:", 15, ' '));
Put_Line (Self.ObjValue.Sint_64val'Image);

-- Obj_Value.Strval
Put (Head(" Strval:", 16, ' '));
Put_Line ("'" & To_String(Self.ObjValue.Strval) & "'");

-- Obj_Value.Byteval
Put (Head(" Byteval:", 16, ' '));
Put_Bytes(Byte_Vector_To_Buffer (Self.ObjValue.Byteval));

-- Obj_Value.Floatval
Put (Head(" Floatval:", 15, ' '));
Put_Line (Self.ObjValue.Floatval'Image);

-- Obj_Value.Doubleval
Put (Head(" Doubleval:", 15, ' '));
Put_Line (Self.ObjValue.Doubleval'Image);

-- Obj_Value.Boolval
Put (Head(" Boolval:", 16, ' '));
Put_Line (Self.ObjValue.Boolval'Image);

-- Result
Put (Head ("Result:", 15, ' '));
Put_Line (Self.Result'Image);

-- Scope
Put (Head ("Scope:", 15, ' '));
Put_Line (Self.Scope'Image);

-- Filter
Put (Head ("Filter:", 15, ' '));
Put_Line (Self.Filter'Image);

-- Auth_Mech
Put (Head ("Auth_Mech:", 16, ' '));
Put_Line ("'" & To_String(Self.Auth_Mech) & "'");

-- Dest_Ae_Inst
Put (Head ("Dest_Ae_Inst:", 16, ' '));
Put_Line ("'" & To_String(Self.Dest_Ae_Inst) & "'");

-- Dest_Ae_Name
Put (Head ("Dest_Ae_Name:", 16, ' '));
Put_Line ("'" & To_String(Self.Dest_Ae_Name & "'"));

-- Dest_Ap_Inst
Put (Head ("Dest_Ap_Inst:", 16, ' '));
Put_Line ("'" & To_String(Self.Dest_Ap_Inst & "'"));

-- Dest_Ap_Name
Put (Head ("Dest_Ap_Name:", 16, ' '));
Put_Line ("'" & To_String(Self.Dest_Ap_Name & "'"));

-- Src_Ae_Inst
Put (Head ("Src_Ae_Inst:", 16, ' '));
Put_Line ("'" & To_String(Self.Src_Ae_Inst & "'"));

-- Src_Ae_Name
Put (Head ("Src_Ae_Name:", 16, ' '));
Put_Line ("'" & To_String(Self.Src_Ae_Name & "'"));

-- Src_Ap_Inst
Put (Head ("Src_Ap_Inst:", 16, ' '));
Put_Line ("'" & To_String(Self.Src_Ap_Inst & "'"));

-- Src_Ap_Name
Put (Head ("Src_Ap_Name:", 16, ' '));
Put_Line ("'" & To_String(Self.Src_Ap_Name & "'"));

-- Result_Reason
Put (Head ("Result_Reason:", 16, ' '));
Put_Line ("'" & To_String(Self.Result_Reason & "'"));

-- Result_Reason
Put (Head ("Result_Reason:", 15, ' '));
Put_Line (Self.Version'Image);

end Put;

function To_CDAP (V : in Byte_Vector) return CDAPMessage is
Result_Msg : CDAPMessage;
Expand All @@ -118,9 +375,20 @@ package body CDAP is
begin
while C /= Byte_Vectors.No_Element loop
if Is_Tag_Field then
Wire_Type := Tag_To_Wire_Type (V (C));
Field_Id := Tag_To_Field_Number (V (C));
Is_Tag_Field := False;
declare
Tag_Vector : Byte_Vector;
begin
while C /= Byte_Vectors.No_Element loop
Tag_Vector.Append (V(C));
exit when (not Has_MSB (V(C)));
C := Byte_Vectors.Next (C);
end loop;

Field_Id := Tag_To_CDAP_Field (Tag_Vector);
Wire_Type := Tag_To_Wire_Type (V (C));
end;

Is_Tag_Field := False;
else
if Wire_Type = VARINT then
declare
Expand Down Expand Up @@ -164,12 +432,7 @@ package body CDAP is
C := Byte_Vectors.Next (C);
end loop;

Put_Line
("Decoded LEN of size (" & LEN_Length'Image &
") to be :: " &
Buffer_To_String (Byte_Vector_To_Buffer (Data_Vector)));
Result_Msg.Set_Field
(Field_Id, Byte_Vector_To_Buffer (Data_Vector));
Result_Msg.Set_Field (Field_Id, Data_Vector);
end;
end if;

Expand Down
Loading

0 comments on commit 618ddb8

Please sign in to comment.