18
18
#include <string.h>
19
19
#include <stdlib.h>
20
20
21
+ #include "clist.h"
21
22
#include "fmt.h"
22
23
23
24
#include "congure/test.h"
24
25
26
+ static congure_snd_msg_t _msgs_pool [CONFIG_CONGURE_TEST_LOST_MSG_POOL_SIZE ];
27
+ static unsigned _msgs_pool_idx ;
28
+ static clist_node_t _msgs ;
29
+
25
30
static bool _scn_u32_dec_with_zero (const char * str , size_t n , uint32_t * res )
26
31
{
27
32
if ((n == 1 ) && str [0 ] == '0' ) {
@@ -35,10 +40,8 @@ static bool _scn_u32_dec_with_zero(const char *str, size_t n, uint32_t *res)
35
40
36
41
int congure_test_clear_state (int argc , char * * argv )
37
42
{
38
- (void )argc ;
39
- (void )argv ;
40
43
memset (congure_test_get_state (), 0 , sizeof (congure_test_snd_t ));
41
- print_str ( "{\"success\":null}\n" );
44
+ congure_test_msgs_reset ( argc , argv );
42
45
return 0 ;
43
46
}
44
47
@@ -126,6 +129,56 @@ int congure_test_call_inter_msg_interval(int argc, char **argv)
126
129
return 0 ;
127
130
}
128
131
132
+ int congure_test_add_msg (int argc , char * * argv )
133
+ {
134
+ uint32_t tmp ;
135
+
136
+ if (argc < 4 ) {
137
+ print_str ("{\"error\":\"At least 3 arguments `msg_send_time`, "
138
+ "`msg_size`, `msg_resends` expected\"}\n" );
139
+ return 1 ;
140
+ }
141
+ if (_msgs_pool_idx >= ARRAY_SIZE (_msgs_pool )) {
142
+ print_str ("{\"error\":\"List element pool depleted\"}\n" );
143
+ return 1 ;
144
+ }
145
+ _msgs_pool [_msgs_pool_idx ].super .next = NULL ;
146
+
147
+ if (!_scn_u32_dec_with_zero (argv [1 ], strlen (argv [1 ]), & tmp )) {
148
+ print_str ("{\"error\":\"`msg_send_time` expected to be "
149
+ "integer\"}\n" );
150
+ return 1 ;
151
+ }
152
+ _msgs_pool [_msgs_pool_idx ].send_time = tmp ;
153
+
154
+ if (!_scn_u32_dec_with_zero (argv [2 ], strlen (argv [2 ]), & tmp )) {
155
+ print_str ("{\"error\":\"`msg_size` expected to be integer\"}\n" );
156
+ return 1 ;
157
+ }
158
+ _msgs_pool [_msgs_pool_idx ].size = tmp ;
159
+
160
+ if (!_scn_u32_dec_with_zero (argv [3 ], strlen (argv [3 ]), & tmp )) {
161
+ print_str ("{\"error\":\"`msg_resends` expected to be "
162
+ "integer\"}\n" );
163
+ return 1 ;
164
+ }
165
+ _msgs_pool [_msgs_pool_idx ].resends = tmp ;
166
+
167
+ clist_rpush (& _msgs , & _msgs_pool [_msgs_pool_idx ++ ].super );
168
+ print_str ("{\"success\":null}\n" );
169
+ return 0 ;
170
+ }
171
+
172
+ int congure_test_msgs_reset (int argc , char * * argv )
173
+ {
174
+ (void )argc ;
175
+ (void )argv ;
176
+ _msgs .next = NULL ;
177
+ _msgs_pool_idx = 0 ;
178
+ print_str ("{\"success\":null}\n" );
179
+ return 0 ;
180
+ }
181
+
129
182
static int _call_report_msg_sent (int argc , char * * argv )
130
183
{
131
184
congure_test_snd_t * c = congure_test_get_state ();
@@ -163,56 +216,15 @@ static int _call_report_msg_discarded(int argc, char **argv)
163
216
}
164
217
165
218
static int _call_report_msgs_timeout_lost (void (* method )(congure_snd_t * ,
166
- congure_snd_msg_t * ),
167
- int argc , char * * argv )
219
+ congure_snd_msg_t * ))
168
220
{
169
- static congure_snd_msg_t list_pool [CONFIG_CONGURE_TEST_LOST_MSG_POOL_SIZE ];
170
- clist_node_t msgs = { .next = NULL };
171
221
congure_test_snd_t * c = congure_test_get_state ();
172
222
173
- if (argc < 4 ) {
174
- print_str ("{\"error\":\"At least 3 arguments `msg_send_time`, "
175
- "`msg_size`, `msg_resends` expected\"}\n" );
223
+ if (_msgs .next == NULL ) {
224
+ print_str ("{\"error\":\"Message not initialized\"}\n" );
176
225
return 1 ;
177
226
}
178
- if ((argc - 1 ) % 3 ) {
179
- print_str ("{\"error\":\"Number of arguments must be divisible "
180
- "by 3\"}\n" );
181
- return 1 ;
182
- }
183
- if ((unsigned )((argc - 1 ) / 3 ) >= ARRAY_SIZE (list_pool )) {
184
- print_str ("{\"error\":\"List element pool depleted\"}" );
185
- return 1 ;
186
- }
187
- for (int i = 1 ; i < argc ; i += 3 ) {
188
- uint32_t tmp ;
189
- unsigned pool_idx = ((i - 1 ) / 3 );
190
-
191
- list_pool [pool_idx ].super .next = NULL ;
192
-
193
- if (!_scn_u32_dec_with_zero (argv [i ], strlen (argv [i ]), & tmp )) {
194
- print_str ("{\"error\":\"`msg_send_time` expected to be "
195
- "integer\"}\n" );
196
- return 1 ;
197
- }
198
- list_pool [pool_idx ].send_time = tmp ;
199
-
200
- if (!_scn_u32_dec_with_zero (argv [i + 1 ], strlen (argv [i + 1 ]), & tmp )) {
201
- print_str ("{\"error\":\"`msg_size` expected to be integer\"}\n" );
202
- return 1 ;
203
- }
204
- list_pool [pool_idx ].size = tmp ;
205
-
206
- if (!_scn_u32_dec_with_zero (argv [i + 2 ], strlen (argv [i + 2 ]), & tmp )) {
207
- print_str ("{\"error\":\"`msg_resends` expected to be "
208
- "integer\"}\n" );
209
- return 1 ;
210
- }
211
- list_pool [pool_idx ].resends = tmp ;
212
-
213
- clist_rpush (& msgs , & list_pool [pool_idx ].super );
214
- }
215
- method (& c -> super , (congure_snd_msg_t * )msgs .next );
227
+ method (& c -> super , (congure_snd_msg_t * )_msgs .next );
216
228
print_str ("{\"success\":null}\n" );
217
229
return 0 ;
218
230
}
@@ -221,76 +233,61 @@ static int _call_report_msgs_timeout(int argc, char **argv)
221
233
{
222
234
congure_test_snd_t * c = congure_test_get_state ();
223
235
224
- return _call_report_msgs_timeout_lost (c -> super .driver -> report_msgs_timeout ,
225
- argc , argv );
236
+ (void )argc ;
237
+ (void )argv ;
238
+ return _call_report_msgs_timeout_lost (c -> super .driver -> report_msgs_timeout );
226
239
}
227
240
228
241
static int _call_report_msgs_lost (int argc , char * * argv )
229
242
{
230
243
congure_test_snd_t * c = congure_test_get_state ();
231
244
232
- return _call_report_msgs_timeout_lost (c -> super .driver -> report_msgs_lost ,
233
- argc , argv );
245
+ (void )argc ;
246
+ (void )argv ;
247
+ return _call_report_msgs_timeout_lost (c -> super .driver -> report_msgs_lost );
234
248
}
235
249
236
250
static int _call_report_msg_acked (int argc , char * * argv )
237
251
{
238
- static congure_snd_msg_t msg = { .size = 0 };
239
252
static congure_snd_ack_t ack = { .size = 0 };
240
253
congure_test_snd_t * c = congure_test_get_state ();
241
254
uint32_t tmp ;
242
255
243
- if (argc < 10 ) {
244
- print_str ("{\"error\":\"At least 9 arguments `msg_send_time`, "
245
- "`msg_size`, `msg_resends`, `ack_recv_time`, `ack_id`, "
246
- "`ack_size`, `ack_clean`, `ack_wnd`, `ack_delay` "
247
- "expected\"}\n" );
248
- return 1 ;
249
- }
250
- if (!_scn_u32_dec_with_zero (argv [1 ], strlen (argv [1 ]), & tmp )) {
251
- print_str ("{\"error\":\"`msg_send_time` expected to be "
252
- "integer\"}\n" );
253
- return 1 ;
254
- }
255
- msg .send_time = tmp ;
256
-
257
- if (!_scn_u32_dec_with_zero (argv [2 ], strlen (argv [2 ]), & tmp )) {
258
- print_str ("{\"error\":\"`msg_size` expected to be integer\"}\n" );
256
+ if (_msgs .next == NULL ) {
257
+ print_str ("{\"error\":\"Message not initialized\"}\n" );
259
258
return 1 ;
260
259
}
261
- msg . size = tmp ;
262
-
263
- if (! _scn_u32_dec_with_zero ( argv [ 3 ], strlen ( argv [ 3 ]), & tmp )) {
264
- print_str ( "{\"error\":\"`msg_resends` expected to be integer \"}\n" );
260
+ if ( argc < 7 ) {
261
+ print_str ( "{\"error\":\"At least 6 arguments `ack_recv_time`, "
262
+ "`ack_id`, `ack_size`, `ack_clean`, `ack_wnd`, `ack_delay` "
263
+ "expected \"}\n" );
265
264
return 1 ;
266
265
}
267
- msg .resends = tmp ;
268
-
269
- if (!_scn_u32_dec_with_zero (argv [4 ], strlen (argv [4 ]), & tmp )) {
266
+ if (!_scn_u32_dec_with_zero (argv [1 ], strlen (argv [1 ]), & tmp )) {
270
267
print_str ("{\"error\":\"`ack_recv_time` expected to be integer\"}\n" );
271
268
return 1 ;
272
269
}
273
270
ack .recv_time = tmp ;
274
271
275
- if (!_scn_u32_dec_with_zero (argv [5 ], strlen (argv [5 ]), & tmp )) {
272
+ if (!_scn_u32_dec_with_zero (argv [2 ], strlen (argv [2 ]), & tmp )) {
276
273
print_str ("{\"error\":\"`ack_id` expected to be integer\"}\n" );
277
274
return 1 ;
278
275
}
279
276
ack .id = tmp ;
280
277
281
- if (!_scn_u32_dec_with_zero (argv [6 ], strlen (argv [6 ]), & tmp )) {
278
+ if (!_scn_u32_dec_with_zero (argv [3 ], strlen (argv [3 ]), & tmp )) {
282
279
print_str ("{\"error\":\"`ack_size` expected to be integer\"}\n" );
283
280
return 1 ;
284
281
}
285
282
ack .size = tmp ;
286
283
287
- if (!_scn_u32_dec_with_zero (argv [7 ], strlen (argv [7 ]), & tmp )) {
284
+ if (!_scn_u32_dec_with_zero (argv [4 ], strlen (argv [4 ]), & tmp )) {
288
285
print_str ("{\"error\":\"`ack_clean` expected to be integer\"}\n" );
289
286
return 1 ;
290
287
}
291
288
ack .clean = (bool )tmp ;
292
289
293
- if (!_scn_u32_dec_with_zero (argv [8 ], strlen (argv [8 ]), & tmp )) {
290
+ if (!_scn_u32_dec_with_zero (argv [5 ], strlen (argv [5 ]), & tmp )) {
294
291
print_str ("{\"error\":\"`ack_wnd` expected to be integer\"}\n" );
295
292
return 1 ;
296
293
}
@@ -300,7 +297,7 @@ static int _call_report_msg_acked(int argc, char **argv)
300
297
}
301
298
ack .wnd = (uint16_t )tmp ;
302
299
303
- if (!_scn_u32_dec_with_zero (argv [9 ], strlen (argv [9 ]), & tmp )) {
300
+ if (!_scn_u32_dec_with_zero (argv [6 ], strlen (argv [6 ]), & tmp )) {
304
301
print_str ("{\"error\":\"`ack_delay` expected to be integer\"}\n" );
305
302
return 1 ;
306
303
}
@@ -310,7 +307,8 @@ static int _call_report_msg_acked(int argc, char **argv)
310
307
}
311
308
ack .delay = (uint16_t )tmp ;
312
309
313
- c -> super .driver -> report_msg_acked (& c -> super , & msg , & ack );
310
+ c -> super .driver -> report_msg_acked (& c -> super ,
311
+ (congure_snd_msg_t * )_msgs .next , & ack );
314
312
print_str ("{\"success\":null}\n" );
315
313
return 0 ;
316
314
}
0 commit comments