wuhkuh / protocol

Protocol generator / parser for Node.JS
MIT License
3 stars 3 forks source link

Incorrectly generated buffers for bit sizes >=8 #14

Open dickr-akka opened 5 years ago

dickr-akka commented 5 years ago

Hi,

while toying around with the library I discovered some strange behaviour.

I weren't able to get the protocol to work with bitfields or numbers >= 10 (Which should indicate, that I'm using the library incorrectly) My expectation was that protocol_bit1 should have worked. But instead the minSeparationTime byte has overwritten the consecutiveFrame byte every time.

Here's the code i've used to test it:

const Protocol = require("protocol");

// protocol
module.exports.control = {
  firstFrame: 1,
  consecutiveFrame: 2,
  flowControl: 3,
  1: "firstFrame",
  2: "consecutiveFrame",
  3: "flowControl"
};

var protocol_bit1 = new Protocol({
  protocolControlInformation: [
    {
      type: { bitLength: 4, dict: module.exports.control },
      flowStatus: { bitLength: 4 },
      blockSize: { bitLength: 8 },
      minSeparationTime: { bitLength: 8 }
    }
  ]
});
var protocol_bit2 = new Protocol({
  protocolControlInformation: [
    {
      type: { bitLength: 4, dict: module.exports.control },
      flowStatus: { bitLength: 4 },
      blockSize: { bitLength: 4 },
      minSeparationTime: { bitLength: 4 }
    }
  ]
});

var protocol_byte1 = new Protocol({
  protocolControlInformation: [
    {
      type: { bitLength: 4, dict: module.exports.control },
      flowStatus: { bitLength: 4 },
      blockSize: { byteLength: 1, encoding: "hex" },
      minSeparationTime: { byteLength: 1, encoding: "hex" }
    }
  ]
});
var protocol_byte2 = new Protocol({
  protocolControlInformation: [
    {
      type: { bitLength: 4, dict: module.exports.control },
      flowStatus: { bitLength: 4 },
      blockSize: { byteLength: 1, encoding: "binary" },
      minSeparationTime: { byteLength: 1, encoding: "binary" }
    }
  ]
});
var protocol_byte3 = new Protocol({
  protocolControlInformation: [
    {
      type: { bitLength: 4, dict: module.exports.control },
      flowStatus: { bitLength: 4 },
      blockSize: { byteLength: 1, encoding: "binary" },
      minSeparationTime: { byteLength: 1, encoding: "hex" }
    }
  ]
});

// test
function test(protocol, blockSize, minSeparationTime) {
  console.log(`# test ###############`);

  var buffer = protocol.generate({
    protocolControlInformation: {
      type: "flowControl",
      flowStatus: 1,
      blockSize: blockSize,
      minSeparationTime: minSeparationTime
    }
  });
  console.log(`buffer: ${JSON.stringify(buffer)}`);
  console.log(`json:   ${JSON.stringify(protocol.parse(buffer))}`);
}

test(protocol_bit1, 3, 4); // incorrect (4,4)
test(protocol_bit2, 3, 4); // correct (3,4)
test(protocol_byte1, 3, 4); // incorrect (-,-)
test(protocol_byte2, 3, 4); // correct (3,4)
test(protocol_byte3, 3, 4); // incorrect (3,-)

test(protocol_bit1, 3, 14); // incorrect (14,14)
test(protocol_bit2, 3, 14); // correct (3,14)
test(protocol_byte1, 3, 14); // incorrect (14,-)
test(protocol_byte2, 3, 14); // incorrect (3,1)
test(protocol_byte3, 3, 14); // correct (3,14)

test(protocol_bit1, 13, 4); // incorrect (4,4)
test(protocol_bit2, 13, 4); // correct (13,4)
test(protocol_byte1, 13, 4); // incorrect (13,-)
test(protocol_byte2, 13, 4); // incorrect (1,3)
test(protocol_byte3, 13, 4); // incorrect (1,33)

// correct: bitsizes < 8, hex for numbers >= 10