C/C++ API Reference
Loading...
Searching...
No Matches
stream.h
1// Copyright 2020 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#pragma once
15
16#include <cstdint>
17#include <optional>
18
19#include "pw_status/status.h"
20#include "pw_stream/stream.h"
21
22namespace pw::stream {
23
25
28 public:
29 // Linux UART configuration structure.
30 //
31 // Properties are listed as optional - we can allow the user to leave the
32 // default properties un-changed.
33 struct Config {
34 std::optional<uint32_t> baud_rate;
35 std::optional<bool> flow_control;
36 };
37
38 constexpr UartStreamLinux() = default;
39
40 // UartStream objects are moveable but not copyable.
41 UartStreamLinux& operator=(UartStreamLinux&& other) {
42 fd_ = other.fd_;
43 other.fd_ = kInvalidFd;
44 return *this;
45 }
46 UartStreamLinux(UartStreamLinux&& other) noexcept : fd_(other.fd_) {
47 other.fd_ = kInvalidFd;
48 }
49 UartStreamLinux(const UartStreamLinux&) = delete;
50 UartStreamLinux& operator=(const UartStreamLinux&) = delete;
51
52 ~UartStreamLinux() override { Close(); }
53
72 Status Open(const char* path, uint32_t baud_rate) {
73 return Open(path, {.baud_rate = baud_rate, .flow_control = std::nullopt});
74 }
75
94 Status Open(const char* path, Config config);
95
96 void Close();
97
98 private:
99 static constexpr int kInvalidFd = -1;
100
103
104 int fd_ = kInvalidFd;
105};
106
107} // namespace pw::stream
Definition: status.h:109
Definition: status_with_size.h:51
Definition: stream.h:650
pw::stream::NonSeekableReaderWriter implementation for UARTs on Linux.
Definition: stream.h:27
Status DoWrite(ConstByteSpan data) override
Virtual Write() function implemented by derived classes.
Status Open(const char *path, uint32_t baud_rate)
Definition: stream.h:72
StatusWithSize DoRead(ByteSpan dest) override
Virtual Read() function implemented by derived classes.
Status Open(const char *path, Config config)