aboutsummaryrefslogtreecommitdiff
path: root/pw_hdlc/wire_packet_parser.cc
blob: d867180957f040ae74f46c22dc0c0da54d0020e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2021 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "pw_hdlc/wire_packet_parser.h"

#include "pw_bytes/endian.h"
#include "pw_checksum/crc32.h"
#include "pw_hdlc/decoder.h"
#include "pw_hdlc/internal/protocol.h"

namespace pw::hdlc {

bool WirePacketParser::Parse(ConstByteSpan packet) {
  if (packet.size_bytes() < Frame::kMinContentSizeBytes) {
    return false;
  }

  if (packet.front() != kFlag || packet.back() != kFlag) {
    return false;
  }

  // Partially decode into a buffer with space only for the address and control
  // fields of the frame. The decoder will verify the frame's FCS field.
  std::array<std::byte, 16> buffer = {};
  Decoder decoder(buffer);
  Status status = Status::Unknown();

  decoder.Process(packet, [&status](const Result<Frame>& result) {
    status = result.status();
  });

  Result<Frame> result = Frame::Parse(buffer);
  if (!result.ok()) {
    return false;
  }

  address_ = result.value().address();

  // RESOURCE_EXHAUSTED is expected as the buffer is too small for the packet.
  return status.ok() || status.IsResourceExhausted();
}

}  // namespace pw::hdlc