Skip to content

Commit

Permalink
Merge pull request #133 from fh1ch/fix/object-type-larger-512
Browse files Browse the repository at this point in the history
fix(asn1): correct encoding of object-types > 512
  • Loading branch information
fh1ch authored Feb 10, 2019
2 parents 196ac4d + 3103ad5 commit 6bbae54
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/asn1.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const decodeEnumerated = module.exports.decodeEnumerated = (buffer, offset, lenV
};

const encodeBacnetObjectId = module.exports.encodeBacnetObjectId = (buffer, objectType, instance) => {
const value = ((objectType & baEnum.ASN1_MAX_OBJECT) << baEnum.ASN1_INSTANCE_BITS) | (instance & baEnum.ASN1_MAX_INSTANCE);
const value = (((objectType & baEnum.ASN1_MAX_OBJECT) << baEnum.ASN1_INSTANCE_BITS) | (instance & baEnum.ASN1_MAX_INSTANCE)) >>> 0;
encodeUnsigned(buffer, value, 4);
};

Expand Down
8 changes: 8 additions & 0 deletions test/unit/asn1.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ describe('bacstack - ASN1 layer', () => {
expect(result).to.deep.equal({len: 4, value: 4294967295});
});
});

describe('encodeBacnetObjectId', () => {
it('should successfully encode with object-type > 512', () => {
const buffer = {buffer: Buffer.alloc(4), offset: 0};
baAsn1.encodeBacnetObjectId(buffer, 600, 600);
expect(buffer).to.deep.equal({buffer: Buffer.from([150, 0, 2, 88]), offset: 4});
});
});
});
11 changes: 11 additions & 0 deletions test/unit/service-read-property.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ describe('bacstack - Services layer ReadProperty unit', () => {
});
});

it('should successfully encode and decode with object-tye > 512', () => {
const buffer = utils.getBuffer();
baServices.readProperty.encode(buffer, 630, 5, 12, 0xFFFFFFFF);
const result = baServices.readProperty.decode(buffer.buffer, 0, buffer.offset);
delete result.len;
expect(result).to.deep.equal({
objectId: {type: 630, instance: 5},
property: {id: 12, index: 0xFFFFFFFF}
});
});

it('should successfully encode and decode with array index', () => {
const buffer = utils.getBuffer();
baServices.readProperty.encode(buffer, 4, 630, 85, 2);
Expand Down

0 comments on commit 6bbae54

Please sign in to comment.