summaryrefslogtreecommitdiff
path: root/lcmodule/source/cnh1606344_ldr_communication_module/source/measurement_tool.c
blob: 67f730ecbafee9c683fa973b5c9eb7c3d4ad559d (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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*******************************************************************************
 * Copyright (C) ST-Ericsson SA 2011
 * License terms: 3-clause BSD license
 ******************************************************************************/
/**
 * @addtogroup ldr_communication_serv
 * @{
 */

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <string.h>
#include "t_basicdefinitions.h"
#include "r_communication_service.h"
#include "r_measurement_tool.h"
#include "r_time_utilities.h"
#include "r_serialization.h"
#include "r_debug_macro.h"
#include "r_memory_utils.h"

/*******************************************************************************
 * File scope types, constants and variables
 ******************************************************************************/

Measurement_t *Measurement_p;

static ChunkMeasurement_t *GetLastChunkDataBlock(ChunkMeasurement_t *ChunkMeasurement_p);
static SessionMeasurement_t *GetLastSessionDataBlock(SessionMeasurement_t *SessionMeasurement_p);
static ChunkMeasurement_t *FindChunkDataBlock(ChunkMeasurement_t *Measurement_p, uint32 ChunkID);
static uint32 CalculateSizeOfResults(SessionMeasurement_t *DataMeasurement_p, uint32 Type, uint32 *SessionNr_pp);
static void SessionDataSerialize(uint8 **Data_pp, SessionMeasurement_t *SessData_p, uint32 *Length_p);
static void ChunkDataSerialize(uint8 **Data_pp, ChunkMeasurement_t *ChunkData_p, uint32 *Length_p);
static ErrorCode_e CompleteBulkFlashResults(Measurement_t *Measurement_p);
static void TimerMeasurmentHandler(void *Data_p, void *Timer_p, void *Param_p);

/*******************************************************************************
 * Definition of external functions
 ******************************************************************************/
/*
 * Initialization of measurement module.
 *
 * @param [in] Measurement_p Pointer to curren measurement data block.
 *
 * @retval  E_SUCCESS If initialization is done.
 */
ErrorCode_e Do_Measurement_Init(Measurement_t *Measurement_p)
{
    return E_SUCCESS;
}

/*
 * This function create general measurement data block.
 *
 * @param [in]  Communication_p    Communication module context.
 * @param [in]  Measurement_pp Pointer to the created measurement data block.
 * @param [in]  Type               Type of measurement.
 *
 * @retval E_SUCCESS         After successful execution.
 * @retval E_ALLOCATE_FAILED If failed to allocate session measurement
 *                           data block.
 */
ErrorCode_e  Do_Measurement_CreateMeasurementData(Communication_t *Communication_p, Measurement_t **Measurement_pp, uint32 Type)
{
    ErrorCode_e ReturnValue = E_ALLOCATE_FAILED;

    if (NULL != *Measurement_pp) {
        ReturnValue = Do_Measurement_DestroyMeasurementData(Measurement_pp);

        if (E_SUCCESS != ReturnValue) {
            return ReturnValue;
        }
    }

    *Measurement_pp = (Measurement_t *)malloc(sizeof(Measurement_t));

    if (NULL == *Measurement_pp) {
        return E_ALLOCATE_FAILED;
    }

    (*Measurement_pp)->MeasurementData_p = malloc(sizeof(MeasurementDataContext_t));

    if (NULL == (*Measurement_pp)->MeasurementData_p) {
        return E_ALLOCATE_FAILED;
    }

    (*Measurement_pp)->MeasurementData_p->SessionNr = 0;
    (*Measurement_pp)->MeasurementData_p->SessionMeasurement_p = NULL;
    (*Measurement_pp)->MeasurementData_p->ProcesFileMeasurement_p = NULL;
    (*Measurement_pp)->Timer.Time = 3600000;
    (*Measurement_pp)->Timer.Time = 3600000;
    (*Measurement_pp)->Timer.PeriodicalTime = 0;
    (*Measurement_pp)->Timer.HandleFunction_p = TimerMeasurmentHandler;
    (*Measurement_pp)->TimerId = Do_Timer_TimerGet(NULL, &(*Measurement_pp)->Timer);
    (*Measurement_pp)->StartCommandTime = 0;
    (*Measurement_pp)->EndCommandTime = 0;
    (*Measurement_pp)->Type = Type;
    (*Measurement_pp)->ResultLength = 0;
    (*Measurement_pp)->Result_p = NULL;
    return E_SUCCESS;
}

/*
 * This function create general measurement data block.
 *
 * @param [in]  Communication_p    Communication module context.
 * @param [in]  Data_pp                      Pointer to the data block.
 * @param [in]  DataLength_p       Pointer to the data block length.
 *
 * @retval E_SUCCESS         After successful execution.
 * @retval E_ALLOCATE_FAILED If failed to allocate session measurement
 *                           data block.
 */
uint32  Do_Measurement_GetMeasurementData(Communication_t *Communication_p, uint8 **Data_pp)
{
    if (NULL == Measurement_p || 0 == Measurement_p->ResultLength || NULL == Measurement_p->Result_p) {
        return 0;
    }

    *Data_pp = (uint8 *)malloc(Measurement_p->ResultLength);
    ASSERT(NULL != *Data_pp);
    memcpy((uint8 *)*Data_pp, Measurement_p->Result_p, Measurement_p->ResultLength);
    return Measurement_p->ResultLength;
}

/*
 * This function create measurement data block for bulk session.
 *
 * @param [in]  Communication_p            Communication module context.
 * @param [in]  SessionMeasurement_pp      Pointer to the created session
 *                                         measurement data block.
 * @param [in]  PreviousSessionDataBlock_p Pointer to the previouse session
 *                                         measurement data block.
 * @param [in]  ChunkNr                    Number of chunks in this session.
 * @param [in]  Session                    Session number.
 *
 * @retval E_SUCCESS         After successful execution.
 * @retval E_ALLOCATE_FAILED If failed to allocate session measurement
 *                           data block.
 */
ErrorCode_e  Do_Measurement_CreateSessionMeasurementData(Communication_t *Communication_p,
        SessionMeasurement_t **SessionMeasurement_pp,
        SessionMeasurement_t *PreviousSessionDataBlock_p,
        uint32 ChunkNr, uint32 Session)
{
    ChunkMeasurement_t *ChunkDataBlock_p = NULL;
    SessionMeasurement_t **NewSessionDataBlock_pp = NULL;
    SessionMeasurement_t *LastSessionMeasurement_p = NULL;
    uint32 i;
    ErrorCode_e ReturnValue = E_ALLOCATE_FAILED;

    if (*SessionMeasurement_pp != NULL) {
        LastSessionMeasurement_p = GetLastSessionDataBlock(*SessionMeasurement_pp);
        NewSessionDataBlock_pp = &LastSessionMeasurement_p->NextDataBlock_p;
    } else {
        NewSessionDataBlock_pp = SessionMeasurement_pp;
    }

    *NewSessionDataBlock_pp = (SessionMeasurement_t *)malloc(sizeof(SessionMeasurement_t));

    if (NULL == *SessionMeasurement_pp) {
        return ReturnValue;
    }

    (*NewSessionDataBlock_pp)->PreviousDataBlock_p = LastSessionMeasurement_p;
    (*NewSessionDataBlock_pp)->SessionId = Session;
    (*NewSessionDataBlock_pp)->ChunkNr = ChunkNr;
    (*NewSessionDataBlock_pp)->OpenSessionTime = 0;
    (*NewSessionDataBlock_pp)->CloseSessionTime = 0;
    (*NewSessionDataBlock_pp)->ChunkMeasurement_p = NULL;
    (*NewSessionDataBlock_pp)->NextDataBlock_p = NULL;

    for (i = 0; i < ChunkNr; i++) {
        ReturnValue = Do_Measurement_CreateChunkMeasurementData(Communication_p, &(*NewSessionDataBlock_pp)->ChunkMeasurement_p, ChunkDataBlock_p, i);
    }

    return ReturnValue;
}

/*
 * This function create measurement data block for measure chunks transfer.
 *
 * @param [in]  Communication_p            Communication module context.
 * @param [in]  ChunkMeasurement_pp        Pointer to the created chunk
 *                                         measurement data block.
 * @param [in]  PreviousChunkDataBlock_p   Pointer to the previouse chunk
 *                                         measurement data block.
 * @param [in]  ChunkId                    Chunk ID.
 *
 * @retval E_SUCCESS         After successful execution.
 * @retval E_ALLOCATE_FAILED If failed to allocate session measurement
 *                           data block.
 */
ErrorCode_e  Do_Measurement_CreateChunkMeasurementData(Communication_t *Communication_p,
        ChunkMeasurement_t **ChunkMeasurement_pp,
        ChunkMeasurement_t *PreviousChunkDataBlock_p,
        uint32 ChunkId)
{
    ChunkMeasurement_t **NewChunkDataBlock_pp = NULL;
    ChunkMeasurement_t *LastChunkMeasurement_p = NULL;

    if (*ChunkMeasurement_pp != NULL) {
        LastChunkMeasurement_p = GetLastChunkDataBlock(*ChunkMeasurement_pp);
        NewChunkDataBlock_pp = &(LastChunkMeasurement_p)->NextDataBlock_p;
    } else {
        NewChunkDataBlock_pp = ChunkMeasurement_pp;
    }

    *NewChunkDataBlock_pp = (ChunkMeasurement_t *)malloc(sizeof(ChunkMeasurement_t));

    if (NULL == *NewChunkDataBlock_pp) {
        return E_ALLOCATE_FAILED;
    }

    (*NewChunkDataBlock_pp)->PreviousDataBlock_p = LastChunkMeasurement_p;
    (*NewChunkDataBlock_pp)->ChunkId = ChunkId;
    (*NewChunkDataBlock_pp)->ReceivedChunkTime = 0;
    (*NewChunkDataBlock_pp)->StartFlashChunkTime = 0;
    (*NewChunkDataBlock_pp)->EndFlashChunkTime = 0;
    (*NewChunkDataBlock_pp)->NextDataBlock_p = NULL;
    return E_SUCCESS;
}

/*
 * This function destroy measurement data block for bulk session.
 *
 * @param [in]  Measurement_pp Pointer to the measurement data block.
 *
 * @retval E_SUCCESS         After successful execution.
 */
ErrorCode_e  Do_Measurement_DestroySessionMeasurementData(SessionMeasurement_t **SessionMeasurement_pp)
{
    ErrorCode_e ReturnValue;
    SessionMeasurement_t *LastSessionDataBlock_p = NULL;

    if (*SessionMeasurement_pp != NULL) {
        LastSessionDataBlock_p = GetLastSessionDataBlock(*SessionMeasurement_pp);

        do {
            *SessionMeasurement_pp = LastSessionDataBlock_p->PreviousDataBlock_p;
            /* free the all chunks in the session data block */
            ReturnValue = Do_Measurement_DestroyChunkMeasurementData(&LastSessionDataBlock_p->ChunkMeasurement_p);

            if (ReturnValue != E_SUCCESS) {
                return ReturnValue;
            }

            /* free the session data block */
            free(LastSessionDataBlock_p);
            LastSessionDataBlock_p = *SessionMeasurement_pp;
        } while (LastSessionDataBlock_p->PreviousDataBlock_p != NULL);

        /* free the all chunks in the last session data block */
        ReturnValue = Do_Measurement_DestroyChunkMeasurementData(&LastSessionDataBlock_p->ChunkMeasurement_p);

        if (ReturnValue != E_SUCCESS) {
            return ReturnValue;
        }

        /* free the last session data block */
        free(LastSessionDataBlock_p);
    }

    return E_SUCCESS;
}

/*
 * This function destroy measurement data block for bulk chunks.
 *
 * @param [in]  Measurement_pp Pointer to the main measurement data block.
 *
 * @retval E_SUCCESS         After successful execution.
 */
ErrorCode_e  Do_Measurement_DestroyChunkMeasurementData(ChunkMeasurement_t **ChunkMeasurement_pp)
{
    ChunkMeasurement_t *LastChunkDataBlock_p = NULL;
    ChunkMeasurement_t *TmpLastChunkDataBlock_p = NULL;

    if (*ChunkMeasurement_pp != NULL) {
        LastChunkDataBlock_p = GetLastChunkDataBlock(*ChunkMeasurement_pp);
        TmpLastChunkDataBlock_p = LastChunkDataBlock_p->PreviousDataBlock_p;
        free(LastChunkDataBlock_p);

        while (TmpLastChunkDataBlock_p != NULL) {
            /* free the chunk data block */
            LastChunkDataBlock_p      = TmpLastChunkDataBlock_p;
            TmpLastChunkDataBlock_p = LastChunkDataBlock_p->PreviousDataBlock_p;
            free(LastChunkDataBlock_p);
        }//while(TmpLastChunkDataBlock_p != NULL);

        //    while(LastChunkDataBlock_p->PreviousDataBlock_p != NULL)
        //    {
        //      *ChunkMeasurement_pp = LastChunkDataBlock_p->PreviousDataBlock_p;
        //      /* free the chunk data block */
        //      free(LastChunkDataBlock_p);
        //      LastChunkDataBlock_p = *ChunkMeasurement_pp;
        //    }
        /* free the last chunk data block */
        //free(*ChunkMeasurement_pp);
    }

    return E_SUCCESS;
}

/*
 * This function destroy general measurement data block.
 *
 * @param [in]  Measurement_pp Pointer to the main measurement data block.
 *
 * @retval E_SUCCESS         After successful execution.
 */
ErrorCode_e  Do_Measurement_DestroyMeasurementData(Measurement_t **Measurement_pp)
{
    ErrorCode_e ReturnValue = E_INVALID_INPUT_PARAMETERS;

    if (NULL == *Measurement_pp) {
        return ReturnValue;
    }

    switch ((*Measurement_pp)->Type) {
    case FLASH_VIA_BULK_TRANSFER:

        /* free the all created session data blocks */
        if (NULL != (*Measurement_pp)->MeasurementData_p) {
            if (NULL != (*Measurement_pp)->MeasurementData_p->SessionMeasurement_p) {
                ReturnValue = Do_Measurement_DestroySessionMeasurementData(&(*Measurement_pp)->MeasurementData_p->SessionMeasurement_p);
                (*Measurement_pp)->MeasurementData_p->SessionMeasurement_p = NULL;
            }

            ReturnValue = E_SUCCESS;
            //free((*Measurement_pp)->MeasurementData_p->ProcesFileMeasurement_p);
            //free((*Measurement_pp)->MeasurementData_p);
        }

        if (E_SUCCESS != ReturnValue) {
            return ReturnValue;
        }

        break;

    default:
        //...
        break;
    }

    //free(*Measurement_pp);
    return ReturnValue;
}

/*
 * This function make zero reference for all measured data.
 *
 * @param [in]  Measurement_p Pointer to the main measurement data block.
 *
 * @retval E_SUCCESS         After successful execution.
 */
ErrorCode_e  Do_Measurement_ZeroReferenceMeasurementData(Measurement_t *Measurement_p)
{
    return E_SUCCESS;
}

/*
 * This function add measured time in specified measurement data block.
 *
 * @param [in]  Measurement_p Pointer to the main measurement data block.
 * @param [in]  Time              Measured time.
 * @param [in]  ChunkID           Chunk ID
 * @param [in]  Parameter         Parameter in the measurement data block.
 *
 * @retval E_SUCCESS         After successful execution.
 */
ErrorCode_e  Do_Measurement_AddMeasurementData(Measurement_t *Measurement_p, uint32 Time, uint32 ChunkID, MeasurementParameter_t Parameter)
{
    ErrorCode_e ReturnValue = E_FAILED_TO_FIND_CHUNK_DATA_BLOCK;
    uint32  NewTime;
    SessionMeasurement_t *SessionMeasurement_p = GetLastSessionDataBlock(Measurement_p->MeasurementData_p->SessionMeasurement_p);
    ChunkMeasurement_t *ChankDataBlock_p = NULL;
    NewTime = Measurement_p->Timer.Time - Time;

    switch (Parameter) {
    case START_SESSION_TIME:
        SessionMeasurement_p->OpenSessionTime = NewTime;
        ReturnValue = E_SUCCESS;
        break;

    case END_SESSION_TIME:
        SessionMeasurement_p->CloseSessionTime = NewTime;
        ReturnValue = E_SUCCESS;
        break;

    case RECEIVED_CHUNK_TIME :
        ChankDataBlock_p = FindChunkDataBlock(SessionMeasurement_p->ChunkMeasurement_p, ChunkID);

        if (ChankDataBlock_p != NULL) {
            ChankDataBlock_p->ReceivedChunkTime = NewTime;
            ReturnValue = E_SUCCESS;
        }

        break;

    case START_FLASH_CHUNK_TIME:
        ChankDataBlock_p = FindChunkDataBlock(SessionMeasurement_p->ChunkMeasurement_p, ChunkID);

        if (ChankDataBlock_p != NULL) {
            ChankDataBlock_p->StartFlashChunkTime = NewTime;
            ReturnValue = E_SUCCESS;
        }

        break;

    case END_FLASH_CHUNK_TIME:
        ChankDataBlock_p = FindChunkDataBlock(SessionMeasurement_p->ChunkMeasurement_p, ChunkID);

        if (ChankDataBlock_p != NULL) {
            ChankDataBlock_p->EndFlashChunkTime = NewTime;
            ReturnValue = E_SUCCESS;
        }

        break;

    case START_COMMAND_TIME:
        Measurement_p->StartCommandTime = NewTime;
        ReturnValue = E_SUCCESS;
        break;

    case END_COMMAND_TIME:
        Measurement_p->EndCommandTime = NewTime;
        ReturnValue = E_SUCCESS;
        break;

    default:
        //…
        break;
    }

    return ReturnValue;
}

/*
 *
 */
ErrorCode_e  Do_Measurement_CompleteResults(Measurement_t *Measurement_p)
{
    ErrorCode_e ReturnValue = E_FAILED_TO_FIND_CHUNK_DATA_BLOCK;
    //SessionMeasurement_t * TmpSessData_p = NULL;
    //uint8* Data_p = NULL;
    uint32 Size = 0;

    switch (Measurement_p->Type) {
    case FLASH_VIA_BULK_TRANSFER:
        Size = CalculateSizeOfResults(Measurement_p->MeasurementData_p->SessionMeasurement_p, Measurement_p->Type, (uint32 *)&Measurement_p->MeasurementData_p->SessionNr);

        if (Size != 0) {
            Size += 2 * sizeof(uint32);
            BUFFER_FREE(Measurement_p->Result_p);

            Measurement_p->Result_p = (uint8 *)malloc(Size);
            //    ASSERT(NULL != Measurement_p->Result_p);
            Measurement_p->ResultLength = Size;

            /* get result from the list of the session data blocks */
            ReturnValue = CompleteBulkFlashResults(Measurement_p);
        } else {
            Measurement_p->ResultLength = Size;
            BUFFER_FREE(Measurement_p->Result_p);

            Measurement_p->Result_p = NULL;
            ReturnValue = E_SUCCESS;
        }

        break;

    default:
        //...
        break;
    }

    return ReturnValue;
}

/*******************************************************************************
 * Definition of internal functions
 ******************************************************************************/
/*
 * This function create Timer callback.
 *
 * @param [in]  Data_p    Pointer to data.
 * @param [in]  Timer_p   Pointer to timer.
 * @param [in]  Param_p   Pointer to parameter.
 *
 */
void TimerMeasurmentHandler(void *Data_p, void *Timer_p, void *Param_p)
{
    ASSERT(FALSE);
}

/*
 * This function search the requested chunk data block in the list of the chunk
 * data blocks.
 *
 * @param [in]  Measurement_p  Pointer to the main measurement data block.
 * @param [in]  ChunkID        Chunk ID for requested chunk data block.
 *
 * @retval pointer Pointer to the requested chunk data block.
 * @retval NULL If the list of the chunk data blocks is empty.
 */
static ChunkMeasurement_t *FindChunkDataBlock(ChunkMeasurement_t *Measurement_p, uint32 ChunkID)
{
    ChunkMeasurement_t *TmpData_p = Measurement_p;

    if (TmpData_p != NULL) {
        while ((TmpData_p->NextDataBlock_p != NULL) && (TmpData_p->ChunkId != ChunkID)) {
            TmpData_p = TmpData_p->NextDataBlock_p;
        }
    }

    return TmpData_p;
}

/*
 * Get last chunk data block from the list of the chunk data blocks.
 *
 * @param [in]  ChunkMeasurement_p  Pointer to the main measurement data block.
 *
 * @retval pointer Pointer to the last chunk data block.
 * @retval NULL If the list of the chunk data blocks is empty.
 */
static ChunkMeasurement_t *GetLastChunkDataBlock(ChunkMeasurement_t *ChunkMeasurement_p)
{
    ChunkMeasurement_t *TmpData_p = ChunkMeasurement_p;

    if (TmpData_p != NULL) {
        while (TmpData_p->NextDataBlock_p != NULL) {
            TmpData_p = TmpData_p->NextDataBlock_p;
        }
    }

    return TmpData_p;
}

/*
 * Get last session data block from the list of the chunk data blocks.
 *
 * @param [in]  ChunkMeasurement_p  Pointer to the main measurement data block.
 *
 * @retval pointer Pointer to the last chunk data block.
 * @retval NULL If the list of the chunk data blocks is empty.
 */
static SessionMeasurement_t *GetLastSessionDataBlock(SessionMeasurement_t *SessionMeasurement_p)
{
    SessionMeasurement_t *TmpData_p = SessionMeasurement_p;

    if (TmpData_p != NULL) {
        while (TmpData_p->NextDataBlock_p != NULL) {
            TmpData_p = TmpData_p->NextDataBlock_p;
        }
    }

    return TmpData_p;
}


static ErrorCode_e CompleteBulkFlashResults(Measurement_t *Measurement_p)
{
    ErrorCode_e ReturnValue = E_SUCCESS;
    SessionMeasurement_t *TmpSessData_p = Measurement_p->MeasurementData_p->SessionMeasurement_p;
    ChunkMeasurement_t *TmpChunkData_p = NULL;
    uint8 *Data_p = Measurement_p->Result_p;

    if (TmpSessData_p != NULL) {
        memcpy(Data_p                               , (uint8 *)&Measurement_p->Type                                              , sizeof(uint32));
        memcpy(Data_p + sizeof(uint32), (uint8 *)&Measurement_p->MeasurementData_p->SessionNr, sizeof(uint32));
        Data_p += 2 * sizeof(uint32);
        //Measurement_p->ResultLength ++;
        //SessionDataSerialize(&Data_p, TmpSessData_p, &Measurement_p->ResultLength);
        TmpChunkData_p = TmpSessData_p->ChunkMeasurement_p;

        //while(TmpSessData_p->NextDataBlock_p != NULL)
        while (TmpSessData_p != NULL) {
            SessionDataSerialize(&Data_p, TmpSessData_p, &Measurement_p->ResultLength);
            TmpChunkData_p = TmpSessData_p->ChunkMeasurement_p;

            if (TmpChunkData_p != NULL) {
                ChunkDataSerialize(&Data_p, TmpChunkData_p, &Measurement_p->ResultLength);

                while (TmpChunkData_p->NextDataBlock_p != NULL) {
                    TmpChunkData_p = TmpChunkData_p->NextDataBlock_p;
                    ChunkDataSerialize(&Data_p, TmpChunkData_p, &Measurement_p->ResultLength);
                }
            }

            TmpSessData_p = TmpSessData_p->NextDataBlock_p;
        }
    }

    return ReturnValue;
}


static void SessionDataSerialize(uint8 **Data_pp, SessionMeasurement_t *SessData_p, uint32 *Length_p)
{
    put_uint32_le((void **)Data_pp, SessData_p->SessionId);
    *Length_p++;
    put_uint32_le((void **)Data_pp, SessData_p->OpenSessionTime);
    *Length_p++;
    put_uint32_le((void **)Data_pp, SessData_p->CloseSessionTime);
    *Length_p++;
    put_uint32_le((void **)Data_pp, SessData_p->ChunkNr);
    *Length_p++;
}


static void ChunkDataSerialize(uint8 **Data_pp, ChunkMeasurement_t *ChunkData_p, uint32 *Length_p)
{
    put_uint32_le((void **)Data_pp, ChunkData_p->ChunkId);
    *Length_p++;
    put_uint32_le((void **)Data_pp, ChunkData_p->ReceivedChunkTime);
    *Length_p++;
    put_uint32_le((void **)Data_pp, ChunkData_p->StartFlashChunkTime);
    *Length_p++;
    put_uint32_le((void **)Data_pp, ChunkData_p->EndFlashChunkTime);
    *Length_p++;
}

/*
 * Calculate size of the results.
 *
 * @param [in]  DataMeasurement_p  Pointer to the main measurement data block.
 * @param [in]  Type               Type of measurement data block.
 *
 * @retval Size Size of the results.
 * @retval 0 If the list of the chunk data blocks is empty.
 */
static uint32 CalculateSizeOfResults(SessionMeasurement_t *DataMeasurement_p, uint32 Type, uint32 *SessionNr_p)
{
    SessionMeasurement_t *TmpData_p = DataMeasurement_p;
    uint32 Size = 0;

    switch (Type) {
    case  FLASH_VIA_BULK_TRANSFER:

        if (TmpData_p != NULL) {
            *SessionNr_p = 0;

            //(SessionMeasurement_t*)TmpData_p = ((SessionMeasurement_t*)TmpData_p)->NextDataBlock_p;
            //while(((SessionMeasurement_t*)TmpData_p)->NextDataBlock_p != NULL)
            while (TmpData_p != NULL) {
                Size = Size + (((SessionMeasurement_t *)TmpData_p)->ChunkNr * 4 * sizeof(uint32));
                Size += (4 * sizeof(uint32));
                //(SessionMeasurement_t*)TmpData_p = ((SessionMeasurement_t*)TmpData_p)->NextDataBlock_p;
                TmpData_p = (SessionMeasurement_t *)TmpData_p->NextDataBlock_p;
                (*SessionNr_p)++;
            }
        }

        break;

    default:
        Size = 0;
        break;

    }

    return Size;
}

/** @} */