xref: /aosp_15_r20/external/pigweed/pw_system/system_async_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 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 <cstddef>
16 
17 #include "pw_async2/dispatcher.h"
18 #include "pw_channel/loopback_channel.h"
19 #include "pw_log/log.h"
20 #include "pw_multibuf/simple_allocator_for_test.h"
21 #include "pw_system/system.h"
22 #include "pw_unit_test/framework.h"
23 
24 namespace {
25 
26 class : public pw::async2::Task {
DoPend(pw::async2::Context &)27   pw::async2::Poll<> DoPend(pw::async2::Context&) override {
28     return pw::async2::Ready();
29   }
30 } my_task;
31 
32 // DOCSTAG: [pw_system-async-example-main]
main()33 int main() {
34   // First, do any required low-level platform initialization.
35 
36   // Initialize a channel to handle pw_system communications, including for
37   // pw_rpc. This example uses LoopbackByteChannel, but a channel that actually
38   // transmits data should be used instead.
39   std::byte channel_buffer[128];
40   pw::multibuf::SimpleAllocator alloc(channel_buffer, pw::System().allocator());
41   pw::channel::LoopbackByteChannel channel(alloc);
42 
43   // Post any async tasks that should run. These will execute after calling
44   // pw::SystemStart.
45   pw::System().dispatcher().Post(my_task);
46 
47   // As needed, start threads to run user code. Or, register a task to start
48   // threads after pw::SystemStart.
49 
50   // When ready, start running the pw::System threads and dispatcher. This
51   // function call never returns.
52   pw::SystemStart(channel.channel());
53 
54   return 0;
55 }
56 // DOCSTAG: [pw_system-async-example-main]
57 
TEST(PwSystem,ReferToExampleMain)58 TEST(PwSystem, ReferToExampleMain) {
59   if (false) {
60     main();
61   }
62 }
63 
64 }  // namespace
65