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 #include <optional> 23 #include <sstream> 24 #include <string> 25 26 #include "packet/bit_inserter.h" 27 #include "packet/iterator.h" 28 29 namespace bluetooth { 30 namespace packet { 31 namespace parser { 32 namespace test { 33 34 class Variable final { 35 public: 36 std::string data; 37 38 Variable() = default; 39 Variable(const Variable&) = default; 40 Variable(const std::string& str); 41 42 void Serialize(BitInserter& bi) const; 43 44 size_t size() const; 45 46 template <bool little_endian> Parse(Variable * instance,Iterator<little_endian> it)47 static std::optional<Iterator<little_endian>> Parse(Variable* instance, Iterator<little_endian> it) { 48 if (it.NumBytesRemaining() < 1) { 49 return {}; 50 } 51 size_t data_length = it.template extract<uint8_t>(); 52 if (data_length > 255) { 53 return {}; 54 } 55 if (it.NumBytesRemaining() < data_length) { 56 return {}; 57 } 58 std::stringstream ss; 59 for (size_t i = 0; i < data_length; i++) { 60 ss << it.template extract<char>(); 61 } 62 *instance = ss.str(); 63 return it; 64 } 65 ToString()66 std::string ToString() const { 67 return data; 68 } 69 }; 70 71 } // namespace test 72 } // namespace parser 73 } // namespace packet 74 } // namespace bluetooth 75