Fix template dir and data file dir to match new templating.
[apps/agl-service-unicens.git] / ucs2-lib / src / ucs_cmd.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 the Command Interpreter.
25  *
26  * \cond UCS_INTERNAL_DOC
27  *
28  * \addtogroup  G_UCS_CMD_INT
29  * @{
30  */
31
32
33 /*------------------------------------------------------------------------------------------------*/
34 /* Includes                                                                                       */
35 /*------------------------------------------------------------------------------------------------*/
36 #include "ucs_cmd.h"
37 #include "ucs_misc.h"
38
39 /*------------------------------------------------------------------------------------------------*/
40 /* Internal prototypes                                                                            */
41 /*------------------------------------------------------------------------------------------------*/
42
43
44 static Ucs_Cmd_Return_t Cmd_SearchMsgId(Ucs_Cmd_MsgId_t msg_id_tab[], uint16_t *index_ptr, 
45                                                uint16_t message_id);
46
47
48 /*------------------------------------------------------------------------------------------------*/
49 /* Implementation                                                                                 */
50 /*------------------------------------------------------------------------------------------------*/
51 void Cmd_Ctor(CCmd *self, CBase *base_ptr)
52 {
53     MISC_MEM_SET((void *)self, 0, sizeof(*self));                 /* reset members to "0" */
54
55     self->msg_id_tab_ptr = NULL;
56     self->ucs_user_ptr   = base_ptr->ucs_user_ptr;
57 }
58
59
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.
64  *  Value                           | Description 
65  *  ------------------------------- | ------------------------------------
66  *  UCS_CMD_RET_SUCCESS             | MessageId Table was successfully added
67  *  UCS_CMD_RET_ERR_ALREADY_ENTERED | MessageId Table already added 
68  */
69 Ucs_Cmd_Return_t Cmd_AddMsgIdTable(CCmd *self, Ucs_Cmd_MsgId_t *msg_id_tab_ptr)
70 {
71     Ucs_Cmd_Return_t ret_val = UCS_CMD_RET_SUCCESS;
72
73
74     if (self->msg_id_tab_ptr != NULL)
75     {
76         ret_val = UCS_CMD_RET_ERR_ALREADY_ENTERED;
77     }
78     else
79     {
80         self->msg_id_tab_ptr = msg_id_tab_ptr;
81     }
82
83     return ret_val;
84 }
85
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.
89  *  Value                        | Description 
90  *  ---------------------------- | ------------------------------------
91  * UCS_CMD_RET_SUCCESS           | MessageId Table was successfully removed
92  */
93 Ucs_Cmd_Return_t Cmd_RemoveMsgIdTable(CCmd *self)
94 {
95     Ucs_Cmd_Return_t ret_val = UCS_CMD_RET_SUCCESS;
96
97     self->msg_id_tab_ptr = NULL;
98
99     return ret_val;
100 }
101
102
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
114  */
115 Ucs_Cmd_Return_t Cmd_DecodeMsg(CCmd *self, Ucs_AmsRx_Msg_t *msg_rx_ptr)
116 {
117     Ucs_Cmd_Return_t result = UCS_CMD_RET_SUCCESS;
118     uint16_t         index;
119
120     result = Cmd_SearchMsgId(self->msg_id_tab_ptr, &index, msg_rx_ptr->msg_id);
121
122     if (result == UCS_CMD_RET_SUCCESS)
123     {
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));
126     }
127
128     return result;
129 }
130
131
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 
135  *           endless loop. 
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
145  */
146 static Ucs_Cmd_Return_t Cmd_SearchMsgId(Ucs_Cmd_MsgId_t msg_id_tab[], uint16_t *index_ptr, 
147                                         uint16_t message_id)
148 {
149     Ucs_Cmd_Return_t ret_val = UCS_CMD_RET_SUCCESS;
150     uint16_t i = 0U;
151
152     if (msg_id_tab == NULL)
153     {
154         ret_val = UCS_CMD_RET_ERR_NULL_PTR;
155     }
156     else
157     {
158         while (msg_id_tab[i].handler_function_ptr != NULL)        /* last entry */
159         {
160             if (msg_id_tab[i].msg_id != message_id)
161             {
162                 ++i;                                        /* goto next list element */
163             }
164             else
165             {
166                 *index_ptr   = i;
167                 break;
168             }
169         }
170
171         if (msg_id_tab[i].handler_function_ptr == NULL)               /* no match found */
172         {
173             ret_val = UCS_CMD_RET_ERR_MSGID_NOTAVAIL;
174         }
175     }
176     return ret_val;
177 }
178
179 /*!
180  * @}
181  * \endcond
182  */
183
184
185
186
187
188 /*------------------------------------------------------------------------------------------------*/
189 /* End of file                                                                                    */
190 /*------------------------------------------------------------------------------------------------*/
191