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

Commit 5bb7de67 authored by Zach Johnson's avatar Zach Johnson
Browse files

Call btu_task_start_up from stack manager

it's also a blockable context

remove stack test that will no longer be valid shortly

Bug: 159815595
Tag: #refactor
Test: compile & verify basic functions working
Change-Id: I137134bf426fa5a727826bcfc8ab3676ce6884ea
parent a7f3195a
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -58,6 +58,8 @@ static void event_clean_up_stack(void* context);
static void event_signal_stack_up(void* context);
static void event_signal_stack_up(void* context);
static void event_signal_stack_down(void* context);
static void event_signal_stack_down(void* context);


void btu_task_start_up();

// Unvetted includes/imports, etc which should be removed or vetted in the
// Unvetted includes/imports, etc which should be removed or vetted in the
// future
// future
static future_t* hack_future;
static future_t* hack_future;
@@ -164,6 +166,7 @@ static void event_start_up_stack(UNUSED_ATTR void* context) {
  }
  }


  BTU_StartUp();
  BTU_StartUp();
  btu_task_start_up();


  if (future_await(local_hack_future) != FUTURE_SUCCESS) {
  if (future_await(local_hack_future) != FUTURE_SUCCESS) {
    LOG_ERROR("%s failed to start up the stack", __func__);
    LOG_ERROR("%s failed to start up the stack", __func__);
+0 −36
Original line number Original line Diff line number Diff line
@@ -398,42 +398,6 @@ cc_test {
    ],
    ],
}
}


// Bluetooth stack message loop tests for target
// ========================================================
cc_test {
    name: "net_test_btu_message_loop",
    defaults: ["fluoride_defaults"],
    local_include_dirs: [
        "include",
        "btm",
    ],
    include_dirs: [
        "packages/modules/Bluetooth/system/",
        "packages/modules/Bluetooth/system/internal_include",
        "packages/modules/Bluetooth/system/btcore/include",
        "packages/modules/Bluetooth/system/bta/include",
    ],
    srcs: [
        "btu/btu_task.cc",
        "test/stack_btu_test.cc",
    ],
    shared_libs: [
        "liblog",
        "libcutils",
        "libprotobuf-cpp-lite",
    ],
    static_libs: [
        "libbt-common",
        "libbluetooth-types",
        "libgmock",
        "libosi",
        "libbt-protos-lite",
    ],
    sanitize: {
        cfi: false,
    },
}

// Bluetooth stack connection multiplexing
// Bluetooth stack connection multiplexing
// ========================================================
// ========================================================
cc_test {
cc_test {
+0 −7
Original line number Original line Diff line number Diff line
@@ -33,7 +33,6 @@ using bluetooth::common::MessageLoopThread;


MessageLoopThread bt_startup_thread("bt_startup_thread");
MessageLoopThread bt_startup_thread("bt_startup_thread");


void btu_task_start_up();
void btu_task_shut_down();
void btu_task_shut_down();


/*****************************************************************************
/*****************************************************************************
@@ -102,12 +101,6 @@ void BTU_StartUp() {
    BTU_ShutDown();
    BTU_ShutDown();
    return;
    return;
  }
  }
  if (!bt_startup_thread.DoInThread(FROM_HERE, base::Bind(btu_task_start_up))) {
    LOG(ERROR) << __func__ << ": Unable to continue start-up on "
               << bt_startup_thread;
    BTU_ShutDown();
    return;
  }
}
}


void BTU_ShutDown() {
void BTU_ShutDown() {
+0 −127
Original line number Original line Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2017 The Android Open Source Project
 *
 *  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.
 *
 ******************************************************************************/

#include <base/bind.h>
#include <base/logging.h>
#include <base/threading/thread.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <semaphore.h>
#include <time.h>
#include <unistd.h>

#include "btcore/include/module.h"
#include "common/message_loop_thread.h"
#include "osi/include/alarm.h"
#include "osi/include/fixed_queue.h"
#include "stack/include/btu.h"

class TimeoutHelper {
 public:
  TimeoutHelper() { sem_init(&sem, 0, 0); }

  ~TimeoutHelper() { sem_destroy(&sem); }

  void wait(int seconds, base::Closure callback) {
    struct timespec timeout;
    clock_gettime(CLOCK_REALTIME, &timeout);
    timeout.tv_sec += seconds;

    int semvalue;
    sem_getvalue(&sem, &semvalue);

    // Call the callback if timeout occured
    if (sem_timedwait(&sem, &timeout) == -1 && !callback.is_null()) {
      callback.Run();
    }
  }

  void notify() { sem_post(&sem); }

 private:
  sem_t sem;
};

TimeoutHelper helper;

// External function definitions
void btu_task_start_up();
void btu_task_shut_down();

/* Below are methods and variables that must be implemented if we don't want to
 * compile the whole stack. They will be removed, or changed into mocks one by
 * one in the future, as the refactoring progresses */
bt_status_t do_in_jni_thread(const base::Location& from_here,
                             base::OnceClosure task) {
  helper.notify();
  return BT_STATUS_SUCCESS;
}

void btu_init_core(){};
void btif_init_ok(unsigned short, char*){};
void BTE_InitStack(){};
void bta_sys_init(){};
void bta_sys_free(){};
void btu_free_core(){};
const module_t* get_module(const char*) { return nullptr; };
bool module_init(module_t const*) { return true; };
void module_clean_up(module_t const*){};

bluetooth::common::MessageLoopThread bt_startup_thread("test alarm thread");

class BtuMessageLoopTest : public testing::Test {
 public:
  MOCK_METHOD0(TestCallback, void(void));
  base::MessageLoop* message_loop;

  void SetUp() override {
    // Initialize alarms to prevent btu_task_shut_down from crashing
    alarm_new("test alarm");
    bt_startup_thread.StartUp();
    // btu_task_start_up calls btif_transfer_context to let the stack know
    // start up is finished
    btu_task_start_up();
    helper.wait(5, base::Bind(&BtuMessageLoopTest::Fail, base::Unretained(this),
                              "BTU startup timed out"));
  }

  void TearDown() override {
    btu_task_shut_down();
    alarm_cleanup();
    bt_startup_thread.ShutDown();
  }

  void Fail(std::string message) { FAIL() << message; }
};

TEST_F(BtuMessageLoopTest, send_message) {
  message_loop = get_main_message_loop();
  EXPECT_FALSE(message_loop == nullptr);

  EXPECT_CALL(*this, TestCallback()).Times(1);
  message_loop->task_runner()->PostTask(
      FROM_HERE,
      base::Bind(&BtuMessageLoopTest::TestCallback, base::Unretained(this)));

  message_loop->task_runner()->PostTask(
      FROM_HERE, base::Bind(&TimeoutHelper::notify, base::Unretained(&helper)));

  // Prevent the test from ending before the message loop posts the function
  helper.wait(5, base::Bind(&BtuMessageLoopTest::Fail, base::Unretained(this),
                            "Timed out waiting for callback"));
}
+0 −1
Original line number Original line Diff line number Diff line
@@ -20,7 +20,6 @@ known_tests=(
  net_test_stack_ad_parser
  net_test_stack_ad_parser
  net_test_stack_smp
  net_test_stack_smp
  net_test_types
  net_test_types
  net_test_btu_message_loop
  net_test_osi
  net_test_osi
  net_test_performance
  net_test_performance
  net_test_stack_rfcomm
  net_test_stack_rfcomm