#ifndef TRANSPORT_H
#define TRANSPORT_H

#include <stddef.h>

#ifdef __cplusplus
extern "C"
{
#endif

enum {TRANSPORT_MAX_DATA_LEN = 32};

struct transport_event
{
    struct transport_event_common
    {
        enum transport_event_type
        {
            TRANSPORT_EVENT_TYPE_CONNECT,
            TRANSPORT_EVENT_TYPE_DISCONNECT,
            TRANSPORT_EVENT_TYPE_DATA
        } type;
    } common;

    union
    {
        struct transport_event_data
        {
            const void *buf;
            size_t n;
        } data;
    } u;
};

union transport_packet;

struct transport_handle
{
    struct transport_cfg
    {
        void *arg;
        int (*write)(const void *, size_t, void *);
        int (*read)(void *, size_t, void *);
        void (*received)(const struct transport_event *, void *);
    } cfg;

    union transport_packet **packets;
    size_t n_packets;

    struct transport_input
    {
        union transport_packet *p;
        struct transport_event ev;
    } input;
};

int transport_connect(struct transport_handle *h);
int transport_disconnect(struct transport_handle *h);
int transport_send(struct transport_handle *h, const void *buf, size_t n);
int transport_update(struct transport_handle *h);

#ifdef __cplusplus
}
#endif

#endif /* TRANSPORT_H */
