net: sockets: Implement send()
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
This commit is contained in:
parent
9405887aa7
commit
41e88ea40c
2 changed files with 34 additions and 0 deletions
|
@ -7,6 +7,7 @@
|
|||
#ifndef __NET_SOCKET_H
|
||||
#define __NET_SOCKET_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <zephyr/types.h>
|
||||
#include <net/net_ip.h>
|
||||
|
||||
|
@ -20,6 +21,7 @@ int zsock_bind(int sock, const struct sockaddr *addr, socklen_t addrlen);
|
|||
int zsock_connect(int sock, const struct sockaddr *addr, socklen_t addrlen);
|
||||
int zsock_listen(int sock, int backlog);
|
||||
int zsock_accept(int sock, struct sockaddr *addr, socklen_t *addrlen);
|
||||
ssize_t zsock_send(int sock, const void *buf, size_t len, int flags);
|
||||
|
||||
#if defined(CONFIG_NET_SOCKETS_POSIX_NAMES)
|
||||
#define socket zsock_socket
|
||||
|
@ -28,6 +30,7 @@ int zsock_accept(int sock, struct sockaddr *addr, socklen_t *addrlen);
|
|||
#define connect zsock_connect
|
||||
#define listen zsock_listen
|
||||
#define accept zsock_accept
|
||||
#define send zsock_send
|
||||
|
||||
#define inet_ntop net_addr_ntop
|
||||
#define inet_pton net_addr_pton
|
||||
|
|
|
@ -141,3 +141,34 @@ int zsock_accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
|
|||
/* TODO: Ensure non-negative */
|
||||
return POINTER_TO_INT(ctx);
|
||||
}
|
||||
|
||||
ssize_t zsock_send(int sock, const void *buf, size_t len, int flags)
|
||||
{
|
||||
ARG_UNUSED(flags);
|
||||
int err;
|
||||
struct net_context *ctx = INT_TO_POINTER(sock);
|
||||
struct net_pkt *send_pkt = net_pkt_get_tx(ctx, K_FOREVER);
|
||||
size_t max_len = net_if_get_mtu(net_context_get_iface(ctx));
|
||||
|
||||
/* Make sure we don't send more data in one packet than
|
||||
* MTU allows. Optimize for number of branches in the code.
|
||||
*/
|
||||
max_len -= NET_IPV4TCPH_LEN;
|
||||
if (net_context_get_family(ctx) != AF_INET) {
|
||||
max_len -= NET_IPV6TCPH_LEN - NET_IPV4TCPH_LEN;
|
||||
}
|
||||
|
||||
if (len > max_len) {
|
||||
len = max_len;
|
||||
}
|
||||
|
||||
len = net_pkt_append(send_pkt, len, buf, K_FOREVER);
|
||||
err = net_context_send(send_pkt, /*cb*/NULL, K_FOREVER, NULL, NULL);
|
||||
if (err < 0) {
|
||||
net_pkt_unref(send_pkt);
|
||||
errno = -err;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue