summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Hansong Zhang <hsz@google.com> 2018-10-04 15:04:18 -0700
committer Hansong Zhang <hsz@google.com> 2018-10-04 19:02:53 -0700
commit8cd8c26b85fc220f181d8dc0d9746eb695003eab (patch)
tree7c7df1db13cfd73172879651df598ca46ac0bd7b
parent30abea02e3565372e6a93be60ea6356a176a23be (diff)
Move btif_state_machine to common/StateMachine
In Bluetooth native infrastructure refactor plan, we need a state machine class for entire stack Test: manual Change-Id: I0e6671e42570482d9a9466b117054b3aadb3e9cc
-rw-r--r--system/btif/Android.bp22
-rw-r--r--system/btif/src/btif_av.cc4
-rw-r--r--system/common/Android.bp1
-rw-r--r--system/common/state_machine.h (renamed from system/btif/include/btif_state_machine.h)29
-rw-r--r--system/common/state_machine_unittest.cc (renamed from system/btif/test/btif_state_machine_test.cc)28
-rwxr-xr-xsystem/test/run_host_unit_tests.py1
-rwxr-xr-xsystem/test/run_unit_tests.sh1
7 files changed, 35 insertions, 51 deletions
diff --git a/system/btif/Android.bp b/system/btif/Android.bp
index c122d1e506..9a0d38126e 100644
--- a/system/btif/Android.bp
+++ b/system/btif/Android.bp
@@ -173,25 +173,3 @@ cc_test {
],
cflags: ["-DBUILDCFG"],
}
-
-// btif state machine unit tests for target
-// ========================================================
-cc_test {
- name: "net_test_btif_state_machine",
- defaults: ["fluoride_defaults"],
- include_dirs: btifCommonIncludes,
- host_supported: true,
- srcs: [
- "test/btif_state_machine_test.cc"
- ],
- header_libs: ["libbluetooth_headers"],
- shared_libs: [
- "liblog",
- "libcutils",
- ],
- static_libs: [
- "libbluetooth-types",
- "libosi",
- ],
- cflags: ["-DBUILDCFG"],
-}
diff --git a/system/btif/src/btif_av.cc b/system/btif/src/btif_av.cc
index 345f577fa2..ff625d108b 100644
--- a/system/btif/src/btif_av.cc
+++ b/system/btif/src/btif_av.cc
@@ -42,9 +42,9 @@
#include "btif_av_co.h"
#include "btif_profile_queue.h"
#include "btif_rc.h"
-#include "btif_state_machine.h"
#include "btif_util.h"
#include "btu.h"
+#include "common/state_machine.h"
#include "osi/include/allocator.h"
#include "osi/include/osi.h"
#include "osi/include/properties.h"
@@ -114,7 +114,7 @@ class BtifAvPeer;
// different than Open state. Suspend flags are needed however to prevent
// media task from trying to restart stream during remote Suspend or while
// we are in the process of a local Suspend.
-class BtifAvStateMachine : public BtifStateMachine {
+class BtifAvStateMachine : public bluetooth::common::StateMachine {
public:
enum {
kStateIdle, // AVDTP disconnected
diff --git a/system/common/Android.bp b/system/common/Android.bp
index 2e2fcad03e..32784e3e7d 100644
--- a/system/common/Android.bp
+++ b/system/common/Android.bp
@@ -31,6 +31,7 @@ cc_test {
"leaky_bonded_queue_unittest.cc",
"message_loop_thread_unittest.cc",
"metrics_unittest.cc",
+ "state_machine_unittest.cc",
"time_util_unittest.cc",
],
shared_libs: [
diff --git a/system/btif/include/btif_state_machine.h b/system/common/state_machine.h
index 49b15835b4..62d92d2636 100644
--- a/system/btif/include/btif_state_machine.h
+++ b/system/common/state_machine.h
@@ -14,18 +14,21 @@
* limitations under the License.
*/
-#ifndef BTIF_STATE_MACHINE_H
-#define BTIF_STATE_MACHINE_H
+#pragma once
#include <map>
#include <utility>
#include <base/logging.h>
+namespace bluetooth {
+
+namespace common {
+
/**
- * State machine used by BTIF components.
+ * State machine used by Bluetooth native stack.
*/
-class BtifStateMachine {
+class StateMachine {
public:
enum { kStateInvalid = -1 };
@@ -33,7 +36,7 @@ class BtifStateMachine {
* A class to represent the state in the State Machine.
*/
class State {
- friend class BtifStateMachine;
+ friend class StateMachine;
public:
/**
@@ -42,7 +45,7 @@ class BtifStateMachine {
* @param sm the State Machine to use
* @param state_id the unique State ID. It should be a non-negative number.
*/
- State(BtifStateMachine& sm, int state_id) : sm_(sm), state_id_(state_id) {}
+ State(StateMachine& sm, int state_id) : sm_(sm), state_id_(state_id) {}
virtual ~State() = default;
@@ -88,20 +91,20 @@ class BtifStateMachine {
*
* @param dest_state the state to transition to. It cannot be nullptr.
*/
- void TransitionTo(BtifStateMachine::State* dest_state) {
+ void TransitionTo(StateMachine::State* dest_state) {
sm_.TransitionTo(dest_state);
}
private:
- BtifStateMachine& sm_;
+ StateMachine& sm_;
int state_id_;
};
- BtifStateMachine()
+ StateMachine()
: initial_state_(nullptr),
previous_state_(nullptr),
current_state_(nullptr) {}
- ~BtifStateMachine() {
+ ~StateMachine() {
for (auto& kv : states_) delete kv.second;
}
@@ -172,7 +175,7 @@ class BtifStateMachine {
*
* @param dest_state the state to transition to. It cannot be nullptr.
*/
- void TransitionTo(BtifStateMachine::State* dest_state) {
+ void TransitionTo(StateMachine::State* dest_state) {
if (current_state_ != nullptr) {
current_state_->OnExit();
}
@@ -206,4 +209,6 @@ class BtifStateMachine {
std::map<int, State*> states_;
};
-#endif // BTIF_STATE_MACHINE_H
+} // namespace common
+
+} // namespace bluetooth
diff --git a/system/btif/test/btif_state_machine_test.cc b/system/common/state_machine_unittest.cc
index d413a4ab20..896e983357 100644
--- a/system/btif/test/btif_state_machine_test.cc
+++ b/system/common/state_machine_unittest.cc
@@ -18,7 +18,9 @@
#include <gtest/gtest.h>
-#include "btif/include/btif_state_machine.h"
+#include "common/state_machine.h"
+
+using bluetooth::common::StateMachine;
namespace {
static constexpr uint32_t kInvalidEvent = 0xffffffff;
@@ -31,7 +33,7 @@ static char dataOne = 1;
static char dataTwo = 2;
} // namespace
-class BtifStateMachineImpl : public BtifStateMachine {
+class StateMachineImpl : public StateMachine {
public:
enum {
kStateZero,
@@ -41,7 +43,7 @@ class BtifStateMachineImpl : public BtifStateMachine {
class StateZero : public State {
public:
- StateZero(BtifStateMachine& sm)
+ StateZero(StateMachine& sm)
: State(sm, kStateZero),
on_enter_(false),
on_exit_(false),
@@ -70,7 +72,7 @@ class BtifStateMachineImpl : public BtifStateMachine {
class StateOne : public State {
public:
- StateOne(BtifStateMachine& sm)
+ StateOne(StateMachine& sm)
: State(sm, kStateOne),
on_enter_(false),
on_exit_(false),
@@ -99,7 +101,7 @@ class BtifStateMachineImpl : public BtifStateMachine {
class StateTwo : public State {
public:
- StateTwo(BtifStateMachine& sm)
+ StateTwo(StateMachine& sm)
: State(sm, kStateTwo),
on_enter_(false),
on_exit_(false),
@@ -126,7 +128,7 @@ class BtifStateMachineImpl : public BtifStateMachine {
void* data_;
};
- BtifStateMachineImpl() {
+ StateMachineImpl() {
state_zero_ = new StateZero(*this);
state_one_ = new StateOne(*this);
state_two_ = new StateTwo(*this);
@@ -142,23 +144,23 @@ class BtifStateMachineImpl : public BtifStateMachine {
StateTwo* state_two_;
};
-class BtifStateMachineTest : public ::testing::Test {
+class StateMachineTest : public ::testing::Test {
protected:
- BtifStateMachineTest() {}
+ StateMachineTest() {}
void SetUp() override { sm_.Start(); }
void TearDown() override { sm_.Quit(); }
- BtifStateMachineImpl sm_;
+ StateMachineImpl sm_;
};
-TEST_F(BtifStateMachineTest, test_initial_state) {
+TEST_F(StateMachineTest, test_initial_state) {
ASSERT_EQ(sm_.kStateZero, sm_.StateId());
ASSERT_EQ(sm_.kStateInvalid, sm_.PreviousStateId());
}
-TEST_F(BtifStateMachineTest, test_invalid_state) {
+TEST_F(StateMachineTest, test_invalid_state) {
sm_.Quit();
ASSERT_EQ(sm_.kStateInvalid, sm_.StateId());
ASSERT_EQ(sm_.kStateInvalid, sm_.PreviousStateId());
@@ -167,7 +169,7 @@ TEST_F(BtifStateMachineTest, test_invalid_state) {
ASSERT_EQ(sm_.kStateInvalid, sm_.PreviousStateId());
}
-TEST_F(BtifStateMachineTest, test_transition_to) {
+TEST_F(StateMachineTest, test_transition_to) {
// Initial state: StateZero
ASSERT_EQ(sm_.kStateZero, sm_.StateId());
ASSERT_EQ(sm_.kStateInvalid, sm_.PreviousStateId());
@@ -195,7 +197,7 @@ TEST_F(BtifStateMachineTest, test_transition_to) {
ASSERT_FALSE(sm_.state_two_->on_exit_);
}
-TEST_F(BtifStateMachineTest, test_process_event) {
+TEST_F(StateMachineTest, test_process_event) {
// Initial state: StateZero
ASSERT_EQ(sm_.kStateZero, sm_.StateId());
ASSERT_EQ(sm_.kStateInvalid, sm_.PreviousStateId());
diff --git a/system/test/run_host_unit_tests.py b/system/test/run_host_unit_tests.py
index f702893f7a..c168b3b219 100755
--- a/system/test/run_host_unit_tests.py
+++ b/system/test/run_host_unit_tests.py
@@ -24,7 +24,6 @@ HOST_TESTS = [
'bluetooth_test_common',
'bluetoothtbd_test',
'net_test_avrcp',
- 'net_test_btif_state_machine',
'net_test_btcore',
'net_test_types',
'net_test_btpackets',
diff --git a/system/test/run_unit_tests.sh b/system/test/run_unit_tests.sh
index ca392cf7ce..1ff0a39564 100755
--- a/system/test/run_unit_tests.sh
+++ b/system/test/run_unit_tests.sh
@@ -10,7 +10,6 @@ known_tests=(
net_test_bta
net_test_btif
net_test_btif_profile_queue
- net_test_btif_state_machine
net_test_device
net_test_hci
net_test_stack