Skip to content

Commit

Permalink
fix(bacnet-asn1): implement missing stubbed functions
Browse files Browse the repository at this point in the history
Closes issue #3
  • Loading branch information
fh1ch committed Apr 14, 2017
1 parent a59b023 commit a49b103
Showing 1 changed file with 125 additions and 9 deletions.
134 changes: 125 additions & 9 deletions lib/bacnet-asn1.js
Expand Up @@ -272,6 +272,50 @@ var bitstring_bytes_used = function(bitString) {
return len;
};

var encode_application_object_id = function(buffer, object_type, instance) {
var tmp = {
buffer: Buffer.alloc(1472),
offset: 0
};
encodeBacnetObjectId(tmp, object_type, instance);
encodeTag(buffer, baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_OBJECT_ID, false, tmp.offset);
tmp.buffer.copy(buffer.buffer, buffer.offset, 0, tmp.offset);
buffer.offset += tmp.offset;
};

var encode_application_unsigned = function(buffer, value) {
var tmp = {
buffer: Buffer.alloc(1472),
offset: 0
};
encodeBacnetUnsigned(tmp, value);
encodeTag(buffer, baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_UNSIGNED_INT, false, tmp.offset);
tmp.buffer.copy(buffer.buffer, buffer.offset, 0, tmp.offset);
buffer.offset += tmp.offset;
};

var encode_application_enumerated = function(buffer, value) {
var tmp = {
buffer: Buffer.alloc(1472),
offset: 0
};
encodeBacnetEnumerated(tmp, value);
encodeTag(buffer, baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_ENUMERATED, false, tmp.offset);
tmp.buffer.copy(buffer.buffer, buffer.offset, 0, tmp.offset);
buffer.offset += tmp.offset;
};

var encode_application_signed = function(buffer, value) {
var tmp = {
buffer: Buffer.alloc(1472),
offset: 0
};
encode_bacnet_signed(tmp, value);
encodeTag(buffer, baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_SIGNED_INT, false, tmp.offset);
tmp.buffer.copy(buffer.buffer, buffer.offset, 0, tmp.offset);
buffer.offset += tmp.offset;
};

var byte_reverse_bits = function(inByte) {
var outByte = 0;
if ((inByte & 1) > 0) {
Expand Down Expand Up @@ -331,16 +375,36 @@ var encode_application_bitstring = function(buffer, bitString) {
encode_bitstring(buffer, bitString);
};

var encode_bacnet_date = function(buffer, value) {
if (value === new Date(1, 1, 1)) {
buffer.buffer[buffer.offset++] = 0xFF;
buffer.buffer[buffer.offset++] = 0xFF;
buffer.buffer[buffer.offset++] = 0xFF;
buffer.buffer[buffer.offset++] = 0xFF;
return;
}
if (value.Year >= 1900) {
buffer.buffer[buffer.offset++] = (value.year - 1900);
} else if (value.Year < 0x100) {
buffer.buffer[buffer.offset++] = value.year;
} else {
return;
}
buffer.buffer[buffer.offset++] = value.month;
buffer.buffer[buffer.offset++] = value.day;
buffer.buffer[buffer.offset++] = value.getDay();
};

var encode_application_date = function(buffer, value) {
encodeTag(buffer, baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_DATE, false, 4);
encode_bacnet_date(buffer, value);
};

var encode_bacnet_time = function(buffer, value) {
buffer.Add(value.Hour);
buffer.Add(value.Minute);
buffer.Add(value.Second);
buffer.Add(value.Millisecond / 10);
buffer.buffer[buffer.offset++] = value.hour;
buffer.buffer[buffer.offset++] = value.minute;
buffer.buffer[buffer.offset++] = value.second;
buffer.buffer[buffer.offset++] = value.millisecond / 10;
};

var encode_application_time = function(buffer, value) {
Expand Down Expand Up @@ -440,7 +504,7 @@ var encode_cov_subscription = function(buffer, value) {

var bacapp_encode_application_data = module.exports.bacapp_encode_application_data = function(buffer, value) {
if (value.value === null) {
buffer.Add(BacnetApplicationTags.BACNET_APPLICATION_TAG_NULL);
buffer.Add(baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_NULL);
return;
}
switch (value.Tag) {
Expand All @@ -465,8 +529,10 @@ var bacapp_encode_application_data = module.exports.bacapp_encode_application_da
encode_application_octetString(buffer, value.Value, 0, (value.Value).Length);
break;
case baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_CHARACTER_STRING:
encode_application_character_string(buffer, value.Value);
break;
// TODO: Implement
throw 'NOT_IMPLEMENTED';
//encode_application_character_string(buffer, value.Value);
//break;
case baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_BIT_STRING:
encode_application_bitstring(buffer, value.Value);
break;
Expand All @@ -479,11 +545,11 @@ var bacapp_encode_application_data = module.exports.bacapp_encode_application_da
case baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_TIME:
encode_application_time(buffer, value.Value);
break;
// Added for EventTimeStamp
case baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_TIMESTAMP:
// TODO: Implement
throw 'NOT_IMPLEMENTED';
//bacapp_encode_timestamp(buffer, value.Value);
break;
//break;
case baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_DATETIME:
bacapp_encode_datetime(buffer, value.Value);
break;
Expand Down Expand Up @@ -725,6 +791,30 @@ var bacapp_decode_application_data = module.exports.bacapp_decode_application_da
return value;
};

var encode_read_access_result = function(buffer, value) {
encode_context_object_id(buffer, 0, value.objectIdentifier.type, value.objectIdentifier.instance);
encode_opening_tag(buffer, 1);
value.values.forEach(function(item) {
encodeContextEnumerated(buffer, 2, item.property.propertyIdentifier);
if (item.property.propertyArrayIndex !== BACNET_ARRAY_ALL) {
encodeContextUnsigned(buffer, 3, item.property.propertyArrayIndex);
}
if (item.value && item.value[0].Value.type === 'BacnetError') {
encode_opening_tag(buffer, 5);
encode_application_enumerated(buffer, item.value[0].Value.error_class);
encode_application_enumerated(buffer, item.value[0].Value.error_code);
encode_closing_tag(buffer, 5);
} else {
encode_opening_tag(buffer, 4);
item.value.forEach(function(subItem) {
bacapp_encode_application_data(buffer, subItem);
});
encode_closing_tag(buffer, 4);
}
});
encode_closing_tag(buffer, 1);
};

var decode_read_access_result = module.exports.decode_read_access_result = function(buffer, offset, apduLen) {
var len = 0;
var value = {};
Expand Down Expand Up @@ -1024,6 +1114,19 @@ var decode_date_safe = function(buffer, offset, lenValue) {
}
};

var decode_application_date = function(buffer, offset) {
var result = decode_tagNumber(buffer, offset);
if (result.tagNumber === baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_DATE) {
var value = decode_date(buffer, offset + 1);
return {
len: value.len + 1,
value: value
};
} else {
return;
}
};

var decode_bacnet_time = function(buffer, offset) {
var value;
var hour = buffer[offset + 0];
Expand Down Expand Up @@ -1053,6 +1156,19 @@ var decode_bacnet_time_safe = function(buffer, offset, lenValue) {
}
};

var decode_application_time = function(buffer, offset) {
var result = decode_tagNumber(buffer, offset);
if (result.tagNumber === baEnum.BacnetApplicationTags.BACNET_APPLICATION_TAG_TIME) {
var value = decode_bacnet_time(buffer, offset + 1);
return {
len: value.len + 1,
value: value
};
} else {
return;
}
};

var decode_bacnet_datetime = function(buffer, offset) {
var len;
var date = decode_application_date(buffer, offset + len);
Expand Down

0 comments on commit a49b103

Please sign in to comment.