Use placement-new to initialize static variables in CLI. (#341)

This commit is contained in:
Jonathan Hui
2016-08-05 11:56:01 -07:00
committed by GitHub
parent b665529dac
commit 90eac68ff5
3 changed files with 52 additions and 11 deletions
+22 -8
View File
@@ -41,6 +41,7 @@
#include "cli.hpp"
#include "cli_dataset.hpp"
#include <common/encoding.hpp>
#include <common/new.hpp>
#include <platform/uart.h>
using Thread::Encoding::BigEndian::HostSwap16;
@@ -89,14 +90,27 @@ const struct Command Interpreter::sCommands[] =
otNetifAddress Interpreter::sAddress;
Ip6::IcmpEcho Interpreter::sIcmpEcho(&HandleEchoResponse, NULL);
static otDEFINE_ALIGNED_VAR(sIcmpEchoBuf, sizeof(Ip6::IcmpEcho), uint64_t);
Ip6::IcmpEcho *Interpreter::sIcmpEcho;
static otDEFINE_ALIGNED_VAR(sPingTimerBuf, sizeof(Timer), uint64_t);
Timer *Interpreter::sPingTimer;
Ip6::SockAddr Interpreter::sSockAddr;
Server *Interpreter::sServer;
uint8_t Interpreter::sEchoRequest[1500];
uint16_t Interpreter::sLength = 8;
uint16_t Interpreter::sCount = 1;
uint32_t Interpreter::sInterval = 1000;
Timer Interpreter::sPingTimer(&HandlePingTimer, NULL);
uint16_t Interpreter::sLength;
uint16_t Interpreter::sCount;
uint32_t Interpreter::sInterval;
void Interpreter::Init(void)
{
sIcmpEcho = new(&sIcmpEchoBuf) Ip6::IcmpEcho(&HandleEchoResponse, NULL);
sPingTimer = new(&sPingTimerBuf) Timer(&HandlePingTimer, NULL);
sLength = 8;
sCount = 1;
sInterval = 1000;
}
int Interpreter::Hex2Bin(const char *aHex, uint8_t *aBin, uint16_t aBinLength)
{
@@ -810,7 +824,7 @@ void Interpreter::ProcessPing(int argc, char *argv[])
long value;
VerifyOrExit(argc > 0, error = kThreadError_Parse);
VerifyOrExit(!sPingTimer.IsRunning(), error = kThreadError_Busy);
VerifyOrExit(!sPingTimer->IsRunning(), error = kThreadError_Busy);
memset(&sSockAddr, 0, sizeof(sSockAddr));
SuccessOrExit(error = sSockAddr.GetAddress().FromString(argv[0]));
@@ -859,12 +873,12 @@ void Interpreter::HandlePingTimer(void *aContext)
uint32_t timestamp = HostSwap32(Timer::GetNow());
memcpy(sEchoRequest, &timestamp, sizeof(timestamp));
sIcmpEcho.SendEchoRequest(sSockAddr, sEchoRequest, sLength);
sIcmpEcho->SendEchoRequest(sSockAddr, sEchoRequest, sLength);
sCount--;
if (sCount)
{
sPingTimer.Start(sInterval);
sPingTimer->Start(sInterval);
}
(void)aContext;
+29 -3
View File
@@ -68,6 +68,12 @@ struct Command
class Interpreter
{
public:
/**
* This method initializes the CLI interpreter.
*
*/
static void Init(void);
/**
* This method interprets a CLI command.
*
@@ -78,7 +84,27 @@ public:
*/
static void ProcessLine(char *aBuf, uint16_t aBufLength, Server &aServer);
static ThreadError ParseLong(char *argv, long &value);
/**
* This method parses an ASCII string as a long.
*
* @param[in] aString A pointer to the ASCII string.
* @param[out] aLong A reference to where the parsed long is placed.
*
* @retval kThreadError_None Successfully parsed the ASCII string.
* @retval kThreadError_Parse Could not parse the ASCII string.
*
*/
static ThreadError ParseLong(char *aString, long &aLong);
/**
* This method converts a hex string to binary.
*
* @param[in] aHex A pointer to the hex string.
* @param[out] aBin A pointer to where the binary representation is placed.
* @param[in] aBinLength Maximum length of the binary representation.
*
* @returns The number of bytes in the binary representation.
*/
static int Hex2Bin(const char *aHex, uint8_t *aBin, uint16_t aBinLength);
private:
@@ -139,13 +165,13 @@ private:
static otNetifAddress sAddress;
static Ip6::SockAddr sSockAddr;
static Ip6::IcmpEcho sIcmpEcho;
static Ip6::IcmpEcho *sIcmpEcho;
static Server *sServer;
static uint8_t sEchoRequest[];
static uint16_t sLength;
static uint16_t sCount;
static uint32_t sInterval;
static Timer sPingTimer;
static Timer *sPingTimer;
};
} // namespace Cli
+1
View File
@@ -58,6 +58,7 @@ static otDEFINE_ALIGNED_VAR(sCliUartRaw, sizeof(Uart), uint64_t);
extern "C" void otCliUartInit(void)
{
sServer = new(&sCliUartRaw) Uart;
Interpreter::Init();
}
Uart::Uart(void)