e4550d6a71af739e62ba0e87a4de770fbc6f4367
[apps/agl-service-unicens.git] / ucs2-lib / src / ucs_amtp.c
1 /*------------------------------------------------------------------------------------------------*/
2 /* UNICENS V2.1.0-3491                                                                            */
3 /* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG.                              */
4 /*                                                                                                */
5 /* This program is free software: you can redistribute it and/or modify                           */
6 /* it under the terms of the GNU General Public License as published by                           */
7 /* the Free Software Foundation, either version 2 of the License, or                              */
8 /* (at your option) any later version.                                                            */
9 /*                                                                                                */
10 /* This program is distributed in the hope that it will be useful,                                */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of                                 */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                  */
13 /* GNU General Public License for more details.                                                   */
14 /*                                                                                                */
15 /* You should have received a copy of the GNU General Public License                              */
16 /* along with this program.  If not, see <http://www.gnu.org/licenses/>.                          */
17 /*                                                                                                */
18 /* You may also obtain this software under a propriety license from Microchip.                    */
19 /* Please contact Microchip for further information.                                              */
20 /*------------------------------------------------------------------------------------------------*/
21
22 /*!
23  * \file
24  * \brief Implementation of Application Message Tx Pool
25  *
26  * \cond UCS_INTERNAL_DOC
27  * \addtogroup  G_AMTP
28  * @{
29  */
30
31 /*------------------------------------------------------------------------------------------------*/
32 /* Includes                                                                                       */
33 /*------------------------------------------------------------------------------------------------*/
34 #include "ucs_amtp.h"
35 #include "ucs_misc.h"
36
37 /*------------------------------------------------------------------------------------------------*/
38 /* Internal prototypes                                                                            */
39 /*------------------------------------------------------------------------------------------------*/
40 static void Amtp_OnMsgFreed(void *self, Ucs_AmsTx_Msg_t* msg_ptr);
41
42 /*------------------------------------------------------------------------------------------------*/
43 /* Initialization                                                                                 */
44 /*------------------------------------------------------------------------------------------------*/
45 /*! \brief Constructor of class CAmtp
46  *  \param self         The instance
47  *  \param msg_ptr      Reference to an array of Amsg_IntMsgTx_t objects
48  *  \param data_ptr     Reference to payload data which is required for the payload of all messages.
49  *                      The data size must be the product of message_cnt and payload_cnt.
50  *  \param msg_cnt      The number of message objects in the array
51  *  \param payload_sz   The payload size for each message. The size must be a multiple of "4".
52  *  \param ucs_user_ptr User reference that needs to be passed in every callback function
53  */
54 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)
55 {
56     uint8_t i = 0U;
57     uint32_t mem_idx = 0U;
58     Ucs_AmsTx_Msg_t *tx_ptr;
59
60     self->ucs_user_ptr = ucs_user_ptr;
61     TR_ASSERT(self->ucs_user_ptr, "[AMTP]", ((payload_sz % 4U) == 0U));          /* payload_sz shall be rounded to full quadlet */
62     TR_ASSERT(self->ucs_user_ptr, "[AMTP]", ((payload_sz * msg_cnt) <= 65535U)); /* total data shall be referenced by uint32_t index */
63
64     Dl_Ctor(&self->msg_queue, self->ucs_user_ptr);
65
66     for (i = 0U; i < msg_cnt; i++)
67     {
68         tx_ptr = (Ucs_AmsTx_Msg_t*)(void*)&(msg_ptr[i]);
69         Amsg_TxCtor(tx_ptr, NULL, &Amtp_OnMsgFreed, self);
70
71         if (payload_sz > 0U)
72         {
73             Amsg_TxSetInternalPayload(tx_ptr, &data_ptr[mem_idx], payload_sz, NULL);
74             mem_idx += payload_sz;
75         }
76
77         Amsg_TxEnqueue(tx_ptr, &self->msg_queue);
78     }
79
80 }
81
82 /*! \brief  Retrieves a Tx application message object
83  *  \param  self     The instance
84  *  \return Retrieves the reference to a Tx application message object if the allocation
85  *          succeeded, otherwise \c NULL.
86  */
87 Ucs_AmsTx_Msg_t* Amtp_AllocMsg(CAmtp *self)
88 {
89     return Amsg_TxDequeue(&self->msg_queue);
90 }
91
92 /*! \brief  Callback function which is invoked if the message object is freed
93  *          by the AMS
94  *  \param  self     The instance
95  *  \param  msg_ptr  Reference to the freed application Tx message object 
96  */
97 static void Amtp_OnMsgFreed(void *self, Ucs_AmsTx_Msg_t* msg_ptr)
98 {
99     CAmtp *self_ = (CAmtp*)self;
100
101     Amsg_TxReuse(msg_ptr);
102     Amsg_TxEnqueue(msg_ptr, &self_->msg_queue);
103 }
104
105
106 /*!
107  * @}
108  * \endcond
109  */
110
111 /*------------------------------------------------------------------------------------------------*/
112 /* End of file                                                                                    */
113 /*------------------------------------------------------------------------------------------------*/
114