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 <bluetooth/log.h>
20 
21 #include <algorithm>
22 
23 #include "iterator.h"
24 
25 namespace bluetooth {
26 
size() const27 size_t Packet::size() const { return packet_end_index_ - packet_start_index_; }
28 
begin() const29 Iterator Packet::begin() const {
30   return Iterator(shared_from_this(), packet_start_index_);
31 }
32 
end() const33 Iterator Packet::end() const {
34   return Iterator(shared_from_this(), packet_end_index_);
35 }
36 
37 // For the Array operator, treat index 0 as the relative start of the packet
operator [](size_t i)38 uint8_t Packet::operator[](size_t i) {
39   return get_at_index(i + packet_start_index_);
40 }
41 
get_length() const42 size_t Packet::get_length() const { return data_->size(); }
43 
44 // Iterators use the absolute index to access data.
get_at_index(size_t index) const45 uint8_t Packet::get_at_index(size_t index) const {
46   log::assert_that(index >= packet_start_index_,
47                    "assert failed: index >= packet_start_index_");
48   log::assert_that(index < packet_end_index_,
49                    "assert failed: index < packet_end_index_");
50   return data_->at(index);
51 }
52 
53 }  // namespace bluetooth
54