#include "cli_uart.hpp"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#if OPENTHREAD_POSIX
#include <signal.h>
#include <sys/types.h>
#endif
#include <openthread/cli.h>
#include <openthread/platform/logging.h>
#include <openthread/platform/uart.h>
#include "cli/cli.hpp"
#include "common/code_utils.hpp"
#include "common/encoding.hpp"
#include "common/logging.hpp"
#include "common/new.hpp"
#include "common/tasklet.hpp"
#include "utils/static_assert.hpp"
#if OPENTHREAD_CONFIG_ENABLE_DEBUG_UART
#include <openthread/platform/debug_uart.h>
#endif
#ifdef OT_CLI_UART_LOCK_HDR_FILE
#include OT_CLI_UART_LOCK_HDR_FILE
#else
#ifndef OT_CLI_UART_OUTPUT_LOCK
#define OT_CLI_UART_OUTPUT_LOCK() \
do \
{ \
} while (0)
#endif
#ifndef OT_CLI_UART_OUTPUT_UNLOCK
#define OT_CLI_UART_OUTPUT_UNLOCK() \
do \
{ \
} while (0)
#endif
#endif
#if OPENTHREAD_CONFIG_DIAG_ENABLE
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE <= OPENTHREAD_CONFIG_CLI_UART_TX_BUFFER_SIZE,
"diag output buffer should be smaller than CLI UART tx buffer");
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE <= OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE,
"diag command line should be smaller than CLI UART rx buffer");
#endif
OT_STATIC_ASSERT(OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH <= OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE,
"command line should be should be smaller than CLI rx buffer");
namespace ot {
namespace Cli {
static OT_DEFINE_ALIGNED_VAR(sCliUartRaw, sizeof(Uart), uint64_t);
extern "C" void otCliUartInit(otInstance *aInstance)
{
Instance *instance = static_cast<Instance *>(aInstance);
Server::sServer = new (&sCliUartRaw) Uart(instance);
}
Uart::Uart(Instance *aInstance)
: Server(aInstance)
{
mRxLength = 0;
mTxHead = 0;
mTxLength = 0;
mSendLength = 0;
IgnoreError(otPlatUartEnable());
}
extern "C" void otPlatUartReceived(const uint8_t *aBuf, uint16_t aBufLength)
{
static_cast<Uart *>(Server::sServer)->ReceiveTask(aBuf, aBufLength);
}
void Uart::ReceiveTask(const uint8_t *aBuf, uint16_t aBufLength)
{
#if !OPENTHREAD_CONFIG_UART_CLI_RAW
static const char sEraseString[] = {'\b', ' ', '\b'};
static const char CRNL[] = {'\r', '\n'};
#endif
static const char sCommandPrompt[] = {'>', ' '};
const uint8_t * end;
end = aBuf + aBufLength;
for (; aBuf < end; aBuf++)
{
switch (*aBuf)
{
case '\r':
case '\n':
#if !OPENTHREAD_CONFIG_UART_CLI_RAW
Output(CRNL, sizeof(CRNL));
#endif
if (mRxLength > 0)
{
mRxBuffer[mRxLength] = '\0';
IgnoreError(ProcessCommand());
}
Output(sCommandPrompt, sizeof(sCommandPrompt));
break;
#if !OPENTHREAD_CONFIG_UART_CLI_RAW
#if OPENTHREAD_POSIX
case 0x03: kill(0, SIGINT);
break;
case 0x04: exit(EXIT_SUCCESS);
break;
#endif
case '\b':
case 127:
if (mRxLength > 0)
{
Output(sEraseString, sizeof(sEraseString));
mRxBuffer[--mRxLength] = '\0';
}
break;
#endif
default:
if (mRxLength < kRxBufferSize - 1)
{
#if !OPENTHREAD_CONFIG_UART_CLI_RAW
Output(reinterpret_cast<const char *>(aBuf), 1);
#endif
mRxBuffer[mRxLength++] = static_cast<char>(*aBuf);
}
break;
}
}
}
otError Uart::ProcessCommand(void)
{
otError error = OT_ERROR_NONE;
while (mRxBuffer[mRxLength - 1] == '\n' || mRxBuffer[mRxLength - 1] == '\r')
{
mRxBuffer[--mRxLength] = '\0';
}
#if OPENTHREAD_CONFIG_LOG_OUTPUT != OPENTHREAD_CONFIG_LOG_OUTPUT_NONE
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
#else
otLogInfoCli("execute command: %s", mRxBuffer);
#endif
#endif
if (mRxLength > 0)
{
mInterpreter.ProcessLine(mRxBuffer, mRxLength, *this);
}
mRxLength = 0;
return error;
}
int Uart::Output(const char *aBuf, uint16_t aBufLength)
{
OT_CLI_UART_OUTPUT_LOCK();
uint16_t sent = 0;
while (aBufLength > 0)
{
uint16_t remaining = kTxBufferSize - mTxLength;
uint16_t tail;
uint16_t sendLength = aBufLength;
if (sendLength > remaining)
{
sendLength = remaining;
}
for (uint16_t i = 0; i < sendLength; i++)
{
tail = (mTxHead + mTxLength) % kTxBufferSize;
mTxBuffer[tail] = *aBuf++;
aBufLength--;
mTxLength++;
}
Send();
sent += sendLength;
if (aBufLength > 0)
{
otError err = otPlatUartFlush();
if (err == OT_ERROR_NONE)
{
SendDoneTask();
}
else
{
break;
}
}
}
OT_CLI_UART_OUTPUT_UNLOCK();
return sent;
}
void Uart::Send(void)
{
VerifyOrExit(mSendLength == 0, OT_NOOP);
if (mTxLength > kTxBufferSize - mTxHead)
{
mSendLength = kTxBufferSize - mTxHead;
}
else
{
mSendLength = mTxLength;
}
if (mSendLength > 0)
{
#if OPENTHREAD_CONFIG_ENABLE_DEBUG_UART
otPlatDebugUart_write_bytes(reinterpret_cast<uint8_t *>(mTxBuffer + mTxHead), mSendLength);
#endif
IgnoreError(otPlatUartSend(reinterpret_cast<uint8_t *>(mTxBuffer + mTxHead), mSendLength));
}
exit:
return;
}
extern "C" void otPlatUartSendDone(void)
{
static_cast<Uart *>(Server::sServer)->SendDoneTask();
}
void Uart::SendDoneTask(void)
{
mTxHead = (mTxHead + mSendLength) % kTxBufferSize;
mTxLength -= mSendLength;
mSendLength = 0;
Send();
}
} }