Pigweed
 
Loading...
Searching...
No Matches
config.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15// Configuration macros for the pw_rpc module.
16#pragma once
17
18#include <cstddef>
19#include <type_traits>
20
21#if defined(PW_RPC_CLIENT_STREAM_END_CALLBACK) && \
22 PW_RPC_CLIENT_STREAM_END_CALLBACK
23#pragma message( \
24 "Warning PW_RPC_CLIENT_STREAM_END_CALLBACK is deprecated! " \
25 "Use PW_RPC_COMPLETION_REQUEST_CALLBACK instead.")
26#define PW_RPC_COMPLETION_REQUEST_CALLBACK 1
27#endif
28
29#undef PW_RPC_CLIENT_STREAM_END_CALLBACK
30
42#ifndef PW_RPC_COMPLETION_REQUEST_CALLBACK
43#define PW_RPC_COMPLETION_REQUEST_CALLBACK 0
44#endif // PW_RPC_COMPLETION_REQUEST_CALLBACK
45
50#ifndef PW_RPC_METHOD_STORES_TYPE
51#define PW_RPC_METHOD_STORES_TYPE 0
52#endif // PW_RPC_METHOD_STORES_TYPE
53
66#ifndef PW_RPC_NANOPB_STRUCT_MIN_BUFFER_SIZE
67#define PW_RPC_NANOPB_STRUCT_MIN_BUFFER_SIZE 64
68#endif // PW_RPC_NANOPB_STRUCT_MIN_BUFFER_SIZE
69
74#ifndef PW_RPC_USE_GLOBAL_MUTEX
75#define PW_RPC_USE_GLOBAL_MUTEX 1
76#endif // PW_RPC_USE_GLOBAL_MUTEX
77
102#ifndef PW_RPC_YIELD_MODE
103#if PW_RPC_USE_GLOBAL_MUTEX == 0
104#define PW_RPC_YIELD_MODE PW_RPC_YIELD_MODE_BUSY_LOOP
105#else
106#define PW_RPC_YIELD_MODE PW_RPC_YIELD_MODE_SLEEP
107#endif // PW_RPC_USE_GLOBAL_MUTEX == 0
108#endif // PW_RPC_YIELD_MODE
109
115// LINT.IfChange
116#define PW_RPC_YIELD_MODE_BUSY_LOOP 100
117#define PW_RPC_YIELD_MODE_SLEEP 101
118#define PW_RPC_YIELD_MODE_YIELD 102
119// LINT.ThenChange(//pw_rpc/BUILD.bazel)
120
125#ifndef PW_RPC_YIELD_SLEEP_DURATION
126
127// When building for a desktop operating system, use a 1ms sleep by default.
128// 1-tick duration sleeps can result in spurious timeouts.
129#if defined(_WIN32) || defined(__APPLE__) || \
130 defined(__linux__) && !defined(__ZEPHYR__)
131#define PW_RPC_YIELD_SLEEP_DURATION std::chrono::milliseconds(1)
132#else
133#define PW_RPC_YIELD_SLEEP_DURATION pw::chrono::SystemClock::duration(1)
134#endif // defined(_WIN32) || defined(__APPLE__) || defined(__linux__)
135 // && !defined(__ZEPHYR__)
136
137#endif // PW_RPC_YIELD_SLEEP_DURATION
138
139// PW_RPC_YIELD_SLEEP_DURATION is not needed for non-sleep yield modes.
140#if PW_RPC_YIELD_MODE != PW_RPC_YIELD_MODE_SLEEP
141#undef PW_RPC_YIELD_SLEEP_DURATION
142#endif // PW_RPC_YIELD_MODE != PW_RPC_YIELD_MODE_SLEEP
143
159#ifndef PW_RPC_CALLBACK_TIMEOUT_TICKS
160#define PW_RPC_CALLBACK_TIMEOUT_TICKS 10000
161#endif // PW_RPC_CALLBACK_TIMEOUT_TICKS
162
181#ifndef PW_RPC_DYNAMIC_ALLOCATION
182#define PW_RPC_DYNAMIC_ALLOCATION 0
183#endif // PW_RPC_DYNAMIC_ALLOCATION
184
185#if defined(PW_RPC_DYNAMIC_CONTAINER) || \
186 defined(PW_RPC_DYNAMIC_CONTAINER_INCLUDE)
187static_assert(
188 PW_RPC_DYNAMIC_ALLOCATION == 1,
189 "PW_RPC_DYNAMIC_ALLOCATION is disabled, so PW_RPC_DYNAMIC_CONTAINER and "
190 "PW_RPC_DYNAMIC_CONTAINER_INCLUDE have no effect and should not be set.");
191#endif // PW_RPC_DYNAMIC_CONTAINER || PW_RPC_DYNAMIC_CONTAINER_INCLUDE
192
207#ifndef PW_RPC_DYNAMIC_CONTAINER
208#define PW_RPC_DYNAMIC_CONTAINER(type) std::vector<type>
209#endif // PW_RPC_DYNAMIC_CONTAINER
210
216#ifndef PW_RPC_DYNAMIC_CONTAINER_INCLUDE
217#define PW_RPC_DYNAMIC_CONTAINER_INCLUDE <vector>
218#endif // PW_RPC_DYNAMIC_CONTAINER_INCLUDE
219
224#ifndef PW_RPC_MAKE_UNIQUE_PTR
225#define PW_RPC_MAKE_UNIQUE_PTR(type, ...) \
226 std::unique_ptr<type>(new type(__VA_ARGS__))
227#endif // PW_RPC_DYNAMIC_CONTAINER
228
232#ifndef PW_RPC_MAKE_UNIQUE_PTR_INCLUDE
233#define PW_RPC_MAKE_UNIQUE_PTR_INCLUDE <memory>
234#endif // PW_RPC_MAKE_UNIQUE_PTR_INCLUDE
235
239#ifndef PW_RPC_ENCODING_BUFFER_SIZE_BYTES
240#define PW_RPC_ENCODING_BUFFER_SIZE_BYTES 512
241#endif // PW_RPC_ENCODING_BUFFER_SIZE_BYTES
242
244#ifndef PW_RPC_CONFIG_LOG_LEVEL
245#define PW_RPC_CONFIG_LOG_LEVEL PW_LOG_LEVEL_INFO
246#endif // PW_RPC_CONFIG_LOG_LEVEL
247
249#ifndef PW_RPC_CONFIG_LOG_MODULE_NAME
250#define PW_RPC_CONFIG_LOG_MODULE_NAME "PW_RPC"
251#endif // PW_RPC_CONFIG_LOG_MODULE_NAME
252
253namespace pw::rpc::cfg {
254
255template <typename...>
256constexpr std::bool_constant<PW_RPC_COMPLETION_REQUEST_CALLBACK>
257 kClientStreamEndCallbackEnabled;
258
259template <typename...>
260constexpr std::bool_constant<PW_RPC_METHOD_STORES_TYPE> kMethodStoresType;
261
262template <typename...>
263constexpr std::bool_constant<PW_RPC_DYNAMIC_ALLOCATION>
264 kDynamicAllocationEnabled;
265
266inline constexpr size_t kNanopbStructMinBufferSize =
267 PW_RPC_NANOPB_STRUCT_MIN_BUFFER_SIZE;
268
269inline constexpr size_t kEncodingBufferSizeBytes =
270 PW_RPC_ENCODING_BUFFER_SIZE_BYTES;
271
272#undef PW_RPC_NANOPB_STRUCT_MIN_BUFFER_SIZE
273#undef PW_RPC_ENCODING_BUFFER_SIZE_BYTES
274
275} // namespace pw::rpc::cfg
276
281#ifndef PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE
282#define PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE 1
283#endif // PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE
284
286#if PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE
287#define _PW_RPC_NANOPB_STRUCT_STORAGE_CLASS
288#else
289#define _PW_RPC_NANOPB_STRUCT_STORAGE_CLASS static
290#endif // PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE
291
292#undef PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE