/*
 * kl_app.h
 * Application layer: command dispatch, handlers, stream sender.
 */

#ifndef KL_APP_H
#define KL_APP_H

#include "kl_protocol.h"

/* ================================================================
 * Application states
 * ================================================================ */
typedef enum {
    KL_APP_IDLE,

    /* Single response: send response -> wait ACK -> send final ACK */
    KL_APP_RESP_TX,
    KL_APP_RESP_WAIT_TX,
    KL_APP_RESP_WAIT_ACK,
    KL_APP_RESP_FINAL_ACK,
    KL_APP_RESP_FINAL_WAIT,

    /* Stream response: send chunks -> wait ACK each -> final ACK */
    KL_APP_STREAM_SEND,
    KL_APP_STREAM_WAIT_TX,
    KL_APP_STREAM_WAIT_ACK,
    KL_APP_STREAM_FINAL_ACK,
    KL_APP_STREAM_FINAL_WAIT,

    /* Simple send (ACK/NAK with no follow-up) */
    KL_APP_SIMPLE_WAIT_TX,

    KL_APP_DONE
} KL_AppState;

/* ================================================================
 * Fault code structure
 * ================================================================ */
typedef struct {
    uint8_t dtc;
    uint8_t status;
    uint8_t extra;
} KL_FaultCode;

/* ================================================================
 * API
 * ================================================================ */
void KL_App_Init(void);
void KL_App_Service(void);
void KL_App_Dispatch(const KL_Packet *cmd);
int  KL_App_Busy(void);
int  KL_App_Done(void);
void KL_App_ResetLocks(void);
void KL_App_Reset(void);

/* DTC list management */
void KL_App_SetDTCList(const KL_FaultCode *list, size_t count);

#endif /* KL_APP_H */
