Zephyr Project API  3.4.0
A Scalable Open Source RTOS
conn_mgr_connectivity.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
13#ifndef ZEPHYR_INCLUDE_CONN_MGR_CONNECTIVITY_H_
14#define ZEPHYR_INCLUDE_CONN_MGR_CONNECTIVITY_H_
15
16#include <zephyr/device.h>
17#include <zephyr/net/net_if.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
34/* Connectivity Events */
35#define _NET_MGMT_CONN_LAYER NET_MGMT_LAYER(NET_MGMT_LAYER_L2)
36#define _NET_MGMT_CONN_CODE NET_MGMT_LAYER_CODE(0x207)
37#define _NET_MGMT_CONN_BASE (_NET_MGMT_CONN_LAYER | _NET_MGMT_CONN_CODE)
38#define _NET_MGMT_CONN_IF_EVENT (NET_MGMT_IFACE_BIT | _NET_MGMT_CONN_BASE)
39
40enum net_event_ethernet_cmd {
41 NET_EVENT_CONN_CMD_IF_TIMEOUT = 1,
42 NET_EVENT_CONN_CMD_IF_FATAL_ERROR,
43};
44
45#define NET_EVENT_CONN_IF_TIMEOUT \
46 (_NET_MGMT_CONN_IF_EVENT | NET_EVENT_CONN_CMD_IF_TIMEOUT)
47
48#define NET_EVENT_CONN_IF_FATAL_ERROR \
49 (_NET_MGMT_CONN_IF_EVENT | NET_EVENT_CONN_CMD_IF_FATAL_ERROR)
50
69 int (*connect)(struct conn_mgr_conn_binding *const binding);
70
80 int (*disconnect)(struct conn_mgr_conn_binding *const binding);
81
92 void (*init)(struct conn_mgr_conn_binding *const binding);
93
112 int (*set_opt)(struct conn_mgr_conn_binding *const binding,
113 int optname, const void *optval, size_t optlen);
114
130 int (*get_opt)(struct conn_mgr_conn_binding *const binding,
131 int optname, void *optval, size_t *optlen);
132};
133
135#define CONN_MGR_CONN_IMPL_GET_NAME(conn_id) __conn_mgr_conn_##conn_id
136#define CONN_MGR_CONN_IMPL_GET_CTX_TYPE(conn_id) conn_id##_CTX_TYPE
144 /* The connectivity API used by the implementation */
146};
147
154#define CONN_MGR_CONN_DEFINE(conn_id, conn_api) \
155 const struct conn_mgr_conn_impl CONN_MGR_CONN_IMPL_GET_NAME(conn_id) = { \
156 .api = conn_api, \
157 };
158
162#define CONN_MGR_CONN_DECLARE_PUBLIC(conn_id) \
163 extern const struct conn_mgr_conn_impl CONN_MGR_CONN_IMPL_GET_NAME(conn_id)
164
166#define CONN_MGR_CONN_BINDING_GET_NAME(dev_id, sfx) __conn_mgr_bndg_##dev_id##_##sfx
167#define CONN_MGR_CONN_BINDING_GET_DATA(dev_id, sfx) __conn_mgr_bndg_data_##dev_id##_##sfx
168#define CONN_MGR_CONN_BINDING_GET_MUTEX(dev_id, sfx) __conn_mgr_bndg_mutex_##dev_id##_##sfx
175 /* Persistent
176 * When set, indicates that the connectivity implementation bound to this iface should
177 * attempt to persist connectivity by automatically reconnecting after connection loss.
178 */
180
182 /* Total number of flags - must be at the end of the enum */
183 CONN_MGR_NUM_IF_FLAGS,
185};
186
187/* Value to use with conn_mgr_conn_binding->timeout to indicate no timeout */
188#define CONN_MGR_IF_NO_TIMEOUT 0
189
197 /* The network interface the connectivity implementation is bound to */
198 struct net_if *iface;
199
200 /* The connectivity implementation the network device is bound to */
202
203 /* Pointer to private, per-iface connectivity context */
204 void *ctx;
205
206 /* Generic connectivity state - Flags
207 * Public boolean state and configuration values supported by all bindings.
208 * See conn_mgr_if_flag for options.
209 */
211
212 /* Generic connectivity state - Timeout (seconds)
213 * Indicates to the connectivity implementation how long it should attempt to
214 * establish connectivity for during a connection attempt before giving up.
215 *
216 * The connectivity implementation should give up on establishing connectivity after this
217 * timeout, even if persistence is enabled.
218 *
219 * Set to CONN_MGR_IF_NO_TIMEOUT to indicate that no timeout should be used.
220 */
222
224 /* Internal-use mutex for protecting access to the binding and API functions. */
225 struct k_mutex *mutex;
227};
228
236#define CONN_MGR_BIND_CONN_INST(dev_id, inst, conn_id) \
237 K_MUTEX_DEFINE(CONN_MGR_CONN_BINDING_GET_MUTEX(dev_id, inst)); \
238 static CONN_MGR_CONN_IMPL_GET_CTX_TYPE(conn_id) \
239 CONN_MGR_CONN_BINDING_GET_DATA(dev_id, inst); \
240 static STRUCT_SECTION_ITERABLE(conn_mgr_conn_binding, \
241 CONN_MGR_CONN_BINDING_GET_NAME(dev_id, inst)) = { \
242 .iface = NET_IF_GET(dev_id, inst), \
243 .impl = &(CONN_MGR_CONN_IMPL_GET_NAME(conn_id)), \
244 .ctx = &(CONN_MGR_CONN_BINDING_GET_DATA(dev_id, inst)), \
245 .mutex = &(CONN_MGR_CONN_BINDING_GET_MUTEX(dev_id, inst)) \
246 };
247
254#define CONN_MGR_BIND_CONN(dev_id, conn_id) \
255 CONN_MGR_BIND_CONN_INST(dev_id, 0, conn_id)
256
273int conn_mgr_if_connect(struct net_if *iface);
274
290
299bool conn_mgr_if_is_bound(struct net_if *iface);
300
320int conn_mgr_if_set_opt(struct net_if *iface, int optname, const void *optval, size_t optlen);
321
348int conn_mgr_if_get_opt(struct net_if *iface, int optname, void *optval, size_t *optlen);
349
363
377int conn_mgr_if_set_flag(struct net_if *iface, enum conn_mgr_if_flag flag, bool value);
378
390
403int conn_mgr_if_set_timeout(struct net_if *iface, int timeout);
404
411
416#ifdef __cplusplus
417}
418#endif
419
420#endif /* ZEPHYR_INCLUDE_CONN_MGR_CONNECTIVITY_H_ */
ZTEST_BMEM int timeout
Definition: main.c:31
bool conn_mgr_if_is_bound(struct net_if *iface)
Check whether the provided network interface supports connectivity / has been bound to a connectivity...
int conn_mgr_if_set_opt(struct net_if *iface, int optname, const void *optval, size_t optlen)
Set implementation-specific connectivity options.
bool conn_mgr_if_get_flag(struct net_if *iface, enum conn_mgr_if_flag flag)
Check the value of connectivity flags.
int conn_mgr_if_set_timeout(struct net_if *iface, int timeout)
Set the connectivity timeout for an iface.
int conn_mgr_if_connect(struct net_if *iface)
Connect interface.
void conn_mgr_conn_init(void)
Initialize all connectivity implementation bindings.
int conn_mgr_if_get_opt(struct net_if *iface, int optname, void *optval, size_t *optlen)
Get implementation-specific connectivity options.
int conn_mgr_if_disconnect(struct net_if *iface)
Disconnect interface.
int conn_mgr_if_get_timeout(struct net_if *iface)
Get the connectivity timeout for an iface.
int conn_mgr_if_set_flag(struct net_if *iface, enum conn_mgr_if_flag flag, bool value)
Set the value of a connectivity flags.
conn_mgr_if_flag
Per-iface connectivity flags.
Definition: conn_mgr_connectivity.h:174
@ CONN_MGR_IF_PERSISTENT
Definition: conn_mgr_connectivity.h:179
struct k_mutex mutex
Definition: kobject.c:1321
Public API for network interface.
static ZTEST_DMEM int flag
Definition: main.c:26
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
Definition: conn_mgr_connectivity.h:59
int(* get_opt)(struct conn_mgr_conn_binding *const binding, int optname, void *optval, size_t *optlen)
Implementation callback for conn_mgr_if_get_opt.
Definition: conn_mgr_connectivity.h:130
int(* set_opt)(struct conn_mgr_conn_binding *const binding, int optname, const void *optval, size_t optlen)
Implementation callback for conn_mgr_if_set_opt.
Definition: conn_mgr_connectivity.h:112
int(* connect)(struct conn_mgr_conn_binding *const binding)
When called, the connectivity implementation should start attempting to establish connectivity for (a...
Definition: conn_mgr_connectivity.h:69
void(* init)(struct conn_mgr_conn_binding *const binding)
Called once for each iface that has been bound to a connectivity implementation using this API.
Definition: conn_mgr_connectivity.h:92
int(* disconnect)(struct conn_mgr_conn_binding *const binding)
When called, the connectivity implementation should disconnect (dissasociate), or stop any in-progres...
Definition: conn_mgr_connectivity.h:80
Connectivity Manager network interface binding structure.
Definition: conn_mgr_connectivity.h:196
const struct conn_mgr_conn_impl * impl
Definition: conn_mgr_connectivity.h:201
struct net_if * iface
Definition: conn_mgr_connectivity.h:198
int timeout
Definition: conn_mgr_connectivity.h:221
uint32_t flags
Definition: conn_mgr_connectivity.h:210
void * ctx
Definition: conn_mgr_connectivity.h:204
conn_mgr Connectivity Implementation struct Declares a conn_mgr connectivity layer implementation wit...
Definition: conn_mgr_connectivity.h:143
struct conn_mgr_conn_api * api
Definition: conn_mgr_connectivity.h:145
Definition: kernel.h:2822
Network Interface structure.
Definition: net_if.h:516