/*
 * Aero capture policy bench reference. This file exercises capture decisions
 * on a host. A device adapter owns microphone power, the indicator, the
 * recording buffer, and delivery to the phone.
 *
 * cc -std=c11 -Wall -Wextra -Werror -DAERO_CAPTURE_SELFTEST \
 *   aero-capture-policy.c -o aero-capture-test
 * ./aero-capture-test
 */

#include <stdbool.h>

typedef enum {
    AERO_WAIT_RELEASE = 0,
    AERO_READY,
    AERO_CAPTURING
} AeroCaptureState;

typedef enum {
    AERO_NO_CHANGE = 0,
    AERO_BEGIN_CAPTURE,
    AERO_DISCARD_CAPTURE,
    AERO_OFFER_REVIEW
} AeroCaptureDecision;

typedef struct {
    bool request_held;
    bool hardware_muted;
    bool indicator_ready;
    bool trusted_link;
    bool deadline_elapsed;
} AeroCaptureInputs;

AeroCaptureDecision aero_capture_step(AeroCaptureState *state, AeroCaptureInputs input)
{
    if (input.hardware_muted || !input.indicator_ready ||
        !input.trusted_link || input.deadline_elapsed) {
        const bool had_capture = *state == AERO_CAPTURING;
        *state = input.request_held ? AERO_WAIT_RELEASE : AERO_READY;
        return had_capture ? AERO_DISCARD_CAPTURE : AERO_NO_CHANGE;
    }

    switch (*state) {
    case AERO_WAIT_RELEASE:
        if (!input.request_held) {
            *state = AERO_READY;
        }
        return AERO_NO_CHANGE;
    case AERO_READY:
        if (input.request_held) {
            *state = AERO_CAPTURING;
            return AERO_BEGIN_CAPTURE;
        }
        return AERO_NO_CHANGE;
    case AERO_CAPTURING:
        if (!input.request_held) {
            *state = AERO_READY;
            return AERO_OFFER_REVIEW;
        }
        return AERO_NO_CHANGE;
    default:
        *state = AERO_WAIT_RELEASE;
        return AERO_DISCARD_CAPTURE;
    }
}

#ifdef AERO_CAPTURE_SELFTEST
#include <assert.h>
#include <stdio.h>

static void check_capture_and_review(void)
{
    AeroCaptureState state = AERO_WAIT_RELEASE;
    AeroCaptureInputs input = {.request_held = true, .indicator_ready = true, .trusted_link = true};
    assert(aero_capture_step(&state, input) == AERO_NO_CHANGE);
    assert(state == AERO_WAIT_RELEASE);
    input.request_held = false;
    assert(aero_capture_step(&state, input) == AERO_NO_CHANGE);
    input.request_held = true;
    assert(aero_capture_step(&state, input) == AERO_BEGIN_CAPTURE);
    assert(aero_capture_step(&state, input) == AERO_NO_CHANGE);
    input.request_held = false;
    assert(aero_capture_step(&state, input) == AERO_OFFER_REVIEW);
    assert(aero_capture_step(&state, input) == AERO_NO_CHANGE);
}

static void check_faults_and_recovery(void)
{
    const AeroCaptureInputs healthy = {.request_held = true, .indicator_ready = true, .trusted_link = true};
    for (unsigned faults = 1; faults < 16; ++faults) {
        AeroCaptureInputs input = healthy;
        input.hardware_muted = (faults & 1u) != 0;
        input.indicator_ready = (faults & 2u) == 0;
        input.trusted_link = (faults & 4u) == 0;
        input.deadline_elapsed = (faults & 8u) != 0;

        AeroCaptureState idle = AERO_READY;
        assert(aero_capture_step(&idle, input) == AERO_NO_CHANGE);
        assert(idle == AERO_WAIT_RELEASE);
        assert(aero_capture_step(&idle, healthy) == AERO_NO_CHANGE);

        AeroCaptureState active = AERO_CAPTURING;
        assert(aero_capture_step(&active, input) == AERO_DISCARD_CAPTURE);
        assert(active == AERO_WAIT_RELEASE);
        assert(aero_capture_step(&active, healthy) == AERO_NO_CHANGE);

        AeroCaptureInputs released = healthy;
        released.request_held = false;
        assert(aero_capture_step(&active, released) == AERO_NO_CHANGE);
        assert(aero_capture_step(&active, healthy) == AERO_BEGIN_CAPTURE);

        input.request_held = false;
        assert(aero_capture_step(&active, input) == AERO_DISCARD_CAPTURE);
    }
}

int main(void)
{
    check_capture_and_review();
    check_faults_and_recovery();
    puts("Aero capture policy passed. Release offers review; all 15 fault combinations block or discard capture.");
    return 0;
}
#endif
