Skip to content

Commit

Permalink
Fix hci support on linux kernel 6.9+
Browse files Browse the repository at this point in the history
Prior to this fix, The stateChanged powered_on event will not arrive for bleno with an error called from setFilter (EINVAL, Invalid argument). The buffer for the socket filter was 14 bytes and is a constant value of the same length. However, AF_BLUETOOTH setsockopt kernel handler validates the len of the struct hci_filter and expects it to be bit aligned, zero padding (16 bytes), see abandonware/node-bluetooth-hci-socket#60 (comment) for more detail
Additionally, change hardcoded literal '2902' in gatt peripheral initialisation for notify to use a constant string rather than string literal.
  • Loading branch information
gareth-evans-sniper authored and rzr committed Jan 21, 2025
1 parent 4d93683 commit 4d72805
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/hci-socket/gatt.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const GATT_INCLUDE_UUID = 0x2802;
const GATT_CHARAC_UUID = 0x2803;

const GATT_CLIENT_CHARAC_CFG_UUID = 0x2902;
const GATT_CLIENT_CHARAC_CFG_UUID_S = '2902'
const GATT_SERVER_CHARAC_CFG_UUID = 0x2903;

const ATT_ECODE_SUCCESS = 0x00;
Expand Down Expand Up @@ -208,7 +209,7 @@ class Gatt extends EventEmitter {
this._handles[clientCharacteristicConfigurationDescriptorHandle] = {
type: 'descriptor',
handle: clientCharacteristicConfigurationDescriptorHandle,
uuid: '2902',
uuid: GATT_CLIENT_CHARAC_CFG_UUID_S,
attribute: characteristic,
properties: (0x02 | 0x04 | 0x08), // read/write
secure: (secure & 0x10) ? (0x02 | 0x04 | 0x08) : 0,
Expand All @@ -218,6 +219,15 @@ class Gatt extends EventEmitter {

for (let k = 0; k < characteristic.descriptors.length; k++) {
const descriptor = characteristic.descriptors[k];

//https://github.com/abandonware/bleno/issues/51
//above, if the characteristic defines notify, the 2902 characteristic descriptor is added automatically
//if the peripheral also declares this, we don't want to add it again
//flags (properties, secure etc) above are better defined
if(descriptor.uuid == GATT_CLIENT_CHARAC_CFG_UUID_S)
{
continue;
}

handle++;
const descriptorHandle = handle;
Expand Down
2 changes: 1 addition & 1 deletion lib/hci-socket/hci.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Hci extends EventEmitter {
}

setSocketFilter() {
const filter = Buffer.alloc(14);
const filter = Buffer.alloc(16);
const typeMask = (1 << HCI_EVENT_PKT) | (1 << HCI_ACLDATA_PKT);
const eventMask1 = (1 << EVT_DISCONN_COMPLETE) | (1 << EVT_ENCRYPT_CHANGE) | (1 << EVT_CMD_COMPLETE) | (1 << EVT_CMD_STATUS) | (1 << EVT_NUMBER_OF_COMPLETED_PACKETS);
const eventMask2 = (1 << (EVT_LE_META_EVENT - 32));
Expand Down

0 comments on commit 4d72805

Please sign in to comment.