adapter.cpp revision 4cebc7e190f29acb56ef8658a9ad02137e82e967
14fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//
24fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//  Copyright (C) 2015 Google, Inc.
34fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//
44fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//  Licensed under the Apache License, Version 2.0 (the "License");
54fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//  you may not use this file except in compliance with the License.
64fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//  You may obtain a copy of the License at:
74fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//
84fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//  http://www.apache.org/licenses/LICENSE-2.0
94fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//
104fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//  Unless required by applicable law or agreed to in writing, software
114fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//  distributed under the License is distributed on an "AS IS" BASIS,
124fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//  See the License for the specific language governing permissions and
144fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//  limitations under the License.
154fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray//
164fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
174fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray#include "service/adapter.h"
184fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
194fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray#include <base/logging.h>
204fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
214fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray#include "service/logging_helpers.h"
224fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
234fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguraynamespace bluetooth {
244fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
2503b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray// static
2603b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Ugurayconst char Adapter::kDefaultAddress[] = "00:00:00:00:00:00";
2703b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray// static
2803b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Ugurayconst char Adapter::kDefaultName[] = "not-initialized";
2903b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray
304cebc7e190f29acb56ef8658a9ad02137e82e967Arman Ugurayvoid Adapter::Observer::OnAdapterStateChanged(Adapter* adapter,
314cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray                                              AdapterState prev_state,
324cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray                                              AdapterState new_state) {
334cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray  // Default implementation does nothing
344cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray}
354cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray
3603b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman UgurayAdapter::Adapter()
3703b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray    : state_(ADAPTER_STATE_OFF),
3803b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray      address_(kDefaultAddress),
3903b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray      name_(kDefaultName) {
404fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  hal::BluetoothInterface::Get()->AddObserver(this);
412117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  hal::BluetoothInterface::Get()->GetHALInterface()->get_adapter_properties();
424fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray}
434fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
444fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman UgurayAdapter::~Adapter() {
454fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  hal::BluetoothInterface::Get()->RemoveObserver(this);
464fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray}
474fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
484cebc7e190f29acb56ef8658a9ad02137e82e967Arman Ugurayvoid Adapter::AddObserver(Observer* observer) {
494cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray  std::lock_guard<std::mutex> lock(observers_lock_);
504cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray  observers_.AddObserver(observer);
514cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray}
524cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray
534cebc7e190f29acb56ef8658a9ad02137e82e967Arman Ugurayvoid Adapter::RemoveObserver(Observer* observer) {
544cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray  std::lock_guard<std::mutex> lock(observers_lock_);
554cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray  observers_.RemoveObserver(observer);
564cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray}
574cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray
582117e520c9f5b105ade7e92c4ab4928ea905f176Arman UgurayAdapterState Adapter::GetState() const {
592117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  return state_.load();
602117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray}
612117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray
622117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguraybool Adapter::IsEnabled() const {
632117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  return state_.load() == ADAPTER_STATE_ON;
644fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray}
654fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
664fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguraybool Adapter::Enable() {
672117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  AdapterState current_state = GetState();
682117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  if (current_state != ADAPTER_STATE_OFF) {
692117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray    LOG(INFO) << "Adapter not disabled - state: "
702117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray              << AdapterStateToString(current_state);
714fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    return false;
724fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  }
734fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
742117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  // Set the state before calling enable() as there might be a race between here
752117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  // and the AdapterStateChangedCallback.
762117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  state_ = ADAPTER_STATE_TURNING_ON;
774cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray  NotifyAdapterStateChanged(current_state, state_);
782117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray
794fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  int status = hal::BluetoothInterface::Get()->GetHALInterface()->enable();
804fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  if (status != BT_STATUS_SUCCESS) {
814fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    LOG(ERROR) << "Failed to enable Bluetooth - status: "
824fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray               << BtStatusText((const bt_status_t)status);
832117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray    state_ = ADAPTER_STATE_OFF;
844cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray    NotifyAdapterStateChanged(ADAPTER_STATE_TURNING_ON, state_);
854fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    return false;
864fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  }
874fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
884fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  return true;
894fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray}
904fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
914fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguraybool Adapter::Disable() {
922117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  if (!IsEnabled()) {
932117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray    LOG(INFO) << "Adapter is not enabled";
944fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    return false;
954fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  }
964fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
972117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  AdapterState current_state = GetState();
982117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray
992117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  // Set the state before calling enable() as there might be a race between here
1002117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  // and the AdapterStateChangedCallback.
1012117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray  state_ = ADAPTER_STATE_TURNING_OFF;
1024cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray  NotifyAdapterStateChanged(current_state, state_);
1032117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray
1044fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  int status = hal::BluetoothInterface::Get()->GetHALInterface()->disable();
1054fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  if (status != BT_STATUS_SUCCESS) {
1064fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    LOG(ERROR) << "Failed to disable Bluetooth - status: "
1074fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray               << BtStatusText((const bt_status_t)status);
1082117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray    state_ = current_state;
1094cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray    NotifyAdapterStateChanged(ADAPTER_STATE_TURNING_OFF, state_);
1104fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    return false;
1114fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  }
1124fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
1134fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  return true;
1144fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray}
1154fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
11603b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguraystd::string Adapter::GetName() const {
11703b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray  return name_.Get();
11803b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray}
11903b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray
1204fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguraybool Adapter::SetName(const std::string& name) {
1214fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  bt_bdname_t hal_name;
1224fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  size_t max_name_len = sizeof(hal_name.name);
1234fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
1244fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  // Include the \0 byte in size measurement.
1254fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  if (name.length() >= max_name_len) {
1264fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    LOG(ERROR) << "Given name \"" << name << "\" is larger than maximum allowed"
1274fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray               << " size: " << max_name_len;
1284fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    return false;
1294fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  }
1304fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
1314fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  strncpy(reinterpret_cast<char*>(hal_name.name), name.c_str(),
1324fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray          name.length() + 1);
1334fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
1344fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  VLOG(1) << "Setting adapter name: " << name;
1354fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
1364fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  if (!SetAdapterProperty(BT_PROPERTY_BDNAME, &hal_name, sizeof(hal_name))) {
1374fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    LOG(ERROR) << "Failed to set adapter name: " << name;
1384fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    return false;
1394fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  }
1404fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
1414fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  return true;
1424fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray}
1434fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
14403b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguraystd::string Adapter::GetAddress() const {
14503b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray  return address_.Get();
14603b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray}
14703b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray
1484fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Ugurayvoid Adapter::AdapterStateChangedCallback(bt_state_t state) {
1494fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  LOG(INFO) << "Adapter state changed: " << BtStateText(state);
1504fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
1514cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray  AdapterState prev_state = GetState();
1524cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray
1534fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  switch (state) {
1544fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  case BT_STATE_OFF:
1552117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray    state_ = ADAPTER_STATE_OFF;
1564fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    break;
1574fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
1584fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  case BT_STATE_ON:
1592117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray    state_ = ADAPTER_STATE_ON;
1604fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    break;
1614fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
1624fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  default:
1634fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    NOTREACHED();
1644fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  }
1652117e520c9f5b105ade7e92c4ab4928ea905f176Arman Uguray
1664cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray  NotifyAdapterStateChanged(prev_state, GetState());
1674fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray}
1684fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
16903b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Ugurayvoid Adapter::AdapterPropertiesCallback(bt_status_t status,
17003b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray                                        int num_properties,
17103b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray                                        bt_property_t* properties) {
17203b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray  LOG(INFO) << "Adapter properties changed";
17303b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray
17403b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray  if (status != BT_STATUS_SUCCESS) {
17503b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray    LOG(ERROR) << "status: " << BtStatusText(status);
17603b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray    return;
17703b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray  }
17803b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray
17903b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray  for (int i = 0; i < num_properties; i++) {
18003b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray    bt_property_t* property = properties + i;
18103b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray    switch (property->type) {
18203b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray      case BT_PROPERTY_BDADDR: {
18303b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray        std::string address = BtAddrString(reinterpret_cast<bt_bdaddr_t*>(
18403b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray            property->val));
18503b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray        LOG(INFO) << "Adapter address changed: " << address;
18603b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray        address_.Set(address);
18703b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray        break;
18803b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray      }
18903b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray      case BT_PROPERTY_BDNAME: {
19003b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray        bt_bdname_t* hal_name = reinterpret_cast<bt_bdname_t*>(property->val);
19103b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray        std::string name = reinterpret_cast<char*>(hal_name->name);
19203b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray        LOG(INFO) << "Adapter name changed: " << name;
19303b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray        name_.Set(name);
19403b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray        break;
19503b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray      }
19603b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray      default:
19703b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray        VLOG(1) << "Unhandled adapter property: "
19803b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray                << BtPropertyText(property->type);
19903b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray        break;
20003b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray    }
20103b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray
20203b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray    // TODO(armansito): notify others of the updated properties
20303b1f0fb7247ddc1c8496bf3b1bdc056110d12faArman Uguray  }
2044fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray}
2054fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
2064fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguraybool Adapter::SetAdapterProperty(bt_property_type_t type,
2074fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray                                 void* value, int length) {
2084fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  CHECK(length > 0);
2094fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  CHECK(value);
2104fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
2114fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  bt_property_t property;
2124fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  property.len = length;
2134fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  property.val = value;
2144fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  property.type = type;
2154fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
2164fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  int status = hal::BluetoothInterface::Get()->GetHALInterface()->
2174fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray      set_adapter_property(&property);
2184fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  if (status != BT_STATUS_SUCCESS) {
2194fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    VLOG(1) << "Failed to set property";
2204fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray    return false;
2214fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  }
2224fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
2234fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray  return true;
2244fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray}
2254fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray
2264cebc7e190f29acb56ef8658a9ad02137e82e967Arman Ugurayvoid Adapter::NotifyAdapterStateChanged(AdapterState prev_state,
2274cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray                                        AdapterState new_state) {
2284cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray  if (prev_state == new_state)
2294cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray    return;
2304cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray
2314cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray  std::lock_guard<std::mutex> lock(observers_lock_);
2324cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray  FOR_EACH_OBSERVER(Observer, observers_,
2334cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray                    OnAdapterStateChanged(this, prev_state, new_state));
2344cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray}
2354cebc7e190f29acb56ef8658a9ad02137e82e967Arman Uguray
2364fdadb6c834ec5299f47fc4e1c59c00410e07a45Arman Uguray}  // namespace bluetooth
237