1 /******************************************************************************
2  *
3  *  Copyright 2019 The Android Open Source Project
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #pragma once
20 
21 #include <stdint.h>
22 
23 #include <optional>
24 #include <sstream>
25 #include <string>
26 
27 #include "packet/bit_inserter.h"
28 #include "packet/iterator.h"
29 
30 namespace bluetooth {
31 namespace packet {
32 namespace parser {
33 namespace test {
34 
35 class Variable final {
36 public:
37   std::string data;
38 
39   Variable() = default;
40   Variable(const Variable&) = default;
41   Variable(const std::string& str);
42 
43   void Serialize(BitInserter& bi) const;
44 
45   size_t size() const;
46 
47   template <bool little_endian>
Parse(Variable * instance,Iterator<little_endian> it)48   static std::optional<Iterator<little_endian>> Parse(Variable* instance,
49                                                       Iterator<little_endian> it) {
50     if (it.NumBytesRemaining() < 1) {
51       return {};
52     }
53     size_t data_length = it.template extract<uint8_t>();
54     if (data_length > 255) {
55       return {};
56     }
57     if (it.NumBytesRemaining() < data_length) {
58       return {};
59     }
60     std::stringstream ss;
61     for (size_t i = 0; i < data_length; i++) {
62       ss << it.template extract<char>();
63     }
64     *instance = ss.str();
65     return it;
66   }
67 
ToString()68   std::string ToString() const { return data; }
69 };
70 
71 }  // namespace test
72 }  // namespace parser
73 }  // namespace packet
74 }  // namespace bluetooth
75