Anjay Lite
Loading...
Searching...
No Matches
anj_net_wrapper.h
Go to the documentation of this file.
1/*
2 * Copyright 2023-2025 AVSystem <avsystem@avsystem.com>
3 * AVSystem Anjay Lite LwM2M SDK
4 * All rights reserved.
5 *
6 * Licensed under AVSystem Anjay Lite LwM2M Client SDK - Non-Commercial License.
7 * See the attached LICENSE file for details.
8 */
9
10#include <anj/init.h>
11
12#ifndef ANJ_NET_WRAPPER_H
13# define ANJ_NET_WRAPPER_H
14
16# ifdef ANJ_NET_WITH_UDP
18# endif // ANJ_NET_WITH_UDP
19# ifdef ANJ_NET_WITH_TCP
21# endif // ANJ_NET_WITH_TCP
22# ifdef ANJ_WITH_DTLS_BINDING
24# endif // ANJ_WITH_DTLS_BINDING
25# ifdef ANJ_WITH_TLS_BINDING
27# endif // ANJ_WITH_TLS_BINDING
28# ifdef ANJ_WITH_NON_IP_BINDING
30# endif // ANJ_WITH_NON_IP_BINDING
31
32# ifdef __cplusplus
33extern "C" {
34# endif
35
36// Wrapper for get_system_socket
38 anj_net_ctx_t *ctx) {
39 switch (type) {
40# if defined(ANJ_NET_WITH_UDP)
42 return anj_udp_get_system_socket(ctx);
43# endif // defined(ANJ_NET_WITH_UDP)
44# if defined(ANJ_NET_WITH_TCP)
46 return anj_tcp_get_system_socket(ctx);
47# endif // defined(ANJ_NET_WITH_TCP)
48# if defined(ANJ_WITH_DTLS_BINDING)
50 return anj_dtls_get_system_socket(ctx);
51# endif // defined(ANJ_WITH_DTLS_BINDING)
52# if defined(ANJ_WITH_TLS_BINDING)
54 return anj_tls_get_system_socket(ctx);
55# endif // defined(ANJ_WITH_TLS_BINDING)
56# if defined(ANJ_WITH_NON_IP_BINDING)
58 return anj_non_ip_get_system_socket(ctx);
59# endif // defined(ANJ_WITH_TLS_BINDING)
60 default:
61 return NULL;
62 }
63}
64
65// Wrapper for create_ctx
67 anj_net_ctx_t **ctx,
68 const anj_net_config_t *config) {
69 switch (type) {
70# if defined(ANJ_NET_WITH_UDP)
72 return anj_udp_create_ctx(ctx, config);
73# endif // defined(ANJ_NET_WITH_UDP)
74# if defined(ANJ_NET_WITH_TCP)
76 return anj_tcp_create_ctx(ctx, config);
77# endif // defined(ANJ_NET_WITH_TCP)
78# if defined(ANJ_WITH_DTLS_BINDING)
80 return anj_dtls_create_ctx(ctx, config);
81# endif // defined(ANJ_WITH_DTLS_BINDING)
82# if defined(ANJ_WITH_TLS_BINDING)
84 return anj_tls_create_ctx(ctx, config);
85# endif // defined(ANJ_WITH_TLS_BINDING)
86# if defined(ANJ_WITH_NON_IP_BINDING)
88 return anj_non_ip_create_ctx(ctx, config);
89# endif // defined(ANJ_WITH_NON_IP_BINDING)
90 default:
91 return ANJ_NET_ENOTSUP;
92 }
93}
94
95// Wrapper for connect
97 anj_net_ctx_t *ctx,
98 const char *hostname,
99 const char *port) {
100 switch (type) {
101# if defined(ANJ_NET_WITH_UDP)
103 return anj_udp_connect(ctx, hostname, port);
104# endif // defined(ANJ_NET_WITH_UDP)
105# if defined(ANJ_NET_WITH_TCP)
107 return anj_tcp_connect(ctx, hostname, port);
108# endif // defined(ANJ_NET_WITH_TCP)
109# if defined(ANJ_WITH_DTLS_BINDING)
111 return anj_dtls_connect(ctx, hostname, port);
112# endif // defined(ANJ_WITH_DTLS_BINDING)
113# if defined(ANJ_WITH_TLS_BINDING)
115 return anj_tls_connect(ctx, hostname, port);
116# endif // defined(ANJ_WITH_TLS_BINDING)
117# if defined(ANJ_WITH_NON_IP_BINDING)
119 return anj_non_ip_connect(ctx, hostname, port);
120# endif // defined(ANJ_WITH_NON_IP_BINDING)
121 default:
122 return ANJ_NET_ENOTSUP;
123 }
124}
125
126// Wrapper for send
128 anj_net_ctx_t *ctx,
129 size_t *bytes_sent,
130 const uint8_t *buf,
131 size_t length) {
132 switch (type) {
133# if defined(ANJ_NET_WITH_UDP)
135 return anj_udp_send(ctx, bytes_sent, buf, length);
136# endif // defined(ANJ_NET_WITH_UDP)
137# if defined(ANJ_NET_WITH_TCP)
139 return anj_tcp_send(ctx, bytes_sent, buf, length);
140# endif // defined(ANJ_NET_WITH_TCP)
141# if defined(ANJ_WITH_DTLS_BINDING)
143 return anj_dtls_send(ctx, bytes_sent, buf, length);
144# endif // defined(ANJ_WITH_DTLS_BINDING)
145# if defined(ANJ_WITH_TLS_BINDING)
147 return anj_tls_send(ctx, bytes_sent, buf, length);
148# endif // defined(ANJ_WITH_TLS_BINDING)
149# if defined(ANJ_WITH_NON_IP_BINDING)
151 return anj_non_ip_send(ctx, bytes_sent, buf, length);
152# endif // defined(ANJ_WITH_NON_IP_BINDING)
153 default:
154 return ANJ_NET_ENOTSUP;
155 }
156}
157
158// Wrapper for recv
160 anj_net_ctx_t *ctx,
161 size_t *bytes_received,
162 uint8_t *buf,
163 size_t length) {
164 switch (type) {
165# if defined(ANJ_NET_WITH_UDP)
167 return anj_udp_recv(ctx, bytes_received, buf, length);
168# endif // defined(ANJ_NET_WITH_UDP)
169# if defined(ANJ_NET_WITH_TCP)
171 return anj_tcp_recv(ctx, bytes_received, buf, length);
172# endif // defined(ANJ_NET_WITH_TCP)
173# if defined(ANJ_WITH_DTLS_BINDING)
175 return anj_dtls_recv(ctx, bytes_received, buf, length);
176# endif // defined(ANJ_WITH_DTLS_BINDING)
177# if defined(ANJ_WITH_TLS_BINDING)
179 return anj_tls_recv(ctx, bytes_received, buf, length);
180# endif // defined(ANJ_WITH_TLS_BINDING)
181# if defined(ANJ_WITH_NON_IP_BINDING)
183 return anj_non_ip_recv(ctx, bytes_received, buf, length);
184# endif // defined(ANJ_WITH_NON_IP_BINDING)
185 default:
186 return ANJ_NET_ENOTSUP;
187 }
188}
189
190// Wrapper for close
192 anj_net_ctx_t *ctx) {
193 switch (type) {
194# if defined(ANJ_NET_WITH_UDP)
196 return anj_udp_close(ctx);
197# endif // defined(ANJ_NET_WITH_UDP)
198# if defined(ANJ_NET_WITH_TCP)
200 return anj_tcp_close(ctx);
201# endif // defined(ANJ_NET_WITH_TCP)
202# if defined(ANJ_WITH_DTLS_BINDING)
204 return anj_dtls_close(ctx);
205# endif // defined(ANJ_WITH_DTLS_BINDING)
206# if defined(ANJ_WITH_TLS_BINDING)
208 return anj_tls_close(ctx);
209# endif // defined(ANJ_WITH_TLS_BINDING)
210# if defined(ANJ_WITH_NON_IP_BINDING)
212 return anj_non_ip_close(ctx);
213# endif // defined(ANJ_WITH_NON_IP_BINDING)
214 default:
215 return ANJ_NET_ENOTSUP;
216 }
217}
218
219// Wrapper for shutdown
221 anj_net_ctx_t *ctx) {
222 switch (type) {
223# if defined(ANJ_NET_WITH_UDP)
225 return anj_udp_shutdown(ctx);
226# endif // defined(ANJ_NET_WITH_UDP)
227# if defined(ANJ_NET_WITH_TCP)
229 return anj_tcp_shutdown(ctx);
230# endif // defined(ANJ_NET_WITH_TCP)
231# if defined(ANJ_WITH_DTLS_BINDING)
233 return anj_dtls_shutdown(ctx);
234# endif // defined(ANJ_WITH_DTLS_BINDING)
235# if defined(ANJ_WITH_TLS_BINDING)
237 return anj_tls_shutdown(ctx);
238# endif // defined(ANJ_WITH_TLS_BINDING)
239# if defined(ANJ_WITH_NON_IP_BINDING)
241 return anj_non_ip_shutdown(ctx);
242# endif // defined(ANJ_WITH_NON_IP_BINDING)
243 default:
244 return ANJ_NET_ENOTSUP;
245 }
246}
247
248// Wrapper for cleanup_ctx
250 anj_net_ctx_t **ctx) {
251 switch (type) {
252# if defined(ANJ_NET_WITH_UDP)
254 return anj_udp_cleanup_ctx(ctx);
255# endif // defined(ANJ_NET_WITH_UDP)
256# if defined(ANJ_NET_WITH_TCP)
258 return anj_tcp_cleanup_ctx(ctx);
259# endif // defined(ANJ_NET_WITH_TCP)
260# if defined(ANJ_WITH_DTLS_BINDING)
262 return anj_dtls_cleanup_ctx(ctx);
263# endif // defined(ANJ_WITH_DTLS_BINDING)
264# if defined(ANJ_WITH_TLS_BINDING)
266 return anj_tls_cleanup_ctx(ctx);
267# endif // defined(ANJ_WITH_TLS_BINDING)
268# if defined(ANJ_WITH_NON_IP_BINDING)
270 return anj_non_ip_cleanup_ctx(ctx);
271# endif // defined(ANJ_WITH_NON_IP_BINDING)
272 default:
273 return ANJ_NET_ENOTSUP;
274 }
275}
276
277// Wrapper for reuse_last_port
279 anj_net_ctx_t *ctx) {
280 switch (type) {
281# if defined(ANJ_NET_WITH_UDP)
283 return anj_udp_reuse_last_port(ctx);
284# endif // defined(ANJ_NET_WITH_UDP)
285# if defined(ANJ_NET_WITH_TCP)
287 return anj_tcp_reuse_last_port(ctx);
288# endif // defined(ANJ_NET_WITH_TCP)
289# if defined(ANJ_WITH_DTLS_BINDING)
291 return anj_dtls_reuse_last_port(ctx);
292# endif // defined(ANJ_WITH_DTLS_BINDING)
293# if defined(ANJ_WITH_TLS_BINDING)
295 return anj_tls_reuse_last_port(ctx);
296# endif // defined(ANJ_WITH_TLS_BINDING)
297# if defined(ANJ_WITH_NON_IP_BINDING)
299 return ANJ_NET_ENOTSUP;
300# endif // defined(ANJ_WITH_NON_IP_BINDING)
301 default:
302 return ANJ_NET_ENOTSUP;
303 }
304}
305
306// Wrapper for get_bytes_received
308 anj_net_ctx_t *ctx,
309 uint64_t *out_value) {
310 switch (type) {
311# if defined(ANJ_NET_WITH_UDP)
313 return anj_udp_get_bytes_received(ctx, out_value);
314# endif // defined(ANJ_NET_WITH_UDP)
315# if defined(ANJ_NET_WITH_TCP)
317 return anj_tcp_get_bytes_received(ctx, out_value);
318# endif // defined(ANJ_NET_WITH_TCP)
319# if defined(ANJ_WITH_DTLS_BINDING)
321 return anj_dtls_get_bytes_received(ctx, out_value);
322# endif // defined(ANJ_WITH_DTLS_BINDING)
323# if defined(ANJ_WITH_TLS_BINDING)
325 return anj_tls_get_bytes_received(ctx, out_value);
326# endif // defined(ANJ_WITH_TLS_BINDING)
327# if defined(ANJ_WITH_NON_IP_BINDING)
329 return anj_non_ip_get_bytes_received(ctx, out_value);
330# endif // defined(ANJ_WITH_NON_IP_BINDING)
331 default:
332 return ANJ_NET_ENOTSUP;
333 }
334}
335
336// Wrapper for get_bytes_sent
338 anj_net_ctx_t *ctx,
339 uint64_t *out_value) {
340 switch (type) {
341# if defined(ANJ_NET_WITH_UDP)
343 return anj_udp_get_bytes_sent(ctx, out_value);
344# endif // defined(ANJ_NET_WITH_UDP)
345# if defined(ANJ_NET_WITH_TCP)
347 return anj_tcp_get_bytes_sent(ctx, out_value);
348# endif // defined(ANJ_NET_WITH_TCP)
349# if defined(ANJ_WITH_DTLS_BINDING)
351 return anj_dtls_bytes_sent(ctx, out_value);
352# endif // defined(ANJ_WITH_DTLS_BINDING)
353# if defined(ANJ_WITH_TLS_BINDING)
355 return anj_tls_bytes_sent(ctx, out_value);
356# endif // defined(ANJ_WITH_TLS_BINDING)
357# if defined(ANJ_WITH_NON_IP_BINDING)
359 return anj_non_ip_get_bytes_sent(ctx, out_value);
360# endif // defined(ANJ_WITH_NON_IP_BINDING)
361 default:
362 return ANJ_NET_ENOTSUP;
363 }
364}
365
366// Wrapper for get_inner_mtu
368 anj_net_ctx_t *ctx,
369 int32_t *out_value) {
370 switch (type) {
371# if defined(ANJ_NET_WITH_UDP)
373 return anj_udp_get_inner_mtu(ctx, out_value);
374# endif // defined(ANJ_NET_WITH_UDP)
375# if defined(ANJ_NET_WITH_TCP)
377 return anj_tcp_get_inner_mtu(ctx, out_value);
378# endif // defined(ANJ_NET_WITH_TCP)
379# if defined(ANJ_WITH_DTLS_BINDING)
381 return anj_dtls_get_inner_mtu(ctx, out_value);
382# endif // defined(ANJ_WITH_DTLS_BINDING)
383# if defined(ANJ_WITH_TLS_BINDING)
385 return anj_tls_get_inner_mtu(ctx, out_value);
386# endif // defined(ANJ_WITH_TLS_BINDING)
387# if defined(ANJ_WITH_NON_IP_BINDING)
389 return anj_non_ip_get_inner_mtu(ctx, out_value);
390# endif // defined(ANJ_WITH_NON_IP_BINDING)
391 default:
392 return ANJ_NET_ENOTSUP;
393 }
394}
395
396// Wrapper for get_state
398 anj_net_ctx_t *ctx,
399 anj_net_socket_state_t *out_value) {
400 switch (type) {
401# if defined(ANJ_NET_WITH_UDP)
403 return anj_udp_get_state(ctx, out_value);
404# endif // defined(ANJ_NET_WITH_UDP)
405# if defined(ANJ_NET_WITH_TCP)
407 return anj_tcp_get_state(ctx, out_value);
408# endif // defined(ANJ_NET_WITH_TCP)
409# if defined(ANJ_WITH_DTLS_BINDING)
411 return anj_dtls_get_state(ctx, out_value);
412# endif // defined(ANJ_WITH_DTLS_BINDING)
413# if defined(ANJ_WITH_TLS_BINDING)
415 return anj_tls_get_state(ctx, out_value);
416# endif // defined(ANJ_WITH_TLS_BINDING)
417# if defined(ANJ_WITH_NON_IP_BINDING)
419 return anj_non_ip_get_state(ctx, out_value);
420# endif // defined(ANJ_WITH_NON_IP_BINDING)
421 default:
422 return ANJ_NET_ENOTSUP;
423 }
424}
425
426# ifdef __cplusplus
427}
428# endif
429
430#endif // ANJ_NET_WRAPPER_H
#define ANJ_NET_ENOTSUP
Definition anj_net_api.h:56
anj_net_binding_type_t
Definition anj_net_api.h:58
@ ANJ_NET_BINDING_TCP
Definition anj_net_api.h:60
@ ANJ_NET_BINDING_TLS
Definition anj_net_api.h:62
@ ANJ_NET_BINDING_NON_IP
Definition anj_net_api.h:63
@ ANJ_NET_BINDING_DTLS
Definition anj_net_api.h:61
@ ANJ_NET_BINDING_UDP
Definition anj_net_api.h:59
struct anj_net_ctx_struct anj_net_ctx_t
Definition anj_net_api.h:386
anj_net_socket_state_t
Definition anj_net_api.h:66
static int anj_net_get_bytes_sent(anj_net_binding_type_t type, anj_net_ctx_t *ctx, uint64_t *out_value)
Definition anj_net_wrapper.h:337
static int anj_net_get_bytes_received(anj_net_binding_type_t type, anj_net_ctx_t *ctx, uint64_t *out_value)
Definition anj_net_wrapper.h:307
static int anj_net_get_state(anj_net_binding_type_t type, anj_net_ctx_t *ctx, anj_net_socket_state_t *out_value)
Definition anj_net_wrapper.h:397
static const void * anj_net_get_system_socket(anj_net_binding_type_t type, anj_net_ctx_t *ctx)
Definition anj_net_wrapper.h:37
static int anj_net_create_ctx(anj_net_binding_type_t type, anj_net_ctx_t **ctx, const anj_net_config_t *config)
Definition anj_net_wrapper.h:66
static int anj_net_shutdown(anj_net_binding_type_t type, anj_net_ctx_t *ctx)
Definition anj_net_wrapper.h:220
static int anj_net_cleanup_ctx(anj_net_binding_type_t type, anj_net_ctx_t **ctx)
Definition anj_net_wrapper.h:249
static int anj_net_connect(anj_net_binding_type_t type, anj_net_ctx_t *ctx, const char *hostname, const char *port)
Definition anj_net_wrapper.h:96
static int anj_net_close(anj_net_binding_type_t type, anj_net_ctx_t *ctx)
Definition anj_net_wrapper.h:191
static int anj_net_recv(anj_net_binding_type_t type, anj_net_ctx_t *ctx, size_t *bytes_received, uint8_t *buf, size_t length)
Definition anj_net_wrapper.h:159
static int anj_net_get_inner_mtu(anj_net_binding_type_t type, anj_net_ctx_t *ctx, int32_t *out_value)
Definition anj_net_wrapper.h:367
static int anj_net_send(anj_net_binding_type_t type, anj_net_ctx_t *ctx, size_t *bytes_sent, const uint8_t *buf, size_t length)
Definition anj_net_wrapper.h:127
static int anj_net_reuse_last_port(anj_net_binding_type_t type, anj_net_ctx_t *ctx)
Definition anj_net_wrapper.h:278
anj_net_recv_t anj_tcp_recv
Definition anj_tcp.h:28
anj_net_get_state_t anj_tcp_get_state
Definition anj_tcp.h:36
anj_net_connect_t anj_tcp_connect
Definition anj_tcp.h:25
anj_net_create_ctx_t anj_tcp_create_ctx
Definition anj_tcp.h:26
anj_net_get_system_socket_t anj_tcp_get_system_socket
Definition anj_tcp.h:23
anj_net_get_bytes_sent_t anj_tcp_get_bytes_sent
Definition anj_tcp.h:34
anj_net_get_inner_mtu_t anj_tcp_get_inner_mtu
Definition anj_tcp.h:35
anj_net_close_t anj_tcp_close
Definition anj_tcp.h:24
anj_net_get_bytes_received_t anj_tcp_get_bytes_received
Definition anj_tcp.h:33
anj_net_shutdown_t anj_tcp_shutdown
Definition anj_tcp.h:29
anj_net_send_t anj_tcp_send
Definition anj_tcp.h:27
anj_net_reuse_last_port_t anj_tcp_reuse_last_port
Definition anj_tcp.h:31
anj_net_cleanup_ctx_t anj_tcp_cleanup_ctx
Definition anj_tcp.h:30
anj_net_reuse_last_port_t anj_udp_reuse_last_port
Definition anj_udp.h:31
anj_net_get_bytes_received_t anj_udp_get_bytes_received
Definition anj_udp.h:33
anj_net_send_t anj_udp_send
Definition anj_udp.h:27
anj_net_connect_t anj_udp_connect
Definition anj_udp.h:25
anj_net_get_bytes_sent_t anj_udp_get_bytes_sent
Definition anj_udp.h:34
anj_net_close_t anj_udp_close
Definition anj_udp.h:24
anj_net_get_system_socket_t anj_udp_get_system_socket
Definition anj_udp.h:23
anj_net_create_ctx_t anj_udp_create_ctx
Definition anj_udp.h:26
anj_net_recv_t anj_udp_recv
Definition anj_udp.h:28
anj_net_cleanup_ctx_t anj_udp_cleanup_ctx
Definition anj_udp.h:30
anj_net_get_state_t anj_udp_get_state
Definition anj_udp.h:36
anj_net_shutdown_t anj_udp_shutdown
Definition anj_udp.h:29
anj_net_get_inner_mtu_t anj_udp_get_inner_mtu
Definition anj_udp.h:35
Definition anj_net_api.h:378