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 a Fat Beacon support and an example #42

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions examples/fatBeacon/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>Fat Beacon Demo</title>
<meta charset='UTF-8'>
<meta name='description' content='Fat Beacon Demo'/>
</head>
<body>
<h1>HelloWorld</h1>
<svg xmlns="http://www.w3.org/2000/svg" width="171" height="202"><g fill="#3F82C4" fill-rule="evenodd"><path d="M141.2 85.3c0-31-25-56-56-56-30.7 0-55.8 25-55.8 56 0 17 7.8 32.5 20 42.8l10-10c-9.8-7.6-16-19.4-16-32.7 0-23.2 18.8-42 42-42 23 0 41.8 18.8 41.8 42 0 13.3-6.2 25-16 32.8l10 10c12.2-10.2 20-25.6 20-42.7"/><path d="M14 85.3C14 46 46 14 85.3 14s71.3 32 71.3 71.3c0 21.4-9.5 40.6-24.5 53.7l10 10c17.6-15.7 28.6-38.4 28.6-63.7 0-47-38.2-85.3-85.3-85.3C38.3 0 0 38.2 0 85.3c0 25.3 11 48 28.5 63.6l10-10C23.5 126 14 106.7 14 85.3"/><path d="M89.2 200.3c-2 2-5.5 2-7.6 0l-35.8-35.8c-2-2-2-5.5 0-7.6l35.8-36c2-2 5.5-2 7.6 0l35.8 36c2 2 2 5.4 0 7.5l-35.8 35.8z"/></g></svg>
</body>
</html>
12 changes: 12 additions & 0 deletions examples/fatBeacon/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Simplest way to create a Eddystone-FatBeacon

// might want to include something like this https://www.npmjs.com/package/html-minify

var eddystoneBeacon = require('./../../index');
var fs = require('fs');

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding this in a separate file and using the fs module to read the contents?

fs.readFile('index.html', function(err, data){
if (!err) {
eddystoneBeacon.advertiseFatBeacon('Fat Beacon Demo', {html: data});
}
});
63 changes: 63 additions & 0 deletions lib/HTMLCharacteristic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var bleno = require('bleno');
var util = require('util');

var currentMTU = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should kill these globals and use bleno.mtu anyway

var queueOffset = 0;

bleno.on('mtuChange', function(mtu) {
//console.log('on -> mtuChange: ' + mtu);
currentMTU = mtu;
});

bleno.on('accept', function(){
queueOffset = 0;
});

function HTMLCharacteristic(html) {
this._html = html;
this._buffer = new Buffer(this._html, 'utf8');
bleno.Characteristic.call(this, {
uuid: 'd1a517f0249946ca9ccc809bc1c966fa',
properties: ['read'],
descriptors: [
new bleno.Descriptor({
uuid: '2901',
value: 'HTML'
})
]
});
}

util.inherits(HTMLCharacteristic,bleno.Characteristic);

HTMLCharacteristic.prototype.onReadRequest = function(offset, callback) {
//var buf = new Buffer(this._html, 'utf8');
if (queueOffset < this._buffer.length) {
var transfer = currentMTU - 5;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hayesjordan can you comment on why physical web is using -5 instead of -3 overhead?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no specific reason for using -5, it can be anything less than 0. The only reason for it is to by pass the Android OS on the client device.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its currently oddly implemented on the android side. Transfer is defaulted to 20 (which would be default (23) -3 as Id expect, but then when mtu changes its mtu (505)-5 Seems like if you wanted it to be mtu-5 then transfer defined above there should be 18 not 20 for consistancy.. though it seems like it should be the size of the BLE spec overhead, which I think is 3.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not aware of a BLE spec for overhead, but the numbers didn't seem to be important at the time. I was only trying to keep them for easy multiples when I was debugging this is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I come from the embedded world more, and I know 20 bytes of the 23 is typical for the nordic devices, I presume this is in the spec, perhaps its their limitations. Ill keep researching and maybe @sandeepmistry can illuminate

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like as low as 1 actually. Ive heard 20 quoted a ton on nrf, wonder if thats correct over there.

3.4.7.1 Handle Value Notification
Attribute Value
0 to (ATT_MTU–3)

3.4.4.4 Read Response
0 to (ATT_MTU–1)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jacobrosenthal 23 bytes is the minimum MTU (and default). I'm reading from unofficial sources the maximum is 517 bytes. On Linux, bleno currently has a maximum MTU of 256 bytes, we can bump this up once we find the official maximum MTU size.

20 bytes is right for the maximum notification data size: 23 - 3

and 22 for the maximum size of read data if the MTU is 23: 23 -1

var end = queueOffset + transfer >= this._buffer.length ? this._buffer.length: queueOffset + transfer;
var slice = this._buffer.slice(queueOffset, end);
callback(this.RESULT_SUCCESS, slice);
queueOffset = end;
} else if (queueOffset === this._buffer.length) {
callback(this.RESULT_SUCCESS, new Buffer());
queueOffset++;
}

}

module.exports = HTMLCharacteristic;
36 changes: 36 additions & 0 deletions lib/beacon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var bleno = require('bleno');

var AdvertisementData = require('./util/advertisement-data');

var HTMLCharacteristic = require('./HTMLCharacteristic');

var TICK_INTERVAL = 100; // ms
var DEFAULT_TX_POWER_LEVEL = -21; // dBm

Expand All @@ -19,6 +21,7 @@ function Beacon() {
this._temperature = -128;
this._secCnt = 0;
this._advCnt = 0;
this._html = null;

setInterval(this._tick.bind(this), TICK_INTERVAL);
}
Expand All @@ -45,6 +48,32 @@ Beacon.prototype.advertiseUrl = function(url, options) {
this._advertiseWhenPoweredOn();
};

Beacon.prototype.advertiseFatBeacon = function(title, options) {

this._parseOptions(options);

var htmlCharacteristic = new HTMLCharacteristic(this._html);
var fatBeaconService = new bleno.PrimaryService({
uuid: 'ae5946d4e5874ba8b6a5a97cca6affd3',
characteristics: [htmlCharacteristic]
});

var services = [fatBeaconService];

if (options.services) {
services = services.concat(options.services);
}

bleno.setServices(services);

this._advertisementData = AdvertisementData.makeFatBeaconBuffer(title, this._txPowerLevel);
this._removeFlagsIfOsX();

this._mainAdvertisementData = this._advertisementData;

this._advertiseWhenPoweredOn();
};

Beacon.prototype.advertiseTlm = function() {
this._advertisementData = AdvertisementData.makeTlmBuffer(this._batteryVoltage, this._temperature, this._advCnt, this._secCnt);

Expand Down Expand Up @@ -76,6 +105,13 @@ Beacon.prototype._parseOptions = function(options) {
this._parseTxPowerLevelOption(options.txPowerLevel);
this._parseTlmOptions(options);
this._parseNameOption(options.name);
this._parseFatBeaconOption(options.html);
};

Beacon.prototype._parseFatBeaconOption = function(html) {
if (html) {
this._html = html;
}
};

Beacon.prototype._parseNameOption = function(name) {
Expand Down
16 changes: 15 additions & 1 deletion lib/util/advertisement-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ var makeUrlBuffer = function (url, txPowerLevel) {
return makeEddystoneBuffer(URL_FRAME_TYPE, data);
};

var makeFatBeaconBuffer = function (title, txPowerLevel) {
var txPowerLevelData = makeTxPowerLevelBuffer(txPowerLevel);

var data = Buffer.concat([
txPowerLevelData,
new Buffer([0x0E]),
new Buffer(title)
]);

return makeEddystoneBuffer(URL_FRAME_TYPE, data);
};


var makeTlmBuffer = function (vBatt, temp, advCnt, secCnt) {
var tlmData = new Buffer(13);

Expand Down Expand Up @@ -99,5 +112,6 @@ var hexStringIdToBuffer = function(hexStringId) {
module.exports = {
makeUidBuffer: makeUidBuffer,
makeUrlBuffer: makeUrlBuffer,
makeTlmBuffer: makeTlmBuffer
makeTlmBuffer: makeTlmBuffer,
makeFatBeaconBuffer: makeFatBeaconBuffer
};