1 /*
2  * Copyright 2023 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 #define LOG_TAG "shim"
18 
19 #include "utils.h"
20 
21 #include <bluetooth/log.h>
22 
23 namespace bluetooth {
24 namespace shim {
parse_gap_data(const std::vector<uint8_t> & raw_data,std::vector<hci::GapData> & output)25 void parse_gap_data(const std::vector<uint8_t> &raw_data, std::vector<hci::GapData> &output) {
26   size_t offset = 0;
27   while (offset < raw_data.size()) {
28     hci::GapData gap_data;
29     uint8_t len = raw_data[offset];
30 
31     if (offset + len + 1 > raw_data.size()) {
32       log::warn("GAP data out of bound");
33       break;
34     }
35 
36     auto begin = raw_data.begin() + offset;
37     auto end = begin + len + 1;  // 1 byte for len
38     auto data_copy = std::make_shared<std::vector<uint8_t>>(begin, end);
39     bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian> packet(data_copy);
40     hci::GapData::Parse(&gap_data, packet.begin());
41     output.push_back(gap_data);
42     offset += len + 1;  // 1 byte for len
43   }
44 }
45 
46 }  // namespace shim
47 }  // namespace bluetooth
48