Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_hdlc / pw_hdlc_private / protocol.h
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 namespace pw::hdlc {
19
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};
23
24 inline constexpr std::array<std::byte, 2> kEscapedFlag = {kEscape,
25                                                           std::byte{0x5E}};
26 inline constexpr std::array<std::byte, 2> kEscapedEscape = {kEscape,
27                                                             std::byte{0x5D}};
28
29 constexpr bool NeedsEscaping(std::byte b) {
30   return (b == kFlag || b == kEscape);
31 }
32
33 constexpr std::byte Escape(std::byte b) { return b ^ kEscapeConstant; }
34
35 // Class that manages the 1-byte control field of an HDLC U-frame.
36 class UFrameControl {
37  public:
38   static constexpr UFrameControl UnnumberedInformation() {
39     return UFrameControl(kUnnumberedInformation);
40   }
41
42   constexpr std::byte data() const { return data_; }
43
44  private:
45   // Types of HDLC U-frames and their bit patterns.
46   enum Type : uint8_t {
47     kUnnumberedInformation = 0x00,
48   };
49
50   constexpr UFrameControl(Type type)
51       : data_(kUFramePattern | std::byte{type}) {}
52
53   // U-frames are identified by having the bottom two control bits set.
54   static constexpr std::byte kUFramePattern = std::byte{0x03};
55
56   std::byte data_;
57 };
58
59 }  // namespace pw::hdlc