---
title: Developer Guide & Best Practices
description: Developer Guide & Best Practices
---

[Skip to content](https://portal.sandgrain.eu/knowledge-base/developer-guide-best-practices#main-content)

English

Show submenu for translations

![SandGrain\_Logo\_RGB\_white-bijgesneden (1)](https://portal.sandgrain.eu/hs-fs/hubfs/SandGrain_Logo_RGB_white-bijgesneden%20(1).png?width=427&height=83&name=SandGrain_Logo_RGB_white-bijgesneden%20(1).png)

Open main navigation

Close main navigation

- English
  
  Show submenu for translations
- [Contact us](https://portal.sandgrain.eu/login)

[Contact us](https://portal.sandgrain.eu/login?hsLang=en)

 How can we help you?

- There are no suggestions because the search field is empty.

1. [Knowledge Base](https://portal.sandgrain.eu/knowledge-base?hsLang=en)
2. [User Documentation](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en)
3. [SandGrain SDK](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en#sandgrain-sdk)

October 6, 2026

# Developer Guide & Best Practices

**Who this is for:** Developers building an application on top of the SandGrain SDK.

**What you'll learn:** How to structure your application around the SDK, the rules to follow for correctness, and where to tune for performance.

### Application architecture recommendations

- Keep the Application Layer's iteration loop thin. For concurrency, wrap or replace the loop (a task scheduler or event loop) without touching the lower layers.
- On the Gateway, prefer `SGR_SDK_GW_Run` as the single dispatch loop rather than calling the individual handlers yourself.

### Best practices (correctness rules)

These aren't style preferences, getting them wrong produces failures that look like hardware faults:

- **Always call `SGR_SDK_Initialize` first.** Nothing else works before initialization.
- **Always check the `sgr_sdk_status_t` return value.** Every SDK function returns one.
- **Complete `SGR_SDK_Verification_FW` successfully before `SGR_SDK_Installation`, in the same session.** The RW from verification must not be discarded between the two calls.
- **Whitelist every device TID before authentication.** An unregistered device fails with `SGR_SDK_NOT_REGISTERED`.

### Common workflows

- **Device:** initialize, authenticate via the gateway, then optionally verify firmware and request installation.
- **Gateway:** initialize (which loads the whitelist and auth DB), then run the dispatcher loop.

### Performance recommendations

- **Disable debug flags in production.** Undefined flags compile out the log calls entirely, with zero runtime overhead.
- **Tune `MAX_RETRIES` / `GW_MAX_RETRIES` and the bit-flip thresholds** to balance strictness against channel noise. See *Compile-Time Configuration*.

### A logging gotcha worth knowing

The Gateway reports "Flow completed" when the framing and transport succeed, **even if the firmware itself was rejected** (FW\_NOK). Don't treat "Flow completed" as success, inspect the actual result/reason field.

---

*Related articles: [The Authentication Flow & Error Management](https://portal.sandgrain.eu/knowledge-base/the-authentication-flow-error-management?hsLang=en) · [SDK User API & Code Examples](https://portal.sandgrain.eu/knowledge-base/sdk-user-api-code-examples?hsLang=en) · [SDK API Reference](https://portal.sandgrain.eu/knowledge-base/sdk-api-reference?hsLang=en) · [Compile-Time Configuration](https://portal.sandgrain.eu/knowledge-base/compile-time-configuration-sdk-sgr-config.h?hsLang=en)*

- [User Documentation](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en#main-content)

    - [Quick Starter Guide](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en#quick-starter-guide)
    - [User Manual](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en#user-manual)
    - [Whitepaper](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en#whitepaper)
    - [Token Leaflet](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en#token-leaflet)
    - [Claim Token Procedure](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en#claim-token-procedure)
    - [Daisy Chain](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en#daisy-chain)
    - [Release Note](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en#release-note)
    - [Naming Convention](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en#naming-convention)
    - [SandGrain SDK](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en#sandgrain-sdk)
    - [TrustBridge](https://portal.sandgrain.eu/knowledge-base/user-documentation?hsLang=en#trustbridge)
- [API Documentation](https://portal.sandgrain.eu/knowledge-base/api-documentation?hsLang=en#main-content)

    - [Device API Integration](https://portal.sandgrain.eu/knowledge-base/api-documentation?hsLang=en#device-api-integration)
    - [Daisy Chain and Attestation](https://portal.sandgrain.eu/knowledge-base/api-documentation?hsLang=en#daisy-chain-and-attestation)
- [SDK](https://portal.sandgrain.eu/knowledge-base/sdk?hsLang=en#main-content)

    - [Core Functions](https://portal.sandgrain.eu/knowledge-base/sdk?hsLang=en#core-functions)
    - [SandGrain SDK](https://portal.sandgrain.eu/knowledge-base/sdk?hsLang=en#sandgrain-sdk)

[![SandGrain\_Logo\_RGB\_white-bijgesneden (1)-1](https://portal.sandgrain.eu/hs-fs/hubfs/SandGrain_Logo_RGB_white-bijgesneden%20(1)-1.png?width=427&height=83&name=SandGrain_Logo_RGB_white-bijgesneden%20(1)-1.png "SandGrain_Logo_RGB_white-bijgesneden (1)-1")](https://portal.sandgrain.eu/?hsLang=en)

<https://www.facebook.com/> <https://www.twitter.com/> <https://www.instagram.com/> <https://podcasts.apple.com/> [mailto:email@email.com](mailto:email@email.com)

Copyright © 2026, sandgrain.eu