• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef NANOPACKET_H_
18 #define NANOPACKET_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <vector>
23 
24 #include "noncopyable.h"
25 
26 namespace android {
27 
28 /*
29  * The various reasons for a NanoPacket to be sent.
30  */
31 enum class PacketReason : uint32_t {
32     Acknowledge        = 0x00000000,
33     NAcknowledge       = 0x00000001,
34     NAcknowledgeBusy   = 0x00000002,
35     GetHardwareVersion = 0x00001000,
36     ReadEventRequest   = 0x00001090,
37     WriteEventRequest  = 0x00001091,
38 };
39 
40 /*
41  * A NanoPacket parsing engine. Used to take a stream of bytes and convert them
42  * into an object that can more easily be worked with.
43  */
44 class NanoPacket : public NonCopyable {
45   public:
46     /*
47      * The result of parsing a buffer into the packet.
48      */
49     enum class ParseResult {
50         Success,
51         Incomplete,
52         CrcMismatch,
53     };
54 
55     // Formats data into NanoPacket format in the provided buffer.
56     NanoPacket(uint32_t sequence_number, PacketReason reason,
57         const std::vector<uint8_t> *data = nullptr);
58 
59     // Creates an empty NanoPacket for data to be parsed into.
60     NanoPacket();
61 
62     // Resets the parsing engine to the idle state and clears parsed content.
63     void Reset();
64 
65     // Parses content from a buffer. Returns true if a packet has been entirely
66     // parsed.
67     ParseResult Parse(uint8_t *buffer, size_t length, size_t *bytes_parsed);
68 
69     // Indicated that parsing of the packet has completed.
70     bool ParsingIsComplete() const;
71 
72     // The entire content of the message.
73     const std::vector<uint8_t>& packet_buffer() const;
74 
75     // Obtains the reason for the packet.
76     uint32_t reason() const;
77 
78     // Obtains the reason as a PacketReason.
79     PacketReason TypedReason() const;
80 
81     // Obtains the data content of the packet.
82     const std::vector<uint8_t>& packet_content() const;
83 
84   private:
85     /*
86      * The current state of the parser.
87      */
88     enum class ParsingState {
89         Idle,
90         ParsingSequenceNumber,
91         ParsingReason,
92         ParsingLength,
93         ParsingContent,
94         ParsingCrc,
95         Complete,
96     };
97 
98     // Parsing engine state.
99     std::vector<uint8_t> packet_buffer_;
100     ParsingState parsing_state_;
101     uint32_t parsing_progress_;
102 
103     // Parsed protocol fields.
104     uint32_t sequence_number_;
105     uint32_t reason_;
106     std::vector<uint8_t> packet_content_;
107     uint32_t crc_;
108 
109     // Validates that the received packet has a CRC that matches a generated
110     // CRC.
111     bool ValidateCrc();
112 
113     // Deserializes a little-endian word using the parsing_progress_ member to
114     // maintain state.
115     template<typename T>
116     bool DeserializeWord(T *destination, uint8_t byte);
117 };
118 
119 }  // namespace android
120 
121 #include "nanopacket_impl.h"
122 
123 #endif  // NANOPACKET_H_
124