Skip to content

Commit d0ac5a9

Browse files
committed
Implement Get_Error_Options_And_Payload and fix some failing verification conditions
1 parent b8e9097 commit d0ac5a9

5 files changed

Lines changed: 99 additions & 6 deletions

File tree

alire.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This version implements the client side of the protocol with some limitations:
1313
1414
See LICENSING for licensing information.
1515
"""
16-
version = "0.9.0"
16+
version = "0.10.0-dev"
1717

1818
authors = ["Manuel Gomez"]
1919
maintainers = ["Manuel Gomez <mgrojo@gmail.com>"]

src/coap_spark-messages.adb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
with Ada.Unchecked_Conversion;
12

23
with CoAP_SPARK.Content_Formats;
34
with CoAP_SPARK.Options.Text_IO;
@@ -69,6 +70,26 @@ is
6970
& Detail_Image;
7071
end Image;
7172

73+
procedure Initialize_With_Text_Payload
74+
(Text : String;
75+
Item : out Content)
76+
is
77+
use type RFLX.RFLX_Types.Index;
78+
function To_Byte
79+
is new Ada.Unchecked_Conversion (Character, RFLX.RFLX_Types.Byte);
80+
begin
81+
Item.Options := CoAP_SPARK.Options.Lists.Empty_Vector;
82+
Item.Format := CoAP_SPARK.Content_Formats.text.plain_charset_utf_8;
83+
84+
Item.Payload :=
85+
new RFLX.RFLX_Types.Bytes'([1 .. Text'Length => 0]);
86+
87+
for I in Item.Payload.all'Range loop
88+
Item.Payload (I) :=
89+
To_Byte (Text (Text'First - 1 + Integer (I)));
90+
end loop;
91+
end Initialize_With_Text_Payload;
92+
7293
procedure Finalize (Item : in out Content)
7394
is
7495
begin

src/coap_spark-messages.ads

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ is
5656
is (CoAP_SPARK.Options.Lists.Is_Empty (Item.Options)
5757
and then Item.Payload = null);
5858

59+
-- Set the payload of Item to Text and set the appropriate content format.
60+
-- The payload is freed if it is not null.
61+
procedure Initialize_With_Text_Payload
62+
(Text : String;
63+
Item : out Content)
64+
with
65+
Pre => Text'Length <= CoAP_SPARK.Max_Payload_Length,
66+
Post => not Is_Empty (Item) and then
67+
Item.Payload'Length = Text'Length;
68+
5969
procedure Finalize (Item : in out Content)
6070
with
6171
Post => Is_Empty (Item);

src/rflx-coap_client-session.adb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ is
7373
is
7474
begin
7575

76-
RFLX_Result := (Length => 0, Options_And_Payload => [others => 0]);
77-
7876
CoAP_SPARK.Messages.Encoding.Encode_Options_And_Payload
7977
(Options_And_Payload => State.Request_Content,
8078
Status => CoAP_SPARK.Status_Type (State.Current_Status),
@@ -92,6 +90,11 @@ is
9290
use type CoAP_Client.Session_Environment.Status_Type;
9391
begin
9492

93+
if not CoAP_SPARK.Messages.Is_Empty (State.Response_Content) then
94+
CoAP_SPARK.Messages.Finalize (State.Response_Content);
95+
pragma Assert (CoAP_SPARK.Messages.Is_Empty (State.Response_Content));
96+
end if;
97+
9598
CoAP_SPARK.Messages.Encoding.Decode_Options_And_Payload
9699
(Data => Data,
97100
Status => CoAP_SPARK.Status_Type (State.Current_Status),

src/rflx-coap_server-main_loop.adb

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ is
3434

3535
begin
3636

37+
RFLX_Result :=
38+
(Message_Class => RFLX.CoAP.Server_Error,
39+
Success_Code => RFLX.CoAP.Success_Response'Last,
40+
Client_Error_Code => RFLX.CoAP.Client_Error_Response'Last,
41+
Server_Error_Code => RFLX.CoAP.Internal_Server_Error,
42+
Padding => 0,
43+
Options_And_Payload_Options_And_Payload => [others => 0],
44+
Options_And_Payload_Length => 0);
45+
3746
RFLX.CoAP.CoAP_Message.Initialize
3847
(Ctx => Context,
3948
Buffer => Buffer,
@@ -74,7 +83,6 @@ is
7483
RFLX.CoAP.Client_Error_Response'Last;
7584
RFLX_Result.Server_Error_Code :=
7685
RFLX.CoAP.Server_Error_Response'Last;
77-
RFLX_Result.Padding := 0;
7886
RFLX_Result.Message_Class := Response_Codes.Code_Class;
7987

8088
case CoAP_SPARK.Messages.Response_Code (Response_Codes.Code_Class)
@@ -97,13 +105,64 @@ is
97105
end if;
98106
end if;
99107

108+
RFLX.CoAP.CoAP_Message.Take_Buffer
109+
(Ctx => Context,
110+
Buffer => Buffer);
111+
pragma Assert (not RFLX.CoAP.CoAP_Message.Has_Buffer (Context));
112+
113+
RFLX.RFLX_Types.Free (Buffer);
100114
end Get_Response;
101115

102116
procedure Get_Error_Options_And_Payload
103117
(State : in out RFLX.CoAP_Server.Main_Loop_Environment.State;
104-
RFLX_Result : out RFLX.CoAP_Server.Options_And_Payload_Data.Structure) is
118+
RFLX_Result : out RFLX.CoAP_Server.Options_And_Payload_Data.Structure)
119+
is
120+
use type CoAP_SPARK.Status_Type;
121+
122+
Status_Image : constant String :=
123+
CoAP_SPARK.Status_Type'Image (State.Current_Status);
124+
Buffer : RFLX_Types.Bytes_Ptr :=
125+
new RFLX_Types.Bytes'([1 .. RFLX_Result.Options_And_Payload'Length => 0]);
126+
Context : RFLX.CoAP.CoAP_Message.Context;
127+
128+
Response_Content : CoAP_SPARK.Messages.Content;
129+
105130
begin
106-
null;
131+
RFLX.CoAP.CoAP_Message.Initialize
132+
(Ctx => Context,
133+
Buffer => Buffer);
134+
135+
-- This procedure is only called when there is an error in the
136+
-- main loop state machine, so if we haven't set an error status yet,
137+
-- we have to set it to Unexpected_Case, since the cause is unknown.
138+
if State.Current_Status = CoAP_SPARK.OK or else
139+
Status_Image'Length > CoAP_SPARK.Max_Payload_Length
140+
then
141+
CoAP_SPARK.Messages.Initialize_With_Text_Payload
142+
(Text => "Unexpected case in main loop",
143+
Item => Response_Content);
144+
else
145+
CoAP_SPARK.Messages.Initialize_With_Text_Payload
146+
(Text => Status_Image,
147+
Item => Response_Content);
148+
end if;
149+
150+
CoAP_SPARK.Messages.Encoding.Encode_Options_And_Payload
151+
(Options_And_Payload => Response_Content,
152+
Status => State.Current_Status,
153+
Encoded_Data => RFLX_Result.Options_And_Payload,
154+
Encoded_Length => RFLX.CoAP.Length_16
155+
(RFLX_Result.Length));
156+
157+
CoAP_SPARK.Messages.Finalize (Response_Content);
158+
pragma Assert (CoAP_SPARK.Messages.Is_Empty (Response_Content));
159+
160+
RFLX.CoAP.CoAP_Message.Take_Buffer
161+
(Ctx => Context,
162+
Buffer => Buffer);
163+
pragma Assert (not RFLX.CoAP.CoAP_Message.Has_Buffer (Context));
164+
165+
RFLX.RFLX_Types.Free (Buffer);
107166
end Get_Error_Options_And_Payload;
108167

109168
end RFLX.CoAP_Server.Main_Loop;

0 commit comments

Comments
 (0)