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 { return Iterator(shared_from_this(), packet_start_index_); }
30 
end() const31 Iterator Packet::end() const { return Iterator(shared_from_this(), packet_end_index_); }
32 
33 // For the Array operator, treat index 0 as the relative start of the packet
operator [](size_t i)34 uint8_t Packet::operator[](size_t i) { return get_at_index(i + packet_start_index_); }
35 
get_length() const36 size_t Packet::get_length() const { return data_->size(); }
37 
38 // Iterators use the absolute index to access data.
get_at_index(size_t index) const39 uint8_t Packet::get_at_index(size_t index) const {
40   log::assert_that(index >= packet_start_index_, "assert failed: index >= packet_start_index_");
41   log::assert_that(index < packet_end_index_, "assert failed: index < packet_end_index_");
42   return data_->at(index);
43 }
44 
45 }  // namespace bluetooth
46