net: sockets: Bootstrap Sockets API implementation
This adds Kconfig and build infrastructure and implements zsock_socket() and zsock_close() functions. Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
This commit is contained in:
parent
3dd51d52a2
commit
3432ff4fca
7 changed files with 92 additions and 0 deletions
21
include/net/socket.h
Normal file
21
include/net/socket.h
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2017 Linaro Limited
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __NET_SOCKET_H
|
||||||
|
#define __NET_SOCKET_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int zsock_socket(int family, int type, int proto);
|
||||||
|
int zsock_close(int sock);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __NET_SOCKET_H */
|
|
@ -1,3 +1,4 @@
|
||||||
|
obj-$(CONFIG_NET_SOCKETS) += sockets/
|
||||||
obj-$(CONFIG_ZOAP) += zoap/
|
obj-$(CONFIG_ZOAP) += zoap/
|
||||||
obj-$(CONFIG_DNS_RESOLVER) += dns/
|
obj-$(CONFIG_DNS_RESOLVER) += dns/
|
||||||
obj-$(CONFIG_MQTT_LIB) += mqtt/
|
obj-$(CONFIG_MQTT_LIB) += mqtt/
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
menu "Network Protocols"
|
menu "Network Protocols"
|
||||||
|
|
||||||
|
source "subsys/net/lib/sockets/Kconfig"
|
||||||
|
|
||||||
source "subsys/net/lib/zoap/Kconfig"
|
source "subsys/net/lib/zoap/Kconfig"
|
||||||
|
|
||||||
source "subsys/net/lib/dns/Kconfig"
|
source "subsys/net/lib/dns/Kconfig"
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
ifdef CONFIG_NET_SOCKETS
|
||||||
|
include $(srctree)/subsys/net/lib/sockets/Makefile
|
||||||
|
endif
|
||||||
|
|
||||||
ifdef CONFIG_ZOAP
|
ifdef CONFIG_ZOAP
|
||||||
include $(srctree)/subsys/net/lib/zoap/Makefile
|
include $(srctree)/subsys/net/lib/zoap/Makefile
|
||||||
endif
|
endif
|
||||||
|
|
24
subsys/net/lib/sockets/Kconfig
Normal file
24
subsys/net/lib/sockets/Kconfig
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# Kconfig - BSD Sockets like API
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright (c) 2017 Linaro Limited.
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
#
|
||||||
|
|
||||||
|
menuconfig NET_SOCKETS
|
||||||
|
bool "BSD Sockets like API"
|
||||||
|
default n
|
||||||
|
help
|
||||||
|
Provide BSD Sockets like API on top of native Zephyr networking API.
|
||||||
|
|
||||||
|
if NET_SOCKETS
|
||||||
|
|
||||||
|
config NET_DEBUG_SOCKETS
|
||||||
|
bool "Debug BSD Sockets like API calls"
|
||||||
|
default n
|
||||||
|
help
|
||||||
|
Enables logging for sockets code. (Logging level is defined by
|
||||||
|
SYS_LOG_NET_LEVEL setting).
|
||||||
|
|
||||||
|
endif # NET_SOCKETS
|
3
subsys/net/lib/sockets/Makefile
Normal file
3
subsys/net/lib/sockets/Makefile
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
ccflags-y += -I$(srctree)/subsys/net/lib/sockets
|
||||||
|
|
||||||
|
obj-$(CONFIG_NET_SOCKETS) += sockets.o
|
37
subsys/net/lib/sockets/sockets.c
Normal file
37
subsys/net/lib/sockets/sockets.c
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2017 Linaro Limited
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if defined(CONFIG_NET_DEBUG_SOCKETS)
|
||||||
|
#define SYS_LOG_DOMAIN "net/sock"
|
||||||
|
#define NET_LOG_ENABLED 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <net/net_context.h>
|
||||||
|
#include <net/net_pkt.h>
|
||||||
|
#include <net/socket.h>
|
||||||
|
|
||||||
|
#define SET_ERRNO(x) \
|
||||||
|
{ int _err = x; if (_err < 0) { errno = -_err; return -1; } }
|
||||||
|
|
||||||
|
int zsock_socket(int family, int type, int proto)
|
||||||
|
{
|
||||||
|
struct net_context *ctx;
|
||||||
|
|
||||||
|
SET_ERRNO(net_context_get(family, type, proto, &ctx));
|
||||||
|
/* recv_q and accept_q are in union */
|
||||||
|
k_fifo_init(&ctx->recv_q);
|
||||||
|
|
||||||
|
/* TODO: Ensure non-negative */
|
||||||
|
return POINTER_TO_INT(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
int zsock_close(int sock)
|
||||||
|
{
|
||||||
|
struct net_context *ctx = INT_TO_POINTER(sock);
|
||||||
|
|
||||||
|
SET_ERRNO(net_context_put(ctx));
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue