1 /*------------------------------------------------------------------------------------------------*/
2 /* UNICENS V2.1.0-3491 */
3 /* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG. */
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. */
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. */
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/>. */
18 /* You may also obtain this software under a propriety license from Microchip. */
19 /* Please contact Microchip for further information. */
20 /*------------------------------------------------------------------------------------------------*/
24 * \brief Implementation of Application Message Tx Pool
26 * \cond UCS_INTERNAL_DOC
31 /*------------------------------------------------------------------------------------------------*/
33 /*------------------------------------------------------------------------------------------------*/
37 /*------------------------------------------------------------------------------------------------*/
38 /* Internal prototypes */
39 /*------------------------------------------------------------------------------------------------*/
40 static void Amtp_OnMsgFreed(void *self, Ucs_AmsTx_Msg_t* msg_ptr);
42 /*------------------------------------------------------------------------------------------------*/
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
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)
57 uint32_t mem_idx = 0U;
58 Ucs_AmsTx_Msg_t *tx_ptr;
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 */
64 Dl_Ctor(&self->msg_queue, self->ucs_user_ptr);
66 for (i = 0U; i < msg_cnt; i++)
68 tx_ptr = (Ucs_AmsTx_Msg_t*)(void*)&(msg_ptr[i]);
69 Amsg_TxCtor(tx_ptr, NULL, &Amtp_OnMsgFreed, self);
73 Amsg_TxSetInternalPayload(tx_ptr, &data_ptr[mem_idx], payload_sz, NULL);
74 mem_idx += payload_sz;
77 Amsg_TxEnqueue(tx_ptr, &self->msg_queue);
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.
87 Ucs_AmsTx_Msg_t* Amtp_AllocMsg(CAmtp *self)
89 return Amsg_TxDequeue(&self->msg_queue);
92 /*! \brief Callback function which is invoked if the message object is freed
94 * \param self The instance
95 * \param msg_ptr Reference to the freed application Tx message object
97 static void Amtp_OnMsgFreed(void *self, Ucs_AmsTx_Msg_t* msg_ptr)
99 CAmtp *self_ = (CAmtp*)self;
101 Amsg_TxReuse(msg_ptr);
102 Amsg_TxEnqueue(msg_ptr, &self_->msg_queue);
111 /*------------------------------------------------------------------------------------------------*/
113 /*------------------------------------------------------------------------------------------------*/