赞
踩
蓝牙接收数据包流程
//底层数据包接收的源头 大约在此处
//再上去就是 监控 fd的守护进程了
void H4Protocol::OnDataReady( int fd )
hci_packetizer_.OnDataReady(fd, hci_packet_type_);
void HciPacketizer::OnDataReady( int fd, HciPacketType packet_type )
{
//ALOGE( "HciPacketizer::OnDataReady state_ = %x", state_ );
switch (state_) {
case HCI_PREAMBLE: {
//ALOGE( "bytes_read_ = %x", bytes_read_ );
ssize_t bytes_read = TEMP_FAILURE_RETRY(
read(fd, preamble_ + bytes_read_,
preamble_size_for_type[packet_type] - bytes_read_));
if (bytes_read == 0) {
// This is only expected if the UART got closed when shutting down.
ALOGE("%s: Unexpected EOF reading the header!", __func__);
sleep(5); // Expect to be shut down within 5 seconds.
return;
}
if (bytes_read < 0) {
LOG_ALWAYS_FATAL("%s: Read header error: %s", __func__,
strerror(errno));
}
//ALOGE( "bytes_read = %x", bytes_read );
bytes_read_ += bytes_read;
//ALOGE( "bytes_read_ = %x", bytes_read_ );
if (bytes_read_ == preamble_size_for_type[packet_type]) {
size_t packet_length = HciGetPacketLengthForType(packet_type, preamble_);
packet_.resize(preamble_size_for_type[packet_type] + packet_length);
memcpy(packet_.data(), preamble_, preamble_size_for_type[packet_type]);
bytes_remaining_ = packet_length;
state_ = HCI_PAYLOAD;
bytes_read_ = 0;
}
break;
}
case HCI_PAYLOAD: {
ssize_t bytes_read = TEMP_FAILURE_RETRY(read(
fd,
packet_.data() + preamble_size_for_type[packet_type] + bytes_read_,
bytes_remaining_));
if (bytes_read == 0) {
// This is only expected if the UART got closed when shutting down.
ALOGE("%s: Unexpected EOF reading the payload!", __func__);
sleep(5); // Expect to be shut down within 5 seconds.
return;
}
if (bytes_read < 0) {
LOG_ALWAYS_FATAL("%s: Read payload error: %s", __func__,
strerror(errno));
}
bytes_remaining_ -= bytes_read;
bytes_read_ += bytes_read;
if (bytes_remaining_ == 0) {
//output_buff((char*)packet_.data(), bytes_read_);
ALOGE("get a packet data from uart" );
macdbg_dmphex_kernel((const char*)packet_.data(), bytes_read_);
//ALOGE("packet_ready_cb_.0 = %x", (unsigned int)&packet_ready_cb_ );
packet_ready_cb_();
//此处是关键 1
//ALOGE("packet_ready_cb_.1 = %p", (void *)&packet_ready_cb_ );
state_ = HCI_PREAMBLE;
bytes_read_ = 0;
}
break;
}
}
}
//此处是关键 1 回调的就是这个函数
void H4Protocol::OnPacketReady()
void H4Protocol::OnPacketReady()
{
ALOGE("in %s", __func__);
//ALOGE("OnPacketReady.1 = %x", (unsigned int)(void (android::hardware::bluetooth::hci::H4Protocol::*)())(H4Protocol::OnPacketReady) );
//android::CallStack stack;
//stack.update();
//stack.log("callstack");
ALOGE( "H4Protocol hci_packet_type_ = %x", hci_packet_type_ );
//bt_hci : hciEventReceived
//07-09 16:30:41.408 2424 2775 I bt_hci : hci_event_received
switch (hci_packet_type_) {
case HCI_PACKET_TYPE_EVENT:
event_cb_(hci_packetizer_.GetPacket());
//此处关键点2
break;
case HCI_PACKET_TYPE_ACL_DATA:
acl_cb_(hci_packetizer_.GetPacket());
break;
case HCI_PACKET_TYPE_SCO_DATA:
sco_cb_(hci_packetizer_.GetPacket());
break;
default:
#ifndef XRADIO_BLUETOOTH
LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__,
static_cast<int>(hci_packet_type_));
#else
break;
#endif
}
Bluetooth_hci.cc
bool rc = VendorInterface::Initialize(
[cb](bool status) {
auto hidl_status = cb->initializationComplete(
status ? Status::SUCCESS : Status::INITIALIZATION_ERROR);
if (!hidl_status.isOk()) {
ALOGE("VendorInterface -> Unable to call initializationComplete()");
}
},
[cb](const hidl_vec<uint8_t>& packet) {
//此处关键点2 event_cb_
ALOGE("VendorInterface -> Unable to call hciEventReceived(1)");
auto hidl_status = cb->hciEventReceived(packet);
//此处关键点3
//由日志可以看出
bluetooth 1714 1 18292 4616 0 0 S android.hardware.bluetooth@1.0-service
bluetooth 2430 1695 1435500 96972 0 0 S com.android.bluetooth
跨进程调用 从hal层 调用
07-09 18:30:44.974 1714 2882 E android.hardware.bluetooth@1.0-impl: VendorInterface -> Unable to call hciEventReceived(1)
07-09 18:30:44.974 2430 2767 I bt_hci : hciEventReceived
07-09 18:30:44.974 2430 2767 E bt_hci : get a packet data from uartxxx
07-09 18:30:44.974 2430 2767 E bt_hci : data.size() = 6
07-09 18:30:44.974 2430 2767 E bt_hci : BT_HDR_SIZE = 8
07-09 18:30:44.975 2430 2767 E bt_hci : packet_size = e
07-09 18:30:44.975 2430 2767 E bt_hci : 0 0e 04 05 52 0c 00 ...R..
07-09 18:30:44.975 2430 2767 I bt_hci : hci_event_received
07-09 18:30:44.975 1714 2882 E android.hardware.bluetooth@1.0-impl: VendorInterface -> Unable to call hciEventReceived(2)
ALOGE("VendorInterface -> Unable to call hciEventReceived(2)");
if (!hidl_status.isOk()) {
ALOGE("VendorInterface -> Unable to call hciEventReceived(3)");
}
},
[cb](const hidl_vec<uint8_t>& packet) {
auto hidl_status = cb->aclDataReceived(packet);
if (!hidl_status.isOk()) {
ALOGE("VendorInterface -> Unable to call aclDataReceived()");
}
},
[cb](const hidl_vec<uint8_t>& packet) {
auto hidl_status = cb->scoDataReceived(packet);
if (!hidl_status.isOk()) {
ALOGE("VendorInterface -> Unable to call scoDataReceived()");
}
});
//此处关键点3
//Hci_layer_android.cc
Return<void> hciEventReceived(const hidl_vec<uint8_t>& event) {
LOG_INFO(LOG_TAG, "%s", __func__);
BT_HDR* packet = WrapPacketAndCopy(MSG_HC_TO_STACK_HCI_EVT, event);
//构造数据包 packet
hci_event_received(FROM_HERE, packet);
return Void();
}
//hci_layer.cc
void hci_event_received(const tracked_objects::Location& from_here,
BT_HDR* packet) {
btsnoop->capture(packet, true);
LOG_INFO(LOG_TAG, "%s", __func__);
if (!filter_incoming_event(packet)) {
send_data_upwards.Run(from_here, packet);
}
}
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);
}
void bte_main_boot_entry(void) {
module_init(get_module(INTEROP_MODULE));
hci = hci_layer_get_interface();
if (!hci) {
LOG_ERROR(LOG_TAG, "%s could not get hci layer interface.", __func__);
return;
}
hci->set_data_cb(base::Bind(&post_to_hci_message_loop));
module_init(get_module(STACK_CONFIG_MODULE));
}
void post_to_hci_message_loop(const tracked_objects::Location& from_here,
BT_HDR* p_msg) {
base::MessageLoop* hci_message_loop = get_message_loop();
if (!hci_message_loop || !hci_message_loop->task_runner().get()) {
LOG_ERROR(LOG_TAG, "%s: HCI message loop not running, accessed from %s",
__func__, from_here.ToString().c_str());
return;
}
hci_message_loop->task_runner()->PostTask(
from_here, base::Bind(&btu_hci_msg_process, p_msg));
}
void btu_hci_msg_process( BT_HDR* p_msg )
{
//LOG(INFO) << "btu_hci_msg_process";
//LOG(INFO) << "p_msg->event = "<< p_msg->event;
/* Determine the input message type. */
switch (p_msg->event & BT_EVT_MASK) {
case BT_EVT_TO_BTU_HCI_ACL:
/* All Acl Data goes to L2CAP */
l2c_rcv_acl_data(p_msg);
break;
case BT_EVT_TO_BTU_L2C_SEG_XMIT:
/* L2CAP segment transmit complete */
l2c_link_segments_xmitted(p_msg);
break;
case BT_EVT_TO_BTU_HCI_SCO:
#if (BTM_SCO_INCLUDED == TRUE)
btm_route_sco_data(p_msg);
break;
#endif
case BT_EVT_TO_BTU_HCI_EVT:
btu_hcif_process_event((uint8_t)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
osi_free(p_msg);
break;
case BT_EVT_TO_BTU_HCI_CMD:
btu_hcif_send_cmd((uint8_t)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
break;
default:
osi_free(p_msg);
break;
}
}
大概意思如下:
串口上传数据包大概流程
由内核态的驱动程序 提供给 /dev/ttyS1 数据包,通过的是tty线路规程
发送一个信号通知用户态接收数据包
用户态进程监控 打开/dev/ttyS1的文件描述符fd
用户态解除阻塞等状态
就到了本文开头的位置
然后就是几个关键点的回调
一步步回调到蓝牙的协议栈里面去
具体细节还待分析
现在只是抓个主要脉络和框架
至于数据包的下发流程 基本是这个流程的逆过程
由hci接口 干到内核态去的
跟踪这个流程的关键是 用 macdbg_dmphex_kernel
打印各个节点的数据包的内容
以一个数据包的传递过程 来跟踪代码流程。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。