Skyward boardcore
Loading...
Searching...
No Matches
APIFrameParser.cpp
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Author: Luca Erbetta
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "APIFrameParser.h"
24
25#include <utils/Debug.h>
26
27namespace Boardcore
28{
29
30namespace Xbee
31{
32
34
36{
37 switch (parserState)
38 {
39 // Look for the start of frame delimiter
41
42 if (byte == START_DELIMITER)
43 parserState = ParserState::READ_LENGTH_1;
44 break;
45 // Read most significant byte of the length
47 frame->length = byte;
48 parserState = ParserState::READ_LENGTH_2;
49
50 break;
51 // Read least significant byte of the length
53 frame->length |= ((uint16_t)byte << 8) & 0xFF00;
54 // At least two frame data bytes (frameType and a payload)
55 if (swapBytes16(frame->length) < 2)
56 {
57 parserState = ParserState::FIND_START;
58 return ParseResult::FAIL;
59 }
60
61 if (frame->getFrameDataLength() > FRAME_DATA_SIZE)
62 {
63 parserState = ParserState::FIND_START;
64 return ParseResult::FAIL;
65 }
66
67 parserState = ParserState::READ_FRAME_TYPE;
68 break;
69 // Read frame type
71 frame->frameType = byte;
72 parserState = ParserState::READ_FRAME_DATA;
73 break;
74 // Read the data frame
76 frame->frameData[currentFrameDataIndex++] = byte;
77
78 if (currentFrameDataIndex == frame->getFrameDataLength())
79 {
80 currentFrameDataIndex = 0;
81 parserState = ParserState::READ_CHECKSUM;
82 }
83 break;
84 // Read & verify checksum
86 frame->checksum = byte;
87 parserState = ParserState::FIND_START;
88
89 if (frame->verifyChecksum())
90 {
91 return ParseResult::SUCCESS;
92 }
93 else
94 {
95 LOG_ERR(logger, "Wrong packet checksum!");
96 return ParseResult::FAIL;
97 }
98 break;
99 }
100
101 if (parserState != ParserState::FIND_START)
102 return ParseResult::PARSING;
103 else
104 return ParseResult::IDLE;
105}
106
107} // namespace Xbee
108
109} // namespace Boardcore
#define LOG_ERR(logger,...)
ParseResult
Result of the last parse operation.
ParseResult parse(uint8_t byte, APIFrame *frame)
Parses a single byte. When this function returns ParseResult:SUCESS, frame contains a valid APIFrame.
Driver for the VN100S IMU.
uint8_t frameData[FRAME_DATA_SIZE]
Definition APIFrames.h:143
bool verifyChecksum() const
Definition APIFrames.h:163
uint16_t getFrameDataLength() const
Definition APIFrames.h:151