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

Commit f3c7a1e8 authored by Arman Uguray's avatar Arman Uguray Committed by Android Git Automerger
Browse files

am 79b0013f: service: Add AdvertiseData and AdvertiseSettings

* commit '79b0013f':
  service: Add AdvertiseData and AdvertiseSettings
parents b45ba552 79b0013f
Loading
Loading
Loading
Loading
+24 −1
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ LOCAL_PATH:= $(call my-dir)
btserviceCommonSrc := \
	adapter.cpp \
	adapter_state.cpp \
	advertise_data.cpp \
	advertise_settings.cpp \
	daemon.cpp \
	gatt_server.cpp \
	hal/bluetooth_gatt_interface.cpp \
@@ -42,7 +44,8 @@ btserviceBinderSrc := \
	ipc/binder/IBluetoothCallback.cpp \
	ipc/binder/IBluetoothLowEnergy.cpp \
	ipc/binder/IBluetoothLowEnergyCallback.cpp \
	ipc/binder/ipc_handler_binder.cpp
	ipc/binder/ipc_handler_binder.cpp \
	ipc/binder/parcel_helpers.cpp

btserviceCommonIncludes := $(LOCAL_PATH)/../

@@ -78,6 +81,7 @@ LOCAL_SRC_FILES := \
	hal/fake_bluetooth_gatt_interface.cpp \
	hal/fake_bluetooth_interface.cpp \
	test/adapter_unittest.cpp \
	test/advertise_data_unittest.cpp \
	test/fake_hal_util.cpp \
	test/ipc_unix_unittest.cpp \
	test/low_energy_client_unittest.cpp \
@@ -94,7 +98,26 @@ LOCAL_STATIC_LIBRARIES += libgmock_host liblog
include $(BUILD_HOST_NATIVE_TEST)
endif

# Native system service unittests for Binder code, for target
# ========================================================
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
	$(btserviceBinderSrc) \
	$(btserviceCommonSrc) \
	test/parcel_helpers_unittest.cpp
LOCAL_C_INCLUDES += $(btserviceCommonIncludes)
LOCAL_CFLAGS += -std=c++11
LOCAL_MODULE_TAGS := tests
LOCAL_MODULE := bt_service_binder_unittests
LOCAL_SHARED_LIBRARIES += \
	libbinder \
	libchrome \
	libutils
LOCAL_STATIC_LIBRARIES += liblog
include $(BUILD_NATIVE_TEST)

# Native system service CLI for target
# ========================================================
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
	$(btserviceBinderSrc) \
+104 −0
Original line number Diff line number Diff line
//
//  Copyright (C) 2015 Google, Inc.
//
//  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 "service/advertise_data.h"

#include <base/logging.h>

#include "stack/include/bt_types.h"
#include "stack/include/hcidefs.h"

namespace bluetooth {

AdvertiseData::AdvertiseData(const std::vector<uint8_t>& data)
    : data_(data),
      include_device_name_(false),
      include_tx_power_level_(false) {
}

AdvertiseData::AdvertiseData()
    : include_device_name_(false),
      include_tx_power_level_(false) {
}

AdvertiseData::AdvertiseData(const AdvertiseData& other)
    : data_(other.data_),
      include_device_name_(other.include_device_name_),
      include_tx_power_level_(other.include_tx_power_level_) {
}

bool AdvertiseData::IsValid() const {
  size_t len = data_.size();

  // Consider empty data as valid.
  if (!len)
    return true;

  for (size_t i = 0, field_len = 0; i < len; i += (field_len + 1)) {
    field_len = data_[i];

    // If the length of the current field would exceed the total data length,
    // then the data is badly formatted.
    if (i + field_len >= len) {
      VLOG(1) << "Advertising data badly formatted";
      return false;
    }

    uint8_t type = data_[i + 1];

    // Clients are not allowed to set the following EIR fields as these are
    // managed by stack.
    switch (type) {
    case HCI_EIR_FLAGS_TYPE:
    case HCI_EIR_TX_POWER_LEVEL_TYPE:
    case HCI_EIR_SHORTENED_LOCAL_NAME_TYPE:
    case HCI_EIR_COMPLETE_LOCAL_NAME_TYPE:
    case HCI_EIR_OOB_BD_ADDR_TYPE:
    case HCI_EIR_OOB_COD_TYPE:
    case HCI_EIR_OOB_SSP_HASH_C_TYPE:
    case HCI_EIR_OOB_SSP_RAND_R_TYPE:
      VLOG(1) << "Cannot set EIR field type: " << type;
      return false;
    default:
      break;
    }
  }

  return true;
}

bool AdvertiseData::operator==(const AdvertiseData& rhs) const {
  if (include_tx_power_level_ != rhs.include_tx_power_level_)
    return false;

  if (include_device_name_ != rhs.include_device_name_)
    return false;

  return data_ == rhs.data_;
}

AdvertiseData& AdvertiseData::operator=(const AdvertiseData& other) {
  if (this == &other)
    return *this;

  data_ = other.data_;
  include_device_name_ = other.include_device_name_;
  include_tx_power_level_ = other.include_tx_power_level_;

  return *this;
}

}  // namespace bluetooth
+79 −0
Original line number Diff line number Diff line
//
//  Copyright (C) 2015 Google, Inc.
//
//  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.
//

#pragma once

#include <stdint.h>

#include <vector>

#include <base/macros.h>

namespace bluetooth {

// Represents a data packet for Bluetooth Low Energy advertisements. This is the
// native equivalent of the Android framework class defined in
// frameworks/base/core/j/android/bluetooth/le/AdvertiseData.java
class AdvertiseData final {
 public:
  // Constructs an AdvertiseData with the given parameters. |data| can only
  // contain the "Service UUIDs", "Service Data", and "Manufacturer Data" fields
  // as specified in the Core Specification Supplement. |data| must be properly
  // formatted according to the supplement and contains the data as it will be
  // sent over the wire.
  //
  // The values for include_device_name() and include_tx_power_level() are
  // initialized to false by default. These can be modified using the setters
  // declared below.
  explicit AdvertiseData(const std::vector<uint8_t>& data);

  // Default constructor initializes all fields to be empty/false.
  AdvertiseData();
  AdvertiseData(const AdvertiseData& other);
  ~AdvertiseData() = default;

  // Returns true if the advertising data is formatted correctly according to
  // the TLV format.
  bool IsValid() const;

  // data() returns the current advertising data contained by this instance. The
  // data is in the TLV format as specified in the Bluetooth Core Specification.
  const std::vector<uint8_t>& data() const { return data_; }

  // Whether the device name should be included in the advertisement packet.
  bool include_device_name() const { return include_device_name_; }
  void set_include_device_name(bool value) { include_device_name_ = value; }

  // Whether the transmission power level should be included in the
  // advertisement packet.
  bool include_tx_power_level() const { return include_tx_power_level_; }
  void set_include_tx_power_level(bool value) {
    include_tx_power_level_ = value;
  }

  // Comparison operator.
  bool operator==(const AdvertiseData& rhs) const;

  // Assignment operator
  AdvertiseData& operator=(const AdvertiseData& other);

 private:
  std::vector<uint8_t> data_;
  bool include_device_name_;
  bool include_tx_power_level_;
};

}  // namespace bluetooth
+55 −0
Original line number Diff line number Diff line
//
//  Copyright (C) 2015 Google, Inc.
//
//  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 "service/advertise_settings.h"

namespace bluetooth {

AdvertiseSettings::AdvertiseSettings(
    Mode mode,
    base::TimeDelta timeout,
    TxPowerLevel tx_power_level,
    bool connectable)
    : mode_(mode),
      timeout_(timeout),
      tx_power_level_(tx_power_level),
      connectable_(connectable) {
}

// Default values are taken from the AdvertiseSettings.java
AdvertiseSettings::AdvertiseSettings()
    : mode_(MODE_LOW_POWER),
      tx_power_level_(TX_POWER_LEVEL_MEDIUM),
      connectable_(true) {
}

bool AdvertiseSettings::operator==(const AdvertiseSettings& rhs) const {
  if (mode_ != rhs.mode_)
    return false;

  if (timeout_ != rhs.timeout_)
    return false;

  if (tx_power_level_ != rhs.tx_power_level_)
    return false;

  if (connectable_ != rhs.connectable_)
    return false;

  return true;
}

}  // namespace bluetooth
+98 −0
Original line number Diff line number Diff line
//
//  Copyright (C) 2015 Google, Inc.
//
//  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.
//

#pragma once

#include <base/macros.h>
#include <base/time/time.h>

namespace bluetooth {

// AdvertiseSettings provides a way to adjust advertising preferences for each
// Bluetooth LE advertisement instance. This is the native equivalent of the
// Android framework class defined in
// frameworks/base/core/java/android/bluetooth/le/AdvertiseSettings.java
class AdvertiseSettings {
 public:
  // Advertising mode describes power consumption mode used for advertising.
  enum Mode {
    // Perform Bluetooth LE advertising in low power mode. This is the default
    // and preferred advertising mode as it consumes the least power.
    MODE_LOW_POWER = 0x00,

    // Perform Bluetooth LE advertising in balanced power mode. This is balanced
    // between advertising frequency and power consumption.
    MODE_BALANCED = 0x01,

    // Perform Bluetooth LE advertising in low latency, high power mode. This
    // has the highest power consumption and should not be used for continuous
    // background advertising.
    MODE_LOW_LATENCY = 0x02,
  };

  // Levels that can be set for advertising transmission power.
  enum TxPowerLevel {
    // Advertise using the lowest transmission (TX) power level. Low
    // transmission power can be used to restrict the visibility range of
    // advertising packets.
    TX_POWER_LEVEL_ULTRA_LOW = 0x00,

    // Advertise using low TX power level.
    TX_POWER_LEVEL_LOW = 0x01,

    // Advertise using medium TX power level.
    TX_POWER_LEVEL_MEDIUM = 0x02,

    // Advertise using high TX power level. This corresponds to largest
    // visibility range of the advertising packet.
    TX_POWER_LEVEL_HIGH = 0x03,
  };

  AdvertiseSettings(Mode mode,
                    base::TimeDelta timeout,
                    TxPowerLevel tx_power_level,
                    bool connectable);

  // The default constructor sets all fields to defaults:
  //   mode: MODE_LOW_POWER
  //   TX power level: TX_POWER_LEVEL_MEDIUM
  //   connectable: true
  AdvertiseSettings();
  ~AdvertiseSettings() = default;

  // Returns the advertise mode.
  Mode mode() const { return mode_; }

  // Returns the advertising time limit in milliseconds.
  const base::TimeDelta& timeout() const { return timeout_; }

  // Returns the TX power level for advertising.
  TxPowerLevel tx_power_level() const { return tx_power_level_; }

  // Returns whether the advertisement will indicate connectable.
  bool connectable() const { return connectable_; }

  // Comparison operator.
  bool operator==(const AdvertiseSettings& rhs) const;

 private:
  Mode mode_;
  base::TimeDelta timeout_;
  TxPowerLevel tx_power_level_;
  bool connectable_;
};

}  // namespace bluetooth
Loading