Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add L2CAP LE Credit Based Connection Support #321

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/bleno.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ var Descriptor = require('./descriptor');
var bindings = null;

var platform = os.platform();
var isHCIbased = false;

if (platform === 'darwin') {
bindings = require('./mac/bindings');
} else if (platform === 'linux' || platform === 'freebsd' || platform === 'win32' || platform === 'android') {
bindings = require('./hci-socket/bindings');
isHCIbased = true;
} else {
throw new Error('Unsupported platform');
}
Expand All @@ -44,6 +46,8 @@ function Bleno() {

this._bindings.on('rssiUpdate', this.onRssiUpdate.bind(this));

this._bindings.on('l2cap-newStream', this.onL2CAPNewStream.bind(this));

this.on('newListener', function(event) {
if (event === 'stateChange' && !this.initialized) {
this._bindings.init();
Expand Down Expand Up @@ -237,4 +241,28 @@ Bleno.prototype.onRssiUpdate = function(rssi) {
this.emit('rssiUpdate', rssi);
};

Bleno.prototype.publishL2CAPChannel = function(psm, callback) {
if (isHCIbased === false) {
debug('Not Supported');
if (callback) {
callback(false, 'Not Supported');
}
return;
}

if (!process.env.HCI_CHANNEL_USER) {
debug('L2CAP Channel requires HCI_CHANNEL_USER');
if (callback) {
callback(false, 'L2CAP Channel requires HCI_CHANNEL_USER');
}
return;
}

this._bindings.publishL2CAPChannel(psm, callback);
};

Bleno.prototype.onL2CAPNewStream = function(stream) {
this.emit('l2cap-newStream', stream);
}

module.exports = Bleno;
19 changes: 19 additions & 0 deletions lib/hci-socket/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var AclStream = require('./acl-stream');
var Hci = require('./hci');
var Gap = require('./gap');
var Gatt = require('./gatt');
var L2cap = require('./l2cap');

var BlenoBindings = function() {
this._state = null;
Expand All @@ -17,6 +18,7 @@ var BlenoBindings = function() {
this._hci = new Hci();
this._gap = new Gap(this._hci);
this._gatt = new Gatt(this._hci);
this._l2cap = new L2cap(this._hci);

this._address = null;
this._handle = null;
Expand Down Expand Up @@ -69,6 +71,16 @@ BlenoBindings.prototype.updateRssi = function() {
}
};

BlenoBindings.prototype.publishL2CAPChannel = function(psm, callback) {
if (this._l2cap) {
this._l2cap.publishL2CAPChannel(psm, callback);
} else {
if (callback) {
callback(false, 'unexpected state');
}
}
};

BlenoBindings.prototype.init = function() {
this.onSigIntBinded = this.onSigInt.bind(this);

Expand All @@ -92,6 +104,8 @@ BlenoBindings.prototype.init = function() {
this._hci.on('leLtkNegReply', this.onLeLtkNegReply.bind(this));
this._hci.on('aclDataPkt', this.onAclDataPkt.bind(this));

this._l2cap.on('newStream', this.onL2CAPNewStream.bind(this));

this.emit('platform', os.platform());

this._hci.init();
Expand Down Expand Up @@ -141,6 +155,7 @@ BlenoBindings.prototype.onLeConnComplete = function(status, handle, role, addres
this._handle = handle;
this._aclStream = new AclStream(this._hci, handle, this._hci.addressType, this._hci.address, addressType, address);
this._gatt.setAclStream(this._aclStream);
this._l2cap.setAclStream(this._aclStream);

this.emit('accept', address);
};
Expand Down Expand Up @@ -195,6 +210,10 @@ BlenoBindings.prototype.onAclDataPkt = function(handle, cid, data) {
}
};

BlenoBindings.prototype.onL2CAPNewStream = function(stream) {
this.emit('l2cap-newStream', stream);
};

BlenoBindings.prototype.onSigInt = function() {
var sigIntListeners = process.listeners('SIGINT');

Expand Down
91 changes: 91 additions & 0 deletions lib/hci-socket/l2cap-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var debug = require('debug')('l2cap-stream');

var events = require('events');
var util = require('util');

var L2CAPStream = function(l2cap, psm, channel, mtu, mps, credits) {
this.psm = psm;

this._isValid = true;
this._l2cap = l2cap;
this._channel = channel;
this._mtu = mtu;
this._mps = mps;
this._credits = credits;

this._expectedLength = 0;
this._pendingBuffer = null;
};

util.inherits(L2CAPStream, events.EventEmitter);


L2CAPStream.prototype.write = function(data) {
if (!this._isValid) {
throw 'Invalid L2CAP Stream';
return;
}

if (!data) {
return;
}

if ((data.length + 2) <= this._mps) {
var lengthBuf = new Buffer(2);
lengthBuf.writeUInt16LE(data.length);
var dataBuf = Buffer.concat([
lengthBuf,
data
]);

this._l2cap.writeChannelData(this._channel, dataBuf);
} else {
var buf = data;
var lengthBuf = new Buffer(2);

while(buf.length) {
var frag = buf.slice(0, (this._mps - 2));
buf = buf.slice(frag.length);
lengthBuf.writeUInt16LE(frag.length);
var dataBuf = Buffer.concat([
lengthBuf,
frag
]);

this._l2cap.writeChannelData(this._channel, dataBuf);
}
}
};

L2CAPStream.prototype.push = function(data) {
if (data) {
if (this._pendingBuffer === null) {
var length = data.readUInt16LE(0);
if (length === (data.length - 2)) {
this.emit('data', data.slice(2));
} else {
this._expectedLength = length;
this._pendingBuffer = data.slice(2);
}
} else {
this._pendingBuffer = Buffer.concat([
this._pendingBuffer,
data
]);
if (this._pendingBuffer.length === this._expectedLength) {
this.emit('data', this._pendingBuffer);
this._pendingBuffer = null;
this._expectedLength = 0;
}
}
} else {
this.emit('end');
}
};

L2CAPStream.prototype.invalidate = function() {
this._isValid = false;
this.emit('invalidate');
}

module.exports = L2CAPStream;
Loading