add Cli Console support (#532)

* add Cli Console support
This commit is contained in:
Marcin K Szczodrak
2016-09-07 14:57:27 -07:00
committed by Jonathan Hui
parent 7cfe32004f
commit f153da566a
5 changed files with 292 additions and 0 deletions
+2
View File
@@ -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 \
+103
View File
@@ -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 <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cli/cli.hpp>
#include <cli/cli_console.hpp>
#include <common/new.hpp>
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<uint16_t>(strlen(buf)));
}
} // namespace Cli
} // namespace Thread
+107
View File
@@ -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 <openthread-types.h>
#include <cli/cli_server.hpp>
#include <cli/cli-console.h>
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_