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
25namespace pw::hdlc {
26
28
56Status WriteUIFrame(uint64_t address,
57 ConstByteSpan payload,
58 stream::Writer& writer);
59
61class Encoder {
62 public:
64 constexpr Encoder(stream::Writer& output) : writer_(output) {}
65
68 Status StartUnnumberedFrame(uint64_t address) {
69 return StartFrame(address, UFrameControl::UnnumberedInformation().data());
70 }
71
75
78
79 private:
80 // Indicates this an information packet with sequence numbers set to 0.
81 static constexpr std::byte kUnusedControl = std::byte{0};
82
83 Status StartFrame(uint64_t address, std::byte control);
84
85 stream::Writer& writer_;
86 checksum::Crc32 fcs_;
87};
88
89} // namespace pw::hdlc
Definition: status.h:120
Encodes and writes HDLC frames.
Definition: encoder.h:61
Definition: stream.h:415
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:68
constexpr Encoder(stream::Writer &output)
Constructs an encoder which will write data to output.
Definition: encoder.h:64