summaryrefslogtreecommitdiffstats
path: root/mnsl/mns_amsmessage.c
blob: 11ba985699a47b191bb564a4baad2915f5f813da (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
/*
 * MOST NetServices "Light" V3.2.7.0.1796 MultiInstance Patch
 *
 * Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You may also obtain this software under a propriety license from Microchip.
 * Please contact Microchip for further information.
 *
 */

/*!
 * \file
 * \brief Implementation of Application Message Classes
 *
 * \cond MNS_INTERNAL_DOC
 * \addtogroup  G_AMSMSG
 * @{
 */

/*------------------------------------------------------------------------------------------------*/
/* Includes                                                                                       */
/*------------------------------------------------------------------------------------------------*/
#include "mns_amsmessage.h"
#include "mns_dl.h"
#include "mns_misc.h"
#include "mns_trace.h"

#define SELF_RX     ((Amsg_IntMsgRx_t*)(void*)(self))
#define SELF_TX     ((Amsg_IntMsgTx_t*)(void*)(self))

#define AMSG_TX_BACKUP_ADDR_NONE    0U

/*------------------------------------------------------------------------------------------------*/
/* Prototypes                                                                                     */
/*------------------------------------------------------------------------------------------------*/
static Mns_AmsRx_ReceiveType_t Amsg_RxGetReceiveType(uint16_t destination_address);
static void Amsg_TxRestoreDestinationAddr(Mns_AmsTx_Msg_t *self);

/*------------------------------------------------------------------------------------------------*/
/* Tx Message                                                                                      */
/*------------------------------------------------------------------------------------------------*/
/*! \brief      Initializes aggregated objects
 *  \details    Needs to be called once before first usage. Call Amsg_TxHandleSetup() before 
 *              repeated usage. 
 *  \param      self          Reference to an internal Application Message Tx handle
 *  \param      info_ptr      Memory information required to free the object
 *  \param      free_fptr     Callback function which is invoked when the object is freed
 *  \param      free_inst_ptr The instance which is passed to free_fptr 
 */
void Amsg_TxCtor(Mns_AmsTx_Msg_t *self, void *info_ptr, Amsg_TxFreedCb_t free_fptr, void *free_inst_ptr)
{
    /* cleanup complete object */
    MISC_MEM_SET((void*)self, 0, sizeof(Amsg_IntMsgTx_t));

    /* reset default references
    SELF_TX->memory_ptr        = NULL;
    SELF_TX->memory_sz         = NULL;
    SELF_TX->memory_info_ptr   = NULL;

    SELF_TX->complete_fptr     = NULL; 
    SELF_TX->complete_inst_ptr = NULL;
    SELF_TX->complete_sia_fptr = NULL; 
    
    SELF_TX->backup_dest_address = AMSG_TX_BACKUP_ADDR_NONE;*/

    SELF_TX->info_ptr = info_ptr;
    SELF_TX->free_fptr = free_fptr;
    SELF_TX->free_inst_ptr = free_inst_ptr;
    SELF_TX->next_segment_cnt = 0xFFFFU;            /* start with TelId "4" */
    SELF_TX->temp_result = MNS_MSG_STAT_OK;
    SELF_TX->internal_status = AMSG_TX_INTRES_NONE;
    SELF_TX->ignore_wrong_target = false;

    Dln_Ctor(&SELF_TX->node, self);                 /* initialize node */
}

/*! \brief  Sets payload memory provided by memory management and updates data pointer and size
 *  \param  self            The instance
 *  \param  mem_ptr         Reference to the provided memory chunk
 *  \param  mem_size        Size of the provided memory chunk
 *  \param  mem_info_ptr    Optional reference for memory management
 */
void Amsg_TxSetInternalPayload(Mns_AmsTx_Msg_t *self, uint8_t *mem_ptr, uint16_t mem_size, void *mem_info_ptr)
{
    SELF_TX->memory_ptr        = mem_ptr;
    SELF_TX->memory_sz         = mem_size;
    SELF_TX->memory_info_ptr   = mem_info_ptr;

    SELF_TX->pb_msg.data_ptr   = mem_ptr;
    SELF_TX->pb_msg.data_size  = mem_size;
}

/*! \brief   Prepares the message object for re-usage
 *  \details The public message structure is re-initialized. The internal payload
 *           is assigned to the public data reference.
 *  \param   self            The instance
 */
void Amsg_TxReuse(Mns_AmsTx_Msg_t *self)
{
    MISC_MEM_SET((void *)&SELF_TX->pb_msg, 0, sizeof(SELF_TX->pb_msg));  /* cleanup public object */
    /* SELF_TX->backup_dest_address = AMSG_TX_BACKUP_ADDR_NONE; */

    SELF_TX->pb_msg.data_ptr   = SELF_TX->memory_ptr;   /* reset public payload */
    SELF_TX->pb_msg.data_size  = SELF_TX->memory_sz;

    SELF_TX->next_segment_cnt  = 0xFFFFU;               /* start with TelId "4" */
    SELF_TX->temp_result       = MNS_MSG_STAT_OK;
    SELF_TX->internal_status   = AMSG_TX_INTRES_NONE;
}

/*! \brief   Assigns a Tx complete callback function
 *  \details It is not possible to assign the single and multiple instance callback
 *           at the same time. This function shall be called before message transmission. 
 *  \param   self     The instance
 *  \param   compl_sia_fptr  Reference to the single instance callback function
 *  \param   compl_fptr      Reference to a multiple instance callback function
 *  \param   compl_inst_ptr  Instance which is invoked by compl_fptr()
 */
void Amsg_TxSetCompleteCallback(Mns_AmsTx_Msg_t *self, Amsg_TxCompleteSiaCb_t compl_sia_fptr, 
                                Amsg_TxCompleteCb_t compl_fptr, void* compl_inst_ptr)
{
    SELF_TX->complete_sia_fptr = compl_sia_fptr;
    SELF_TX->complete_fptr     = compl_fptr;
    SELF_TX->complete_inst_ptr = compl_inst_ptr;
}

/*! \brief  Invokes the correct callback function to notify the transmission result
 *          and frees the memory
 *  \param  self    Reference to the related message object
 *  \param  result  The transmission result
 *  \param  info    The INIC transmission result
 */
void Amsg_TxNotifyComplete(Mns_AmsTx_Msg_t *self, Mns_AmsTx_Result_t result, Mns_AmsTx_Info_t info)
{
    Amsg_TxRestoreDestinationAddr(self); 

    if (SELF_TX->complete_sia_fptr != NULL)                      /* invoke single instance API callback */
    {
        SELF_TX->complete_sia_fptr(self, result, info);
    }
    else if (SELF_TX->complete_fptr != NULL)
    {
        SELF_TX->complete_fptr(SELF_TX->complete_inst_ptr, self, result, info);
    }

    TR_ASSERT(0xFU, "[AMSG_TX]", (SELF_TX->free_fptr != NULL));
    if (SELF_TX->free_fptr != NULL)
    {
        SELF_TX->free_fptr(SELF_TX->free_inst_ptr, self);
    }
}

/*! \brief  Frees an unused message object to the owning pool
 *  \param  self    Reference to the message object
 */
void Amsg_TxFreeUnused(Mns_AmsTx_Msg_t *self)
{
    TR_ASSERT(0xFU, "[AMSG_TX]", (SELF_TX->free_fptr != NULL));
    if (SELF_TX->free_fptr != NULL)
    {
        SELF_TX->free_fptr(SELF_TX->free_inst_ptr, self);
    }
}

/*! \brief    Updates the transmission result
 *  \param    self    Reference to the related message object
 *  \param    result  The latest MCM transmission result
 *  \details  Since the transmission result of an application message may
 *            consist of multiple telegram transmission results, it is 
 *            important to store the final transmission error. An error cannot
 *            be overwritten by a success.
 */
void Amsg_TxUpdateResult(Mns_AmsTx_Msg_t *self, Mns_MsgTxStatus_t result)
{
    if (result != MNS_MSG_STAT_OK)            /* store the latest error and do not overwrite with success */
    {
        SELF_TX->temp_result = result;
    }
}

/*! \brief    Updates the internal transmission result
 *  \param    self    Reference to the related message object
 *  \param    result  The internal transmission result
 *  \details  The internal transmission result must be updated if the 
 *            the message is transmitted to the internal Rx queue.
 */
void Amsg_TxUpdateInternalResult(Mns_AmsTx_Msg_t *self, Amsg_TxIntStatus_t result)
{
    SELF_TX->internal_status = result;
}

/*! \brief  Returns the latest AMS transmission result code
 *  \param  self    Reference to the related message object
 *  \return Returns the transmission result which shall be notified to the application
 */
Mns_AmsTx_Result_t Amsg_TxGetResultCode(Mns_AmsTx_Msg_t *self)
{
    Mns_AmsTx_Result_t res = MNS_AMSTX_RES_SUCCESS;                 /* success is the expected result */

    switch (SELF_TX->temp_result)
    {
        case MNS_MSG_STAT_OK:
            if (SELF_TX->internal_status == AMSG_TX_INTRES_ERRBUF)
            {
                res = MNS_AMSTX_RES_ERR_BUF_INTERNAL;               /* internal transmission error overrules network success */
            }
            break;
        case MNS_MSG_STAT_ERROR_BF: 
        case MNS_MSG_STAT_ERROR_CRC: 
        case MNS_MSG_STAT_ERROR_ID: 
        case MNS_MSG_STAT_ERROR_ACK: 
        case MNS_MSG_STAT_ERROR_TIMEOUT:
            res = MNS_AMSTX_RES_ERR_RETRIES_EXP;                    /* transmission failed, retries are possible */
            break;
        case MNS_MSG_STAT_ERROR_FATAL_WT:
        case MNS_MSG_STAT_ERROR_FATAL_OA:
            if (SELF_TX->internal_status == AMSG_TX_INTRES_ERRBUF)
            {
                res = MNS_AMSTX_RES_ERR_BUF_INTERNAL;               /* internal transmission error and network node not found */
            }
            else if (SELF_TX->internal_status == AMSG_TX_INTRES_NONE)
            {
                res = MNS_AMSTX_RES_ERR_INVALID_TGT;                /* not transmitted internally and no network node found */
            }
            /* else -> internal success -> target node was found locally */
            break;
        case MNS_MSG_STAT_ERROR_NA_TRANS:
        case MNS_MSG_STAT_ERROR_NA_OFF:
            if (SELF_TX->internal_status != AMSG_TX_INTRES_SUCCESS)
            {
                res = MNS_AMSTX_RES_ERR_NOT_AVAILABLE;              /* successful if internal transmission succeeded, otherwise "not available" */
            }
            break;
        case MNS_MSG_STAT_ERROR_SYNC:
            res = MNS_AMSTX_RES_ERR_NOT_AVAILABLE;
            break;
        default:
            res = MNS_AMSTX_RES_ERR_UNEXPECTED;                     /* unexpected network transmission state */
            break;
    }

    return res;
}

/*! \brief  Returns the latest MCM transmission error
 *  \param  self    Reference to the related message object
 *  \return Returns the INIC transmission result which is provided as additional info
 */
Mns_AmsTx_Info_t Amsg_TxGetResultInfo(Mns_AmsTx_Msg_t *self)
{
    Mns_AmsTx_Info_t res = (Mns_AmsTx_Info_t)SELF_TX->temp_result;

    if ((SELF_TX->temp_result == MNS_MSG_STAT_ERROR_FATAL_WT) && (SELF_TX->ignore_wrong_target != false))
    {
        res = MNS_AMSTX_I_SUCCESS;
    }

    return res;
}

/*! \brief  Queues a Tx message at the tail of a list
 *  \param  self     The instance
 *  \param  list_ptr Reference to the list
 */
void Amsg_TxEnqueue(Mns_AmsTx_Msg_t* self, CDlList* list_ptr)
{
    Dl_InsertTail(list_ptr, &SELF_TX->node);
}

/*! \brief  Retrieves the next segment count
 *  \param  self     The instance
 *  \return The next segment count as uint16_t
 */
uint16_t Amsg_TxGetNextSegmCnt(Mns_AmsTx_Msg_t *self)
{
    return SELF_TX->next_segment_cnt;
}

/*! \brief  Increments the next segment count
 *  \param  self     The instance
 */
void Amsg_TxIncrementNextSegmCnt(Mns_AmsTx_Msg_t *self)
{
    SELF_TX->next_segment_cnt++;
}

/*! \brief  Retrieves the follower id which labels all telegrams of a segmented message
 *  \param  self     The instance
 *  \return The follower id
 */
uint8_t Amsg_TxGetFollowerId(Mns_AmsTx_Msg_t *self)
{
    return SELF_TX->follower_id;
}

/*! \brief  Sets the follower id which labels all telegrams of a segmented message
 *  \param  self     The instance
 *  \param  id       The follower id
 */
void Amsg_TxSetFollowerId(Mns_AmsTx_Msg_t *self, uint8_t id)
{
    SELF_TX->follower_id = id;
}

/*! \brief   Replaces the current destination address by a new one. 
 *  \details The current destination address can be restore by Amsg_TxRestoreDestinationAddr().
 *  \param   self               The instance
 *  \param   new_destination    The new destination address
 */
void Amsg_TxReplaceDestinationAddr(Mns_AmsTx_Msg_t *self, uint16_t new_destination)
{
    SELF_TX->backup_dest_address = self->destination_address;    /* internal backup of current destination address */
    self->destination_address = new_destination;                 /* replace public destination address */
}

/*! \brief   Restores the destination address which was saved by calling Amsg_TxReplaceDestinationAddr().
 *  \param   self               The instance
 */
static void Amsg_TxRestoreDestinationAddr(Mns_AmsTx_Msg_t *self)
{
    if (SELF_TX->backup_dest_address != AMSG_TX_BACKUP_ADDR_NONE)
    {
        self->destination_address = SELF_TX->backup_dest_address;/* restore public destination address */ 
    }
}

/*! \brief  Removes a message from a given queue
 *  \param  self     The instance
 *  \param  list_ptr The queue that contains the message
 */
void Amsg_TxRemoveFromQueue(Mns_AmsTx_Msg_t *self, CDlList *list_ptr)
{
    (void)Dl_Remove(list_ptr, &SELF_TX->node);
}

/*! \brief  Peeks a Tx message from the head of a list
 *  \param  list_ptr Reference to the list
 *  \return Reference to the Tx message
 */
Mns_AmsTx_Msg_t* Amsg_TxPeek(CDlList* list_ptr)
{
    Mns_AmsTx_Msg_t *msg_ptr = NULL;
    CDlNode *node_ptr = Dl_PeekHead(list_ptr);

    if (node_ptr != NULL)
    {
        msg_ptr = (Mns_AmsTx_Msg_t*)Dln_GetData(node_ptr);
    }

    return msg_ptr;
}

/*! \brief  Removes a Tx message from the head of a list
 *  \param  list_ptr Reference to the list
 *  \return Reference to the Tx message
 */
Mns_AmsTx_Msg_t* Amsg_TxDequeue(CDlList* list_ptr)
{
    Mns_AmsTx_Msg_t *msg_ptr = NULL;
    CDlNode *node_ptr = Dl_PopHead(list_ptr);

    if (node_ptr != NULL)
    {
        msg_ptr = (Mns_AmsTx_Msg_t*)Dln_GetData(node_ptr);
    }

    return msg_ptr;
}

/*------------------------------------------------------------------------------------------------*/
/* Rx Message                                                                                     */
/*------------------------------------------------------------------------------------------------*/
/*! \brief      Initializes aggregated objects
 *  \details    Needs to be called once before first usage. Call Amsg_RxHandleSetup() before 
 *              repeated usage. 
 *  \param      self     Reference to an internal Application Message Rx handle
 *  \param      info_ptr Memory information required to free the object
 */
void Amsg_RxCtor(Mns_AmsRx_Msg_t *self, void *info_ptr)
{
    Dln_Ctor(&SELF_RX->node, SELF_RX);
    SELF_RX->info_ptr          = info_ptr;                              /* reset memory information */
    SELF_RX->memory_sz         = 0U;
    SELF_RX->memory_ptr        = NULL;
    SELF_RX->memory_info_ptr   = NULL;
}

/*! \brief      Copies all attributes and payload from a Tx message to the Rx message
 *  \details    The caller has to ensure that the payload size of the Rx message is equal
 *              or greater than the payload size of the Tx message.
 *  \param      self           Reference to an Rx message object
 *  \param      tx_ptr         Reference to an Tx message object
 *  \param      source_address The source address that shall be set in the Rx message
 */
void Amsg_RxBuildFromTx(Mns_AmsRx_Msg_t *self, Mns_AmsTx_Msg_t *tx_ptr, uint16_t source_address)
{
    TR_ASSERT(0xFU,"[AMSG]", (SELF_RX->memory_sz >= tx_ptr->data_size));

    self->receive_type      = Amsg_RxGetReceiveType(tx_ptr->destination_address);
    self->source_address    = source_address;
    self->fblock_id         = tx_ptr->fblock_id;
    self->instance_id       = tx_ptr->instance_id;
    self->function_id       = tx_ptr->function_id;
    self->op_type           = tx_ptr->op_type;
    self->data_size         = tx_ptr->data_size;

    Misc_MemCpy(self->data_ptr, tx_ptr->data_ptr, (size_t)self->data_size);
}

/*! \brief      Sets all attributes of an internal Rx message to valid values
 *  \param      self    Reference to an internal Rx message object
 *  \details    Assigned payload memory has to be freed before calling this function
 */
void Amsg_RxHandleSetup(Mns_AmsRx_Msg_t *self)
{
    MISC_MEM_SET((void *)&SELF_RX->pb_msg, 0, sizeof(SELF_RX->pb_msg)); /* cleanup public message object */
    SELF_RX->pb_msg.data_ptr   = SELF_RX->memory_ptr;                   /* set data to valid memory */
    SELF_RX->gc_marker         = false;                                 /* reset garbage collector flag */
    SELF_RX->exp_tel_cnt       = 0U;                                    /* reset TelCnt */
}

/*! \brief  Evaluates if an Application Message has the same functional address
 *          as a MOST telegram
 *  \param  self    Reference to an internal Application Message Rx handle
 *  \param  tel_ptr Reference to a MOST message object
 *  \return Returns \c true if both message objects have the same functional address,
 *          otherwise \c false.
 */
bool Amsg_RxHandleIsIdentical(Mns_AmsRx_Msg_t *self, Msg_MostTel_t *tel_ptr)
{
    bool result;

    if ((self->source_address == tel_ptr->source_addr)
        && (self->fblock_id == tel_ptr->id.fblock_id)
        && (self->instance_id == tel_ptr->id.instance_id)
        && (self->function_id == tel_ptr->id.function_id)
        && (self->op_type == tel_ptr->id.op_type))
    {
        result = true;
    }
    else
    {
        result = false;
    }

    return result;
}

/*! \brief  Copies the Rx message signature from a MOST message object to an 
 *          internal Application message object
 *  \param  self    Reference to an internal Application Message Rx handle
 *  \param  src_ptr Reference to a MOST message object
 */
void Amsg_RxCopySignatureFromTel(Mns_AmsRx_Msg_t *self, Msg_MostTel_t* src_ptr)
{
    self->source_address   = src_ptr->source_addr;
    self->receive_type     = Amsg_RxGetReceiveType(src_ptr->destination_addr);
    self->fblock_id        = src_ptr->id.fblock_id;
    self->instance_id      = src_ptr->id.instance_id;
    self->function_id      = src_ptr->id.function_id;
    self->op_type          = src_ptr->id.op_type;
}

/*! \brief  Copies the Rx message signature from an internal Application
 *          message object to a MOST message object
 *  \param  self     Reference to an internal Application Message Rx handle
 *  \param  target_ptr  Reference to a MOST message object
 */
void Amsg_RxCopySignatureToTel(Mns_AmsRx_Msg_t *self, Msg_MostTel_t* target_ptr)
{
    target_ptr->source_addr        = self->source_address;
    target_ptr->destination_addr   = MNS_ADDR_DEBUG;
    target_ptr->id.fblock_id       = self->fblock_id;
    target_ptr->id.instance_id     = self->instance_id;
    target_ptr->id.function_id     = self->function_id;
    target_ptr->id.op_type         = self->op_type;
}

/*! \brief  Retrieves the addressing type related to a destination_address of an Rx message
 *  \param  destination_address  The destination address of an Rx message
 *  \return The receive type related to the destination address
 */
static Mns_AmsRx_ReceiveType_t Amsg_RxGetReceiveType(uint16_t destination_address)
{
    Mns_AmsRx_ReceiveType_t ret = MNS_AMSRX_RCT_SINGLECAST;

    if ((destination_address == MNS_ADDR_BROADCAST_BLOCKING) ||
        (destination_address == MNS_ADDR_BROADCAST_UNBLOCKING))
    {
        ret = MNS_AMSRX_RCT_BROADCAST;
    }
    else if ((destination_address >= 0x0300U) &&        /* 0x300..0x3FF is reserved for group cast */
             (destination_address <  0x0400U))
    {
        ret = MNS_AMSRX_RCT_GROUPCAST;
    }

    return ret;
}

/*! \brief  Appends payload of an Rx MOST message object to internal Application
 *          message object
 *  \param  self    Reference to an internal Application Message Rx handle
 *  \param  src_ptr Reference to a MOST message object
 *  \return Returns \c true if the payload was appended successfully, 
 *          otherwise \c false.
 */
bool Amsg_RxAppendPayload(Mns_AmsRx_Msg_t *self, Msg_MostTel_t* src_ptr)
{
    uint8_t cnt;
    bool ret = false;
    const uint16_t curr_size = SELF_RX->pb_msg.data_size;                /* get current message size */

    if ((SELF_RX->memory_sz - src_ptr->tel.tel_len) >= SELF_RX->pb_msg.data_size) /* is size sufficient */
    {
        for (cnt = 0U; cnt < src_ptr->tel.tel_len; cnt++)
        {
             SELF_RX->pb_msg.data_ptr[curr_size + (uint16_t)cnt] = src_ptr->tel.tel_data_ptr[cnt];
        }

         SELF_RX->pb_msg.data_size = curr_size + src_ptr->tel.tel_len;    /* update message size */
         SELF_RX->exp_tel_cnt++;
        ret = true;
    }

    return ret;
}

/*! \brief   Copies data to allocated payload buffer 
 *  \param   self       The instance
 *  \param   data       Reference to external payload data
 *  \param   data_sz    Size of external payload data 
 */
void Amsg_RxCopyToPayload(Mns_AmsRx_Msg_t *self, uint8_t data[], uint8_t data_sz)
{
    MISC_MEM_CPY(&self->data_ptr[0], &data[0], (size_t)data_sz); /* parasoft-suppress  MISRA2004-20_3 "data_sz is limited and checked via Msg_VerifyContent()" */
    self->data_size = data_sz;         /* remember payload size */
}

/*! \brief   Checks if the message has externally allocated payload memory
 *  \param   self       The instance
 *  \return  Returns \c true if external payload is assigned to the message, otherwise \c false.
 */
bool Amsg_RxHasExternalPayload(Mns_AmsRx_Msg_t *self)
{
    return (SELF_RX->memory_sz > 0U);
}

/*! \brief  Sets payload memory provided by memory management and updates data pointer and size
 *  \param  self     The instance
 *  \param  mem_ptr  Reference to the provided memory chunk
 *  \param  mem_size Size of the provided memory chunk
 *  \param  info_ptr Optional reference for memory management
 */
void Amsg_RxHandleSetMemory(Mns_AmsRx_Msg_t *self, uint8_t *mem_ptr, uint16_t mem_size, void *info_ptr)
{
    SELF_RX->memory_ptr        = mem_ptr;
    SELF_RX->memory_info_ptr   = info_ptr;
    SELF_RX->memory_sz         = mem_size;

    SELF_RX->pb_msg.data_ptr   = mem_ptr;
    SELF_RX->pb_msg.data_size  = 0U;
}

/*! \brief  Queues an Rx message at the tail of a list
 *  \param  self     The instance
 *  \param  list_ptr Reference to the list
 */
void Amsg_RxEnqueue(Mns_AmsRx_Msg_t* self, CDlList* list_ptr)
{
    Dl_InsertTail(list_ptr, &SELF_RX->node);
}

/*! \brief  Sets or resets the garbage collector flag
 *  \param  self    The instance
 *  \param  value   New value of the flag
 */
void Amsg_RxSetGcMarker(Mns_AmsRx_Msg_t* self, bool value)
{
    SELF_RX->gc_marker = value; 
}

/*! \brief  Retrieves the value of the garbage collector flag
 *  \param  self    The instance
 *  \return The current value of the flag
 */
bool Amsg_RxGetGcMarker(Mns_AmsRx_Msg_t* self)
{
    return SELF_RX->gc_marker;
}

/*! \brief  Retrieves the next expected telegram count
 *  \param  self    The instance
 *  \return The next  expected telegram count as uint8_t
 */
uint8_t Amsg_RxGetExpTelCnt(Mns_AmsRx_Msg_t* self)
{
    return SELF_RX->exp_tel_cnt;
}

/*! \brief  Peeks an Rx message from the head of a list
 *  \param  list_ptr Reference to the list
 *  \return Reference to the Rx message
 */
Mns_AmsRx_Msg_t* Amsg_RxPeek(CDlList* list_ptr)
{
    Mns_AmsRx_Msg_t *msg_ptr = NULL;
    CDlNode *node_ptr = Dl_PeekHead(list_ptr);

    if (node_ptr != NULL)
    {
        msg_ptr = (Mns_AmsRx_Msg_t*)Dln_GetData(node_ptr);
    }

    return msg_ptr;
}

/*! \brief  Removes an Rx message from the head of a list
 *  \param  list_ptr Reference to the list
 *  \return Reference to the Rx message
 */
Mns_AmsRx_Msg_t* Amsg_RxDequeue(CDlList* list_ptr)
{
    Mns_AmsRx_Msg_t *msg_ptr = NULL;
    CDlNode *node_ptr = Dl_PopHead(list_ptr);

    if (node_ptr != NULL)
    {
        msg_ptr = (Mns_AmsRx_Msg_t*)Dln_GetData(node_ptr);
    }

    return msg_ptr;
}

/*!
 * @}
 * \endcond
 */

/*------------------------------------------------------------------------------------------------*/
/* End of file                                                                                    */
/*------------------------------------------------------------------------------------------------*/