Merged revision(s) 3031 from branches/releases/bacnet-stack-0-8-0:
Added checks for OPTIONAL context tagged decoding to be sure it is not a closing tag. Added makefile for unit test in bacdevobjpropref.c module, and improved unit test. https://sourceforge.net/p/bacnet/bugs/49/ ........
This commit is contained in:
@@ -60,6 +60,16 @@ int bacapp_encode_context_device_obj_property_ref(
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* BACnetDeviceObjectPropertyReference ::= SEQUENCE {
|
||||
object-identifier [0] BACnetObjectIdentifier,
|
||||
property-identifier [1] BACnetPropertyIdentifier,
|
||||
property-array-index [2] Unsigned OPTIONAL,
|
||||
-- used only with array datatype
|
||||
-- if omitted with an array then
|
||||
-- the entire array is referenced
|
||||
device-identifier [3] BACnetObjectIdentifier OPTIONAL
|
||||
}
|
||||
*/
|
||||
int bacapp_encode_device_obj_property_ref(
|
||||
uint8_t * apdu,
|
||||
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE * value)
|
||||
@@ -67,26 +77,26 @@ int bacapp_encode_device_obj_property_ref(
|
||||
int len;
|
||||
int apdu_len = 0;
|
||||
|
||||
/* object-identifier [0] BACnetObjectIdentifier */
|
||||
len =
|
||||
encode_context_object_id(&apdu[apdu_len], 0,
|
||||
(int) value->objectIdentifier.type, value->objectIdentifier.instance);
|
||||
apdu_len += len;
|
||||
|
||||
/* property-identifier [1] BACnetPropertyIdentifier */
|
||||
len =
|
||||
encode_context_enumerated(&apdu[apdu_len], 1,
|
||||
value->propertyIdentifier);
|
||||
apdu_len += len;
|
||||
|
||||
/* Array index is optional so check if needed before inserting */
|
||||
/* property-array-index [2] Unsigned OPTIONAL */
|
||||
/* Check if needed before inserting */
|
||||
if (value->arrayIndex != BACNET_ARRAY_ALL) {
|
||||
len = encode_context_unsigned(&apdu[apdu_len], 2, value->arrayIndex);
|
||||
apdu_len += len;
|
||||
}
|
||||
|
||||
/* device-identifier [3] BACnetObjectIdentifier OPTIONAL */
|
||||
/* Likewise, device id is optional so see if needed
|
||||
* (set type to BACNET_NO_DEV_TYPE or something other than OBJECT_DEVICE to
|
||||
* omit */
|
||||
|
||||
if (value->deviceIndentifier.type == OBJECT_DEVICE) {
|
||||
len =
|
||||
encode_context_object_id(&apdu[apdu_len], 3,
|
||||
@@ -97,6 +107,16 @@ int bacapp_encode_device_obj_property_ref(
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* BACnetDeviceObjectPropertyReference ::= SEQUENCE {
|
||||
object-identifier [0] BACnetObjectIdentifier,
|
||||
property-identifier [1] BACnetPropertyIdentifier,
|
||||
property-array-index [2] Unsigned OPTIONAL,
|
||||
-- used only with array datatype
|
||||
-- if omitted with an array then
|
||||
-- the entire array is referenced
|
||||
device-identifier [3] BACnetObjectIdentifier OPTIONAL
|
||||
}
|
||||
*/
|
||||
int bacapp_decode_device_obj_property_ref(
|
||||
uint8_t * apdu,
|
||||
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE * value)
|
||||
@@ -104,6 +124,8 @@ int bacapp_decode_device_obj_property_ref(
|
||||
int len;
|
||||
int apdu_len = 0;
|
||||
uint32_t enumValue;
|
||||
|
||||
/* object-identifier [0] BACnetObjectIdentifier */
|
||||
if (-1 == (len =
|
||||
decode_context_object_id(&apdu[apdu_len], 0,
|
||||
&value->objectIdentifier.type,
|
||||
@@ -111,15 +133,16 @@ int bacapp_decode_device_obj_property_ref(
|
||||
return -1;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
/* property-identifier [1] BACnetPropertyIdentifier */
|
||||
if (-1 == (len =
|
||||
decode_context_enumerated(&apdu[apdu_len], 1, &enumValue))) {
|
||||
return -1;
|
||||
}
|
||||
value->propertyIdentifier = (BACNET_PROPERTY_ID) enumValue;
|
||||
apdu_len += len;
|
||||
|
||||
if (decode_is_context_tag(&apdu[apdu_len], 2)) {
|
||||
/* property-array-index [2] Unsigned OPTIONAL */
|
||||
if (decode_is_context_tag(&apdu[apdu_len], 2) &&
|
||||
!decode_is_closing_tag(&apdu[apdu_len])) {
|
||||
if (-1 == (len =
|
||||
decode_context_unsigned(&apdu[apdu_len], 2,
|
||||
&value->arrayIndex))) {
|
||||
@@ -129,8 +152,9 @@ int bacapp_decode_device_obj_property_ref(
|
||||
} else {
|
||||
value->arrayIndex = BACNET_ARRAY_ALL;
|
||||
}
|
||||
|
||||
if (decode_is_context_tag(&apdu[apdu_len], 3)) {
|
||||
/* device-identifier [3] BACnetObjectIdentifier OPTIONAL */
|
||||
if (decode_is_context_tag(&apdu[apdu_len], 3) &&
|
||||
!decode_is_closing_tag(&apdu[apdu_len])) {
|
||||
if (-1 == (len =
|
||||
decode_context_object_id(&apdu[apdu_len], 3,
|
||||
&value->deviceIndentifier.type,
|
||||
@@ -176,7 +200,10 @@ int bacapp_decode_context_device_obj_property_ref(
|
||||
}
|
||||
|
||||
|
||||
/* Functions for BACnetDeviceObjectReference: */
|
||||
/* BACnetDeviceObjectReference ::= SEQUENCE {
|
||||
device-identifier [0] BACnetObjectIdentifier OPTIONAL,
|
||||
object-identifier [1] BACnetObjectIdentifier
|
||||
}*/
|
||||
int bacapp_encode_context_device_obj_ref(
|
||||
uint8_t * apdu,
|
||||
uint8_t tag_number,
|
||||
@@ -197,6 +224,10 @@ int bacapp_encode_context_device_obj_ref(
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* BACnetDeviceObjectReference ::= SEQUENCE {
|
||||
device-identifier [0] BACnetObjectIdentifier OPTIONAL,
|
||||
object-identifier [1] BACnetObjectIdentifier
|
||||
}*/
|
||||
int bacapp_encode_device_obj_ref(
|
||||
uint8_t * apdu,
|
||||
BACNET_DEVICE_OBJECT_REFERENCE * value)
|
||||
@@ -204,10 +235,10 @@ int bacapp_encode_device_obj_ref(
|
||||
int len;
|
||||
int apdu_len = 0;
|
||||
|
||||
/* device-identifier [0] BACnetObjectIdentifier OPTIONAL */
|
||||
/* Device id is optional so see if needed
|
||||
* (set type to BACNET_NO_DEV_TYPE or something other than OBJECT_DEVICE to
|
||||
* omit */
|
||||
|
||||
if (value->deviceIndentifier.type == OBJECT_DEVICE) {
|
||||
len =
|
||||
encode_context_object_id(&apdu[apdu_len], 0,
|
||||
@@ -215,7 +246,7 @@ int bacapp_encode_device_obj_ref(
|
||||
value->deviceIndentifier.instance);
|
||||
apdu_len += len;
|
||||
}
|
||||
|
||||
/* object-identifier [1] BACnetObjectIdentifier */
|
||||
len =
|
||||
encode_context_object_id(&apdu[apdu_len], 1,
|
||||
(int) value->objectIdentifier.type, value->objectIdentifier.instance);
|
||||
@@ -224,6 +255,10 @@ int bacapp_encode_device_obj_ref(
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* BACnetDeviceObjectReference ::= SEQUENCE {
|
||||
device-identifier [0] BACnetObjectIdentifier OPTIONAL,
|
||||
object-identifier [1] BACnetObjectIdentifier
|
||||
}*/
|
||||
int bacapp_decode_device_obj_ref(
|
||||
uint8_t * apdu,
|
||||
BACNET_DEVICE_OBJECT_REFERENCE * value)
|
||||
@@ -231,8 +266,9 @@ int bacapp_decode_device_obj_ref(
|
||||
int len;
|
||||
int apdu_len = 0;
|
||||
|
||||
/* Device ID is optional */
|
||||
if (decode_is_context_tag(&apdu[apdu_len], 0)) {
|
||||
/* device-identifier [0] BACnetObjectIdentifier OPTIONAL */
|
||||
if (decode_is_context_tag(&apdu[apdu_len], 0) &&
|
||||
!decode_is_closing_tag(&apdu[apdu_len])) {
|
||||
if (-1 == (len =
|
||||
decode_context_object_id(&apdu[apdu_len], 0,
|
||||
&value->deviceIndentifier.type,
|
||||
@@ -244,7 +280,7 @@ int bacapp_decode_device_obj_ref(
|
||||
value->deviceIndentifier.type = BACNET_NO_DEV_TYPE;
|
||||
value->deviceIndentifier.instance = BACNET_NO_DEV_ID;
|
||||
}
|
||||
|
||||
/* object-identifier [1] BACnetObjectIdentifier */
|
||||
if (-1 == (len =
|
||||
decode_context_object_id(&apdu[apdu_len], 1,
|
||||
&value->objectIdentifier.type,
|
||||
@@ -284,51 +320,89 @@ int bacapp_decode_context_device_obj_ref(
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
#ifdef TEST
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "ctest.h"
|
||||
static void testDevObjPropRef(
|
||||
Test * pTest,
|
||||
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE *inData)
|
||||
{
|
||||
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE outData;
|
||||
uint8_t buffer[MAX_APDU] = {0};
|
||||
int inLen = 0;
|
||||
int outLen = 0;
|
||||
|
||||
void testDevIdPropRef(
|
||||
/* encode */
|
||||
inLen = bacapp_encode_device_obj_property_ref(buffer, inData);
|
||||
/* add a closing tag at the end of the buffer to verify proper handling
|
||||
when that is encountered in real packets */
|
||||
encode_closing_tag(&buffer[inLen], 3);
|
||||
/* decode */
|
||||
outLen = bacapp_decode_device_obj_property_ref(buffer, &outData);
|
||||
ct_test(pTest, outLen == inLen);
|
||||
ct_test(pTest,
|
||||
inData->objectIdentifier.instance == outData.objectIdentifier.instance);
|
||||
ct_test(pTest,
|
||||
inData->objectIdentifier.type == outData.objectIdentifier.type);
|
||||
ct_test(pTest, inData->propertyIdentifier == outData.propertyIdentifier);
|
||||
if (inData->arrayIndex != BACNET_ARRAY_ALL) {
|
||||
ct_test(pTest, inData->arrayIndex == outData.arrayIndex);
|
||||
} else {
|
||||
ct_test(pTest, outData.arrayIndex == BACNET_ARRAY_ALL);
|
||||
}
|
||||
if (inData->deviceIndentifier.type == OBJECT_DEVICE) {
|
||||
ct_test(pTest,
|
||||
inData->deviceIndentifier.instance ==
|
||||
outData.deviceIndentifier.instance);
|
||||
ct_test(pTest,
|
||||
inData->deviceIndentifier.type == outData.deviceIndentifier.type);
|
||||
} else {
|
||||
ct_test(pTest,outData.deviceIndentifier.instance == BACNET_NO_DEV_ID);
|
||||
ct_test(pTest,outData.deviceIndentifier.type == BACNET_NO_DEV_TYPE);
|
||||
}
|
||||
}
|
||||
|
||||
static void testDevIdPropRef(
|
||||
Test * pTest)
|
||||
{
|
||||
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE inData;
|
||||
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE outData;
|
||||
uint8_t buffer[MAX_APDU];
|
||||
int inLen;
|
||||
int outLen;
|
||||
|
||||
|
||||
/* everything encoded */
|
||||
inData.objectIdentifier.instance = 0x1234;
|
||||
inData.objectIdentifier.type = 15;
|
||||
|
||||
inData.propertyIdentifier = 25;
|
||||
|
||||
inData.arrayIndex = 0x5678;
|
||||
|
||||
inData.deviceIndentifier.instance = 0x4343;
|
||||
inData.deviceIndentifier.type = 28;
|
||||
|
||||
inLen = bacapp_encode_device_obj_property_ref(buffer, &inData);
|
||||
outLen = bacapp_decode_device_obj_property_ref(buffer, &outData);
|
||||
|
||||
ct_test(pTest, outLen == inLen);
|
||||
|
||||
ct_test(pTest,
|
||||
inData.objectIdentifier.instance == outData.objectIdentifier.instance);
|
||||
ct_test(pTest,
|
||||
inData.objectIdentifier.type == outData.objectIdentifier.type);
|
||||
|
||||
ct_test(pTest, inData.propertyIdentifier == outData.propertyIdentifier);
|
||||
|
||||
ct_test(pTest, inData.arrayIndex == outData.arrayIndex);
|
||||
|
||||
ct_test(pTest,
|
||||
inData.deviceIndentifier.instance ==
|
||||
outData.deviceIndentifier.instance);
|
||||
ct_test(pTest,
|
||||
inData.deviceIndentifier.type == outData.deviceIndentifier.type);
|
||||
inData.deviceIndentifier.type = OBJECT_DEVICE;
|
||||
testDevObjPropRef(pTest, &inData);
|
||||
/* optional array */
|
||||
inData.objectIdentifier.instance = 0x1234;
|
||||
inData.objectIdentifier.type = 15;
|
||||
inData.propertyIdentifier = 25;
|
||||
inData.arrayIndex = BACNET_ARRAY_ALL;
|
||||
inData.deviceIndentifier.instance = 0x4343;
|
||||
inData.deviceIndentifier.type = OBJECT_DEVICE;
|
||||
testDevObjPropRef(pTest, &inData);
|
||||
/* optional device ID */
|
||||
inData.objectIdentifier.instance = 0x1234;
|
||||
inData.objectIdentifier.type = 15;
|
||||
inData.propertyIdentifier = 25;
|
||||
inData.arrayIndex = 1;
|
||||
inData.deviceIndentifier.instance = 0;
|
||||
inData.deviceIndentifier.type = BACNET_NO_DEV_TYPE;
|
||||
testDevObjPropRef(pTest, &inData);
|
||||
/* optional array + optional device ID */
|
||||
inData.objectIdentifier.instance = 0x1234;
|
||||
inData.objectIdentifier.type = 15;
|
||||
inData.propertyIdentifier = 25;
|
||||
inData.arrayIndex = BACNET_ARRAY_ALL;
|
||||
inData.deviceIndentifier.instance = 0;
|
||||
inData.deviceIndentifier.type = BACNET_NO_DEV_TYPE;
|
||||
testDevObjPropRef(pTest, &inData);
|
||||
}
|
||||
|
||||
void testDevIdRef(
|
||||
static void testDevIdRef(
|
||||
Test * pTest)
|
||||
{
|
||||
BACNET_DEVICE_OBJECT_REFERENCE inData;
|
||||
@@ -338,13 +412,10 @@ void testDevIdRef(
|
||||
int outLen;
|
||||
|
||||
inData.deviceIndentifier.instance = 0x4343;
|
||||
inData.deviceIndentifier.type = 28;
|
||||
|
||||
inData.deviceIndentifier.type = OBJECT_DEVICE;
|
||||
inLen = bacapp_encode_device_obj_ref(buffer, &inData);
|
||||
outLen = bacapp_decode_device_obj_ref(buffer, &outData);
|
||||
|
||||
ct_test(pTest, outLen == inLen);
|
||||
|
||||
ct_test(pTest,
|
||||
inData.deviceIndentifier.instance ==
|
||||
outData.deviceIndentifier.instance);
|
||||
@@ -352,6 +423,17 @@ void testDevIdRef(
|
||||
inData.deviceIndentifier.type == outData.deviceIndentifier.type);
|
||||
}
|
||||
|
||||
void testBACnetDeviceObjectPropertyReference(
|
||||
Test * pTest)
|
||||
{
|
||||
bool rc;
|
||||
|
||||
/* individual tests */
|
||||
rc = ct_addTestFunction(pTest, testDevIdPropRef);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, testDevIdRef);
|
||||
assert(rc);
|
||||
}
|
||||
|
||||
#ifdef TEST_DEV_ID_PROP_REF
|
||||
#include <assert.h>
|
||||
@@ -359,14 +441,9 @@ int main(
|
||||
void)
|
||||
{
|
||||
Test *pTest;
|
||||
bool rc;
|
||||
|
||||
pTest = ct_create("BACnet Prop Ref", NULL);
|
||||
/* individual tests */
|
||||
rc = ct_addTestFunction(pTest, testDevIdPropRef);
|
||||
assert(rc);
|
||||
rc = ct_addTestFunction(pTest, testDevIdRef);
|
||||
assert(rc);
|
||||
testBACnetDeviceObjectPropertyReference(pTest);
|
||||
|
||||
ct_setStream(pTest, stdout);
|
||||
ct_run(pTest);
|
||||
|
||||
Reference in New Issue
Block a user