#include <stdbool.h>
#include <stdint.h>
#include "queue.h"

#ifndef BUFFERED_PERIPHERAL_H
#define BUFFERED_PERIPHERAL_H

typedef bool (*transceiver_can_receive_t)(void* peripheral);
typedef bool (*transceiver_can_transmit_t)(void* peripheral);

typedef uint16_t (*transceiver_receive_t)(void* peripheral, void* buffer, uint16_t max_size);
typedef uint16_t (*transceiver_transmit_t)(void* peripheral, void* data, uint16_t size);

typedef enum {
  OK,
  WOULD_BLOCK,
  TASK_ERROR,
} task_result_t;

typedef struct {
  transceiver_can_receive_t can_receive;
  transceiver_can_transmit_t can_transmit;
  transceiver_receive_t receive;
  transceiver_transmit_t transmit;
} buffered_transceiver_vtable_t;

typedef struct {
  queue_t* rx_queue;
  queue_t* tx_queue;
  void* peripheral;
  buffered_transceiver_vtable_t vtable;
} buffered_transceiver_t;

void buffered_transceiver_init(buffered_transceiver_t *transceiver,
                               queue_t *rx_queue, queue_t *tx_queue,
                               void *peripheral, buffered_transceiver_vtable_t vtable);

uint16_t buffered_transceiver_transmit(buffered_transceiver_t* transceiver,
                                            void* buffer,
                                            uint16_t size);

uint16_t buffered_transceiver_receive(buffered_transceiver_t* transceiver,
                                       void* buffer,
                                       uint16_t max_size);

void buffered_transceiver_trigger_receive(buffered_transceiver_t* transceiver, uint16_t size);

bool buffered_transceiver_trigger_transmit(buffered_transceiver_t* transceiver, uint16_t size);

#endif // BUFFERED_PERIPHERAL_H
