Coverage Report

Created: 2025-08-03 06:23

/src/opendnp3/cpp/lib/src/link/LinkLayerParser.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2013-2022 Step Function I/O, LLC
3
 *
4
 * Licensed to Green Energy Corp (www.greenenergycorp.com) and Step Function I/O
5
 * LLC (https://stepfunc.io) under one or more contributor license agreements.
6
 * See the NOTICE file distributed with this work for additional information
7
 * regarding copyright ownership. Green Energy Corp and Step Function I/O LLC license
8
 * this file to you under the Apache License, Version 2.0 (the "License"); you
9
 * may not use this file except in compliance with the License. You may obtain
10
 * a copy of the License at:
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
#ifndef OPENDNP3_LINKLAYERPARSER_H
21
#define OPENDNP3_LINKLAYERPARSER_H
22
23
#include "link/IFrameSink.h"
24
#include "link/LinkFrame.h"
25
#include "link/LinkHeader.h"
26
#include "link/ShiftableBuffer.h"
27
28
#include "opendnp3/link/LinkStatistics.h"
29
#include "opendnp3/logging/Logger.h"
30
31
#include <ser4cpp/container/SequenceTypes.h>
32
33
namespace opendnp3
34
{
35
36
/// Parses FT3 frames
37
class LinkLayerParser
38
{
39
    enum class State
40
    {
41
        FindSync,
42
        ReadHeader,
43
        ReadBody,
44
        Complete
45
    };
46
47
public:
48
    /// @param logger_ Logger that the receiver is to use.
49
    /// @param pSink_ Completely parsed frames are sent to this interface
50
    LinkLayerParser(const Logger& logger);
51
52
    /// Called when valid data has been written to the current buffer write position
53
    /// Parses the new data and calls the specified frame sink
54
    /// @param numBytes Number of bytes written
55
    void OnRead(size_t numBytes, IFrameSink& sink);
56
57
    /// @return Buffer that can currently be used for writing
58
    ser4cpp::wseq_t WriteBuff() const;
59
60
    /// Resets the state of parser
61
    void Reset();
62
63
    const LinkStatistics::Parser& Statistics() const
64
0
    {
65
0
        return this->statistics;
66
0
    }
67
68
private:
69
    State ParseUntilComplete();
70
    State ParseOneStep();
71
    State ParseSync();
72
    State ParseHeader();
73
    State ParseBody();
74
75
    void PushFrame(IFrameSink& sink);
76
77
    bool ReadHeader();
78
    bool ValidateBody();
79
    bool ValidateHeaderParameters();
80
    bool ValidateFunctionCode();
81
    void FailFrame();
82
83
    void TransferUserData();
84
85
    Logger logger;
86
    LinkStatistics::Parser statistics;
87
88
    LinkHeader header;
89
90
    State state;
91
    size_t frameSize;
92
    ser4cpp::rseq_t userData;
93
94
    // buffer where received data is written
95
    uint8_t rxBuffer[LPDU_MAX_FRAME_SIZE];
96
97
    // facade over the rxBuffer that provides ability to "shift" as data is read
98
    ShiftableBuffer buffer;
99
};
100
101
} // namespace opendnp3
102
103
#endif