C/C++ API Reference
Loading...
Searching...
No Matches
encoder.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 <cstddef>
17#include <cstdint>
18
19#include "pw_bytes/span.h"
20#include "pw_checksum/crc32.h"
21#include "pw_hdlc/internal/protocol.h"
22#include "pw_status/status.h"
23#include "pw_stream/stream.h"
24
26namespace pw::hdlc {
27
29
63Status WriteUIFrame(uint64_t address,
64 ConstByteSpan payload,
65 stream::Writer& writer);
66
68class Encoder {
69 public:
71 constexpr Encoder(stream::Writer& output) : writer_(output) {}
72
75 Status StartUnnumberedFrame(uint64_t address) {
76 return StartFrame(address, UFrameControl::UnnumberedInformation().data());
77 }
78
82
85
86 private:
87 // Indicates this an information packet with sequence numbers set to 0.
88 static constexpr std::byte kUnusedControl = std::byte{0};
89
90 Status StartFrame(uint64_t address, std::byte control);
91
92 stream::Writer& writer_;
93 checksum::Crc32 fcs_;
94};
95
96} // namespace pw::hdlc
Definition: status.h:86
Encodes and writes HDLC frames.
Definition: encoder.h:68
Definition: stream.h:440
Status WriteUIFrame(uint64_t address, ConstByteSpan payload, stream::Writer &writer)
Writes an HDLC unnumbered information frame (UI frame) to the provided pw::stream writer.
Status FinishFrame()
Finishes a frame. Writes the frame check sequence and a terminating flag.
Status WriteData(ConstByteSpan data)
Status StartUnnumberedFrame(uint64_t address)
Definition: encoder.h:75
constexpr Encoder(stream::Writer &output)
Construct an encoder which will write data to output.
Definition: encoder.h:71
Serial comms library.
Definition: decoder.h:29