• 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 #include "pw_multisink/drain.h"
15 
16 #include "pw_assert/light.h"
17 
18 namespace pw {
19 namespace multisink {
20 
GetEntry(ByteSpan entry,uint32_t & drop_count_out)21 Result<ConstByteSpan> Drain::GetEntry(ByteSpan entry,
22                                       uint32_t& drop_count_out) {
23   uint32_t entry_sequence_id = 0;
24   drop_count_out = 0;
25   const Result<ConstByteSpan> result =
26       MultiSink::GetEntry(*this, entry, entry_sequence_id);
27 
28   // Exit immediately if the result isn't OK or OUT_OF_RANGE, as the
29   // entry_sequence_id cannot be used for computation. Later invocations to
30   // GetEntry will permit readers to determine how far the sequence ID moved
31   // forward.
32   if (!result.ok() && !result.status().IsOutOfRange()) {
33     return result;
34   }
35 
36   // Compute the drop count delta by comparing this entry's sequence ID with the
37   // last sequence ID this drain successfully read.
38   //
39   // The drop count calculation simply computes the difference between the
40   // current and last sequence IDs. Consecutive successful reads will always
41   // differ by one at least, so it is subtracted out. If the read was not
42   // successful, the difference is not adjusted.
43   drop_count_out =
44       entry_sequence_id - last_handled_sequence_id_ - (result.ok() ? 1 : 0);
45 
46   last_handled_sequence_id_ = entry_sequence_id;
47   return result;
48 }
49 
50 }  // namespace multisink
51 }  // namespace pw
52