OSDN Git Service

Move btif_state_machine to common/StateMachine
authorHansong Zhang <hsz@google.com>
Thu, 4 Oct 2018 22:04:18 +0000 (15:04 -0700)
committerHansong Zhang <hsz@google.com>
Fri, 5 Oct 2018 02:02:53 +0000 (19:02 -0700)
In Bluetooth native infrastructure refactor plan, we need a state
machine class for entire stack

Test: manual
Change-Id: I0e6671e42570482d9a9466b117054b3aadb3e9cc

btif/Android.bp
btif/src/btif_av.cc
common/Android.bp
common/state_machine.h [moved from btif/include/btif_state_machine.h with 91% similarity]
common/state_machine_unittest.cc [moved from btif/test/btif_state_machine_test.cc with 93% similarity]
test/run_host_unit_tests.py
test/run_unit_tests.sh

index b329a02..88e74c4 100644 (file)
@@ -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"],
-}
index 345f577..ff625d1 100644 (file)
@@ -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
index f0388a9..b8e7753 100644 (file)
@@ -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: [
similarity index 91%
rename from btif/include/btif_state_machine.h
rename to common/state_machine.h
index 49b1583..62d92d2 100644 (file)
  * 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
similarity index 93%
rename from btif/test/btif_state_machine_test.cc
rename to common/state_machine_unittest.cc
index d413a4a..896e983 100644 (file)
@@ -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());
index f702893..c168b3b 100755 (executable)
@@ -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',
index ca392cf..1ff0a39 100755 (executable)
@@ -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