summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Demo/AWS/Device_Defender_Windows_Simulator/Device_Defender_Demo/report_builder.c
blob: e773bd9ffc4afc6751169b382869be4fe7e3a1db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
 * FreeRTOS V202104.00
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 */

/* Standard includes. */
#include <stdio.h>
#include <string.h>
#include <stdint.h>

/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"

/* Demo config. */
#include "demo_config.h"

/* Device Defender Client Library. */
#include "defender.h"

/* Interface include. */
#include "report_builder.h"

/* Various JSON characters. */
#define reportbuilderJSON_ARRAY_OPEN_MARKER         '['
#define reportbuilderJSON_ARRAY_CLOSE_MARKER        ']'
#define reportbuilderJSON_ARRAY_OBJECT_SEPARATOR    ','

/* Helper macro to check if snprintf was successful. */
#define reportbuilderSNPRINTF_SUCCESS( retVal, bufLen )    ( ( retVal > 0 ) && ( ( uint32_t ) retVal < bufLen ) )

/* Formats used to generate the JSON report. */
#define reportbuilderJSON_PORT_OBJECT_FORMAT \
    "{"                                      \
    "\""DEFENDER_REPORT_PORT_KEY"\": %u"     \
    "},"

#define reportbuilderJSON_CONNECTION_OBJECT_FORMAT              \
    "{"                                                         \
    "\""DEFENDER_REPORT_LOCAL_PORT_KEY"\": %u,"                 \
    "\""DEFENDER_REPORT_REMOTE_ADDR_KEY"\": \"%u.%u.%u.%u:%u\"" \
    "},"

#define reportbuilderJSON_REPORT_FORMAT_PART1          \
    "{"                                                \
    "\""DEFENDER_REPORT_HEADER_KEY"\": {"              \
    "\""DEFENDER_REPORT_ID_KEY"\": %u,"                \
    "\""DEFENDER_REPORT_VERSION_KEY"\": \"%u.%u\""     \
    "},"                                               \
    "\""DEFENDER_REPORT_METRICS_KEY"\": {"             \
    "\""DEFENDER_REPORT_TCP_LISTENING_PORTS_KEY"\": {" \
    "\""DEFENDER_REPORT_PORTS_KEY"\": "

#define reportbuilderJSON_REPORT_FORMAT_PART2          \
    ","                                                \
    "\""DEFENDER_REPORT_TOTAL_KEY"\": %u"              \
    "},"                                               \
    "\""DEFENDER_REPORT_UDP_LISTENING_PORTS_KEY"\": {" \
    "\""DEFENDER_REPORT_PORTS_KEY"\": "

#define reportbuilderJSON_REPORT_FORMAT_PART3              \
    ","                                                    \
    "\""DEFENDER_REPORT_TOTAL_KEY"\": %u"                  \
    "},"                                                   \
    "\""DEFENDER_REPORT_NETWORK_STATS_KEY"\": {"           \
    "\""DEFENDER_REPORT_BYTES_IN_KEY"\": %u,"              \
    "\""DEFENDER_REPORT_BYTES_OUT_KEY"\": %u,"             \
    "\""DEFENDER_REPORT_PKTS_IN_KEY"\": %u,"               \
    "\""DEFENDER_REPORT_PKTS_OUT_KEY"\": %u"               \
    "},"                                                   \
    "\""DEFENDER_REPORT_TCP_CONNECTIONS_KEY"\": {"         \
    "\""DEFENDER_REPORT_ESTABLISHED_CONNECTIONS_KEY"\": {" \
    "\""DEFENDER_REPORT_CONNECTIONS_KEY"\": "

#define reportbuilderJSON_REPORT_FORMAT_PART4     \
    ","                                           \
    "\""DEFENDER_REPORT_TOTAL_KEY"\": %u"         \
    "}"                                           \
    "}"                                           \
    "},"                                          \
    "\""DEFENDER_REPORT_CUSTOM_METRICS_KEY"\": {" \
    "\"stack_high_water_mark\": ["                \
    "{"                                           \
    "\""DEFENDER_REPORT_NUMBER_KEY"\": %u"        \
    "}"                                           \
    "],"                                          \
    "\"task_numbers\": ["                         \
    "{"                                           \
    "\""DEFENDER_REPORT_NUMBER_LIST_KEY"\": "

#define reportbuilderJSON_REPORT_FORMAT_PART5 \
    "}"                                       \
    "]"                                       \
    "}"                                       \
    "}"

/*-----------------------------------------------------------*/

/**
 * @brief Write ports array to the given buffer in the format expected by the
 * AWS IoT Device Defender Service.
 *
 * This function writes an array of the following format:
 * [
 *    {
 *        "port":44207
 *    },
 *    {
 *        "port":53
 *    }
 * ]
 *
 * @param[in] pcBuffer The buffer to write the ports array.
 * @param[in] ulBufferLength The length of the buffer.
 * @param[in] pusOpenPortsArray The array containing the open ports.
 * @param[in] ulOpenPortsArrayLength Length of the pusOpenPortsArray array.
 * @param[out] pulOutCharsWritten Number of characters written to the buffer.
 *
 * @return #ReportBuilderSuccess if the array is successfully written;
 * #ReportBuilderBufferTooSmall if the buffer cannot hold the full array.
 */
static eReportBuilderStatus prvWritePortsArray( char * pcBuffer,
                                                uint32_t ulBufferLength,
                                                const uint16_t * pusOpenPortsArray,
                                                uint32_t ulOpenPortsArrayLength,
                                                uint32_t * pulOutCharsWritten );

/**
 * @brief Write established connections array to the given buffer in the format
 * expected by the AWS IoT Device Defender Service.
 *
 * This function write array of the following format:
 * [
 *     {
 *         "local_port":44207,
 *         "remote_addr":"127.0.0.1:45148"
 *     },
 *     {
 *         "local_port":22,
 *         "remote_addr":"24.16.237.194:63552"
 *     }
 * ]
 *
 * @param[in] pcBuffer The buffer to write the connections array.
 * @param[in] ulBufferLength The length of the buffer.
 * @param[in] pxConnectionsArray The array containing the established connections.
 * @param[in] ulConnectionsArrayLength Length of the pxConnectionsArray array.
 * @param[out] pulOutCharsWritten Number of characters written to the buffer.
 *
 * @return #ReportBuilderSuccess if the array is successfully written;
 * #ReportBuilderBufferTooSmall if the buffer cannot hold the full array.
 */
static eReportBuilderStatus prvWriteConnectionsArray( char * pcBuffer,
                                                      uint32_t ulBufferLength,
                                                      const Connection_t * pxConnectionsArray,
                                                      uint32_t ulConnectionsArrayLength,
                                                      uint32_t * pulOutCharsWritten );

/**
 * @brief Write task ID array to the given buffer as a JSON array.
 *
 * @param[in] pcBuffer The buffer to write the array of task IDs.
 * @param[in] ulBufferLength The length of the buffer.
 * @param[in] pulTaskIdArray The array containing the task IDs.
 * @param[in] pulTaskIdArrayLength Length of the pulTaskIdsArray array.
 * @param[out] pulOutCharsWritten Number of characters written to the buffer.
 *
 * @return #ReportBuilderSuccess if the array is successfully written;
 * #ReportBuilderBufferTooSmall if the buffer cannot hold the full array.
 */
static eReportBuilderStatus prvWriteTaskIdArray( char * pcBuffer,
                                                  uint32_t ulBufferLength,
                                                  const uint32_t * pulTaskIdArray,
                                                  uint32_t pulTaskIdArrayLength,
                                                  uint32_t * pulOutCharsWritten );
/*-----------------------------------------------------------*/

static eReportBuilderStatus prvWritePortsArray( char * pcBuffer,
                                                uint32_t ulBufferLength,
                                                const uint16_t * pusOpenPortsArray,
                                                uint32_t ulOpenPortsArrayLength,
                                                uint32_t * pulOutCharsWritten )
{
    char * pcCurrentWritePos = pcBuffer;
    uint32_t i, ulRemainingBufferLength = ulBufferLength;
    int32_t lCharactersWritten;
    eReportBuilderStatus eStatus = eReportBuilderSuccess;

    configASSERT( pcBuffer != NULL );
    configASSERT( pusOpenPortsArray != NULL );
    configASSERT( pulOutCharsWritten != NULL );

    /* Write the JSON array open marker. */
    if( ulRemainingBufferLength > 1 )
    {
        *pcCurrentWritePos = reportbuilderJSON_ARRAY_OPEN_MARKER;
        ulRemainingBufferLength -= 1;
        pcCurrentWritePos += 1;
    }
    else
    {
        eStatus = eReportBuilderBufferTooSmall;
    }

    /* Write the array elements. */
    for( i = 0; ( ( i < ulOpenPortsArrayLength ) && ( eStatus == eReportBuilderSuccess ) ); i++ )
    {
        lCharactersWritten = snprintf( pcCurrentWritePos,
                                       ulRemainingBufferLength,
                                       reportbuilderJSON_PORT_OBJECT_FORMAT,
                                       pusOpenPortsArray[ i ] );

        if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
        {
            eStatus = eReportBuilderBufferTooSmall;
        }
        else
        {
            ulRemainingBufferLength -= ( uint32_t ) lCharactersWritten;
            pcCurrentWritePos += lCharactersWritten;
        }
    }

    if( eStatus == eReportBuilderSuccess )
    {
        /* Discard the last comma. */
        if( ulOpenPortsArrayLength > 0 )
        {
            pcCurrentWritePos -= 1;
            ulRemainingBufferLength += 1;
        }

        /* Write the JSON array close marker. */
        if( ulRemainingBufferLength > 1 )
        {
            *pcCurrentWritePos = reportbuilderJSON_ARRAY_CLOSE_MARKER;
            ulRemainingBufferLength -= 1;
            pcCurrentWritePos += 1;
            *pulOutCharsWritten = ulBufferLength - ulRemainingBufferLength;
        }
        else
        {
            eStatus = eReportBuilderBufferTooSmall;
        }
    }

    return eStatus;
}
/*-----------------------------------------------------------*/

static eReportBuilderStatus prvWriteConnectionsArray( char * pcBuffer,
                                                      uint32_t ulBufferLength,
                                                      const Connection_t * pxConnectionsArray,
                                                      uint32_t ulConnectionsArrayLength,
                                                      uint32_t * pulOutCharsWritten )
{
    char * pcCurrentWritePos = pcBuffer;
    uint32_t i, ulRemainingBufferLength = ulBufferLength;
    int32_t lCharactersWritten;
    eReportBuilderStatus eStatus = eReportBuilderSuccess;
    const Connection_t * pxConn;

    configASSERT( pcBuffer != NULL );
    configASSERT( pxConnectionsArray != NULL );
    configASSERT( pulOutCharsWritten != NULL );

    /* Write the JSON array open marker. */
    if( ulRemainingBufferLength > 1 )
    {
        *pcCurrentWritePos = reportbuilderJSON_ARRAY_OPEN_MARKER;
        ulRemainingBufferLength -= 1;
        pcCurrentWritePos += 1;
    }
    else
    {
        eStatus = eReportBuilderBufferTooSmall;
    }

    /* Write the array elements. */
    for( i = 0; ( ( i < ulConnectionsArrayLength ) && ( eStatus == eReportBuilderSuccess ) ); i++ )
    {
        pxConn = &( pxConnectionsArray[ i ] );
        lCharactersWritten = snprintf( pcCurrentWritePos,
                                       ulRemainingBufferLength,
                                       reportbuilderJSON_CONNECTION_OBJECT_FORMAT,
                                       pxConn->usLocalPort,
                                       ( pxConn->ulRemoteIp >> 24 ) & 0xFF,
                                       ( pxConn->ulRemoteIp >> 16 ) & 0xFF,
                                       ( pxConn->ulRemoteIp >> 8 ) & 0xFF,
                                       ( pxConn->ulRemoteIp ) & 0xFF,
                                       pxConn->usRemotePort );

        if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
        {
            eStatus = eReportBuilderBufferTooSmall;
        }
        else
        {
            ulRemainingBufferLength -= lCharactersWritten;
            pcCurrentWritePos += lCharactersWritten;
        }
    }

    if( eStatus == eReportBuilderSuccess )
    {
        /* Discard the last comma. */
        if( ulConnectionsArrayLength > 0 )
        {
            pcCurrentWritePos -= 1;
            ulRemainingBufferLength += 1;
        }

        /* Write the JSON array close marker. */
        if( ulRemainingBufferLength > 1 )
        {
            *pcCurrentWritePos = reportbuilderJSON_ARRAY_CLOSE_MARKER;
            ulRemainingBufferLength -= 1;
            pcCurrentWritePos += 1;
            *pulOutCharsWritten = ulBufferLength - ulRemainingBufferLength;
        }
        else
        {
            eStatus = eReportBuilderBufferTooSmall;
        }
    }

    return eStatus;
}
/*-----------------------------------------------------------*/

static eReportBuilderStatus prvWriteTaskIdArray( char * pcBuffer,
                                                  uint32_t ulBufferLength,
                                                  const uint32_t * pulTaskIdArray,
                                                  uint32_t pulTaskIdArrayLength,
                                                  uint32_t * pulOutCharsWritten )
{
    char * pcCurrentWritePos = pcBuffer;
    uint32_t i, ulRemainingBufferLength = ulBufferLength;
    int32_t lCharactersWritten;
    eReportBuilderStatus eStatus = eReportBuilderSuccess;

    configASSERT( pcBuffer != NULL );
    configASSERT( pulTaskIdArray != NULL );
    configASSERT( pulOutCharsWritten != NULL );

    /* Write the JSON array open marker. */
    if( ulRemainingBufferLength > 1 )
    {
        *pcCurrentWritePos = reportbuilderJSON_ARRAY_OPEN_MARKER;
        ulRemainingBufferLength -= 1;
        pcCurrentWritePos += 1;
    }
    else
    {
        eStatus = eReportBuilderBufferTooSmall;
    }

    /* Write the array elements. */
    for( i = 0; ( ( i < pulTaskIdArrayLength ) && ( eStatus == eReportBuilderSuccess ) ); i++ )
    {
        lCharactersWritten = snprintf( pcCurrentWritePos,
                                       ulRemainingBufferLength,
                                       "%u,",
                                       pulTaskIdArray[ i ] );

        if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
        {
            eStatus = eReportBuilderBufferTooSmall;
        }
        else
        {
            ulRemainingBufferLength -= ( uint32_t ) lCharactersWritten;
            pcCurrentWritePos += lCharactersWritten;
        }
    }

    if( eStatus == eReportBuilderSuccess )
    {
        /* Discard the last comma. */
        if( pulTaskIdArrayLength > 0 )
        {
            pcCurrentWritePos -= 1;
            ulRemainingBufferLength += 1;
        }

        /* Write the JSON array close marker. */
        if( ulRemainingBufferLength > 1 )
        {
            *pcCurrentWritePos = reportbuilderJSON_ARRAY_CLOSE_MARKER;
            ulRemainingBufferLength -= 1;
            pcCurrentWritePos += 1;
            *pulOutCharsWritten = ulBufferLength - ulRemainingBufferLength;
        }
        else
        {
            eStatus = eReportBuilderBufferTooSmall;
        }
    }

    return eStatus;
}
/*-----------------------------------------------------------*/

eReportBuilderStatus eGenerateJsonReport( char * pcBuffer,
                                          uint32_t ulBufferLength,
                                          const ReportMetrics_t * pxMetrics,
                                          uint32_t ulMajorReportVersion,
                                          uint32_t ulMinorReportVersion,
                                          uint32_t ulReportId,
                                          uint32_t * pulOutReportLength )
{
    char * pcCurrentWritePos = pcBuffer;
    uint32_t ulRemainingBufferLength = ulBufferLength;
    uint32_t bufferWritten;
    eReportBuilderStatus eStatus = eReportBuilderSuccess;
    int32_t lCharactersWritten;

    configASSERT( pcBuffer != NULL );
    configASSERT( pxMetrics != NULL );
    configASSERT( pulOutReportLength != NULL );
    configASSERT( ulBufferLength != 0 );

    if( ( pcBuffer == NULL ) ||
        ( ulBufferLength == 0 ) ||
        ( pxMetrics == NULL ) ||
        ( pulOutReportLength == NULL ) )
    {
        LogError( ( "Invalid parameters. pcBuffer: %p, ulBufferLength: %u"
                    " pMetrics: %p, pOutReprotLength: %p.",
                    pcBuffer,
                    ulBufferLength,
                    pxMetrics,
                    pulOutReportLength ) );
        eStatus = eReportBuilderBadParameter;
    }

    /* Write part1. */
    if( eStatus == eReportBuilderSuccess )
    {
        lCharactersWritten = snprintf( pcCurrentWritePos,
                                       ulRemainingBufferLength,
                                       reportbuilderJSON_REPORT_FORMAT_PART1,
                                       ulReportId,
                                       ulMajorReportVersion,
                                       ulMinorReportVersion );

        if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
        {
            LogError( ( "Failed to write part 1." ) );
            eStatus = eReportBuilderBufferTooSmall;
        }
        else
        {
            ulRemainingBufferLength -= lCharactersWritten;
            pcCurrentWritePos += lCharactersWritten;
        }
    }

    /* Write TCP ports array. */
    if( eStatus == eReportBuilderSuccess )
    {
        eStatus = prvWritePortsArray( pcCurrentWritePos,
                                      ulRemainingBufferLength,
                                      pxMetrics->pusOpenTcpPortsArray,
                                      pxMetrics->ulOpenTcpPortsArrayLength,
                                      &( bufferWritten ) );

        if( eStatus == eReportBuilderSuccess )
        {
            pcCurrentWritePos += bufferWritten;
            ulRemainingBufferLength -= bufferWritten;
        }
        else
        {
            LogError( ( "Failed to write TCP ports array." ) );
        }
    }

    /* Write part2. */
    if( eStatus == eReportBuilderSuccess )
    {
        lCharactersWritten = snprintf( pcCurrentWritePos,
                                       ulRemainingBufferLength,
                                       reportbuilderJSON_REPORT_FORMAT_PART2,
                                       pxMetrics->ulOpenTcpPortsArrayLength );

        if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
        {
            LogError( ( "Failed to write part 2." ) );
            eStatus = eReportBuilderBufferTooSmall;
        }
        else
        {
            ulRemainingBufferLength -= lCharactersWritten;
            pcCurrentWritePos += lCharactersWritten;
        }
    }

    /* Write UDP ports array. */
    if( eStatus == eReportBuilderSuccess )
    {
        eStatus = prvWritePortsArray( pcCurrentWritePos,
                                      ulRemainingBufferLength,
                                      pxMetrics->pusOpenUdpPortsArray,
                                      pxMetrics->ulOpenUdpPortsArrayLength,
                                      &( bufferWritten ) );

        if( eStatus == eReportBuilderSuccess )
        {
            pcCurrentWritePos += bufferWritten;
            ulRemainingBufferLength -= bufferWritten;
        }
        else
        {
            LogError( ( "Failed to write UDP ports array." ) );
        }
    }

    /* Write part3. */
    if( eStatus == eReportBuilderSuccess )
    {
        lCharactersWritten = snprintf( pcCurrentWritePos,
                                       ulRemainingBufferLength,
                                       reportbuilderJSON_REPORT_FORMAT_PART3,
                                       pxMetrics->ulOpenUdpPortsArrayLength,
                                       pxMetrics->pxNetworkStats->ulBytesReceived,
                                       pxMetrics->pxNetworkStats->ulBytesSent,
                                       pxMetrics->pxNetworkStats->ulPacketsReceived,
                                       pxMetrics->pxNetworkStats->ulPacketsSent,
                                       DEFENDER_REPORT_ESTABLISHED_CONNECTIONS_KEY );

        if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
        {
            LogError( ( "Failed to write part 3." ) );
            eStatus = eReportBuilderBufferTooSmall;
        }
        else
        {
            ulRemainingBufferLength -= lCharactersWritten;
            pcCurrentWritePos += lCharactersWritten;
        }
    }

    /* Write connections array. */
    if( eStatus == eReportBuilderSuccess )
    {
        eStatus = prvWriteConnectionsArray( pcCurrentWritePos,
                                            ulRemainingBufferLength,
                                            pxMetrics->pxEstablishedConnectionsArray,
                                            pxMetrics->ulEstablishedConnectionsArrayLength,
                                            &( bufferWritten ) );

        if( eStatus == eReportBuilderSuccess )
        {
            pcCurrentWritePos += bufferWritten;
            ulRemainingBufferLength -= bufferWritten;
        }
        else
        {
            LogError( ( "Failed to write established connections array." ) );
        }
    }

    /* Write part4. */
    if( eStatus == eReportBuilderSuccess )
    {
        lCharactersWritten = snprintf( pcCurrentWritePos,
                                       ulRemainingBufferLength,
                                       reportbuilderJSON_REPORT_FORMAT_PART4,
                                       pxMetrics->ulEstablishedConnectionsArrayLength,
                                       pxMetrics->ulStackHighWaterMark );

        if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
        {
            LogError( ( "Failed to write part 4." ) );
            eStatus = eReportBuilderBufferTooSmall;
        }
        else
        {
            ulRemainingBufferLength -= lCharactersWritten;
            pcCurrentWritePos += lCharactersWritten;
        }
    }

    /* Write task ids array. */
    if( eStatus == eReportBuilderSuccess )
    {
        eStatus = prvWriteTaskIdArray( pcCurrentWritePos,
                                        ulRemainingBufferLength,
                                        pxMetrics->pulTaskIdArray,
                                        pxMetrics->ulTaskIdArrayLength,
                                        &( bufferWritten ) );

        if( eStatus == eReportBuilderSuccess )
        {
            pcCurrentWritePos += bufferWritten;
            ulRemainingBufferLength -= bufferWritten;
        }
        else
        {
            LogError( ( "Failed to write task ID array." ) );
        }
    }

    /* Write part5. */
    if( eStatus == eReportBuilderSuccess )
    {
        lCharactersWritten = snprintf( pcCurrentWritePos,
                                       ulRemainingBufferLength,
                                       reportbuilderJSON_REPORT_FORMAT_PART5 );

        if( !reportbuilderSNPRINTF_SUCCESS( lCharactersWritten, ulRemainingBufferLength ) )
        {
            LogError( ( "Failed to write part 5." ) );
            eStatus = eReportBuilderBufferTooSmall;
        }
        else
        {
            ulRemainingBufferLength -= lCharactersWritten;
            pcCurrentWritePos += lCharactersWritten;
            *pulOutReportLength = ulBufferLength - ulRemainingBufferLength;
        }
    }

    return eStatus;
}
/*-----------------------------------------------------------*/