Logo
UNICENS V2.3.0-4567
User Manual and API Reference
Command Interpreter

Introduction

The Command Interpreter provides structures and functions for parsing incoming messages.

Features:

  • allows dynamic adding and removing of MessageId Table
  • returns a function pointer to the belonging handler function

The application provides a MessageId Table which contains all supported MessageIds together with their belonging handler functions. The MessageId Table is an array of Ucs_Cmd_MsgId_t elements. Each element contains a MessageId (0x0000 .. 0xFFFF) and a pointer to the belonging handler function.

Usage

The application announces the MessageId Table with the function Ucs_Cmd_AddMsgIdTable(). It The connection to the MessageId Table can be removed with the function Ucs_Cmd_RemoveMsgIdTable().

A received message is given to Ucs_Cmd_DecodeMsg(). This function retrieves the MessageId from the message and returns a pointer the belonging handler function. The application can then call the handler function. This functions returns a 16 bit value. The application defines the meaning of the possible return values.

Example

The following code example shows how the Command Interpreter API function can be used:

Ucs_Cmd_MsgId_t App_MsgIdTable[] =
{
{ 0x1234U, App_Handler_1234},
{ 0x1238U, App_Handler_1238}
};
void main(uint8_t argc, char *argv[])
{
...
Ucs_Cmd_AddMsgIdTable(ucs_inst_ptr, &App_MsgIdTable[0], sizeof(App_MsgIdTable)/sizeof(Ucs_Cmd_MsgId_t));
...
while (1)
{
Ucs_AmsRx_Msg_t *msg_rx_ptr = Ucs_AmsRx_PeekMsg(ucs_inst_ptr);
if (msg_rx_ptr != NULL)
{
uint16_t result;
fkt_ptr = Ucs_Cmd_DecodeMsg(ucs_inst_ptr, msg_rx_ptr);
if (fkt_ptr != NULL)
{
result = fkt_ptr(msg_ptr, APP_USER_PTR);
}
Ucs_AmsRx_ReleaseMsg(ucs_inst_ptr);
}
}
}
uint16_t App_Handler_1234(Ucs_AmsRx_Msg_t *msg_rx_ptr, void *user_ptr)
{
uint16_t cnt;
/* print source address */
(void)printf("App_Handler_1234() called from 0x%04X.\n", msg_rx_ptr->source_address);
/* print payload */
(void)printf(" Payload: ");
for (cnt = 0U; cnt < msg_rx_ptr->data_size; cnt++)
{
(void)printf("%02X ", msg_rx_ptr->data_ptr[cnt]);
}
(void)printf("\n");
return 0U;
}
uint16_t App_Handler_1238(Ucs_AmsRx_Msg_t *msg_rx_ptr, void *user_ptr)
{
uint16_t cnt;
/* print source address */
(void)printf("App_Handler_1238() called from 0x%04X.\n", msg_rx_ptr->source_address);
/* print payload */
(void)printf(" Payload: ");
for (cnt = 0U; cnt < msg_rx_ptr->data_size; cnt++)
{
(void)printf("%02X ", msg_rx_ptr->data_ptr[cnt]);
}
(void)printf("\n");
return 0U;
}