Safe API Framework
Layered API framework for safety-related applications (ERTMS RBC reference targeting CENELEC EN 50128 SIL 4)
Loading...
Searching...
No Matches
Types Module - User Guide

What is Types?

  • Types module defines fixed-width integer types for safety-critical systems.
  • All framework code uses uint32_t, int16_t, etc. instead of bare int or
  • long. This ensures consistent behavior across platforms (8-bit MCU, 32-bit ARM,
  • 64-bit x86).
  • Key principle: Platform-independent sizing guarantees.

Available Types

  • * // Unsigned integers
    * uint8_t uint16_t uint32_t uint64_t
    *
    * // Signed integers
    * int8_t int16_t int32_t int64_t
    *
    * // Boolean
    * bool (C11 standard, true/false)
    *
    * // Standard types
    * size_t (pointer-sized)
    *

Quick Start

1. Include Header

  • * #include "safeapi/types/sapi_types.h"
    *

2. Use Fixed-Width Types

  • * // BAD: Platform-dependent
    * int counter; // Could be 16, 32, or 64 bits!
    * unsigned long value; // Size varies
    *
    * // GOOD: Platform-independent
    * uint32_t counter; // Always 32 bits
    * uint64_t value; // Always 64 bits
    *

Type Ranges

  •  * uint8_t:   0 to 255
     * uint16_t:  0 to 65,535
     * uint32_t:  0 to 4,294,967,295
     * uint64_t:  0 to (2^64 - 1)
     *
     * int8_t:    -128 to 127
     * int16_t:   -32,768 to 32,767
     * int32_t:   -2,147,483,648 to 2,147,483,647
     * int64_t:   -(2^63) to +(2^63 - 1)
     * 

Practical Examples

Example 1: Type-Safe Structure

  • * // For wire protocol (network or storage)
    * typedef struct {
    * uint32_t message_id; // Always 4 bytes
    * uint16_t length; // Always 2 bytes
    * int16_t temperature; // -50°C to +50°C
    * uint8_t flags; // Bit flags
    * } train_msg_t;
    *
    * // Size is predictable on ALL platforms
    * STATIC_ASSERT(sizeof(train_msg_t) == 9);
    *

Example 2: Sized Counters

  • * uint16_t packet_count; // 0-65535 packets
    * uint32_t byte_count; // 0-4B packets
    * uint64_t total_uptime_ms; // Milliseconds since boot
    *

Best Practices

  • 1. Never use bare int, long, char
  • - Always explicit type: int32_t, uint8_t, etc.
  • - Makes size guarantees visible
  • 2. Match type to purpose
  • - 16-bit counters: values 0-65535?
  • - 32-bit time: milliseconds for ~49 days?
  • - Don't over-allocate
  • 3. Use uint8_t for bytes
  • - Not unsigned char (confusing)
  • - Not int (wrong size)
  • - Always uint8_t for byte arrays
  • 4. Use bool for single flags
  • - bool enabled = true;
  • - For multiple flags: uint32_t flags; with bit manipulation
  • 5. Watch for overflow
  • - uint8_t max = 255; max++; // Wraps to 0
  • - int32_t sum = MAX + 1; // Undefined behavior!
  • - Use next size up if needed

See Also