Skip to content

Commit 2203a57

Browse files
JordanYateskartben
authored andcommitted
net: nsos_sockets: conn_mgr support
Add support for using native sockets with the connectivity API. This allows libraries that use the connectivity API to be tested in an application with real connectivity, without external hardware. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent 797779d commit 2203a57

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

drivers/net/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,20 @@ config NET_NATIVE_OFFLOADED_SOCKETS_EPOLL_WAIT_INTERVAL
269269
Decrease that value when lower network traffic latency is expected, at the expense of more
270270
CPU processing overhead.
271271

272+
config NET_NATIVE_OFFLOADED_SOCKETS_CONNECTIVITY_SIM
273+
bool "Support for the network connectivity API"
274+
help
275+
Simulate an interface that transitions through the dormant state based on calls
276+
to the network connectivity API, after some delay.
277+
278+
config NET_NATIVE_OFFLOADED_SOCKETS_CONNECTIVITY_SIM_AUTO_CONNECT
279+
bool "Automatically connect"
280+
depends on NET_NATIVE_OFFLOADED_SOCKETS_CONNECTIVITY_SIM
281+
help
282+
If this option is set the Connection Manager will automatically call connect after the
283+
network interface has been brought up. This option sets the default value, the option has
284+
a corresponding flag that can be set at run time by calling conn_mgr_if_set_flag().
285+
272286
endif # NET_NATIVE_OFFLOADED_SOCKETS
273287

274288
endif # NET_DRIVERS

drivers/net/nsos_sockets.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ LOG_MODULE_REGISTER(nsos_sockets);
1818

1919
#include <soc.h>
2020
#include <string.h>
21+
#include <zephyr/net/conn_mgr_connectivity.h>
22+
#include <zephyr/net/conn_mgr_connectivity_impl.h>
2123
#include <zephyr/net/ethernet.h>
2224
#include <zephyr/net/net_ip.h>
2325
#include <zephyr/net/offloaded_netdev.h>
@@ -1591,3 +1593,108 @@ NET_DEVICE_OFFLOAD_INIT(nsos_socket, "nsos_socket",
15911593
NULL,
15921594
NULL, NULL,
15931595
0, &nsos_iface_offload_api, NET_ETH_MTU);
1596+
1597+
#ifdef CONFIG_NET_NATIVE_OFFLOADED_SOCKETS_CONNECTIVITY_SIM
1598+
1599+
struct nsos_conn_data {
1600+
struct k_work_delayable work;
1601+
struct net_if *iface;
1602+
k_timeout_t connect_delay;
1603+
};
1604+
1605+
#define NSOS_NET_IF_CTX_TYPE struct nsos_conn_data
1606+
1607+
static void nsos_delayed_connect_fn(struct k_work *work)
1608+
{
1609+
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
1610+
struct nsos_conn_data *data = CONTAINER_OF(dwork, struct nsos_conn_data, work);
1611+
1612+
LOG_INF("NSOS: active");
1613+
net_if_dormant_off(data->iface);
1614+
}
1615+
1616+
static void nsos_net_if_init(struct conn_mgr_conn_binding *binding)
1617+
{
1618+
struct nsos_conn_data *data = binding->ctx;
1619+
1620+
LOG_DBG("");
1621+
1622+
/* Setup connection worker */
1623+
k_work_init_delayable(&data->work, nsos_delayed_connect_fn);
1624+
data->iface = binding->iface;
1625+
1626+
/* Set default auto connect state */
1627+
if (!IS_ENABLED(CONFIG_NET_NATIVE_OFFLOADED_SOCKETS_CONNECTIVITY_SIM_AUTO_CONNECT)) {
1628+
conn_mgr_binding_set_flag(binding, CONN_MGR_IF_NO_AUTO_CONNECT, true);
1629+
}
1630+
1631+
/* Default delay */
1632+
data->connect_delay = K_SECONDS(1);
1633+
1634+
/* Mark the interface as dormant */
1635+
net_if_dormant_on(binding->iface);
1636+
}
1637+
1638+
static int nsos_net_connect(struct conn_mgr_conn_binding *const binding)
1639+
{
1640+
struct nsos_conn_data *data = binding->ctx;
1641+
1642+
LOG_INF("NSOS: connecting");
1643+
k_work_reschedule(&data->work, data->connect_delay);
1644+
return 0;
1645+
}
1646+
1647+
static int nsos_net_if_disconnect(struct conn_mgr_conn_binding *const binding)
1648+
{
1649+
struct nsos_conn_data *data = binding->ctx;
1650+
1651+
LOG_INF("NSOS: dormant");
1652+
k_work_cancel_delayable(&data->work);
1653+
net_if_dormant_on(binding->iface);
1654+
return 0;
1655+
}
1656+
1657+
int nsos_net_if_get_opt(struct conn_mgr_conn_binding *const binding, int optname,
1658+
void *optval, size_t *optlen)
1659+
{
1660+
struct nsos_conn_data *data = binding->ctx;
1661+
1662+
if (optname != 0) {
1663+
return -EINVAL;
1664+
}
1665+
if (*optlen < sizeof(k_timeout_t)) {
1666+
return -EINVAL;
1667+
}
1668+
memcpy(optval, &data->connect_delay, sizeof(data->connect_delay));
1669+
*optlen = sizeof(data->connect_delay);
1670+
return 0;
1671+
}
1672+
1673+
int nsos_net_if_set_opt(struct conn_mgr_conn_binding *const binding, int optname,
1674+
const void *optval, size_t optlen)
1675+
{
1676+
struct nsos_conn_data *data = binding->ctx;
1677+
const k_timeout_t *opt = optval;
1678+
1679+
if (optname != 0) {
1680+
return -EINVAL;
1681+
}
1682+
if (optlen != sizeof(k_timeout_t)) {
1683+
return -EINVAL;
1684+
}
1685+
data->connect_delay = *opt;
1686+
return 0;
1687+
}
1688+
1689+
static struct conn_mgr_conn_api nsos_conn_mgr_api = {
1690+
.init = nsos_net_if_init,
1691+
.connect = nsos_net_connect,
1692+
.disconnect = nsos_net_if_disconnect,
1693+
.get_opt = nsos_net_if_get_opt,
1694+
.set_opt = nsos_net_if_set_opt,
1695+
};
1696+
1697+
CONN_MGR_CONN_DEFINE(NSOS_NET_IF, &nsos_conn_mgr_api);
1698+
CONN_MGR_BIND_CONN(nsos_socket, NSOS_NET_IF);
1699+
1700+
#endif /* CONFIG_NET_NATIVE_OFFLOADED_SOCKETS_CONNECTIVITY_SIM */

0 commit comments

Comments
 (0)