/**
 * @file socket_inet.h
 * @author LokLiang
 * @brief 主要把 sockets 标准接口封装为 sb_data_port.h::sb_data_port_t 接口
 * @version 0.1
 * @date 2023-11-10
 *
 * @copyright Copyright (c) 2023
 *
 */

#pragma once

#include <netinet/in.h>
#include "drivers/data_port/sb_data_port.h"
#include "os/os.h"

typedef void *socket_listen_t;
typedef socket_listen_t socket_listen_tcp_t;

typedef void (*connect_cb)(bool is_connect, sb_data_port_t *port);
typedef void (*rx_event_cb)(sb_data_port_t *port);

typedef struct
{
    uint16_t rx_buf_size;      // 为该接口提供多大的接收缓存（建议取值 256..512，更多依赖协议栈的缓存）
    rx_event_cb rx_event;      // 当该接口收到数据时执行的回调函数，可取值为 NULL
    os_work_t *rx_resume_work; // 当该接口收到数据时唤醒的工作项，可取值为 NULL
} socket_server_bind_t;

int socket_inet_init(void);

sb_data_port_t *socket_inet_server_bind_udp(uint16_t listen_port, const socket_server_bind_t *bind_param);
void socket_inet_server_unbind_udp(sb_data_port_t *port);

int socket_inet_server_udp_read_addr(sb_data_port_t *port, in_addr_t *s_addr, in_port_t *sin_port);
int socket_inet_server_udp_broadcast(sb_data_port_t *port, uint8_t ip_v4[4], uint16_t broadcast_port, const void *data, uint32_t size);

socket_listen_tcp_t socket_inet_server_listen_tcp(uint16_t listen_port, uint8_t tcp_backlog, const connect_cb connect);
sb_data_port_t *socket_inet_server_bind_tcp(socket_listen_tcp_t listen_tcp, const socket_server_bind_t *bind_param);
void socket_inet_server_unbind_tcp(socket_listen_tcp_t listen_tcp);
