|
Safe API Framework
Layered API framework for safety-related applications (ERTMS RBC reference targeting CENELEC EN 50128 SIL 4)
|
* DEGRADED (0) - Can return to caller * └─ Use for: single component failure, reduced redundancy * └─ Example: backup channel failed, primary still working * └─ Behavior: handler runs, can return, system continues * * SAFE (1) - Cannot return * └─ Use for: critical fail-safe state, critical functions disabled * └─ Example: both channels down, or voting disagreement * └─ Behavior: handler runs, MUST NOT return, infinite loop if it does * * REBOOT (2) - Cannot return * └─ Use for: emergency restart required * └─ Example: unrecoverable software error, corruption detected * └─ Behavior: handler runs, MUST NOT return, infinite loop if it does *
* SAPI_SAFESTATE_REASON_UNSPECIFIED (0) - No specific reason * SAPI_SAFESTATE_REASON_ASSERT_FAILED (1) - SAPI_ASSERT() condition false * SAPI_SAFESTATE_REASON_APPLICATION_BASE - 4096, start of app-specific codes *
* Application calls SAPI_SAFESTATE(level, reason) * │ * ├─ Captures __FILE__, __LINE__ * └─ Calls sapi_safestate_enter(level, reason, file, line, NULL) * │ * ├─ Retrieves registered handler for level * ├─ If handler != NULL: invokes it with context * └─ Handler returns (DEGRADED only) or doesn't (SAFE/REBOOT) * * If DEGRADED: * └─ Handler returns normally * └─ sapi_safestate_enter() returns to caller * └─ Application continues execution * * If SAFE or REBOOT: * ├─ If handler returns: * │ └─ Framework enters infinite loop (infinite while(1)) * └─ Handler never returns: * └─ Caller never regains control *
* Level 0 (DEGRADED) ──> Handler A * Level 1 (SAFE) ──> Handler B * Level 2 (REBOOT) ──> Handler C *
* Rule 2.1 │ ✓ │ No unreachable code * Rule 5.1 │ ✓ │ External identifiers unique * Rule 7.2 │ ✓ │ Correct signedness * Rule 8.4 │ ✓ │ Consistent declarations * Rule 14.4 │ ✓ │ Boolean operators correct * Rule 17.1 │ ✓ │ Pointer validity checked * Rule 20.6 │ ✓ │ No malloc/free *