xref: /aosp_15_r20/external/pigweed/pw_system/system_async_host_example.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 <arpa/inet.h>
16 #include <netdb.h>
17 #include <netinet/in.h>
18 #include <poll.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 #include <cstddef>
24 
25 #include "pw_assert/check.h"
26 #include "pw_channel/epoll_channel.h"
27 #include "pw_log/log.h"
28 #include "pw_multibuf/simple_allocator_for_test.h"
29 #include "pw_system/system.h"
30 
31 namespace pw {
32 namespace {
33 
34 constexpr int kInvalidFd = -1;
35 
WaitForTcpConnection(uint16_t port)36 int WaitForTcpConnection(uint16_t port) {
37   int listener_fd = socket(AF_INET6, SOCK_STREAM, 0);
38   if (listener_fd < 0) {
39     PW_LOG_ERROR("socket failed: %s", std::strerror(errno));
40     return kInvalidFd;
41   }
42 
43   // Allow binding to an address that may still be in use by a closed socket.
44   constexpr int value = 1;
45   setsockopt(listener_fd,
46              SOL_SOCKET,
47              SO_REUSEADDR,
48              reinterpret_cast<const char*>(&value),
49              sizeof(int));
50 
51   if (port != 0) {
52     struct sockaddr_in6 addr = {};
53     socklen_t addr_len = sizeof(addr);
54     addr.sin6_family = AF_INET6;
55     addr.sin6_port = htons(port);
56     addr.sin6_addr = in6addr_any;
57     if (bind(listener_fd, reinterpret_cast<sockaddr*>(&addr), addr_len) < 0) {
58       close(listener_fd);
59       PW_LOG_ERROR("bind failed: %s", std::strerror(errno));
60       return kInvalidFd;
61     }
62   }
63 
64   if (listen(listener_fd, 1) < 0) {
65     close(listener_fd);
66     PW_LOG_ERROR("listen failed: %s", std::strerror(errno));
67     return kInvalidFd;
68   }
69 
70   struct sockaddr_in6 sockaddr_client_ = {};
71   socklen_t len = sizeof(sockaddr_client_);
72 
73   int connection =
74       accept(listener_fd, reinterpret_cast<sockaddr*>(&sockaddr_client_), &len);
75   if (connection < 0) {
76     PW_LOG_ERROR("accept failed: %s", std::strerror(errno));
77     return kInvalidFd;
78   }
79   return connection;
80 }
81 
82 }  // namespace
83 }  // namespace pw
84 
85 constexpr uint16_t kPort = 33000;  // This should be configurable.
86 
main()87 int main() {
88   pw::multibuf::test::SimpleAllocatorForTest<4096, 4096> mb_alloc;
89   PW_LOG_INFO("Waiting for TCP connection on port %hu", kPort);
90   int socket_fd = pw::WaitForTcpConnection(kPort);
91   PW_CHECK_INT_NE(socket_fd, pw::kInvalidFd);
92   PW_LOG_INFO("Connected; socket descriptor %d", socket_fd);
93 
94   pw::channel::EpollChannel channel(
95       socket_fd, pw::System().dispatcher(), mb_alloc);
96   PW_CHECK(channel.is_read_or_write_open());
97 
98   pw::SystemStart(channel.channel());
99   return 0;
100 }
101