Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for OEM SKU and Serial Numbers #3030

Merged
merged 9 commits into from
Nov 8, 2024

Conversation

josesimoes
Copy link
Member

@josesimoes josesimoes commented Oct 22, 2024

Description

  • Add declarations and weak implementation for several platforms.

Motivation and Context

  • Provides a default implementation for device's serial number computed from unique ID contained in a device.
  • OEM SKU is provided globally as there is no need to do it per platform.
  • The implementation is provided as weak to allow manufacturers to include a strong implementation with their own providers.

How Has This Been Tested?

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dev Containers (changes related with Dev Containers, has no impact on code or features)
  • Dependencies/declarations (update dependencies or assembly declarations and changes associated, has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).

Summary by CodeRabbit

  • New Features

    • Introduced new functions to retrieve OEM model SKU and serial numbers, enhancing device identification capabilities.
  • Bug Fixes

    • Minor adjustments to redundant code in network configuration initialization functions for improved clarity.
  • Documentation

    • Updated comments for clarity in configuration management functions, maintaining existing functionality.

- Add declarations and weak implementation for several platforms.
Copy link

coderabbitai bot commented Oct 22, 2024

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes introduce two new functions, ConfigurationManager_GetOemModelSku and ConfigurationManager_GetSerialNumbers, to the configuration management system. These functions are designed to retrieve the OEM model SKU and serial numbers, respectively, and are marked as weak to allow for target-specific implementations. The updates involve modifications across several files, including header and source files, with existing functionalities remaining intact and no alterations to existing functions or data structures.

Changes

File Path Change Summary
src/HAL/Include/nanoHAL_ConfigurationManager.h Added methods: void ConfigurationManager_GetOemModelSku(char *model, size_t modelSkuSize) and void ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize).
src/HAL/nanoHAL_ConfigurationManager.c Added method: void ConfigurationManager_GetOemModelSku(char *model, size_t modelSkuSize). Implemented to initialize model buffer to zero.
src/HAL/nanoHAL_SystemInformation.cpp Modified GetHalSystemInfo to call new functions for populating m_OemModelInfo and m_OemSerialNumbers. Removed previous memcpy logic.
targets/AzureRTOS/Maxim/_common/targetHAL_ConfigurationManager.cpp Added method: void ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize).
targets/AzureRTOS/ST/_common/targetHAL_ConfigurationManager.cpp Added method: __nfweak void ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize).
targets/AzureRTOS/SiliconLabs/_common/targetHAL_ConfigurationManager.cpp Added method: __nfweak void ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize).
targets/ChibiOS/_common/targetHAL_ConfigurationManager.cpp Added method: __nfweak void ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize).
targets/ESP32/_common/targetHAL_ConfigurationManager.cpp Added method: __nfweak void ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize).
targets/FreeRTOS/NXP/_common/targetHAL_ConfigurationManager.cpp Added method: __nfweak void ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize).
targets/TI_SimpleLink/_common/targetHAL_ConfigurationManager_CC13xx_26xx.cpp Added method: __nfweak void ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize). Minor adjustment in InitialiseNetworkDefaultConfig.
targets/TI_SimpleLink/_common/targetHAL_ConfigurationManager_CC32xx.cpp Added method: __nfweak void ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize). Minor adjustment in InitialiseNetworkDefaultConfig.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant System
    participant ConfigurationManager

    User->>System: Request OEM Model SKU
    System->>ConfigurationManager: ConfigurationManager_GetOemModelSku(model, modelSkuSize)
    ConfigurationManager-->>System: Return OEM Model SKU

    User->>System: Request Serial Numbers
    System->>ConfigurationManager: ConfigurationManager_GetSerialNumbers(serialNumbers, serialNumbersSize)
    ConfigurationManager-->>System: Return Serial Numbers
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

🧹 Outside diff range and nitpick comments (5)
src/HAL/nanoHAL_SystemInformation.cpp (3)

36-36: LGTM. Consider adding error handling.

The introduction of ConfigurationManager_GetOemModelSku aligns with the PR objectives and provides a more modular approach for retrieving the OEM model SKU.

Consider adding error handling to account for potential failures in retrieving the OEM model SKU:

-    ConfigurationManager_GetOemModelSku((char *)&systemInfo.m_OemModelInfo, sizeof(systemInfo.m_OemModelInfo));
+    if (ConfigurationManager_GetOemModelSku((char *)&systemInfo.m_OemModelInfo, sizeof(systemInfo.m_OemModelInfo)) != TRUE)
+    {
+        // Handle error or log warning
+    }

39-41: LGTM. Consider adding error handling.

The introduction of ConfigurationManager_GetSerialNumbers aligns with the PR objectives and provides a more modular approach for retrieving serial numbers. The multi-line function call improves readability.

Consider adding error handling to account for potential failures in retrieving the serial numbers:

-    ConfigurationManager_GetSerialNumbers(
-        (char *)&systemInfo.m_OemSerialNumbers,
-        sizeof(systemInfo.m_OemSerialNumbers));
+    if (ConfigurationManager_GetSerialNumbers(
+        (char *)&systemInfo.m_OemSerialNumbers,
+        sizeof(systemInfo.m_OemSerialNumbers)) != TRUE)
+    {
+        // Handle error or log warning
+    }

36-41: Overall changes look good. Consider adding documentation.

The introduction of ConfigurationManager_GetOemModelSku and ConfigurationManager_GetSerialNumbers provides a more flexible approach for OEM customization, aligning well with the PR objectives. The weak implementations allow for platform-specific overrides, which is beneficial for device manufacturers.

Consider adding a brief comment above these new function calls to explain the rationale behind using these functions and their weak implementation nature. This would help future maintainers understand the design decision:

+    // Use weak implementations for OEM model SKU and serial numbers
+    // to allow platform-specific overrides if needed
     ConfigurationManager_GetOemModelSku((char *)&systemInfo.m_OemModelInfo, sizeof(systemInfo.m_OemModelInfo));

     // OEM_SERIAL_NUMBERS:
     ConfigurationManager_GetSerialNumbers(
         (char *)&systemInfo.m_OemSerialNumbers,
         sizeof(systemInfo.m_OemSerialNumbers));
src/HAL/Include/nanoHAL_ConfigurationManager.h (2)

221-223: LGTM! Consider adding error handling.

The function declaration for ConfigurationManager_GetOemModelSku is well-defined and aligns with the PR objectives. The weak attribute allows for platform-specific implementations, which is a good design choice.

Consider modifying the function to return a boolean indicating success or failure, which would improve error handling. For example:

bool ConfigurationManager_GetOemModelSku(char *model, size_t modelSkuSize);

221-227: Summary: New functions align well with PR objectives.

The addition of ConfigurationManager_GetOemModelSku and ConfigurationManager_GetSerialNumbers functions successfully implements support for OEM SKU and serial numbers as outlined in the PR objectives. The weak attribute allows for platform-specific implementations, providing the flexibility mentioned in the PR description.

To further improve the implementation:

  1. Consider adding error handling to both functions.
  2. Clarify the use of plural "SerialNumbers" in the second function.
  3. Ensure that platform-specific implementations are provided where necessary.
  4. Update relevant documentation to reflect these new capabilities.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 95a9516 and a927223.

📒 Files selected for processing (11)
  • src/HAL/Include/nanoHAL_ConfigurationManager.h (2 hunks)
  • src/HAL/nanoHAL_ConfigurationManager.c (1 hunks)
  • src/HAL/nanoHAL_SystemInformation.cpp (1 hunks)
  • targets/AzureRTOS/Maxim/_common/targetHAL_ConfigurationManager.cpp (1 hunks)
  • targets/AzureRTOS/ST/_common/targetHAL_ConfigurationManager.cpp (1 hunks)
  • targets/AzureRTOS/SiliconLabs/_common/targetHAL_ConfigurationManager.cpp (1 hunks)
  • targets/ChibiOS/_common/targetHAL_ConfigurationManager.cpp (1 hunks)
  • targets/ESP32/_common/targetHAL_ConfigurationManager.cpp (1 hunks)
  • targets/FreeRTOS/NXP/_common/targetHAL_ConfigurationManager.cpp (1 hunks)
  • targets/TI_SimpleLink/_common/targetHAL_ConfigurationManager_CC13xx_26xx.cpp (1 hunks)
  • targets/TI_SimpleLink/_common/targetHAL_ConfigurationManager_CC32xx.cpp (1 hunks)
🧰 Additional context used
🔇 Additional comments (6)
src/HAL/Include/nanoHAL_ConfigurationManager.h (1)

225-227: LGTM! Consider error handling and clarify "SerialNumbers".

The function declaration for ConfigurationManager_GetSerialNumbers is well-defined and aligns with the PR objectives. The weak attribute allows for platform-specific implementations, which is a good design choice.

  1. Consider modifying the function to return a boolean indicating success or failure, which would improve error handling. For example:
bool ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize);
  1. The plural form "SerialNumbers" suggests multiple serial numbers. Could you clarify if this is intentional? If it's meant to retrieve a single serial number, consider renaming it to ConfigurationManager_GetSerialNumber for consistency.
src/HAL/nanoHAL_ConfigurationManager.c (2)

Line range hint 1-348: Overall assessment: LGTM with minor suggestions

The addition of ConfigurationManager_GetOemModelSku is well-integrated into the existing codebase. It provides a default implementation while allowing for target-specific overrides, which aligns with the PR objectives. The existing functions remain unchanged, maintaining the overall structure and functionality of the configuration management system.

To further improve the implementation:

  1. Consider adding more comprehensive error handling and input validation across all functions in this file.
  2. Ensure consistent use of the __nfweak attribute for other functions that might benefit from target-specific implementations.
  3. Update the file header comment to include information about the new OEM model SKU functionality.

343-348: Approve the new function with suggestions for improvement.

The addition of ConfigurationManager_GetOemModelSku with the __nfweak attribute aligns well with the PR objectives, allowing for target-specific implementations. However, consider the following improvements:

  1. Add input validation for modelSkuSize to prevent potential buffer overflows.
  2. Include a comment explaining the expected behavior for target-specific implementations.

Consider updating the function as follows:

__nfweak void ConfigurationManager_GetOemModelSku(char *model, size_t modelSkuSize)
{
    // Default implementation
    // Target-specific implementations should populate 'model' with the actual OEM model SKU
    if (model != NULL && modelSkuSize > 0)
    {
        memset(model, 0, modelSkuSize);
    }
}

Let's verify the function declaration in the header file:

✅ Verification successful

Function ConfigurationManager_GetOemModelSku Declared Correctly in Header

The function ConfigurationManager_GetOemModelSku is properly declared in src/HAL/Include/nanoHAL_ConfigurationManager.h, ensuring correct linkage and usage across the codebase.

Suggestions for Improvement:

  1. Add input validation for modelSkuSize to prevent potential buffer overflows.
  2. Include a comment explaining the expected behavior for target-specific implementations.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if the function is correctly declared in the header file
rg -n "void\s+ConfigurationManager_GetOemModelSku\s*\(\s*char\s*\*\s*model\s*,\s*size_t\s+modelSkuSize\s*\)" src/HAL/Include/nanoHAL_ConfigurationManager.h

Length of output: 241

targets/AzureRTOS/Maxim/_common/targetHAL_ConfigurationManager.cpp (1)

530-536: Summary: New function aligns with PR objectives but needs implementation

The addition of ConfigurationManager_GetSerialNumbers aligns with the PR objective of adding support for OEM SKU and serial numbers. However, the current implementation is a placeholder and needs to be completed to actually retrieve and return the unique device ID.

To ensure this change doesn't conflict with other parts of the codebase, please run the following script:

This will help verify that the weak implementation is appropriate and identify any existing usages that might need to be updated.

✅ Verification successful

Verified: Weak implementation aligns with existing patterns

The added ConfigurationManager_GetSerialNumbers function follows the established pattern of weak implementations, allowing manufacturers to provide their own strong implementations. No conflicting or existing strong implementations were found, ensuring consistency and preventing conflicts.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other implementations or usages of ConfigurationManager_GetSerialNumbers

# Search for other implementations
echo "Searching for other implementations of ConfigurationManager_GetSerialNumbers:"
rg -p "ConfigurationManager_GetSerialNumbers\s*\([^)]*\)\s*{" --type cpp

# Search for usages of the function
echo "Searching for usages of ConfigurationManager_GetSerialNumbers:"
rg -p "ConfigurationManager_GetSerialNumbers\s*\(" --type cpp

Length of output: 2213


Script:

#!/bin/bash
# Description: Check for other implementations or usages of ConfigurationManager_GetSerialNumbers

# Search for other implementations
echo "Searching for other implementations of ConfigurationManager_GetSerialNumbers:"
rg -e "ConfigurationManager_GetSerialNumbers\s*\([^)]*\)\s*{" --type cpp

# Search for usages of the function
echo "Searching for usages of ConfigurationManager_GetSerialNumbers:"
rg -e "ConfigurationManager_GetSerialNumbers\s*\(" --type cpp

Length of output: 2165


Script:

#!/bin/bash
# Description: Check for other implementations or usages of ConfigurationManager_GetSerialNumbers

# Search for other implementations (strong implementations)
echo "Searching for strong implementations of ConfigurationManager_GetSerialNumbers:"
rg -e "ConfigurationManager_GetSerialNumbers\s*\([^)]*\)\s*\{" --type cpp

# Search for usages of the function
echo "Searching for usages of ConfigurationManager_GetSerialNumbers:"
rg -e "ConfigurationManager_GetSerialNumbers\s*\(" --type cpp

Length of output: 1998

targets/ChibiOS/_common/targetHAL_ConfigurationManager.cpp (1)

645-651: ⚠️ Potential issue

Implement the actual serial number retrieval logic

The current implementation of ConfigurationManager_GetSerialNumbers doesn't actually retrieve any serial numbers. Instead, it just zeroes out the provided buffer. This doesn't align with the function's intended purpose.

Consider the following improvements:

  1. Implement the actual logic to retrieve the device's unique ID or serial number.
  2. Add error handling to deal with potential failures in retrieving the serial number.
  3. Consider returning a boolean to indicate success or failure of the operation.

Here's a suggested improvement:

- __nfweak void ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize)
+ __nfweak bool ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize)
 {
-    // do the thing to get unique device ID
-    memset(serialNumbers, 0, serialNumbersSize);
+    // Implement the actual logic to get the unique device ID
+    // This is a placeholder and should be replaced with actual implementation
+    const char* deviceId = "EXAMPLE_DEVICE_ID";
+    size_t idLength = strlen(deviceId);
+
+    if (serialNumbersSize < idLength + 1) {
+        return false;  // Buffer too small
+    }
+
+    strncpy(serialNumbers, deviceId, serialNumbersSize);
+    serialNumbers[serialNumbersSize - 1] = '\0';  // Ensure null-termination
+
+    return true;
 }

To ensure this function is properly implemented across different targets, run the following script:

This will help identify any target-specific implementations that might need to be updated as well.

targets/AzureRTOS/SiliconLabs/_common/targetHAL_ConfigurationManager.cpp (1)

776-783: ⚠️ Potential issue

Implement proper serial number generation.

The current implementation of ConfigurationManager_GetSerialNumbers doesn't actually generate or retrieve a unique device ID. It simply clears the provided buffer.

Consider implementing a proper method to generate or retrieve a unique device ID. For example:

__nfweak void ConfigurationManager_GetSerialNumbers(char *serialNumbers, size_t serialNumbersSize)
{
-    // do the thing to get unique device ID
-    memset(serialNumbers, 0, serialNumbersSize);
+    // Example implementation (replace with actual unique ID generation)
+    uint32_t uniqueId = GetDeviceUniqueId(); // Replace with actual function to get unique ID
+    snprintf(serialNumbers, serialNumbersSize, "%08X", uniqueId);
}

To ensure this function is properly implemented across different targets, we should check for other implementations:

This will help us verify if there are target-specific implementations that might override this weak implementation.

…_GetSystemSerialNumber and ConfigurationManager_GetModuleSerialNumber. Implemented and tested for ESP32_S3_ALL.
@frobijn
Copy link
Collaborator

frobijn commented Nov 3, 2024

I'll commit the changes per platform, to make reviewing the code changes easier.

The first is ESP32, as that's the platform I can test on real hardware. I've split the ConfigurationManager_GetSerialNumbers into ConfigurationManager_GetModuleSerialNumber and ConfigurationManager_GetSystemSerialNumber and implemented the latter. Tested on ESP32 S3 (ESP32_S3_ALL) and ESP32 C3 (XIAO_ESP32C3).

PS I could not build and test the ESP32_C6_THREAD target in de dev container (nanoFramework_All), there's an error triggered by IP network code.

…769I_DISCOVERY target now has SN and compiles correctly, no SN for ST_STM32F429I_DISCOVERY and ST_NUCLEO64_F091RC targets as nanoHAL_ConfigurationManager_stubs is used instead of nanoHAL_ConfigurationManager.
@frobijn
Copy link
Collaborator

frobijn commented Nov 3, 2024

STM32: The ST_STM32F429I_DISCOVERY and ST_NUCLEO64_F091RC targets do not include nanoHAL_ConfigurationManager.c but use nanoHAL_ConfigurationManager_stubs.c instead. I've added the three methods to the stub as well. The target does not include the ChibiOS-specific nanoHAL_ConfigurationManager.cpp and hence has no serial number. (Compilation of the ST_NUCLEO64_F091RC target fails - region `flash0' overflowed by 22208 bytes - but I can't image that is due to this change as 22K seems to be a lot of code).

The ST_STM32F769I_DISCOVERY target does include nanoHAL_ConfigurationManager.cpp. Copied the code for the unique device ID. Cannot test it as I don't have such a MCU.

@frobijn
Copy link
Collaborator

frobijn commented Nov 3, 2024

Silabs Giant Gecko: added the device unique ID code. Build of SL_STK3701A and SL_STK3701A_REVB succeeds. Don't have the device, cannot test the code any further.

Frank Robijn and others added 2 commits November 3, 2024 21:39
Automated fixes for code style.
@nfbot
Copy link
Member

nfbot commented Nov 4, 2024

@josesimoes there are issues with the code style on the source files.
A PR was submitted with the code style fixes. Please click #3032, review the changes if you want and merge it.

Make sure to follow the project code style. Check the details here on how it works and the tools required to help you with that.

Copy link
Member Author

@josesimoes josesimoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frobijn just pushed a couple of minor changes.
It looks good.
OK to merge?

@frobijn
Copy link
Collaborator

frobijn commented Nov 8, 2024

@josesimoes LGTM!

@josesimoes josesimoes marked this pull request as ready for review November 8, 2024 10:10
@josesimoes josesimoes merged commit c3300b1 into main Nov 8, 2024
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants