• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 // clang-format off
16 #include "pw_rpc/internal/log_config.h"  // PW_LOG_* macros must be first.
17 
18 #include "pw_rpc/internal/endpoint.h"
19 // clang-format on
20 
21 #include "pw_log/log.h"
22 #include "pw_rpc/internal/lock.h"
23 
24 namespace pw::rpc::internal {
25 
rpc_lock()26 RpcLock& rpc_lock() {
27   static RpcLock lock;
28   return lock;
29 }
30 
~Endpoint()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 
ProcessPacket(std::span<const std::byte> data,Packet::Destination destination)39 Result<Packet> Endpoint::ProcessPacket(std::span<const std::byte> data,
40                                        Packet::Destination destination) {
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 
60   return result;
61 }
62 
RegisterCall(Call & call)63 void Endpoint::RegisterCall(Call& call) {
64   Call* const existing_call = FindCallById(
65       call.channel_id_locked(), call.service_id(), call.method_id());
66 
67   RegisterUniqueCall(call);
68 
69   if (existing_call != nullptr) {
70     // TODO(pwbug/597): Ensure call object is locked when calling callback. For
71     //     on_error, could potentially move the callback and call it after the
72     //     lock is released.
73     existing_call->HandleError(Status::Cancelled());
74     rpc_lock().lock();
75   }
76 }
77 
FindCallById(uint32_t channel_id,uint32_t service_id,uint32_t method_id)78 Call* Endpoint::FindCallById(uint32_t channel_id,
79                              uint32_t service_id,
80                              uint32_t method_id) {
81   for (Call& call : calls_) {
82     if (channel_id == call.channel_id_locked() &&
83         service_id == call.service_id() && method_id == call.method_id()) {
84       return &call;
85     }
86   }
87   return nullptr;
88 }
89 
CloseChannel(uint32_t channel_id)90 Status Endpoint::CloseChannel(uint32_t channel_id) {
91   LockGuard lock(rpc_lock());
92 
93   Channel* channel = channels_.Get(channel_id);
94   if (channel == nullptr) {
95     return Status::NotFound();
96   }
97   channel->Close();
98 
99   // Close pending calls on the channel that's going away.
100   auto previous = calls_.before_begin();
101   auto current = calls_.begin();
102 
103   while (current != calls_.end()) {
104     if (channel_id == current->channel_id_locked()) {
105       current->HandleChannelClose();
106       current = calls_.erase_after(previous);  // previous stays the same
107     } else {
108       previous = current;
109       ++current;
110     }
111   }
112 
113   return OkStatus();
114 }
115 
116 }  // namespace pw::rpc::internal
117