Imported more library files
Not compiling currently
This commit is contained in:
374
Libs/Matter/third_party/matter_sdk/examples/providers/DeviceInfoProviderImpl.cpp
vendored
Normal file
374
Libs/Matter/third_party/matter_sdk/examples/providers/DeviceInfoProviderImpl.cpp
vendored
Normal file
@@ -0,0 +1,374 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2022 Project CHIP Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <DeviceInfoProviderImpl.h>
|
||||
|
||||
#include <lib/core/TLV.h>
|
||||
#include <lib/support/CHIPMemString.h>
|
||||
#include <lib/support/CodeUtils.h>
|
||||
#include <lib/support/DefaultStorageKeyAllocator.h>
|
||||
#include <lib/support/SafeInt.h>
|
||||
#include <platform/internal/CHIPDeviceLayerInternal.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace chip {
|
||||
namespace DeviceLayer {
|
||||
|
||||
namespace {
|
||||
constexpr TLV::Tag kLabelNameTag = TLV::ContextTag(0);
|
||||
constexpr TLV::Tag kLabelValueTag = TLV::ContextTag(1);
|
||||
} // anonymous namespace
|
||||
|
||||
DeviceInfoProviderImpl & DeviceInfoProviderImpl::GetDefaultInstance()
|
||||
{
|
||||
static DeviceInfoProviderImpl sInstance;
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
DeviceInfoProvider::FixedLabelIterator * DeviceInfoProviderImpl::IterateFixedLabel(EndpointId endpoint)
|
||||
{
|
||||
return chip::Platform::New<FixedLabelIteratorImpl>(endpoint);
|
||||
}
|
||||
|
||||
DeviceInfoProviderImpl::FixedLabelIteratorImpl::FixedLabelIteratorImpl(EndpointId endpoint) : mEndpoint(endpoint)
|
||||
{
|
||||
mIndex = 0;
|
||||
}
|
||||
|
||||
size_t DeviceInfoProviderImpl::FixedLabelIteratorImpl::Count()
|
||||
{
|
||||
// A hardcoded labelList on all endpoints.
|
||||
return 4;
|
||||
}
|
||||
|
||||
bool DeviceInfoProviderImpl::FixedLabelIteratorImpl::Next(FixedLabelType & output)
|
||||
{
|
||||
bool retval = true;
|
||||
|
||||
// A hardcoded list for testing only
|
||||
CHIP_ERROR err = CHIP_NO_ERROR;
|
||||
|
||||
const char * labelPtr = nullptr;
|
||||
const char * valuePtr = nullptr;
|
||||
|
||||
VerifyOrReturnError(mIndex < 4, false);
|
||||
|
||||
ChipLogProgress(DeviceLayer, "Get the fixed label with index:%u at endpoint:%d", static_cast<unsigned>(mIndex), mEndpoint);
|
||||
|
||||
switch (mIndex)
|
||||
{
|
||||
case 0:
|
||||
labelPtr = "room";
|
||||
valuePtr = "bedroom 2";
|
||||
break;
|
||||
case 1:
|
||||
labelPtr = "orientation";
|
||||
valuePtr = "North";
|
||||
break;
|
||||
case 2:
|
||||
labelPtr = "floor";
|
||||
valuePtr = "2";
|
||||
break;
|
||||
case 3:
|
||||
labelPtr = "direction";
|
||||
valuePtr = "up";
|
||||
break;
|
||||
default:
|
||||
err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
|
||||
break;
|
||||
}
|
||||
|
||||
if (err == CHIP_NO_ERROR)
|
||||
{
|
||||
VerifyOrReturnError(std::strlen(labelPtr) <= kMaxLabelNameLength, false);
|
||||
VerifyOrReturnError(std::strlen(valuePtr) <= kMaxLabelValueLength, false);
|
||||
|
||||
Platform::CopyString(mFixedLabelNameBuf, labelPtr);
|
||||
Platform::CopyString(mFixedLabelValueBuf, valuePtr);
|
||||
|
||||
output.label = CharSpan::fromCharString(mFixedLabelNameBuf);
|
||||
output.value = CharSpan::fromCharString(mFixedLabelValueBuf);
|
||||
|
||||
mIndex++;
|
||||
|
||||
retval = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
retval = false;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
CHIP_ERROR DeviceInfoProviderImpl::SetUserLabelLength(EndpointId endpoint, size_t val)
|
||||
{
|
||||
return mStorage->SyncSetKeyValue(DefaultStorageKeyAllocator::UserLabelLengthKey(endpoint).KeyName(), &val,
|
||||
static_cast<uint16_t>(sizeof(val)));
|
||||
}
|
||||
|
||||
CHIP_ERROR DeviceInfoProviderImpl::GetUserLabelLength(EndpointId endpoint, size_t & val)
|
||||
{
|
||||
uint16_t len = static_cast<uint16_t>(sizeof(val));
|
||||
|
||||
return mStorage->SyncGetKeyValue(DefaultStorageKeyAllocator::UserLabelLengthKey(endpoint).KeyName(), &val, len);
|
||||
}
|
||||
|
||||
CHIP_ERROR DeviceInfoProviderImpl::SetUserLabelAt(EndpointId endpoint, size_t index, const UserLabelType & userLabel)
|
||||
{
|
||||
VerifyOrReturnError(CanCastTo<uint32_t>(index), CHIP_ERROR_INVALID_ARGUMENT);
|
||||
|
||||
uint8_t buf[UserLabelTLVMaxSize()];
|
||||
TLV::TLVWriter writer;
|
||||
writer.Init(buf);
|
||||
|
||||
TLV::TLVType outerType;
|
||||
ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, outerType));
|
||||
ReturnErrorOnFailure(writer.PutString(kLabelNameTag, userLabel.label));
|
||||
ReturnErrorOnFailure(writer.PutString(kLabelValueTag, userLabel.value));
|
||||
ReturnErrorOnFailure(writer.EndContainer(outerType));
|
||||
|
||||
return mStorage->SyncSetKeyValue(
|
||||
DefaultStorageKeyAllocator::UserLabelIndexKey(endpoint, static_cast<uint32_t>(index)).KeyName(), buf,
|
||||
static_cast<uint16_t>(writer.GetLengthWritten()));
|
||||
}
|
||||
|
||||
CHIP_ERROR DeviceInfoProviderImpl::DeleteUserLabelAt(EndpointId endpoint, size_t index)
|
||||
{
|
||||
return mStorage->SyncDeleteKeyValue(
|
||||
DefaultStorageKeyAllocator::UserLabelIndexKey(endpoint, static_cast<uint32_t>(index)).KeyName());
|
||||
}
|
||||
|
||||
DeviceInfoProvider::UserLabelIterator * DeviceInfoProviderImpl::IterateUserLabel(EndpointId endpoint)
|
||||
{
|
||||
return chip::Platform::New<UserLabelIteratorImpl>(*this, endpoint);
|
||||
}
|
||||
|
||||
DeviceInfoProviderImpl::UserLabelIteratorImpl::UserLabelIteratorImpl(DeviceInfoProviderImpl & provider, EndpointId endpoint) :
|
||||
mProvider(provider), mEndpoint(endpoint)
|
||||
{
|
||||
size_t total = 0;
|
||||
|
||||
ReturnOnFailure(mProvider.GetUserLabelLength(mEndpoint, total));
|
||||
mTotal = total;
|
||||
mIndex = 0;
|
||||
}
|
||||
|
||||
bool DeviceInfoProviderImpl::UserLabelIteratorImpl::Next(UserLabelType & output)
|
||||
{
|
||||
CHIP_ERROR err = CHIP_NO_ERROR;
|
||||
|
||||
VerifyOrReturnError(mIndex < mTotal, false);
|
||||
VerifyOrReturnError(CanCastTo<uint32_t>(mIndex), false);
|
||||
|
||||
uint8_t buf[UserLabelTLVMaxSize()];
|
||||
uint16_t len = static_cast<uint16_t>(sizeof(buf));
|
||||
|
||||
err = mProvider.mStorage->SyncGetKeyValue(
|
||||
DefaultStorageKeyAllocator::UserLabelIndexKey(mEndpoint, static_cast<uint32_t>(mIndex)).KeyName(), buf, len);
|
||||
VerifyOrReturnError(err == CHIP_NO_ERROR, false);
|
||||
|
||||
TLV::ContiguousBufferTLVReader reader;
|
||||
reader.Init(buf);
|
||||
err = reader.Next(TLV::kTLVType_Structure, TLV::AnonymousTag());
|
||||
VerifyOrReturnError(err == CHIP_NO_ERROR, false);
|
||||
|
||||
TLV::TLVType containerType;
|
||||
VerifyOrReturnError(reader.EnterContainer(containerType) == CHIP_NO_ERROR, false);
|
||||
|
||||
chip::CharSpan label;
|
||||
chip::CharSpan value;
|
||||
|
||||
VerifyOrReturnError(reader.Next(kLabelNameTag) == CHIP_NO_ERROR, false);
|
||||
VerifyOrReturnError(reader.Get(label) == CHIP_NO_ERROR, false);
|
||||
|
||||
VerifyOrReturnError(reader.Next(kLabelValueTag) == CHIP_NO_ERROR, false);
|
||||
VerifyOrReturnError(reader.Get(value) == CHIP_NO_ERROR, false);
|
||||
|
||||
VerifyOrReturnError(reader.VerifyEndOfContainer() == CHIP_NO_ERROR, false);
|
||||
VerifyOrReturnError(reader.ExitContainer(containerType) == CHIP_NO_ERROR, false);
|
||||
|
||||
Platform::CopyString(mUserLabelNameBuf, label);
|
||||
Platform::CopyString(mUserLabelValueBuf, value);
|
||||
|
||||
output.label = CharSpan::fromCharString(mUserLabelNameBuf);
|
||||
output.value = CharSpan::fromCharString(mUserLabelValueBuf);
|
||||
|
||||
mIndex++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
DeviceInfoProvider::SupportedLocalesIterator * DeviceInfoProviderImpl::IterateSupportedLocales()
|
||||
{
|
||||
return chip::Platform::New<SupportedLocalesIteratorImpl>();
|
||||
}
|
||||
|
||||
size_t DeviceInfoProviderImpl::SupportedLocalesIteratorImpl::Count()
|
||||
{
|
||||
// Hardcoded list of locales
|
||||
// {("en-US"), ("de-DE"), ("fr-FR"), ("en-GB"), ("es-ES"), ("zh-CN"), ("it-IT"), ("ja-JP")}
|
||||
|
||||
return 8;
|
||||
}
|
||||
|
||||
bool DeviceInfoProviderImpl::SupportedLocalesIteratorImpl::Next(CharSpan & output)
|
||||
{
|
||||
bool retval = true;
|
||||
|
||||
// Hardcoded list of locales
|
||||
CHIP_ERROR err = CHIP_NO_ERROR;
|
||||
|
||||
const char * activeLocalePtr = nullptr;
|
||||
|
||||
VerifyOrReturnError(mIndex < 8, false);
|
||||
|
||||
switch (mIndex)
|
||||
{
|
||||
case 0:
|
||||
activeLocalePtr = "en-US";
|
||||
break;
|
||||
case 1:
|
||||
activeLocalePtr = "de-DE";
|
||||
break;
|
||||
case 2:
|
||||
activeLocalePtr = "fr-FR";
|
||||
break;
|
||||
case 3:
|
||||
activeLocalePtr = "en-GB";
|
||||
break;
|
||||
case 4:
|
||||
activeLocalePtr = "es-ES";
|
||||
break;
|
||||
case 5:
|
||||
activeLocalePtr = "zh-CN";
|
||||
break;
|
||||
case 6:
|
||||
activeLocalePtr = "it-IT";
|
||||
break;
|
||||
case 7:
|
||||
activeLocalePtr = "ja-JP";
|
||||
break;
|
||||
default:
|
||||
err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
|
||||
break;
|
||||
}
|
||||
|
||||
if (err == CHIP_NO_ERROR)
|
||||
{
|
||||
VerifyOrReturnError(std::strlen(activeLocalePtr) <= kMaxActiveLocaleLength, false);
|
||||
|
||||
Platform::CopyString(mActiveLocaleBuf, kMaxActiveLocaleLength + 1, activeLocalePtr);
|
||||
|
||||
output = CharSpan::fromCharString(mActiveLocaleBuf);
|
||||
|
||||
mIndex++;
|
||||
|
||||
retval = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
retval = false;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
DeviceInfoProvider::SupportedCalendarTypesIterator * DeviceInfoProviderImpl::IterateSupportedCalendarTypes()
|
||||
{
|
||||
return chip::Platform::New<SupportedCalendarTypesIteratorImpl>();
|
||||
}
|
||||
|
||||
size_t DeviceInfoProviderImpl::SupportedCalendarTypesIteratorImpl::Count()
|
||||
{
|
||||
// Hardcoded list of strings
|
||||
// {("kBuddhist"), ("kChinese"), ("kCoptic"), ("kEthiopian"), ("kGregorian"), ("kHebrew"), ("kIndian"), ("kJapanese"),
|
||||
// ("kKorean"), ("kPersian"), ("kTaiwanese"), ("kIslamic")}
|
||||
|
||||
return 12;
|
||||
}
|
||||
|
||||
bool DeviceInfoProviderImpl::SupportedCalendarTypesIteratorImpl::Next(CalendarType & output)
|
||||
{
|
||||
bool retval = true;
|
||||
|
||||
// Hardcoded list of Strings that are valid values for the Calendar Types.
|
||||
CHIP_ERROR err = CHIP_NO_ERROR;
|
||||
|
||||
VerifyOrReturnError(mIndex < 12, false);
|
||||
|
||||
switch (mIndex)
|
||||
{
|
||||
case 0:
|
||||
output = app::Clusters::TimeFormatLocalization::CalendarTypeEnum::kBuddhist;
|
||||
break;
|
||||
case 1:
|
||||
output = app::Clusters::TimeFormatLocalization::CalendarTypeEnum::kChinese;
|
||||
break;
|
||||
case 2:
|
||||
output = app::Clusters::TimeFormatLocalization::CalendarTypeEnum::kCoptic;
|
||||
break;
|
||||
case 3:
|
||||
output = app::Clusters::TimeFormatLocalization::CalendarTypeEnum::kEthiopian;
|
||||
break;
|
||||
case 4:
|
||||
output = app::Clusters::TimeFormatLocalization::CalendarTypeEnum::kGregorian;
|
||||
break;
|
||||
case 5:
|
||||
output = app::Clusters::TimeFormatLocalization::CalendarTypeEnum::kHebrew;
|
||||
break;
|
||||
case 6:
|
||||
output = app::Clusters::TimeFormatLocalization::CalendarTypeEnum::kIndian;
|
||||
break;
|
||||
case 7:
|
||||
output = app::Clusters::TimeFormatLocalization::CalendarTypeEnum::kJapanese;
|
||||
break;
|
||||
case 8:
|
||||
output = app::Clusters::TimeFormatLocalization::CalendarTypeEnum::kKorean;
|
||||
break;
|
||||
case 9:
|
||||
output = app::Clusters::TimeFormatLocalization::CalendarTypeEnum::kPersian;
|
||||
break;
|
||||
case 10:
|
||||
output = app::Clusters::TimeFormatLocalization::CalendarTypeEnum::kTaiwanese;
|
||||
break;
|
||||
case 11:
|
||||
output = app::Clusters::TimeFormatLocalization::CalendarTypeEnum::kIslamic;
|
||||
break;
|
||||
default:
|
||||
err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
|
||||
break;
|
||||
}
|
||||
|
||||
if (err == CHIP_NO_ERROR)
|
||||
{
|
||||
mIndex++;
|
||||
retval = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
retval = false;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
} // namespace DeviceLayer
|
||||
} // namespace chip
|
||||
107
Libs/Matter/third_party/matter_sdk/examples/providers/DeviceInfoProviderImpl.h
vendored
Normal file
107
Libs/Matter/third_party/matter_sdk/examples/providers/DeviceInfoProviderImpl.h
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2022 Project CHIP Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <lib/support/EnforceFormat.h>
|
||||
#include <platform/DeviceInfoProvider.h>
|
||||
|
||||
namespace chip {
|
||||
namespace DeviceLayer {
|
||||
|
||||
class DeviceInfoProviderImpl : public DeviceInfoProvider
|
||||
{
|
||||
public:
|
||||
DeviceInfoProviderImpl() = default;
|
||||
~DeviceInfoProviderImpl() override {}
|
||||
|
||||
// Iterators
|
||||
FixedLabelIterator * IterateFixedLabel(EndpointId endpoint) override;
|
||||
UserLabelIterator * IterateUserLabel(EndpointId endpoint) override;
|
||||
SupportedLocalesIterator * IterateSupportedLocales() override;
|
||||
SupportedCalendarTypesIterator * IterateSupportedCalendarTypes() override;
|
||||
|
||||
static DeviceInfoProviderImpl & GetDefaultInstance();
|
||||
|
||||
protected:
|
||||
class FixedLabelIteratorImpl : public FixedLabelIterator
|
||||
{
|
||||
public:
|
||||
FixedLabelIteratorImpl(EndpointId endpoint);
|
||||
size_t Count() override;
|
||||
bool Next(FixedLabelType & output) override;
|
||||
void Release() override { chip::Platform::Delete(this); }
|
||||
|
||||
private:
|
||||
EndpointId mEndpoint = 0;
|
||||
size_t mIndex = 0;
|
||||
char mFixedLabelNameBuf[kMaxLabelNameLength + 1];
|
||||
char mFixedLabelValueBuf[kMaxLabelValueLength + 1];
|
||||
};
|
||||
|
||||
class UserLabelIteratorImpl : public UserLabelIterator
|
||||
{
|
||||
public:
|
||||
UserLabelIteratorImpl(DeviceInfoProviderImpl & provider, EndpointId endpoint);
|
||||
size_t Count() override { return mTotal; }
|
||||
bool Next(UserLabelType & output) override;
|
||||
void Release() override { chip::Platform::Delete(this); }
|
||||
|
||||
private:
|
||||
DeviceInfoProviderImpl & mProvider;
|
||||
EndpointId mEndpoint = 0;
|
||||
size_t mIndex = 0;
|
||||
size_t mTotal = 0;
|
||||
char mUserLabelNameBuf[kMaxLabelNameLength + 1];
|
||||
char mUserLabelValueBuf[kMaxLabelValueLength + 1];
|
||||
};
|
||||
|
||||
class SupportedLocalesIteratorImpl : public SupportedLocalesIterator
|
||||
{
|
||||
public:
|
||||
SupportedLocalesIteratorImpl() = default;
|
||||
size_t Count() override;
|
||||
bool Next(CharSpan & output) override;
|
||||
void Release() override { chip::Platform::Delete(this); }
|
||||
|
||||
private:
|
||||
size_t mIndex = 0;
|
||||
char mActiveLocaleBuf[kMaxActiveLocaleLength + 1];
|
||||
};
|
||||
|
||||
class SupportedCalendarTypesIteratorImpl : public SupportedCalendarTypesIterator
|
||||
{
|
||||
public:
|
||||
SupportedCalendarTypesIteratorImpl() = default;
|
||||
size_t Count() override;
|
||||
bool Next(CalendarType & output) override;
|
||||
void Release() override { chip::Platform::Delete(this); }
|
||||
|
||||
private:
|
||||
size_t mIndex = 0;
|
||||
};
|
||||
|
||||
CHIP_ERROR SetUserLabelLength(EndpointId endpoint, size_t val) override;
|
||||
CHIP_ERROR GetUserLabelLength(EndpointId endpoint, size_t & val) override;
|
||||
CHIP_ERROR SetUserLabelAt(EndpointId endpoint, size_t index, const UserLabelType & userLabel) override;
|
||||
CHIP_ERROR DeleteUserLabelAt(EndpointId endpoint, size_t index) override;
|
||||
|
||||
private:
|
||||
static constexpr size_t UserLabelTLVMaxSize() { return TLV::EstimateStructOverhead(kMaxLabelNameLength, kMaxLabelValueLength); }
|
||||
};
|
||||
|
||||
} // namespace DeviceLayer
|
||||
} // namespace chip
|
||||
72
Libs/Matter/third_party/matter_sdk/examples/shell/shell_common/cmd_misc.cpp
vendored
Normal file
72
Libs/Matter/third_party/matter_sdk/examples/shell/shell_common/cmd_misc.cpp
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2020 Project CHIP Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <lib/shell/Engine.h>
|
||||
|
||||
#include <crypto/RandUtils.h>
|
||||
#include <lib/core/CHIPCore.h>
|
||||
#include <lib/support/Base64.h>
|
||||
#include <lib/support/CHIPArgParser.hpp>
|
||||
#include <lib/support/CodeUtils.h>
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <ChipShellCollection.h>
|
||||
|
||||
using namespace chip;
|
||||
using namespace chip::Shell;
|
||||
using namespace chip::Logging;
|
||||
|
||||
CHIP_ERROR cmd_echo(int argc, char ** argv)
|
||||
{
|
||||
for (int i = 0; i < argc; i++)
|
||||
{
|
||||
streamer_printf(streamer_get(), "%s ", argv[i]);
|
||||
}
|
||||
streamer_printf(streamer_get(), "\n\r");
|
||||
return CHIP_NO_ERROR;
|
||||
}
|
||||
|
||||
CHIP_ERROR cmd_log(int argc, char ** argv)
|
||||
{
|
||||
for (int i = 0; i < argc; i++)
|
||||
{
|
||||
ChipLogProgress(chipTool, "%s", argv[i]);
|
||||
}
|
||||
return CHIP_NO_ERROR;
|
||||
}
|
||||
|
||||
CHIP_ERROR cmd_rand(int argc, char ** argv)
|
||||
{
|
||||
streamer_printf(streamer_get(), "%d\n\r", static_cast<int>(chip::Crypto::GetRandU8()));
|
||||
return CHIP_NO_ERROR;
|
||||
}
|
||||
|
||||
static shell_command_t cmds_misc[] = {
|
||||
{ &cmd_echo, "echo", "Echo back provided inputs" },
|
||||
{ &cmd_log, "log", "Logging utilities" },
|
||||
{ &cmd_rand, "rand", "Random number utilities" },
|
||||
};
|
||||
|
||||
void cmd_misc_init()
|
||||
{
|
||||
Engine::Root().RegisterCommands(cmds_misc, ArraySize(cmds_misc));
|
||||
}
|
||||
204
Libs/Matter/third_party/matter_sdk/examples/shell/shell_common/cmd_otcli.cpp
vendored
Normal file
204
Libs/Matter/third_party/matter_sdk/examples/shell/shell_common/cmd_otcli.cpp
vendored
Normal file
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2020 Project CHIP Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <lib/core/CHIPCore.h>
|
||||
|
||||
#include <ChipShellCollection.h>
|
||||
|
||||
#if CONFIG_DEVICE_LAYER
|
||||
#include <platform/CHIPDeviceLayer.h>
|
||||
#endif
|
||||
|
||||
#if CHIP_ENABLE_OPENTHREAD
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <lib/shell/Engine.h>
|
||||
#include <lib/support/CHIPArgParser.hpp>
|
||||
#include <lib/support/CHIPMem.h>
|
||||
#include <lib/support/CodeUtils.h>
|
||||
#include <platform/ThreadStackManager.h>
|
||||
|
||||
#if CHIP_TARGET_STYLE_EMBEDDED
|
||||
#include <openthread/cli.h>
|
||||
#include <openthread/instance.h>
|
||||
#include <openthread/ip6.h>
|
||||
#include <openthread/link.h>
|
||||
#include <openthread/thread.h>
|
||||
#if OPENTHREAD_API_VERSION >= 85
|
||||
#if !CHIP_DEVICE_CONFIG_THREAD_ENABLE_CLI
|
||||
#ifndef SHELL_OTCLI_TX_BUFFER_SIZE
|
||||
#define SHELL_OTCLI_TX_BUFFER_SIZE 1024
|
||||
#endif
|
||||
static char sTxBuffer[SHELL_OTCLI_TX_BUFFER_SIZE];
|
||||
static constexpr uint16_t sTxLength = SHELL_OTCLI_TX_BUFFER_SIZE;
|
||||
#endif // !CHIP_DEVICE_CONFIG_THREAD_ENABLE_CLI)
|
||||
#endif
|
||||
static constexpr uint16_t kMaxLineLength = 384;
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
using namespace chip;
|
||||
using namespace chip::Shell;
|
||||
using namespace chip::Platform;
|
||||
using namespace chip::DeviceLayer;
|
||||
using namespace chip::Logging;
|
||||
using namespace chip::ArgParser;
|
||||
|
||||
static chip::Shell::Engine sShellOtcliSubcommands;
|
||||
|
||||
CHIP_ERROR cmd_otcli_help_iterator(shell_command_t * command, void * arg)
|
||||
{
|
||||
streamer_printf(streamer_get(), " %-15s %s\n\r", command->cmd_name, command->cmd_help);
|
||||
return CHIP_NO_ERROR;
|
||||
}
|
||||
|
||||
CHIP_ERROR cmd_otcli_help(int argc, char ** argv)
|
||||
{
|
||||
sShellOtcliSubcommands.ForEachCommand(cmd_otcli_help_iterator, nullptr);
|
||||
return CHIP_NO_ERROR;
|
||||
}
|
||||
|
||||
#if CHIP_TARGET_STYLE_EMBEDDED
|
||||
|
||||
CHIP_ERROR cmd_otcli_dispatch(int argc, char ** argv)
|
||||
{
|
||||
CHIP_ERROR error = CHIP_NO_ERROR;
|
||||
|
||||
char buff[kMaxLineLength] = { 0 };
|
||||
char * buff_ptr = buff;
|
||||
int i = 0;
|
||||
|
||||
VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT);
|
||||
|
||||
for (i = 0; i < argc; i++)
|
||||
{
|
||||
size_t arg_len = strlen(argv[i]);
|
||||
|
||||
/* Make sure that the next argument won't overflow the buffer */
|
||||
VerifyOrExit(buff_ptr + arg_len < buff + kMaxLineLength, error = CHIP_ERROR_BUFFER_TOO_SMALL);
|
||||
|
||||
strncpy(buff_ptr, argv[i], arg_len);
|
||||
buff_ptr += arg_len;
|
||||
|
||||
/* Make sure that there is enough buffer for a space char */
|
||||
if (buff_ptr + sizeof(char) < buff + kMaxLineLength)
|
||||
{
|
||||
strncpy(buff_ptr, " ", sizeof(char));
|
||||
buff_ptr++;
|
||||
}
|
||||
}
|
||||
buff_ptr = 0;
|
||||
chip::DeviceLayer::ThreadStackMgr().LockThreadStack();
|
||||
#if OPENTHREAD_API_VERSION >= 85
|
||||
otCliInputLine(buff);
|
||||
#else
|
||||
otCliConsoleInputLine(buff, buff_ptr - buff);
|
||||
#endif
|
||||
chip::DeviceLayer::ThreadStackMgr().UnlockThreadStack();
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
#elif CHIP_TARGET_STYLE_UNIX
|
||||
|
||||
CHIP_ERROR cmd_otcli_dispatch(int argc, char ** argv)
|
||||
{
|
||||
int pid;
|
||||
uid_t euid = geteuid();
|
||||
char ctl_command[] = "/usr/local/sbin/ot-ctl";
|
||||
|
||||
// Must run as sudo.
|
||||
if (euid != 0)
|
||||
{
|
||||
streamer_printf(streamer_get(), "Error otcli: requires running chip-shell as sudo\n\r");
|
||||
return CHIP_ERROR_INCORRECT_STATE;
|
||||
}
|
||||
|
||||
VerifyOrReturnError(argc > 0, CHIP_ERROR_INVALID_ARGUMENT);
|
||||
|
||||
// Fork and execute the command.
|
||||
pid = fork();
|
||||
VerifyOrReturnError(pid != -1, CHIP_ERROR_INCORRECT_STATE);
|
||||
|
||||
if (pid == 0)
|
||||
{
|
||||
// Child process to execute the command with provided arguments
|
||||
--argv; // Restore access to entry [0] containing the command;
|
||||
argv[0] = ctl_command;
|
||||
if (execvp(ctl_command, argv) < 0)
|
||||
{
|
||||
streamer_printf(streamer_get(), "Error exec %s: %s\n", ctl_command, strerror(errno));
|
||||
}
|
||||
exit(errno);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Parent process to wait on child.
|
||||
int status;
|
||||
wait(&status);
|
||||
return (status) ? CHIP_ERROR_INCORRECT_STATE : CHIP_NO_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // CHIP_TARGET_STYLE_UNIX
|
||||
|
||||
static const shell_command_t cmds_otcli_root = { &cmd_otcli_dispatch, "otcli", "Dispatch OpenThread CLI command" };
|
||||
|
||||
#if CHIP_TARGET_STYLE_EMBEDDED
|
||||
#if OPENTHREAD_API_VERSION >= 85
|
||||
#if !CHIP_DEVICE_CONFIG_THREAD_ENABLE_CLI
|
||||
static int OnOtCliOutput(void * aContext, const char * aFormat, va_list aArguments)
|
||||
{
|
||||
int rval = vsnprintf(sTxBuffer, sTxLength, aFormat, aArguments);
|
||||
VerifyOrExit(rval >= 0 && rval < sTxLength, rval = CHIP_ERROR_BUFFER_TOO_SMALL.AsInteger());
|
||||
return streamer_write(streamer_get(), (const char *) sTxBuffer, rval);
|
||||
exit:
|
||||
return rval;
|
||||
}
|
||||
#endif // !CHIP_DEVICE_CONFIG_THREAD_ENABLE_CLI
|
||||
#else
|
||||
|
||||
static int OnOtCliOutput(const char * aBuf, uint16_t aBufLength, void * aContext)
|
||||
{
|
||||
return streamer_write(streamer_get(), aBuf, aBufLength);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif // CHIP_ENABLE_OPENTHREAD
|
||||
|
||||
void cmd_otcli_init()
|
||||
{
|
||||
#if CHIP_ENABLE_OPENTHREAD
|
||||
#if CHIP_TARGET_STYLE_EMBEDDED
|
||||
#if !CHIP_DEVICE_CONFIG_THREAD_ENABLE_CLI
|
||||
#if OPENTHREAD_API_VERSION >= 85
|
||||
otCliInit(otInstanceInitSingle(), &OnOtCliOutput, NULL);
|
||||
#else
|
||||
otCliConsoleInit(otInstanceInitSingle(), &OnOtCliOutput, NULL);
|
||||
#endif // OPENTHREAD_API_VERSION >= 85
|
||||
#endif // !CHIP_DEVICE_CONFIG_THREAD_ENABLE_CLI
|
||||
#endif // CHIP_TARGET_STYLE_EMBEDDED
|
||||
|
||||
// Register the root otcli command with the top-level shell.
|
||||
Engine::Root().RegisterCommands(&cmds_otcli_root, 1);
|
||||
#endif // CHIP_ENABLE_OPENTHREAD
|
||||
}
|
||||
33
Libs/Matter/third_party/matter_sdk/examples/shell/shell_common/globals.cpp
vendored
Normal file
33
Libs/Matter/third_party/matter_sdk/examples/shell/shell_common/globals.cpp
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2021 Project CHIP Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <Globals.h>
|
||||
|
||||
chip::FabricTable gFabricTable;
|
||||
chip::secure_channel::MessageCounterManager gMessageCounterManager;
|
||||
chip::Messaging::ExchangeManager gExchangeManager;
|
||||
chip::SessionManager gSessionManager;
|
||||
chip::Inet::IPAddress gDestAddr;
|
||||
chip::SessionHolder gSession;
|
||||
chip::TestPersistentStorageDelegate gStorage;
|
||||
|
||||
chip::FabricIndex gFabricIndex = 0;
|
||||
|
||||
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
|
||||
chip::TransportMgr<chip::Transport::TCP<kMaxTcpActiveConnectionCount, kMaxTcpPendingPackets>> gTCPManager;
|
||||
#endif
|
||||
chip::TransportMgr<chip::Transport::UDP> gUDPManager;
|
||||
23
Libs/Matter/third_party/matter_sdk/examples/shell/shell_common/include/ChipShellCollection.h
vendored
Normal file
23
Libs/Matter/third_party/matter_sdk/examples/shell/shell_common/include/ChipShellCollection.h
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2020 Project CHIP Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
// A list of shell commands provided by ChipShell
|
||||
void cmd_misc_init(void);
|
||||
void cmd_otcli_init(void);
|
||||
void cmd_app_server_init(void);
|
||||
51
Libs/Matter/third_party/matter_sdk/examples/shell/shell_common/include/Globals.h
vendored
Normal file
51
Libs/Matter/third_party/matter_sdk/examples/shell/shell_common/include/Globals.h
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
*
|
||||
* Copyright (c) 2021 Project CHIP Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <credentials/FabricTable.h>
|
||||
#include <lib/core/CHIPCore.h>
|
||||
#include <lib/support/TestPersistentStorageDelegate.h>
|
||||
#include <messaging/ExchangeMgr.h>
|
||||
#include <protocols/secure_channel/MessageCounterManager.h>
|
||||
#include <transport/SessionHolder.h>
|
||||
#include <transport/SessionManager.h>
|
||||
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
|
||||
#include <transport/raw/TCP.h>
|
||||
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
|
||||
#include <transport/raw/UDP.h>
|
||||
|
||||
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
|
||||
inline constexpr size_t kMaxTcpActiveConnectionCount = 4;
|
||||
inline constexpr size_t kMaxTcpPendingPackets = 4;
|
||||
#endif
|
||||
inline constexpr chip::System::Clock::Timeout kResponseTimeOut = chip::System::Clock::Seconds16(1);
|
||||
|
||||
extern chip::FabricTable gFabricTable;
|
||||
extern chip::secure_channel::MessageCounterManager gMessageCounterManager;
|
||||
extern chip::Messaging::ExchangeManager gExchangeManager;
|
||||
extern chip::SessionManager gSessionManager;
|
||||
extern chip::Inet::IPAddress gDestAddr;
|
||||
extern chip::SessionHolder gSession;
|
||||
extern chip::TestPersistentStorageDelegate gStorage;
|
||||
|
||||
extern chip::FabricIndex gFabricIndex;
|
||||
|
||||
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
|
||||
extern chip::TransportMgr<chip::Transport::TCP<kMaxTcpActiveConnectionCount, kMaxTcpPendingPackets>> gTCPManager;
|
||||
#endif
|
||||
extern chip::TransportMgr<chip::Transport::UDP> gUDPManager;
|
||||
Reference in New Issue
Block a user