Wyatt Hepler | ddfc077 | 2021-09-03 16:46:25 -0700 | [diff] [blame] | 1 | // Copyright 2021 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 | |
Ewout van Bekkum | 7f5b305 | 2021-11-11 17:35:23 -0800 | [diff] [blame] | 15 | // clang-format off |
| 16 | #include "pw_rpc/internal/log_config.h" // PW_LOG_* macros must be first. |
| 17 | |
Wyatt Hepler | ddfc077 | 2021-09-03 16:46:25 -0700 | [diff] [blame] | 18 | #include "pw_rpc/internal/endpoint.h" |
Ewout van Bekkum | 7f5b305 | 2021-11-11 17:35:23 -0800 | [diff] [blame] | 19 | // clang-format on |
Wyatt Hepler | ddfc077 | 2021-09-03 16:46:25 -0700 | [diff] [blame] | 20 | |
| 21 | #include "pw_log/log.h" |
Wyatt Hepler | c8bdc7d | 2021-10-20 09:29:01 -0700 | [diff] [blame] | 22 | #include "pw_rpc/internal/lock.h" |
Wyatt Hepler | ddfc077 | 2021-09-03 16:46:25 -0700 | [diff] [blame] | 23 | |
| 24 | namespace pw::rpc::internal { |
| 25 | |
Wyatt Hepler | c8bdc7d | 2021-10-20 09:29:01 -0700 | [diff] [blame] | 26 | RpcLock& rpc_lock() { |
| 27 | static RpcLock lock; |
| 28 | return lock; |
| 29 | } |
| 30 | |
Wyatt Hepler | ddfc077 | 2021-09-03 16:46:25 -0700 | [diff] [blame] | 31 | Endpoint::~Endpoint() { |
| 32 | // Since the calls remove themselves from the Endpoint in |
| 33 | // CloseAndSendResponse(), close responders until no responders remain. |
| 34 | while (!calls_.empty()) { |
| 35 | calls_.front().CloseAndSendResponse(OkStatus()).IgnoreError(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | Result<Packet> Endpoint::ProcessPacket(std::span<const std::byte> data, |
Wyatt Hepler | 1d000cc | 2021-10-20 11:05:06 -0700 | [diff] [blame] | 40 | Packet::Destination destination) { |
Wyatt Hepler | ddfc077 | 2021-09-03 16:46:25 -0700 | [diff] [blame] | 41 | Result<Packet> result = Packet::FromBuffer(data); |
| 42 | |
| 43 | if (!result.ok()) { |
| 44 | PW_LOG_WARN("Failed to decode pw_rpc packet"); |
| 45 | return Status::DataLoss(); |
| 46 | } |
| 47 | |
| 48 | Packet& packet = *result; |
| 49 | |
| 50 | if (packet.channel_id() == Channel::kUnassignedChannelId || |
| 51 | packet.service_id() == 0 || packet.method_id() == 0) { |
| 52 | PW_LOG_WARN("Received malformed pw_rpc packet"); |
| 53 | return Status::DataLoss(); |
| 54 | } |
| 55 | |
| 56 | if (packet.destination() != destination) { |
| 57 | return Status::InvalidArgument(); |
| 58 | } |
| 59 | |
Wyatt Hepler | ddfc077 | 2021-09-03 16:46:25 -0700 | [diff] [blame] | 60 | return result; |
| 61 | } |
| 62 | |
Wyatt Hepler | 960f5df | 2021-09-08 10:17:21 -0700 | [diff] [blame] | 63 | void Endpoint::RegisterCall(Call& call) { |
Wyatt Hepler | 16edd40 | 2022-01-06 17:25:39 -0800 | [diff] [blame] | 64 | rpc_lock().lock(); |
Wyatt Hepler | c8bdc7d | 2021-10-20 09:29:01 -0700 | [diff] [blame] | 65 | |
Wyatt Hepler | b1c4799 | 2022-01-06 11:05:50 -0800 | [diff] [blame] | 66 | Call* const existing_call = FindCallById( |
| 67 | call.channel_id_locked(), call.service_id(), call.method_id()); |
Wyatt Hepler | 960f5df | 2021-09-08 10:17:21 -0700 | [diff] [blame] | 68 | |
Wyatt Hepler | 960f5df | 2021-09-08 10:17:21 -0700 | [diff] [blame] | 69 | RegisterUniqueCall(call); |
Wyatt Hepler | a06939d | 2022-01-06 09:55:18 -0800 | [diff] [blame] | 70 | |
| 71 | if (existing_call != nullptr) { |
| 72 | existing_call->ReplaceWithNewInstance(call); |
| 73 | } else { |
| 74 | rpc_lock().unlock(); |
| 75 | } |
Wyatt Hepler | ddfc077 | 2021-09-03 16:46:25 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Channel* Endpoint::GetInternalChannel(uint32_t id) const { |
| 79 | for (Channel& c : channels_) { |
| 80 | if (c.id() == id) { |
| 81 | return &c; |
| 82 | } |
| 83 | } |
| 84 | return nullptr; |
| 85 | } |
| 86 | |
| 87 | Channel* Endpoint::AssignChannel(uint32_t id, ChannelOutput& interface) { |
| 88 | internal::Channel* channel = |
| 89 | GetInternalChannel(Channel::kUnassignedChannelId); |
| 90 | if (channel == nullptr) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
| 94 | *channel = Channel(id, &interface); |
| 95 | return channel; |
| 96 | } |
| 97 | |
Wyatt Hepler | 960f5df | 2021-09-08 10:17:21 -0700 | [diff] [blame] | 98 | Call* Endpoint::FindCallById(uint32_t channel_id, |
| 99 | uint32_t service_id, |
| 100 | uint32_t method_id) { |
| 101 | for (Call& call : calls_) { |
Wyatt Hepler | b1c4799 | 2022-01-06 11:05:50 -0800 | [diff] [blame] | 102 | if (channel_id == call.channel_id_locked() && |
| 103 | service_id == call.service_id() && method_id == call.method_id()) { |
Wyatt Hepler | 960f5df | 2021-09-08 10:17:21 -0700 | [diff] [blame] | 104 | return &call; |
| 105 | } |
| 106 | } |
| 107 | return nullptr; |
| 108 | } |
| 109 | |
Wyatt Hepler | ddfc077 | 2021-09-03 16:46:25 -0700 | [diff] [blame] | 110 | } // namespace pw::rpc::internal |