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

Commit 2569ae9d authored by Ajay Panicker's avatar Ajay Panicker Committed by android-build-merger
Browse files

Merge "Swap BTA and HCI queues with message loops"

am: 7f1cb7b9

Change-Id: I499fd6260777c054fa1763d1febf9be01234f1bc
parents b525fa43 7f1cb7b9
Loading
Loading
Loading
Loading
+16 −5
Original line number Original line Diff line number Diff line
@@ -24,7 +24,9 @@


#define LOG_TAG "bt_bta_sys_main"
#define LOG_TAG "bt_bta_sys_main"


#include <base/bind.h>
#include <base/logging.h>
#include <base/logging.h>
#include <base/threading/thread.h>
#include <pthread.h>
#include <pthread.h>
#include <string.h>
#include <string.h>


@@ -34,6 +36,7 @@
#include "bta_sys.h"
#include "bta_sys.h"
#include "bta_sys_int.h"
#include "bta_sys_int.h"
#include "btm_api.h"
#include "btm_api.h"
#include "btu.h"
#include "osi/include/alarm.h"
#include "osi/include/alarm.h"
#include "osi/include/fixed_queue.h"
#include "osi/include/fixed_queue.h"
#include "osi/include/log.h"
#include "osi/include/log.h"
@@ -529,16 +532,23 @@ bool bta_sys_is_register(uint8_t id) { return bta_sys_cb.is_reg[id]; }
 *                  optimize sending of messages to BTA.  It is called by BTA
 *                  optimize sending of messages to BTA.  It is called by BTA
 *                  API functions and call-in functions.
 *                  API functions and call-in functions.
 *
 *
 *                  TODO (apanicke): Add location object as parameter for easier
 *                  future debugging when doing alarm refactor
 *
 *
 *
 * Returns          void
 * Returns          void
 *
 *
 ******************************************************************************/
 ******************************************************************************/
void bta_sys_sendmsg(void* p_msg) {
void bta_sys_sendmsg(void* p_msg) {
  // There is a race condition that occurs if the stack is shut down while
  base::MessageLoop* bta_message_loop = get_message_loop();
  // there is a procedure in progress that can schedule a task via this

  // message queue. This causes |btu_bta_msg_queue| to get cleaned up before
  if (!bta_message_loop || !bta_message_loop->task_runner().get()) {
  // it gets used here; hence we check for NULL before using it.
    APPL_TRACE_ERROR("%s: MessageLooper not initialized", __func__);
  if (btu_bta_msg_queue) fixed_queue_enqueue(btu_bta_msg_queue, p_msg);
    return;
  }

  bta_message_loop->task_runner()->PostTask(
      FROM_HERE, base::Bind(&bta_sys_event, static_cast<BT_HDR*>(p_msg)));
}
}


/*******************************************************************************
/*******************************************************************************
@@ -557,6 +567,7 @@ void bta_sys_start_timer(alarm_t* alarm, period_ms_t interval, uint16_t event,


  p_buf->event = event;
  p_buf->event = event;
  p_buf->layer_specific = layer_specific;
  p_buf->layer_specific = layer_specific;

  alarm_set_on_queue(alarm, interval, bta_sys_sendmsg, p_buf,
  alarm_set_on_queue(alarm, interval, bta_sys_sendmsg, p_buf,
                     btu_bta_alarm_queue);
                     btu_bta_alarm_queue);
}
}
+10 −8
Original line number Original line Diff line number Diff line
@@ -18,11 +18,12 @@


#pragma once
#pragma once


#include <base/bind.h>
#include <base/tracked_objects.h>
#include <stdbool.h>
#include <stdbool.h>


#include "bt_types.h"
#include "bt_types.h"
#include "osi/include/allocator.h"
#include "osi/include/allocator.h"
#include "osi/include/data_dispatcher.h"
#include "osi/include/fixed_queue.h"
#include "osi/include/fixed_queue.h"
#include "osi/include/future.h"
#include "osi/include/future.h"
#include "osi/include/osi.h"
#include "osi/include/osi.h"
@@ -67,12 +68,10 @@ typedef void (*command_status_cb)(uint8_t status, BT_HDR* command,
                                  void* context);
                                  void* context);


typedef struct hci_t {
typedef struct hci_t {
  // Register with this data dispatcher to receive events flowing upward out of
  // Set the callback that the HCI layer uses to send data upwards
  // the HCI layer
  void (*set_data_cb)(
  data_dispatcher_t* event_dispatcher;
      base::Callback<void(const tracked_objects::Location&, BT_HDR*)>

          send_data_cb);
  // Set the queue to receive ACL data in
  void (*set_data_queue)(fixed_queue_t* queue);


  // Send a command through the HCI layer
  // Send a command through the HCI layer
  void (*transmit_command)(BT_HDR* command,
  void (*transmit_command)(BT_HDR* command,
@@ -82,7 +81,7 @@ typedef struct hci_t {
  future_t* (*transmit_command_futured)(BT_HDR* command);
  future_t* (*transmit_command_futured)(BT_HDR* command);


  // Send some data downward through the HCI layer
  // Send some data downward through the HCI layer
  void (*transmit_downward)(data_dispatcher_type_t type, void* data);
  void (*transmit_downward)(uint16_t type, void* data);
} hci_t;
} hci_t;


const hci_t* hci_layer_get_interface();
const hci_t* hci_layer_get_interface();
@@ -92,4 +91,7 @@ const hci_t* hci_layer_get_test_interface(
    const btsnoop_t* btsnoop_interface,
    const btsnoop_t* btsnoop_interface,
    const packet_fragmenter_t* packet_fragmenter_interface);
    const packet_fragmenter_t* packet_fragmenter_interface);


void post_to_hci_message_loop(const tracked_objects::Location& from_here,
                              BT_HDR* p_msg);

void hci_layer_cleanup_interface();
void hci_layer_cleanup_interface();
+18 −19
Original line number Original line Diff line number Diff line
@@ -101,7 +101,8 @@ static list_t* commands_pending_response;
static std::recursive_mutex commands_pending_response_mutex;
static std::recursive_mutex commands_pending_response_mutex;


// The hand-off point for data going to a higher layer, set by the higher layer
// The hand-off point for data going to a higher layer, set by the higher layer
static fixed_queue_t* upwards_data_queue;
static base::Callback<void(const tracked_objects::Location&, BT_HDR*)>
    send_data_upwards;


static bool filter_incoming_event(BT_HDR* packet);
static bool filter_incoming_event(BT_HDR* packet);
static waiting_command_t* get_waiting_command(command_opcode_t opcode);
static waiting_command_t* get_waiting_command(command_opcode_t opcode);
@@ -130,12 +131,12 @@ void initialization_complete() {
      FROM_HERE, base::Bind(&event_finish_startup, nullptr));
      FROM_HERE, base::Bind(&event_finish_startup, nullptr));
}
}


void hci_event_received(BT_HDR* packet) {
void hci_event_received(const tracked_objects::Location& from_here,
                        BT_HDR* packet) {
  btsnoop->capture(packet, true);
  btsnoop->capture(packet, true);


  if (!filter_incoming_event(packet)) {
  if (!filter_incoming_event(packet)) {
    data_dispatcher_dispatch(interface.event_dispatcher, packet->data[0],
    send_data_upwards.Run(from_here, packet);
                             packet);
  }
  }
}
}


@@ -281,7 +282,11 @@ EXPORT_SYMBOL extern const module_t hci_module = {


// Interface functions
// Interface functions


static void set_data_queue(fixed_queue_t* queue) { upwards_data_queue = queue; }
static void set_data_cb(
    base::Callback<void(const tracked_objects::Location&, BT_HDR*)>
        send_data_cb) {
  send_data_upwards = std::move(send_data_cb);
}


static void transmit_command(BT_HDR* command,
static void transmit_command(BT_HDR* command,
                             command_complete_cb complete_callback,
                             command_complete_cb complete_callback,
@@ -321,7 +326,7 @@ static future_t* transmit_command_futured(BT_HDR* command) {
  return future;
  return future;
}
}


static void transmit_downward(data_dispatcher_type_t type, void* data) {
static void transmit_downward(uint16_t type, void* data) {
  if (type == MSG_STACK_TO_HC_HCI_CMD) {
  if (type == MSG_STACK_TO_HC_HCI_CMD) {
    // TODO(zachoverflow): eliminate this call
    // TODO(zachoverflow): eliminate this call
    transmit_command((BT_HDR*)data, NULL, NULL, NULL);
    transmit_command((BT_HDR*)data, NULL, NULL, NULL);
@@ -406,8 +411,8 @@ static void fragmenter_transmit_finished(BT_HDR* packet,
    // This is kind of a weird case, since we're dispatching a partially sent
    // This is kind of a weird case, since we're dispatching a partially sent
    // packet up to a higher layer.
    // packet up to a higher layer.
    // TODO(zachoverflow): rework upper layer so this isn't necessary.
    // TODO(zachoverflow): rework upper layer so this isn't necessary.
    data_dispatcher_dispatch(interface.event_dispatcher,

                             packet->event & MSG_EVT_MASK, packet);
    send_data_upwards.Run(FROM_HERE, packet);
  }
  }
}
}


@@ -538,9 +543,9 @@ intercepted:
static void dispatch_reassembled(BT_HDR* packet) {
static void dispatch_reassembled(BT_HDR* packet) {
  // Events should already have been dispatched before this point
  // Events should already have been dispatched before this point
  CHECK((packet->event & MSG_EVT_MASK) != MSG_HC_TO_STACK_HCI_EVT);
  CHECK((packet->event & MSG_EVT_MASK) != MSG_HC_TO_STACK_HCI_EVT);
  CHECK(upwards_data_queue != NULL);
  CHECK(!send_data_upwards.is_null());


  fixed_queue_enqueue(upwards_data_queue, packet);
  send_data_upwards.Run(FROM_HERE, packet);
}
}


// Misc internal functions
// Misc internal functions
@@ -579,13 +584,8 @@ static void init_layer_interface() {
  if (!interface_created) {
  if (!interface_created) {
    // It's probably ok for this to live forever. It's small and
    // It's probably ok for this to live forever. It's small and
    // there's only one instance of the hci interface.
    // there's only one instance of the hci interface.
    interface.event_dispatcher = data_dispatcher_new("hci_layer");
    if (!interface.event_dispatcher) {
      LOG_ERROR(LOG_TAG, "%s could not create upward dispatcher.", __func__);
      return;
    }


    interface.set_data_queue = set_data_queue;
    interface.set_data_cb = set_data_cb;
    interface.transmit_command = transmit_command;
    interface.transmit_command = transmit_command;
    interface.transmit_command_futured = transmit_command_futured;
    interface.transmit_command_futured = transmit_command_futured;
    interface.transmit_downward = transmit_downward;
    interface.transmit_downward = transmit_downward;
@@ -595,10 +595,9 @@ static void init_layer_interface() {


void hci_layer_cleanup_interface() {
void hci_layer_cleanup_interface() {
  if (interface_created) {
  if (interface_created) {
    data_dispatcher_free(interface.event_dispatcher);
    send_data_upwards.Reset();
    interface.event_dispatcher = NULL;


    interface.set_data_queue = NULL;
    interface.set_data_cb = NULL;
    interface.transmit_command = NULL;
    interface.transmit_command = NULL;
    interface.transmit_command_futured = NULL;
    interface.transmit_command_futured = NULL;
    interface.transmit_downward = NULL;
    interface.transmit_downward = NULL;
+4 −2
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@


#include "hci_layer.h"
#include "hci_layer.h"


#include <base/location.h>
#include <base/logging.h>
#include <base/logging.h>
#include "buffer_allocator.h"
#include "buffer_allocator.h"
#include "osi/include/log.h"
#include "osi/include/log.h"
@@ -39,7 +40,8 @@ using ::android::hardware::Void;
using ::android::hardware::hidl_vec;
using ::android::hardware::hidl_vec;


extern void initialization_complete();
extern void initialization_complete();
extern void hci_event_received(BT_HDR* packet);
extern void hci_event_received(const tracked_objects::Location& from_here,
                               BT_HDR* packet);
extern void acl_event_received(BT_HDR* packet);
extern void acl_event_received(BT_HDR* packet);
extern void sco_data_received(BT_HDR* packet);
extern void sco_data_received(BT_HDR* packet);


@@ -73,7 +75,7 @@ class BluetoothHciCallbacks : public IBluetoothHciCallbacks {


  Return<void> hciEventReceived(const hidl_vec<uint8_t>& event) {
  Return<void> hciEventReceived(const hidl_vec<uint8_t>& event) {
    BT_HDR* packet = WrapPacketAndCopy(MSG_HC_TO_STACK_HCI_EVT, event);
    BT_HDR* packet = WrapPacketAndCopy(MSG_HC_TO_STACK_HCI_EVT, event);
    hci_event_received(packet);
    hci_event_received(FROM_HERE, packet);
    return Void();
    return Void();
  }
  }


+5 −3
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@
 *
 *
 **********************************************************************/
 **********************************************************************/
#include <base/bind.h>
#include <base/bind.h>
#include <base/location.h>
#include <base/logging.h>
#include <base/logging.h>
#include <base/threading/thread.h>
#include <base/threading/thread.h>
#include <errno.h>
#include <errno.h>
@@ -92,7 +93,8 @@ enum HciPacketType {
};
};


extern void initialization_complete();
extern void initialization_complete();
extern void hci_event_received(BT_HDR* packet);
extern void hci_event_received(const tracked_objects::Location& from_here,
                               BT_HDR* packet);
extern void acl_event_received(BT_HDR* packet);
extern void acl_event_received(BT_HDR* packet);
extern void sco_data_received(BT_HDR* packet);
extern void sco_data_received(BT_HDR* packet);


@@ -129,7 +131,7 @@ void monitor_socket(int ctrl_fd, int fd) {
    switch (type) {
    switch (type) {
      case HCI_PACKET_TYPE_COMMAND:
      case HCI_PACKET_TYPE_COMMAND:
        packet->event = MSG_HC_TO_STACK_HCI_EVT;
        packet->event = MSG_HC_TO_STACK_HCI_EVT;
        hci_event_received(packet);
        hci_event_received(FROM_HERE, packet);
        break;
        break;
      case HCI_PACKET_TYPE_ACL_DATA:
      case HCI_PACKET_TYPE_ACL_DATA:
        packet->event = MSG_HC_TO_STACK_HCI_ACL;
        packet->event = MSG_HC_TO_STACK_HCI_ACL;
@@ -141,7 +143,7 @@ void monitor_socket(int ctrl_fd, int fd) {
        break;
        break;
      case HCI_PACKET_TYPE_EVENT:
      case HCI_PACKET_TYPE_EVENT:
        packet->event = MSG_HC_TO_STACK_HCI_EVT;
        packet->event = MSG_HC_TO_STACK_HCI_EVT;
        hci_event_received(packet);
        hci_event_received(FROM_HERE, packet);
        break;
        break;
      default:
      default:
        LOG(FATAL) << "Unexpected event type: " << +type;
        LOG(FATAL) << "Unexpected event type: " << +type;
Loading