Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 83676d8d authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Move btif_state_machine to common/StateMachine"

parents ce1de8b0 8cd8c26b
Loading
Loading
Loading
Loading
+0 −22
Original line number Diff line number Diff line
@@ -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"],
}
+2 −2
Original line number Diff line number Diff line
@@ -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
+1 −0
Original line number Diff line number Diff line
@@ -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: [
+17 −12
Original line number Diff line number Diff line
@@ -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
+15 −13
Original line number Diff line number Diff line
@@ -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());
Loading