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 * This structure represents a CLI command. 50 * 51 */ 52 typedef struct otCliCommand 53 { 54 const char *mName; ///< A pointer to the command string. 55 void (*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 * This function 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 * This method 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 */ 108 void otCliSetUserCommands(const otCliCommand *aUserCommands, uint8_t aLength, void *aContext); 109 110 /** 111 * Write a number of bytes to the CLI console as a hex string. 112 * 113 * @param[in] aBytes A pointer to data which should be printed. 114 * @param[in] aLength @p aBytes length. 115 * 116 */ 117 void otCliOutputBytes(const uint8_t *aBytes, uint8_t aLength); 118 119 /** 120 * Write formatted string to the CLI console 121 * 122 * @param[in] aFmt A pointer to the format string. 123 * @param[in] ... A matching list of arguments. 124 * 125 */ 126 void otCliOutputFormat(const char *aFmt, ...); 127 128 /** 129 * Write error code to the CLI console 130 * 131 * If the @p aError is `OT_ERROR_PENDING` nothing will be outputted. 132 * 133 * @param[in] aError Error code value. 134 * 135 */ 136 void otCliAppendResult(otError aError); 137 138 /** 139 * Callback to write the OpenThread Log to the CLI console 140 * 141 * @param[in] aLogLevel The log level. 142 * @param[in] aLogRegion The log region. 143 * @param[in] aFormat A pointer to the format string. 144 * @param[in] aArgs va_list matching aFormat. 145 * 146 */ 147 void otCliPlatLogv(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aArgs); 148 149 /** 150 * @} 151 * 152 */ 153 154 #ifdef __cplusplus 155 } // extern "C" 156 #endif 157 158 #endif // OPENTHREAD_CLI_H_ 159