• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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 #pragma once
15 
16 #include "pw_rpc/internal/channel.h"
17 #include "pw_rpc/internal/config.h"
18 #include "pw_span/span.h"
19 
20 // With dynamic allocation enabled, include the specified header and don't
21 // require the constructor to be constexpr.
22 #if PW_RPC_DYNAMIC_ALLOCATION
23 
24 #include PW_RPC_DYNAMIC_CONTAINER_INCLUDE
25 
26 #define _PW_RPC_CONSTEXPR
27 
28 #else  // Otherwise, channels are stored in a constexpr constructible span.
29 
30 #define _PW_RPC_CONSTEXPR constexpr
31 
32 #endif  // PW_RPC_DYNAMIC_ALLOCATION
33 
34 namespace pw::rpc::internal {
35 
36 class ChannelList {
37  public:
38   _PW_RPC_CONSTEXPR ChannelList() = default;
39 
ChannelList(span<Channel> channels)40   _PW_RPC_CONSTEXPR ChannelList(span<Channel> channels)
41   // If dynamic allocation is enabled, channels aren't typically allocated
42   // beforehand, though they can be. If they are, push them one-by-one to the
43   // vector to avoid requiring a constructor that does that.
44 #if PW_RPC_DYNAMIC_ALLOCATION
45   {
46     for (const Channel& channel : channels) {
47       channels_.emplace_back(channel);
48     }
49 #else   // Without dynamic allocation, simply initialize the span.
50       : channels_(channels) {
51 #endif  // PW_RPC_DYNAMIC_ALLOCATION
52   }
53 
54   // Returns the first channel with the matching ID or nullptr if none match.
55   // Except for Channel::kUnassignedChannelId, there should be no duplicate
56   // channels.
57   const Channel* Get(uint32_t channel_id) const;
58 
59   Channel* Get(uint32_t channel_id) {
60     return const_cast<Channel*>(
61         static_cast<const ChannelList&>(*this).Get(channel_id));
62   }
63 
64   // Adds the channel with the requested ID to the list. Returns:
65   //
66   //   OK - the channel was added
67   //   ALREADY_EXISTS - a channel with this ID is already present; remove it
68   //       first
69   //   RESOURCE_EXHAUSTED - no unassigned channels are available; only possible
70   //       if PW_RPC_DYNAMIC_ALLOCATION is disabled
71   //
72   Status Add(uint32_t channel_id, ChannelOutput& output);
73 
74   // Removes the channel with the requested ID. Returns:
75   //
76   //   OK - the channel was removed
77   //   NOT_FOUND - no channel with the provided ID was found
78   //
79   Status Remove(uint32_t channel_id);
80 
81 #if PW_RPC_DYNAMIC_ALLOCATION
82   PW_RPC_DYNAMIC_CONTAINER(Channel) channels_;
83 #else
84   span<Channel> channels_;
85 #endif  // PW_RPC_DYNAMIC_ALLOCATION
86 };
87 
88 }  // namespace pw::rpc::internal
89