Skip to content

Commit

Permalink
Adding support for OEM SKU and Serial Numbers (#3030)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Nov 8, 2024
1 parent 8f13c66 commit c3300b1
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 8 deletions.
14 changes: 13 additions & 1 deletion src/HAL/Include/nanoHAL_ConfigurationManager.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
Expand Down Expand Up @@ -218,6 +218,18 @@ extern "C"
// gets the HAL_Configuration_NetworkInterface configuration block that has the SpecificConfig Id, if that exists
int32_t ConfigurationManager_FindNetworkConfigurationMatchingWirelessConfigurationFromId(uint32_t configurationId);

// Gets the OEM model SKU.
// This is defined as weak to allow the target/platform to provide the implementation.
void ConfigurationManager_GetOemModelSku(char *model, size_t modelSkuSize);

// Gets the module serial number.
// This is defined as weak to allow the target/platform to provide the implementation.
void ConfigurationManager_GetModuleSerialNumber(char *serialNumber, size_t serialNumberSize);

// Gets the system serial number.
// This is defined as weak to allow the target/platform to provide the implementation.
void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize);

#ifdef __cplusplus
}
#endif
Expand Down
14 changes: 14 additions & 0 deletions src/HAL/nanoHAL_ConfigurationManager.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,17 @@ __nfweak bool ConfigurationManager_CheckExistingConfigurationBlock(

return memcmp(cursor1, cursor2, existingConfigBlockSize) == 0;
}

__nfweak void ConfigurationManager_GetOemModelSku(char *model, size_t modelSkuSize)
{
// default implementation
// this is weak so the target can provide the implementation
memset(model, 0, modelSkuSize);
}

__nfweak void ConfigurationManager_GetModuleSerialNumber(char *serialNumber, size_t serialNumberSize)
{
// default implementation
// this is weak so a manufacturer can provide a strong implementation
memset(serialNumber, 0, serialNumberSize);
}
17 changes: 16 additions & 1 deletion src/HAL/nanoHAL_ConfigurationManager_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <nanoHAL_v2.h>

__nfweak void ConfigurationManager_Initialize(){};
__nfweak void ConfigurationManager_Initialize() {};

__nfweak void *ConfigurationManager_FindNetworkConfigurationBlocks(uint32_t startAddress, uint32_t endAddress)
{
Expand Down Expand Up @@ -70,3 +70,18 @@ __nfweak bool ConfigurationManager_CheckExistingConfigurationBlock(

return false;
}

__nfweak void ConfigurationManager_GetOemModelSku(char *model, size_t modelSkuSize)
{
memset(model, 0, modelSkuSize);
}

__nfweak void ConfigurationManager_GetModuleSerialNumber(char *serialNumber, size_t serialNumberSize)
{
memset(serialNumber, 0, serialNumberSize);
}

__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize)
{
memset(serialNumber, 0, serialNumberSize);
}
14 changes: 8 additions & 6 deletions src/HAL/nanoHAL_SystemInformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ bool GetHalSystemInfo(HalSystemInfo &systemInfo)
len = sizeof(systemInfo.m_releaseInfo.TargetName);
hal_strncpy_s((char *)&systemInfo.m_releaseInfo.TargetName[0], len, PLATFORMNAMESTRING, len - 1);

// we are not supporting this at this time
// OEM_MODEL_SKU:
// memcpy((void*)&(systemInfo.m_OemModelInfo), (void*)&(g_ConfigurationSector.OEM_Model_SKU),
// sizeof(OEM_MODEL_SKU));
ConfigurationManager_GetOemModelSku((char *)&systemInfo.m_OemModelInfo, sizeof(systemInfo.m_OemModelInfo));

// we are not supporting this at this time
// OEM_SERIAL_NUMBERS:
// memcpy((void*)&(systemInfo.m_OemSerialNumbers), (void*)&(g_ConfigurationSector.OemSerialNumbers),
// sizeof(OEM_SERIAL_NUMBERS));
ConfigurationManager_GetModuleSerialNumber(
(char *)&systemInfo.m_OemSerialNumbers.module_serial_number,
sizeof(systemInfo.m_OemSerialNumbers.module_serial_number));

ConfigurationManager_GetSystemSerialNumber(
(char *)&systemInfo.m_OemSerialNumbers.system_serial_number,
sizeof(systemInfo.m_OemSerialNumbers.system_serial_number));

return TRUE;
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,11 @@ __nfweak bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface
// can't create a "default" network config because we are lacking definition of a MAC address
return FALSE;
}

// default implementation
// this is weak so a manufacturer can provide a strong implementation
__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize)
{
// do the thing to get unique device ID
memset(serialNumber, 0, serialNumberSize);
}
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,11 @@ int32_t ConfigurationManager_FindNetworkConfigurationMatchingWirelessConfigurati
// not found
return -1;
}

// default implementation
// this is weak so a manufacturer can provide a strong implementation
__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize)
{
// do the thing to get unique device ID
memset(serialNumber, 0, serialNumberSize);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <nanoHAL_v2.h>
#include <nanoWeak.h>
// #include <network_options.h>
#include <em_device.h>

#if defined(WIFI_DRIVER_ISM43362) && defined(I_AM_NANOCLR)
#include <wifi.h>
Expand Down Expand Up @@ -773,3 +774,35 @@ int32_t ConfigurationManager_FindNetworkConfigurationMatchingWirelessConfigurati
// not found
return -1;
}

// default implementation
// this is weak so a manufacturer can provide a strong implementation
__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize)
{
memset(serialNumber, 0, serialNumberSize);

// Use the device Unique ID which is 64 bits long
// Put it in the LSB of the serial number
int startOfId = serialNumberSize - 8;

// high 32 bits
uint32_t rawId = DEVINFO->UNIQUEH;
for (int i = 3; i >= 0; --i)
{
serialNumber[startOfId + i] = rawId & 0xFF;
rawId >>= 8;
}

// low 32 bits
rawId = DEVINFO->UNIQUEL;
for (int i = 7; i >= 4; --i)
{
serialNumber[startOfId + i] = rawId & 0xFF;
rawId >>= 8;
}

// Disambiguation is needed because the hardware-specific identifier used to create the
// default serial number on other platforms may be in the same range.
// Set the first byte to a number that is unique (within the nanoFramework CLR) for the Giant Gecko.
serialNumber[0] = 3;
}
16 changes: 16 additions & 0 deletions targets/ChibiOS/_common/targetHAL_ConfigurationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,19 @@ __nfweak bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface
// can't create a "default" network config because we are lacking definition of a MAC address
return FALSE;
}

// default implementation
// this is weak so a manufacturer can provide a strong implementation
__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize)
{
// do the thing to get unique device ID
memset(serialNumber, 0, serialNumberSize);
// Use the 96 bit unique device ID => 12 bytes
// memory copy from the address pointed by UID_BASE define (from STM32 HAL)
memcpy(&serialNumber[serialNumberSize - 12], ((uint8_t *)UID_BASE), 12);

// Disambiguation is needed because the hardware-specific identifier used to create the
// default serial number on other platforms may be in the same range.
// Set the first byte to a number that is unique (within the nanoFramework CLR) for STM32.
serialNumber[0] = 2;
}
20 changes: 20 additions & 0 deletions targets/ESP32/_common/targetHAL_ConfigurationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,23 @@ HAL_Configuration_X509DeviceCertificate *ConfigurationManager_GetDeviceCertifica
// not found, or failed to allocate memory
return NULL;
}

// default implementation
// this is weak so a manufacturer can provide a strong implementation
__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize)
{
memset(serialNumber, 0, serialNumberSize);

// Use the factory-provided MAC address as unique ID
uint8_t macAddress[6];
esp_err_t err = esp_read_mac(macAddress, ESP_MAC_EFUSE_FACTORY);
if (err == ESP_OK)
{
memcpy(&serialNumber[serialNumberSize - sizeof(macAddress)], macAddress, sizeof(macAddress));

// Disambiguation is needed because the hardware-specific identifier used to create the
// default serial number on other platforms may be in the same range.
// Set the first byte to a number that is unique (within the nanoFramework CLR) for ESP32.
serialNumber[0] = 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,11 @@ bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface *pconfig,
// can't create a "default" network config because we are lacking definition of a MAC address
return FALSE;
}

// default implementation
// this is weak so a manufacturer can provide a strong implementation
__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize)
{
// do the thing to get unique device ID
memset(serialNumber, 0, serialNumberSize);
}
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,11 @@ bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface *pconfig,

return true;
}

// default implementation
// this is weak so a manufacturer can provide a strong implementation
__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize)
{
// do the thing to get unique device ID
memset(serialNumber, 0, serialNumberSize);
}
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,11 @@ bool InitialiseNetworkDefaultConfig(HAL_Configuration_NetworkInterface *pconfig,

return true;
}

// default implementation
// this is weak so a manufacturer can provide a strong implementation
__nfweak void ConfigurationManager_GetSystemSerialNumber(char *serialNumber, size_t serialNumberSize)
{
// do the thing to get unique device ID
memset(serialNumber, 0, serialNumberSize);
}

0 comments on commit c3300b1

Please sign in to comment.