summaryrefslogtreecommitdiffstats
path: root/ucs2-lib/src/ucs_amtp.c
blob: e4550d6a71af739e62ba0e87a4de770fbc6f4367 (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
/*------------------------------------------------------------------------------------------------*/
/* UNICENS V2.1.0-3491                                                                            */
/* Copyright (c) 2017 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 2 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 Tx Pool
 *
 * \cond UCS_INTERNAL_DOC
 * \addtogroup  G_AMTP
 * @{
 */

/*------------------------------------------------------------------------------------------------*/
/* Includes                                                                                       */
/*------------------------------------------------------------------------------------------------*/
#include "ucs_amtp.h"
#include "ucs_misc.h"

/*------------------------------------------------------------------------------------------------*/
/* Internal prototypes                                                                            */
/*------------------------------------------------------------------------------------------------*/
static void Amtp_OnMsgFreed(void *self, Ucs_AmsTx_Msg_t* msg_ptr);

/*------------------------------------------------------------------------------------------------*/
/* Initialization                                                                                 */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Constructor of class CAmtp
 *  \param self         The instance
 *  \param msg_ptr      Reference to an array of Amsg_IntMsgTx_t objects
 *  \param data_ptr     Reference to payload data which is required for the payload of all messages.
 *                      The data size must be the product of message_cnt and payload_cnt.
 *  \param msg_cnt      The number of message objects in the array
 *  \param payload_sz   The payload size for each message. The size must be a multiple of "4".
 *  \param ucs_user_ptr User reference that needs to be passed in every callback function
 */
void Amtp_Ctor(CAmtp *self, Amsg_IntMsgTx_t msg_ptr[], uint8_t data_ptr[], uint8_t msg_cnt, uint16_t payload_sz, void *ucs_user_ptr)
{
    uint8_t i = 0U;
    uint32_t mem_idx = 0U;
    Ucs_AmsTx_Msg_t *tx_ptr;

    self->ucs_user_ptr = ucs_user_ptr;
    TR_ASSERT(self->ucs_user_ptr, "[AMTP]", ((payload_sz % 4U) == 0U));          /* payload_sz shall be rounded to full quadlet */
    TR_ASSERT(self->ucs_user_ptr, "[AMTP]", ((payload_sz * msg_cnt) <= 65535U)); /* total data shall be referenced by uint32_t index */

    Dl_Ctor(&self->msg_queue, self->ucs_user_ptr);

    for (i = 0U; i < msg_cnt; i++)
    {
        tx_ptr = (Ucs_AmsTx_Msg_t*)(void*)&(msg_ptr[i]);
        Amsg_TxCtor(tx_ptr, NULL, &Amtp_OnMsgFreed, self);

        if (payload_sz > 0U)
        {
            Amsg_TxSetInternalPayload(tx_ptr, &data_ptr[mem_idx], payload_sz, NULL);
            mem_idx += payload_sz;
        }

        Amsg_TxEnqueue(tx_ptr, &self->msg_queue);
    }

}

/*! \brief  Retrieves a Tx application message object
 *  \param  self     The instance
 *  \return Retrieves the reference to a Tx application message object if the allocation
 *          succeeded, otherwise \c NULL.
 */
Ucs_AmsTx_Msg_t* Amtp_AllocMsg(CAmtp *self)
{
    return Amsg_TxDequeue(&self->msg_queue);
}

/*! \brief  Callback function which is invoked if the message object is freed
 *          by the AMS
 *  \param  self     The instance
 *  \param  msg_ptr  Reference to the freed application Tx message object 
 */
static void Amtp_OnMsgFreed(void *self, Ucs_AmsTx_Msg_t* msg_ptr)
{
    CAmtp *self_ = (CAmtp*)self;

    Amsg_TxReuse(msg_ptr);
    Amsg_TxEnqueue(msg_ptr, &self_->msg_queue);
}


/*!
 * @}
 * \endcond
 */

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