• 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 <span>
17 #include <vector>
18 
19 #include "pw_rpc/internal/channel.h"
20 #include "pw_rpc/internal/config.h"
21 
22 namespace pw::rpc::internal {
23 
24 #if PW_RPC_DYNAMIC_ALLOCATION && !defined(__cpp_lib_constexpr_vector)
25 #define _PW_RPC_CONSTEXPR
26 #else
27 #define _PW_RPC_CONSTEXPR constexpr
28 #endif  // PW_RPC_DYNAMIC_ALLOCATION && !defined(__cpp_lib_constexpr_vector)
29 
30 class ChannelList {
31  public:
ChannelList(std::span<Channel> channels)32   _PW_RPC_CONSTEXPR ChannelList(std::span<Channel> channels)
33       : channels_(channels.begin(), channels.end()) {}
34 
35   // Returns the first channel with the matching ID or nullptr if none match.
36   // Except for Channel::kUnassignedChannelId, there should be no duplicate
37   // channels.
38   const Channel* Get(uint32_t channel_id) const;
39 
Get(uint32_t channel_id)40   Channel* Get(uint32_t channel_id) {
41     return const_cast<Channel*>(
42         static_cast<const ChannelList&>(*this).Get(channel_id));
43   }
44 
45   // Adds the channel with the requested ID to the list. Returns:
46   //
47   //   OK - the channel was added
48   //   ALREADY_EXISTS - a channel with this ID is already present; remove it
49   //       first
50   //   RESOURCE_EXHAUSTED - no unassigned channels are available; only possible
51   //       if PW_RPC_DYNAMIC_ALLOCATION is disabled
52   //
53   Status Add(uint32_t channel_id, ChannelOutput& output);
54 
55   // Removes the channel with the requested ID. Returns:
56   //
57   //   OK - the channel was removed
58   //   NOT_FOUND - no channel with the provided ID was found
59   //
60   Status Remove(uint32_t channel_id);
61 
62 #if PW_RPC_DYNAMIC_ALLOCATION
63   std::vector<Channel> channels_;
64 #else
65   std::span<Channel> channels_;
66 #endif  // PW_RPC_DYNAMIC_ALLOCATION
67 };
68 
69 }  // namespace pw::rpc::internal
70