• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 typedef struct otCliCommand
52 {
53     const char *mName; ///< A pointer to the command string.
54     otError (*mCommand)(void   *aContext,
55                         uint8_t aArgsLength,
56                         char   *aArgs[]); ///< A function pointer to process the command.
57 } otCliCommand;
58 
59 /**
60  * @addtogroup api-cli
61  *
62  * @brief
63  *   This module includes functions that control the Thread stack's execution.
64  *
65  * @{
66  */
67 
68 /**
69  * Pointer is called to notify about Console output.
70  *
71  * @param[out] aContext    A user context pointer.
72  * @param[in]  aFormat     The format string.
73  * @param[in]  aArguments  The format string arguments.
74  *
75  * @returns                Number of bytes written by the callback.
76  */
77 typedef int (*otCliOutputCallback)(void *aContext, const char *aFormat, va_list aArguments);
78 
79 /**
80  * Initialize the CLI module.
81  *
82  * @param[in]  aInstance   The OpenThread instance structure.
83  * @param[in]  aCallback   A callback method called to process CLI output.
84  * @param[in]  aContext    A user context pointer.
85  */
86 void otCliInit(otInstance *aInstance, otCliOutputCallback aCallback, void *aContext);
87 
88 /**
89  * Is called to feed in a console input line.
90  *
91  * @param[in]  aBuf        A pointer to a null-terminated string.
92  */
93 void otCliInputLine(char *aBuf);
94 
95 /**
96  * Set a user command table.
97  *
98  * @param[in]  aUserCommands  A pointer to an array with user commands.
99  * @param[in]  aLength        @p aUserCommands length.
100  * @param[in]  aContext       @p The context passed to the handler.
101  *
102  * @retval OT_ERROR_NONE    Successfully updated command table with commands from @p aUserCommands.
103  * @retval OT_ERROR_FAILED  Maximum number of command entries have already been set.
104  */
105 otError otCliSetUserCommands(const otCliCommand *aUserCommands, uint8_t aLength, void *aContext);
106 
107 /**
108  * Write a number of bytes to the CLI console as a hex string.
109  *
110  * @param[in]  aBytes   A pointer to data which should be printed.
111  * @param[in]  aLength  @p aBytes length.
112  */
113 void otCliOutputBytes(const uint8_t *aBytes, uint8_t aLength);
114 
115 /**
116  * Write formatted string to the CLI console
117  *
118  * @param[in]  aFmt   A pointer to the format string.
119  * @param[in]  ...    A matching list of arguments.
120  */
121 void otCliOutputFormat(const char *aFmt, ...);
122 
123 /**
124  * Write error code to the CLI console
125  *
126  * If the @p aError is `OT_ERROR_PENDING` nothing will be outputted.
127  *
128  * @param[in]  aError Error code value.
129  */
130 void otCliAppendResult(otError aError);
131 
132 /**
133  * Callback to write the OpenThread Log to the CLI console
134  *
135  * @param[in]  aLogLevel   The log level.
136  * @param[in]  aLogRegion  The log region.
137  * @param[in]  aFormat     A pointer to the format string.
138  * @param[in]  aArgs       va_list matching aFormat.
139  */
140 void otCliPlatLogv(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aArgs);
141 
142 /**
143  * Callback to allow vendor specific commands to be added to the user command table.
144  *
145  * Available when `OPENTHREAD_CONFIG_CLI_VENDOR_COMMANDS_ENABLE` is enabled and
146  * `OPENTHREAD_CONFIG_CLI_MAX_USER_CMD_ENTRIES` is greater than 1.
147  */
148 extern void otCliVendorSetUserCommands(void);
149 
150 /**
151  * @}
152  */
153 
154 #ifdef __cplusplus
155 } // extern "C"
156 #endif
157 
158 #endif // OPENTHREAD_CLI_H_
159