/*
 * Copyright (C) 2012 - 2020, Samsung Electronics Co., Ltd.
 *
 * TUI LL common definitions for all drivers
 */

#pragma once

#include "tuill_defs.h"

#if __GNUC__ >= 4
  #define TA_EXPORT __attribute__ ((visibility ("default")))
#else
  #error "unsupported compiler"
#endif

struct socket_callbacks;

typedef int32_t (*callback_t)(void *user_data);
typedef int32_t (*income_callback_t)(void *user_data, struct tuill_buffer *buff);
typedef int32_t (*accept_callback_t)(int32_t socket_family,
                                     int32_t client_fd,
                                     void **user_data,
                                     struct socket_callbacks *cb,
                                     void *cred_data,
                                     uint32_t cred_size);
typedef int32_t (*handshake_callback_t)(int32_t server_fd, void *user_data);

struct socket_callbacks {
    accept_callback_t accept;      //called for server entities to accept a connection
    handshake_callback_t handshake;   //called for client entities when connection was accepted
    income_callback_t income;  //EPOLLIN
    callback_t outcome; //EPOLLOUT
    callback_t error;   //EPOLLERR
    callback_t hangup;  //EPOLLHUP
    callback_t cleanup; //called for all entities befor entity destruction
};

TA_EXPORT int32_t tuill_socket_init(void **ctx);
TA_EXPORT void tuill_socket_uninit(void *ctx);
// run socket event loop. WARNING: this function is blocking
TA_EXPORT void tuill_socket_run_event_loop(void *ctx);
// send termination event to event loop
TA_EXPORT void tuill_socket_stop_event_loop(void *ctx);

//this function must be used on server side only
TA_EXPORT int32_t tuill_socket_create_server(void *ctx,
                                             char *name,
                                             int32_t socket_family,
                                             struct socket_callbacks *cb,
                                             void *user_data);
//this function must be used on client side only
//on server side clients are created automatically in server accept function
TA_EXPORT int32_t tuill_socket_create_client(void *ctx,
                                             char *server_name,
                                             struct socket_callbacks *cb,
                                             void *user_data);
TA_EXPORT int32_t tuill_socket_create_driver(void *ctx,
                                             char *name,
                                             const struct socket_callbacks *cb,
                                             void *user_data,
                                             uint32_t *id);
TA_EXPORT int32_t tuill_socket_delete_driver(void *ctx, uint32_t id);
TA_EXPORT int32_t tuill_socket_write_driver(void *ctx, uint32_t id, void *data, ssize_t len);

TA_EXPORT int32_t tuill_socket_client_send_to_server(void *ctx, struct tuill_internal_command *cmd);
TA_EXPORT int32_t tuill_socket_server_send_to_client(int32_t fd, struct tuill_internal_command *cmd);
