Loading system/service/Android.mk +3 −1 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ LOCAL_PATH:= $(call my-dir) # ======================================================== btserviceCommonSrc := \ adapter.cpp \ adapter_state.cpp \ daemon.cpp \ gatt_server.cpp \ hal/bluetooth_interface.cpp \ Loading Loading @@ -66,8 +67,8 @@ ifeq ($(HOST_OS),linux) include $(CLEAR_VARS) LOCAL_SRC_FILES := \ $(btserviceCommonSrc) \ hal/fake_bluetooth_interface.cpp \ test/adapter_unittest.cpp \ test/fake_hal_bluetooth_interface.cpp \ test/fake_hal_util.cpp \ test/ipc_unix_unittest.cpp \ test/settings_unittest.cpp \ Loading @@ -87,6 +88,7 @@ endif include $(CLEAR_VARS) LOCAL_SRC_FILES := \ $(btserviceBinderSrc) \ adapter_state.cpp \ client/main.cpp LOCAL_C_INCLUDES += $(btserviceCommonIncludes) LOCAL_CFLAGS += -std=c++11 Loading system/service/adapter.cpp +34 −12 Original line number Diff line number Diff line Loading @@ -22,50 +22,70 @@ namespace bluetooth { Adapter::Adapter() : enabled_(false) { Adapter::Adapter() : state_(ADAPTER_STATE_OFF) { hal::BluetoothInterface::Get()->AddObserver(this); // TODO(armansito): Get all initial adapter properties here and set local // state. hal::BluetoothInterface::Get()->GetHALInterface()->get_adapter_properties(); } Adapter::~Adapter() { hal::BluetoothInterface::Get()->RemoveObserver(this); } bool Adapter::IsEnabled() { return enabled_.load(); AdapterState Adapter::GetState() const { return state_.load(); } bool Adapter::IsEnabled() const { return state_.load() == ADAPTER_STATE_ON; } bool Adapter::Enable() { if (enabled_.load()) { LOG(INFO) << "Adapter already enabled"; AdapterState current_state = GetState(); if (current_state != ADAPTER_STATE_OFF) { LOG(INFO) << "Adapter not disabled - state: " << AdapterStateToString(current_state); return false; } // Set the state before calling enable() as there might be a race between here // and the AdapterStateChangedCallback. state_ = ADAPTER_STATE_TURNING_ON; int status = hal::BluetoothInterface::Get()->GetHALInterface()->enable(); if (status != BT_STATUS_SUCCESS) { LOG(ERROR) << "Failed to enable Bluetooth - status: " << BtStatusText((const bt_status_t)status); state_ = ADAPTER_STATE_OFF; return false; } // TODO(armansito): Notify others of the state change. return true; } bool Adapter::Disable() { if (!enabled_) { LOG(INFO) << "Adapter already disabled"; if (!IsEnabled()) { LOG(INFO) << "Adapter is not enabled"; return false; } AdapterState current_state = GetState(); // Set the state before calling enable() as there might be a race between here // and the AdapterStateChangedCallback. state_ = ADAPTER_STATE_TURNING_OFF; int status = hal::BluetoothInterface::Get()->GetHALInterface()->disable(); if (status != BT_STATUS_SUCCESS) { LOG(ERROR) << "Failed to disable Bluetooth - status: " << BtStatusText((const bt_status_t)status); state_ = current_state; return false; } // TODO(armansito): Notify others of the state change. return true; } Loading Loading @@ -98,16 +118,18 @@ void Adapter::AdapterStateChangedCallback(bt_state_t state) { switch (state) { case BT_STATE_OFF: enabled_ = false; state_ = ADAPTER_STATE_OFF; break; case BT_STATE_ON: enabled_ = true; state_ = ADAPTER_STATE_ON; break; default: NOTREACHED(); } // TODO(armansito): Notify others of the state change. } void Adapter::AdapterPropertiesCallback(bt_status_t /* status */, Loading system/service/adapter.h +8 −4 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ #include <base/macros.h> #include "service/adapter_state.h" #include "service/hal/bluetooth_interface.h" namespace bluetooth { Loading @@ -30,10 +31,13 @@ namespace bluetooth { class Adapter : hal::BluetoothInterface::Observer { public: Adapter(); ~Adapter(); ~Adapter() override; // Returns the current Adapter state. AdapterState GetState() const; // Returns true, if the adapter radio is current powered. bool IsEnabled(); bool IsEnabled() const; // Enables Bluetooth. This method will send a request to the Bluetooth adapter // to power up its radio. Returns true, if the request was successfully sent Loading @@ -60,8 +64,8 @@ class Adapter : hal::BluetoothInterface::Observer { // Sends a request to set the given HAL adapter property type and value. bool SetAdapterProperty(bt_property_type_t type, void* value, int length); // True if the adapter radio is currently powered. std::atomic_bool enabled_; // The current adapter state. std::atomic<AdapterState> state_; DISALLOW_COPY_AND_ASSIGN(Adapter); }; Loading system/service/test/fake_hal_bluetooth_interface.cpp→system/service/adapter_state.cpp +44 −0 Original line number Diff line number Diff line Loading @@ -14,35 +14,31 @@ // limitations under the License. // #include "service/test/fake_hal_bluetooth_interface.h" #include "service/adapter_state.h" namespace bluetooth { namespace testing { FakeHALBluetoothInterface::FakeHALBluetoothInterface(bt_interface_t* hal_iface) : hal_iface_(hal_iface) { std::string AdapterStateToString(AdapterState state) { switch (state) { case ADAPTER_STATE_DISCONNECTED: return "ADAPTER_STATE_DISCONNECTED"; case ADAPTER_STATE_CONNECTING: return "ADAPTER_STATE_CONNECTING"; case ADAPTER_STATE_CONNECTED: return "ADAPTER_STATE_CONNECTED"; case ADAPTER_STATE_DISCONNECTING: return "ADAPTER_STATE_DISCONNECTING"; case ADAPTER_STATE_OFF: return "ADAPTER_STATE_OFF"; case ADAPTER_STATE_TURNING_ON: return "ADAPTER_STATE_TURNING_ON"; case ADAPTER_STATE_ON: return "ADAPTER_STATE_ON"; case ADAPTER_STATE_TURNING_OFF: return "ADAPTER_STATE_TURNING_OFF"; default: return "unknown state"; } void FakeHALBluetoothInterface::NotifyAdapterStateChanged(bt_state_t state) { FOR_EACH_OBSERVER(Observer, observers_, AdapterStateChangedCallback(state)); } void FakeHALBluetoothInterface::AddObserver(Observer* observer) { observers_.AddObserver(observer); } void FakeHALBluetoothInterface::RemoveObserver(Observer* observer) { observers_.RemoveObserver(observer); } const bt_interface_t* FakeHALBluetoothInterface::GetHALInterface() const { return hal_iface_; } const bluetooth_device_t* FakeHALBluetoothInterface::GetHALAdapter() const { // TODO(armansito): Do something meaningful here to simulate test behavior. return nullptr; } } // namespace testing } // namespace bluetooth system/service/adapter_state.h 0 → 100644 +42 −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 <string> namespace bluetooth { // Possible Adapter states. The values for each enumration have been copied // from frameworks/base/core/java/android/bluetooth/BluetoothAdapter.java. // These values need to match their android.bluetooth.BluetoothAdapter // counterparts for this to be compatible with the framework, hence we // redeclare them here. enum AdapterState { ADAPTER_STATE_DISCONNECTED = 0, ADAPTER_STATE_CONNECTING = 1, ADAPTER_STATE_CONNECTED = 2, ADAPTER_STATE_DISCONNECTING = 3, ADAPTER_STATE_OFF = 10, ADAPTER_STATE_TURNING_ON = 11, ADAPTER_STATE_ON = 12, ADAPTER_STATE_TURNING_OFF = 13, }; // Returns a string for the given Adapter state |state|. std::string AdapterStateToString(AdapterState state); } // namespace bluetooth Loading
system/service/Android.mk +3 −1 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ LOCAL_PATH:= $(call my-dir) # ======================================================== btserviceCommonSrc := \ adapter.cpp \ adapter_state.cpp \ daemon.cpp \ gatt_server.cpp \ hal/bluetooth_interface.cpp \ Loading Loading @@ -66,8 +67,8 @@ ifeq ($(HOST_OS),linux) include $(CLEAR_VARS) LOCAL_SRC_FILES := \ $(btserviceCommonSrc) \ hal/fake_bluetooth_interface.cpp \ test/adapter_unittest.cpp \ test/fake_hal_bluetooth_interface.cpp \ test/fake_hal_util.cpp \ test/ipc_unix_unittest.cpp \ test/settings_unittest.cpp \ Loading @@ -87,6 +88,7 @@ endif include $(CLEAR_VARS) LOCAL_SRC_FILES := \ $(btserviceBinderSrc) \ adapter_state.cpp \ client/main.cpp LOCAL_C_INCLUDES += $(btserviceCommonIncludes) LOCAL_CFLAGS += -std=c++11 Loading
system/service/adapter.cpp +34 −12 Original line number Diff line number Diff line Loading @@ -22,50 +22,70 @@ namespace bluetooth { Adapter::Adapter() : enabled_(false) { Adapter::Adapter() : state_(ADAPTER_STATE_OFF) { hal::BluetoothInterface::Get()->AddObserver(this); // TODO(armansito): Get all initial adapter properties here and set local // state. hal::BluetoothInterface::Get()->GetHALInterface()->get_adapter_properties(); } Adapter::~Adapter() { hal::BluetoothInterface::Get()->RemoveObserver(this); } bool Adapter::IsEnabled() { return enabled_.load(); AdapterState Adapter::GetState() const { return state_.load(); } bool Adapter::IsEnabled() const { return state_.load() == ADAPTER_STATE_ON; } bool Adapter::Enable() { if (enabled_.load()) { LOG(INFO) << "Adapter already enabled"; AdapterState current_state = GetState(); if (current_state != ADAPTER_STATE_OFF) { LOG(INFO) << "Adapter not disabled - state: " << AdapterStateToString(current_state); return false; } // Set the state before calling enable() as there might be a race between here // and the AdapterStateChangedCallback. state_ = ADAPTER_STATE_TURNING_ON; int status = hal::BluetoothInterface::Get()->GetHALInterface()->enable(); if (status != BT_STATUS_SUCCESS) { LOG(ERROR) << "Failed to enable Bluetooth - status: " << BtStatusText((const bt_status_t)status); state_ = ADAPTER_STATE_OFF; return false; } // TODO(armansito): Notify others of the state change. return true; } bool Adapter::Disable() { if (!enabled_) { LOG(INFO) << "Adapter already disabled"; if (!IsEnabled()) { LOG(INFO) << "Adapter is not enabled"; return false; } AdapterState current_state = GetState(); // Set the state before calling enable() as there might be a race between here // and the AdapterStateChangedCallback. state_ = ADAPTER_STATE_TURNING_OFF; int status = hal::BluetoothInterface::Get()->GetHALInterface()->disable(); if (status != BT_STATUS_SUCCESS) { LOG(ERROR) << "Failed to disable Bluetooth - status: " << BtStatusText((const bt_status_t)status); state_ = current_state; return false; } // TODO(armansito): Notify others of the state change. return true; } Loading Loading @@ -98,16 +118,18 @@ void Adapter::AdapterStateChangedCallback(bt_state_t state) { switch (state) { case BT_STATE_OFF: enabled_ = false; state_ = ADAPTER_STATE_OFF; break; case BT_STATE_ON: enabled_ = true; state_ = ADAPTER_STATE_ON; break; default: NOTREACHED(); } // TODO(armansito): Notify others of the state change. } void Adapter::AdapterPropertiesCallback(bt_status_t /* status */, Loading
system/service/adapter.h +8 −4 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ #include <base/macros.h> #include "service/adapter_state.h" #include "service/hal/bluetooth_interface.h" namespace bluetooth { Loading @@ -30,10 +31,13 @@ namespace bluetooth { class Adapter : hal::BluetoothInterface::Observer { public: Adapter(); ~Adapter(); ~Adapter() override; // Returns the current Adapter state. AdapterState GetState() const; // Returns true, if the adapter radio is current powered. bool IsEnabled(); bool IsEnabled() const; // Enables Bluetooth. This method will send a request to the Bluetooth adapter // to power up its radio. Returns true, if the request was successfully sent Loading @@ -60,8 +64,8 @@ class Adapter : hal::BluetoothInterface::Observer { // Sends a request to set the given HAL adapter property type and value. bool SetAdapterProperty(bt_property_type_t type, void* value, int length); // True if the adapter radio is currently powered. std::atomic_bool enabled_; // The current adapter state. std::atomic<AdapterState> state_; DISALLOW_COPY_AND_ASSIGN(Adapter); }; Loading
system/service/test/fake_hal_bluetooth_interface.cpp→system/service/adapter_state.cpp +44 −0 Original line number Diff line number Diff line Loading @@ -14,35 +14,31 @@ // limitations under the License. // #include "service/test/fake_hal_bluetooth_interface.h" #include "service/adapter_state.h" namespace bluetooth { namespace testing { FakeHALBluetoothInterface::FakeHALBluetoothInterface(bt_interface_t* hal_iface) : hal_iface_(hal_iface) { std::string AdapterStateToString(AdapterState state) { switch (state) { case ADAPTER_STATE_DISCONNECTED: return "ADAPTER_STATE_DISCONNECTED"; case ADAPTER_STATE_CONNECTING: return "ADAPTER_STATE_CONNECTING"; case ADAPTER_STATE_CONNECTED: return "ADAPTER_STATE_CONNECTED"; case ADAPTER_STATE_DISCONNECTING: return "ADAPTER_STATE_DISCONNECTING"; case ADAPTER_STATE_OFF: return "ADAPTER_STATE_OFF"; case ADAPTER_STATE_TURNING_ON: return "ADAPTER_STATE_TURNING_ON"; case ADAPTER_STATE_ON: return "ADAPTER_STATE_ON"; case ADAPTER_STATE_TURNING_OFF: return "ADAPTER_STATE_TURNING_OFF"; default: return "unknown state"; } void FakeHALBluetoothInterface::NotifyAdapterStateChanged(bt_state_t state) { FOR_EACH_OBSERVER(Observer, observers_, AdapterStateChangedCallback(state)); } void FakeHALBluetoothInterface::AddObserver(Observer* observer) { observers_.AddObserver(observer); } void FakeHALBluetoothInterface::RemoveObserver(Observer* observer) { observers_.RemoveObserver(observer); } const bt_interface_t* FakeHALBluetoothInterface::GetHALInterface() const { return hal_iface_; } const bluetooth_device_t* FakeHALBluetoothInterface::GetHALAdapter() const { // TODO(armansito): Do something meaningful here to simulate test behavior. return nullptr; } } // namespace testing } // namespace bluetooth
system/service/adapter_state.h 0 → 100644 +42 −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 <string> namespace bluetooth { // Possible Adapter states. The values for each enumration have been copied // from frameworks/base/core/java/android/bluetooth/BluetoothAdapter.java. // These values need to match their android.bluetooth.BluetoothAdapter // counterparts for this to be compatible with the framework, hence we // redeclare them here. enum AdapterState { ADAPTER_STATE_DISCONNECTED = 0, ADAPTER_STATE_CONNECTING = 1, ADAPTER_STATE_CONNECTED = 2, ADAPTER_STATE_DISCONNECTING = 3, ADAPTER_STATE_OFF = 10, ADAPTER_STATE_TURNING_ON = 11, ADAPTER_STATE_ON = 12, ADAPTER_STATE_TURNING_OFF = 13, }; // Returns a string for the given Adapter state |state|. std::string AdapterStateToString(AdapterState state); } // namespace bluetooth