Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
26 public:
27 // Linux UART configuration structure.
28 //
29 // Properties are listed as optional - we can allow the user to leave the
30 // default properties un-changed.
31 struct Config {
32 std::optional<uint32_t> baud_rate;
33 std::optional<bool> flow_control;
34 };
35
36 constexpr UartStreamLinux() = default;
37
38 // UartStream objects are moveable but not copyable.
39 UartStreamLinux& operator=(UartStreamLinux&& other) {
40 fd_ = other.fd_;
41 other.fd_ = kInvalidFd;
42 return *this;
43 }
44 UartStreamLinux(UartStreamLinux&& other) noexcept : fd_(other.fd_) {
45 other.fd_ = kInvalidFd;
46 }
47 UartStreamLinux(const UartStreamLinux&) = delete;
48 UartStreamLinux& operator=(const UartStreamLinux&) = delete;
49
50 ~UartStreamLinux() override { Close(); }
51
70 Status Open(const char* path, uint32_t baud_rate) {
71 return Open(path, {.baud_rate = baud_rate, .flow_control = std::nullopt});
72 }
73
92 Status Open(const char* path, Config config);
93
94 void Close();
95
96 private:
97 static constexpr int kInvalidFd = -1;
98
99 Status DoWrite(ConstByteSpan data) override;
100 StatusWithSize DoRead(ByteSpan dest) override;
101
102 int fd_ = kInvalidFd;
103};
104
105} // namespace pw::stream
Definition: status.h:85
Definition: status_with_size.h:49
Definition: stream.h:636
pw::stream::NonSeekableReaderWriter implementation for UARTs on Linux.
Definition: stream.h:25
Status DoWrite(ConstByteSpan data) override
Virtual Write() function implemented by derived classes.
Status Open(const char *path, uint32_t baud_rate)
Definition: stream.h:70
StatusWithSize DoRead(ByteSpan dest) override
Virtual Read() function implemented by derived classes.
Status Open(const char *path, Config config)