• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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 <cstdio>
16 #include <mutex>
17 
18 #include "pw_assert/check.h"
19 #include "pw_stream/socket_stream.h"
20 #include "pw_stream/stream.h"
21 #include "pw_system/config.h"
22 #include "pw_system/io.h"
23 
24 namespace pw::system {
25 namespace {
26 
27 constexpr uint16_t kPort = PW_SYSTEM_SOCKET_IO_PORT;
28 
GetStream()29 stream::SocketStream& GetStream() {
30   static bool running = false;
31   static std::mutex socket_open_lock;
32   static stream::SocketStream socket_stream;
33   std::lock_guard guard(socket_open_lock);
34   if (!running) {
35     printf("Awaiting connection on port %d\n", static_cast<int>(kPort));
36     PW_CHECK_OK(socket_stream.Serve(kPort));
37     printf("Client connected\n");
38     running = true;
39   }
40   return socket_stream;
41 }
42 
43 }  // namespace
44 
GetReader()45 stream::Reader& GetReader() { return GetStream(); }
GetWriter()46 stream::Writer& GetWriter() { return GetStream(); }
47 
48 }  // namespace pw::system
49