This file provides guidelines and instructions for AI agents working on the Matter SDK codebase.
- When in Rome: Match the prevailing style of the code being modified. See docs/style/CODING_STYLE_GUIDE.md.
- Atomicity: Make small, incremental changes. Do not mix refactoring with feature implementation.
- No Filler Names: Avoid names like "support", "common", "helpers", "util", "core". Use concrete names.
- Error Handling: Use
CHIP_ERRORas the standard return type for fallible operations. PreferVerifyOrReturnErrorandReturnErrorOnFailuremacros for concise error checking and propagation. - Ensure resources are cleaned up appropriately, especially on early returns. Generally prefer RAII patterns for cleanup.
- Logging: Use the
ChipLog*macros (e.g.,ChipLogProgress,ChipLogError,ChipLogDetail) for logging. Ensure logs are appropriately categorized by module (e.g.,AppServer,InteractionModel).
When searching for files or code patterns, ignore the following directories unless explicitly asked to look there:
third_party/(contains external dependencies)out/(contains build artifacts)
- Do not comment on content for XML files or .matter content for clusters.
- The SDK implements an in-progress Matter specification that may be in flux and may not be available to all contributors. Assume the Matter specification is unknown and out of scope unless you have explicit access to the latest version (e.g., via a specialized tool or skill).
- Avoid "pat on the back" style comments that just restate what the code is doing. Focus on suggesting concrete code improvements.
- Be concise. Do not over-explain code.
- Look for common typos and suggest fixes.
- Do not comment on whitespace or formatting (auto-formatters handle this).
- Review changes for embedded development:
- Minimize use of heap allocation.
- Optimize for resource usage (RAM/Flash).
- Be cautious with complex templates that could lead to code bloat.
- Prefer using
chip::Spanfromsrc/lib/support/Span.hto pointer + size groups. PassSpanby value rather than const reference (treat it as astring_view) - Use
"foo"_span(i.e.operator _span) for const char spans instead offromCharString. - Prefer
std::optionaltochip::Optional - Prefer
StringBuilderfromsrc/lib/support/StringBuilder.hto usingsnprintffor string formatting.
Refer to docs/style/CODING_STYLE_GUIDE.md for full details.
- C++: C++17 standard.
- Use fixed-width integer types from
<cstdint>for POD integer types - Avoid top-level
using namespacein headers. - Use anonymous namespaces for file-internal classes/objects.
- Avoid heap allocation and auto-resizing containers in core SDK.
- Use fixed-width integer types from
- Python: Python 3.11 standard.
- Use type hints on public APIs.
- Include docstrings for public APIs.
- Always include
{}bracketing for control flows, even if using one liners (e.g. forif,while,forand such)
- Unit tests are required for all changes unless unit testing is impossible (e.g., platform-specific code).
- Tests in
src/python_testingandsrc/app/tests/suiteswhich verify expected failures should clearly indicate why the failure is expected. Include a summary of the relevant specification requirements if possible.
Code-driven clusters are implementations in src/app/clusters that use
DefaultServerCluster as a base class. When developing them:
ReadAttribute,WriteAttribute, andInvokeCommandare by API contract only called for existent paths. Do not add path validity checks — they increase code size and are redundant as long asAttributesorAcceptedCommandsare correct.- Ember APIs and generated ZAP accessors must not be used outside the
CodegenIntegrationlayer.CodegenIntegration.h/cppis the documented bridge between generated configuration and code-driven cluster logic. Avoid types likeEmberAfStatusor functions likeemberAfContainsServer,emberAfReadAttribute, oremberAfWriteAttributein core cluster code. - When adding files: codegen-specific files belong in
app_config_dependent_sources.cmake/gni; all others belong inBUILD.gn. Ensure every file (especially headers) is listed in one of these — there should be no unreferenced files.
Most commands require an activated environment.
You can run commands within the environment using scripts/run_in_build_env.sh:
scripts/run_in_build_env.sh "command"
Alternatively, you can activate the environment in your shell:
source scripts/activate.sh
-
List available targets:
scripts/run_in_build_env.sh "./scripts/build/build_examples.py targets" -
Generate Ninja files:
scripts/run_in_build_env.sh "./scripts/build/build_examples.py --target linux-x64-tests-clang --quiet gen" -
Build and run all tests:
scripts/run_in_build_env.sh "./scripts/build/build_examples.py --target linux-x64-tests-clang --quiet build" -
Run a specific test:
scripts/run_in_build_env.sh "ninja -C out/linux-x64-tests-clang --quiet path/to/test:test_name.run"-
Explicit example:
scripts/run_in_build_env.sh "ninja -C out/linux-x64-tests-clang src/app/clusters/occupancy-sensor-server/tests:TestOccupancySensingCluster.run" -
Compile and run can be separated (e.g. if running under some memory debugger or needing to set other options):
```bash scripts/run_in_build_env.sh "ninja -C out/linux-x64-tests-clang src/app/clusters/occupancy-sensor-server/tests:TestOccupancySensingCluster"` ./out/linux-x64-tests-clang/tests/TestOccupancySensingCluster ```
-
- chip-tool (Interactive commissioning tool):
scripts/run_in_build_env.sh "./scripts/build/build_examples.py --target linux-x64-chip-tool-clang --quiet build" - all-clusters-app (Feature-rich device simulator):
scripts/run_in_build_env.sh "./scripts/build/build_examples.py --target linux-x64-all-clusters-clang --quiet build" - all-devices-app (Alternative feature-rich simulator):
scripts/run_in_build_env.sh "./scripts/build/build_examples.py --target linux-x64-all-devices-clang --quiet build"