有关HCI过滤器插座选项的详细信息?

时间:2018-12-19 19:19:26

标签: python c sockets bluetooth hci

虽然我正在使用python的socket库测试一些基本的HCI命令,但为了使用AF_BLUETOOTHBTPROTO_HCI获得任何套接字流量,似乎需要“全部通过”过滤器在套接字选项中设置:

from socket import socket, AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI, SOL_HCI, HCI_FILTER
from struct import pack

PASS_ALL = pack("IIIh2x", 0xffffffff, 0xffffffff, 0xffffffff, 0)

def open_socket_with_hci(dev_id: int):
    hci = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)
    hci.bind((dev_id,))
    hci.setsockopt(SOL_HCI, HCI_FILTER, PASS_ALL)
    return hci

我发现hci_filter结构定义为:

struct hci_filter {
    uint32_t type_mask;
    uint32_t event_mask[2];
    uint16_t opcode;
};

操作码掩码足够直接。我假设type_mask是值的掩码(来自hci.h的代码):

/* HCI data types */
#define HCI_COMMAND_PKT     0x01
#define HCI_ACLDATA_PKT     0x02
#define HCI_SCODATA_PKT     0x03
#define HCI_EVENT_PKT       0x04
#define HCI_DIAG_PKT        0xf0
#define HCI_VENDOR_PKT      0xff

但是有人可以解释2个event_mask吗? HCI事件类型是第一个,子事件类型是2个(即LE元事件)吗?

1 个答案:

答案 0 :(得分:0)

因此,我正在浏览noble source并发现了这一点:

Hci.prototype.setSocketFilter = function() {
  var filter = new Buffer(14);
  var typeMask = (1 << HCI_COMMAND_PKT) | (1 << HCI_EVENT_PKT) | (1 << HCI_ACLDATA_PKT);
  var eventMask1 = (1 << EVT_DISCONN_COMPLETE) | (1 << EVT_ENCRYPT_CHANGE) | (1 << EVT_CMD_COMPLETE) | (1 << EVT_CMD_STATUS);
  var eventMask2 = (1 << (EVT_LE_META_EVENT - 32));
  var opcode = 0;

  filter.writeUInt32LE(typeMask, 0);
  filter.writeUInt32LE(eventMask1, 4);
  filter.writeUInt32LE(eventMask2, 8);
  filter.writeUInt16LE(opcode, 12);

  debug('setting filter to: ' + filter.toString('hex'));
  this._socket.setFilter(filter);
};

因此,将掩码定义为由蓝牙核心第2卷第E部分第5和7节中定义的常数值左移的位。