Files
kunlun/cli/communicator/cli_lwip_api.c
2024-09-28 14:24:04 +08:00

104 lines
3.3 KiB
C
Executable File

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
#include "lwip/sockets.h"
#include "iot_config.h"
#if (IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_LWIP_SOCKET)
int cli_open_lwip_udp_socket(const char* bind_ip, uint32_t port)
{
struct sockaddr_in bindaddr = {0};
bindaddr.sin_addr.s_addr = inet_addr(bind_ip);
bindaddr.sin_family = AF_INET;
bindaddr.sin_len = sizeof(bindaddr);
bindaddr.sin_port = PP_HTONS((uint16_t)port);
int socket = lwip_socket(AF_INET, SOCK_DGRAM, 0);
lwip_bind(socket, (const struct sockaddr*)&bindaddr, sizeof(bindaddr));
return socket;
}
int cli_lwip_sendto(int s, const void *data, size_t size,
const char* remote_ip, uint32_t port)
{
struct sockaddr_in remoteaddr = { 0 };
remoteaddr.sin_addr.s_addr = inet_addr(remote_ip);
remoteaddr.sin_family = AF_INET;
remoteaddr.sin_len = sizeof(remoteaddr);
remoteaddr.sin_port = PP_HTONS((uint16_t)port);;
return lwip_sendto(s, data, size, 0,
(const struct sockaddr*)&remoteaddr, sizeof(remoteaddr));
}
int cli_lwip_recvfrom(int s, void *mem, size_t len, int flags,
void *from, uint32_t addrlen)
{
return lwip_recvfrom(s, mem, len, flags, (struct sockaddr*)from,
(socklen_t*)&addrlen);
}
int cli_open_lwip_tcp_socket(uint32_t port)
{
struct sockaddr_in bindaddr = { 0 };
bindaddr.sin_addr.s_addr = IPADDR_ANY;
bindaddr.sin_family = AF_INET;
bindaddr.sin_len = sizeof(bindaddr);
bindaddr.sin_port = PP_HTONS((uint16_t)port);
int skt = lwip_socket(AF_INET, SOCK_STREAM, 0);
if (skt < 0) {
return skt;
}
int ret = lwip_bind(skt, (const struct sockaddr*)&bindaddr,
sizeof(bindaddr));
if (ret < 0) {
return ret;
}
int opt = 1;
setsockopt(skt, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(int));
setsockopt(skt, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(int));
ret = lwip_listen(skt, 1); // currently, only support 1 client
if (ret < 0) {
return ret;
}
return skt;
}
int cli_lwip_accept(int s)
{
struct sockaddr_in remote;
socklen_t len = sizeof(remote);
int opt = 1;
int new_s = lwip_accept(s, (struct sockaddr*)&remote, &len);
setsockopt(new_s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(int));
return new_s;
}
int cli_lwip_read(int s, void*men, size_t len)
{
return lwip_read(s, men, len);
}
int cli_lwip_send(int s, void*buf, size_t len)
{
return lwip_send(s, buf, len, 0);
}
#endif /*IOT_CLI_INF_MODE == IOT_CLI_INF_MODE_LWIP_SOCKET*/