1 /**
2 *********************************************************************************************************
3 *               Copyright(c) 2016, Realtek Semiconductor Corporation. All rights reserved.
4 **********************************************************************************************************
5 * @file     user_cmd_parse.h
6 * @brief    Parse user command from lower Data UART data.
7 * @details  Structure of command parse results.
8 * @author
9 * @date     2016-02-18
10 * @version  v0.1
11 *********************************************************************************************************
12 */
13 #ifndef __USER_CMD_PARSE_CMD_H
14 #define __USER_CMD_PARSE_CMD_H
15 
16 #ifdef  __cplusplus
17 extern "C" {
18 #endif      /* __cplusplus */
19 
20 #include <stdint.h>
21 #include <stdbool.h>
22 
23 /** @defgroup DATA_UART_CMD Data Uart Command Module
24   * @{
25   */
26 /** @defgroup USER_CMD_PARSE User Command Parse Module
27   * @{
28   */
29 /*============================================================================*
30  *                         Micros
31  *============================================================================*/
32 /** @defgroup USER_CMD_PARSE_Exported_Micros User Command Parse Module Exported Micros
33     @brief  command parse related macros.
34     * @{
35     */
36 
37 #define USER_CMD_MAX_COMMAND_LINE       70  /**< max. length of command line in bytes */
38 #define USER_CMD_MAX_HISTORY_LINE       3   /**< max. num of history command line */
39 #define USER_CMD_MAX_PARAMETERS         8   /**< max. number of parameters that the parser will scan */
40 /** End of USER_CMD_PARSE_Exported_Micros
41   * @}
42   */
43 
44 
45 /*============================================================================*
46  *                         Types
47  *============================================================================*/
48 /** @defgroup USER_CMD_PARSE_Exported_Types User Command Parse Module Exported Types
49     * @{
50     */
51 
52 /**
53 * @brief Data UART command parse result.
54 *
55 * This is the structure where the command line parser puts its result.
56 */
57 typedef enum
58 {
59     RESULT_SUCESS,                             //!< Operation success.
60     RESULT_GAP_CAUSE_ALREADY_IN_REQ     = 0x01,//!< Operation already in progress.
61     RESULT_GAP_CAUSE_INVALID_STATE      = 0x02,//!< Invalid state.
62     RESULT_GAP_CAUSE_INVALID_PARAM      = 0x03,//!< Invalid parameter.
63     RESULT_GAP_CAUSE_NON_CONN           = 0x04,//!< No connection establishment.
64     RESULT_GAP_CAUSE_NOT_FIND_IRK       = 0x05,//!< IRK not found.
65     RESULT_GAP_CAUSE_ERROR_CREDITS      = 0x06,//!< Credits error.
66     RESULT_GAP_CAUSE_SEND_REQ_FAILED    = 0x07,//!< Send Request failed.
67     RESULT_GAP_CAUSE_NO_RESOURCE        = 0x08,//!< No resource.
68     RESULT_GAP_CAUSE_INVALID_PDU_SIZE   = 0x09,//!< Invalid PDU size.
69     RESULT_GAP_CAUSE_NOT_FIND           = 0x0a,//!< Not Found.
70     RESULT_GAP_CAUSE_CONN_LIMIT         = 0x0b,//!< Connection reachs limited count.
71 
72     RESULT_ERR                          = 0x20,
73     RESULT_CMD_EMPTY_LINE               = 0x21,
74     RESULT_CMD_NOT_FOUND                = 0x22,
75     RESULT_CMD_ERR_PARAM                = 0x23,
76     RESULT_CMD_ERR_PARAM_NUM            = 0x24,
77     RESULT_CMD_OUT_OF_RANGE             = 0x25,
78     RESULT_CMD_NOT_SUPPORT              = 0x26,
79     RESULT_GAP_CAUSE_ERROR_UNKNOWN      = 0xFF,//!< Unknown error.
80 } T_USER_CMD_PARSE_RESULT;
81 
82 /**
83  * @brief Data UART command parse value.
84  *
85  * This is the structure where the command line parser puts its values.
86  */
87 typedef struct
88 {
89     char       *p_cmd;                              /**< pointer to command */
90     int32_t     param_count;                        /**< number of found parameters */
91     uint32_t    dw_param[USER_CMD_MAX_PARAMETERS];  /**< automatically parsed parameters */
92     char       *p_param[USER_CMD_MAX_PARAMETERS];   /**< automatically parsed parameters */
93 } T_USER_CMD_PARSED_VALUE;
94 
95 /** @brief Command interface. */
96 typedef struct
97 {
98     char      cmdline_buf[USER_CMD_MAX_COMMAND_LINE + 2];
99     uint8_t   cmd_cur;
100     uint8_t   cmd_history[USER_CMD_MAX_HISTORY_LINE][USER_CMD_MAX_COMMAND_LINE + 2];
101     uint8_t   cmd_history_len[USER_CMD_MAX_HISTORY_LINE];
102     uint8_t   history_head;
103     uint8_t   history_tail;
104     uint8_t   history_cur;
105     int32_t   accum_cmd_len;            /**< accumulated length of command */
106 } T_USER_CMD_IF;
107 
108 /** @brief Prototype of functions that can be called from command table. */
109 typedef T_USER_CMD_PARSE_RESULT(*T_USER_CMD_FUNC)(T_USER_CMD_PARSED_VALUE *p_parse_value);
110 
111 /**
112  * @brief Command table entry.
113  *
114  */
115 typedef struct
116 {
117     char               *p_cmd;
118     char               *p_option;
119     char               *p_help;
120     T_USER_CMD_FUNC     func;
121 } T_USER_CMD_TABLE_ENTRY;
122 /** End of USER_CMD_PARSE_Exported_Types
123   * @}
124   */
125 
126 /*============================================================================*
127  *                         Functions
128  *============================================================================*/
129 /** @defgroup USER_CMD_PARSE_Exported_Functions User Command Parse Module Exported Functions
130     * @{
131     */
132 
133 /**
134  * @brief  Initiate command interface structure
135  * @param[in] p_user_cmd_if   Store parsed commands.
136  * @param[in] project_name    Initiate project name.
137  * @return void
138  *
139  * <b>Example usage</b>
140  * \code{.c}
141     void app_main_task(void *p_param)
142     {
143         char event;
144 
145         os_msg_queue_create(&io_queue_handle, MAX_NUMBER_OF_IO_MESSAGE, sizeof(T_IO_MSG));
146         os_msg_queue_create(&evt_queue_handle, MAX_NUMBER_OF_EVENT_MESSAGE, sizeof(unsigned char));
147 
148         gap_start_bt_stack(evt_queue_handle, io_queue_handle, MAX_NUMBER_OF_GAP_MESSAGE);
149 
150         data_uart_init(evt_queue_handle, io_queue_handle);
151         user_cmd_init(&user_cmd_if, "central");
152         ......
153     }
154  * \endcode
155  */
156 void user_cmd_init(T_USER_CMD_IF *p_user_cmd_if, char *project_name);
157 
158 /**
159  * @brief  Collect command characters.
160  *
161  * @param[in] p_user_cmd_if   Store parsed commands.
162  * @param[in] p_data          Data to be parsed.
163  * @param[in] len             Length of data to be command parsed.
164  * @param[in] p_cmd_table     Command table to execute function.
165  * @return  Command collect result.
166  * @retval 1 true.
167  * @retval 0 false.
168  *
169  * <b>Example usage</b>
170  * \code{.c}
171     void app_handle_io_msg(T_IO_MSG io_msg)
172     {
173         uint16_t msg_type = io_msg.type;
174         uint8_t rx_char;
175 
176         switch (msg_type)
177         {
178         case IO_MSG_TYPE_UART:
179             // We handle user command informations from Data UART in this branch.
180             rx_char = (uint8_t)io_msg.subtype;
181             user_cmd_collect(&user_cmd_if, &rx_char, sizeof(rx_char), user_cmd_table);
182             break;
183         default:
184             break;
185         }
186     }
187  * \endcode
188  */
189 bool user_cmd_collect(T_USER_CMD_IF *p_user_cmd_if, uint8_t *p_data, int32_t len,
190                       const T_USER_CMD_TABLE_ENTRY *p_cmd_table);
191 /** End of USER_CMD_PARSE_Exported_Functions
192   * @}
193   */
194 
195 /** @} */ /* End of group USER_CMD_PARSE */
196 /** @} */ /* End of group DATA_UART_CMD */
197 #ifdef  __cplusplus
198 }
199 #endif  /*  __cplusplus */
200 
201 #endif  /* __USER_CMD_PARSE_CMD_H */
202 
203