Commit 32ac1c8d authored by zork's avatar zork Committed by Commit bot

Clean up protocol for Bluetooth Pairing.

- Add helper buffer for sending IOBuffer
- Add initialization error states.
- Make the device name more human-readable.

BUG=405744,405774

Review URL: https://codereview.chromium.org/575273002

Cr-Commit-Position: refs/heads/master@{#295629}
parent 2d952ff0
......@@ -92,6 +92,16 @@ void BluetoothControllerPairingController::DeviceLost(
}
}
void BluetoothControllerPairingController::SendBuffer(
scoped_refptr<net::IOBuffer> io_buffer, int size) {
socket_->Send(
io_buffer, size,
base::Bind(&BluetoothControllerPairingController::OnSendComplete,
ptr_factory_.GetWeakPtr()),
base::Bind(&BluetoothControllerPairingController::OnErrorWithMessage,
ptr_factory_.GetWeakPtr()));
}
void BluetoothControllerPairingController::OnSetPowered() {
DCHECK(thread_checker_.CalledOnValidThread());
adapter_->StartDiscoverySession(
......@@ -175,15 +185,15 @@ void BluetoothControllerPairingController::OnReceiveComplete(
}
void BluetoothControllerPairingController::OnError() {
// TODO(zork): Add a stage for initialization error. (http://crbug.com/405744)
LOG(ERROR) << "Pairing initialization failed";
ChangeStage(STAGE_INITIALIZATION_ERROR);
Reset();
}
void BluetoothControllerPairingController::OnErrorWithMessage(
const std::string& message) {
// TODO(zork): Add a stage for initialization error. (http://crbug.com/405744)
LOG(ERROR) << message;
ChangeStage(STAGE_INITIALIZATION_ERROR);
Reset();
}
......@@ -228,9 +238,8 @@ void BluetoothControllerPairingController::StartPairing() {
current_stage_ == STAGE_DEVICE_NOT_FOUND ||
current_stage_ == STAGE_ESTABLISHING_CONNECTION_ERROR ||
current_stage_ == STAGE_HOST_ENROLLMENT_ERROR);
// TODO(zork): Add a stage for no bluetooth. (http://crbug.com/405744)
if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
ChangeStage(STAGE_DEVICE_NOT_FOUND);
ChangeStage(STAGE_INITIALIZATION_ERROR);
return;
}
......@@ -309,8 +318,20 @@ void BluetoothControllerPairingController::SetHostConfiguration(
const std::string& timezone,
bool send_reports,
const std::string& keyboard_layout) {
// TODO(zork): Get configuration from UI and send to Host.
// (http://crbug.com/405744)
pairing_api::ConfigureHost host_config;
host_config.set_api_version(kPairingAPIVersion);
host_config.mutable_parameters()->set_accepted_eula(accepted_eula);
host_config.mutable_parameters()->set_lang(lang);
host_config.mutable_parameters()->set_timezone(timezone);
host_config.mutable_parameters()->set_send_reports(send_reports);
host_config.mutable_parameters()->set_keyboard_layout(keyboard_layout);
int size = 0;
scoped_refptr<net::IOBuffer> io_buffer(
ProtoDecoder::SendConfigureHost(host_config, &size));
SendBuffer(io_buffer, size);
}
void BluetoothControllerPairingController::OnAuthenticationDone(
......@@ -326,12 +347,7 @@ void BluetoothControllerPairingController::OnAuthenticationDone(
scoped_refptr<net::IOBuffer> io_buffer(
ProtoDecoder::SendPairDevices(pair_devices, &size));
socket_->Send(
io_buffer, size,
base::Bind(&BluetoothControllerPairingController::OnSendComplete,
ptr_factory_.GetWeakPtr()),
base::Bind(&BluetoothControllerPairingController::OnErrorWithMessage,
ptr_factory_.GetWeakPtr()));
SendBuffer(io_buffer, size);
ChangeStage(STAGE_HOST_ENROLLMENT_IN_PROGRESS);
}
......@@ -355,13 +371,7 @@ void BluetoothControllerPairingController::OnHostStatusMessage(
scoped_refptr<net::IOBuffer> io_buffer(
ProtoDecoder::SendCompleteSetup(complete_setup, &size));
socket_->Send(
io_buffer, size,
base::Bind(&BluetoothControllerPairingController::OnSendComplete,
ptr_factory_.GetWeakPtr()),
base::Bind(
&BluetoothControllerPairingController::OnErrorWithMessage,
ptr_factory_.GetWeakPtr()));
SendBuffer(io_buffer, size);
ChangeStage(STAGE_PAIRING_DONE);
} else {
got_initial_status_ = true;
......
......@@ -38,6 +38,7 @@ class BluetoothControllerPairingController
void Reset();
void DeviceFound(device::BluetoothDevice* device);
void DeviceLost(device::BluetoothDevice* device);
void SendBuffer(scoped_refptr<net::IOBuffer> io_buffer, int size);
void OnSetPowered();
void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
......
......@@ -5,6 +5,7 @@
#include "components/pairing/bluetooth_host_pairing_controller.h"
#include "base/bind.h"
#include "base/hash.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "components/pairing/bluetooth_pairing_constants.h"
......@@ -118,9 +119,10 @@ void BluetoothHostPairingController::OnGetAdapter(
}
void BluetoothHostPairingController::SetName() {
// TODO(zork): Make the device name prettier. (http://crbug.com/405774)
device_name_ = base::StringPrintf("%s%s", kDeviceNamePrefix,
adapter_->GetAddress().c_str());
// Hash the bluetooth address and take the lower 2 bytes to create a human
// readable device name.
const uint32 device_id = base::Hash(adapter_->GetAddress()) & 0xFFFF;
device_name_ = base::StringPrintf("%s%04X", kDeviceNamePrefix, device_id);
adapter_->SetName(
device_name_,
......@@ -232,14 +234,12 @@ void BluetoothHostPairingController::OnReceiveComplete(
void BluetoothHostPairingController::OnCreateServiceError(
const std::string& message) {
LOG(ERROR) << message;
// TODO(zork): Add a stage for initialization error. (http://crbug.com/405744)
ChangeStage(STAGE_NONE);
ChangeStage(STAGE_INITIALIZATION_ERROR);
}
void BluetoothHostPairingController::OnSetError() {
adapter_->RemovePairingDelegate(this);
// TODO(zork): Add a stage for initialization error. (http://crbug.com/405744)
ChangeStage(STAGE_NONE);
ChangeStage(STAGE_INITIALIZATION_ERROR);
}
void BluetoothHostPairingController::OnAcceptError(
......@@ -267,7 +267,12 @@ void BluetoothHostPairingController::OnHostStatusMessage(
void BluetoothHostPairingController::OnConfigureHostMessage(
const pairing_api::ConfigureHost& message) {
// TODO(zork): Add event to API to handle this case. (http://crbug.com/405744)
FOR_EACH_OBSERVER(Observer, observers_,
ConfigureHost(message.parameters().accepted_eula(),
message.parameters().lang(),
message.parameters().timezone(),
message.parameters().send_reports(),
message.parameters().keyboard_layout()));
}
void BluetoothHostPairingController::OnPairDevicesMessage(
......@@ -337,9 +342,10 @@ void BluetoothHostPairingController::StartPairing() {
DCHECK_EQ(current_stage_, STAGE_NONE);
bool bluetooth_available =
device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable();
// TODO(zork): Add a stage for initialization error. (http://crbug.com/405744)
if (!bluetooth_available)
if (!bluetooth_available) {
ChangeStage(STAGE_INITIALIZATION_ERROR);
return;
}
device::BluetoothAdapterFactory::GetAdapter(
base::Bind(&BluetoothHostPairingController::OnGetAdapter,
......
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