Zephyr API Documentation  3.6.0
A Scalable Open Source RTOS
3.6.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
socket_service.h
Go to the documentation of this file.
1
9/*
10 * Copyright (c) 2023 Nordic Semiconductor ASA
11 *
12 * SPDX-License-Identifier: Apache-2.0
13 */
14
15#ifndef ZEPHYR_INCLUDE_NET_SOCKET_SERVICE_H_
16#define ZEPHYR_INCLUDE_NET_SOCKET_SERVICE_H_
17
25#include <sys/types.h>
26#include <zephyr/types.h>
27#include <zephyr/net/socket.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
39 struct k_work work;
45 void *user_data;
48};
49
61#if CONFIG_NET_SOCKETS_LOG_LEVEL >= LOG_LEVEL_DBG
66 const char *owner;
67#endif
75 int *idx;
76};
77
78#define __z_net_socket_svc_get_name(_svc_id) __z_net_socket_service_##_svc_id
79#define __z_net_socket_svc_get_idx(_svc_id) __z_net_socket_service_idx_##_svc_id
80#define __z_net_socket_svc_get_owner __FILE__ ":" STRINGIFY(__LINE__)
81
83
84#if CONFIG_NET_SOCKETS_LOG_LEVEL >= LOG_LEVEL_DBG
85#define NET_SOCKET_SERVICE_OWNER .owner = __z_net_socket_svc_get_owner,
86#else
87#define NET_SOCKET_SERVICE_OWNER
88#endif
89
90#define NET_SOCKET_SERVICE_CALLBACK_MODE(_flag) \
91 IF_ENABLED(_flag, \
92 (.work = Z_WORK_INITIALIZER(net_socket_service_callback),))
93
94#define __z_net_socket_service_define(_name, _work_q, _cb, _count, _async, ...) \
95 static int __z_net_socket_svc_get_idx(_name); \
96 static struct net_socket_service_event \
97 __z_net_socket_svc_get_name(_name)[_count] = { \
98 [0 ... ((_count) - 1)] = { \
99 .event.fd = -1, /* Invalid socket */ \
100 NET_SOCKET_SERVICE_CALLBACK_MODE(_async) \
101 .callback = _cb, \
102 } \
103 }; \
104 COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (), __VA_ARGS__) \
105 const STRUCT_SECTION_ITERABLE(net_socket_service_desc, _name) = { \
106 NET_SOCKET_SERVICE_OWNER \
107 .work_q = (_work_q), \
108 .pev = __z_net_socket_svc_get_name(_name), \
109 .pev_len = (_count), \
110 .idx = &__z_net_socket_svc_get_idx(_name), \
111 }
112
133#define NET_SOCKET_SERVICE_ASYNC_DEFINE(name, work_q, cb, count) \
134 __z_net_socket_service_define(name, work_q, cb, count, 1)
135
148#define NET_SOCKET_SERVICE_ASYNC_DEFINE_STATIC(name, work_q, cb, count) \
149 __z_net_socket_service_define(name, work_q, cb, count, 1, static)
150
171#define NET_SOCKET_SERVICE_SYNC_DEFINE(name, work_q, cb, count) \
172 __z_net_socket_service_define(name, work_q, cb, count, 0)
173
186#define NET_SOCKET_SERVICE_SYNC_DEFINE_STATIC(name, work_q, cb, count) \
187 __z_net_socket_service_define(name, work_q, cb, count, 0, static)
188
201__syscall int net_socket_service_register(const struct net_socket_service_desc *service,
202 struct zsock_pollfd *fds, int len, void *user_data);
203
213static inline int net_socket_service_unregister(const struct net_socket_service_desc *service)
214{
215 return net_socket_service_register(service, NULL, 0, NULL);
216}
217
225typedef void (*net_socket_service_cb_t)(const struct net_socket_service_desc *svc,
226 void *user_data);
227
235
236#ifdef __cplusplus
237}
238#endif
239
240#include <syscalls/socket_service.h>
241
246#endif /* ZEPHYR_INCLUDE_NET_SOCKET_SERVICE_H_ */
void(* net_socket_service_cb_t)(const struct net_socket_service_desc *svc, void *user_data)
Callback used while iterating over socket services.
Definition: socket_service.h:225
int net_socket_service_register(const struct net_socket_service_desc *service, struct zsock_pollfd *fds, int len, void *user_data)
Register pollable sockets.
void net_socket_service_callback(struct k_work *work)
void net_socket_service_foreach(net_socket_service_cb_t cb, void *user_data)
Go through all the socket services and call callback for each service.
static int net_socket_service_unregister(const struct net_socket_service_desc *service)
Unregister pollable sockets.
Definition: socket_service.h:213
void(* k_work_handler_t)(struct k_work *work)
The signature for a work item handler function.
Definition: kernel.h:3262
BSD Sockets compatible API definitions.
A structure used to hold work until it can be processed.
Definition: kernel.h:4008
A structure used to submit work.
Definition: kernel.h:3861
Main structure holding socket service configuration information.
Definition: socket_service.h:60
const char * owner
Owner name.
Definition: socket_service.h:66
int * idx
Where are my pollfd entries in the global list.
Definition: socket_service.h:75
int pev_len
Length of the pollable socket array for this service.
Definition: socket_service.h:73
struct net_socket_service_event * pev
Pointer to the list of services that we are listening.
Definition: socket_service.h:71
struct k_work_q * work_q
Workqueue where the work is submitted.
Definition: socket_service.h:69
This struct contains information which socket triggered calls to the callback function.
Definition: socket_service.h:37
struct k_work work
k_work that is done when there is desired activity in file descriptor.
Definition: socket_service.h:39
k_work_handler_t callback
Callback to be called for desired socket activity.
Definition: socket_service.h:41
struct net_socket_service_desc * svc
Service back pointer.
Definition: socket_service.h:47
struct zsock_pollfd event
Socket information that triggered this event.
Definition: socket_service.h:43
void * user_data
User data.
Definition: socket_service.h:45
Definition of the monitored socket/file descriptor.
Definition: socket.h:43