Commit 7e132b24 authored by Alberto Herrera's avatar Alberto Herrera Committed by Commit Bot

[Bluetooth] Add battery level field to device::BluetoothDevice.

Also plumb the battery value so that it will be available to Ash Tray
and the Settings UI.

Bug: 785758
Change-Id: I93698c967d92066c388c64a50ad80b81f2aff93f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1642135
Commit-Queue: Alberto Herrera <ahrfgb@google.com>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#669042}
parent c58afb4e
...@@ -25,9 +25,11 @@ ...@@ -25,9 +25,11 @@
#include "device/bluetooth/chromeos/bluetooth_utils.h" #include "device/bluetooth/chromeos/bluetooth_utils.h"
#include "services/device/public/cpp/bluetooth/bluetooth_utils.h" #include "services/device/public/cpp/bluetooth/bluetooth_utils.h"
using device::mojom::BluetoothSystem; using device::mojom::BluetoothDeviceBatteryInfo;
using device::mojom::BluetoothDeviceBatteryInfoPtr;
using device::mojom::BluetoothDeviceInfo; using device::mojom::BluetoothDeviceInfo;
using device::mojom::BluetoothDeviceInfoPtr; using device::mojom::BluetoothDeviceInfoPtr;
using device::mojom::BluetoothSystem;
namespace ash { namespace ash {
namespace { namespace {
...@@ -96,6 +98,10 @@ BluetoothDeviceInfoPtr GetBluetoothDeviceInfo(device::BluetoothDevice* device) { ...@@ -96,6 +98,10 @@ BluetoothDeviceInfoPtr GetBluetoothDeviceInfo(device::BluetoothDevice* device) {
info->address = AddressStrToBluetoothAddress(device->GetAddress()); info->address = AddressStrToBluetoothAddress(device->GetAddress());
info->name = device->GetName(); info->name = device->GetName();
info->is_paired = device->IsPaired(); info->is_paired = device->IsPaired();
if (device->battery_percentage()) {
info->battery_info =
BluetoothDeviceBatteryInfo::New(device->battery_percentage().value());
}
switch (device->GetDeviceType()) { switch (device->GetDeviceType()) {
case device::BluetoothDeviceType::UNKNOWN: case device::BluetoothDeviceType::UNKNOWN:
......
...@@ -581,6 +581,29 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice { ...@@ -581,6 +581,29 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice {
const AbortWriteErrorCallback& error_callback) = 0; const AbortWriteErrorCallback& error_callback) = 0;
#endif #endif
// Set the remaining battery of the device to show in the UI. This value must
// be between 0 and 100, inclusive.
// TODO(https://crbug.com/973237): Battery percentage is populated by
// ash::GattBatteryPoller and used only by Chrome OS. In the future, when
// there is a unified Mojo service, this logic will be moved to
// BluetoothDeviceInfo.
void set_battery_percentage(base::Optional<uint8_t> battery_percentage) {
if (battery_percentage) {
DCHECK(battery_percentage.value() >= 0 &&
battery_percentage.value() <= 100);
}
battery_percentage_ = battery_percentage;
}
// Returns the remaining battery for the device.
// TODO(https://crbug.com/973237): Battery percentage is populated by
// ash::GattBatteryPoller and used only by Chrome OS. In the future, when
// there is a unified Mojo service, this logic will be moved to
// BluetoothDeviceInfo.
const base::Optional<uint8_t>& battery_percentage() const {
return battery_percentage_;
}
protected: protected:
// BluetoothGattConnection is a friend to call Add/RemoveGattConnection. // BluetoothGattConnection is a friend to call Add/RemoveGattConnection.
friend BluetoothGattConnection; friend BluetoothGattConnection;
...@@ -714,6 +737,14 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice { ...@@ -714,6 +737,14 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice {
// a device type for display when |name_| is empty. // a device type for display when |name_| is empty.
base::string16 GetAddressWithLocalizedDeviceTypeName() const; base::string16 GetAddressWithLocalizedDeviceTypeName() const;
// Remaining battery level of the device.
// TODO(https://crbug.com/973237): This field is populated by
// ash::GattBatteryPoller and used only by Chrome OS. This field is different
// from others because it is not filled by the platform. In the future, when
// there is a unified Mojo service, this field will be moved to
// BluetoothDeviceInfo.
base::Optional<uint8_t> battery_percentage_;
DISALLOW_COPY_AND_ASSIGN(BluetoothDevice); DISALLOW_COPY_AND_ASSIGN(BluetoothDevice);
}; };
......
...@@ -159,6 +159,11 @@ void BluetoothDeviceToApiDevice(const device::BluetoothDevice& device, ...@@ -159,6 +159,11 @@ void BluetoothDeviceToApiDevice(const device::BluetoothDevice& device,
else else
out->inquiry_tx_power.reset(); out->inquiry_tx_power.reset();
if (device.battery_percentage())
out->battery_percentage.reset(new int(device.battery_percentage().value()));
else
out->battery_percentage.reset();
#if defined(OS_LINUX) #if defined(OS_LINUX)
ConvertTransportToApi(device.GetType(), &(out->transport)); ConvertTransportToApi(device.GetType(), &(out->transport));
#endif #endif
......
...@@ -96,6 +96,13 @@ namespace bluetooth { ...@@ -96,6 +96,13 @@ namespace bluetooth {
// The transport type of the bluetooth device. // The transport type of the bluetooth device.
Transport? transport; Transport? transport;
// The remaining battery of the device.
// TODO(https://crbug.com/973237): This field is only used by Chrome OS and
// it is different from others because it is not filled by the platform. In
// the future, when there is a unified Mojo service, this field will be
// moved to BluetoothDeviceInfo.
long? batteryPercentage;
}; };
dictionary BluetoothFilter { dictionary BluetoothFilter {
......
...@@ -11,6 +11,12 @@ struct BluetoothAddress { ...@@ -11,6 +11,12 @@ struct BluetoothAddress {
array<uint8, 6> address; array<uint8, 6> address;
}; };
// These fields are placed in a struct to be nullable.
struct BluetoothDeviceBatteryInfo {
// The remaining battery of the device.
uint8 battery_percentage;
};
// Holds information about a Bluetooth Device. // Holds information about a Bluetooth Device.
struct BluetoothDeviceInfo { struct BluetoothDeviceInfo {
enum ConnectionState { enum ConnectionState {
...@@ -60,6 +66,8 @@ struct BluetoothDeviceInfo { ...@@ -60,6 +66,8 @@ struct BluetoothDeviceInfo {
// [1] https://www.bluetooth.com/specifications/assigned-numbers/baseband // [1] https://www.bluetooth.com/specifications/assigned-numbers/baseband
// [2] https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.gap.appearance.xml // [2] https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.gap.appearance.xml
DeviceType device_type; DeviceType device_type;
BluetoothDeviceBatteryInfo? battery_info;
}; };
// Factory to get an instance of the BluetoothSystem interface. // Factory to get an instance of the BluetoothSystem interface.
......
...@@ -94,7 +94,8 @@ chrome.bluetooth.AdapterState; ...@@ -94,7 +94,8 @@ chrome.bluetooth.AdapterState;
* uuids: (!Array<string>|undefined), * uuids: (!Array<string>|undefined),
* inquiryRssi: (number|undefined), * inquiryRssi: (number|undefined),
* inquiryTxPower: (number|undefined), * inquiryTxPower: (number|undefined),
* transport: (!chrome.bluetooth.Transport|undefined) * transport: (!chrome.bluetooth.Transport|undefined),
* batteryPercentage: (number|undefined)
* }} * }}
* @see https://developer.chrome.com/extensions/bluetooth#type-Device * @see https://developer.chrome.com/extensions/bluetooth#type-Device
*/ */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment