1 /*
2  * Copyright 2018 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 #pragma once
18 
19 #include <iterator>
20 #include <memory>
21 
22 namespace bluetooth {
23 
24 // Forward declare Packet class
25 class Packet;
26 
27 // Iterator is a custom iterator class for Packets.
28 class Iterator
29     : public std::iterator<std::random_access_iterator_tag, uint8_t> {
30  public:
31   Iterator(std::shared_ptr<const Packet> packet, size_t i);
32   Iterator(const Iterator& itr);
33 
34   // All addition and subtraction operators are bounded from 0 to the length of
35   // the packet.
36   Iterator operator+(size_t offset);
37   Iterator& operator+=(size_t offset);
38   Iterator operator++(int);
39   Iterator& operator++();
40 
41   Iterator operator-(size_t offset);
42   int operator-(const Iterator& itr);
43   Iterator& operator-=(size_t offset);
44   Iterator operator--(int);
45   Iterator& operator--();
46 
47   Iterator& operator=(const Iterator& itr);
48 
49   bool operator!=(const Iterator& itr) const;
50   bool operator==(const Iterator& itr) const;
51 
52   bool operator<(const Iterator& itr) const;
53   bool operator>(const Iterator& itr) const;
54 
55   bool operator<=(const Iterator& itr) const;
56   bool operator>=(const Iterator& itr) const;
57 
58   uint8_t operator*() const;
59 
60   template <typename FixedWidthIntegerType>
extract()61   FixedWidthIntegerType extract() {
62     static_assert(std::is_integral<FixedWidthIntegerType>::value,
63                   "Iterator::extract requires an integral type.");
64 
65     FixedWidthIntegerType extracted_value = 0;
66     for (size_t i = 0; i < sizeof(FixedWidthIntegerType); i++) {
67       extracted_value |= static_cast<FixedWidthIntegerType>(**this) << i * 8;
68       (*this)++;
69     }
70 
71     return extracted_value;
72   }
73 
74   // Extract in Little Endian Format
75   template <typename FixedWidthIntegerType>
extractBE()76   FixedWidthIntegerType extractBE() {
77     static_assert(std::is_integral<FixedWidthIntegerType>::value,
78                   "Iterator::extract requires an integral type.");
79 
80     FixedWidthIntegerType extracted_value = 0;
81     for (size_t i = 0; i < sizeof(FixedWidthIntegerType); i++) {
82       extracted_value |= static_cast<FixedWidthIntegerType>(**this)
83                          << (sizeof(FixedWidthIntegerType) - 1 - i) * 8;
84       (*this)++;
85     }
86 
87     return extracted_value;
88   }
89 
extract8()90   uint8_t extract8() { return extract<uint8_t>(); }
extract16()91   uint16_t extract16() { return extract<uint16_t>(); }
extract32()92   uint32_t extract32() { return extract<uint32_t>(); }
extract64()93   uint64_t extract64() { return extract<uint64_t>(); }
94 
95  private:
96   std::shared_ptr<const Packet> packet_;
97   size_t index_;
98 };  // Iterator
99 
100 }  // namespace bluetooth
101