#ifndef SSP_SERVICE_H
#define SSP_SERVICE_H

#include "ssp.h"

#define  SSP_VERSION_MAJOR          2
#define  SSP_VERSION_MINOR          0
#define  SSP_VERSION_RELEASE        8

#define  SSP_STATE_SELECTABLE       0x07
#define  SSP_STATE_PERSONALIZED     0x0F
#define  SSP_STATE_RESET            0x1F
#define  SSP_STATE_BLOCKED          0x2F
#define  SSP_STATE_TERMINATED       0x3F


#define AES_BLOCK_SIZE              16
#define AES_256_KEY_SIZE            32

typedef struct ssp_applet_state {
    uint8_t state;
    uint8_t version_major;
    uint8_t version_minor;
    uint8_t version_release;
} ssp_applet_state_t;

typedef struct ssp_session_st {
    uint32_t id;
    uint8_t  channel;

    uint8_t tid[SSP_TID_SIZE]; 
#ifdef OT
    ssp_applet_state_t applet_state;
#endif
    uint8_t  cmac[AES_BLOCK_SIZE];
    uint8_t  enc_key[AES_256_KEY_SIZE];
    uint8_t  mac_key[AES_256_KEY_SIZE];
} ssp_session_t;

/*
 * SSP communication APIs
 */
SSPSTATUS ssp_openSession( uint32_t *sessionId, uint8_t channel,
                           const uint8_t *otp_key_blob, uint32_t otp_key_blob_size,
                           const uint8_t tid[SSP_TID_SIZE], const uint8_t *aId, uint32_t aIdLen );
SSPSTATUS ssp_transaction( uint32_t sessionId, p_secEse_7816_cpdu_t cpdu, p_secEse_7816_rpdu_t rpdu );

SSPSTATUS ssp_openSession_with_command( uint32_t *sessionId, uint8_t channel,
                           const uint8_t *otp_key_blob, uint32_t otp_key_blob_size,
                           const uint8_t tid[SSP_TID_SIZE], const uint8_t *aId, uint32_t aIdLen,
                           uint8_t *apdu, uint32_t apduLen, p_secEse_7816_rpdu_t rpdu );
//SSPSTATUS ssp_transaction_with_command( uint32_t sessionId, p_secEse_7816_cpdu_t cpdu, p_secEse_7816_rpdu_t rpdu );

SSPSTATUS ssp_closeSession( uint32_t sessionId );

// For 3rd party
SSPSTATUS ssp_openSession_public( const uint8_t *otp_key_blob, uint32_t otp_key_blob_size,
                           uint8_t tId[SSP_TID_SIZE], uint32_t tIdLen, const uint8_t *aId, uint32_t aIdLen,
                           uint8_t *wrappedSessionInfo, uint32_t *wrappedSessionInfoSize );

SSPSTATUS ssp_service_doProvisioning(
    uint8_t *drk_cert, uint32_t drk_cert_size, uint8_t *service_cert, uint32_t service_cert_size,
    uint8_t service_key[SSP_ECC_PRIVKEY_SIZE], uint8_t *otp_key_blob_out, uint32_t *otp_key_blob_out_size );

#if 0
SSPSTATUS  ssp_service_requestResetRandom( uint8_t enc_token[SSP_RESET_CHALLENGE_SIZE] );
SSPSTATUS  ssp_service_doReset( const uint8_t token[SSP_RESET_RANDOM_SIZE] );
#endif
SSPSTATUS  ssp_service_doResetForFactory( void );

SSPSTATUS  ssp_getAppletState( ssp_applet_state_t *out );

#endif /* SSP_SERVICE_H */
