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 the Command Interpreter.
26 * \cond UCS_INTERNAL_DOC
28 * \addtogroup G_UCS_CMD_INT
33 /*------------------------------------------------------------------------------------------------*/
35 /*------------------------------------------------------------------------------------------------*/
39 /*------------------------------------------------------------------------------------------------*/
40 /* Internal prototypes */
41 /*------------------------------------------------------------------------------------------------*/
44 static Ucs_Cmd_Return_t Cmd_SearchMsgId(Ucs_Cmd_MsgId_t msg_id_tab[], uint16_t *index_ptr,
48 /*------------------------------------------------------------------------------------------------*/
50 /*------------------------------------------------------------------------------------------------*/
51 void Cmd_Ctor(CCmd *self, CBase *base_ptr)
53 MISC_MEM_SET((void *)self, 0, sizeof(*self)); /* reset members to "0" */
55 self->msg_id_tab_ptr = NULL;
56 self->ucs_user_ptr = base_ptr->ucs_user_ptr;
60 /*! \brief Add a MessageId Table to the Command Interpreter.
61 * \param self Instance pointer
62 * \param msg_id_tab_ptr Reference to a MessageId Table
63 * \return Possible return values are shown in the table below.
65 * ------------------------------- | ------------------------------------
66 * UCS_CMD_RET_SUCCESS | MessageId Table was successfully added
67 * UCS_CMD_RET_ERR_ALREADY_ENTERED | MessageId Table already added
69 Ucs_Cmd_Return_t Cmd_AddMsgIdTable(CCmd *self, Ucs_Cmd_MsgId_t *msg_id_tab_ptr)
71 Ucs_Cmd_Return_t ret_val = UCS_CMD_RET_SUCCESS;
74 if (self->msg_id_tab_ptr != NULL)
76 ret_val = UCS_CMD_RET_ERR_ALREADY_ENTERED;
80 self->msg_id_tab_ptr = msg_id_tab_ptr;
86 /*! \brief Remove an MessageId Table from the Command Interpreter.
87 * \param self Instance pointer of Cmd
88 * \return Possible return values are shown in the table below.
90 * ---------------------------- | ------------------------------------
91 * UCS_CMD_RET_SUCCESS | MessageId Table was successfully removed
93 Ucs_Cmd_Return_t Cmd_RemoveMsgIdTable(CCmd *self)
95 Ucs_Cmd_Return_t ret_val = UCS_CMD_RET_SUCCESS;
97 self->msg_id_tab_ptr = NULL;
103 /*! \brief Decode an MCM message
104 * \param self Instance pointer
105 * \param msg_rx_ptr Pointer to the message to decode
106 * \return Possible return values are shown in the table below.
107 * Value | Description
108 * -------------------------------- | ------------------------------------
109 * UCS_CMD_RET_SUCCESS | decoding was successful
110 * UCS_CMD_RET_ERR_MSGID_NOTAVAIL | MessageId not found
111 * UCS_CMD_RET_ERR_TX_BUSY | no Tx Buffer available
112 * UCS_CMD_RET_ERR_APPL | error happened in handler function
113 * UCS_CMD_RET_ERR_NULL_PTR | No MessageId Table available
115 Ucs_Cmd_Return_t Cmd_DecodeMsg(CCmd *self, Ucs_AmsRx_Msg_t *msg_rx_ptr)
117 Ucs_Cmd_Return_t result = UCS_CMD_RET_SUCCESS;
120 result = Cmd_SearchMsgId(self->msg_id_tab_ptr, &index, msg_rx_ptr->msg_id);
122 if (result == UCS_CMD_RET_SUCCESS)
124 /* call handler function */
125 result = (Ucs_Cmd_Return_t)(self->msg_id_tab_ptr[index].handler_function_ptr(msg_rx_ptr, self->ucs_user_ptr));
132 /*! \brief Search in a MessageId Table for matching MessageId
133 * \details Function expects that the MessageId Table ends with a termination entry
134 * (handler_function_ptr == NULL). If this entry is not present, the search may end in an
136 * \param msg_id_tab MessageId Table
137 * \param index_ptr pointer to the matching element
138 * \param message_id MessageId
139 * \return Possible return values are shown in the table below.
140 * Value | Description
141 * ------------------------------- | ------------------------------------
142 * UCS_CMD_RET_SUCCESS | decoding was successful
143 * UCS_CMD_RET_ERR_MSGID_NOTAVAIL | MessageId not found
144 * UCS_CMD_RET_ERR_NULL_PTR | No MessageId Table available
146 static Ucs_Cmd_Return_t Cmd_SearchMsgId(Ucs_Cmd_MsgId_t msg_id_tab[], uint16_t *index_ptr,
149 Ucs_Cmd_Return_t ret_val = UCS_CMD_RET_SUCCESS;
152 if (msg_id_tab == NULL)
154 ret_val = UCS_CMD_RET_ERR_NULL_PTR;
158 while (msg_id_tab[i].handler_function_ptr != NULL) /* last entry */
160 if (msg_id_tab[i].msg_id != message_id)
162 ++i; /* goto next list element */
171 if (msg_id_tab[i].handler_function_ptr == NULL) /* no match found */
173 ret_val = UCS_CMD_RET_ERR_MSGID_NOTAVAIL;
188 /*------------------------------------------------------------------------------------------------*/
190 /*------------------------------------------------------------------------------------------------*/