1 // Copyright 2021 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
18 #include "pw_varint/varint.h"
19
20 namespace pw::hdlc {
21
22 inline constexpr std::byte kFlag = std::byte{0x7E};
23 inline constexpr std::byte kEscape = std::byte{0x7D};
24 inline constexpr std::byte kEscapeConstant = std::byte{0x20};
25
26 inline constexpr std::array<std::byte, 2> kEscapedFlag = {kEscape,
27 std::byte{0x5E}};
28 inline constexpr std::array<std::byte, 2> kEscapedEscape = {kEscape,
29 std::byte{0x5D}};
30
31 // This implementation also only supports addresses up to the maximum value of a
32 // 64-bit unsigned integer.
33 inline constexpr size_t kMinAddressSize = 1;
34 inline constexpr size_t kMaxAddressSize =
35 varint::EncodedSize(std::numeric_limits<uint64_t>::max());
36
37 // The biggest on-the-wire size of a FCS after HDLC escaping. If, for example,
38 // the FCS is 0x7e7e7e7e the encoded value will be:
39 // [ 0x7e, 0x5e, 0x7e, 0x5e, 0x7e, 0x5e, 0x7e, 0x5e ]
40 inline constexpr size_t kMaxEscapedFcsSize = 8;
41
42 // The biggest on-the-wire size of an HDLC address after escaping. This number
43 // is not just kMaxAddressSize * 2 because the kFlag and kEscape bytes are not
44 // valid terminating bytes of a one-terminated least significant varint. This
45 // means that the practical biggest varint BEFORE escaping looks like this:
46 // [ 0x7e, 0x7e, 0x7e, 0x7e, 0x7e, 0x7e, 0x7e, 0x7e, 0x7e, 0x03 ]
47 // The first nine bytes will be escaped, but the tenth never will be escaped
48 // since any values other than 0x03 would cause a uint64_t to overflow.
49 inline constexpr size_t kMaxEscapedVarintAddressSize =
50 (kMaxAddressSize * 2) - 1;
51
52 // This implementation does not support extended control fields.
53 inline constexpr size_t kControlSize = 1;
54
55 // The control byte may need to be escaped. When it is, this is its maximum
56 // size.
57 inline constexpr size_t kMaxEscapedControlSize = 2;
58
59 // This implementation only supports a 32-bit CRC-32 Frame Check Sequence (FCS).
60 inline constexpr size_t kFcsSize = sizeof(uint32_t);
61
62 inline constexpr varint::Format kAddressFormat =
63 varint::Format::kOneTerminatedLeastSignificant;
64
NeedsEscaping(std::byte b)65 constexpr bool NeedsEscaping(std::byte b) {
66 return (b == kFlag || b == kEscape);
67 }
68
Escape(std::byte b)69 constexpr std::byte Escape(std::byte b) { return b ^ kEscapeConstant; }
70
71 // Class that manages the 1-byte control field of an HDLC U-frame.
72 class UFrameControl {
73 public:
UnnumberedInformation()74 static constexpr UFrameControl UnnumberedInformation() {
75 return UFrameControl(kUnnumberedInformation);
76 }
77
data()78 constexpr std::byte data() const { return data_; }
79
80 private:
81 // Types of HDLC U-frames and their bit patterns.
82 enum Type : uint8_t {
83 kUnnumberedInformation = 0x00,
84 };
85
UFrameControl(Type type)86 constexpr UFrameControl(Type type)
87 : data_(kUFramePattern | std::byte{type}) {}
88
89 // U-frames are identified by having the bottom two control bits set.
90 static constexpr std::byte kUFramePattern = std::byte{0x03};
91
92 std::byte data_;
93 };
94
95 } // namespace pw::hdlc
96