Feature/comments and review (#102)
* Added comments. Replaced -1 with BACNET_STATUS_ERROR macro. * Added some DogyGen comments * Added missing return value check for booleans. * Check used bits against unused bits in bitstrings. * Added missing return value check for bitstrings. * Added sanity checks and comments. * Fixed implicit type conv warning. * Chose to keep simpler C function methods. Co-authored-by: Steve Karg <steve@kargs.net> Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
+182
-84
@@ -41,6 +41,18 @@
|
||||
/** @file bacdevobjpropref.c BACnet Application Device Object (Property)
|
||||
* Reference */
|
||||
|
||||
/**
|
||||
* Encode a property reference for the device object.
|
||||
* Add an opening tag, encode the property and finally
|
||||
* add a closing tag as well.
|
||||
*
|
||||
* @param apdu Pointer to the buffer to encode to.
|
||||
* @param tag_number Tag number.
|
||||
* @param value Pointer to the object property reference,
|
||||
* used for encoding.
|
||||
*
|
||||
* @return Bytes encoded or 0 on failure.
|
||||
*/
|
||||
int bacapp_encode_context_device_obj_property_ref(uint8_t *apdu,
|
||||
uint8_t tag_number,
|
||||
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE *value)
|
||||
@@ -48,28 +60,39 @@ int bacapp_encode_context_device_obj_property_ref(uint8_t *apdu,
|
||||
int len;
|
||||
int apdu_len = 0;
|
||||
|
||||
len = encode_opening_tag(&apdu[apdu_len], tag_number);
|
||||
apdu_len += len;
|
||||
if (value) {
|
||||
len = encode_opening_tag(&apdu[apdu_len], tag_number);
|
||||
apdu_len += len;
|
||||
|
||||
len = bacapp_encode_device_obj_property_ref(&apdu[apdu_len], value);
|
||||
apdu_len += len;
|
||||
len = bacapp_encode_device_obj_property_ref(&apdu[apdu_len], value);
|
||||
apdu_len += len;
|
||||
|
||||
len = encode_closing_tag(&apdu[apdu_len], tag_number);
|
||||
apdu_len += len;
|
||||
len = encode_closing_tag(&apdu[apdu_len], tag_number);
|
||||
apdu_len += len;
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
*/
|
||||
/**
|
||||
* Encode a property reference for the device object.
|
||||
*
|
||||
* 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
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer to encode to.
|
||||
* @param value Pointer to the object property reference,
|
||||
* used for encoding.
|
||||
*
|
||||
* @return Bytes encoded.
|
||||
*/
|
||||
int bacapp_encode_device_obj_property_ref(
|
||||
uint8_t *apdu, BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE *value)
|
||||
{
|
||||
@@ -102,45 +125,56 @@ 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
|
||||
}
|
||||
*/
|
||||
/**
|
||||
* Decode a property reference of a device object.
|
||||
*
|
||||
* 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
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer containing the data to decode.
|
||||
* @param value Pointer to the object property reference,
|
||||
* used to decode the data into.
|
||||
*
|
||||
* @return Bytes decoded or BACNET_STATUS_ERROR on failure.
|
||||
*/
|
||||
int bacapp_decode_device_obj_property_ref(
|
||||
uint8_t *apdu, BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE *value)
|
||||
{
|
||||
int len;
|
||||
int apdu_len = 0;
|
||||
uint32_t enumValue;
|
||||
uint32_t enumValue = 0;
|
||||
|
||||
/* object-identifier [0] BACnetObjectIdentifier */
|
||||
if (-1 ==
|
||||
(len = decode_context_object_id(&apdu[apdu_len], 0,
|
||||
&value->objectIdentifier.type,
|
||||
&value->objectIdentifier.instance))) {
|
||||
return -1;
|
||||
len =
|
||||
decode_context_object_id(&apdu[apdu_len], 0,
|
||||
&value->objectIdentifier.type,
|
||||
&value->objectIdentifier.instance);
|
||||
if (len == BACNET_STATUS_ERROR) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
/* property-identifier [1] BACnetPropertyIdentifier */
|
||||
if (-1 ==
|
||||
(len = decode_context_enumerated(&apdu[apdu_len], 1, &enumValue))) {
|
||||
return -1;
|
||||
len =
|
||||
decode_context_enumerated(&apdu[apdu_len], 1, &enumValue);
|
||||
if (len == BACNET_STATUS_ERROR) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
value->propertyIdentifier = (BACNET_PROPERTY_ID)enumValue;
|
||||
apdu_len += len;
|
||||
/* 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))) {
|
||||
return -1;
|
||||
len = decode_context_unsigned(
|
||||
&apdu[apdu_len], 2, &value->arrayIndex);
|
||||
if (len == BACNET_STATUS_ERROR) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
} else {
|
||||
@@ -164,7 +198,19 @@ int bacapp_decode_device_obj_property_ref(
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
int bacapp_decode_context_device_obj_property_ref(uint8_t *apdu,
|
||||
/**
|
||||
* Decode the opening tag and the property reference of
|
||||
* an device object and check for the closing tag as well.
|
||||
*
|
||||
* @param apdu Pointer to the buffer containing the data to decode.
|
||||
* @param tag_number Tag number
|
||||
* @param value Pointer to the object property reference,
|
||||
* used to decode the data into.
|
||||
*
|
||||
* @return Bytes decoded or BACNET_STATUS_ERROR on failure.
|
||||
*/
|
||||
int bacapp_decode_context_device_obj_property_ref(
|
||||
uint8_t *apdu,
|
||||
uint8_t tag_number,
|
||||
BACNET_DEVICE_OBJECT_PROPERTY_REFERENCE *value)
|
||||
{
|
||||
@@ -175,51 +221,77 @@ int bacapp_decode_context_device_obj_property_ref(uint8_t *apdu,
|
||||
len++;
|
||||
section_length =
|
||||
bacapp_decode_device_obj_property_ref(&apdu[len], value);
|
||||
|
||||
if (section_length == -1) {
|
||||
len = -1;
|
||||
if (section_length == BACNET_STATUS_ERROR) {
|
||||
len = BACNET_STATUS_ERROR;
|
||||
} else {
|
||||
len += section_length;
|
||||
if (decode_is_closing_tag_number(&apdu[len], tag_number)) {
|
||||
len++;
|
||||
} else {
|
||||
len = -1;
|
||||
len = BACNET_STATUS_ERROR;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
len = -1;
|
||||
len = BACNET_STATUS_ERROR;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/* BACnetDeviceObjectReference ::= SEQUENCE {
|
||||
device-identifier [0] BACnetObjectIdentifier OPTIONAL,
|
||||
object-identifier [1] BACnetObjectIdentifier
|
||||
}*/
|
||||
/**
|
||||
* Encode the opening tag and the device object reference
|
||||
* and finally for the closing tag as well.
|
||||
*
|
||||
* BACnetDeviceObjectReference ::= SEQUENCE {
|
||||
* device-identifier [0] BACnetObjectIdentifier OPTIONAL,
|
||||
* object-identifier [1] BACnetObjectIdentifier
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer used for encoding.
|
||||
* @param tag_number Tag number
|
||||
* @param value Pointer to the device object reference,
|
||||
* used to encode.
|
||||
*
|
||||
* @return Bytes encoded or 0 on failure.
|
||||
*/
|
||||
int bacapp_encode_context_device_obj_ref(
|
||||
uint8_t *apdu, uint8_t tag_number, BACNET_DEVICE_OBJECT_REFERENCE *value)
|
||||
uint8_t *apdu,
|
||||
uint8_t tag_number,
|
||||
BACNET_DEVICE_OBJECT_REFERENCE *value)
|
||||
{
|
||||
int len;
|
||||
int apdu_len = 0;
|
||||
|
||||
len = encode_opening_tag(&apdu[apdu_len], tag_number);
|
||||
apdu_len += len;
|
||||
if (value) {
|
||||
len = encode_opening_tag(&apdu[apdu_len], tag_number);
|
||||
apdu_len += len;
|
||||
|
||||
len = bacapp_encode_device_obj_ref(&apdu[apdu_len], value);
|
||||
apdu_len += len;
|
||||
len = bacapp_encode_device_obj_ref(&apdu[apdu_len], value);
|
||||
apdu_len += len;
|
||||
|
||||
len = encode_closing_tag(&apdu[apdu_len], tag_number);
|
||||
apdu_len += len;
|
||||
len = encode_closing_tag(&apdu[apdu_len], tag_number);
|
||||
apdu_len += len;
|
||||
}
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* BACnetDeviceObjectReference ::= SEQUENCE {
|
||||
device-identifier [0] BACnetObjectIdentifier OPTIONAL,
|
||||
object-identifier [1] BACnetObjectIdentifier
|
||||
}*/
|
||||
/**
|
||||
* Encode the device object reference.
|
||||
*
|
||||
* BACnetDeviceObjectReference ::= SEQUENCE {
|
||||
* device-identifier [0] BACnetObjectIdentifier OPTIONAL,
|
||||
* object-identifier [1] BACnetObjectIdentifier
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer used for encoding.
|
||||
* @param value Pointer to the device object reference,
|
||||
* used to encode.
|
||||
*
|
||||
* @return Bytes encoded or 0 on failure.
|
||||
*/
|
||||
int bacapp_encode_device_obj_ref(
|
||||
uint8_t *apdu, BACNET_DEVICE_OBJECT_REFERENCE *value)
|
||||
uint8_t * apdu,
|
||||
BACNET_DEVICE_OBJECT_REFERENCE * value)
|
||||
{
|
||||
int len;
|
||||
int apdu_len = 0;
|
||||
@@ -234,19 +306,31 @@ int bacapp_encode_device_obj_ref(
|
||||
apdu_len += len;
|
||||
}
|
||||
/* object-identifier [1] BACnetObjectIdentifier */
|
||||
len = encode_context_object_id(&apdu[apdu_len], 1,
|
||||
value->objectIdentifier.type, value->objectIdentifier.instance);
|
||||
len =
|
||||
encode_context_object_id(&apdu[apdu_len], 1,
|
||||
value->objectIdentifier.type, value->objectIdentifier.instance);
|
||||
apdu_len += len;
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/* BACnetDeviceObjectReference ::= SEQUENCE {
|
||||
device-identifier [0] BACnetObjectIdentifier OPTIONAL,
|
||||
object-identifier [1] BACnetObjectIdentifier
|
||||
}*/
|
||||
/**
|
||||
* Decode the device object reference.
|
||||
*
|
||||
* BACnetDeviceObjectReference ::= SEQUENCE {
|
||||
* device-identifier [0] BACnetObjectIdentifier OPTIONAL,
|
||||
* object-identifier [1] BACnetObjectIdentifier
|
||||
* }
|
||||
*
|
||||
* @param apdu Pointer to the buffer containing the data to decode.
|
||||
* @param value Pointer to the object object reference,
|
||||
* that shall be decoded.
|
||||
*
|
||||
* @return Bytes decoded or BACNET_STATUS_ERROR on failure.
|
||||
*/
|
||||
int bacapp_decode_device_obj_ref(
|
||||
uint8_t *apdu, BACNET_DEVICE_OBJECT_REFERENCE *value)
|
||||
uint8_t * apdu,
|
||||
BACNET_DEVICE_OBJECT_REFERENCE * value)
|
||||
{
|
||||
int len;
|
||||
int apdu_len = 0;
|
||||
@@ -254,11 +338,12 @@ int bacapp_decode_device_obj_ref(
|
||||
/* 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->deviceIdentifier.type,
|
||||
&value->deviceIdentifier.instance))) {
|
||||
return -1;
|
||||
len =
|
||||
decode_context_object_id(&apdu[apdu_len], 0,
|
||||
&value->deviceIdentifier.type,
|
||||
&value->deviceIdentifier.instance);
|
||||
if (len == BACNET_STATUS_ERROR) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
} else {
|
||||
@@ -266,19 +351,33 @@ int bacapp_decode_device_obj_ref(
|
||||
value->deviceIdentifier.instance = BACNET_NO_DEV_ID;
|
||||
}
|
||||
/* object-identifier [1] BACnetObjectIdentifier */
|
||||
if (-1 ==
|
||||
(len = decode_context_object_id(&apdu[apdu_len], 1,
|
||||
&value->objectIdentifier.type,
|
||||
&value->objectIdentifier.instance))) {
|
||||
return -1;
|
||||
len =
|
||||
decode_context_object_id(&apdu[apdu_len], 1,
|
||||
&value->objectIdentifier.type,
|
||||
&value->objectIdentifier.instance);
|
||||
if (len == BACNET_STATUS_ERROR) {
|
||||
return BACNET_STATUS_ERROR;
|
||||
}
|
||||
apdu_len += len;
|
||||
|
||||
return apdu_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode the context device object reference. Check for
|
||||
* an opening tag and a closing tag as well.
|
||||
*
|
||||
* @param apdu Pointer to the buffer containing the data to decode.
|
||||
* @param tag_number Tag number
|
||||
* @param value Pointer to the context device object reference,
|
||||
* that shall be decoded.
|
||||
*
|
||||
* @return Bytes decoded or BACNET_STATUS_ERROR on failure.
|
||||
*/
|
||||
int bacapp_decode_context_device_obj_ref(
|
||||
uint8_t *apdu, uint8_t tag_number, BACNET_DEVICE_OBJECT_REFERENCE *value)
|
||||
uint8_t * apdu,
|
||||
uint8_t tag_number,
|
||||
BACNET_DEVICE_OBJECT_REFERENCE * value)
|
||||
{
|
||||
int len = 0;
|
||||
int section_length;
|
||||
@@ -286,19 +385,18 @@ int bacapp_decode_context_device_obj_ref(
|
||||
if (decode_is_opening_tag_number(&apdu[len], tag_number)) {
|
||||
len++;
|
||||
section_length = bacapp_decode_device_obj_ref(&apdu[len], value);
|
||||
|
||||
if (section_length == -1) {
|
||||
len = -1;
|
||||
if (section_length == BACNET_STATUS_ERROR) {
|
||||
len = BACNET_STATUS_ERROR;
|
||||
} else {
|
||||
len += section_length;
|
||||
if (decode_is_closing_tag_number(&apdu[len], tag_number)) {
|
||||
len++;
|
||||
} else {
|
||||
len = -1;
|
||||
len = BACNET_STATUS_ERROR;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
len = -1;
|
||||
len = BACNET_STATUS_ERROR;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user