Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
service.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_NET_HTTP_SERVICE_H_
8#define ZEPHYR_INCLUDE_NET_HTTP_SERVICE_H_
9
22#include <stdint.h>
23#include <stddef.h>
24
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
36 const char *resource;
38 void *detail;
39};
40
58#define HTTP_RESOURCE_DEFINE(_name, _service, _resource, _detail) \
59 const STRUCT_SECTION_ITERABLE_ALTERNATE(http_resource_desc_##_service, http_resource_desc, \
60 _name) = { \
61 .resource = _resource, \
62 .detail = (void *)(_detail), \
63 }
64
67struct http_service_desc {
68 const char *host;
69 uint16_t *port;
70 int *fd;
71 void *detail;
72 size_t concurrent;
73 size_t backlog;
74 struct http_resource_desc *res_begin;
75 struct http_resource_desc *res_end;
76#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
77 const sec_tag_t *sec_tag_list;
78 size_t sec_tag_list_size;
79#endif
80};
81
82#define __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, _res_begin, \
83 _res_end, ...) \
84 static int _name##_fd = -1; \
85 const STRUCT_SECTION_ITERABLE(http_service_desc, _name) = { \
86 .host = _host, \
87 .port = (uint16_t *)(_port), \
88 .fd = &_name##_fd, \
89 .detail = (void *)(_detail), \
90 .concurrent = (_concurrent), \
91 .backlog = (_backlog), \
92 .res_begin = (_res_begin), \
93 .res_end = (_res_end), \
94 COND_CODE_1(CONFIG_NET_SOCKETS_SOCKOPT_TLS, \
95 (.sec_tag_list = COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (NULL), \
96 (GET_ARG_N(1, __VA_ARGS__))),), ()) \
97 COND_CODE_1(CONFIG_NET_SOCKETS_SOCKOPT_TLS, \
98 (.sec_tag_list_size = COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (0),\
99 (GET_ARG_N(1, GET_ARGS_LESS_N(1, __VA_ARGS__))))), ())\
100 }
101
123#define HTTP_SERVICE_DEFINE_EMPTY(_name, _host, _port, _concurrent, _backlog, _detail) \
124 __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, NULL, NULL)
125
147#define HTTPS_SERVICE_DEFINE_EMPTY(_name, _host, _port, _concurrent, _backlog, _detail, \
148 _sec_tag_list, _sec_tag_list_size) \
149 __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, NULL, NULL, \
150 _sec_tag_list, _sec_tag_list_size); \
151 BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS), \
152 "TLS is required for HTTP secure (CONFIG_NET_SOCKETS_SOCKOPT_TLS)")
153
173#define HTTP_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, _detail) \
174 extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_start)[]; \
175 extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_end)[]; \
176 __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
177 &_CONCAT(_http_resource_desc_##_name, _list_start)[0], \
178 &_CONCAT(_http_resource_desc_##_name, _list_end)[0])
179
201#define HTTPS_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, _detail, \
202 _sec_tag_list, _sec_tag_list_size) \
203 extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_start)[]; \
204 extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_end)[]; \
205 __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
206 &_CONCAT(_http_resource_desc_##_name, _list_start)[0], \
207 &_CONCAT(_http_resource_desc_##_name, _list_end)[0], \
208 _sec_tag_list, _sec_tag_list_size); \
209 BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS), \
210 "TLS is required for HTTP secure (CONFIG_NET_SOCKETS_SOCKOPT_TLS)")
211
217#define HTTP_SERVICE_COUNT(_dst) STRUCT_SECTION_COUNT(http_service_desc, _dst)
218
224#define HTTP_SERVICE_RESOURCE_COUNT(_service) ((_service)->res_end - (_service)->res_begin)
225
231#define HTTP_SERVICE_FOREACH(_it) STRUCT_SECTION_FOREACH(http_service_desc, _it)
232
241#define HTTP_RESOURCE_FOREACH(_service, _it) \
242 STRUCT_SECTION_FOREACH_ALTERNATE(http_resource_desc_##_service, http_resource_desc, _it)
243
253#define HTTP_SERVICE_FOREACH_RESOURCE(_service, _it) \
254 for (struct http_resource_desc *_it = (_service)->res_begin; ({ \
255 __ASSERT(_it <= (_service)->res_end, "unexpected list end location"); \
256 _it < (_service)->res_end; \
257 }); \
258 _it++)
259
260#ifdef __cplusplus
261}
262#endif
263
268#endif /* ZEPHYR_INCLUDE_NET_HTTP_SERVICE_H_ */
int sec_tag_t
Secure tag, a reference to TLS credential.
Definition tls_credentials.h:74
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
HTTP resource description.
Definition service.h:34
const char * resource
Resource name.
Definition service.h:36
void * detail
Detail associated with this resource.
Definition service.h:38
TLS credentials management.
Macro utilities.