• 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 #include "pw_bloat/bloat_this_binary.h"
16 #include "pw_ring_buffer/prefixed_entry_ring_buffer.h"
17 #include "pw_status/status.h"
18 
19 constexpr size_t kRingBufferSize = 1024;
20 constexpr size_t kReaderCount = 4;
21 constexpr std::byte kValue = (std::byte)0xFF;
22 constexpr std::byte kData[1] = {kValue};
23 
main()24 int main() {
25   pw::bloat::BloatThisBinary();
26 
27   pw::ring_buffer::PrefixedEntryRingBufferMulti ring(true /* user_preamble */);
28   std::byte buffer[kRingBufferSize];
29 
30   pw::Status status = ring.SetBuffer(buffer);
31   if (!status.ok()) {
32     return 1;
33   }
34 
35   // Attach readers.
36   pw::ring_buffer::PrefixedEntryRingBufferMulti::Reader readers[kReaderCount];
37   for (auto& reader : readers) {
38     ring.AttachReader(reader)
39         .IgnoreError();  // TODO(b/242598609): Handle Status properly
40   }
41 
42   // Push entries until the buffer is full.
43   size_t total_entries = 0;
44   while (true) {
45     status = ring.TryPushBack(kData);
46     if (status == pw::Status::ResourceExhausted()) {
47       break;
48     } else if (!status.ok()) {
49       return 2;
50     }
51     total_entries++;
52   }
53 
54   // Forcefully push an entry.
55   status = ring.PushBack(kData);
56   if (!status.ok()) {
57     return 3;
58   }
59 
60   // Dering the buffer.
61   status = ring.Dering();
62   if (!status.ok()) {
63     return 4;
64   }
65 
66   // Peek and pop all entries.
67   __attribute__((unused)) std::byte value[1];
68   __attribute__((unused)) size_t value_size;
69   for (size_t i = 0; i < total_entries; ++i) {
70     for (auto& reader : readers) {
71       status = reader.PeekFront(value, &value_size);
72       if (!status.ok()) {
73         return 5;
74       }
75       status = reader.PeekFrontWithPreamble(value, &value_size);
76       if (!status.ok()) {
77         return 6;
78       }
79       if (reader.FrontEntryDataSizeBytes() == 0) {
80         return 7;
81       }
82       if (reader.FrontEntryTotalSizeBytes() == 0) {
83         return 8;
84       }
85       if (reader.EntryCount() == 0) {
86         return 9;
87       }
88       status = reader.PopFront();
89       if (!status.ok()) {
90         return 10;
91       }
92     }
93   }
94 
95   for (auto& reader : readers) {
96     ring.DetachReader(reader)
97         .IgnoreError();  // TODO(b/242598609): Handle Status properly
98   }
99   ring.Clear();
100   return 0;
101 }
102