• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // In client and bidirectional RPCs, pw_rpc clients may signal that they have
22 // finished sending requests with a CLIENT_STREAM_END packet. While this can be
23 // useful in some circumstances, it is often not necessary.
24 //
25 // This option controls whether or not include a callback that is called when
26 // the client stream ends. The callback is included in all ServerReader/Writer
27 // objects as a pw::Function, so may have a significant cost.
28 #ifndef PW_RPC_CLIENT_STREAM_END_CALLBACK
29 #define PW_RPC_CLIENT_STREAM_END_CALLBACK 0
30 #endif  // PW_RPC_CLIENT_STREAM_END_CALLBACK
31 
32 // The Nanopb-based pw_rpc implementation allocates memory to use for Nanopb
33 // structs for the request and response protobufs. The template function that
34 // allocates these structs rounds struct sizes up to this value so that
35 // different structs can be allocated with the same function. Structs with sizes
36 // larger than this value cause an extra function to be created, which slightly
37 // increases code size.
38 //
39 // Ideally, this value will be set to the size of the largest Nanopb struct used
40 // as an RPC request or response. The buffer can be stack or globally allocated
41 // (see PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE).
42 #ifndef PW_RPC_NANOPB_STRUCT_MIN_BUFFER_SIZE
43 #define PW_RPC_NANOPB_STRUCT_MIN_BUFFER_SIZE 64
44 #endif  // PW_RPC_NANOPB_STRUCT_MIN_BUFFER_SIZE
45 
46 // Enable global synchronization for RPC calls. If this is set, a backend must
47 // be configured for pw_sync:mutex.
48 #ifndef PW_RPC_USE_GLOBAL_MUTEX
49 #define PW_RPC_USE_GLOBAL_MUTEX 0
50 #endif  // PW_RPC_USE_GLOBAL_MUTEX
51 
52 // Whether pw_rpc should use dynamic memory allocation internally. If enabled,
53 // pw_rpc dynamically allocates channels and its encoding buffers. RPC users may
54 // use dynamic allocation independently of this option (e.g. to allocate pw_rpc
55 // call objects).
56 //
57 // The semantics for allocating and initializing channels change depending on
58 // this option. If dynamic allocation is disabled, pw_rpc endpoints (servers or
59 // clients) use an externally-allocated, fixed-size array of channels.
60 // That array must include unassigned channels or existing channels must be
61 // closed to add new channels.
62 //
63 // If dynamic allocation is enabled, an span of channels may be passed to the
64 // endpoint at construction, but these channels are only used to initialize its
65 // internal std::vector of channels. External channel objects are NOT used by
66 // the endpoint cannot be updated if dynamic allocation is enabled. No
67 // unassigned channels should be passed to the endpoint; they will be ignored.
68 // Any number of channels may be added to the endpoint, without closing existing
69 // channels, but adding channels will use more memory.
70 #ifndef PW_RPC_DYNAMIC_ALLOCATION
71 #define PW_RPC_DYNAMIC_ALLOCATION 0
72 #endif  // PW_RPC_DYNAMIC_ALLOCATION
73 
74 #if PW_RPC_DYNAMIC_ALLOCATION && defined(PW_RPC_ENCODING_BUFFER_SIZE_BYTES)
75 static_assert(false,
76               "PW_RPC_ENCODING_BUFFER_SIZE_BYTES cannot be set if "
77               "PW_RPC_DYNAMIC_ALLOCATION is enabled");
78 #endif  // PW_RPC_DYNAMIC_ALLOCATION && PW_RPC_ENCODING_BUFFER_SIZE_BYTES
79 
80 // Size of the global RPC packet encoding buffer in bytes.
81 #ifndef PW_RPC_ENCODING_BUFFER_SIZE_BYTES
82 #define PW_RPC_ENCODING_BUFFER_SIZE_BYTES 512
83 #endif  // PW_RPC_ENCODING_BUFFER_SIZE_BYTES
84 
85 // The log level to use for this module. Logs below this level are omitted.
86 #ifndef PW_RPC_CONFIG_LOG_LEVEL
87 #define PW_RPC_CONFIG_LOG_LEVEL PW_LOG_LEVEL_INFO
88 #endif  // PW_RPC_CONFIG_LOG_LEVEL
89 
90 // The log module name to use for this module.
91 #ifndef PW_RPC_CONFIG_LOG_MODULE_NAME
92 #define PW_RPC_CONFIG_LOG_MODULE_NAME "PW_RPC"
93 #endif  // PW_RPC_CONFIG_LOG_MODULE_NAME
94 
95 namespace pw::rpc::cfg {
96 
97 template <typename...>
98 constexpr std::bool_constant<PW_RPC_CLIENT_STREAM_END_CALLBACK>
99     kClientStreamEndCallbackEnabled;
100 
101 template <typename...>
102 constexpr std::bool_constant<PW_RPC_DYNAMIC_ALLOCATION>
103     kDynamicAllocationEnabled;
104 
105 inline constexpr size_t kNanopbStructMinBufferSize =
106     PW_RPC_NANOPB_STRUCT_MIN_BUFFER_SIZE;
107 
108 inline constexpr size_t kEncodingBufferSizeBytes =
109     PW_RPC_ENCODING_BUFFER_SIZE_BYTES;
110 
111 #undef PW_RPC_NANOPB_STRUCT_MIN_BUFFER_SIZE
112 #undef PW_RPC_ENCODING_BUFFER_SIZE_BYTES
113 
114 }  // namespace pw::rpc::cfg
115 
116 // This option determines whether to allocate the Nanopb structs on the stack or
117 // in a global variable. Globally allocated structs are NOT thread safe, but
118 // work fine when the RPC server's ProcessPacket function is only called from
119 // one thread.
120 #ifndef PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE
121 #define PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE 1
122 #endif  // PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE
123 
124 #if PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE
125 #define _PW_RPC_NANOPB_STRUCT_STORAGE_CLASS
126 #else
127 #define _PW_RPC_NANOPB_STRUCT_STORAGE_CLASS static
128 #endif  // PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE
129 
130 #undef PW_RPC_NANOPB_STRUCT_BUFFER_STACK_ALLOCATE
131