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

Commit d9c07bb0 authored by Hui Peng's avatar Hui Peng Committed by Gerrit Code Review
Browse files

Merge "Factor out duplicate code for parsing gap data" into main

parents 55522c4d ac5ccf4c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -195,6 +195,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",
+1 −0
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>
@@ -45,6 +46,7 @@ using bluetooth::hci::AdvertiserAddressType;
using bluetooth::hci::ErrorCode;
using bluetooth::hci::GapData;
using bluetooth::hci::OwnAddressType;
using bluetooth::shim::parse_gap_data;
using std::vector;

namespace {
@@ -107,23 +109,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);
  }
@@ -147,33 +134,8 @@ class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface,
    bluetooth::hci::AdvertisingConfig 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,
@@ -196,47 +158,9 @@ class BleAdvertiserInterfaceImpl : public BleAdvertiserInterface,
    parse_periodic_advertising_parameter(config.periodic_advertising_parameters,
                                         periodic_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
    }

    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);

    // if registered by native client, add the register id
    if (client_id != kAdvertiserClientIdJni) {
@@ -270,23 +194,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