1 // Copyright 2021 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
20 inline constexpr std::byte kFlag = std::byte{0x7E};
21 inline constexpr std::byte kEscape = std::byte{0x7D};
22 inline constexpr std::byte kEscapeConstant = std::byte{0x20};
24 inline constexpr std::array<std::byte, 2> kEscapedFlag = {kEscape,
26 inline constexpr std::array<std::byte, 2> kEscapedEscape = {kEscape,
29 constexpr bool NeedsEscaping(std::byte b) {
30 return (b == kFlag || b == kEscape);
33 constexpr std::byte Escape(std::byte b) { return b ^ kEscapeConstant; }
35 // Class that manages the 1-byte control field of an HDLC U-frame.
38 static constexpr UFrameControl UnnumberedInformation() {
39 return UFrameControl(kUnnumberedInformation);
42 constexpr std::byte data() const { return data_; }
45 // Types of HDLC U-frames and their bit patterns.
47 kUnnumberedInformation = 0x00,
50 constexpr UFrameControl(Type type)
51 : data_(kUFramePattern | std::byte{type}) {}
53 // U-frames are identified by having the bottom two control bits set.
54 static constexpr std::byte kUFramePattern = std::byte{0x03};
59 } // namespace pw::hdlc