Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cd546f8c authored by Hui Peng's avatar Hui Peng Committed by Automerger Merge Worker
Browse files

Factor out duplicate code for parsing gap data am: 08690d66

parents b78a735e 08690d66
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -188,6 +188,7 @@ cc_test {
        "shim/metrics_api.cc",
        "shim/shim.cc",
        "shim/stack.cc",
        "shim/utils.cc",
        "test/common_stack_test.cc",
        "test/main_shim_dumpsys_test.cc",
        "test/main_shim_test.cc",
+2 −1
Original line number Diff line number Diff line
@@ -29,5 +29,6 @@ filegroup {
        "metrics_api.cc",
        "shim.cc",
        "stack.cc",
    ]
        "utils.cc",
    ],
}
+1 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ source_set("LibBluetoothShimSources") {
    "metrics_api.cc",
    "shim.cc",
    "stack.cc",
    "utils.cc",
  ]

  include_dirs = [
+9 −100
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#define LOG_TAG "bt_shim_advertiser"

#include "le_advertising_manager.h"
#include "utils.h"

#include <base/logging.h>
#include <hardware/bluetooth.h>
@@ -43,6 +44,7 @@ using bluetooth::hci::AddressType;
using bluetooth::hci::ErrorCode;
using bluetooth::hci::GapData;
using bluetooth::hci::OwnAddressType;
using bluetooth::shim::parse_gap_data;
using std::vector;

namespace {
@@ -88,23 +90,8 @@ class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface,
  void SetData(int advertiser_id, bool set_scan_rsp, vector<uint8_t> data,
               StatusCallback cb) override {
    LOG(INFO) << __func__ << " in shim layer";

    size_t offset = 0;
    std::vector<GapData> advertising_data = {};

    while (offset < data.size()) {
      GapData gap_data;
      uint8_t len = data[offset];
      auto begin = data.begin() + offset;
      auto end = begin + len + 1;  // 1 byte for len
      auto data_copy = std::make_shared<std::vector<uint8_t>>(begin, end);
      bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian> packet(
          data_copy);
      GapData::Parse(&gap_data, packet.begin());
      advertising_data.push_back(gap_data);
      offset += len + 1;  // 1 byte for len
    }

    parse_gap_data(data, advertising_data);
    bluetooth::shim::GetAdvertising()->SetData(advertiser_id, set_scan_rsp,
                                               advertising_data);
  }
@@ -128,33 +115,8 @@ class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface,
    bluetooth::hci::ExtendedAdvertisingConfig config{};
    parse_parameter(config, params);

    size_t offset = 0;
    while (offset < advertise_data.size()) {
      GapData gap_data;
      uint8_t len = advertise_data[offset];
      auto begin = advertise_data.begin() + offset;
      auto end = begin + len + 1;  // 1 byte for len
      auto data_copy = std::make_shared<std::vector<uint8_t>>(begin, end);
      bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian> packet(
          data_copy);
      GapData::Parse(&gap_data, packet.begin());
      config.advertisement.push_back(gap_data);
      offset += len + 1;  // 1 byte for len
    }

    offset = 0;
    while (offset < scan_response_data.size()) {
      GapData gap_data;
      uint8_t len = scan_response_data[offset];
      auto begin = scan_response_data.begin() + offset;
      auto end = begin + len + 1;  // 1 byte for len
      auto data_copy = std::make_shared<std::vector<uint8_t>>(begin, end);
      bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian> packet(
          data_copy);
      GapData::Parse(&gap_data, packet.begin());
      config.scan_response.push_back(gap_data);
      offset += len + 1;  // 1 byte for len
    }
    parse_gap_data(advertise_data, config.advertisement);
    parse_gap_data(scan_response_data, config.scan_response);

    bluetooth::shim::GetAdvertising()->StartAdvertising(
        advertiser_id, config, timeout_s * 100, cb, timeout_cb, scan_callback,
@@ -180,47 +142,9 @@ class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface,
        periodic_params.periodic_advertising_properties;
    config.periodic_advertising_parameters = periodic_parameters;

    size_t offset = 0;
    while (offset < advertise_data.size()) {
      GapData gap_data;
      uint8_t len = advertise_data[offset];
      auto begin = advertise_data.begin() + offset;
      auto end = begin + len + 1;  // 1 byte for len
      auto data_copy = std::make_shared<std::vector<uint8_t>>(begin, end);
      bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian> packet(
          data_copy);
      GapData::Parse(&gap_data, packet.begin());
      config.advertisement.push_back(gap_data);
      offset += len + 1;  // 1 byte for len
    }

    offset = 0;
    while (offset < scan_response_data.size()) {
      GapData gap_data;
      uint8_t len = scan_response_data[offset];
      auto begin = scan_response_data.begin() + offset;
      auto end = begin + len + 1;  // 1 byte for len
      auto data_copy = std::make_shared<std::vector<uint8_t>>(begin, end);
      bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian> packet(
          data_copy);
      GapData::Parse(&gap_data, packet.begin());
      config.scan_response.push_back(gap_data);
      offset += len + 1;  // 1 byte for len
    }

    offset = 0;
    while (offset < periodic_data.size()) {
      GapData gap_data;
      uint8_t len = periodic_data[offset];
      auto begin = periodic_data.begin() + offset;
      auto end = begin + len + 1;  // 1 byte for len
      auto data_copy = std::make_shared<std::vector<uint8_t>>(begin, end);
      bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian> packet(
          data_copy);
      GapData::Parse(&gap_data, packet.begin());
      config.periodic_data.push_back(gap_data);
      offset += len + 1;  // 1 byte for len
    }
    parse_gap_data(advertise_data, config.advertisement);
    parse_gap_data(scan_response_data, config.scan_response);
    parse_gap_data(periodic_data, config.periodic_data);

    bluetooth::hci::AdvertiserId id =
        bluetooth::shim::GetAdvertising()->ExtendedCreateAdvertiser(
@@ -249,23 +173,8 @@ class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface,
  void SetPeriodicAdvertisingData(int advertiser_id, std::vector<uint8_t> data,
                                  StatusCallback cb) override {
    LOG(INFO) << __func__ << " in shim layer";

    size_t offset = 0;
    std::vector<GapData> advertising_data = {};

    while (offset < data.size()) {
      GapData gap_data;
      uint8_t len = data[offset];
      auto begin = data.begin() + offset;
      auto end = begin + len + 1;  // 1 byte for len
      auto data_copy = std::make_shared<std::vector<uint8_t>>(begin, end);
      bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian> packet(
          data_copy);
      GapData::Parse(&gap_data, packet.begin());
      advertising_data.push_back(gap_data);
      offset += len + 1;  // 1 byte for len
    }

    parse_gap_data(data, advertising_data);
    bluetooth::shim::GetAdvertising()->SetPeriodicData(advertiser_id,
                                                       advertising_data);
  }
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "utils.h"

namespace bluetooth {
namespace shim {
void parse_gap_data(const std::vector<uint8_t> &raw_data,
                    std::vector<hci::GapData> &output) {
    size_t offset = 0;
    while (offset < raw_data.size()) {
      hci::GapData gap_data;
      uint8_t len = raw_data[offset];

      auto begin = raw_data.begin() + offset;
      auto end = begin + len + 1;  // 1 byte for len
      auto data_copy = std::make_shared<std::vector<uint8_t>>(begin, end);
      bluetooth::packet::PacketView<bluetooth::packet::kLittleEndian> packet(
          data_copy);
      hci::GapData::Parse(&gap_data, packet.begin());
      output.push_back(gap_data);
      offset += len + 1;  // 1 byte for len
    }
}

}  // namespace shim
}  // namespace bluetooth
Loading