Skip to content

Commit

Permalink
Customized basic cluster android (#12610)
Browse files Browse the repository at this point in the history
* added example to read product-id and product-name in android world

* added hardware version & hardware version string

* added software version & software version string

* added ManufacturingDate and SerialNum

* added SetupPinCode and SetupDiscriminator

* fix restyled-io and ci errors

* move android read to android platform which is not needed for other platfroms

* fix restyled-io and ci errors
  • Loading branch information
xylophone21 authored Dec 6, 2021
1 parent 2de0792 commit db6d572
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/platform/android/AndroidConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,20 @@ const char AndroidConfig::kConfigNamespace_ChipConfig[] = "chip-config";
const char AndroidConfig::kConfigNamespace_ChipCounters[] = "chip-counters";

// Keys stored in the Chip-factory namespace
const AndroidConfig::Key AndroidConfig::kConfigKey_SerialNum = { kConfigNamespace_ChipFactory, "serial-num" };
const AndroidConfig::Key AndroidConfig::kConfigKey_MfrDeviceId = { kConfigNamespace_ChipFactory, "device-id" };
const AndroidConfig::Key AndroidConfig::kConfigKey_MfrDeviceCert = { kConfigNamespace_ChipFactory, "device-cert" };
const AndroidConfig::Key AndroidConfig::kConfigKey_MfrDeviceICACerts = { kConfigNamespace_ChipFactory, "device-ca-certs" };
const AndroidConfig::Key AndroidConfig::kConfigKey_MfrDevicePrivateKey = { kConfigNamespace_ChipFactory, "device-key" };
const AndroidConfig::Key AndroidConfig::kConfigKey_HardwareVersion = { kConfigNamespace_ChipFactory, "hardware-ver" };
const AndroidConfig::Key AndroidConfig::kConfigKey_ManufacturingDate = { kConfigNamespace_ChipFactory, "mfg-date" };
const AndroidConfig::Key AndroidConfig::kConfigKey_SetupPinCode = { kConfigNamespace_ChipFactory, "pin-code" };
const AndroidConfig::Key AndroidConfig::kConfigKey_SetupDiscriminator = { kConfigNamespace_ChipFactory, "discriminator" };
const AndroidConfig::Key AndroidConfig::kConfigKey_SerialNum = { kConfigNamespace_ChipFactory, "serial-num" };
const AndroidConfig::Key AndroidConfig::kConfigKey_MfrDeviceId = { kConfigNamespace_ChipFactory, "device-id" };
const AndroidConfig::Key AndroidConfig::kConfigKey_MfrDeviceCert = { kConfigNamespace_ChipFactory, "device-cert" };
const AndroidConfig::Key AndroidConfig::kConfigKey_MfrDeviceICACerts = { kConfigNamespace_ChipFactory, "device-ca-certs" };
const AndroidConfig::Key AndroidConfig::kConfigKey_MfrDevicePrivateKey = { kConfigNamespace_ChipFactory, "device-key" };
const AndroidConfig::Key AndroidConfig::kConfigKey_HardwareVersion = { kConfigNamespace_ChipFactory, "hardware-ver" };
const AndroidConfig::Key AndroidConfig::kConfigKey_HardwareVersionString = { kConfigNamespace_ChipFactory, "hardware-ver-str" };
const AndroidConfig::Key AndroidConfig::kConfigKey_ManufacturingDate = { kConfigNamespace_ChipFactory, "mfg-date" };
const AndroidConfig::Key AndroidConfig::kConfigKey_SetupPinCode = { kConfigNamespace_ChipFactory, "pin-code" };
const AndroidConfig::Key AndroidConfig::kConfigKey_SetupDiscriminator = { kConfigNamespace_ChipFactory, "discriminator" };
const AndroidConfig::Key AndroidConfig::kConfigKey_ProductId = { kConfigNamespace_ChipFactory, "product-id" };
const AndroidConfig::Key AndroidConfig::kConfigKey_ProductName = { kConfigNamespace_ChipFactory, "product-name" };
const AndroidConfig::Key AndroidConfig::kConfigKey_SoftwareVersion = { kConfigNamespace_ChipFactory, "software-version" };
const AndroidConfig::Key AndroidConfig::kConfigKey_SoftwareVersionString = { kConfigNamespace_ChipFactory, "software-version-str" };

// Keys stored in the Chip-config namespace
const AndroidConfig::Key AndroidConfig::kConfigKey_FabricId = { kConfigNamespace_ChipConfig, "fabric-id" };
Expand Down
5 changes: 5 additions & 0 deletions src/platform/android/AndroidConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class AndroidConfig
static const Key kConfigKey_MfrDeviceICACerts;
static const Key kConfigKey_MfrDevicePrivateKey;
static const Key kConfigKey_HardwareVersion;
static const Key kConfigKey_HardwareVersionString;
static const Key kConfigKey_ManufacturingDate;
static const Key kConfigKey_SetupPinCode;
static const Key kConfigKey_FabricId;
Expand All @@ -74,6 +75,10 @@ class AndroidConfig
static const Key kConfigKey_RegulatoryLocation;
static const Key kConfigKey_CountryCode;
static const Key kConfigKey_Breadcrumb;
static const Key kConfigKey_ProductId;
static const Key kConfigKey_ProductName;
static const Key kConfigKey_SoftwareVersion;
static const Key kConfigKey_SoftwareVersionString;

static const char kGroupKeyNamePrefix[];

Expand Down
79 changes: 79 additions & 0 deletions src/platform/android/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,84 @@ void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
return;
}

CHIP_ERROR ConfigurationManagerImpl::GetProductId(uint16_t & productId)
{
CHIP_ERROR err;
uint32_t u32ProductId = 0;
err = ReadConfigValue(AndroidConfig::kConfigKey_ProductId, u32ProductId);

if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
{
productId = static_cast<uint16_t>(CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID);
}
else
{
productId = static_cast<uint16_t>(u32ProductId);
}

return CHIP_NO_ERROR;
}

CHIP_ERROR ConfigurationManagerImpl::GetProductName(char * buf, size_t bufSize)
{
CHIP_ERROR err;
size_t productNameSize = 0; // without counting null-terminator
err = ReadConfigValueStr(AndroidConfig::kConfigKey_ProductName, buf, bufSize, productNameSize);
if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
{
ReturnErrorCodeIf(bufSize < sizeof(CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_NAME), CHIP_ERROR_BUFFER_TOO_SMALL);
strcpy(buf, CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_NAME);
}

return CHIP_NO_ERROR;
}

CHIP_ERROR ConfigurationManagerImpl::GetSoftwareVersion(uint16_t & softwareVer)
{
CHIP_ERROR err;
uint32_t u32SoftwareVer = 0;
err = ReadConfigValue(AndroidConfig::kConfigKey_SoftwareVersion, u32SoftwareVer);

if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
{
softwareVer = static_cast<uint16_t>(CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION);
}
else
{
softwareVer = static_cast<uint16_t>(u32SoftwareVer);
}

return CHIP_NO_ERROR;
}

CHIP_ERROR ConfigurationManagerImpl::GetSoftwareVersionString(char * buf, size_t bufSize)
{
CHIP_ERROR err;
size_t u32SoftwareVerSize = 0; // without counting null-terminator
err = ReadConfigValueStr(AndroidConfig::kConfigKey_SoftwareVersionString, buf, bufSize, u32SoftwareVerSize);

if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
{
ReturnErrorCodeIf(bufSize < sizeof(CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING), CHIP_ERROR_BUFFER_TOO_SMALL);
strcpy(buf, CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING);
}

return CHIP_NO_ERROR;
}

CHIP_ERROR ConfigurationManagerImpl::GetHardwareVersionString(char * buf, size_t bufSize)
{
CHIP_ERROR err;
size_t hardwareVersionLen = 0; // without counting null-terminator
err = ReadConfigValueStr(AndroidConfig::kConfigKey_HardwareVersionString, buf, bufSize, hardwareVersionLen);
if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
{
ReturnErrorCodeIf(bufSize < sizeof(CHIP_DEVICE_CONFIG_DEFAULT_DEVICE_HARDWARE_VERSION_STRING), CHIP_ERROR_BUFFER_TOO_SMALL);
strcpy(buf, CHIP_DEVICE_CONFIG_DEFAULT_DEVICE_HARDWARE_VERSION_STRING);
}

return CHIP_NO_ERROR;
}

} // namespace DeviceLayer
} // namespace chip
5 changes: 5 additions & 0 deletions src/platform/android/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
public:
void InitializeWithObject(jobject managerObject);
static ConfigurationManagerImpl & GetDefaultInstance();
CHIP_ERROR GetProductId(uint16_t & productId) override;
CHIP_ERROR GetProductName(char * buf, size_t bufSize) override;
CHIP_ERROR GetHardwareVersionString(char * buf, size_t bufSize) override;
CHIP_ERROR GetSoftwareVersionString(char * buf, size_t bufSize) override;
CHIP_ERROR GetSoftwareVersion(uint16_t & softwareVer) override;

private:
// ===== Members that implement the ConfigurationManager public interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ public interface ConfigurationManager {
String kConfigKey_MfrDeviceICACerts = "device-ca-certs";
String kConfigKey_MfrDevicePrivateKey = "device-key";
String kConfigKey_HardwareVersion = "hardware-ver";
String kConfigKey_HardwareVersionString = "hardware-ver-str";
String kConfigKey_ManufacturingDate = "mfg-date";
String kConfigKey_SetupPinCode = "pin-code";
String kConfigKey_SetupDiscriminator = "discriminator";
String kConfigKey_ProductId = "product-id";
String kConfigKey_ProductName = "product-name";
String kConfigKey_SoftwareVersion = "software-version";
String kConfigKey_SoftwareVersionString = "software-version-str";

// Keys stored in the Chip-config namespace
String kConfigKey_FabricId = "fabric-id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,48 @@ public PreferencesConfigurationManager(Context context) {
public long readConfigValueLong(String namespace, String name)
throws AndroidChipPlatformException {
String key = getKey(namespace, name);

switch (key) {
/**
* The unique id assigned by the device vendor to identify the product or device type. This
* number is scoped to the device vendor id. return a different value than
* src/include/platform/CHIPDeviceConfig.h for debug
*/
case kConfigNamespace_ChipFactory + ":" + kConfigKey_ProductId:
return 65278;

/**
* The default hardware version number assigned to the device or product by the device
* vendor.
*
* <p>Hardware versions are specific to a particular device vendor and product id, and
* typically correspond to a revision of the physical device, a change to its packaging,
* and/or a change to its marketing presentation. This value is generally *not* incremented
* for device software revisions.
*
* <p>This is a default value which is used when a hardware version has not been stored in
* device persistent storage (e.g. by a factory provisioning process).
*
* <p>return a different value than src/include/platform/CHIPDeviceConfig.h for debug
*/
case kConfigNamespace_ChipFactory + ":" + kConfigKey_HardwareVersion:
return 1;

/**
* A monothonic number identifying the software version running on the device.
*
* <p>return a different value than src/include/platform/CHIPDeviceConfig.h for debug
*/
case kConfigNamespace_ChipFactory + ":" + kConfigKey_SoftwareVersion:
return 1;

case kConfigNamespace_ChipFactory + ":" + kConfigKey_SetupPinCode:
return 20202021;

case kConfigNamespace_ChipFactory + ":" + kConfigKey_SetupDiscriminator:
return 0xF00;
}

if (preferences.contains(key)) {
long value = preferences.getLong(key, Long.MAX_VALUE);
return value;
Expand All @@ -51,6 +93,51 @@ public long readConfigValueLong(String namespace, String name)
public String readConfigValueStr(String namespace, String name)
throws AndroidChipPlatformException {
String key = getKey(namespace, name);

switch (key) {
/**
* Human readable name of the device model. return a different value than
* src/include/platform/CHIPDeviceConfig.h for debug
*/
case kConfigNamespace_ChipFactory + ":" + kConfigKey_ProductName:
return "TEST_ANDROID_PRODUCT";

/**
* Human readable string identifying version of the product assigned by the device vendor.
*
* <p>return a different value than src/include/platform/CHIPDeviceConfig.h for debug
*/
case kConfigNamespace_ChipFactory + ":" + kConfigKey_HardwareVersionString:
return "TEST_ANDROID_VERSION";

/**
* A string identifying the software version running on the device.
*
* <p>return a different value than src/include/platform/CHIPDeviceConfig.h for debug
*/
case kConfigNamespace_ChipFactory + ":" + kConfigKey_SoftwareVersionString:
return "prerelease(android)";

/**
* The ManufacturingDate attribute SHALL specify the date that the Node was manufactured.
* The first 8 characters SHALL specify the date of manufacture of the Node in international
* date notation according to ISO 8601, i.e., YYYYMMDD, e.g., 20060814. The final 8
* characters MAY include country, factory, line, shift or other related information at the
* option of the vendor. The format of this information is vendor defined.
*/
case kConfigNamespace_ChipFactory + ":" + kConfigKey_ManufacturingDate:
return "2021-12-06";

/**
* Enables the use of a hard-coded default serial number if none * is found in Chip NV
* storage.
*
* <p>return a different value than src/include/platform/CHIPDeviceConfig.h for debug
*/
case kConfigNamespace_ChipFactory + ":" + kConfigKey_SerialNum:
return "TEST_ANDROID_SN";
}

if (preferences.contains(key)) {
String value = preferences.getString(key, null);
return value;
Expand Down

0 comments on commit db6d572

Please sign in to comment.