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

Handling raw data obtained from Android's ScanRecord.getBytes #10

Open
wants to merge 3 commits 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
10 changes: 6 additions & 4 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
var Packet = require('./packet').Packet;

// Expects payload to be a buffer
function split(payload) {
function split(payload, {noPayloadLength} = {}) {
var splits = [];
var i = 0;
var length = payload[i++];
var length = noPayloadLength ? payload.length : payload[i++];

while (i < length) {
// Grab the length of the entire packet
var packetLength = payload[i++];
if (packetLength == 0)
break;
// Grab the kind of packet
var type = payload[i++];
// The length of the data is the whole thing minus the type byte
Expand All @@ -35,7 +37,7 @@ function split(payload) {
return splits;
}

function parse(buffer, byteOrder, callback) {
function parse(buffer, byteOrder, callback, {noPayloadLength} = {}) {

// If a byte order wasn't passed it, make it big endian by default
byteOrder = byteOrder ? byteOrder : "BE";
Expand All @@ -53,7 +55,7 @@ function parse(buffer, byteOrder, callback) {
}

// Split up the payload into packet chunks
var splits = split(buffer);
var splits = split(buffer, {noPayloadLength});

var packets = [];

Expand Down
12 changes: 12 additions & 0 deletions test/parser-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ describe("Parser", function() {
it("should split up an advertisement packet into distinct data formats", function() {
expect(parser.split(testPayload).length).to.equal(3);
});

it("should discard the following data assuming them zero-padding when the leading-length octet of AD structure was 0x00.", function() {
var newPayload = Buffer.from(testPayload);
// Set the length of second AD structure to zero.
newPayload[4] = 0x00;
expect(parser.split(newPayload).length).to.equal(1);
});
});

describe("#parse()", function() {
it ("should create the correct number of packets", function() {
var parsed = parser.parse(testPayload);
expect(parsed.length).to.equal(3);
});
it ("should parse the entire buffer when noPayloadLength is true, assuming the payload not to have the leading length octet.", function() {
var newPayload = new Buffer([ 2, 1, 6, 3, 2, 160, 255 ]);
var parsed = parser.parse(newPayload, null, null, {noPayloadLength: true});
expect(parsed.length).to.equal(2);
});
it("should properly parse a payload into the correct kinds of data", function() {
var parsed = parser.parse(testPayload);
parsed.forEach(function(packet) {
Expand Down