Skip to content

Commit

Permalink
Allow chip-tool listen port to be configured (#3675)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored Dec 10, 2020
1 parent ece9baa commit ccbffc1
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 84 deletions.
1 change: 0 additions & 1 deletion examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ executable("chip-tool") {
"commands/clusters/ModelCommand.cpp",
"commands/common/Command.cpp",
"commands/common/Commands.cpp",
"commands/common/Logging.cpp",
"commands/common/NetworkCommand.cpp",
"commands/echo/EchoCommand.cpp",
"commands/pairing/PairingCommand.cpp",
Expand Down
4 changes: 2 additions & 2 deletions examples/chip-tool/commands/common/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "Commands.h"

#include "Command.h"
#include "Logging.h"

#include <algorithm>
#include <string>
Expand All @@ -38,14 +37,15 @@ int Commands::Run(NodeId localId, NodeId remoteId, int argc, char ** argv)
{
CHIP_ERROR err = CHIP_NO_ERROR;
PersistentStorage storage;
ConfigureChipLogging();

err = chip::Platform::MemoryInit();
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init Memory failure: %s", chip::ErrorStr(err)));

err = storage.Init();
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init Storage failure: %s", chip::ErrorStr(err)));

chip::Logging::SetLogFilter(storage.GetLoggingLevel());

err = RunCommand(storage, localId, remoteId, argc, argv);
SuccessOrExit(err);

Expand Down
59 changes: 0 additions & 59 deletions examples/chip-tool/commands/common/Logging.cpp

This file was deleted.

21 changes: 0 additions & 21 deletions examples/chip-tool/commands/common/Logging.h

This file was deleted.

3 changes: 3 additions & 0 deletions examples/chip-tool/commands/common/NetworkCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ CHIP_ERROR NetworkCommand::Run(PersistentStorage & storage, NodeId localId, Node
{
CHIP_ERROR err = CHIP_NO_ERROR;

err = mCommissioner.SetUdpListenPort(storage.GetListenPort());
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init failure! Commissioner: %s", chip::ErrorStr(err)));

err = mCommissioner.Init(localId, &storage);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(Controller, "Init failure! Commissioner: %s", chip::ErrorStr(err)));

Expand Down
63 changes: 62 additions & 1 deletion examples/chip-tool/config/PersistentStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ using Sections = std::map<String, Section>;

using namespace ::chip;
using namespace ::chip::Controller;
using namespace ::chip::Logging;

constexpr const char kFilename[] = "/tmp/chip_tool_config.ini";
constexpr const char kDefaultSectionName[] = "Default";
constexpr const char kPortKey[] = "ListenPort";
constexpr const char kLoggingKey[] = "LoggingLevel";
constexpr LogCategory kDefaultLoggingLevel = kLogCategory_Detail;

CHIP_ERROR PersistentStorage::Init()
{
Expand Down Expand Up @@ -66,7 +70,7 @@ CHIP_ERROR PersistentStorage::GetKeyValue(const char * key, char * value, uint16
VerifyOrExit(inipp::extract(section[key], iniValue), err = CHIP_ERROR_INVALID_ARGUMENT);

iniValueLength = iniValue.size();
VerifyOrExit(iniValueLength < static_cast<size_t>(size) - 1, err = CHIP_ERROR_BUFFER_TOO_SMALL);
VerifyOrExit(iniValueLength <= static_cast<size_t>(size) - 1, err = CHIP_ERROR_BUFFER_TOO_SMALL);

iniValueLength = iniValue.copy(value, iniValueLength);
value[iniValueLength] = '\0';
Expand Down Expand Up @@ -112,3 +116,60 @@ CHIP_ERROR PersistentStorage::CommitConfig()
exit:
return err;
}

uint16_t PersistentStorage::GetListenPort()
{
CHIP_ERROR err = CHIP_NO_ERROR;
uint16_t chipListenPort = CHIP_PORT;

char value[6];
uint16_t size = static_cast<uint16_t>(sizeof(value));
err = GetKeyValue(kPortKey, value, size);
if (CHIP_NO_ERROR == err)
{
uint16_t tmpValue;
std::stringstream ss(value);
ss >> tmpValue;
if (!ss.fail() && ss.eof())
{
chipListenPort = tmpValue;
}
}

return chipListenPort;
}

LogCategory PersistentStorage::GetLoggingLevel()
{
CHIP_ERROR err = CHIP_NO_ERROR;
LogCategory chipLogLevel = kDefaultLoggingLevel;

char value[9];
uint16_t size = static_cast<uint16_t>(sizeof(value));
err = GetKeyValue(kLoggingKey, value, size);
if (CHIP_NO_ERROR == err)
{
if (strcasecmp(value, "none") == 0)
{
chipLogLevel = kLogCategory_None;
}
else if (strcasecmp(value, "error") == 0)
{
chipLogLevel = kLogCategory_Error;
}
else if (strcasecmp(value, "progress") == 0)
{
chipLogLevel = kLogCategory_Progress;
}
else if (strcasecmp(value, "detail") == 0)
{
chipLogLevel = kLogCategory_Detail;
}
else if (strcasecmp(value, "retain") == 0)
{
chipLogLevel = kLogCategory_Retain;
}
}

return chipLogLevel;
}
4 changes: 4 additions & 0 deletions examples/chip-tool/config/PersistentStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <controller/CHIPDeviceController.h>
#include <inipp/inipp.h>
#include <support/logging/CHIPLogging.h>

class PersistentStorage : public chip::Controller::PersistentStorageDelegate
{
Expand All @@ -33,6 +34,9 @@ class PersistentStorage : public chip::Controller::PersistentStorageDelegate
void SetKeyValue(const char * key, const char * value) override;
void DeleteKeyValue(const char * key) override;

uint16_t GetListenPort();
chip::Logging::LogCategory GetLoggingLevel();

private:
CHIP_ERROR CommitConfig();
inipp::Ini<char> mConfig;
Expand Down

0 comments on commit ccbffc1

Please sign in to comment.