Bugfix/deprecate decode tag number and value (#481)
* added or updated secure the BACnet primitive value decoders - the core codecs - named bacnet_x_decode(), bacnet_x_application_decode() and bacnet_x_context_decode where x is one of the 13 BACnet primitive value names. The updated API includes an APDU size to prevent over-reading of an APDU buffer while decoding. Improved or added unit test code coverage for the BACnet primitive value decoders. * marked the insecure decoding API as 'deprecated' which is defined in src/bacnet/basic/sys/platform.h and can be disabled during a build. * added secure decoders for BACnetTimeValue, BACnetHostNPort, BACnetTimeStamp, BACnetAddress, and Weekly_Schedule and improved unit test code coverage. * improved test code coverage for BACnet objects and properties. * secured AtomicReadFile and AtomicWriteFile service decoders and improved unit test code coverage. * secured BACnet Error service decoder and improved unit test code coverage. --------- Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
+472
-251
@@ -39,135 +39,225 @@
|
||||
|
||||
/** @file arf.c Atomic Read File */
|
||||
|
||||
/* encode service */
|
||||
/**
|
||||
* @brief Encode the AtomicReadFile service request
|
||||
*
|
||||
* AtomicReadFile-Request ::= SEQUENCE {
|
||||
* file-identifier BACnetObjectIdentifier,
|
||||
* access-method CHOICE {
|
||||
* stream-access [0] SEQUENCE {
|
||||
* file-start-position INTEGER,
|
||||
* requested-octet-count Unsigned
|
||||
* },
|
||||
* record-access [1] SEQUENCE {
|
||||
* file-start-record INTEGER,
|
||||
* requested-record-count Unsigned
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer for encoded values
|
||||
* @param data Pointer to the service data used for encoding values
|
||||
* @return number of bytes encoded
|
||||
*/
|
||||
int arf_service_encode_apdu(uint8_t *apdu, BACNET_ATOMIC_READ_FILE_DATA *data)
|
||||
{
|
||||
int apdu_len = 0; /* total length of the apdu, return value */
|
||||
int len = 0;
|
||||
|
||||
len = encode_application_object_id(
|
||||
apdu, data->object_type, data->object_instance);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
switch (data->access) {
|
||||
case FILE_STREAM_ACCESS:
|
||||
len = encode_opening_tag(apdu, 0);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_signed(
|
||||
apdu, data->type.stream.fileStartPosition);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_unsigned(
|
||||
apdu, data->type.stream.requestedOctetCount);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_closing_tag(apdu, 0);
|
||||
apdu_len += len;
|
||||
break;
|
||||
case FILE_RECORD_ACCESS:
|
||||
len = encode_opening_tag(apdu, 1);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_signed(
|
||||
apdu, data->type.record.fileStartRecord);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_unsigned(
|
||||
apdu, data->type.record.RecordCount);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_closing_tag(apdu, 1);
|
||||
apdu_len += len;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode the AtomicReadFile service request
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param invoke_id original invoke id for request
|
||||
* @param data Pointer to the property decoded data to be stored
|
||||
* @return number of bytes encoded
|
||||
*/
|
||||
int arf_encode_apdu(
|
||||
uint8_t *apdu, uint8_t invoke_id, BACNET_ATOMIC_READ_FILE_DATA *data)
|
||||
{
|
||||
int apdu_len = 0; /* total length of the apdu, return value */
|
||||
int len = 0;
|
||||
|
||||
if (apdu) {
|
||||
apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST;
|
||||
apdu[1] = encode_max_segs_max_apdu(0, MAX_APDU);
|
||||
apdu[2] = invoke_id;
|
||||
apdu[3] = SERVICE_CONFIRMED_ATOMIC_READ_FILE; /* service choice */
|
||||
apdu_len = 4;
|
||||
apdu_len += encode_application_object_id(
|
||||
&apdu[apdu_len], data->object_type, data->object_instance);
|
||||
switch (data->access) {
|
||||
case FILE_STREAM_ACCESS:
|
||||
apdu_len += encode_opening_tag(&apdu[apdu_len], 0);
|
||||
apdu_len += encode_application_signed(
|
||||
&apdu[apdu_len], data->type.stream.fileStartPosition);
|
||||
apdu_len += encode_application_unsigned(
|
||||
&apdu[apdu_len], data->type.stream.requestedOctetCount);
|
||||
apdu_len += encode_closing_tag(&apdu[apdu_len], 0);
|
||||
break;
|
||||
case FILE_RECORD_ACCESS:
|
||||
apdu_len += encode_opening_tag(&apdu[apdu_len], 1);
|
||||
apdu_len += encode_application_signed(
|
||||
&apdu[apdu_len], data->type.record.fileStartRecord);
|
||||
apdu_len += encode_application_unsigned(
|
||||
&apdu[apdu_len], data->type.record.RecordCount);
|
||||
apdu_len += encode_closing_tag(&apdu[apdu_len], 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
len = 4;
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = arf_service_encode_apdu(apdu, data);
|
||||
apdu_len += len;
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* decode the service request only */
|
||||
/**
|
||||
* @brief Decode the AtomicReadFile service request
|
||||
*
|
||||
* AtomicReadFile-Request ::= SEQUENCE {
|
||||
* file-identifier BACnetObjectIdentifier,
|
||||
* access-method CHOICE {
|
||||
* stream-access [0] SEQUENCE {
|
||||
* file-start-position INTEGER,
|
||||
* requested-octet-count Unsigned
|
||||
* },
|
||||
* record-access [1] SEQUENCE {
|
||||
* file-start-record INTEGER,
|
||||
* requested-record-count Unsigned
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param apdu_size Count of valid bytes in the buffer.
|
||||
* @param data Pointer to the property decoded data to be stored
|
||||
* or NULL for length
|
||||
*
|
||||
* @return number of bytes decoded or BACNET_STATUS_ERROR on error.
|
||||
*/
|
||||
int arf_decode_service_request(
|
||||
uint8_t *apdu, unsigned apdu_len_max, BACNET_ATOMIC_READ_FILE_DATA *data)
|
||||
uint8_t *apdu, unsigned apdu_size, BACNET_ATOMIC_READ_FILE_DATA *data)
|
||||
{
|
||||
int len = 0;
|
||||
int apdu_len = BACNET_STATUS_ERROR;
|
||||
int tag_len = 0;
|
||||
int apdu_len = 0;
|
||||
BACNET_OBJECT_TYPE object_type = OBJECT_NONE;
|
||||
uint32_t object_instance = 0;
|
||||
int32_t signed_integer;
|
||||
BACNET_UNSIGNED_INTEGER unsigned_integer;
|
||||
|
||||
/* check for value pointers */
|
||||
if ((apdu_len_max == 0) || (!data)) {
|
||||
tag_len = bacnet_object_id_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &object_type, &object_instance);
|
||||
if (tag_len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len = bacnet_object_id_application_decode(
|
||||
&apdu[0], apdu_len_max, &object_type, &object_instance);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
if (data) {
|
||||
data->object_type = (BACNET_OBJECT_TYPE)object_type;
|
||||
data->object_instance = object_instance;
|
||||
}
|
||||
data->object_type = (BACNET_OBJECT_TYPE)object_type;
|
||||
data->object_instance = object_instance;
|
||||
apdu_len = len;
|
||||
if (apdu_len < apdu_len_max) {
|
||||
if (decode_is_opening_tag_number(&apdu[apdu_len], 0)) {
|
||||
apdu_len += tag_len;
|
||||
if (bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 0, &tag_len)) {
|
||||
if (data) {
|
||||
data->access = FILE_STREAM_ACCESS;
|
||||
/* tag number 0 is not extended so only one octet */
|
||||
apdu_len++;
|
||||
/* fileStartPosition */
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len = bacnet_signed_application_decode(&apdu[apdu_len],
|
||||
apdu_len_max - apdu_len, &data->type.stream.fileStartPosition);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* requestedOctetCount */
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len = bacnet_unsigned_application_decode(&apdu[apdu_len],
|
||||
apdu_len_max, &data->type.stream.requestedOctetCount);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* closing tag */
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (!decode_is_closing_tag_number(&apdu[apdu_len], 0)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* tag number 0 is not extended so only one octet */
|
||||
apdu_len++;
|
||||
} else if (decode_is_opening_tag_number(&apdu[len], 1)) {
|
||||
data->access = FILE_RECORD_ACCESS;
|
||||
/* tag number 1 is not extended so only one octet */
|
||||
apdu_len++;
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* fileStartRecord */
|
||||
len = bacnet_signed_application_decode(&apdu[apdu_len],
|
||||
apdu_len_max - apdu_len, &data->type.record.fileStartRecord);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* RecordCount */
|
||||
len = bacnet_unsigned_application_decode(
|
||||
&apdu[apdu_len], apdu_len_max, &data->type.record.RecordCount);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (!decode_is_closing_tag_number(&apdu[apdu_len], 1)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* tag number 1 is not extended so only one octet */
|
||||
apdu_len++;
|
||||
} else {
|
||||
}
|
||||
apdu_len += tag_len;
|
||||
/* fileStartPosition */
|
||||
tag_len = bacnet_signed_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &signed_integer);
|
||||
if (tag_len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (data) {
|
||||
data->type.stream.fileStartPosition = signed_integer;
|
||||
}
|
||||
apdu_len += tag_len;
|
||||
/* requestedOctetCount */
|
||||
tag_len = bacnet_unsigned_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &unsigned_integer);
|
||||
if (tag_len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (data) {
|
||||
data->type.stream.requestedOctetCount = unsigned_integer;
|
||||
}
|
||||
apdu_len += tag_len;
|
||||
/* closing tag */
|
||||
if (!bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 0, &tag_len)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += tag_len;
|
||||
} else if (bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 1, &tag_len)) {
|
||||
if (data) {
|
||||
data->access = FILE_RECORD_ACCESS;
|
||||
}
|
||||
apdu_len += tag_len;
|
||||
/* fileStartRecord */
|
||||
tag_len = bacnet_signed_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &signed_integer);
|
||||
if (tag_len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (data) {
|
||||
data->type.record.fileStartRecord = signed_integer;
|
||||
}
|
||||
apdu_len += tag_len;
|
||||
/* RecordCount */
|
||||
tag_len = bacnet_unsigned_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &unsigned_integer);
|
||||
if (tag_len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (data) {
|
||||
data->type.record.RecordCount = unsigned_integer;
|
||||
}
|
||||
apdu_len += tag_len;
|
||||
if (!bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 1, &tag_len)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += tag_len;
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
@@ -175,205 +265,336 @@ int arf_decode_service_request(
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decoding for AtomicReadFile APDU service data
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param apdu_size size of the buffer for decoding.
|
||||
* @param invoke_id [in] Invoked service ID.
|
||||
* @param data Pointer to the property data values to be stored,
|
||||
* or NULL for length
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR on error
|
||||
*/
|
||||
int arf_decode_apdu(uint8_t *apdu,
|
||||
unsigned apdu_len,
|
||||
unsigned apdu_size,
|
||||
uint8_t *invoke_id,
|
||||
BACNET_ATOMIC_READ_FILE_DATA *data)
|
||||
{
|
||||
int len = 0;
|
||||
unsigned offset = 0;
|
||||
int apdu_len = 0, len = 0;
|
||||
|
||||
if (!apdu) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* optional checking - most likely was already done prior to this call */
|
||||
if (apdu_size < 4) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (apdu[0] != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* apdu[1] = encode_max_segs_max_apdu(0, MAX_APDU); */
|
||||
*invoke_id = apdu[2]; /* invoke id - filled in by net layer */
|
||||
if (invoke_id) {
|
||||
*invoke_id = apdu[2]; /* invoke id - filled in by net layer */
|
||||
}
|
||||
if (apdu[3] != SERVICE_CONFIRMED_ATOMIC_READ_FILE) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
offset = 4;
|
||||
|
||||
if (apdu_len > offset) {
|
||||
len =
|
||||
arf_decode_service_request(&apdu[offset], apdu_len - offset, data);
|
||||
len = 4;
|
||||
apdu_len += len;
|
||||
len =
|
||||
arf_decode_service_request(&apdu[apdu_len], apdu_size - apdu_len, data);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
return len;
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* encode service */
|
||||
int arf_ack_encode_apdu(
|
||||
uint8_t *apdu, uint8_t invoke_id, BACNET_ATOMIC_READ_FILE_DATA *data)
|
||||
/**
|
||||
* @brief Encode the AtomicReadFile-ACK service request
|
||||
*
|
||||
* AtomicReadFile-ACK ::= SEQUENCE {
|
||||
* end-of-file BOOLEAN,
|
||||
* access-method CHOICE {
|
||||
* stream-access [0] SEQUENCE {
|
||||
* file-start-position INTEGER,
|
||||
* file-data OCTET STRING
|
||||
* },
|
||||
* record-access [1] SEQUENCE {
|
||||
* file-start-record INTEGER,
|
||||
* returned-record-count Unsigned,
|
||||
* file-record-data SEQUENCE OF OCTET STRING
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer for encoding, or NULL for length
|
||||
* @param data Pointer to the data to be encoded
|
||||
* @return number of bytes encoded
|
||||
*/
|
||||
int arf_ack_service_encode_apdu(
|
||||
uint8_t *apdu, BACNET_ATOMIC_READ_FILE_DATA *data)
|
||||
{
|
||||
int apdu_len = 0; /* total length of the apdu, return value */
|
||||
int len = 0;
|
||||
uint32_t i = 0;
|
||||
|
||||
/* endOfFile */
|
||||
len = encode_application_boolean(apdu, data->endOfFile);
|
||||
if (apdu) {
|
||||
apdu[0] = PDU_TYPE_COMPLEX_ACK;
|
||||
apdu[1] = invoke_id;
|
||||
apdu[2] = SERVICE_CONFIRMED_ATOMIC_READ_FILE; /* service choice */
|
||||
apdu_len = 3;
|
||||
/* endOfFile */
|
||||
apdu_len +=
|
||||
encode_application_boolean(&apdu[apdu_len], data->endOfFile);
|
||||
switch (data->access) {
|
||||
case FILE_STREAM_ACCESS:
|
||||
apdu_len += encode_opening_tag(&apdu[apdu_len], 0);
|
||||
apdu_len += encode_application_signed(
|
||||
&apdu[apdu_len], data->type.stream.fileStartPosition);
|
||||
apdu_len += encode_application_octet_string(
|
||||
&apdu[apdu_len], &data->fileData[0]);
|
||||
apdu_len += encode_closing_tag(&apdu[apdu_len], 0);
|
||||
break;
|
||||
case FILE_RECORD_ACCESS:
|
||||
apdu_len += encode_opening_tag(&apdu[apdu_len], 1);
|
||||
apdu_len += encode_application_signed(
|
||||
&apdu[apdu_len], data->type.record.fileStartRecord);
|
||||
apdu_len += encode_application_unsigned(
|
||||
&apdu[apdu_len], data->type.record.RecordCount);
|
||||
for (i = 0; i < data->type.record.RecordCount; i++) {
|
||||
apdu_len += encode_application_octet_string(
|
||||
&apdu[apdu_len], &data->fileData[i]);
|
||||
apdu += len;
|
||||
}
|
||||
apdu_len += len;
|
||||
switch (data->access) {
|
||||
case FILE_STREAM_ACCESS:
|
||||
len = encode_opening_tag(apdu, 0);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_signed(
|
||||
apdu, data->type.stream.fileStartPosition);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_octet_string(apdu, &data->fileData[0]);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_closing_tag(apdu, 0);
|
||||
apdu_len += len;
|
||||
break;
|
||||
case FILE_RECORD_ACCESS:
|
||||
len = encode_opening_tag(apdu, 1);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_signed(
|
||||
apdu, data->type.record.fileStartRecord);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_unsigned(
|
||||
apdu, data->type.record.RecordCount);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
for (i = 0; i < data->type.record.RecordCount; i++) {
|
||||
len = encode_application_octet_string(apdu, &data->fileData[i]);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
apdu_len += encode_closing_tag(&apdu[apdu_len], 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
len = encode_closing_tag(apdu, 1);
|
||||
apdu_len += len;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* decode the service request only */
|
||||
int arf_ack_decode_service_request(
|
||||
uint8_t *apdu, unsigned apdu_len, BACNET_ATOMIC_READ_FILE_DATA *data)
|
||||
/**
|
||||
* @brief Encode the AtomicReadFile-ACK service request
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param invoke_id original invoke id for request
|
||||
* @param data Pointer to the property decoded data to be stored
|
||||
* @return number of bytes encoded
|
||||
*/
|
||||
int arf_ack_encode_apdu(
|
||||
uint8_t *apdu, uint8_t invoke_id, BACNET_ATOMIC_READ_FILE_DATA *data)
|
||||
{
|
||||
int apdu_len = 0; /* total length of the apdu, return value */
|
||||
int len = 0;
|
||||
int tag_len = 0;
|
||||
int decoded_len = 0;
|
||||
uint8_t tag_number = 0;
|
||||
uint32_t len_value_type = 0;
|
||||
uint32_t i = 0;
|
||||
|
||||
/* check for value pointers */
|
||||
if (apdu_len && data) {
|
||||
len =
|
||||
decode_tag_number_and_value(&apdu[0], &tag_number, &len_value_type);
|
||||
if (tag_number != BACNET_APPLICATION_TAG_BOOLEAN) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
data->endOfFile = decode_boolean(len_value_type);
|
||||
if (decode_is_opening_tag_number(&apdu[len], 0)) {
|
||||
data->access = FILE_STREAM_ACCESS;
|
||||
/* a tag number is not extended so only one octet */
|
||||
len++;
|
||||
/* fileStartPosition */
|
||||
tag_len = decode_tag_number_and_value(
|
||||
&apdu[len], &tag_number, &len_value_type);
|
||||
len += tag_len;
|
||||
if (tag_number != BACNET_APPLICATION_TAG_SIGNED_INT) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len += decode_signed(&apdu[len], len_value_type,
|
||||
&data->type.stream.fileStartPosition);
|
||||
/* fileData */
|
||||
tag_len = decode_tag_number_and_value(
|
||||
&apdu[len], &tag_number, &len_value_type);
|
||||
len += tag_len;
|
||||
if (tag_number != BACNET_APPLICATION_TAG_OCTET_STRING) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
decoded_len = decode_octet_string(
|
||||
&apdu[len], len_value_type, &data->fileData[0]);
|
||||
if ((uint32_t)decoded_len != len_value_type) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len += decoded_len;
|
||||
if (!decode_is_closing_tag_number(&apdu[len], 0)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* a tag number is not extended so only one octet */
|
||||
len++;
|
||||
} else if (decode_is_opening_tag_number(&apdu[len], 1)) {
|
||||
data->access = FILE_RECORD_ACCESS;
|
||||
/* a tag number is not extended so only one octet */
|
||||
len++;
|
||||
/* fileStartRecord */
|
||||
tag_len = decode_tag_number_and_value(
|
||||
&apdu[len], &tag_number, &len_value_type);
|
||||
len += tag_len;
|
||||
if (tag_number != BACNET_APPLICATION_TAG_SIGNED_INT) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len += decode_signed(
|
||||
&apdu[len], len_value_type, &data->type.record.fileStartRecord);
|
||||
/* returnedRecordCount */
|
||||
tag_len = decode_tag_number_and_value(
|
||||
&apdu[len], &tag_number, &len_value_type);
|
||||
len += tag_len;
|
||||
if (tag_number != BACNET_APPLICATION_TAG_UNSIGNED_INT) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len += decode_unsigned(
|
||||
&apdu[len], len_value_type, &data->type.record.RecordCount);
|
||||
for (i = 0; i < data->type.record.RecordCount; i++) {
|
||||
/* fileData */
|
||||
tag_len = decode_tag_number_and_value(
|
||||
&apdu[len], &tag_number, &len_value_type);
|
||||
len += tag_len;
|
||||
if (tag_number != BACNET_APPLICATION_TAG_OCTET_STRING) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
decoded_len = decode_octet_string(
|
||||
&apdu[len], len_value_type, &data->fileData[i]);
|
||||
if ((uint32_t)decoded_len != len_value_type) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len += decoded_len;
|
||||
}
|
||||
if (!decode_is_closing_tag_number(&apdu[len], 1)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* a tag number is not extended so only one octet */
|
||||
len++;
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (apdu) {
|
||||
apdu[0] = PDU_TYPE_COMPLEX_ACK;
|
||||
apdu[1] = invoke_id;
|
||||
apdu[2] = SERVICE_CONFIRMED_ATOMIC_READ_FILE; /* service choice */
|
||||
}
|
||||
len = 3;
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = arf_ack_service_encode_apdu(apdu, data);
|
||||
apdu_len += len;
|
||||
|
||||
return len;
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decoding for AtomicReadFile-ACK APDU service data
|
||||
*
|
||||
* AtomicReadFile-ACK ::= SEQUENCE {
|
||||
* end-of-file BOOLEAN,
|
||||
* access-method CHOICE {
|
||||
* stream-access [0] SEQUENCE {
|
||||
* file-start-position INTEGER,
|
||||
* file-data OCTET STRING
|
||||
* },
|
||||
* record-access [1] SEQUENCE {
|
||||
* file-start-record INTEGER,
|
||||
* returned-record-count Unsigned,
|
||||
* file-record-data SEQUENCE OF OCTET STRING
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param apdu_size size of the buffer for decoding.
|
||||
* @param data Pointer to the property data to be encoded,
|
||||
* or NULL for length
|
||||
* @return Bytes encoded or BACNET_STATUS_ERROR on error.
|
||||
*/
|
||||
int arf_ack_decode_service_request(
|
||||
uint8_t *apdu, unsigned apdu_size, BACNET_ATOMIC_READ_FILE_DATA *data)
|
||||
{
|
||||
int apdu_len = 0;
|
||||
int len = 0;
|
||||
bool endOfFile;
|
||||
int32_t signed_integer;
|
||||
BACNET_UNSIGNED_INTEGER record_count, i;
|
||||
BACNET_OCTET_STRING *octet_string = NULL;
|
||||
|
||||
len = bacnet_boolean_application_decode(apdu, apdu_size, &endOfFile);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (data) {
|
||||
data->endOfFile = endOfFile;
|
||||
}
|
||||
apdu_len += len;
|
||||
if (bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 0, &len)) {
|
||||
if (data) {
|
||||
data->access = FILE_STREAM_ACCESS;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* fileStartPosition */
|
||||
len = bacnet_signed_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &signed_integer);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (data) {
|
||||
data->type.stream.fileStartPosition = signed_integer;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* fileData */
|
||||
if (data) {
|
||||
octet_string = &data->fileData[0];
|
||||
}
|
||||
len = bacnet_octet_string_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, octet_string);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
if (!bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 0, &len)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
} else if (bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 1, &len)) {
|
||||
if (data) {
|
||||
data->access = FILE_RECORD_ACCESS;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* fileStartRecord */
|
||||
len = bacnet_signed_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &signed_integer);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (data) {
|
||||
data->type.record.fileStartRecord = signed_integer;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* returnedRecordCount */
|
||||
len = bacnet_unsigned_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &record_count);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (data) {
|
||||
data->type.record.RecordCount = record_count;
|
||||
}
|
||||
apdu_len += len;
|
||||
for (i = 0; i < record_count; i++) {
|
||||
/* fileData */
|
||||
if (i >= BACNET_READ_FILE_RECORD_COUNT) {
|
||||
octet_string = NULL;
|
||||
} else if (data) {
|
||||
octet_string = &data->fileData[i];
|
||||
} else {
|
||||
octet_string = NULL;
|
||||
}
|
||||
len = bacnet_octet_string_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, octet_string);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
}
|
||||
if (!bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 1, &len)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decoding for AtomicReadFile-ACK APDU service data
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param apdu_size size of the buffer for decoding.
|
||||
* @param invoke_id [in] Invoked service ID.
|
||||
* @param data Pointer to the property data values to be stored,
|
||||
* or NULL for length
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR on error
|
||||
*/
|
||||
int arf_ack_decode_apdu(uint8_t *apdu,
|
||||
unsigned apdu_len,
|
||||
unsigned apdu_size,
|
||||
uint8_t *invoke_id,
|
||||
BACNET_ATOMIC_READ_FILE_DATA *data)
|
||||
{
|
||||
int len = 0;
|
||||
unsigned offset = 0;
|
||||
int apdu_len = 0, len = 0;
|
||||
|
||||
if (!apdu) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* optional checking - most likely was already done prior to this call */
|
||||
if (apdu_size < 3) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (apdu[0] != PDU_TYPE_COMPLEX_ACK) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
*invoke_id = apdu[1]; /* invoke id - filled in by net layer */
|
||||
if (invoke_id) {
|
||||
*invoke_id = apdu[1]; /* invoke id - filled in by net layer */
|
||||
}
|
||||
if (apdu[2] != SERVICE_CONFIRMED_ATOMIC_READ_FILE) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
offset = 3;
|
||||
|
||||
if (apdu_len > offset) {
|
||||
len = arf_ack_decode_service_request(
|
||||
&apdu[offset], apdu_len - offset, data);
|
||||
len = 3;
|
||||
apdu_len += len;
|
||||
len = arf_ack_decode_service_request(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, data);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
return len;
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
@@ -65,6 +65,10 @@ extern "C" {
|
||||
uint8_t * apdu,
|
||||
uint8_t invoke_id,
|
||||
BACNET_ATOMIC_READ_FILE_DATA * data);
|
||||
BACNET_STACK_EXPORT
|
||||
int arf_service_encode_apdu(
|
||||
uint8_t *apdu,
|
||||
BACNET_ATOMIC_READ_FILE_DATA *data);
|
||||
|
||||
/* decode the service request only */
|
||||
BACNET_STACK_EXPORT
|
||||
@@ -88,6 +92,10 @@ extern "C" {
|
||||
uint8_t * apdu,
|
||||
uint8_t invoke_id,
|
||||
BACNET_ATOMIC_READ_FILE_DATA * data);
|
||||
BACNET_STACK_EXPORT
|
||||
int arf_ack_service_encode_apdu(
|
||||
uint8_t *apdu,
|
||||
BACNET_ATOMIC_READ_FILE_DATA *data);
|
||||
|
||||
/* decode the service request only */
|
||||
BACNET_STACK_EXPORT
|
||||
|
||||
+376
-185
@@ -39,275 +39,466 @@
|
||||
|
||||
/** @file awf.c Atomic Write File */
|
||||
|
||||
/* encode service */
|
||||
/**
|
||||
* @brief Encode the AtomicWriteFile service request
|
||||
*
|
||||
* AtomicWriteFile-Request ::= SEQUENCE {
|
||||
* file-identifier BACnetObjectIdentifier,
|
||||
* access-method CHOICE {
|
||||
* stream-access [0] SEQUENCE {
|
||||
* file-start-position INTEGER,
|
||||
* file-data OCTET STRING
|
||||
* },
|
||||
* record-access [1] SEQUENCE {
|
||||
* file-start-record INTEGER,
|
||||
* record-count Unsigned
|
||||
* file-record-data SEQUENCE OF OCTET STRING
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer for encoded values
|
||||
* @param data Pointer to the service data used for encoding values
|
||||
* @return number of bytes encoded
|
||||
*/
|
||||
int awf_service_encode_apdu(uint8_t *apdu, BACNET_ATOMIC_WRITE_FILE_DATA *data)
|
||||
{
|
||||
int apdu_len = 0; /* total length of the apdu, return value */
|
||||
int len = 0;
|
||||
uint32_t i = 0;
|
||||
|
||||
len = encode_application_object_id(
|
||||
apdu, data->object_type, data->object_instance);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
switch (data->access) {
|
||||
case FILE_STREAM_ACCESS:
|
||||
len = encode_opening_tag(apdu, 0);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_signed(
|
||||
apdu, data->type.stream.fileStartPosition);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_octet_string(apdu, &data->fileData[0]);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_closing_tag(apdu, 0);
|
||||
apdu_len += len;
|
||||
break;
|
||||
case FILE_RECORD_ACCESS:
|
||||
len = encode_opening_tag(apdu, 1);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_signed(
|
||||
apdu, data->type.record.fileStartRecord);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_unsigned(
|
||||
apdu, data->type.record.returnedRecordCount);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
for (i = 0; i < data->type.record.returnedRecordCount; i++) {
|
||||
len = encode_application_octet_string(apdu, &data->fileData[i]);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
}
|
||||
len = encode_closing_tag(apdu, 1);
|
||||
apdu_len += len;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode the AtomicWriteFile service request
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param invoke_id original invoke id for request
|
||||
* @param data Pointer to the property decoded data to be stored
|
||||
* @return number of bytes encoded
|
||||
*/
|
||||
int awf_encode_apdu(
|
||||
uint8_t *apdu, uint8_t invoke_id, BACNET_ATOMIC_WRITE_FILE_DATA *data)
|
||||
{
|
||||
int apdu_len = 0; /* total length of the apdu, return value */
|
||||
uint32_t i = 0;
|
||||
int len = 0;
|
||||
|
||||
if (apdu) {
|
||||
apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST;
|
||||
apdu[1] = encode_max_segs_max_apdu(0, MAX_APDU);
|
||||
apdu[2] = invoke_id;
|
||||
apdu[3] = SERVICE_CONFIRMED_ATOMIC_WRITE_FILE; /* service choice */
|
||||
apdu_len = 4;
|
||||
apdu_len += encode_application_object_id(
|
||||
&apdu[apdu_len], data->object_type, data->object_instance);
|
||||
switch (data->access) {
|
||||
case FILE_STREAM_ACCESS:
|
||||
apdu_len += encode_opening_tag(&apdu[apdu_len], 0);
|
||||
apdu_len += encode_application_signed(
|
||||
&apdu[apdu_len], data->type.stream.fileStartPosition);
|
||||
apdu_len += encode_application_octet_string(
|
||||
&apdu[apdu_len], &data->fileData[0]);
|
||||
apdu_len += encode_closing_tag(&apdu[apdu_len], 0);
|
||||
break;
|
||||
case FILE_RECORD_ACCESS:
|
||||
apdu_len += encode_opening_tag(&apdu[apdu_len], 1);
|
||||
apdu_len += encode_application_signed(
|
||||
&apdu[apdu_len], data->type.record.fileStartRecord);
|
||||
apdu_len += encode_application_unsigned(
|
||||
&apdu[apdu_len], data->type.record.returnedRecordCount);
|
||||
for (i = 0; i < data->type.record.returnedRecordCount; i++) {
|
||||
apdu_len += encode_application_octet_string(
|
||||
&apdu[apdu_len], &data->fileData[i]);
|
||||
}
|
||||
apdu_len += encode_closing_tag(&apdu[apdu_len], 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
len = 4;
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = awf_service_encode_apdu(apdu, data);
|
||||
apdu_len += len;
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* decode the service request only */
|
||||
/**
|
||||
* @brief Decode the AtomicWriteFile service request
|
||||
*
|
||||
* AtomicWriteFile-Request ::= SEQUENCE {
|
||||
* file-identifier BACnetObjectIdentifier,
|
||||
* access-method CHOICE {
|
||||
* stream-access [0] SEQUENCE {
|
||||
* file-start-position INTEGER,
|
||||
* file-data OCTET STRING
|
||||
* },
|
||||
* record-access [1] SEQUENCE {
|
||||
* file-start-record INTEGER,
|
||||
* record-count Unsigned
|
||||
* file-record-data SEQUENCE OF OCTET STRING
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param apdu_size Count of valid bytes in the buffer.
|
||||
* @param data Pointer to the property decoded data to be stored,
|
||||
* or NULL for length
|
||||
*
|
||||
* @return number of bytes decoded or BACNET_STATUS_ERROR on error.
|
||||
*/
|
||||
int awf_decode_service_request(
|
||||
uint8_t *apdu, unsigned apdu_len_max, BACNET_ATOMIC_WRITE_FILE_DATA *data)
|
||||
uint8_t *apdu, unsigned apdu_size, BACNET_ATOMIC_WRITE_FILE_DATA *data)
|
||||
{
|
||||
/* return value */
|
||||
int apdu_len = 0;
|
||||
int len = 0;
|
||||
int apdu_len = BACNET_STATUS_ERROR;
|
||||
BACNET_OBJECT_TYPE object_type = OBJECT_NONE;
|
||||
uint32_t object_instance = 0;
|
||||
int32_t signed_integer;
|
||||
BACNET_UNSIGNED_INTEGER unsigned_value = 0;
|
||||
uint32_t record_count = 0;
|
||||
uint32_t i = 0;
|
||||
BACNET_OCTET_STRING *octet_string = NULL;
|
||||
|
||||
/* check for value pointers */
|
||||
if ((apdu_len_max == 0) || (!data)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len = bacnet_object_id_application_decode(
|
||||
&apdu[0], apdu_len_max, &object_type, &object_instance);
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &object_type, &object_instance);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
data->object_type = object_type;
|
||||
data->object_instance = object_instance;
|
||||
apdu_len = len;
|
||||
if (apdu_len < apdu_len_max) {
|
||||
if (decode_is_opening_tag_number(&apdu[apdu_len], 0)) {
|
||||
if (data) {
|
||||
data->object_type = object_type;
|
||||
data->object_instance = object_instance;
|
||||
}
|
||||
apdu_len += len;
|
||||
if (bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 0, &len)) {
|
||||
if (data) {
|
||||
data->access = FILE_STREAM_ACCESS;
|
||||
/* a tag number of 0 is not extended so only one octet */
|
||||
apdu_len++;
|
||||
/* fileStartPosition */
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len = bacnet_signed_application_decode(&apdu[apdu_len],
|
||||
apdu_len_max - apdu_len, &data->type.stream.fileStartPosition);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* fileData */
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len = bacnet_octet_string_application_decode(
|
||||
&apdu[apdu_len], apdu_len_max, &data->fileData[0]);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* closing tag */
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (!decode_is_closing_tag_number(&apdu[apdu_len], 0)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* a tag number of 0 is not extended so only one octet */
|
||||
apdu_len++;
|
||||
} else if (decode_is_opening_tag_number(&apdu[apdu_len], 1)) {
|
||||
data->access = FILE_RECORD_ACCESS;
|
||||
/* a tag number of 0 is not extended so only one octet */
|
||||
apdu_len++;
|
||||
/* fileStartRecord */
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len = bacnet_signed_application_decode(&apdu[apdu_len],
|
||||
apdu_len_max - apdu_len, &data->type.record.fileStartRecord);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* returnedRecordCount */
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len = bacnet_unsigned_application_decode(
|
||||
&apdu[apdu_len], apdu_len_max, &unsigned_value);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
data->type.record.returnedRecordCount = unsigned_value;
|
||||
apdu_len += len;
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* fileData */
|
||||
for (i = 0; i < data->type.record.returnedRecordCount; i++) {
|
||||
if (i < BACNET_WRITE_FILE_RECORD_COUNT) {
|
||||
len = bacnet_octet_string_application_decode(
|
||||
&apdu[apdu_len], apdu_len_max, &data->fileData[i]);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* closing tag or another record */
|
||||
if (apdu_len >= apdu_len_max) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
}
|
||||
if (!decode_is_closing_tag_number(&apdu[apdu_len], 1)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* tag number 1 is not extended so only one octet */
|
||||
apdu_len++;
|
||||
} else {
|
||||
}
|
||||
apdu_len += len;
|
||||
/* fileStartPosition */
|
||||
len = bacnet_signed_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &signed_integer);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (data) {
|
||||
data->type.stream.fileStartPosition = signed_integer;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* fileData */
|
||||
if (data) {
|
||||
octet_string = &data->fileData[0];
|
||||
}
|
||||
len = bacnet_octet_string_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, octet_string);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* closing tag */
|
||||
if (!bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 0, &len)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
} else if (bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 1, &len)) {
|
||||
if (data) {
|
||||
data->access = FILE_RECORD_ACCESS;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* fileStartRecord */
|
||||
len = bacnet_signed_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &signed_integer);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (data) {
|
||||
data->type.record.fileStartRecord = signed_integer;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* returnedRecordCount */
|
||||
len = bacnet_unsigned_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &unsigned_value);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (data) {
|
||||
data->type.record.returnedRecordCount = unsigned_value;
|
||||
}
|
||||
record_count = unsigned_value;
|
||||
apdu_len += len;
|
||||
for (i = 0; i < record_count; i++) {
|
||||
/* fileData */
|
||||
if (i >= BACNET_WRITE_FILE_RECORD_COUNT) {
|
||||
octet_string = NULL;
|
||||
} else if (data) {
|
||||
octet_string = &data->fileData[i];
|
||||
} else {
|
||||
octet_string = NULL;
|
||||
}
|
||||
len = bacnet_octet_string_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, octet_string);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
}
|
||||
if (!bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 1, &len)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decoding for AtomicWriteFile APDU service data
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param apdu_size size of the buffer for decoding.
|
||||
* @param invoke_id [in] Invoked service ID.
|
||||
* @param data Pointer to the property data values to be stored,
|
||||
* or NULL for length
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR on error
|
||||
*/
|
||||
int awf_decode_apdu(uint8_t *apdu,
|
||||
unsigned apdu_len,
|
||||
unsigned apdu_size,
|
||||
uint8_t *invoke_id,
|
||||
BACNET_ATOMIC_WRITE_FILE_DATA *data)
|
||||
{
|
||||
int len = 0;
|
||||
unsigned offset = 0;
|
||||
int apdu_len = 0, len = 0;
|
||||
|
||||
if (!apdu) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* optional checking - most likely was already done prior to this call */
|
||||
if (apdu_size < 4) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (apdu[0] != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* apdu[1] = encode_max_segs_max_apdu(0, MAX_APDU); */
|
||||
*invoke_id = apdu[2]; /* invoke id - filled in by net layer */
|
||||
if (invoke_id) {
|
||||
*invoke_id = apdu[2];
|
||||
}
|
||||
if (apdu[3] != SERVICE_CONFIRMED_ATOMIC_WRITE_FILE) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
offset = 4;
|
||||
|
||||
if (apdu_len > offset) {
|
||||
len =
|
||||
awf_decode_service_request(&apdu[offset], apdu_len - offset, data);
|
||||
len = 4;
|
||||
apdu_len += len;
|
||||
len =
|
||||
awf_decode_service_request(&apdu[apdu_len], apdu_size - apdu_len, data);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
return len;
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode the AtomicWriteFile-ACK service request
|
||||
*
|
||||
* AtomicWriteFile-ACK ::= CHOICE {
|
||||
* file-start-position [0] INTEGER,
|
||||
* file-start-record [1] INTEGER
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer for encoding, or NULL for length
|
||||
* @param data Pointer to the data to be encoded
|
||||
* @return number of bytes encoded
|
||||
*/
|
||||
int awf_ack_service_encode_apdu(
|
||||
uint8_t *apdu, BACNET_ATOMIC_WRITE_FILE_DATA *data)
|
||||
{
|
||||
int apdu_len = 0; /* total length of the apdu, return value */
|
||||
|
||||
switch (data->access) {
|
||||
case FILE_STREAM_ACCESS:
|
||||
apdu_len = encode_context_signed(
|
||||
apdu, 0, data->type.stream.fileStartPosition);
|
||||
break;
|
||||
case FILE_RECORD_ACCESS:
|
||||
apdu_len = encode_context_signed(
|
||||
apdu, 1, data->type.record.fileStartRecord);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode the AtomicWriteFile-ACK service request
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param invoke_id original invoke id for request
|
||||
* @param data Pointer to the property decoded data to be stored
|
||||
* @return number of bytes encoded
|
||||
*/
|
||||
int awf_ack_encode_apdu(
|
||||
uint8_t *apdu, uint8_t invoke_id, BACNET_ATOMIC_WRITE_FILE_DATA *data)
|
||||
{
|
||||
int apdu_len = 0; /* total length of the apdu, return value */
|
||||
int len = 0;
|
||||
|
||||
if (apdu) {
|
||||
apdu[0] = PDU_TYPE_COMPLEX_ACK;
|
||||
apdu[1] = invoke_id;
|
||||
apdu[2] = SERVICE_CONFIRMED_ATOMIC_WRITE_FILE; /* service choice */
|
||||
apdu_len = 3;
|
||||
switch (data->access) {
|
||||
case FILE_STREAM_ACCESS:
|
||||
apdu_len += encode_context_signed(
|
||||
&apdu[apdu_len], 0, data->type.stream.fileStartPosition);
|
||||
break;
|
||||
case FILE_RECORD_ACCESS:
|
||||
apdu_len += encode_context_signed(
|
||||
&apdu[apdu_len], 1, data->type.record.fileStartRecord);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
len = 3;
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = awf_ack_service_encode_apdu(apdu, data);
|
||||
apdu_len += len;
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decoding for AtomicWriteFile-ACK APDU service data
|
||||
*
|
||||
* AtomicWriteFile-ACK ::= CHOICE {
|
||||
* file-start-position [0] INTEGER,
|
||||
* file-start-record [1] INTEGER
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param apdu_size size of the buffer for decoding.
|
||||
* @param data Pointer to the property data to be encoded,
|
||||
* or NULL for length
|
||||
* @return number of bytes encoded or BACNET_STATUS_ERROR on error.
|
||||
*/
|
||||
int awf_ack_decode_service_request(
|
||||
uint8_t *apdu, unsigned apdu_size, BACNET_ATOMIC_WRITE_FILE_DATA *data)
|
||||
{
|
||||
int len = 0, apdu_len = 0;
|
||||
int32_t signed_integer;
|
||||
BACNET_TAG tag = { 0 };
|
||||
|
||||
len = bacnet_tag_decode(apdu, apdu_size, &tag);
|
||||
if ((len > 0) && tag.context) {
|
||||
if (tag.number == 0) {
|
||||
/* file-start-position [0] INTEGER */
|
||||
len = bacnet_signed_context_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 0, &signed_integer);
|
||||
if (len > 0) {
|
||||
if (data) {
|
||||
data->access = FILE_STREAM_ACCESS;
|
||||
data->type.stream.fileStartPosition = signed_integer;
|
||||
}
|
||||
apdu_len += len;
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
} else if (tag.number == 1) {
|
||||
/* file-start-record [1] INTEGER */
|
||||
len = bacnet_signed_context_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 1, &signed_integer);
|
||||
if (len > 0) {
|
||||
if (data) {
|
||||
data->access = FILE_RECORD_ACCESS;
|
||||
data->type.record.fileStartRecord = signed_integer;
|
||||
}
|
||||
apdu_len += len;
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* decode the service request only */
|
||||
int awf_ack_decode_service_request(
|
||||
uint8_t *apdu, unsigned apdu_len, BACNET_ATOMIC_WRITE_FILE_DATA *data)
|
||||
{
|
||||
int len = 0;
|
||||
uint8_t tag_number = 0;
|
||||
uint32_t len_value_type = 0;
|
||||
|
||||
/* check for value pointers */
|
||||
if (apdu_len && data) {
|
||||
len =
|
||||
decode_tag_number_and_value(&apdu[0], &tag_number, &len_value_type);
|
||||
if (tag_number == 0) {
|
||||
data->access = FILE_STREAM_ACCESS;
|
||||
len += decode_signed(&apdu[len], len_value_type,
|
||||
&data->type.stream.fileStartPosition);
|
||||
} else if (tag_number == 1) {
|
||||
data->access = FILE_RECORD_ACCESS;
|
||||
len += decode_signed(
|
||||
&apdu[len], len_value_type, &data->type.record.fileStartRecord);
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decoding for AtomicWriteFile-ACK APDU service data
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param apdu_size size of the buffer for decoding.
|
||||
* @param invoke_id [in] Invoked service ID.
|
||||
* @param data Pointer to the property data values to be stored,
|
||||
* or NULL for length
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR on error
|
||||
*/
|
||||
int awf_ack_decode_apdu(uint8_t *apdu,
|
||||
unsigned apdu_len,
|
||||
unsigned apdu_size,
|
||||
uint8_t *invoke_id,
|
||||
BACNET_ATOMIC_WRITE_FILE_DATA *data)
|
||||
{
|
||||
int len = 0;
|
||||
unsigned offset = 0;
|
||||
int apdu_len = 0;
|
||||
|
||||
if (!apdu) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (apdu_size < 3) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* optional checking - most likely was already done prior to this call */
|
||||
if (apdu[0] != PDU_TYPE_COMPLEX_ACK) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
*invoke_id = apdu[1]; /* invoke id - filled in by net layer */
|
||||
if (invoke_id) {
|
||||
*invoke_id = apdu[1]; /* invoke id - filled in by net layer */
|
||||
}
|
||||
if (apdu[2] != SERVICE_CONFIRMED_ATOMIC_WRITE_FILE) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
offset = 3;
|
||||
|
||||
if (apdu_len > offset) {
|
||||
len = awf_ack_decode_service_request(
|
||||
&apdu[offset], apdu_len - offset, data);
|
||||
len = 3;
|
||||
apdu_len += len;
|
||||
len = awf_ack_decode_service_request(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, data);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
|
||||
return len;
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
+12
-12
@@ -53,47 +53,47 @@ typedef struct BACnet_Atomic_Write_File_Data {
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/* Atomic Write File */
|
||||
/* encode service */
|
||||
BACNET_STACK_EXPORT
|
||||
int awf_service_encode_apdu(
|
||||
uint8_t *apdu,
|
||||
BACNET_ATOMIC_WRITE_FILE_DATA *data);
|
||||
BACNET_STACK_EXPORT
|
||||
int awf_encode_apdu(
|
||||
uint8_t * apdu,
|
||||
uint8_t invoke_id,
|
||||
BACNET_ATOMIC_WRITE_FILE_DATA * data);
|
||||
|
||||
/* decode the service request only */
|
||||
BACNET_STACK_EXPORT
|
||||
int awf_decode_service_request(
|
||||
uint8_t * apdu,
|
||||
unsigned apdu_len,
|
||||
unsigned apdu_size,
|
||||
BACNET_ATOMIC_WRITE_FILE_DATA * data);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
int awf_decode_apdu(
|
||||
uint8_t * apdu,
|
||||
unsigned apdu_len,
|
||||
unsigned apdu_size,
|
||||
uint8_t * invoke_id,
|
||||
BACNET_ATOMIC_WRITE_FILE_DATA * data);
|
||||
|
||||
/* Atomic Write File Ack */
|
||||
/* encode service */
|
||||
BACNET_STACK_EXPORT
|
||||
int awf_ack_service_encode_apdu(
|
||||
uint8_t *apdu,
|
||||
BACNET_ATOMIC_WRITE_FILE_DATA *data);
|
||||
BACNET_STACK_EXPORT
|
||||
int awf_ack_encode_apdu(
|
||||
uint8_t * apdu,
|
||||
uint8_t invoke_id,
|
||||
BACNET_ATOMIC_WRITE_FILE_DATA * data);
|
||||
|
||||
/* decode the service request only */
|
||||
BACNET_STACK_EXPORT
|
||||
int awf_ack_decode_service_request(
|
||||
uint8_t * apdu,
|
||||
unsigned apdu_len,
|
||||
unsigned apdu_size,
|
||||
BACNET_ATOMIC_WRITE_FILE_DATA * data);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
int awf_ack_decode_apdu(
|
||||
uint8_t * apdu,
|
||||
unsigned apdu_len,
|
||||
unsigned apdu_size,
|
||||
uint8_t * invoke_id,
|
||||
BACNET_ATOMIC_WRITE_FILE_DATA * data);
|
||||
|
||||
|
||||
@@ -37,8 +37,10 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "bacnet/config.h"
|
||||
#include "bacnet/bacdcode.h"
|
||||
#include "bacnet/bacdef.h"
|
||||
#include "bacnet/bacint.h"
|
||||
#include "bacnet/bacstr.h"
|
||||
#include "bacnet/bacaddr.h"
|
||||
|
||||
/** @file bacaddr.c BACnet Address structure utilities */
|
||||
@@ -260,3 +262,178 @@ bool bacnet_address_mac_from_ascii(BACNET_MAC_ADDRESS *mac, const char *arg)
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decodes a BACnetAddress value from APDU buffer
|
||||
* From clause 21. FORMAL DESCRIPTION OF APPLICATION PROTOCOL DATA UNITS
|
||||
*
|
||||
* BACnetAddress ::= SEQUENCE {
|
||||
* network-number Unsigned16, -- A value of 0 indicates the local network
|
||||
* mac-address OCTET STRING -- A string of length 0 indicates a broadcast
|
||||
* }
|
||||
*
|
||||
* @param apdu - buffer of data to be decoded
|
||||
* @param apdu_size - number of bytes in the buffer
|
||||
* @param value - decoded value, if decoded (if not NULL)
|
||||
*
|
||||
* @return the number of apdu bytes consumed, or #BACNET_STATUS_ERROR (-1)
|
||||
*/
|
||||
int bacnet_address_decode(
|
||||
uint8_t *apdu, uint32_t apdu_size, BACNET_ADDRESS *value)
|
||||
{
|
||||
int len = 0;
|
||||
int apdu_len = 0;
|
||||
uint8_t i = 0;
|
||||
BACNET_UNSIGNED_INTEGER decoded_unsigned = 0;
|
||||
BACNET_OCTET_STRING mac_addr = { 0 };
|
||||
|
||||
/* network number */
|
||||
len = bacnet_unsigned_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &decoded_unsigned);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (decoded_unsigned <= UINT16_MAX) {
|
||||
/* bounds checking - passed! */
|
||||
if (value) {
|
||||
value->net = (uint16_t)decoded_unsigned;
|
||||
}
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* mac address as an octet-string */
|
||||
len = bacnet_octet_string_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &mac_addr);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (value) {
|
||||
if (mac_addr.length > sizeof(value->mac)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
/* bounds checking - passed! */
|
||||
value->mac_len = mac_addr.length;
|
||||
/* copy address */
|
||||
for (i = 0; i < value->mac_len; i++) {
|
||||
value->mac[i] = mac_addr.value[i];
|
||||
}
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decodes a context tagged BACnetAddress value from APDU buffer
|
||||
* @param apdu - the APDU buffer
|
||||
* @param apdu_size - the APDU buffer size
|
||||
* @param tag_number - context tag number to be encoded
|
||||
* @param value - parameter to store the value after decoding
|
||||
* @return length of the APDU buffer decoded, or BACNET_STATUS_ERROR
|
||||
*/
|
||||
int bacnet_address_context_decode(uint8_t *apdu,
|
||||
uint32_t apdu_size,
|
||||
uint8_t tag_number,
|
||||
BACNET_ADDRESS *value)
|
||||
{
|
||||
int len = 0;
|
||||
int apdu_len = 0;
|
||||
|
||||
if (!bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
len = bacnet_address_decode(&apdu[apdu_len], apdu_size - apdu_len, value);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
if (!bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a BACnetAddress and returns the number of apdu bytes consumed.
|
||||
*
|
||||
* @param apdu - buffer to hold encoded data, or NULL for length
|
||||
* @param destination Pointer to the destination address to be encoded.
|
||||
*
|
||||
* @return number of apdu bytes created
|
||||
*/
|
||||
int encode_bacnet_address(uint8_t *apdu, BACNET_ADDRESS *destination)
|
||||
{
|
||||
int apdu_len = 0;
|
||||
BACNET_OCTET_STRING mac_addr;
|
||||
|
||||
if (destination) {
|
||||
/* network number */
|
||||
apdu_len += encode_application_unsigned(apdu, destination->net);
|
||||
/* encode mac address as an octet-string */
|
||||
if (destination->len != 0) {
|
||||
octetstring_init(&mac_addr, destination->adr, destination->len);
|
||||
} else {
|
||||
octetstring_init(&mac_addr, destination->mac, destination->mac_len);
|
||||
}
|
||||
if (apdu) {
|
||||
apdu += apdu_len;
|
||||
}
|
||||
apdu_len += encode_application_octet_string(apdu, &mac_addr);
|
||||
}
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decode a BACnetAddress and returns the number of apdu bytes consumed.
|
||||
* @param apdu Receive buffer
|
||||
* @param value - parameter to store the value after decoding
|
||||
* @return length of the APDU buffer decoded, or BACNET_STATUS_ERROR
|
||||
* @deprecated use bacnet_address_decode() instead
|
||||
*/
|
||||
int decode_bacnet_address(uint8_t *apdu, BACNET_ADDRESS *value)
|
||||
{
|
||||
return bacnet_address_decode(apdu, MAX_APDU, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode a context encoded BACnetAddress
|
||||
* @param apdu - buffer to hold encoded data, or NULL for length
|
||||
* @param destination Pointer to the destination address to be encoded.
|
||||
* @return number of apdu bytes created
|
||||
*/
|
||||
int encode_context_bacnet_address(
|
||||
uint8_t *apdu, uint8_t tag_number, BACNET_ADDRESS *destination)
|
||||
{
|
||||
int len = 0;
|
||||
uint8_t *apdu_offset = NULL;
|
||||
|
||||
len += encode_opening_tag(apdu, tag_number);
|
||||
if (apdu) {
|
||||
apdu_offset = &apdu[len];
|
||||
}
|
||||
len += encode_bacnet_address(apdu_offset, destination);
|
||||
if (apdu) {
|
||||
apdu_offset = &apdu[len];
|
||||
}
|
||||
len += encode_closing_tag(apdu_offset, tag_number);
|
||||
return len;
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Decodes a context tagged BACnetAddress value from APDU buffer
|
||||
* @param apdu - the APDU buffer
|
||||
* @param tag_number - context tag number to be encoded
|
||||
* @param value - parameter to store the value after decoding
|
||||
* @return length of the APDU buffer decoded, or BACNET_STATUS_ERROR
|
||||
* @deprecated use bacnet_address_context_decode() instead
|
||||
*/
|
||||
int decode_context_bacnet_address(
|
||||
uint8_t *apdu, uint8_t tag_number, BACNET_ADDRESS *value)
|
||||
{
|
||||
return bacnet_address_context_decode(apdu, MAX_APDU, tag_number, value);
|
||||
}
|
||||
|
||||
+25
-2
@@ -28,6 +28,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "bacnet/bacnet_stack_exports.h"
|
||||
#include "bacnet/basic/sys/platform.h"
|
||||
#include "bacnet/bacdef.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -45,14 +46,36 @@ bool bacnet_address_init(BACNET_ADDRESS *dest,
|
||||
BACNET_MAC_ADDRESS *adr);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
bool bacnet_address_mac_same(
|
||||
BACNET_MAC_ADDRESS *dest, BACNET_MAC_ADDRESS *src);
|
||||
bool bacnet_address_mac_same(BACNET_MAC_ADDRESS *dest, BACNET_MAC_ADDRESS *src);
|
||||
BACNET_STACK_EXPORT
|
||||
void bacnet_address_mac_init(
|
||||
BACNET_MAC_ADDRESS *mac, uint8_t *adr, uint8_t len);
|
||||
BACNET_STACK_EXPORT
|
||||
bool bacnet_address_mac_from_ascii(BACNET_MAC_ADDRESS *mac, const char *arg);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
int bacnet_address_decode(
|
||||
uint8_t *apdu, uint32_t adpu_size, BACNET_ADDRESS *value);
|
||||
BACNET_STACK_EXPORT
|
||||
int bacnet_address_context_decode(uint8_t *apdu,
|
||||
uint32_t adpu_size,
|
||||
uint8_t tag_number,
|
||||
BACNET_ADDRESS *value);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
int encode_bacnet_address(uint8_t *apdu, BACNET_ADDRESS *destination);
|
||||
BACNET_STACK_EXPORT
|
||||
int encode_context_bacnet_address(
|
||||
uint8_t *apdu, uint8_t tag_number, BACNET_ADDRESS *destination);
|
||||
|
||||
BACNET_STACK_DEPRECATED("Use bacnet_address_decode() instead")
|
||||
BACNET_STACK_EXPORT
|
||||
int decode_bacnet_address(uint8_t *apdu, BACNET_ADDRESS *destination);
|
||||
BACNET_STACK_DEPRECATED("Use bacnet_address_context_decode() instead")
|
||||
BACNET_STACK_EXPORT
|
||||
int decode_context_bacnet_address(
|
||||
uint8_t *apdu, uint8_t tag_number, BACNET_ADDRESS *destination);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
+31
-24
@@ -310,7 +310,8 @@ int bacapp_decode_data(uint8_t *apdu,
|
||||
#endif
|
||||
#if defined(BACAPP_TYPES_EXTRA)
|
||||
case BACNET_APPLICATION_TAG_DATETIME:
|
||||
len = bacapp_decode_datetime(apdu, &value->type.Date_Time);
|
||||
len = bacnet_datetime_decode(
|
||||
apdu, len_value_type, &value->type.Date_Time);
|
||||
break;
|
||||
case BACNET_APPLICATION_TAG_LIGHTING_COMMAND:
|
||||
len = lighting_command_decode(
|
||||
@@ -1234,7 +1235,8 @@ int bacapp_decode_known_property(uint8_t *apdu,
|
||||
case PROP_EXPIRATION_TIME:
|
||||
case PROP_LAST_USE_TIME:
|
||||
/* Properties using BACnetDateTime value */
|
||||
len = bacapp_decode_datetime(apdu, &value->type.Date_Time);
|
||||
len = bacnet_datetime_decode(
|
||||
apdu, max_apdu_len, &value->type.Date_Time);
|
||||
break;
|
||||
|
||||
case PROP_OBJECT_PROPERTY_REFERENCE:
|
||||
@@ -3004,28 +3006,31 @@ int bacapp_property_value_decode(
|
||||
/* property-identifier [0] BACnetPropertyIdentifier */
|
||||
len = bacnet_enumerated_context_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 0, &enumerated_value);
|
||||
if (len == BACNET_STATUS_ERROR) {
|
||||
if (len > 0) {
|
||||
property_identifier = enumerated_value;
|
||||
if (value) {
|
||||
value->propertyIdentifier = property_identifier;
|
||||
}
|
||||
apdu_len += len;
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
property_identifier = enumerated_value;
|
||||
if (value) {
|
||||
value->propertyIdentifier = property_identifier;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* property-array-index [1] Unsigned OPTIONAL */
|
||||
if (bacnet_is_context_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 1, NULL)) {
|
||||
len = bacnet_unsigned_context_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 1, &unsigned_value);
|
||||
if (len == BACNET_STATUS_ERROR) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
} else if (unsigned_value > UINT32_MAX) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
} else {
|
||||
apdu_len += len;
|
||||
if (value) {
|
||||
value->propertyArrayIndex = unsigned_value;
|
||||
if (len > 0) {
|
||||
if (unsigned_value > UINT32_MAX) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
} else {
|
||||
apdu_len += len;
|
||||
if (value) {
|
||||
value->propertyArrayIndex = unsigned_value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
} else {
|
||||
if (value) {
|
||||
@@ -3077,15 +3082,17 @@ int bacapp_property_value_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 3, NULL)) {
|
||||
len = bacnet_unsigned_context_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 3, &unsigned_value);
|
||||
if (len == BACNET_STATUS_ERROR) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
} else if (unsigned_value > UINT8_MAX) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
} else {
|
||||
apdu_len += len;
|
||||
if (value) {
|
||||
value->priority = unsigned_value;
|
||||
if (len > 0) {
|
||||
if (unsigned_value > UINT8_MAX) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
} else {
|
||||
apdu_len += len;
|
||||
if (value) {
|
||||
value->priority = unsigned_value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
} else {
|
||||
if (value) {
|
||||
|
||||
+1409
-629
File diff suppressed because it is too large
Load Diff
+421
-580
File diff suppressed because it is too large
Load Diff
+92
-31
@@ -39,90 +39,151 @@
|
||||
|
||||
/** @file bacerror.c Encode/Decode BACnet Errors */
|
||||
|
||||
/* encode service */
|
||||
/**
|
||||
* @brief Encodes BACnet Error class and code values into a PDU
|
||||
* From clause 21. FORMAL DESCRIPTION OF APPLICATION PROTOCOL DATA UNITS
|
||||
*
|
||||
* Error ::= SEQUENCE {
|
||||
* -- NOTE: The valid combinations of error-class and error-code
|
||||
* -- are defined in Clause 18.
|
||||
* error-class ENUMERATED,
|
||||
* error-code ENUMERATED
|
||||
* }
|
||||
*
|
||||
* @param apdu - buffer for the data to be encoded, or NULL for length
|
||||
* @param invoke_id - invokeID to be encoded
|
||||
* @param service - BACnet service to be encoded
|
||||
* @param error_class - #BACNET_ERROR_CLASS value to be encoded
|
||||
* @param error_code - #BACNET_ERROR_CODE value to be encoded
|
||||
* @return number of bytes encoded
|
||||
*/
|
||||
int bacerror_encode_apdu(uint8_t *apdu,
|
||||
uint8_t invoke_id,
|
||||
BACNET_CONFIRMED_SERVICE service,
|
||||
BACNET_ERROR_CLASS error_class,
|
||||
BACNET_ERROR_CODE error_code)
|
||||
{
|
||||
int apdu_len = 0; /* total length of the apdu, return value */
|
||||
/* length of the specific element of the PDU */
|
||||
int len = 0;
|
||||
/* total length of the apdu, return value */
|
||||
int apdu_len = 0;
|
||||
|
||||
if (apdu) {
|
||||
apdu[0] = PDU_TYPE_ERROR;
|
||||
apdu[1] = invoke_id;
|
||||
apdu[2] = service;
|
||||
apdu_len = 3;
|
||||
/* service parameters */
|
||||
apdu_len += encode_application_enumerated(&apdu[apdu_len], error_class);
|
||||
apdu_len += encode_application_enumerated(&apdu[apdu_len], error_code);
|
||||
}
|
||||
len = 3;
|
||||
apdu_len = len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
/* service parameters */
|
||||
len = encode_application_enumerated(apdu, error_class);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_application_enumerated(apdu, error_code);
|
||||
apdu_len += len;
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
#if !BACNET_SVC_SERVER
|
||||
/* decode the application class and code */
|
||||
/**
|
||||
* @brief Decodes from bytes a BACnet Error service APDU
|
||||
* From clause 21. FORMAL DESCRIPTION OF APPLICATION PROTOCOL DATA UNITS
|
||||
*
|
||||
* Error ::= SEQUENCE {
|
||||
* -- NOTE: The valid combinations of error-class and error-code
|
||||
* -- are defined in Clause 18.
|
||||
* error-class ENUMERATED,
|
||||
* error-code ENUMERATED
|
||||
* }
|
||||
*
|
||||
* @param apdu - buffer of data to be decoded
|
||||
* @param apdu_size - number of bytes in the buffer
|
||||
* @param error_class - decoded #BACNET_ERROR_CLASS value
|
||||
* @param error_code - decoded #BACNET_ERROR_CODE value
|
||||
*
|
||||
* @return number of bytes decoded, or #BACNET_STATUS_ERROR (-1) if malformed
|
||||
*/
|
||||
int bacerror_decode_error_class_and_code(uint8_t *apdu,
|
||||
unsigned apdu_len,
|
||||
unsigned apdu_size,
|
||||
BACNET_ERROR_CLASS *error_class,
|
||||
BACNET_ERROR_CODE *error_code)
|
||||
{
|
||||
int len = 0;
|
||||
uint8_t tag_number = 0;
|
||||
uint32_t len_value_type = 0;
|
||||
int apdu_len = 0;
|
||||
int tag_len = 0;
|
||||
uint32_t decoded_value = 0;
|
||||
|
||||
if (apdu && apdu_len) {
|
||||
if (apdu) {
|
||||
/* error class */
|
||||
len += decode_tag_number_and_value(
|
||||
&apdu[len], &tag_number, &len_value_type);
|
||||
if (tag_number != BACNET_APPLICATION_TAG_ENUMERATED) {
|
||||
return 0;
|
||||
tag_len = bacnet_enumerated_application_decode(
|
||||
&apdu[apdu_len], apdu_size-apdu_len, &decoded_value);
|
||||
if (tag_len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len += decode_enumerated(&apdu[len], len_value_type, &decoded_value);
|
||||
if (error_class) {
|
||||
*error_class = (BACNET_ERROR_CLASS)decoded_value;
|
||||
}
|
||||
apdu_len += tag_len;
|
||||
/* error code */
|
||||
len += decode_tag_number_and_value(
|
||||
&apdu[len], &tag_number, &len_value_type);
|
||||
if (tag_number != BACNET_APPLICATION_TAG_ENUMERATED) {
|
||||
return 0;
|
||||
tag_len = bacnet_enumerated_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &decoded_value);
|
||||
if (tag_len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len += decode_enumerated(&apdu[len], len_value_type, &decoded_value);
|
||||
if (error_code) {
|
||||
*error_code = (BACNET_ERROR_CODE)decoded_value;
|
||||
}
|
||||
apdu_len += tag_len;
|
||||
}
|
||||
|
||||
return len;
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* decode the service request only */
|
||||
/**
|
||||
* @brief Decodes from bytes a BACnet Error service
|
||||
* @param apdu - buffer of data to be decoded
|
||||
* @param apdu_size - number of bytes in the buffer
|
||||
* @param invoke_id - decoded invokeID
|
||||
* @param service - decoded BACnet service
|
||||
* @param error_class - decoded #BACNET_ERROR_CLASS value
|
||||
* @param error_code - decoded #BACNET_ERROR_CODE value
|
||||
*
|
||||
* @return number of bytes decoded, or #BACNET_STATUS_ERROR (-1) if malformed
|
||||
*/
|
||||
int bacerror_decode_service_request(uint8_t *apdu,
|
||||
unsigned apdu_len,
|
||||
unsigned apdu_size,
|
||||
uint8_t *invoke_id,
|
||||
BACNET_CONFIRMED_SERVICE *service,
|
||||
BACNET_ERROR_CLASS *error_class,
|
||||
BACNET_ERROR_CODE *error_code)
|
||||
{
|
||||
int apdu_len = BACNET_STATUS_ERROR;
|
||||
int len = 0;
|
||||
|
||||
if (apdu && apdu_len > 2) {
|
||||
if (apdu && (apdu_size > 2)) {
|
||||
if (invoke_id) {
|
||||
*invoke_id = apdu[0];
|
||||
}
|
||||
if (service) {
|
||||
*service = (BACNET_CONFIRMED_SERVICE)apdu[1];
|
||||
}
|
||||
len += 2;
|
||||
|
||||
len = 2;
|
||||
apdu_len = len;
|
||||
/* decode the application class and code */
|
||||
len += bacerror_decode_error_class_and_code(
|
||||
&apdu[2], apdu_len - 2, error_class, error_code);
|
||||
len = bacerror_decode_error_class_and_code(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, error_class, error_code);
|
||||
if (len > 0) {
|
||||
apdu_len += len;
|
||||
} else {
|
||||
apdu_len = BACNET_STATUS_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
return apdu_len;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -44,7 +44,7 @@ extern "C" {
|
||||
BACNET_STACK_EXPORT
|
||||
int bacerror_decode_service_request(
|
||||
uint8_t * apdu,
|
||||
unsigned apdu_len,
|
||||
unsigned apdu_size,
|
||||
uint8_t * invoke_id,
|
||||
BACNET_CONFIRMED_SERVICE * service,
|
||||
BACNET_ERROR_CLASS * error_class,
|
||||
@@ -53,7 +53,7 @@ extern "C" {
|
||||
BACNET_STACK_EXPORT
|
||||
int bacerror_decode_error_class_and_code(
|
||||
uint8_t * apdu,
|
||||
unsigned apdu_len,
|
||||
unsigned apdu_size,
|
||||
BACNET_ERROR_CLASS * error_class,
|
||||
BACNET_ERROR_CODE * error_code);
|
||||
|
||||
|
||||
+100
-30
@@ -74,6 +74,23 @@ static bool is_data_value_schedule_compatible(uint8_t tag)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode the BACnetTimeValue
|
||||
*
|
||||
* From clause 21. FORMAL DESCRIPTION OF APPLICATION PROTOCOL DATA UNITS
|
||||
*
|
||||
* BACnetTimeValue ::= SEQUENCE {
|
||||
* time Time,
|
||||
* value ABSTRACT-SYNTAX.&Type
|
||||
* -- any primitive datatype;
|
||||
* -- complex types cannot be decoded
|
||||
* }
|
||||
*
|
||||
* @param apdu - buffer of data to be encoded, or NULL for length
|
||||
* @param tag_number - context tag number to be encoded
|
||||
* @param value - value to be encoded
|
||||
* @return the number of apdu bytes encoded
|
||||
*/
|
||||
int bacnet_time_value_encode(uint8_t *apdu, BACNET_TIME_VALUE *value)
|
||||
{
|
||||
int len;
|
||||
@@ -106,6 +123,14 @@ int bacapp_encode_time_value(uint8_t *apdu, BACNET_TIME_VALUE *value)
|
||||
return bacnet_time_value_encode(apdu, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode the BACnetTimeValue as Context Tagged
|
||||
* as defined in clause 20.2.1 General Rules for Encoding BACnet Tags
|
||||
* @param apdu - buffer of data to be encoded, or NULL for length
|
||||
* @param tag_number - context tag number to be encoded
|
||||
* @param value - value to be encoded
|
||||
* @return the number of apdu bytes encoded
|
||||
*/
|
||||
int bacnet_time_value_context_encode(
|
||||
uint8_t *apdu, uint8_t tag_number, BACNET_TIME_VALUE *value)
|
||||
{
|
||||
@@ -140,7 +165,12 @@ int bacapp_encode_context_time_value(
|
||||
return bacnet_time_value_context_encode(apdu, tag_number, value);
|
||||
}
|
||||
|
||||
/** returns 0 if OK, -1 on error */
|
||||
/**
|
||||
* @brief Convert primitive value from application data value
|
||||
* @param dest Primitive Data Value
|
||||
* @param src Application Data Value
|
||||
* @return BACNET_STATUS_OK, or BACNET_STATUS_ERROR if an error occurs
|
||||
*/
|
||||
int bacnet_application_to_primitive_data_value(
|
||||
struct BACnet_Primitive_Data_Value *dest,
|
||||
const struct BACnet_Application_Data_Value *src)
|
||||
@@ -155,7 +185,12 @@ int bacnet_application_to_primitive_data_value(
|
||||
return BACNET_STATUS_OK;
|
||||
}
|
||||
|
||||
/** returns 0 if OK, -1 on error */
|
||||
/**
|
||||
* @brief Convert primitive value to application data value
|
||||
* @param dest Application Data Value
|
||||
* @param src Primitive Data Value
|
||||
* @return BACNET_STATUS_OK, or BACNET_STATUS_ERROR if an error occurs
|
||||
*/
|
||||
int bacnet_primitive_to_application_data_value(
|
||||
struct BACnet_Application_Data_Value *dest,
|
||||
const struct BACnet_Primitive_Data_Value *src)
|
||||
@@ -167,9 +202,17 @@ int bacnet_primitive_to_application_data_value(
|
||||
memset(dest, 0, sizeof(struct BACnet_Application_Data_Value));
|
||||
dest->tag = src->tag;
|
||||
memcpy(&dest->type, &src->type, sizeof(src->type));
|
||||
return BACNET_STATUS_OK; /* OK */
|
||||
return BACNET_STATUS_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief decode a BACnetTimeValue
|
||||
*
|
||||
* @param apdu - buffer of data to be decoded
|
||||
* @param max_apdu_len - number of bytes in the buffer
|
||||
* @param value - stores the decoded property value
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR if errors occur
|
||||
*/
|
||||
int bacnet_time_value_decode(
|
||||
uint8_t *apdu, int max_apdu_len, BACNET_TIME_VALUE *value)
|
||||
{
|
||||
@@ -180,7 +223,7 @@ int bacnet_time_value_decode(
|
||||
len = bacnet_time_application_decode(
|
||||
&apdu[apdu_len], max_apdu_len, &value->Time);
|
||||
if (len <= 0) {
|
||||
return -1;
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
@@ -192,7 +235,7 @@ int bacnet_time_value_decode(
|
||||
if (BACNET_STATUS_OK !=
|
||||
bacnet_application_to_primitive_data_value(
|
||||
&value->Value, &full_data_value)) {
|
||||
return -1;
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
@@ -204,6 +247,14 @@ int bacapp_decode_time_value(uint8_t *apdu, BACNET_TIME_VALUE *value)
|
||||
return bacnet_time_value_decode(apdu, MAX_APDU, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief decode a context encoded BACnetTimeValue
|
||||
* @param apdu - buffer of data to be decoded
|
||||
* @param max_apdu_len - number of bytes in the buffer
|
||||
* @param tag_number - context tag number to match
|
||||
* @param value - stores the decoded property value
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR if an error occurs
|
||||
*/
|
||||
int bacnet_time_value_context_decode(uint8_t *apdu,
|
||||
int max_apdu_len,
|
||||
uint8_t tag_number,
|
||||
@@ -212,26 +263,24 @@ int bacnet_time_value_context_decode(uint8_t *apdu,
|
||||
int len;
|
||||
int apdu_len = 0;
|
||||
|
||||
if ((max_apdu_len - apdu_len) >= 1 &&
|
||||
decode_is_opening_tag_number(&apdu[apdu_len], tag_number)) {
|
||||
apdu_len += 1;
|
||||
if (bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], max_apdu_len - apdu_len, tag_number, &len)) {
|
||||
apdu_len += len;
|
||||
} else {
|
||||
return -1;
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
|
||||
len = bacnet_time_value_decode(
|
||||
&apdu[apdu_len], max_apdu_len - apdu_len, value);
|
||||
if (len > 0) {
|
||||
apdu_len += len;
|
||||
} else {
|
||||
return -1;
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
|
||||
if ((max_apdu_len - apdu_len) >= 1 &&
|
||||
decode_is_closing_tag_number(&apdu[apdu_len], tag_number)) {
|
||||
apdu_len += 1;
|
||||
if (bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], max_apdu_len - apdu_len, tag_number, &len)) {
|
||||
apdu_len += len;
|
||||
} else {
|
||||
return -1;
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
@@ -243,6 +292,15 @@ int bacapp_decode_context_time_value(
|
||||
return bacnet_time_value_context_decode(apdu, MAX_APDU, tag_number, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief decode a context encoded list of BACnetTimeValue
|
||||
* @param apdu - buffer of data to be decoded
|
||||
* @param max_apdu_len - number of bytes in the buffer
|
||||
* @param tag_number - context tag number to match
|
||||
* @param time_values - stores the decoded property values
|
||||
* @param max_time_values - number of values to be able to store
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR if an error occurs
|
||||
*/
|
||||
int bacnet_time_values_context_decode(uint8_t *apdu,
|
||||
const int max_apdu_len,
|
||||
const uint8_t tag_number,
|
||||
@@ -257,10 +315,11 @@ int bacnet_time_values_context_decode(uint8_t *apdu,
|
||||
BACNET_TIME_VALUE dummy;
|
||||
|
||||
/* day-schedule [0] SEQUENCE OF BACnetTimeValue */
|
||||
if (decode_is_opening_tag_number(&apdu[apdu_len], tag_number)) {
|
||||
apdu_len++;
|
||||
while ((apdu_len < max_apdu_len) &&
|
||||
!decode_is_closing_tag_number(&apdu[apdu_len], tag_number)) {
|
||||
if (bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], max_apdu_len - apdu_len, tag_number, &len)) {
|
||||
apdu_len += len;
|
||||
while (!bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], max_apdu_len - apdu_len, tag_number, &len)) {
|
||||
if (count_values < max_time_values) {
|
||||
len = bacnet_time_value_decode(&apdu[apdu_len],
|
||||
max_apdu_len - apdu_len, &time_values[count_values++]);
|
||||
@@ -269,9 +328,10 @@ int bacnet_time_values_context_decode(uint8_t *apdu,
|
||||
&apdu[apdu_len], max_apdu_len - apdu_len, &dummy);
|
||||
}
|
||||
if (len < 0) {
|
||||
return -1;
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
len = 0;
|
||||
}
|
||||
/* Zeroing other values */
|
||||
for (j = count_values; j < max_time_values; j++) {
|
||||
@@ -282,20 +342,29 @@ int bacnet_time_values_context_decode(uint8_t *apdu,
|
||||
time_values[j].Time.sec = 0;
|
||||
time_values[j].Time.hundredths = 0;
|
||||
}
|
||||
/* overflow ! */
|
||||
if (apdu_len >= max_apdu_len) {
|
||||
return -1;
|
||||
/* closing tag */
|
||||
if (len > 0) {
|
||||
apdu_len += len;
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len++; /* closing tag */
|
||||
if (out_count) {
|
||||
*out_count = count_values;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
return -1;
|
||||
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
|
||||
/* Encodes a : [x] SEQUENCE OF BACnetTimeValue into a fixed-size buffer */
|
||||
/**
|
||||
* @brief Encodes a : [x] SEQUENCE OF BACnetTimeValue into a fixed-size buffer
|
||||
* @param apdu - buffer of data to be encoded, or NULL for buffer length
|
||||
* @param tag_number - context tag number to be encoded
|
||||
* @param value - value to be encoded
|
||||
* @return the number of apdu bytes encoded, or BACNET_STATUS_ERROR
|
||||
*/
|
||||
int bacnet_time_values_context_encode(uint8_t *apdu,
|
||||
uint8_t tag_number,
|
||||
BACNET_TIME_VALUE *time_values,
|
||||
@@ -312,7 +381,6 @@ int bacnet_time_values_context_encode(uint8_t *apdu,
|
||||
apdu_offset = &apdu[apdu_len];
|
||||
}
|
||||
apdu_len += encode_opening_tag(apdu_offset, tag_number);
|
||||
|
||||
for (j = 0; j < max_time_values; j++)
|
||||
/* Encode only non-null values (NULL,00:00:00.00) */
|
||||
if (time_values[j].Value.tag != BACNET_APPLICATION_TAG_NULL ||
|
||||
@@ -321,8 +389,9 @@ int bacnet_time_values_context_encode(uint8_t *apdu,
|
||||
apdu_offset = &apdu[apdu_len];
|
||||
}
|
||||
len = bacnet_time_value_encode(apdu_offset, &time_values[j]);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
if (len < 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
}
|
||||
/* close tag */
|
||||
@@ -330,5 +399,6 @@ int bacnet_time_values_context_encode(uint8_t *apdu,
|
||||
apdu_offset = &apdu[apdu_len];
|
||||
}
|
||||
apdu_len += encode_closing_tag(apdu_offset, tag_number);
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
@@ -151,10 +151,61 @@ bool Access_Credential_Object_Name(
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode a BACnetARRAY property element
|
||||
* @param object_instance [in] BACnet network port object instance number
|
||||
* @param index [in] array index requested:
|
||||
* 0 to N for individual array members
|
||||
* @param apdu [out] Buffer in which the APDU contents are built, or NULL to
|
||||
* return the length of buffer if it had been built
|
||||
* @return The length of the apdu encoded or
|
||||
* BACNET_STATUS_ERROR for ERROR_CODE_INVALID_ARRAY_INDEX
|
||||
*/
|
||||
static int Access_Credential_Authentication_Factor_Array_Encode(
|
||||
uint32_t object_instance, BACNET_ARRAY_INDEX index, uint8_t *apdu)
|
||||
{
|
||||
int apdu_len = BACNET_STATUS_ERROR;
|
||||
|
||||
if (object_instance < MAX_ACCESS_CREDENTIALS) {
|
||||
if (index < ac_descr[object_instance].auth_factors_count) {
|
||||
apdu_len = bacapp_encode_credential_authentication_factor(
|
||||
apdu, &ac_descr[object_instance].auth_factors[index]);
|
||||
}
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode a BACnetARRAY property element
|
||||
* @param object_instance [in] BACnet network port object instance number
|
||||
* @param index [in] array index requested:
|
||||
* 0 to N for individual array members
|
||||
* @param apdu [out] Buffer in which the APDU contents are built, or NULL to
|
||||
* return the length of buffer if it had been built
|
||||
* @return The length of the apdu encoded or
|
||||
* BACNET_STATUS_ERROR for ERROR_CODE_INVALID_ARRAY_INDEX
|
||||
*/
|
||||
static int Access_Credential_Assigned_Access_Rights_Array_Encode(
|
||||
uint32_t object_instance, BACNET_ARRAY_INDEX index, uint8_t *apdu)
|
||||
{
|
||||
int apdu_len = BACNET_STATUS_ERROR;
|
||||
|
||||
if (object_instance < MAX_ACCESS_CREDENTIALS) {
|
||||
if (index < ac_descr[object_instance].assigned_access_rights_count) {
|
||||
apdu_len = bacapp_encode_assigned_access_rights(
|
||||
apdu, &ac_descr[object_instance].assigned_access_rights[index]);
|
||||
}
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* return apdu len, or BACNET_STATUS_ERROR on error */
|
||||
int Access_Credential_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata)
|
||||
{
|
||||
int len = 0;
|
||||
int apdu_size;
|
||||
int apdu_len = 0; /* return value */
|
||||
BACNET_BIT_STRING bit_string;
|
||||
BACNET_CHARACTER_STRING char_string;
|
||||
@@ -167,6 +218,7 @@ int Access_Credential_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata)
|
||||
return 0;
|
||||
}
|
||||
apdu = rpdata->application_data;
|
||||
apdu_size = rpdata->application_data_len;
|
||||
object_index = Access_Credential_Instance_To_Index(rpdata->object_instance);
|
||||
switch (rpdata->object_property) {
|
||||
case PROP_OBJECT_IDENTIFIER:
|
||||
@@ -218,35 +270,16 @@ int Access_Credential_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata)
|
||||
}
|
||||
break;
|
||||
case PROP_AUTHENTICATION_FACTORS:
|
||||
if (rpdata->array_index == 0) {
|
||||
apdu_len = encode_application_unsigned(
|
||||
&apdu[0], ac_descr[object_index].auth_factors_count);
|
||||
} else if (rpdata->array_index == BACNET_ARRAY_ALL) {
|
||||
for (i = 0; i < ac_descr[object_index].auth_factors_count;
|
||||
i++) {
|
||||
len = bacapp_encode_credential_authentication_factor(
|
||||
&apdu[0], &ac_descr[object_index].auth_factors[i]);
|
||||
if (apdu_len + len < MAX_APDU) {
|
||||
apdu_len += len;
|
||||
} else {
|
||||
rpdata->error_code =
|
||||
ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
|
||||
apdu_len = BACNET_STATUS_ABORT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (rpdata->array_index <=
|
||||
ac_descr[object_index].auth_factors_count) {
|
||||
apdu_len =
|
||||
bacapp_encode_credential_authentication_factor(&apdu[0],
|
||||
&ac_descr[object_index]
|
||||
.auth_factors[rpdata->array_index - 1]);
|
||||
} else {
|
||||
rpdata->error_class = ERROR_CLASS_PROPERTY;
|
||||
rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX;
|
||||
apdu_len = BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len = bacnet_array_encode(rpdata->object_instance,
|
||||
rpdata->array_index,
|
||||
Access_Credential_Authentication_Factor_Array_Encode,
|
||||
ac_descr[object_index].auth_factors_count, apdu, apdu_size);
|
||||
if (apdu_len == BACNET_STATUS_ABORT) {
|
||||
rpdata->error_code =
|
||||
ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
|
||||
} else if (apdu_len == BACNET_STATUS_ERROR) {
|
||||
rpdata->error_class = ERROR_CLASS_PROPERTY;
|
||||
rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX;
|
||||
}
|
||||
break;
|
||||
case PROP_ACTIVATION_TIME:
|
||||
@@ -262,35 +295,16 @@ int Access_Credential_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata)
|
||||
&apdu[0], ac_descr[object_index].credential_disable);
|
||||
break;
|
||||
case PROP_ASSIGNED_ACCESS_RIGHTS:
|
||||
if (rpdata->array_index == 0) {
|
||||
apdu_len = encode_application_unsigned(&apdu[0],
|
||||
ac_descr[object_index].assigned_access_rights_count);
|
||||
} else if (rpdata->array_index == BACNET_ARRAY_ALL) {
|
||||
for (i = 0;
|
||||
i < ac_descr[object_index].assigned_access_rights_count;
|
||||
i++) {
|
||||
len = bacapp_encode_assigned_access_rights(&apdu[0],
|
||||
&ac_descr[object_index].assigned_access_rights[i]);
|
||||
if (apdu_len + len < MAX_APDU) {
|
||||
apdu_len += len;
|
||||
} else {
|
||||
rpdata->error_code =
|
||||
ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
|
||||
apdu_len = BACNET_STATUS_ABORT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (rpdata->array_index <=
|
||||
ac_descr[object_index].assigned_access_rights_count) {
|
||||
apdu_len = bacapp_encode_assigned_access_rights(&apdu[0],
|
||||
&ac_descr[object_index]
|
||||
.assigned_access_rights[rpdata->array_index - 1]);
|
||||
} else {
|
||||
rpdata->error_class = ERROR_CLASS_PROPERTY;
|
||||
rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX;
|
||||
apdu_len = BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len = bacnet_array_encode(rpdata->object_instance,
|
||||
rpdata->array_index,
|
||||
Access_Credential_Assigned_Access_Rights_Array_Encode,
|
||||
ac_descr[object_index].assigned_access_rights_count, apdu, apdu_size);
|
||||
if (apdu_len == BACNET_STATUS_ABORT) {
|
||||
rpdata->error_code =
|
||||
ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
|
||||
} else if (apdu_len == BACNET_STATUS_ERROR) {
|
||||
rpdata->error_class = ERROR_CLASS_PROPERTY;
|
||||
rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -291,6 +291,10 @@ int Access_Point_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata)
|
||||
}
|
||||
}
|
||||
break;
|
||||
case PROP_PRIORITY_FOR_WRITING:
|
||||
apdu_len = encode_application_unsigned(
|
||||
&apdu[0], ap_descr[object_index].priority_for_writing);
|
||||
break;
|
||||
default:
|
||||
rpdata->error_class = ERROR_CLASS_PROPERTY;
|
||||
rpdata->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include "bacnet/bacdef.h"
|
||||
#include "bacnet/cov.h"
|
||||
#include "bacnet/datetime.h"
|
||||
#include "bacnet/readrange.h"
|
||||
#include "bacnet/rp.h"
|
||||
#include "bacnet/wp.h"
|
||||
|
||||
|
||||
@@ -30,7 +30,9 @@
|
||||
#endif
|
||||
|
||||
/* marking some code as 'deprecated' */
|
||||
# if defined(_MSC_VER)
|
||||
#if defined(BACNET_STACK_DEPRECATED_DISABLE)
|
||||
# define BACNET_STACK_DEPRECATED(message)
|
||||
#elif defined(_MSC_VER)
|
||||
# define BACNET_STACK_DEPRECATED(message) __declspec(deprecated(message))
|
||||
#elif defined(__GNUC__)
|
||||
# define BACNET_STACK_DEPRECATED(message) __attribute__((deprecated(message)))
|
||||
|
||||
+11
-25
@@ -107,7 +107,7 @@ int create_object_encode_service_request(
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param apdu_len Count of valid bytes in the buffer.
|
||||
* @param apdu_size Count of valid bytes in the buffer.
|
||||
* @param data Pointer to the property decoded data to be stored
|
||||
*
|
||||
* @return Bytes decoded or BACNET_STATUS_REJECT on error.
|
||||
@@ -135,7 +135,7 @@ int create_object_decode_service_request(
|
||||
/* object-identifier [1] BACnetObjectIdentifier */
|
||||
len = bacnet_object_id_context_decode(&apdu[apdu_len], apdu_size - apdu_len,
|
||||
1, &object_type, &object_instance);
|
||||
if ((len != BACNET_STATUS_ERROR) && (len != 0)) {
|
||||
if (len > 0) {
|
||||
if ((object_type >= MAX_BACNET_OBJECT_TYPE) ||
|
||||
(object_instance >= BACNET_MAX_INSTANCE)) {
|
||||
if (data) {
|
||||
@@ -152,7 +152,7 @@ int create_object_decode_service_request(
|
||||
/* object-type [0] BACnetObjectType */
|
||||
len = bacnet_enumerated_context_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 0, &enumerated_value);
|
||||
if ((len != BACNET_STATUS_ERROR) && (len != 0)) {
|
||||
if (len > 0) {
|
||||
if (enumerated_value >= MAX_BACNET_OBJECT_TYPE) {
|
||||
if (data) {
|
||||
data->error_code = ERROR_CODE_REJECT_PARAMETER_OUT_OF_RANGE;
|
||||
@@ -188,7 +188,7 @@ int create_object_decode_service_request(
|
||||
}
|
||||
len = bacapp_property_value_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, list_of_initial_values);
|
||||
if (len == BACNET_STATUS_ERROR) {
|
||||
if (len <= 0) {
|
||||
if (data) {
|
||||
data->error_code = ERROR_CODE_REJECT_INVALID_TAG;
|
||||
}
|
||||
@@ -261,7 +261,7 @@ int create_object_ack_encode(
|
||||
* @param apdu Pointer to the buffer for decoding.
|
||||
* @param apdu_size size of the buffer for decoding.
|
||||
* @param data Pointer to the property data to be encoded.
|
||||
* @return Bytes encoded or BACNET_STATUS_REJECT on error.
|
||||
* @return Bytes encoded or #BACNET_STATUS_ERROR on error.
|
||||
*/
|
||||
int create_object_ack_service_decode(
|
||||
uint8_t *apdu, uint16_t apdu_size, BACNET_CREATE_OBJECT_DATA *data)
|
||||
@@ -272,7 +272,9 @@ int create_object_ack_service_decode(
|
||||
|
||||
apdu_len = bacnet_object_id_application_decode(
|
||||
apdu, apdu_size, &object_type, &object_instance);
|
||||
if (apdu_len > 0) {
|
||||
if (apdu_len <= 0) {
|
||||
apdu_len = BACNET_STATUS_ERROR;
|
||||
} else {
|
||||
if (data) {
|
||||
data->object_instance = object_instance;
|
||||
data->object_type = object_type;
|
||||
@@ -327,7 +329,7 @@ int create_object_error_ack_service_encode(
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode an Error acknowledge in the APDU.
|
||||
* @brief Encode a CreateObject Error acknowledge in the APDU.
|
||||
* @param apdu [in] The APDU buffer.
|
||||
* @param invoke_id [in] Invoked service ID.
|
||||
* @param data [in] Data of the invoked property.
|
||||
@@ -378,21 +380,13 @@ int create_object_error_ack_service_decode(
|
||||
data->error_class = ERROR_CLASS_SERVICES;
|
||||
data->error_code = ERROR_CODE_REJECT_PARAMETER_OUT_OF_RANGE;
|
||||
}
|
||||
if (apdu_size < apdu_len) {
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
/* Opening Context tag 0 - Error */
|
||||
if (decode_is_opening_tag_number(apdu, 0)) {
|
||||
/* opening tag 0 is 1 byte */
|
||||
len = 1;
|
||||
if (bacnet_is_opening_tag_number(apdu, apdu_size, 0, &len)) {
|
||||
apdu_len += len;
|
||||
apdu += len;
|
||||
} else {
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
if (apdu_size < apdu_len) {
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
len = bacerror_decode_error_class_and_code(
|
||||
apdu, apdu_size - apdu_len, &error_class, &error_code);
|
||||
if (len > 0) {
|
||||
@@ -405,21 +399,13 @@ int create_object_error_ack_service_decode(
|
||||
} else {
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
if (apdu_size < apdu_len) {
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
/* Closing Context tag 0 - Error */
|
||||
if (decode_is_closing_tag_number(apdu, 0)) {
|
||||
/* closing tag 0 is 1 byte */
|
||||
len = 1;
|
||||
if (bacnet_is_closing_tag_number(apdu, apdu_size-apdu_len, 0, &len)) {
|
||||
apdu_len += len;
|
||||
apdu += len;
|
||||
} else {
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
if (apdu_size < apdu_len) {
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
len = bacnet_unsigned_context_decode(
|
||||
apdu, apdu_size - apdu_len, 1, &first_failed_element_number);
|
||||
if (len > 0) {
|
||||
|
||||
+108
-44
@@ -1043,85 +1043,149 @@ bool datetime_local_to_utc(BACNET_DATE_TIME *utc_time,
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode a BACnetDateTime complex data type
|
||||
* From clause 21. FORMAL DESCRIPTION OF APPLICATION PROTOCOL DATA UNITS
|
||||
*
|
||||
* BACnetDateTime ::= SEQUENCE {
|
||||
* date Date, -- see Clause 20.2.12 for restrictions
|
||||
* time Time -- see Clause 20.2.13 for restrictions
|
||||
* }
|
||||
*
|
||||
* @param apdu buffer to be encoded, or NULL for length
|
||||
* @param value The value to be encoded.
|
||||
* @return the number of apdu bytes encoded
|
||||
*/
|
||||
int bacapp_encode_datetime(uint8_t *apdu, BACNET_DATE_TIME *value)
|
||||
{
|
||||
int len = 0;
|
||||
int apdu_len = 0;
|
||||
|
||||
if (apdu && value) {
|
||||
len = encode_application_date(&apdu[0], &value->date);
|
||||
if (value) {
|
||||
len = encode_application_date(apdu, &value->date);
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
len = encode_application_time(&apdu[apdu_len], &value->time);
|
||||
len = encode_application_time(apdu, &value->time);
|
||||
apdu_len += len;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode a context tagged BACnetDateTime complex data type
|
||||
* @param apdu buffer to be encoded, or NULL for length
|
||||
* @param tag_number - context tag number to be encoded
|
||||
* @param value The value to be encoded.
|
||||
* @return the number of apdu bytes encoded
|
||||
*/
|
||||
int bacapp_encode_context_datetime(
|
||||
uint8_t *apdu, uint8_t tag_number, BACNET_DATE_TIME *value)
|
||||
{
|
||||
int len = 0;
|
||||
int apdu_len = 0;
|
||||
|
||||
if (apdu && value) {
|
||||
len = encode_opening_tag(&apdu[apdu_len], tag_number);
|
||||
if (value) {
|
||||
len = encode_opening_tag(apdu, tag_number);
|
||||
apdu_len += len;
|
||||
|
||||
len = bacapp_encode_datetime(&apdu[apdu_len], value);
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = bacapp_encode_datetime(apdu, value);
|
||||
apdu_len += len;
|
||||
|
||||
len = encode_closing_tag(&apdu[apdu_len], tag_number);
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_closing_tag(apdu, tag_number);
|
||||
apdu_len += len;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decodes a BACnetDateTime value from APDU buffer
|
||||
* @param apdu - the APDU buffer
|
||||
* @param apdu_size - the APDU buffer size
|
||||
* @param value - parameter to store the value after decoding
|
||||
* @return length of the APDU buffer decoded, or BACNET_STATUS_ERROR
|
||||
*/
|
||||
int bacnet_datetime_decode(
|
||||
uint8_t *apdu, uint32_t apdu_size, BACNET_DATE_TIME *value)
|
||||
{
|
||||
int len = 0;
|
||||
int apdu_len = 0;
|
||||
BACNET_DATE *bdate = NULL;
|
||||
BACNET_TIME *btime = NULL;
|
||||
|
||||
if (value) {
|
||||
bdate = &value->date;
|
||||
}
|
||||
len = bacnet_date_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, bdate);
|
||||
if (len < 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
if (value) {
|
||||
btime = &value->time;
|
||||
}
|
||||
len = bacnet_time_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, btime);
|
||||
if (len < 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
int bacapp_decode_datetime(uint8_t *apdu, BACNET_DATE_TIME *value)
|
||||
{
|
||||
int len = 0;
|
||||
int section_len;
|
||||
return bacnet_datetime_decode(apdu, MAX_APDU, value);
|
||||
}
|
||||
|
||||
if (-1 ==
|
||||
(section_len = decode_application_date(&apdu[len], &value->date))) {
|
||||
return -1;
|
||||
/**
|
||||
* @brief Decodes a context tagged BACnetDateTime value from APDU buffer
|
||||
* @param apdu - the APDU buffer
|
||||
* @param apdu_size - the APDU buffer size
|
||||
* @param tag_number - context tag number to be encoded
|
||||
* @param value - parameter to store the value after decoding
|
||||
* @return length of the APDU buffer decoded, or BACNET_STATUS_ERROR
|
||||
*/
|
||||
int bacnet_datetime_context_decode(uint8_t *apdu,
|
||||
uint32_t apdu_size,
|
||||
uint8_t tag_number,
|
||||
BACNET_DATE_TIME *value)
|
||||
{
|
||||
int apdu_len = 0;
|
||||
int len;
|
||||
|
||||
if (!bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len += section_len;
|
||||
|
||||
if (-1 ==
|
||||
(section_len = decode_application_time(&apdu[len], &value->time))) {
|
||||
return -1;
|
||||
apdu_len += len;
|
||||
len = bacnet_datetime_decode(&apdu[apdu_len], apdu_size - apdu_len, value);
|
||||
if (len < 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
if (!bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
len += section_len;
|
||||
|
||||
return len;
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
int bacapp_decode_context_datetime(
|
||||
uint8_t *apdu, uint8_t tag_number, BACNET_DATE_TIME *value)
|
||||
{
|
||||
int apdu_len = 0;
|
||||
int len;
|
||||
|
||||
if (decode_is_opening_tag_number(&apdu[apdu_len], tag_number)) {
|
||||
apdu_len++;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (-1 == (len = bacapp_decode_datetime(&apdu[apdu_len], value))) {
|
||||
return -1;
|
||||
} else {
|
||||
apdu_len += len;
|
||||
}
|
||||
|
||||
if (decode_is_closing_tag_number(&apdu[apdu_len], tag_number)) {
|
||||
apdu_len++;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
return apdu_len;
|
||||
return bacnet_datetime_context_decode(apdu, MAX_APDU, tag_number, value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+16
-6
@@ -27,6 +27,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "bacnet/bacnet_stack_exports.h"
|
||||
#include "bacnet/basic/sys/platform.h"
|
||||
|
||||
/* define our epic beginnings */
|
||||
#define BACNET_DATE_YEAR_EPOCH 1900
|
||||
@@ -264,17 +265,26 @@ bool datetime_time_init_ascii(BACNET_TIME *btime, const char *ascii);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
int bacapp_encode_datetime(uint8_t *apdu, BACNET_DATE_TIME *value);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
int bacapp_encode_context_datetime(
|
||||
uint8_t *apdu, uint8_t tag_number, BACNET_DATE_TIME *value);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
int bacapp_decode_datetime(uint8_t *apdu, BACNET_DATE_TIME *value);
|
||||
|
||||
int bacnet_datetime_decode(
|
||||
uint8_t *apdu, uint32_t apdu_size, BACNET_DATE_TIME *value);
|
||||
BACNET_STACK_EXPORT
|
||||
int bacapp_decode_context_datetime(
|
||||
uint8_t *apdu, uint8_t tag_number, BACNET_DATE_TIME *value);
|
||||
int bacnet_datetime_context_decode(
|
||||
uint8_t *apdu, uint32_t apdu_size, uint8_t tag_number,
|
||||
BACNET_DATE_TIME *value);
|
||||
|
||||
BACNET_STACK_DEPRECATED("Use bacnet_datetime_decode() instead")
|
||||
BACNET_STACK_EXPORT
|
||||
int bacapp_decode_datetime(
|
||||
uint8_t *apdu, BACNET_DATE_TIME *value);
|
||||
BACNET_STACK_DEPRECATED("Use bacnet_datetime_context_decode() instead")
|
||||
BACNET_STACK_EXPORT
|
||||
int bacapp_decode_context_datetime(uint8_t *apdu,
|
||||
uint8_t tag_number,
|
||||
BACNET_DATE_TIME *value);
|
||||
|
||||
/* implementation agnostic functions - create your own! */
|
||||
BACNET_STACK_EXPORT
|
||||
|
||||
@@ -66,12 +66,17 @@ int delete_object_decode_service_request(
|
||||
uint32_t object_instance = 0;
|
||||
|
||||
/* object-identifier BACnetObjectIdentifier */
|
||||
len = bacnet_object_id_application_decode(&apdu[apdu_len],
|
||||
apdu_size - apdu_len, &object_type, &object_instance);
|
||||
len = bacnet_object_id_application_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, &object_type, &object_instance);
|
||||
if (len == BACNET_STATUS_ERROR) {
|
||||
if (data) {
|
||||
data->error_code = ERROR_CODE_REJECT_MISSING_REQUIRED_PARAMETER;
|
||||
}
|
||||
if (data) {
|
||||
data->error_code = ERROR_CODE_REJECT_MISSING_REQUIRED_PARAMETER;
|
||||
}
|
||||
return BACNET_STATUS_REJECT;
|
||||
} else if (len == 0) {
|
||||
if (data) {
|
||||
data->error_code = ERROR_CODE_REJECT_INVALID_TAG;
|
||||
}
|
||||
return BACNET_STATUS_REJECT;
|
||||
} else {
|
||||
if ((object_type >= MAX_BACNET_OBJECT_TYPE) ||
|
||||
|
||||
+90
-78
@@ -42,42 +42,37 @@ int host_n_port_encode(uint8_t *apdu, BACNET_HOST_N_PORT *address)
|
||||
{
|
||||
int len = 0;
|
||||
int apdu_len = 0;
|
||||
uint8_t *apdu_offset = NULL;
|
||||
|
||||
if (address) {
|
||||
/* host [0] BACnetHostAddress - opening */
|
||||
len = encode_opening_tag(apdu, 0);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
if (address->host_ip_address) {
|
||||
/* CHOICE - ip-address [1] OCTET STRING */
|
||||
if (apdu) {
|
||||
apdu_offset = &apdu[apdu_len];
|
||||
}
|
||||
len = encode_context_octet_string(
|
||||
apdu_offset, 1, &address->host.ip_address);
|
||||
apdu_len += len;
|
||||
len =
|
||||
encode_context_octet_string(apdu, 1, &address->host.ip_address);
|
||||
} else if (address->host_name) {
|
||||
/* CHOICE - name [2] CharacterString */
|
||||
if (apdu) {
|
||||
apdu_offset = &apdu[apdu_len];
|
||||
}
|
||||
len = encode_context_character_string(
|
||||
apdu_offset, 1, &address->host.name);
|
||||
apdu_len += len;
|
||||
len = encode_context_character_string(apdu, 2, &address->host.name);
|
||||
} else {
|
||||
/* none */
|
||||
/* CHOICE - none [0] NULL */
|
||||
len = encode_context_null(apdu, 0);
|
||||
}
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
/* host [0] BACnetHostAddress - closing */
|
||||
if (apdu) {
|
||||
apdu_offset = &apdu[apdu_len];
|
||||
}
|
||||
len = encode_closing_tag(apdu_offset, 0);
|
||||
len = encode_closing_tag(apdu, 0);
|
||||
apdu_len += len;
|
||||
/* port [1] Unsigned16 */
|
||||
if (apdu) {
|
||||
apdu_offset = &apdu[apdu_len];
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_context_unsigned(apdu_offset, 1, address->port);
|
||||
/* port [1] Unsigned16 */
|
||||
len = encode_context_unsigned(apdu, 1, address->port);
|
||||
apdu_len += len;
|
||||
}
|
||||
|
||||
@@ -87,7 +82,7 @@ int host_n_port_encode(uint8_t *apdu, BACNET_HOST_N_PORT *address)
|
||||
/**
|
||||
* @brief Encode a BACnetHostNPort complex data type
|
||||
* @param apdu - the APDU buffer
|
||||
* @param tag_number - the APDU buffer size
|
||||
* @param tag_number - context tag number to be encoded
|
||||
* @param address - IP address and port number
|
||||
* @return length of the APDU buffer, or 0 if not able to encode
|
||||
*/
|
||||
@@ -96,21 +91,19 @@ int host_n_port_context_encode(
|
||||
{
|
||||
int len = 0;
|
||||
int apdu_len = 0;
|
||||
uint8_t *apdu_offset = NULL;
|
||||
|
||||
if (address) {
|
||||
apdu_offset = apdu;
|
||||
len = encode_opening_tag(apdu_offset, tag_number);
|
||||
len = encode_opening_tag(apdu, tag_number);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu_offset = &apdu[apdu_len];
|
||||
apdu += len;
|
||||
}
|
||||
len = host_n_port_encode(apdu_offset, address);
|
||||
len = host_n_port_encode(apdu, address);
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu_offset = &apdu[apdu_len];
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_closing_tag(apdu_offset, tag_number);
|
||||
len = encode_closing_tag(apdu, tag_number);
|
||||
apdu_len += len;
|
||||
}
|
||||
|
||||
@@ -134,64 +127,80 @@ int host_n_port_context_encode(
|
||||
* }
|
||||
*
|
||||
* @param apdu - the APDU buffer
|
||||
* @param apdu_len - the APDU buffer length
|
||||
* @param apdu_size - the APDU buffer length
|
||||
* @param error_code - error or reject or abort when error occurs
|
||||
* @param ip_address - IP address and port number
|
||||
* @return length of the APDU buffer decoded, or ERROR, REJECT, or ABORT
|
||||
*/
|
||||
int host_n_port_decode(uint8_t *apdu,
|
||||
uint16_t apdu_len,
|
||||
uint32_t apdu_size,
|
||||
BACNET_ERROR_CODE *error_code,
|
||||
BACNET_HOST_N_PORT *address)
|
||||
{
|
||||
int len = 0;
|
||||
BACNET_OCTET_STRING octet_string = { 0 };
|
||||
BACNET_CHARACTER_STRING char_string = { 0 };
|
||||
uint8_t tag_number = 0;
|
||||
uint32_t len_value_type = 0;
|
||||
int apdu_len = 0, len = 0;
|
||||
BACNET_OCTET_STRING *octet_string = NULL;
|
||||
BACNET_CHARACTER_STRING *char_string = NULL;
|
||||
BACNET_TAG tag = { 0 };
|
||||
BACNET_UNSIGNED_INTEGER unsigned_value = 0;
|
||||
|
||||
/* default reject code */
|
||||
if (error_code) {
|
||||
*error_code = ERROR_CODE_REJECT_MISSING_REQUIRED_PARAMETER;
|
||||
}
|
||||
/* check for value pointers */
|
||||
if ((apdu_len == 0) || (!apdu)) {
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
/* host [0] BACnetHostAddress - opening */
|
||||
if (!decode_is_opening_tag_number(&apdu[len++], 0)) {
|
||||
if (!bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 0, &len)) {
|
||||
if (error_code) {
|
||||
*error_code = ERROR_CODE_REJECT_INVALID_TAG;
|
||||
}
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
if (len > apdu_len) {
|
||||
apdu_len += len;
|
||||
len = bacnet_tag_decode(&apdu[apdu_len], apdu_size - apdu_len, &tag);
|
||||
if (len <= 0) {
|
||||
if (error_code) {
|
||||
*error_code = ERROR_CODE_REJECT_INVALID_TAG;
|
||||
}
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
len +=
|
||||
decode_tag_number_and_value(&apdu[len], &tag_number, &len_value_type);
|
||||
if (tag_number == 0) {
|
||||
apdu_len += len;
|
||||
if (tag.context && (tag.number == 0)) {
|
||||
/* CHOICE - none [0] NULL */
|
||||
address->host_ip_address = false;
|
||||
address->host_name = false;
|
||||
} else if (tag_number == 1) {
|
||||
if (address) {
|
||||
address->host_ip_address = false;
|
||||
address->host_name = false;
|
||||
}
|
||||
} else if (tag.context && (tag.number == 1)) {
|
||||
/* CHOICE - ip-address [1] OCTET STRING */
|
||||
address->host_ip_address = true;
|
||||
address->host_name = false;
|
||||
len += decode_octet_string(&apdu[len], len_value_type, &octet_string);
|
||||
if (len > apdu_len) {
|
||||
if (address) {
|
||||
address->host_ip_address = true;
|
||||
address->host_name = false;
|
||||
octet_string = &address->host.ip_address;
|
||||
}
|
||||
len = bacnet_octet_string_decode(&apdu[apdu_len], apdu_size - apdu_len,
|
||||
tag.len_value_type, octet_string);
|
||||
if (len < 0) {
|
||||
if (error_code) {
|
||||
*error_code = ERROR_CODE_REJECT_BUFFER_OVERFLOW;
|
||||
}
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
(void)octetstring_copy(&address->host.ip_address, &octet_string);
|
||||
} else if (tag_number == 2) {
|
||||
address->host_ip_address = false;
|
||||
address->host_name = true;
|
||||
len +=
|
||||
decode_character_string(&apdu[len], len_value_type, &char_string);
|
||||
if (len > apdu_len) {
|
||||
apdu_len += len;
|
||||
} else if (tag.context && (tag.number == 2)) {
|
||||
if (address) {
|
||||
address->host_ip_address = false;
|
||||
address->host_name = true;
|
||||
char_string = &address->host.name;
|
||||
}
|
||||
len = bacnet_character_string_decode(&apdu[apdu_len],
|
||||
apdu_size - apdu_len, tag.len_value_type, char_string);
|
||||
if (len == 0) {
|
||||
if (error_code) {
|
||||
*error_code = ERROR_CODE_REJECT_BUFFER_OVERFLOW;
|
||||
}
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
(void)characterstring_copy(&address->host.name, &char_string);
|
||||
apdu_len += len;
|
||||
} else {
|
||||
if (error_code) {
|
||||
*error_code = ERROR_CODE_REJECT_INVALID_TAG;
|
||||
@@ -199,36 +208,39 @@ int host_n_port_decode(uint8_t *apdu,
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
/* host [0] BACnetHostAddress - closing */
|
||||
if (!decode_is_closing_tag_number(&apdu[len++], 0)) {
|
||||
if (!bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 0, &len)) {
|
||||
if (error_code) {
|
||||
*error_code = ERROR_CODE_REJECT_INVALID_TAG;
|
||||
}
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
if (len > apdu_len) {
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* port [1] Unsigned16 */
|
||||
len +=
|
||||
decode_tag_number_and_value(&apdu[len], &tag_number, &len_value_type);
|
||||
if (tag_number != 1) {
|
||||
if (error_code) {
|
||||
*error_code = ERROR_CODE_REJECT_INVALID_TAG;
|
||||
len = bacnet_unsigned_context_decode(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, 1, &unsigned_value);
|
||||
if (len > 0) {
|
||||
if (unsigned_value <= UINT16_MAX) {
|
||||
if (address) {
|
||||
address->port = unsigned_value;
|
||||
}
|
||||
} else {
|
||||
if (error_code) {
|
||||
*error_code = ERROR_CODE_REJECT_PARAMETER_OUT_OF_RANGE;
|
||||
}
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
len += decode_unsigned(&apdu[len], len_value_type, &unsigned_value);
|
||||
if (len > apdu_len) {
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
if (unsigned_value <= UINT16_MAX) {
|
||||
address->port = unsigned_value;
|
||||
} else {
|
||||
if (error_code) {
|
||||
*error_code = ERROR_CODE_REJECT_PARAMETER_OUT_OF_RANGE;
|
||||
if (len == 0) {
|
||||
*error_code = ERROR_CODE_REJECT_INVALID_TAG;
|
||||
} else {
|
||||
*error_code = ERROR_CODE_REJECT_OTHER;
|
||||
}
|
||||
}
|
||||
return BACNET_STATUS_REJECT;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ extern "C" {
|
||||
BACNET_HOST_N_PORT *address);
|
||||
BACNET_STACK_EXPORT
|
||||
int host_n_port_decode(uint8_t *apdu,
|
||||
uint16_t apdu_len,
|
||||
uint32_t apdu_len,
|
||||
BACNET_ERROR_CODE *error_code,
|
||||
BACNET_HOST_N_PORT *address);
|
||||
BACNET_STACK_EXPORT
|
||||
|
||||
+142
-83
@@ -117,20 +117,20 @@ int bacapp_encode_timestamp(uint8_t *apdu, BACNET_TIMESTAMP *value)
|
||||
{
|
||||
int len = 0; /* length of each encoding */
|
||||
|
||||
if (value && apdu) {
|
||||
if (value) {
|
||||
switch (value->tag) {
|
||||
case TIME_STAMP_TIME:
|
||||
len = encode_context_time(&apdu[0], 0, &value->value.time);
|
||||
len = encode_context_time(apdu, value->tag, &value->value.time);
|
||||
break;
|
||||
|
||||
case TIME_STAMP_SEQUENCE:
|
||||
len = encode_context_unsigned(
|
||||
&apdu[0], 1, value->value.sequenceNum);
|
||||
apdu, value->tag, value->value.sequenceNum);
|
||||
break;
|
||||
|
||||
case TIME_STAMP_DATETIME:
|
||||
len = bacapp_encode_context_datetime(
|
||||
&apdu[0], 2, &value->value.dateTime);
|
||||
apdu, value->tag, &value->value.dateTime);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -160,109 +160,168 @@ int bacapp_encode_context_timestamp(
|
||||
int len = 0; /* length of each encoding */
|
||||
int apdu_len = 0;
|
||||
|
||||
if (value && apdu) {
|
||||
len = encode_opening_tag(&apdu[apdu_len], tag_number);
|
||||
if (value) {
|
||||
len = encode_opening_tag(apdu, tag_number);
|
||||
apdu_len += len;
|
||||
len = bacapp_encode_timestamp(&apdu[apdu_len], value);
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = bacapp_encode_timestamp(apdu, value);
|
||||
apdu_len += len;
|
||||
len = encode_closing_tag(&apdu[apdu_len], tag_number);
|
||||
if (apdu) {
|
||||
apdu += len;
|
||||
}
|
||||
len = encode_closing_tag(apdu, tag_number);
|
||||
apdu_len += len;
|
||||
}
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/** Decode a time stamp from the given buffer.
|
||||
*
|
||||
/**
|
||||
* @brief Decode a time stamp from the given buffer.
|
||||
* @param apdu Pointer to the APDU buffer.
|
||||
* @param apdu_size - the APDU buffer length
|
||||
* @param value Pointer to the variable that shall
|
||||
* take the time stamp values.
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR if an error occurs
|
||||
*/
|
||||
int bacnet_timestamp_decode(
|
||||
uint8_t *apdu, uint32_t apdu_size, BACNET_TIMESTAMP *value)
|
||||
{
|
||||
int len = 0;
|
||||
int apdu_len = 0;
|
||||
BACNET_TAG tag = { 0 };
|
||||
BACNET_UNSIGNED_INTEGER unsigned_value = 0;
|
||||
BACNET_TIME *btime = NULL;
|
||||
BACNET_DATE_TIME *bdatetime = NULL;
|
||||
|
||||
if (!apdu) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len = bacnet_tag_decode(&apdu[apdu_len], apdu_size - apdu_len, &tag);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (value) {
|
||||
value->tag = tag.number;
|
||||
}
|
||||
switch (tag.number) {
|
||||
case TIME_STAMP_TIME:
|
||||
if (value) {
|
||||
btime = &value->value.time;
|
||||
}
|
||||
len = bacnet_time_context_decode(&apdu[apdu_len],
|
||||
apdu_size - apdu_len, tag.number, btime);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
break;
|
||||
|
||||
case TIME_STAMP_SEQUENCE:
|
||||
len = bacnet_unsigned_context_decode(&apdu[apdu_len],
|
||||
apdu_size - apdu_len, tag.number, &unsigned_value);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
if (unsigned_value <= UINT16_MAX) {
|
||||
if (value) {
|
||||
value->value.sequenceNum = (uint16_t)unsigned_value;
|
||||
}
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
break;
|
||||
case TIME_STAMP_DATETIME:
|
||||
if (value) {
|
||||
bdatetime = &value->value.dateTime;
|
||||
}
|
||||
len = bacnet_datetime_context_decode(&apdu[apdu_len],
|
||||
apdu_size - apdu_len, tag.number, bdatetime);
|
||||
if (len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
break;
|
||||
default:
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decode a time stamp from the given buffer.
|
||||
* @param apdu Pointer to the APDU buffer.
|
||||
* @param value Pointer to the variable that shall
|
||||
* take the time stamp values.
|
||||
*
|
||||
* @return Bytes decoded or -1 on error.
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR if an error occurs
|
||||
* @deprecated use bacnet_timestamp_decode() instead
|
||||
*/
|
||||
int bacapp_decode_timestamp(uint8_t *apdu, BACNET_TIMESTAMP *value)
|
||||
{
|
||||
int len = 0;
|
||||
int section_len;
|
||||
uint32_t len_value_type;
|
||||
BACNET_UNSIGNED_INTEGER unsigned_value;
|
||||
|
||||
if (apdu && value) {
|
||||
section_len = decode_tag_number_and_value(
|
||||
&apdu[len], &value->tag, &len_value_type);
|
||||
|
||||
if (-1 == section_len) {
|
||||
return -1;
|
||||
}
|
||||
switch (value->tag) {
|
||||
case TIME_STAMP_TIME:
|
||||
if ((section_len = decode_context_bacnet_time(&apdu[len],
|
||||
TIME_STAMP_TIME, &value->value.time)) == -1) {
|
||||
return -1;
|
||||
} else {
|
||||
len += section_len;
|
||||
}
|
||||
break;
|
||||
|
||||
case TIME_STAMP_SEQUENCE:
|
||||
if ((section_len = decode_context_unsigned(&apdu[len],
|
||||
TIME_STAMP_SEQUENCE, &unsigned_value)) == -1) {
|
||||
return -1;
|
||||
} else {
|
||||
if (unsigned_value <= 0xffff) {
|
||||
len += section_len;
|
||||
value->value.sequenceNum = (uint16_t)unsigned_value;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case TIME_STAMP_DATETIME:
|
||||
if ((section_len = bacapp_decode_context_datetime(&apdu[len],
|
||||
TIME_STAMP_DATETIME, &value->value.dateTime)) == -1) {
|
||||
return -1;
|
||||
} else {
|
||||
len += section_len;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return len;
|
||||
return bacnet_timestamp_decode(apdu, MAX_APDU, value);
|
||||
}
|
||||
|
||||
/** Decode a time stamp and check for
|
||||
* opening and closing tags.
|
||||
*
|
||||
/**
|
||||
* @brief Decode a time stamp and check for opening and closing tags.
|
||||
* @param apdu Pointer to the APDU buffer.
|
||||
* @param apdu_size - the APDU buffer length
|
||||
* @param tag_number The tag number that shall
|
||||
* hold the time stamp.
|
||||
* @param value Pointer to the variable that shall
|
||||
* take the time stamp values.
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR if an error occurs
|
||||
*/
|
||||
int bacnet_timestamp_context_decode(uint8_t *apdu,
|
||||
uint32_t apdu_size,
|
||||
uint8_t tag_number,
|
||||
BACNET_TIMESTAMP *value)
|
||||
{
|
||||
int len = 0;
|
||||
int apdu_len = 0;
|
||||
|
||||
if (!bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
len = bacnet_timestamp_decode(&apdu[apdu_len], apdu_size - apdu_len, value);
|
||||
if (len < 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
if (!bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decode a time stamp and check for opening and closing tags.
|
||||
* @param apdu Pointer to the APDU buffer.
|
||||
* @param tag_number The tag number that shall
|
||||
* hold the time stamp.
|
||||
* @param value Pointer to the variable that shall
|
||||
* take the time stamp values.
|
||||
*
|
||||
* @return Bytes decoded or -1 on error.
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR if an error occurs
|
||||
* @deprecated use bacnet_timestamp_context_decode() instead
|
||||
*/
|
||||
int bacapp_decode_context_timestamp(
|
||||
uint8_t *apdu, uint8_t tag_number, BACNET_TIMESTAMP *value)
|
||||
{
|
||||
int len = 0;
|
||||
int section_len;
|
||||
const uint32_t apdu_size = MAX_APDU;
|
||||
int len;
|
||||
|
||||
if (decode_is_opening_tag_number(&apdu[len], tag_number)) {
|
||||
len++;
|
||||
section_len = bacapp_decode_timestamp(&apdu[len], value);
|
||||
if (section_len > 0) {
|
||||
len += section_len;
|
||||
if (decode_is_closing_tag_number(&apdu[len], tag_number)) {
|
||||
len++;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
len = bacnet_timestamp_context_decode(apdu, apdu_size, tag_number, value);
|
||||
if (len <= 0) {
|
||||
len = BACNET_STATUS_ERROR;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
@@ -334,9 +393,9 @@ bool bacapp_timestamp_init_ascii(BACNET_TIMESTAMP *timestamp, const char *ascii)
|
||||
}
|
||||
}
|
||||
if (!status) {
|
||||
count = sscanf(ascii, "%4d", &sequence);
|
||||
count = sscanf(ascii, "%5d", &sequence);
|
||||
if (count == 1) {
|
||||
timestamp->tag = TIME_STAMP_DATETIME;
|
||||
timestamp->tag = TIME_STAMP_SEQUENCE;
|
||||
timestamp->value.sequenceNum = (uint16_t)sequence;
|
||||
status = true;
|
||||
}
|
||||
|
||||
+20
-6
@@ -25,6 +25,7 @@
|
||||
#define _TIMESTAMP_H_
|
||||
#include <stdint.h>
|
||||
#include "bacnet/bacnet_stack_exports.h"
|
||||
#include "bacnet/basic/sys/platform.h"
|
||||
#include "bacnet/bacenum.h"
|
||||
#include "bacnet/bacdcode.h"
|
||||
|
||||
@@ -73,17 +74,30 @@ extern "C" {
|
||||
int bacapp_encode_timestamp(
|
||||
uint8_t * apdu,
|
||||
BACNET_TIMESTAMP * value);
|
||||
BACNET_STACK_EXPORT
|
||||
int bacapp_decode_timestamp(
|
||||
uint8_t * apdu,
|
||||
BACNET_TIMESTAMP * value);
|
||||
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
int bacapp_encode_context_timestamp(
|
||||
uint8_t * apdu,
|
||||
uint8_t tag_number,
|
||||
BACNET_TIMESTAMP * value);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
int bacnet_timestamp_decode(
|
||||
uint8_t * apdu,
|
||||
uint32_t apdu_size,
|
||||
BACNET_TIMESTAMP * value);
|
||||
BACNET_STACK_EXPORT
|
||||
int bacnet_timestamp_context_decode(
|
||||
uint8_t * apdu,
|
||||
uint32_t apdu_size,
|
||||
uint8_t tag_number,
|
||||
BACNET_TIMESTAMP * value);
|
||||
|
||||
BACNET_STACK_DEPRECATED("Use bacnet_timestamp_decode() instead")
|
||||
BACNET_STACK_EXPORT
|
||||
int bacapp_decode_timestamp(
|
||||
uint8_t * apdu,
|
||||
BACNET_TIMESTAMP * value);
|
||||
BACNET_STACK_DEPRECATED("Use bacnet_timestamp_context_decode() instead")
|
||||
BACNET_STACK_EXPORT
|
||||
int bacapp_decode_context_timestamp(
|
||||
uint8_t * apdu,
|
||||
|
||||
+61
-25
@@ -37,6 +37,13 @@ License.
|
||||
#include "bacnet/bacdcode.h"
|
||||
#include "bacapp.h"
|
||||
|
||||
/**
|
||||
* @brief decode a BACnetWeeklySchedule
|
||||
* @param apdu - buffer of data to be decoded
|
||||
* @param max_apdu_len - number of bytes in the buffer
|
||||
* @param value - stores the decoded property value
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR if errors occur
|
||||
*/
|
||||
int bacnet_weeklyschedule_decode(
|
||||
uint8_t *apdu, int max_apdu_len, BACNET_WEEKLY_SCHEDULE *value)
|
||||
{
|
||||
@@ -44,6 +51,12 @@ int bacnet_weeklyschedule_decode(
|
||||
int apdu_len = 0;
|
||||
int wi;
|
||||
|
||||
if (!apdu) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (max_apdu_len <= 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
value->singleDay = false;
|
||||
for (wi = 0; wi < 7; wi++) {
|
||||
len = bacnet_dailyschedule_decode(&apdu[apdu_len],
|
||||
@@ -53,13 +66,24 @@ int bacnet_weeklyschedule_decode(
|
||||
value->singleDay = true;
|
||||
break;
|
||||
}
|
||||
return -1;
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode the Weekly_Schedule property
|
||||
* from clause 12 Schedule Object Type
|
||||
* BACnetARRAY[7] of BACnetDailySchedule
|
||||
*
|
||||
* @param apdu - buffer of data to be encoded, or NULL for length
|
||||
* @param tag_number - context tag number to be encoded
|
||||
* @param value - value to be encoded
|
||||
* @return the number of apdu bytes encoded, or BACNET_STATUS_ERROR
|
||||
*/
|
||||
int bacnet_weeklyschedule_encode(uint8_t *apdu, BACNET_WEEKLY_SCHEDULE *value)
|
||||
{
|
||||
int apdu_len = 0;
|
||||
@@ -74,16 +98,18 @@ int bacnet_weeklyschedule_encode(uint8_t *apdu, BACNET_WEEKLY_SCHEDULE *value)
|
||||
}
|
||||
len = bacnet_dailyschedule_encode(
|
||||
apdu_offset, &value->weeklySchedule[wi]);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
if (len < 0) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Encode a context tagged WeeklySchedule complex data type
|
||||
* @param apdu - the APDU buffer
|
||||
* @brief Encode a context tagged Weekly_Schedule complex data type
|
||||
* @param apdu - the APDU buffer, or NULL for buffer length
|
||||
* @param tag_number - the APDU buffer size
|
||||
* @param value - WeeklySchedule structure
|
||||
* @return length of the APDU buffer, or 0 if not able to encode
|
||||
@@ -103,6 +129,9 @@ int bacnet_weeklyschedule_context_encode(
|
||||
apdu_offset = &apdu[apdu_len];
|
||||
}
|
||||
len = bacnet_weeklyschedule_encode(apdu_offset, value);
|
||||
if (len == BACNET_STATUS_ERROR) {
|
||||
return 0;
|
||||
}
|
||||
apdu_len += len;
|
||||
if (apdu) {
|
||||
apdu_offset = &apdu[apdu_len];
|
||||
@@ -114,6 +143,14 @@ int bacnet_weeklyschedule_context_encode(
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief decode a context encoded Weekly_Schedule property
|
||||
* @param apdu - buffer of data to be decoded
|
||||
* @param max_apdu_len - number of bytes in the buffer
|
||||
* @param tag_number - context tag number to match
|
||||
* @param value - stores the decoded property value
|
||||
* @return number of bytes decoded, or BACNET_STATUS_ERROR if an error occurs
|
||||
*/
|
||||
int bacnet_weeklyschedule_context_decode(uint8_t *apdu,
|
||||
int max_apdu_len,
|
||||
uint8_t tag_number,
|
||||
@@ -122,34 +159,33 @@ int bacnet_weeklyschedule_context_decode(uint8_t *apdu,
|
||||
int apdu_len = 0;
|
||||
int len;
|
||||
|
||||
if ((max_apdu_len - apdu_len) >= 1 &&
|
||||
decode_is_opening_tag_number(&apdu[apdu_len], tag_number)) {
|
||||
apdu_len += 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (-1 ==
|
||||
(len = bacnet_weeklyschedule_decode(
|
||||
&apdu[apdu_len], max_apdu_len - apdu_len, value))) {
|
||||
return -1;
|
||||
} else {
|
||||
if (bacnet_is_opening_tag_number(
|
||||
&apdu[apdu_len], max_apdu_len - apdu_len, tag_number, &len)) {
|
||||
apdu_len += len;
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
len = bacnet_weeklyschedule_decode(
|
||||
&apdu[apdu_len], max_apdu_len - apdu_len, value);
|
||||
if (len > 0) {
|
||||
apdu_len += len;
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
if (bacnet_is_closing_tag_number(
|
||||
&apdu[apdu_len], max_apdu_len - apdu_len, tag_number, &len)) {
|
||||
apdu_len += len;
|
||||
} else {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
|
||||
if ((max_apdu_len - apdu_len) >= 1 &&
|
||||
decode_is_closing_tag_number(&apdu[apdu_len], tag_number)) {
|
||||
apdu_len += 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Compare the BACnetWeeklySchedule complex data
|
||||
* @param value1 - BACNET_COLOR_COMMAND structure
|
||||
* @param value2 - BACNET_COLOR_COMMAND structure
|
||||
* @param value1 - BACNET_WEEKLY_SCHEDULE structure
|
||||
* @param value2 - BACNET_WEEKLY_SCHEDULE structure
|
||||
* @return true if the same
|
||||
*/
|
||||
bool bacnet_weeklyschedule_same(
|
||||
|
||||
Reference in New Issue
Block a user