@@ -18,6 +18,8 @@ LOG_MODULE_REGISTER(nsos_sockets);
18
18
19
19
#include <soc.h>
20
20
#include <string.h>
21
+ #include <zephyr/net/conn_mgr_connectivity.h>
22
+ #include <zephyr/net/conn_mgr_connectivity_impl.h>
21
23
#include <zephyr/net/ethernet.h>
22
24
#include <zephyr/net/net_ip.h>
23
25
#include <zephyr/net/offloaded_netdev.h>
@@ -1591,3 +1593,108 @@ NET_DEVICE_OFFLOAD_INIT(nsos_socket, "nsos_socket",
1591
1593
NULL ,
1592
1594
NULL , NULL ,
1593
1595
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