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 #include "packet.h"
18 
19 #include <base/logging.h>
20 #include <algorithm>
21 
22 #include "iterator.h"
23 
24 namespace bluetooth {
25 
size() const26 size_t Packet::size() const { return packet_end_index_ - packet_start_index_; }
27 
begin() const28 Iterator Packet::begin() const {
29   return Iterator(shared_from_this(), packet_start_index_);
30 }
31 
end() const32 Iterator Packet::end() const {
33   return Iterator(shared_from_this(), packet_end_index_);
34 }
35 
36 // For the Array operator, treat index 0 as the relative start of the packet
operator [](size_t i)37 uint8_t Packet::operator[](size_t i) {
38   return get_at_index(i + packet_start_index_);
39 }
40 
get_length() const41 size_t Packet::get_length() const { return data_->size(); }
42 
43 // Iterators use the absolute index to access data.
get_at_index(size_t index) const44 uint8_t Packet::get_at_index(size_t index) const {
45   CHECK_GE(index, packet_start_index_);
46   CHECK_LT(index, packet_end_index_);
47   return data_->at(index);
48 }
49 
50 }  // namespace bluetooth