summaryrefslogtreecommitdiffstats
path: root/ucs2-lib/src/ucs_cmd.c
blob: 4865b8883dbc16febee76039fd0a4bdd2f561909 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*------------------------------------------------------------------------------------------------*/
/* 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 the Command Interpreter.
 *
 * \cond UCS_INTERNAL_DOC
 *
 * \addtogroup  G_UCS_CMD_INT
 * @{
 */


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

/*------------------------------------------------------------------------------------------------*/
/* Internal prototypes                                                                            */
/*------------------------------------------------------------------------------------------------*/


static Ucs_Cmd_Return_t Cmd_SearchMsgId(Ucs_Cmd_MsgId_t msg_id_tab[], uint16_t *index_ptr, 
                                               uint16_t message_id);


/*------------------------------------------------------------------------------------------------*/
/* Implementation                                                                                 */
/*------------------------------------------------------------------------------------------------*/
void Cmd_Ctor(CCmd *self, CBase *base_ptr)
{
    MISC_MEM_SET((void *)self, 0, sizeof(*self));                 /* reset members to "0" */

    self->msg_id_tab_ptr = NULL;
    self->ucs_user_ptr   = base_ptr->ucs_user_ptr;
}


/*! \brief  Add a MessageId Table to the Command Interpreter.
 *  \param  self            Instance pointer
 *  \param  msg_id_tab_ptr    Reference to a MessageId Table
 *  \return  Possible return values are shown in the table below.
 *  Value                           | Description 
 *  ------------------------------- | ------------------------------------
 *  UCS_CMD_RET_SUCCESS             | MessageId Table was successfully added
 *  UCS_CMD_RET_ERR_ALREADY_ENTERED | MessageId Table already added 
 */
Ucs_Cmd_Return_t Cmd_AddMsgIdTable(CCmd *self, Ucs_Cmd_MsgId_t *msg_id_tab_ptr)
{
    Ucs_Cmd_Return_t ret_val = UCS_CMD_RET_SUCCESS;


    if (self->msg_id_tab_ptr != NULL)
    {
        ret_val = UCS_CMD_RET_ERR_ALREADY_ENTERED;
    }
    else
    {
        self->msg_id_tab_ptr = msg_id_tab_ptr;
    }

    return ret_val;
}

/*! \brief   Remove an MessageId Table from the Command Interpreter.
 *  \param   self  Instance pointer of Cmd
 *  \return  Possible return values are shown in the table below.
 *  Value                        | Description 
 *  ---------------------------- | ------------------------------------
 * UCS_CMD_RET_SUCCESS           | MessageId Table was successfully removed
 */
Ucs_Cmd_Return_t Cmd_RemoveMsgIdTable(CCmd *self)
{
    Ucs_Cmd_Return_t ret_val = UCS_CMD_RET_SUCCESS;

    self->msg_id_tab_ptr = NULL;

    return ret_val;
}


/*! \brief  Decode an MCM message 
 *  \param  self            Instance pointer
 *  \param  msg_rx_ptr      Pointer to the message to decode
 *  \return  Possible return values are shown in the table below.
 *  Value                            | Description 
 *  -------------------------------- | ------------------------------------
 *  UCS_CMD_RET_SUCCESS              | decoding was successful
 *  UCS_CMD_RET_ERR_MSGID_NOTAVAIL   | MessageId not found 
 *  UCS_CMD_RET_ERR_TX_BUSY          | no Tx Buffer available
 *  UCS_CMD_RET_ERR_APPL             | error happened in handler function
 *  UCS_CMD_RET_ERR_NULL_PTR         | No MessageId Table available
 */
Ucs_Cmd_Return_t Cmd_DecodeMsg(CCmd *self, Ucs_AmsRx_Msg_t *msg_rx_ptr)
{
    Ucs_Cmd_Return_t result = UCS_CMD_RET_SUCCESS;
    uint16_t         index;

    result = Cmd_SearchMsgId(self->msg_id_tab_ptr, &index, msg_rx_ptr->msg_id);

    if (result == UCS_CMD_RET_SUCCESS)
    {
        /* call handler function */
        result = (Ucs_Cmd_Return_t)(self->msg_id_tab_ptr[index].handler_function_ptr(msg_rx_ptr, self->ucs_user_ptr));
    }

    return result;
}


/*! \brief  Search in a MessageId Table for matching MessageId
 *  \details Function expects that the MessageId Table ends with a termination entry 
 *           (handler_function_ptr == NULL). If this entry is not present, the search may end in an 
 *           endless loop. 
 *  \param   msg_id_tab         MessageId Table
 *  \param   index_ptr          pointer to the matching element
 *  \param   message_id         MessageId
 *  \return  Possible return values are shown in the table below.
 *  Value                           | Description 
 *  ------------------------------- | ------------------------------------
 *  UCS_CMD_RET_SUCCESS             | decoding was successful
 *  UCS_CMD_RET_ERR_MSGID_NOTAVAIL  | MessageId not found
 *  UCS_CMD_RET_ERR_NULL_PTR        | No MessageId Table available
 */
static Ucs_Cmd_Return_t Cmd_SearchMsgId(Ucs_Cmd_MsgId_t msg_id_tab[], uint16_t *index_ptr, 
                                        uint16_t message_id)
{
    Ucs_Cmd_Return_t ret_val = UCS_CMD_RET_SUCCESS;
    uint16_t i = 0U;

    if (msg_id_tab == NULL)
    {
        ret_val = UCS_CMD_RET_ERR_NULL_PTR;
    }
    else
    {
        while (msg_id_tab[i].handler_function_ptr != NULL)        /* last entry */
        {
            if (msg_id_tab[i].msg_id != message_id)
            {
                ++i;                                        /* goto next list element */
            }
            else
            {
                *index_ptr   = i;
                break;
            }
        }

        if (msg_id_tab[i].handler_function_ptr == NULL)               /* no match found */
        {
            ret_val = UCS_CMD_RET_ERR_MSGID_NOTAVAIL;
        }
    }
    return ret_val;
}

/*!
 * @}
 * \endcond
 */





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