#ifndef I2C_H_
#define I2C_H_

// Max message size in bytes. Corresponds to number of registers
// in i2c-stub
#define MAX_BUFFER_SIZE 256U

// Connection context
typedef struct con_t {
    int dev;    // connection file descriptor
} con_t;

// Stores a buffer stores message to write or that was read.
typedef struct msg_t {
    buf_t *buf;     // buffer stores data received/to be sent to i2c slave
    uint8_t reg;    // register in i2c-stub to start with
    int is_write;   // 1 for write, 0 for read
} msg_t;


/* -----------------------------------------------------------------------------
 * @brief   Opens persistent connection to I2C slave
 *
 * @param   [out] ctx operation context
 * @param   [in ] device_num number corresponds to character device
 *                which should be used for connection. For example if
 *                device to be used is /dev/i2c-8, then device_num=8
 * @param   [in ] slave_addr address of the I2C slave. In this example
 *                it is always set to 0x03
 * @returns 1 on success and sets 'ctx', 0 otherwise.
-------------------------------------------------------------------------------- */
int i2c_dev_open(con_t *ctx, int device_num, int slave_id);

/* -----------------------------------------------------------------------------
 * @brief   Closes connection to I2C slave
 *
 * @param   [in] ctx operation context
-------------------------------------------------------------------------------- */
void i2c_dev_close(con_t *ctx);

/* -----------------------------------------------------------------------------
 * @brief   Checks if connection is opened and I2C slave responds to commands
 *
 * @param   [in] ctx operation context
 *
 * @returns 1 on success, otherwise 0
-------------------------------------------------------------------------------- */
int i2c_is_connected(con_t *ctx);

/* -----------------------------------------------------------------------------
 * @brief   Encrypts, authenticates and sends data to I2C device
 *
 * @param   [in] ctx operation context
 * @param   [in] plaintext - buffer with a plaintext to send
 * @returns 1 on success, 0 otherwise.
-------------------------------------------------------------------------------- */
int i2c_send(const con_t *ctx, const buf_t *plaintext);

/* -----------------------------------------------------------------------------
 * @brief   Receives, authenticates and decrypts data from I2C slave
 *
 * @param   [in] ctx operation context
 * @param   [out] plaintext - buffer to store decrypted data in. Buffer
 *          must be big enough to store data + TAG_LEN bytes.
 * @returns 1 on success, 0 otherwise (in case data can't be received or
            decrypted or authenticated).
-------------------------------------------------------------------------------- */
int i2c_recv(const con_t *ctx, buf_t *plaintext);

#endif // I2C_H_