From 87d46f5734206439c20353b0dc37d2dcd5eebc53 Mon Sep 17 00:00:00 2001 From: "Marcus M. Scheunemann" Date: Mon, 5 Jun 2017 18:13:29 +0100 Subject: [PATCH 1/4] Need to accept all data packages of all sizes. For example "collision notifications" are of 22 bytes, so they may come in two separate packages of 24 and 4 bytes (containing sphero.js packages of 20 and 2 bytes) --- lib/adaptors/ble.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/adaptors/ble.js b/lib/adaptors/ble.js index b67d97e..a3fb84f 100644 --- a/lib/adaptors/ble.js +++ b/lib/adaptors/ble.js @@ -165,7 +165,7 @@ Adaptor.prototype.devModeOn = function(callback) { function(e, c) { if (e) { return callback(e); } c.on("read", function(data) { - if (data && data.length > 5) { + if (data) { self.readHandler(data); } }); From f0501b70cf74e43322486ee6a955dc976f004423 Mon Sep 17 00:00:00 2001 From: "Marcus M. Scheunemann" Date: Mon, 5 Jun 2017 18:21:38 +0100 Subject: [PATCH 2/4] Transfer all small packages to the next step. Only discard if packets are of minimal length (6 bytes) and have not a valid header. --- lib/packet.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/packet.js b/lib/packet.js index 0ccdee3..95e0581 100644 --- a/lib/packet.js +++ b/lib/packet.js @@ -69,31 +69,42 @@ Packet.prototype.create = function(opts) { }; Packet.prototype.parse = function(buffer) { + + //Todo: test for header before concat? if (this.partialBuffer.length > 0) { buffer = Buffer.concat( [this.partialBuffer, buffer], - buffer.length + this.partialBuffer.length + this.partialBuffer.length + buffer.length ); - this.partialBuffer = new Buffer(0); - } else { - this.partialBuffer = new Buffer(buffer); } - if (this._checkSOPs(buffer)) { - // Check the packet is at least 6 bytes long - if (this._checkMinSize(buffer)) { - // Check the buffer length matches the - // DLEN value specified in the buffer + + if (this._checkMinSize(buffer)) { + // Packet is at least 6 bytes long + + if (this._checkSOPs(buffer)) { + // Packet has valid header + if (this._checkExpectedSize(buffer) > -1) { - // If the packet looks good parse it + // If the buffer is at least of length + // specified in the DLEN value of the buffer, + // try parsing, deal with extra bytes within + // (e.g., put extra bytes in partial buffer) return this._parse(buffer); } + + } else if (!this._checkSOPs(buffer)) { + // Discard packet of minimal size, + // but without a valid header + this.partialBuffer = new Buffer(0); + return null; } - this.partialBuffer = new Buffer(buffer); } + // Transfer too small packets + this.partialBuffer = new Buffer(buffer); return null; }; From 8f72d9bb85ff5e0a21154d65bb57edd013592b2c Mon Sep 17 00:00:00 2001 From: "Marcus M. Scheunemann" Date: Tue, 20 Jun 2017 15:13:24 +0100 Subject: [PATCH 3/4] Introduce functions for checking fragments for being valid (header + size correct + some extra bytes) or unvalid (proper header for fragments >= 2 bytes) Change logic, drop first package if two valid packages are received --- lib/packet.js | 80 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 21 deletions(-) diff --git a/lib/packet.js b/lib/packet.js index 95e0581..dc9c52a 100644 --- a/lib/packet.js +++ b/lib/packet.js @@ -68,17 +68,7 @@ Packet.prototype.create = function(opts) { return packet; }; -Packet.prototype.parse = function(buffer) { - - //Todo: test for header before concat? - if (this.partialBuffer.length > 0) { - buffer = Buffer.concat( - [this.partialBuffer, buffer], - this.partialBuffer.length + buffer.length - ); - - } - +Packet.prototype._checkIfValid = function(buffer) { if (this._checkMinSize(buffer)) { // Packet is at least 6 bytes long @@ -87,25 +77,73 @@ Packet.prototype.parse = function(buffer) { // Packet has valid header if (this._checkExpectedSize(buffer) > -1) { - // If the buffer is at least of length - // specified in the DLEN value of the buffer, - // try parsing, deal with extra bytes within - // (e.g., put extra bytes in partial buffer) - return this._parse(buffer); + // If the buffer is at least of length + // specified in the DLEN value the buffer + // is valid (deal with extra bytes later) + return true; } + } + } + + return false; +}; - } else if (!this._checkSOPs(buffer)) { - // Discard packet of minimal size, +Packet.prototype._checkIfInvalid = function(buffer) { + + if (buffer.length >= 2) { + + if (!this._checkSOPs(buffer)) { + // Discard packet of minimal size, // but without a valid header - this.partialBuffer = new Buffer(0); - return null; + return true; } + } + + return false; +}; + +Packet.prototype.parse = function(buffer) { + + console.log("Received data", buffer); + + if (this._checkIfValid(buffer)) { + // HACK: prevent having two valid packets + // in buffer and only react on the most recent one + // If received buffer is valid, compute + // it and drop all previous + this.partialBuffer = new Buffer(0); + console.log("Parse buffer", buffer); + return this._parse(buffer); + } + + if (this.partialBuffer.length > 0) { + // Concatenate with previous fragment + buffer = Buffer.concat( + [this.partialBuffer, buffer], + this.partialBuffer.length + buffer.length + ); + } + + if (this._checkIfInvalid(buffer)) { + // Drop if concatenation or received + // fragment is clearly invalid + console.log("Discard buffer", buffer); + this.partialBuffer = new Buffer(0); + return null; } - // Transfer too small packets + if (this._checkIfValid(buffer)) { + // Parse if valid, take care of + // extra bytes within + console.log("Parse buffer", buffer); + return this._parse(buffer); + } + + // Transfer too small packets to next step this.partialBuffer = new Buffer(buffer); return null; + }; Packet.prototype._parse = function(buffer) { From ae4a881a90434759496c82387836e4dd428afcb6 Mon Sep 17 00:00:00 2001 From: "Marcus M. Scheunemann" Date: Wed, 21 Jun 2017 18:46:04 +0100 Subject: [PATCH 4/4] delete debug messages --- lib/packet.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/packet.js b/lib/packet.js index dc9c52a..7c720fa 100644 --- a/lib/packet.js +++ b/lib/packet.js @@ -105,15 +105,12 @@ Packet.prototype._checkIfInvalid = function(buffer) { Packet.prototype.parse = function(buffer) { - console.log("Received data", buffer); - if (this._checkIfValid(buffer)) { // HACK: prevent having two valid packets // in buffer and only react on the most recent one // If received buffer is valid, compute // it and drop all previous this.partialBuffer = new Buffer(0); - console.log("Parse buffer", buffer); return this._parse(buffer); } @@ -128,7 +125,6 @@ Packet.prototype.parse = function(buffer) { if (this._checkIfInvalid(buffer)) { // Drop if concatenation or received // fragment is clearly invalid - console.log("Discard buffer", buffer); this.partialBuffer = new Buffer(0); return null; } @@ -136,7 +132,6 @@ Packet.prototype.parse = function(buffer) { if (this._checkIfValid(buffer)) { // Parse if valid, take care of // extra bytes within - console.log("Parse buffer", buffer); return this._parse(buffer); }