Pigweed
 
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
60Status WriteUIFrame(uint64_t address,
61 ConstByteSpan payload,
62 stream::Writer& writer);
63
65class Encoder {
66 public:
68 constexpr Encoder(stream::Writer& output) : writer_(output) {}
69
72 Status StartUnnumberedFrame(uint64_t address) {
73 return StartFrame(address, UFrameControl::UnnumberedInformation().data());
74 }
75
78 Status WriteData(ConstByteSpan data);
79
82
83 private:
84 // Indicates this an information packet with sequence numbers set to 0.
85 static constexpr std::byte kUnusedControl = std::byte{0};
86
87 Status StartFrame(uint64_t address, std::byte control);
88
89 stream::Writer& writer_;
90 checksum::Crc32 fcs_;
91};
92
93} // namespace pw::hdlc
Definition: status.h:85
Encodes and writes HDLC frames.
Definition: encoder.h:65
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:72
constexpr Encoder(stream::Writer &output)
Construct an encoder which will write data to output.
Definition: encoder.h:68
Definition: stream.h:430