From f153da566a32012c960a39bf3488a56ce96c0e1e Mon Sep 17 00:00:00 2001 From: Marcin K Szczodrak Date: Wed, 7 Sep 2016 14:57:27 -0700 Subject: [PATCH] add Cli Console support (#532) * add Cli Console support --- include/cli/Makefile.am | 1 + include/cli/cli-console.h | 79 ++++++++++++++++++++++++++++ src/cli/Makefile.am | 2 + src/cli/cli_console.cpp | 103 ++++++++++++++++++++++++++++++++++++ src/cli/cli_console.hpp | 107 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 292 insertions(+) create mode 100644 include/cli/cli-console.h create mode 100644 src/cli/cli_console.cpp create mode 100644 src/cli/cli_console.hpp diff --git a/include/cli/Makefile.am b/include/cli/Makefile.am index 2be9dac90..3ffd72335 100644 --- a/include/cli/Makefile.am +++ b/include/cli/Makefile.am @@ -29,6 +29,7 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am ot_cli_headers = \ + cli-console.h \ cli-uart.h \ $(NULL) diff --git a/include/cli/cli-console.h b/include/cli/cli-console.h new file mode 100644 index 000000000..084b86730 --- /dev/null +++ b/include/cli/cli-console.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2016, Nest Labs, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * @brief + * This file defines the top-level functions for the OpenThread CLI server. + */ + +#ifndef CLI_CONSOLE_H_ +#define CLI_CONSOLE_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * This function pointer is called to notify about Console output. + * + * @param[in] aBuf A pointer to a buffer with an output. + * @param[in] aBufLength A length of the output data stored in the buffer. + * @param[out] aContext A user context pointer. + * + * @returns Number of bytes processed by the callback. + * + */ +typedef int (*otCliConsoleOutputCallback)(const char *aBuf, + uint16_t aBufLength, void *aContext); + +/** + * Initialize the CLI CONSOLE module. + * + * @param[in] aCallback A callback method called to process console output. + * @param[in] aContext A user context pointer. + * + */ +void otCliConsoleInit(otCliConsoleOutputCallback aCallback, void *aContext); + +/** + * This method is called to feed in a console input line. + * + * @param[in] aBuf A pointer to a buffer with an input. + * @param[in] aBufLength A length of the input data stored in the buffer. + * + */ +void otCliConsoleInputLine(char *aBuf, uint16_t aBufLength); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif diff --git a/src/cli/Makefile.am b/src/cli/Makefile.am index 30f1bbec0..86ee5b40f 100644 --- a/src/cli/Makefile.am +++ b/src/cli/Makefile.am @@ -39,6 +39,7 @@ libopenthread_cli_a_CPPFLAGS = \ libopenthread_cli_a_SOURCES = \ cli.cpp \ + cli_console.cpp \ cli_dataset.cpp \ cli_uart.cpp \ cli_udp.cpp \ @@ -46,6 +47,7 @@ libopenthread_cli_a_SOURCES = \ noinst_HEADERS = \ cli.hpp \ + cli_console.hpp \ cli_dataset.hpp \ cli_uart.hpp \ cli_server.hpp \ diff --git a/src/cli/cli_console.cpp b/src/cli/cli_console.cpp new file mode 100644 index 000000000..e332eb3b7 --- /dev/null +++ b/src/cli/cli_console.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2016, Nest Labs, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file implements the CLI server on the CONSOLE service. + */ + +#include +#include +#include +#include + +#include +#include +#include + +namespace Thread { +namespace Cli { + +static Console *sServer; + +static otDEFINE_ALIGNED_VAR(sCliConsoleRaw, sizeof(Console), uint64_t); + +extern "C" void otCliConsoleInit(otCliConsoleOutputCallback aCallback, + void *aContext) +{ + sServer = new(&sCliConsoleRaw) Console; + sServer->SetOutputCallback(aCallback); + sServer->SetContext(aContext); + Interpreter::Init(); +} + +extern "C" void otCliConsoleInputLine(char *aBuf, uint16_t aBufLength) +{ + sServer->ReceiveTask(aBuf, aBufLength); +} + +Console::Console(void) + : mCallback(NULL) +{ + +} + +void Console::SetContext(void *aContext) +{ + mContext = aContext; +} + +void Console::SetOutputCallback(otCliConsoleOutputCallback aCallback) +{ + mCallback = aCallback; +} + +void Console::ReceiveTask(char *aBuf, uint16_t aBufLength) +{ + Interpreter::ProcessLine(aBuf, aBufLength, *this); +} + +int Console::Output(const char *aBuf, uint16_t aBufLength) +{ + return mCallback(aBuf, aBufLength, mContext); +} + +int Console::OutputFormat(const char *fmt, ...) +{ + char buf[kMaxLineLength]; + va_list ap; + + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + + return Output(buf, static_cast(strlen(buf))); +} + +} // namespace Cli +} // namespace Thread diff --git a/src/cli/cli_console.hpp b/src/cli/cli_console.hpp new file mode 100644 index 000000000..4b66c94c2 --- /dev/null +++ b/src/cli/cli_console.hpp @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2016, Nest Labs, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file + * This file contains definitions for a CLI server on the CONSOLE service. + */ + +#ifndef CLI_CONSOLE_HPP_ +#define CLI_CONSOLE_HPP_ + +#include +#include +#include + +namespace Thread { +namespace Cli { + +/** + * This class implements the CLI server on top of the CONSOLE platform abstraction. + * + */ +class Console: public Server +{ +public: + Console(void); + + /** + * This method delivers raw characters to the client. + * + * @param[in] aBuf A pointer to a buffer. + * @param[in] aBufLength Number of bytes in the buffer. + * + * @returns The number of bytes placed in the output queue. + * + */ + int Output(const char *aBuf, uint16_t aBufLength); + + /** + * This method delivers formatted output to the client. + * + * @param[in] aFmt A pointer to the format string. + * @param[in] ... A variable list of arguments to format. + * + * @returns The number of bytes placed in the output queue. + * + */ + int OutputFormat(const char *fmt, ...); + + /** + * This method sets a callback that is called when console has some output. + * + * @param[in] aCallback A pointer to a callback method. + * + */ + void SetOutputCallback(otCliConsoleOutputCallback aCallback); + + /** + * This method sets a context that is returned with the callback. + * + * @param[in] aContext A pointer to a user context. + * + */ + void SetContext(void *aContext); + + void ReceiveTask(char *aBuf, uint16_t aBufLength); + +private: + enum + { + kMaxLineLength = 128, + }; + + otCliConsoleOutputCallback mCallback; + void *mContext; + +}; + +} // namespace Cli +} // namespace Thread + +#endif // CLI_CONSOLE_HPP_