Matter SDK Coverage Report
Current view: top level - include/platform/internal - GenericDeviceInstanceInfoProvider.ipp (source / functions) Coverage Total Hit
Test: SHA:4d2388ac7eed75b2fe5e05e20de377999c632502 Lines: 56.9 % 65 37
Test Date: 2025-07-27 07:17:09 Functions: 38.5 % 13 5

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2022 Project CHIP Authors
       4              :  *
       5              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       6              :  *    you may not use this file except in compliance with the License.
       7              :  *    You may obtain a copy of the License at
       8              :  *
       9              :  *        http://www.apache.org/licenses/LICENSE-2.0
      10              :  *
      11              :  *    Unless required by applicable law or agreed to in writing, software
      12              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      13              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14              :  *    See the License for the specific language governing permissions and
      15              :  *    limitations under the License.
      16              :  */
      17              : 
      18              : #include <platform/internal/GenericDeviceInstanceInfoProvider.h>
      19              : 
      20              : namespace chip {
      21              : namespace DeviceLayer {
      22              : namespace Internal {
      23              : 
      24              : template <class ConfigClass>
      25            0 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetVendorId(uint16_t & vendorId)
      26              : {
      27            0 :     vendorId = static_cast<uint16_t>(CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID);
      28            0 :     return CHIP_NO_ERROR;
      29              : }
      30              : 
      31              : template <class ConfigClass>
      32            0 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetProductId(uint16_t & productId)
      33              : {
      34            0 :     productId = static_cast<uint16_t>(CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID);
      35            0 :     return CHIP_NO_ERROR;
      36              : }
      37              : 
      38              : template <class ConfigClass>
      39            1 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetVendorName(char * buf, size_t bufSize)
      40              : {
      41            1 :     VerifyOrReturnError(bufSize >= sizeof(CHIP_DEVICE_CONFIG_DEVICE_VENDOR_NAME), CHIP_ERROR_BUFFER_TOO_SMALL);
      42            1 :     strcpy(buf, CHIP_DEVICE_CONFIG_DEVICE_VENDOR_NAME);
      43            1 :     return CHIP_NO_ERROR;
      44              : }
      45              : 
      46              : template <class ConfigClass>
      47            1 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetProductName(char * buf, size_t bufSize)
      48              : {
      49            1 :     VerifyOrReturnError(bufSize >= sizeof(CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_NAME), CHIP_ERROR_BUFFER_TOO_SMALL);
      50            1 :     strcpy(buf, CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_NAME);
      51              : 
      52            1 :     return CHIP_NO_ERROR;
      53              : }
      54              : 
      55              : template <class ConfigClass>
      56            0 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetPartNumber(char * buf, size_t bufSize)
      57              : {
      58            0 :     return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
      59              : }
      60              : 
      61              : template <class ConfigClass>
      62            0 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetProductURL(char * buf, size_t bufSize)
      63              : {
      64              : #if CHIP_DEVICE_LAYER_TARGET_ESP32
      65              :     CHIP_ERROR err = mGenericConfigManager.ReadConfigValueStr(ConfigClass::kConfigKey_ProductURL, buf, bufSize, bufSize);
      66              :     if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
      67              :     {
      68              :         return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
      69              :     }
      70              :     return err;
      71              : #else
      72            0 :     return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
      73              : #endif
      74              : }
      75              : 
      76              : template <class ConfigClass>
      77            0 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetProductLabel(char * buf, size_t bufSize)
      78              : {
      79              : #if CHIP_DEVICE_LAYER_TARGET_ESP32
      80              :     CHIP_ERROR err = mGenericConfigManager.ReadConfigValueStr(ConfigClass::kConfigKey_ProductLabel, buf, bufSize, bufSize);
      81              :     if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
      82              :     {
      83              :         return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
      84              :     }
      85              :     return err;
      86              : #else
      87            0 :     return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
      88              : #endif
      89              : }
      90              : 
      91              : template <class ConfigClass>
      92            2 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetSerialNumber(char * buf, size_t bufSize)
      93              : {
      94            2 :     ChipError err       = CHIP_NO_ERROR;
      95            2 :     size_t serialNumLen = 0; // without counting null-terminator
      96              : 
      97            2 :     err = mGenericConfigManager.ReadConfigValueStr(ConfigClass::kConfigKey_SerialNum, buf, bufSize, serialNumLen);
      98              : 
      99              : #ifdef CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER
     100            2 :     if (CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER[0] != 0 && err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
     101              :     {
     102            0 :         VerifyOrReturnError(sizeof(CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER) <= bufSize, CHIP_ERROR_BUFFER_TOO_SMALL);
     103            0 :         memcpy(buf, CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER, sizeof(CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER));
     104            0 :         serialNumLen = sizeof(CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER) - 1;
     105            0 :         err          = CHIP_NO_ERROR;
     106              :     }
     107              : #endif // CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER
     108            2 :     ReturnErrorOnFailure(err);
     109              : 
     110            2 :     VerifyOrReturnError(serialNumLen < bufSize, CHIP_ERROR_BUFFER_TOO_SMALL);
     111            2 :     VerifyOrReturnError(buf[serialNumLen] == 0, CHIP_ERROR_INVALID_STRING_LENGTH);
     112              : 
     113            2 :     return err;
     114              : }
     115              : 
     116              : template <class ConfigClass>
     117            1 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetManufacturingDate(uint16_t & year, uint8_t & month, uint8_t & day)
     118              : {
     119              :     CHIP_ERROR err;
     120              :     enum
     121              :     {
     122              :         kDateStringLength = 10 // YYYY-MM-DD
     123              :     };
     124              :     char dateStr[kDateStringLength + 1];
     125              :     size_t dateLen;
     126              :     char * parseEnd;
     127              : 
     128            1 :     err = mGenericConfigManager.ReadConfigValueStr(ConfigClass::kConfigKey_ManufacturingDate, dateStr, sizeof(dateStr), dateLen);
     129            1 :     SuccessOrExit(err);
     130              : 
     131            1 :     VerifyOrExit(dateLen == kDateStringLength, err = CHIP_ERROR_INVALID_ARGUMENT);
     132              : 
     133              :     // Cast does not lose information, because we then check that we only parsed
     134              :     // 4 digits, so our number can't be bigger than 9999.
     135            1 :     year = static_cast<uint16_t>(strtoul(dateStr, &parseEnd, 10));
     136            1 :     VerifyOrExit(parseEnd == dateStr + 4, err = CHIP_ERROR_INVALID_ARGUMENT);
     137              : 
     138              :     // Cast does not lose information, because we then check that we only parsed
     139              :     // 2 digits, so our number can't be bigger than 99.
     140            1 :     month = static_cast<uint8_t>(strtoul(dateStr + 5, &parseEnd, 10));
     141            1 :     VerifyOrExit(parseEnd == dateStr + 7, err = CHIP_ERROR_INVALID_ARGUMENT);
     142              : 
     143              :     // Cast does not lose information, because we then check that we only parsed
     144              :     // 2 digits, so our number can't be bigger than 99.
     145            1 :     day = static_cast<uint8_t>(strtoul(dateStr + 8, &parseEnd, 10));
     146            1 :     VerifyOrExit(parseEnd == dateStr + 10, err = CHIP_ERROR_INVALID_ARGUMENT);
     147              : 
     148            1 : exit:
     149            1 :     if (err != CHIP_NO_ERROR && err != CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
     150              :     {
     151            0 :         ChipLogError(DeviceLayer, "Invalid manufacturing date: %s", dateStr);
     152              :     }
     153            1 :     return err;
     154              : }
     155              : 
     156              : template <class ConfigClass>
     157            1 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetHardwareVersion(uint16_t & hardwareVersion)
     158              : {
     159            1 :     ChipError err   = CHIP_NO_ERROR;
     160            1 :     uint32_t valInt = 0;
     161              : 
     162            1 :     err = mGenericConfigManager.ReadConfigValue(ConfigClass::kConfigKey_HardwareVersion, valInt);
     163            1 :     if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
     164              :     {
     165            0 :         hardwareVersion = static_cast<uint16_t>(CHIP_DEVICE_CONFIG_DEFAULT_DEVICE_HARDWARE_VERSION);
     166            0 :         err             = CHIP_NO_ERROR;
     167              :     }
     168              :     else
     169              :     {
     170            1 :         hardwareVersion = static_cast<uint16_t>(valInt);
     171              :     }
     172              : 
     173            1 :     return err;
     174              : }
     175              : 
     176              : template <class ConfigClass>
     177            0 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetHardwareVersionString(char * buf, size_t bufSize)
     178              : {
     179            0 :     VerifyOrReturnError(bufSize >= sizeof(CHIP_DEVICE_CONFIG_DEFAULT_DEVICE_HARDWARE_VERSION_STRING), CHIP_ERROR_BUFFER_TOO_SMALL);
     180            0 :     strcpy(buf, CHIP_DEVICE_CONFIG_DEFAULT_DEVICE_HARDWARE_VERSION_STRING);
     181            0 :     return CHIP_NO_ERROR;
     182              : }
     183              : 
     184              : template <class ConfigClass>
     185            0 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetSoftwareVersionString(char * buf, size_t bufSize)
     186              : {
     187            0 :     return mGenericConfigManager.GetSoftwareVersionString(buf, bufSize);
     188              : }
     189              : 
     190              : template <class ConfigClass>
     191            0 : CHIP_ERROR GenericDeviceInstanceInfoProvider<ConfigClass>::GetRotatingDeviceIdUniqueId(MutableByteSpan & uniqueIdSpan)
     192              : {
     193            0 :     ChipError err = CHIP_ERROR_WRONG_KEY_TYPE;
     194              : #if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
     195              :     if (chip::DeviceLayer::ConfigurationMgr().GetRotatingDeviceIdUniqueId(uniqueIdSpan) != CHIP_NO_ERROR)
     196              :     {
     197              :         static_assert(ConfigurationManager::kRotatingDeviceIDUniqueIDLength >=
     198              :                           ConfigurationManager::kMinRotatingDeviceIDUniqueIDLength,
     199              :                       "Length of unique ID for rotating device ID is smaller than minimum.");
     200              : 
     201              :         constexpr uint8_t uniqueId[] = CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID;
     202              : 
     203              :         VerifyOrReturnError(sizeof(uniqueId) <= uniqueIdSpan.size(), CHIP_ERROR_BUFFER_TOO_SMALL);
     204              :         VerifyOrReturnError(sizeof(uniqueId) == ConfigurationManager::kRotatingDeviceIDUniqueIDLength, CHIP_ERROR_BUFFER_TOO_SMALL);
     205              :         memcpy(uniqueIdSpan.data(), uniqueId, sizeof(uniqueId));
     206              :         uniqueIdSpan.reduce_size(sizeof(uniqueId));
     207              :     }
     208              :     return CHIP_NO_ERROR;
     209              : #endif
     210            0 :     return err;
     211              : }
     212              : 
     213              : } // namespace Internal
     214              : } // namespace DeviceLayer
     215              : } // namespace chip
        

Generated by: LCOV version 2.0-1