Skip to content

Commit

Permalink
Added option to override Vendor & Product IDs via cmd-line (#17001)
Browse files Browse the repository at this point in the history
  • Loading branch information
vijs authored and pull[bot] committed May 3, 2022
1 parent 23e149e commit 1379690
Show file tree
Hide file tree
Showing 18 changed files with 314 additions and 5 deletions.
24 changes: 21 additions & 3 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <lib/core/NodeId.h>
#include <lib/support/logging/CHIPLogging.h>

#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/GroupDataProviderImpl.h>
#include <credentials/attestation_verifier/DefaultDeviceAttestationVerifier.h>
#include <credentials/attestation_verifier/DeviceAttestationVerifier.h>
Expand Down Expand Up @@ -221,6 +220,21 @@ CHIP_ERROR InitCommissionableDataProvider(LinuxCommissionableDataProvider & prov
options.payload.discriminator);
}

CHIP_ERROR InitConfigurationManager(ConfigurationManagerImpl & configManager, LinuxDeviceOptions & options)
{
if (options.payload.vendorID != 0)
{
configManager.StoreVendorId(options.payload.vendorID);
}

if (options.payload.productID != 0)
{
configManager.StoreProductId(options.payload.productID);
}

return CHIP_NO_ERROR;
}

void Cleanup()
{
#if CHIP_CONFIG_TRANSPORT_TRACE_ENABLED
Expand Down Expand Up @@ -294,6 +308,10 @@ int ChipLinuxAppInit(int argc, char ** argv, OptionSet * customOptions)
SuccessOrExit(err);
DeviceLayer::SetCommissionableDataProvider(&gCommissionableDataProvider);

err = InitConfigurationManager(reinterpret_cast<ConfigurationManagerImpl &>(ConfigurationMgr()),
LinuxDeviceOptions::GetInstance());
SuccessOrExit(err);

err = GetSetupPayload(LinuxDeviceOptions::GetInstance().payload, rendezvousFlags);
SuccessOrExit(err);

Expand Down Expand Up @@ -691,7 +709,7 @@ CommissionerDiscoveryController * GetCommissionerDiscoveryController()

#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE

void ChipLinuxAppMainLoop()
void ChipLinuxAppMainLoop(DeviceAttestationCredentialsProvider * dacProvider)
{
static chip::CommonCaseDeviceServerInitParams initParams;
VerifyOrDie(initParams.InitializeStaticResourcesBeforeServerInit() == CHIP_NO_ERROR);
Expand Down Expand Up @@ -724,7 +742,7 @@ void ChipLinuxAppMainLoop()
PrintOnboardingCodes(LinuxDeviceOptions::GetInstance().payload);

// Initialize device attestation config
SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider());
SetDeviceAttestationCredentialsProvider(dacProvider == nullptr ? Examples::GetExampleDACProvider() : dacProvider);

#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
ChipLogProgress(AppServer, "Starting commissioner");
Expand Down
3 changes: 2 additions & 1 deletion examples/platform/linux/AppMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <controller/CHIPDeviceController.h>
#include <controller/CommissionerDiscoveryController.h>
#include <credentials/DeviceAttestationCredsProvider.h>
#include <lib/core/CHIPError.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/PlatformManager.h>
Expand All @@ -28,7 +29,7 @@
#include "Options.h"

int ChipLinuxAppInit(int argc, char ** argv, chip::ArgParser::OptionSet * customOptions = nullptr);
void ChipLinuxAppMainLoop();
void ChipLinuxAppMainLoop(chip::Credentials::DeviceAttestationCredentialsProvider * dacProvider = nullptr);

#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE

Expand Down
41 changes: 41 additions & 0 deletions src/platform/Darwin/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <lib/core/CHIPVendorIdentifiers.hpp>
#include <platform/CHIPDeviceConfig.h>
#include <platform/ConfigurationManager.h>
#include <platform/Darwin/DiagnosticDataProviderImpl.h>
#include <platform/Darwin/PosixConfig.h>
Expand Down Expand Up @@ -141,6 +142,16 @@ CHIP_ERROR ConfigurationManagerImpl::Init()
// Initialize the generic implementation base class.
ReturnErrorOnFailure(Internal::GenericConfigurationManagerImpl<PosixConfig>::Init());

if (!PosixConfig::ConfigValueExists(PosixConfig::kConfigKey_VendorId))
{
ReturnErrorOnFailure(StoreVendorId(CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID));
}

if (!PosixConfig::ConfigValueExists(PosixConfig::kConfigKey_ProductId))
{
ReturnErrorOnFailure(StoreProductId(CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID));
}

uint32_t rebootCount;
if (!PosixConfig::ConfigValueExists(PosixConfig::kCounterKey_RebootCount))
{
Expand Down Expand Up @@ -207,6 +218,26 @@ void ConfigurationManagerImpl::InitiateFactoryReset()
ChipLogError(DeviceLayer, "InitiateFactoryReset not implemented");
}

CHIP_ERROR ConfigurationManagerImpl::GetVendorId(uint16_t & vendorId)
{
return ReadConfigValue(PosixConfig::kConfigKey_VendorId, vendorId);
}

CHIP_ERROR ConfigurationManagerImpl::GetProductId(uint16_t & productId)
{
return ReadConfigValue(PosixConfig::kConfigKey_ProductId, productId);
}

CHIP_ERROR ConfigurationManagerImpl::StoreVendorId(uint16_t vendorId)
{
return WriteConfigValue(PosixConfig::kConfigKey_VendorId, vendorId);
}

CHIP_ERROR ConfigurationManagerImpl::StoreProductId(uint16_t productId)
{
return WriteConfigValue(PosixConfig::kConfigKey_ProductId, productId);
}

CHIP_ERROR ConfigurationManagerImpl::GetRebootCount(uint32_t & rebootCount)
{
return ReadConfigValue(PosixConfig::kCounterKey_RebootCount, rebootCount);
Expand Down Expand Up @@ -290,6 +321,11 @@ CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, bool & val)
return PosixConfig::ReadConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, uint16_t & val)
{
return PosixConfig::ReadConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::ReadConfigValue(Key key, uint32_t & val)
{
return PosixConfig::ReadConfigValue(key, val);
Expand All @@ -315,6 +351,11 @@ CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, bool val)
return PosixConfig::WriteConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, uint16_t val)
{
return PosixConfig::WriteConfigValue(key, val);
}

CHIP_ERROR ConfigurationManagerImpl::WriteConfigValue(Key key, uint32_t val)
{
return PosixConfig::WriteConfigValue(key, val);
Expand Down
8 changes: 8 additions & 0 deletions src/platform/Darwin/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ static constexpr int kCountryCodeLength = 2;
class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImpl<Internal::PosixConfig>
{
public:
CHIP_ERROR StoreVendorId(uint16_t vendorId);
CHIP_ERROR StoreProductId(uint16_t productId);

CHIP_ERROR GetVendorId(uint16_t & vendorId) override;
CHIP_ERROR GetProductId(uint16_t & productId) override;

// This returns an instance of this class.
static ConfigurationManagerImpl & GetDefaultInstance();

Expand All @@ -60,6 +66,8 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
CHIP_ERROR WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value) override;

// NOTE: Other public interface methods are implemented by GenericConfigurationManagerImpl<>.
CHIP_ERROR WriteConfigValue(Key key, uint16_t val);
CHIP_ERROR ReadConfigValue(Key key, uint16_t & val);

// ===== Members that implement the GenericConfigurationManagerImpl protected interface.
CHIP_ERROR ReadConfigValue(Key key, bool & val) override;
Expand Down
13 changes: 13 additions & 0 deletions src/platform/Darwin/PosixConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const PosixConfig::Key PosixConfig::kConfigKey_SetupDiscriminator = { kConfig
const PosixConfig::Key PosixConfig::kConfigKey_Spake2pIterationCount = { kConfigNamespace_ChipFactory, "iteration-count" };
const PosixConfig::Key PosixConfig::kConfigKey_Spake2pSalt = { kConfigNamespace_ChipFactory, "salt" };
const PosixConfig::Key PosixConfig::kConfigKey_Spake2pVerifier = { kConfigNamespace_ChipFactory, "verifier" };
const PosixConfig::Key PosixConfig::kConfigKey_VendorId = { kConfigNamespace_ChipFactory, "vendor-id" };
const PosixConfig::Key PosixConfig::kConfigKey_ProductId = { kConfigNamespace_ChipFactory, "product-id" };

// Keys stored in the Chip-config namespace
const PosixConfig::Key PosixConfig::kConfigKey_FabricId = { kConfigNamespace_ChipConfig, "fabric-id" };
Expand Down Expand Up @@ -95,6 +97,12 @@ CHIP_ERROR PosixConfig::ReadConfigValue(Key key, bool & val)
return ReadConfigValueBin(key, reinterpret_cast<uint8_t *>(&val), sizeof(val), outLen);
}

CHIP_ERROR PosixConfig::ReadConfigValue(Key key, uint16_t & val)
{
size_t outLen = 0;
return ReadConfigValueBin(key, reinterpret_cast<uint8_t *>(&val), sizeof(val), outLen);
}

CHIP_ERROR PosixConfig::ReadConfigValue(Key key, uint32_t & val)
{
size_t outLen = 0;
Expand Down Expand Up @@ -127,6 +135,11 @@ CHIP_ERROR PosixConfig::WriteConfigValue(Key key, bool val)
return WriteConfigValueBin(key, reinterpret_cast<uint8_t *>(&val), sizeof(val));
}

CHIP_ERROR PosixConfig::WriteConfigValue(Key key, uint16_t val)
{
return WriteConfigValueBin(key, reinterpret_cast<uint8_t *>(&val), sizeof(val));
}

CHIP_ERROR PosixConfig::WriteConfigValue(Key key, uint32_t val)
{
return WriteConfigValueBin(key, reinterpret_cast<uint8_t *>(&val), sizeof(val));
Expand Down
4 changes: 4 additions & 0 deletions src/platform/Darwin/PosixConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class PosixConfig
static const Key kConfigKey_Spake2pSalt;
static const Key kConfigKey_Spake2pVerifier;
static const Key kConfigKey_LocationCapability;
static const Key kConfigKey_VendorId;
static const Key kConfigKey_ProductId;

static const Key kCounterKey_TotalOperationalHours;
static const Key kCounterKey_RebootCount;
Expand All @@ -90,11 +92,13 @@ class PosixConfig

// Config value accessors.
static CHIP_ERROR ReadConfigValue(Key key, bool & val);
static CHIP_ERROR ReadConfigValue(Key key, uint16_t & val);
static CHIP_ERROR ReadConfigValue(Key key, uint32_t & val);
static CHIP_ERROR ReadConfigValue(Key key, uint64_t & val);
static CHIP_ERROR ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen);
static CHIP_ERROR ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen);
static CHIP_ERROR WriteConfigValue(Key key, bool val);
static CHIP_ERROR WriteConfigValue(Key key, uint16_t val);
static CHIP_ERROR WriteConfigValue(Key key, uint32_t val);
static CHIP_ERROR WriteConfigValue(Key key, uint64_t val);
static CHIP_ERROR WriteConfigValueStr(Key key, const char * str);
Expand Down
22 changes: 22 additions & 0 deletions src/platform/Linux/CHIPLinuxStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ CHIP_ERROR ChipLinuxStorage::ReadValue(const char * key, bool & val)
return retval;
}

CHIP_ERROR ChipLinuxStorage::ReadValue(const char * key, uint16_t & val)
{
CHIP_ERROR retval = CHIP_NO_ERROR;

mLock.lock();

retval = ChipLinuxStorageIni::GetUInt16Value(key, val);

mLock.unlock();

return retval;
}

CHIP_ERROR ChipLinuxStorage::ReadValue(const char * key, uint32_t & val)
{
CHIP_ERROR retval = CHIP_NO_ERROR;
Expand Down Expand Up @@ -171,6 +184,15 @@ CHIP_ERROR ChipLinuxStorage::WriteValue(const char * key, bool val)
return retval;
}

CHIP_ERROR ChipLinuxStorage::WriteValue(const char * key, uint16_t val)
{
char buf[16];

snprintf(buf, sizeof(buf), "%" PRIu16, val);

return WriteValueStr(key, buf);
}

CHIP_ERROR ChipLinuxStorage::WriteValue(const char * key, uint32_t val)
{
char buf[32];
Expand Down
2 changes: 2 additions & 0 deletions src/platform/Linux/CHIPLinuxStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ class ChipLinuxStorage : private ChipLinuxStorageIni

CHIP_ERROR Init(const char * configFile);
CHIP_ERROR ReadValue(const char * key, bool & val);
CHIP_ERROR ReadValue(const char * key, uint16_t & val);
CHIP_ERROR ReadValue(const char * key, uint32_t & val);
CHIP_ERROR ReadValue(const char * key, uint64_t & val);
CHIP_ERROR ReadValueStr(const char * key, char * buf, size_t bufSize, size_t & outLen);
CHIP_ERROR ReadValueBin(const char * key, uint8_t * buf, size_t bufSize, size_t & outLen);
CHIP_ERROR WriteValue(const char * key, bool val);
CHIP_ERROR WriteValue(const char * key, uint16_t val);
CHIP_ERROR WriteValue(const char * key, uint32_t val);
CHIP_ERROR WriteValue(const char * key, uint64_t val);
CHIP_ERROR WriteValueStr(const char * key, const char * val);
Expand Down
27 changes: 27 additions & 0 deletions src/platform/Linux/CHIPLinuxStorageIni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,33 @@ CHIP_ERROR ChipLinuxStorageIni::CommitConfig(const std::string & configFile)
return retval;
}

CHIP_ERROR ChipLinuxStorageIni::GetUInt16Value(const char * key, uint16_t & val)
{
CHIP_ERROR retval = CHIP_NO_ERROR;
std::map<std::string, std::string> section;

retval = GetDefaultSection(section);

if (retval == CHIP_NO_ERROR)
{
auto it = section.find(key);

if (it != section.end())
{
if (!inipp::extract(section[key], val))
{
retval = CHIP_ERROR_INVALID_ARGUMENT;
}
}
else
{
retval = CHIP_ERROR_KEY_NOT_FOUND;
}
}

return retval;
}

CHIP_ERROR ChipLinuxStorageIni::GetUIntValue(const char * key, uint32_t & val)
{
CHIP_ERROR retval = CHIP_NO_ERROR;
Expand Down
1 change: 1 addition & 0 deletions src/platform/Linux/CHIPLinuxStorageIni.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ChipLinuxStorageIni
CHIP_ERROR Init();
CHIP_ERROR AddConfig(const std::string & configFile);
CHIP_ERROR CommitConfig(const std::string & configFile);
CHIP_ERROR GetUInt16Value(const char * key, uint16_t & val);
CHIP_ERROR GetUIntValue(const char * key, uint32_t & val);
CHIP_ERROR GetUInt64Value(const char * key, uint64_t & val);
CHIP_ERROR GetStringValue(const char * key, char * buf, size_t bufSize, size_t & outLen);
Expand Down
Loading

0 comments on commit 1379690

Please sign in to comment.