From db6d57206f29b20bc98b842794b59233b67b134a Mon Sep 17 00:00:00 2001 From: "Hui.Li-TCL" Date: Tue, 7 Dec 2021 05:21:24 +0800 Subject: [PATCH] Customized basic cluster android (#12610) * 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 --- src/platform/android/AndroidConfig.cpp | 23 +++-- src/platform/android/AndroidConfig.h | 5 ++ .../android/ConfigurationManagerImpl.cpp | 79 +++++++++++++++++ .../android/ConfigurationManagerImpl.h | 5 ++ .../chip/platform/ConfigurationManager.java | 5 ++ .../PreferencesConfigurationManager.java | 87 +++++++++++++++++++ 6 files changed, 195 insertions(+), 9 deletions(-) diff --git a/src/platform/android/AndroidConfig.cpp b/src/platform/android/AndroidConfig.cpp index 16b0fa7b68dec9..62c4ceb59fc18b 100644 --- a/src/platform/android/AndroidConfig.cpp +++ b/src/platform/android/AndroidConfig.cpp @@ -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" }; diff --git a/src/platform/android/AndroidConfig.h b/src/platform/android/AndroidConfig.h index 1476ce42358cfd..afe20d5be0650d 100644 --- a/src/platform/android/AndroidConfig.h +++ b/src/platform/android/AndroidConfig.h @@ -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; @@ -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[]; diff --git a/src/platform/android/ConfigurationManagerImpl.cpp b/src/platform/android/ConfigurationManagerImpl.cpp index 3c61769abd0cd0..4920f2a3ba94d7 100644 --- a/src/platform/android/ConfigurationManagerImpl.cpp +++ b/src/platform/android/ConfigurationManagerImpl.cpp @@ -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(CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID); + } + else + { + productId = static_cast(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(CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION); + } + else + { + softwareVer = static_cast(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 diff --git a/src/platform/android/ConfigurationManagerImpl.h b/src/platform/android/ConfigurationManagerImpl.h index a8ee9fbca5c896..574dfeac8cfae7 100644 --- a/src/platform/android/ConfigurationManagerImpl.h +++ b/src/platform/android/ConfigurationManagerImpl.h @@ -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. diff --git a/src/platform/android/java/chip/platform/ConfigurationManager.java b/src/platform/android/java/chip/platform/ConfigurationManager.java index c437cd6fa0493d..5bec114ad5b1bf 100644 --- a/src/platform/android/java/chip/platform/ConfigurationManager.java +++ b/src/platform/android/java/chip/platform/ConfigurationManager.java @@ -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"; diff --git a/src/platform/android/java/chip/platform/PreferencesConfigurationManager.java b/src/platform/android/java/chip/platform/PreferencesConfigurationManager.java index 185eda83ca0a6e..fffd75456c35f1 100644 --- a/src/platform/android/java/chip/platform/PreferencesConfigurationManager.java +++ b/src/platform/android/java/chip/platform/PreferencesConfigurationManager.java @@ -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. + * + *

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. + * + *

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). + * + *

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. + * + *

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; @@ -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. + * + *

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. + * + *

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. + * + *

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;