add support for connect with socket buffer size

This commit is contained in:
mculover666
2021-04-16 16:50:39 +08:00
parent 3fc92b3c91
commit 6084de9e41
6 changed files with 162 additions and 42 deletions

View File

@@ -410,7 +410,7 @@
<Group>
<GroupName>examples</GroupName>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>

View File

@@ -249,31 +249,81 @@ static int ec600s_init(void)
return 0;
}
static int ec600s_connect(const char *ip, const char *port, sal_proto_t proto)
static int ec600s_connect_establish(int id, sal_proto_t proto)
{
int id;
at_echo_t echo;
char except_str[16];
id = tos_at_channel_alloc(ip, port);
if (id == -1) {
char echo_buffer[64];
char *query_result_str = NULL;
char *remote_ip = NULL;
char *remote_port = NULL;
tos_at_echo_create(&echo, echo_buffer, sizeof(echo_buffer), NULL);
tos_at_cmd_exec(&echo, 1000, "AT+QISTATE=1,%d\r\n", id);
if (echo.status != AT_ECHO_STATUS_OK) {
printf("query socket %d state fail\r\n", id);
return -1;
}
tos_at_echo_create(&echo, NULL, 0, NULL);
tos_at_cmd_exec(&echo, 1000, "AT+QICLOSE=%d\r\n", id);
if (echo.status != AT_ECHO_STATUS_OK) {
sprintf(except_str, "+QISTATE: %d", id);
query_result_str = strstr(echo_buffer, except_str);
if (query_result_str) {
printf("socket %d established on module already\r\n", id);
tos_at_echo_create(&echo, NULL, 0, NULL);
tos_at_cmd_exec(&echo, 1000, "AT+QICLOSE=%d\r\n", id);
}
memset(except_str, 0, sizeof(except_str));
sprintf(except_str, "+QIOPEN: %d,0", id);
remote_ip = (char*)tos_at_channel_ip_get(id);
remote_port = (char*)tos_at_channel_port_get(id);
if (!remote_ip || !remote_port) {
return -2;
}
tos_at_echo_create(&echo, NULL, 0, except_str);
tos_at_cmd_exec_until(&echo, 4000, "AT+QIOPEN=1,%d,\"%s\",\"%s\",%d,0,1\r\n",
id, proto == TOS_SAL_PROTO_UDP ? "UDP" : "TCP", remote_ip, atoi(remote_port));
if (echo.status != AT_ECHO_STATUS_EXPECT) {
printf("establish socket %d on module fail\r\n", id);
return -3;
}
return 0;
}
static int ec600s_connect(const char *ip, const char *port, sal_proto_t proto)
{
int id;
id = tos_at_channel_alloc(ip, port);
if (id == -1) {
printf("at channel alloc fail\r\n");
return -1;
}
if (ec600s_connect_establish(id, proto) < 0) {
tos_at_channel_free(id);
return -2;
}
return id;
}
static int ec600s_connect_with_size(const char *ip, const char *port, sal_proto_t proto, size_t socket_buffer_size)
{
int id;
id = tos_at_channel_alloc_with_size(ip, port, socket_buffer_size);
if (id == -1) {
printf("at channel alloc fail\r\n");
return -1;
}
sprintf(except_str, "+QIOPEN: %d,0", id);
tos_at_echo_create(&echo, NULL, 0, except_str);
tos_at_cmd_exec_until(&echo, 4000, "AT+QIOPEN=1,%d,\"%s\",\"%s\",%d,0,1\r\n",
id, proto == TOS_SAL_PROTO_UDP ? "UDP" : "TCP", ip, atoi(port));
if (echo.status != AT_ECHO_STATUS_EXPECT) {
if (ec600s_connect_establish(id, proto) < 0) {
tos_at_channel_free(id);
return -1;
return -2;
}
return id;
@@ -533,16 +583,17 @@ at_event_t ec600s_at_event[] = {
};
sal_module_t sal_module_ec600s = {
.init = ec600s_init,
.connect = ec600s_connect,
.send = ec600s_send,
.recv_timeout = ec600s_recv_timeout,
.recv = ec600s_recv,
.sendto = ec600s_sendto,
.recvfrom = ec600s_recvfrom,
.recvfrom_timeout = ec600s_recvfrom_timeout,
.close = ec600s_close,
.parse_domain = ec600s_parse_domain,
.init = ec600s_init,
.connect = ec600s_connect,
.connect_with_size = ec600s_connect_with_size,
.send = ec600s_send,
.recv_timeout = ec600s_recv_timeout,
.recv = ec600s_recv,
.sendto = ec600s_sendto,
.recvfrom = ec600s_recvfrom,
.recvfrom_timeout = ec600s_recvfrom_timeout,
.close = ec600s_close,
.parse_domain = ec600s_parse_domain,
};
int ec600s_sal_init(hal_uart_port_t uart_port)

View File

@@ -21,16 +21,16 @@
#include "tos_k.h"
#include "tos_hal.h"
#define AT_DATA_CHANNEL_NUM 6
#define AT_DATA_CHANNEL_FIFO_BUFFER_SIZE (2048 + 1024)
#define AT_DATA_CHANNEL_NUM 6
#define AT_DATA_CHANNEL_FIFO_BUFFER_DEFAULT_SIZE (2048 + 1024)
#define AT_UART_RX_FIFO_BUFFER_SIZE (2048 + 1024)
#define AT_RECV_CACHE_SIZE 2048
#define AT_UART_RX_FIFO_BUFFER_SIZE (2048 + 1024)
#define AT_RECV_CACHE_SIZE 2048
#define AT_CMD_BUFFER_SIZE 512
#define AT_CMD_BUFFER_SIZE 512
#define AT_PARSER_TASK_STACK_SIZE 2048
#define AT_PARSER_TASK_PRIO 2
#define AT_PARSER_TASK_STACK_SIZE 2048
#define AT_PARSER_TASK_PRIO 2
typedef enum at_status_en {
AT_STATUS_OK,
@@ -210,6 +210,23 @@ __API__ int tos_at_channel_alloc_id(int channel_id, const char *ip, const char *
*/
__API__ int tos_at_channel_alloc(const char *ip, const char *port);
/**
* @brief Allocate a channel.
* Allocate a channel with certain socket buffer size.
*
* @attention None
*
* @param[in] channel_id id of the channel.
* @param[in] ip remote ip of the channel.
* @param[in] port remote port of the channel.
* @param[in] socket_buffer_size buffer size of the channel.
*
* @return errcode
* @retval -1 allocate failed(error).
* @retval none -1 the id of the channel.
*/
__API__ int tos_at_channel_alloc_with_size(const char *ip, const char *port, size_t socket_buffer_size);
/**
* @brief Free a channel.
* Free a channel with certain id.
@@ -458,7 +475,7 @@ __API__ int tos_at_uart_drain(uint8_t *buffer, size_t buffer_len);
*
* @return remote ip of the channel.
*/
__API__ const char *tos_at_agent_channel_ip_get(int channel_id);
__API__ const char *tos_at_channel_ip_get(int channel_id);
/**
* @brief Get the remote port of a channel.
@@ -470,7 +487,7 @@ __API__ const char *tos_at_agent_channel_ip_get(int channel_id);
*
* @return remote port of the channel.
*/
__API__ const char *tos_at_agent_channel_port_get(int channel_id);
__API__ const char *tos_at_channel_port_get(int channel_id);
#endif /* _TOS_AT_H_ */

View File

@@ -701,11 +701,12 @@ __API__ int tos_at_channel_write(int channel_id, uint8_t *buffer, size_t buffer_
return ret;
}
__STATIC_INLINE__ int at_channel_construct(at_data_channel_t *data_channel, const char *ip, const char *port)
__STATIC_INLINE__ int at_channel_construct(at_data_channel_t *data_channel, const char *ip, const char *port, size_t socket_buffer_size)
{
uint8_t *fifo_buffer = K_NULL;
fifo_buffer = tos_mmheap_alloc(AT_DATA_CHANNEL_FIFO_BUFFER_SIZE);
fifo_buffer = tos_mmheap_alloc(socket_buffer_size);
if (!fifo_buffer) {
return -1;
}
@@ -719,7 +720,7 @@ __STATIC_INLINE__ int at_channel_construct(at_data_channel_t *data_channel, cons
}
data_channel->rx_fifo_buffer = fifo_buffer;
tos_chr_fifo_create(&data_channel->rx_fifo, fifo_buffer, AT_DATA_CHANNEL_FIFO_BUFFER_SIZE);
tos_chr_fifo_create(&data_channel->rx_fifo, fifo_buffer, socket_buffer_size);
data_channel->remote_ip = ip;
data_channel->remote_port = port;
@@ -736,13 +737,15 @@ errout:
__API__ int tos_at_channel_alloc_id(int channel_id, const char *ip, const char *port)
{
at_data_channel_t *data_channel = K_NULL;
size_t socket_buffer_size = 0;
data_channel = at_channel_get(channel_id, K_TRUE);
if (!data_channel) {
return -1;
}
if (at_channel_construct(data_channel, ip, port) != 0) {
socket_buffer_size = AT_DATA_CHANNEL_FIFO_BUFFER_DEFAULT_SIZE;
if (at_channel_construct(data_channel, ip, port, socket_buffer_size) != 0) {
return -1;
}
@@ -750,6 +753,31 @@ __API__ int tos_at_channel_alloc_id(int channel_id, const char *ip, const char *
}
__API__ int tos_at_channel_alloc(const char *ip, const char *port)
{
int id = 0;
at_data_channel_t *data_channel = K_NULL;
size_t socket_buffer_size = 0;
for (id = 0; id < AT_DATA_CHANNEL_NUM; ++id) {
data_channel = &AT_AGENT->data_channel[id];
if (data_channel->is_free) {
break;
}
}
if (id == AT_DATA_CHANNEL_NUM || !data_channel) {
return -1;
}
socket_buffer_size = AT_DATA_CHANNEL_FIFO_BUFFER_DEFAULT_SIZE;
if (at_channel_construct(data_channel, ip, port, socket_buffer_size) != 0) {
return -1;
}
return id;
}
__API__ int tos_at_channel_alloc_with_size(const char *ip, const char *port, size_t socket_buffer_size)
{
int id = 0;
at_data_channel_t *data_channel = K_NULL;
@@ -765,7 +793,7 @@ __API__ int tos_at_channel_alloc(const char *ip, const char *port)
return -1;
}
if (at_channel_construct(data_channel, ip, port) != 0) {
if (at_channel_construct(data_channel, ip, port, socket_buffer_size) != 0) {
return -1;
}
@@ -846,7 +874,7 @@ __API__ const char *tos_at_channel_ip_get(int channel_id)
return K_NULL;
}
return data_channel[channel_id].remote_ip;
return data_channel->remote_ip;
}
__API__ const char *tos_at_channel_port_get(int channel_id)
@@ -858,7 +886,7 @@ __API__ const char *tos_at_channel_port_get(int channel_id)
return K_NULL;
}
return data_channel[channel_id].remote_port;
return data_channel->remote_port;
}
__STATIC__ void at_event_table_set(at_event_t *event_table, size_t event_table_size)

View File

@@ -43,6 +43,14 @@ int tos_sal_module_connect(const char *ip, const char *port, sal_proto_t proto)
return -1;
}
int tos_sal_module_connect_with_size(const char *ip, const char *port, sal_proto_t proto, size_t socket_buffer_size)
{
if (g_sal_module && g_sal_module->connect_with_size) {
return g_sal_module->connect_with_size(ip, port, proto, socket_buffer_size);
}
return -1;
}
int tos_sal_module_send(int sock, const void *buf, size_t len)
{
if (g_sal_module && g_sal_module->send) {

View File

@@ -37,6 +37,8 @@ typedef struct sal_module_st {
int (*connect)(const char *ip, const char *port, sal_proto_t proto);
int (*connect_with_size)(const char *ip, const char *port, sal_proto_t proto, size_t socket_buffer_size);
int (*send)(int sock, const void *buf, size_t len);
int (*recv_timeout)(int sock, void *buf, size_t len, uint32_t timeout);
@@ -109,6 +111,20 @@ int tos_sal_module_parse_domain(const char *host_name, char *host_ip, size_t hos
*/
int tos_sal_module_connect(const char *ip, const char *port, sal_proto_t proto);
/**
* @brief Connect to remote host with socket buffer size.
*
* @attention None
*
* @param[in] ip ip address of the remote host
* @param[in] port port number of the remote host
* @param[in] proto protocol of the connection(TCP/UDP)
* @param[in] socket_buffer_size the buffer size of the socket
*
* @return socket id if succuss, -1 if failed.
*/
int tos_sal_module_connect_with_size(const char *ip, const char *port, sal_proto_t proto, size_t socket_buffer_size);
/**
* @brief Send data to the remote host(TCP).
*