1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
16 #include "pw_checksum/crc32.h"
17 #include "pw_stream/stream.h"
19 namespace pw::hdlc::internal {
21 // Encodes and writes HDLC frames.
24 constexpr Encoder(stream::Writer& output) : writer_(output) {}
26 // Writes the header for an I-frame. After successfully calling
27 // StartInformationFrame, WriteData may be called any number of times.
28 [[maybe_unused]] Status StartInformationFrame(uint8_t address);
30 // Writes the header for an U-frame. After successfully calling
31 // StartUnnumberedFrame, WriteData may be called any number of times.
32 Status StartUnnumberedFrame(uint8_t address);
34 // Writes data for an ongoing frame. Must only be called after a successful
35 // StartInformationFrame call, and prior to a FinishFrame() call.
36 Status WriteData(ConstByteSpan data);
38 // Finishes a frame. Writes the frame check sequence and a terminating flag.
41 // Runs a pass through a payload, returning the worst-case encoded size for a
42 // frame containing it. Does not calculate CRC to improve efficiency.
43 static size_t MaxEncodedSize(uint8_t address, ConstByteSpan payload);
46 stream::Writer& writer_;
50 } // namespace pw::hdlc::internal