1 /*
2  *  Copyright (c) 2016, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  * @brief
32  *  This file defines the top-level functions for the OpenThread CLI server.
33  */
34 
35 #ifndef OPENTHREAD_CLI_H_
36 #define OPENTHREAD_CLI_H_
37 
38 #include <stdarg.h>
39 #include <stdint.h>
40 
41 #include <openthread/instance.h>
42 #include <openthread/platform/logging.h>
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 /**
49  * Represents a CLI command.
50  *
51  */
52 typedef struct otCliCommand
53 {
54     const char *mName; ///< A pointer to the command string.
55     otError (*mCommand)(void   *aContext,
56                         uint8_t aArgsLength,
57                         char   *aArgs[]); ///< A function pointer to process the command.
58 } otCliCommand;
59 
60 /**
61  * @addtogroup api-cli
62  *
63  * @brief
64  *   This module includes functions that control the Thread stack's execution.
65  *
66  * @{
67  *
68  */
69 
70 /**
71  * Pointer is called to notify about Console output.
72  *
73  * @param[out] aContext    A user context pointer.
74  * @param[in]  aFormat     The format string.
75  * @param[in]  aArguments  The format string arguments.
76  *
77  * @returns                Number of bytes written by the callback.
78  *
79  */
80 typedef int (*otCliOutputCallback)(void *aContext, const char *aFormat, va_list aArguments);
81 
82 /**
83  * Initialize the CLI module.
84  *
85  * @param[in]  aInstance   The OpenThread instance structure.
86  * @param[in]  aCallback   A callback method called to process CLI output.
87  * @param[in]  aContext    A user context pointer.
88  *
89  */
90 void otCliInit(otInstance *aInstance, otCliOutputCallback aCallback, void *aContext);
91 
92 /**
93  * Is called to feed in a console input line.
94  *
95  * @param[in]  aBuf        A pointer to a null-terminated string.
96  *
97  */
98 void otCliInputLine(char *aBuf);
99 
100 /**
101  * Set a user command table.
102  *
103  * @param[in]  aUserCommands  A pointer to an array with user commands.
104  * @param[in]  aLength        @p aUserCommands length.
105  * @param[in]  aContext       @p The context passed to the handler.
106  *
107  * @retval OT_ERROR_NONE    Successfully updated command table with commands from @p aUserCommands.
108  * @retval OT_ERROR_FAILED  Maximum number of command entries have already been set.
109  */
110 otError otCliSetUserCommands(const otCliCommand *aUserCommands, uint8_t aLength, void *aContext);
111 
112 /**
113  * Write a number of bytes to the CLI console as a hex string.
114  *
115  * @param[in]  aBytes   A pointer to data which should be printed.
116  * @param[in]  aLength  @p aBytes length.
117  *
118  */
119 void otCliOutputBytes(const uint8_t *aBytes, uint8_t aLength);
120 
121 /**
122  * Write formatted string to the CLI console
123  *
124  * @param[in]  aFmt   A pointer to the format string.
125  * @param[in]  ...    A matching list of arguments.
126  *
127  */
128 void otCliOutputFormat(const char *aFmt, ...);
129 
130 /**
131  * Write error code to the CLI console
132  *
133  * If the @p aError is `OT_ERROR_PENDING` nothing will be outputted.
134  *
135  * @param[in]  aError Error code value.
136  *
137  */
138 void otCliAppendResult(otError aError);
139 
140 /**
141  * Callback to write the OpenThread Log to the CLI console
142  *
143  * @param[in]  aLogLevel   The log level.
144  * @param[in]  aLogRegion  The log region.
145  * @param[in]  aFormat     A pointer to the format string.
146  * @param[in]  aArgs       va_list matching aFormat.
147  *
148  */
149 void otCliPlatLogv(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aArgs);
150 
151 /**
152  * Callback to allow vendor specific commands to be added to the user command table.
153  *
154  * Available when `OPENTHREAD_CONFIG_CLI_VENDOR_COMMANDS_ENABLE` is enabled and
155  * `OPENTHREAD_CONFIG_CLI_MAX_USER_CMD_ENTRIES` is greater than 1.
156  *
157  */
158 extern void otCliVendorSetUserCommands(void);
159 
160 /**
161  * @}
162  *
163  */
164 
165 #ifdef __cplusplus
166 } // extern "C"
167 #endif
168 
169 #endif // OPENTHREAD_CLI_H_
170