Skip to content

Commit ee936c0

Browse files
author
Marco Crespi
committed
fix(mtu): Fix mtu negotiation
1 parent 60c13e4 commit ee936c0

File tree

7 files changed

+27
-27
lines changed

7 files changed

+27
-27
lines changed

lib/bindings/hci/Peripheral.js

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bindings/hci/Peripheral.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bindings/hci/gatt/local/Gatt.js

Lines changed: 5 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/bindings/hci/gatt/local/Gatt.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bindings/hci/Peripheral.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { HciAdapter } from './Adapter';
44
import { HciGattRemote } from './gatt';
55
import { Hci, Signaling } from './misc';
66

7+
// 512 bytes is max char size + 1 byte att opcode + 2 bytes handle + 2 bytes offset for long writes
8+
const DEFAULT_MTU = 517;
9+
710
export class HciPeripheral extends Peripheral {
811
public adapter: HciAdapter;
912

@@ -25,8 +28,7 @@ export class HciPeripheral extends Peripheral {
2528
this.signaling = new Signaling(this.hci, this.handle);
2629

2730
this.gatt = new HciGattRemote(this, hci, handle);
28-
await this.gatt.exchangeMtu(256);
29-
this.mtuExchanged = true;
31+
this.mtuExchanged = false;
3032

3133
this._state = 'connected';
3234
}
@@ -57,10 +59,10 @@ export class HciPeripheral extends Peripheral {
5759
throw new Error(`Peripheral is not connected`);
5860
}
5961

60-
/*if (!this.mtuExchanged) {
61-
await this.gatt.exchangeMtu(requestMtu || 256);
62+
if (!this.mtuExchanged) {
63+
await this.gatt.exchangeMtu(requestMtu || DEFAULT_MTU);
6264
this.mtuExchanged = true;
63-
}*/
65+
}
6466

6567
return this.gatt;
6668
}

src/bindings/hci/gatt/local/Gatt.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import { HciAdapter } from '../../Adapter';
33
import { Hci } from '../../misc';
44
import * as CONST from '../Constants';
55

6+
// 512 bytes is max char size + 1 byte att opcode + 2 bytes handle + 2 bytes offset for long writes
7+
const DEFAULT_MAX_MTU = 517;
8+
69
export class HciGattLocal extends GattLocal {
710
private hci: Hci;
811

912
private negotiatedMtus: Map<number, number>;
1013

11-
public constructor(adapter: HciAdapter, hci: Hci, maxMtu: number = 256) {
14+
public constructor(adapter: HciAdapter, hci: Hci, maxMtu: number = DEFAULT_MAX_MTU) {
1215
super(adapter, maxMtu);
1316

1417
this.hci = hci;
@@ -98,12 +101,8 @@ export class HciGattLocal extends GattLocal {
98101
private handleMtuRequest(_handle: number, _cid: number, request: Buffer) {
99102
let mtu = request.readUInt16LE(1);
100103

101-
if (mtu < 23) {
102-
mtu = 23;
103-
} else if (mtu > this.maxMtu) {
104-
mtu = this.maxMtu;
105-
}
106-
104+
mtu = Math.max(23, Math.min(mtu, this.maxMtu));
105+
console.log('mtu negotiated', mtu);
107106
this.negotiatedMtus.set(_handle, mtu);
108107

109108
const response = Buffer.alloc(3);

tests/advertise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const main = async () => {
7171

7272
console.log('Starting advertisement...');
7373

74-
await adapter.startAdvertising(NAME);
74+
await adapter.startAdvertising(NAME, ['48ee0000bf49460ca3d77ec7a512a4ce']);
7575

7676
console.log(adapter.address);
7777

0 commit comments

Comments
 (0)